2021-12-22 15:58:35 +00:00
|
|
|
package tencentcloud
|
|
|
|
|
|
|
|
import (
|
2022-07-17 12:40:24 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-12-22 15:58:35 +00:00
|
|
|
|
|
|
|
"github.com/go-acme/lego/v4/challenge/dns01"
|
|
|
|
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
|
2022-07-17 12:40:24 +00:00
|
|
|
errorsdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
|
2021-12-22 15:58:35 +00:00
|
|
|
dnspod "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dnspod/v20210323"
|
2022-07-17 12:40:24 +00:00
|
|
|
"golang.org/x/net/idna"
|
2021-12-22 15:58:35 +00:00
|
|
|
)
|
|
|
|
|
2022-07-17 12:40:24 +00:00
|
|
|
func (d *DNSProvider) getHostedZone(domain string) (*dnspod.DomainListItem, error) {
|
|
|
|
request := dnspod.NewDescribeDomainListRequest()
|
|
|
|
|
|
|
|
var domains []*dnspod.DomainListItem
|
|
|
|
|
|
|
|
for {
|
|
|
|
response, err := d.client.DescribeDomainList(request)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("API call failed: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
domains = append(domains, response.Response.DomainList...)
|
|
|
|
|
|
|
|
if uint64(len(domains)) >= *response.Response.DomainCountInfo.AllTotal {
|
|
|
|
break
|
|
|
|
}
|
2021-12-22 15:58:35 +00:00
|
|
|
|
2022-07-17 12:40:24 +00:00
|
|
|
request.Offset = common.Int64Ptr(int64(len(domains)))
|
|
|
|
}
|
|
|
|
|
2022-11-25 17:12:21 +00:00
|
|
|
authZone, err := dns01.FindZoneByFqdn(domain)
|
2021-12-22 15:58:35 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return nil, fmt.Errorf("could not find zone for FQDN %q : %w", domain, err)
|
2021-12-22 15:58:35 +00:00
|
|
|
}
|
|
|
|
|
2022-07-17 12:40:24 +00:00
|
|
|
var hostedZone *dnspod.DomainListItem
|
|
|
|
for _, zone := range domains {
|
2022-12-18 16:54:03 +00:00
|
|
|
unfqdn := dns01.UnFqdn(authZone)
|
|
|
|
if *zone.Name == unfqdn || *zone.Punycode == unfqdn {
|
2022-07-17 12:40:24 +00:00
|
|
|
hostedZone = zone
|
|
|
|
}
|
|
|
|
}
|
2021-12-22 15:58:35 +00:00
|
|
|
|
2022-07-17 12:40:24 +00:00
|
|
|
if hostedZone == nil {
|
|
|
|
return nil, fmt.Errorf("zone %s not found in dnspod for domain %s", authZone, domain)
|
2021-12-22 15:58:35 +00:00
|
|
|
}
|
|
|
|
|
2022-07-17 12:40:24 +00:00
|
|
|
return hostedZone, nil
|
2021-12-22 15:58:35 +00:00
|
|
|
}
|
|
|
|
|
2022-07-17 12:40:24 +00:00
|
|
|
func (d *DNSProvider) findTxtRecords(zone *dnspod.DomainListItem, fqdn string) ([]*dnspod.RecordListItem, error) {
|
|
|
|
recordName, err := extractRecordName(fqdn, *zone.Name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-12-22 15:58:35 +00:00
|
|
|
request := dnspod.NewDescribeRecordListRequest()
|
2022-07-17 12:40:24 +00:00
|
|
|
request.Domain = zone.Name
|
|
|
|
request.DomainId = zone.DomainId
|
|
|
|
request.Subdomain = common.StringPtr(recordName)
|
2021-12-22 15:58:35 +00:00
|
|
|
request.RecordType = common.StringPtr("TXT")
|
2022-07-17 12:40:24 +00:00
|
|
|
request.RecordLine = common.StringPtr("默认")
|
2021-12-22 15:58:35 +00:00
|
|
|
|
|
|
|
response, err := d.client.DescribeRecordList(request)
|
|
|
|
if err != nil {
|
2022-07-17 12:40:24 +00:00
|
|
|
var sdkError *errorsdk.TencentCloudSDKError
|
|
|
|
if errors.As(err, &sdkError) {
|
|
|
|
if sdkError.Code == dnspod.RESOURCENOTFOUND_NODATAOFRECORD {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}
|
2021-12-22 15:58:35 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return response.Response.RecordList, nil
|
|
|
|
}
|
|
|
|
|
2022-07-17 12:40:24 +00:00
|
|
|
func extractRecordName(fqdn, zone string) (string, error) {
|
|
|
|
asciiDomain, err := idna.ToASCII(zone)
|
2021-12-22 15:58:35 +00:00
|
|
|
if err != nil {
|
2022-07-17 12:40:24 +00:00
|
|
|
return "", fmt.Errorf("fail to convert punycode: %w", err)
|
2021-12-22 15:58:35 +00:00
|
|
|
}
|
|
|
|
|
2022-11-28 17:53:13 +00:00
|
|
|
subDomain, err := dns01.ExtractSubDomain(fqdn, asciiDomain)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2022-07-17 12:40:24 +00:00
|
|
|
}
|
2022-11-28 17:53:13 +00:00
|
|
|
|
|
|
|
return subDomain, nil
|
2021-12-22 15:58:35 +00:00
|
|
|
}
|