2021-06-06 00:40:44 +02:00
|
|
|
package iface
|
|
|
|
|
|
|
|
import (
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"golang.zx2c4.com/wireguard/ipc"
|
|
|
|
"golang.zx2c4.com/wireguard/tun"
|
|
|
|
"golang.zx2c4.com/wireguard/windows/tunnel/winipcfg"
|
2021-06-06 21:51:56 +02:00
|
|
|
"net"
|
2021-06-06 00:40:44 +02:00
|
|
|
)
|
|
|
|
|
2021-06-24 11:46:33 +02:00
|
|
|
// Create Creates a new Wireguard interface, sets a given IP and brings it up.
|
|
|
|
func Create(iface string, address string) error {
|
|
|
|
return CreateWithUserspace(iface, address)
|
|
|
|
}
|
|
|
|
|
2021-06-06 00:40:44 +02:00
|
|
|
// assignAddr Adds IP address to the tunnel interface and network route based on the range provided
|
2021-06-23 16:11:54 +02:00
|
|
|
func assignAddr(address string, ifaceName string) error {
|
|
|
|
|
2021-06-24 11:46:33 +02:00
|
|
|
nativeTunDevice := tunIface.(*tun.NativeTun)
|
2021-06-06 00:40:44 +02:00
|
|
|
luid := winipcfg.LUID(nativeTunDevice.LUID())
|
|
|
|
|
|
|
|
ip, ipnet, _ := net.ParseCIDR(address)
|
|
|
|
|
|
|
|
log.Debugf("adding address %s to interface: %s", address, ifaceName)
|
2021-06-24 10:59:41 +02:00
|
|
|
err := luid.SetIPAddresses([]net.IPNet{{ip, ipnet.Mask}})
|
2021-06-06 00:40:44 +02:00
|
|
|
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)
|
|
|
|
}
|
2021-07-19 15:02:11 +02:00
|
|
|
|
|
|
|
// Closes the tunnel interface
|
|
|
|
func Close() error {
|
|
|
|
return CloseWithUserspace()
|
|
|
|
}
|