Continue refactoring

This commit is contained in:
Neil Alexander 2022-08-06 15:05:12 +01:00
parent 5616b9fc84
commit 4c889703b1
7 changed files with 82 additions and 60 deletions

View file

@ -2,6 +2,8 @@ package core
import (
"crypto/ed25519"
"sync/atomic"
"time"
//"encoding/hex"
"encoding/json"
@ -19,14 +21,13 @@ import (
//"github.com/Arceliar/phony"
)
type Self struct {
type SelfInfo struct {
Key ed25519.PublicKey
Root ed25519.PublicKey
Coords []uint64
}
/*
type Peer struct {
type PeerInfo struct {
Key ed25519.PublicKey
Root ed25519.PublicKey
Coords []uint64
@ -36,25 +37,24 @@ type Peer struct {
TXBytes uint64
Uptime time.Duration
}
*/
type DHTEntry struct {
type DHTEntryInfo struct {
Key ed25519.PublicKey
Port uint64
Rest uint64
}
type PathEntry struct {
type PathEntryInfo struct {
Key ed25519.PublicKey
Path []uint64
}
type Session struct {
type SessionInfo struct {
Key ed25519.PublicKey
}
func (c *Core) GetSelf() Self {
var self Self
func (c *Core) GetSelf() SelfInfo {
var self SelfInfo
s := c.PacketConn.PacketConn.Debug.GetSelf()
self.Key = s.Key
self.Root = s.Root
@ -62,9 +62,8 @@ func (c *Core) GetSelf() Self {
return self
}
/*
func (c *Core) GetPeers() []Peer {
var peers []Peer
func (c *Core) GetPeers() []PeerInfo {
var peers []PeerInfo
names := make(map[net.Conn]string)
c.links.mutex.Lock()
for _, info := range c.links.links {
@ -73,7 +72,7 @@ func (c *Core) GetPeers() []Peer {
c.links.mutex.Unlock()
ps := c.PacketConn.PacketConn.Debug.GetPeers()
for _, p := range ps {
var info Peer
var info PeerInfo
info.Key = p.Key
info.Root = p.Root
info.Coords = p.Coords
@ -91,13 +90,12 @@ func (c *Core) GetPeers() []Peer {
}
return peers
}
*/
func (c *Core) GetDHT() []DHTEntry {
var dhts []DHTEntry
func (c *Core) GetDHT() []DHTEntryInfo {
var dhts []DHTEntryInfo
ds := c.PacketConn.PacketConn.Debug.GetDHT()
for _, d := range ds {
var info DHTEntry
var info DHTEntryInfo
info.Key = d.Key
info.Port = d.Port
info.Rest = d.Rest
@ -106,11 +104,11 @@ func (c *Core) GetDHT() []DHTEntry {
return dhts
}
func (c *Core) GetPaths() []PathEntry {
var paths []PathEntry
func (c *Core) GetPaths() []PathEntryInfo {
var paths []PathEntryInfo
ps := c.PacketConn.PacketConn.Debug.GetPaths()
for _, p := range ps {
var info PathEntry
var info PathEntryInfo
info.Key = p.Key
info.Path = p.Path
paths = append(paths, info)
@ -118,11 +116,11 @@ func (c *Core) GetPaths() []PathEntry {
return paths
}
func (c *Core) GetSessions() []Session {
var sessions []Session
func (c *Core) GetSessions() []SessionInfo {
var sessions []SessionInfo
ss := c.PacketConn.Debug.GetSessions()
for _, s := range ss {
var info Session
var info SessionInfo
info.Key = s.Key
sessions = append(sessions, info)
}

View file

@ -15,7 +15,6 @@ import (
"github.com/Arceliar/phony"
"github.com/gologme/log"
"github.com/yggdrasil-network/yggdrasil-go/src/crypto"
"github.com/yggdrasil-network/yggdrasil-go/src/version"
//"github.com/yggdrasil-network/yggdrasil-go/src/crypto"
)
@ -61,6 +60,9 @@ func New(secret ed25519.PrivateKey, opts ...SetupOption) (*Core, error) {
if c.PacketConn, err = iwe.NewPacketConn(c.secret); err != nil {
return nil, fmt.Errorf("error creating encryption: %w", err)
}
c.config._peers = map[Peer]struct{}{}
c.config._listeners = map[ListenAddress]struct{}{}
c.config._allowedPublicKeys = map[[32]byte]struct{}{}
for _, opt := range opts {
c._applyOption(opt)
}
@ -101,7 +103,7 @@ func (c *Core) _applyOption(opt SetupOption) {
case IfMTU:
c.config.ifmtu = v
case AllowedPublicKey:
pk := crypto.SigPubKey{}
pk := [32]byte{}
copy(pk[:], v)
c.config._allowedPublicKeys[pk] = struct{}{}
}

View file

@ -2,6 +2,7 @@ package core
import (
"bytes"
"crypto/ed25519"
"math/rand"
"net/url"
"os"
@ -9,21 +10,8 @@ import (
"time"
"github.com/gologme/log"
"github.com/yggdrasil-network/yggdrasil-go/src/config"
"github.com/yggdrasil-network/yggdrasil-go/src/defaults"
)
// GenerateConfig produces default configuration with suitable modifications for tests.
func GenerateConfig() *config.NodeConfig {
cfg := defaults.GenerateConfig()
cfg.AdminListen = "none"
cfg.Listen = []string{"tcp://127.0.0.1:0"}
cfg.IfName = "none"
return cfg
}
// GetLoggerWithPrefix creates a new logger instance with prefix.
// If verbose is set to true, three log levels are enabled: "info", "warn", "error".
func GetLoggerWithPrefix(prefix string, verbose bool) *log.Logger {
@ -40,13 +28,18 @@ func GetLoggerWithPrefix(prefix string, verbose bool) *log.Logger {
// CreateAndConnectTwo creates two nodes. nodeB connects to nodeA.
// Verbosity flag is passed to logger.
func CreateAndConnectTwo(t testing.TB, verbose bool) (nodeA *Core, nodeB *Core) {
nodeA = new(Core)
if err := nodeA.Start(GenerateConfig(), GetLoggerWithPrefix("A: ", verbose)); err != nil {
var err error
var skA, skB ed25519.PrivateKey
if _, skA, err = ed25519.GenerateKey(nil); err != nil {
t.Fatal(err)
}
nodeB = new(Core)
if err := nodeB.Start(GenerateConfig(), GetLoggerWithPrefix("B: ", verbose)); err != nil {
if _, skB, err = ed25519.GenerateKey(nil); err != nil {
t.Fatal(err)
}
if nodeA, err = New(skA, ListenAddress("tcp://127.0.0.1:0"), IfName("none")); err != nil {
t.Fatal(err)
}
if nodeB, err = New(skB, ListenAddress("tcp://127.0.0.1:0"), IfName("none")); err != nil {
t.Fatal(err)
}

View file

@ -17,6 +17,7 @@ import (
"sync/atomic"
"github.com/Arceliar/phony"
"github.com/yggdrasil-network/yggdrasil-go/src/address"
"github.com/yggdrasil-network/yggdrasil-go/src/util"
"golang.org/x/net/proxy"
@ -62,7 +63,14 @@ func (l *links) init(c *Core) error {
l.mutex.Unlock()
l.stopped = make(chan struct{})
if err := l.tcp.init(l); err != nil {
var listeners []ListenAddress
phony.Block(c, func() {
listeners = make([]ListenAddress, 0, len(c.config._listeners))
for listener := range c.config._listeners {
listeners = append(listeners, listener)
}
})
if err := l.tcp.init(l, listeners); err != nil {
c.log.Errorln("Failed to start TCP interface")
return err
}
@ -71,10 +79,6 @@ func (l *links) init(c *Core) error {
}
func (l *links) call(u *url.URL, sintf string) error {
//u, err := url.Parse(uri)
//if err != nil {
// return fmt.Errorf("peer %s is not correctly formatted (%s)", uri, err)
//}
tcpOpts := tcpOptions{}
if pubkeys, ok := u.Query()["key"]; ok && len(pubkeys) > 0 {
tcpOpts.pinnedEd25519Keys = make(map[keyArray]struct{})