175 lines
4.6 KiB
Go
175 lines
4.6 KiB
Go
package dnsprovider
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
"net/url"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type Zone struct {
|
|
ID string `json:"id"`
|
|
Name string `json:"name"`
|
|
}
|
|
|
|
func DiscoverZones(ctx context.Context, name string, c Credential, hint string) ([]Zone, error) {
|
|
if name == "cloudflare" && c.ZoneID == "" {
|
|
c.ZoneID = "discovery"
|
|
}
|
|
provider, err := New(name, hint, c)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
ctx, cancel := context.WithTimeout(ctx, 40*time.Second)
|
|
defer cancel()
|
|
all := []Zone{}
|
|
for page := 1; page <= 20; page++ {
|
|
total := 0
|
|
switch p := provider.(type) {
|
|
case *cloudflare:
|
|
var out struct {
|
|
Success bool `json:"success"`
|
|
Result []struct {
|
|
ID string `json:"id"`
|
|
Name string `json:"name"`
|
|
} `json:"result"`
|
|
Info struct {
|
|
TotalCount int `json:"total_count"`
|
|
} `json:"result_info"`
|
|
}
|
|
req, e := http.NewRequestWithContext(ctx, "GET", "https://api.cloudflare.com/client/v4/zones?per_page=100&page="+strconv.Itoa(page), nil)
|
|
if e != nil {
|
|
return nil, e
|
|
}
|
|
req.Header.Set("Authorization", "Bearer "+c.APIToken)
|
|
if e = doJSON(p.client, req, &out); e != nil {
|
|
return nil, e
|
|
}
|
|
if !out.Success {
|
|
return nil, errors.New("Cloudflare 未授权读取 DNS 区域")
|
|
}
|
|
for _, z := range out.Result {
|
|
all = append(all, Zone{z.ID, z.Name})
|
|
}
|
|
total = out.Info.TotalCount
|
|
case *aliyun:
|
|
var out struct {
|
|
Code string `json:"Code"`
|
|
TotalCount int `json:"TotalCount"`
|
|
Domains struct {
|
|
Domain []struct {
|
|
Name string `json:"DomainName"`
|
|
} `json:"Domain"`
|
|
} `json:"Domains"`
|
|
}
|
|
if e := p.call(ctx, "DescribeDomains", url.Values{"PageNumber": {strconv.Itoa(page)}, "PageSize": {"100"}}, &out); e != nil {
|
|
return nil, e
|
|
}
|
|
if out.Code != "" {
|
|
return nil, errors.New("阿里云未授权读取域名列表")
|
|
}
|
|
for _, z := range out.Domains.Domain {
|
|
all = append(all, Zone{z.Name, z.Name})
|
|
}
|
|
total = out.TotalCount
|
|
case *tencent:
|
|
var out struct {
|
|
Response struct {
|
|
Error *providerError `json:"Error"`
|
|
Count int `json:"DomainCountInfoAll"`
|
|
DomainCount struct {
|
|
All int `json:"AllTotal"`
|
|
} `json:"DomainCountInfo"`
|
|
Domains []struct {
|
|
ID uint64 `json:"DomainId"`
|
|
Name string `json:"Name"`
|
|
} `json:"DomainList"`
|
|
} `json:"Response"`
|
|
}
|
|
if e := p.call(ctx, "DescribeDomainList", map[string]any{"Offset": (page - 1) * 100, "Limit": 100}, &out); e != nil {
|
|
return nil, e
|
|
}
|
|
if out.Response.Error != nil {
|
|
return nil, out.Response.Error
|
|
}
|
|
for _, z := range out.Response.Domains {
|
|
all = append(all, Zone{strconv.FormatUint(z.ID, 10), z.Name})
|
|
}
|
|
total = out.Response.DomainCount.All
|
|
case *west:
|
|
if c.DomainKey != "" {
|
|
if hint == "" {
|
|
return nil, errors.New("单域名密钥请先填写指定域名")
|
|
}
|
|
if _, e := p.modernRecords(ctx, hint, "TXT"); e != nil {
|
|
return nil, e
|
|
}
|
|
return []Zone{{hint, hint}}, nil
|
|
}
|
|
var out struct {
|
|
Total int `json:"total"`
|
|
Items []struct {
|
|
Domain string `json:"domain"`
|
|
} `json:"items"`
|
|
}
|
|
if e := p.call(ctx, "getdomains", url.Values{"page": {strconv.Itoa(page)}, "limit": {"100"}}, &out); e != nil {
|
|
return nil, e
|
|
}
|
|
for _, z := range out.Items {
|
|
all = append(all, Zone{z.Domain, z.Domain})
|
|
}
|
|
total = out.Total
|
|
case *dnsmgr:
|
|
var out struct {
|
|
Total *int `json:"total"`
|
|
Rows []struct {
|
|
ID jsonID `json:"id"`
|
|
Name string `json:"name"`
|
|
} `json:"rows"`
|
|
}
|
|
if e := p.call(ctx, "domain", url.Values{"offset": {strconv.Itoa((page - 1) * 100)}, "limit": {"100"}}, &out); e != nil {
|
|
return nil, e
|
|
}
|
|
if out.Total == nil {
|
|
return nil, errors.New("彩虹 DNS 未返回域名列表")
|
|
}
|
|
for _, z := range out.Rows {
|
|
all = append(all, Zone{string(z.ID), z.Name})
|
|
}
|
|
total = *out.Total
|
|
default:
|
|
return nil, errors.New("服务商不支持读取区域列表")
|
|
}
|
|
if len(all) >= total {
|
|
return normalizeZones(all), nil
|
|
}
|
|
}
|
|
return nil, errors.New("域名数量超过 2000,请使用权限更小的 API 票据")
|
|
}
|
|
|
|
type jsonID string
|
|
|
|
func (v *jsonID) UnmarshalJSON(raw []byte) error {
|
|
id := rawID(raw)
|
|
if !positiveID(id) {
|
|
return errors.New("域名编号无效")
|
|
}
|
|
*v = jsonID(id)
|
|
return nil
|
|
}
|
|
func normalizeZones(items []Zone) []Zone {
|
|
out := []Zone{}
|
|
seen := map[string]bool{}
|
|
for _, z := range items {
|
|
z.Name = strings.ToLower(strings.TrimSuffix(z.Name, "."))
|
|
if z.ID != "" && z.Name != "" && !seen[z.Name] {
|
|
out = append(out, z)
|
|
seen[z.Name] = true
|
|
}
|
|
}
|
|
return out
|
|
}
|