|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "net" |
| 8 | + |
| 9 | + "github.com/fatih/color" |
| 10 | +) |
| 11 | + |
| 12 | +func main() { |
| 13 | + s, err := net.ResolveUDPAddr("udp4", ":31000") |
| 14 | + if err != nil { |
| 15 | + panic(err) |
| 16 | + } |
| 17 | + |
| 18 | + connection, err := net.ListenUDP("udp4", s) |
| 19 | + if err != nil { |
| 20 | + panic(err) |
| 21 | + } |
| 22 | + defer connection.Close() |
| 23 | + |
| 24 | + for { |
| 25 | + res, err := Read(connection) |
| 26 | + if err != nil { |
| 27 | + _ = fmt.Errorf("%v: %w", "unknown response", err) |
| 28 | + continue |
| 29 | + } |
| 30 | + if res.Type == "ApiCallAttempt" { // omit Verdicts |
| 31 | + msg := fmt.Sprintf("%v %-50.50v %v\n", res.HTTPStatusCode, fmt.Sprintf("%s:%s", res.Service, res.API), res.Fqdn) |
| 32 | + if res.HTTPStatusCode >= 400 { |
| 33 | + color.Red(msg) |
| 34 | + } else { |
| 35 | + color.Green(msg) |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +type Response struct { |
| 42 | + // Attempt |
| 43 | + Version int `json:"Version"` |
| 44 | + ClientID string `json:"ClientId"` |
| 45 | + Type string `json:"Type"` |
| 46 | + Service string `json:"Service"` |
| 47 | + API string `json:"Api"` |
| 48 | + Timestamp int64 `json:"Timestamp"` |
| 49 | + AttemptLatency int `json:"AttemptLatency"` |
| 50 | + Fqdn string `json:"Fqdn"` |
| 51 | + UserAgent string `json:"UserAgent"` |
| 52 | + AccessKey string `json:"AccessKey"` |
| 53 | + Region string `json:"Region"` |
| 54 | + SessionToken string `json:"SessionToken"` |
| 55 | + HTTPStatusCode int `json:"HttpStatusCode"` |
| 56 | + XAmznRequestID string `json:"XAmznRequestId"` |
| 57 | + |
| 58 | + // Verdict |
| 59 | + AttemptCount int `json:"AttemptCount"` |
| 60 | + FinalHTTPStatusCode int `json:"FinalHttpStatusCode"` |
| 61 | + Latency int `json:"Latency"` |
| 62 | + MaxRetriesExceeded int `json:"MaxRetriesExceeded"` |
| 63 | +} |
| 64 | + |
| 65 | +func Read(conn *net.UDPConn) (*Response, error) { |
| 66 | + b := make([]byte, 1024) |
| 67 | + oob := make([]byte, 40) |
| 68 | + |
| 69 | + //nolint:dogsled |
| 70 | + _, _, _, _, err := conn.ReadMsgUDP(b, oob) |
| 71 | + if err != nil { |
| 72 | + return nil, err |
| 73 | + } |
| 74 | + |
| 75 | + // Remove NULL characters |
| 76 | + b = bytes.Trim(b, "\x00") |
| 77 | + ret := &Response{} |
| 78 | + |
| 79 | + if err := json.Unmarshal(b, ret); err != nil { |
| 80 | + return nil, err |
| 81 | + } |
| 82 | + |
| 83 | + return ret, err |
| 84 | +} |
0 commit comments