refactort: extract method to create Wireguard interface using kernel module

This commit is contained in:
braginini 2021-06-24 11:02:40 +02:00
parent 729b16e599
commit f2ca2fc7c1
3 changed files with 54 additions and 48 deletions

View File

@ -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)
} }

View File

@ -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 {

View File

@ -11,6 +11,16 @@ import (
func Create(iface string, address string) error { func Create(iface string, address string) error {
if WireguardModExists() { if WireguardModExists() {
return CreateWithKernel(iface, address)
} else {
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 := netlink.NewLinkAttrs()
attrs.Name = iface attrs.Name = iface
@ -55,10 +65,6 @@ func Create(iface string, address string) error {
} }
return nil return nil
} else {
return CreateInUserspace(iface, address)
}
} }
// assignAddr Adds IP address to the tunnel interface // assignAddr Adds IP address to the tunnel interface