5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-11-14 22:00:28 +00:00
yggdrasil-go/src/yggdrasil/nodeinfo.go

209 lines
5.2 KiB
Go
Raw Normal View History

2018-12-15 22:37:11 +00:00
package yggdrasil
import (
"encoding/json"
"errors"
"runtime"
"strings"
2018-12-15 22:37:11 +00:00
"time"
2020-01-05 22:15:52 +00:00
"github.com/Arceliar/phony"
"github.com/yggdrasil-network/yggdrasil-go/src/crypto"
"github.com/yggdrasil-network/yggdrasil-go/src/version"
2018-12-15 22:37:11 +00:00
)
type nodeinfo struct {
2020-01-05 22:15:52 +00:00
phony.Inbox
core *Core
myNodeInfo NodeInfoPayload
callbacks map[crypto.BoxPubKey]nodeinfoCallback
cache map[crypto.BoxPubKey]nodeinfoCached
2018-12-15 22:37:11 +00:00
}
type nodeinfoCached struct {
payload NodeInfoPayload
2018-12-15 22:37:11 +00:00
created time.Time
}
type nodeinfoCallback struct {
call func(nodeinfo *NodeInfoPayload)
2018-12-15 22:37:11 +00:00
created time.Time
}
// Represents a session nodeinfo packet.
type nodeinfoReqRes struct {
SendPermPub crypto.BoxPubKey // Sender's permanent key
SendCoords []byte // Sender's coords
2018-12-15 22:37:11 +00:00
IsResponse bool
NodeInfo NodeInfoPayload
2018-12-15 22:37:11 +00:00
}
// Initialises the nodeinfo cache/callback maps, and starts a goroutine to keep
// the cache/callback maps clean of stale entries
func (m *nodeinfo) init(core *Core) {
m.Act(nil, func() {
2020-01-05 22:15:52 +00:00
m._init(core)
})
}
func (m *nodeinfo) _init(core *Core) {
2018-12-15 22:37:11 +00:00
m.core = core
m.callbacks = make(map[crypto.BoxPubKey]nodeinfoCallback)
m.cache = make(map[crypto.BoxPubKey]nodeinfoCached)
2018-12-15 22:37:11 +00:00
2020-01-05 22:15:52 +00:00
m._cleanup()
}
func (m *nodeinfo) _cleanup() {
for boxPubKey, callback := range m.callbacks {
if time.Since(callback.created) > time.Minute {
delete(m.callbacks, boxPubKey)
}
2020-01-05 22:15:52 +00:00
}
for boxPubKey, cache := range m.cache {
if time.Since(cache.created) > time.Hour {
delete(m.cache, boxPubKey)
2018-12-15 22:37:11 +00:00
}
}
2020-01-05 22:15:52 +00:00
time.AfterFunc(time.Second*30, func() {
m.Act(nil, m._cleanup)
2020-01-05 22:15:52 +00:00
})
2018-12-15 22:37:11 +00:00
}
// Add a callback for a nodeinfo lookup
func (m *nodeinfo) addCallback(sender crypto.BoxPubKey, call func(nodeinfo *NodeInfoPayload)) {
m.Act(nil, func() {
2020-01-05 22:15:52 +00:00
m._addCallback(sender, call)
})
}
func (m *nodeinfo) _addCallback(sender crypto.BoxPubKey, call func(nodeinfo *NodeInfoPayload)) {
2018-12-15 22:37:11 +00:00
m.callbacks[sender] = nodeinfoCallback{
created: time.Now(),
call: call,
}
}
// Handles the callback, if there is one
2020-01-05 22:15:52 +00:00
func (m *nodeinfo) _callback(sender crypto.BoxPubKey, nodeinfo NodeInfoPayload) {
2018-12-15 22:37:11 +00:00
if callback, ok := m.callbacks[sender]; ok {
callback.call(&nodeinfo)
delete(m.callbacks, sender)
}
}
// Get the current node's nodeinfo
2020-01-05 22:15:52 +00:00
func (m *nodeinfo) getNodeInfo() (p NodeInfoPayload) {
phony.Block(m, func() {
p = m._getNodeInfo()
})
return
}
func (m *nodeinfo) _getNodeInfo() NodeInfoPayload {
2018-12-15 22:37:11 +00:00
return m.myNodeInfo
}
// Set the current node's nodeinfo
2020-01-05 22:15:52 +00:00
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(),
2018-12-15 22:37:11 +00:00
"buildplatform": runtime.GOOS,
"buildarch": runtime.GOARCH,
}
newnodeinfo := make(map[string]interface{})
if !privacy {
for k, v := range defaults {
newnodeinfo[k] = v
}
}
2018-12-15 22:37:11 +00:00
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)
}
2018-12-15 22:37:11 +00:00
continue
}
newnodeinfo[key] = value
2018-12-15 22:37:11 +00:00
}
}
if newjson, err := json.Marshal(newnodeinfo); err == nil {
if len(newjson) > 16384 {
return errors.New("NodeInfo exceeds max length of 16384 bytes")
}
m.myNodeInfo = newjson
return nil
} else {
return err
}
}
// Add nodeinfo into the cache for a node
2020-01-05 22:15:52 +00:00
func (m *nodeinfo) _addCachedNodeInfo(key crypto.BoxPubKey, payload NodeInfoPayload) {
2018-12-15 22:37:11 +00:00
m.cache[key] = nodeinfoCached{
created: time.Now(),
payload: payload,
}
}
// Get a nodeinfo entry from the cache
2020-01-05 22:15:52 +00:00
func (m *nodeinfo) _getCachedNodeInfo(key crypto.BoxPubKey) (NodeInfoPayload, error) {
2018-12-15 22:37:11 +00:00
if nodeinfo, ok := m.cache[key]; ok {
return nodeinfo.payload, nil
}
return NodeInfoPayload{}, errors.New("No cache entry found")
2018-12-15 22:37:11 +00:00
}
// Handles a nodeinfo request/response - called from the router
func (m *nodeinfo) handleNodeInfo(from phony.Actor, nodeinfo *nodeinfoReqRes) {
m.Act(from, func() {
2020-01-05 22:15:52 +00:00
m._handleNodeInfo(nodeinfo)
})
}
func (m *nodeinfo) _handleNodeInfo(nodeinfo *nodeinfoReqRes) {
2018-12-15 22:37:11 +00:00
if nodeinfo.IsResponse {
2020-01-05 22:15:52 +00:00
m._callback(nodeinfo.SendPermPub, nodeinfo.NodeInfo)
m._addCachedNodeInfo(nodeinfo.SendPermPub, nodeinfo.NodeInfo)
2018-12-15 22:37:11 +00:00
} else {
2020-01-05 22:15:52 +00:00
m._sendNodeInfo(nodeinfo.SendPermPub, nodeinfo.SendCoords, true)
2018-12-15 22:37:11 +00:00
}
}
// Send nodeinfo request or response - called from the router
func (m *nodeinfo) sendNodeInfo(key crypto.BoxPubKey, coords []byte, isResponse bool) {
m.Act(nil, func() {
2020-01-05 22:15:52 +00:00
m._sendNodeInfo(key, coords, isResponse)
})
}
func (m *nodeinfo) _sendNodeInfo(key crypto.BoxPubKey, coords []byte, isResponse bool) {
loc := m.core.switchTable.getLocator()
2018-12-15 22:37:11 +00:00
nodeinfo := nodeinfoReqRes{
SendCoords: loc.getCoords(),
2018-12-15 22:37:11 +00:00
IsResponse: isResponse,
2020-01-05 22:15:52 +00:00
NodeInfo: m._getNodeInfo(),
2018-12-15 22:37:11 +00:00
}
bs := nodeinfo.encode()
shared := m.core.router.sessions.getSharedKey(&m.core.boxPriv, &key)
payload, nonce := crypto.BoxSeal(shared, bs, nil)
2018-12-15 22:37:11 +00:00
p := wire_protoTrafficPacket{
Coords: coords,
ToKey: key,
FromKey: m.core.boxPub,
Nonce: *nonce,
Payload: payload,
}
packet := p.encode()
m.core.router.out(packet)
}