mirror of
https://github.com/netbirdio/netbird.git
synced 2025-04-15 06:58:29 +02:00
feature: add feature to determine when to run wireguard userspace implementation or native one (linux)
This commit is contained in:
parent
dd72a01ecf
commit
4216cd2986
@ -2,52 +2,8 @@
|
|||||||
|
|
||||||
package iface
|
package iface
|
||||||
|
|
||||||
import (
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
"golang.zx2c4.com/wireguard/conn"
|
|
||||||
"golang.zx2c4.com/wireguard/device"
|
|
||||||
"golang.zx2c4.com/wireguard/tun"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Saves tun device object - is it required?
|
|
||||||
var tunIface tun.Device
|
|
||||||
|
|
||||||
// Create Creates a new Wireguard interface, sets a given IP and brings it up.
|
// Create Creates a new Wireguard interface, sets a given IP and brings it up.
|
||||||
// Will reuse an existing one.
|
// Will reuse an existing one.
|
||||||
func Create(iface string, address string) error {
|
func Create(iface string, address string) error {
|
||||||
var err error
|
return CreateInUserspace(iface, address)
|
||||||
tunIface, err = tun.CreateTUN(iface, defaultMTU)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// We need to create a wireguard-go device and listen to configuration requests
|
|
||||||
tunDevice := device.NewDevice(tunIface, conn.NewDefaultBind(), device.NewLogger(device.LogLevelSilent, "[wiretrustee] "))
|
|
||||||
err = tunDevice.Up()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
uapi, err := getUAPI(iface)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
for {
|
|
||||||
uapiConn, err := uapi.Accept()
|
|
||||||
if err != nil {
|
|
||||||
log.Debugln(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
go tunDevice.IpcHandle(uapiConn)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
log.Debugln("UAPI listener started")
|
|
||||||
|
|
||||||
err = assignAddr(address, tunIface)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
package iface
|
package iface
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"golang.zx2c4.com/wireguard/conn"
|
||||||
|
"golang.zx2c4.com/wireguard/device"
|
||||||
|
"golang.zx2c4.com/wireguard/tun"
|
||||||
"net"
|
"net"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -23,6 +26,49 @@ func ConfigureWithKeyGen(iface string) (*wgtypes.Key, error) {
|
|||||||
return &key, Configure(iface, key.String())
|
return &key, Configure(iface, key.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CreateInUserspace Creates a new Wireguard interface, using wireguard-go userspace implementation
|
||||||
|
func CreateInUserspace(iface string, address string) error {
|
||||||
|
var err error
|
||||||
|
tunIface, err := tun.CreateTUN(iface, defaultMTU)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// We need to create a wireguard-go device and listen to configuration requests
|
||||||
|
tunDevice := device.NewDevice(tunIface, conn.NewDefaultBind(), device.NewLogger(device.LogLevelSilent, "[wiretrustee] "))
|
||||||
|
err = tunDevice.Up()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
uapi, err := getUAPI(iface)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
uapiConn, err := uapi.Accept()
|
||||||
|
if err != nil {
|
||||||
|
log.Debugln(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
go tunDevice.IpcHandle(uapiConn)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
log.Debugln("UAPI listener started")
|
||||||
|
|
||||||
|
ifaceName, err := tunIface.Name()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = assignAddr(address, ifaceName)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// Configure configures a Wireguard interface
|
// Configure configures a Wireguard interface
|
||||||
// The interface must exist before calling this method (e.g. call interface.Create() before)
|
// The interface must exist before calling this method (e.g. call interface.Create() before)
|
||||||
func Configure(iface string, privateKey string) error {
|
func Configure(iface string, privateKey string) error {
|
||||||
|
@ -2,7 +2,6 @@ package iface
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"golang.zx2c4.com/wireguard/tun"
|
|
||||||
"net"
|
"net"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
@ -13,8 +12,7 @@ import (
|
|||||||
//)
|
//)
|
||||||
|
|
||||||
// assignAddr Adds IP address to the tunnel interface and network route based on the range provided
|
// assignAddr Adds IP address to the tunnel interface and network route based on the range provided
|
||||||
func assignAddr(address string, tunDevice tun.Device) error {
|
func assignAddr(address string, ifaceName string) error {
|
||||||
ifaceName, err := tunDevice.Name()
|
|
||||||
ip := strings.Split(address, "/")
|
ip := strings.Split(address, "/")
|
||||||
cmd := exec.Command("ifconfig", ifaceName, "inet", address, ip[0])
|
cmd := exec.Command("ifconfig", ifaceName, "inet", address, ip[0])
|
||||||
if out, err := cmd.CombinedOutput(); err != nil {
|
if out, err := cmd.CombinedOutput(); err != nil {
|
||||||
|
@ -9,50 +9,56 @@ import (
|
|||||||
// Create Creates a new Wireguard interface, sets a given IP and brings it up.
|
// Create Creates a new Wireguard interface, sets a given IP and brings it up.
|
||||||
// Will reuse an existing one.
|
// Will reuse an existing one.
|
||||||
func Create(iface string, address string) error {
|
func Create(iface string, address string) error {
|
||||||
attrs := netlink.NewLinkAttrs()
|
|
||||||
attrs.Name = iface
|
|
||||||
|
|
||||||
link := wgLink{
|
if WireguardModExists() {
|
||||||
attrs: &attrs,
|
attrs := netlink.NewLinkAttrs()
|
||||||
|
attrs.Name = iface
|
||||||
|
|
||||||
|
link := wgLink{
|
||||||
|
attrs: &attrs,
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Debugf("adding device: %s", iface)
|
||||||
|
err := netlink.LinkAdd(&link)
|
||||||
|
if os.IsExist(err) {
|
||||||
|
log.Infof("interface %s already exists. Will reuse.", iface)
|
||||||
|
} else if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Debugf("adding address %s to interface: %s", address, iface)
|
||||||
|
addr, _ := netlink.ParseAddr(address)
|
||||||
|
err = netlink.AddrAdd(&link, addr)
|
||||||
|
if os.IsExist(err) {
|
||||||
|
log.Infof("interface %s already has the address: %s", iface, address)
|
||||||
|
} else if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = assignAddr(address, iface)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// todo do a discovery
|
||||||
|
log.Debugf("setting MTU: %s", iface)
|
||||||
|
err = netlink.LinkSetMTU(&link, defaultMTU)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("error setting MTU on interface: %s", iface)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Debugf("bringing up interface: %s", iface)
|
||||||
|
err = netlink.LinkSetUp(&link)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("error bringing up interface: %s", iface)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
} else {
|
||||||
|
return CreateInUserspace(iface, address)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debugf("adding device: %s", iface)
|
|
||||||
err := netlink.LinkAdd(&link)
|
|
||||||
if os.IsExist(err) {
|
|
||||||
log.Infof("interface %s already exists. Will reuse.", iface)
|
|
||||||
} else if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Debugf("adding address %s to interface: %s", address, iface)
|
|
||||||
addr, _ := netlink.ParseAddr(address)
|
|
||||||
err = netlink.AddrAdd(&link, addr)
|
|
||||||
if os.IsExist(err) {
|
|
||||||
log.Infof("interface %s already has the address: %s", iface, address)
|
|
||||||
} else if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
err = assignAddr(address, iface)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// todo do a discovery
|
|
||||||
log.Debugf("setting MTU: %s", iface)
|
|
||||||
err = netlink.LinkSetMTU(&link, defaultMTU)
|
|
||||||
if err != nil {
|
|
||||||
log.Errorf("error setting MTU on interface: %s", iface)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Debugf("bringing up interface: %s", iface)
|
|
||||||
err = netlink.LinkSetUp(&link)
|
|
||||||
if err != nil {
|
|
||||||
log.Errorf("error bringing up interface: %s", iface)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// assignAddr Adds IP address to the tunnel interface
|
// assignAddr Adds IP address to the tunnel interface
|
||||||
|
@ -9,8 +9,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// assignAddr Adds IP address to the tunnel interface and network route based on the range provided
|
// assignAddr Adds IP address to the tunnel interface and network route based on the range provided
|
||||||
func assignAddr(address string, tunDevice tun.Device) error {
|
func assignAddr(address string, ifaceName string) error {
|
||||||
ifaceName, err := tunDevice.Name()
|
|
||||||
nativeTunDevice := tunDevice.(*tun.NativeTun)
|
nativeTunDevice := tunDevice.(*tun.NativeTun)
|
||||||
luid := winipcfg.LUID(nativeTunDevice.LUID())
|
luid := winipcfg.LUID(nativeTunDevice.LUID())
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user