From 094f80f39c2fdd25a1cee2eee7584abecea379b0 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Sun, 22 Oct 2023 15:51:30 +0100 Subject: [PATCH] Fix `RetryPeersNow`, move startup logging, don't set TUN address if not available --- cmd/yggdrasil/main.go | 6 +++++- contrib/mobile/mobile.go | 4 ++++ src/core/core.go | 14 ++++++++------ src/tun/tun.go | 7 +++++-- src/tun/tun_bsd.go | 5 ++++- src/tun/tun_darwin.go | 5 ++++- src/tun/tun_linux.go | 5 ++++- src/tun/tun_other.go | 5 ++++- src/tun/tun_windows.go | 8 +++++--- 9 files changed, 43 insertions(+), 16 deletions(-) diff --git a/cmd/yggdrasil/main.go b/cmd/yggdrasil/main.go index 2aef529d..64cc6ae9 100644 --- a/cmd/yggdrasil/main.go +++ b/cmd/yggdrasil/main.go @@ -67,7 +67,7 @@ func main() { case "syslog": if syslogger, err := gsyslog.NewLogger(gsyslog.LOG_NOTICE, "DAEMON", version.BuildName()); err == nil { - logger = log.New(syslogger, "", log.Flags() &^ (log.Ldate | log.Ltime)) + logger = log.New(syslogger, "", log.Flags()&^(log.Ldate|log.Ltime)) } default: @@ -205,6 +205,10 @@ func main() { if n.core, err = core.New(cfg.Certificate, logger, options...); err != nil { panic(err) } + address, subnet := n.core.Address(), n.core.Subnet() + logger.Infof("Your public key is %s", hex.EncodeToString(n.core.PublicKey())) + logger.Infof("Your IPv6 address is %s", address.String()) + logger.Infof("Your IPv6 subnet is %s", subnet.String()) } // Setup the admin socket. diff --git a/contrib/mobile/mobile.go b/contrib/mobile/mobile.go index 31e2f0af..3e7a5f21 100644 --- a/contrib/mobile/mobile.go +++ b/contrib/mobile/mobile.go @@ -78,6 +78,10 @@ func (m *Yggdrasil) StartJSON(configjson []byte) error { if err != nil { panic(err) } + address, subnet := m.core.Address(), m.core.Subnet() + logger.Infof("Your public key is %s", hex.EncodeToString(m.core.PublicKey())) + logger.Infof("Your IPv6 address is %s", address.String()) + logger.Infof("Your IPv6 subnet is %s", subnet.String()) } // Setup the multicast module. diff --git a/src/core/core.go b/src/core/core.go index e641c8cc..79c59d5d 100644 --- a/src/core/core.go +++ b/src/core/core.go @@ -4,7 +4,6 @@ import ( "context" "crypto/ed25519" "crypto/tls" - "encoding/hex" "fmt" "io" "net" @@ -104,10 +103,6 @@ func New(cert *tls.Certificate, logger Logger, opts ...SetupOption) (*Core, erro ); err != nil { return nil, fmt.Errorf("error creating encryption: %w", err) } - address, subnet := c.Address(), c.Subnet() - c.log.Infof("Your public key is %s", hex.EncodeToString(c.public)) - c.log.Infof("Your IPv6 address is %s", address.String()) - c.log.Infof("Your IPv6 subnet is %s", subnet.String()) c.proto.init(c) if err := c.links.init(c); err != nil { return nil, fmt.Errorf("error initialising links: %w", err) @@ -140,7 +135,14 @@ func New(cert *tls.Certificate, logger Logger, opts ...SetupOption) (*Core, erro } func (c *Core) RetryPeersNow() { - // TODO: figure out a way to retrigger peer connections. + phony.Block(&c.links, func() { + for _, l := range c.links._links { + select { + case l.kick <- struct{}{}: + default: + } + } + }) } // Stop shuts down the Yggdrasil node. diff --git a/src/tun/tun.go b/src/tun/tun.go index cca3af5b..83c82670 100644 --- a/src/tun/tun.go +++ b/src/tun/tun.go @@ -44,7 +44,7 @@ type TunAdapter struct { isOpen bool isEnabled bool // Used by the writer to drop sessionTraffic if not enabled config struct { - fd int32 + fd int32 name InterfaceName mtu InterfaceMTU } @@ -116,7 +116,10 @@ func (tun *TunAdapter) _start() error { tun.addr = tun.rwc.Address() tun.subnet = tun.rwc.Subnet() prefix := address.GetPrefix() - addr := fmt.Sprintf("%s/%d", net.IP(tun.addr[:]).String(), 8*len(prefix[:])-1) + var addr string + if tun.addr.IsValid() { + addr = fmt.Sprintf("%s/%d", net.IP(tun.addr[:]).String(), 8*len(prefix[:])-1) + } if tun.config.name == "none" || tun.config.name == "dummy" { tun.log.Debugln("Not starting TUN as ifname is none or dummy") tun.isEnabled = false diff --git a/src/tun/tun_bsd.go b/src/tun/tun_bsd.go index 3910ccef..5f42f52b 100644 --- a/src/tun/tun_bsd.go +++ b/src/tun/tun_bsd.go @@ -86,7 +86,10 @@ func (tun *TunAdapter) setup(ifname string, addr string, mtu uint64) error { } else { tun.mtu = 0 } - return tun.setupAddress(addr) + if addr != "" { + return tun.setupAddress(addr) + } + return nil } // Configures the "utun" adapter from an existing file descriptor. diff --git a/src/tun/tun_darwin.go b/src/tun/tun_darwin.go index b8e32a2f..8fde93a1 100644 --- a/src/tun/tun_darwin.go +++ b/src/tun/tun_darwin.go @@ -32,7 +32,10 @@ func (tun *TunAdapter) setup(ifname string, addr string, mtu uint64) error { } else { tun.mtu = 0 } - return tun.setupAddress(addr) + if addr != "" { + return tun.setupAddress(addr) + } + return nil } // Configures the "utun" adapter from an existing file descriptor. diff --git a/src/tun/tun_linux.go b/src/tun/tun_linux.go index d87552b7..9cf21446 100644 --- a/src/tun/tun_linux.go +++ b/src/tun/tun_linux.go @@ -27,7 +27,10 @@ func (tun *TunAdapter) setup(ifname string, addr string, mtu uint64) error { } else { tun.mtu = 0 } - return tun.setupAddress(addr) + if addr != "" { + return tun.setupAddress(addr) + } + return nil } // Configures the "utun" adapter from an existing file descriptor. diff --git a/src/tun/tun_other.go b/src/tun/tun_other.go index 075ccfe1..bf8ac892 100644 --- a/src/tun/tun_other.go +++ b/src/tun/tun_other.go @@ -24,7 +24,10 @@ func (tun *TunAdapter) setup(ifname string, addr string, mtu uint64) error { } else { tun.mtu = 0 } - return tun.setupAddress(addr) + if addr != "" { + return tun.setupAddress(addr) + } + return nil } // Configures the "utun" adapter from an existing file descriptor. diff --git a/src/tun/tun_windows.go b/src/tun/tun_windows.go index 10fb5f1a..19d9248c 100644 --- a/src/tun/tun_windows.go +++ b/src/tun/tun_windows.go @@ -35,9 +35,11 @@ func (tun *TunAdapter) setup(ifname string, addr string, mtu uint64) error { return err } tun.iface = iface - if err = tun.setupAddress(addr); err != nil { - tun.log.Errorln("Failed to set up TUN address:", err) - return err + if addr != "" { + if err = tun.setupAddress(addr); err != nil { + tun.log.Errorln("Failed to set up TUN address:", err) + return err + } } if err = tun.setupMTU(getSupportedMTU(mtu)); err != nil { tun.log.Errorln("Failed to set up TUN MTU:", err)