changed register response, simplified host port check dereference

This commit is contained in:
0xdcarns 2023-03-10 11:18:29 -05:00
parent 286781a1a3
commit ee9df20b05
4 changed files with 22 additions and 14 deletions

View file

@ -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)
}

View file

@ -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
}
}

View file

@ -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 {

View file

@ -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 {