mirror of
https://github.com/nezhahq/nezha.git
synced 2025-01-22 12:48:14 -05:00
653d0cf2e9
* [WIP] feat: user roles * update * update * admin handler * update * feat: user-specific connection secret * simplify some logics * cleanup * update waf * update user api error handling * update waf api * fix codeql * update waf table * fix several problems * add pagination for waf api * update permission checks * switch to runtime check * 1 * cover? * some changes
72 lines
1.4 KiB
Go
72 lines
1.4 KiB
Go
package singleton
|
|
|
|
import (
|
|
"cmp"
|
|
"slices"
|
|
"sync"
|
|
|
|
"github.com/nezhahq/nezha/model"
|
|
"github.com/nezhahq/nezha/pkg/utils"
|
|
)
|
|
|
|
var (
|
|
NATCache = make(map[string]*model.NAT)
|
|
NATCacheRwLock sync.RWMutex
|
|
|
|
NATIDToDomain = make(map[uint64]string)
|
|
NATList []*model.NAT
|
|
NATListLock sync.RWMutex
|
|
)
|
|
|
|
func initNAT() {
|
|
DB.Find(&NATList)
|
|
NATCache = make(map[string]*model.NAT)
|
|
for i := 0; i < len(NATList); i++ {
|
|
NATCache[NATList[i].Domain] = NATList[i]
|
|
NATIDToDomain[NATList[i].ID] = NATList[i].Domain
|
|
}
|
|
}
|
|
|
|
func OnNATUpdate(n *model.NAT) {
|
|
NATCacheRwLock.Lock()
|
|
defer NATCacheRwLock.Unlock()
|
|
|
|
if oldDomain, ok := NATIDToDomain[n.ID]; ok && oldDomain != n.Domain {
|
|
delete(NATCache, oldDomain)
|
|
}
|
|
|
|
NATCache[n.Domain] = n
|
|
NATIDToDomain[n.ID] = n.Domain
|
|
}
|
|
|
|
func OnNATDelete(id []uint64) {
|
|
NATCacheRwLock.Lock()
|
|
defer NATCacheRwLock.Unlock()
|
|
|
|
for _, i := range id {
|
|
if domain, ok := NATIDToDomain[i]; ok {
|
|
delete(NATCache, domain)
|
|
delete(NATIDToDomain, i)
|
|
}
|
|
}
|
|
}
|
|
|
|
func UpdateNATList() {
|
|
NATCacheRwLock.RLock()
|
|
defer NATCacheRwLock.RUnlock()
|
|
|
|
NATListLock.Lock()
|
|
defer NATListLock.Unlock()
|
|
|
|
NATList = utils.MapValuesToSlice(NATCache)
|
|
slices.SortFunc(NATList, func(a, b *model.NAT) int {
|
|
return cmp.Compare(a.ID, b.ID)
|
|
})
|
|
}
|
|
|
|
func GetNATConfigByDomain(domain string) *model.NAT {
|
|
NATCacheRwLock.RLock()
|
|
defer NATCacheRwLock.RUnlock()
|
|
return NATCache[domain]
|
|
}
|