From bc09348f5a59d59622eb246a282b9fbff8214755 Mon Sep 17 00:00:00 2001 From: Zoltan Papp Date: Mon, 15 Jul 2024 14:45:18 +0200 Subject: [PATCH] Add logging option for wg device (#2271) --- iface/tun_android.go | 2 +- iface/tun_darwin.go | 2 +- iface/tun_ios.go | 2 +- iface/tun_netstack.go | 2 +- iface/tun_usp_unix.go | 2 +- iface/tun_windows.go | 3 ++- iface/wg_log.go | 15 +++++++++++++++ 7 files changed, 22 insertions(+), 6 deletions(-) create mode 100644 iface/wg_log.go diff --git a/iface/tun_android.go b/iface/tun_android.go index dc6abea36..504993094 100644 --- a/iface/tun_android.go +++ b/iface/tun_android.go @@ -64,7 +64,7 @@ func (t *wgTunDevice) Create(routes []string, dns string, searchDomains []string t.wrapper = newDeviceWrapper(tunDevice) log.Debugf("attaching to interface %v", name) - t.device = device.NewDevice(t.wrapper, t.iceBind, device.NewLogger(device.LogLevelSilent, "[wiretrustee] ")) + t.device = device.NewDevice(t.wrapper, t.iceBind, device.NewLogger(wgLogLevel(), "[wiretrustee] ")) // without this property mobile devices can discover remote endpoints if the configured one was wrong. // this helps with support for the older NetBird clients that had a hardcoded direct mode // t.device.DisableSomeRoamingForBrokenMobileSemantics() diff --git a/iface/tun_darwin.go b/iface/tun_darwin.go index 7d684f52e..364e5dfad 100644 --- a/iface/tun_darwin.go +++ b/iface/tun_darwin.go @@ -49,7 +49,7 @@ func (t *tunDevice) Create() (wgConfigurer, error) { t.device = device.NewDevice( t.wrapper, t.iceBind, - device.NewLogger(device.LogLevelSilent, "[netbird] "), + device.NewLogger(wgLogLevel(), "[netbird] "), ) err = t.assignAddr() diff --git a/iface/tun_ios.go b/iface/tun_ios.go index 83e26e08d..6d53cc333 100644 --- a/iface/tun_ios.go +++ b/iface/tun_ios.go @@ -64,7 +64,7 @@ func (t *tunDevice) Create() (wgConfigurer, error) { t.wrapper = newDeviceWrapper(tunDevice) log.Debug("Attaching to interface") - t.device = device.NewDevice(t.wrapper, t.iceBind, device.NewLogger(device.LogLevelSilent, "[wiretrustee] ")) + t.device = device.NewDevice(t.wrapper, t.iceBind, device.NewLogger(wgLogLevel(), "[wiretrustee] ")) // without this property mobile devices can discover remote endpoints if the configured one was wrong. // this helps with support for the older NetBird clients that had a hardcoded direct mode // t.device.DisableSomeRoamingForBrokenMobileSemantics() diff --git a/iface/tun_netstack.go b/iface/tun_netstack.go index beb3acc3f..df2f75c45 100644 --- a/iface/tun_netstack.go +++ b/iface/tun_netstack.go @@ -54,7 +54,7 @@ func (t *tunNetstackDevice) Create() (wgConfigurer, error) { t.device = device.NewDevice( t.wrapper, t.iceBind, - device.NewLogger(device.LogLevelSilent, "[netbird] "), + device.NewLogger(wgLogLevel(), "[netbird] "), ) t.configurer = newWGUSPConfigurer(t.device, t.name) diff --git a/iface/tun_usp_unix.go b/iface/tun_usp_unix.go index b18794b25..814c9ca89 100644 --- a/iface/tun_usp_unix.go +++ b/iface/tun_usp_unix.go @@ -57,7 +57,7 @@ func (t *tunUSPDevice) Create() (wgConfigurer, error) { t.device = device.NewDevice( t.wrapper, t.iceBind, - device.NewLogger(device.LogLevelSilent, "[netbird] "), + device.NewLogger(wgLogLevel(), "[netbird] "), ) err = t.assignAddr() diff --git a/iface/tun_windows.go b/iface/tun_windows.go index 5c77f1d16..0d658059f 100644 --- a/iface/tun_windows.go +++ b/iface/tun_windows.go @@ -41,6 +41,7 @@ func newTunDevice(name string, address WGAddress, port int, key string, mtu int, } func (t *tunDevice) Create() (wgConfigurer, error) { + log.Info("create tun interface") tunDevice, err := tun.CreateTUN(t.name, t.mtu) if err != nil { return nil, err @@ -52,7 +53,7 @@ func (t *tunDevice) Create() (wgConfigurer, error) { t.device = device.NewDevice( t.wrapper, t.iceBind, - device.NewLogger(device.LogLevelSilent, "[netbird] "), + device.NewLogger(wgLogLevel(), "[netbird] "), ) luid := winipcfg.LUID(t.nativeTunDevice.LUID()) diff --git a/iface/wg_log.go b/iface/wg_log.go new file mode 100644 index 000000000..b44f6fc0b --- /dev/null +++ b/iface/wg_log.go @@ -0,0 +1,15 @@ +package iface + +import ( + "os" + + "golang.zx2c4.com/wireguard/device" +) + +func wgLogLevel() int { + if os.Getenv("NB_WG_DEBUG") == "true" { + return device.LogLevelVerbose + } else { + return device.LogLevelSilent + } +}