peers.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. package logic
  2. import (
  3. "errors"
  4. "net"
  5. "net/netip"
  6. "github.com/gravitl/netmaker/database"
  7. "github.com/gravitl/netmaker/logger"
  8. "github.com/gravitl/netmaker/logic/acls/nodeacls"
  9. "github.com/gravitl/netmaker/models"
  10. "github.com/gravitl/netmaker/servercfg"
  11. "golang.org/x/exp/slices"
  12. "golang.org/x/exp/slog"
  13. "golang.zx2c4.com/wireguard/wgctrl/wgtypes"
  14. )
  15. // GetPeerUpdateForHost - gets the consolidated peer update for the host from all networks
  16. func GetPeerUpdateForHost(network string, host *models.Host, allNodes []models.Node,
  17. deletedNode *models.Node, deletedClients []models.ExtClient) (models.HostPeerUpdate, error) {
  18. if host == nil {
  19. return models.HostPeerUpdate{}, errors.New("host is nil")
  20. }
  21. // track which nodes are deleted
  22. // after peer calculation, if peer not in list, add delete config of peer
  23. hostPeerUpdate := models.HostPeerUpdate{
  24. Host: *host,
  25. Server: servercfg.GetServer(),
  26. ServerVersion: servercfg.GetVersion(),
  27. ServerAddrs: []models.ServerAddr{},
  28. FwUpdate: models.FwUpdate{
  29. IngressInfo: models.IngressInfo{
  30. ExtPeers: make(map[string]models.ExtClientInfo),
  31. },
  32. EgressInfo: make(map[string]models.EgressInfo),
  33. },
  34. PeerIDs: make(models.PeerMap, 0),
  35. Peers: []wgtypes.PeerConfig{},
  36. NodePeers: []wgtypes.PeerConfig{},
  37. HostNetworkInfo: models.HostInfoMap{},
  38. }
  39. // endpoint detection always comes from the server
  40. hostPeerUpdate.EndpointDetection = servercfg.EndpointDetectionEnabled()
  41. slog.Debug("peer update for host", "hostId", host.ID.String())
  42. peerIndexMap := make(map[string]int)
  43. for _, nodeID := range host.Nodes {
  44. nodeID := nodeID
  45. node, err := GetNodeByID(nodeID)
  46. if err != nil {
  47. continue
  48. }
  49. if !node.Connected || node.PendingDelete || node.Action == models.NODE_DELETE {
  50. continue
  51. }
  52. if host.OS == models.OS_Types.IoT {
  53. hostPeerUpdate.NodeAddrs = append(hostPeerUpdate.NodeAddrs, node.PrimaryAddressIPNet())
  54. if node.IsRelayed {
  55. relayNode, err := GetNodeByID(node.RelayedBy)
  56. if err != nil {
  57. continue
  58. }
  59. relayHost, err := GetHost(relayNode.HostID.String())
  60. if err != nil {
  61. continue
  62. }
  63. relayPeer := wgtypes.PeerConfig{
  64. PublicKey: relayHost.PublicKey,
  65. PersistentKeepaliveInterval: &relayNode.PersistentKeepalive,
  66. ReplaceAllowedIPs: true,
  67. AllowedIPs: GetAllowedIPs(&node, &relayNode, nil),
  68. }
  69. uselocal := false
  70. if host.EndpointIP.String() == relayHost.EndpointIP.String() {
  71. // peer is on same network
  72. // set to localaddress
  73. uselocal = true
  74. if node.LocalAddress.IP == nil {
  75. // use public endpint
  76. uselocal = false
  77. }
  78. if node.LocalAddress.String() == relayNode.LocalAddress.String() {
  79. uselocal = false
  80. }
  81. }
  82. relayPeer.Endpoint = &net.UDPAddr{
  83. IP: relayHost.EndpointIP,
  84. Port: GetPeerListenPort(relayHost),
  85. }
  86. if uselocal {
  87. relayPeer.Endpoint.IP = relayNode.LocalAddress.IP
  88. relayPeer.Endpoint.Port = relayHost.ListenPort
  89. }
  90. hostPeerUpdate.Peers = append(hostPeerUpdate.Peers, relayPeer)
  91. } else if deletedNode != nil && deletedNode.IsRelay {
  92. relayHost, err := GetHost(deletedNode.HostID.String())
  93. if err != nil {
  94. continue
  95. }
  96. relayPeer := wgtypes.PeerConfig{
  97. PublicKey: relayHost.PublicKey,
  98. Remove: true,
  99. }
  100. hostPeerUpdate.Peers = append(hostPeerUpdate.Peers, relayPeer)
  101. }
  102. continue
  103. }
  104. currentPeers := GetNetworkNodesMemory(allNodes, node.Network)
  105. var nodePeerMap map[string]models.PeerRouteInfo
  106. if node.IsIngressGateway || node.IsEgressGateway {
  107. nodePeerMap = make(map[string]models.PeerRouteInfo)
  108. }
  109. for _, peer := range currentPeers {
  110. peer := peer
  111. if peer.ID.String() == node.ID.String() {
  112. logger.Log(2, "peer update, skipping self")
  113. //skip yourself
  114. continue
  115. }
  116. peerHost, err := GetHost(peer.HostID.String())
  117. if err != nil {
  118. logger.Log(1, "no peer host", peer.HostID.String(), err.Error())
  119. return models.HostPeerUpdate{}, err
  120. }
  121. peerConfig := wgtypes.PeerConfig{
  122. PublicKey: peerHost.PublicKey,
  123. PersistentKeepaliveInterval: &peer.PersistentKeepalive,
  124. ReplaceAllowedIPs: true,
  125. }
  126. if peer.IsEgressGateway {
  127. hostPeerUpdate.EgressRoutes = append(hostPeerUpdate.EgressRoutes, models.EgressNetworkRoutes{
  128. NodeAddr: node.PrimaryAddressIPNet(),
  129. EgressRanges: peer.EgressGatewayRanges,
  130. })
  131. }
  132. if node.IsIngressGateway || node.IsEgressGateway {
  133. if peer.IsIngressGateway {
  134. _, extPeerIDAndAddrs, err := getExtPeers(&peer, &node)
  135. if err == nil {
  136. for _, extPeerIdAndAddr := range extPeerIDAndAddrs {
  137. extPeerIdAndAddr := extPeerIdAndAddr
  138. nodePeerMap[extPeerIdAndAddr.ID] = models.PeerRouteInfo{
  139. PeerAddr: net.IPNet{
  140. IP: net.ParseIP(extPeerIdAndAddr.Address),
  141. Mask: getCIDRMaskFromAddr(extPeerIdAndAddr.Address),
  142. },
  143. PeerKey: extPeerIdAndAddr.ID,
  144. Allow: true,
  145. ID: extPeerIdAndAddr.ID,
  146. }
  147. }
  148. }
  149. }
  150. if node.IsIngressGateway && peer.IsEgressGateway {
  151. hostPeerUpdate.FwUpdate.IngressInfo.EgressRanges = append(hostPeerUpdate.FwUpdate.IngressInfo.EgressRanges,
  152. peer.EgressGatewayRanges...)
  153. }
  154. nodePeerMap[peerHost.PublicKey.String()] = models.PeerRouteInfo{
  155. PeerAddr: net.IPNet{
  156. IP: net.ParseIP(peer.PrimaryAddress()),
  157. Mask: getCIDRMaskFromAddr(peer.PrimaryAddress()),
  158. },
  159. PeerKey: peerHost.PublicKey.String(),
  160. Allow: true,
  161. ID: peer.ID.String(),
  162. }
  163. }
  164. if (node.IsRelayed && node.RelayedBy != peer.ID.String()) || (peer.IsRelayed && peer.RelayedBy != node.ID.String()) {
  165. // if node is relayed and peer is not the relay, set remove to true
  166. if _, ok := peerIndexMap[peerHost.PublicKey.String()]; ok {
  167. continue
  168. }
  169. peerConfig.Remove = true
  170. hostPeerUpdate.Peers = append(hostPeerUpdate.Peers, peerConfig)
  171. peerIndexMap[peerHost.PublicKey.String()] = len(hostPeerUpdate.Peers) - 1
  172. continue
  173. }
  174. uselocal := false
  175. if host.EndpointIP.String() == peerHost.EndpointIP.String() {
  176. // peer is on same network
  177. // set to localaddress
  178. uselocal = true
  179. if node.LocalAddress.IP == nil {
  180. // use public endpint
  181. uselocal = false
  182. }
  183. if node.LocalAddress.String() == peer.LocalAddress.String() {
  184. uselocal = false
  185. }
  186. }
  187. peerConfig.Endpoint = &net.UDPAddr{
  188. IP: peerHost.EndpointIP,
  189. Port: GetPeerListenPort(peerHost),
  190. }
  191. if uselocal {
  192. peerConfig.Endpoint.IP = peer.LocalAddress.IP
  193. peerConfig.Endpoint.Port = peerHost.ListenPort
  194. }
  195. allowedips := GetAllowedIPs(&node, &peer, nil)
  196. if peer.Action != models.NODE_DELETE &&
  197. !peer.PendingDelete &&
  198. peer.Connected &&
  199. nodeacls.AreNodesAllowed(nodeacls.NetworkID(node.Network), nodeacls.NodeID(node.ID.String()), nodeacls.NodeID(peer.ID.String())) &&
  200. (deletedNode == nil || (deletedNode != nil && peer.ID.String() != deletedNode.ID.String())) {
  201. peerConfig.AllowedIPs = allowedips // only append allowed IPs if valid connection
  202. }
  203. peerPort := GetPeerListenPort(peerHost)
  204. var nodePeer wgtypes.PeerConfig
  205. if _, ok := peerIndexMap[peerHost.PublicKey.String()]; !ok {
  206. hostPeerUpdate.Peers = append(hostPeerUpdate.Peers, peerConfig)
  207. peerIndexMap[peerHost.PublicKey.String()] = len(hostPeerUpdate.Peers) - 1
  208. hostPeerUpdate.HostNetworkInfo[peerHost.PublicKey.String()] = models.HostNetworkInfo{
  209. Interfaces: peerHost.Interfaces,
  210. ListenPort: peerPort,
  211. }
  212. nodePeer = peerConfig
  213. } else {
  214. peerAllowedIPs := hostPeerUpdate.Peers[peerIndexMap[peerHost.PublicKey.String()]].AllowedIPs
  215. peerAllowedIPs = append(peerAllowedIPs, peerConfig.AllowedIPs...)
  216. hostPeerUpdate.Peers[peerIndexMap[peerHost.PublicKey.String()]].AllowedIPs = peerAllowedIPs
  217. hostPeerUpdate.Peers[peerIndexMap[peerHost.PublicKey.String()]].Remove = false
  218. hostPeerUpdate.HostNetworkInfo[peerHost.PublicKey.String()] = models.HostNetworkInfo{
  219. Interfaces: peerHost.Interfaces,
  220. ListenPort: peerPort,
  221. }
  222. nodePeer = hostPeerUpdate.Peers[peerIndexMap[peerHost.PublicKey.String()]]
  223. }
  224. if node.Network == network { // add to peers map for metrics
  225. hostPeerUpdate.PeerIDs[peerHost.PublicKey.String()] = models.IDandAddr{
  226. ID: peer.ID.String(),
  227. Address: peer.PrimaryAddress(),
  228. Name: peerHost.Name,
  229. Network: peer.Network,
  230. ListenPort: GetPeerListenPort(peerHost),
  231. }
  232. hostPeerUpdate.NodePeers = append(hostPeerUpdate.NodePeers, nodePeer)
  233. }
  234. }
  235. var extPeers []wgtypes.PeerConfig
  236. var extPeerIDAndAddrs []models.IDandAddr
  237. if node.IsIngressGateway {
  238. hostPeerUpdate.FwUpdate.IsIngressGw = true
  239. extPeers, extPeerIDAndAddrs, err = getExtPeers(&node, &node)
  240. if err == nil {
  241. for _, extPeerIdAndAddr := range extPeerIDAndAddrs {
  242. extPeerIdAndAddr := extPeerIdAndAddr
  243. nodePeerMap[extPeerIdAndAddr.ID] = models.PeerRouteInfo{
  244. PeerAddr: net.IPNet{
  245. IP: net.ParseIP(extPeerIdAndAddr.Address),
  246. Mask: getCIDRMaskFromAddr(extPeerIdAndAddr.Address),
  247. },
  248. PeerKey: extPeerIdAndAddr.ID,
  249. Allow: true,
  250. ID: extPeerIdAndAddr.ID,
  251. }
  252. }
  253. hostPeerUpdate.Peers = append(hostPeerUpdate.Peers, extPeers...)
  254. for _, extPeerIdAndAddr := range extPeerIDAndAddrs {
  255. extPeerIdAndAddr := extPeerIdAndAddr
  256. hostPeerUpdate.FwUpdate.IngressInfo.ExtPeers[extPeerIdAndAddr.ID] = models.ExtClientInfo{
  257. Masquerade: true,
  258. IngGwAddr: net.IPNet{
  259. IP: net.ParseIP(node.PrimaryAddress()),
  260. Mask: getCIDRMaskFromAddr(node.PrimaryAddress()),
  261. },
  262. Network: node.PrimaryNetworkRange(),
  263. ExtPeerAddr: net.IPNet{
  264. IP: net.ParseIP(extPeerIdAndAddr.Address),
  265. Mask: getCIDRMaskFromAddr(extPeerIdAndAddr.Address),
  266. },
  267. ExtPeerKey: extPeerIdAndAddr.ID,
  268. Peers: filterNodeMapForClientACLs(extPeerIdAndAddr.ID, node.Network, nodePeerMap),
  269. }
  270. if node.Network == network {
  271. hostPeerUpdate.PeerIDs[extPeerIdAndAddr.ID] = extPeerIdAndAddr
  272. hostPeerUpdate.NodePeers = append(hostPeerUpdate.NodePeers, extPeers...)
  273. }
  274. }
  275. } else if !database.IsEmptyRecord(err) {
  276. logger.Log(1, "error retrieving external clients:", err.Error())
  277. }
  278. }
  279. if node.IsEgressGateway {
  280. hostPeerUpdate.FwUpdate.IsEgressGw = true
  281. hostPeerUpdate.FwUpdate.EgressInfo[node.ID.String()] = models.EgressInfo{
  282. EgressID: node.ID.String(),
  283. Network: node.PrimaryNetworkRange(),
  284. EgressGwAddr: net.IPNet{
  285. IP: net.ParseIP(node.PrimaryAddress()),
  286. Mask: getCIDRMaskFromAddr(node.PrimaryAddress()),
  287. },
  288. GwPeers: nodePeerMap,
  289. EgressGWCfg: node.EgressGatewayRequest,
  290. }
  291. }
  292. }
  293. // == post peer calculations ==
  294. // indicate removal if no allowed IPs were calculated
  295. for i := range hostPeerUpdate.Peers {
  296. peer := hostPeerUpdate.Peers[i]
  297. if len(peer.AllowedIPs) == 0 {
  298. peer.Remove = true
  299. }
  300. hostPeerUpdate.Peers[i] = peer
  301. }
  302. if deletedNode != nil && host.OS != models.OS_Types.IoT {
  303. peerHost, err := GetHost(deletedNode.HostID.String())
  304. if err == nil && host.ID != peerHost.ID {
  305. if _, ok := peerIndexMap[peerHost.PublicKey.String()]; !ok {
  306. hostPeerUpdate.Peers = append(hostPeerUpdate.Peers, wgtypes.PeerConfig{
  307. PublicKey: peerHost.PublicKey,
  308. Remove: true,
  309. })
  310. }
  311. }
  312. }
  313. for i := range hostPeerUpdate.NodePeers {
  314. peer := hostPeerUpdate.NodePeers[i]
  315. if len(peer.AllowedIPs) == 0 {
  316. peer.Remove = true
  317. }
  318. hostPeerUpdate.NodePeers[i] = peer
  319. }
  320. if len(deletedClients) > 0 {
  321. for i := range deletedClients {
  322. deletedClient := deletedClients[i]
  323. key, err := wgtypes.ParseKey(deletedClient.PublicKey)
  324. if err == nil {
  325. hostPeerUpdate.Peers = append(hostPeerUpdate.Peers, wgtypes.PeerConfig{
  326. PublicKey: key,
  327. Remove: true,
  328. })
  329. }
  330. }
  331. }
  332. return hostPeerUpdate, nil
  333. }
  334. // GetPeerListenPort - given a host, retrieve it's appropriate listening port
  335. func GetPeerListenPort(host *models.Host) int {
  336. peerPort := host.ListenPort
  337. if host.WgPublicListenPort != 0 {
  338. peerPort = host.WgPublicListenPort
  339. }
  340. return peerPort
  341. }
  342. func getExtPeers(node, peer *models.Node) ([]wgtypes.PeerConfig, []models.IDandAddr, error) {
  343. var peers []wgtypes.PeerConfig
  344. var idsAndAddr []models.IDandAddr
  345. extPeers, err := GetNetworkExtClients(node.Network)
  346. if err != nil {
  347. return peers, idsAndAddr, err
  348. }
  349. host, err := GetHost(node.HostID.String())
  350. if err != nil {
  351. return peers, idsAndAddr, err
  352. }
  353. for _, extPeer := range extPeers {
  354. extPeer := extPeer
  355. if !IsClientNodeAllowed(&extPeer, peer.ID.String()) {
  356. continue
  357. }
  358. pubkey, err := wgtypes.ParseKey(extPeer.PublicKey)
  359. if err != nil {
  360. logger.Log(1, "error parsing ext pub key:", err.Error())
  361. continue
  362. }
  363. if host.PublicKey.String() == extPeer.PublicKey ||
  364. extPeer.IngressGatewayID != node.ID.String() || !extPeer.Enabled {
  365. continue
  366. }
  367. var allowedips []net.IPNet
  368. var peer wgtypes.PeerConfig
  369. if extPeer.Address != "" {
  370. var peeraddr = net.IPNet{
  371. IP: net.ParseIP(extPeer.Address),
  372. Mask: net.CIDRMask(32, 32),
  373. }
  374. if peeraddr.IP != nil && peeraddr.Mask != nil {
  375. allowedips = append(allowedips, peeraddr)
  376. }
  377. }
  378. if extPeer.Address6 != "" {
  379. var addr6 = net.IPNet{
  380. IP: net.ParseIP(extPeer.Address6),
  381. Mask: net.CIDRMask(128, 128),
  382. }
  383. if addr6.IP != nil && addr6.Mask != nil {
  384. allowedips = append(allowedips, addr6)
  385. }
  386. }
  387. primaryAddr := extPeer.Address
  388. if primaryAddr == "" {
  389. primaryAddr = extPeer.Address6
  390. }
  391. peer = wgtypes.PeerConfig{
  392. PublicKey: pubkey,
  393. ReplaceAllowedIPs: true,
  394. AllowedIPs: allowedips,
  395. }
  396. peers = append(peers, peer)
  397. idsAndAddr = append(idsAndAddr, models.IDandAddr{
  398. ID: peer.PublicKey.String(),
  399. Name: extPeer.ClientID,
  400. Address: primaryAddr,
  401. IsExtClient: true,
  402. })
  403. }
  404. return peers, idsAndAddr, nil
  405. }
  406. // GetAllowedIPs - calculates the wireguard allowedip field for a peer of a node based on the peer and node settings
  407. func GetAllowedIPs(node, peer *models.Node, metrics *models.Metrics) []net.IPNet {
  408. var allowedips []net.IPNet
  409. allowedips = getNodeAllowedIPs(peer, node)
  410. // handle ingress gateway peers
  411. if peer.IsIngressGateway {
  412. extPeers, _, err := getExtPeers(peer, node)
  413. if err != nil {
  414. logger.Log(2, "could not retrieve ext peers for ", peer.ID.String(), err.Error())
  415. }
  416. for _, extPeer := range extPeers {
  417. allowedips = append(allowedips, extPeer.AllowedIPs...)
  418. }
  419. // if node is a failover node, add allowed ips from nodes it is handling
  420. if metrics != nil && peer.Failover && metrics.FailoverPeers != nil {
  421. // traverse through nodes that need handling
  422. logger.Log(3, "peer", peer.ID.String(), "was found to be failover for", node.ID.String(), "checking failover peers...")
  423. for k := range metrics.FailoverPeers {
  424. // if FailoverNode is me for this node, add allowedips
  425. if metrics.FailoverPeers[k] == peer.ID.String() {
  426. // get original node so we can traverse the allowed ips
  427. nodeToFailover, err := GetNodeByID(k)
  428. if err == nil {
  429. failoverNodeMetrics, err := GetMetrics(nodeToFailover.ID.String())
  430. if err == nil && failoverNodeMetrics != nil {
  431. if len(failoverNodeMetrics.NodeName) > 0 {
  432. allowedips = append(allowedips, getNodeAllowedIPs(&nodeToFailover, peer)...)
  433. logger.Log(0, "failing over node", nodeToFailover.ID.String(), nodeToFailover.PrimaryAddress(), "to failover node", peer.ID.String())
  434. }
  435. }
  436. }
  437. }
  438. }
  439. }
  440. }
  441. if node.IsRelayed && node.RelayedBy == peer.ID.String() {
  442. allowedips = append(allowedips, getAllowedIpsForRelayed(node, peer)...)
  443. }
  444. return allowedips
  445. }
  446. func getEgressIPs(peer *models.Node) []net.IPNet {
  447. peerHost, err := GetHost(peer.HostID.String())
  448. if err != nil {
  449. logger.Log(0, "error retrieving host for peer", peer.ID.String(), err.Error())
  450. }
  451. //check for internet gateway
  452. internetGateway := false
  453. if slices.Contains(peer.EgressGatewayRanges, "0.0.0.0/0") || slices.Contains(peer.EgressGatewayRanges, "::/0") {
  454. internetGateway = true
  455. }
  456. allowedips := []net.IPNet{}
  457. for _, iprange := range peer.EgressGatewayRanges { // go through each cidr for egress gateway
  458. _, ipnet, err := net.ParseCIDR(iprange) // confirming it's valid cidr
  459. if err != nil {
  460. logger.Log(1, "could not parse gateway IP range. Not adding ", iprange)
  461. continue // if can't parse CIDR
  462. }
  463. // getting the public ip of node
  464. if ipnet.Contains(peerHost.EndpointIP) && !internetGateway { // ensuring egress gateway range does not contain endpoint of node
  465. logger.Log(2, "egress IP range of ", iprange, " overlaps with ", peerHost.EndpointIP.String(), ", omitting")
  466. continue // skip adding egress range if overlaps with node's ip
  467. }
  468. // TODO: Could put in a lot of great logic to avoid conflicts / bad routes
  469. if ipnet.Contains(peer.LocalAddress.IP) && !internetGateway { // ensuring egress gateway range does not contain public ip of node
  470. logger.Log(2, "egress IP range of ", iprange, " overlaps with ", peer.LocalAddress.String(), ", omitting")
  471. continue // skip adding egress range if overlaps with node's local ip
  472. }
  473. if err != nil {
  474. logger.Log(1, "error encountered when setting egress range", err.Error())
  475. } else {
  476. allowedips = append(allowedips, *ipnet)
  477. }
  478. }
  479. return allowedips
  480. }
  481. func getNodeAllowedIPs(peer, node *models.Node) []net.IPNet {
  482. var allowedips = []net.IPNet{}
  483. if peer.Address.IP != nil {
  484. allowed := net.IPNet{
  485. IP: peer.Address.IP,
  486. Mask: net.CIDRMask(32, 32),
  487. }
  488. allowedips = append(allowedips, allowed)
  489. }
  490. if peer.Address6.IP != nil {
  491. allowed := net.IPNet{
  492. IP: peer.Address6.IP,
  493. Mask: net.CIDRMask(128, 128),
  494. }
  495. allowedips = append(allowedips, allowed)
  496. }
  497. // handle egress gateway peers
  498. if peer.IsEgressGateway {
  499. //hasGateway = true
  500. egressIPs := getEgressIPs(peer)
  501. allowedips = append(allowedips, egressIPs...)
  502. }
  503. if peer.IsRelay {
  504. for _, relayedNodeID := range peer.RelayedNodes {
  505. if node.ID.String() == relayedNodeID {
  506. continue
  507. }
  508. relayedNode, err := GetNodeByID(relayedNodeID)
  509. if err != nil {
  510. continue
  511. }
  512. allowed := getRelayedAddresses(relayedNodeID)
  513. if relayedNode.IsEgressGateway {
  514. allowed = append(allowed, getEgressIPs(&relayedNode)...)
  515. }
  516. allowedips = append(allowedips, allowed...)
  517. }
  518. }
  519. return allowedips
  520. }
  521. // getAllowedIpsForRelayed - returns the peerConfig for a node relayed by relay
  522. func getAllowedIpsForRelayed(relayed, relay *models.Node) (allowedIPs []net.IPNet) {
  523. if relayed.RelayedBy != relay.ID.String() {
  524. logger.Log(0, "RelayedByRelay called with invalid parameters")
  525. return
  526. }
  527. peers, err := GetNetworkNodes(relay.Network)
  528. if err != nil {
  529. logger.Log(0, "error getting network clients", err.Error())
  530. return
  531. }
  532. for _, peer := range peers {
  533. if peer.ID == relayed.ID || peer.ID == relay.ID {
  534. continue
  535. }
  536. if nodeacls.AreNodesAllowed(nodeacls.NetworkID(relayed.Network), nodeacls.NodeID(relayed.ID.String()), nodeacls.NodeID(peer.ID.String())) {
  537. allowedIPs = append(allowedIPs, GetAllowedIPs(relayed, &peer, nil)...)
  538. }
  539. }
  540. return
  541. }
  542. func getCIDRMaskFromAddr(addr string) net.IPMask {
  543. cidr := net.CIDRMask(32, 32)
  544. ipAddr, err := netip.ParseAddr(addr)
  545. if err != nil {
  546. return cidr
  547. }
  548. if ipAddr.Is6() {
  549. cidr = net.CIDRMask(128, 128)
  550. }
  551. return cidr
  552. }
  553. // accounts for ext client ACLs
  554. func filterNodeMapForClientACLs(publicKey, network string, nodePeerMap map[string]models.PeerRouteInfo) map[string]models.PeerRouteInfo {
  555. if !isEE {
  556. return nodePeerMap
  557. }
  558. if nodePeerMap == nil {
  559. return map[string]models.PeerRouteInfo{}
  560. }
  561. if len(publicKey) == 0 || len(network) == 0 {
  562. return nodePeerMap
  563. }
  564. client, err := GetExtClientByPubKey(publicKey, network)
  565. if err != nil {
  566. return nodePeerMap
  567. }
  568. for k := range nodePeerMap {
  569. currNodePeer := nodePeerMap[k]
  570. if _, ok := client.DeniedACLs[currNodePeer.ID]; ok {
  571. delete(nodePeerMap, k)
  572. }
  573. }
  574. return nodePeerMap
  575. }