Browse Source

check host ports on join

Matthew R Kasun 2 years ago
parent
commit
1f92d06d7c
2 changed files with 53 additions and 0 deletions
  1. 3 0
      controllers/node.go
  2. 50 0
      logic/hosts.go

+ 3 - 0
controllers/node.go

@@ -579,6 +579,9 @@ func createNode(w http.ResponseWriter, r *http.Request) {
 	// consume password before hashing for mq client creation
 	hostPassword := data.Host.HostPass
 	data.Node.Server = servercfg.GetServer()
+	if !logic.HostExists(&data.Host) {
+		logic.CheckHostPorts(&data.Host)
+	}
 	if err := logic.CreateHost(&data.Host); err != nil {
 		if errors.Is(err, logic.ErrHostExists) {
 			logger.Log(3, "host exists .. no need to create")

+ 50 - 0
logic/hosts.go

@@ -315,3 +315,53 @@ func GetRelatedHosts(hostID string) []models.Host {
 	}
 	return relatedHosts
 }
+
+// CheckHostPort checks host endpoints to ensures that hosts on the same server
+// with the same endpoint have different listen ports
+// in the case of 64535 hosts or more with same endpoint, ports will not be changed
+func CheckHostPorts(h *models.Host) {
+	listenPort := h.ListenPort
+	proxyPort := h.ProxyListenPort
+	count := 0
+	reset := false
+	hosts, err := GetAllHosts()
+	if err != nil {
+		return
+	}
+	for _, host := range hosts {
+		if host.EndpointIP.Equal(h.EndpointIP) {
+			if host.ListenPort == h.ListenPort || host.ProxyListenPort == h.ProxyListenPort {
+				updateListenPorts(h)
+				count++
+				//protect against endless recursion
+				if count > 64535 {
+					reset = true
+					break
+				}
+				CheckHostPorts(h)
+			}
+		}
+	}
+	if reset {
+		h.ListenPort = listenPort
+		h.ProxyListenPort = proxyPort
+	}
+}
+
+// HostExists - checks if given host already exists
+func HostExists(h *models.Host) bool {
+	_, err := GetHost(h.ID.String())
+	if (err != nil && !database.IsEmptyRecord(err)) || (err == nil) {
+		return true
+	}
+	return false
+}
+
+func updateListenPorts(h *models.Host) {
+	h.ListenPort++
+	h.ProxyListenPort++
+	if h.ListenPort > 65535 || h.ProxyListenPort > 65535 {
+		h.ListenPort = 1000
+		h.ProxyListenPort = 1001
+	}
+}