From b933083d20b3db4a3d6a8134efe312eb6ff3d8e2 Mon Sep 17 00:00:00 2001 From: Keuin Date: Sat, 9 Mar 2024 20:19:35 +0800 Subject: initial version --- protocol/handshake.go | 14 ++++++++++++++ protocol/messages.go | 29 +++++++++++++++++++++++++++++ protocol/observe.go | 14 ++++++++++++++ protocol/publish.go | 8 ++++++++ 4 files changed, 65 insertions(+) create mode 100644 protocol/handshake.go create mode 100644 protocol/messages.go create mode 100644 protocol/observe.go create mode 100644 protocol/publish.go (limited to 'protocol') diff --git a/protocol/handshake.go b/protocol/handshake.go new file mode 100644 index 0000000..02e3f0d --- /dev/null +++ b/protocol/handshake.go @@ -0,0 +1,14 @@ +package protocol + +const ( + TokenHeader = "X-Observatory-Token" + ObserverIDHeader = "X-Observatory-Observer-ID" +) + +// ServerPushInfo is the first server->client packet, applying configs to observers +type ServerPushInfo struct { + Version uint64 `json:"server_push_info_version"` + Targets []Target `json:"targets"` +} + +const CurrentServerPushInfoVersion uint64 = 240308 diff --git a/protocol/messages.go b/protocol/messages.go new file mode 100644 index 0000000..d8f5697 --- /dev/null +++ b/protocol/messages.go @@ -0,0 +1,29 @@ +package protocol + +import ( + "encoding/json" + "time" +) + +type TimeStamp time.Time + +func (t *TimeStamp) UnmarshalJSON(data []byte) error { + var ts int64 + err := json.Unmarshal(data, &ts) + if err != nil { + return err + } + *t = TimeStamp(time.UnixMilli(ts)) + return nil +} + +func (t TimeStamp) MarshalJSON() ([]byte, error) { + return json.Marshal(time.Time(t).UnixMilli()) +} + +type Observation struct { + Time TimeStamp `json:"time"` + Target Target `json:"target"` + Online bool `json:"online"` + Latency uint32 `json:"latency"` +} diff --git a/protocol/observe.go b/protocol/observe.go new file mode 100644 index 0000000..48d052a --- /dev/null +++ b/protocol/observe.go @@ -0,0 +1,14 @@ +package protocol + +import ( + "fmt" +) + +type Target struct { + Host string `json:"host"` + Port uint16 `json:"port"` +} + +func (t Target) String() string { + return fmt.Sprintf("%s:%d", t.Host, t.Port) +} diff --git a/protocol/publish.go b/protocol/publish.go new file mode 100644 index 0000000..13e6ee1 --- /dev/null +++ b/protocol/publish.go @@ -0,0 +1,8 @@ +package protocol + +type TargetStats map[string][]SourcedObservation + +type SourcedObservation struct { + Observation + Source string `json:"source,omitempty"` +} -- cgit v1.2.3