2023-03-17 10:37:27 +01:00
|
|
|
package iface
|
|
|
|
|
|
|
|
import (
|
2023-04-17 11:15:37 +02:00
|
|
|
"strings"
|
2023-03-17 10:37:27 +01:00
|
|
|
|
2023-04-13 17:00:01 +02:00
|
|
|
"github.com/pion/transport/v2"
|
2023-03-17 10:37:27 +01:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
"golang.zx2c4.com/wireguard/device"
|
|
|
|
"golang.zx2c4.com/wireguard/tun"
|
2023-04-17 11:50:12 +02:00
|
|
|
|
|
|
|
"github.com/netbirdio/netbird/iface/bind"
|
2023-03-17 10:37:27 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type tunDevice struct {
|
|
|
|
address WGAddress
|
|
|
|
mtu int
|
2023-04-17 11:15:37 +02:00
|
|
|
routes []string
|
2023-03-17 10:37:27 +01:00
|
|
|
tunAdapter TunAdapter
|
|
|
|
|
2023-04-13 17:00:01 +02:00
|
|
|
fd int
|
|
|
|
name string
|
|
|
|
device *device.Device
|
|
|
|
iceBind *bind.ICEBind
|
2023-03-17 10:37:27 +01:00
|
|
|
}
|
|
|
|
|
2023-04-17 11:15:37 +02:00
|
|
|
func newTunDevice(address WGAddress, mtu int, routes []string, tunAdapter TunAdapter, transportNet transport.Net) *tunDevice {
|
2023-03-17 10:37:27 +01:00
|
|
|
return &tunDevice{
|
|
|
|
address: address,
|
|
|
|
mtu: mtu,
|
2023-04-17 11:15:37 +02:00
|
|
|
routes: routes,
|
2023-03-17 10:37:27 +01:00
|
|
|
tunAdapter: tunAdapter,
|
2023-04-13 17:00:01 +02:00
|
|
|
iceBind: bind.NewICEBind(transportNet),
|
2023-03-17 10:37:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tunDevice) Create() error {
|
|
|
|
var err error
|
2023-04-17 11:15:37 +02:00
|
|
|
routesString := t.routesToString()
|
|
|
|
t.fd, err = t.tunAdapter.ConfigureInterface(t.address.String(), t.mtu, routesString)
|
2023-03-17 10:37:27 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed to create Android interface: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
tunDevice, name, err := tun.CreateUnmonitoredTUNFromFD(t.fd)
|
|
|
|
if err != nil {
|
|
|
|
unix.Close(t.fd)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
t.name = name
|
|
|
|
|
|
|
|
log.Debugf("attaching to interface %v", name)
|
2023-04-13 17:00:01 +02:00
|
|
|
t.device = device.NewDevice(tunDevice, t.iceBind, device.NewLogger(device.LogLevelSilent, "[wiretrustee] "))
|
2023-04-28 16:26:54 +02:00
|
|
|
// without this property mobile devices can discover remote endpoints if the configured one was wrong.
|
|
|
|
// this helps with support for the older NetBird clients that had a hardcoded direct mode
|
|
|
|
//t.device.DisableSomeRoamingForBrokenMobileSemantics()
|
2023-03-17 10:37:27 +01:00
|
|
|
|
|
|
|
err = t.device.Up()
|
|
|
|
if err != nil {
|
|
|
|
t.device.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Debugf("device is ready to use: %s", name)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tunDevice) Device() *device.Device {
|
|
|
|
return t.device
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tunDevice) DeviceName() string {
|
|
|
|
return t.name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tunDevice) WgAddress() WGAddress {
|
|
|
|
return t.address
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tunDevice) UpdateAddr(addr WGAddress) error {
|
|
|
|
// todo implement
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tunDevice) Close() (err error) {
|
|
|
|
if t.device != nil {
|
|
|
|
t.device.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2023-04-17 11:15:37 +02:00
|
|
|
|
|
|
|
func (t *tunDevice) routesToString() string {
|
|
|
|
return strings.Join(t.routes, ";")
|
|
|
|
}
|