From a4c80626f43767d375afcce3bf51f62dbc88ea5f Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Sun, 4 Sep 2022 16:25:56 +0100 Subject: [PATCH] More tweaking --- cmd/yggdrasil/main.go | 7 ++----- contrib/mobile/mobile.go | 5 +---- src/core/core.go | 2 -- src/core/core_test.go | 4 ++-- src/core/link.go | 25 ++++++++++--------------- src/core/link_tcp.go | 8 +++++--- src/core/link_tls.go | 8 +++++--- src/core/options.go | 8 -------- 8 files changed, 25 insertions(+), 42 deletions(-) diff --git a/cmd/yggdrasil/main.go b/cmd/yggdrasil/main.go index 2486df45..bd5294fd 100644 --- a/cmd/yggdrasil/main.go +++ b/cmd/yggdrasil/main.go @@ -28,9 +28,9 @@ import ( "github.com/yggdrasil-network/yggdrasil-go/src/admin" "github.com/yggdrasil-network/yggdrasil-go/src/config" "github.com/yggdrasil-network/yggdrasil-go/src/defaults" + "github.com/yggdrasil-network/yggdrasil-go/src/ipv6rwc" "github.com/yggdrasil-network/yggdrasil-go/src/core" - "github.com/yggdrasil-network/yggdrasil-go/src/ipv6rwc" "github.com/yggdrasil-network/yggdrasil-go/src/multicast" "github.com/yggdrasil-network/yggdrasil-go/src/tuntap" "github.com/yggdrasil-network/yggdrasil-go/src/version" @@ -290,10 +290,7 @@ func run(args yggArgs, ctx context.Context, done chan struct{}) { if err != nil { panic(err) } - options := []core.SetupOption{ - core.IfName(cfg.IfName), - core.IfMTU(cfg.IfMTU), - } + options := []core.SetupOption{} for _, addr := range cfg.Listen { options = append(options, core.ListenAddress(addr)) } diff --git a/contrib/mobile/mobile.go b/contrib/mobile/mobile.go index d7da03de..776de3cc 100644 --- a/contrib/mobile/mobile.go +++ b/contrib/mobile/mobile.go @@ -55,10 +55,7 @@ func (m *Yggdrasil) StartJSON(configjson []byte) error { if err != nil { panic(err) } - options := []core.SetupOption{ - core.IfName("none"), - core.IfMTU(m.config.IfMTU), - } + options := []core.SetupOption{} for _, peer := range m.config.Peers { options = append(options, core.Peer{URI: peer}) } diff --git a/src/core/core.go b/src/core/core.go index 24fc7aea..8b22ead1 100644 --- a/src/core/core.go +++ b/src/core/core.go @@ -40,8 +40,6 @@ type Core struct { _listeners map[ListenAddress]struct{} // configurable after startup nodeinfo NodeInfo // immutable after startup nodeinfoPrivacy NodeInfoPrivacy // immutable after startup - ifname IfName // immutable after startup - ifmtu IfMTU // immutable after startup _allowedPublicKeys map[[32]byte]struct{} // configurable after startup } } diff --git a/src/core/core_test.go b/src/core/core_test.go index ed5b4255..8d57f336 100644 --- a/src/core/core_test.go +++ b/src/core/core_test.go @@ -37,10 +37,10 @@ func CreateAndConnectTwo(t testing.TB, verbose bool) (nodeA *Core, nodeB *Core) t.Fatal(err) } logger := GetLoggerWithPrefix("", false) - if nodeA, err = New(skA, logger, ListenAddress("tcp://127.0.0.1:0"), IfName("none")); err != nil { + if nodeA, err = New(skA, logger, ListenAddress("tcp://127.0.0.1:0")); err != nil { t.Fatal(err) } - if nodeB, err = New(skB, logger, ListenAddress("tcp://127.0.0.1:0"), IfName("none")); err != nil { + if nodeB, err = New(skB, logger, ListenAddress("tcp://127.0.0.1:0")); err != nil { t.Fatal(err) } diff --git a/src/core/link.go b/src/core/link.go index 5ff6c9a3..e00cb6f6 100644 --- a/src/core/link.go +++ b/src/core/link.go @@ -81,17 +81,12 @@ func (l *links) init(c *Core) error { func (l *links) isConnectedTo(info linkInfo) bool { l.mutex.RLock() defer l.mutex.RUnlock() - fmt.Println(l.links) _, isConnected := l.links[info] return isConnected } func (l *links) call(u *url.URL, sintf string) error { - info := linkInfo{ - linkType: strings.ToLower(u.Scheme), - local: sintf, - remote: u.Host, - } + info := linkInfoFor(u.Scheme, sintf, u.Host) if l.isConnectedTo(info) { return fmt.Errorf("already connected to this node") } @@ -171,30 +166,30 @@ func (l *links) create(conn net.Conn, name string, info linkInfo, incoming, forc force: force, } go func() { - if err := intf.handler(info); err != nil { + if err := intf.handler(); err != nil { l.core.log.Errorf("Link handler error (%s): %s", conn.RemoteAddr(), err) } }() return nil } -func (intf *link) handler(info linkInfo) error { +func (intf *link) handler() error { defer intf.conn.Close() // Don't connect to this link more than once. - if intf.links.isConnectedTo(info) { - return fmt.Errorf("already connected to %+v", info) + if intf.links.isConnectedTo(intf.info) { + return fmt.Errorf("already connected to %+v", intf.info) } // Mark the connection as in progress. intf.links.mutex.Lock() - intf.links.links[info] = nil + intf.links.links[intf.info] = nil intf.links.mutex.Unlock() // When we're done, clean up the connection entry. defer func() { intf.links.mutex.Lock() - delete(intf.links.links, info) + delete(intf.links.links, intf.info) intf.links.mutex.Unlock() }() @@ -275,7 +270,7 @@ func (intf *link) handler(info linkInfo) error { } intf.links.mutex.Lock() - intf.links.links[info] = intf + intf.links.links[intf.info] = intf intf.links.mutex.Unlock() remoteAddr := net.IP(address.AddrForKey(meta.key)[:]).String() @@ -304,13 +299,13 @@ func (intf *link) name() string { return intf.lname } -func linkInfoFor(linkType, local, remote string) linkInfo { +func linkInfoFor(linkType, sintf, remote string) linkInfo { if h, _, err := net.SplitHostPort(remote); err == nil { remote = h } return linkInfo{ linkType: linkType, - local: local, + local: sintf, remote: remote, } } diff --git a/src/core/link_tcp.go b/src/core/link_tcp.go index 818ba1ef..4649b332 100644 --- a/src/core/link_tcp.go +++ b/src/core/link_tcp.go @@ -5,6 +5,7 @@ import ( "fmt" "net" "net/url" + "strings" "time" "github.com/Arceliar/phony" @@ -39,7 +40,7 @@ func (l *links) newLinkTCP() *linkTCP { } func (l *linkTCP) dial(url *url.URL, options tcpOptions, sintf string) error { - info := linkInfoFor("tcp", url.Host, sintf) + info := linkInfoFor("tcp", sintf, strings.SplitN(url.Host, "%", 2)[0]) if l.links.isConnectedTo(info) { return fmt.Errorf("duplicate connection attempt") } @@ -86,8 +87,9 @@ func (l *linkTCP) listen(url *url.URL, sintf string) (*Listener, error) { cancel() return } - name := fmt.Sprintf("tcp://%s", conn.RemoteAddr()) - info := linkInfoFor("tcp", sintf, conn.RemoteAddr().String()) + addr := conn.RemoteAddr().(*net.TCPAddr) + name := fmt.Sprintf("tls://%s", addr) + info := linkInfoFor("tcp", sintf, strings.SplitN(addr.IP.String(), "%", 2)[0]) if err = l.handler(name, info, conn, tcpOptions{}, true); err != nil { l.core.log.Errorln("Failed to create inbound link:", err) } diff --git a/src/core/link_tls.go b/src/core/link_tls.go index e85a336b..fa4d37c1 100644 --- a/src/core/link_tls.go +++ b/src/core/link_tls.go @@ -13,6 +13,7 @@ import ( "math/big" "net" "net/url" + "strings" "time" "github.com/Arceliar/phony" @@ -46,7 +47,7 @@ func (l *links) newLinkTLS(tcp *linkTCP) *linkTLS { } func (l *linkTLS) dial(url *url.URL, options tcpOptions, sintf string) error { - info := linkInfoFor("tls", url.Host, sintf) + info := linkInfoFor("tls", sintf, strings.SplitN(url.Host, "%", 2)[0]) if l.links.isConnectedTo(info) { return fmt.Errorf("duplicate connection attempt") } @@ -98,8 +99,9 @@ func (l *linkTLS) listen(url *url.URL, sintf string) (*Listener, error) { cancel() return } - name := fmt.Sprintf("tls://%s", conn.RemoteAddr()) - info := linkInfoFor("tls", sintf, conn.RemoteAddr().String()) + addr := conn.RemoteAddr().(*net.TCPAddr) + name := fmt.Sprintf("tls://%s", addr) + info := linkInfoFor("tls", sintf, strings.SplitN(addr.IP.String(), "%", 2)[0]) if err = l.handler(name, info, conn, tcpOptions{}, true); err != nil { l.core.log.Errorln("Failed to create inbound link:", err) } diff --git a/src/core/options.go b/src/core/options.go index b3d06c63..8009aa3b 100644 --- a/src/core/options.go +++ b/src/core/options.go @@ -14,10 +14,6 @@ func (c *Core) _applyOption(opt SetupOption) { c.config.nodeinfo = v case NodeInfoPrivacy: c.config.nodeinfoPrivacy = v - case IfName: - c.config.ifname = v - case IfMTU: - c.config.ifmtu = v case AllowedPublicKey: pk := [32]byte{} copy(pk[:], v) @@ -36,14 +32,10 @@ type Peer struct { } type NodeInfo map[string]interface{} type NodeInfoPrivacy bool -type IfName string -type IfMTU uint16 type AllowedPublicKey ed25519.PublicKey func (a ListenAddress) isSetupOption() {} func (a Peer) isSetupOption() {} func (a NodeInfo) isSetupOption() {} func (a NodeInfoPrivacy) isSetupOption() {} -func (a IfName) isSetupOption() {} -func (a IfMTU) isSetupOption() {} func (a AllowedPublicKey) isSetupOption() {}