Created
July 26, 2016 13:12
-
-
Save 599316527/b6e23a9e78873903f2a76c5b896cc15a to your computer and use it in GitHub Desktop.
DnsPod 动态DNS 脚本
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @file ddns script for dnspod | |
* @author Kyle He | |
*/ | |
const exec = require('child_process').exec; | |
const FormData = require('form-data'); | |
const fetch = require('node-fetch'); | |
(new Promise(function (resolve, reject) { | |
// 写这个脚本主要是为了能方便地远程桌面到局域网内的另一台的 windows 上,所以这里是读取局域网ip | |
// 有其他需求的可以改写下这块,socket请求 ns1.dnspod.net:6666 得到 | |
// https://github.com/ohsc/dnspod-client/blob/master/src/dnspod.js#L58 | |
let cmdForGettingIp = 'netsh interface ip show address "无线网络连接" | findstr "IP 地址"' | |
exec(cmdForGettingIp, (error, stdout, stderr) => { | |
if (error) { | |
throw error | |
} | |
let ipAddr = stdout.trim().split(/\s+/).pop() | |
resolve(ipAddr) | |
}); | |
})).then(function (ipAddr) { | |
console.log('本机的局域网地址是:' + ipAddr) | |
if (!/^(\d{1,3}\.){3}\d{1,3}$/.test(ipAddr)) { | |
throw new Error('IP 地址不对') | |
} | |
let data = new FormData() | |
// login token: https://support.dnspod.cn/Kb/showarticle/tsid/227 | |
data.append('login_token', 'xxxx,xxxxxxxxxxxxxxxxxxxxx') | |
data.append('format', 'json') | |
data.append('lang', 'cn') | |
data.append('error_on_empty', 'no') | |
data.append('domain_id', 'xxxxxxx') | |
data.append('domain', 'xxxxxx.xx') | |
data.append('record_id', 'xxxxxxxxxx') | |
data.append('sub_domain', 'xxxxx') | |
data.append('record_type', 'A') | |
data.append('record_line', '默认') | |
data.append('value', ipAddr) | |
data.append('mx', '10') | |
data.append('ttl', '600') | |
data.append('status', 'enable') | |
console.log('请求中...') | |
return fetch('https://dnsapi.cn/Record.Modify', { | |
method: 'POST', | |
headers: { | |
'User-Agent': 'Node Dnspod Client/1.0.0' | |
}, | |
body: data | |
}) | |
}).then(function (response) { | |
if (response.ok) { | |
try { | |
return response.json() | |
} catch (err) {} | |
} | |
return response.text().then(function (text) { | |
console.log(text) | |
throw new Error('Network response was not ok.') | |
}) | |
}).then(function ({status, record}) { | |
if (status && status.code == 1) { | |
console.log(status.message) | |
console.log(record.id + ' ' + record.name + ' ' + record.value) | |
} else { | |
throw new Error(status.code + ' ' + status.message) | |
} | |
}).catch(function (err) { | |
console.log('出错了!') | |
console.log(err.stack) | |
process.exit(1) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment