-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.go
258 lines (232 loc) · 6.56 KB
/
main.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package main
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
)
type appConfig struct {
dnspodId string
dnspodToken string
recordId string
domain string
subDomain string
internal int
email string
}
// 验证appConfig
func (c appConfig) Validate() (err error) {
if c.dnspodId == "" {
return errors.New("environment DNSPOD_ID required")
}
if c.dnspodToken == "" {
return errors.New("environment DNSPOD_TOKEN required")
}
if c.recordId == "" && c.subDomain == "" {
return errors.New("environment DNSPOD_RECORDID or DNSPOD_SUBDOMAIN required")
}
if c.domain == "" {
return errors.New("environment DNSPOD_DOMAIN required")
}
if c.internal < 5 {
return errors.New("environment DNSPOD_INTERNAL should range from 0 to 5")
}
return
}
var (
config = appConfig{
dnspodId: os.Getenv("DNSPOD_ID"),
dnspodToken: os.Getenv("DNSPOD_TOKEN"),
domain: os.Getenv("DNSPOD_DOMAIN"),
subDomain: os.Getenv("DNSPOD_SUBDOMAIN"),
internal: 60,
email: os.Getenv("DNSPOD_EMAIL"),
}
)
const (
ClientUserAgent = "DNSPOD-DDNS-CLIENT"
Version = "1.0.0"
StatusOk = "1"
)
func init() {
internal := os.Getenv("DNSPOD_INTERNAL")
config.internal, _ = strconv.Atoi(internal)
if config.internal < 5 {
config.internal = 60
}
if config.email == "" {
config.email = "[email protected]"
}
}
func main() {
var (
err error
lastPublicIP string
publicIP string
)
if err := config.Validate(); err != nil {
fmt.Println("[error]", err)
os.Exit(1)
}
fmt.Println("start")
for {
publicIP, err = GetPublicIP()
if err != nil {
fmt.Println(err.Error())
time.Sleep(time.Duration(config.internal) * time.Second)
continue
}
if config.recordId == "" || lastPublicIP == "" {
config.recordId, lastPublicIP, err = GetRecord()
if err != nil {
fmt.Println(err.Error())
time.Sleep(time.Duration(config.internal) * time.Second)
continue
}
}
if publicIP != lastPublicIP {
fmt.Println("发现公网IP变化,开始更新")
if err = UpdateRecord(config.recordId, publicIP); err != nil {
fmt.Println(err.Error())
time.Sleep(time.Duration(config.internal) * time.Second)
continue
}
fmt.Println("公网IP更新成功,新的公网IP:", publicIP)
lastPublicIP = publicIP
}
fmt.Println("下次更新时间:", time.Now().Add(time.Duration(config.internal)*time.Second).Format("2006-01-02 15:04:05"))
time.Sleep(time.Duration(config.internal) * time.Second)
}
}
// 公共返回参数
type CommonResponse struct {
Status struct {
Code string `json:"code"`
Message string `json:"message"`
CreateTime string `json:"created_at"`
} `json:"status"`
}
// 记录列表返回值
type RecordListResponse struct {
CommonResponse
Records []struct {
SubDomain string `json:"name"`
Id string `json:"id"`
PublicIP string `json:"value"`
} `json:"records"`
}
// 更新record记录
func UpdateRecord(recordId string, publicIP string) (err error) {
var (
request *http.Request
response *http.Response
c *http.Client
body = url.Values{}
responseData CommonResponse
)
body.Add("login_token", fmt.Sprintf("%s,%s", config.dnspodId, config.dnspodToken))
body.Add("format", "json")
body.Add("lang", "cn")
body.Add("error_on_empty", "no")
body.Add("domain", config.domain)
body.Add("sub_domain", config.subDomain)
body.Add("record_id", recordId)
body.Add("record_type", "A")
body.Add("record_line", "默认")
body.Add("value", publicIP)
request, err = http.NewRequest("POST", "https://dnsapi.cn/Record.Modify", strings.NewReader(body.Encode()))
if err != nil {
err = errors.New("request对象创建失败,err:" + err.Error())
return
}
request.Header.Add("User-Agent", fmt.Sprintf("%s/%s(%s)", ClientUserAgent, Version, config.email))
request.Header.Add("Content-Type", "application/x-www-form-urlencoded")
c = &http.Client{Timeout: time.Second * 30}
response, err = c.Do(request)
if err != nil {
err = errors.New("请求出错,err:" + err.Error())
return
}
if err = json.NewDecoder(response.Body).Decode(&responseData); err != nil {
err = errors.New("解析数据失败,err:" + err.Error())
return
}
defer response.Body.Close()
if responseData.Status.Code != StatusOk {
err = errors.New(fmt.Sprintf("更新失败,code:%s,message:%s", responseData.Status.Code, responseData.Status.Message))
return
}
return
}
// 获取recordid
func GetRecord() (recordId, IP string, err error) {
var (
request *http.Request
response *http.Response
c *http.Client
body = url.Values{}
responseData RecordListResponse
)
body.Add("login_token", fmt.Sprintf("%s,%s", config.dnspodId, config.dnspodToken))
body.Add("format", "json")
body.Add("lang", "cn")
body.Add("error_on_empty", "no")
body.Add("domain", config.domain)
body.Add("sub_domain", config.subDomain)
request, err = http.NewRequest("POST", "https://dnsapi.cn/Record.List", strings.NewReader(body.Encode()))
if err != nil {
err = errors.New("request对象创建失败,err:" + err.Error())
return
}
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
request.Header.Set("User-Agent", fmt.Sprintf("%s/%s(%s)", ClientUserAgent, Version, config.email))
c = &http.Client{Timeout: time.Second * 30}
response, err = c.Do(request)
if err != nil {
err = errors.New("请求出错,err:" + err.Error())
return
}
if err = json.NewDecoder(response.Body).Decode(&responseData); err != nil {
err = errors.New("解析数据失败,err:" + err.Error())
return
}
defer response.Body.Close()
if responseData.Status.Code != StatusOk {
err = errors.New(fmt.Sprintf("获取record失败,code:%s,message:%s", responseData.Status.Code, responseData.Status.Message))
return
}
for _, v := range responseData.Records {
if v.SubDomain == config.subDomain {
recordId = v.Id
IP = v.PublicIP
return
}
}
err = errors.New("没有找到相关记录,请先前往dnspod进行添加")
return
}
type getPublicIPResponse struct {
IP string `json:"origin"`
}
// 获取公网IP,如果出错,返回第二个参数
func GetPublicIP() (publicIP string, err error) {
var (
response *http.Response
responseData getPublicIPResponse
)
if response, err = http.Get("http://www.httpbin.org/ip"); err != nil {
err = errors.New("获取公网IP出错,err:" + err.Error())
return
}
if err = json.NewDecoder(response.Body).Decode(&responseData); err != nil {
err = errors.New("获取公网IP出错,err:" + err.Error())
return
}
defer response.Body.Close()
return responseData.IP, nil
}