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-04-13 17:00:01 +02:00
|
|
|
"sync"
|
2023-03-17 10:37:27 +01:00
|
|
|
|
2023-04-13 17:00:01 +02:00
|
|
|
"github.com/pion/transport/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewWGIFace Creates a new WireGuard interface instance
|
2023-05-31 18:25:24 +02:00
|
|
|
func NewWGIFace(ifaceName string, address string, mtu int, tunAdapter TunAdapter, transportNet transport.Net) (*WGIface, error) {
|
2023-04-13 17:00:01 +02:00
|
|
|
wgIFace := &WGIface{
|
2023-03-17 10:37:27 +01:00
|
|
|
mu: sync.Mutex{},
|
|
|
|
}
|
|
|
|
|
|
|
|
wgAddress, err := parseWGAddress(address)
|
|
|
|
if err != nil {
|
2023-04-13 17:00:01 +02:00
|
|
|
return wgIFace, err
|
2023-03-17 10:37:27 +01:00
|
|
|
}
|
|
|
|
|
2023-05-31 18:25:24 +02:00
|
|
|
tun := newTunDevice(wgAddress, mtu, tunAdapter, transportNet)
|
2023-04-13 17:00:01 +02:00
|
|
|
wgIFace.tun = tun
|
|
|
|
|
|
|
|
wgIFace.configurer = newWGConfigurer(tun)
|
2023-03-17 10:37:27 +01:00
|
|
|
|
2023-04-13 17:00:01 +02:00
|
|
|
wgIFace.userspaceBind = !WireGuardModuleIsLoaded()
|
2023-03-17 10:37:27 +01:00
|
|
|
|
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-06-12 14:43:55 +02:00
|
|
|
// CreateOnMobile creates a new Wireguard interface, sets a given IP and brings it up.
|
|
|
|
// Will reuse an existing one.
|
|
|
|
func (w *WGIface) CreateOnMobile(mIFaceArgs MobileIFaceArguments) error {
|
|
|
|
w.mu.Lock()
|
|
|
|
defer w.mu.Unlock()
|
|
|
|
return w.tun.Create(mIFaceArgs)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create this function make sense on mobile only
|
|
|
|
func (w *WGIface) Create() error {
|
|
|
|
return fmt.Errorf("this function has not implemented on mobile")
|
2023-05-31 18:25:24 +02:00
|
|
|
}
|