2017-12-29 04:16:20 +00:00
|
|
|
package yggdrasil
|
|
|
|
|
2018-06-12 22:50:08 +00:00
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net"
|
2018-12-29 18:51:51 +00:00
|
|
|
"sync"
|
2019-01-14 17:21:15 +00:00
|
|
|
"time"
|
2018-06-12 22:50:08 +00:00
|
|
|
|
2018-12-15 00:30:36 +00:00
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/address"
|
2018-12-08 01:56:04 +00:00
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/config"
|
2018-12-15 00:30:36 +00:00
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/crypto"
|
2018-12-08 01:56:04 +00:00
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/defaults"
|
2018-06-12 22:50:08 +00:00
|
|
|
)
|
2017-12-29 04:16:20 +00:00
|
|
|
|
2018-12-07 22:20:11 +00:00
|
|
|
var buildName string
|
|
|
|
var buildVersion string
|
|
|
|
|
2018-12-29 18:51:51 +00:00
|
|
|
type module interface {
|
2018-12-29 19:14:26 +00:00
|
|
|
init(*Core, *config.NodeConfig) error
|
2018-12-29 18:51:51 +00:00
|
|
|
start() error
|
|
|
|
}
|
|
|
|
|
2018-05-27 21:13:37 +00:00
|
|
|
// The Core object represents the Yggdrasil node. You should create a Core
|
|
|
|
// object for each Yggdrasil node you plan to run.
|
2017-12-29 04:16:20 +00:00
|
|
|
type Core struct {
|
2018-01-04 22:37:51 +00:00
|
|
|
// This is the main data structure that holds everything else for a node
|
2018-12-29 18:51:51 +00:00
|
|
|
// We're going to keep our own copy of the provided config - that way we can
|
|
|
|
// guarantee that it will be covered by the mutex
|
|
|
|
config config.NodeConfig // Active config
|
|
|
|
configOld config.NodeConfig // Previous config
|
|
|
|
configMutex sync.RWMutex // Protects both config and configOld
|
2018-12-29 19:14:26 +00:00
|
|
|
boxPub crypto.BoxPubKey
|
|
|
|
boxPriv crypto.BoxPrivKey
|
|
|
|
sigPub crypto.SigPubKey
|
|
|
|
sigPriv crypto.SigPrivKey
|
2018-01-04 22:37:51 +00:00
|
|
|
switchTable switchTable
|
|
|
|
peers peers
|
|
|
|
sessions sessions
|
|
|
|
router router
|
|
|
|
dht dht
|
2018-01-21 00:17:15 +00:00
|
|
|
admin admin
|
2018-01-04 22:37:51 +00:00
|
|
|
searches searches
|
2018-05-23 10:13:53 +00:00
|
|
|
multicast multicast
|
2018-05-27 18:22:21 +00:00
|
|
|
tcp tcpInterface
|
2019-01-23 19:42:33 +00:00
|
|
|
link link
|
2018-12-29 19:14:26 +00:00
|
|
|
log *log.Logger
|
2017-12-29 04:16:20 +00:00
|
|
|
}
|
|
|
|
|
2018-12-29 19:14:26 +00:00
|
|
|
func (c *Core) init() error {
|
2018-01-04 22:37:51 +00:00
|
|
|
// TODO separate init and start functions
|
|
|
|
// Init sets up structs
|
|
|
|
// Start launches goroutines that depend on structs being set up
|
2018-01-26 23:30:51 +00:00
|
|
|
// This is pretty much required to completely avoid race conditions
|
2018-05-27 21:13:37 +00:00
|
|
|
if c.log == nil {
|
|
|
|
c.log = log.New(ioutil.Discard, "", 0)
|
|
|
|
}
|
2018-12-29 19:14:26 +00:00
|
|
|
|
|
|
|
boxPubHex, err := hex.DecodeString(c.config.EncryptionPublicKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
boxPrivHex, err := hex.DecodeString(c.config.EncryptionPrivateKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
sigPubHex, err := hex.DecodeString(c.config.SigningPublicKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
sigPrivHex, err := hex.DecodeString(c.config.SigningPrivateKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
copy(c.boxPub[:], boxPubHex)
|
|
|
|
copy(c.boxPriv[:], boxPrivHex)
|
|
|
|
copy(c.sigPub[:], sigPubHex)
|
|
|
|
copy(c.sigPriv[:], sigPrivHex)
|
|
|
|
|
|
|
|
c.admin.init(c)
|
2018-01-04 22:37:51 +00:00
|
|
|
c.searches.init(c)
|
|
|
|
c.dht.init(c)
|
|
|
|
c.sessions.init(c)
|
2018-05-23 10:13:53 +00:00
|
|
|
c.multicast.init(c)
|
2018-01-04 22:37:51 +00:00
|
|
|
c.peers.init(c)
|
|
|
|
c.router.init(c)
|
2018-12-29 19:14:26 +00:00
|
|
|
c.switchTable.init(c) // TODO move before peers? before router?
|
|
|
|
|
|
|
|
return nil
|
2017-12-29 04:16:20 +00:00
|
|
|
}
|
|
|
|
|
2019-01-14 17:21:15 +00:00
|
|
|
// If any static peers were provided in the configuration above then we should
|
|
|
|
// configure them. The loop ensures that disconnected peers will eventually
|
|
|
|
// be reconnected with.
|
|
|
|
func (c *Core) addPeerLoop() {
|
|
|
|
for {
|
|
|
|
// Get the peers from the config - these could change!
|
|
|
|
c.configMutex.RLock()
|
|
|
|
peers := c.config.Peers
|
|
|
|
interfacepeers := c.config.InterfacePeers
|
|
|
|
c.configMutex.RUnlock()
|
|
|
|
|
|
|
|
// Add peers from the Peers section
|
|
|
|
for _, peer := range peers {
|
|
|
|
c.AddPeer(peer, "")
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add peers from the InterfacePeers section
|
|
|
|
for intf, intfpeers := range interfacepeers {
|
|
|
|
for _, peer := range intfpeers {
|
|
|
|
c.AddPeer(peer, intf)
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sit for a while
|
|
|
|
time.Sleep(time.Minute)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-29 18:51:51 +00:00
|
|
|
// UpdateConfig updates the configuration in Core and then signals the
|
|
|
|
// various module goroutines to reconfigure themselves if needed
|
|
|
|
func (c *Core) UpdateConfig(config *config.NodeConfig) {
|
|
|
|
c.configMutex.Lock()
|
|
|
|
c.configOld = c.config
|
|
|
|
c.config = *config
|
|
|
|
c.configMutex.Unlock()
|
|
|
|
|
2018-12-30 12:04:42 +00:00
|
|
|
components := []chan chan error{
|
|
|
|
c.admin.reconfigure,
|
2019-01-14 14:25:52 +00:00
|
|
|
c.searches.reconfigure,
|
|
|
|
c.dht.reconfigure,
|
|
|
|
c.sessions.reconfigure,
|
|
|
|
c.peers.reconfigure,
|
|
|
|
c.router.reconfigure,
|
|
|
|
c.router.tun.reconfigure,
|
2019-01-14 18:34:15 +00:00
|
|
|
c.router.cryptokey.reconfigure,
|
2019-01-14 14:25:52 +00:00
|
|
|
c.switchTable.reconfigure,
|
2018-12-30 15:21:09 +00:00
|
|
|
c.tcp.reconfigure,
|
2018-12-30 12:04:42 +00:00
|
|
|
c.multicast.reconfigure,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, component := range components {
|
|
|
|
response := make(chan error)
|
|
|
|
component <- response
|
|
|
|
if err := <-response; err != nil {
|
|
|
|
c.log.Println(err)
|
|
|
|
}
|
|
|
|
}
|
2018-12-29 18:51:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetBuildName gets the current build name. This is usually injected if built
|
|
|
|
// from git, or returns "unknown" otherwise.
|
2018-12-07 22:20:11 +00:00
|
|
|
func GetBuildName() string {
|
|
|
|
if buildName == "" {
|
|
|
|
return "unknown"
|
|
|
|
}
|
|
|
|
return buildName
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the current build version. This is usually injected if built from git,
|
|
|
|
// or returns "unknown" otherwise.
|
|
|
|
func GetBuildVersion() string {
|
|
|
|
if buildVersion == "" {
|
|
|
|
return "unknown"
|
|
|
|
}
|
|
|
|
return buildVersion
|
|
|
|
}
|
|
|
|
|
2018-05-27 21:13:37 +00:00
|
|
|
// Starts up Yggdrasil using the provided NodeConfig, and outputs debug logging
|
|
|
|
// through the provided log.Logger. The started stack will include TCP and UDP
|
|
|
|
// sockets, a multicast discovery socket, an admin socket, router, switch and
|
|
|
|
// DHT node.
|
|
|
|
func (c *Core) Start(nc *config.NodeConfig, log *log.Logger) error {
|
|
|
|
c.log = log
|
2018-12-07 22:24:01 +00:00
|
|
|
|
2018-12-08 11:01:05 +00:00
|
|
|
if name := GetBuildName(); name != "unknown" {
|
|
|
|
c.log.Println("Build name:", name)
|
2018-12-07 22:24:01 +00:00
|
|
|
}
|
2018-12-08 11:01:05 +00:00
|
|
|
if version := GetBuildVersion(); version != "unknown" {
|
|
|
|
c.log.Println("Build version:", version)
|
2018-12-07 22:24:01 +00:00
|
|
|
}
|
|
|
|
|
2018-05-27 21:13:37 +00:00
|
|
|
c.log.Println("Starting up...")
|
|
|
|
|
2018-12-29 18:51:51 +00:00
|
|
|
c.configMutex.Lock()
|
|
|
|
c.config = *nc
|
|
|
|
c.configOld = c.config
|
|
|
|
c.configMutex.Unlock()
|
|
|
|
|
2018-12-29 19:14:26 +00:00
|
|
|
c.init()
|
2018-05-27 21:13:37 +00:00
|
|
|
|
2019-01-14 14:01:38 +00:00
|
|
|
if err := c.tcp.init(c); err != nil {
|
2018-05-27 21:13:37 +00:00
|
|
|
c.log.Println("Failed to start TCP interface")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-01-19 12:19:24 +00:00
|
|
|
if err := c.link.init(c); err != nil {
|
|
|
|
c.log.Println("Failed to start link interfaces")
|
2019-01-19 00:42:53 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-01-04 17:14:40 +00:00
|
|
|
|
2018-12-02 23:24:54 +00:00
|
|
|
if nc.SwitchOptions.MaxTotalQueueSize >= SwitchQueueTotalMinSize {
|
|
|
|
c.switchTable.queueTotalMaxSize = nc.SwitchOptions.MaxTotalQueueSize
|
|
|
|
}
|
|
|
|
|
2018-06-24 00:08:32 +00:00
|
|
|
if err := c.switchTable.start(); err != nil {
|
|
|
|
c.log.Println("Failed to start switch")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-05-27 21:13:37 +00:00
|
|
|
if err := c.router.start(); err != nil {
|
|
|
|
c.log.Println("Failed to start router")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.admin.start(); err != nil {
|
|
|
|
c.log.Println("Failed to start admin socket")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.multicast.start(); err != nil {
|
|
|
|
c.log.Println("Failed to start multicast interface")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-01-14 14:25:52 +00:00
|
|
|
if err := c.router.tun.start(); err != nil {
|
2018-05-27 21:35:30 +00:00
|
|
|
c.log.Println("Failed to start TUN/TAP")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-01-14 17:21:15 +00:00
|
|
|
go c.addPeerLoop()
|
|
|
|
|
2018-05-27 21:13:37 +00:00
|
|
|
c.log.Println("Startup complete")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stops the Yggdrasil node.
|
|
|
|
func (c *Core) Stop() {
|
|
|
|
c.log.Println("Stopping...")
|
2018-12-14 17:35:02 +00:00
|
|
|
c.router.tun.close()
|
2018-07-07 19:04:11 +00:00
|
|
|
c.admin.close()
|
2018-05-27 21:13:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Generates a new encryption keypair. The encryption keys are used to
|
|
|
|
// encrypt traffic and to derive the IPv6 address/subnet of the node.
|
2018-12-15 02:49:18 +00:00
|
|
|
func (c *Core) NewEncryptionKeys() (*crypto.BoxPubKey, *crypto.BoxPrivKey) {
|
|
|
|
return crypto.NewBoxKeys()
|
2018-05-27 21:13:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Generates a new signing keypair. The signing keys are used to derive the
|
|
|
|
// structure of the spanning tree.
|
2018-12-15 02:49:18 +00:00
|
|
|
func (c *Core) NewSigningKeys() (*crypto.SigPubKey, *crypto.SigPrivKey) {
|
|
|
|
return crypto.NewSigKeys()
|
2018-05-27 21:13:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Gets the node ID.
|
2018-12-15 02:49:18 +00:00
|
|
|
func (c *Core) GetNodeID() *crypto.NodeID {
|
|
|
|
return crypto.GetNodeID(&c.boxPub)
|
2017-12-29 04:16:20 +00:00
|
|
|
}
|
|
|
|
|
2018-05-27 21:13:37 +00:00
|
|
|
// Gets the tree ID.
|
2018-12-15 02:49:18 +00:00
|
|
|
func (c *Core) GetTreeID() *crypto.TreeID {
|
|
|
|
return crypto.GetTreeID(&c.sigPub)
|
2017-12-29 04:16:20 +00:00
|
|
|
}
|
2018-05-21 15:15:31 +00:00
|
|
|
|
2018-05-27 21:13:37 +00:00
|
|
|
// Gets the IPv6 address of the Yggdrasil node. This is always a /128.
|
|
|
|
func (c *Core) GetAddress() *net.IP {
|
2018-12-15 02:49:18 +00:00
|
|
|
address := net.IP(address.AddrForNodeID(c.GetNodeID())[:])
|
2018-05-27 21:13:37 +00:00
|
|
|
return &address
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gets the routed IPv6 subnet of the Yggdrasil node. This is always a /64.
|
|
|
|
func (c *Core) GetSubnet() *net.IPNet {
|
2018-12-15 02:49:18 +00:00
|
|
|
subnet := address.SubnetForNodeID(c.GetNodeID())[:]
|
2018-05-27 21:13:37 +00:00
|
|
|
subnet = append(subnet, 0, 0, 0, 0, 0, 0, 0, 0)
|
2018-06-02 20:21:05 +00:00
|
|
|
return &net.IPNet{IP: subnet, Mask: net.CIDRMask(64, 128)}
|
2018-05-27 21:13:37 +00:00
|
|
|
}
|
|
|
|
|
2018-12-15 22:37:11 +00:00
|
|
|
// Gets the nodeinfo.
|
|
|
|
func (c *Core) GetNodeInfo() nodeinfoPayload {
|
2019-01-14 19:05:16 +00:00
|
|
|
return c.router.nodeinfo.getNodeInfo()
|
2018-12-12 22:40:49 +00:00
|
|
|
}
|
|
|
|
|
2018-12-15 22:37:11 +00:00
|
|
|
// Sets the nodeinfo.
|
2018-12-21 09:56:34 +00:00
|
|
|
func (c *Core) SetNodeInfo(nodeinfo interface{}, nodeinfoprivacy bool) {
|
2019-01-14 19:05:16 +00:00
|
|
|
c.router.nodeinfo.setNodeInfo(nodeinfo, nodeinfoprivacy)
|
2018-12-12 22:40:49 +00:00
|
|
|
}
|
|
|
|
|
2018-05-27 21:13:37 +00:00
|
|
|
// Sets the output logger of the Yggdrasil node after startup. This may be
|
|
|
|
// useful if you want to redirect the output later.
|
|
|
|
func (c *Core) SetLogger(log *log.Logger) {
|
|
|
|
c.log = log
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adds a peer. This should be specified in the peer URI format, i.e.
|
|
|
|
// tcp://a.b.c.d:e, udp://a.b.c.d:e, socks://a.b.c.d:e/f.g.h.i:j
|
2018-09-25 15:55:57 +00:00
|
|
|
func (c *Core) AddPeer(addr string, sintf string) error {
|
|
|
|
return c.admin.addPeer(addr, sintf)
|
2018-05-27 21:13:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Adds an allowed public key. This allow peerings to be restricted only to
|
|
|
|
// keys that you have selected.
|
|
|
|
func (c *Core) AddAllowedEncryptionPublicKey(boxStr string) error {
|
|
|
|
return c.admin.addAllowedEncryptionPublicKey(boxStr)
|
|
|
|
}
|
|
|
|
|
2018-07-07 11:08:52 +00:00
|
|
|
// Gets the default admin listen address for your platform.
|
|
|
|
func (c *Core) GetAdminDefaultListen() string {
|
|
|
|
return defaults.GetDefaults().DefaultAdminListen
|
|
|
|
}
|
|
|
|
|
2018-05-27 21:13:37 +00:00
|
|
|
// Gets the default TUN/TAP interface name for your platform.
|
|
|
|
func (c *Core) GetTUNDefaultIfName() string {
|
2018-07-07 11:08:52 +00:00
|
|
|
return defaults.GetDefaults().DefaultIfName
|
2018-05-27 21:13:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Gets the default TUN/TAP interface MTU for your platform. This can be as high
|
|
|
|
// as 65535, depending on platform, but is never lower than 1280.
|
|
|
|
func (c *Core) GetTUNDefaultIfMTU() int {
|
2018-07-07 11:08:52 +00:00
|
|
|
return defaults.GetDefaults().DefaultIfMTU
|
2018-05-27 21:13:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Gets the maximum supported TUN/TAP interface MTU for your platform. This
|
|
|
|
// can be as high as 65535, depending on platform, but is never lower than 1280.
|
|
|
|
func (c *Core) GetTUNMaximumIfMTU() int {
|
2018-07-07 11:08:52 +00:00
|
|
|
return defaults.GetDefaults().MaximumIfMTU
|
2018-05-27 21:13:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Gets the default TUN/TAP interface mode for your platform.
|
|
|
|
func (c *Core) GetTUNDefaultIfTAPMode() bool {
|
2018-07-07 11:08:52 +00:00
|
|
|
return defaults.GetDefaults().DefaultIfTAPMode
|
2018-05-27 21:13:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Gets the current TUN/TAP interface name.
|
|
|
|
func (c *Core) GetTUNIfName() string {
|
2018-12-14 17:35:02 +00:00
|
|
|
return c.router.tun.iface.Name()
|
2018-05-21 15:15:31 +00:00
|
|
|
}
|
|
|
|
|
2018-05-27 21:13:37 +00:00
|
|
|
// Gets the current TUN/TAP interface MTU.
|
|
|
|
func (c *Core) GetTUNIfMTU() int {
|
2018-12-14 17:35:02 +00:00
|
|
|
return c.router.tun.mtu
|
2018-05-21 15:15:31 +00:00
|
|
|
}
|