ev/app/peerfinder/peer.go

339 lines
7.3 KiB
Go
Raw Normal View History

2022-08-23 21:24:13 -06:00
package peerfinder
import (
"bytes"
"encoding/json"
2022-10-25 20:37:59 -06:00
"fmt"
2022-11-20 10:28:50 -07:00
"net"
2022-08-23 21:24:13 -06:00
"net/netip"
"strconv"
2022-11-20 10:28:50 -07:00
"strings"
2022-08-23 21:24:13 -06:00
"time"
2022-11-20 10:28:50 -07:00
"github.com/tj/go-semver"
"github.com/oklog/ulid/v2"
2022-08-23 21:24:13 -06:00
"github.com/sour-is/ev/pkg/es/event"
)
2022-11-20 10:28:50 -07:00
type Request struct {
2022-10-30 09:18:08 -06:00
event.AggregateRoot
2022-11-20 10:28:50 -07:00
RequestID string `json:"req_id"`
RequestIP string `json:"req_ip"`
Hidden bool `json:"hide,omitempty"`
Created time.Time `json:"req_created"`
2022-10-30 09:18:08 -06:00
Responses []Response `json:"responses"`
}
var _ event.Aggregate = (*Request)(nil)
2022-11-20 10:28:50 -07:00
2022-10-30 09:18:08 -06:00
func (a *Request) ApplyEvent(lis ...event.Event) {
for _, e := range lis {
2022-11-20 10:28:50 -07:00
switch e := e.(type) {
2022-10-30 09:18:08 -06:00
case *RequestSubmitted:
2022-11-20 10:28:50 -07:00
a.RequestID = e.eventMeta.EventID.String()
2022-10-30 09:18:08 -06:00
a.RequestIP = e.RequestIP
a.Hidden = e.Hidden
2022-11-20 10:28:50 -07:00
a.Created = ulid.Time(e.EventMeta().EventID.Time())
2022-10-30 09:18:08 -06:00
case *ResultSubmitted:
2022-11-20 10:28:50 -07:00
a.Responses = append(a.Responses, Response{
PeerID: e.PeerID,
ScriptVersion: e.PeerVersion,
Latency: e.Latency,
Jitter: e.Jitter,
MinRTT: e.MinRTT,
MaxRTT: e.MaxRTT,
Sent: e.Sent,
Received: e.Received,
Unreachable: e.Unreachable,
Created: ulid.Time(e.EventMeta().EventID.Time()),
})
2022-10-30 09:18:08 -06:00
}
}
}
2022-11-20 10:28:50 -07:00
type Time time.Time
func (t *Time) UnmarshalJSON(b []byte) error {
time, err := time.Parse(`"2006-01-02 15:04:05"`, string(b))
*t = Time(time)
return err
}
func (t *Time) MarshalJSON() ([]byte, error) {
if t == nil {
return nil, nil
}
i := *t
return time.Time(i).MarshalJSON()
}
type ipFamily string
const (
ipFamilyV4 ipFamily = "IPv4"
ipFamilyV6 ipFamily = "IPv6"
ipFamilyBoth ipFamily = "both"
ipFamilyNone ipFamily = "none"
)
func (t *ipFamily) UnmarshalJSON(b []byte) error {
i, err := strconv.Atoi(strings.Trim(string(b), `"`))
switch i {
case 1:
*t = ipFamilyV4
case 2:
*t = ipFamilyV6
case 3:
*t = ipFamilyBoth
default:
*t = ipFamilyNone
}
return err
}
type peerType []string
func (t *peerType) UnmarshalJSON(b []byte) error {
*t = strings.Split(strings.Trim(string(b), `"`), ",")
return nil
}
type Peer struct {
ID string `json:"peer_id,omitempty"`
Owner string `json:"peer_owner"`
Nick string `json:"peer_nick"`
Name string `json:"peer_name"`
Country string `json:"peer_country"`
Note string `json:"peer_note"`
Family ipFamily `json:"peer_family"`
Type peerType `json:"peer_type"`
Created Time `json:"peer_created"`
}
func (p *Peer) CanSupport(ip string) bool {
addr := net.ParseIP(ip)
if addr == nil {
return false
}
if !addr.IsGlobalUnicast() {
return false
}
switch p.Family {
case ipFamilyV4:
return addr.To4() != nil
case ipFamilyV6:
return addr.To16() != nil
case ipFamilyNone:
return false
}
return true
}
2022-10-30 09:18:08 -06:00
type Response struct {
2022-11-20 10:28:50 -07:00
Peer *Peer `json:"peer"`
PeerID string `json:"-"`
ScriptVersion string `json:"peer_scriptver"`
Latency float64 `json:"res_latency"`
Jitter float64 `json:"res_jitter,omitempty"`
MaxRTT float64 `json:"res_maxrtt,omitempty"`
MinRTT float64 `json:"res_minrtt,omitempty"`
Sent int `json:"res_sent,omitempty"`
Received int `json:"res_recv,omitempty"`
Unreachable bool `json:"unreachable,omitempty"`
Created time.Time `json:"res_created"`
2022-10-30 09:18:08 -06:00
}
type RequestSubmitted struct {
2022-08-23 21:24:13 -06:00
eventMeta event.Meta
RequestIP string `json:"req_ip"`
Hidden bool `json:"hide,omitempty"`
}
2022-10-30 09:18:08 -06:00
func (r *RequestSubmitted) StreamID() string {
2022-08-23 21:24:13 -06:00
return r.EventMeta().GetEventID()
}
2022-10-30 09:18:08 -06:00
func (r *RequestSubmitted) RequestID() string {
2022-08-23 21:24:13 -06:00
return r.EventMeta().GetEventID()
}
2022-10-30 09:18:08 -06:00
func (r *RequestSubmitted) Created() time.Time {
2022-08-23 21:24:13 -06:00
return r.EventMeta().Created()
}
2022-10-30 09:18:08 -06:00
func (r *RequestSubmitted) CreatedString() string {
2022-08-23 21:24:13 -06:00
return r.Created().Format("2006-01-02 15:04:05")
}
2022-10-30 09:18:08 -06:00
func (r *RequestSubmitted) Family() int {
2022-08-23 21:24:13 -06:00
if r == nil {
return 0
}
ip, err := netip.ParseAddr(r.RequestIP)
switch {
case err != nil:
return 0
case ip.Is4():
return 1
default:
return 2
}
}
2022-10-30 09:18:08 -06:00
var _ event.Event = (*RequestSubmitted)(nil)
2022-08-23 21:24:13 -06:00
2022-10-30 09:18:08 -06:00
func (e *RequestSubmitted) EventMeta() event.Meta {
2022-08-23 21:24:13 -06:00
if e == nil {
return event.Meta{}
}
return e.eventMeta
}
2022-10-30 09:18:08 -06:00
func (e *RequestSubmitted) SetEventMeta(m event.Meta) {
2022-08-23 21:24:13 -06:00
if e != nil {
e.eventMeta = m
}
}
2022-10-30 09:18:08 -06:00
func (e *RequestSubmitted) MarshalBinary() (text []byte, err error) {
2022-08-23 21:24:13 -06:00
return json.Marshal(e)
}
2022-10-30 09:18:08 -06:00
func (e *RequestSubmitted) UnmarshalBinary(b []byte) error {
2022-08-23 21:24:13 -06:00
return json.Unmarshal(b, e)
}
2022-10-30 09:18:08 -06:00
func (e *RequestSubmitted) MarshalEnviron() ([]byte, error) {
2022-08-23 21:24:13 -06:00
if e == nil {
return nil, nil
}
var b bytes.Buffer
b.WriteString("REQ_ID=")
b.WriteString(e.RequestID())
b.WriteRune('\n')
b.WriteString("REQ_IP=")
b.WriteString(e.RequestIP)
b.WriteRune('\n')
b.WriteString("REQ_FAMILY=")
if family := e.Family(); family > 0 {
b.WriteString(strconv.Itoa(family))
}
b.WriteRune('\n')
b.WriteString("REQ_CREATED=")
b.WriteString(e.CreatedString())
b.WriteRune('\n')
return b.Bytes(), nil
}
2022-10-30 09:18:08 -06:00
type ResultSubmitted struct {
2022-08-23 21:24:13 -06:00
eventMeta event.Meta
RequestID string `json:"req_id"`
PeerID string `json:"peer_id"`
PeerVersion string `json:"peer_version"`
Latency float64 `json:"latency,omitempty"`
2022-11-20 10:28:50 -07:00
Jitter float64 `json:"jitter,omitempty"`
MaxRTT float64 `json:"maxrtt,omitempty"`
MinRTT float64 `json:"minrtt,omitempty"`
Sent int `json:"res_sent,omitempty"`
Received int `json:"res_recv,omitempty"`
Unreachable bool `json:"unreachable,omitempty"`
2022-08-23 21:24:13 -06:00
}
2022-10-30 09:18:08 -06:00
func (r *ResultSubmitted) Created() time.Time {
2022-08-23 21:24:13 -06:00
return r.eventMeta.Created()
}
2022-10-30 09:18:08 -06:00
var _ event.Event = (*ResultSubmitted)(nil)
2022-08-23 21:24:13 -06:00
2022-10-30 09:18:08 -06:00
func (e *ResultSubmitted) EventMeta() event.Meta {
2022-08-23 21:24:13 -06:00
if e == nil {
return event.Meta{}
}
return e.eventMeta
}
2022-10-30 09:18:08 -06:00
func (e *ResultSubmitted) SetEventMeta(m event.Meta) {
2022-08-23 21:24:13 -06:00
if e != nil {
e.eventMeta = m
}
}
2022-10-30 09:18:08 -06:00
func (e *ResultSubmitted) MarshalBinary() (text []byte, err error) {
2022-08-23 21:24:13 -06:00
return json.Marshal(e)
}
2022-10-30 09:18:08 -06:00
func (e *ResultSubmitted) UnmarshalBinary(b []byte) error {
2022-08-23 21:24:13 -06:00
return json.Unmarshal(b, e)
}
2022-10-30 09:18:08 -06:00
func (e *ResultSubmitted) String() string {
2022-10-25 20:37:59 -06:00
return fmt.Sprintf("id: %s\npeer: %s\nversion: %s\nlatency: %0.4f", e.RequestID, e.PeerID, e.PeerVersion, e.Latency)
}
2022-08-23 21:24:13 -06:00
type Info struct {
ScriptVersion string `json:"script_version"`
event.AggregateRoot
}
var _ event.Aggregate = (*Info)(nil)
func (a *Info) ApplyEvent(lis ...event.Event) {
for _, e := range lis {
switch e := e.(type) {
case *VersionChanged:
a.ScriptVersion = e.ScriptVersion
}
}
}
func (a *Info) MarshalEnviron() ([]byte, error) {
var b bytes.Buffer
b.WriteString("SCRIPT_VERSION=")
b.WriteString(a.ScriptVersion)
b.WriteRune('\n')
return b.Bytes(), nil
}
2022-11-20 10:28:50 -07:00
func (a *Info) OnUpsert() error {
2022-08-23 21:24:13 -06:00
if a.StreamVersion() == 0 {
event.Raise(a, &VersionChanged{ScriptVersion: initVersion})
}
2022-11-20 10:28:50 -07:00
current, _ := semver.Parse(initVersion)
previous, _ := semver.Parse(a.ScriptVersion)
if current.Compare(previous) > 0 {
event.Raise(a, &VersionChanged{ScriptVersion: initVersion})
}
2022-08-23 21:24:13 -06:00
return nil
}
type VersionChanged struct {
ScriptVersion string `json:"script_version"`
eventMeta event.Meta
}
var _ event.Event = (*VersionChanged)(nil)
func (e *VersionChanged) EventMeta() event.Meta {
if e == nil {
return event.Meta{}
}
return e.eventMeta
}
func (e *VersionChanged) SetEventMeta(m event.Meta) {
if e != nil {
e.eventMeta = m
}
}
func (e *VersionChanged) MarshalBinary() (text []byte, err error) {
return json.Marshal(e)
}
func (e *VersionChanged) UnmarshalBinary(b []byte) error {
return json.Unmarshal(b, e)
}