2024-02-24 08:10:27 -05:00
|
|
|
package singleton
|
|
|
|
|
|
|
|
import (
|
2024-12-08 07:21:35 -05:00
|
|
|
"cmp"
|
2024-02-24 08:10:27 -05:00
|
|
|
"fmt"
|
2024-10-23 22:21:59 -04:00
|
|
|
"slices"
|
2024-07-06 22:19:58 -04:00
|
|
|
|
2024-10-17 09:03:03 -04:00
|
|
|
"github.com/libdns/cloudflare"
|
2024-10-29 09:39:45 -04:00
|
|
|
tencentcloud "github.com/nezhahq/libdns-tencentcloud"
|
2024-10-17 09:03:03 -04:00
|
|
|
|
2024-11-28 06:38:54 -05:00
|
|
|
"github.com/nezhahq/nezha/model"
|
|
|
|
ddns2 "github.com/nezhahq/nezha/pkg/ddns"
|
|
|
|
"github.com/nezhahq/nezha/pkg/ddns/dummy"
|
|
|
|
"github.com/nezhahq/nezha/pkg/ddns/webhook"
|
2024-12-21 11:05:41 -05:00
|
|
|
"github.com/nezhahq/nezha/pkg/utils"
|
2024-02-24 08:10:27 -05:00
|
|
|
)
|
|
|
|
|
2025-02-21 10:08:12 -05:00
|
|
|
type DDNSClass struct {
|
|
|
|
class[uint64, *model.DDNSProfile]
|
2024-10-23 22:21:59 -04:00
|
|
|
}
|
|
|
|
|
2025-02-21 10:08:12 -05:00
|
|
|
func NewDDNSClass() *DDNSClass {
|
|
|
|
var sortedList []*model.DDNSProfile
|
2024-10-23 22:21:59 -04:00
|
|
|
|
2025-02-21 10:08:12 -05:00
|
|
|
DB.Find(&sortedList)
|
|
|
|
list := make(map[uint64]*model.DDNSProfile, len(sortedList))
|
|
|
|
for _, profile := range sortedList {
|
|
|
|
list[profile.ID] = profile
|
|
|
|
}
|
2024-10-23 22:21:59 -04:00
|
|
|
|
2025-02-21 10:08:12 -05:00
|
|
|
dc := &DDNSClass{
|
|
|
|
class: class[uint64, *model.DDNSProfile]{
|
|
|
|
list: list,
|
|
|
|
sortedList: sortedList,
|
|
|
|
},
|
2024-10-23 22:21:59 -04:00
|
|
|
}
|
|
|
|
|
2025-02-21 10:08:12 -05:00
|
|
|
OnNameserverUpdate()
|
|
|
|
return dc
|
|
|
|
}
|
2024-10-23 22:21:59 -04:00
|
|
|
|
2025-02-21 10:08:12 -05:00
|
|
|
func (c *DDNSClass) Update(p *model.DDNSProfile) {
|
|
|
|
c.listMu.Lock()
|
|
|
|
c.list[p.ID] = p
|
|
|
|
c.listMu.Unlock()
|
2024-12-05 08:00:02 -05:00
|
|
|
|
2025-02-21 10:08:12 -05:00
|
|
|
c.sortList()
|
2024-02-24 08:10:27 -05:00
|
|
|
}
|
2024-04-27 01:36:36 -04:00
|
|
|
|
2025-02-21 10:08:12 -05:00
|
|
|
func (c *DDNSClass) Delete(idList []uint64) {
|
|
|
|
c.listMu.Lock()
|
|
|
|
for _, id := range idList {
|
|
|
|
delete(c.list, id)
|
|
|
|
}
|
|
|
|
c.listMu.Unlock()
|
|
|
|
|
|
|
|
c.sortList()
|
2024-10-18 10:06:01 -04:00
|
|
|
}
|
|
|
|
|
2025-02-21 10:08:12 -05:00
|
|
|
func (c *DDNSClass) GetDDNSProvidersFromProfiles(profileId []uint64, ip *model.IP) ([]*ddns2.Provider, error) {
|
2024-10-17 09:03:03 -04:00
|
|
|
profiles := make([]*model.DDNSProfile, 0, len(profileId))
|
2025-02-21 10:08:12 -05:00
|
|
|
|
|
|
|
c.listMu.RLock()
|
2024-10-17 09:03:03 -04:00
|
|
|
for _, id := range profileId {
|
2025-02-21 10:08:12 -05:00
|
|
|
if profile, ok := c.list[id]; ok {
|
2024-10-17 09:03:03 -04:00
|
|
|
profiles = append(profiles, profile)
|
|
|
|
} else {
|
2025-02-21 10:08:12 -05:00
|
|
|
c.listMu.RUnlock()
|
2024-10-17 09:03:03 -04:00
|
|
|
return nil, fmt.Errorf("无法找到DDNS配置 ID %d", id)
|
|
|
|
}
|
2024-04-27 01:36:36 -04:00
|
|
|
}
|
2025-02-21 10:08:12 -05:00
|
|
|
c.listMu.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:
|
2024-10-21 12:04:17 -04:00
|
|
|
return nil, fmt.Errorf("无法找到配置的DDNS提供者 %s", 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
|
|
|
}
|
2025-02-21 10:08:12 -05:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|