Skip to content

Commit

Permalink
refactort: extract method to create Wireguard interface using kernel …
Browse files Browse the repository at this point in the history
…module
  • Loading branch information
braginini committed Jun 24, 2021
1 parent 729b16e commit f2ca2fc
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 48 deletions.
2 changes: 1 addition & 1 deletion iface/iface.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ package iface
// Create Creates a new Wireguard interface, sets a given IP and brings it up.
// Will reuse an existing one.
func Create(iface string, address string) error {
return CreateInUserspace(iface, address)
return CreateWithUserspace(iface, address)
}
4 changes: 2 additions & 2 deletions iface/iface_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ func ConfigureWithKeyGen(iface string) (*wgtypes.Key, error) {
return &key, Configure(iface, key.String())
}

// CreateInUserspace Creates a new Wireguard interface, using wireguard-go userspace implementation
func CreateInUserspace(iface string, address string) error {
// CreateWithUserspace Creates a new Wireguard interface, using wireguard-go userspace implementation
func CreateWithUserspace(iface string, address string) error {
var err error
tunIface, err := tun.CreateTUN(iface, defaultMTU)
if err != nil {
Expand Down
96 changes: 51 additions & 45 deletions iface/iface_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,56 +11,62 @@ import (
func Create(iface string, address string) error {

if WireguardModExists() {
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
return CreateWithKernel(iface, address)
} 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
func assignAddr(address, name string) error {
var err error
Expand Down

0 comments on commit f2ca2fc

Please sign in to comment.