From 6e4c232ff290ecdea5071fd489a813b89a611326 Mon Sep 17 00:00:00 2001 From: mlsmaycon Date: Sun, 6 Jun 2021 15:48:57 +0200 Subject: [PATCH] Split create Interface based on OS --- iface/iface.go | 3 +-- iface/iface_unix.go | 6 ++++++ iface/iface_windows.go | 18 +++++++++++++++++- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/iface/iface.go b/iface/iface.go index eaf6ee541..3f5763f0b 100644 --- a/iface/iface.go +++ b/iface/iface.go @@ -22,8 +22,7 @@ var tunIface tun.Device // Will reuse an existing one. func Create(iface string, address string) error { var err error - - tunIface, err = tun.CreateTUN(iface, defaultMTU) + tunIface, err = createIface(iface, defaultMTU) if err != nil { return err } diff --git a/iface/iface_unix.go b/iface/iface_unix.go index ccdfa69cf..7285dbec0 100644 --- a/iface/iface_unix.go +++ b/iface/iface_unix.go @@ -4,9 +4,15 @@ package iface import ( "golang.zx2c4.com/wireguard/ipc" + "golang.zx2c4.com/wireguard/tun" "net" ) +// createIface creates a tun device +func createIface(iface string, defaultMTU int) (tun.Device, error) { + return tun.CreateTUN(iface, defaultMTU) +} + // getUAPI returns a Listener func getUAPI(iface string) (net.Listener, error) { tunSock, err := ipc.UAPIOpen(iface) diff --git a/iface/iface_windows.go b/iface/iface_windows.go index 2d3117f1b..053aa06b9 100644 --- a/iface/iface_windows.go +++ b/iface/iface_windows.go @@ -6,7 +6,7 @@ import ( "golang.zx2c4.com/wireguard/ipc" "golang.zx2c4.com/wireguard/tun" - + "golang.zx2c4.com/wireguard/windows/elevate" "golang.zx2c4.com/wireguard/windows/tunnel/winipcfg" ) @@ -32,6 +32,22 @@ func assignAddr(address string, tunDevice tun.Device) error { return nil } +// createIface creates a tun device +func createIface(iface string, defaultMTU int) (tun.Device, error) { + + var tunDevice tun.Device + err := elevate.DoAsSystem(func() error { + var err error + tunDevice, err = tun.CreateTUNWithRequestedGUID(iface, &windows.GUID{12, 12, 12, [8]byte{12, 12, 12, 12, 12, 12, 12, 12}}, defaultMTU) + return err + }) + if err != nil { + log.Errorln("Failed to create the tunnel device: ", err) + return nil, err + } + return tunDevice, err +} + // getUAPI returns a Listener func getUAPI(iface string) (net.Listener, error) { return ipc.UAPIListen(iface)