refactor: simplify server & service manipulation (#993)

* refactor: simplify server & service manipulation

* update

* fix

* update for nat, ddns & notification

* chore

* update cron

* update dependencies

* use of function iterators

* update default dns servers
This commit is contained in:
UUBulb
2025-02-21 23:08:12 +08:00
committed by GitHub
parent 21eefde995
commit 91bef2882a
32 changed files with 987 additions and 1083 deletions

View File

@@ -4,7 +4,6 @@ import (
"cmp"
"fmt"
"slices"
"sync"
"github.com/libdns/cloudflare"
tencentcloud "github.com/nezhahq/libdns-tencentcloud"
@@ -16,67 +15,61 @@ import (
"github.com/nezhahq/nezha/pkg/utils"
)
var (
DDNSCache map[uint64]*model.DDNSProfile
DDNSCacheLock sync.RWMutex
DDNSList []*model.DDNSProfile
DDNSListLock sync.RWMutex
)
type DDNSClass struct {
class[uint64, *model.DDNSProfile]
}
func initDDNS() {
DB.Find(&DDNSList)
DDNSCache = make(map[uint64]*model.DDNSProfile)
for i := 0; i < len(DDNSList); i++ {
DDNSCache[DDNSList[i].ID] = DDNSList[i]
func NewDDNSClass() *DDNSClass {
var sortedList []*model.DDNSProfile
DB.Find(&sortedList)
list := make(map[uint64]*model.DDNSProfile, len(sortedList))
for _, profile := range sortedList {
list[profile.ID] = profile
}
dc := &DDNSClass{
class: class[uint64, *model.DDNSProfile]{
list: list,
sortedList: sortedList,
},
}
OnNameserverUpdate()
return dc
}
func OnDDNSUpdate(p *model.DDNSProfile) {
DDNSCacheLock.Lock()
defer DDNSCacheLock.Unlock()
DDNSCache[p.ID] = p
func (c *DDNSClass) Update(p *model.DDNSProfile) {
c.listMu.Lock()
c.list[p.ID] = p
c.listMu.Unlock()
c.sortList()
}
func OnDDNSDelete(id []uint64) {
DDNSCacheLock.Lock()
defer DDNSCacheLock.Unlock()
for _, i := range id {
delete(DDNSCache, i)
func (c *DDNSClass) Delete(idList []uint64) {
c.listMu.Lock()
for _, id := range idList {
delete(c.list, id)
}
c.listMu.Unlock()
c.sortList()
}
func UpdateDDNSList() {
DDNSCacheLock.RLock()
defer DDNSCacheLock.RUnlock()
DDNSListLock.Lock()
defer DDNSListLock.Unlock()
DDNSList = utils.MapValuesToSlice(DDNSCache)
slices.SortFunc(DDNSList, func(a, b *model.DDNSProfile) int {
return cmp.Compare(a.ID, b.ID)
})
}
func OnNameserverUpdate() {
ddns2.InitDNSServers(Conf.DNSServers)
}
func GetDDNSProvidersFromProfiles(profileId []uint64, ip *ddns2.IP) ([]*ddns2.Provider, error) {
func (c *DDNSClass) GetDDNSProvidersFromProfiles(profileId []uint64, ip *model.IP) ([]*ddns2.Provider, error) {
profiles := make([]*model.DDNSProfile, 0, len(profileId))
DDNSCacheLock.RLock()
c.listMu.RLock()
for _, id := range profileId {
if profile, ok := DDNSCache[id]; ok {
if profile, ok := c.list[id]; ok {
profiles = append(profiles, profile)
} else {
DDNSCacheLock.RUnlock()
c.listMu.RUnlock()
return nil, fmt.Errorf("无法找到DDNS配置 ID %d", id)
}
}
DDNSCacheLock.RUnlock()
c.listMu.RUnlock()
providers := make([]*ddns2.Provider, 0, len(profiles))
for _, profile := range profiles {
@@ -100,3 +93,21 @@ func GetDDNSProvidersFromProfiles(profileId []uint64, ip *ddns2.IP) ([]*ddns2.Pr
}
return providers, nil
}
func (c *DDNSClass) sortList() {
c.listMu.RLock()
defer c.listMu.RUnlock()
sortedList := utils.MapValuesToSlice(c.list)
slices.SortFunc(sortedList, func(a, b *model.DDNSProfile) int {
return cmp.Compare(a.ID, b.ID)
})
c.sortedListMu.Lock()
defer c.sortedListMu.Unlock()
c.sortedList = sortedList
}
func OnNameserverUpdate() {
ddns2.InitDNSServers(Conf.DNSServers)
}