Add windows support and update wireguard-go deps

This commit is contained in:
mlsmaycon
2021-06-06 00:40:44 +02:00
parent 3520b6471b
commit 59360519d6
7 changed files with 152 additions and 63 deletions
+5 -24
View File
@@ -2,9 +2,8 @@ package iface
import (
log "github.com/sirupsen/logrus"
"github.com/vishvananda/netlink"
"golang.zx2c4.com/wireguard/conn"
"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"
@@ -30,13 +29,9 @@ func Create(iface string, address string) error {
}
// We need to create a wireguard-go device and listen to configuration requests
tunDevice := device.NewDevice(tunIface, device.NewLogger(device.LogLevelSilent, "[wiretrustee] "))
tunDevice := device.NewDevice(tunIface, conn.NewDefaultBind(), device.NewLogger(device.LogLevelSilent, "[wiretrustee] "))
tunDevice.Up()
tunSock, err := ipc.UAPIOpen(iface)
if err != nil {
return err
}
uapi, err := ipc.UAPIListen(iface, tunSock)
uapi, err := getUAPI(iface)
if err != nil {
return err
}
@@ -54,7 +49,7 @@ func Create(iface string, address string) error {
log.Debugln("UAPI listener started")
err = assignAddr(iface, address)
err = assignAddr(address, tunIface)
if err != nil {
return err
}
@@ -176,7 +171,7 @@ func UpdatePeer(iface string, peerKey string, allowedIps string, keepAlive time.
log.Debugf("got Wireguard device %s", iface)
//parse allowed ips
ipNet, err := netlink.ParseIPNet(allowedIps)
_, ipNet, err := net.ParseCIDR(allowedIps)
if err != nil {
return err
}
@@ -259,17 +254,3 @@ func UpdatePeerEndpoint(iface string, peerKey string, newEndpoint string) error
return nil
}
type wgLink struct {
attrs *netlink.LinkAttrs
}
// Attrs returns the Wireguard's default attributes
func (w *wgLink) Attrs() *netlink.LinkAttrs {
return w.attrs
}
// Type returns the interface type
func (w *wgLink) Type() string {
return "wireguard"
}
+5 -3
View File
@@ -2,6 +2,7 @@ package iface
import (
log "github.com/sirupsen/logrus"
"golang.zx2c4.com/wireguard/tun"
"net"
"os/exec"
"strings"
@@ -12,15 +13,16 @@ import (
//)
// assignAddr Adds IP address to the tunnel interface and network route based on the range provided
func assignAddr(iface string, address string) error {
func assignAddr(address string, tunDevice tun.Device) error {
ifaceName, err := tunDevice.Name()
ip := strings.Split(address, "/")
cmd := exec.Command("ifconfig", iface, "inet", address, ip[0])
cmd := exec.Command("ifconfig", ifaceName, "inet", address, ip[0])
if out, err := cmd.CombinedOutput(); err != nil {
log.Infoln("Command: %v failed with output %s and error: ", cmd.String(), out)
return err
}
_, resolvedNet, err := net.ParseCIDR(address)
err = addRoute(iface, resolvedNet)
err = addRoute(ifaceName, resolvedNet)
if err != nil {
log.Infoln("Adding route failed with error:", err)
}
+22 -5
View File
@@ -3,6 +3,8 @@ package iface
import (
log "github.com/sirupsen/logrus"
"github.com/vishvananda/netlink"
"golang.zx2c4.com/wireguard/tun"
"os"
)
@@ -11,19 +13,20 @@ import (
//)
// assignAddr Adds IP address to the tunnel interface
func assignAddr(iface string, address string) error {
func assignAddr(address string, tunDevice tun.Device) error {
var err error
attrs := netlink.NewLinkAttrs()
attrs.Name = iface
attrs.Name, err = tunDevice.Name()
link := wgLink{
attrs: &attrs,
}
log.Debugf("adding address %s to interface: %s", address, iface)
log.Debugf("adding address %s to interface: %s", address, attrs.Name)
addr, _ := netlink.ParseAddr(address)
err := netlink.AddrAdd(&link, addr)
err = netlink.AddrAdd(&link, addr)
if os.IsExist(err) {
log.Infof("interface %s already has the address: %s", iface, address)
log.Infof("interface %s already has the address: %s", attrs.Name, address)
} else if err != nil {
return err
}
@@ -31,3 +34,17 @@ func assignAddr(iface string, address string) error {
err = netlink.LinkSetUp(&link)
return err
}
type wgLink struct {
attrs *netlink.LinkAttrs
}
// Attrs returns the Wireguard's default attributes
func (w *wgLink) Attrs() *netlink.LinkAttrs {
return w.attrs
}
// Type returns the interface type
func (w *wgLink) Type() string {
return "wireguard"
}
+17
View File
@@ -0,0 +1,17 @@
// +build linux darwin
package iface
import (
"golang.zx2c4.com/wireguard/ipc"
"net"
)
// getUAPI returns a Listener
func getUAPI(iface string) (net.Listener, error) {
tunSock, err := ipc.UAPIOpen(iface)
if err != nil {
return nil, err
}
return ipc.UAPIListen(iface, tunSock)
}
+38
View File
@@ -0,0 +1,38 @@
package iface
import (
log "github.com/sirupsen/logrus"
"net"
"golang.zx2c4.com/wireguard/ipc"
"golang.zx2c4.com/wireguard/tun"
"golang.zx2c4.com/wireguard/windows/tunnel/winipcfg"
)
// assignAddr Adds IP address to the tunnel interface and network route based on the range provided
func assignAddr(address string, tunDevice tun.Device) error {
ifaceName, err := tunDevice.Name()
nativeTunDevice := tunDevice.(*tun.NativeTun)
luid := winipcfg.LUID(nativeTunDevice.LUID())
ip, ipnet, _ := net.ParseCIDR(address)
log.Debugf("adding address %s to interface: %s", address, ifaceName)
err = luid.SetIPAddresses([]net.IPNet{{ip, ipnet.Mask}})
if err != nil {
return err
}
log.Debugf("adding Routes to interface: %s", ifaceName)
err = luid.SetRoutes([]*winipcfg.RouteData{{*ipnet, ipnet.IP, 0}})
if err != nil {
return err
}
return nil
}
// getUAPI returns a Listener
func getUAPI(iface string) (net.Listener, error) {
return ipc.UAPIListen(iface)
}