소스 검색

adding local listen port

afeiszli 3 년 전
부모
커밋
91bdd433f3
11개의 변경된 파일77개의 추가작업 그리고 15개의 파일을 삭제
  1. 1 0
      controllers/node_grpc.go
  2. 5 0
      logic/peers.go
  3. 12 7
      logic/server.go
  4. 1 0
      logic/util.go
  5. 7 0
      models/node.go
  6. 9 7
      models/structs.go
  7. 1 0
      netclient/config/config.go
  8. 2 1
      netclient/functions/join.go
  9. 19 0
      netclient/functions/mqpublish.go
  10. 16 0
      netclient/local/local.go
  11. 4 0
      netclient/server/grpc.go

+ 1 - 0
controllers/node_grpc.go

@@ -279,6 +279,7 @@ func (s *NodeServiceServer) GetExtPeers(ctx context.Context, req *nodepb.Object)
 			PublicKey:           peers[i].PublicKey,
 			PersistentKeepalive: peers[i].KeepAlive,
 			ListenPort:          peers[i].ListenPort,
+			LocalListenPort:     peers[i].LocalListenPort,
 			LocalAddress:        peers[i].LocalAddress,
 		})
 	}

+ 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

@@ -124,6 +124,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"`
@@ -132,13 +133,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

@@ -233,6 +233,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")

+ 2 - 1
netclient/functions/join.go

@@ -139,6 +139,7 @@ func JoinNetwork(cfg *config.ClientConfig, privateKey string, iscomms bool) erro
 		PostDown:            cfg.Node.PostDown,
 		PersistentKeepalive: cfg.Node.PersistentKeepalive,
 		LocalAddress:        cfg.Node.LocalAddress,
+		LocalListenPort:     cfg.Node.ListenPort,
 		Interface:           cfg.Node.Interface,
 		PublicKey:           cfg.Node.PublicKey,
 		DNSOn:               cfg.Node.DNSOn,
@@ -239,7 +240,7 @@ func JoinNetwork(cfg *config.ClientConfig, privateKey string, iscomms bool) erro
 	//	if node.DNSOn == "yes" {
 	//		for _, server := range node.NetworkSettings.DefaultServerAddrs {
 	//			if server.IsLeader {
-	//				go func() {
+	//				go func()
 	//					if !local.SetDNSWithRetry(node, server.Address) {
 	//						cfg.Node.DNSOn = "no"
 	//						var currentCommsCfg = getCommsCfgByNode(&cfg.Node)

+ 19 - 0
netclient/functions/mqpublish.go

@@ -10,6 +10,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"
 )
 
@@ -64,6 +65,24 @@ func Checkin(ctx context.Context, wg *sync.WaitGroup, currentComms map[string]bo
 								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(&currCommsCfg, &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
+}

+ 4 - 0
netclient/server/grpc.go

@@ -152,6 +152,10 @@ func GetPeers(macaddress string, network string, server string, dualstack bool,
 		if nodecfg.Endpoint == node.Endpoint {
 			if nodecfg.LocalAddress != node.LocalAddress && node.LocalAddress != "" {
 				node.Endpoint = node.LocalAddress
+				if nodecfg.LocalListenPort != node.LocalListenPort && node.LocalListenPort != 0 {
+					node.ListenPort = node.LocalListenPort
+
+				}
 			} else {
 				continue
 			}