netmaker/netclient/wireguard/common.go

563 lines
17 KiB
Go
Raw Normal View History

2021-09-20 02:03:47 +08:00
package wireguard
import (
"fmt"
2021-09-20 02:03:47 +08:00
"log"
2022-02-03 11:00:59 +08:00
"net"
2021-09-20 02:03:47 +08:00
"runtime"
"strconv"
"strings"
2021-09-22 05:50:09 +08:00
"time"
2021-09-20 02:03:47 +08:00
"github.com/gravitl/netmaker/logger"
2021-09-20 02:03:47 +08:00
"github.com/gravitl/netmaker/models"
"github.com/gravitl/netmaker/netclient/config"
"github.com/gravitl/netmaker/netclient/local"
"github.com/gravitl/netmaker/netclient/ncutils"
"golang.zx2c4.com/wireguard/wgctrl"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
"gopkg.in/ini.v1"
2021-09-20 02:03:47 +08:00
)
2022-01-05 23:32:20 +08:00
const (
section_interface = "Interface"
section_peers = "Peer"
)
2021-10-09 03:07:12 +08:00
// SetPeers - sets peers on a given WireGuard interface
2022-02-21 22:45:42 +08:00
func SetPeers(iface string, node *models.Node, peers []wgtypes.PeerConfig) error {
2021-11-11 05:08:29 +08:00
var devicePeers []wgtypes.Peer
2022-02-21 22:45:42 +08:00
var keepalive = node.PersistentKeepalive
2022-02-03 11:00:59 +08:00
var oldPeerAllowedIps = make(map[string][]net.IPNet, len(peers))
2021-11-11 05:08:29 +08:00
var err error
2022-04-26 04:30:18 +08:00
devicePeers, err = GetDevicePeers(iface)
if err != nil {
return err
2021-11-11 05:08:29 +08:00
}
2022-04-26 04:30:18 +08:00
2021-09-20 02:03:47 +08:00
if len(devicePeers) > 1 && len(peers) == 0 {
logger.Log(1, "no peers pulled")
2021-09-20 02:03:47 +08:00
return err
}
2022-02-08 03:43:06 +08:00
for _, peer := range peers {
for _, currentPeer := range devicePeers {
if currentPeer.AllowedIPs[0].String() == peer.AllowedIPs[0].String() &&
currentPeer.PublicKey.String() != peer.PublicKey.String() {
_, err := ncutils.RunCmd("wg set "+iface+" peer "+currentPeer.PublicKey.String()+" remove", true)
if err != nil {
log.Println("error removing peer", peer.Endpoint.String())
}
2021-09-20 02:03:47 +08:00
}
}
2022-02-08 03:43:06 +08:00
udpendpoint := peer.Endpoint.String()
var allowedips string
var iparr []string
for _, ipaddr := range peer.AllowedIPs {
2022-04-23 01:04:34 +08:00
if len(peer.AllowedIPs) > 0 && (&ipaddr) != nil {
iparr = append(iparr, ipaddr.String())
}
2022-02-08 03:43:06 +08:00
}
2022-05-11 03:18:29 +08:00
if iparr != nil && len(iparr) > 0 {
allowedips = strings.Join(iparr, ",")
}
2022-02-08 03:43:06 +08:00
keepAliveString := strconv.Itoa(int(keepalive))
if keepAliveString == "0" {
keepAliveString = "15"
}
2022-03-21 23:30:18 +08:00
if node.IsHub == "yes" || node.IsServer == "yes" || peer.Endpoint == nil {
2022-02-08 03:43:06 +08:00
_, err = ncutils.RunCmd("wg set "+iface+" peer "+peer.PublicKey.String()+
" persistent-keepalive "+keepAliveString+
" allowed-ips "+allowedips, true)
} else {
_, err = ncutils.RunCmd("wg set "+iface+" peer "+peer.PublicKey.String()+
2022-02-21 22:45:42 +08:00
" endpoint "+udpendpoint+
2022-02-08 03:43:06 +08:00
" persistent-keepalive "+keepAliveString+
" allowed-ips "+allowedips, true)
}
if err != nil {
log.Println("error setting peer", peer.PublicKey.String())
2021-09-20 02:03:47 +08:00
}
}
2022-05-12 00:07:06 +08:00
if devicePeers != nil && len(devicePeers) > 0 {
2022-05-11 03:18:29 +08:00
for _, currentPeer := range devicePeers {
shouldDelete := true
2022-05-12 00:07:06 +08:00
if peers != nil && len(peers) > 0 {
2022-05-11 03:18:29 +08:00
for _, peer := range peers {
if peer.AllowedIPs[0].String() == currentPeer.AllowedIPs[0].String() {
shouldDelete = false
}
// re-check this if logic is not working, added in case of allowedips not working
if peer.PublicKey.String() == currentPeer.PublicKey.String() {
shouldDelete = false
}
}
if shouldDelete {
output, err := ncutils.RunCmd("wg set "+iface+" peer "+currentPeer.PublicKey.String()+" remove", true)
if err != nil {
log.Println(output, "error removing peer", currentPeer.PublicKey.String())
}
}
oldPeerAllowedIps[currentPeer.PublicKey.String()] = currentPeer.AllowedIPs
2021-09-20 02:03:47 +08:00
}
}
}
2022-01-12 23:03:23 +08:00
if ncutils.IsMac() {
err = SetMacPeerRoutes(iface)
return err
2022-02-08 03:43:06 +08:00
} else if ncutils.IsLinux() {
2022-05-11 03:18:29 +08:00
if len(peers) > 0 {
local.SetPeerRoutes(iface, oldPeerAllowedIps, peers)
}
2022-01-12 23:03:23 +08:00
}
2021-09-20 02:03:47 +08:00
return nil
}
2021-10-09 03:07:12 +08:00
// Initializes a WireGuard interface
2022-04-22 03:53:44 +08:00
func InitWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig, syncconf bool) error {
2021-09-20 02:03:47 +08:00
key, err := wgtypes.ParseKey(privkey)
if err != nil {
return err
}
wgclient, err := wgctrl.New()
if err != nil {
return err
}
2021-12-11 04:01:10 +08:00
defer wgclient.Close()
2021-09-20 02:03:47 +08:00
modcfg, err := config.ReadConfig(node.Network)
if err != nil {
return err
}
nodecfg := modcfg.Node
2021-09-22 02:01:52 +08:00
var ifacename string
2021-09-20 02:03:47 +08:00
if nodecfg.Interface != "" {
ifacename = nodecfg.Interface
} else if node.Interface != "" {
ifacename = node.Interface
} else {
return fmt.Errorf("no interface to configure")
2021-09-20 02:03:47 +08:00
}
2022-04-20 04:18:03 +08:00
if node.PrimaryAddress() == "" {
return fmt.Errorf("no address to configure")
2021-09-20 02:03:47 +08:00
}
2022-04-20 04:18:03 +08:00
if node.UDPHolePunch == "yes" {
2022-01-22 02:15:54 +08:00
node.ListenPort = 0
2021-09-20 02:03:47 +08:00
}
2022-01-22 02:15:54 +08:00
if err := WriteWgConfig(&modcfg.Node, key.String(), peers); err != nil {
logger.Log(1, "error writing wg conf file: ", err.Error())
return err
}
// spin up userspace / windows interface + apply the conf file
2022-01-25 20:31:50 +08:00
confPath := ncutils.GetNetclientPathSpecific() + ifacename + ".conf"
var deviceiface = ifacename
2022-04-26 08:37:05 +08:00
var mErr error
if ncutils.IsMac() { // if node is Mac (Darwin) get the tunnel name first
2022-04-26 08:37:05 +08:00
deviceiface, mErr = local.GetMacIface(node.PrimaryAddress())
if mErr != nil || deviceiface == "" {
deviceiface = ifacename
}
}
// ensure you clear any existing interface first
2022-03-15 09:03:49 +08:00
RemoveConfGraceful(deviceiface)
ApplyConf(node, ifacename, confPath) // Apply initially
logger.Log(1, "waiting for interface...") // ensure interface is created
output, _ := ncutils.RunCmd("wg", false)
starttime := time.Now()
2022-02-08 00:30:15 +08:00
ifaceReady := strings.Contains(output, deviceiface)
2022-02-06 03:31:58 +08:00
for !ifaceReady && !(time.Now().After(starttime.Add(time.Second << 4))) {
2022-02-08 00:30:15 +08:00
if ncutils.IsMac() { // if node is Mac (Darwin) get the tunnel name first
2022-04-26 08:37:05 +08:00
deviceiface, mErr = local.GetMacIface(node.PrimaryAddress())
if mErr != nil || deviceiface == "" {
2022-02-08 00:30:15 +08:00
deviceiface = ifacename
}
}
output, _ = ncutils.RunCmd("wg", false)
2022-02-09 08:13:46 +08:00
err = ApplyConf(node, node.Interface, confPath)
time.Sleep(time.Second)
2022-02-08 00:30:15 +08:00
ifaceReady = strings.Contains(output, deviceiface)
}
//wgclient does not work well on freebsd
if node.OS == "freebsd" {
if !ifaceReady {
return fmt.Errorf("could not reliably create interface, please check wg installation and retry")
}
} else {
_, devErr := wgclient.Device(deviceiface)
if !ifaceReady || devErr != nil {
2022-03-15 23:11:22 +08:00
fmt.Printf("%v\n", devErr)
return fmt.Errorf("could not reliably create interface, please check wg installation and retry")
}
}
logger.Log(1, "interface ready - netclient.. ENGAGE")
if syncconf { // should never be called really.
2022-03-03 05:19:53 +08:00
fmt.Println("why here")
err = SyncWGQuickConf(ifacename, confPath)
}
if !ncutils.HasWgQuick() && ncutils.IsLinux() {
2022-02-21 22:45:42 +08:00
err = SetPeers(ifacename, node, peers)
if err != nil {
logger.Log(1, "error setting peers: ", err.Error())
}
2022-04-26 08:37:05 +08:00
time.Sleep(time.Second)
}
2022-04-20 04:18:03 +08:00
//ipv4
if node.Address != "" {
_, cidr, cidrErr := net.ParseCIDR(modcfg.NetworkSettings.AddressRange)
if cidrErr == nil {
local.SetCIDRRoute(ifacename, node.Address, cidr)
} else {
logger.Log(1, "could not set cidr route properly: ", cidrErr.Error())
}
local.SetCurrentPeerRoutes(ifacename, node.Address, peers)
}
if node.Address6 != "" {
//ipv6
_, cidr, cidrErr := net.ParseCIDR(modcfg.NetworkSettings.AddressRange6)
if cidrErr == nil {
local.SetCIDRRoute(ifacename, node.Address6, cidr)
} else {
logger.Log(1, "could not set cidr route properly: ", cidrErr.Error())
}
2022-04-26 08:37:05 +08:00
2022-04-20 04:18:03 +08:00
local.SetCurrentPeerRoutes(ifacename, node.Address6, peers)
2021-11-15 05:50:20 +08:00
}
2021-09-20 02:03:47 +08:00
return err
}
2021-10-09 03:07:12 +08:00
// SetWGConfig - sets the WireGuard Config of a given network and checks if it needs a peer update
2022-04-26 04:30:18 +08:00
func SetWGConfig(network string, peerupdate bool, peers []wgtypes.PeerConfig) error {
2021-09-20 02:03:47 +08:00
cfg, err := config.ReadConfig(network)
if err != nil {
return err
}
servercfg := cfg.Server
nodecfg := cfg.Node
2021-09-23 02:31:31 +08:00
2021-09-20 02:03:47 +08:00
privkey, err := RetrievePrivKey(network)
if err != nil {
return err
}
2022-04-20 04:18:03 +08:00
if peerupdate && !ncutils.IsFreeBSD() && !(ncutils.IsLinux() && !ncutils.IsKernel()) {
var iface string
iface = nodecfg.Interface
if ncutils.IsMac() {
iface, err = local.GetMacIface(nodecfg.PrimaryAddress())
if err != nil {
return err
}
2021-09-20 02:03:47 +08:00
}
2022-04-26 04:30:18 +08:00
err = SetPeers(iface, &nodecfg, peers)
2021-11-13 00:24:29 +08:00
} else if peerupdate {
2022-04-26 04:30:18 +08:00
err = InitWireguard(&nodecfg, privkey, peers, true)
2021-09-20 02:03:47 +08:00
} else {
2022-04-26 04:30:18 +08:00
err = InitWireguard(&nodecfg, privkey, peers, false)
2021-09-20 02:03:47 +08:00
}
if nodecfg.DNSOn == "yes" {
_ = local.UpdateDNS(nodecfg.Interface, nodecfg.Network, servercfg.CoreDNSAddr)
}
2021-09-20 02:03:47 +08:00
return err
}
2021-10-09 03:07:12 +08:00
// RemoveConf - removes a configuration for a given WireGuard interface
2021-09-20 02:03:47 +08:00
func RemoveConf(iface string, printlog bool) error {
os := runtime.GOOS
2022-03-03 05:19:53 +08:00
if ncutils.IsLinux() && !ncutils.HasWgQuick() {
os = "nowgquick"
}
2021-09-20 02:03:47 +08:00
var err error
switch os {
case "nowgquick":
err = RemoveWithoutWGQuick(iface)
2021-09-20 02:03:47 +08:00
case "windows":
err = RemoveWindowsConf(iface, printlog)
2022-01-12 07:28:41 +08:00
case "darwin":
err = RemoveConfMac(iface)
2021-09-20 02:03:47 +08:00
default:
confPath := ncutils.GetNetclientPathSpecific() + iface + ".conf"
err = RemoveWGQuickConf(confPath, printlog)
}
return err
}
2021-10-09 03:07:12 +08:00
// ApplyConf - applys a conf on disk to WireGuard interface
func ApplyConf(node *models.Node, ifacename string, confPath string) error {
2021-09-20 02:03:47 +08:00
os := runtime.GOOS
if ncutils.IsLinux() && !ncutils.HasWgQuick() {
os = "nowgquick"
}
2021-09-20 02:03:47 +08:00
var err error
switch os {
case "windows":
2022-02-21 00:12:51 +08:00
ApplyWindowsConf(confPath)
2022-01-12 07:28:41 +08:00
case "darwin":
2022-02-21 00:12:51 +08:00
ApplyMacOSConf(node, ifacename, confPath)
2022-03-03 05:19:53 +08:00
case "nowgquick":
ApplyWithoutWGQuick(node, ifacename, confPath)
2021-09-20 02:03:47 +08:00
default:
2022-02-21 00:12:51 +08:00
ApplyWGQuickConf(confPath, ifacename)
2021-09-20 02:03:47 +08:00
}
2022-02-21 00:12:51 +08:00
var nodeCfg config.ClientConfig
nodeCfg.Network = node.Network
nodeCfg.ReadConfig()
2022-04-26 08:37:05 +08:00
if nodeCfg.NetworkSettings.AddressRange != "" {
ip, cidr, err := net.ParseCIDR(nodeCfg.NetworkSettings.AddressRange)
if err == nil {
local.SetCIDRRoute(node.Interface, ip.String(), cidr)
}
}
if nodeCfg.NetworkSettings.AddressRange6 != "" {
ip, cidr, err := net.ParseCIDR(nodeCfg.NetworkSettings.AddressRange6)
if err == nil {
local.SetCIDRRoute(node.Interface, ip.String(), cidr)
}
2022-02-21 00:12:51 +08:00
}
2021-09-20 02:03:47 +08:00
return err
}
// WriteWgConfig - creates a wireguard config file
2022-01-22 02:15:54 +08:00
//func WriteWgConfig(cfg *config.ClientConfig, privateKey string, peers []wgtypes.PeerConfig) error {
func WriteWgConfig(node *models.Node, privateKey string, peers []wgtypes.PeerConfig) error {
options := ini.LoadOptions{
AllowNonUniqueSections: true,
AllowShadows: true,
}
wireguard := ini.Empty(options)
2022-01-05 23:32:20 +08:00
wireguard.Section(section_interface).Key("PrivateKey").SetValue(privateKey)
2022-01-31 05:26:32 +08:00
if node.ListenPort > 0 && node.UDPHolePunch != "yes" {
2022-01-22 02:15:54 +08:00
wireguard.Section(section_interface).Key("ListenPort").SetValue(strconv.Itoa(int(node.ListenPort)))
}
addrString := node.Address
2022-01-22 02:15:54 +08:00
if node.Address6 != "" {
if addrString != "" {
addrString += ","
}
addrString += node.Address6
}
wireguard.Section(section_interface).Key("Address").SetValue(addrString)
2022-01-22 02:15:54 +08:00
// need to figure out DNS
//if node.DNSOn == "yes" {
// wireguard.Section(section_interface).Key("DNS").SetValue(cfg.Server.CoreDNSAddr)
//}
if node.PostUp != "" {
wireguard.Section(section_interface).Key("PostUp").SetValue(node.PostUp)
}
2022-01-22 02:15:54 +08:00
if node.PostDown != "" {
wireguard.Section(section_interface).Key("PostDown").SetValue(node.PostDown)
}
2022-01-24 23:58:12 +08:00
if node.MTU != 0 {
wireguard.Section(section_interface).Key("MTU").SetValue(strconv.FormatInt(int64(node.MTU), 10))
}
for i, peer := range peers {
2022-01-05 23:32:20 +08:00
wireguard.SectionWithIndex(section_peers, i).Key("PublicKey").SetValue(peer.PublicKey.String())
if peer.PresharedKey != nil {
2022-01-05 23:32:20 +08:00
wireguard.SectionWithIndex(section_peers, i).Key("PreSharedKey").SetValue(peer.PresharedKey.String())
}
if peer.AllowedIPs != nil {
var allowedIPs string
2022-01-20 04:00:03 +08:00
for i, ip := range peer.AllowedIPs {
if i == 0 {
allowedIPs = ip.String()
} else {
allowedIPs = allowedIPs + ", " + ip.String()
}
}
2022-01-05 23:32:20 +08:00
wireguard.SectionWithIndex(section_peers, i).Key("AllowedIps").SetValue(allowedIPs)
}
if peer.Endpoint != nil {
2022-01-05 23:32:20 +08:00
wireguard.SectionWithIndex(section_peers, i).Key("Endpoint").SetValue(peer.Endpoint.String())
}
2022-01-22 02:15:54 +08:00
if peer.PersistentKeepaliveInterval != nil && peer.PersistentKeepaliveInterval.Seconds() > 0 {
wireguard.SectionWithIndex(section_peers, i).Key("PersistentKeepalive").SetValue(strconv.FormatInt((int64)(peer.PersistentKeepaliveInterval.Seconds()), 10))
}
}
2022-01-22 02:15:54 +08:00
if err := wireguard.SaveTo(ncutils.GetNetclientPathSpecific() + node.Interface + ".conf"); err != nil {
return err
}
return nil
}
// UpdateWgPeers - updates the peers of a network
2022-01-23 00:38:56 +08:00
func UpdateWgPeers(file string, peers []wgtypes.PeerConfig) error {
options := ini.LoadOptions{
AllowNonUniqueSections: true,
AllowShadows: true,
}
wireguard, err := ini.LoadSources(options, file)
if err != nil {
return err
}
2022-01-20 04:00:03 +08:00
//delete the peers sections as they are going to be replaced
wireguard.DeleteSection(section_peers)
for i, peer := range peers {
2022-01-05 23:32:20 +08:00
wireguard.SectionWithIndex(section_peers, i).Key("PublicKey").SetValue(peer.PublicKey.String())
2022-01-23 00:38:56 +08:00
if peer.PresharedKey != nil {
wireguard.SectionWithIndex(section_peers, i).Key("PreSharedKey").SetValue(peer.PresharedKey.String())
}
if peer.AllowedIPs != nil {
var allowedIPs string
2022-01-20 04:00:03 +08:00
for i, ip := range peer.AllowedIPs {
if i == 0 {
allowedIPs = ip.String()
} else {
allowedIPs = allowedIPs + ", " + ip.String()
}
}
2022-01-05 23:32:20 +08:00
wireguard.SectionWithIndex(section_peers, i).Key("AllowedIps").SetValue(allowedIPs)
}
if peer.Endpoint != nil {
2022-01-05 23:32:20 +08:00
wireguard.SectionWithIndex(section_peers, i).Key("Endpoint").SetValue(peer.Endpoint.String())
}
2022-01-22 02:15:54 +08:00
if peer.PersistentKeepaliveInterval != nil && peer.PersistentKeepaliveInterval.Seconds() > 0 {
wireguard.SectionWithIndex(section_peers, i).Key("PersistentKeepalive").SetValue(strconv.FormatInt((int64)(peer.PersistentKeepaliveInterval.Seconds()), 10))
}
}
if err := wireguard.SaveTo(file); err != nil {
return err
}
return nil
}
// UpdateWgInterface - updates the interface section of a wireguard config file
2022-01-23 00:38:56 +08:00
func UpdateWgInterface(file, privateKey, nameserver string, node models.Node) error {
options := ini.LoadOptions{
AllowNonUniqueSections: true,
AllowShadows: true,
}
wireguard, err := ini.LoadSources(options, file)
if err != nil {
return err
}
if node.UDPHolePunch == "yes" {
node.ListenPort = 0
}
2022-01-05 23:32:20 +08:00
wireguard.Section(section_interface).Key("PrivateKey").SetValue(privateKey)
wireguard.Section(section_interface).Key("ListenPort").SetValue(strconv.Itoa(int(node.ListenPort)))
2022-04-26 10:06:33 +08:00
addrString := node.Address
if node.Address6 != "" {
2022-04-26 10:06:33 +08:00
if addrString != "" {
addrString += ","
}
addrString += node.Address6
}
2022-04-26 10:06:33 +08:00
wireguard.Section(section_interface).Key("Address").SetValue(addrString)
2022-01-25 04:46:38 +08:00
//if node.DNSOn == "yes" {
// wireguard.Section(section_interface).Key("DNS").SetValue(nameserver)
//}
if node.PostUp != "" {
2022-01-05 23:32:20 +08:00
wireguard.Section(section_interface).Key("PostUp").SetValue(node.PostUp)
}
if node.PostDown != "" {
2022-01-05 23:32:20 +08:00
wireguard.Section(section_interface).Key("PostDown").SetValue(node.PostDown)
}
2022-01-24 23:58:12 +08:00
if node.MTU != 0 {
wireguard.Section(section_interface).Key("MTU").SetValue(strconv.FormatInt(int64(node.MTU), 10))
}
if err := wireguard.SaveTo(file); err != nil {
return err
}
return nil
}
// UpdatePrivateKey - updates the private key of a wireguard config file
2022-01-23 00:38:56 +08:00
func UpdatePrivateKey(file, privateKey string) error {
options := ini.LoadOptions{
AllowNonUniqueSections: true,
AllowShadows: true,
}
wireguard, err := ini.LoadSources(options, file)
if err != nil {
return err
}
2022-01-05 23:32:20 +08:00
wireguard.Section(section_interface).Key("PrivateKey").SetValue(privateKey)
if err := wireguard.SaveTo(file); err != nil {
return err
}
return nil
}
2022-03-15 09:03:49 +08:00
2022-03-21 19:07:50 +08:00
// UpdateKeepAlive - updates the persistentkeepalive of all peers
func UpdateKeepAlive(file string, keepalive int32) error {
options := ini.LoadOptions{
AllowNonUniqueSections: true,
AllowShadows: true,
}
wireguard, err := ini.LoadSources(options, file)
if err != nil {
return err
}
peers, err := wireguard.SectionsByName(section_peers)
if err != nil {
return err
}
newvalue := strconv.Itoa(int(keepalive))
for i := range peers {
wireguard.SectionWithIndex(section_peers, i).Key("PersistentKeepALive").SetValue(newvalue)
}
if err := wireguard.SaveTo(file); err != nil {
return err
}
return nil
}
2022-03-15 09:03:49 +08:00
// RemoveConfGraceful - Run remove conf and wait for it to actually be gone before proceeding
func RemoveConfGraceful(ifacename string) {
// ensure you clear any existing interface first
wgclient, err := wgctrl.New()
if err != nil {
logger.Log(0, "could not create wgclient")
2022-03-15 09:03:49 +08:00
return
}
defer wgclient.Close()
d, _ := wgclient.Device(ifacename)
startTime := time.Now()
for d != nil && d.Name == ifacename {
if err = RemoveConf(ifacename, false); err != nil { // remove interface first
if strings.Contains(err.Error(), "does not exist") {
err = nil
break
}
}
time.Sleep(time.Second >> 2)
d, _ = wgclient.Device(ifacename)
if time.Now().After(startTime.Add(time.Second << 4)) {
break
}
}
2022-03-15 23:11:22 +08:00
time.Sleep(time.Second << 1)
2022-03-15 09:03:49 +08:00
}
2022-04-26 04:30:18 +08:00
// GetDevicePeers - gets the current device's peers
func GetDevicePeers(iface string) ([]wgtypes.Peer, error) {
if ncutils.IsFreeBSD() {
if devicePeers, err := ncutils.GetPeers(iface); err != nil {
return nil, err
} else {
return devicePeers, nil
}
} else {
client, err := wgctrl.New()
if err != nil {
logger.Log(0, "failed to start wgctrl")
return nil, err
}
defer client.Close()
device, err := client.Device(iface)
if err != nil {
logger.Log(0, "failed to parse interface")
return nil, err
}
return device.Peers, nil
}
}