mirror of
https://github.com/netbirdio/netbird.git
synced 2025-06-20 01:38:41 +02:00
refactort: extract method to create Wireguard interface using kernel module
This commit is contained in:
parent
729b16e599
commit
f2ca2fc7c1
@ -5,5 +5,5 @@ package iface
|
|||||||
// 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 {
|
||||||
return CreateInUserspace(iface, address)
|
return CreateWithUserspace(iface, address)
|
||||||
}
|
}
|
||||||
|
@ -26,8 +26,8 @@ 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
|
// CreateWithUserspace Creates a new Wireguard interface, using wireguard-go userspace implementation
|
||||||
func CreateInUserspace(iface string, address string) error {
|
func CreateWithUserspace(iface string, address string) error {
|
||||||
var err error
|
var err error
|
||||||
tunIface, err := tun.CreateTUN(iface, defaultMTU)
|
tunIface, err := tun.CreateTUN(iface, defaultMTU)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -11,56 +11,62 @@ import (
|
|||||||
func Create(iface string, address string) error {
|
func Create(iface string, address string) error {
|
||||||
|
|
||||||
if WireguardModExists() {
|
if WireguardModExists() {
|
||||||
attrs := netlink.NewLinkAttrs()
|
return CreateWithKernel(iface, address)
|
||||||
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 {
|
} else {
|
||||||
return CreateInUserspace(iface, address)
|
return CreateWithUserspace(iface, address)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CreateWithKernel Creates a new Wireguard interface using kernel Wireguard module.
|
||||||
|
// Works for Linux and offers much better network performance
|
||||||
|
func CreateWithKernel(iface string, address string) error {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
// assignAddr Adds IP address to the tunnel interface
|
// assignAddr Adds IP address to the tunnel interface
|
||||||
func assignAddr(address, name string) error {
|
func assignAddr(address, name string) error {
|
||||||
var err error
|
var err error
|
||||||
|
Loading…
x
Reference in New Issue
Block a user