mirror of
https://github.com/Buriburizaem0n/nezha_domains.git
synced 2026-02-05 21:20:06 +00:00
update ddns on server update (#1050)
This commit is contained in:
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user