mirror of
				https://github.com/TeaOSLab/EdgeAPI.git
				synced 2025-11-04 07:50:25 +08:00 
			
		
		
		
	DNS API支持查询多个同名记录/优化ACME申请
This commit is contained in:
		@@ -5,12 +5,18 @@ import (
 | 
				
			|||||||
	"github.com/TeaOSLab/EdgeAPI/internal/dnsclients/dnstypes"
 | 
						"github.com/TeaOSLab/EdgeAPI/internal/dnsclients/dnstypes"
 | 
				
			||||||
	"github.com/TeaOSLab/EdgeAPI/internal/errors"
 | 
						"github.com/TeaOSLab/EdgeAPI/internal/errors"
 | 
				
			||||||
	"github.com/go-acme/lego/v4/challenge/dns01"
 | 
						"github.com/go-acme/lego/v4/challenge/dns01"
 | 
				
			||||||
 | 
						"github.com/iwind/TeaGo/lists"
 | 
				
			||||||
 | 
						"os"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
 | 
						"sync"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type DNSProvider struct {
 | 
					type DNSProvider struct {
 | 
				
			||||||
	raw       dnsclients.ProviderInterface
 | 
						raw       dnsclients.ProviderInterface
 | 
				
			||||||
	dnsDomain string
 | 
						dnsDomain string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						locker             sync.Mutex
 | 
				
			||||||
 | 
						deletedRecordNames []string
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func NewDNSProvider(raw dnsclients.ProviderInterface, dnsDomain string) *DNSProvider {
 | 
					func NewDNSProvider(raw dnsclients.ProviderInterface, dnsDomain string) *DNSProvider {
 | 
				
			||||||
@@ -21,6 +27,7 @@ func NewDNSProvider(raw dnsclients.ProviderInterface, dnsDomain string) *DNSProv
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (this *DNSProvider) Present(domain, token, keyAuth string) error {
 | 
					func (this *DNSProvider) Present(domain, token, keyAuth string) error {
 | 
				
			||||||
 | 
						_ = os.Setenv("LEGO_DISABLE_CNAME_SUPPORT", "true")
 | 
				
			||||||
	fqdn, value := dns01.GetRecord(domain, keyAuth)
 | 
						fqdn, value := dns01.GetRecord(domain, keyAuth)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// 设置记录
 | 
						// 设置记录
 | 
				
			||||||
@@ -29,31 +36,38 @@ func (this *DNSProvider) Present(domain, token, keyAuth string) error {
 | 
				
			|||||||
		return errors.New("invalid fqdn value")
 | 
							return errors.New("invalid fqdn value")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	var recordName = fqdn[:index]
 | 
						var recordName = fqdn[:index]
 | 
				
			||||||
	record, err := this.raw.QueryRecord(this.dnsDomain, recordName, dnstypes.RecordTypeTXT)
 | 
					
 | 
				
			||||||
	if err != nil {
 | 
						// 先删除老的
 | 
				
			||||||
		return errors.New("query DNS record failed: " + err.Error())
 | 
						this.locker.Lock()
 | 
				
			||||||
 | 
						var wasDeleted = lists.ContainsString(this.deletedRecordNames, recordName)
 | 
				
			||||||
 | 
						this.locker.Unlock()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if !wasDeleted {
 | 
				
			||||||
 | 
							records, err := this.raw.QueryRecords(this.dnsDomain, recordName, dnstypes.RecordTypeTXT)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								return errors.New("query DNS record failed: " + err.Error())
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							for _, record := range records {
 | 
				
			||||||
 | 
								err = this.raw.DeleteRecord(this.dnsDomain, record)
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									return err
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							this.locker.Lock()
 | 
				
			||||||
 | 
							this.deletedRecordNames = append(this.deletedRecordNames, recordName)
 | 
				
			||||||
 | 
							this.locker.Unlock()
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if record == nil {
 | 
					
 | 
				
			||||||
		err = this.raw.AddRecord(this.dnsDomain, &dnstypes.Record{
 | 
						// 添加新的
 | 
				
			||||||
			Id:    "",
 | 
						err := this.raw.AddRecord(this.dnsDomain, &dnstypes.Record{
 | 
				
			||||||
			Name:  recordName,
 | 
							Id:    "",
 | 
				
			||||||
			Type:  dnstypes.RecordTypeTXT,
 | 
							Name:  recordName,
 | 
				
			||||||
			Value: value,
 | 
							Type:  dnstypes.RecordTypeTXT,
 | 
				
			||||||
			Route: this.raw.DefaultRoute(),
 | 
							Value: value,
 | 
				
			||||||
		})
 | 
							Route: this.raw.DefaultRoute(),
 | 
				
			||||||
		if err != nil {
 | 
						})
 | 
				
			||||||
			return errors.New("create DNS record failed: " + err.Error())
 | 
						if err != nil {
 | 
				
			||||||
		}
 | 
							return errors.New("create DNS record failed: " + err.Error())
 | 
				
			||||||
	} else {
 | 
					 | 
				
			||||||
		err = this.raw.UpdateRecord(this.dnsDomain, record, &dnstypes.Record{
 | 
					 | 
				
			||||||
			Name:  recordName,
 | 
					 | 
				
			||||||
			Type:  dnstypes.RecordTypeTXT,
 | 
					 | 
				
			||||||
			Value: value,
 | 
					 | 
				
			||||||
			Route: this.raw.DefaultRoute(),
 | 
					 | 
				
			||||||
		})
 | 
					 | 
				
			||||||
		if err != nil {
 | 
					 | 
				
			||||||
			return errors.New("update DNS record failed: " + err.Error())
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -72,7 +72,7 @@ func (this *DomainRecordsCache) WriteDomainRecords(providerId int64, domain stri
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// QueryDomainRecord 从缓存中读取域名记录
 | 
					// QueryDomainRecord 从缓存中读取单条域名记录
 | 
				
			||||||
func (this *DomainRecordsCache) QueryDomainRecord(providerId int64, domain string, recordName string, recordType string) (record *dnstypes.Record, hasRecords bool, ok bool) {
 | 
					func (this *DomainRecordsCache) QueryDomainRecord(providerId int64, domain string, recordName string, recordType string) (record *dnstypes.Record, hasRecords bool, ok bool) {
 | 
				
			||||||
	if providerId <= 0 || len(domain) == 0 {
 | 
						if providerId <= 0 || len(domain) == 0 {
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
@@ -117,6 +117,52 @@ func (this *DomainRecordsCache) QueryDomainRecord(providerId int64, domain strin
 | 
				
			|||||||
	return
 | 
						return
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// QueryDomainRecords 从缓存中读取多条域名记录
 | 
				
			||||||
 | 
					func (this *DomainRecordsCache) QueryDomainRecords(providerId int64, domain string, recordName string, recordType string) (records []*dnstypes.Record, hasRecords bool, ok bool) {
 | 
				
			||||||
 | 
						if providerId <= 0 || len(domain) == 0 {
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						domain = types.String(providerId) + "@" + domain
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						this.locker.Lock()
 | 
				
			||||||
 | 
						defer this.locker.Unlock()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// check version
 | 
				
			||||||
 | 
						var key = "DomainRecordsCache" + "@" + types.String(providerId) + "@" + domain
 | 
				
			||||||
 | 
						version, err := models.SharedSysLockerDAO.Read(nil, key)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							remotelogs.Error("dnsclients.BaseProvider", "ReadDomainRecordsCache: "+err.Error())
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// find list
 | 
				
			||||||
 | 
						list, recordsOk := this.domainRecordsMap[domain]
 | 
				
			||||||
 | 
						if !recordsOk {
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if version != list.version {
 | 
				
			||||||
 | 
							delete(this.domainRecordsMap, domain)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// check timestamp
 | 
				
			||||||
 | 
						if list.updatedAt < time.Now().Unix()-86400 /** 缓存有效期为一天 **/ {
 | 
				
			||||||
 | 
							delete(this.domainRecordsMap, domain)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						hasRecords = true
 | 
				
			||||||
 | 
						for _, r := range list.records {
 | 
				
			||||||
 | 
							if r.Name == recordName && r.Type == recordType {
 | 
				
			||||||
 | 
								records = append(records, r)
 | 
				
			||||||
 | 
								ok = true
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// DeleteDomainRecord 删除域名记录缓存
 | 
					// DeleteDomainRecord 删除域名记录缓存
 | 
				
			||||||
func (this *DomainRecordsCache) DeleteDomainRecord(providerId int64, domain string, recordId string) {
 | 
					func (this *DomainRecordsCache) DeleteDomainRecord(providerId int64, domain string, recordId string) {
 | 
				
			||||||
	if providerId <= 0 || len(domain) == 0 || len(recordId) == 0 {
 | 
						if providerId <= 0 || len(domain) == 0 || len(recordId) == 0 {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -0,0 +1,21 @@
 | 
				
			|||||||
 | 
					// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					package edgeapi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type FindNSRecordsWithNameAndTypeResponse struct {
 | 
				
			||||||
 | 
						BaseResponse
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						Data struct {
 | 
				
			||||||
 | 
							NSRecords []struct {
 | 
				
			||||||
 | 
								Id       int64  `json:"id"`
 | 
				
			||||||
 | 
								Name     string `json:"name"`
 | 
				
			||||||
 | 
								Type     string `json:"type"`
 | 
				
			||||||
 | 
								Value    string `json:"value"`
 | 
				
			||||||
 | 
								TTL      int32  `json:"ttl"`
 | 
				
			||||||
 | 
								NSRoutes []struct {
 | 
				
			||||||
 | 
									Name string `json:"name"`
 | 
				
			||||||
 | 
									Code string `json:"code"`
 | 
				
			||||||
 | 
								} `json:"nsRoutes"`
 | 
				
			||||||
 | 
							} `json:"nsRecords"`
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -154,7 +154,30 @@ func (this *AliDNSProvider) QueryRecord(domain string, name string, recordType d
 | 
				
			|||||||
			return record, nil
 | 
								return record, nil
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return nil, err
 | 
						return nil, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// QueryRecords 查询多个记录
 | 
				
			||||||
 | 
					func (this *AliDNSProvider) QueryRecords(domain string, name string, recordType dnstypes.RecordType) ([]*dnstypes.Record, error) {
 | 
				
			||||||
 | 
						// 从缓存中读取
 | 
				
			||||||
 | 
						if this.ProviderId > 0 {
 | 
				
			||||||
 | 
							records, hasRecords, _ := sharedDomainRecordsCache.QueryDomainRecords(this.ProviderId, domain, name, recordType)
 | 
				
			||||||
 | 
							if hasRecords { // 有效的搜索
 | 
				
			||||||
 | 
								return records, nil
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						records, err := this.GetRecords(domain)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						var result = []*dnstypes.Record{}
 | 
				
			||||||
 | 
						for _, record := range records {
 | 
				
			||||||
 | 
							if record.Name == name && record.Type == recordType {
 | 
				
			||||||
 | 
								result = append(result, record)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return result, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// AddRecord 设置记录
 | 
					// AddRecord 设置记录
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -53,6 +53,20 @@ func TestAliDNSProvider_QueryRecord(t *testing.T) {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestAliDNSProvider_QueryRecords(t *testing.T) {
 | 
				
			||||||
 | 
						provider, err := testAliDNSProvider()
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Fatal(err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							records, err := provider.QueryRecords("meloy.cn", "www", "A")
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								t.Fatal(err)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							t.Logf("%+v", records)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestAliDNSProvider_DeleteRecord(t *testing.T) {
 | 
					func TestAliDNSProvider_DeleteRecord(t *testing.T) {
 | 
				
			||||||
	provider, err := testAliDNSProvider()
 | 
						provider, err := testAliDNSProvider()
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -30,6 +30,9 @@ var cloudFlareHTTPClient = &http.Client{
 | 
				
			|||||||
		TLSClientConfig: &tls.Config{
 | 
							TLSClientConfig: &tls.Config{
 | 
				
			||||||
			InsecureSkipVerify: true,
 | 
								InsecureSkipVerify: true,
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
 | 
							/**Proxy: func(req *http.Request) (*url.URL, error) {
 | 
				
			||||||
 | 
								return url.Parse("socks5://127.0.0.1:7890")
 | 
				
			||||||
 | 
							},**/
 | 
				
			||||||
	},
 | 
						},
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -142,7 +145,7 @@ func (this *CloudFlareProvider) QueryRecord(domain string, name string, recordTy
 | 
				
			|||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	resp := new(cloudflare.GetDNSRecordsResponse)
 | 
						var resp = new(cloudflare.GetDNSRecordsResponse)
 | 
				
			||||||
	err = this.doAPI(http.MethodGet, "zones/"+zoneId+"/dns_records", map[string]string{
 | 
						err = this.doAPI(http.MethodGet, "zones/"+zoneId+"/dns_records", map[string]string{
 | 
				
			||||||
		"per_page": "100",
 | 
							"per_page": "100",
 | 
				
			||||||
		"name":     name + "." + domain,
 | 
							"name":     name + "." + domain,
 | 
				
			||||||
@@ -155,7 +158,7 @@ func (this *CloudFlareProvider) QueryRecord(domain string, name string, recordTy
 | 
				
			|||||||
		return nil, nil
 | 
							return nil, nil
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	record := resp.Result[0]
 | 
						var record = resp.Result[0]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// 修正Record
 | 
						// 修正Record
 | 
				
			||||||
	if record.Type == dnstypes.RecordTypeCNAME && !strings.HasSuffix(record.Content, ".") {
 | 
						if record.Type == dnstypes.RecordTypeCNAME && !strings.HasSuffix(record.Content, ".") {
 | 
				
			||||||
@@ -174,6 +177,46 @@ func (this *CloudFlareProvider) QueryRecord(domain string, name string, recordTy
 | 
				
			|||||||
	}, nil
 | 
						}, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// QueryRecords 查询多个记录
 | 
				
			||||||
 | 
					func (this *CloudFlareProvider) QueryRecords(domain string, name string, recordType dnstypes.RecordType) (records []*dnstypes.Record, err error) {
 | 
				
			||||||
 | 
						zoneId, err := this.findZoneIdWithDomain(domain)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						var resp = new(cloudflare.GetDNSRecordsResponse)
 | 
				
			||||||
 | 
						err = this.doAPI(http.MethodGet, "zones/"+zoneId+"/dns_records", map[string]string{
 | 
				
			||||||
 | 
							"per_page": "100",
 | 
				
			||||||
 | 
							"name":     name + "." + domain,
 | 
				
			||||||
 | 
							"type":     recordType,
 | 
				
			||||||
 | 
						}, nil, resp)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if len(resp.Result) == 0 {
 | 
				
			||||||
 | 
							return nil, nil
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for _, record := range resp.Result {
 | 
				
			||||||
 | 
							// 修正Record
 | 
				
			||||||
 | 
							if record.Type == dnstypes.RecordTypeCNAME && !strings.HasSuffix(record.Content, ".") {
 | 
				
			||||||
 | 
								record.Content += "."
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							record.Name = strings.TrimSuffix(record.Name, "."+domain)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							records = append(records, &dnstypes.Record{
 | 
				
			||||||
 | 
								Id:    record.Id,
 | 
				
			||||||
 | 
								Name:  record.Name,
 | 
				
			||||||
 | 
								Type:  record.Type,
 | 
				
			||||||
 | 
								Value: record.Content,
 | 
				
			||||||
 | 
								TTL:   types.Int32(record.Ttl),
 | 
				
			||||||
 | 
								Route: CloudFlareDefaultRoute,
 | 
				
			||||||
 | 
							})
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return records, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// AddRecord 设置记录
 | 
					// AddRecord 设置记录
 | 
				
			||||||
func (this *CloudFlareProvider) AddRecord(domain string, newRecord *dnstypes.Record) error {
 | 
					func (this *CloudFlareProvider) AddRecord(domain string, newRecord *dnstypes.Record) error {
 | 
				
			||||||
	zoneId, err := this.findZoneIdWithDomain(domain)
 | 
						zoneId, err := this.findZoneIdWithDomain(domain)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -86,6 +86,21 @@ func TestCloudFlareProvider_QueryRecord(t *testing.T) {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestCloudFlareProvider_QueryRecords(t *testing.T) {
 | 
				
			||||||
 | 
						provider, err := testCloudFlareProvider()
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Fatal(err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							t.Log("== www.meloy.cn/A ==")
 | 
				
			||||||
 | 
							records, err := provider.QueryRecords("meloy.cn", "www", dnstypes.RecordTypeA)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								t.Fatal(err)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							logs.PrintAsJSON(records, t)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestCloudFlareProvider_AddRecord(t *testing.T) {
 | 
					func TestCloudFlareProvider_AddRecord(t *testing.T) {
 | 
				
			||||||
	provider, err := testCloudFlareProvider()
 | 
						provider, err := testCloudFlareProvider()
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -103,7 +103,7 @@ func (this *CustomHTTPProvider) QueryRecord(domain string, name string, recordTy
 | 
				
			|||||||
	if len(resp) == 0 || string(resp) == "null" {
 | 
						if len(resp) == 0 || string(resp) == "null" {
 | 
				
			||||||
		return nil, nil
 | 
							return nil, nil
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	record := &dnstypes.Record{}
 | 
						var record = &dnstypes.Record{}
 | 
				
			||||||
	err = json.Unmarshal(resp, record)
 | 
						err = json.Unmarshal(resp, record)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
@@ -114,6 +114,28 @@ func (this *CustomHTTPProvider) QueryRecord(domain string, name string, recordTy
 | 
				
			|||||||
	return record, nil
 | 
						return record, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// QueryRecords 查询多个记录
 | 
				
			||||||
 | 
					func (this *CustomHTTPProvider) QueryRecords(domain string, name string, recordType dnstypes.RecordType) (result []*dnstypes.Record, err error) {
 | 
				
			||||||
 | 
						resp, err := this.post(maps.Map{
 | 
				
			||||||
 | 
							"action":     "QueryRecords",
 | 
				
			||||||
 | 
							"domain":     domain,
 | 
				
			||||||
 | 
							"name":       name,
 | 
				
			||||||
 | 
							"recordType": recordType,
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if len(resp) == 0 || string(resp) == "null" {
 | 
				
			||||||
 | 
							return nil, nil
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						result = []*dnstypes.Record{}
 | 
				
			||||||
 | 
						err = json.Unmarshal(resp, &result)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return result, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// AddRecord 设置记录
 | 
					// AddRecord 设置记录
 | 
				
			||||||
func (this *CustomHTTPProvider) AddRecord(domain string, newRecord *dnstypes.Record) error {
 | 
					func (this *CustomHTTPProvider) AddRecord(domain string, newRecord *dnstypes.Record) error {
 | 
				
			||||||
	_, err := this.post(maps.Map{
 | 
						_, err := this.post(maps.Map{
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -186,7 +186,30 @@ func (this *DNSPodProvider) QueryRecord(domain string, name string, recordType d
 | 
				
			|||||||
			return record, nil
 | 
								return record, nil
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return nil, err
 | 
						return nil, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// QueryRecords 查询多个记录
 | 
				
			||||||
 | 
					func (this *DNSPodProvider) QueryRecords(domain string, name string, recordType dnstypes.RecordType) ([]*dnstypes.Record, error) {
 | 
				
			||||||
 | 
						// 从缓存中读取
 | 
				
			||||||
 | 
						if this.ProviderId > 0 {
 | 
				
			||||||
 | 
							records, hasRecords, _ := sharedDomainRecordsCache.QueryDomainRecords(this.ProviderId, domain, name, recordType)
 | 
				
			||||||
 | 
							if hasRecords { // 有效的搜索
 | 
				
			||||||
 | 
								return records, nil
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						records, err := this.GetRecords(domain)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						var result = []*dnstypes.Record{}
 | 
				
			||||||
 | 
						for _, record := range records {
 | 
				
			||||||
 | 
							if record.Name == name && record.Type == recordType {
 | 
				
			||||||
 | 
								result = append(result, record)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return result, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// AddRecord 设置记录
 | 
					// AddRecord 设置记录
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -98,6 +98,21 @@ func TestDNSPodProvider_QueryRecord(t *testing.T) {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestDNSPodProvider_QueryRecords(t *testing.T) {
 | 
				
			||||||
 | 
						provider, _, err := testDNSPodProvider()
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Fatal(err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							records, err := provider.QueryRecords(DNSPodTestDomain, "hello-forward", dnstypes.RecordTypeCNAME)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								t.Fatal(err)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							logs.PrintAsJSON(records, t)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestDNSPodProvider_UpdateRecord(t *testing.T) {
 | 
					func TestDNSPodProvider_UpdateRecord(t *testing.T) {
 | 
				
			||||||
	provider, isInternational, err := testDNSPodProvider()
 | 
						provider, isInternational, err := testDNSPodProvider()
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -269,6 +269,54 @@ func (this *EdgeDNSAPIProvider) QueryRecord(domain string, name string, recordTy
 | 
				
			|||||||
	}, nil
 | 
						}, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// QueryRecords 查询多个记录
 | 
				
			||||||
 | 
					func (this *EdgeDNSAPIProvider) QueryRecords(domain string, name string, recordType dnstypes.RecordType) ([]*dnstypes.Record, error) {
 | 
				
			||||||
 | 
						var domainResp = &edgeapi.FindDomainWithNameResponse{}
 | 
				
			||||||
 | 
						err := this.doAPI("/NSDomainService/FindNSDomainWithName", map[string]any{
 | 
				
			||||||
 | 
							"name": domain,
 | 
				
			||||||
 | 
						}, domainResp)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						var domainId = domainResp.Data.NSDomain.Id
 | 
				
			||||||
 | 
						if domainId == 0 {
 | 
				
			||||||
 | 
							return nil, errors.New("can not find domain '" + domain + "'")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						var recordResp = &edgeapi.FindNSRecordsWithNameAndTypeResponse{}
 | 
				
			||||||
 | 
						err = this.doAPI("/NSRecordService/FindNSRecordsWithNameAndType", map[string]any{
 | 
				
			||||||
 | 
							"nsDomainId": domainId,
 | 
				
			||||||
 | 
							"name":       name,
 | 
				
			||||||
 | 
							"type":       recordType,
 | 
				
			||||||
 | 
						}, recordResp)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						var result = []*dnstypes.Record{}
 | 
				
			||||||
 | 
						for _, record := range recordResp.Data.NSRecords {
 | 
				
			||||||
 | 
							if record.Id <= 0 {
 | 
				
			||||||
 | 
								return nil, nil
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							var routeCode = this.DefaultRoute()
 | 
				
			||||||
 | 
							if len(record.NSRoutes) > 0 {
 | 
				
			||||||
 | 
								routeCode = record.NSRoutes[0].Code
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							result = append(result, &dnstypes.Record{
 | 
				
			||||||
 | 
								Id:    types.String(record.Id),
 | 
				
			||||||
 | 
								Name:  record.Name,
 | 
				
			||||||
 | 
								Type:  record.Type,
 | 
				
			||||||
 | 
								Value: record.Value,
 | 
				
			||||||
 | 
								Route: routeCode,
 | 
				
			||||||
 | 
								TTL:   record.TTL,
 | 
				
			||||||
 | 
							})
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return result, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// AddRecord 设置记录
 | 
					// AddRecord 设置记录
 | 
				
			||||||
func (this *EdgeDNSAPIProvider) AddRecord(domain string, newRecord *dnstypes.Record) error {
 | 
					func (this *EdgeDNSAPIProvider) AddRecord(domain string, newRecord *dnstypes.Record) error {
 | 
				
			||||||
	var domainResp = &edgeapi.FindDomainWithNameResponse{}
 | 
						var domainResp = &edgeapi.FindDomainWithNameResponse{}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -63,6 +63,18 @@ func TestEdgeDNSAPIProvider_QueryRecord(t *testing.T) {
 | 
				
			|||||||
	logs.PrintAsJSON(record)
 | 
						logs.PrintAsJSON(record)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestEdgeDNSAPIProvider_QueryRecords(t *testing.T) {
 | 
				
			||||||
 | 
						provider, err := testEdgeDNSAPIProvider()
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Fatal(err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						record, err := provider.QueryRecords(edgeDNSAPIDomainName, "cdn", dnstypes.RecordTypeA)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Fatal(err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						logs.PrintAsJSON(record)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestEdgeDNSAPIProvider_AddRecord(t *testing.T) {
 | 
					func TestEdgeDNSAPIProvider_AddRecord(t *testing.T) {
 | 
				
			||||||
	provider, err := testEdgeDNSAPIProvider()
 | 
						provider, err := testEdgeDNSAPIProvider()
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
@@ -153,8 +165,8 @@ func testEdgeDNSAPIProvider() (dnsclients.ProviderInterface, error) {
 | 
				
			|||||||
	err := provider.Auth(maps.Map{
 | 
						err := provider.Auth(maps.Map{
 | 
				
			||||||
		"role":            "user",
 | 
							"role":            "user",
 | 
				
			||||||
		"host":            "http://127.0.0.1:8004",
 | 
							"host":            "http://127.0.0.1:8004",
 | 
				
			||||||
		"accessKeyId":     "JOvsyXIFqkQbh5kl",
 | 
							"accessKeyId":     "zr9cmR42AEZxRyIV",
 | 
				
			||||||
		"accessKeySecret": "t0RY8YO3R58VbJJNp0RqKw9KWNpObwtE",
 | 
							"accessKeySecret": "2w5p5NSZZuplUPsfPMzM7dFmTrI7xyja",
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1328,6 +1328,41 @@ func (this *HuaweiDNSProvider) QueryRecord(domain string, name string, recordTyp
 | 
				
			|||||||
	}, nil
 | 
						}, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// QueryRecords 查询多个记录
 | 
				
			||||||
 | 
					func (this *HuaweiDNSProvider) QueryRecords(domain string, name string, recordType dnstypes.RecordType) ([]*dnstypes.Record, error) {
 | 
				
			||||||
 | 
						var resp = new(huaweidns.RecordSetsResponse)
 | 
				
			||||||
 | 
						err := this.doAPI(http.MethodGet, "/v2.1/recordsets", map[string]string{
 | 
				
			||||||
 | 
							"name": name + "." + domain + ".",
 | 
				
			||||||
 | 
							"type": recordType,
 | 
				
			||||||
 | 
						}, maps.Map{}, resp)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if len(resp.RecordSets) == 0 {
 | 
				
			||||||
 | 
							return nil, nil
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						var result = []*dnstypes.Record{}
 | 
				
			||||||
 | 
						for _, recordSet := range resp.RecordSets {
 | 
				
			||||||
 | 
							if len(recordSet.Records) == 0 {
 | 
				
			||||||
 | 
								continue
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							for _, record := range recordSet.Records {
 | 
				
			||||||
 | 
								result = append(result, &dnstypes.Record{
 | 
				
			||||||
 | 
									Id:    recordSet.Id + "@" + record,
 | 
				
			||||||
 | 
									Name:  name,
 | 
				
			||||||
 | 
									Type:  recordType,
 | 
				
			||||||
 | 
									Value: record,
 | 
				
			||||||
 | 
									Route: recordSet.Line,
 | 
				
			||||||
 | 
									TTL:   types.Int32(recordSet.Ttl),
 | 
				
			||||||
 | 
								})
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return result, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// AddRecord 设置记录
 | 
					// AddRecord 设置记录
 | 
				
			||||||
func (this *HuaweiDNSProvider) AddRecord(domain string, newRecord *dnstypes.Record) error {
 | 
					func (this *HuaweiDNSProvider) AddRecord(domain string, newRecord *dnstypes.Record) error {
 | 
				
			||||||
	zoneId, err := this.findZoneIdWithDomain(domain)
 | 
						zoneId, err := this.findZoneIdWithDomain(domain)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -57,7 +57,19 @@ func TestHuaweiDNSProvider_QueryRecord(t *testing.T) {
 | 
				
			|||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		t.Fatal(err)
 | 
							t.Fatal(err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	logs.PrintAsJSON(record)
 | 
						logs.PrintAsJSON(record, t)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestHuaweiDNSProvider_QueryRecords(t *testing.T) {
 | 
				
			||||||
 | 
						provider, err := testHuaweiDNSProvider()
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Fatal(err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						records, err := provider.QueryRecords("yun4s.cn", "abc", dnstypes.RecordTypeA)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Fatal(err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						logs.PrintAsJSON(records, t)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestHuaweiDNSProvider_AddRecord(t *testing.T) {
 | 
					func TestHuaweiDNSProvider_AddRecord(t *testing.T) {
 | 
				
			||||||
@@ -71,7 +83,7 @@ func TestHuaweiDNSProvider_AddRecord(t *testing.T) {
 | 
				
			|||||||
		Type:  "A",
 | 
							Type:  "A",
 | 
				
			||||||
		Value: "192.168.2.40",
 | 
							Value: "192.168.2.40",
 | 
				
			||||||
		Route: "Beijing",
 | 
							Route: "Beijing",
 | 
				
			||||||
		TTL: 120,
 | 
							TTL:   120,
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	err = provider.AddRecord("yun4s.cn", record)
 | 
						err = provider.AddRecord("yun4s.cn", record)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -22,6 +22,9 @@ type ProviderInterface interface {
 | 
				
			|||||||
	// QueryRecord 查询单个记录
 | 
						// QueryRecord 查询单个记录
 | 
				
			||||||
	QueryRecord(domain string, name string, recordType dnstypes.RecordType) (*dnstypes.Record, error)
 | 
						QueryRecord(domain string, name string, recordType dnstypes.RecordType) (*dnstypes.Record, error)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// QueryRecords 查询多个记录
 | 
				
			||||||
 | 
						QueryRecords(domain string, name string, recordType dnstypes.RecordType) ([]*dnstypes.Record, error)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// AddRecord 设置记录
 | 
						// AddRecord 设置记录
 | 
				
			||||||
	AddRecord(domain string, newRecord *dnstypes.Record) error
 | 
						AddRecord(domain string, newRecord *dnstypes.Record) error
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							
		Reference in New Issue
	
	Block a user