diff --git a/routes/ipinfo.js b/routes/ipinfo.js index 02818ed..81b8ec1 100644 --- a/routes/ipinfo.js +++ b/routes/ipinfo.js @@ -3,6 +3,7 @@ const router = express.Router(); const libqqwry = require("lib-qqwry"); const dns = require("dns"); const qqwry = libqqwry(); //初始化IP库解析器 +const dnspod = require("../utils/dnspod"); /* GET home page. */ router.get("/", function (req, res) { @@ -10,17 +11,57 @@ router.get("/", function (req, res) { dns.lookup(ip, (err, address) => { let ipL; if (err) { - ipL = { "ip": ip, "msg": "域名解析IP失败" }; + ipL = { + "ip": ip, + "msg": "域名解析IP失败" + }; } else { ip = address; try { ipL = qqwry.searchIP(ip); //查询IP信息 } catch (e) { - ipL = { "ip": ip, "msg": e }; + ipL = { + "ip": ip, + "msg": e + }; } } res.json(ipL); }); }); +router.get("/ddns", async function (req, res) { + let ip = req.ip, + subdomain = req.query.subdomain; + if (ip.substr(0, 7) === "::ffff:") { + ip = ip.substr(7); + } + if (!subdomain || subdomain === "") { + res.json({ msg: "请提供subdomain参数" }); + return; + } + const record_type = (ip.indexOf(":") !== -1)? "AAAA": "A"; + + const records = await dnspod.get_record("home999.cc", subdomain, record_type); + if (!records || records.length !== 1) { + res.json({ msg: "获取不到相关记录,或者记录条数不是1。" }); + return; + } + const record = records[0]; + if (record.value === ip) { + res.json({ msg: "不需要更新IP" }); + } else { + record.value = ip; + const status = await dnspod.update_record("home999.cc",record); + if (status.code==="1") { + res.json({msg: "更新成功" ,ip}); + } else { + res.json({ + msg: "更新失败", + status + }); + } + } +}); + module.exports = router; diff --git a/schedule/cf2dns.js b/schedule/cf2dns.js index 22a9c57..f2c5348 100644 --- a/schedule/cf2dns.js +++ b/schedule/cf2dns.js @@ -1,5 +1,5 @@ const axios = require("axios"); -const dnspod = require("./dnspod"); +const dnspod = require("../utils/dnspod"); const KEY = "o1zrmHAF"; const DOMAINS = { diff --git a/schedule/dnspod.js b/utils/dnspod.js similarity index 85% rename from schedule/dnspod.js rename to utils/dnspod.js index c722a99..7a45ee2 100644 --- a/schedule/dnspod.js +++ b/utils/dnspod.js @@ -6,12 +6,13 @@ const instance = axios.create({ baseURL: "https://dnsapi.cn" }); -async function get_record(domain, subdomain) { +async function get_record(domain, subdomain, recordtype) { const api = "/Record.List"; const data = querystring.stringify({ "domain": domain, "sub_domain": subdomain, "login_token": token, + "record_type": recordtype, "format": "json" }); const res = await instance.post(api, data); @@ -32,7 +33,7 @@ async function update_record(domain, record) { "format": "json", }); const res = await instance.post(api, data); - return res.data; + return res.data.status; } async function add_record(domain, record) { @@ -67,14 +68,8 @@ async function del_record(domain, record) { module.exports = {get_record, update_record, add_record, del_record}; if (!module.parent) { - get_record("home999.cc", "gd").then(res => { + get_record("home999.cc", "n1","AAAA").then(res => { console.log(res); - for (const record of res) { - if (record.line !== "默认") { - del_record("home999.cc", record).then(res => { - console.log(res); - }); - } - } }); } +