mirror of
https://github.com/cwinfo/yggdrasil-go.git
synced 2024-11-09 16:20:26 +00:00
Tidy up
This commit is contained in:
parent
962665189c
commit
69632bacb5
@ -13,14 +13,13 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/yggdrasil-network/yggdrasil-go/src/core"
|
"github.com/yggdrasil-network/yggdrasil-go/src/core"
|
||||||
"github.com/yggdrasil-network/yggdrasil-go/src/util"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO: Add authentication
|
// TODO: Add authentication
|
||||||
|
|
||||||
type AdminSocket struct {
|
type AdminSocket struct {
|
||||||
core *core.Core
|
core *core.Core
|
||||||
log util.Logger
|
log core.Logger
|
||||||
listener net.Listener
|
listener net.Listener
|
||||||
handlers map[string]handler
|
handlers map[string]handler
|
||||||
done chan struct{}
|
done chan struct{}
|
||||||
@ -72,7 +71,7 @@ func (a *AdminSocket) AddHandler(name, desc string, args []string, handlerfunc c
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Init runs the initial admin setup.
|
// 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{
|
a := &AdminSocket{
|
||||||
core: c,
|
core: c,
|
||||||
log: log,
|
log: log,
|
||||||
|
@ -19,14 +19,12 @@ package config
|
|||||||
import (
|
import (
|
||||||
"crypto/ed25519"
|
"crypto/ed25519"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"sync"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// NodeConfig is the main configuration structure, containing configuration
|
// NodeConfig is the main configuration structure, containing configuration
|
||||||
// options that are necessary for an Yggdrasil node to run. You will need to
|
// 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.
|
// supply one of these structs to the Yggdrasil core when starting a node.
|
||||||
type NodeConfig struct {
|
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."`
|
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."`
|
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."`
|
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."`
|
||||||
|
@ -2,25 +2,15 @@ package core
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/ed25519"
|
"crypto/ed25519"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"net/url"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
//"encoding/hex"
|
|
||||||
"encoding/json"
|
|
||||||
//"errors"
|
|
||||||
//"fmt"
|
|
||||||
"net"
|
|
||||||
"net/url"
|
|
||||||
|
|
||||||
//"sort"
|
|
||||||
//"time"
|
|
||||||
|
|
||||||
"github.com/Arceliar/phony"
|
"github.com/Arceliar/phony"
|
||||||
"github.com/yggdrasil-network/yggdrasil-go/src/address"
|
"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 {
|
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
|
// 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
|
// expects a Logger from the github.com/gologme/log package and not from Go's
|
||||||
// built-in log package.
|
// built-in log package.
|
||||||
func (c *Core) SetLogger(log util.Logger) {
|
func (c *Core) SetLogger(log Logger) {
|
||||||
c.log = log
|
c.log = log
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,9 +14,7 @@ import (
|
|||||||
"github.com/Arceliar/phony"
|
"github.com/Arceliar/phony"
|
||||||
"github.com/gologme/log"
|
"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/version"
|
||||||
//"github.com/yggdrasil-network/yggdrasil-go/src/crypto"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// The Core object represents the Yggdrasil node. You should create a Core
|
// The Core object represents the Yggdrasil node. You should create a Core
|
||||||
@ -33,7 +31,7 @@ type Core struct {
|
|||||||
public ed25519.PublicKey
|
public ed25519.PublicKey
|
||||||
links links
|
links links
|
||||||
proto protoHandler
|
proto protoHandler
|
||||||
log util.Logger
|
log Logger
|
||||||
addPeerTimer *time.Timer
|
addPeerTimer *time.Timer
|
||||||
config struct {
|
config struct {
|
||||||
_peers map[Peer]*linkInfo // configurable after startup
|
_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{
|
c := &Core{
|
||||||
log: logger,
|
log: logger,
|
||||||
}
|
}
|
||||||
@ -193,3 +191,16 @@ func (c *Core) WriteTo(p []byte, addr net.Addr) (n int, err error) {
|
|||||||
}
|
}
|
||||||
return
|
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{})
|
||||||
|
}
|
||||||
|
@ -5,7 +5,6 @@ package core
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"net/http"
|
"net/http"
|
||||||
_ "net/http/pprof"
|
_ "net/http/pprof"
|
||||||
"os"
|
"os"
|
||||||
|
@ -9,15 +9,11 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
//"sync/atomic"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/Arceliar/phony"
|
"github.com/Arceliar/phony"
|
||||||
"github.com/yggdrasil-network/yggdrasil-go/src/address"
|
"github.com/yggdrasil-network/yggdrasil-go/src/address"
|
||||||
//"github.com/Arceliar/phony" // TODO? use instead of mutexes
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type links struct {
|
type links struct {
|
||||||
@ -28,7 +24,6 @@ type links struct {
|
|||||||
unix *linkUNIX // UNIX interface support
|
unix *linkUNIX // UNIX interface support
|
||||||
socks *linkSOCKS // SOCKS interface support
|
socks *linkSOCKS // SOCKS interface support
|
||||||
_links map[linkInfo]*link // *link is nil if connection in progress
|
_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
|
// linkInfo is used as a map key
|
||||||
|
@ -11,9 +11,6 @@ import (
|
|||||||
|
|
||||||
iwt "github.com/Arceliar/ironwood/types"
|
iwt "github.com/Arceliar/ironwood/types"
|
||||||
"github.com/Arceliar/phony"
|
"github.com/Arceliar/phony"
|
||||||
|
|
||||||
//"github.com/yggdrasil-network/yggdrasil-go/src/crypto"
|
|
||||||
|
|
||||||
"github.com/yggdrasil-network/yggdrasil-go/src/version"
|
"github.com/yggdrasil-network/yggdrasil-go/src/version"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -14,9 +14,9 @@ import (
|
|||||||
"golang.zx2c4.com/wireguard/tun"
|
"golang.zx2c4.com/wireguard/tun"
|
||||||
|
|
||||||
"github.com/yggdrasil-network/yggdrasil-go/src/address"
|
"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/defaults"
|
||||||
"github.com/yggdrasil-network/yggdrasil-go/src/ipv6rwc"
|
"github.com/yggdrasil-network/yggdrasil-go/src/ipv6rwc"
|
||||||
"github.com/yggdrasil-network/yggdrasil-go/src/util"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type MTU uint16
|
type MTU uint16
|
||||||
@ -27,7 +27,7 @@ type MTU uint16
|
|||||||
// calling yggdrasil.Start().
|
// calling yggdrasil.Start().
|
||||||
type TunAdapter struct {
|
type TunAdapter struct {
|
||||||
rwc *ipv6rwc.ReadWriteCloser
|
rwc *ipv6rwc.ReadWriteCloser
|
||||||
log util.Logger
|
log core.Logger
|
||||||
addr address.Address
|
addr address.Address
|
||||||
subnet address.Subnet
|
subnet address.Subnet
|
||||||
mtu uint64
|
mtu uint64
|
||||||
@ -90,7 +90,7 @@ func MaximumMTU() uint64 {
|
|||||||
|
|
||||||
// Init initialises the TUN module. You must have acquired a Listener from
|
// 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.
|
// 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{
|
tun := &TunAdapter{
|
||||||
rwc: rwc,
|
rwc: rwc,
|
||||||
log: log,
|
log: log,
|
||||||
|
@ -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
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user