netmaker/netclient/ncutils/peerhelper.go

98 lines
2.5 KiB
Go
Raw Normal View History

2021-11-11 05:08:29 +08:00
package ncutils
import (
2021-12-11 04:01:10 +08:00
"net"
2021-11-11 05:08:29 +08:00
"strconv"
"strings"
"time"
"github.com/gravitl/netmaker/logger"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
2021-11-11 05:08:29 +08:00
)
// GetPeers - gets the peers from a given WireGuard interface
2021-11-11 05:08:29 +08:00
func GetPeers(iface string) ([]wgtypes.Peer, error) {
2021-11-11 06:02:45 +08:00
2021-11-11 05:08:29 +08:00
var peers []wgtypes.Peer
2021-12-11 04:01:10 +08:00
output, err := RunCmd("wg show "+iface+" dump", true)
2021-11-11 05:08:29 +08:00
if err != nil {
return peers, err
}
2021-11-11 06:02:45 +08:00
for i, line := range strings.Split(strings.TrimSuffix(output, "\n"), "\n") {
if i == 0 {
continue
2021-11-11 05:08:29 +08:00
}
var allowedIPs []net.IPNet
2021-11-11 06:02:45 +08:00
fields := strings.Fields(line)
if len(fields) < 4 {
logger.Log(0, "error parsing peer: "+line)
2021-11-11 06:02:45 +08:00
continue
}
2021-11-11 05:08:29 +08:00
pubkeystring := fields[0]
2021-11-11 06:02:45 +08:00
endpointstring := fields[2]
2021-11-11 05:08:29 +08:00
allowedipstring := fields[3]
2021-11-11 06:02:45 +08:00
var pkeepalivestring string
if len(fields) > 7 {
pkeepalivestring = fields[7]
}
2021-11-11 05:08:29 +08:00
// AllowedIPs = private IP + defined networks
pubkey, err := wgtypes.ParseKey(pubkeystring)
if err != nil {
logger.Log(0, "error parsing peer key "+pubkeystring)
2021-11-11 05:08:29 +08:00
continue
}
ipstrings := strings.Split(allowedipstring, ",")
for _, ipstring := range ipstrings {
var netip net.IP
2021-12-11 04:01:10 +08:00
if netip = net.ParseIP(strings.Split(ipstring, "/")[0]); netip != nil {
2021-11-11 05:08:29 +08:00
allowedIPs = append(
allowedIPs,
net.IPNet{
IP: netip,
Mask: netip.DefaultMask(),
},
)
}
}
if len(allowedIPs) == 0 {
logger.Log(0, "error parsing peer "+pubkeystring+", no allowedips found")
2021-11-11 05:08:29 +08:00
continue
}
var endpointarr []string
var endpointip net.IP
2021-12-11 04:01:10 +08:00
if endpointarr = strings.Split(endpointstring, ":"); len(endpointarr) != 2 {
logger.Log(0, "error parsing peer "+pubkeystring+", could not parse endpoint: "+endpointstring)
2021-11-11 05:08:29 +08:00
continue
}
if endpointip = net.ParseIP(endpointarr[0]); endpointip == nil {
logger.Log(0, "error parsing peer "+pubkeystring+", could not parse endpoint: "+endpointarr[0])
2021-11-11 05:08:29 +08:00
continue
}
var port int
if port, err = strconv.Atoi(endpointarr[1]); err != nil {
logger.Log(0, "error parsing peer "+pubkeystring+", could not parse port: "+err.Error())
2021-11-11 05:08:29 +08:00
continue
}
2021-12-11 04:01:10 +08:00
var endpoint = net.UDPAddr{
IP: endpointip,
2021-11-11 05:08:29 +08:00
Port: port,
}
var dur time.Duration
if pkeepalivestring != "" {
2021-12-11 04:01:10 +08:00
if dur, err = time.ParseDuration(pkeepalivestring + "s"); err != nil {
logger.Log(0, "error parsing peer "+pubkeystring+", could not parse keepalive: "+err.Error())
2021-11-11 05:08:29 +08:00
}
}
peers = append(peers, wgtypes.Peer{
2021-12-11 04:01:10 +08:00
PublicKey: pubkey,
Endpoint: &endpoint,
AllowedIPs: allowedIPs,
2021-11-11 05:08:29 +08:00
PersistentKeepaliveInterval: dur,
})
}
return peers, err
2021-11-11 06:02:45 +08:00
}