|
@@ -1,9 +1,7 @@
|
|
package logic
|
|
package logic
|
|
|
|
|
|
import (
|
|
import (
|
|
- "context"
|
|
|
|
"errors"
|
|
"errors"
|
|
- "fmt"
|
|
|
|
"net"
|
|
"net"
|
|
"net/netip"
|
|
"net/netip"
|
|
|
|
|
|
@@ -16,22 +14,6 @@ import (
|
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
|
)
|
|
)
|
|
|
|
|
|
-var (
|
|
|
|
- // PeerUpdateCtx context to send to host peer updates
|
|
|
|
- PeerUpdateCtx context.Context
|
|
|
|
- // PeerUpdateStop - the cancel for PeerUpdateCtx
|
|
|
|
- PeerUpdateStop context.CancelFunc
|
|
|
|
-)
|
|
|
|
-
|
|
|
|
-// ResetPeerUpdateContext - kills any current peer updates and resets the context
|
|
|
|
-func ResetPeerUpdateContext() {
|
|
|
|
- if PeerUpdateCtx != nil && PeerUpdateStop != nil {
|
|
|
|
- PeerUpdateStop() // tell any current peer updates to stop
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- PeerUpdateCtx, PeerUpdateStop = context.WithCancel(context.Background())
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
func NodePeersInfo(client *models.Client) (models.NodePeersInfo, error) {
|
|
func NodePeersInfo(client *models.Client) (models.NodePeersInfo, error) {
|
|
nodePeersInfo := models.NodePeersInfo{
|
|
nodePeersInfo := models.NodePeersInfo{
|
|
PeerIDs: make(models.PeerMap),
|
|
PeerIDs: make(models.PeerMap),
|
|
@@ -124,7 +106,7 @@ func NodePeersInfo(client *models.Client) (models.NodePeersInfo, error) {
|
|
}
|
|
}
|
|
|
|
|
|
// GetPeerUpdateForHost - gets the consolidated peer update for the host from all networks
|
|
// GetPeerUpdateForHost - gets the consolidated peer update for the host from all networks
|
|
-func GetPeerUpdateForHost(ctx context.Context, network string, host *models.Host, deletedNode *models.Node, deletedClients []models.ExtClient) (models.HostPeerUpdate, error) {
|
|
|
|
|
|
+func GetPeerUpdateForHost(host *models.Host) (models.HostPeerUpdate, error) {
|
|
if host == nil {
|
|
if host == nil {
|
|
return models.HostPeerUpdate{}, errors.New("host is nil")
|
|
return models.HostPeerUpdate{}, errors.New("host is nil")
|
|
}
|
|
}
|
|
@@ -155,94 +137,88 @@ func GetPeerUpdateForHost(ctx context.Context, network string, host *models.Host
|
|
}
|
|
}
|
|
currentPeers := GetNetworkNodesMemory(allNodes, node.Network)
|
|
currentPeers := GetNetworkNodesMemory(allNodes, node.Network)
|
|
for _, peer := range currentPeers {
|
|
for _, peer := range currentPeers {
|
|
- select {
|
|
|
|
- case <-ctx.Done():
|
|
|
|
- logger.Log(2, "cancelled peer update for host", host.Name, host.ID.String())
|
|
|
|
- return models.HostPeerUpdate{}, fmt.Errorf("peer update cancelled")
|
|
|
|
- default:
|
|
|
|
- peer := peer
|
|
|
|
- if peer.ID.String() == node.ID.String() {
|
|
|
|
- logger.Log(2, "peer update, skipping self")
|
|
|
|
- //skip yourself
|
|
|
|
- continue
|
|
|
|
- }
|
|
|
|
- if peer.IsRelayed {
|
|
|
|
- // skip relayed peers; will be included in relay peer
|
|
|
|
- continue
|
|
|
|
- }
|
|
|
|
- var peerConfig wgtypes.PeerConfig
|
|
|
|
- peerHost, err := GetHost(peer.HostID.String())
|
|
|
|
- if err != nil {
|
|
|
|
- logger.Log(1, "no peer host", peer.HostID.String(), err.Error())
|
|
|
|
- return models.HostPeerUpdate{}, err
|
|
|
|
- }
|
|
|
|
|
|
+ peer := peer
|
|
|
|
+ if peer.ID.String() == node.ID.String() {
|
|
|
|
+ logger.Log(2, "peer update, skipping self")
|
|
|
|
+ //skip yourself
|
|
|
|
+ continue
|
|
|
|
+ }
|
|
|
|
+ if peer.IsRelayed {
|
|
|
|
+ // skip relayed peers; will be included in relay peer
|
|
|
|
+ continue
|
|
|
|
+ }
|
|
|
|
+ var peerConfig wgtypes.PeerConfig
|
|
|
|
+ peerHost, err := GetHost(peer.HostID.String())
|
|
|
|
+ if err != nil {
|
|
|
|
+ logger.Log(1, "no peer host", peer.HostID.String(), err.Error())
|
|
|
|
+ return models.HostPeerUpdate{}, err
|
|
|
|
+ }
|
|
|
|
|
|
- peerConfig.PublicKey = peerHost.PublicKey
|
|
|
|
- peerConfig.PersistentKeepaliveInterval = &peer.PersistentKeepalive
|
|
|
|
- peerConfig.ReplaceAllowedIPs = true
|
|
|
|
- uselocal := false
|
|
|
|
- if host.EndpointIP.String() == peerHost.EndpointIP.String() {
|
|
|
|
- // peer is on same network
|
|
|
|
- // set to localaddress
|
|
|
|
- uselocal = true
|
|
|
|
- if node.LocalAddress.IP == nil {
|
|
|
|
- // use public endpint
|
|
|
|
- uselocal = false
|
|
|
|
- }
|
|
|
|
- if node.LocalAddress.String() == peer.LocalAddress.String() {
|
|
|
|
- uselocal = false
|
|
|
|
- }
|
|
|
|
|
|
+ peerConfig.PublicKey = peerHost.PublicKey
|
|
|
|
+ peerConfig.PersistentKeepaliveInterval = &peer.PersistentKeepalive
|
|
|
|
+ peerConfig.ReplaceAllowedIPs = true
|
|
|
|
+ uselocal := false
|
|
|
|
+ if host.EndpointIP.String() == peerHost.EndpointIP.String() {
|
|
|
|
+ // peer is on same network
|
|
|
|
+ // set to localaddress
|
|
|
|
+ uselocal = true
|
|
|
|
+ if node.LocalAddress.IP == nil {
|
|
|
|
+ // use public endpint
|
|
|
|
+ uselocal = false
|
|
}
|
|
}
|
|
- peerConfig.Endpoint = &net.UDPAddr{
|
|
|
|
- IP: peerHost.EndpointIP,
|
|
|
|
- Port: getPeerWgListenPort(peerHost),
|
|
|
|
|
|
+ if node.LocalAddress.String() == peer.LocalAddress.String() {
|
|
|
|
+ uselocal = false
|
|
}
|
|
}
|
|
|
|
+ }
|
|
|
|
+ peerConfig.Endpoint = &net.UDPAddr{
|
|
|
|
+ IP: peerHost.EndpointIP,
|
|
|
|
+ Port: getPeerWgListenPort(peerHost),
|
|
|
|
+ }
|
|
|
|
|
|
- if uselocal {
|
|
|
|
- peerConfig.Endpoint.IP = peer.LocalAddress.IP
|
|
|
|
- peerConfig.Endpoint.Port = peerHost.ListenPort
|
|
|
|
- }
|
|
|
|
- allowedips := GetAllowedIPs(&node, &peer, nil)
|
|
|
|
- if peer.IsIngressGateway {
|
|
|
|
- for _, entry := range peer.IngressGatewayRange {
|
|
|
|
- _, cidr, err := net.ParseCIDR(string(entry))
|
|
|
|
- if err == nil {
|
|
|
|
- allowedips = append(allowedips, *cidr)
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- if peer.IsEgressGateway {
|
|
|
|
- host, err := GetHost(peer.HostID.String())
|
|
|
|
|
|
+ if uselocal {
|
|
|
|
+ peerConfig.Endpoint.IP = peer.LocalAddress.IP
|
|
|
|
+ peerConfig.Endpoint.Port = peerHost.ListenPort
|
|
|
|
+ }
|
|
|
|
+ allowedips := GetAllowedIPs(&node, &peer, nil)
|
|
|
|
+ if peer.IsIngressGateway {
|
|
|
|
+ for _, entry := range peer.IngressGatewayRange {
|
|
|
|
+ _, cidr, err := net.ParseCIDR(string(entry))
|
|
if err == nil {
|
|
if err == nil {
|
|
- allowedips = append(allowedips, getEgressIPs(
|
|
|
|
- &models.Client{
|
|
|
|
- Host: *host,
|
|
|
|
- Node: peer,
|
|
|
|
- })...)
|
|
|
|
|
|
+ allowedips = append(allowedips, *cidr)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- if peer.Action != models.NODE_DELETE &&
|
|
|
|
- !peer.PendingDelete &&
|
|
|
|
- peer.Connected &&
|
|
|
|
- nodeacls.AreNodesAllowed(nodeacls.NetworkID(node.Network), nodeacls.NodeID(node.ID.String()), nodeacls.NodeID(peer.ID.String())) &&
|
|
|
|
- (deletedNode == nil || (deletedNode != nil && peer.ID.String() != deletedNode.ID.String())) {
|
|
|
|
- peerConfig.AllowedIPs = allowedips // only append allowed IPs if valid connection
|
|
|
|
|
|
+ }
|
|
|
|
+ if peer.IsEgressGateway {
|
|
|
|
+ host, err := GetHost(peer.HostID.String())
|
|
|
|
+ if err == nil {
|
|
|
|
+ allowedips = append(allowedips, getEgressIPs(
|
|
|
|
+ &models.Client{
|
|
|
|
+ Host: *host,
|
|
|
|
+ Node: peer,
|
|
|
|
+ })...)
|
|
}
|
|
}
|
|
- if _, ok := peerIndexMap[peerHost.PublicKey.String()]; !ok {
|
|
|
|
- hostPeerUpdate.Peers = append(hostPeerUpdate.Peers, peerConfig)
|
|
|
|
- peerIndexMap[peerHost.PublicKey.String()] = len(hostPeerUpdate.Peers) - 1
|
|
|
|
- hostPeerUpdate.HostNetworkInfo[peerHost.PublicKey.String()] = models.HostNetworkInfo{
|
|
|
|
- Interfaces: peerHost.Interfaces,
|
|
|
|
- }
|
|
|
|
- } else {
|
|
|
|
- peerAllowedIPs := hostPeerUpdate.Peers[peerIndexMap[peerHost.PublicKey.String()]].AllowedIPs
|
|
|
|
- peerAllowedIPs = append(peerAllowedIPs, allowedips...)
|
|
|
|
- hostPeerUpdate.Peers[peerIndexMap[peerHost.PublicKey.String()]].AllowedIPs = peerAllowedIPs
|
|
|
|
- hostPeerUpdate.HostNetworkInfo[peerHost.PublicKey.String()] = models.HostNetworkInfo{
|
|
|
|
- Interfaces: peerHost.Interfaces,
|
|
|
|
- }
|
|
|
|
|
|
+ }
|
|
|
|
+ if peer.Action != models.NODE_DELETE &&
|
|
|
|
+ !peer.PendingDelete &&
|
|
|
|
+ peer.Connected &&
|
|
|
|
+ nodeacls.AreNodesAllowed(nodeacls.NetworkID(node.Network), nodeacls.NodeID(node.ID.String()), nodeacls.NodeID(peer.ID.String())) {
|
|
|
|
+ peerConfig.AllowedIPs = allowedips // only append allowed IPs if valid connection
|
|
|
|
+ }
|
|
|
|
+ if _, ok := peerIndexMap[peerHost.PublicKey.String()]; !ok {
|
|
|
|
+ hostPeerUpdate.Peers = append(hostPeerUpdate.Peers, peerConfig)
|
|
|
|
+ peerIndexMap[peerHost.PublicKey.String()] = len(hostPeerUpdate.Peers) - 1
|
|
|
|
+ hostPeerUpdate.HostNetworkInfo[peerHost.PublicKey.String()] = models.HostNetworkInfo{
|
|
|
|
+ Interfaces: peerHost.Interfaces,
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ peerAllowedIPs := hostPeerUpdate.Peers[peerIndexMap[peerHost.PublicKey.String()]].AllowedIPs
|
|
|
|
+ peerAllowedIPs = append(peerAllowedIPs, allowedips...)
|
|
|
|
+ hostPeerUpdate.Peers[peerIndexMap[peerHost.PublicKey.String()]].AllowedIPs = peerAllowedIPs
|
|
|
|
+ hostPeerUpdate.HostNetworkInfo[peerHost.PublicKey.String()] = models.HostNetworkInfo{
|
|
|
|
+ Interfaces: peerHost.Interfaces,
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|
|
if node.IsIngressGateway {
|
|
if node.IsIngressGateway {
|