mirror of
https://github.com/Buriburizaem0n/nezha_domains.git
synced 2026-02-04 04:30:05 +00:00
update ddns on server update (#1050)
This commit is contained in:
@@ -65,7 +65,7 @@ func (c *DDNSClass) GetDDNSProvidersFromProfiles(profileId []uint64, ip *model.I
|
||||
profiles = append(profiles, profile)
|
||||
} else {
|
||||
c.listMu.RUnlock()
|
||||
return nil, fmt.Errorf("无法找到DDNS配置 ID %d", id)
|
||||
return nil, fmt.Errorf("cannot find DDNS profile %d", id)
|
||||
}
|
||||
}
|
||||
c.listMu.RUnlock()
|
||||
@@ -90,7 +90,7 @@ func (c *DDNSClass) GetDDNSProvidersFromProfiles(profileId []uint64, ip *model.I
|
||||
provider.Setter = &he.Provider{APIKey: profile.AccessSecret}
|
||||
providers = append(providers, provider)
|
||||
default:
|
||||
return nil, fmt.Errorf("无法找到配置的DDNS提供者 %s", profile.Provider)
|
||||
return nil, fmt.Errorf("cannot find DDNS provider %s", profile.Provider)
|
||||
}
|
||||
}
|
||||
return providers, nil
|
||||
|
||||
@@ -2,9 +2,13 @@ package singleton
|
||||
|
||||
import (
|
||||
"cmp"
|
||||
"context"
|
||||
"log"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"github.com/nezhahq/nezha/model"
|
||||
"github.com/nezhahq/nezha/pkg/ddns"
|
||||
"github.com/nezhahq/nezha/pkg/utils"
|
||||
)
|
||||
|
||||
@@ -14,14 +18,19 @@ type ServerClass struct {
|
||||
uuidToID map[string]uint64
|
||||
|
||||
sortedListForGuest []*model.Server
|
||||
|
||||
conf *ConfigClass
|
||||
dc *DDNSClass
|
||||
}
|
||||
|
||||
func NewServerClass() *ServerClass {
|
||||
func NewServerClass(conf *ConfigClass, dc *DDNSClass) *ServerClass {
|
||||
sc := &ServerClass{
|
||||
class: class[uint64, *model.Server]{
|
||||
list: make(map[uint64]*model.Server),
|
||||
},
|
||||
uuidToID: make(map[string]uint64),
|
||||
conf: conf,
|
||||
dc: dc,
|
||||
}
|
||||
|
||||
var servers []model.Server
|
||||
@@ -47,6 +56,12 @@ func (c *ServerClass) Update(s *model.Server, uuid string) {
|
||||
|
||||
c.listMu.Unlock()
|
||||
|
||||
if s.EnableDDNS {
|
||||
if err := c.UpdateDDNS(s, nil); err != nil {
|
||||
log.Printf("NEZHA>> Failed to update DDNS for server %d: %v", err, s.ID)
|
||||
}
|
||||
}
|
||||
|
||||
c.sortList()
|
||||
}
|
||||
|
||||
@@ -79,6 +94,25 @@ func (c *ServerClass) UUIDToID(uuid string) (id uint64, ok bool) {
|
||||
return
|
||||
}
|
||||
|
||||
func (c *ServerClass) UpdateDDNS(server *model.Server, ip *model.IP) error {
|
||||
confServers := strings.Split(c.conf.DNSServers, ",")
|
||||
ctx := context.WithValue(context.Background(), ddns.DNSServerKey{}, utils.IfOr(confServers[0] != "", confServers, utils.DNSServers))
|
||||
|
||||
providers, err := c.dc.GetDDNSProvidersFromProfiles(server.DDNSProfiles, utils.IfOr(ip != nil, ip, &server.GeoIP.IP))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, provider := range providers {
|
||||
domains := server.OverrideDDNSDomains[provider.GetProfileID()]
|
||||
go func(provider *ddns.Provider) {
|
||||
provider.UpdateDomain(ctx, domains...)
|
||||
}(provider)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *ServerClass) sortList() {
|
||||
c.listMu.RLock()
|
||||
defer c.listMu.RUnlock()
|
||||
|
||||
@@ -52,13 +52,13 @@ func InitTimezoneAndCache() error {
|
||||
|
||||
// LoadSingleton 加载子服务并执行
|
||||
func LoadSingleton(bus chan<- *model.Service) (err error) {
|
||||
initUser() // 加载用户ID绑定表
|
||||
initI18n() // 加载本地化服务
|
||||
NotificationShared = NewNotificationClass() // 加载通知服务
|
||||
ServerShared = NewServerClass() // 加载服务器列表
|
||||
CronShared = NewCronClass() // 加载定时任务
|
||||
initUser() // 加载用户ID绑定表
|
||||
initI18n() // 加载本地化服务
|
||||
NATShared = NewNATClass()
|
||||
DDNSShared = NewDDNSClass()
|
||||
NotificationShared = NewNotificationClass() // 加载通知服务
|
||||
ServerShared = NewServerClass(Conf, DDNSShared) // 加载服务器列表
|
||||
CronShared = NewCronClass() // 加载定时任务
|
||||
ServiceSentinelShared, err = NewServiceSentinel(bus, ServerShared, NotificationShared, CronShared)
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user