2021-05-01 12:45:37 +02:00
|
|
|
package iface
|
|
|
|
|
2021-06-24 11:46:33 +02:00
|
|
|
import (
|
2022-06-04 19:41:01 +02:00
|
|
|
"fmt"
|
2022-09-07 21:59:01 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"golang.zx2c4.com/wireguard/conn"
|
|
|
|
"golang.zx2c4.com/wireguard/device"
|
|
|
|
"golang.zx2c4.com/wireguard/tun"
|
2021-06-24 11:46:33 +02:00
|
|
|
"net"
|
2022-01-17 14:01:58 +01:00
|
|
|
"os"
|
|
|
|
"runtime"
|
2022-06-04 19:41:01 +02:00
|
|
|
"sync"
|
2021-06-24 11:46:33 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-05-08 11:04:57 +02:00
|
|
|
DefaultMTU = 1280
|
2022-03-01 14:07:33 +01:00
|
|
|
DefaultWgPort = 51820
|
2021-06-24 11:46:33 +02:00
|
|
|
)
|
|
|
|
|
2022-01-17 14:01:58 +01:00
|
|
|
// WGIface represents a interface instance
|
|
|
|
type WGIface struct {
|
|
|
|
Name string
|
|
|
|
Port int
|
|
|
|
MTU int
|
|
|
|
Address WGAddress
|
|
|
|
Interface NetInterface
|
2022-06-04 19:41:01 +02:00
|
|
|
mu sync.Mutex
|
2022-09-06 20:06:51 +02:00
|
|
|
Bind *ICEBind
|
2022-01-17 14:01:58 +01:00
|
|
|
}
|
2021-06-24 11:46:33 +02:00
|
|
|
|
2022-01-17 14:01:58 +01:00
|
|
|
// WGAddress Wireguard parsed address
|
|
|
|
type WGAddress struct {
|
|
|
|
IP net.IP
|
|
|
|
Network *net.IPNet
|
|
|
|
}
|
2021-06-24 11:46:33 +02:00
|
|
|
|
2022-06-04 19:41:01 +02:00
|
|
|
func (addr *WGAddress) String() string {
|
|
|
|
maskSize, _ := addr.Network.Mask.Size()
|
|
|
|
return fmt.Sprintf("%s/%d", addr.IP.String(), maskSize)
|
|
|
|
}
|
|
|
|
|
2022-01-17 14:01:58 +01:00
|
|
|
// NetInterface represents a generic network tunnel interface
|
|
|
|
type NetInterface interface {
|
|
|
|
Close() error
|
2021-06-24 11:46:33 +02:00
|
|
|
}
|
|
|
|
|
2022-06-04 19:41:01 +02:00
|
|
|
// NewWGIFace Creates a new Wireguard interface instance
|
|
|
|
func NewWGIFace(iface string, address string, mtu int) (*WGIface, error) {
|
|
|
|
wgIface := &WGIface{
|
2022-01-17 14:01:58 +01:00
|
|
|
Name: iface,
|
|
|
|
MTU: mtu,
|
2022-06-04 19:41:01 +02:00
|
|
|
mu: sync.Mutex{},
|
2021-06-24 11:46:33 +02:00
|
|
|
}
|
2021-07-19 15:02:11 +02:00
|
|
|
|
2022-01-17 14:01:58 +01:00
|
|
|
wgAddress, err := parseAddress(address)
|
2021-07-19 15:02:11 +02:00
|
|
|
if err != nil {
|
2022-01-17 14:01:58 +01:00
|
|
|
return wgIface, err
|
2021-07-19 15:02:11 +02:00
|
|
|
}
|
|
|
|
|
2022-01-17 14:01:58 +01:00
|
|
|
wgIface.Address = wgAddress
|
|
|
|
|
|
|
|
return wgIface, nil
|
2021-06-24 11:46:33 +02:00
|
|
|
}
|
|
|
|
|
2022-01-17 14:01:58 +01:00
|
|
|
// parseAddress parse a string ("1.2.3.4/24") address to WG Address
|
|
|
|
func parseAddress(address string) (WGAddress, error) {
|
|
|
|
ip, network, err := net.ParseCIDR(address)
|
2021-06-24 11:46:33 +02:00
|
|
|
if err != nil {
|
2022-01-17 14:01:58 +01:00
|
|
|
return WGAddress{}, err
|
2021-06-24 11:46:33 +02:00
|
|
|
}
|
2022-01-17 14:01:58 +01:00
|
|
|
return WGAddress{
|
|
|
|
IP: ip,
|
|
|
|
Network: network,
|
|
|
|
}, nil
|
2021-06-24 11:46:33 +02:00
|
|
|
}
|
|
|
|
|
2022-06-04 19:41:01 +02:00
|
|
|
// Close closes the tunnel interface
|
2022-01-17 14:01:58 +01:00
|
|
|
func (w *WGIface) Close() error {
|
2022-06-04 19:41:01 +02:00
|
|
|
w.mu.Lock()
|
|
|
|
defer w.mu.Unlock()
|
2021-06-24 11:46:33 +02:00
|
|
|
|
2022-01-17 14:01:58 +01:00
|
|
|
err := w.Interface.Close()
|
2021-06-24 11:46:33 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-17 14:01:58 +01:00
|
|
|
if runtime.GOOS == "darwin" {
|
|
|
|
sockPath := "/var/run/wireguard/" + w.Name + ".sock"
|
|
|
|
if _, statErr := os.Stat(sockPath); statErr == nil {
|
|
|
|
statErr = os.Remove(sockPath)
|
|
|
|
if statErr != nil {
|
|
|
|
return statErr
|
|
|
|
}
|
|
|
|
}
|
2021-06-24 11:46:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-09-07 21:59:01 +02:00
|
|
|
|
|
|
|
func (w *WGIface) CreateNew(bind conn.Bind) error {
|
|
|
|
w.mu.Lock()
|
|
|
|
defer w.mu.Unlock()
|
|
|
|
|
|
|
|
return w.createWithUserspaceNew(bind)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *WGIface) createWithUserspaceNew(bind conn.Bind) 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, bind, 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
|
|
|
|
}
|