5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-11-09 23:20:28 +00:00

possibly fix admin socket getnodeinfo

This commit is contained in:
Arceliar 2021-05-16 15:55:30 -05:00
parent fad071ffe9
commit eb4a22724f
2 changed files with 7 additions and 27 deletions

View File

@ -23,7 +23,6 @@ type nodeinfo struct {
tun *TunAdapter tun *TunAdapter
myNodeInfo NodeInfoPayload myNodeInfo NodeInfoPayload
callbacks map[keyArray]nodeinfoCallback callbacks map[keyArray]nodeinfoCallback
cache map[keyArray]nodeinfoCached
} }
type nodeinfoCached struct { type nodeinfoCached struct {
@ -54,8 +53,6 @@ func (m *nodeinfo) init(tun *TunAdapter) {
func (m *nodeinfo) _init(tun *TunAdapter) { func (m *nodeinfo) _init(tun *TunAdapter) {
m.tun = tun m.tun = tun
m.callbacks = make(map[keyArray]nodeinfoCallback) m.callbacks = make(map[keyArray]nodeinfoCallback)
m.cache = make(map[keyArray]nodeinfoCached)
m._cleanup() m._cleanup()
} }
@ -65,11 +62,6 @@ func (m *nodeinfo) _cleanup() {
delete(m.callbacks, boxPubKey) delete(m.callbacks, boxPubKey)
} }
} }
for boxPubKey, cache := range m.cache {
if time.Since(cache.created) > time.Hour {
delete(m.cache, boxPubKey)
}
}
time.AfterFunc(time.Second*30, func() { time.AfterFunc(time.Second*30, func() {
m.Act(nil, m._cleanup) m.Act(nil, m._cleanup)
}) })
@ -152,22 +144,6 @@ func (m *nodeinfo) _setNodeInfo(given interface{}, privacy bool) error {
return err return err
} }
// Add nodeinfo into the cache for a node
func (m *nodeinfo) _addCachedNodeInfo(key keyArray, payload NodeInfoPayload) {
m.cache[key] = nodeinfoCached{
created: time.Now(),
payload: payload,
}
}
// Get a nodeinfo entry from the cache
func (m *nodeinfo) _getCachedNodeInfo(key keyArray) (NodeInfoPayload, error) {
if nodeinfo, ok := m.cache[key]; ok {
return nodeinfo.payload, nil
}
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.Act(from, func() {
m._sendReq(key, callback) m._sendReq(key, callback)
@ -190,7 +166,6 @@ 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 NodeInfoPayload) {
m.Act(from, func() { m.Act(from, func() {
m._callback(key, info) m._callback(key, info)
m._addCachedNodeInfo(key, info)
}) })
} }
@ -204,7 +179,7 @@ func (m *nodeinfo) _sendRes(key keyArray) {
type GetNodeInfoRequest struct { type GetNodeInfoRequest struct {
Key string `json:"key"` Key string `json:"key"`
} }
type GetNodeInfoResponse map[string]NodeInfoPayload type GetNodeInfoResponse map[string]interface{}
func (m *nodeinfo) nodeInfoAdminHandler(in json.RawMessage) (interface{}, error) { func (m *nodeinfo) nodeInfoAdminHandler(in json.RawMessage) (interface{}, error) {
var req GetNodeInfoRequest var req GetNodeInfoRequest
@ -228,7 +203,11 @@ func (m *nodeinfo) nodeInfoAdminHandler(in json.RawMessage) (interface{}, error)
case <-timer.C: case <-timer.C:
return nil, errors.New("timeout") return nil, errors.New("timeout")
case info := <-ch: case info := <-ch:
res := GetNodeInfoResponse{req.Key: info} var msg json.RawMessage
if err := msg.UnmarshalJSON(info); err != nil {
return nil, err
}
res := GetNodeInfoResponse{req.Key: msg}
return res, nil return res, nil
} }
} }

View File

@ -109,6 +109,7 @@ func (tun *TunAdapter) Init(core *yggdrasil.Core, config *config.NodeState, log
tun.config = config tun.config = config
tun.log = log tun.log = log
tun.nodeinfo.init(tun) tun.nodeinfo.init(tun)
tun.nodeinfo.setNodeInfo(config.Current.NodeInfo, config.Current.NodeInfoPrivacy)
if err := tun.core.SetOutOfBandHandler(tun.oobHandler); err != nil { if err := tun.core.SetOutOfBandHandler(tun.oobHandler); err != nil {
return fmt.Errorf("tun.core.SetOutOfBandHander: %w", err) return fmt.Errorf("tun.core.SetOutOfBandHander: %w", err)
} }