From d6c2b46019bfefacf0436a37e66b7d982b11b5d4 Mon Sep 17 00:00:00 2001 From: braginini Date: Mon, 27 Mar 2023 19:09:01 +0200 Subject: [PATCH] Make it work --- client/internal/engine.go | 48 ++++++++++----------------------------- go.mod | 4 ++-- iface/iface.go | 6 +++++ iface/tun_android.go | 14 +++++++----- iface/tun_linux.go | 4 ++-- iface/tun_unix.go | 4 ++-- iface/tun_windows.go | 3 ++- 7 files changed, 34 insertions(+), 49 deletions(-) diff --git a/client/internal/engine.go b/client/internal/engine.go index a7fa82c11..f64c820b4 100644 --- a/client/internal/engine.go +++ b/client/internal/engine.go @@ -3,6 +3,7 @@ package internal import ( "context" "fmt" + "github.com/netbirdio/netbird/iface/bind" "math/rand" "net" "net/netip" @@ -102,8 +103,7 @@ type Engine struct { wgInterface *iface.WGIface - udpMux ice.UDPMux - udpMuxSrflx ice.UniversalUDPMux + udpMux *bind.UniversalUDPMuxDefault udpMuxConn *net.UDPConn udpMuxConnSrflx *net.UDPConn @@ -184,35 +184,10 @@ func (e *Engine) Start() error { return err } - networkName := "udp" - if e.config.DisableIPv6Discovery { - networkName = "udp4" - } - - transportNet, err := e.newStdNet() + /*transportNet, err := e.newStdNet() if err != nil { log.Warnf("failed to create pion's stdnet: %s", err) - } - - e.udpMuxConn, err = net.ListenUDP(networkName, &net.UDPAddr{Port: e.config.UDPMuxPort}) - if err != nil { - log.Errorf("failed listening on UDP port %d: [%s]", e.config.UDPMuxPort, err.Error()) - e.close() - return err - } - udpMuxParams := ice.UDPMuxParams{ - UDPConn: e.udpMuxConn, - Net: transportNet, - } - e.udpMux = ice.NewUDPMuxDefault(udpMuxParams) - - e.udpMuxConnSrflx, err = net.ListenUDP(networkName, &net.UDPAddr{Port: e.config.UDPMuxSrflxPort}) - if err != nil { - log.Errorf("failed listening on UDP port %d: [%s]", e.config.UDPMuxSrflxPort, err.Error()) - e.close() - return err - } - e.udpMuxSrflx = ice.NewUniversalUDPMuxDefault(ice.UniversalUDPMuxParams{UDPConn: e.udpMuxConnSrflx, Net: transportNet}) + }*/ err = e.wgInterface.Create() if err != nil { @@ -228,6 +203,13 @@ func (e *Engine) Start() error { return err } + iceBind := e.wgInterface.GetBind() + e.udpMux, err = iceBind.GetICEMux() + if err != nil { + e.close() + return err + } + e.routeManager = routemanager.NewManager(e.ctx, e.config.WgPrivateKey.PublicKey().String(), e.wgInterface, e.statusRecorder) if e.dnsServer == nil { @@ -818,7 +800,7 @@ func (e Engine) createPeerConn(pubKey string, allowedIPs string) (*peer.Conn, er DisableIPv6Discovery: e.config.DisableIPv6Discovery, Timeout: timeout, UDPMux: e.udpMux, - UDPMuxSrflx: e.udpMuxSrflx, + UDPMuxSrflx: e.udpMux, ProxyConfig: proxyConfig, LocalWgPort: e.config.WgPort, NATExternalIPs: e.parseNATExternalIPMappings(), @@ -1006,12 +988,6 @@ func (e *Engine) close() { } } - if e.udpMuxSrflx != nil { - if err := e.udpMuxSrflx.Close(); err != nil { - log.Debugf("close server reflexive udp mux: %v", err) - } - } - if e.udpMuxConn != nil { if err := e.udpMuxConn.Close(); err != nil { log.Debugf("close udp mux connection: %v", err) diff --git a/go.mod b/go.mod index d6518467f..8b7c266be 100644 --- a/go.mod +++ b/go.mod @@ -47,6 +47,8 @@ require ( github.com/mitchellh/hashstructure/v2 v2.0.2 github.com/open-policy-agent/opa v0.49.0 github.com/patrickmn/go-cache v2.1.0+incompatible + github.com/pion/logging v0.2.2 + github.com/pion/stun v0.4.0 github.com/pion/transport/v2 v2.0.2 github.com/prometheus/client_golang v1.14.0 github.com/rs/xid v1.3.0 @@ -102,10 +104,8 @@ require ( github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c // indirect github.com/pegasus-kv/thrift v0.13.0 // indirect github.com/pion/dtls/v2 v2.2.6 // indirect - github.com/pion/logging v0.2.2 // indirect github.com/pion/mdns v0.0.7 // indirect github.com/pion/randutil v0.1.0 // indirect - github.com/pion/stun v0.4.0 // indirect github.com/pion/turn/v2 v2.1.0 // indirect github.com/pion/udp/v2 v2.0.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect diff --git a/iface/iface.go b/iface/iface.go index 131558e77..d1ea561e0 100644 --- a/iface/iface.go +++ b/iface/iface.go @@ -1,6 +1,7 @@ package iface import ( + "github.com/netbirdio/netbird/iface/bind" "net" "sync" "time" @@ -21,6 +22,11 @@ type WGIface struct { mu sync.Mutex } +// GetBind returns a userspace implementation of WireGuard Bind interface +func (w *WGIface) GetBind() *bind.ICEBind { + return w.tun.iceBind +} + // Create creates a new Wireguard interface, sets a given IP and brings it up. // Will reuse an existing one. func (w *WGIface) Create() error { diff --git a/iface/tun_android.go b/iface/tun_android.go index da258e8ec..44e7f51c3 100644 --- a/iface/tun_android.go +++ b/iface/tun_android.go @@ -1,11 +1,11 @@ package iface import ( + "github.com/netbirdio/netbird/iface/bind" "net" log "github.com/sirupsen/logrus" "golang.org/x/sys/unix" - "golang.zx2c4.com/wireguard/conn" "golang.zx2c4.com/wireguard/device" "golang.zx2c4.com/wireguard/ipc" "golang.zx2c4.com/wireguard/tun" @@ -16,10 +16,11 @@ type tunDevice struct { mtu int tunAdapter TunAdapter - fd int - name string - device *device.Device - uapi net.Listener + fd int + name string + device *device.Device + uapi net.Listener + iceBind *bind.ICEBind } func newTunDevice(address WGAddress, mtu int, tunAdapter TunAdapter) *tunDevice { @@ -27,6 +28,7 @@ func newTunDevice(address WGAddress, mtu int, tunAdapter TunAdapter) *tunDevice address: address, mtu: mtu, tunAdapter: tunAdapter, + iceBind: &bind.ICEBind{}, } } @@ -46,7 +48,7 @@ func (t *tunDevice) Create() error { t.name = name log.Debugf("attaching to interface %v", name) - t.device = device.NewDevice(tunDevice, conn.NewStdNetBind(), device.NewLogger(device.LogLevelSilent, "[wiretrustee] ")) + t.device = device.NewDevice(tunDevice, t.iceBind, device.NewLogger(device.LogLevelSilent, "[wiretrustee] ")) t.device.DisableSomeRoamingForBrokenMobileSemantics() log.Debugf("create uapi") diff --git a/iface/tun_linux.go b/iface/tun_linux.go index 9bc7da754..9f8db54b5 100644 --- a/iface/tun_linux.go +++ b/iface/tun_linux.go @@ -11,10 +11,10 @@ import ( ) func (c *tunDevice) Create() error { - if WireguardModuleIsLoaded() { + /*if WireguardModuleIsLoaded() { log.Info("using kernel WireGuard") return c.createWithKernel() - } + }*/ if !tunModuleIsLoaded() { return fmt.Errorf("couldn't check or load tun module") diff --git a/iface/tun_unix.go b/iface/tun_unix.go index 991e7c29b..9a57ad8e3 100644 --- a/iface/tun_unix.go +++ b/iface/tun_unix.go @@ -8,7 +8,6 @@ import ( "os" log "github.com/sirupsen/logrus" - "golang.zx2c4.com/wireguard/conn" "golang.zx2c4.com/wireguard/device" "golang.zx2c4.com/wireguard/ipc" "golang.zx2c4.com/wireguard/tun" @@ -27,6 +26,7 @@ func newTunDevice(name string, address WGAddress, mtu int) *tunDevice { name: name, address: address, mtu: mtu, + iceBind: &bind.ICEBind{}, } } @@ -71,7 +71,7 @@ func (c *tunDevice) createWithUserspace() (NetInterface, error) { } // We need to create a wireguard-go device and listen to configuration requests - tunDevice := device.NewDevice(tunIface, conn.NewDefaultBind(), device.NewLogger(device.LogLevelSilent, "[wiretrustee] ")) + tunDevice := device.NewDevice(tunIface, c.iceBind, device.NewLogger(device.LogLevelSilent, "[wiretrustee] ")) err = tunDevice.Up() if err != nil { return tunIface, err diff --git a/iface/tun_windows.go b/iface/tun_windows.go index d25b6fc9c..cd4589df9 100644 --- a/iface/tun_windows.go +++ b/iface/tun_windows.go @@ -13,10 +13,11 @@ type tunDevice struct { name string address WGAddress netInterface NetInterface + iceBind *bind.ICEBind } func newTunDevice(name string, address WGAddress, mtu int) *tunDevice { - return &tunDevice{name: name, address: address} + return &tunDevice{name: name, address: address, iceBind: &bind.ICEBind{}} } func (c *tunDevice) Create() error {