This repository has been archived by the owner on Mar 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathaliddns.js
272 lines (236 loc) · 8.89 KB
/
aliddns.js
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
272
'use strict'
const https = require('https')
const crypto = require('crypto')
const { URL } = require('url')
const config = {
rr: 'ddns',
domain: 'example.com',
accessKeyId: '',
accessKeySecret: '',
mode: 'both',
interval: 0,
alidnsAPI: 'https://alidns.aliyuncs.com/',
ip4Api: 'https://api.ipify.org/?format=json',
ip6Api: 'https://api6.ipify.org?format=json'
}
/**
* 原生 https.get 的简单包装
*
* @param {string} url
* @param {object} params 请求参数.
* @param {object} options NodeJS request 选项.
* @returns {Promise} 成功 resolve 响应正文,失败 reject 错误对象
*/
function get(url, params = {}, options = {}) {
url = new URL(url)
for (const [k, v] of Object.entries(params)) {
url.searchParams.append(k, v)
}
Object.assign(options, {
hostname: url.hostname,
path: url.pathname + url.search
})
return new Promise((resolve, reject) => {
const req = https.get(options, (resp) => {
if (resp.statusCode !== 200) {
reject(new Error('HTTP request error. HTTP status code: ' + resp.statusCode))
}
let data = ''
resp.on('data', (chunk) => {
data += chunk
})
resp.on('end', () => {
resolve(data)
})
})
req.on('error', (e) => {
reject(new Error('HTTP request error. Code: ' + e.code))
})
req.setTimeout(30 * 1000, () => {
req.abort()
})
})
}
const { getIp4, getIp6 } = (function () {
const ipv4RegExp = /^((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])$/
const ipv6RegExp = new RegExp([
'^([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|',
'([0-9a-fA-F]{1,4}:){1,7}:|',
'([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|',
'([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|',
'([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|',
'([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|',
'([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|',
'[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|',
':((:[0-9a-fA-F]{1,4}){1,7}|:)|',
'fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|',
'::(ffff(:0{1,4}){0,1}:){0,1}',
'((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}',
'(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|',
'([0-9a-fA-F]{1,4}:){1,4}:',
'((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}',
'(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])$'
].join(''))
async function getIp(family, ipApi, regexp) {
const request = await get(ipApi, undefined, { family: family })
try {
var data = JSON.parse(request).ip
if (typeof (data) !== 'string' || !regexp.test(data)) {
throw new Error()
}
} catch (e) {
throw new Error(`Get IP fail. Can't parse server respone.`)
}
return data
}
return {
/**
* 获取公网 IPv4 地址
* @returns 成功则 resolve IP 字符串,失败则 reject 错误对象
*/
getIp4: () => getIp(4, config.ip4Api, ipv4RegExp),
getIp6: () => getIp(6, config.ip6Api, ipv6RegExp)
}
})()
const { getRecord4, getRecord6, addRecord4, addRecord6, updateRecord4, updateRecord6 } = (function () {
// 将公共参数和签名附加到请求参数
function attachParam(specParam) {
function percentEncode(str) {
return encodeURIComponent(str).replace(/[!'()*]/g, c => '%' + c.charCodeAt(0).toString(16))
}
function makeSignature(parames) {
const magicPrefix = 'GET&%2F&'
const magicKeyPostfix = '&'
const canonicalizedQueryString = Object.keys(parames)
.sort()
.map(k => percentEncode(k) + '=' + percentEncode(parames[k]))
.join('&')
const data = magicPrefix + percentEncode(canonicalizedQueryString)
const key = config.accessKeySecret + magicKeyPostfix
return crypto.createHmac('sha1', key).update(data).digest('base64')
}
let parames = Object.assign({
'Format': 'JSON',
'Version': '2015-01-09',
'AccessKeyId': config.accessKeyId,
'SignatureMethod': 'HMAC-SHA1',
'SignatureVersion': '1.0',
'SignatureNonce': crypto.randomBytes(16).toString('hex'),
'Timestamp': (new Date()).toISOString(),
}, specParam)
return Object.assign(parames, { 'Signature': makeSignature(parames) })
}
// 获取解析记录
async function getRecord(type) {
const request = await get(config.alidnsAPI, attachParam({
'Action': 'DescribeSubDomainRecords',
'SubDomain': config.rr + '.' + config.domain,
'Type': type
}))
try {
var data = JSON.parse(request)
} catch (e) {
throw new Error(`Query record fail. Can't parse server respone.`)
}
if (data.TotalCount !== 1 && data.TotalCount !== 0) {
throw new Error(`Query record fail. ${data.Code ? 'Code: ' + data.Code : ''}`)
}
return data.TotalCount ? data.DomainRecords.Record[0] : null
}
// 修改(更新、新增)解析记录
async function changeRecord(ip, recordId, type) {
let parame = {
'RR': config.rr,
'Value': ip,
'Type': type,
'Action': recordId ? 'UpdateDomainRecord' : 'AddDomainRecord'
}
Object.assign(parame, recordId ? { 'RecordId': recordId } : { 'DomainName': config.domain })
let request = await get(config.alidnsAPI, attachParam(parame))
try {
var data = JSON.parse(request)
} catch (e) {
throw new Error(`Update record fail. Can't parse server respone.`)
}
if (!data.RecordId) {
throw new Error(`Update record fail. ${data.Code ? 'Code: ' + data.Code : ''}`)
}
return data
}
return {
/**
* 获取域名当前解析记录
* @returns 成功则 resolve 解析记录,失败则 reject 错误对象
*/
getRecord4: () => getRecord('A'),
getRecord6: () => getRecord('AAAA'),
/**
* 为域名添加一条解析记录
* @param ip 新的 IP 地址
* @returns 成功则 resolve 接口的响应数据,失败则 reject 错误对象
*/
addRecord4: (ip) => changeRecord(ip, 0, 'A'),
addRecord6: (ip) => changeRecord(ip, 0, 'AAAA'),
/**
* 更新域名的解析记录
* @param ip 新的 IP 地址
* @param recordId 旧的解析记录 ID
* @returns 成功则 resolve 接口的响应数据,失败则 reject 错误对象
*/
updateRecord4: (ip, recordId) => changeRecord(ip, recordId, 'A'),
updateRecord6: (ip, recordId) => changeRecord(ip, recordId, 'AAAA')
}
})()
async function start() {
let ip4, ip6
if (config.mode === 'both' || config.mode === 'ipv4') {
try {
ip4 = await getIp4()
} catch (e) {
console.log(`(Warning) Can't query IPv4 address, skip. Reason: ${e.message}`)
}
}
if (ip4) {
try {
const record = await getRecord4()
if (record && record.Value === ip4) {
console.log(`(No change) (IPv4) ${config.rr}.${config.domain} ${ip4}`)
} else if (record && record.Value !== ip4) {
await updateRecord4(ip4, record.RecordId)
console.log(`(Updated) (IPv4) ${config.rr}.${config.domain} ${record.Value} -> ${ip4}`)
} else {
await addRecord4(ip4)
console.log(`(Added) (IPv4) ${config.rr}.${config.domain} ${ip4}`)
}
} catch (e) {
console.log(`(Error) ${e.message}`)
}
}
if (config.mode === 'both' || config.mode === 'ipv6') {
try {
ip6 = await getIp6()
} catch (e) {
console.log(`(Warning) Can't query IPv6 address, skip. Reason: ${e.message}`)
}
}
if (ip6) {
try {
const record = await getRecord6()
if (record && record.Value === ip6) {
console.log(`(No change) (IPv6) ${config.rr}.${config.domain} ${ip6}`)
} else if (record && record.Value !== ip6) {
await updateRecord6(ip6, record.RecordId)
console.log(`(Updated) (IPv6) ${config.rr}.${config.domain} ${record.Value} -> ${ip6}`)
} else {
await addRecord6(ip6)
console.log(`(Added) (IPv6) ${config.rr}.${config.domain} ${ip6}`)
}
} catch (e) {
console.log(`(Error) ${e.message}`)
}
}
}
if (config.interval) {
setInterval(start, config.interval * 1000)
}
start()