-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgo_aliddns.go
216 lines (180 loc) · 4.58 KB
/
go_aliddns.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
package main
import (
"fmt"
"github.com/aliyun/alibaba-cloud-sdk-go/services/alidns"
"github.com/robfig/cron"
"github.com/spf13/viper"
"io/ioutil"
"net/http"
"os"
"strings"
)
var (
DomainName,
RR,
Type,
Value,
AccessKeyId,
AccessSecret,
RecordId string
TTL int32
)
// 文件相关操作
// ------------------------------------------------
const ipFileName = "ip.txt"
const RecordIdFileName = "RecordId.txt"
// 判断文件是否存在
func CheckFileIsExist(filename string) bool {
if _, err := os.Stat(filename); os.IsNotExist(err) {
return false
}
return true
}
// 读取文件
func ReaderFile(filename string) string {
ip, err := ioutil.ReadFile(filename)
if err != nil {
return ""
}
return string(ip)
}
// 写入文件
func WriteFile(filename, str string) bool {
err := ioutil.WriteFile(filename, []byte(str), 0777)
if err != nil {
return false
}
return true
}
// ------------------------------------------------
// ip相关
// ------------------------------------------------
// 获取公网ip地址
func GetIPAddressByHttp() string {
resp, err := http.Get("http://members.3322.org/dyndns/getip")
if err != nil {
fmt.Println("获取公网ip失败!")
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("获取公网ip失败!")
}
ip := body[:len(body)-1] // 返回的数据最后有一个空格
return string(ip)
}
func CheckIPChange() bool {
ip := GetIPAddressByHttp()
Value = ip
if tmpIp := ReaderFile(ipFileName); ip != tmpIp { // 如果存在且地址变化了 重新写入
WriteFile(ipFileName, ip)
return true
} else {
return false
}
}
// ------------------------------------------------
// 阿里云相关操作
// ------------------------------------------------
// 检测解析记录是否生效
func ChecCheckDomainRecordk() bool {
client, err := alidns.NewClientWithAccessKey("cn-hangzhou", AccessKeyId, AccessSecret)
request := alidns.CreateCheckDomainRecordRequest()
request.DomainName = DomainName
request.Value = Value
request.Type = Type
request.RR = RR
response, err := client.CheckDomainRecord(request)
if err != nil {
fmt.Print("检测解析记录失败")
return false
}
if response.IsExist {
fmt.Println("解析记录已生效")
return true
} else {
fmt.Println("解析记录未生效")
return false
}
}
// 添加解析记录
func AddDomainRecord() {
client, err := alidns.NewClientWithAccessKey("cn-hangzhou", AccessKeyId, AccessSecret)
request := alidns.CreateAddDomainRecordRequest()
request.Value = Value
request.Type = Type
request.RR = RR
request.DomainName = DomainName
response, err := client.AddDomainRecord(request)
if err != nil {
fmt.Print(err.Error())
}
fmt.Println("新增记录成功")
WriteFile(RecordIdFileName, response.RecordId)
}
// 更新解析记录
func UpdateDomainRecord() {
RecordId = ReaderFile(RecordIdFileName)
client, err := alidns.NewClientWithAccessKey("cn-hangzhou", AccessKeyId, AccessSecret)
request := alidns.CreateUpdateDomainRecordRequest()
request.Value = Value
request.Type = Type
request.RR = RR
request.RecordId = RecordId
response, err := client.UpdateDomainRecord(request)
if err != nil {
fmt.Print(err.Error())
}
fmt.Println("更新记录成功")
WriteFile(RecordIdFileName, response.RecordId)
}
// ------------------------------------------------
// 定时任务
// ------------------------------------------------
func Time_task() {
fmt.Println("定时任务启动")
if CheckIPChange() { // ip改变了
if CheckFileIsExist(RecordIdFileName) && ReaderFile(RecordIdFileName) != "" { // 已有解析记录
UpdateDomainRecord()
} else {
AddDomainRecord()
}
}
}
// ------------------------------------------------
// 初始化相关操作
// ------------------------------------------------
func initViper() {
const CONFIG = "config"
viper.New()
viper.SetEnvPrefix(CONFIG)
viper.AutomaticEnv()
replacer := strings.NewReplacer(".", "_")
viper.SetEnvKeyReplacer(replacer)
viper.SetConfigName(CONFIG)
viper.AddConfigPath("./")
err := viper.ReadInConfig()
if err != nil {
fmt.Println(fmt.Errorf("Fatal error when reading %s config file:%s", CONFIG, err))
os.Exit(1)
}
}
func init() {
initViper()
DomainName = viper.GetString("DomainName")
RR = viper.GetString("RR")
Type = viper.GetString("Type")
AccessKeyId = viper.GetString("AccessKeyId")
AccessSecret = viper.GetString("AccessSecret")
TTL = viper.GetInt32("TTL")
}
// ------------------------------------------------
// main
func main() {
Time_task()
c := cron.New()
c.AddFunc("0 30 * * * *", Time_task)
c.Start()
fmt.Println("起飞")
select {}
}