yggdrasil-go/src/yggdrasil/core.go

369 lines
9.8 KiB
Go
Raw Normal View History

2017-12-29 07:16:20 +03:00
package yggdrasil
2018-06-13 01:50:08 +03:00
import (
"encoding/hex"
"io/ioutil"
"net"
"time"
2018-06-13 01:50:08 +03:00
"github.com/gologme/log"
"github.com/yggdrasil-network/yggdrasil-go/src/address"
2018-12-08 04:56:04 +03:00
"github.com/yggdrasil-network/yggdrasil-go/src/config"
"github.com/yggdrasil-network/yggdrasil-go/src/crypto"
2018-12-08 04:56:04 +03:00
"github.com/yggdrasil-network/yggdrasil-go/src/defaults"
2018-06-13 01:50:08 +03:00
)
2017-12-29 07:16:20 +03:00
var buildName string
var buildVersion string
type module interface {
2018-12-29 22:14:26 +03:00
init(*Core, *config.NodeConfig) error
start() error
}
2018-05-28 00:13:37 +03: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 07:16:20 +03:00
type Core struct {
2018-01-05 01:37:51 +03:00
// This is the main data structure that holds everything else for a node
// 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.NodeState // Config
2018-12-29 22:14:26 +03:00
boxPub crypto.BoxPubKey
boxPriv crypto.BoxPrivKey
sigPub crypto.SigPubKey
sigPriv crypto.SigPrivKey
2018-01-05 01:37:51 +03:00
switchTable switchTable
peers peers
sessions sessions
router router
dht dht
2018-01-21 03:17:15 +03:00
admin admin
2018-01-05 01:37:51 +03:00
searches searches
//multicast multicast
link link
log *log.Logger
2017-12-29 07:16:20 +03:00
}
2018-12-29 22:14:26 +03:00
func (c *Core) init() error {
2018-01-05 01:37:51 +03:00
// TODO separate init and start functions
// Init sets up structs
// Start launches goroutines that depend on structs being set up
// This is pretty much required to completely avoid race conditions
2018-05-28 00:13:37 +03:00
if c.log == nil {
c.log = log.New(ioutil.Discard, "", 0)
}
2018-12-29 22:14:26 +03:00
current, _ := c.config.Get()
boxPubHex, err := hex.DecodeString(current.EncryptionPublicKey)
2018-12-29 22:14:26 +03:00
if err != nil {
return err
}
boxPrivHex, err := hex.DecodeString(current.EncryptionPrivateKey)
2018-12-29 22:14:26 +03:00
if err != nil {
return err
}
sigPubHex, err := hex.DecodeString(current.SigningPublicKey)
2018-12-29 22:14:26 +03:00
if err != nil {
return err
}
sigPrivHex, err := hex.DecodeString(current.SigningPrivateKey)
2018-12-29 22:14:26 +03:00
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-05 01:37:51 +03:00
c.searches.init(c)
c.dht.init(c)
c.sessions.init(c)
//c.multicast.init(c)
2018-01-05 01:37:51 +03:00
c.peers.init(c)
c.router.init(c)
2018-12-29 22:14:26 +03:00
c.switchTable.init(c) // TODO move before peers? before router?
return nil
2017-12-29 07:16:20 +03: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!
current, _ := c.config.Get()
// Add peers from the Peers section
for _, peer := range current.Peers {
c.AddPeer(peer, "")
time.Sleep(time.Second)
}
// Add peers from the InterfacePeers section
for intf, intfpeers := range current.InterfacePeers {
for _, peer := range intfpeers {
c.AddPeer(peer, intf)
time.Sleep(time.Second)
}
}
// Sit for a while
time.Sleep(time.Minute)
}
}
// 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.log.Infoln("Reloading configuration...")
c.config.Replace(*config)
errors := 0
2018-12-30 15:04:42 +03:00
components := []chan chan error{
c.admin.reconfigure,
2019-01-14 17:25:52 +03:00
c.searches.reconfigure,
c.dht.reconfigure,
c.sessions.reconfigure,
c.peers.reconfigure,
c.router.reconfigure,
//c.router.tun.Reconfigure,
c.router.cryptokey.reconfigure,
2019-01-14 17:25:52 +03:00
c.switchTable.reconfigure,
c.link.reconfigure,
//c.multicast.reconfigure,
2018-12-30 15:04:42 +03:00
}
for _, component := range components {
response := make(chan error)
component <- response
if err := <-response; err != nil {
c.log.Errorln(err)
errors++
2018-12-30 15:04:42 +03:00
}
}
if errors > 0 {
c.log.Warnln(errors, "modules reported errors during configuration reload")
} else {
c.log.Infoln("Configuration reloaded successfully")
}
}
// GetBuildName gets the current build name. This is usually injected if built
// from git, or returns "unknown" otherwise.
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
}
// Set the router adapter
func (c *Core) SetRouterAdapter(adapter adapterImplementation) {
c.router.tun = adapter
}
// Starts up Yggdrasil using the provided NodeState, and outputs debug logging
2018-05-28 00:13:37 +03:00
// 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
c.config = config.NodeState{
Current: *nc,
Previous: *nc,
}
if name := GetBuildName(); name != "unknown" {
c.log.Infoln("Build name:", name)
}
if version := GetBuildVersion(); version != "unknown" {
c.log.Infoln("Build version:", version)
}
c.log.Infoln("Starting up...")
2018-05-28 00:13:37 +03:00
2018-12-29 22:14:26 +03:00
c.init()
2018-05-28 00:13:37 +03:00
if err := c.link.init(c); err != nil {
c.log.Errorln("Failed to start link interfaces")
return err
}
c.config.Mutex.RLock()
if c.config.Current.SwitchOptions.MaxTotalQueueSize >= SwitchQueueTotalMinSize {
c.switchTable.queueTotalMaxSize = c.config.Current.SwitchOptions.MaxTotalQueueSize
}
c.config.Mutex.RUnlock()
if err := c.switchTable.start(); err != nil {
c.log.Errorln("Failed to start switch")
return err
}
2018-05-28 00:13:37 +03:00
if err := c.router.start(); err != nil {
c.log.Errorln("Failed to start router")
2018-05-28 00:13:37 +03:00
return err
}
if err := c.admin.start(); err != nil {
c.log.Errorln("Failed to start admin socket")
2018-05-28 00:13:37 +03:00
return err
}
/*if err := c.multicast.start(); err != nil {
c.log.Errorln("Failed to start multicast interface")
2018-05-28 00:13:37 +03:00
return err
}*/
2018-05-28 00:13:37 +03:00
if err := c.router.tun.Start(c.router.addr, c.router.subnet); err != nil {
c.log.Errorln("Failed to start TUN/TAP")
2018-05-28 00:35:30 +03:00
return err
}
go c.addPeerLoop()
c.log.Infoln("Startup complete")
2018-05-28 00:13:37 +03:00
return nil
}
// Stops the Yggdrasil node.
func (c *Core) Stop() {
c.log.Infoln("Stopping...")
c.router.tun.Close()
2018-07-07 22:04:11 +03:00
c.admin.close()
2018-05-28 00:13:37 +03:00
}
// ListenOn starts a new listener
func (c *Core) ListenTCP(uri string) (*TcpListener, error) {
return c.link.tcp.listen(uri)
}
2018-05-28 00:13:37 +03:00
// Generates a new encryption keypair. The encryption keys are used to
// encrypt traffic and to derive the IPv6 address/subnet of the node.
func (c *Core) NewEncryptionKeys() (*crypto.BoxPubKey, *crypto.BoxPrivKey) {
return crypto.NewBoxKeys()
2018-05-28 00:13:37 +03:00
}
// Generates a new signing keypair. The signing keys are used to derive the
// structure of the spanning tree.
func (c *Core) NewSigningKeys() (*crypto.SigPubKey, *crypto.SigPrivKey) {
return crypto.NewSigKeys()
2018-05-28 00:13:37 +03:00
}
// Gets the node ID.
func (c *Core) GetNodeID() *crypto.NodeID {
return crypto.GetNodeID(&c.boxPub)
2017-12-29 07:16:20 +03:00
}
2018-05-28 00:13:37 +03:00
// Gets the tree ID.
func (c *Core) GetTreeID() *crypto.TreeID {
return crypto.GetTreeID(&c.sigPub)
2017-12-29 07:16:20 +03:00
}
2018-05-28 00:13:37 +03:00
// Gets the IPv6 address of the Yggdrasil node. This is always a /128.
func (c *Core) GetAddress() *net.IP {
address := net.IP(address.AddrForNodeID(c.GetNodeID())[:])
2018-05-28 00:13:37 +03:00
return &address
}
// Gets the routed IPv6 subnet of the Yggdrasil node. This is always a /64.
func (c *Core) GetSubnet() *net.IPNet {
subnet := address.SubnetForNodeID(c.GetNodeID())[:]
2018-05-28 00:13:37 +03:00
subnet = append(subnet, 0, 0, 0, 0, 0, 0, 0, 0)
2018-06-02 23:21:05 +03:00
return &net.IPNet{IP: subnet, Mask: net.CIDRMask(64, 128)}
2018-05-28 00:13:37 +03:00
}
2018-12-16 01:37:11 +03:00
// Gets the nodeinfo.
func (c *Core) GetNodeInfo() nodeinfoPayload {
2019-01-14 22:05:16 +03:00
return c.router.nodeinfo.getNodeInfo()
}
2018-12-16 01:37:11 +03:00
// Sets the nodeinfo.
func (c *Core) SetNodeInfo(nodeinfo interface{}, nodeinfoprivacy bool) {
2019-01-14 22:05:16 +03:00
c.router.nodeinfo.setNodeInfo(nodeinfo, nodeinfoprivacy)
}
2018-05-28 00:13:37 +03: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. This adds the
// peer to the peer list, so that they will be called again if the connection
// drops.
func (c *Core) AddPeer(addr string, sintf string) error {
return c.admin.addPeer(addr, sintf)
2018-05-28 00:13:37 +03:00
}
// Calls 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. This calls the
// peer once, and if the connection drops, it won't be called again.
func (c *Core) CallPeer(addr string, sintf string) error {
return c.link.call(addr, sintf)
}
2018-05-28 00:13:37 +03: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)
}
// Gets the default admin listen address for your platform.
func (c *Core) GetAdminDefaultListen() string {
return defaults.GetDefaults().DefaultAdminListen
}
2018-05-28 00:13:37 +03:00
// Gets the default TUN/TAP interface name for your platform.
func (c *Core) GetTUNDefaultIfName() string {
return defaults.GetDefaults().DefaultIfName
2018-05-28 00:13:37 +03: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 {
return defaults.GetDefaults().DefaultIfMTU
2018-05-28 00:13:37 +03: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 {
return defaults.GetDefaults().MaximumIfMTU
2018-05-28 00:13:37 +03:00
}
// Gets the default TUN/TAP interface mode for your platform.
func (c *Core) GetTUNDefaultIfTAPMode() bool {
return defaults.GetDefaults().DefaultIfTAPMode
2018-05-28 00:13:37 +03:00
}
// Gets the current TUN/TAP interface name.
func (c *Core) GetTUNIfName() string {
//return c.router.tun.iface.Name()
return c.router.tun.Name()
}
2018-05-28 00:13:37 +03:00
// Gets the current TUN/TAP interface MTU.
func (c *Core) GetTUNIfMTU() int {
//return c.router.tun.mtu
return c.router.tun.MTU()
}