5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-11-24 04:31:35 +00:00

Fix setting nodeinfo (closes #954)

This commit is contained in:
Neil Alexander 2022-10-15 15:42:52 +01:00
parent 69632bacb5
commit ee21c56e43
2 changed files with 28 additions and 41 deletions

View File

@ -290,7 +290,10 @@ func run(args yggArgs, ctx context.Context, done chan struct{}) {
if err != nil { if err != nil {
panic(err) panic(err)
} }
options := []core.SetupOption{} options := []core.SetupOption{
core.NodeInfo(cfg.NodeInfo),
core.NodeInfoPrivacy(cfg.NodeInfoPrivacy),
}
for _, addr := range cfg.Listen { for _, addr := range cfg.Listen {
options = append(options, core.ListenAddress(addr)) options = append(options, core.ListenAddress(addr))
} }

View File

@ -6,7 +6,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"runtime" "runtime"
"strings"
"time" "time"
iwt "github.com/Arceliar/ironwood/types" iwt "github.com/Arceliar/ironwood/types"
@ -14,18 +13,15 @@ import (
"github.com/yggdrasil-network/yggdrasil-go/src/version" "github.com/yggdrasil-network/yggdrasil-go/src/version"
) )
// NodeInfoPayload represents a RequestNodeInfo response, in bytes.
type NodeInfoPayload []byte
type nodeinfo struct { type nodeinfo struct {
phony.Inbox phony.Inbox
proto *protoHandler proto *protoHandler
myNodeInfo NodeInfoPayload myNodeInfo json.RawMessage
callbacks map[keyArray]nodeinfoCallback callbacks map[keyArray]nodeinfoCallback
} }
type nodeinfoCallback struct { type nodeinfoCallback struct {
call func(nodeinfo NodeInfoPayload) call func(nodeinfo json.RawMessage)
created time.Time created time.Time
} }
@ -54,7 +50,7 @@ func (m *nodeinfo) _cleanup() {
}) })
} }
func (m *nodeinfo) _addCallback(sender keyArray, call func(nodeinfo NodeInfoPayload)) { func (m *nodeinfo) _addCallback(sender keyArray, call func(nodeinfo json.RawMessage)) {
m.callbacks[sender] = nodeinfoCallback{ m.callbacks[sender] = nodeinfoCallback{
created: time.Now(), created: time.Now(),
call: call, call: call,
@ -62,67 +58,55 @@ func (m *nodeinfo) _addCallback(sender keyArray, call func(nodeinfo NodeInfoPayl
} }
// Handles the callback, if there is one // Handles the callback, if there is one
func (m *nodeinfo) _callback(sender keyArray, nodeinfo NodeInfoPayload) { func (m *nodeinfo) _callback(sender keyArray, nodeinfo json.RawMessage) {
if callback, ok := m.callbacks[sender]; ok { if callback, ok := m.callbacks[sender]; ok {
callback.call(nodeinfo) callback.call(nodeinfo)
delete(m.callbacks, sender) delete(m.callbacks, sender)
} }
} }
func (m *nodeinfo) _getNodeInfo() NodeInfoPayload { func (m *nodeinfo) _getNodeInfo() json.RawMessage {
return m.myNodeInfo return m.myNodeInfo
} }
// Set the current node's nodeinfo // Set the current node's nodeinfo
func (m *nodeinfo) setNodeInfo(given interface{}, privacy bool) (err error) { func (m *nodeinfo) setNodeInfo(given map[string]interface{}, privacy bool) (err error) {
phony.Block(m, func() { phony.Block(m, func() {
err = m._setNodeInfo(given, privacy) err = m._setNodeInfo(given, privacy)
}) })
return return
} }
func (m *nodeinfo) _setNodeInfo(given interface{}, privacy bool) error { func (m *nodeinfo) _setNodeInfo(given map[string]interface{}, privacy bool) error {
defaults := map[string]interface{}{ newnodeinfo := make(map[string]interface{}, len(given))
"buildname": version.BuildName(), for k, v := range given {
"buildversion": version.BuildVersion(),
"buildplatform": runtime.GOOS,
"buildarch": runtime.GOARCH,
}
newnodeinfo := make(map[string]interface{})
if !privacy {
for k, v := range defaults {
newnodeinfo[k] = v newnodeinfo[k] = v
} }
} if !privacy {
if nodeinfomap, ok := given.(map[string]interface{}); ok { newnodeinfo["buildname"] = version.BuildName()
for key, value := range nodeinfomap { newnodeinfo["buildversion"] = version.BuildVersion()
if _, ok := defaults[key]; ok { newnodeinfo["buildplatform"] = runtime.GOOS
if strvalue, strok := value.(string); strok && strings.EqualFold(strvalue, "null") || value == nil { newnodeinfo["buildarch"] = runtime.GOARCH
delete(newnodeinfo, key)
}
continue
}
newnodeinfo[key] = value
}
} }
newjson, err := json.Marshal(newnodeinfo) newjson, err := json.Marshal(newnodeinfo)
if err == nil { switch {
if len(newjson) > 16384 { case err != nil:
return errors.New("NodeInfo exceeds max length of 16384 bytes") return fmt.Errorf("NodeInfo marshalling failed: %w", err)
} case len(newjson) > 16384:
return fmt.Errorf("NodeInfo exceeds max length of 16384 bytes")
default:
m.myNodeInfo = newjson m.myNodeInfo = newjson
return nil return nil
} }
return err
} }
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 json.RawMessage)) {
m.Act(from, func() { m.Act(from, func() {
m._sendReq(key, callback) m._sendReq(key, callback)
}) })
} }
func (m *nodeinfo) _sendReq(key keyArray, callback func(nodeinfo NodeInfoPayload)) { func (m *nodeinfo) _sendReq(key keyArray, callback func(nodeinfo json.RawMessage)) {
if callback != nil { if callback != nil {
m._addCallback(key, callback) m._addCallback(key, callback)
} }
@ -135,7 +119,7 @@ func (m *nodeinfo) handleReq(from phony.Actor, key keyArray) {
}) })
} }
func (m *nodeinfo) handleRes(from phony.Actor, key keyArray, info NodeInfoPayload) { func (m *nodeinfo) handleRes(from phony.Actor, key keyArray, info json.RawMessage) {
m.Act(from, func() { m.Act(from, func() {
m._callback(key, info) m._callback(key, info)
}) })
@ -169,7 +153,7 @@ func (m *nodeinfo) nodeInfoAdminHandler(in json.RawMessage) (interface{}, error)
} }
copy(key[:], kbs) copy(key[:], kbs)
ch := make(chan []byte, 1) ch := make(chan []byte, 1)
m.sendReq(nil, key, func(info NodeInfoPayload) { m.sendReq(nil, key, func(info json.RawMessage) {
ch <- info ch <- info
}) })
timer := time.NewTimer(6 * time.Second) timer := time.NewTimer(6 * time.Second)