5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-11-09 16:20:26 +00:00

use url.URL in place of string for most internal listen/peer address handling

This commit is contained in:
Arceliar 2021-05-23 20:34:13 -05:00
parent 58af92812e
commit 70c5b06286
4 changed files with 61 additions and 46 deletions

View File

@ -3,9 +3,10 @@ package core
import ( import (
"crypto/ed25519" "crypto/ed25519"
//"encoding/hex" //"encoding/hex"
"errors" //"errors"
//"fmt" //"fmt"
"net" "net"
"net/url"
//"sort" //"sort"
//"time" //"time"
@ -150,6 +151,7 @@ func (c *Core) SetLogger(log *log.Logger) {
// socks://a.b.c.d:e/f.g.h.i:j // socks://a.b.c.d:e/f.g.h.i:j
// This adds the peer to the peer list, so that they will be called again if the // This adds the peer to the peer list, so that they will be called again if the
// connection drops. // connection drops.
/*
func (c *Core) AddPeer(addr string, sintf string) error { func (c *Core) AddPeer(addr string, sintf string) error {
if err := c.CallPeer(addr, sintf); err != nil { if err := c.CallPeer(addr, sintf); err != nil {
// TODO: We maybe want this to write the peer to the persistent // TODO: We maybe want this to write the peer to the persistent
@ -184,6 +186,7 @@ func (c *Core) AddPeer(addr string, sintf string) error {
} }
return nil return nil
} }
*/
/* /*
func (c *Core) RemovePeer(addr string, sintf string) error { func (c *Core) RemovePeer(addr string, sintf string) error {
@ -224,5 +227,9 @@ func (c *Core) RemovePeer(addr string, sintf string) error {
// This does not add the peer to the peer list, so if the connection drops, the // This does not add the peer to the peer list, so if the connection drops, the
// peer will not be called again automatically. // peer will not be called again automatically.
func (c *Core) CallPeer(addr string, sintf string) error { func (c *Core) CallPeer(addr string, sintf string) error {
return c.links.call(addr, sintf) u, err := url.Parse(addr)
if err != nil {
return err
}
return c.links.call(u, sintf)
} }

View File

@ -68,7 +68,7 @@ func (c *Core) _addPeerLoop() {
// Add peers from the Peers section // Add peers from the Peers section
for _, peer := range current.Peers { for _, peer := range current.Peers {
go func(peer, intf string) { go func(peer string, intf string) {
if err := c.CallPeer(peer, intf); err != nil { if err := c.CallPeer(peer, intf); err != nil {
c.log.Errorln("Failed to add peer:", err) c.log.Errorln("Failed to add peer:", err)
} }
@ -78,7 +78,7 @@ func (c *Core) _addPeerLoop() {
// Add peers from the InterfacePeers section // Add peers from the InterfacePeers section
for intf, intfpeers := range current.InterfacePeers { for intf, intfpeers := range current.InterfacePeers {
for _, peer := range intfpeers { for _, peer := range intfpeers {
go func(peer, intf string) { go func(peer string, intf string) {
if err := c.CallPeer(peer, intf); err != nil { if err := c.CallPeer(peer, intf); err != nil {
c.log.Errorln("Failed to add peer:", err) c.log.Errorln("Failed to add peer:", err)
} }

View File

@ -73,11 +73,11 @@ func (l *links) reconfigure() {
l.tcp.reconfigure() l.tcp.reconfigure()
} }
func (l *links) call(uri string, sintf string) error { func (l *links) call(u *url.URL, sintf string) error {
u, err := url.Parse(uri) //u, err := url.Parse(uri)
if err != nil { //if err != nil {
return fmt.Errorf("peer %s is not correctly formatted (%s)", uri, err) // return fmt.Errorf("peer %s is not correctly formatted (%s)", uri, err)
} //}
pathtokens := strings.Split(strings.Trim(u.Path, "/"), "/") pathtokens := strings.Split(strings.Trim(u.Path, "/"), "/")
tcpOpts := tcpOptions{} tcpOpts := tcpOptions{}
if pubkeys, ok := u.Query()["ed25519"]; ok && len(pubkeys) > 0 { if pubkeys, ok := u.Query()["ed25519"]; ok && len(pubkeys) > 0 {

View File

@ -19,6 +19,7 @@ import (
"fmt" "fmt"
"math/rand" "math/rand"
"net" "net"
"net/url"
"strings" "strings"
"sync" "sync"
"time" "time"
@ -26,7 +27,7 @@ import (
"golang.org/x/net/proxy" "golang.org/x/net/proxy"
"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/util"
) )
const default_timeout = 6 * time.Second const default_timeout = 6 * time.Second
@ -107,17 +108,21 @@ func (t *tcp) init(l *links) error {
t.links.core.config.Mutex.RLock() t.links.core.config.Mutex.RLock()
defer t.links.core.config.Mutex.RUnlock() defer t.links.core.config.Mutex.RUnlock()
for _, listenaddr := range t.links.core.config.Current.Listen { for _, listenaddr := range t.links.core.config.Current.Listen {
switch listenaddr[:6] { u, err := url.Parse(listenaddr)
case "tcp://": if err != nil {
if _, err := t.listen(listenaddr[6:], nil); err != nil { t.links.core.log.Errorln("Failed to parse listener: listener", listenaddr, "is not correctly formatted, ignoring")
}
switch u.Scheme {
case "tcp":
if _, err := t.listen(u.Host, nil); err != nil {
return err return err
} }
case "tls://": case "tls":
if _, err := t.listen(listenaddr[6:], t.tls.forListener); err != nil { if _, err := t.listen(u.Host, t.tls.forListener); err != nil {
return err return err
} }
default: default:
t.links.core.log.Errorln("Failed to add listener: listener", listenaddr, "is not correctly formatted, ignoring") t.links.core.log.Errorln("Failed to add listener: listener", u.String(), "is not correctly formatted, ignoring")
} }
} }
@ -135,40 +140,43 @@ func (t *tcp) stop() error {
} }
func (t *tcp) reconfigure() { func (t *tcp) reconfigure() {
t.links.core.config.Mutex.RLock() panic("TODO cleanup obsolete reconfigure() stuff")
added := util.Difference(t.links.core.config.Current.Listen, t.links.core.config.Previous.Listen) /*
deleted := util.Difference(t.links.core.config.Previous.Listen, t.links.core.config.Current.Listen) t.links.core.config.Mutex.RLock()
t.links.core.config.Mutex.RUnlock() added := util.Difference(t.links.core.config.Current.Listen, t.links.core.config.Previous.Listen)
if len(added) > 0 || len(deleted) > 0 { deleted := util.Difference(t.links.core.config.Previous.Listen, t.links.core.config.Current.Listen)
for _, a := range added { t.links.core.config.Mutex.RUnlock()
switch a[:6] { if len(added) > 0 || len(deleted) > 0 {
case "tcp://": for _, a := range added {
if _, err := t.listen(a[6:], nil); err != nil { switch a[:6] {
t.links.core.log.Errorln("Error adding TCP", a[6:], "listener:", err) case "tcp://":
if _, err := t.listen(a[6:], nil); err != nil {
t.links.core.log.Errorln("Error adding TCP", a[6:], "listener:", err)
}
case "tls://":
if _, err := t.listen(a[6:], t.tls.forListener); err != nil {
t.links.core.log.Errorln("Error adding TLS", a[6:], "listener:", err)
}
default:
t.links.core.log.Errorln("Failed to add listener: listener", a, "is not correctly formatted, ignoring")
} }
case "tls://": }
if _, err := t.listen(a[6:], t.tls.forListener); err != nil { for _, d := range deleted {
t.links.core.log.Errorln("Error adding TLS", a[6:], "listener:", err) if d[:6] != "tcp://" && d[:6] != "tls://" {
t.links.core.log.Errorln("Failed to delete listener: listener", d, "is not correctly formatted, ignoring")
continue
}
t.mutex.Lock()
if listener, ok := t.listeners[d[6:]]; ok {
t.mutex.Unlock()
listener.Stop()
t.links.core.log.Infoln("Stopped TCP listener:", d[6:])
} else {
t.mutex.Unlock()
} }
default:
t.links.core.log.Errorln("Failed to add listener: listener", a, "is not correctly formatted, ignoring")
} }
} }
for _, d := range deleted { */
if d[:6] != "tcp://" && d[:6] != "tls://" {
t.links.core.log.Errorln("Failed to delete listener: listener", d, "is not correctly formatted, ignoring")
continue
}
t.mutex.Lock()
if listener, ok := t.listeners[d[6:]]; ok {
t.mutex.Unlock()
listener.Stop()
t.links.core.log.Infoln("Stopped TCP listener:", d[6:])
} else {
t.mutex.Unlock()
}
}
}
} }
func (t *tcp) listen(listenaddr string, upgrade *TcpUpgrade) (*TcpListener, error) { func (t *tcp) listen(listenaddr string, upgrade *TcpUpgrade) (*TcpListener, error) {