| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479 | package logicimport (	"context"	"encoding/json"	"errors"	"fmt"	"maps"	"net"	"sort"	"sync"	"time"	"github.com/gravitl/netmaker/database"	"github.com/gravitl/netmaker/db"	"github.com/gravitl/netmaker/models"	"github.com/gravitl/netmaker/schema"	"github.com/gravitl/netmaker/servercfg")// TODO: Write Diff Funcsvar IsNodeAllowedToCommunicate = isNodeAllowedToCommunicatevar GetFwRulesForNodeAndPeerOnGw = getFwRulesForNodeAndPeerOnGwvar GetFwRulesForUserNodesOnGw = func(node models.Node, nodes []models.Node) (rules []models.FwRule) { return }func GetFwRulesOnIngressGateway(node models.Node) (rules []models.FwRule) {	// fetch user access to static clients via policies	defer func() {		sort.Slice(rules, func(i, j int) bool {			if !rules[i].SrcIP.IP.Equal(rules[j].SrcIP.IP) {				return string(rules[i].SrcIP.IP.To16()) < string(rules[j].SrcIP.IP.To16())			}			return string(rules[i].DstIP.IP.To16()) < string(rules[j].DstIP.IP.To16())		})	}()	defaultDevicePolicy, _ := GetDefaultPolicy(models.NetworkID(node.Network), models.DevicePolicy)	nodes, _ := GetNetworkNodes(node.Network)	nodes = append(nodes, GetStaticNodesByNetwork(models.NetworkID(node.Network), true)...)	rules = GetFwRulesForUserNodesOnGw(node, nodes)	if defaultDevicePolicy.Enabled {		return	}	for _, nodeI := range nodes {		if !nodeI.IsStatic || nodeI.IsUserNode {			continue		}		// if nodeI.StaticNode.IngressGatewayID != node.ID.String() {		// 	continue		// }		for _, peer := range nodes {			if peer.StaticNode.ClientID == nodeI.StaticNode.ClientID || peer.IsUserNode {				continue			}			if nodeI.StaticNode.IngressGatewayID != node.ID.String() &&				((!peer.IsStatic && peer.ID.String() != node.ID.String()) ||					(peer.IsStatic && peer.StaticNode.IngressGatewayID != node.ID.String())) {				continue			}			if peer.IsStatic {				peer = peer.StaticNode.ConvertToStaticNode()			}			var allowedPolicies1 []models.Acl			var ok bool			if ok, allowedPolicies1 = IsNodeAllowedToCommunicate(nodeI.StaticNode.ConvertToStaticNode(), peer, true); ok {				rules = append(rules, GetFwRulesForNodeAndPeerOnGw(nodeI.StaticNode.ConvertToStaticNode(), peer, allowedPolicies1)...)			}			if ok, allowedPolicies2 := IsNodeAllowedToCommunicate(peer, nodeI.StaticNode.ConvertToStaticNode(), true); ok {				rules = append(rules,					GetFwRulesForNodeAndPeerOnGw(peer, nodeI.StaticNode.ConvertToStaticNode(),						getUniquePolicies(allowedPolicies1, allowedPolicies2))...)			}		}	}	return}func getFwRulesForNodeAndPeerOnGw(node, peer models.Node, allowedPolicies []models.Acl) (rules []models.FwRule) {	for _, policy := range allowedPolicies {		// if static peer dst rule not for ingress node -> skip		if node.Address.IP != nil {			rules = append(rules, models.FwRule{				SrcIP: net.IPNet{					IP:   node.Address.IP,					Mask: net.CIDRMask(32, 32),				},				DstIP: net.IPNet{					IP:   peer.Address.IP,					Mask: net.CIDRMask(32, 32),				},				Allow: true,			})		}		if node.Address6.IP != nil {			rules = append(rules, models.FwRule{				SrcIP: net.IPNet{					IP:   node.Address6.IP,					Mask: net.CIDRMask(128, 128),				},				DstIP: net.IPNet{					IP:   peer.Address6.IP,					Mask: net.CIDRMask(128, 128),				},				Allow: true,			})		}		if policy.AllowedDirection == models.TrafficDirectionBi {			if node.Address.IP != nil {				rules = append(rules, models.FwRule{					SrcIP: net.IPNet{						IP:   peer.Address.IP,						Mask: net.CIDRMask(32, 32),					},					DstIP: net.IPNet{						IP:   node.Address.IP,						Mask: net.CIDRMask(32, 32),					},					Allow: true,				})			}			if node.Address6.IP != nil {				rules = append(rules, models.FwRule{					SrcIP: net.IPNet{						IP:   peer.Address6.IP,						Mask: net.CIDRMask(128, 128),					},					DstIP: net.IPNet{						IP:   node.Address6.IP,						Mask: net.CIDRMask(128, 128),					},					Allow: true,				})			}		}		if len(node.StaticNode.ExtraAllowedIPs) > 0 {			for _, additionalAllowedIPNet := range node.StaticNode.ExtraAllowedIPs {				_, ipNet, err := net.ParseCIDR(additionalAllowedIPNet)				if err != nil {					continue				}				if ipNet.IP.To4() != nil && peer.Address.IP != nil {					rules = append(rules, models.FwRule{						SrcIP: net.IPNet{							IP:   peer.Address.IP,							Mask: net.CIDRMask(32, 32),						},						DstIP: *ipNet,						Allow: true,					})				} else if peer.Address6.IP != nil {					rules = append(rules, models.FwRule{						SrcIP: net.IPNet{							IP:   peer.Address6.IP,							Mask: net.CIDRMask(128, 128),						},						DstIP: *ipNet,						Allow: true,					})				}			}		}		if len(peer.StaticNode.ExtraAllowedIPs) > 0 {			for _, additionalAllowedIPNet := range peer.StaticNode.ExtraAllowedIPs {				_, ipNet, err := net.ParseCIDR(additionalAllowedIPNet)				if err != nil {					continue				}				if ipNet.IP.To4() != nil && node.Address.IP != nil {					rules = append(rules, models.FwRule{						SrcIP: net.IPNet{							IP:   node.Address.IP,							Mask: net.CIDRMask(32, 32),						},						DstIP: *ipNet,						Allow: true,					})				} else if node.Address6.IP != nil {					rules = append(rules, models.FwRule{						SrcIP: net.IPNet{							IP:   node.Address6.IP,							Mask: net.CIDRMask(128, 128),						},						DstIP: *ipNet,						Allow: true,					})				}			}		}		// add egress range rules		for _, dstI := range policy.Dst {			if dstI.ID == models.EgressID {				e := schema.Egress{ID: dstI.Value}				err := e.Get(db.WithContext(context.TODO()))				if err != nil {					continue				}				dstI.Value = e.Range				ip, cidr, err := net.ParseCIDR(dstI.Value)				if err == nil {					if ip.To4() != nil {						if node.Address.IP != nil {							rules = append(rules, models.FwRule{								SrcIP: net.IPNet{									IP:   node.Address.IP,									Mask: net.CIDRMask(32, 32),								},								DstIP: *cidr,								Allow: true,							})						}					} else {						if node.Address6.IP != nil {							rules = append(rules, models.FwRule{								SrcIP: net.IPNet{									IP:   node.Address6.IP,									Mask: net.CIDRMask(128, 128),								},								DstIP: *cidr,								Allow: true,							})						}					}				}			}		}	}	return}func getUniquePolicies(policies1, policies2 []models.Acl) []models.Acl {	policies1Map := make(map[string]struct{})	for _, policy1I := range policies1 {		policies1Map[policy1I.ID] = struct{}{}	}	for i := len(policies2) - 1; i >= 0; i-- {		if _, ok := policies1Map[policies2[i].ID]; ok {			policies2 = append(policies2[:i], policies2[i+1:]...)		}	}	return policies2}// Sort a slice of net.IP addressesfunc sortIPs(ips []net.IP) {	sort.Slice(ips, func(i, j int) bool {		ip1, ip2 := ips[i].To16(), ips[j].To16()		return string(ip1) < string(ip2) // Compare as byte slices	})}func GetStaticNodeIps(node models.Node) (ips []net.IP) {	defer func() {		sortIPs(ips)	}()	defaultUserPolicy, _ := GetDefaultPolicy(models.NetworkID(node.Network), models.UserPolicy)	defaultDevicePolicy, _ := GetDefaultPolicy(models.NetworkID(node.Network), models.DevicePolicy)	extclients := GetStaticNodesByNetwork(models.NetworkID(node.Network), false)	for _, extclient := range extclients {		if extclient.IsUserNode && defaultUserPolicy.Enabled {			continue		}		if !extclient.IsUserNode && defaultDevicePolicy.Enabled {			continue		}		if extclient.StaticNode.Address != "" {			ips = append(ips, extclient.StaticNode.AddressIPNet4().IP)		}		if extclient.StaticNode.Address6 != "" {			ips = append(ips, extclient.StaticNode.AddressIPNet6().IP)		}	}	return}var MigrateToGws = func() {	nodes, err := GetAllNodes()	if err != nil {		return	}	for _, node := range nodes {		if node.IsIngressGateway || node.IsRelay {			node.IsGw = true			node.IsIngressGateway = true			node.IsRelay = true			if node.Tags == nil {				node.Tags = make(map[models.TagID]struct{})			}			UpsertNode(&node)		}	}}func CheckIfNodeHasAccessToAllResources(targetnode *models.Node, acls []models.Acl) bool {	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)	}	if targetNodeTags == nil {		targetNodeTags = make(map[models.TagID]struct{})	}	targetNodeTags[models.TagID(targetnode.ID.String())] = struct{}{}	targetNodeTags["*"] = struct{}{}	if targetnode.IsGw {		targetNodeTags[models.TagID(fmt.Sprintf("%s.%s", targetnode.Network, models.GwTagName))] = struct{}{}	}	for _, acl := range acls {		if !acl.Enabled || acl.RuleType != models.DevicePolicy {			continue		}		srcTags := ConvAclTagToValueMap(acl.Src)		dstTags := ConvAclTagToValueMap(acl.Dst)		_, srcAll := srcTags["*"]		_, dstAll := dstTags["*"]		for nodeTag := range targetNodeTags {			var existsInSrcTag bool			var existsInDstTag bool			if _, ok := srcTags[nodeTag.String()]; ok {				existsInSrcTag = true			}			if _, ok := srcTags[targetnode.ID.String()]; ok {				existsInSrcTag = true			}			if _, ok := dstTags[nodeTag.String()]; ok {				existsInDstTag = true			}			if _, ok := dstTags[targetnode.ID.String()]; ok {				existsInDstTag = true			}			if acl.AllowedDirection == models.TrafficDirectionBi {				if existsInSrcTag && dstAll || existsInDstTag && srcAll {					return true				}			} else {				if existsInDstTag && srcAll {					return true				}			}		}	}	return false}var CheckIfAnyPolicyisUniDirectional = func(targetNode models.Node, acls []models.Acl) bool {	return false}var CheckIfAnyActiveEgressPolicy = func(targetNode models.Node, acls []models.Acl) bool {	if !targetNode.EgressDetails.IsEgressGateway {		return false	}	var targetNodeTags = make(map[models.TagID]struct{})	targetNodeTags[models.TagID(targetNode.ID.String())] = struct{}{}	targetNodeTags["*"] = struct{}{}	if targetNode.IsGw {		targetNodeTags[models.TagID(fmt.Sprintf("%s.%s", targetNode.Network, models.GwTagName))] = struct{}{}	}	for _, acl := range acls {		if !acl.Enabled || acl.RuleType != models.DevicePolicy {			continue		}		srcTags := ConvAclTagToValueMap(acl.Src)		for _, dst := range acl.Dst {			if dst.ID == models.EgressID {				e := schema.Egress{ID: dst.Value}				err := e.Get(db.WithContext(context.TODO()))				if err == nil && e.Status {					for nodeTag := range targetNodeTags {						if _, ok := srcTags[nodeTag.String()]; ok {							return true						}						if _, ok := srcTags[targetNode.ID.String()]; ok {							return true						}					}				}			}		}	}	return false}var GetAclRulesForNode = func(targetnodeI *models.Node) (rules map[string]models.AclRule) {	targetnode := *targetnodeI	rules = make(map[string]models.AclRule)	acls := ListDevicePolicies(models.NetworkID(targetnode.Network))	targetNodeTags := make(map[models.TagID]struct{})	targetNodeTags[models.TagID(targetnode.ID.String())] = struct{}{}	targetNodeTags["*"] = struct{}{}	for _, acl := range acls {		if !acl.Enabled {			continue		}		srcTags := ConvAclTagToValueMap(acl.Src)		dstTags := ConvAclTagToValueMap(acl.Dst)		nodes := []models.Node{}		for _, dst := range acl.Dst {			if dst.ID == models.EgressID {				e := schema.Egress{ID: dst.Value}				err := e.Get(db.WithContext(context.TODO()))				if err == nil && e.Status {					for nodeID := range e.Nodes {						dstTags[nodeID] = struct{}{}					}				}			}		}		_, srcAll := srcTags["*"]		_, dstAll := dstTags["*"]		aclRule := models.AclRule{			ID:              acl.ID,			AllowedProtocol: acl.Proto,			AllowedPorts:    acl.Port,			Direction:       acl.AllowedDirection,			Allowed:         true,		}		for nodeTag := range targetNodeTags {			if acl.AllowedDirection == models.TrafficDirectionBi {				var existsInSrcTag bool				var existsInDstTag bool				if _, ok := srcTags[nodeTag.String()]; ok || srcAll {					existsInSrcTag = true				}				if _, ok := srcTags[targetnode.ID.String()]; ok || srcAll {					existsInSrcTag = true				}				if _, ok := dstTags[nodeTag.String()]; ok || dstAll {					existsInDstTag = true				}				if _, ok := dstTags[targetnode.ID.String()]; ok || dstAll {					existsInDstTag = true				}				if existsInSrcTag /* && !existsInDstTag*/ {					// get all dst tags					for dst := range dstTags {						if dst == nodeTag.String() {							continue						}						// Get peers in the tags and add allowed rules						if dst != targetnode.ID.String() {							node, err := GetNodeByID(dst)							if err == nil {								nodes = append(nodes, node)							}						}					}					for _, node := range nodes {						if node.ID == targetnode.ID {							continue						}						if node.IsStatic && node.StaticNode.IngressGatewayID == targetnode.ID.String() {							continue						}						if node.Address.IP != nil {							aclRule.IPList = append(aclRule.IPList, node.AddressIPNet4())						}						if node.Address6.IP != nil {							aclRule.IP6List = append(aclRule.IP6List, node.AddressIPNet6())						}						if node.IsStatic && node.StaticNode.Address != "" {							aclRule.IPList = append(aclRule.IPList, node.StaticNode.AddressIPNet4())						}						if node.IsStatic && node.StaticNode.Address6 != "" {							aclRule.IP6List = append(aclRule.IP6List, node.StaticNode.AddressIPNet6())						}					}				}				if existsInDstTag /*&& !existsInSrcTag*/ {					// get all src tags					for src := range srcTags {						if src == nodeTag.String() {							continue						}						// Get peers in the tags and add allowed rules						if src != targetnode.ID.String() {							node, err := GetNodeByID(src)							if err == nil {								nodes = append(nodes, node)							}						}					}					for _, node := range nodes {						if node.ID == targetnode.ID {							continue						}						if node.IsStatic && node.StaticNode.IngressGatewayID == targetnode.ID.String() {							continue						}						if node.Address.IP != nil {							aclRule.IPList = append(aclRule.IPList, node.AddressIPNet4())						}						if node.Address6.IP != nil {							aclRule.IP6List = append(aclRule.IP6List, node.AddressIPNet6())						}						if node.IsStatic && node.StaticNode.Address != "" {							aclRule.IPList = append(aclRule.IPList, node.StaticNode.AddressIPNet4())						}						if node.IsStatic && node.StaticNode.Address6 != "" {							aclRule.IP6List = append(aclRule.IP6List, node.StaticNode.AddressIPNet6())						}					}				}			}		}		if len(aclRule.IPList) > 0 || len(aclRule.IP6List) > 0 {			aclRule.IPList = UniqueIPNetList(aclRule.IPList)			aclRule.IP6List = UniqueIPNetList(aclRule.IP6List)			rules[acl.ID] = aclRule		}	}	return rules}var GetEgressRulesForNode = func(targetnode models.Node) (rules map[string]models.AclRule) {	return}var GetAclRuleForInetGw = func(targetnode models.Node) (rules map[string]models.AclRule) {	return}// Compare two IPs and return true if ip1 < ip2func lessIP(ip1, ip2 net.IP) bool {	ip1 = ip1.To16() // Ensure IPv4 is converted to IPv6-mapped format	ip2 = ip2.To16()	return string(ip1) < string(ip2)}// Sort by IP first, then by prefix lengthfunc sortIPNets(ipNets []net.IPNet) {	sort.Slice(ipNets, func(i, j int) bool {		ip1, ip2 := ipNets[i].IP, ipNets[j].IP		mask1, _ := ipNets[i].Mask.Size()		mask2, _ := ipNets[j].Mask.Size()		// Compare IPs first		if ip1.Equal(ip2) {			return mask1 < mask2 // If same IP, sort by subnet mask size		}		return lessIP(ip1, ip2)	})}func UniqueIPNetList(ipnets []net.IPNet) []net.IPNet {	uniqueMap := make(map[string]net.IPNet)	for _, ipnet := range ipnets {		key := ipnet.String() // Uses CIDR notation as a unique key		if _, exists := uniqueMap[key]; !exists {			uniqueMap[key] = ipnet		}	}	// Convert map back to slice	uniqueList := make([]net.IPNet, 0, len(uniqueMap))	for _, ipnet := range uniqueMap {		uniqueList = append(uniqueList, ipnet)	}	sortIPNets(uniqueList)	return uniqueList}func checkIfAclTagisValid(a models.Acl, t models.AclPolicyTag, isSrc bool) (err error) {	switch t.ID {	case models.NodeID:		if a.RuleType == models.UserPolicy && isSrc {			return errors.New("user policy source mismatch")		}		_, nodeErr := GetNodeByID(t.Value)		if nodeErr != nil {			_, staticNodeErr := GetExtClient(t.Value, a.NetworkID.String())			if staticNodeErr != nil {				return errors.New("invalid node " + t.Value)			}		}	case models.EgressID, models.EgressRange:		e := schema.Egress{			ID: t.Value,		}		err := e.Get(db.WithContext(context.TODO()))		if err != nil {			return errors.New("invalid egress")		}	default:		return errors.New("invalid policy")	}	return nil}var IsAclPolicyValid = func(acl models.Acl) (err error) {	//check if src and dst are valid	if acl.AllowedDirection == models.TrafficDirectionUni {		return errors.New("uni traffic flow not allowed on CE")	}	switch acl.RuleType {	case models.DevicePolicy:		for _, srcI := range acl.Src {			if srcI.Value == "*" {				continue			}			if srcI.ID == models.NodeTagID && srcI.Value == fmt.Sprintf("%s.%s", acl.NetworkID.String(), models.GwTagName) {				continue			}			if err = checkIfAclTagisValid(acl, srcI, true); err != nil {				return err			}		}		for _, dstI := range acl.Dst {			if dstI.Value == "*" {				continue			}			if dstI.ID == models.NodeTagID && dstI.Value == fmt.Sprintf("%s.%s", acl.NetworkID.String(), models.GwTagName) {				continue			}			if err = checkIfAclTagisValid(acl, dstI, false); err != nil {				return			}		}	default:		return errors.New("unknown acl policy type " + string(acl.RuleType))	}	return nil}var IsPeerAllowed = func(node, peer models.Node, checkDefaultPolicy bool) bool {	var nodeId, peerId string	if node.IsGw && peer.IsRelayed && peer.RelayedBy == node.ID.String() {		return true	}	if peer.IsGw && node.IsRelayed && node.RelayedBy == peer.ID.String() {		return true	}	if node.IsStatic {		nodeId = node.StaticNode.ClientID		node = node.StaticNode.ConvertToStaticNode()	} else {		nodeId = node.ID.String()	}	if peer.IsStatic {		peerId = peer.StaticNode.ClientID		peer = peer.StaticNode.ConvertToStaticNode()	} else {		peerId = peer.ID.String()	}	peerTags := make(map[models.TagID]struct{})	nodeTags := make(map[models.TagID]struct{})	nodeTags[models.TagID(nodeId)] = struct{}{}	peerTags[models.TagID(peerId)] = struct{}{}	if peer.IsGw {		peerTags[models.TagID(fmt.Sprintf("%s.%s", peer.Network, models.GwTagName))] = struct{}{}	}	if node.IsGw {		nodeTags[models.TagID(fmt.Sprintf("%s.%s", node.Network, models.GwTagName))] = struct{}{}	}	if checkDefaultPolicy {		// check default policy if all allowed return true		defaultPolicy, err := GetDefaultPolicy(models.NetworkID(node.Network), models.DevicePolicy)		if err == nil {			if defaultPolicy.Enabled {				return true			}		}	}	// list device policies	policies := ListDevicePolicies(models.NetworkID(peer.Network))	srcMap := make(map[string]struct{})	dstMap := make(map[string]struct{})	defer func() {		srcMap = nil		dstMap = nil	}()	for _, policy := range policies {		if !policy.Enabled {			continue		}		srcMap = ConvAclTagToValueMap(policy.Src)		dstMap = ConvAclTagToValueMap(policy.Dst)		for _, dst := range policy.Dst {			if dst.ID == models.EgressID {				e := schema.Egress{ID: dst.Value}				err := e.Get(db.WithContext(context.TODO()))				if err == nil && e.Status {					for nodeID := range e.Nodes {						dstMap[nodeID] = struct{}{}					}				}			}		}		if CheckTagGroupPolicy(srcMap, dstMap, node, peer, nodeTags, peerTags) {			return true		}	}	return false}func CheckTagGroupPolicy(srcMap, dstMap map[string]struct{}, node, peer models.Node,	nodeTags, peerTags map[models.TagID]struct{}) bool {	// check for node ID	if _, ok := srcMap[node.ID.String()]; ok {		if _, ok = dstMap[peer.ID.String()]; ok {			return true		}	}	if _, ok := dstMap[node.ID.String()]; ok {		if _, ok = srcMap[peer.ID.String()]; ok {			return true		}	}	for tagID := range nodeTags {		if _, ok := dstMap[tagID.String()]; ok {			if _, ok := srcMap["*"]; ok {				return true			}			for tagID := range peerTags {				if _, ok := srcMap[tagID.String()]; ok {					return true				}			}		}		if _, ok := srcMap[tagID.String()]; ok {			if _, ok := dstMap["*"]; ok {				return true			}			for tagID := range peerTags {				if _, ok := dstMap[tagID.String()]; ok {					return true				}			}		}	}	for tagID := range peerTags {		if _, ok := dstMap[tagID.String()]; ok {			if _, ok := srcMap["*"]; ok {				return true			}			for tagID := range nodeTags {				if _, ok := srcMap[tagID.String()]; ok {					return true				}			}		}		if _, ok := srcMap[tagID.String()]; ok {			if _, ok := dstMap["*"]; ok {				return true			}			for tagID := range nodeTags {				if _, ok := dstMap[tagID.String()]; ok {					return true				}			}		}	}	return false}var GetInetClientsFromAclPolicies = func(eID string) (inetClientIDs []string) {	e := schema.Egress{ID: eID}	err := e.Get(db.WithContext(context.TODO()))	if err != nil || !e.Status {		return	}	acls, _ := ListAclsByNetwork(models.NetworkID(e.Network))	for _, acl := range acls {		for _, dstI := range acl.Dst {			if dstI.ID == models.EgressID {				if dstI.Value != eID {					continue				}				for _, srcI := range acl.Src {					if srcI.Value == "*" {						continue					}					if srcI.ID == models.NodeID {						inetClientIDs = append(inetClientIDs, srcI.Value)					}				}			}		}	}	return}var (	CreateDefaultTags = func(netID models.NetworkID) {}	DeleteAllNetworkTags = func(networkID models.NetworkID) {}	IsUserAllowedToCommunicate = func(userName string, peer models.Node) (bool, []models.Acl) {		return false, []models.Acl{}	}	RemoveUserFromAclPolicy = func(userName string) {})var (	aclCacheMutex = &sync.RWMutex{}	aclCacheMap   = make(map[string]models.Acl))func MigrateAclPolicies() {	acls := ListAcls()	for _, acl := range acls {		if acl.Proto.String() == "" {			acl.Proto = models.ALL			acl.ServiceType = models.Any			acl.Port = []string{}			UpsertAcl(acl)		}	}}// IsNodeAllowedToCommunicate - check node is allowed to communicate with the peer // ADD ALLOWED DIRECTION - 0 => node -> peer, 1 => peer-> node,func isNodeAllowedToCommunicate(node, peer models.Node, checkDefaultPolicy bool) (bool, []models.Acl) {	var nodeId, peerId string	// if node.IsGw && peer.IsRelayed && peer.RelayedBy == node.ID.String() {	// 	return true, []models.Acl{}	// }	// if peer.IsGw && node.IsRelayed && node.RelayedBy == peer.ID.String() {	// 	return true, []models.Acl{}	// }	if node.IsStatic {		nodeId = node.StaticNode.ClientID		node = node.StaticNode.ConvertToStaticNode()	} else {		nodeId = node.ID.String()	}	if peer.IsStatic {		peerId = peer.StaticNode.ClientID		peer = peer.StaticNode.ConvertToStaticNode()	} else {		peerId = peer.ID.String()	}	nodeTags := make(map[models.TagID]struct{})	peerTags := make(map[models.TagID]struct{})	nodeTags[models.TagID(nodeId)] = struct{}{}	peerTags[models.TagID(peerId)] = struct{}{}	if peer.IsGw {		peerTags[models.TagID(fmt.Sprintf("%s.%s", peer.Network, models.GwTagName))] = struct{}{}	}	if node.IsGw {		nodeTags[models.TagID(fmt.Sprintf("%s.%s", node.Network, models.GwTagName))] = struct{}{}	}	if checkDefaultPolicy {		// check default policy if all allowed return true		defaultPolicy, err := GetDefaultPolicy(models.NetworkID(node.Network), models.DevicePolicy)		if err == nil {			if defaultPolicy.Enabled {				return true, []models.Acl{defaultPolicy}			}		}	}	allowedPolicies := []models.Acl{}	defer func() {		allowedPolicies = UniquePolicies(allowedPolicies)	}()	// list device policies	policies := ListDevicePolicies(models.NetworkID(peer.Network))	srcMap := make(map[string]struct{})	dstMap := make(map[string]struct{})	defer func() {		srcMap = nil		dstMap = nil	}()	for _, policy := range policies {		if !policy.Enabled {			continue		}		allowed := false		srcMap = ConvAclTagToValueMap(policy.Src)		dstMap = ConvAclTagToValueMap(policy.Dst)		for _, dst := range policy.Dst {			if dst.ID == models.EgressID {				e := schema.Egress{ID: dst.Value}				err := e.Get(db.WithContext(context.TODO()))				if err == nil && e.Status {					for nodeID := range e.Nodes {						dstMap[nodeID] = struct{}{}					}				}			}		}		_, srcAll := srcMap["*"]		_, dstAll := dstMap["*"]		if policy.AllowedDirection == models.TrafficDirectionBi {			if _, ok := srcMap[nodeId]; ok || srcAll {				if _, ok := dstMap[peerId]; ok || dstAll {					allowedPolicies = append(allowedPolicies, policy)					continue				}			}			if _, ok := dstMap[nodeId]; ok || dstAll {				if _, ok := srcMap[peerId]; ok || srcAll {					allowedPolicies = append(allowedPolicies, policy)					continue				}			}		}		if _, ok := dstMap[peerId]; ok || dstAll {			if _, ok := srcMap[nodeId]; ok || srcAll {				allowedPolicies = append(allowedPolicies, policy)				continue			}		}		if policy.AllowedDirection == models.TrafficDirectionBi {			for tagID := range nodeTags {				if _, ok := dstMap[tagID.String()]; ok || dstAll {					if srcAll {						allowed = true						break					}					for tagID := range peerTags {						if _, ok := srcMap[tagID.String()]; ok {							allowed = true							break						}					}				}				if allowed {					allowedPolicies = append(allowedPolicies, policy)					break				}				if _, ok := srcMap[tagID.String()]; ok || srcAll {					if dstAll {						allowed = true						break					}					for tagID := range peerTags {						if _, ok := dstMap[tagID.String()]; ok {							allowed = true							break						}					}				}				if allowed {					break				}			}			if allowed {				allowedPolicies = append(allowedPolicies, policy)				continue			}		}		for tagID := range peerTags {			if _, ok := dstMap[tagID.String()]; ok || dstAll {				if srcAll {					allowed = true					break				}				for tagID := range nodeTags {					if _, ok := srcMap[tagID.String()]; ok {						allowed = true						break					}				}			}			if allowed {				break			}		}		if allowed {			allowedPolicies = append(allowedPolicies, policy)		}	}	if len(allowedPolicies) > 0 {		return true, allowedPolicies	}	return false, allowedPolicies}// GetDefaultPolicy - fetches default policy in the network by ruleTypefunc GetDefaultPolicy(netID models.NetworkID, ruleType models.AclPolicyType) (models.Acl, error) {	aclID := "all-users"	if ruleType == models.DevicePolicy {		aclID = "all-nodes"	}	acl, err := GetAcl(fmt.Sprintf("%s.%s", netID, aclID))	if err != nil {		return models.Acl{}, errors.New("default rule not found")	}	if acl.Enabled {		return acl, nil	}	// check if there are any custom all policies	srcMap := make(map[string]struct{})	dstMap := make(map[string]struct{})	defer func() {		srcMap = nil		dstMap = nil	}()	policies, _ := ListAclsByNetwork(netID)	for _, policy := range policies {		if !policy.Enabled {			continue		}		if policy.RuleType == ruleType {			dstMap = ConvAclTagToValueMap(policy.Dst)			srcMap = ConvAclTagToValueMap(policy.Src)			if _, ok := srcMap["*"]; ok {				if _, ok := dstMap["*"]; ok {					return policy, nil				}			}		}	}	return acl, nil}// ListAcls - lists all acl policiesfunc ListAclsByNetwork(netID models.NetworkID) ([]models.Acl, error) {	allAcls := ListAcls()	netAcls := []models.Acl{}	for _, acl := range allAcls {		if !servercfg.IsPro && acl.RuleType == models.UserPolicy {			continue		}		if acl.NetworkID == netID {			netAcls = append(netAcls, acl)		}	}	return netAcls, nil}// ListEgressAcls - list egress acl policiesfunc ListEgressAcls(eID string) ([]models.Acl, error) {	allAcls := ListAcls()	egressAcls := []models.Acl{}	for _, acl := range allAcls {		if !servercfg.IsPro && acl.RuleType == models.UserPolicy {			continue		}		for _, dst := range acl.Dst {			if dst.ID == models.EgressID && dst.Value == eID {				egressAcls = append(egressAcls, acl)			}		}	}	return egressAcls, nil}// ListDevicePolicies - lists all device policies in a networkfunc ListDevicePolicies(netID models.NetworkID) []models.Acl {	allAcls := ListAcls()	deviceAcls := []models.Acl{}	for _, acl := range allAcls {		if acl.NetworkID == netID && acl.RuleType == models.DevicePolicy {			deviceAcls = append(deviceAcls, acl)		}	}	return deviceAcls}func ConvAclTagToValueMap(acltags []models.AclPolicyTag) map[string]struct{} {	aclValueMap := make(map[string]struct{})	for _, aclTagI := range acltags {		aclValueMap[aclTagI.Value] = struct{}{}	}	return aclValueMap}func UniqueAclPolicyTags(tags []models.AclPolicyTag) []models.AclPolicyTag {	seen := make(map[string]bool)	var result []models.AclPolicyTag	for _, tag := range tags {		key := fmt.Sprintf("%v-%s", tag.ID, tag.Value)		if !seen[key] {			seen[key] = true			result = append(result, tag)		}	}	return result}// UpdateAcl - updates allowed fields on acls and commits to DBfunc UpdateAcl(newAcl, acl models.Acl) error {	if !acl.Default {		acl.Name = newAcl.Name		acl.Src = newAcl.Src		acl.Dst = newAcl.Dst		acl.AllowedDirection = newAcl.AllowedDirection		acl.Port = newAcl.Port		acl.Proto = newAcl.Proto		acl.ServiceType = newAcl.ServiceType	}	if newAcl.ServiceType == models.Any {		acl.Port = []string{}		acl.Proto = models.ALL	}	acl.Enabled = newAcl.Enabled	d, err := json.Marshal(acl)	if err != nil {		return err	}	err = database.Insert(acl.ID, string(d), database.ACLS_TABLE_NAME)	if err == nil && servercfg.CacheEnabled() {		storeAclInCache(acl)	}	return err}// UpsertAcl - upserts aclfunc UpsertAcl(acl models.Acl) error {	d, err := json.Marshal(acl)	if err != nil {		return err	}	err = database.Insert(acl.ID, string(d), database.ACLS_TABLE_NAME)	if err == nil && servercfg.CacheEnabled() {		storeAclInCache(acl)	}	return err}// DeleteAcl - deletes acl policyfunc DeleteAcl(a models.Acl) error {	err := database.DeleteRecord(database.ACLS_TABLE_NAME, a.ID)	if err == nil && servercfg.CacheEnabled() {		removeAclFromCache(a)	}	return err}func ListAcls() (acls []models.Acl) {	if servercfg.CacheEnabled() && len(aclCacheMap) > 0 {		return listAclFromCache()	}	data, err := database.FetchRecords(database.ACLS_TABLE_NAME)	if err != nil && !database.IsEmptyRecord(err) {		return []models.Acl{}	}	for _, dataI := range data {		acl := models.Acl{}		err := json.Unmarshal([]byte(dataI), &acl)		if err != nil {			continue		}		if !servercfg.IsPro {			if acl.RuleType == models.UserPolicy {				continue			}			skip := false			for _, srcI := range acl.Src {				if srcI.ID == models.NodeTagID && (srcI.Value != "*" && srcI.Value != fmt.Sprintf("%s.%s", acl.NetworkID.String(), models.GwTagName)) {					skip = true					break				}			}			if skip {				continue			}			for _, dstI := range acl.Dst {				if dstI.ID == models.NodeTagID && (dstI.Value != "*" && dstI.Value != fmt.Sprintf("%s.%s", acl.NetworkID.String(), models.GwTagName)) {					skip = true					break				}			}			if skip {				continue			}		}		acls = append(acls, acl)		if servercfg.CacheEnabled() {			storeAclInCache(acl)		}	}	return}func UniquePolicies(items []models.Acl) []models.Acl {	if len(items) == 0 {		return items	}	seen := make(map[string]bool)	var result []models.Acl	for _, item := range items {		if !seen[item.ID] {			seen[item.ID] = true			result = append(result, item)		}	}	return result}// DeleteNetworkPolicies - deletes all default network acl policiesfunc DeleteNetworkPolicies(netId models.NetworkID) {	acls, _ := ListAclsByNetwork(netId)	for _, acl := range acls {		if acl.NetworkID == netId {			DeleteAcl(acl)		}	}}// SortTagEntrys - Sorts slice of Tag entries by their idfunc SortAclEntrys(acls []models.Acl) {	sort.Slice(acls, func(i, j int) bool {		return acls[i].Name < acls[j].Name	})}// ValidateCreateAclReq - validates create req for aclfunc ValidateCreateAclReq(req models.Acl) error {	// check if acl network exists	_, err := GetNetwork(req.NetworkID.String())	if err != nil {		return errors.New("failed to get network details for " + req.NetworkID.String())	}	// err = CheckIDSyntax(req.Name)	// if err != nil {	// 	return err	// }	return nil}func listAclFromCache() (acls []models.Acl) {	aclCacheMutex.RLock()	defer aclCacheMutex.RUnlock()	for _, acl := range aclCacheMap {		acls = append(acls, acl)	}	return}func storeAclInCache(a models.Acl) {	aclCacheMutex.Lock()	defer aclCacheMutex.Unlock()	aclCacheMap[a.ID] = a}func removeAclFromCache(a models.Acl) {	aclCacheMutex.Lock()	defer aclCacheMutex.Unlock()	delete(aclCacheMap, a.ID)}func getAclFromCache(aID string) (a models.Acl, ok bool) {	aclCacheMutex.RLock()	defer aclCacheMutex.RUnlock()	a, ok = aclCacheMap[aID]	return}// InsertAcl - creates acl policyfunc InsertAcl(a models.Acl) error {	d, err := json.Marshal(a)	if err != nil {		return err	}	err = database.Insert(a.ID, string(d), database.ACLS_TABLE_NAME)	if err == nil && servercfg.CacheEnabled() {		storeAclInCache(a)	}	return err}// GetAcl - gets acl info by idfunc GetAcl(aID string) (models.Acl, error) {	a := models.Acl{}	if servercfg.CacheEnabled() {		var ok bool		a, ok = getAclFromCache(aID)		if ok {			return a, nil		}	}	d, err := database.FetchRecord(database.ACLS_TABLE_NAME, aID)	if err != nil {		return a, err	}	err = json.Unmarshal([]byte(d), &a)	if err != nil {		return a, err	}	if servercfg.CacheEnabled() {		storeAclInCache(a)	}	return a, nil}// IsAclExists - checks if acl existsfunc IsAclExists(aclID string) bool {	_, err := GetAcl(aclID)	return err == nil}func RemoveNodeFromAclPolicy(node models.Node) {	var nodeID string	if node.IsStatic {		nodeID = node.StaticNode.ClientID	} else {		nodeID = node.ID.String()	}	acls, _ := ListAclsByNetwork(models.NetworkID(node.Network))	for _, acl := range acls {		delete := false		update := false		if acl.RuleType == models.DevicePolicy {			for i := len(acl.Src) - 1; i >= 0; i-- {				if acl.Src[i].ID == models.NodeID && acl.Src[i].Value == nodeID {					if len(acl.Src) == 1 {						// delete policy						delete = true						break					} else {						acl.Src = append(acl.Src[:i], acl.Src[i+1:]...)						update = true					}				}			}			if delete {				DeleteAcl(acl)				continue			}			for i := len(acl.Dst) - 1; i >= 0; i-- {				if acl.Dst[i].ID == models.NodeID && acl.Dst[i].Value == nodeID {					if len(acl.Dst) == 1 {						// delete policy						delete = true						break					} else {						acl.Dst = append(acl.Dst[:i], acl.Dst[i+1:]...)						update = true					}				}			}			if delete {				DeleteAcl(acl)				continue			}			if update {				UpsertAcl(acl)			}		}		if acl.RuleType == models.UserPolicy {			for i := len(acl.Dst) - 1; i >= 0; i-- {				if acl.Dst[i].ID == models.NodeID && acl.Dst[i].Value == nodeID {					if len(acl.Dst) == 1 {						// delete policy						delete = true						break					} else {						acl.Dst = append(acl.Dst[:i], acl.Dst[i+1:]...)						update = true					}				}			}			if delete {				DeleteAcl(acl)				continue			}			if update {				UpsertAcl(acl)			}		}	}}// CreateDefaultAclNetworkPolicies - create default acl network policiesfunc CreateDefaultAclNetworkPolicies(netID models.NetworkID) {	if netID.String() == "" {		return	}	_, _ = ListAclsByNetwork(netID)	if !IsAclExists(fmt.Sprintf("%s.%s", netID, "all-nodes")) {		defaultDeviceAcl := models.Acl{			ID:          fmt.Sprintf("%s.%s", netID, "all-nodes"),			Name:        "All Nodes",			MetaData:    "This Policy allows all nodes in the network to communicate with each other",			Default:     true,			NetworkID:   netID,			Proto:       models.ALL,			ServiceType: models.Any,			Port:        []string{},			RuleType:    models.DevicePolicy,			Src: []models.AclPolicyTag{				{					ID:    models.NodeTagID,					Value: "*",				}},			Dst: []models.AclPolicyTag{				{					ID:    models.NodeTagID,					Value: "*",				}},			AllowedDirection: models.TrafficDirectionBi,			Enabled:          true,			CreatedBy:        "auto",			CreatedAt:        time.Now().UTC(),		}		InsertAcl(defaultDeviceAcl)	}	if !IsAclExists(fmt.Sprintf("%s.%s", netID, "all-gateways")) {		defaultUserAcl := models.Acl{			ID:          fmt.Sprintf("%s.%s", netID, "all-gateways"),			Default:     true,			Name:        "All Gateways",			NetworkID:   netID,			Proto:       models.ALL,			ServiceType: models.Any,			Port:        []string{},			RuleType:    models.DevicePolicy,			Src: []models.AclPolicyTag{				{					ID:    models.NodeTagID,					Value: fmt.Sprintf("%s.%s", netID, models.GwTagName),				},			},			Dst: []models.AclPolicyTag{				{					ID:    models.NodeTagID,					Value: "*",				},			},			AllowedDirection: models.TrafficDirectionBi,			Enabled:          true,			CreatedBy:        "auto",			CreatedAt:        time.Now().UTC(),		}		InsertAcl(defaultUserAcl)	}	CreateDefaultUserPolicies(netID)}
 |