mirror of
https://github.com/netbirdio/netbird.git
synced 2025-06-30 22:50:22 +02:00
Mobile prerefactor (#680)
Small code cleaning in the iface package. These changes necessary to get a clean code in case if we involve more platforms. The OS related functions has been distributed into separate files and it has been mixed with not OS related logic. The goal is to get a clear picture of the layer between WireGuard and business logic.
This commit is contained in:
@ -2,15 +2,12 @@ package iface
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/vishvananda/netlink"
|
||||
"os"
|
||||
)
|
||||
|
||||
type NativeLink struct {
|
||||
Link *netlink.Link
|
||||
}
|
||||
|
||||
// Create creates a new Wireguard interface, sets a given IP and brings it up.
|
||||
// Will reuse an existing one.
|
||||
func (w *WGIface) Create() error {
|
||||
@ -33,10 +30,10 @@ func (w *WGIface) Create() error {
|
||||
// Works for Linux and offers much better network performance
|
||||
func (w *WGIface) createWithKernel() error {
|
||||
|
||||
link := newWGLink(w.Name)
|
||||
link := newWGLink(w.name)
|
||||
|
||||
// check if interface exists
|
||||
l, err := netlink.LinkByName(w.Name)
|
||||
l, err := netlink.LinkByName(w.name)
|
||||
if err != nil {
|
||||
switch err.(type) {
|
||||
case netlink.LinkNotFoundError:
|
||||
@ -54,15 +51,15 @@ func (w *WGIface) createWithKernel() error {
|
||||
}
|
||||
}
|
||||
|
||||
log.Debugf("adding device: %s", w.Name)
|
||||
log.Debugf("adding device: %s", w.name)
|
||||
err = netlink.LinkAdd(link)
|
||||
if os.IsExist(err) {
|
||||
log.Infof("interface %s already exists. Will reuse.", w.Name)
|
||||
log.Infof("interface %s already exists. Will reuse.", w.name)
|
||||
} else if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
w.Interface = link
|
||||
w.netInterface = link
|
||||
|
||||
err = w.assignAddr()
|
||||
if err != nil {
|
||||
@ -70,17 +67,17 @@ func (w *WGIface) createWithKernel() error {
|
||||
}
|
||||
|
||||
// todo do a discovery
|
||||
log.Debugf("setting MTU: %d interface: %s", w.MTU, w.Name)
|
||||
err = netlink.LinkSetMTU(link, w.MTU)
|
||||
log.Debugf("setting MTU: %d interface: %s", w.mtu, w.name)
|
||||
err = netlink.LinkSetMTU(link, w.mtu)
|
||||
if err != nil {
|
||||
log.Errorf("error setting MTU on interface: %s", w.Name)
|
||||
log.Errorf("error setting MTU on interface: %s", w.name)
|
||||
return err
|
||||
}
|
||||
|
||||
log.Debugf("bringing up interface: %s", w.Name)
|
||||
log.Debugf("bringing up interface: %s", w.name)
|
||||
err = netlink.LinkSetUp(link)
|
||||
if err != nil {
|
||||
log.Errorf("error bringing up interface: %s", w.Name)
|
||||
log.Errorf("error bringing up interface: %s", w.name)
|
||||
return err
|
||||
}
|
||||
|
||||
@ -89,7 +86,7 @@ func (w *WGIface) createWithKernel() error {
|
||||
|
||||
// assignAddr Adds IP address to the tunnel interface
|
||||
func (w *WGIface) assignAddr() error {
|
||||
link := newWGLink(w.Name)
|
||||
link := newWGLink(w.name)
|
||||
|
||||
//delete existing addresses
|
||||
list, err := netlink.AddrList(link, 0)
|
||||
@ -105,11 +102,11 @@ func (w *WGIface) assignAddr() error {
|
||||
}
|
||||
}
|
||||
|
||||
log.Debugf("adding address %s to interface: %s", w.Address.String(), w.Name)
|
||||
addr, _ := netlink.ParseAddr(w.Address.String())
|
||||
log.Debugf("adding address %s to interface: %s", w.address.String(), w.name)
|
||||
addr, _ := netlink.ParseAddr(w.address.String())
|
||||
err = netlink.AddrAdd(link, addr)
|
||||
if os.IsExist(err) {
|
||||
log.Infof("interface %s already has the address: %s", w.Name, w.Address.String())
|
||||
log.Infof("interface %s already has the address: %s", w.name, w.address.String())
|
||||
} else if err != nil {
|
||||
return err
|
||||
}
|
||||
|
Reference in New Issue
Block a user