2024-02-24 08:10:27 -05:00
|
|
|
package singleton
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-10-17 09:03:03 -04:00
|
|
|
"sync"
|
2024-07-06 22:19:58 -04:00
|
|
|
|
2024-10-17 09:03:03 -04:00
|
|
|
"github.com/libdns/cloudflare"
|
|
|
|
"github.com/libdns/tencentcloud"
|
|
|
|
|
|
|
|
"github.com/naiba/nezha/model"
|
2024-07-06 22:19:58 -04:00
|
|
|
ddns2 "github.com/naiba/nezha/pkg/ddns"
|
2024-10-17 09:03:03 -04:00
|
|
|
"github.com/naiba/nezha/pkg/ddns/dummy"
|
|
|
|
"github.com/naiba/nezha/pkg/ddns/webhook"
|
2024-02-24 08:10:27 -05:00
|
|
|
)
|
|
|
|
|
2024-10-17 09:03:03 -04:00
|
|
|
var (
|
2024-10-21 04:22:30 -04:00
|
|
|
DDNSCache map[uint64]*model.DDNSProfile
|
|
|
|
DDNSCacheLock sync.RWMutex
|
2024-08-23 23:11:06 -04:00
|
|
|
)
|
|
|
|
|
2024-10-17 09:03:03 -04:00
|
|
|
func initDDNS() {
|
|
|
|
OnDDNSUpdate()
|
2024-10-18 10:06:01 -04:00
|
|
|
OnNameserverUpdate()
|
2024-02-24 08:10:27 -05:00
|
|
|
}
|
|
|
|
|
2024-10-17 09:03:03 -04:00
|
|
|
func OnDDNSUpdate() {
|
|
|
|
var ddns []*model.DDNSProfile
|
|
|
|
DB.Find(&ddns)
|
2024-10-21 04:22:30 -04:00
|
|
|
DDNSCacheLock.Lock()
|
|
|
|
defer DDNSCacheLock.Unlock()
|
|
|
|
DDNSCache = make(map[uint64]*model.DDNSProfile)
|
2024-10-17 09:03:03 -04:00
|
|
|
for i := 0; i < len(ddns); i++ {
|
2024-10-21 04:22:30 -04:00
|
|
|
DDNSCache[ddns[i].ID] = ddns[i]
|
2024-02-24 08:10:27 -05:00
|
|
|
}
|
|
|
|
}
|
2024-04-27 01:36:36 -04:00
|
|
|
|
2024-10-18 10:06:01 -04:00
|
|
|
func OnNameserverUpdate() {
|
|
|
|
ddns2.InitDNSServers(Conf.DNSServers)
|
|
|
|
}
|
|
|
|
|
2024-10-17 09:03:03 -04:00
|
|
|
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()
|
2024-10-17 09:03:03 -04:00
|
|
|
for _, id := range profileId {
|
2024-10-21 04:22:30 -04:00
|
|
|
if profile, ok := DDNSCache[id]; ok {
|
2024-10-17 09:03:03 -04:00
|
|
|
profiles = append(profiles, profile)
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("无法找到DDNS配置 ID %d", id)
|
|
|
|
}
|
2024-04-27 01:36:36 -04:00
|
|
|
}
|
2024-10-21 04:22:30 -04:00
|
|
|
DDNSCacheLock.RUnlock()
|
2024-04-27 01:36:36 -04:00
|
|
|
|
2024-10-17 09:03:03 -04:00
|
|
|
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提供者ID %d", profile.Provider)
|
2024-04-27 01:36:36 -04:00
|
|
|
}
|
|
|
|
}
|
2024-10-17 09:03:03 -04:00
|
|
|
return providers, nil
|
2024-04-27 01:36:36 -04:00
|
|
|
}
|