mirror of
https://github.com/gravitl/netmaker.git
synced 2025-09-07 13:44:17 +08:00
commit
06484ada33
11 changed files with 295 additions and 73 deletions
|
@ -558,7 +558,7 @@ func createNetwork(w http.ResponseWriter, r *http.Request) {
|
||||||
logic.CreateDefaultNetworkRolesAndGroups(models.NetworkID(network.NetID))
|
logic.CreateDefaultNetworkRolesAndGroups(models.NetworkID(network.NetID))
|
||||||
logic.CreateDefaultAclNetworkPolicies(models.NetworkID(network.NetID))
|
logic.CreateDefaultAclNetworkPolicies(models.NetworkID(network.NetID))
|
||||||
logic.CreateDefaultTags(models.NetworkID(network.NetID))
|
logic.CreateDefaultTags(models.NetworkID(network.NetID))
|
||||||
//add new network to allocated ip map
|
|
||||||
go logic.AddNetworkToAllocatedIpMap(network.NetID)
|
go logic.AddNetworkToAllocatedIpMap(network.NetID)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
|
|
|
@ -17,7 +17,6 @@ import (
|
||||||
var (
|
var (
|
||||||
aclCacheMutex = &sync.RWMutex{}
|
aclCacheMutex = &sync.RWMutex{}
|
||||||
aclCacheMap = make(map[string]models.Acl)
|
aclCacheMap = make(map[string]models.Acl)
|
||||||
aclTagsMutex = &sync.RWMutex{}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func MigrateAclPolicies() {
|
func MigrateAclPolicies() {
|
||||||
|
@ -577,10 +576,22 @@ func IsPeerAllowed(node, peer models.Node, checkDefaultPolicy bool) bool {
|
||||||
if peer.IsStatic {
|
if peer.IsStatic {
|
||||||
peer = peer.StaticNode.ConvertToStaticNode()
|
peer = peer.StaticNode.ConvertToStaticNode()
|
||||||
}
|
}
|
||||||
aclTagsMutex.RLock()
|
var nodeTags, peerTags map[models.TagID]struct{}
|
||||||
peerTags := maps.Clone(peer.Tags)
|
if node.Mutex != nil {
|
||||||
nodeTags := maps.Clone(node.Tags)
|
node.Mutex.Lock()
|
||||||
aclTagsMutex.RUnlock()
|
nodeTags = maps.Clone(node.Tags)
|
||||||
|
node.Mutex.Unlock()
|
||||||
|
} else {
|
||||||
|
nodeTags = node.Tags
|
||||||
|
}
|
||||||
|
if peer.Mutex != nil {
|
||||||
|
peer.Mutex.Lock()
|
||||||
|
peerTags = maps.Clone(peer.Tags)
|
||||||
|
peer.Mutex.Unlock()
|
||||||
|
} else {
|
||||||
|
peerTags = peer.Tags
|
||||||
|
}
|
||||||
|
|
||||||
if checkDefaultPolicy {
|
if checkDefaultPolicy {
|
||||||
// check default policy if all allowed return true
|
// check default policy if all allowed return true
|
||||||
defaultPolicy, err := GetDefaultPolicy(models.NetworkID(node.Network), models.DevicePolicy)
|
defaultPolicy, err := GetDefaultPolicy(models.NetworkID(node.Network), models.DevicePolicy)
|
||||||
|
@ -663,10 +674,21 @@ func IsNodeAllowedToCommunicate(node, peer models.Node, checkDefaultPolicy bool)
|
||||||
if peer.IsStatic {
|
if peer.IsStatic {
|
||||||
peer = peer.StaticNode.ConvertToStaticNode()
|
peer = peer.StaticNode.ConvertToStaticNode()
|
||||||
}
|
}
|
||||||
aclTagsMutex.RLock()
|
var nodeTags, peerTags map[models.TagID]struct{}
|
||||||
peerTags := maps.Clone(peer.Tags)
|
if node.Mutex != nil {
|
||||||
nodeTags := maps.Clone(node.Tags)
|
node.Mutex.Lock()
|
||||||
aclTagsMutex.RUnlock()
|
nodeTags = maps.Clone(node.Tags)
|
||||||
|
node.Mutex.Unlock()
|
||||||
|
} else {
|
||||||
|
nodeTags = node.Tags
|
||||||
|
}
|
||||||
|
if peer.Mutex != nil {
|
||||||
|
peer.Mutex.Lock()
|
||||||
|
peerTags = maps.Clone(peer.Tags)
|
||||||
|
peer.Mutex.Unlock()
|
||||||
|
} else {
|
||||||
|
peerTags = peer.Tags
|
||||||
|
}
|
||||||
if checkDefaultPolicy {
|
if checkDefaultPolicy {
|
||||||
// check default policy if all allowed return true
|
// check default policy if all allowed return true
|
||||||
defaultPolicy, err := GetDefaultPolicy(models.NetworkID(node.Network), models.DevicePolicy)
|
defaultPolicy, err := GetDefaultPolicy(models.NetworkID(node.Network), models.DevicePolicy)
|
||||||
|
@ -864,7 +886,15 @@ func getUserAclRulesForNode(targetnode *models.Node,
|
||||||
userGrpMap := GetUserGrpMap()
|
userGrpMap := GetUserGrpMap()
|
||||||
allowedUsers := make(map[string][]models.Acl)
|
allowedUsers := make(map[string][]models.Acl)
|
||||||
acls := listUserPolicies(models.NetworkID(targetnode.Network))
|
acls := listUserPolicies(models.NetworkID(targetnode.Network))
|
||||||
for nodeTag := range targetnode.Tags {
|
var targetNodeTags = make(map[models.TagID]struct{})
|
||||||
|
if targetnode.Mutex != nil {
|
||||||
|
targetnode.Mutex.Lock()
|
||||||
|
targetNodeTags = maps.Clone(targetnode.Tags)
|
||||||
|
targetnode.Mutex.Unlock()
|
||||||
|
} else {
|
||||||
|
targetNodeTags = maps.Clone(targetnode.Tags)
|
||||||
|
}
|
||||||
|
for nodeTag := range targetNodeTags {
|
||||||
for _, acl := range acls {
|
for _, acl := range acls {
|
||||||
if !acl.Enabled {
|
if !acl.Enabled {
|
||||||
continue
|
continue
|
||||||
|
@ -888,6 +918,7 @@ func getUserAclRulesForNode(targetnode *models.Node,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, userNode := range userNodes {
|
for _, userNode := range userNodes {
|
||||||
if !userNode.StaticNode.Enabled {
|
if !userNode.StaticNode.Enabled {
|
||||||
continue
|
continue
|
||||||
|
@ -944,8 +975,17 @@ func GetAclRulesForNode(targetnode *models.Node) (rules map[string]models.AclRul
|
||||||
}
|
}
|
||||||
|
|
||||||
acls := listDevicePolicies(models.NetworkID(targetnode.Network))
|
acls := listDevicePolicies(models.NetworkID(targetnode.Network))
|
||||||
targetnode.Tags["*"] = struct{}{}
|
|
||||||
for nodeTag := range targetnode.Tags {
|
var targetNodeTags = make(map[models.TagID]struct{})
|
||||||
|
if targetnode.Mutex != nil {
|
||||||
|
targetnode.Mutex.Lock()
|
||||||
|
targetNodeTags = maps.Clone(targetnode.Tags)
|
||||||
|
targetnode.Mutex.Unlock()
|
||||||
|
} else {
|
||||||
|
targetNodeTags = maps.Clone(targetnode.Tags)
|
||||||
|
}
|
||||||
|
targetNodeTags["*"] = struct{}{}
|
||||||
|
for nodeTag := range targetNodeTags {
|
||||||
for _, acl := range acls {
|
for _, acl := range acls {
|
||||||
if !acl.Enabled {
|
if !acl.Enabled {
|
||||||
continue
|
continue
|
||||||
|
|
|
@ -28,6 +28,9 @@ var (
|
||||||
func getAllExtClientsFromCache() (extClients []models.ExtClient) {
|
func getAllExtClientsFromCache() (extClients []models.ExtClient) {
|
||||||
extClientCacheMutex.RLock()
|
extClientCacheMutex.RLock()
|
||||||
for _, extclient := range extClientCacheMap {
|
for _, extclient := range extClientCacheMap {
|
||||||
|
if extclient.Mutex == nil {
|
||||||
|
extclient.Mutex = &sync.Mutex{}
|
||||||
|
}
|
||||||
extClients = append(extClients, extclient)
|
extClients = append(extClients, extclient)
|
||||||
}
|
}
|
||||||
extClientCacheMutex.RUnlock()
|
extClientCacheMutex.RUnlock()
|
||||||
|
@ -43,12 +46,18 @@ func deleteExtClientFromCache(key string) {
|
||||||
func getExtClientFromCache(key string) (extclient models.ExtClient, ok bool) {
|
func getExtClientFromCache(key string) (extclient models.ExtClient, ok bool) {
|
||||||
extClientCacheMutex.RLock()
|
extClientCacheMutex.RLock()
|
||||||
extclient, ok = extClientCacheMap[key]
|
extclient, ok = extClientCacheMap[key]
|
||||||
|
if extclient.Mutex == nil {
|
||||||
|
extclient.Mutex = &sync.Mutex{}
|
||||||
|
}
|
||||||
extClientCacheMutex.RUnlock()
|
extClientCacheMutex.RUnlock()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func storeExtClientInCache(key string, extclient models.ExtClient) {
|
func storeExtClientInCache(key string, extclient models.ExtClient) {
|
||||||
extClientCacheMutex.Lock()
|
extClientCacheMutex.Lock()
|
||||||
|
if extclient.Mutex == nil {
|
||||||
|
extclient.Mutex = &sync.Mutex{}
|
||||||
|
}
|
||||||
extClientCacheMap[key] = extclient
|
extClientCacheMap[key] = extclient
|
||||||
extClientCacheMutex.Unlock()
|
extClientCacheMutex.Unlock()
|
||||||
}
|
}
|
||||||
|
@ -96,14 +105,14 @@ func DeleteExtClient(network string, clientid string) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
//recycle ip address
|
|
||||||
if extClient.Address != "" {
|
|
||||||
RemoveIpFromAllocatedIpMap(network, extClient.Address)
|
|
||||||
}
|
|
||||||
if extClient.Address6 != "" {
|
|
||||||
RemoveIpFromAllocatedIpMap(network, extClient.Address6)
|
|
||||||
}
|
|
||||||
if servercfg.CacheEnabled() {
|
if servercfg.CacheEnabled() {
|
||||||
|
// recycle ip address
|
||||||
|
if extClient.Address != "" {
|
||||||
|
RemoveIpFromAllocatedIpMap(network, extClient.Address)
|
||||||
|
}
|
||||||
|
if extClient.Address6 != "" {
|
||||||
|
RemoveIpFromAllocatedIpMap(network, extClient.Address6)
|
||||||
|
}
|
||||||
deleteExtClientFromCache(key)
|
deleteExtClientFromCache(key)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
@ -333,15 +342,16 @@ func SaveExtClient(extclient *models.ExtClient) error {
|
||||||
}
|
}
|
||||||
if servercfg.CacheEnabled() {
|
if servercfg.CacheEnabled() {
|
||||||
storeExtClientInCache(key, *extclient)
|
storeExtClientInCache(key, *extclient)
|
||||||
}
|
if _, ok := allocatedIpMap[extclient.Network]; ok {
|
||||||
if _, ok := allocatedIpMap[extclient.Network]; ok {
|
if extclient.Address != "" {
|
||||||
if extclient.Address != "" {
|
AddIpToAllocatedIpMap(extclient.Network, net.ParseIP(extclient.Address))
|
||||||
AddIpToAllocatedIpMap(extclient.Network, net.ParseIP(extclient.Address))
|
}
|
||||||
}
|
if extclient.Address6 != "" {
|
||||||
if extclient.Address6 != "" {
|
AddIpToAllocatedIpMap(extclient.Network, net.ParseIP(extclient.Address6))
|
||||||
AddIpToAllocatedIpMap(extclient.Network, net.ParseIP(extclient.Address6))
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return SetNetworkNodesLastModified(extclient.Network)
|
return SetNetworkNodesLastModified(extclient.Network)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -30,6 +30,9 @@ var (
|
||||||
|
|
||||||
// SetAllocatedIpMap - set allocated ip map for networks
|
// SetAllocatedIpMap - set allocated ip map for networks
|
||||||
func SetAllocatedIpMap() error {
|
func SetAllocatedIpMap() error {
|
||||||
|
if !servercfg.CacheEnabled() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
logger.Log(0, "start setting up allocated ip map")
|
logger.Log(0, "start setting up allocated ip map")
|
||||||
if allocatedIpMap == nil {
|
if allocatedIpMap == nil {
|
||||||
allocatedIpMap = map[string]map[string]net.IP{}
|
allocatedIpMap = map[string]map[string]net.IP{}
|
||||||
|
@ -84,16 +87,25 @@ func SetAllocatedIpMap() error {
|
||||||
|
|
||||||
// ClearAllocatedIpMap - set allocatedIpMap to nil
|
// ClearAllocatedIpMap - set allocatedIpMap to nil
|
||||||
func ClearAllocatedIpMap() {
|
func ClearAllocatedIpMap() {
|
||||||
|
if !servercfg.CacheEnabled() {
|
||||||
|
return
|
||||||
|
}
|
||||||
allocatedIpMap = nil
|
allocatedIpMap = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func AddIpToAllocatedIpMap(networkName string, ip net.IP) {
|
func AddIpToAllocatedIpMap(networkName string, ip net.IP) {
|
||||||
|
if !servercfg.CacheEnabled() {
|
||||||
|
return
|
||||||
|
}
|
||||||
networkCacheMutex.Lock()
|
networkCacheMutex.Lock()
|
||||||
allocatedIpMap[networkName][ip.String()] = ip
|
allocatedIpMap[networkName][ip.String()] = ip
|
||||||
networkCacheMutex.Unlock()
|
networkCacheMutex.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func RemoveIpFromAllocatedIpMap(networkName string, ip string) {
|
func RemoveIpFromAllocatedIpMap(networkName string, ip string) {
|
||||||
|
if !servercfg.CacheEnabled() {
|
||||||
|
return
|
||||||
|
}
|
||||||
networkCacheMutex.Lock()
|
networkCacheMutex.Lock()
|
||||||
delete(allocatedIpMap[networkName], ip)
|
delete(allocatedIpMap[networkName], ip)
|
||||||
networkCacheMutex.Unlock()
|
networkCacheMutex.Unlock()
|
||||||
|
@ -101,6 +113,10 @@ func RemoveIpFromAllocatedIpMap(networkName string, ip string) {
|
||||||
|
|
||||||
// AddNetworkToAllocatedIpMap - add network to allocated ip map when network is added
|
// AddNetworkToAllocatedIpMap - add network to allocated ip map when network is added
|
||||||
func AddNetworkToAllocatedIpMap(networkName string) {
|
func AddNetworkToAllocatedIpMap(networkName string) {
|
||||||
|
//add new network to allocated ip map
|
||||||
|
if !servercfg.CacheEnabled() {
|
||||||
|
return
|
||||||
|
}
|
||||||
networkCacheMutex.Lock()
|
networkCacheMutex.Lock()
|
||||||
allocatedIpMap[networkName] = make(map[string]net.IP)
|
allocatedIpMap[networkName] = make(map[string]net.IP)
|
||||||
networkCacheMutex.Unlock()
|
networkCacheMutex.Unlock()
|
||||||
|
@ -108,6 +124,9 @@ func AddNetworkToAllocatedIpMap(networkName string) {
|
||||||
|
|
||||||
// RemoveNetworkFromAllocatedIpMap - remove network from allocated ip map when network is deleted
|
// RemoveNetworkFromAllocatedIpMap - remove network from allocated ip map when network is deleted
|
||||||
func RemoveNetworkFromAllocatedIpMap(networkName string) {
|
func RemoveNetworkFromAllocatedIpMap(networkName string) {
|
||||||
|
if !servercfg.CacheEnabled() {
|
||||||
|
return
|
||||||
|
}
|
||||||
networkCacheMutex.Lock()
|
networkCacheMutex.Lock()
|
||||||
delete(allocatedIpMap, networkName)
|
delete(allocatedIpMap, networkName)
|
||||||
networkCacheMutex.Unlock()
|
networkCacheMutex.Unlock()
|
||||||
|
@ -354,7 +373,7 @@ func GetNetworkSettings(networkname string) (models.Network, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// UniqueAddress - get a unique ipv4 address
|
// UniqueAddress - get a unique ipv4 address
|
||||||
func UniqueAddress(networkName string, reverse bool) (net.IP, error) {
|
func UniqueAddressCache(networkName string, reverse bool) (net.IP, error) {
|
||||||
add := net.IP{}
|
add := net.IP{}
|
||||||
var network models.Network
|
var network models.Network
|
||||||
network, err := GetParentNetwork(networkName)
|
network, err := GetParentNetwork(networkName)
|
||||||
|
@ -396,6 +415,49 @@ func UniqueAddress(networkName string, reverse bool) (net.IP, error) {
|
||||||
return add, errors.New("ERROR: No unique addresses available. Check network subnet")
|
return add, errors.New("ERROR: No unique addresses available. Check network subnet")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UniqueAddress - get a unique ipv4 address
|
||||||
|
func UniqueAddressDB(networkName string, reverse bool) (net.IP, error) {
|
||||||
|
add := net.IP{}
|
||||||
|
var network models.Network
|
||||||
|
network, err := GetParentNetwork(networkName)
|
||||||
|
if err != nil {
|
||||||
|
logger.Log(0, "UniqueAddressServer encountered an error")
|
||||||
|
return add, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if network.IsIPv4 == "no" {
|
||||||
|
return add, fmt.Errorf("IPv4 not active on network " + networkName)
|
||||||
|
}
|
||||||
|
//ensure AddressRange is valid
|
||||||
|
if _, _, err := net.ParseCIDR(network.AddressRange); err != nil {
|
||||||
|
logger.Log(0, "UniqueAddress encountered an error")
|
||||||
|
return add, err
|
||||||
|
}
|
||||||
|
net4 := iplib.Net4FromStr(network.AddressRange)
|
||||||
|
newAddrs := net4.FirstAddress()
|
||||||
|
|
||||||
|
if reverse {
|
||||||
|
newAddrs = net4.LastAddress()
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
if IsIPUnique(networkName, newAddrs.String(), database.NODES_TABLE_NAME, false) &&
|
||||||
|
IsIPUnique(networkName, newAddrs.String(), database.EXT_CLIENT_TABLE_NAME, false) {
|
||||||
|
return newAddrs, nil
|
||||||
|
}
|
||||||
|
if reverse {
|
||||||
|
newAddrs, err = net4.PreviousIP(newAddrs)
|
||||||
|
} else {
|
||||||
|
newAddrs, err = net4.NextIP(newAddrs)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return add, errors.New("ERROR: No unique addresses available. Check network subnet")
|
||||||
|
}
|
||||||
|
|
||||||
// IsIPUnique - checks if an IP is unique
|
// IsIPUnique - checks if an IP is unique
|
||||||
func IsIPUnique(network string, ip string, tableName string, isIpv6 bool) bool {
|
func IsIPUnique(network string, ip string, tableName string, isIpv6 bool) bool {
|
||||||
|
|
||||||
|
@ -439,9 +501,67 @@ func IsIPUnique(network string, ip string, tableName string, isIpv6 bool) bool {
|
||||||
|
|
||||||
return isunique
|
return isunique
|
||||||
}
|
}
|
||||||
|
func UniqueAddress(networkName string, reverse bool) (net.IP, error) {
|
||||||
|
if servercfg.CacheEnabled() {
|
||||||
|
return UniqueAddressCache(networkName, reverse)
|
||||||
|
}
|
||||||
|
return UniqueAddressDB(networkName, reverse)
|
||||||
|
}
|
||||||
|
|
||||||
// UniqueAddress6 - see if ipv6 address is unique
|
|
||||||
func UniqueAddress6(networkName string, reverse bool) (net.IP, error) {
|
func UniqueAddress6(networkName string, reverse bool) (net.IP, error) {
|
||||||
|
if servercfg.CacheEnabled() {
|
||||||
|
return UniqueAddress6Cache(networkName, reverse)
|
||||||
|
}
|
||||||
|
return UniqueAddress6DB(networkName, reverse)
|
||||||
|
}
|
||||||
|
|
||||||
|
// UniqueAddress6DB - see if ipv6 address is unique
|
||||||
|
func UniqueAddress6DB(networkName string, reverse bool) (net.IP, error) {
|
||||||
|
add := net.IP{}
|
||||||
|
var network models.Network
|
||||||
|
network, err := GetParentNetwork(networkName)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Network Not Found")
|
||||||
|
return add, err
|
||||||
|
}
|
||||||
|
if network.IsIPv6 == "no" {
|
||||||
|
return add, fmt.Errorf("IPv6 not active on network " + networkName)
|
||||||
|
}
|
||||||
|
|
||||||
|
//ensure AddressRange is valid
|
||||||
|
if _, _, err := net.ParseCIDR(network.AddressRange6); err != nil {
|
||||||
|
return add, err
|
||||||
|
}
|
||||||
|
net6 := iplib.Net6FromStr(network.AddressRange6)
|
||||||
|
|
||||||
|
newAddrs, err := net6.NextIP(net6.FirstAddress())
|
||||||
|
if reverse {
|
||||||
|
newAddrs, err = net6.PreviousIP(net6.LastAddress())
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return add, err
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
if IsIPUnique(networkName, newAddrs.String(), database.NODES_TABLE_NAME, true) &&
|
||||||
|
IsIPUnique(networkName, newAddrs.String(), database.EXT_CLIENT_TABLE_NAME, true) {
|
||||||
|
return newAddrs, nil
|
||||||
|
}
|
||||||
|
if reverse {
|
||||||
|
newAddrs, err = net6.PreviousIP(newAddrs)
|
||||||
|
} else {
|
||||||
|
newAddrs, err = net6.NextIP(newAddrs)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return add, errors.New("ERROR: No unique IPv6 addresses available. Check network subnet")
|
||||||
|
}
|
||||||
|
|
||||||
|
// UniqueAddress6Cache - see if ipv6 address is unique using cache
|
||||||
|
func UniqueAddress6Cache(networkName string, reverse bool) (net.IP, error) {
|
||||||
add := net.IP{}
|
add := net.IP{}
|
||||||
var network models.Network
|
var network models.Network
|
||||||
network, err := GetParentNetwork(networkName)
|
network, err := GetParentNetwork(networkName)
|
||||||
|
|
119
logic/nodes.go
119
logic/nodes.go
|
@ -35,12 +35,20 @@ var (
|
||||||
func getNodeFromCache(nodeID string) (node models.Node, ok bool) {
|
func getNodeFromCache(nodeID string) (node models.Node, ok bool) {
|
||||||
nodeCacheMutex.RLock()
|
nodeCacheMutex.RLock()
|
||||||
node, ok = nodesCacheMap[nodeID]
|
node, ok = nodesCacheMap[nodeID]
|
||||||
|
if node.Mutex == nil {
|
||||||
|
node.Mutex = &sync.Mutex{}
|
||||||
|
}
|
||||||
nodeCacheMutex.RUnlock()
|
nodeCacheMutex.RUnlock()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
func getNodesFromCache() (nodes []models.Node) {
|
func getNodesFromCache() (nodes []models.Node) {
|
||||||
nodeCacheMutex.RLock()
|
nodeCacheMutex.RLock()
|
||||||
nodes = slices.Collect(maps.Values(nodesCacheMap))
|
for _, node := range nodesCacheMap {
|
||||||
|
if node.Mutex == nil {
|
||||||
|
node.Mutex = &sync.Mutex{}
|
||||||
|
}
|
||||||
|
nodes = append(nodes, node)
|
||||||
|
}
|
||||||
nodeCacheMutex.RUnlock()
|
nodeCacheMutex.RUnlock()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -356,12 +364,15 @@ func DeleteNodeByID(node *models.Node) error {
|
||||||
logger.Log(1, "unable to remove metrics from DB for node", node.ID.String(), err.Error())
|
logger.Log(1, "unable to remove metrics from DB for node", node.ID.String(), err.Error())
|
||||||
}
|
}
|
||||||
//recycle ip address
|
//recycle ip address
|
||||||
if node.Address.IP != nil {
|
if servercfg.CacheEnabled() {
|
||||||
RemoveIpFromAllocatedIpMap(node.Network, node.Address.IP.String())
|
if node.Address.IP != nil {
|
||||||
}
|
RemoveIpFromAllocatedIpMap(node.Network, node.Address.IP.String())
|
||||||
if node.Address6.IP != nil {
|
}
|
||||||
RemoveIpFromAllocatedIpMap(node.Network, node.Address6.IP.String())
|
if node.Address6.IP != nil {
|
||||||
|
RemoveIpFromAllocatedIpMap(node.Network, node.Address6.IP.String())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -423,6 +434,9 @@ func GetAllNodes() ([]models.Node, error) {
|
||||||
}
|
}
|
||||||
// add node to our array
|
// add node to our array
|
||||||
nodes = append(nodes, node)
|
nodes = append(nodes, node)
|
||||||
|
if node.Mutex == nil {
|
||||||
|
node.Mutex = &sync.Mutex{}
|
||||||
|
}
|
||||||
nodesMap[node.ID.String()] = node
|
nodesMap[node.ID.String()] = node
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -683,15 +697,16 @@ func createNode(node *models.Node) error {
|
||||||
if servercfg.CacheEnabled() {
|
if servercfg.CacheEnabled() {
|
||||||
storeNodeInCache(*node)
|
storeNodeInCache(*node)
|
||||||
storeNodeInNetworkCache(*node, node.Network)
|
storeNodeInNetworkCache(*node, node.Network)
|
||||||
}
|
if _, ok := allocatedIpMap[node.Network]; ok {
|
||||||
if _, ok := allocatedIpMap[node.Network]; ok {
|
if node.Address.IP != nil {
|
||||||
if node.Address.IP != nil {
|
AddIpToAllocatedIpMap(node.Network, node.Address.IP)
|
||||||
AddIpToAllocatedIpMap(node.Network, node.Address.IP)
|
}
|
||||||
}
|
if node.Address6.IP != nil {
|
||||||
if node.Address6.IP != nil {
|
AddIpToAllocatedIpMap(node.Network, node.Address6.IP)
|
||||||
AddIpToAllocatedIpMap(node.Network, node.Address6.IP)
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = nodeacls.CreateNodeACL(nodeacls.NetworkID(node.Network), nodeacls.NodeID(node.ID.String()), defaultACLVal)
|
_, err = nodeacls.CreateNodeACL(nodeacls.NetworkID(node.Network), nodeacls.NodeID(node.ID.String()), defaultACLVal)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Log(1, "failed to create node ACL for node,", node.ID.String(), "err:", err.Error())
|
logger.Log(1, "failed to create node ACL for node,", node.ID.String(), "err:", err.Error())
|
||||||
|
@ -737,16 +752,14 @@ func ValidateParams(nodeid, netid string) (models.Node, error) {
|
||||||
func ValidateNodeIp(currentNode *models.Node, newNode *models.ApiNode) error {
|
func ValidateNodeIp(currentNode *models.Node, newNode *models.ApiNode) error {
|
||||||
|
|
||||||
if currentNode.Address.IP != nil && currentNode.Address.String() != newNode.Address {
|
if currentNode.Address.IP != nil && currentNode.Address.String() != newNode.Address {
|
||||||
newIp, _, _ := net.ParseCIDR(newNode.Address)
|
if !IsIPUnique(newNode.Network, newNode.Address, database.NODES_TABLE_NAME, false) ||
|
||||||
ipAllocated := allocatedIpMap[currentNode.Network]
|
!IsIPUnique(newNode.Network, newNode.Address, database.EXT_CLIENT_TABLE_NAME, false) {
|
||||||
if _, ok := ipAllocated[newIp.String()]; ok {
|
|
||||||
return errors.New("ip specified is already allocated: " + newNode.Address)
|
return errors.New("ip specified is already allocated: " + newNode.Address)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if currentNode.Address6.IP != nil && currentNode.Address6.String() != newNode.Address6 {
|
if currentNode.Address6.IP != nil && currentNode.Address6.String() != newNode.Address6 {
|
||||||
newIp, _, _ := net.ParseCIDR(newNode.Address6)
|
if !IsIPUnique(newNode.Network, newNode.Address6, database.NODES_TABLE_NAME, false) ||
|
||||||
ipAllocated := allocatedIpMap[currentNode.Network]
|
!IsIPUnique(newNode.Network, newNode.Address6, database.EXT_CLIENT_TABLE_NAME, false) {
|
||||||
if _, ok := ipAllocated[newIp.String()]; ok {
|
|
||||||
return errors.New("ip specified is already allocated: " + newNode.Address6)
|
return errors.New("ip specified is already allocated: " + newNode.Address6)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -809,9 +822,16 @@ func GetTagMapWithNodes() (tagNodesMap map[models.TagID][]models.Node) {
|
||||||
if nodeI.Tags == nil {
|
if nodeI.Tags == nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
if nodeI.Mutex != nil {
|
||||||
|
nodeI.Mutex.Lock()
|
||||||
|
}
|
||||||
for nodeTagID := range nodeI.Tags {
|
for nodeTagID := range nodeI.Tags {
|
||||||
tagNodesMap[nodeTagID] = append(tagNodesMap[nodeTagID], nodeI)
|
tagNodesMap[nodeTagID] = append(tagNodesMap[nodeTagID], nodeI)
|
||||||
}
|
}
|
||||||
|
if nodeI.Mutex != nil {
|
||||||
|
nodeI.Mutex.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -823,9 +843,15 @@ func GetTagMapWithNodesByNetwork(netID models.NetworkID, withStaticNodes bool) (
|
||||||
if nodeI.Tags == nil {
|
if nodeI.Tags == nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
if nodeI.Mutex != nil {
|
||||||
|
nodeI.Mutex.Lock()
|
||||||
|
}
|
||||||
for nodeTagID := range nodeI.Tags {
|
for nodeTagID := range nodeI.Tags {
|
||||||
tagNodesMap[nodeTagID] = append(tagNodesMap[nodeTagID], nodeI)
|
tagNodesMap[nodeTagID] = append(tagNodesMap[nodeTagID], nodeI)
|
||||||
}
|
}
|
||||||
|
if nodeI.Mutex != nil {
|
||||||
|
nodeI.Mutex.Unlock()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
tagNodesMap["*"] = nodes
|
tagNodesMap["*"] = nodes
|
||||||
if !withStaticNodes {
|
if !withStaticNodes {
|
||||||
|
@ -844,17 +870,16 @@ func AddTagMapWithStaticNodes(netID models.NetworkID,
|
||||||
if extclient.Tags == nil || extclient.RemoteAccessClientID != "" {
|
if extclient.Tags == nil || extclient.RemoteAccessClientID != "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
for tagID := range extclient.Tags {
|
if extclient.Mutex != nil {
|
||||||
tagNodesMap[tagID] = append(tagNodesMap[tagID], models.Node{
|
extclient.Mutex.Lock()
|
||||||
IsStatic: true,
|
}
|
||||||
StaticNode: extclient,
|
for tagID := range extclient.Tags {
|
||||||
})
|
tagNodesMap[tagID] = append(tagNodesMap[tagID], extclient.ConvertToStaticNode())
|
||||||
tagNodesMap["*"] = append(tagNodesMap["*"], models.Node{
|
tagNodesMap["*"] = append(tagNodesMap["*"], extclient.ConvertToStaticNode())
|
||||||
IsStatic: true,
|
}
|
||||||
StaticNode: extclient,
|
if extclient.Mutex != nil {
|
||||||
})
|
extclient.Mutex.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
return tagNodesMap
|
return tagNodesMap
|
||||||
}
|
}
|
||||||
|
@ -869,11 +894,14 @@ func AddTagMapWithStaticNodesWithUsers(netID models.NetworkID,
|
||||||
if extclient.Tags == nil {
|
if extclient.Tags == nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
if extclient.Mutex != nil {
|
||||||
|
extclient.Mutex.Lock()
|
||||||
|
}
|
||||||
for tagID := range extclient.Tags {
|
for tagID := range extclient.Tags {
|
||||||
tagNodesMap[tagID] = append(tagNodesMap[tagID], models.Node{
|
tagNodesMap[tagID] = append(tagNodesMap[tagID], extclient.ConvertToStaticNode())
|
||||||
IsStatic: true,
|
}
|
||||||
StaticNode: extclient,
|
if extclient.Mutex != nil {
|
||||||
})
|
extclient.Mutex.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -891,9 +919,15 @@ func GetNodesWithTag(tagID models.TagID) map[string]models.Node {
|
||||||
if nodeI.Tags == nil {
|
if nodeI.Tags == nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
if nodeI.Mutex != nil {
|
||||||
|
nodeI.Mutex.Lock()
|
||||||
|
}
|
||||||
if _, ok := nodeI.Tags[tagID]; ok {
|
if _, ok := nodeI.Tags[tagID]; ok {
|
||||||
nMap[nodeI.ID.String()] = nodeI
|
nMap[nodeI.ID.String()] = nodeI
|
||||||
}
|
}
|
||||||
|
if nodeI.Mutex != nil {
|
||||||
|
nodeI.Mutex.Unlock()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return AddStaticNodesWithTag(tag, nMap)
|
return AddStaticNodesWithTag(tag, nMap)
|
||||||
}
|
}
|
||||||
|
@ -907,13 +941,15 @@ func AddStaticNodesWithTag(tag models.Tag, nMap map[string]models.Node) map[stri
|
||||||
if extclient.RemoteAccessClientID != "" {
|
if extclient.RemoteAccessClientID != "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if _, ok := extclient.Tags[tag.ID]; ok {
|
if extclient.Mutex != nil {
|
||||||
nMap[extclient.ClientID] = models.Node{
|
extclient.Mutex.Lock()
|
||||||
IsStatic: true,
|
}
|
||||||
StaticNode: extclient,
|
if _, ok := extclient.Tags[tag.ID]; ok {
|
||||||
}
|
nMap[extclient.ClientID] = extclient.ConvertToStaticNode()
|
||||||
|
}
|
||||||
|
if extclient.Mutex != nil {
|
||||||
|
extclient.Mutex.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
return nMap
|
return nMap
|
||||||
}
|
}
|
||||||
|
@ -929,10 +965,7 @@ func GetStaticNodeWithTag(tagID models.TagID) map[string]models.Node {
|
||||||
return nMap
|
return nMap
|
||||||
}
|
}
|
||||||
for _, extclient := range extclients {
|
for _, extclient := range extclients {
|
||||||
nMap[extclient.ClientID] = models.Node{
|
nMap[extclient.ClientID] = extclient.ConvertToStaticNode()
|
||||||
IsStatic: true,
|
|
||||||
StaticNode: extclient,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return nMap
|
return nMap
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package models
|
package models
|
||||||
|
|
||||||
|
import "sync"
|
||||||
|
|
||||||
// ExtClient - struct for external clients
|
// ExtClient - struct for external clients
|
||||||
type ExtClient struct {
|
type ExtClient struct {
|
||||||
ClientID string `json:"clientid" bson:"clientid"`
|
ClientID string `json:"clientid" bson:"clientid"`
|
||||||
|
@ -25,6 +27,7 @@ type ExtClient struct {
|
||||||
DeviceName string `json:"device_name"`
|
DeviceName string `json:"device_name"`
|
||||||
PublicEndpoint string `json:"public_endpoint"`
|
PublicEndpoint string `json:"public_endpoint"`
|
||||||
Country string `json:"country"`
|
Country string `json:"country"`
|
||||||
|
Mutex *sync.Mutex `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// CustomExtClient - struct for CustomExtClient params
|
// CustomExtClient - struct for CustomExtClient params
|
||||||
|
@ -55,5 +58,6 @@ func (ext *ExtClient) ConvertToStaticNode() Node {
|
||||||
Tags: ext.Tags,
|
Tags: ext.Tags,
|
||||||
IsStatic: true,
|
IsStatic: true,
|
||||||
StaticNode: *ext,
|
StaticNode: *ext,
|
||||||
|
Mutex: ext.Mutex,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net"
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
@ -119,6 +120,7 @@ type Node struct {
|
||||||
IsUserNode bool `json:"is_user_node"`
|
IsUserNode bool `json:"is_user_node"`
|
||||||
StaticNode ExtClient `json:"static_node"`
|
StaticNode ExtClient `json:"static_node"`
|
||||||
Status NodeStatus `json:"node_status"`
|
Status NodeStatus `json:"node_status"`
|
||||||
|
Mutex *sync.Mutex `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// LegacyNode - legacy struct for node model
|
// LegacyNode - legacy struct for node model
|
||||||
|
|
|
@ -245,6 +245,12 @@ func getUserEmailFromClaims(token string) string {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
claims, _ := accessToken.Claims.(jwt.MapClaims)
|
claims, _ := accessToken.Claims.(jwt.MapClaims)
|
||||||
|
if claims == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
if claims["email"] == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
return claims["email"].(string)
|
return claims["email"].(string)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -199,6 +199,10 @@ func getAzureUserInfo(state string, code string) (*OAuthUser, error) {
|
||||||
if userInfo.Email == "" {
|
if userInfo.Email == "" {
|
||||||
userInfo.Email = getUserEmailFromClaims(token.AccessToken)
|
userInfo.Email = getUserEmailFromClaims(token.AccessToken)
|
||||||
}
|
}
|
||||||
|
if userInfo.Email == "" && userInfo.UserPrincipalName != "" {
|
||||||
|
userInfo.Email = userInfo.UserPrincipalName
|
||||||
|
|
||||||
|
}
|
||||||
if userInfo.Email == "" {
|
if userInfo.Email == "" {
|
||||||
err = errors.New("failed to fetch user email from SSO state")
|
err = errors.New("failed to fetch user email from SSO state")
|
||||||
return userInfo, err
|
return userInfo, err
|
||||||
|
|
|
@ -1104,6 +1104,9 @@ func getUserRemoteAccessGwsV1(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
gws := userGws[node.Network]
|
gws := userGws[node.Network]
|
||||||
|
if extClient.DNS == "" {
|
||||||
|
extClient.DNS = node.IngressDNS
|
||||||
|
}
|
||||||
extClient.AllowedIPs = logic.GetExtclientAllowedIPs(extClient)
|
extClient.AllowedIPs = logic.GetExtclientAllowedIPs(extClient)
|
||||||
gws = append(gws, models.UserRemoteGws{
|
gws = append(gws, models.UserRemoteGws{
|
||||||
GwID: node.ID.String(),
|
GwID: node.ID.String(),
|
||||||
|
|
|
@ -55,6 +55,6 @@ func GetClient() (e EmailSender) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func IsValid(email string) bool {
|
func IsValid(email string) bool {
|
||||||
emailRegex := regexp.MustCompile(`^[a-z0-9._%+\-]+@[a-z0-9.\-]+\.[a-z]{2,4}$`)
|
emailRegex := regexp.MustCompile(`^[a-z0-9._%+\-]+@[a-z0-9.\-]+\.[a-z]{2,}$`)
|
||||||
return emailRegex.MatchString(email)
|
return emailRegex.MatchString(email)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue