73 lines
1.8 KiB
Go
73 lines
1.8 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
)
|
|
|
|
type Response struct {
|
|
Request struct {
|
|
Operation string `json:"operation"`
|
|
IP string `json:"ip"`
|
|
} `json:"request"`
|
|
Reply struct {
|
|
Code int `json:"code"`
|
|
Detail string `json:"detail"`
|
|
RecordID string `json:"record_id"`
|
|
} `json:"reply"`
|
|
}
|
|
|
|
// PerformDNSUpdate 使用 Namesilo API 更新指定域名的 DNS 记录
|
|
func PerformDNSUpdate(key string, domain string, rrid string, rrhost string, rrvalue string) (*Response, error) {
|
|
url := fmt.Sprintf("https://www.namesilo.com/api/dnsUpdateRecord?version=1&type=json&key=%s&domain=%s&rrid=%s&rrhost=%s&rrvalue=%s&rrttl=3600", key, domain, rrid, rrhost, rrvalue)
|
|
|
|
resp, err := http.Get(url)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var apiResponse Response
|
|
if err := json.Unmarshal(body, &apiResponse); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &apiResponse, nil
|
|
}
|
|
|
|
// ProcessDNSUpdateForDomain 处理指定域名的 DNS 记录更新,并返回操作状态码
|
|
func ProcessDNSUpdateForDomain(key string, Domain string, rrid string, rrhost string, rrvalue string) int {
|
|
// 更新DNS
|
|
response, err := PerformDNSUpdate(key, Domain, rrid, rrhost, rrvalue)
|
|
if err != nil {
|
|
log.Println("获取 DNS 记录失败:", err)
|
|
return 1
|
|
}
|
|
|
|
// 打印完整 JSON 数据
|
|
responseJson, err := json.MarshalIndent(response, "", " ")
|
|
if err != nil {
|
|
log.Println("JSON 格式化失败:", err)
|
|
return 1
|
|
}
|
|
fmt.Println(string(responseJson))
|
|
|
|
// 判断 Code 字段是否表示成功
|
|
if response.Reply.Code == 300 {
|
|
log.Println("DNS 记录更新成功!")
|
|
return 0
|
|
} else {
|
|
log.Println("DNS 记录更新失败,错误代码:", response.Reply.Code)
|
|
}
|
|
|
|
return 1
|
|
}
|