Browse Source

GRA-985: rm locallistenport, added proxy public listen port field

Abhishek Kondur 2 years ago
parent
commit
c077e3d1c1
4 changed files with 52 additions and 63 deletions
  1. 18 26
      logic/peers.go
  2. 1 2
      models/api_host.go
  3. 33 33
      models/host.go
  4. 0 2
      models/node.go

+ 18 - 26
logic/peers.go

@@ -49,7 +49,7 @@ func GetPeersForProxy(node *models.Node, onlyPeers bool) (proxy_models.ProxyMana
 				if err != nil {
 					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 {
 					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 {
 							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 {
 							relayPeersMap[host.PublicKey.String()] = proxy_models.RelayedConf{
 								RelayedPeerEndpoint: relayedEndpoint,
@@ -106,7 +106,7 @@ func GetPeersForProxy(node *models.Node, onlyPeers bool) (proxy_models.ProxyMana
 			continue
 		}
 		proxyStatus := host.ProxyEnabled
-		listenPort := host.LocalListenPort
+		listenPort := host.ListenPort
 		if proxyStatus {
 			listenPort = host.ProxyListenPort
 			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())
 					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 {
 					peerConfMap[host.PublicKey.String()] = proxy_models.PeerConf{
 
@@ -200,14 +200,12 @@ func GetProxyUpdateForHost(host *models.Host) (proxy_models.ProxyManagerPayload,
 	if host.IsRelayed {
 		relayHost, err := GetHost(host.RelayedBy)
 		if err == nil {
-			listenPort := relayHost.LocalListenPort
+			listenPort := relayHost.ListenPort
 			if relayHost.ProxyEnabled {
-				listenPort = relayHost.ProxyListenPort
+				listenPort = relayHost.ProxyPublicListenPort
 				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))
 			if err != nil {
@@ -226,14 +224,12 @@ func GetProxyUpdateForHost(host *models.Host) (proxy_models.ProxyManagerPayload,
 		for _, relayedHost := range relayedHosts {
 			payload, err := GetPeerUpdateForHost(&relayedHost)
 			if err == nil {
-				listenPort := relayedHost.LocalListenPort
+				listenPort := relayedHost.ListenPort
 				if relayedHost.ProxyEnabled {
-					listenPort = relayedHost.ProxyListenPort
+					listenPort = relayedHost.ProxyPublicListenPort
 					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))
 				if udpErr == nil {
@@ -271,14 +267,12 @@ func GetProxyUpdateForHost(host *models.Host) (proxy_models.ProxyManagerPayload,
 				continue
 			}
 			proxyStatus := peerHost.ProxyEnabled
-			listenPort := peerHost.LocalListenPort
+			listenPort := peerHost.ListenPort
 			if proxyStatus {
-				listenPort = peerHost.ProxyListenPort
+				listenPort = peerHost.ProxyPublicListenPort
 				if listenPort == 0 {
 					listenPort = proxy_models.NmProxyPort
 				}
-			} else if listenPort == 0 {
-				listenPort = peerHost.ListenPort
 			}
 			var currPeerConf proxy_models.PeerConf
 			var found bool
@@ -293,14 +287,12 @@ func GetProxyUpdateForHost(host *models.Host) (proxy_models.ProxyManagerPayload,
 			if peerHost.IsRelayed && peerHost.RelayedBy != host.ID.String() {
 				relayHost, err := GetHost(peerHost.RelayedBy)
 				if err == nil {
-					listenPort := peerHost.LocalListenPort
+					listenPort := peerHost.ListenPort
 					if proxyStatus {
-						listenPort = peerHost.ProxyListenPort
+						listenPort = peerHost.ProxyPublicListenPort
 						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))
 					if err == nil {
@@ -667,8 +659,8 @@ func GetPeerUpdateLegacy(node *models.Node) (models.PeerUpdate, error) {
 			// set_local
 			if node.LocalAddress.String() != peer.LocalAddress.String() && peer.LocalAddress.IP != nil {
 				peerHost.EndpointIP = peer.LocalAddress.IP
-				if peerHost.LocalListenPort != 0 {
-					peerHost.ListenPort = peerHost.LocalListenPort
+				if peerHost.ListenPort != 0 {
+					peerHost.ListenPort = peerHost.ListenPort
 				}
 			} else {
 				continue
@@ -700,8 +692,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
 			// or, if port is for some reason zero use the LocalListenPort
 			// 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 = peerHost.ListenPort
 			}
 
 			endpoint := peerHost.EndpointIP.String() + ":" + strconv.FormatInt(int64(peerHost.ListenPort), 10)
@@ -1068,8 +1060,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
 	// or, if port is for some reason zero use the LocalListenPort
 	// 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)

+ 1 - 2
models/api_host.go

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

+ 33 - 33
models/host.go

@@ -12,39 +12,39 @@ const WIREGUARD_INTERFACE = "netmaker"
 
 // Host - represents a host on the network
 type Host struct {
-	ID               uuid.UUID        `json:"id" yaml:"id"`
-	Verbosity        int              `json:"verbosity" yaml:"verbosity"`
-	FirewallInUse    string           `json:"firewallinuse" yaml:"firewallinuse"`
-	Version          string           `json:"version" yaml:"version"`
-	IPForwarding     bool             `json:"ipforwarding" yaml:"ipforwarding"`
-	DaemonInstalled  bool             `json:"daemoninstalled" yaml:"daemoninstalled"`
-	HostPass         string           `json:"hostpass" yaml:"hostpass"`
-	Name             string           `json:"name" yaml:"name"`
-	OS               string           `json:"os" yaml:"os"`
-	Interface        string           `json:"interface" yaml:"interface"`
-	Debug            bool             `json:"debug" yaml:"debug"`
-	ListenPort       int              `json:"listenport" yaml:"listenport"`
-	LocalAddress     net.IPNet        `json:"localaddress" yaml:"localaddress"`
-	LocalRange       net.IPNet        `json:"localrange" yaml:"localrange"`
-	LocalListenPort  int              `json:"locallistenport" yaml:"locallistenport"`
-	ProxyListenPort  int              `json:"proxy_listen_port" yaml:"proxy_listen_port"`
-	MTU              int              `json:"mtu" yaml:"mtu"`
-	PublicKey        wgtypes.Key      `json:"publickey" yaml:"publickey"`
-	MacAddress       net.HardwareAddr `json:"macaddress" yaml:"macaddress"`
-	TrafficKeyPublic []byte           `json:"traffickeypublic" yaml:"trafficekeypublic"`
-	InternetGateway  net.UDPAddr      `json:"internetgateway" yaml:"internetgateway"`
-	Nodes            []string         `json:"nodes" yaml:"nodes"`
-	IsRelayed        bool             `json:"isrelayed" yaml:"isrelayed"`
-	RelayedBy        string           `json:"relayed_by" yaml:"relayed_by"`
-	IsRelay          bool             `json:"isrelay" yaml:"isrelay"`
-	RelayedHosts     []string         `json:"relay_hosts" yaml:"relay_hosts"`
-	Interfaces       []Iface          `json:"interfaces" yaml:"interfaces"`
-	EndpointIP       net.IP           `json:"endpointip" yaml:"endpointip"`
-	ProxyEnabled     bool             `json:"proxy_enabled" yaml:"proxy_enabled"`
-	IsDocker         bool             `json:"isdocker" yaml:"isdocker"`
-	IsK8S            bool             `json:"isk8s" yaml:"isk8s"`
-	IsStatic         bool             `json:"isstatic" yaml:"isstatic"`
-	IsDefault        bool             `json:"isdefault" yaml:"isdefault"`
+	ID                    uuid.UUID        `json:"id" yaml:"id"`
+	Verbosity             int              `json:"verbosity" yaml:"verbosity"`
+	FirewallInUse         string           `json:"firewallinuse" yaml:"firewallinuse"`
+	Version               string           `json:"version" yaml:"version"`
+	IPForwarding          bool             `json:"ipforwarding" yaml:"ipforwarding"`
+	DaemonInstalled       bool             `json:"daemoninstalled" yaml:"daemoninstalled"`
+	HostPass              string           `json:"hostpass" yaml:"hostpass"`
+	Name                  string           `json:"name" yaml:"name"`
+	OS                    string           `json:"os" yaml:"os"`
+	Interface             string           `json:"interface" yaml:"interface"`
+	Debug                 bool             `json:"debug" yaml:"debug"`
+	ListenPort            int              `json:"listenport" yaml:"listenport"`
+	LocalAddress          net.IPNet        `json:"localaddress" yaml:"localaddress"`
+	LocalRange            net.IPNet        `json:"localrange" yaml:"localrange"`
+	ProxyPublicListenPort int              `json:"proxy_public_listen_port" yaml:"proxy_public_listen_port"`
+	ProxyListenPort       int              `json:"proxy_listen_port" yaml:"proxy_listen_port"`
+	MTU                   int              `json:"mtu" yaml:"mtu"`
+	PublicKey             wgtypes.Key      `json:"publickey" yaml:"publickey"`
+	MacAddress            net.HardwareAddr `json:"macaddress" yaml:"macaddress"`
+	TrafficKeyPublic      []byte           `json:"traffickeypublic" yaml:"trafficekeypublic"`
+	InternetGateway       net.UDPAddr      `json:"internetgateway" yaml:"internetgateway"`
+	Nodes                 []string         `json:"nodes" yaml:"nodes"`
+	IsRelayed             bool             `json:"isrelayed" yaml:"isrelayed"`
+	RelayedBy             string           `json:"relayed_by" yaml:"relayed_by"`
+	IsRelay               bool             `json:"isrelay" yaml:"isrelay"`
+	RelayedHosts          []string         `json:"relay_hosts" yaml:"relay_hosts"`
+	Interfaces            []Iface          `json:"interfaces" yaml:"interfaces"`
+	EndpointIP            net.IP           `json:"endpointip" yaml:"endpointip"`
+	ProxyEnabled          bool             `json:"proxy_enabled" yaml:"proxy_enabled"`
+	IsDocker              bool             `json:"isdocker" yaml:"isdocker"`
+	IsK8S                 bool             `json:"isk8s" yaml:"isk8s"`
+	IsStatic              bool             `json:"isstatic" yaml:"isstatic"`
+	IsDefault             bool             `json:"isdefault" yaml:"isdefault"`
 }
 
 // FormatBool converts a boolean to a [yes|no] string

+ 0 - 2
models/node.go

@@ -502,7 +502,6 @@ func (ln *LegacyNode) ConvertToNewNode() (*Host, *Node) {
 				host.LocalRange = *cidr
 			}
 		}
-		host.LocalListenPort = int(ln.LocalListenPort)
 		host.ProxyListenPort = int(ln.ProxyListenPort)
 		host.MTU = int(ln.MTU)
 		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.NetworkSettings = *net
 	l.ListenPort = int32(h.ListenPort)
-	l.LocalListenPort = int32(h.LocalListenPort)
 	l.ProxyListenPort = int32(h.ProxyListenPort)
 	l.PublicKey = h.PublicKey.String()
 	l.Endpoint = h.EndpointIP.String()