2023-03-17 10:37:27 +01:00
|
|
|
//go:build (linux || darwin) && !android
|
2021-06-06 00:40:44 +02:00
|
|
|
|
|
|
|
package iface
|
|
|
|
|
|
|
|
import (
|
2023-02-13 18:34:56 +01:00
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
|
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
|
|
|
)
|
|
|
|
|
2023-03-17 10:37:27 +01:00
|
|
|
type tunDevice struct {
|
|
|
|
name string
|
|
|
|
address WGAddress
|
|
|
|
mtu int
|
|
|
|
netInterface NetInterface
|
2023-02-13 18:34:56 +01:00
|
|
|
}
|
|
|
|
|
2023-03-17 10:37:27 +01:00
|
|
|
func newTunDevice(name string, address WGAddress, mtu int) *tunDevice {
|
|
|
|
return &tunDevice{
|
|
|
|
name: name,
|
|
|
|
address: address,
|
|
|
|
mtu: mtu,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *tunDevice) UpdateAddr(address WGAddress) error {
|
|
|
|
c.address = address
|
|
|
|
return c.assignAddr()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *tunDevice) WgAddress() WGAddress {
|
|
|
|
return c.address
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *tunDevice) DeviceName() string {
|
|
|
|
return c.name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *tunDevice) Close() error {
|
|
|
|
if c.netInterface == nil {
|
2023-02-13 18:34:56 +01:00
|
|
|
return nil
|
|
|
|
}
|
2023-03-17 10:37:27 +01:00
|
|
|
err := c.netInterface.Close()
|
2023-02-13 18:34:56 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-17 10:37:27 +01:00
|
|
|
sockPath := "/var/run/wireguard/" + c.name + ".sock"
|
2023-02-13 18:34:56 +01:00
|
|
|
if _, statErr := os.Stat(sockPath); statErr == nil {
|
|
|
|
statErr = os.Remove(sockPath)
|
|
|
|
if statErr != nil {
|
|
|
|
return statErr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-04 19:41:01 +02:00
|
|
|
// createWithUserspace Creates a new Wireguard interface, using wireguard-go userspace implementation
|
2023-03-17 10:37:27 +01:00
|
|
|
func (c *tunDevice) createWithUserspace() (NetInterface, error) {
|
|
|
|
tunIface, err := tun.CreateTUN(c.name, c.mtu)
|
2022-01-17 14:01:58 +01:00
|
|
|
if err != nil {
|
2023-03-17 10:37:27 +01:00
|
|
|
return nil, err
|
2022-01-17 14:01:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2023-03-17 10:37:27 +01:00
|
|
|
return tunIface, err
|
2022-01-17 14:01:58 +01:00
|
|
|
}
|
2023-03-17 10:37:27 +01:00
|
|
|
|
|
|
|
// todo: after this line in case of error close the tunSock
|
|
|
|
uapi, err := c.getUAPI(c.name)
|
2022-01-17 14:01:58 +01:00
|
|
|
if err != nil {
|
2023-03-17 10:37:27 +01:00
|
|
|
return tunIface, err
|
2022-01-17 14:01:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
2023-03-17 10:37:27 +01:00
|
|
|
return tunIface, nil
|
2022-01-17 14:01:58 +01:00
|
|
|
}
|
|
|
|
|
2021-06-06 00:40:44 +02:00
|
|
|
// getUAPI returns a Listener
|
2023-03-17 10:37:27 +01:00
|
|
|
func (c *tunDevice) getUAPI(iface string) (net.Listener, error) {
|
2021-06-06 00:40:44 +02:00
|
|
|
tunSock, err := ipc.UAPIOpen(iface)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ipc.UAPIListen(iface, tunSock)
|
|
|
|
}
|