added logic to check peers

This commit is contained in:
0xdcarns
2022-02-02 22:00:59 -05:00
parent e336d62321
commit 284283c130
5 changed files with 39 additions and 122 deletions

View File

@@ -3,6 +3,7 @@ package wireguard
import (
"errors"
"log"
"net"
"runtime"
"strconv"
"strings"
@@ -25,9 +26,8 @@ const (
// SetPeers - sets peers on a given WireGuard interface
func SetPeers(iface string, keepalive int32, peers []wgtypes.PeerConfig) error {
var oldIPList []string
var newIPList []string
var devicePeers []wgtypes.Peer
var oldPeerAllowedIps = make(map[string][]net.IPNet, len(peers))
var err error
if ncutils.IsFreeBSD() {
if devicePeers, err = ncutils.GetPeers(iface); err != nil {
@@ -59,12 +59,6 @@ func SetPeers(iface string, keepalive int32, peers []wgtypes.PeerConfig) error {
_, err := ncutils.RunCmd("wg set "+iface+" peer "+currentPeer.PublicKey.String()+" remove", true)
if err != nil {
log.Println("error removing peer", peer.Endpoint.String())
} else {
for _, address := range currentPeer.AllowedIPs {
if err = local.DeleteRoute(iface, address.String()); err != nil {
ncutils.PrintLog(err.Error(), 1)
}
}
}
}
}
@@ -109,12 +103,13 @@ func SetPeers(iface string, keepalive int32, peers []wgtypes.PeerConfig) error {
log.Println(output, "error removing peer", currentPeer.PublicKey.String())
}
}
oldPeerAllowedIps[currentPeer.PublicKey.String()] = currentPeer.AllowedIPs
}
if ncutils.IsMac() {
err = SetMacPeerRoutes(iface)
return err
} else if ncutils.IsLinux() {
err = local.SetLinuxPeerRoutes(devicePeers, peers)
local.SetPeerRoutes(iface, oldPeerAllowedIps, peers)
}
return nil