From 69632bacb516e8fd7ded1fbb6860d3f224429f08 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Sun, 2 Oct 2022 13:20:39 +0100 Subject: [PATCH] Tidy up --- src/admin/admin.go | 5 ++--- src/config/config.go | 2 -- src/core/api.go | 18 ++++------------ src/core/core.go | 19 +++++++++++++---- src/core/debug.go | 1 - src/core/link.go | 7 +----- src/core/nodeinfo.go | 3 --- src/tun/tun.go | 6 +++--- src/util/util.go | 51 -------------------------------------------- 9 files changed, 25 insertions(+), 87 deletions(-) delete mode 100644 src/util/util.go diff --git a/src/admin/admin.go b/src/admin/admin.go index 7f5f467e..b24bf0de 100644 --- a/src/admin/admin.go +++ b/src/admin/admin.go @@ -13,14 +13,13 @@ import ( "time" "github.com/yggdrasil-network/yggdrasil-go/src/core" - "github.com/yggdrasil-network/yggdrasil-go/src/util" ) // TODO: Add authentication type AdminSocket struct { core *core.Core - log util.Logger + log core.Logger listener net.Listener handlers map[string]handler done chan struct{} @@ -72,7 +71,7 @@ func (a *AdminSocket) AddHandler(name, desc string, args []string, handlerfunc c } // Init runs the initial admin setup. -func New(c *core.Core, log util.Logger, opts ...SetupOption) (*AdminSocket, error) { +func New(c *core.Core, log core.Logger, opts ...SetupOption) (*AdminSocket, error) { a := &AdminSocket{ core: c, log: log, diff --git a/src/config/config.go b/src/config/config.go index c93699e1..5bdeec4b 100644 --- a/src/config/config.go +++ b/src/config/config.go @@ -19,14 +19,12 @@ package config import ( "crypto/ed25519" "encoding/hex" - "sync" ) // NodeConfig is the main configuration structure, containing configuration // options that are necessary for an Yggdrasil node to run. You will need to // supply one of these structs to the Yggdrasil core when starting a node. type NodeConfig struct { - sync.RWMutex `json:"-"` Peers []string `comment:"List of connection strings for outbound peer connections in URI format,\ne.g. tls://a.b.c.d:e or socks://a.b.c.d:e/f.g.h.i:j. These connections\nwill obey the operating system routing table, therefore you should\nuse this section when you may connect via different interfaces."` InterfacePeers map[string][]string `comment:"List of connection strings for outbound peer connections in URI format,\narranged by source interface, e.g. { \"eth0\": [ \"tls://a.b.c.d:e\" ] }.\nNote that SOCKS peerings will NOT be affected by this option and should\ngo in the \"Peers\" section instead."` Listen []string `comment:"Listen addresses for incoming connections. You will need to add\nlisteners in order to accept incoming peerings from non-local nodes.\nMulticast peer discovery will work regardless of any listeners set\nhere. Each listener should be specified in URI format as above, e.g.\ntls://0.0.0.0:0 or tls://[::]:0 to listen on all interfaces."` diff --git a/src/core/api.go b/src/core/api.go index dfc309d3..0fa6dd33 100644 --- a/src/core/api.go +++ b/src/core/api.go @@ -2,25 +2,15 @@ package core import ( "crypto/ed25519" + "encoding/json" "fmt" + "net" + "net/url" "sync/atomic" "time" - //"encoding/hex" - "encoding/json" - //"errors" - //"fmt" - "net" - "net/url" - - //"sort" - //"time" - "github.com/Arceliar/phony" "github.com/yggdrasil-network/yggdrasil-go/src/address" - "github.com/yggdrasil-network/yggdrasil-go/src/util" - //"github.com/yggdrasil-network/yggdrasil-go/src/crypto" - //"github.com/Arceliar/phony" ) type SelfInfo struct { @@ -176,7 +166,7 @@ func (c *Core) Subnet() net.IPNet { // may be useful if you want to redirect the output later. Note that this // expects a Logger from the github.com/gologme/log package and not from Go's // built-in log package. -func (c *Core) SetLogger(log util.Logger) { +func (c *Core) SetLogger(log Logger) { c.log = log } diff --git a/src/core/core.go b/src/core/core.go index b8315416..67f927a6 100644 --- a/src/core/core.go +++ b/src/core/core.go @@ -14,9 +14,7 @@ import ( "github.com/Arceliar/phony" "github.com/gologme/log" - "github.com/yggdrasil-network/yggdrasil-go/src/util" "github.com/yggdrasil-network/yggdrasil-go/src/version" - //"github.com/yggdrasil-network/yggdrasil-go/src/crypto" ) // The Core object represents the Yggdrasil node. You should create a Core @@ -33,7 +31,7 @@ type Core struct { public ed25519.PublicKey links links proto protoHandler - log util.Logger + log Logger addPeerTimer *time.Timer config struct { _peers map[Peer]*linkInfo // configurable after startup @@ -44,7 +42,7 @@ type Core struct { } } -func New(secret ed25519.PrivateKey, logger util.Logger, opts ...SetupOption) (*Core, error) { +func New(secret ed25519.PrivateKey, logger Logger, opts ...SetupOption) (*Core, error) { c := &Core{ log: logger, } @@ -193,3 +191,16 @@ func (c *Core) WriteTo(p []byte, addr net.Addr) (n int, err error) { } return } + +type Logger interface { + Printf(string, ...interface{}) + Println(...interface{}) + Infof(string, ...interface{}) + Infoln(...interface{}) + Warnf(string, ...interface{}) + Warnln(...interface{}) + Errorf(string, ...interface{}) + Errorln(...interface{}) + Debugf(string, ...interface{}) + Debugln(...interface{}) +} diff --git a/src/core/debug.go b/src/core/debug.go index ee1f1ed8..6085ceeb 100644 --- a/src/core/debug.go +++ b/src/core/debug.go @@ -5,7 +5,6 @@ package core import ( "fmt" - "net/http" _ "net/http/pprof" "os" diff --git a/src/core/link.go b/src/core/link.go index 860c3b83..b4515276 100644 --- a/src/core/link.go +++ b/src/core/link.go @@ -9,15 +9,11 @@ import ( "net" "net/url" "strings" - - //"sync/atomic" - "time" - "sync/atomic" + "time" "github.com/Arceliar/phony" "github.com/yggdrasil-network/yggdrasil-go/src/address" - //"github.com/Arceliar/phony" // TODO? use instead of mutexes ) type links struct { @@ -28,7 +24,6 @@ type links struct { unix *linkUNIX // UNIX interface support socks *linkSOCKS // SOCKS interface support _links map[linkInfo]*link // *link is nil if connection in progress - // TODO timeout (to remove from switch), read from config.ReadTimeout } // linkInfo is used as a map key diff --git a/src/core/nodeinfo.go b/src/core/nodeinfo.go index e02c79c7..bac0935b 100644 --- a/src/core/nodeinfo.go +++ b/src/core/nodeinfo.go @@ -11,9 +11,6 @@ import ( iwt "github.com/Arceliar/ironwood/types" "github.com/Arceliar/phony" - - //"github.com/yggdrasil-network/yggdrasil-go/src/crypto" - "github.com/yggdrasil-network/yggdrasil-go/src/version" ) diff --git a/src/tun/tun.go b/src/tun/tun.go index 0f7a70e3..ce1bd169 100644 --- a/src/tun/tun.go +++ b/src/tun/tun.go @@ -14,9 +14,9 @@ import ( "golang.zx2c4.com/wireguard/tun" "github.com/yggdrasil-network/yggdrasil-go/src/address" + "github.com/yggdrasil-network/yggdrasil-go/src/core" "github.com/yggdrasil-network/yggdrasil-go/src/defaults" "github.com/yggdrasil-network/yggdrasil-go/src/ipv6rwc" - "github.com/yggdrasil-network/yggdrasil-go/src/util" ) type MTU uint16 @@ -27,7 +27,7 @@ type MTU uint16 // calling yggdrasil.Start(). type TunAdapter struct { rwc *ipv6rwc.ReadWriteCloser - log util.Logger + log core.Logger addr address.Address subnet address.Subnet mtu uint64 @@ -90,7 +90,7 @@ func MaximumMTU() uint64 { // Init initialises the TUN module. You must have acquired a Listener from // the Yggdrasil core before this point and it must not be in use elsewhere. -func New(rwc *ipv6rwc.ReadWriteCloser, log util.Logger, opts ...SetupOption) (*TunAdapter, error) { +func New(rwc *ipv6rwc.ReadWriteCloser, log core.Logger, opts ...SetupOption) (*TunAdapter, error) { tun := &TunAdapter{ rwc: rwc, log: log, diff --git a/src/util/util.go b/src/util/util.go deleted file mode 100644 index e2e21464..00000000 --- a/src/util/util.go +++ /dev/null @@ -1,51 +0,0 @@ -// Package util contains miscellaneous utilities used by yggdrasil. -// In particular, this includes a crypto worker pool, Cancellation machinery, and a sync.Pool used to reuse []byte. -package util - -// These are misc. utility functions that didn't really fit anywhere else - -import ( - "time" -) - -// Any logger that satisfies this interface is suitable for Yggdrasil. -type Logger interface { - Printf(string, ...interface{}) - Println(...interface{}) - Infof(string, ...interface{}) - Infoln(...interface{}) - Warnf(string, ...interface{}) - Warnln(...interface{}) - Errorf(string, ...interface{}) - Errorln(...interface{}) - Debugf(string, ...interface{}) - Debugln(...interface{}) -} - -// TimerStop stops a timer and makes sure the channel is drained, returns true if the timer was stopped before firing. -func TimerStop(t *time.Timer) bool { - stopped := t.Stop() - select { - case <-t.C: - default: - } - return stopped -} - -// FuncTimeout runs the provided function in a separate goroutine, and returns true if the function finishes executing before the timeout passes, or false if the timeout passes. -// It includes no mechanism to stop the function if the timeout fires, so the user is expected to do so on their own (such as with a Cancellation or a context). -func FuncTimeout(timeout time.Duration, f func()) bool { - success := make(chan struct{}) - go func() { - defer close(success) - f() - }() - timer := time.NewTimer(timeout) - defer TimerStop(timer) - select { - case <-success: - return true - case <-timer.C: - return false - } -}