ddns: retreive dns servers from context (#1034)

This commit is contained in:
UUBulb
2025-03-17 23:11:40 +08:00
committed by GitHub
parent c3ec52e392
commit 38c2374bad
5 changed files with 17 additions and 21 deletions

View File

@@ -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 (
dnsTimeOut = 10 * time.Second
customDNSServers []string
type DNSServerKey struct{}
const (
dnsTimeOut = 10 * time.Second
)
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
}

View File

@@ -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)
}