修复namesilo api接口升级错误
This commit is contained in:
@@ -6,9 +6,10 @@ import (
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// NameSilo API 响应结构
|
||||
// NameSiloAPIResponse 严格对应你提供的 JSON 返回
|
||||
type NameSiloAPIResponse struct {
|
||||
Request struct {
|
||||
Operation string `json:"operation"`
|
||||
@@ -18,33 +19,22 @@ type NameSiloAPIResponse struct {
|
||||
Code int `json:"code"`
|
||||
Detail string `json:"detail"`
|
||||
ResourceRecords []struct {
|
||||
RecordID string `json:"record_id"`
|
||||
Type string `json:"type"`
|
||||
Host string `json:"host"`
|
||||
Value string `json:"value"`
|
||||
TTL string `json:"ttl"`
|
||||
Distance json.RawMessage `json:"distance"` // 兼容数字和字符串
|
||||
RecordID string `json:"record_id"`
|
||||
Type string `json:"type"`
|
||||
Host string `json:"host"`
|
||||
Value string `json:"value"`
|
||||
TTL int `json:"ttl"` // JSON 中显示为数字
|
||||
Distance int `json:"distance"` // JSON 中显示为数字 0
|
||||
} `json:"resource_record"`
|
||||
} `json:"reply"`
|
||||
}
|
||||
|
||||
// 解析 distance,兼容数字和字符串
|
||||
func parseDistance(raw json.RawMessage) string {
|
||||
var str string
|
||||
if err := json.Unmarshal(raw, &str); err == nil {
|
||||
return str
|
||||
}
|
||||
var num int
|
||||
if err := json.Unmarshal(raw, &num); err == nil {
|
||||
return fmt.Sprintf("%d", num)
|
||||
}
|
||||
return "0"
|
||||
}
|
||||
|
||||
// RetrieveDNSRecords 使用 Namesilo API 获取指定域名的所有 DNS 记录
|
||||
func RetrieveDNSRecords(apiKey, domain string) (*NameSiloAPIResponse, error) {
|
||||
url := fmt.Sprintf("https://www.namesilo.com/api/dnsListRecords?version=1&type=json&key=%s&domain=%s", apiKey, domain)
|
||||
|
||||
// log.Println("Requesting URL:", url) // 调试用,如果需要隐藏 Key 请注释
|
||||
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -58,38 +48,51 @@ func RetrieveDNSRecords(apiKey, domain string) (*NameSiloAPIResponse, error) {
|
||||
|
||||
var apiResponse NameSiloAPIResponse
|
||||
if err := json.Unmarshal(body, &apiResponse); err != nil {
|
||||
log.Printf("JSON 解析错误: %v\n原始数据: %s\n", err, string(body))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &apiResponse, nil
|
||||
}
|
||||
|
||||
// FetchSubdomainRecord 获取指定子域名的 DNS 记录信息
|
||||
// FetchSubdomainRecord 获取指定子域名的 DNS 记录 ID
|
||||
// Subdomain 参数通常是完整域名,如 "v6.aixiao.me"
|
||||
func FetchSubdomainRecord(key string, domain string, Subdomain string) string {
|
||||
|
||||
// 获取 DNS 记录
|
||||
// 获取 DNS 记录列表
|
||||
response, err := RetrieveDNSRecords(key, domain)
|
||||
if err != nil {
|
||||
log.Println("获取 DNS 记录失败:", err)
|
||||
log.Println("获取 DNS 记录网络请求失败:", err)
|
||||
return "NULL"
|
||||
}
|
||||
|
||||
// 打印完整 JSON 数据
|
||||
_, err = json.MarshalIndent(response, "", " ")
|
||||
if err != nil {
|
||||
log.Println("JSON 格式化失败:", err)
|
||||
// 检查 API 返回码
|
||||
if response.Reply.Code != 300 {
|
||||
log.Printf("NameSilo API 错误: Code %d, Detail: %s\n", response.Reply.Code, response.Reply.Detail)
|
||||
return "NULL"
|
||||
}
|
||||
|
||||
// 只获取指定子域名的信息
|
||||
// 计算主机头 (Host Prefix)
|
||||
// 如果 Subdomain 是 "v6.aixiao.me",Domain 是 "aixiao.me"
|
||||
// 那么我们需要匹配的 Host 应该是 "v6"
|
||||
targetHost := Subdomain
|
||||
if strings.HasSuffix(Subdomain, "."+domain) {
|
||||
targetHost = strings.TrimSuffix(Subdomain, "."+domain)
|
||||
}
|
||||
|
||||
// 遍历查找
|
||||
for _, record := range response.Reply.ResourceRecords {
|
||||
if record.Host == Subdomain {
|
||||
log.Printf("RecordID: %s, Host: %s, Type: %s, Value: %s, TTL: %s, Distance: %s\n",
|
||||
record.RecordID, record.Host, record.Type, record.Value, record.TTL, parseDistance(record.Distance))
|
||||
|
||||
// 1. 匹配主机名 (Host)
|
||||
// 2. 匹配记录类型 (Type) 必须为 AAAA (IPv6)
|
||||
if record.Host == targetHost && record.Type == "AAAA" {
|
||||
log.Printf("匹配成功! RecordID: %s | Host: %s | Type: %s | Current Value: %s\n",
|
||||
record.RecordID, record.Host, record.Type, record.Value)
|
||||
return record.RecordID
|
||||
}
|
||||
}
|
||||
|
||||
log.Printf("未在 NameSilo 找到主机头为 [%s] 的 AAAA 记录。\n", targetHost)
|
||||
log.Println("请检查 NameSilo 后台是否已手动创建了该域名的 AAAA 记录。")
|
||||
|
||||
return "NULL"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user