Browse Source

Merge pull request #1949 from gravitl/GRA-985/host_listen_port

Gra 985/host listen port
dcarns 2 years ago
parent
commit
c459594137
6 changed files with 37 additions and 78 deletions
  1. 30 65
      logic/peers.go
  2. 2 2
      models/api_host.go
  3. 1 1
      models/host.go
  4. 0 2
      models/node.go
  5. 4 0
      mq/handlers.go
  6. 0 8
      stun-server/stun-server.go

+ 30 - 65
logic/peers.go

@@ -49,7 +49,7 @@ func GetPeersForProxy(node *models.Node, onlyPeers bool) (proxy_models.ProxyMana
 				if err != nil {
 				if err != nil {
 					logger.Log(0, "error retrieving host for relay node", relayNode.HostID.String(), err.Error())
 					logger.Log(0, "error retrieving host for relay node", relayNode.HostID.String(), err.Error())
 				}
 				}
-				relayEndpoint, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", relayHost.EndpointIP, host.LocalListenPort))
+				relayEndpoint, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", relayHost.EndpointIP, host.ListenPort))
 				if err != nil {
 				if err != nil {
 					logger.Log(1, "failed to resolve relay node endpoint: ", err.Error())
 					logger.Log(1, "failed to resolve relay node endpoint: ", err.Error())
 				}
 				}
@@ -78,7 +78,7 @@ func GetPeersForProxy(node *models.Node, onlyPeers bool) (proxy_models.ProxyMana
 						if err != nil {
 						if err != nil {
 							logger.Log(0, "error retrieving host for relayNode", relayedNode.ID.String(), err.Error())
 							logger.Log(0, "error retrieving host for relayNode", relayedNode.ID.String(), err.Error())
 						}
 						}
-						relayedEndpoint, udpErr := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", relayedHost.EndpointIP, host.LocalListenPort))
+						relayedEndpoint, udpErr := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", relayedHost.EndpointIP, host.ListenPort))
 						if udpErr == nil {
 						if udpErr == nil {
 							relayPeersMap[host.PublicKey.String()] = proxy_models.RelayedConf{
 							relayPeersMap[host.PublicKey.String()] = proxy_models.RelayedConf{
 								RelayedPeerEndpoint: relayedEndpoint,
 								RelayedPeerEndpoint: relayedEndpoint,
@@ -106,7 +106,7 @@ func GetPeersForProxy(node *models.Node, onlyPeers bool) (proxy_models.ProxyMana
 			continue
 			continue
 		}
 		}
 		proxyStatus := host.ProxyEnabled
 		proxyStatus := host.ProxyEnabled
-		listenPort := host.LocalListenPort
+		listenPort := host.ListenPort
 		if proxyStatus {
 		if proxyStatus {
 			listenPort = host.ProxyListenPort
 			listenPort = host.ProxyListenPort
 			if listenPort == 0 {
 			if listenPort == 0 {
@@ -149,7 +149,7 @@ func GetPeersForProxy(node *models.Node, onlyPeers bool) (proxy_models.ProxyMana
 					logger.Log(0, "error retrieving host for relayNode", relayNode.ID.String(), err.Error())
 					logger.Log(0, "error retrieving host for relayNode", relayNode.ID.String(), err.Error())
 					continue
 					continue
 				}
 				}
-				relayTo, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", relayHost.EndpointIP, relayHost.LocalListenPort))
+				relayTo, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", relayHost.EndpointIP, relayHost.ListenPort))
 				if err == nil {
 				if err == nil {
 					peerConfMap[host.PublicKey.String()] = proxy_models.PeerConf{
 					peerConfMap[host.PublicKey.String()] = proxy_models.PeerConf{
 
 
@@ -200,16 +200,7 @@ func GetProxyUpdateForHost(host *models.Host) (proxy_models.ProxyManagerPayload,
 	if host.IsRelayed {
 	if host.IsRelayed {
 		relayHost, err := GetHost(host.RelayedBy)
 		relayHost, err := GetHost(host.RelayedBy)
 		if err == nil {
 		if err == nil {
-			listenPort := relayHost.LocalListenPort
-			if relayHost.ProxyEnabled {
-				listenPort = relayHost.ProxyListenPort
-				if listenPort == 0 {
-					listenPort = proxy_models.NmProxyPort
-				}
-			} else if listenPort == 0 {
-				listenPort = relayHost.ListenPort
-			}
-			relayEndpoint, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", relayHost.EndpointIP, listenPort))
+			relayEndpoint, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", relayHost.EndpointIP, getPeerListenPort(relayHost)))
 			if err != nil {
 			if err != nil {
 				logger.Log(1, "failed to resolve relay node endpoint: ", err.Error())
 				logger.Log(1, "failed to resolve relay node endpoint: ", err.Error())
 			}
 			}
@@ -226,16 +217,7 @@ func GetProxyUpdateForHost(host *models.Host) (proxy_models.ProxyManagerPayload,
 		for _, relayedHost := range relayedHosts {
 		for _, relayedHost := range relayedHosts {
 			payload, err := GetPeerUpdateForHost(&relayedHost)
 			payload, err := GetPeerUpdateForHost(&relayedHost)
 			if err == nil {
 			if err == nil {
-				listenPort := relayedHost.LocalListenPort
-				if relayedHost.ProxyEnabled {
-					listenPort = relayedHost.ProxyListenPort
-					if listenPort == 0 {
-						listenPort = proxy_models.NmProxyPort
-					}
-				} else if listenPort == 0 {
-					listenPort = relayedHost.ListenPort
-				}
-				relayedEndpoint, udpErr := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", relayedHost.EndpointIP, listenPort))
+				relayedEndpoint, udpErr := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", relayedHost.EndpointIP, getPeerListenPort(&relayedHost)))
 				if udpErr == nil {
 				if udpErr == nil {
 					relayPeersMap[relayedHost.PublicKey.String()] = proxy_models.RelayedConf{
 					relayPeersMap[relayedHost.PublicKey.String()] = proxy_models.RelayedConf{
 						RelayedPeerEndpoint: relayedEndpoint,
 						RelayedPeerEndpoint: relayedEndpoint,
@@ -270,39 +252,20 @@ func GetProxyUpdateForHost(host *models.Host) (proxy_models.ProxyManagerPayload,
 			if err != nil {
 			if err != nil {
 				continue
 				continue
 			}
 			}
-			proxyStatus := peerHost.ProxyEnabled
-			listenPort := peerHost.LocalListenPort
-			if proxyStatus {
-				listenPort = peerHost.ProxyListenPort
-				if listenPort == 0 {
-					listenPort = proxy_models.NmProxyPort
-				}
-			} else if listenPort == 0 {
-				listenPort = peerHost.ListenPort
-			}
+
 			var currPeerConf proxy_models.PeerConf
 			var currPeerConf proxy_models.PeerConf
 			var found bool
 			var found bool
 			if currPeerConf, found = peerConfMap[peerHost.PublicKey.String()]; !found {
 			if currPeerConf, found = peerConfMap[peerHost.PublicKey.String()]; !found {
 				currPeerConf = proxy_models.PeerConf{
 				currPeerConf = proxy_models.PeerConf{
-					Proxy:            proxyStatus,
-					PublicListenPort: int32(listenPort),
+					Proxy:            peerHost.ProxyEnabled,
+					PublicListenPort: int32(getPeerListenPort(peerHost)),
 				}
 				}
-
 			}
 			}
 
 
 			if peerHost.IsRelayed && peerHost.RelayedBy != host.ID.String() {
 			if peerHost.IsRelayed && peerHost.RelayedBy != host.ID.String() {
 				relayHost, err := GetHost(peerHost.RelayedBy)
 				relayHost, err := GetHost(peerHost.RelayedBy)
 				if err == nil {
 				if err == nil {
-					listenPort := peerHost.LocalListenPort
-					if proxyStatus {
-						listenPort = peerHost.ProxyListenPort
-						if listenPort == 0 {
-							listenPort = proxy_models.NmProxyPort
-						}
-					} else if listenPort == 0 {
-						listenPort = peerHost.ListenPort
-					}
-					relayTo, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", relayHost.EndpointIP, listenPort))
+					relayTo, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", relayHost.EndpointIP, getPeerListenPort(peerHost)))
 					if err == nil {
 					if err == nil {
 						currPeerConf.IsRelayed = true
 						currPeerConf.IsRelayed = true
 						currPeerConf.RelayedTo = relayTo
 						currPeerConf.RelayedTo = relayTo
@@ -401,16 +364,9 @@ func GetPeerUpdateForHost(host *models.Host) (models.HostPeerUpdate, error) {
 			}
 			}
 			peerConfig.Endpoint = &net.UDPAddr{
 			peerConfig.Endpoint = &net.UDPAddr{
 				IP:   peerHost.EndpointIP,
 				IP:   peerHost.EndpointIP,
-				Port: peerHost.ListenPort,
+				Port: getPeerListenPort(peerHost),
 			}
 			}
-			if peerHost.ProxyEnabled {
-				if peerHost.ProxyListenPort == 0 {
-					peerConfig.Endpoint.Port = proxy_models.NmProxyPort
-				} else {
-					peerConfig.Endpoint.Port = peerHost.ProxyListenPort
-				}
 
 
-			}
 			if uselocal {
 			if uselocal {
 				peerConfig.Endpoint.IP = peer.LocalAddress.IP
 				peerConfig.Endpoint.IP = peer.LocalAddress.IP
 			}
 			}
@@ -423,9 +379,6 @@ func GetPeerUpdateForHost(host *models.Host) (models.HostPeerUpdate, error) {
 					}
 					}
 				}
 				}
 			}
 			}
-			if peer.IsRelay {
-				allowedips = append(allowedips, getRelayAllowedIPs(&node, &peer)...)
-			}
 			if peer.IsEgressGateway {
 			if peer.IsEgressGateway {
 				allowedips = append(allowedips, getEgressIPs(&node, &peer)...)
 				allowedips = append(allowedips, getEgressIPs(&node, &peer)...)
 			}
 			}
@@ -477,6 +430,18 @@ func GetPeerUpdateForHost(host *models.Host) (models.HostPeerUpdate, error) {
 	return hostPeerUpdate, nil
 	return hostPeerUpdate, nil
 }
 }
 
 
+func getPeerListenPort(host *models.Host) int {
+	peerPort := host.ListenPort
+	if host.ProxyEnabled {
+		if host.PublicListenPort != 0 {
+			peerPort = host.PublicListenPort
+		} else if host.ProxyListenPort != 0 {
+			peerPort = host.ProxyListenPort
+		}
+	}
+	return peerPort
+}
+
 // GetPeerUpdate - gets a wireguard peer config for each peer of a node
 // GetPeerUpdate - gets a wireguard peer config for each peer of a node
 func GetPeerUpdate(node *models.Node, host *models.Host) (models.PeerUpdate, error) {
 func GetPeerUpdate(node *models.Node, host *models.Host) (models.PeerUpdate, error) {
 	log.Println("peer update for node ", node.ID)
 	log.Println("peer update for node ", node.ID)
@@ -535,7 +500,7 @@ func GetPeerUpdate(node *models.Node, host *models.Host) (models.PeerUpdate, err
 			Port: peerHost.ListenPort,
 			Port: peerHost.ListenPort,
 		}
 		}
 		if peerHost.ProxyEnabled {
 		if peerHost.ProxyEnabled {
-			peerConfig.Endpoint.Port = peerHost.ProxyListenPort
+			peerConfig.Endpoint.Port = getPeerListenPort(peerHost)
 		}
 		}
 		if uselocal {
 		if uselocal {
 			peerConfig.Endpoint.IP = peer.LocalAddress.IP
 			peerConfig.Endpoint.IP = peer.LocalAddress.IP
@@ -667,8 +632,8 @@ func GetPeerUpdateLegacy(node *models.Node) (models.PeerUpdate, error) {
 			// set_local
 			// set_local
 			if node.LocalAddress.String() != peer.LocalAddress.String() && peer.LocalAddress.IP != nil {
 			if node.LocalAddress.String() != peer.LocalAddress.String() && peer.LocalAddress.IP != nil {
 				peerHost.EndpointIP = peer.LocalAddress.IP
 				peerHost.EndpointIP = peer.LocalAddress.IP
-				if peerHost.LocalListenPort != 0 {
-					peerHost.ListenPort = peerHost.LocalListenPort
+				if peerHost.ListenPort != 0 {
+					peerHost.ListenPort = getPeerListenPort(peerHost)
 				}
 				}
 			} else {
 			} else {
 				continue
 				continue
@@ -700,8 +665,8 @@ func GetPeerUpdateLegacy(node *models.Node) (models.PeerUpdate, error) {
 			// if udp hole punching is on, but udp hole punching did not set it, use the LocalListenPort instead
 			// if udp hole punching is on, but udp hole punching did not set it, use the LocalListenPort instead
 			// or, if port is for some reason zero use the LocalListenPort
 			// or, if port is for some reason zero use the LocalListenPort
 			// but only do this if LocalListenPort is not zero
 			// but only do this if LocalListenPort is not zero
-			if ((!setUDPPort) || peerHost.ListenPort == 0) && peerHost.LocalListenPort != 0 {
-				peerHost.ListenPort = peerHost.LocalListenPort
+			if ((!setUDPPort) || peerHost.ListenPort == 0) && peerHost.ListenPort != 0 {
+				peerHost.ListenPort = getPeerListenPort(peerHost)
 			}
 			}
 
 
 			endpoint := peerHost.EndpointIP.String() + ":" + strconv.FormatInt(int64(peerHost.ListenPort), 10)
 			endpoint := peerHost.EndpointIP.String() + ":" + strconv.FormatInt(int64(peerHost.ListenPort), 10)
@@ -1068,8 +1033,8 @@ func GetPeerUpdateForRelayedNode(node *models.Node, udppeers map[string]string)
 	// if udp hole punching is on, but udp hole punching did not set it, use the LocalListenPort instead
 	// if udp hole punching is on, but udp hole punching did not set it, use the LocalListenPort instead
 	// or, if port is for some reason zero use the LocalListenPort
 	// or, if port is for some reason zero use the LocalListenPort
 	// but only do this if LocalListenPort is not zero
 	// but only do this if LocalListenPort is not zero
-	if ((!setUDPPort) || relayHost.ListenPort == 0) && relayHost.LocalListenPort != 0 {
-		listenPort = relayHost.LocalListenPort
+	if ((!setUDPPort) || relayHost.ListenPort == 0) && relayHost.ListenPort != 0 {
+		listenPort = relayHost.ListenPort
 	}
 	}
 
 
 	endpoint := relayHost.EndpointIP.String() + ":" + strconv.FormatInt(int64(listenPort), 10)
 	endpoint := relayHost.EndpointIP.String() + ":" + strconv.FormatInt(int64(listenPort), 10)

+ 2 - 2
models/api_host.go

@@ -50,7 +50,6 @@ func (h *Host) ConvertNMHostToAPI() *ApiHost {
 	}
 	}
 	a.IsStatic = h.IsStatic
 	a.IsStatic = h.IsStatic
 	a.ListenPort = h.ListenPort
 	a.ListenPort = h.ListenPort
-	a.LocalListenPort = h.LocalListenPort
 	a.LocalRange = h.LocalRange.String()
 	a.LocalRange = h.LocalRange.String()
 	if isEmptyAddr(a.LocalRange) {
 	if isEmptyAddr(a.LocalRange) {
 		a.LocalRange = ""
 		a.LocalRange = ""
@@ -92,7 +91,8 @@ func (a *ApiHost) ConvertAPIHostToNMHost(currentHost *Host) *Host {
 	h.IsK8S = currentHost.IsK8S
 	h.IsK8S = currentHost.IsK8S
 	h.IsStatic = a.IsStatic
 	h.IsStatic = a.IsStatic
 	h.ListenPort = a.ListenPort
 	h.ListenPort = a.ListenPort
-	h.LocalListenPort = currentHost.ListenPort
+	h.ProxyListenPort = a.ProxyListenPort
+	h.PublicListenPort = currentHost.PublicListenPort
 	h.MTU = a.MTU
 	h.MTU = a.MTU
 	h.MacAddress = currentHost.MacAddress
 	h.MacAddress = currentHost.MacAddress
 	h.PublicKey = currentHost.PublicKey
 	h.PublicKey = currentHost.PublicKey

+ 1 - 1
models/host.go

@@ -26,7 +26,7 @@ type Host struct {
 	ListenPort       int              `json:"listenport" yaml:"listenport"`
 	ListenPort       int              `json:"listenport" yaml:"listenport"`
 	LocalAddress     net.IPNet        `json:"localaddress" yaml:"localaddress"`
 	LocalAddress     net.IPNet        `json:"localaddress" yaml:"localaddress"`
 	LocalRange       net.IPNet        `json:"localrange" yaml:"localrange"`
 	LocalRange       net.IPNet        `json:"localrange" yaml:"localrange"`
-	LocalListenPort  int              `json:"locallistenport" yaml:"locallistenport"`
+	PublicListenPort int              `json:"public_listen_port" yaml:"public_listen_port"`
 	ProxyListenPort  int              `json:"proxy_listen_port" yaml:"proxy_listen_port"`
 	ProxyListenPort  int              `json:"proxy_listen_port" yaml:"proxy_listen_port"`
 	MTU              int              `json:"mtu" yaml:"mtu"`
 	MTU              int              `json:"mtu" yaml:"mtu"`
 	PublicKey        wgtypes.Key      `json:"publickey" yaml:"publickey"`
 	PublicKey        wgtypes.Key      `json:"publickey" yaml:"publickey"`

+ 0 - 2
models/node.go

@@ -502,7 +502,6 @@ func (ln *LegacyNode) ConvertToNewNode() (*Host, *Node) {
 				host.LocalRange = *cidr
 				host.LocalRange = *cidr
 			}
 			}
 		}
 		}
-		host.LocalListenPort = int(ln.LocalListenPort)
 		host.ProxyListenPort = int(ln.ProxyListenPort)
 		host.ProxyListenPort = int(ln.ProxyListenPort)
 		host.MTU = int(ln.MTU)
 		host.MTU = int(ln.MTU)
 		host.PublicKey, _ = wgtypes.ParseKey(ln.PublicKey)
 		host.PublicKey, _ = wgtypes.ParseKey(ln.PublicKey)
@@ -563,7 +562,6 @@ func (n *Node) Legacy(h *Host, s *ServerConfig, net *Network) *LegacyNode {
 	l.Name = h.Name
 	l.Name = h.Name
 	l.NetworkSettings = *net
 	l.NetworkSettings = *net
 	l.ListenPort = int32(h.ListenPort)
 	l.ListenPort = int32(h.ListenPort)
-	l.LocalListenPort = int32(h.LocalListenPort)
 	l.ProxyListenPort = int32(h.ProxyListenPort)
 	l.ProxyListenPort = int32(h.ProxyListenPort)
 	l.PublicKey = h.PublicKey.String()
 	l.PublicKey = h.PublicKey.String()
 	l.Endpoint = h.EndpointIP.String()
 	l.Endpoint = h.EndpointIP.String()

+ 4 - 0
mq/handlers.go

@@ -188,6 +188,10 @@ func updateHostFromClient(newHost, currHost *models.Host) (sendPeerUpdate bool)
 		currHost.ProxyListenPort = newHost.ProxyListenPort
 		currHost.ProxyListenPort = newHost.ProxyListenPort
 		sendPeerUpdate = true
 		sendPeerUpdate = true
 	}
 	}
+	if newHost.PublicListenPort != 0 && currHost.PublicListenPort != newHost.PublicListenPort {
+		currHost.PublicListenPort = newHost.PublicListenPort
+		sendPeerUpdate = true
+	}
 	if currHost.ProxyEnabled != newHost.ProxyEnabled {
 	if currHost.ProxyEnabled != newHost.ProxyEnabled {
 		currHost.ProxyEnabled = newHost.ProxyEnabled
 		currHost.ProxyEnabled = newHost.ProxyEnabled
 		sendPeerUpdate = true
 		sendPeerUpdate = true

+ 0 - 8
stun-server/stun-server.go

@@ -14,7 +14,6 @@ import (
 	"github.com/gravitl/netmaker/logger"
 	"github.com/gravitl/netmaker/logger"
 	"github.com/gravitl/netmaker/servercfg"
 	"github.com/gravitl/netmaker/servercfg"
 	"github.com/pkg/errors"
 	"github.com/pkg/errors"
-	"github.com/sirupsen/logrus"
 	"gortc.io/stun"
 	"gortc.io/stun"
 )
 )
 
 
@@ -28,14 +27,7 @@ type Server struct {
 	Ctx  context.Context
 	Ctx  context.Context
 }
 }
 
 
-// Logger is used for logging formatted messages.
-type Logger interface {
-	// Printf must have the same semantics as log.Printf.
-	Printf(format string, args ...interface{})
-}
-
 var (
 var (
-	defaultLogger     = logrus.New()
 	software          = stun.NewSoftware("netmaker-stun")
 	software          = stun.NewSoftware("netmaker-stun")
 	errNotSTUNMessage = errors.New("not stun message")
 	errNotSTUNMessage = errors.New("not stun message")
 )
 )