From 88925d3e06921107a603eb96def354cc938bea52 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Mon, 4 Mar 2019 22:45:35 +0000 Subject: [PATCH] Centralise call/listen functions in link.go --- src/yggdrasil/admin.go | 15 +++---------- src/yggdrasil/link.go | 44 ++++++++++++++++++++++++++++++++------ src/yggdrasil/multicast.go | 2 +- src/yggdrasil/tcp.go | 25 ++++------------------ 4 files changed, 45 insertions(+), 41 deletions(-) diff --git a/src/yggdrasil/admin.go b/src/yggdrasil/admin.go index ae5b02a2..228c43c6 100644 --- a/src/yggdrasil/admin.go +++ b/src/yggdrasil/admin.go @@ -573,18 +573,9 @@ func (a *admin) printInfos(infos []admin_nodeInfo) string { // addPeer triggers a connection attempt to a node. func (a *admin) addPeer(addr string, sintf string) error { - u, err := url.Parse(addr) - if err == nil { - switch strings.ToLower(u.Scheme) { - case "tcp": - a.core.link.tcp.connect(u.Host, sintf) - case "socks": - a.core.link.tcp.connectSOCKS(u.Host, u.Path[1:]) - default: - return errors.New("invalid peer: " + addr) - } - } else { - return errors.New("invalid peer: " + addr) + err := a.core.link.call(addr, sintf) + if err != nil { + return err } return nil } diff --git a/src/yggdrasil/link.go b/src/yggdrasil/link.go index 4f7c6e1f..cbbdb5e9 100644 --- a/src/yggdrasil/link.go +++ b/src/yggdrasil/link.go @@ -6,6 +6,7 @@ import ( "fmt" "io" "net" + "net/url" "strings" "sync" @@ -68,21 +69,20 @@ func (l *link) init(c *Core) error { } if err := l.awdl.init(l); err != nil { - l.core.log.Errorln("Failed to start AWDL interface") + c.log.Errorln("Failed to start AWDL interface") return err } go func() { for { e := <-l.reconfigure - tcpresponse := make(chan error) - awdlresponse := make(chan error) - l.tcp.reconfigure <- tcpresponse - l.awdl.reconfigure <- awdlresponse - if err := <-tcpresponse; err != nil { + response := make(chan error) + l.tcp.reconfigure <- response + if err := <-response; err != nil { e <- err } - if err := <-awdlresponse; err != nil { + l.awdl.reconfigure <- response + if err := <-response; err != nil { e <- err } e <- nil @@ -92,6 +92,36 @@ func (l *link) init(c *Core) error { return nil } +func (l *link) call(uri string, sintf string) error { + u, err := url.Parse(uri) + if err != nil { + return err + } + pathtokens := strings.Split(strings.Trim(u.Path, "/"), "/") + switch u.Scheme { + case "tcp": + l.tcp.call(u.Host, nil, sintf) + case "socks": + l.tcp.call(pathtokens[0], &u.Host, sintf) + default: + return errors.New("unknown call scheme: " + u.Scheme) + } + return nil +} + +func (l *link) listen(uri string) error { + u, err := url.Parse(uri) + if err != nil { + return err + } + switch u.Scheme { + case "tcp": + return l.tcp.listen(u.Host) + default: + return errors.New("unknown listen scheme: " + u.Scheme) + } +} + func (l *link) create(msgIO linkInterfaceMsgIO, name, linkType, local, remote string, incoming, force bool) (*linkInterface, error) { // Technically anything unique would work for names, but lets pick something human readable, just for debugging intf := linkInterface{ diff --git a/src/yggdrasil/multicast.go b/src/yggdrasil/multicast.go index f416ea26..59f0eea5 100644 --- a/src/yggdrasil/multicast.go +++ b/src/yggdrasil/multicast.go @@ -183,6 +183,6 @@ func (m *multicast) listen() { } addr.Zone = from.Zone saddr := addr.String() - m.core.link.tcp.connect(saddr, addr.Zone) + m.core.link.call("tcp://"+saddr, addr.Zone) } } diff --git a/src/yggdrasil/tcp.go b/src/yggdrasil/tcp.go index c0d568a5..74f14d8a 100644 --- a/src/yggdrasil/tcp.go +++ b/src/yggdrasil/tcp.go @@ -64,16 +64,6 @@ func (t *tcp) getAddr() *net.TCPAddr { return nil } -// Attempts to initiate a connection to the provided address. -func (t *tcp) connect(addr string, intf string) { - t.call(addr, nil, intf) -} - -// Attempst to initiate a connection to the provided address, viathe provided socks proxy address. -func (t *tcp) connectSOCKS(socksaddr, peeraddr string) { - t.call(peeraddr, &socksaddr, "") -} - // Initializes the struct. func (t *tcp) init(l *link) error { t.link = l @@ -104,14 +94,6 @@ func (t *tcp) init(l *link) error { } for _, delete := range deleted { t.link.core.log.Warnln("Removing listener", delete, "not currently implemented") - /*t.mutex.Lock() - if listener, ok := t.listeners[delete]; ok { - listener.Close() - } - if listener, ok := t.listenerstops[delete]; ok { - listener <- true - } - t.mutex.Unlock()*/ } e <- nil } else { @@ -202,7 +184,7 @@ func (t *tcp) isAlreadyCalling(saddr string) bool { // If the dial is successful, it launches the handler. // When finished, it removes the outgoing call, so reconnection attempts can be made later. // This all happens in a separate goroutine that it spawns. -func (t *tcp) call(saddr string, socksaddr *string, sintf string) { +func (t *tcp) call(saddr string, options interface{}, sintf string) { go func() { callname := saddr if sintf != "" { @@ -224,12 +206,13 @@ func (t *tcp) call(saddr string, socksaddr *string, sintf string) { }() var conn net.Conn var err error - if socksaddr != nil { + socksaddr, issocks := options.(string) + if issocks { if sintf != "" { return } var dialer proxy.Dialer - dialer, err = proxy.SOCKS5("tcp", *socksaddr, nil, proxy.Direct) + dialer, err = proxy.SOCKS5("tcp", socksaddr, nil, proxy.Direct) if err != nil { return }