2021-05-23 14:42:26 -05:00
|
|
|
package core
|
2017-12-28 22:16:20 -06:00
|
|
|
|
2018-06-12 17:50:08 -05:00
|
|
|
import (
|
2021-06-12 06:06:39 -05:00
|
|
|
"context"
|
2021-05-08 08:35:58 -05:00
|
|
|
"crypto/ed25519"
|
2018-06-12 17:50:08 -05:00
|
|
|
"encoding/hex"
|
2019-07-16 09:49:28 +01:00
|
|
|
"errors"
|
2021-06-13 04:54:06 -05:00
|
|
|
"fmt"
|
2018-06-12 17:50:08 -05:00
|
|
|
"io/ioutil"
|
2021-05-23 21:47:12 -05:00
|
|
|
"net/url"
|
2019-01-14 17:21:15 +00:00
|
|
|
"time"
|
2018-06-12 17:50:08 -05:00
|
|
|
|
2021-05-08 08:35:58 -05:00
|
|
|
iw "github.com/Arceliar/ironwood/encrypted"
|
2019-08-28 12:17:19 +01:00
|
|
|
"github.com/Arceliar/phony"
|
2019-01-27 13:31:43 +00:00
|
|
|
"github.com/gologme/log"
|
|
|
|
|
2018-12-07 19:56:04 -06:00
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/config"
|
2021-05-08 08:35:58 -05:00
|
|
|
//"github.com/yggdrasil-network/yggdrasil-go/src/crypto"
|
2019-08-11 00:31:22 +03:00
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/version"
|
2018-06-12 17:50:08 -05:00
|
|
|
)
|
2017-12-28 22:16:20 -06:00
|
|
|
|
2018-05-27 22:13:37 +01:00
|
|
|
// The Core object represents the Yggdrasil node. You should create a Core
|
|
|
|
// object for each Yggdrasil node you plan to run.
|
2017-12-28 22:16:20 -06: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
|
2019-08-28 19:31:04 +01:00
|
|
|
phony.Inbox
|
2021-06-13 04:54:06 -05:00
|
|
|
pc *iw.PacketConn
|
2021-06-02 14:19:32 +01:00
|
|
|
config *config.NodeConfig // Config
|
2021-05-08 08:35:58 -05:00
|
|
|
secret ed25519.PrivateKey
|
|
|
|
public ed25519.PublicKey
|
2020-05-23 10:23:55 -05:00
|
|
|
links links
|
2021-06-13 04:54:06 -05:00
|
|
|
proto protoHandler
|
|
|
|
store keyStore
|
2019-09-18 15:22:17 +01:00
|
|
|
log *log.Logger
|
|
|
|
addPeerTimer *time.Timer
|
2021-06-12 06:06:39 -05:00
|
|
|
ctx context.Context
|
|
|
|
ctxCancel context.CancelFunc
|
2017-12-28 22:16:20 -06:00
|
|
|
}
|
|
|
|
|
2019-08-28 19:53:52 +01: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 17:30:51 -06:00
|
|
|
// This is pretty much required to completely avoid race conditions
|
2021-06-13 04:54:06 -05:00
|
|
|
c.config.RLock()
|
|
|
|
defer c.config.RUnlock()
|
2018-05-27 22:13:37 +01:00
|
|
|
if c.log == nil {
|
|
|
|
c.log = log.New(ioutil.Discard, "", 0)
|
|
|
|
}
|
2018-12-29 19:14:26 +00:00
|
|
|
|
2021-06-02 14:19:32 +01:00
|
|
|
sigPriv, err := hex.DecodeString(c.config.PrivateKey)
|
2018-12-29 19:14:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-08 08:35:58 -05:00
|
|
|
if len(sigPriv) < ed25519.PrivateKeySize {
|
2021-05-15 15:00:12 -05:00
|
|
|
return errors.New("PrivateKey is incorrect length")
|
2019-07-16 09:49:28 +01:00
|
|
|
}
|
2018-12-29 19:14:26 +00:00
|
|
|
|
2021-05-08 08:35:58 -05:00
|
|
|
c.secret = ed25519.PrivateKey(sigPriv)
|
2021-05-10 22:06:38 +01:00
|
|
|
c.public = c.secret.Public().(ed25519.PublicKey)
|
2021-05-15 15:00:12 -05:00
|
|
|
// TODO check public against current.PublicKey, error if they don't match
|
2019-07-16 09:49:28 +01:00
|
|
|
|
2021-06-13 04:22:21 -05:00
|
|
|
c.pc, err = iw.NewPacketConn(c.secret)
|
2021-06-12 06:06:39 -05:00
|
|
|
c.ctx, c.ctxCancel = context.WithCancel(context.Background())
|
2021-06-13 04:22:21 -05:00
|
|
|
c.store.init(c)
|
|
|
|
c.proto.init(c)
|
2021-06-13 04:54:06 -05:00
|
|
|
if err := c.proto.nodeinfo.setNodeInfo(c.config.NodeInfo, c.config.NodeInfoPrivacy); err != nil {
|
|
|
|
return fmt.Errorf("setNodeInfo: %w", err)
|
|
|
|
}
|
2021-05-10 22:06:38 +01:00
|
|
|
return err
|
2017-12-28 22:16:20 -06: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.
|
2019-08-28 19:53:52 +01:00
|
|
|
func (c *Core) _addPeerLoop() {
|
2021-06-02 14:19:32 +01:00
|
|
|
c.config.RLock()
|
|
|
|
defer c.config.RUnlock()
|
2019-01-14 17:21:15 +00:00
|
|
|
|
2021-06-12 06:06:39 -05:00
|
|
|
if c.addPeerTimer == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-28 19:53:52 +01:00
|
|
|
// Add peers from the Peers section
|
2021-06-02 14:19:32 +01:00
|
|
|
for _, peer := range c.config.Peers {
|
2021-05-23 20:34:13 -05:00
|
|
|
go func(peer string, intf string) {
|
2021-05-23 21:47:12 -05:00
|
|
|
u, err := url.Parse(peer)
|
|
|
|
if err != nil {
|
|
|
|
c.log.Errorln("Failed to parse peer url:", peer, err)
|
|
|
|
}
|
|
|
|
if err := c.CallPeer(u, intf); err != nil {
|
2019-09-18 15:01:19 +01:00
|
|
|
c.log.Errorln("Failed to add peer:", err)
|
|
|
|
}
|
2019-09-18 16:15:33 +01:00
|
|
|
}(peer, "") // TODO: this should be acted and not in a goroutine?
|
2019-08-28 19:53:52 +01:00
|
|
|
}
|
2019-01-14 17:21:15 +00:00
|
|
|
|
2019-08-28 19:53:52 +01:00
|
|
|
// Add peers from the InterfacePeers section
|
2021-06-02 14:19:32 +01:00
|
|
|
for intf, intfpeers := range c.config.InterfacePeers {
|
2019-08-28 19:53:52 +01:00
|
|
|
for _, peer := range intfpeers {
|
2021-05-23 20:34:13 -05:00
|
|
|
go func(peer string, intf string) {
|
2021-05-23 21:47:12 -05:00
|
|
|
u, err := url.Parse(peer)
|
|
|
|
if err != nil {
|
|
|
|
c.log.Errorln("Failed to parse peer url:", peer, err)
|
|
|
|
}
|
|
|
|
if err := c.CallPeer(u, intf); err != nil {
|
2019-09-18 15:01:19 +01:00
|
|
|
c.log.Errorln("Failed to add peer:", err)
|
|
|
|
}
|
2019-09-18 16:15:33 +01:00
|
|
|
}(peer, intf) // TODO: this should be acted and not in a goroutine?
|
2019-08-28 19:53:52 +01:00
|
|
|
}
|
2019-01-14 17:21:15 +00:00
|
|
|
}
|
2019-08-28 19:53:52 +01:00
|
|
|
|
2021-06-12 06:06:39 -05:00
|
|
|
c.addPeerTimer = time.AfterFunc(time.Minute, func() {
|
|
|
|
c.Act(nil, c._addPeerLoop)
|
|
|
|
})
|
2019-01-14 17:21:15 +00:00
|
|
|
}
|
2018-12-29 18:51:51 +00:00
|
|
|
|
2019-03-29 18:05:17 +00:00
|
|
|
// Start starts up Yggdrasil using the provided config.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. A config.NodeState is returned which contains both the
|
|
|
|
// current and previous configurations (from reconfigures).
|
2021-06-02 14:19:32 +01:00
|
|
|
func (c *Core) Start(nc *config.NodeConfig, log *log.Logger) (err error) {
|
2019-08-28 19:53:52 +01:00
|
|
|
phony.Block(c, func() {
|
2021-06-02 14:19:32 +01:00
|
|
|
err = c._start(nc, log)
|
2019-08-28 19:53:52 +01:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function is unsafe and should only be ran by the core actor.
|
2021-06-02 14:19:32 +01:00
|
|
|
func (c *Core) _start(nc *config.NodeConfig, log *log.Logger) error {
|
2018-05-27 22:13:37 +01:00
|
|
|
c.log = log
|
2021-06-02 14:19:32 +01:00
|
|
|
c.config = nc
|
2019-03-28 00:30:25 +00:00
|
|
|
|
2019-08-11 00:31:22 +03:00
|
|
|
if name := version.BuildName(); name != "unknown" {
|
2019-01-27 13:31:43 +00:00
|
|
|
c.log.Infoln("Build name:", name)
|
2018-12-07 22:24:01 +00:00
|
|
|
}
|
2019-08-11 00:31:22 +03:00
|
|
|
if version := version.BuildVersion(); version != "unknown" {
|
2019-01-27 13:31:43 +00:00
|
|
|
c.log.Infoln("Build version:", version)
|
2018-12-07 22:24:01 +00:00
|
|
|
}
|
|
|
|
|
2019-01-27 13:31:43 +00:00
|
|
|
c.log.Infoln("Starting up...")
|
2020-03-31 18:14:20 -05:00
|
|
|
if err := c._init(); err != nil {
|
|
|
|
c.log.Errorln("Failed to initialize core")
|
2021-06-02 14:19:32 +01:00
|
|
|
return err
|
2020-03-31 18:14:20 -05:00
|
|
|
}
|
2018-05-27 22:13:37 +01:00
|
|
|
|
2020-05-23 10:23:55 -05:00
|
|
|
if err := c.links.init(c); err != nil {
|
2019-01-27 20:54:21 +00:00
|
|
|
c.log.Errorln("Failed to start link interfaces")
|
2021-06-02 14:19:32 +01:00
|
|
|
return err
|
2019-01-19 00:42:53 +00:00
|
|
|
}
|
2019-01-04 17:14:40 +00:00
|
|
|
|
2021-05-29 21:37:13 -05:00
|
|
|
c.addPeerTimer = time.AfterFunc(0, func() {
|
|
|
|
c.Act(nil, c._addPeerLoop)
|
|
|
|
})
|
2019-01-14 17:21:15 +00:00
|
|
|
|
2019-01-27 13:31:43 +00:00
|
|
|
c.log.Infoln("Startup complete")
|
2021-06-02 14:19:32 +01:00
|
|
|
return nil
|
2018-05-27 22:13:37 +01:00
|
|
|
}
|
|
|
|
|
2019-03-29 18:05:17 +00:00
|
|
|
// Stop shuts down the Yggdrasil node.
|
2018-05-27 22:13:37 +01:00
|
|
|
func (c *Core) Stop() {
|
2019-08-28 19:53:52 +01:00
|
|
|
phony.Block(c, c._stop)
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function is unsafe and should only be ran by the core actor.
|
|
|
|
func (c *Core) _stop() {
|
2021-06-12 06:07:33 -05:00
|
|
|
c.log.Infoln("Stopping...")
|
2021-06-12 06:06:39 -05:00
|
|
|
c.ctxCancel()
|
2021-06-13 04:22:21 -05:00
|
|
|
c.pc.Close()
|
2019-09-18 15:34:26 +01:00
|
|
|
if c.addPeerTimer != nil {
|
|
|
|
c.addPeerTimer.Stop()
|
2021-05-29 21:37:13 -05:00
|
|
|
c.addPeerTimer = nil
|
2019-09-18 15:34:26 +01:00
|
|
|
}
|
2021-06-02 14:40:09 +01:00
|
|
|
_ = c.links.stop()
|
2020-03-29 00:48:41 -05:00
|
|
|
/* FIXME this deadlocks, need a waitgroup or something to coordinate shutdown
|
2019-09-18 15:22:17 +01:00
|
|
|
for _, peer := range c.GetPeers() {
|
|
|
|
c.DisconnectPeer(peer.Port)
|
|
|
|
}
|
2020-03-29 00:48:41 -05:00
|
|
|
*/
|
2019-09-18 16:32:22 +01:00
|
|
|
c.log.Infoln("Stopped")
|
2018-05-27 22:13:37 +01:00
|
|
|
}
|