diff --git a/iface/iface.go b/iface/iface.go index e4b4dd013..fca1b727a 100644 --- a/iface/iface.go +++ b/iface/iface.go @@ -2,6 +2,10 @@ package iface import ( "fmt" + log "github.com/sirupsen/logrus" + "golang.zx2c4.com/wireguard/conn" + "golang.zx2c4.com/wireguard/device" + "golang.zx2c4.com/wireguard/tun" "net" "os" "runtime" @@ -92,3 +96,49 @@ func (w *WGIface) Close() error { return nil } + +func (w *WGIface) CreateNew(bind conn.Bind) error { + w.mu.Lock() + defer w.mu.Unlock() + + return w.createWithUserspaceNew(bind) +} + +func (w *WGIface) createWithUserspaceNew(bind conn.Bind) error { + tunIface, err := tun.CreateTUN(w.Name, w.MTU) + if err != nil { + return err + } + + w.Interface = tunIface + + // We need to create a wireguard-go device and listen to configuration requests + tunDevice := device.NewDevice(tunIface, bind, device.NewLogger(device.LogLevelSilent, "[wiretrustee] ")) + err = tunDevice.Up() + if err != nil { + return err + } + uapi, err := getUAPI(w.Name) + if err != nil { + return err + } + + go func() { + for { + uapiConn, uapiErr := uapi.Accept() + if uapiErr != nil { + log.Traceln("uapi Accept failed with error: ", uapiErr) + continue + } + go tunDevice.IpcHandle(uapiConn) + } + }() + + log.Debugln("UAPI listener started") + + err = w.assignAddr() + if err != nil { + return err + } + return nil +} diff --git a/iface/iface_linux.go b/iface/iface_linux.go index d497cb96b..e95ee4546 100644 --- a/iface/iface_linux.go +++ b/iface/iface_linux.go @@ -2,7 +2,6 @@ package iface import ( "errors" - "golang.zx2c4.com/wireguard/conn" "math" "os" "syscall" @@ -33,12 +32,6 @@ func WireguardModExists() bool { return errors.Is(err, syscall.EINVAL) } -func (w *WGIface) CreateNew(bind conn.Bind) error { - w.mu.Lock() - defer w.mu.Unlock() - - return w.createWithUserspaceNew(bind) -} // Create creates a new Wireguard interface, sets a given IP and brings it up. // Will reuse an existing one. diff --git a/iface/iface_unix.go b/iface/iface_unix.go index 719114da5..66d316997 100644 --- a/iface/iface_unix.go +++ b/iface/iface_unix.go @@ -12,45 +12,6 @@ import ( "net" ) -func (w *WGIface) createWithUserspaceNew(bind conn.Bind) error { - tunIface, err := tun.CreateTUN(w.Name, w.MTU) - if err != nil { - return err - } - - w.Interface = tunIface - - // We need to create a wireguard-go device and listen to configuration requests - tunDevice := device.NewDevice(tunIface, bind, device.NewLogger(device.LogLevelSilent, "[wiretrustee] ")) - err = tunDevice.Up() - if err != nil { - return err - } - uapi, err := getUAPI(w.Name) - if err != nil { - return err - } - - go func() { - for { - uapiConn, uapiErr := uapi.Accept() - if uapiErr != nil { - log.Traceln("uapi Accept failed with error: ", uapiErr) - continue - } - go tunDevice.IpcHandle(uapiConn) - } - }() - - log.Debugln("UAPI listener started") - - err = w.assignAddr() - if err != nil { - return err - } - return nil -} - // createWithUserspace Creates a new Wireguard interface, using wireguard-go userspace implementation func (w *WGIface) createWithUserspace() error { diff --git a/iface/iface_windows.go b/iface/iface_windows.go index 4c6d4a4ab..f54dd65bb 100644 --- a/iface/iface_windows.go +++ b/iface/iface_windows.go @@ -4,6 +4,7 @@ import ( "fmt" log "github.com/sirupsen/logrus" "golang.org/x/sys/windows" + "golang.zx2c4.com/wireguard/ipc" "golang.zx2c4.com/wireguard/windows/driver" "golang.zx2c4.com/wireguard/windows/tunnel/winipcfg" "net" @@ -62,3 +63,8 @@ func (w *WGIface) UpdateAddr(newAddr string) error { func WireguardModExists() bool { return false } + +// getUAPI returns a Listener +func getUAPI(iface string) (net.Listener, error) { + return ipc.UAPIListen(iface) +}