diff --git a/controllers/enrollmentkeys.go b/controllers/enrollmentkeys.go index 73966187..56546d76 100644 --- a/controllers/enrollmentkeys.go +++ b/controllers/enrollmentkeys.go @@ -200,9 +200,13 @@ func handleHostRegister(w http.ResponseWriter, r *http.Request) { // ready the response server := servercfg.GetServerInfo() server.TrafficKey = key + response := models.RegisterResponse{ + ServerConf: server, + RequestedHost: newHost, + } logger.Log(0, newHost.Name, newHost.ID.String(), "registered with Netmaker") w.WriteHeader(http.StatusOK) - json.NewEncoder(w).Encode(&server) + json.NewEncoder(w).Encode(&response) // notify host of changes, peer and node updates go checkNetRegAndHostUpdate(enrollmentKey.Networks, &newHost) } diff --git a/logic/hosts.go b/logic/hosts.go index addb8dc0..d220d445 100644 --- a/logic/hosts.go +++ b/logic/hosts.go @@ -208,7 +208,6 @@ func UpdateHostNetwork(h *models.Host, network string, add bool) (*models.Node, } else { return nil, errors.New("host already part of network " + network) } - } } if !add { @@ -362,13 +361,13 @@ func GetRelatedHosts(hostID string) []models.Host { // 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) { - portsInUse := make(map[int]bool) + portsInUse := make(map[int]bool, 0) hosts, err := GetAllHosts() if err != nil { return } for _, host := range hosts { - if host.ID == h.ID { + if host.ID.String() == h.ID.String() { //skip self continue } @@ -380,12 +379,18 @@ func CheckHostPorts(h *models.Host) { } // iterate until port is not found or max iteration is reached for i := 0; portsInUse[h.ListenPort] && i < maxPort-minPort+1; i++ { - updatePort(&h.ListenPort) + h.ListenPort++ + if h.ListenPort > maxPort { + h.ListenPort = minPort + } } // allocate h.ListenPort so it is unavailable to h.ProxyListenPort portsInUse[h.ListenPort] = true for i := 0; portsInUse[h.ProxyListenPort] && i < maxPort-minPort+1; i++ { - updatePort(&h.ProxyListenPort) + h.ProxyListenPort++ + if h.ProxyListenPort > maxPort { + h.ProxyListenPort = minPort + } } } @@ -409,10 +414,3 @@ func GetHostByNodeID(id string) *models.Host { } return nil } - -func updatePort(p *int) { - *p++ - if *p > maxPort { - *p = minPort - } -} diff --git a/logic/peers.go b/logic/peers.go index 2af7cee9..5d945df6 100644 --- a/logic/peers.go +++ b/logic/peers.go @@ -198,7 +198,7 @@ func GetPeerUpdateForHost(ctx context.Context, network string, host *models.Host peerConfig.ReplaceAllowedIPs = true uselocal := false if host.EndpointIP.String() == peerHost.EndpointIP.String() { - //peer is on same network + // peer is on same network // set to localaddress uselocal = true if node.LocalAddress.IP == nil { diff --git a/models/enrollment_key.go b/models/enrollment_key.go index 1cba2ec3..63d1b6dd 100644 --- a/models/enrollment_key.go +++ b/models/enrollment_key.go @@ -34,6 +34,12 @@ type APIEnrollmentKey struct { Tags []string `json:"tags"` } +// RegisterResponse - the response to a successful enrollment register +type RegisterResponse struct { + ServerConf ServerConfig `json:"server_config"` + RequestedHost Host `json:"requested_host"` +} + // EnrollmentKey.IsValid - checks if the key is still valid to use func (k *EnrollmentKey) IsValid() bool { if k == nil {