mirror of
https://github.com/Buriburizaem0n/nezha_domains.git
synced 2026-02-04 04:30:05 +00:00
chore: cleanup some code (#1069)
* chore * modernize loop * ddns: simpify Provider struct
This commit is contained in:
@@ -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"
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -8,21 +8,11 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
ErrGjsonNotFound = errors.New("specified path does not exist")
|
||||
ErrGjsonWrongType = errors.New("wrong type")
|
||||
)
|
||||
|
||||
var emptyIterator = func(yield func(string, string) bool) {}
|
||||
|
||||
func GjsonGet(json []byte, path string) (gjson.Result, error) {
|
||||
result := gjson.GetBytes(json, path)
|
||||
if !result.Exists() {
|
||||
return result, ErrGjsonNotFound
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func GjsonIter(json string) (iter.Seq2[string, string], error) {
|
||||
if json == "" {
|
||||
return emptyIterator, nil
|
||||
|
||||
Reference in New Issue
Block a user