2023-08-09 15:21:53 +02:00
|
|
|
//go:build !android && !ios
|
|
|
|
// +build !android,!ios
|
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-04-13 17:00:01 +02:00
|
|
|
wgIFace.tun = newTunDevice(iFaceName, wgAddress, mtu, transportNet)
|
2023-03-17 10:37:27 +01:00
|
|
|
|
2023-04-13 17:00:01 +02:00
|
|
|
wgIFace.configurer = newWGConfigurer(iFaceName)
|
|
|
|
wgIFace.userspaceBind = !WireGuardModuleIsLoaded()
|
|
|
|
return wgIFace, nil
|
2023-03-17 10:37:27 +01:00
|
|
|
}
|
2023-05-31 18:25:24 +02:00
|
|
|
|
2023-08-09 15:21:53 +02:00
|
|
|
// CreateOnAndroid this function make sense on mobile only
|
|
|
|
func (w *WGIface) CreateOnAndroid(mIFaceArgs MobileIFaceArguments) error {
|
|
|
|
return fmt.Errorf("this function has not implemented on non mobile")
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateOniOS this function make sense on mobile only
|
|
|
|
func (w *WGIface) CreateOniOS(tunFd int32) error {
|
2023-06-12 14:43:55 +02:00
|
|
|
return fmt.Errorf("this function has not implemented on non mobile")
|
|
|
|
}
|
2023-05-31 18:25:24 +02:00
|
|
|
|
2023-06-12 14:43:55 +02:00
|
|
|
// Create creates a new Wireguard interface, sets a given IP and brings it up.
|
|
|
|
// Will reuse an existing one.
|
|
|
|
func (w *WGIface) Create() error {
|
|
|
|
w.mu.Lock()
|
|
|
|
defer w.mu.Unlock()
|
|
|
|
return w.tun.Create()
|
2023-05-31 18:25:24 +02:00
|
|
|
}
|