5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-09-19 00:59:37 +00:00

WIP on nodeinfo admin handler

This commit is contained in:
Arceliar 2021-05-16 15:27:51 -05:00
parent 058dec0cca
commit fad071ffe9
5 changed files with 48 additions and 14 deletions

2
go.mod
View File

@ -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

4
go.sum
View File

@ -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=

View File

@ -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
}

View File

@ -34,4 +34,5 @@ func (t *TunAdapter) SetupAdminHandlers(a *admin.AdminSocket) {
}
return res, nil
})
_ = a.AddHandler("getNodeInfo", []string{"key"}, t.nodeinfo.nodeInfoAdminHandler)
}

View File

@ -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
}
}