From b68a02aceeea6338f76c283310f3839cdd437e64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zolt=C3=A1n=20Papp?= Date: Thu, 18 Apr 2024 15:59:50 +0200 Subject: [PATCH] Close turn connection Without it the WG can not exit from the read loop --- client/firewall/nftables/manager_linux.go | 2 +- client/internal/engine.go | 3 +++ client/internal/relay/turn.go | 14 ++++++++++++++ iface/bind/bind.go | 1 - iface/tun_usp_linux.go | 2 +- 5 files changed, 19 insertions(+), 3 deletions(-) diff --git a/client/firewall/nftables/manager_linux.go b/client/firewall/nftables/manager_linux.go index 8395fc270..9ee24a695 100644 --- a/client/firewall/nftables/manager_linux.go +++ b/client/firewall/nftables/manager_linux.go @@ -143,7 +143,7 @@ func (m *Manager) AllowNetbird() error { } if rule := m.detectAllowNetbirdRule(rules); rule != nil { - log.Debugf("allow netbird rule already exists: %v", rule) + log.Debugf("allow netbird rule already exists: %#v", rule) return nil } diff --git a/client/internal/engine.go b/client/internal/engine.go index 07ffd4f93..c4170330a 100644 --- a/client/internal/engine.go +++ b/client/internal/engine.go @@ -200,6 +200,7 @@ func NewEngineWithProbes( networkSerial: 0, sshServerFunc: nbssh.DefaultSSHServer, statusRecorder: statusRecorder, + wgProxyFactory: &wgproxy.Factory{}, mgmProbe: mgmProbe, signalProbe: signalProbe, relayProbe: relayProbe, @@ -1157,6 +1158,8 @@ func (e *Engine) close() { log.Errorf("failed closing ebpf proxy: %s", err) } + e.turnRelay.Close() + // stop/restore DNS first so dbus and friends don't complain because of a missing interface if e.dnsServer != nil { e.dnsServer.Stop() diff --git a/client/internal/relay/turn.go b/client/internal/relay/turn.go index 771593282..ab32dcef9 100644 --- a/client/internal/relay/turn.go +++ b/client/internal/relay/turn.go @@ -87,6 +87,20 @@ func (r *PermanentTurn) RelayConn() net.PacketConn { return r.relayConn } +func (r *PermanentTurn) Close() { + r.turnClient.Close() + + err := r.relayConn.Close() + if err != nil { + log.Errorf("failed to close relayConn: %s", err.Error()) + } + + err = r.stunConn.Close() + if err != nil { + log.Errorf("failed to close stunConn: %s", err.Error()) + } +} + func (r *PermanentTurn) discoverPublicIP() (*net.UDPAddr, error) { addr, err := r.turnClient.SendBindingRequest() if err != nil { diff --git a/iface/bind/bind.go b/iface/bind/bind.go index 79eae1a31..c0aa9023d 100644 --- a/iface/bind/bind.go +++ b/iface/bind/bind.go @@ -76,7 +76,6 @@ func (s *ICEBind) createIPv4ReceiverFn(ipv4MsgsPool *sync.Pool, pc *ipv4.PacketC msg := &(*msgs)[0] msg.N, msg.Addr, err = netConn.ReadFrom(msg.Buffers[0]) if err != nil { - log.Debugf("read err from turn server: %v", err) return 0, err } log.Debugf("----msg address is: %s, size: %d", msg.Addr.String(), msg.N) diff --git a/iface/tun_usp_linux.go b/iface/tun_usp_linux.go index 8a23ef4f0..d97eee052 100644 --- a/iface/tun_usp_linux.go +++ b/iface/tun_usp_linux.go @@ -54,7 +54,7 @@ func (t *tunUSPDevice) Create() (wgConfigurer, error) { t.device = device.NewDevice( t.wrapper, t.iceBind, - device.NewLogger(device.LogLevelVerbose, "[netbird] "), + device.NewLogger(device.LogLevelError, "[netbird] "), ) err = t.assignAddr()