2023-12-18 11:46:58 +01:00
|
|
|
//go:build android
|
|
|
|
|
2024-10-02 18:24:22 +02:00
|
|
|
package device
|
2023-03-17 10:37:27 +01:00
|
|
|
|
|
|
|
import (
|
2023-04-17 11:15:37 +02:00
|
|
|
"strings"
|
2023-03-17 10:37:27 +01:00
|
|
|
|
2023-12-20 23:02:42 +01:00
|
|
|
"github.com/pion/transport/v3"
|
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
|
|
|
|
2024-10-02 18:24:22 +02:00
|
|
|
"github.com/netbirdio/netbird/client/iface/bind"
|
|
|
|
"github.com/netbirdio/netbird/client/iface/configurer"
|
2023-03-17 10:37:27 +01:00
|
|
|
)
|
|
|
|
|
2024-10-02 18:24:22 +02:00
|
|
|
// WGTunDevice ignore the WGTunDevice interface on Android because the creation of the tun device is different on this platform
|
|
|
|
type WGTunDevice struct {
|
2023-03-17 10:37:27 +01:00
|
|
|
address WGAddress
|
2024-01-03 16:06:20 +01:00
|
|
|
port int
|
|
|
|
key string
|
2023-03-17 10:37:27 +01:00
|
|
|
mtu int
|
2023-06-12 14:43:55 +02:00
|
|
|
iceBind *bind.ICEBind
|
2024-01-03 16:06:20 +01:00
|
|
|
tunAdapter TunAdapter
|
2023-03-17 10:37:27 +01:00
|
|
|
|
2024-10-02 18:24:22 +02:00
|
|
|
name string
|
|
|
|
device *device.Device
|
|
|
|
filteredDevice *FilteredDevice
|
|
|
|
udpMux *bind.UniversalUDPMuxDefault
|
|
|
|
configurer WGConfigurer
|
2023-03-17 10:37:27 +01:00
|
|
|
}
|
|
|
|
|
2024-10-02 18:24:22 +02:00
|
|
|
func NewTunDevice(address WGAddress, port int, key string, mtu int, transportNet transport.Net, tunAdapter TunAdapter, filterFn bind.FilterFn) *WGTunDevice {
|
|
|
|
return &WGTunDevice{
|
2023-03-17 10:37:27 +01:00
|
|
|
address: address,
|
2024-01-03 16:06:20 +01:00
|
|
|
port: port,
|
|
|
|
key: key,
|
2023-03-17 10:37:27 +01:00
|
|
|
mtu: mtu,
|
2024-06-19 12:12:11 +02:00
|
|
|
iceBind: bind.NewICEBind(transportNet, filterFn),
|
2024-01-03 16:06:20 +01:00
|
|
|
tunAdapter: tunAdapter,
|
2023-03-17 10:37:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-02 18:24:22 +02:00
|
|
|
func (t *WGTunDevice) Create(routes []string, dns string, searchDomains []string) (WGConfigurer, error) {
|
2023-07-14 21:56:22 +02:00
|
|
|
log.Info("create tun interface")
|
2024-01-03 16:06:20 +01:00
|
|
|
|
|
|
|
routesString := routesToString(routes)
|
|
|
|
searchDomainsToString := searchDomainsToString(searchDomains)
|
|
|
|
|
|
|
|
fd, err := t.tunAdapter.ConfigureInterface(t.address.String(), t.mtu, dns, searchDomainsToString, routesString)
|
2023-03-17 10:37:27 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed to create Android interface: %s", err)
|
2024-01-03 16:06:20 +01:00
|
|
|
return nil, err
|
2023-03-17 10:37:27 +01:00
|
|
|
}
|
|
|
|
|
2024-01-03 16:06:20 +01:00
|
|
|
tunDevice, name, err := tun.CreateUnmonitoredTUNFromFD(fd)
|
2023-03-17 10:37:27 +01:00
|
|
|
if err != nil {
|
2024-01-03 16:06:20 +01:00
|
|
|
_ = unix.Close(fd)
|
|
|
|
log.Errorf("failed to create Android interface: %s", err)
|
|
|
|
return nil, err
|
2023-03-17 10:37:27 +01:00
|
|
|
}
|
|
|
|
t.name = name
|
2024-10-02 18:24:22 +02:00
|
|
|
t.filteredDevice = newDeviceFilter(tunDevice)
|
2023-03-17 10:37:27 +01:00
|
|
|
|
|
|
|
log.Debugf("attaching to interface %v", name)
|
2024-10-02 18:24:22 +02:00
|
|
|
t.device = device.NewDevice(t.filteredDevice, t.iceBind, device.NewLogger(wgLogLevel(), "[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
|
2023-12-18 11:46:58 +01:00
|
|
|
// t.device.DisableSomeRoamingForBrokenMobileSemantics()
|
2023-03-17 10:37:27 +01:00
|
|
|
|
2024-10-02 18:24:22 +02:00
|
|
|
t.configurer = configurer.NewUSPConfigurer(t.device, t.name)
|
|
|
|
err = t.configurer.ConfigureInterface(t.key, t.port)
|
2023-03-17 10:37:27 +01:00
|
|
|
if err != nil {
|
|
|
|
t.device.Close()
|
2024-10-02 18:24:22 +02:00
|
|
|
t.configurer.Close()
|
2024-01-03 16:06:20 +01:00
|
|
|
return nil, err
|
2023-03-17 10:37:27 +01:00
|
|
|
}
|
2024-01-03 16:06:20 +01:00
|
|
|
return t.configurer, nil
|
2023-03-17 10:37:27 +01:00
|
|
|
}
|
2024-10-02 18:24:22 +02:00
|
|
|
func (t *WGTunDevice) Up() (*bind.UniversalUDPMuxDefault, error) {
|
2024-01-03 16:06:20 +01:00
|
|
|
err := t.device.Up()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-03-17 10:37:27 +01:00
|
|
|
|
2024-01-03 16:06:20 +01:00
|
|
|
udpMux, err := t.iceBind.GetICEMux()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
t.udpMux = udpMux
|
|
|
|
log.Debugf("device is ready to use: %s", t.name)
|
|
|
|
return udpMux, nil
|
2023-03-17 10:37:27 +01:00
|
|
|
}
|
|
|
|
|
2024-10-02 18:24:22 +02:00
|
|
|
func (t *WGTunDevice) UpdateAddr(addr WGAddress) error {
|
2023-03-17 10:37:27 +01:00
|
|
|
// todo implement
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-10-02 18:24:22 +02:00
|
|
|
func (t *WGTunDevice) Close() error {
|
2024-01-03 16:06:20 +01:00
|
|
|
if t.configurer != nil {
|
2024-10-02 18:24:22 +02:00
|
|
|
t.configurer.Close()
|
2024-01-03 16:06:20 +01:00
|
|
|
}
|
|
|
|
|
2023-03-17 10:37:27 +01:00
|
|
|
if t.device != nil {
|
|
|
|
t.device.Close()
|
2024-01-03 16:06:20 +01:00
|
|
|
t.device = nil
|
2023-03-17 10:37:27 +01:00
|
|
|
}
|
|
|
|
|
2024-01-03 16:06:20 +01:00
|
|
|
if t.udpMux != nil {
|
|
|
|
return t.udpMux.Close()
|
|
|
|
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-10-02 18:24:22 +02:00
|
|
|
func (t *WGTunDevice) Device() *device.Device {
|
2024-01-03 16:06:20 +01:00
|
|
|
return t.device
|
|
|
|
}
|
|
|
|
|
2024-10-02 18:24:22 +02:00
|
|
|
func (t *WGTunDevice) DeviceName() string {
|
2024-01-03 16:06:20 +01:00
|
|
|
return t.name
|
|
|
|
}
|
|
|
|
|
2024-10-02 18:24:22 +02:00
|
|
|
func (t *WGTunDevice) WgAddress() WGAddress {
|
2024-01-03 16:06:20 +01:00
|
|
|
return t.address
|
|
|
|
}
|
|
|
|
|
2024-10-02 18:24:22 +02:00
|
|
|
func (t *WGTunDevice) FilteredDevice() *FilteredDevice {
|
|
|
|
return t.filteredDevice
|
2023-03-17 10:37:27 +01:00
|
|
|
}
|
2023-04-17 11:15:37 +02:00
|
|
|
|
2024-01-03 16:06:20 +01:00
|
|
|
func routesToString(routes []string) string {
|
2023-06-12 14:43:55 +02:00
|
|
|
return strings.Join(routes, ";")
|
2023-04-17 11:15:37 +02:00
|
|
|
}
|
2023-11-02 19:04:33 +01:00
|
|
|
|
2024-01-03 16:06:20 +01:00
|
|
|
func searchDomainsToString(searchDomains []string) string {
|
2023-11-02 19:04:33 +01:00
|
|
|
return strings.Join(searchDomains, ";")
|
|
|
|
}
|