forked from OpenIoTHub/aliddns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
271 lines (261 loc) · 7.29 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
259
260
261
262
263
264
265
266
267
268
269
270
271
package main
import (
"fmt"
"github.com/OpenIoTHub/aliddns/config"
"github.com/OpenIoTHub/aliddns/utils"
"github.com/aliyun/alibaba-cloud-sdk-go/services/alidns"
"github.com/urfave/cli/v2"
"log"
"os"
"time"
)
var (
version = "dev"
commit = ""
date = ""
builtBy = ""
)
func main() {
myApp := cli.NewApp()
myApp.Name = "aliddns"
myApp.Usage = "-c [config file path]"
myApp.Version = buildVersion(version, commit, date, builtBy)
myApp.Commands = []*cli.Command{
{
Name: "init",
Aliases: []string{"i"},
Usage: "init config file",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "config",
Aliases: []string{"c"},
Value: config.ConfigFilePath,
Usage: "config file path",
EnvVars: []string{"ConfigFilePath"},
Destination: &config.ConfigFilePath,
},
},
Action: func(c *cli.Context) error {
config.LoadSnapcraftConfigPath()
config.InitConfigFile()
return nil
},
},
{
Name: "run",
Aliases: []string{"r"},
Usage: "run without config file",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "id",
Aliases: []string{"i"},
Value: config.ConfigModel.AccessId,
Usage: "aliyun AccessId",
EnvVars: []string{"AccessId"},
Destination: &config.ConfigModel.AccessId,
},
&cli.StringFlag{
Name: "key",
Aliases: []string{"k"},
Value: config.ConfigModel.AccessKey,
Usage: "aliyun AccessKey",
EnvVars: []string{"AccessKey"},
Destination: &config.ConfigModel.AccessKey,
},
&cli.StringFlag{
Name: "maindomain",
Aliases: []string{"m"},
Value: config.ConfigModel.MainDomain,
Usage: "aliyun MainDomain",
EnvVars: []string{"MainDomain"},
Destination: &config.ConfigModel.MainDomain,
},
&cli.StringFlag{
Name: "subdomain",
Aliases: []string{"s"},
Value: config.ConfigModel.SubDomainName,
Usage: "SubDomainName",
EnvVars: []string{"SubDomainName"},
Destination: &config.ConfigModel.SubDomainName,
},
&cli.IntFlag{
Name: "interval",
Aliases: []string{"c"},
Value: config.ConfigModel.CheckUpdateInterval,
Usage: "CheckUpdateInterval",
EnvVars: []string{"CheckUpdateInterval"},
Destination: &config.ConfigModel.CheckUpdateInterval,
},
&cli.StringFlag{
Name: "protocol",
Aliases: []string{"p"},
Value: config.ConfigModel.Protocol,
Usage: "Protocol",
EnvVars: []string{"Protocol"},
Destination: &config.ConfigModel.Protocol,
},
&cli.StringFlag{
Name: "ipv4ApiUrl",
Aliases: []string{"v4"},
Value: config.ConfigModel.Ipv4ApiUrl,
Usage: "Ipv4ApiUrl",
EnvVars: []string{"Ipv4ApiUrl"},
Destination: &config.ConfigModel.Ipv4ApiUrl,
},
&cli.StringFlag{
Name: "ipv6ApiUrl",
Aliases: []string{"v6"},
Value: config.ConfigModel.Ipv6ApiUrl,
Usage: "Ipv6ApiUrl",
EnvVars: []string{"Ipv6ApiUrl"},
Destination: &config.ConfigModel.Ipv6ApiUrl,
},
},
Action: func(c *cli.Context) error {
return timerFunction()
},
},
{
Name: "test",
Aliases: []string{"t"},
Usage: "test this command",
Action: func(c *cli.Context) error {
fmt.Println("ok")
return nil
},
},
}
myApp.Flags = []cli.Flag{
&cli.StringFlag{
Name: "config",
Aliases: []string{"c"},
Value: config.ConfigFilePath,
Usage: "config file path",
EnvVars: []string{"ConfigFilePath"},
Destination: &config.ConfigFilePath,
},
}
myApp.Action = func(c *cli.Context) error {
config.LoadSnapcraftConfigPath()
_, err := os.Stat(config.ConfigFilePath)
if err != nil {
config.InitConfigFile()
}
config.UseConfigFile()
return timerFunction()
}
err := myApp.Run(os.Args)
if err != nil {
log.Println(err.Error())
}
}
func update() {
// 获取 IP
protocol := config.ConfigModel.Protocol
publicIpv4 := ""
publicIpv6 := ""
if protocol == "ipv4" || protocol == "all" {
publicIpv4 = utils.GetMyPublicIpv4()
if publicIpv4 == "" {
log.Println("获取自己的IPV4地址失败!")
return
}
}
if protocol == "ipv6" || protocol == "all" {
publicIpv6 = utils.GetMyPublicIpv6()
if publicIpv6 == "" {
log.Println("获取自己的IPV6地址失败!你很可能没有ipv6地址\n" +
"Ipv6记录将不会被更新,如果你不希望再看见本警告可以在配置文件中将协议(protocol)由`all`改为`ipv4`或者命令执行时带上`-p ipv4`参数")
}
}
subDomains, err := utils.GetSubDomains(config.ConfigModel.MainDomain)
if err != nil {
log.Println(err)
return
}
// log.Printf("%+v", subDomains.DomainRecords.Record)
var ipv4Finded bool
var ipv6Finded bool
for _, sub := range subDomains.DomainRecords.Record {
if sub.RR == config.ConfigModel.SubDomainName {
if sub.Type == "A" { // V4
ipv4Finded = true
// 如果域名 IP 与 现在 IP 不一致
if sub.Value != publicIpv4 && publicIpv4 != "" {
log.Printf("ipv4 与服务器不一致,开始更新, %s->%s", sub.Value, publicIpv4)
// 更新域名绑定的 IP 地址。
sub.Value = publicIpv4
_ = utils.UpdateSubDomain(&sub)
}
} else if sub.Type == "AAAA" { // V6
ipv6Finded = true
// 如果域名 IP 与 现在 IP 不一致
if sub.Value != publicIpv6 && publicIpv6 != "" {
log.Printf("ipv6 与服务器不一致,开始更新, %s->%s", sub.Value, publicIpv6)
// 更新域名绑定的 IP 地址。
sub.Value = publicIpv6
_ = utils.UpdateSubDomain(&sub)
}
}
}
}
//{
// "RR": "tc1",
// "Line": "default",
// "Status": "ENABLE",
// "Locked": false,
// "Type": "AAAA",
// "DomainName": "iotserv.com",
// "Value": "240e:360:6801:4ba:1a8b:153a:fd16:5e2b",
// "RecordId": "20354963348952768",
// "TTL": 600,
// "Weight": 1
//}
// 如果没有相应记录,又想要更新,那就新建一个
if !ipv4Finded && (protocol == "ipv4" || protocol == "all") {
var sub = &alidns.Record{
DomainName: config.ConfigModel.MainDomain,
RR: config.ConfigModel.SubDomainName,
Type: "A",
Value: publicIpv4,
TTL: 600,
}
log.Println("未找到 IPv4 记录,现尝试创建一个")
_ = utils.AddSubDomainRecord(sub)
}
if !ipv6Finded && (protocol == "ipv6" || protocol == "all") {
var sub = &alidns.Record{
DomainName: config.ConfigModel.MainDomain,
RR: config.ConfigModel.SubDomainName,
Type: "AAAA",
Value: publicIpv6,
TTL: 600,
}
log.Println("未找到 IPv6 记录,现尝试创建一个")
_ = utils.AddSubDomainRecord(sub)
}
log.Println("<<<<<<<<<<<< 域名记录更新成功 >>>>>>>>>>>")
}
func timerFunction() error {
update()
tick := time.Tick(time.Second * time.Duration(config.ConfigModel.CheckUpdateInterval))
for {
select {
case <-tick:
update()
}
}
}
func buildVersion(version, commit, date, builtBy string) string {
var result = version
if commit != "" {
result = fmt.Sprintf("%s\ncommit: %s", result, commit)
}
if date != "" {
result = fmt.Sprintf("%s\nbuilt at: %s", result, date)
}
if builtBy != "" {
result = fmt.Sprintf("%s\nbuilt by: %s", result, builtBy)
}
return result
}