nezha/service/singleton/ddns.go

107 lines
2.6 KiB
Go
Raw Normal View History

package singleton
import (
"fmt"
2024-10-23 22:21:59 -04:00
"slices"
"sync"
2024-07-06 22:19:58 -04:00
"github.com/libdns/cloudflare"
tencentcloud "github.com/nezhahq/libdns-tencentcloud"
"github.com/naiba/nezha/model"
2024-07-06 22:19:58 -04:00
ddns2 "github.com/naiba/nezha/pkg/ddns"
"github.com/naiba/nezha/pkg/ddns/dummy"
"github.com/naiba/nezha/pkg/ddns/webhook"
)
var (
2024-10-21 04:22:30 -04:00
DDNSCache map[uint64]*model.DDNSProfile
DDNSCacheLock sync.RWMutex
2024-10-23 22:21:59 -04:00
DDNSList []*model.DDNSProfile
)
func initDDNS() {
DB.Find(&DDNSList)
2024-10-21 04:22:30 -04:00
DDNSCacheLock.Lock()
DDNSCache = make(map[uint64]*model.DDNSProfile)
for i := 0; i < len(DDNSList); i++ {
DDNSCache[DDNSList[i].ID] = DDNSList[i]
}
2024-10-23 22:21:59 -04:00
DDNSCacheLock.Unlock()
OnNameserverUpdate()
}
func OnDDNSUpdate(p *model.DDNSProfile) {
DDNSCacheLock.Lock()
defer DDNSCacheLock.Unlock()
DDNSCache[p.ID] = p
}
func OnDDNSDelete(id []uint64) {
DDNSCacheLock.Lock()
defer DDNSCacheLock.Unlock()
for _, i := range id {
delete(DDNSCache, i)
}
}
func UpdateDDNSList() {
DDNSCacheLock.RLock()
defer DDNSCacheLock.RUnlock()
DDNSList = make([]*model.DDNSProfile, 0, len(DDNSCache))
for _, p := range DDNSCache {
DDNSList = append(DDNSList, p)
}
slices.SortFunc(DDNSList, func(a, b *model.DDNSProfile) int {
if a.ID < b.ID {
return -1
} else if a.ID == b.ID {
return 0
}
return 1
})
}
func OnNameserverUpdate() {
ddns2.InitDNSServers(Conf.DNSServers)
}
func GetDDNSProvidersFromProfiles(profileId []uint64, ip *ddns2.IP) ([]*ddns2.Provider, error) {
profiles := make([]*model.DDNSProfile, 0, len(profileId))
2024-10-21 04:22:30 -04:00
DDNSCacheLock.RLock()
for _, id := range profileId {
2024-10-21 04:22:30 -04:00
if profile, ok := DDNSCache[id]; ok {
profiles = append(profiles, profile)
} else {
2024-10-30 15:34:25 -04:00
DDNSCacheLock.RUnlock()
return nil, fmt.Errorf("无法找到DDNS配置 ID %d", id)
}
}
2024-10-21 04:22:30 -04:00
DDNSCacheLock.RUnlock()
providers := make([]*ddns2.Provider, 0, len(profiles))
for _, profile := range profiles {
provider := &ddns2.Provider{DDNSProfile: profile, IPAddrs: ip}
switch profile.Provider {
case model.ProviderDummy:
provider.Setter = &dummy.Provider{}
providers = append(providers, provider)
case model.ProviderWebHook:
provider.Setter = &webhook.Provider{DDNSProfile: profile}
providers = append(providers, provider)
case model.ProviderCloudflare:
provider.Setter = &cloudflare.Provider{APIToken: profile.AccessSecret}
providers = append(providers, provider)
case model.ProviderTencentCloud:
provider.Setter = &tencentcloud.Provider{SecretId: profile.AccessID, SecretKey: profile.AccessSecret}
providers = append(providers, provider)
default:
return nil, fmt.Errorf("无法找到配置的DDNS提供者 %s", profile.Provider)
}
}
return providers, nil
}