2022-01-17 14:01:58 +01:00
|
|
|
//go:build linux || darwin
|
2021-06-06 00:40:44 +02:00
|
|
|
// +build linux darwin
|
|
|
|
|
|
|
|
package iface
|
|
|
|
|
|
|
|
import (
|
2022-01-17 14:01:58 +01:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"golang.zx2c4.com/wireguard/conn"
|
|
|
|
"golang.zx2c4.com/wireguard/device"
|
2021-06-06 00:40:44 +02:00
|
|
|
"golang.zx2c4.com/wireguard/ipc"
|
2022-01-17 14:01:58 +01:00
|
|
|
"golang.zx2c4.com/wireguard/tun"
|
2021-06-06 00:40:44 +02:00
|
|
|
"net"
|
|
|
|
)
|
|
|
|
|
2022-01-17 14:01:58 +01:00
|
|
|
// CreateWithUserspace Creates a new Wireguard interface, using wireguard-go userspace implementation
|
|
|
|
func (w *WGIface) CreateWithUserspace() error {
|
|
|
|
|
|
|
|
tunIface, err := tun.CreateTUN(w.Name, w.MTU)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Interface = tunIface
|
|
|
|
|
|
|
|
// We need to create a wireguard-go device and listen to configuration requests
|
|
|
|
tunDevice := device.NewDevice(tunIface, conn.NewDefaultBind(), device.NewLogger(device.LogLevelSilent, "[wiretrustee] "))
|
|
|
|
err = tunDevice.Up()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
uapi, err := getUAPI(w.Name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
uapiConn, uapiErr := uapi.Accept()
|
|
|
|
if uapiErr != nil {
|
|
|
|
log.Traceln("uapi Accept failed with error: ", uapiErr)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
go tunDevice.IpcHandle(uapiConn)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
log.Debugln("UAPI listener started")
|
|
|
|
|
|
|
|
err = w.assignAddr()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-06 00:40:44 +02:00
|
|
|
// 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)
|
|
|
|
}
|