mirror of
https://github.com/netbirdio/netbird.git
synced 2024-11-22 16:13:31 +01:00
1a8c03bef0
* created InitializePeer and ClosePeerConnection functions * feature: simplify peer stopping * chore: remove unused code * feature: basic management service implementation (#44) * feat: basic management service implementation [FAILING TESTS] * test: fix healthcheck test * test: #39 add peer registration endpoint test * feat: #39 add setup key handling * feat: #39 add peer management store persistence * refactor: extract config read/write to the utility package * refactor: move file contents copy to the utility package * refactor: use Accounts instead of Users in the Store * feature: add management server Docker file * refactor: introduce datadir instead of config * chore: use filepath.Join to concat filepaths instead of string concat * refactor: move stop channel to the root * refactor: move stop channel to the root * review: fix PR review notes Co-authored-by: braginini <hello@wiretrustee.com> * Handle read config file errors * feature: add letsencrypt support to the management service * fix: lint warnings * chore: change default datadir * refactor: set default flags in code not Dockerfile * chore: remove unused code * Added RemovePeer and centralized configureDevice code * remove peer from the wg interface when closing proxy * remove config file * add iface tests * fix tests, validate if file exists before removing it * removed unused functions UpdateListenPort and ConfigureWithKeyGen * Ensure we don't wait for timeout when closing * Rename ClosePeerConnection to RemovePeerConnection * Avoid returning on uapi Accept failures * Added engine tests * Remove extra add address code * Adding iface.Close * Ensure Close the interface and disable parallel test execution * check err var when listing interfaces * chore: add synchronisation to peer management * chore: add connection status to track peer connection * refactor: remove unused code Co-authored-by: braginini <hello@wiretrustee.com> Co-authored-by: Mikhail Bragin <bangvalo@gmail.com>
135 lines
2.8 KiB
Go
135 lines
2.8 KiB
Go
package iface
|
|
|
|
import (
|
|
"fmt"
|
|
log "github.com/sirupsen/logrus"
|
|
"github.com/vishvananda/netlink"
|
|
"golang.zx2c4.com/wireguard/wgctrl"
|
|
"os"
|
|
)
|
|
|
|
// 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 {
|
|
|
|
if WireguardModExists() {
|
|
log.Debug("using kernel Wireguard module")
|
|
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.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
|
|
}
|
|
|
|
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
|
|
attrs := netlink.NewLinkAttrs()
|
|
attrs.Name = name
|
|
|
|
link := wgLink{
|
|
attrs: &attrs,
|
|
}
|
|
|
|
log.Debugf("adding address %s to interface: %s", address, attrs.Name)
|
|
addr, _ := netlink.ParseAddr(address)
|
|
err = netlink.AddrAdd(&link, addr)
|
|
if os.IsExist(err) {
|
|
log.Infof("interface %s already has the address: %s", attrs.Name, address)
|
|
} else if err != nil {
|
|
return err
|
|
}
|
|
// On linux, the link must be brought up
|
|
err = netlink.LinkSetUp(&link)
|
|
return err
|
|
}
|
|
|
|
type wgLink struct {
|
|
attrs *netlink.LinkAttrs
|
|
}
|
|
|
|
// Attrs returns the Wireguard's default attributes
|
|
func (w *wgLink) Attrs() *netlink.LinkAttrs {
|
|
return w.attrs
|
|
}
|
|
|
|
// Type returns the interface type
|
|
func (w *wgLink) Type() string {
|
|
return "wireguard"
|
|
}
|
|
|
|
// Closes the tunnel interface
|
|
func Close() error {
|
|
|
|
if tunIface != nil {
|
|
return CloseWithUserspace()
|
|
} else {
|
|
var iface = ""
|
|
wg, err := wgctrl.New()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer wg.Close()
|
|
devList, err := wg.Devices()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, wgDev := range devList {
|
|
if wgDev.ListenPort == WgPort {
|
|
iface = wgDev.Name
|
|
break
|
|
}
|
|
}
|
|
if iface == "" {
|
|
return fmt.Errorf("Wireguard Interface not found")
|
|
}
|
|
attrs := netlink.NewLinkAttrs()
|
|
attrs.Name = iface
|
|
|
|
link := wgLink{
|
|
attrs: &attrs,
|
|
}
|
|
return netlink.LinkDel(&link)
|
|
}
|
|
}
|