netmaker/logic/peers.go

825 lines
25 KiB
Go
Raw Normal View History

package logic
import (
2022-06-14 22:36:30 +08:00
"errors"
2022-03-14 23:34:25 +08:00
"fmt"
"net"
"strconv"
"strings"
"time"
2022-05-27 04:28:08 +08:00
"github.com/c-robinson/iplib"
"github.com/gravitl/netmaker/database"
2022-01-31 04:18:15 +08:00
"github.com/gravitl/netmaker/logger"
"github.com/gravitl/netmaker/logic/acls/nodeacls"
"github.com/gravitl/netmaker/models"
2022-11-20 12:05:29 +08:00
"github.com/gravitl/netmaker/netclient/ncutils"
2022-11-07 03:55:04 +08:00
"github.com/gravitl/netmaker/nm-proxy/manager"
"github.com/gravitl/netmaker/servercfg"
"golang.org/x/exp/slices"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
)
2022-11-07 03:55:04 +08:00
func GetPeersForProxy(node *models.Node, onlyPeers bool) (manager.ManagerPayload, error) {
proxyPayload := manager.ManagerPayload{}
2022-10-28 19:32:22 +08:00
var peers []wgtypes.PeerConfig
2022-11-07 03:55:04 +08:00
peerConfMap := make(map[string]manager.PeerConf)
2022-10-28 19:32:22 +08:00
var err error
currentPeers, err := GetNetworkNodes(node.Network)
if err != nil {
2022-11-07 03:55:04 +08:00
return proxyPayload, err
2022-10-28 19:32:22 +08:00
}
2022-11-21 12:32:33 +08:00
var metrics *models.Metrics
if servercfg.Is_EE {
metrics, _ = GetMetrics(node.ID)
}
if metrics == nil {
metrics = &models.Metrics{}
}
if metrics.FailoverPeers == nil {
metrics.FailoverPeers = make(map[string]string)
}
2022-11-07 03:55:04 +08:00
if !onlyPeers {
if node.IsRelayed == "yes" {
relayNode := FindRelay(node)
2022-11-18 13:18:28 +08:00
if relayNode != nil {
relayEndpoint, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", relayNode.Endpoint, relayNode.LocalListenPort))
if err != nil {
logger.Log(1, "failed to resolve relay node endpoint: ", err.Error())
}
proxyPayload.IsRelayed = true
proxyPayload.RelayedTo = relayEndpoint
} else {
logger.Log(0, "couldn't find relay node for: ", node.ID, node.PublicKey)
2022-11-07 03:55:04 +08:00
}
}
if node.IsRelay == "yes" {
relayedNodes, err := GetRelayedNodes(node)
if err != nil {
logger.Log(1, "failed to relayed nodes: ", node.Name, err.Error())
proxyPayload.IsRelay = false
} else {
2022-11-09 11:16:16 +08:00
2022-11-07 13:14:14 +08:00
relayPeersMap := make(map[string]manager.RelayedConf)
2022-11-07 03:55:04 +08:00
for _, relayedNode := range relayedNodes {
payload, err := GetPeersForProxy(&relayedNode, true)
if err == nil {
2022-11-07 13:14:14 +08:00
relayedEndpoint, udpErr := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", relayedNode.Endpoint, relayedNode.LocalListenPort))
if udpErr == nil {
relayPeersMap[relayedNode.PublicKey] = manager.RelayedConf{
RelayedPeerEndpoint: relayedEndpoint,
RelayedPeerPubKey: relayedNode.PublicKey,
Peers: payload.Peers,
}
}
2022-11-07 03:55:04 +08:00
}
}
proxyPayload.IsRelay = true
2022-11-07 13:14:14 +08:00
proxyPayload.RelayedPeerConf = relayPeersMap
2022-11-07 03:55:04 +08:00
}
}
2022-11-09 11:16:16 +08:00
2022-11-07 03:55:04 +08:00
}
2022-10-28 19:32:22 +08:00
for _, peer := range currentPeers {
if peer.ID == node.ID {
//skip yourself
continue
}
pubkey, err := wgtypes.ParseKey(peer.PublicKey)
if err != nil {
logger.Log(1, "failed to parse node pub key: ", peer.ID)
continue
}
2022-11-20 12:05:29 +08:00
listenPort := peer.LocalListenPort
if listenPort == 0 {
listenPort = peer.ListenPort
}
endpoint, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", peer.Endpoint, listenPort))
2022-10-28 19:32:22 +08:00
if err != nil {
logger.Log(1, "failed to resolve udp addr for node: ", peer.ID, peer.Endpoint, err.Error())
continue
}
2022-11-21 12:32:33 +08:00
allowedips := GetAllowedIPs(node, &peer, metrics, false)
2022-10-28 19:32:22 +08:00
var keepalive time.Duration
if node.PersistentKeepalive != 0 {
// set_keepalive
keepalive, _ = time.ParseDuration(strconv.FormatInt(int64(node.PersistentKeepalive), 10) + "s")
}
2022-11-20 12:05:29 +08:00
proxyStatus := peer.Proxy
if peer.IsServer == "yes" {
2022-11-20 12:05:29 +08:00
proxyStatus = servercfg.IsProxyEnabled()
}
2022-10-28 19:32:22 +08:00
peers = append(peers, wgtypes.PeerConfig{
PublicKey: pubkey,
Endpoint: endpoint,
AllowedIPs: allowedips,
PersistentKeepaliveInterval: &keepalive,
ReplaceAllowedIPs: true,
})
peerConfMap[peer.PublicKey] = manager.PeerConf{
Address: peer.PrimaryAddress(),
2022-11-20 12:05:29 +08:00
Proxy: proxyStatus,
}
2022-11-20 12:05:29 +08:00
2022-11-07 03:55:04 +08:00
if !onlyPeers && peer.IsRelayed == "yes" {
relayNode := FindRelay(&peer)
if relayNode != nil {
2022-11-07 13:14:14 +08:00
relayTo, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", relayNode.Endpoint, relayNode.LocalListenPort))
2022-11-07 03:55:04 +08:00
if err == nil {
peerConfMap[peer.PublicKey] = manager.PeerConf{
2022-11-07 03:55:04 +08:00
IsRelayed: true,
RelayedTo: relayTo,
2022-11-15 12:01:31 +08:00
Address: peer.PrimaryAddress(),
2022-11-20 12:05:29 +08:00
Proxy: proxyStatus,
2022-11-07 03:55:04 +08:00
}
}
}
}
2022-10-28 19:32:22 +08:00
}
2022-11-21 12:32:33 +08:00
if node.IsIngressGateway == "yes" {
var extPeers []wgtypes.PeerConfig
extPeers, peerConfMap, err = getExtPeersForProxy(node, peerConfMap)
if err == nil {
peers = append(peers, extPeers...)
2022-11-09 11:16:16 +08:00
2022-11-21 12:32:33 +08:00
} else if !database.IsEmptyRecord(err) {
logger.Log(1, "error retrieving external clients:", err.Error())
}
2022-11-09 11:16:16 +08:00
}
2022-11-21 12:32:33 +08:00
2022-11-15 18:54:58 +08:00
proxyPayload.IsIngress = node.IsIngressGateway == "yes"
2022-11-21 12:39:49 +08:00
addr := node.Address
if addr == "" {
addr = node.Address6
}
proxyPayload.WgAddr = addr
2022-11-07 03:55:04 +08:00
proxyPayload.Peers = peers
proxyPayload.PeerMap = peerConfMap
proxyPayload.InterfaceName = node.Interface
return proxyPayload, nil
2022-10-28 19:32:22 +08:00
}
2022-01-26 07:15:27 +08:00
// GetPeerUpdate - gets a wireguard peer config for each peer of a node
func GetPeerUpdate(node *models.Node) (models.PeerUpdate, error) {
var peerUpdate models.PeerUpdate
var peers []wgtypes.PeerConfig
2022-01-31 00:51:39 +08:00
var serverNodeAddresses = []models.ServerAddr{}
2022-06-18 02:36:32 +08:00
var isP2S bool
network, err := GetNetwork(node.Network)
if err != nil {
return peerUpdate, err
} else if network.IsPointToSite == "yes" && node.IsHub != "yes" {
isP2S = true
}
2022-09-14 03:25:56 +08:00
var peerMap = make(models.PeerMap)
2022-01-31 04:18:15 +08:00
var metrics *models.Metrics
if servercfg.Is_EE {
metrics, _ = GetMetrics(node.ID)
}
2022-09-29 02:59:21 +08:00
if metrics == nil {
metrics = &models.Metrics{}
}
2022-09-29 01:05:41 +08:00
if metrics.FailoverPeers == nil {
metrics.FailoverPeers = make(map[string]string)
}
// udppeers = the peers parsed from the local interface
// gives us correct port to reach
udppeers, errN := database.GetPeers(node.Network)
if errN != nil {
logger.Log(2, errN.Error())
}
2022-06-16 02:29:25 +08:00
currentPeers, err := GetNetworkNodes(node.Network)
if err != nil {
return models.PeerUpdate{}, err
}
2022-11-20 12:05:29 +08:00
if node.IsRelayed == "yes" {
return GetPeerUpdateForRelayedNode(node, udppeers)
}
2022-06-16 02:29:25 +08:00
2022-01-31 04:18:15 +08:00
// #1 Set Keepalive values: set_keepalive
// #2 Set local address: set_local - could be a LOT BETTER and fix some bugs with additional logic
// #3 Set allowedips: set_allowedips
2022-01-31 00:51:39 +08:00
for _, peer := range currentPeers {
if peer.ID == node.ID {
//skip yourself
continue
}
2022-06-21 23:19:54 +08:00
// on point to site networks -- get peers regularily if you are the hub --- otherwise the only peer is the hub
if node.NetworkSettings.IsPointToSite == "yes" && node.IsHub == "no" && peer.IsHub == "no" {
continue
}
if node.Connected != "yes" {
//skip unconnected nodes
continue
}
2022-06-21 23:19:54 +08:00
// if the node is not a server, set the endpoint
var setEndpoint = !(node.IsServer == "yes")
2022-11-20 12:05:29 +08:00
if peer.IsRelayed == "yes" {
if !(node.IsRelay == "yes" && ncutils.StringSliceContains(node.RelayAddrs, peer.PrimaryAddress())) {
//skip -- will be added to relay
continue
} else if node.IsRelay == "yes" && ncutils.StringSliceContains(node.RelayAddrs, peer.PrimaryAddress()) {
// dont set peer endpoint if it's relayed by node
setEndpoint = false
}
}
2022-06-14 22:36:30 +08:00
if !nodeacls.AreNodesAllowed(nodeacls.NetworkID(node.Network), nodeacls.NodeID(node.ID), nodeacls.NodeID(peer.ID)) {
//skip if not permitted by acl
continue
}
2022-06-18 02:36:32 +08:00
if isP2S && peer.IsHub != "yes" {
continue
}
2022-09-29 03:49:02 +08:00
if len(metrics.FailoverPeers[peer.ID]) > 0 && IsFailoverPresent(node.Network) {
2022-09-29 03:22:12 +08:00
logger.Log(2, "peer", peer.Name, peer.PrimaryAddress(), "was found to be in failover peers list for node", node.Name, node.PrimaryAddress())
2022-09-29 02:59:21 +08:00
continue
}
pubkey, err := wgtypes.ParseKey(peer.PublicKey)
if err != nil {
return models.PeerUpdate{}, err
}
if node.Endpoint == peer.Endpoint {
//peer is on same network
2022-01-31 04:18:15 +08:00
// set_local
if node.LocalAddress != peer.LocalAddress && peer.LocalAddress != "" {
peer.Endpoint = peer.LocalAddress
2022-04-25 01:47:23 +08:00
if peer.LocalListenPort != 0 {
peer.ListenPort = peer.LocalListenPort
}
2022-04-19 20:07:21 +08:00
} else {
continue
}
}
2022-06-15 20:45:11 +08:00
// set address if setEndpoint is true
// otherwise, will get inserted as empty value
var address *net.UDPAddr
// Sets ListenPort to UDP Hole Punching Port assuming:
// - UDP Hole Punching is enabled
// - udppeers retrieval did not return an error
// - the endpoint is valid
2022-06-15 20:45:11 +08:00
if setEndpoint {
var setUDPPort = false
if peer.UDPHolePunch == "yes" && errN == nil && CheckEndpoint(udppeers[peer.PublicKey]) {
endpointstring := udppeers[peer.PublicKey]
endpointarr := strings.Split(endpointstring, ":")
if len(endpointarr) == 2 {
port, err := strconv.Atoi(endpointarr[1])
if err == nil {
setUDPPort = true
peer.ListenPort = int32(port)
}
}
}
2022-06-15 20:45:11 +08:00
// if udp hole punching is on, but udp hole punching did not set it, use the LocalListenPort instead
// or, if port is for some reason zero use the LocalListenPort
// but only do this if LocalListenPort is not zero
if ((peer.UDPHolePunch == "yes" && !setUDPPort) || peer.ListenPort == 0) && peer.LocalListenPort != 0 {
peer.ListenPort = peer.LocalListenPort
}
2022-06-15 20:45:11 +08:00
endpoint := peer.Endpoint + ":" + strconv.FormatInt(int64(peer.ListenPort), 10)
address, err = net.ResolveUDPAddr("udp", endpoint)
if err != nil {
return models.PeerUpdate{}, err
}
}
2022-09-29 02:59:21 +08:00
2022-11-21 12:32:33 +08:00
allowedips := GetAllowedIPs(node, &peer, metrics, true)
var keepalive time.Duration
if node.PersistentKeepalive != 0 {
2022-01-31 04:18:15 +08:00
// set_keepalive
keepalive, _ = time.ParseDuration(strconv.FormatInt(int64(node.PersistentKeepalive), 10) + "s")
}
var peerData = wgtypes.PeerConfig{
PublicKey: pubkey,
Endpoint: address,
ReplaceAllowedIPs: true,
AllowedIPs: allowedips,
PersistentKeepaliveInterval: &keepalive,
}
2022-06-15 20:45:11 +08:00
peers = append(peers, peerData)
2022-09-14 03:25:56 +08:00
peerMap[peer.PublicKey] = models.IDandAddr{
Name: peer.Name,
ID: peer.ID,
Address: peer.PrimaryAddress(),
IsServer: peer.IsServer,
}
2022-01-26 11:14:31 +08:00
if peer.IsServer == "yes" {
serverNodeAddresses = append(serverNodeAddresses, models.ServerAddr{IsLeader: IsLeader(&peer), Address: peer.Address})
2022-01-26 11:14:31 +08:00
}
}
2022-11-15 21:07:58 +08:00
if node.IsIngressGateway == "yes" {
extPeers, idsAndAddr, err := getExtPeers(node)
if err == nil {
peers = append(peers, extPeers...)
for i := range idsAndAddr {
peerMap[idsAndAddr[i].ID] = idsAndAddr[i]
}
} else if !database.IsEmptyRecord(err) {
logger.Log(1, "error retrieving external clients:", err.Error())
2022-01-31 04:18:15 +08:00
}
}
peerUpdate.Network = node.Network
peerUpdate.ServerVersion = servercfg.Version
peerUpdate.Peers = peers
2022-01-26 11:14:31 +08:00
peerUpdate.ServerAddrs = serverNodeAddresses
peerUpdate.DNS = getPeerDNS(node.Network)
2022-09-14 03:25:56 +08:00
peerUpdate.PeerIDs = peerMap
return peerUpdate, nil
}
2022-09-14 03:25:56 +08:00
func getExtPeers(node *models.Node) ([]wgtypes.PeerConfig, []models.IDandAddr, error) {
2022-01-31 04:18:15 +08:00
var peers []wgtypes.PeerConfig
2022-09-14 03:25:56 +08:00
var idsAndAddr []models.IDandAddr
2022-11-09 11:16:16 +08:00
extPeers, err := GetNetworkExtClients(node.Network)
2022-01-31 04:18:15 +08:00
if err != nil {
2022-09-14 03:25:56 +08:00
return peers, idsAndAddr, err
2022-01-31 04:18:15 +08:00
}
for _, extPeer := range extPeers {
pubkey, err := wgtypes.ParseKey(extPeer.PublicKey)
if err != nil {
logger.Log(1, "error parsing ext pub key:", err.Error())
continue
}
if node.PublicKey == extPeer.PublicKey {
continue
}
2022-04-23 01:04:34 +08:00
var allowedips []net.IPNet
2022-01-31 04:18:15 +08:00
var peer wgtypes.PeerConfig
2022-04-23 01:04:34 +08:00
if extPeer.Address != "" {
var peeraddr = net.IPNet{
IP: net.ParseIP(extPeer.Address),
Mask: net.CIDRMask(32, 32),
}
if peeraddr.IP != nil && peeraddr.Mask != nil {
allowedips = append(allowedips, peeraddr)
}
2022-01-31 04:18:15 +08:00
}
if extPeer.Address6 != "" {
var addr6 = net.IPNet{
IP: net.ParseIP(extPeer.Address6),
Mask: net.CIDRMask(128, 128),
}
2022-04-23 01:04:34 +08:00
if addr6.IP != nil && addr6.Mask != nil {
allowedips = append(allowedips, addr6)
}
2022-01-31 04:18:15 +08:00
}
2022-09-14 03:25:56 +08:00
primaryAddr := extPeer.Address
if primaryAddr == "" {
primaryAddr = extPeer.Address6
}
2022-01-31 04:18:15 +08:00
peer = wgtypes.PeerConfig{
PublicKey: pubkey,
ReplaceAllowedIPs: true,
AllowedIPs: allowedips,
}
peers = append(peers, peer)
2022-09-14 03:25:56 +08:00
idsAndAddr = append(idsAndAddr, models.IDandAddr{
ID: peer.PublicKey.String(),
Address: primaryAddr,
})
2022-01-31 04:18:15 +08:00
}
2022-09-14 03:25:56 +08:00
return peers, idsAndAddr, nil
2022-01-31 04:18:15 +08:00
}
2022-11-09 11:16:16 +08:00
func getExtPeersForProxy(node *models.Node, proxyPeerConf map[string]manager.PeerConf) ([]wgtypes.PeerConfig, map[string]manager.PeerConf, error) {
var peers []wgtypes.PeerConfig
extPeers, err := GetNetworkExtClients(node.Network)
if err != nil {
return peers, proxyPeerConf, err
}
for _, extPeer := range extPeers {
pubkey, err := wgtypes.ParseKey(extPeer.PublicKey)
if err != nil {
logger.Log(1, "error parsing ext pub key:", err.Error())
continue
}
if node.PublicKey == extPeer.PublicKey {
continue
}
var allowedips []net.IPNet
var peer wgtypes.PeerConfig
if extPeer.Address != "" {
var peeraddr = net.IPNet{
IP: net.ParseIP(extPeer.Address),
Mask: net.CIDRMask(32, 32),
}
if peeraddr.IP != nil && peeraddr.Mask != nil {
allowedips = append(allowedips, peeraddr)
}
}
if extPeer.Address6 != "" {
var addr6 = net.IPNet{
IP: net.ParseIP(extPeer.Address6),
Mask: net.CIDRMask(128, 128),
}
if addr6.IP != nil && addr6.Mask != nil {
allowedips = append(allowedips, addr6)
}
}
peer = wgtypes.PeerConfig{
PublicKey: pubkey,
ReplaceAllowedIPs: true,
AllowedIPs: allowedips,
}
extConf := manager.PeerConf{
IsExtClient: true,
2022-11-14 16:40:52 +08:00
Address: extPeer.Address,
2022-11-09 11:16:16 +08:00
}
if extPeer.IngressGatewayID == node.ID {
extConf.IsAttachedExtClient = true
}
ingGatewayUdpAddr, err := net.ResolveUDPAddr("udp", extPeer.IngressGatewayEndpoint)
if err == nil {
extConf.IngressGatewayEndPoint = ingGatewayUdpAddr
}
proxyPeerConf[peer.PublicKey.String()] = extConf
peers = append(peers, peer)
}
return peers, proxyPeerConf, nil
}
2022-01-26 07:15:27 +08:00
// GetAllowedIPs - calculates the wireguard allowedip field for a peer of a node based on the peer and node settings
2022-11-21 12:32:33 +08:00
func GetAllowedIPs(node, peer *models.Node, metrics *models.Metrics, fetchRelayedIps bool) []net.IPNet {
var allowedips []net.IPNet
allowedips = getNodeAllowedIPs(peer, node)
2022-06-14 22:36:30 +08:00
// handle ingress gateway peers
2022-11-15 21:07:58 +08:00
if peer.IsIngressGateway == "yes" {
extPeers, _, err := getExtPeers(peer)
if err != nil {
logger.Log(2, "could not retrieve ext peers for ", peer.Name, err.Error())
}
for _, extPeer := range extPeers {
allowedips = append(allowedips, extPeer.AllowedIPs...)
}
// if node is a failover node, add allowed ips from nodes it is handling
2022-11-21 12:32:33 +08:00
if metrics != nil && peer.Failover == "yes" && metrics.FailoverPeers != nil {
2022-11-15 21:07:58 +08:00
// traverse through nodes that need handling
logger.Log(3, "peer", peer.Name, "was found to be failover for", node.Name, "checking failover peers...")
for k := range metrics.FailoverPeers {
// if FailoverNode is me for this node, add allowedips
if metrics.FailoverPeers[k] == peer.ID {
// get original node so we can traverse the allowed ips
nodeToFailover, err := GetNodeByID(k)
if err == nil {
failoverNodeMetrics, err := GetMetrics(nodeToFailover.ID)
if err == nil && failoverNodeMetrics != nil {
if len(failoverNodeMetrics.NodeName) > 0 {
allowedips = append(allowedips, getNodeAllowedIPs(&nodeToFailover, peer)...)
logger.Log(0, "failing over node", nodeToFailover.Name, nodeToFailover.PrimaryAddress(), "to failover node", peer.Name)
}
}
}
}
}
}
}
2022-06-14 22:36:30 +08:00
// handle relay gateway peers
2022-11-21 12:32:33 +08:00
if fetchRelayedIps && peer.IsRelay == "yes" {
2022-11-20 12:05:29 +08:00
for _, ip := range peer.RelayAddrs {
//find node ID of relayed peer
relayedPeer, err := findNode(ip)
if err != nil {
logger.Log(0, "failed to find node for ip ", ip, err.Error())
continue
}
if relayedPeer == nil {
continue
}
if relayedPeer.ID == node.ID {
//skip self
continue
}
//check if acl permits comms
if !nodeacls.AreNodesAllowed(nodeacls.NetworkID(node.Network), nodeacls.NodeID(node.ID), nodeacls.NodeID(relayedPeer.ID)) {
continue
}
if iplib.Version(net.ParseIP(ip)) == 4 {
relayAddr := net.IPNet{
IP: net.ParseIP(ip),
Mask: net.CIDRMask(32, 32),
}
allowedips = append(allowedips, relayAddr)
}
if iplib.Version(net.ParseIP(ip)) == 6 {
relayAddr := net.IPNet{
IP: net.ParseIP(ip),
Mask: net.CIDRMask(128, 128),
}
allowedips = append(allowedips, relayAddr)
}
relayedNode, err := findNode(ip)
if err != nil {
logger.Log(1, "unable to find node for relayed address", ip, err.Error())
continue
}
if relayedNode.IsEgressGateway == "yes" {
extAllowedIPs := getEgressIPs(node, relayedNode)
allowedips = append(allowedips, extAllowedIPs...)
}
if relayedNode.IsIngressGateway == "yes" {
extPeers, _, err := getExtPeers(relayedNode)
if err == nil {
for _, extPeer := range extPeers {
allowedips = append(allowedips, extPeer.AllowedIPs...)
}
} else {
logger.Log(0, "failed to retrieve extclients from relayed ingress", err.Error())
}
}
}
}
return allowedips
}
func getPeerDNS(network string) string {
var dns string
if nodes, err := GetNetworkNodes(network); err == nil {
for i := range nodes {
dns = dns + fmt.Sprintf("%s %s.%s\n", nodes[i].Address, nodes[i].Name, nodes[i].Network)
}
}
if customDNSEntries, err := GetCustomDNS(network); err == nil {
for _, entry := range customDNSEntries {
// TODO - filter entries based on ACLs / given peers vs nodes in network
dns = dns + fmt.Sprintf("%s %s.%s\n", entry.Address, entry.Name, entry.Network)
}
}
return dns
}
2022-06-14 22:36:30 +08:00
2022-06-14 23:30:22 +08:00
// GetPeerUpdateForRelayedNode - calculates peer update for a relayed node by getting the relay
// copying the relay node's allowed ips and making appropriate substitutions
2022-06-16 02:29:25 +08:00
func GetPeerUpdateForRelayedNode(node *models.Node, udppeers map[string]string) (models.PeerUpdate, error) {
2022-06-14 22:36:30 +08:00
var peerUpdate models.PeerUpdate
var peers []wgtypes.PeerConfig
var serverNodeAddresses = []models.ServerAddr{}
var allowedips []net.IPNet
//find node that is relaying us
relay := FindRelay(node)
if relay == nil {
return models.PeerUpdate{}, errors.New("not found")
}
2022-06-16 02:29:25 +08:00
2022-06-14 22:36:30 +08:00
//add relay to lists of allowed ip
if relay.Address != "" {
relayIP := net.IPNet{
IP: net.ParseIP(relay.Address),
Mask: net.CIDRMask(32, 32),
}
allowedips = append(allowedips, relayIP)
}
if relay.Address6 != "" {
relayIP6 := net.IPNet{
IP: net.ParseIP(relay.Address6),
Mask: net.CIDRMask(128, 128),
}
allowedips = append(allowedips, relayIP6)
}
//get PeerUpdate for relayed node
relayPeerUpdate, err := GetPeerUpdate(relay)
if err != nil {
return models.PeerUpdate{}, err
}
//add the relays allowed ips from all of the relay's peers
for _, peer := range relayPeerUpdate.Peers {
allowedips = append(allowedips, peer.AllowedIPs...)
}
//delete any ips not permitted by acl
2022-06-15 23:18:49 +08:00
for i := len(allowedips) - 1; i >= 0; i-- {
target, err := findNode(allowedips[i].IP.String())
2022-06-14 22:36:30 +08:00
if err != nil {
2022-06-15 23:18:49 +08:00
logger.Log(0, "failed to find node for ip", allowedips[i].IP.String(), err.Error())
2022-06-14 22:36:30 +08:00
continue
}
2022-06-15 03:39:59 +08:00
if target == nil {
2022-06-15 23:18:49 +08:00
logger.Log(0, "failed to find node for ip", allowedips[i].IP.String())
2022-06-15 03:39:59 +08:00
continue
}
2022-06-14 22:36:30 +08:00
if !nodeacls.AreNodesAllowed(nodeacls.NetworkID(node.Network), nodeacls.NodeID(node.ID), nodeacls.NodeID(target.ID)) {
logger.Log(0, "deleting node from relayednode per acl", node.Name, target.Name)
2022-06-15 23:18:49 +08:00
allowedips = append(allowedips[:i], allowedips[i+1:]...)
2022-06-14 22:36:30 +08:00
}
}
//delete self from allowed ips
2022-06-15 23:18:49 +08:00
for i := len(allowedips) - 1; i >= 0; i-- {
if allowedips[i].IP.String() == node.Address || allowedips[i].IP.String() == node.Address6 {
allowedips = append(allowedips[:i], allowedips[i+1:]...)
2022-06-14 22:36:30 +08:00
}
}
//delete egressrange from allowedip if we are egress gateway
if node.IsEgressGateway == "yes" {
for i := len(allowedips) - 1; i >= 0; i-- {
if StringSliceContains(node.EgressGatewayRanges, allowedips[i].String()) {
allowedips = append(allowedips[:i], allowedips[i+1:]...)
}
}
}
2022-07-26 23:24:23 +08:00
//delete extclients from allowedip if we are ingress gateway
if node.IsIngressGateway == "yes" {
for i := len(allowedips) - 1; i >= 0; i-- {
if strings.Contains(node.IngressGatewayRange, allowedips[i].IP.String()) {
allowedips = append(allowedips[:i], allowedips[i+1:]...)
}
}
}
//add egress range if relay is egress
if relay.IsEgressGateway == "yes" {
var ip *net.IPNet
for _, cidr := range relay.EgressGatewayRanges {
_, ip, err = net.ParseCIDR(cidr)
if err != nil {
continue
}
}
allowedips = append(allowedips, *ip)
}
2022-06-14 22:36:30 +08:00
pubkey, err := wgtypes.ParseKey(relay.PublicKey)
if err != nil {
return models.PeerUpdate{}, err
}
2022-06-16 02:29:25 +08:00
var setUDPPort = false
if relay.UDPHolePunch == "yes" && CheckEndpoint(udppeers[relay.PublicKey]) {
endpointstring := udppeers[relay.PublicKey]
endpointarr := strings.Split(endpointstring, ":")
if len(endpointarr) == 2 {
port, err := strconv.Atoi(endpointarr[1])
if err == nil {
setUDPPort = true
relay.ListenPort = int32(port)
}
}
}
// if udp hole punching is on, but udp hole punching did not set it, use the LocalListenPort instead
// or, if port is for some reason zero use the LocalListenPort
// but only do this if LocalListenPort is not zero
if ((relay.UDPHolePunch == "yes" && !setUDPPort) || relay.ListenPort == 0) && relay.LocalListenPort != 0 {
relay.ListenPort = relay.LocalListenPort
}
2022-06-14 22:36:30 +08:00
endpoint := relay.Endpoint + ":" + strconv.FormatInt(int64(relay.ListenPort), 10)
address, err := net.ResolveUDPAddr("udp", endpoint)
if err != nil {
return models.PeerUpdate{}, err
}
var keepalive time.Duration
if node.PersistentKeepalive != 0 {
// set_keepalive
keepalive, _ = time.ParseDuration(strconv.FormatInt(int64(node.PersistentKeepalive), 10) + "s")
}
var peerData = wgtypes.PeerConfig{
PublicKey: pubkey,
Endpoint: address,
ReplaceAllowedIPs: true,
AllowedIPs: allowedips,
PersistentKeepaliveInterval: &keepalive,
}
peers = append(peers, peerData)
if relay.IsServer == "yes" {
serverNodeAddresses = append(serverNodeAddresses, models.ServerAddr{IsLeader: IsLeader(relay), Address: relay.Address})
}
2022-07-26 23:24:23 +08:00
//if ingress add extclients
if node.IsIngressGateway == "yes" {
2022-09-14 03:25:56 +08:00
extPeers, _, err := getExtPeers(node)
2022-07-26 23:24:23 +08:00
if err == nil {
peers = append(peers, extPeers...)
} else {
2022-07-28 19:57:19 +08:00
logger.Log(2, "could not retrieve ext peers for ", node.Name, err.Error())
2022-07-26 23:24:23 +08:00
}
}
2022-06-14 22:36:30 +08:00
peerUpdate.Network = node.Network
peerUpdate.ServerVersion = servercfg.Version
peerUpdate.Peers = peers
peerUpdate.ServerAddrs = serverNodeAddresses
peerUpdate.DNS = getPeerDNS(node.Network)
return peerUpdate, nil
}
func getEgressIPs(node, peer *models.Node) []net.IPNet {
//check for internet gateway
internetGateway := false
if slices.Contains(peer.EgressGatewayRanges, "0.0.0.0/0") || slices.Contains(peer.EgressGatewayRanges, "::/0") {
internetGateway = true
}
allowedips := []net.IPNet{}
for _, iprange := range peer.EgressGatewayRanges { // go through each cidr for egress gateway
_, ipnet, err := net.ParseCIDR(iprange) // confirming it's valid cidr
if err != nil {
logger.Log(1, "could not parse gateway IP range. Not adding ", iprange)
continue // if can't parse CIDR
}
nodeEndpointArr := strings.Split(peer.Endpoint, ":") // getting the public ip of node
if ipnet.Contains(net.ParseIP(nodeEndpointArr[0])) && !internetGateway { // ensuring egress gateway range does not contain endpoint of node
logger.Log(2, "egress IP range of ", iprange, " overlaps with ", node.Endpoint, ", omitting")
continue // skip adding egress range if overlaps with node's ip
}
// TODO: Could put in a lot of great logic to avoid conflicts / bad routes
if ipnet.Contains(net.ParseIP(node.LocalAddress)) && !internetGateway { // ensuring egress gateway range does not contain public ip of node
logger.Log(2, "egress IP range of ", iprange, " overlaps with ", node.LocalAddress, ", omitting")
continue // skip adding egress range if overlaps with node's local ip
}
if err != nil {
logger.Log(1, "error encountered when setting egress range", err.Error())
} else {
allowedips = append(allowedips, *ipnet)
}
}
return allowedips
}
func getNodeAllowedIPs(peer, node *models.Node) []net.IPNet {
var allowedips = []net.IPNet{}
if peer.Address != "" {
var peeraddr = net.IPNet{
IP: net.ParseIP(peer.Address),
Mask: net.CIDRMask(32, 32),
}
allowedips = append(allowedips, peeraddr)
}
if peer.Address6 != "" {
var addr6 = net.IPNet{
IP: net.ParseIP(peer.Address6),
Mask: net.CIDRMask(128, 128),
}
allowedips = append(allowedips, addr6)
}
// handle manually set peers
for _, allowedIp := range peer.AllowedIPs {
// parsing as a CIDR first. If valid CIDR, append
if _, ipnet, err := net.ParseCIDR(allowedIp); err == nil {
nodeEndpointArr := strings.Split(node.Endpoint, ":")
if !ipnet.Contains(net.IP(nodeEndpointArr[0])) && ipnet.IP.String() != peer.Address { // don't need to add an allowed ip that already exists..
allowedips = append(allowedips, *ipnet)
}
} else { // parsing as an IP second. If valid IP, check if ipv4 or ipv6, then append
if iplib.Version(net.ParseIP(allowedIp)) == 4 && allowedIp != peer.Address {
ipnet := net.IPNet{
IP: net.ParseIP(allowedIp),
Mask: net.CIDRMask(32, 32),
}
allowedips = append(allowedips, ipnet)
} else if iplib.Version(net.ParseIP(allowedIp)) == 6 && allowedIp != peer.Address6 {
ipnet := net.IPNet{
IP: net.ParseIP(allowedIp),
Mask: net.CIDRMask(128, 128),
}
allowedips = append(allowedips, ipnet)
}
}
}
// handle egress gateway peers
if peer.IsEgressGateway == "yes" {
//hasGateway = true
egressIPs := getEgressIPs(node, peer)
// remove internet gateway if server
if node.IsServer == "yes" {
for i := len(egressIPs) - 1; i >= 0; i-- {
if egressIPs[i].String() == "0.0.0.0/0" || egressIPs[i].String() == "::/0" {
egressIPs = append(egressIPs[:i], egressIPs[i+1:]...)
}
}
}
allowedips = append(allowedips, egressIPs...)
}
return allowedips
}