2018-12-15 22:37:11 +00:00
|
|
|
package yggdrasil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"runtime"
|
2018-12-21 10:04:32 +00:00
|
|
|
"strings"
|
2018-12-15 22:37:11 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
2018-12-15 18:11:02 -06:00
|
|
|
|
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/crypto"
|
2018-12-15 22:37:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type nodeinfo struct {
|
|
|
|
core *Core
|
2019-07-28 11:30:24 +01:00
|
|
|
myNodeInfo NodeInfoPayload
|
2018-12-15 22:37:11 +00:00
|
|
|
myNodeInfoMutex sync.RWMutex
|
2018-12-15 18:11:02 -06:00
|
|
|
callbacks map[crypto.BoxPubKey]nodeinfoCallback
|
2018-12-15 22:37:11 +00:00
|
|
|
callbacksMutex sync.Mutex
|
2018-12-15 18:11:02 -06:00
|
|
|
cache map[crypto.BoxPubKey]nodeinfoCached
|
2018-12-15 22:37:11 +00:00
|
|
|
cacheMutex sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
type nodeinfoCached struct {
|
2019-07-28 11:30:24 +01:00
|
|
|
payload NodeInfoPayload
|
2018-12-15 22:37:11 +00:00
|
|
|
created time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
type nodeinfoCallback struct {
|
2019-07-28 11:30:24 +01:00
|
|
|
call func(nodeinfo *NodeInfoPayload)
|
2018-12-15 22:37:11 +00:00
|
|
|
created time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
// Represents a session nodeinfo packet.
|
|
|
|
type nodeinfoReqRes struct {
|
2018-12-15 18:11:02 -06:00
|
|
|
SendPermPub crypto.BoxPubKey // Sender's permanent key
|
|
|
|
SendCoords []byte // Sender's coords
|
2018-12-15 22:37:11 +00:00
|
|
|
IsResponse bool
|
2019-07-28 11:30:24 +01:00
|
|
|
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.core = core
|
2018-12-15 18:11:02 -06:00
|
|
|
m.callbacks = make(map[crypto.BoxPubKey]nodeinfoCallback)
|
|
|
|
m.cache = make(map[crypto.BoxPubKey]nodeinfoCached)
|
2018-12-15 22:37:11 +00:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
m.callbacksMutex.Lock()
|
|
|
|
for boxPubKey, callback := range m.callbacks {
|
|
|
|
if time.Since(callback.created) > time.Minute {
|
|
|
|
delete(m.callbacks, boxPubKey)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m.callbacksMutex.Unlock()
|
|
|
|
m.cacheMutex.Lock()
|
|
|
|
for boxPubKey, cache := range m.cache {
|
|
|
|
if time.Since(cache.created) > time.Hour {
|
|
|
|
delete(m.cache, boxPubKey)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m.cacheMutex.Unlock()
|
|
|
|
time.Sleep(time.Second * 30)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a callback for a nodeinfo lookup
|
2019-07-28 11:30:24 +01:00
|
|
|
func (m *nodeinfo) addCallback(sender crypto.BoxPubKey, call func(nodeinfo *NodeInfoPayload)) {
|
2018-12-15 22:37:11 +00:00
|
|
|
m.callbacksMutex.Lock()
|
|
|
|
defer m.callbacksMutex.Unlock()
|
|
|
|
m.callbacks[sender] = nodeinfoCallback{
|
|
|
|
created: time.Now(),
|
|
|
|
call: call,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handles the callback, if there is one
|
2019-07-28 11:30:24 +01:00
|
|
|
func (m *nodeinfo) callback(sender crypto.BoxPubKey, nodeinfo NodeInfoPayload) {
|
2018-12-15 22:37:11 +00:00
|
|
|
m.callbacksMutex.Lock()
|
|
|
|
defer m.callbacksMutex.Unlock()
|
|
|
|
if callback, ok := m.callbacks[sender]; ok {
|
|
|
|
callback.call(&nodeinfo)
|
|
|
|
delete(m.callbacks, sender)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the current node's nodeinfo
|
2019-07-28 11:30:24 +01:00
|
|
|
func (m *nodeinfo) getNodeInfo() NodeInfoPayload {
|
2018-12-15 22:37:11 +00:00
|
|
|
m.myNodeInfoMutex.RLock()
|
|
|
|
defer m.myNodeInfoMutex.RUnlock()
|
|
|
|
return m.myNodeInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the current node's nodeinfo
|
2018-12-21 09:56:34 +00:00
|
|
|
func (m *nodeinfo) setNodeInfo(given interface{}, privacy bool) error {
|
2018-12-15 22:37:11 +00:00
|
|
|
m.myNodeInfoMutex.Lock()
|
|
|
|
defer m.myNodeInfoMutex.Unlock()
|
2018-12-21 09:56:34 +00:00
|
|
|
defaults := map[string]interface{}{
|
2019-03-29 18:24:57 +00:00
|
|
|
"buildname": BuildName(),
|
|
|
|
"buildversion": BuildVersion(),
|
2018-12-15 22:37:11 +00:00
|
|
|
"buildplatform": runtime.GOOS,
|
|
|
|
"buildarch": runtime.GOARCH,
|
|
|
|
}
|
2018-12-21 09:56:34 +00:00
|
|
|
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 {
|
2018-12-21 09:56:34 +00:00
|
|
|
if _, ok := defaults[key]; ok {
|
2018-12-21 10:04:32 +00:00
|
|
|
if strvalue, strok := value.(string); strok && strings.EqualFold(strvalue, "null") || value == nil {
|
2018-12-20 23:49:15 +00:00
|
|
|
delete(newnodeinfo, key)
|
|
|
|
}
|
2018-12-15 22:37:11 +00:00
|
|
|
continue
|
|
|
|
}
|
2018-12-21 09:56:34 +00:00
|
|
|
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
|
2019-07-28 11:30:24 +01:00
|
|
|
func (m *nodeinfo) addCachedNodeInfo(key crypto.BoxPubKey, payload NodeInfoPayload) {
|
2018-12-15 22:37:11 +00:00
|
|
|
m.cacheMutex.Lock()
|
|
|
|
defer m.cacheMutex.Unlock()
|
|
|
|
m.cache[key] = nodeinfoCached{
|
|
|
|
created: time.Now(),
|
|
|
|
payload: payload,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get a nodeinfo entry from the cache
|
2019-07-28 11:30:24 +01:00
|
|
|
func (m *nodeinfo) getCachedNodeInfo(key crypto.BoxPubKey) (NodeInfoPayload, error) {
|
2018-12-15 22:37:11 +00:00
|
|
|
m.cacheMutex.RLock()
|
|
|
|
defer m.cacheMutex.RUnlock()
|
|
|
|
if nodeinfo, ok := m.cache[key]; ok {
|
|
|
|
return nodeinfo.payload, nil
|
|
|
|
}
|
2019-07-28 11:30:24 +01:00
|
|
|
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(nodeinfo *nodeinfoReqRes) {
|
|
|
|
if nodeinfo.IsResponse {
|
|
|
|
m.callback(nodeinfo.SendPermPub, nodeinfo.NodeInfo)
|
|
|
|
m.addCachedNodeInfo(nodeinfo.SendPermPub, nodeinfo.NodeInfo)
|
|
|
|
} else {
|
|
|
|
m.sendNodeInfo(nodeinfo.SendPermPub, nodeinfo.SendCoords, true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send nodeinfo request or response - called from the router
|
2018-12-15 18:11:02 -06:00
|
|
|
func (m *nodeinfo) sendNodeInfo(key crypto.BoxPubKey, coords []byte, isResponse bool) {
|
2018-12-15 22:37:11 +00:00
|
|
|
table := m.core.switchTable.table.Load().(lookupTable)
|
|
|
|
nodeinfo := nodeinfoReqRes{
|
|
|
|
SendCoords: table.self.getCoords(),
|
|
|
|
IsResponse: isResponse,
|
2019-01-14 19:05:16 +00:00
|
|
|
NodeInfo: m.getNodeInfo(),
|
2018-12-15 22:37:11 +00:00
|
|
|
}
|
|
|
|
bs := nodeinfo.encode()
|
|
|
|
shared := m.core.sessions.getSharedKey(&m.core.boxPriv, &key)
|
2018-12-15 18:11:02 -06:00
|
|
|
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)
|
|
|
|
}
|