chore: cleanup some code (#1069)

* chore

* modernize loop

* ddns: simpify Provider struct
This commit is contained in:
UUBulb
2025-04-26 18:28:21 +08:00
committed by GitHub
parent 65f728e5b1
commit 4871211f93
8 changed files with 12 additions and 38 deletions

View File

@@ -20,10 +20,8 @@ const (
)
type Provider struct {
ipAddr string
recordType string
prefix string
zone string
prefix string
zone string
DDNSProfile *model.DDNSProfile
IPAddrs *model.IP
@@ -36,7 +34,7 @@ func (provider *Provider) GetProfileID() uint64 {
func (provider *Provider) UpdateDomain(ctx context.Context, overrideDomains ...string) {
for _, domain := range utils.IfOr(len(overrideDomains) > 0, overrideDomains, provider.DDNSProfile.Domains) {
for retries := 0; retries < int(provider.DDNSProfile.MaxRetries); retries++ {
for retries := range int(provider.DDNSProfile.MaxRetries) {
log.Printf("NEZHA>> Updating DNS Record of domain %s: %d/%d", domain, retries+1, provider.DDNSProfile.MaxRetries)
if err := provider.updateDomain(ctx, domain); err != nil {
log.Printf("NEZHA>> Failed to update DNS record of domain %s: %v", domain, err)
@@ -57,17 +55,13 @@ func (provider *Provider) updateDomain(ctx context.Context, domain string) error
// 当IPv4和IPv6同时成功才算作成功
if *provider.DDNSProfile.EnableIPv4 {
provider.recordType = getRecordString(true)
provider.ipAddr = provider.IPAddrs.IPv4Addr
if err = provider.addDomainRecord(ctx); err != nil {
if err = provider.addDomainRecord(ctx, "A", provider.IPAddrs.IPv4Addr); err != nil {
return err
}
}
if *provider.DDNSProfile.EnableIPv6 {
provider.recordType = getRecordString(false)
provider.ipAddr = provider.IPAddrs.IPv6Addr
if err = provider.addDomainRecord(ctx); err != nil {
if err = provider.addDomainRecord(ctx, "AAAA", provider.IPAddrs.IPv6Addr); err != nil {
return err
}
}
@@ -75,13 +69,13 @@ func (provider *Provider) updateDomain(ctx context.Context, domain string) error
return nil
}
func (provider *Provider) addDomainRecord(ctx context.Context) error {
func (provider *Provider) addDomainRecord(ctx context.Context, recType, addr string) error {
_, err := provider.Setter.SetRecords(ctx, provider.zone,
[]libdns.Record{
{
Type: provider.recordType,
Type: recType,
Name: provider.prefix,
Value: provider.ipAddr,
Value: addr,
TTL: time.Minute,
},
})
@@ -123,10 +117,3 @@ func (provider *Provider) splitDomainSOA(ctx context.Context, domain string) (pr
return "", "", fmt.Errorf("SOA record not found for domain: %s", domain)
}
func getRecordString(isIpv4 bool) string {
if isIpv4 {
return "A"
}
return "AAAA"
}

View File

@@ -7,8 +7,7 @@ import (
)
// Internal use
type Provider struct {
}
type Provider struct{}
func (provider *Provider) SetRecords(ctx context.Context, zone string,
recs []libdns.Record) ([]libdns.Record, error) {