2023-12-18 11:46:58 +01:00
|
|
|
//go:build android
|
|
|
|
// +build android
|
|
|
|
|
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-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
|
|
|
|
|
|
|
"github.com/netbirdio/netbird/iface/bind"
|
2023-03-17 10:37:27 +01:00
|
|
|
)
|
|
|
|
|
2024-01-03 16:06:20 +01:00
|
|
|
// 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-01-03 16:06:20 +01:00
|
|
|
name string
|
|
|
|
device *device.Device
|
|
|
|
wrapper *DeviceWrapper
|
|
|
|
udpMux *bind.UniversalUDPMuxDefault
|
|
|
|
configurer wgConfigurer
|
2023-03-17 10:37:27 +01:00
|
|
|
}
|
|
|
|
|
2024-06-19 12:12:11 +02:00
|
|
|
func newTunDevice(address WGAddress, port int, key string, mtu int, transportNet transport.Net, tunAdapter TunAdapter, filterFn bind.FilterFn) wgTunDevice {
|
2024-01-03 16:06:20 +01:00
|
|
|
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-01-03 16:06:20 +01: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
|
2023-05-29 16:00:18 +02:00
|
|
|
t.wrapper = newDeviceWrapper(tunDevice)
|
2023-03-17 10:37:27 +01:00
|
|
|
|
|
|
|
log.Debugf("attaching to interface %v", name)
|
2024-07-15 14:45:18 +02:00
|
|
|
t.device = device.NewDevice(t.wrapper, 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-01-03 16:06:20 +01:00
|
|
|
t.configurer = newWGUSPConfigurer(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-01-03 16:06:20 +01:00
|
|
|
t.configurer.close()
|
|
|
|
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-01-03 16:06:20 +01:00
|
|
|
func (t *wgTunDevice) Up() (*bind.UniversalUDPMuxDefault, error) {
|
|
|
|
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-01-03 16:06:20 +01:00
|
|
|
func (t *wgTunDevice) UpdateAddr(addr WGAddress) error {
|
2023-03-17 10:37:27 +01:00
|
|
|
// todo implement
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-03 16:06:20 +01:00
|
|
|
func (t *wgTunDevice) Close() error {
|
|
|
|
if t.configurer != nil {
|
|
|
|
t.configurer.close()
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *wgTunDevice) Device() *device.Device {
|
|
|
|
return t.device
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *wgTunDevice) DeviceName() string {
|
|
|
|
return t.name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *wgTunDevice) WgAddress() WGAddress {
|
|
|
|
return t.address
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *wgTunDevice) Wrapper() *DeviceWrapper {
|
|
|
|
return t.wrapper
|
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, ";")
|
|
|
|
}
|