2023-03-17 10:37:27 +01:00
|
|
|
package iface
|
|
|
|
|
2023-04-13 17:00:01 +02:00
|
|
|
import (
|
|
|
|
"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
|
|
|
|
|
|
|
// SetInitialRoutes store the given routes and on the tun creation will be used
|
|
|
|
func (w *WGIface) SetInitialRoutes(routes []string) {
|
|
|
|
w.tun.SetRoutes(routes)
|
|
|
|
}
|