-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
vercel.go
176 lines (155 loc) · 4.19 KB
/
vercel.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package dns
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
"github.com/jeessy2/ddns-go/v6/config"
"github.com/jeessy2/ddns-go/v6/util"
)
type Vercel struct {
DNS config.DNS
Domains config.Domains
TTL int
}
type ListExistingRecordsResponse struct {
Records []Record `json:"records"`
}
type Record struct {
ID string `json:"id"` // 记录ID
Slug string `json:"slug"`
Name string `json:"name"` // 记录名称
Type string `json:"type"` // 记录类型
Value string `json:"value"` // 记录值
Creator string `json:"creator"`
Created int64 `json:"created"`
Updated int64 `json:"updated"`
CreatedAt int64 `json:"createdAt"`
UpdatedAt int64 `json:"updatedAt"`
TTL int64 `json:"ttl"`
Comment *string `json:"comment,omitempty"`
}
func (v *Vercel) Init(dnsConf *config.DnsConfig, ipv4cache *util.IpCache, ipv6cache *util.IpCache) {
v.Domains.Ipv4Cache = ipv4cache
v.Domains.Ipv6Cache = ipv6cache
v.DNS = dnsConf.DNS
v.Domains.GetNewIp(dnsConf)
// Must be greater than 60
ttl, err := strconv.Atoi(dnsConf.TTL)
if err != nil {
ttl = 60
}
if ttl < 60 {
ttl = 60
}
v.TTL = ttl
}
func (v *Vercel) AddUpdateDomainRecords() (domains config.Domains) {
v.addUpdateDomainRecords("A")
v.addUpdateDomainRecords("AAAA")
return v.Domains
}
func (v *Vercel) addUpdateDomainRecords(recordType string) {
ipAddr, domains := v.Domains.GetNewIpResult(recordType)
if ipAddr == "" {
return
}
ipAddr = strings.ToLower(ipAddr)
var (
records []Record
err error
)
for _, domain := range domains {
records, err = v.listExistingRecords(domain)
if err != nil {
util.Log("查询域名信息发生异常! %s", err)
continue
}
var targetRecord *Record
for _, record := range records {
if record.Name == domain.SubDomain {
targetRecord = &record
break
}
}
if targetRecord == nil {
err = v.createRecord(domain, recordType, ipAddr)
} else {
if strings.ToLower(targetRecord.Value) == ipAddr {
util.Log("你的IP %s 没有变化, 域名 %s", ipAddr, domain)
domain.UpdateStatus = config.UpdatedNothing
continue
} else {
err = v.updateRecord(targetRecord, recordType, ipAddr)
}
}
operation := "新增"
if targetRecord != nil {
operation = "更新"
}
if err == nil {
util.Log(operation+"域名解析 %s 成功! IP: %s", domain, ipAddr)
domain.UpdateStatus = config.UpdatedSuccess
} else {
util.Log(operation+"域名解析 %s 失败! 异常信息: %s", domain, err)
domain.UpdateStatus = config.UpdatedFailed
}
}
}
func (v *Vercel) listExistingRecords(domain *config.Domain) (records []Record, err error) {
var result ListExistingRecordsResponse
err = v.request(http.MethodGet, "https://api.vercel.com/v4/domains/"+domain.DomainName+"/records", nil, &result)
if err != nil {
return
}
records = result.Records
return
}
func (v *Vercel) createRecord(domain *config.Domain, recordType string, recordValue string) (err error) {
err = v.request(http.MethodPost, "https://api.vercel.com/v2/domains/"+domain.DomainName+"/records", map[string]interface{}{
"name": domain.SubDomain,
"type": recordType,
"value": recordValue,
"ttl": v.TTL,
"comment": "Created by ddns-go",
}, nil)
return
}
func (v *Vercel) updateRecord(record *Record, recordType string, recordValue string) (err error) {
err = v.request(http.MethodPatch, "https://api.vercel.com/v1/domains/records/"+record.ID, map[string]interface{}{
"type": recordType,
"value": recordValue,
"ttl": v.TTL,
}, nil)
return
}
func (v *Vercel) request(method, api string, data, result interface{}) (err error) {
var payload []byte
if data != nil {
payload, _ = json.Marshal(data)
}
req, err := http.NewRequest(
method,
api,
bytes.NewBuffer(payload),
)
if err != nil {
return
}
req.Header.Set("Authorization", "Bearer "+v.DNS.Secret)
req.Header.Set("Content-Type", "application/json")
client := util.CreateHTTPClient()
resp, err := client.Do(req)
if err != nil {
return err
}
if resp.StatusCode != 200 {
return fmt.Errorf("Vercel API returned status code %d", resp.StatusCode)
}
if result != nil {
err = util.GetHTTPResponse(resp, err, result)
}
return
}