Jelajahi Sumber

adding local listen port

afeiszli 3 tahun lalu
induk
melakukan
ccc0ed851d

+ 5 - 0
logic/peers.go

@@ -208,6 +208,11 @@ func GetPeerUpdate(node *models.Node) (models.PeerUpdate, error) {
 			} else {
 				continue
 			}
+			if node.LocalListenPort != peer.LocalListenPort && peer.LocalListenPort != 0 {
+				peer.ListenPort = peer.LocalListenPort
+			} else {
+				continue
+			}
 		}
 		endpoint := peer.Endpoint + ":" + strconv.FormatInt(int64(peer.ListenPort), 10)
 		address, err := net.ResolveUDPAddr("udp", endpoint)

+ 12 - 7
logic/server.go

@@ -225,13 +225,18 @@ func GetServerPeers(serverNode *models.Node) ([]wgtypes.PeerConfig, bool, []stri
 		if serverNode.PublicKey == node.PublicKey {
 			continue
 		}
-		if serverNode.Endpoint == node.Endpoint {
-			if serverNode.LocalAddress != node.LocalAddress && node.LocalAddress != "" {
-				node.Endpoint = node.LocalAddress
-			} else {
-				continue
-			}
-		}
+		/*
+			//		Test This: Removed logic to set local address for nodes on same network as server
+			//		This may be causing issues setting nodes on server currently
+			//		Removing may fix but could cause other issues
+					if serverNode.Endpoint == node.Endpoint {
+						if serverNode.LocalAddress != node.LocalAddress && node.LocalAddress != "" {
+							node.Endpoint = node.LocalAddress
+						} else {
+							continue
+						}
+					}
+		*/
 		if currentNetworkACL != nil && currentNetworkACL.IsAllowed(acls.AclID(serverNode.ID), acls.AclID(node.ID)) {
 			continue
 		}

+ 1 - 0
logic/util.go

@@ -154,6 +154,7 @@ func setPeerInfo(node *models.Node) models.Node {
 	peer.Name = node.Name
 	peer.Network = node.Network
 	peer.LocalAddress = node.LocalAddress
+	peer.LocalListenPort = node.LocalListenPort
 	peer.ListenPort = node.ListenPort
 	peer.AllowedIPs = node.AllowedIPs
 	peer.UDPHolePunch = node.UDPHolePunch

+ 7 - 0
models/node.go

@@ -37,6 +37,7 @@ type Node struct {
 	Address             string   `json:"address" bson:"address" yaml:"address" validate:"omitempty,ipv4"`
 	Address6            string   `json:"address6" bson:"address6" yaml:"address6" validate:"omitempty,ipv6"`
 	LocalAddress        string   `json:"localaddress" bson:"localaddress" yaml:"localaddress" validate:"omitempty,ip"`
+	LocalListenPort     int32    `json:"locallistenport" bson:"locallistenport" yaml:"locallistenport" validate:"numeric,min=1024,max=65535"`
 	Name                string   `json:"name" bson:"name" yaml:"name" validate:"omitempty,max=62,in_charset"`
 	NetworkSettings     Network  `json:"networksettings" bson:"networksettings" yaml:"networksettings" validate:"-"`
 	ListenPort          int32    `json:"listenport" bson:"listenport" yaml:"listenport" validate:"omitempty,numeric,min=1024,max=65535"`
@@ -268,6 +269,12 @@ func (newNode *Node) Fill(currentNode *Node) {
 	if newNode.ListenPort == 0 && newNode.IsStatic != "yes" {
 		newNode.ListenPort = currentNode.ListenPort
 	}
+	if newNode.LocalListenPort == 0 && newNode.IsStatic != "yes" {
+		newNode.LocalListenPort = currentNode.LocalListenPort
+	}
+	if newNode.LocalListenPort == 0 {
+		newNode.LocalListenPort = currentNode.ListenPort
+	}
 	if newNode.PublicKey == "" && newNode.IsStatic != "yes" {
 		newNode.PublicKey = currentNode.PublicKey
 	}

+ 9 - 7
models/structs.go

@@ -122,6 +122,7 @@ type PeersResponse struct {
 	Address             string `json:"address" bson:"address"`
 	Address6            string `json:"address6" bson:"address6"`
 	LocalAddress        string `json:"localaddress" bson:"localaddress"`
+	LocalListenPort     int32  `json:"locallistenport" bson:"locallistenport"`
 	IsEgressGateway     string `json:"isegressgateway" bson:"isegressgateway"`
 	EgressGatewayRanges string `json:"egressgatewayrange" bson:"egressgatewayrange"`
 	ListenPort          int32  `json:"listenport" bson:"listenport"`
@@ -130,13 +131,14 @@ type PeersResponse struct {
 
 // ExtPeersResponse - ext peers response
 type ExtPeersResponse struct {
-	PublicKey    string `json:"publickey" bson:"publickey"`
-	Endpoint     string `json:"endpoint" bson:"endpoint"`
-	Address      string `json:"address" bson:"address"`
-	Address6     string `json:"address6" bson:"address6"`
-	LocalAddress string `json:"localaddress" bson:"localaddress"`
-	ListenPort   int32  `json:"listenport" bson:"listenport"`
-	KeepAlive    int32  `json:"persistentkeepalive" bson:"persistentkeepalive"`
+	PublicKey       string `json:"publickey" bson:"publickey"`
+	Endpoint        string `json:"endpoint" bson:"endpoint"`
+	Address         string `json:"address" bson:"address"`
+	Address6        string `json:"address6" bson:"address6"`
+	LocalAddress    string `json:"localaddress" bson:"localaddress"`
+	LocalListenPort int32  `json:"locallistenport" bson:"locallistenport"`
+	ListenPort      int32  `json:"listenport" bson:"listenport"`
+	KeepAlive       int32  `json:"persistentkeepalive" bson:"persistentkeepalive"`
 }
 
 // EgressGatewayRequest - egress gateway request

+ 1 - 0
netclient/config/config.go

@@ -241,6 +241,7 @@ func GetCLIConfig(c *cli.Context) (ClientConfig, string, error) {
 	cfg.Node.PostUp = c.String("postup")
 	cfg.Node.PostDown = c.String("postdown")
 	cfg.Node.ListenPort = int32(c.Int("port"))
+	cfg.Node.LocalListenPort = int32(c.Int("localport"))
 	cfg.Node.PersistentKeepalive = int32(c.Int("keepalive"))
 	cfg.Node.PublicKey = c.String("publickey")
 	privateKey := c.String("privatekey")

+ 19 - 0
netclient/functions/mqpublish.go

@@ -12,6 +12,7 @@ import (
 	"github.com/gravitl/netmaker/logger"
 	"github.com/gravitl/netmaker/netclient/auth"
 	"github.com/gravitl/netmaker/netclient/config"
+	"github.com/gravitl/netmaker/netclient/local"
 	"github.com/gravitl/netmaker/netclient/ncutils"
 	"github.com/gravitl/netmaker/tls"
 )
@@ -60,6 +61,24 @@ func Checkin(ctx context.Context, wg *sync.WaitGroup) {
 							logger.Log(0, "could not publish local address change")
 						}
 					}
+					var deviceiface = nodeCfg.Node.Interface
+					if ncutils.IsMac() { // if node is Mac (Darwin) get the tunnel name first
+						deviceiface, err = local.GetMacIface(nodeCfg.Node.Address)
+						if err != nil || deviceiface == "" {
+							deviceiface = nodeCfg.Node.Interface
+						}
+					}
+					localPort, err := local.GetLocalListenPort(deviceiface)
+					if err != nil {
+						logger.Log(1, "error encountered checking private ip addresses: ", err.Error())
+					}
+					if nodeCfg.Node.LocalListenPort != localPort && localPort != 0 {
+						logger.Log(1, "local port has changed from ", string(nodeCfg.Node.LocalListenPort), " to ", string(localPort))
+						nodeCfg.Node.LocalListenPort = localPort
+						if err := PublishNodeUpdate(&nodeCfg); err != nil {
+							logger.Log(0, "could not publish local port change")
+						}
+					}
 				} else if nodeCfg.Node.IsLocal == "yes" && nodeCfg.Node.LocalRange != "" {
 					localIP, err := ncutils.GetLocalIP(nodeCfg.Node.LocalRange)
 					if err != nil {

+ 16 - 0
netclient/local/local.go

@@ -8,6 +8,7 @@ import (
 	"os"
 	"os/exec"
 	"runtime"
+	"strconv"
 	"strings"
 
 	"github.com/gravitl/netmaker/netclient/ncutils"
@@ -121,3 +122,18 @@ func GetMacIface(ipstring string) (string, error) {
 func HasNetwork(network string) bool {
 	return ncutils.FileExists(ncutils.GetNetclientPathSpecific() + "netconfig-" + network)
 }
+
+// Get LocalListenPort - Gets the port running on the local interface
+func GetLocalListenPort(ifacename string) (int32, error) {
+	portstring, err := ncutils.RunCmd("wg show "+ifacename+" listen-port", false)
+	if err != nil {
+		return 0, err
+	}
+	i, err := strconv.ParseInt(portstring, 10, 32)
+	if err != nil {
+		return 0, err
+	} else if i == 0 {
+		return 0, errors.New("parsed port is unset or invalid")
+	}
+	return int32(i), nil
+}