2023-03-17 10:37:27 +01:00
|
|
|
package iface
|
|
|
|
|
2023-04-13 17:00:01 +02:00
|
|
|
import (
|
2023-06-12 14:43:55 +02:00
|
|
|
"fmt"
|
2023-03-17 10:37:27 +01:00
|
|
|
|
2023-12-20 23:02:42 +01:00
|
|
|
"github.com/pion/transport/v3"
|
2024-06-19 12:12:11 +02:00
|
|
|
|
|
|
|
"github.com/netbirdio/netbird/iface/bind"
|
2023-04-13 17:00:01 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewWGIFace Creates a new WireGuard interface instance
|
2024-06-19 12:12:11 +02:00
|
|
|
func NewWGIFace(iFaceName string, address string, wgPort int, wgPrivKey string, mtu int, transportNet transport.Net, args *MobileIFaceArguments, filterFn bind.FilterFn) (*WGIface, error) {
|
2023-03-17 10:37:27 +01:00
|
|
|
wgAddress, err := parseWGAddress(address)
|
|
|
|
if err != nil {
|
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
|
|
|
wgIFace := &WGIface{
|
2024-06-19 12:12:11 +02:00
|
|
|
tun: newTunDevice(wgAddress, wgPort, wgPrivKey, mtu, transportNet, args.TunAdapter, filterFn),
|
2024-01-03 16:06:20 +01:00
|
|
|
userspaceBind: true,
|
|
|
|
}
|
2023-04-13 17:00:01 +02:00
|
|
|
return wgIFace, nil
|
2023-03-17 10:37:27 +01:00
|
|
|
}
|
2023-05-31 18:25:24 +02:00
|
|
|
|
2023-12-18 11:46:58 +01:00
|
|
|
// CreateOnAndroid creates a new Wireguard interface, sets a given IP and brings it up.
|
2023-06-12 14:43:55 +02:00
|
|
|
// Will reuse an existing one.
|
2024-01-03 16:06:20 +01:00
|
|
|
func (w *WGIface) CreateOnAndroid(routes []string, dns string, searchDomains []string) error {
|
2023-06-12 14:43:55 +02:00
|
|
|
w.mu.Lock()
|
|
|
|
defer w.mu.Unlock()
|
|
|
|
|
2024-01-03 16:06:20 +01:00
|
|
|
cfgr, err := w.tun.Create(routes, dns, searchDomains)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
w.configurer = cfgr
|
|
|
|
return nil
|
2023-12-18 11:46:58 +01:00
|
|
|
}
|
|
|
|
|
2023-06-12 14:43:55 +02:00
|
|
|
// Create this function make sense on mobile only
|
|
|
|
func (w *WGIface) Create() error {
|
2024-01-03 16:06:20 +01:00
|
|
|
return fmt.Errorf("this function has not implemented on this platform")
|
2023-05-31 18:25:24 +02:00
|
|
|
}
|