|
@@ -6,17 +6,19 @@ import (
|
|
"encoding/json"
|
|
"encoding/json"
|
|
"errors"
|
|
"errors"
|
|
"fmt"
|
|
"fmt"
|
|
|
|
+ "net/http"
|
|
|
|
+ "sort"
|
|
|
|
+ "strconv"
|
|
|
|
+ "sync"
|
|
|
|
+
|
|
"github.com/devilcove/httpclient"
|
|
"github.com/devilcove/httpclient"
|
|
"github.com/google/uuid"
|
|
"github.com/google/uuid"
|
|
|
|
+ "golang.org/x/crypto/bcrypt"
|
|
|
|
+
|
|
"github.com/gravitl/netmaker/database"
|
|
"github.com/gravitl/netmaker/database"
|
|
"github.com/gravitl/netmaker/logger"
|
|
"github.com/gravitl/netmaker/logger"
|
|
"github.com/gravitl/netmaker/models"
|
|
"github.com/gravitl/netmaker/models"
|
|
"github.com/gravitl/netmaker/servercfg"
|
|
"github.com/gravitl/netmaker/servercfg"
|
|
- "golang.org/x/crypto/bcrypt"
|
|
|
|
- "net/http"
|
|
|
|
- "sort"
|
|
|
|
- "strconv"
|
|
|
|
- "sync"
|
|
|
|
)
|
|
)
|
|
|
|
|
|
var (
|
|
var (
|
|
@@ -65,6 +67,7 @@ func deleteHostFromCache(hostID string) {
|
|
delete(hostsCacheMap, hostID)
|
|
delete(hostsCacheMap, hostID)
|
|
hostCacheMutex.Unlock()
|
|
hostCacheMutex.Unlock()
|
|
}
|
|
}
|
|
|
|
+
|
|
func loadHostsIntoCache(hMap map[string]models.Host) {
|
|
func loadHostsIntoCache(hMap map[string]models.Host) {
|
|
hostCacheMutex.Lock()
|
|
hostCacheMutex.Lock()
|
|
hostsCacheMap = hMap
|
|
hostsCacheMap = hMap
|
|
@@ -78,7 +81,6 @@ const (
|
|
|
|
|
|
// GetAllHosts - returns all hosts in flat list or error
|
|
// GetAllHosts - returns all hosts in flat list or error
|
|
func GetAllHosts() ([]models.Host, error) {
|
|
func GetAllHosts() ([]models.Host, error) {
|
|
-
|
|
|
|
currHosts := getHostsFromCache()
|
|
currHosts := getHostsFromCache()
|
|
if len(currHosts) != 0 {
|
|
if len(currHosts) != 0 {
|
|
return currHosts, nil
|
|
return currHosts, nil
|
|
@@ -138,7 +140,6 @@ func GetHostsMap() (map[string]models.Host, error) {
|
|
|
|
|
|
// GetHost - gets a host from db given id
|
|
// GetHost - gets a host from db given id
|
|
func GetHost(hostid string) (*models.Host, error) {
|
|
func GetHost(hostid string) (*models.Host, error) {
|
|
-
|
|
|
|
if host, ok := getHostFromCache(hostid); ok {
|
|
if host, ok := getHostFromCache(hostid); ok {
|
|
return &host, nil
|
|
return &host, nil
|
|
}
|
|
}
|
|
@@ -223,7 +224,6 @@ func UpdateHost(newHost, currentHost *models.Host) {
|
|
|
|
|
|
// UpdateHostFromClient - used for updating host on server with update recieved from client
|
|
// UpdateHostFromClient - used for updating host on server with update recieved from client
|
|
func UpdateHostFromClient(newHost, currHost *models.Host) (sendPeerUpdate bool) {
|
|
func UpdateHostFromClient(newHost, currHost *models.Host) (sendPeerUpdate bool) {
|
|
-
|
|
|
|
if newHost.PublicKey != currHost.PublicKey {
|
|
if newHost.PublicKey != currHost.PublicKey {
|
|
currHost.PublicKey = newHost.PublicKey
|
|
currHost.PublicKey = newHost.PublicKey
|
|
sendPeerUpdate = true
|
|
sendPeerUpdate = true
|
|
@@ -232,7 +232,8 @@ func UpdateHostFromClient(newHost, currHost *models.Host) (sendPeerUpdate bool)
|
|
currHost.ListenPort = newHost.ListenPort
|
|
currHost.ListenPort = newHost.ListenPort
|
|
sendPeerUpdate = true
|
|
sendPeerUpdate = true
|
|
}
|
|
}
|
|
- if newHost.WgPublicListenPort != 0 && currHost.WgPublicListenPort != newHost.WgPublicListenPort {
|
|
|
|
|
|
+ if newHost.WgPublicListenPort != 0 &&
|
|
|
|
+ currHost.WgPublicListenPort != newHost.WgPublicListenPort {
|
|
currHost.WgPublicListenPort = newHost.WgPublicListenPort
|
|
currHost.WgPublicListenPort = newHost.WgPublicListenPort
|
|
sendPeerUpdate = true
|
|
sendPeerUpdate = true
|
|
}
|
|
}
|
|
@@ -490,7 +491,7 @@ func CheckHostPorts(h *models.Host) {
|
|
}
|
|
}
|
|
for _, host := range hosts {
|
|
for _, host := range hosts {
|
|
if host.ID.String() == h.ID.String() {
|
|
if host.ID.String() == h.ID.String() {
|
|
- //skip self
|
|
|
|
|
|
+ // skip self
|
|
continue
|
|
continue
|
|
}
|
|
}
|
|
if !host.EndpointIP.Equal(h.EndpointIP) {
|
|
if !host.EndpointIP.Equal(h.EndpointIP) {
|
|
@@ -505,7 +506,6 @@ func CheckHostPorts(h *models.Host) {
|
|
h.ListenPort = minPort
|
|
h.ListenPort = minPort
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
}
|
|
}
|
|
|
|
|
|
// HostExists - checks if given host already exists
|
|
// HostExists - checks if given host already exists
|