Split create Interface based on OS

This commit is contained in:
mlsmaycon 2021-06-06 15:48:57 +02:00
parent 59360519d6
commit 6e4c232ff2
3 changed files with 24 additions and 3 deletions

View File

@ -22,8 +22,7 @@ var tunIface tun.Device
// Will reuse an existing one. // Will reuse an existing one.
func Create(iface string, address string) error { func Create(iface string, address string) error {
var err error var err error
tunIface, err = createIface(iface, defaultMTU)
tunIface, err = tun.CreateTUN(iface, defaultMTU)
if err != nil { if err != nil {
return err return err
} }

View File

@ -4,9 +4,15 @@ package iface
import ( import (
"golang.zx2c4.com/wireguard/ipc" "golang.zx2c4.com/wireguard/ipc"
"golang.zx2c4.com/wireguard/tun"
"net" "net"
) )
// createIface creates a tun device
func createIface(iface string, defaultMTU int) (tun.Device, error) {
return tun.CreateTUN(iface, defaultMTU)
}
// getUAPI returns a Listener // getUAPI returns a Listener
func getUAPI(iface string) (net.Listener, error) { func getUAPI(iface string) (net.Listener, error) {
tunSock, err := ipc.UAPIOpen(iface) tunSock, err := ipc.UAPIOpen(iface)

View File

@ -6,7 +6,7 @@ import (
"golang.zx2c4.com/wireguard/ipc" "golang.zx2c4.com/wireguard/ipc"
"golang.zx2c4.com/wireguard/tun" "golang.zx2c4.com/wireguard/tun"
"golang.zx2c4.com/wireguard/windows/elevate"
"golang.zx2c4.com/wireguard/windows/tunnel/winipcfg" "golang.zx2c4.com/wireguard/windows/tunnel/winipcfg"
) )
@ -32,6 +32,22 @@ func assignAddr(address string, tunDevice tun.Device) error {
return nil 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 // getUAPI returns a Listener
func getUAPI(iface string) (net.Listener, error) { func getUAPI(iface string) (net.Listener, error) {
return ipc.UAPIListen(iface) return ipc.UAPIListen(iface)