Skip to content

Commit ee21c56

Browse files
committed
Fix setting nodeinfo (closes #954)
1 parent 69632ba commit ee21c56

File tree

2 files changed

+28
-41
lines changed

2 files changed

+28
-41
lines changed

cmd/yggdrasil/main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,10 @@ func run(args yggArgs, ctx context.Context, done chan struct{}) {
290290
if err != nil {
291291
panic(err)
292292
}
293-
options := []core.SetupOption{}
293+
options := []core.SetupOption{
294+
core.NodeInfo(cfg.NodeInfo),
295+
core.NodeInfoPrivacy(cfg.NodeInfoPrivacy),
296+
}
294297
for _, addr := range cfg.Listen {
295298
options = append(options, core.ListenAddress(addr))
296299
}

src/core/nodeinfo.go

Lines changed: 24 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,22 @@ import (
66
"errors"
77
"fmt"
88
"runtime"
9-
"strings"
109
"time"
1110

1211
iwt "github.com/Arceliar/ironwood/types"
1312
"github.com/Arceliar/phony"
1413
"github.com/yggdrasil-network/yggdrasil-go/src/version"
1514
)
1615

17-
// NodeInfoPayload represents a RequestNodeInfo response, in bytes.
18-
type NodeInfoPayload []byte
19-
2016
type nodeinfo struct {
2117
phony.Inbox
2218
proto *protoHandler
23-
myNodeInfo NodeInfoPayload
19+
myNodeInfo json.RawMessage
2420
callbacks map[keyArray]nodeinfoCallback
2521
}
2622

2723
type nodeinfoCallback struct {
28-
call func(nodeinfo NodeInfoPayload)
24+
call func(nodeinfo json.RawMessage)
2925
created time.Time
3026
}
3127

@@ -54,75 +50,63 @@ func (m *nodeinfo) _cleanup() {
5450
})
5551
}
5652

57-
func (m *nodeinfo) _addCallback(sender keyArray, call func(nodeinfo NodeInfoPayload)) {
53+
func (m *nodeinfo) _addCallback(sender keyArray, call func(nodeinfo json.RawMessage)) {
5854
m.callbacks[sender] = nodeinfoCallback{
5955
created: time.Now(),
6056
call: call,
6157
}
6258
}
6359

6460
// Handles the callback, if there is one
65-
func (m *nodeinfo) _callback(sender keyArray, nodeinfo NodeInfoPayload) {
61+
func (m *nodeinfo) _callback(sender keyArray, nodeinfo json.RawMessage) {
6662
if callback, ok := m.callbacks[sender]; ok {
6763
callback.call(nodeinfo)
6864
delete(m.callbacks, sender)
6965
}
7066
}
7167

72-
func (m *nodeinfo) _getNodeInfo() NodeInfoPayload {
68+
func (m *nodeinfo) _getNodeInfo() json.RawMessage {
7369
return m.myNodeInfo
7470
}
7571

7672
// Set the current node's nodeinfo
77-
func (m *nodeinfo) setNodeInfo(given interface{}, privacy bool) (err error) {
73+
func (m *nodeinfo) setNodeInfo(given map[string]interface{}, privacy bool) (err error) {
7874
phony.Block(m, func() {
7975
err = m._setNodeInfo(given, privacy)
8076
})
8177
return
8278
}
8379

84-
func (m *nodeinfo) _setNodeInfo(given interface{}, privacy bool) error {
85-
defaults := map[string]interface{}{
86-
"buildname": version.BuildName(),
87-
"buildversion": version.BuildVersion(),
88-
"buildplatform": runtime.GOOS,
89-
"buildarch": runtime.GOARCH,
80+
func (m *nodeinfo) _setNodeInfo(given map[string]interface{}, privacy bool) error {
81+
newnodeinfo := make(map[string]interface{}, len(given))
82+
for k, v := range given {
83+
newnodeinfo[k] = v
9084
}
91-
newnodeinfo := make(map[string]interface{})
9285
if !privacy {
93-
for k, v := range defaults {
94-
newnodeinfo[k] = v
95-
}
96-
}
97-
if nodeinfomap, ok := given.(map[string]interface{}); ok {
98-
for key, value := range nodeinfomap {
99-
if _, ok := defaults[key]; ok {
100-
if strvalue, strok := value.(string); strok && strings.EqualFold(strvalue, "null") || value == nil {
101-
delete(newnodeinfo, key)
102-
}
103-
continue
104-
}
105-
newnodeinfo[key] = value
106-
}
86+
newnodeinfo["buildname"] = version.BuildName()
87+
newnodeinfo["buildversion"] = version.BuildVersion()
88+
newnodeinfo["buildplatform"] = runtime.GOOS
89+
newnodeinfo["buildarch"] = runtime.GOARCH
10790
}
10891
newjson, err := json.Marshal(newnodeinfo)
109-
if err == nil {
110-
if len(newjson) > 16384 {
111-
return errors.New("NodeInfo exceeds max length of 16384 bytes")
112-
}
92+
switch {
93+
case err != nil:
94+
return fmt.Errorf("NodeInfo marshalling failed: %w", err)
95+
case len(newjson) > 16384:
96+
return fmt.Errorf("NodeInfo exceeds max length of 16384 bytes")
97+
default:
11398
m.myNodeInfo = newjson
11499
return nil
115100
}
116-
return err
117101
}
118102

119-
func (m *nodeinfo) sendReq(from phony.Actor, key keyArray, callback func(nodeinfo NodeInfoPayload)) {
103+
func (m *nodeinfo) sendReq(from phony.Actor, key keyArray, callback func(nodeinfo json.RawMessage)) {
120104
m.Act(from, func() {
121105
m._sendReq(key, callback)
122106
})
123107
}
124108

125-
func (m *nodeinfo) _sendReq(key keyArray, callback func(nodeinfo NodeInfoPayload)) {
109+
func (m *nodeinfo) _sendReq(key keyArray, callback func(nodeinfo json.RawMessage)) {
126110
if callback != nil {
127111
m._addCallback(key, callback)
128112
}
@@ -135,7 +119,7 @@ func (m *nodeinfo) handleReq(from phony.Actor, key keyArray) {
135119
})
136120
}
137121

138-
func (m *nodeinfo) handleRes(from phony.Actor, key keyArray, info NodeInfoPayload) {
122+
func (m *nodeinfo) handleRes(from phony.Actor, key keyArray, info json.RawMessage) {
139123
m.Act(from, func() {
140124
m._callback(key, info)
141125
})
@@ -169,7 +153,7 @@ func (m *nodeinfo) nodeInfoAdminHandler(in json.RawMessage) (interface{}, error)
169153
}
170154
copy(key[:], kbs)
171155
ch := make(chan []byte, 1)
172-
m.sendReq(nil, key, func(info NodeInfoPayload) {
156+
m.sendReq(nil, key, func(info json.RawMessage) {
173157
ch <- info
174158
})
175159
timer := time.NewTimer(6 * time.Second)

0 commit comments

Comments
 (0)