mirror of
https://github.com/Buriburizaem0n/nezha_domains.git
synced 2026-02-04 04:30:05 +00:00
ddns: retreive dns servers from context (#1034)
This commit is contained in:
@@ -109,7 +109,6 @@ func updateConfig(c *gin.Context) (any, error) {
|
||||
return nil, newGormError("%v", err)
|
||||
}
|
||||
|
||||
singleton.OnNameserverUpdate()
|
||||
singleton.OnUpdateLang(singleton.Conf.Language)
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/libdns/libdns"
|
||||
@@ -14,9 +13,10 @@ import (
|
||||
"github.com/nezhahq/nezha/pkg/utils"
|
||||
)
|
||||
|
||||
var (
|
||||
type DNSServerKey struct{}
|
||||
|
||||
const (
|
||||
dnsTimeOut = 10 * time.Second
|
||||
customDNSServers []string
|
||||
)
|
||||
|
||||
type Provider struct {
|
||||
@@ -31,12 +31,6 @@ type Provider struct {
|
||||
Setter libdns.RecordSetter
|
||||
}
|
||||
|
||||
func InitDNSServers(s string) {
|
||||
if s != "" {
|
||||
customDNSServers = strings.Split(s, ",")
|
||||
}
|
||||
}
|
||||
|
||||
func (provider *Provider) GetProfileID() uint64 {
|
||||
return provider.DDNSProfile.ID
|
||||
}
|
||||
@@ -58,7 +52,7 @@ func (provider *Provider) UpdateDomain(ctx context.Context, overrideDomains ...s
|
||||
|
||||
func (provider *Provider) updateDomain(domain string) error {
|
||||
var err error
|
||||
provider.prefix, provider.zone, err = splitDomainSOA(domain)
|
||||
provider.prefix, provider.zone, err = provider.splitDomainSOA(domain)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -96,13 +90,15 @@ func (provider *Provider) addDomainRecord() error {
|
||||
return err
|
||||
}
|
||||
|
||||
func splitDomainSOA(domain string) (prefix string, zone string, err error) {
|
||||
func (provider *Provider) splitDomainSOA(domain string) (prefix string, zone string, err error) {
|
||||
c := &dns.Client{Timeout: dnsTimeOut}
|
||||
|
||||
domain += "."
|
||||
indexes := dns.Split(domain)
|
||||
|
||||
servers := utils.DNSServers
|
||||
|
||||
customDNSServers, _ := provider.ctx.Value(DNSServerKey{}).([]string)
|
||||
if len(customDNSServers) > 0 {
|
||||
servers = customDNSServers
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package ddns
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
@@ -34,8 +35,9 @@ func TestSplitDomainSOA(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
provider := &Provider{ctx: context.WithValue(context.Background(), DNSServerKey{}, []string{"1.1.1.1:53"})}
|
||||
for _, c := range cases {
|
||||
prefix, zone, err := splitDomainSOA(c.domain)
|
||||
prefix, zone, err := provider.splitDomainSOA(c.domain)
|
||||
if err != nil {
|
||||
t.Fatalf("Error: %s", err)
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -13,6 +14,7 @@ import (
|
||||
"github.com/nezhahq/nezha/pkg/ddns"
|
||||
geoipx "github.com/nezhahq/nezha/pkg/geoip"
|
||||
"github.com/nezhahq/nezha/pkg/grpcx"
|
||||
"github.com/nezhahq/nezha/pkg/utils"
|
||||
|
||||
"github.com/nezhahq/nezha/model"
|
||||
pb "github.com/nezhahq/nezha/proto"
|
||||
@@ -236,12 +238,15 @@ func (s *NezhaHandler) ReportGeoIP(c context.Context, r *pb.GeoIP) (*pb.GeoIP, e
|
||||
ipv4 := geoip.IP.IPv4Addr
|
||||
ipv6 := geoip.IP.IPv6Addr
|
||||
|
||||
dnsServers := strings.Split(singleton.Conf.DNSServers, ",")
|
||||
ctx := context.WithValue(context.Background(), ddns.DNSServerKey{}, utils.IfOr(len(dnsServers) > 0, dnsServers, utils.DNSServers))
|
||||
|
||||
providers, err := singleton.DDNSShared.GetDDNSProvidersFromProfiles(server.DDNSProfiles, &model.IP{IPv4Addr: ipv4, IPv6Addr: ipv6})
|
||||
if err == nil {
|
||||
for _, provider := range providers {
|
||||
domains := server.OverrideDDNSDomains[provider.GetProfileID()]
|
||||
go func(provider *ddns.Provider) {
|
||||
provider.UpdateDomain(context.Background(), domains...)
|
||||
provider.UpdateDomain(ctx, domains...)
|
||||
}(provider)
|
||||
}
|
||||
} else {
|
||||
|
||||
@@ -34,8 +34,6 @@ func NewDDNSClass() *DDNSClass {
|
||||
sortedList: sortedList,
|
||||
},
|
||||
}
|
||||
|
||||
OnNameserverUpdate()
|
||||
return dc
|
||||
}
|
||||
|
||||
@@ -107,7 +105,3 @@ func (c *DDNSClass) sortList() {
|
||||
defer c.sortedListMu.Unlock()
|
||||
c.sortedList = sortedList
|
||||
}
|
||||
|
||||
func OnNameserverUpdate() {
|
||||
ddns2.InitDNSServers(Conf.DNSServers)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user