|
@@ -3,11 +3,13 @@ package logic
|
|
|
import (
|
|
|
"encoding/json"
|
|
|
"fmt"
|
|
|
+ "net"
|
|
|
"reflect"
|
|
|
"sync"
|
|
|
"time"
|
|
|
|
|
|
"github.com/gravitl/netmaker/database"
|
|
|
+ "github.com/gravitl/netmaker/logger"
|
|
|
"github.com/gravitl/netmaker/models"
|
|
|
"golang.org/x/exp/slog"
|
|
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
|
@@ -184,6 +186,9 @@ func CreateExtClient(extclient *models.ExtClient) error {
|
|
|
} else if len(extclient.PrivateKey) == 0 && len(extclient.PublicKey) > 0 {
|
|
|
extclient.PrivateKey = "[ENTER PRIVATE KEY]"
|
|
|
}
|
|
|
+ if extclient.ExtraAllowedIPs == nil {
|
|
|
+ extclient.ExtraAllowedIPs = []string{}
|
|
|
+ }
|
|
|
|
|
|
parentNetwork, err := GetNetwork(extclient.Network)
|
|
|
if err != nil {
|
|
@@ -247,9 +252,7 @@ func UpdateExtClient(old *models.ExtClient, update *models.CustomExtClient) mode
|
|
|
if update.Enabled != old.Enabled {
|
|
|
new.Enabled = update.Enabled
|
|
|
}
|
|
|
- if update.ExtraAllowedIPs != nil && StringDifference(old.ExtraAllowedIPs, update.ExtraAllowedIPs) != nil {
|
|
|
- new.ExtraAllowedIPs = update.ExtraAllowedIPs
|
|
|
- }
|
|
|
+ new.ExtraAllowedIPs = update.ExtraAllowedIPs
|
|
|
if update.DeniedACLs != nil && !reflect.DeepEqual(old.DeniedACLs, update.DeniedACLs) {
|
|
|
new.DeniedACLs = update.DeniedACLs
|
|
|
}
|
|
@@ -318,3 +321,107 @@ func ToggleExtClientConnectivity(client *models.ExtClient, enable bool) (models.
|
|
|
|
|
|
return newClient, nil
|
|
|
}
|
|
|
+
|
|
|
+func getExtPeers(node, peer *models.Node) ([]wgtypes.PeerConfig, []models.IDandAddr, []models.EgressNetworkRoutes, error) {
|
|
|
+ var peers []wgtypes.PeerConfig
|
|
|
+ var idsAndAddr []models.IDandAddr
|
|
|
+ var egressRoutes []models.EgressNetworkRoutes
|
|
|
+ extPeers, err := GetNetworkExtClients(node.Network)
|
|
|
+ if err != nil {
|
|
|
+ return peers, idsAndAddr, egressRoutes, err
|
|
|
+ }
|
|
|
+ host, err := GetHost(node.HostID.String())
|
|
|
+ if err != nil {
|
|
|
+ return peers, idsAndAddr, egressRoutes, err
|
|
|
+ }
|
|
|
+ for _, extPeer := range extPeers {
|
|
|
+ extPeer := extPeer
|
|
|
+ if !IsClientNodeAllowed(&extPeer, peer.ID.String()) {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ pubkey, err := wgtypes.ParseKey(extPeer.PublicKey)
|
|
|
+ if err != nil {
|
|
|
+ logger.Log(1, "error parsing ext pub key:", err.Error())
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ if host.PublicKey.String() == extPeer.PublicKey ||
|
|
|
+ extPeer.IngressGatewayID != node.ID.String() || !extPeer.Enabled {
|
|
|
+ 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)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for _, extraAllowedIP := range extPeer.ExtraAllowedIPs {
|
|
|
+ _, cidr, err := net.ParseCIDR(extraAllowedIP)
|
|
|
+ if err == nil {
|
|
|
+ allowedips = append(allowedips, *cidr)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ egressRoutes = append(egressRoutes, getExtPeerEgressRoute(extPeer)...)
|
|
|
+ primaryAddr := extPeer.Address
|
|
|
+ if primaryAddr == "" {
|
|
|
+ primaryAddr = extPeer.Address6
|
|
|
+ }
|
|
|
+ peer = wgtypes.PeerConfig{
|
|
|
+ PublicKey: pubkey,
|
|
|
+ ReplaceAllowedIPs: true,
|
|
|
+ AllowedIPs: allowedips,
|
|
|
+ }
|
|
|
+ peers = append(peers, peer)
|
|
|
+ idsAndAddr = append(idsAndAddr, models.IDandAddr{
|
|
|
+ ID: peer.PublicKey.String(),
|
|
|
+ Name: extPeer.ClientID,
|
|
|
+ Address: primaryAddr,
|
|
|
+ IsExtClient: true,
|
|
|
+ })
|
|
|
+ }
|
|
|
+ return peers, idsAndAddr, egressRoutes, nil
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+func getExtPeerEgressRoute(extPeer models.ExtClient) (egressRoutes []models.EgressNetworkRoutes) {
|
|
|
+ if extPeer.Address != "" {
|
|
|
+ egressRoutes = append(egressRoutes, models.EgressNetworkRoutes{
|
|
|
+ NodeAddr: extPeer.AddressIPNet4(),
|
|
|
+ EgressRanges: extPeer.ExtraAllowedIPs,
|
|
|
+ })
|
|
|
+ }
|
|
|
+ if extPeer.Address6 != "" {
|
|
|
+ egressRoutes = append(egressRoutes, models.EgressNetworkRoutes{
|
|
|
+ NodeAddr: extPeer.AddressIPNet6(),
|
|
|
+ EgressRanges: extPeer.ExtraAllowedIPs,
|
|
|
+ })
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+func getExtpeersExtraRoutes(network string) (egressRoutes []models.EgressNetworkRoutes) {
|
|
|
+ extPeers, err := GetNetworkExtClients(network)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ for _, extPeer := range extPeers {
|
|
|
+ egressRoutes = append(egressRoutes, getExtPeerEgressRoute(extPeer)...)
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|