From 5844c690d9b8cb810b1eebcb392f77682468d540 Mon Sep 17 00:00:00 2001 From: braginini Date: Wed, 14 Apr 2021 14:33:11 +0200 Subject: [PATCH] refactor: simplify iface package --- iface/{wgctl.go => cfg.go} | 52 ++++++++++++++++++ iface/{iface_darwin.go => darwin.go} | 0 iface/iface.go | 82 ---------------------------- iface/{iface_linux.go => linux.go} | 0 4 files changed, 52 insertions(+), 82 deletions(-) rename iface/{wgctl.go => cfg.go} (80%) rename iface/{iface_darwin.go => darwin.go} (100%) delete mode 100644 iface/iface.go rename iface/{iface_linux.go => linux.go} (100%) diff --git a/iface/wgctl.go b/iface/cfg.go similarity index 80% rename from iface/wgctl.go rename to iface/cfg.go index ef9e3a568..8a6dee41f 100644 --- a/iface/wgctl.go +++ b/iface/cfg.go @@ -3,12 +3,64 @@ package iface import ( log "github.com/sirupsen/logrus" "github.com/vishvananda/netlink" + "golang.zx2c4.com/wireguard/device" + "golang.zx2c4.com/wireguard/ipc" + "golang.zx2c4.com/wireguard/tun" "golang.zx2c4.com/wireguard/wgctrl" "golang.zx2c4.com/wireguard/wgctrl/wgtypes" "net" "time" ) +const ( + defaultMTU = 1280 +) + +// Saves tun device object - is it required? +var tunIface tun.Device + +// Create Creates a new Wireguard interface, sets a given IP and brings it up. +// Will reuse an existing one. +func Create(iface string, address string) error { + var err error + + tunIface, err = tun.CreateTUN(iface, defaultMTU) + if err != nil { + return err + } + + // We need to create a wireguard-go device and listen to configuration requests + tunDevice := device.NewDevice(tunIface, device.NewLogger(device.LogLevelSilent, "[wiretrustee] ")) + tunDevice.Up() + tunSock, err := ipc.UAPIOpen(iface) + if err != nil { + return err + } + uapi, err := ipc.UAPIListen(iface, tunSock) + if err != nil { + return err + } + + go func() { + for { + conn, err := uapi.Accept() + if err != nil { + log.Debugln(err) + return + } + go tunDevice.IpcHandle(conn) + } + }() + + log.Debugln("UAPI listener started") + + err = assignAddr(iface, address) + if err != nil { + return err + } + return nil +} + // Extends the functionality of Configure(iface string, privateKey string) by generating a new Wireguard private key func ConfigureWithKeyGen(iface string) (*wgtypes.Key, error) { key, err := wgtypes.GeneratePrivateKey() diff --git a/iface/iface_darwin.go b/iface/darwin.go similarity index 100% rename from iface/iface_darwin.go rename to iface/darwin.go diff --git a/iface/iface.go b/iface/iface.go deleted file mode 100644 index 7a2a62d74..000000000 --- a/iface/iface.go +++ /dev/null @@ -1,82 +0,0 @@ -package iface - -import ( - //log "github.com/sirupsen/logrus" - "errors" - "fmt" - log "github.com/sirupsen/logrus" - "golang.zx2c4.com/wireguard/device" - "golang.zx2c4.com/wireguard/ipc" - "golang.zx2c4.com/wireguard/tun" - "net" - "strconv" -) - -const ( - defaultMTU = 1280 - interfaceLimit = 10 // can be higher. Need to check different OS limits -) - -// Saves tun device object - is it required? -var tunIface tun.Device - -// Create Creates a new Wireguard interface, sets a given IP and brings it up. -// Will reuse an existing one. -func Create(iface string, address string) error { - var err error - - tunIface, err = tun.CreateTUN(iface, defaultMTU) - if err != nil { - return err - } - - // We need to create a wireguard-go device and listen to configuration requests - tunDevice := device.NewDevice(tunIface, device.NewLogger(device.LogLevelSilent, "[wiretrustee] ")) - tunDevice.Up() - tunSock, err := ipc.UAPIOpen(iface) - if err != nil { - return err - } - uapi, err := ipc.UAPIListen(iface, tunSock) - if err != nil { - return err - } - - go func() { - for { - conn, err := uapi.Accept() - if err != nil { - log.Debugln(err) - return - } - go tunDevice.IpcHandle(conn) - } - }() - - log.Debugln("UAPI listener started") - - err = assignAddr(iface, address) - if err != nil { - return err - } - return nil -} - -// Deletes an existing Wireguard interface -func Delete() error { - return tunIface.Close() -} - -// GetIfaceName loops through the OS' interfaceLimit and returns the first available interface name based on -// interface prefixes and index -func GetIfaceName() (string, error) { - for i := 0; i < interfaceLimit; i++ { - _, err := net.InterfaceByName(interfacePrefix + strconv.Itoa(i)) - if err != nil { - if err.Error() != "no such network interface" { - return interfacePrefix + strconv.Itoa(i), nil - } - } - } - return "none", errors.New(fmt.Sprintf("Couldn't find an available interface index within the limit of: %d", interfaceLimit)) -} diff --git a/iface/iface_linux.go b/iface/linux.go similarity index 100% rename from iface/iface_linux.go rename to iface/linux.go