-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
89 lines (77 loc) · 1.68 KB
/
client.go
File metadata and controls
89 lines (77 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"log/slog"
"net/http"
"os"
"time"
)
func main() {
/*
e := events[0]
data, err := json.Marshal(e)
if err != nil {
fmt.Println("ERROR:", err)
return
}
r := bytes.NewReader(data)
*/
r, w, err := os.Pipe()
if err != nil {
fmt.Println("ERROR:", err)
return
}
go func() {
defer w.Close()
enc := json.NewEncoder(w)
for _, e := range events {
if err := enc.Encode(e); err != nil {
slog.Error("encode", "error", err)
return
}
}
}()
const url = "http://localhost:8080/events"
// BUG: No timeout
// resp, err := http.Post(url, "application/json", r)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, r)
if err != nil {
fmt.Println("ERROR:", err)
return
}
req.Header.Set("Authorization", "Bearer s3cr3t")
resp, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Println("ERROR:", err)
return
}
defer func() {
io.Copy(io.Discard, resp.Body)
resp.Body.Close()
}()
if resp.StatusCode != http.StatusOK {
fmt.Println("ERROR:", resp.Status)
return
}
io.Copy(os.Stdout, resp.Body)
}
var events = []Event{
{asTime("2025-05-21T14:31:49Z"), "elliot", "read", "file:///etc/passwd"},
{asTime("2025-05-21T14:42:32Z"), "elliot", "read", "file:///etc/shadow"},
{asTime("2025-05-21T14:43:07Z"), "elliot", "read", "file:///root/.ssh/config"},
}
type Event struct {
Time time.Time `json:"time"`
Login string `json:"login"`
Action string `json:"action"`
URI string `json:"uri"`
}
func asTime(s string) time.Time {
t, _ := time.Parse(s, time.RFC3339)
return t
}