forked from jeessy2/ddns-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnamecheap.go
140 lines (117 loc) · 3.29 KB
/
namecheap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package dns
import (
"io"
"log"
"net/http"
"strings"
"github.com/jeessy2/ddns-go/v5/config"
"github.com/jeessy2/ddns-go/v5/util"
)
const (
nameCheapEndpoint string = "https://dynamicdns.park-your-domain.com/update?host=#{host}&domain=#{domain}&password=#{password}&ip=#{ip}"
)
// NameCheap Domain
type NameCheap struct {
DNS config.DNS
Domains config.Domains
lastIpv4 string
lastIpv6 string
}
// NameCheap 修改域名解析结果
type NameCheapResp struct {
Status string
Errors []string
}
// Init 初始化
func (nc *NameCheap) Init(dnsConf *config.DnsConfig, ipv4cache *util.IpCache, ipv6cache *util.IpCache) {
nc.Domains.Ipv4Cache = ipv4cache
nc.Domains.Ipv6Cache = ipv6cache
nc.lastIpv4 = ipv4cache.Addr
nc.lastIpv6 = ipv6cache.Addr
nc.DNS = dnsConf.DNS
nc.Domains.GetNewIp(dnsConf)
}
// AddUpdateDomainRecords 添加或更新IPv4/IPv6记录
func (nc *NameCheap) AddUpdateDomainRecords() config.Domains {
nc.addUpdateDomainRecords("A")
nc.addUpdateDomainRecords("AAAA")
return nc.Domains
}
func (nc *NameCheap) addUpdateDomainRecords(recordType string) {
ipAddr, domains := nc.Domains.GetNewIpResult(recordType)
if ipAddr == "" {
return
}
// 防止多次发送Webhook通知
if recordType == "A" {
if nc.lastIpv4 == ipAddr {
log.Println("你的IPv4未变化, 未触发Namecheap请求")
return
}
} else {
// https://www.namecheap.com/support/knowledgebase/article.aspx/29/11/how-to-dynamically-update-the-hosts-ip-with-an-http-request/
log.Println("Namecheap DDNS 不支持更新 IPv6!")
return
// if nc.lastIpv6 == ipAddr {
// log.Println("你的IPv6未变化, 未触发Namecheap请求")
// return
// }
}
for _, domain := range domains {
nc.modify(domain, recordType, ipAddr)
}
}
// 修改
func (nc *NameCheap) modify(domain *config.Domain, recordType string, ipAddr string) {
var result NameCheapResp
err := nc.request(&result, ipAddr, domain)
if err != nil {
log.Printf("修改域名解析 %s 失败!", domain)
domain.UpdateStatus = config.UpdatedFailed
return
}
switch result.Status {
case "Success":
log.Printf("修改域名解析 %s 成功!IP: %s\n", domain, ipAddr)
domain.UpdateStatus = config.UpdatedSuccess
default:
log.Printf("修改域名解析 %s 失败!Status: %s\n", domain, result.Status)
domain.UpdateStatus = config.UpdatedFailed
}
}
// request 统一请求接口
func (nc *NameCheap) request(result *NameCheapResp, ipAddr string, domain *config.Domain) (err error) {
var url string = nameCheapEndpoint
url = strings.ReplaceAll(url, "#{host}", domain.GetSubDomain())
url = strings.ReplaceAll(url, "#{domain}", domain.DomainName)
url = strings.ReplaceAll(url, "#{password}", nc.DNS.Secret)
url = strings.ReplaceAll(url, "#{ip}", ipAddr)
req, err := http.NewRequest(
http.MethodGet,
url,
http.NoBody,
)
if err != nil {
log.Println("http.NewRequest失败. Error: ", err)
return
}
client := util.CreateHTTPClient()
resp, err := client.Do(req)
if err != nil {
log.Println("client.Do失败. Error: ", err)
return
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
log.Println("请求namecheap失败")
return err
}
status := string(data)
if strings.Contains(status, "<ErrCount>0</ErrCount>") {
result.Status = "Success"
} else {
result.Status = status
}
return
}