5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-11-22 15:20:30 +00:00

Use metadata from config file

This commit is contained in:
Neil Alexander 2018-12-15 11:38:51 +00:00
parent d9884a5cac
commit 92bb63f196
No known key found for this signature in database
GPG Key ID: A02A2019A2BB0944
3 changed files with 42 additions and 31 deletions

View File

@ -19,7 +19,7 @@ type NodeConfig struct {
SessionFirewall SessionFirewall `comment:"The session firewall controls who can send/receive network traffic\nto/from. This is useful if you want to protect this node without\nresorting to using a real firewall. This does not affect traffic\nbeing routed via this node to somewhere else. Rules are prioritised as\nfollows: blacklist, whitelist, always allow outgoing, direct, remote."`
TunnelRouting TunnelRouting `comment:"Allow tunneling non-Yggdrasil traffic over Yggdrasil. This effectively\nallows you to use Yggdrasil to route to, or to bridge other networks,\nsimilar to a VPN tunnel. Tunnelling works between any two nodes and\ndoes not require them to be directly peered."`
SwitchOptions SwitchOptions `comment:"Advanced options for tuning the switch. Normally you will not need\nto edit these options."`
Metadata interface{} `comment:"Optional node metadata. Entirely optional but visible to all\npeers and nodes with open sessions."`
Metadata map[string]interface{} `comment:"Optional node metadata. This must be a { \"key\": \"value\", ... } map\nor set as null. This is entirely optional but, if set, is visible\nto the whole network on request."`
//Net NetConfig `comment:"Extended options for connecting to peers over other networks."`
}

View File

@ -2,13 +2,11 @@ package yggdrasil
import (
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net"
"regexp"
"runtime"
"github.com/yggdrasil-network/yggdrasil-go/src/config"
"github.com/yggdrasil-network/yggdrasil-go/src/defaults"
@ -126,15 +124,7 @@ func (c *Core) Start(nc *config.NodeConfig, log *log.Logger) error {
c.admin.init(c, nc.AdminListen)
c.metadata.init(c)
m := map[string]string{
"buildname": GetBuildName(),
"buildversion": GetBuildVersion(),
"buildplatform": runtime.GOOS,
"buildarch": runtime.GOARCH,
}
if json, err := json.Marshal(m); err == nil {
c.metadata.setMetadata(json)
}
c.metadata.setMetadata(nc.Metadata)
if err := c.tcp.init(c, nc.Listen, nc.ReadTimeout); err != nil {
c.log.Println("Failed to start TCP interface")

View File

@ -1,7 +1,9 @@
package yggdrasil
import (
"encoding/json"
"errors"
"runtime"
"sync"
"time"
)
@ -84,10 +86,29 @@ func (m *metadata) getMetadata() metadataPayload {
}
// Set the current node's metadata
func (m *metadata) setMetadata(meta metadataPayload) {
func (m *metadata) setMetadata(given interface{}) error {
m.myMetadataMutex.Lock()
defer m.myMetadataMutex.Unlock()
m.myMetadata = meta
newmeta := map[string]interface{}{
"buildname": GetBuildName(),
"buildversion": GetBuildVersion(),
"buildplatform": runtime.GOOS,
"buildarch": runtime.GOARCH,
}
if metamap, ok := given.(map[string]interface{}); ok {
for key, value := range metamap {
if _, ok := newmeta[key]; ok {
continue
}
newmeta[key] = value
}
}
if newjson, err := json.Marshal(newmeta); err == nil {
m.myMetadata = newjson
return nil
} else {
return err
}
}
// Add metadata into the cache for a node