From fad071ffe98f3bd8f70d7688775e6700bf286410 Mon Sep 17 00:00:00 2001 From: Arceliar Date: Sun, 16 May 2021 15:27:51 -0500 Subject: [PATCH] WIP on nodeinfo admin handler --- go.mod | 2 +- go.sum | 4 ++-- src/admin/getself.go | 7 +++--- src/tuntap/admin.go | 1 + src/tuntap/nodeinfo.go | 48 ++++++++++++++++++++++++++++++++++++------ 5 files changed, 48 insertions(+), 14 deletions(-) diff --git a/go.mod b/go.mod index f830cd2..9c35842 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/yggdrasil-network/yggdrasil-go go 1.16 require ( - github.com/Arceliar/ironwood v0.0.0-20210515192645-e94fc534a8e0 + github.com/Arceliar/ironwood v0.0.0-20210516202558-11160a0940e1 github.com/Arceliar/phony v0.0.0-20210209235338-dde1a8dca979 github.com/cheggaaa/pb/v3 v3.0.6 github.com/fatih/color v1.10.0 // indirect diff --git a/go.sum b/go.sum index eda3ca6..e9fe524 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -github.com/Arceliar/ironwood v0.0.0-20210515192645-e94fc534a8e0 h1:ljxlQafrAuWzPhUDBVlWkshOlJDyKn9NXAPLJY5tAU0= -github.com/Arceliar/ironwood v0.0.0-20210515192645-e94fc534a8e0/go.mod h1:RP72rucOFm5udrnEzTmIWLRVGQiV/fSUAQXJ0RST/nk= +github.com/Arceliar/ironwood v0.0.0-20210516202558-11160a0940e1 h1:EVa/kL+8lgZmnAit/a0DIacKNZ/lj7NwjmO4B++t4PU= +github.com/Arceliar/ironwood v0.0.0-20210516202558-11160a0940e1/go.mod h1:RP72rucOFm5udrnEzTmIWLRVGQiV/fSUAQXJ0RST/nk= github.com/Arceliar/phony v0.0.0-20210209235338-dde1a8dca979 h1:WndgpSW13S32VLQ3ugUxx2EnnWmgba1kCqPkd4Gk1yQ= github.com/Arceliar/phony v0.0.0-20210209235338-dde1a8dca979/go.mod h1:6Lkn+/zJilRMsKmbmG1RPoamiArC6HS73xbwRyp3UyI= github.com/VividCortex/ewma v1.1.1 h1:MnEK4VOv6n0RSY4vtRe3h11qjxL3+t0B8yOL8iMXdcM= diff --git a/src/admin/getself.go b/src/admin/getself.go index bec084c..7effcc4 100644 --- a/src/admin/getself.go +++ b/src/admin/getself.go @@ -1,7 +1,6 @@ package admin import ( - "crypto/ed25519" "encoding/hex" "github.com/yggdrasil-network/yggdrasil-go/src/version" @@ -23,15 +22,15 @@ type SelfEntry struct { func (a *AdminSocket) getSelfHandler(req *GetSelfRequest, res *GetSelfResponse) error { res.Self = make(map[string]SelfEntry) - public := a.core.PrivateKey().Public().(ed25519.PublicKey) + self := a.core.GetSelf() addr := a.core.Address().String() snet := a.core.Subnet() res.Self[addr] = SelfEntry{ BuildName: version.BuildName(), BuildVersion: version.BuildVersion(), - PublicKey: hex.EncodeToString(public[:]), + PublicKey: hex.EncodeToString(self.Key[:]), Subnet: snet.String(), - // TODO: coords + Coords: self.Coords, } return nil } diff --git a/src/tuntap/admin.go b/src/tuntap/admin.go index 862a3c6..01cb56f 100644 --- a/src/tuntap/admin.go +++ b/src/tuntap/admin.go @@ -34,4 +34,5 @@ func (t *TunAdapter) SetupAdminHandlers(a *admin.AdminSocket) { } return res, nil }) + _ = a.AddHandler("getNodeInfo", []string{"key"}, t.nodeinfo.nodeInfoAdminHandler) } diff --git a/src/tuntap/nodeinfo.go b/src/tuntap/nodeinfo.go index a88916c..6e6a207 100644 --- a/src/tuntap/nodeinfo.go +++ b/src/tuntap/nodeinfo.go @@ -1,6 +1,7 @@ package tuntap import ( + "encoding/hex" "encoding/json" "errors" "runtime" @@ -23,7 +24,6 @@ type nodeinfo struct { myNodeInfo NodeInfoPayload callbacks map[keyArray]nodeinfoCallback cache map[keyArray]nodeinfoCached - //table *lookupTable } type nodeinfoCached struct { @@ -32,7 +32,7 @@ type nodeinfoCached struct { } type nodeinfoCallback struct { - call func(nodeinfo *NodeInfoPayload) + call func(nodeinfo NodeInfoPayload) created time.Time } @@ -76,13 +76,13 @@ func (m *nodeinfo) _cleanup() { } // Add a callback for a nodeinfo lookup -func (m *nodeinfo) addCallback(sender keyArray, call func(nodeinfo *NodeInfoPayload)) { +func (m *nodeinfo) addCallback(sender keyArray, call func(nodeinfo NodeInfoPayload)) { m.Act(nil, func() { m._addCallback(sender, call) }) } -func (m *nodeinfo) _addCallback(sender keyArray, call func(nodeinfo *NodeInfoPayload)) { +func (m *nodeinfo) _addCallback(sender keyArray, call func(nodeinfo NodeInfoPayload)) { m.callbacks[sender] = nodeinfoCallback{ created: time.Now(), call: call, @@ -92,7 +92,7 @@ func (m *nodeinfo) _addCallback(sender keyArray, call func(nodeinfo *NodeInfoPay // Handles the callback, if there is one func (m *nodeinfo) _callback(sender keyArray, nodeinfo NodeInfoPayload) { if callback, ok := m.callbacks[sender]; ok { - callback.call(&nodeinfo) + callback.call(nodeinfo) delete(m.callbacks, sender) } } @@ -168,13 +168,13 @@ func (m *nodeinfo) _getCachedNodeInfo(key keyArray) (NodeInfoPayload, error) { return NodeInfoPayload{}, errors.New("No cache entry found") } -func (m *nodeinfo) sendReq(from phony.Actor, key keyArray, callback func(nodeinfo *NodeInfoPayload)) { +func (m *nodeinfo) sendReq(from phony.Actor, key keyArray, callback func(nodeinfo NodeInfoPayload)) { m.Act(from, func() { m._sendReq(key, callback) }) } -func (m *nodeinfo) _sendReq(key keyArray, callback func(nodeinfo *NodeInfoPayload)) { +func (m *nodeinfo) _sendReq(key keyArray, callback func(nodeinfo NodeInfoPayload)) { if callback != nil { m._addCallback(key, callback) } @@ -198,3 +198,37 @@ func (m *nodeinfo) _sendRes(key keyArray) { bs := append([]byte{typeSessionNodeInfoResponse}, m._getNodeInfo()...) m.tun.core.WriteTo(bs, iwt.Addr(key[:])) } + +// Admin socket stuff + +type GetNodeInfoRequest struct { + Key string `json:"key"` +} +type GetNodeInfoResponse map[string]NodeInfoPayload + +func (m *nodeinfo) nodeInfoAdminHandler(in json.RawMessage) (interface{}, error) { + var req GetNodeInfoRequest + if err := json.Unmarshal(in, &req); err != nil { + return nil, err + } + var key keyArray + var kbs []byte + var err error + if kbs, err = hex.DecodeString(req.Key); err != nil { + return nil, err + } + copy(key[:], kbs) + ch := make(chan []byte, 1) + m.sendReq(nil, key, func(info NodeInfoPayload) { + ch <- info + }) + timer := time.NewTimer(6 * time.Second) + defer timer.Stop() + select { + case <-timer.C: + return nil, errors.New("timeout") + case info := <-ch: + res := GetNodeInfoResponse{req.Key: info} + return res, nil + } +}