2021-06-13 04:22:21 -05:00
|
|
|
package core
|
2021-05-16 13:52:52 -05:00
|
|
|
|
|
|
|
import (
|
2021-05-16 15:27:51 -05:00
|
|
|
"encoding/hex"
|
2021-05-16 13:52:52 -05:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2021-05-23 12:19:27 -05:00
|
|
|
"net"
|
2021-05-16 13:52:52 -05:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2021-05-23 12:19:27 -05:00
|
|
|
iwt "github.com/Arceliar/ironwood/types"
|
2021-05-16 13:52:52 -05:00
|
|
|
"github.com/Arceliar/phony"
|
2021-05-23 12:19:27 -05:00
|
|
|
|
2021-05-16 13:52:52 -05:00
|
|
|
//"github.com/yggdrasil-network/yggdrasil-go/src/crypto"
|
2021-05-23 12:19:27 -05:00
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/address"
|
2021-05-16 13:52:52 -05:00
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/version"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NodeInfoPayload represents a RequestNodeInfo response, in bytes.
|
|
|
|
type NodeInfoPayload []byte
|
|
|
|
|
|
|
|
type nodeinfo struct {
|
|
|
|
phony.Inbox
|
2021-05-23 11:58:52 -05:00
|
|
|
proto *protoHandler
|
2021-05-16 13:52:52 -05:00
|
|
|
myNodeInfo NodeInfoPayload
|
|
|
|
callbacks map[keyArray]nodeinfoCallback
|
|
|
|
}
|
|
|
|
|
|
|
|
type nodeinfoCallback struct {
|
2021-05-16 15:27:51 -05:00
|
|
|
call func(nodeinfo NodeInfoPayload)
|
2021-05-16 13:52:52 -05:00
|
|
|
created time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialises the nodeinfo cache/callback maps, and starts a goroutine to keep
|
|
|
|
// the cache/callback maps clean of stale entries
|
2021-05-23 11:58:52 -05:00
|
|
|
func (m *nodeinfo) init(proto *protoHandler) {
|
2021-05-16 13:52:52 -05:00
|
|
|
m.Act(nil, func() {
|
2021-05-23 11:58:52 -05:00
|
|
|
m._init(proto)
|
2021-05-16 13:52:52 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-05-23 11:58:52 -05:00
|
|
|
func (m *nodeinfo) _init(proto *protoHandler) {
|
|
|
|
m.proto = proto
|
2021-05-16 13:52:52 -05:00
|
|
|
m.callbacks = make(map[keyArray]nodeinfoCallback)
|
|
|
|
m._cleanup()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *nodeinfo) _cleanup() {
|
|
|
|
for boxPubKey, callback := range m.callbacks {
|
|
|
|
if time.Since(callback.created) > time.Minute {
|
|
|
|
delete(m.callbacks, boxPubKey)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
time.AfterFunc(time.Second*30, func() {
|
|
|
|
m.Act(nil, m._cleanup)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-05-16 15:27:51 -05:00
|
|
|
func (m *nodeinfo) _addCallback(sender keyArray, call func(nodeinfo NodeInfoPayload)) {
|
2021-05-16 13:52:52 -05:00
|
|
|
m.callbacks[sender] = nodeinfoCallback{
|
|
|
|
created: time.Now(),
|
|
|
|
call: call,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handles the callback, if there is one
|
|
|
|
func (m *nodeinfo) _callback(sender keyArray, nodeinfo NodeInfoPayload) {
|
|
|
|
if callback, ok := m.callbacks[sender]; ok {
|
2021-05-16 15:27:51 -05:00
|
|
|
callback.call(nodeinfo)
|
2021-05-16 13:52:52 -05:00
|
|
|
delete(m.callbacks, sender)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *nodeinfo) _getNodeInfo() NodeInfoPayload {
|
|
|
|
return m.myNodeInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the current node's nodeinfo
|
|
|
|
func (m *nodeinfo) setNodeInfo(given interface{}, privacy bool) (err error) {
|
|
|
|
phony.Block(m, func() {
|
|
|
|
err = m._setNodeInfo(given, privacy)
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *nodeinfo) _setNodeInfo(given interface{}, privacy bool) error {
|
|
|
|
defaults := map[string]interface{}{
|
|
|
|
"buildname": version.BuildName(),
|
|
|
|
"buildversion": version.BuildVersion(),
|
|
|
|
"buildplatform": runtime.GOOS,
|
|
|
|
"buildarch": runtime.GOARCH,
|
|
|
|
}
|
|
|
|
newnodeinfo := make(map[string]interface{})
|
|
|
|
if !privacy {
|
|
|
|
for k, v := range defaults {
|
|
|
|
newnodeinfo[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if nodeinfomap, ok := given.(map[string]interface{}); ok {
|
|
|
|
for key, value := range nodeinfomap {
|
|
|
|
if _, ok := defaults[key]; ok {
|
|
|
|
if strvalue, strok := value.(string); strok && strings.EqualFold(strvalue, "null") || value == nil {
|
|
|
|
delete(newnodeinfo, key)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
newnodeinfo[key] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
newjson, err := json.Marshal(newnodeinfo)
|
|
|
|
if err == nil {
|
|
|
|
if len(newjson) > 16384 {
|
|
|
|
return errors.New("NodeInfo exceeds max length of 16384 bytes")
|
|
|
|
}
|
|
|
|
m.myNodeInfo = newjson
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-05-16 15:27:51 -05:00
|
|
|
func (m *nodeinfo) sendReq(from phony.Actor, key keyArray, callback func(nodeinfo NodeInfoPayload)) {
|
2021-05-16 13:52:52 -05:00
|
|
|
m.Act(from, func() {
|
|
|
|
m._sendReq(key, callback)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-05-16 15:27:51 -05:00
|
|
|
func (m *nodeinfo) _sendReq(key keyArray, callback func(nodeinfo NodeInfoPayload)) {
|
2021-05-16 13:52:52 -05:00
|
|
|
if callback != nil {
|
|
|
|
m._addCallback(key, callback)
|
|
|
|
}
|
2021-06-13 04:22:21 -05:00
|
|
|
_, _ = m.proto.core.pc.WriteTo([]byte{typeSessionProto, typeProtoNodeInfoRequest}, iwt.Addr(key[:]))
|
2021-05-16 13:52:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *nodeinfo) handleReq(from phony.Actor, key keyArray) {
|
|
|
|
m.Act(from, func() {
|
|
|
|
m._sendRes(key)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *nodeinfo) handleRes(from phony.Actor, key keyArray, info NodeInfoPayload) {
|
|
|
|
m.Act(from, func() {
|
|
|
|
m._callback(key, info)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *nodeinfo) _sendRes(key keyArray) {
|
2021-05-23 11:58:52 -05:00
|
|
|
bs := append([]byte{typeSessionProto, typeProtoNodeInfoResponse}, m._getNodeInfo()...)
|
2021-06-13 04:22:21 -05:00
|
|
|
_, _ = m.proto.core.pc.WriteTo(bs, iwt.Addr(key[:]))
|
2021-05-16 13:52:52 -05:00
|
|
|
}
|
2021-05-16 15:27:51 -05:00
|
|
|
|
|
|
|
// Admin socket stuff
|
|
|
|
|
|
|
|
type GetNodeInfoRequest struct {
|
|
|
|
Key string `json:"key"`
|
|
|
|
}
|
2021-05-16 15:55:30 -05:00
|
|
|
type GetNodeInfoResponse map[string]interface{}
|
2021-05-16 15:27:51 -05:00
|
|
|
|
|
|
|
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:
|
2021-05-16 15:55:30 -05:00
|
|
|
var msg json.RawMessage
|
|
|
|
if err := msg.UnmarshalJSON(info); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-23 12:19:27 -05:00
|
|
|
ip := net.IP(address.AddrForKey(kbs)[:])
|
|
|
|
res := GetNodeInfoResponse{ip.String(): msg}
|
2021-05-16 15:27:51 -05:00
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
}
|