mirror of
https://github.com/gravitl/netmaker.git
synced 2025-10-06 01:07:41 +08:00
check host ports on join
This commit is contained in:
@@ -579,6 +579,9 @@ func createNode(w http.ResponseWriter, r *http.Request) {
|
|||||||
// consume password before hashing for mq client creation
|
// consume password before hashing for mq client creation
|
||||||
hostPassword := data.Host.HostPass
|
hostPassword := data.Host.HostPass
|
||||||
data.Node.Server = servercfg.GetServer()
|
data.Node.Server = servercfg.GetServer()
|
||||||
|
if !logic.HostExists(&data.Host) {
|
||||||
|
logic.CheckHostPorts(&data.Host)
|
||||||
|
}
|
||||||
if err := logic.CreateHost(&data.Host); err != nil {
|
if err := logic.CreateHost(&data.Host); err != nil {
|
||||||
if errors.Is(err, logic.ErrHostExists) {
|
if errors.Is(err, logic.ErrHostExists) {
|
||||||
logger.Log(3, "host exists .. no need to create")
|
logger.Log(3, "host exists .. no need to create")
|
||||||
|
@@ -315,3 +315,53 @@ func GetRelatedHosts(hostID string) []models.Host {
|
|||||||
}
|
}
|
||||||
return relatedHosts
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user