diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 0000000..7e8b8c4 --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,31 @@ + + + + \ No newline at end of file diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..79ee123 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..03d9549 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/jsLinters/eslint.xml b/.idea/jsLinters/eslint.xml new file mode 100644 index 0000000..541945b --- /dev/null +++ b/.idea/jsLinters/eslint.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/app.js b/app.js index 48b3007..b270c99 100644 --- a/app.js +++ b/app.js @@ -12,14 +12,6 @@ const danmakuRouter = require("./routes/danmaku"); const ipinfoRouter = require("./routes/ipinfo"); const airportsubRouter = require("./routes/airportsub"); const DEBUG = process.env.DEBUG==="true" || false; -if (!DEBUG) { - console.log("PRODUCTION MODE!该模式下TG机器人与定时任务正常运行"); - // 引入定时任务模块 - require("./schedule/schedule"); - // 引入TG机器人 - require("./tgbot/bot"); -} else - console.log("DEBUG MODE!该模式下将关闭TG机器人和定时任务"); const app = express(); @@ -53,5 +45,15 @@ app.use(function (err, req, res) { res.status(err.status || 500); res.render("error"); }); +// 引入定时任务模块 +const schedule = require("./schedule/schedule"); +schedule(app); + +if (!DEBUG) { + console.log("PRODUCTION MODE!该模式下TG机器人正常运行"); + // 引入TG机器人 + require("./tgbot/bot"); +} else + console.log("DEBUG MODE!该模式下将关闭TG机器人"); module.exports = app; diff --git a/package-lock.json b/package-lock.json index b9ba983..a9dda6b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -29,6 +29,8 @@ "node-cron": "^3.0.2", "node-telegram-bot-api": "^0.60.0", "pako": "^1.0.11", + "querystring": "^0.2.1", + "tencentcloud-sdk-nodejs": "^4.0.480", "whacko": "^0.19.1", "xml-js": "^1.6.11", "xml2js": "^0.4.23" @@ -4428,7 +4430,6 @@ "version": "2.0.1", "resolved": "https://registry.npmjs.org/is-stream/-/is-stream-2.0.1.tgz", "integrity": "sha512-hFoiJiTl63nn+kstHGBtewWSKnQLpyb155KHheA1l39uvtO9nWIop1p3udqPcUd/xbF1VLMO4n7OI6p7RbngDg==", - "dev": true, "engines": { "node": ">=8" }, @@ -5536,6 +5537,25 @@ "node": ">=6.0.0" } }, + "node_modules/node-fetch": { + "version": "2.6.7", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.7.tgz", + "integrity": "sha512-ZjMPFEfVx5j+y2yF35Kzx5sF7kDzxuDj6ziH4FFbOp87zKDZNx8yExJIb05OGF4Nlt9IHFIMBkRl41VdvcNdbQ==", + "dependencies": { + "whatwg-url": "^5.0.0" + }, + "engines": { + "node": "4.x || >=6.0.0" + }, + "peerDependencies": { + "encoding": "^0.1.0" + }, + "peerDependenciesMeta": { + "encoding": { + "optional": true + } + } + }, "node_modules/node-preload": { "version": "0.2.1", "resolved": "https://registry.npmjs.org/node-preload/-/node-preload-0.2.1.tgz", @@ -6403,6 +6423,15 @@ "node": ">=0.6" } }, + "node_modules/querystring": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/querystring/-/querystring-0.2.1.tgz", + "integrity": "sha512-wkvS7mL/JMugcup3/rMitHmd9ecIGd2lhFhK9N3UUQ450h66d1r3Y9nvXzQAW1Lq+wyx61k/1pfKS5KuKiyEbg==", + "deprecated": "The querystring API is considered Legacy. new code should use the URLSearchParams API instead.", + "engines": { + "node": ">=0.4.x" + } + }, "node_modules/queue-microtask": { "version": "1.2.3", "resolved": "https://registry.npmjs.org/queue-microtask/-/queue-microtask-1.2.3.tgz", @@ -7281,6 +7310,51 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/tencentcloud-sdk-nodejs": { + "version": "4.0.480", + "resolved": "https://registry.npmjs.org/tencentcloud-sdk-nodejs/-/tencentcloud-sdk-nodejs-4.0.480.tgz", + "integrity": "sha512-EXFK9u3axlNLqNOe/eNMpvC0Ajv20NBDuJ8p3Qe74ecORcBzvwWZLGIcc/6TFNtiGFYGyEj1ioBYszLOzAAvWg==", + "dependencies": { + "form-data": "^3.0.0", + "get-stream": "^6.0.0", + "https-proxy-agent": "^5.0.0", + "is-stream": "^2.0.0", + "node-fetch": "^2.2.0", + "tslib": "1.13.0" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/tencentcloud-sdk-nodejs/node_modules/form-data": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-3.0.1.tgz", + "integrity": "sha512-RHkBKtLWUVwd7SqRIvCZMEvAMoGUp0XU+seQiZejj0COz3RI3hWP4sCv3gZWWLjJTd7rGwcsF5eKZGii0r/hbg==", + "dependencies": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + }, + "engines": { + "node": ">= 6" + } + }, + "node_modules/tencentcloud-sdk-nodejs/node_modules/get-stream": { + "version": "6.0.1", + "resolved": "https://registry.npmjs.org/get-stream/-/get-stream-6.0.1.tgz", + "integrity": "sha512-ts6Wi+2j3jQjqi70w5AlN8DFnkSwC+MqmxEzdEALB2qXZYV3X/b1CTfgPLGJNMeAWxdPfU8FO1ms3NUfaHCPYg==", + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/tencentcloud-sdk-nodejs/node_modules/tslib": { + "version": "1.13.0", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.13.0.tgz", + "integrity": "sha512-i/6DQjL8Xf3be4K/E6Wgpekn5Qasl1usyw++dAA35Ue5orEn65VIxOA+YvNNl9HV3qv70T7CNwjODHZrLwvd1Q==" + }, "node_modules/test-exclude": { "version": "6.0.0", "resolved": "https://registry.npmjs.org/test-exclude/-/test-exclude-6.0.0.tgz", @@ -7384,6 +7458,11 @@ "node": ">=0.8" } }, + "node_modules/tr46": { + "version": "0.0.3", + "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", + "integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==" + }, "node_modules/tsconfig-paths": { "version": "3.14.1", "resolved": "https://registry.npmjs.org/tsconfig-paths/-/tsconfig-paths-3.14.1.tgz", @@ -7694,6 +7773,11 @@ "node": ">=6.0" } }, + "node_modules/webidl-conversions": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz", + "integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==" + }, "node_modules/whacko": { "version": "0.19.1", "resolved": "https://registry.npmjs.org/whacko/-/whacko-0.19.1.tgz", @@ -7713,6 +7797,15 @@ "resolved": "https://registry.npmjs.org/lodash/-/lodash-3.10.1.tgz", "integrity": "sha512-9mDDwqVIma6OZX79ZlDACZl8sBm0TEnkf99zV3iMA4GzkIT/9hiqP5mY0HoT1iNLCrKc/R1HByV+yJfRWVJryQ==" }, + "node_modules/whatwg-url": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz", + "integrity": "sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==", + "dependencies": { + "tr46": "~0.0.3", + "webidl-conversions": "^3.0.0" + } + }, "node_modules/which": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz", @@ -11256,8 +11349,7 @@ "is-stream": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/is-stream/-/is-stream-2.0.1.tgz", - "integrity": "sha512-hFoiJiTl63nn+kstHGBtewWSKnQLpyb155KHheA1l39uvtO9nWIop1p3udqPcUd/xbF1VLMO4n7OI6p7RbngDg==", - "dev": true + "integrity": "sha512-hFoiJiTl63nn+kstHGBtewWSKnQLpyb155KHheA1l39uvtO9nWIop1p3udqPcUd/xbF1VLMO4n7OI6p7RbngDg==" }, "is-string": { "version": "1.0.7", @@ -12122,6 +12214,14 @@ "uuid": "8.3.2" } }, + "node-fetch": { + "version": "2.6.7", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.7.tgz", + "integrity": "sha512-ZjMPFEfVx5j+y2yF35Kzx5sF7kDzxuDj6ziH4FFbOp87zKDZNx8yExJIb05OGF4Nlt9IHFIMBkRl41VdvcNdbQ==", + "requires": { + "whatwg-url": "^5.0.0" + } + }, "node-preload": { "version": "0.2.1", "resolved": "https://registry.npmjs.org/node-preload/-/node-preload-0.2.1.tgz", @@ -12790,6 +12890,11 @@ "resolved": "https://registry.npmjs.org/qs/-/qs-6.5.2.tgz", "integrity": "sha512-N5ZAX4/LxJmF+7wN74pUD6qAh9/wnvdQcjq9TZjevvXzSUo7bfmw91saqMjzGS2xq91/odN2dW/WOl7qQHNDGA==" }, + "querystring": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/querystring/-/querystring-0.2.1.tgz", + "integrity": "sha512-wkvS7mL/JMugcup3/rMitHmd9ecIGd2lhFhK9N3UUQ450h66d1r3Y9nvXzQAW1Lq+wyx61k/1pfKS5KuKiyEbg==" + }, "queue-microtask": { "version": "1.2.3", "resolved": "https://registry.npmjs.org/queue-microtask/-/queue-microtask-1.2.3.tgz", @@ -13451,6 +13556,41 @@ "integrity": "sha512-ot0WnXS9fgdkgIcePe6RHNk1WA8+muPa6cSjeR3V8K27q9BB1rTE3R1p7Hv0z1ZyAc8s6Vvv8DIyWf681MAt0w==", "dev": true }, + "tencentcloud-sdk-nodejs": { + "version": "4.0.480", + "resolved": "https://registry.npmjs.org/tencentcloud-sdk-nodejs/-/tencentcloud-sdk-nodejs-4.0.480.tgz", + "integrity": "sha512-EXFK9u3axlNLqNOe/eNMpvC0Ajv20NBDuJ8p3Qe74ecORcBzvwWZLGIcc/6TFNtiGFYGyEj1ioBYszLOzAAvWg==", + "requires": { + "form-data": "^3.0.0", + "get-stream": "^6.0.0", + "https-proxy-agent": "^5.0.0", + "is-stream": "^2.0.0", + "node-fetch": "^2.2.0", + "tslib": "1.13.0" + }, + "dependencies": { + "form-data": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-3.0.1.tgz", + "integrity": "sha512-RHkBKtLWUVwd7SqRIvCZMEvAMoGUp0XU+seQiZejj0COz3RI3hWP4sCv3gZWWLjJTd7rGwcsF5eKZGii0r/hbg==", + "requires": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + } + }, + "get-stream": { + "version": "6.0.1", + "resolved": "https://registry.npmjs.org/get-stream/-/get-stream-6.0.1.tgz", + "integrity": "sha512-ts6Wi+2j3jQjqi70w5AlN8DFnkSwC+MqmxEzdEALB2qXZYV3X/b1CTfgPLGJNMeAWxdPfU8FO1ms3NUfaHCPYg==" + }, + "tslib": { + "version": "1.13.0", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.13.0.tgz", + "integrity": "sha512-i/6DQjL8Xf3be4K/E6Wgpekn5Qasl1usyw++dAA35Ue5orEn65VIxOA+YvNNl9HV3qv70T7CNwjODHZrLwvd1Q==" + } + } + }, "test-exclude": { "version": "6.0.0", "resolved": "https://registry.npmjs.org/test-exclude/-/test-exclude-6.0.0.tgz", @@ -13533,6 +13673,11 @@ "punycode": "^2.1.1" } }, + "tr46": { + "version": "0.0.3", + "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", + "integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==" + }, "tsconfig-paths": { "version": "3.14.1", "resolved": "https://registry.npmjs.org/tsconfig-paths/-/tsconfig-paths-3.14.1.tgz", @@ -13770,6 +13915,11 @@ "acorn-walk": "^8.2.0" } }, + "webidl-conversions": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz", + "integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==" + }, "whacko": { "version": "0.19.1", "resolved": "https://registry.npmjs.org/whacko/-/whacko-0.19.1.tgz", @@ -13788,6 +13938,15 @@ } } }, + "whatwg-url": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz", + "integrity": "sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==", + "requires": { + "tr46": "~0.0.3", + "webidl-conversions": "^3.0.0" + } + }, "which": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz", diff --git a/package.json b/package.json index 20b8608..242d9d7 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,8 @@ "node-cron": "^3.0.2", "node-telegram-bot-api": "^0.60.0", "pako": "^1.0.11", + "querystring": "^0.2.1", + "tencentcloud-sdk-nodejs": "^4.0.480", "whacko": "^0.19.1", "xml-js": "^1.6.11", "xml2js": "^0.4.23" diff --git a/schedule/cf2dns.js b/schedule/cf2dns.js new file mode 100644 index 0000000..b6a9d39 --- /dev/null +++ b/schedule/cf2dns.js @@ -0,0 +1,59 @@ +const axios = require("axios"); +const dnspod = require("./dnspod"); + +const KEY = "o1zrmHAF"; +const DOMAINS = { + "home999.cc": { + "gd": { + "移动": "CM", + "联通": "CU", + "电信": "CT", + "境内": "CT", + }, + } +}; + +async function get_optimization_ip() { + const res = await axios.post("https://api.hostmonit.com/get_optimization_ip", { "key": KEY , "type": "v4" }); + return res.data; +} + +async function main() { + const cfips = await get_optimization_ip(); + if (cfips.code !== 200) { + console.log("GET CLOUDFLARE IP ERROR: ----Time: " + new Date().toLocaleString()); + return; + } + for (const domain in DOMAINS) { + const sub_domains = DOMAINS[domain]; + for (const sub_domain in sub_domains) { + console.log(sub_domain,domain, "删除非默认线路的记录"); + const records = await dnspod.get_record(domain, sub_domain); + for (const record of records) { + if (record.line !== "默认") { + const res = await dnspod.del_record(domain, record); + if (res.status.code!=="1") + return "fail!"+res.status.message; + // console.log(record); + } + } + console.log(sub_domain,domain, "添加非默认线路的记录"); + const record = JSON.parse(JSON.stringify(records[0])); + for (const line in sub_domains[sub_domain]) { + for (let i=0;i<2;i++) { + const ISP = sub_domains[sub_domain][line]; + record.line = line; + record.type = "A"; + record.value = cfips["info"][ISP][i].ip; + const res = await dnspod.add_record(domain, record); + if (res.status.code!=="1") + return "fail!"+res.status.message; + console.log(cfips["info"][ISP][i]); + } + } + } + } + return "success!"; +} + +module.exports = main; diff --git a/schedule/dnspod.js b/schedule/dnspod.js new file mode 100644 index 0000000..cdb3ce2 --- /dev/null +++ b/schedule/dnspod.js @@ -0,0 +1,80 @@ +const axios = require("axios"); +const token = "359232,18633dc7590e5c7554cc966e3469b9ed"; +const querystring = require("querystring"); +// 创建实例时配置默认值 +const instance = axios.create({ + baseURL: "https://dnsapi.cn" +}); + +async function get_record(domain, subdomain) { + const api = "/Record.List"; + const data = querystring.stringify({ + "domain": domain, + "sub_domain": subdomain, + "login_token": token, + "format": "json" + }); + const res = await instance.post(api, data); + return res.data.records; +} + +async function update_record(domain, record) { + const {id, line, type, name, value} = record; + const api = "/Record.Modify"; + const data = querystring.stringify({ + domain, + record_id: id, + value, + record_line: line, + record_type: type, + sub_domain: name, + "login_token": token, + "format": "json", + }); + const res = await instance.post(api, data); + return res.data; +} + +async function add_record(domain, record) { + const {line, type, name, value} = record; + const api = "/Record.Create"; + const data = querystring.stringify({ + domain, + value, + record_line: line, + record_type: type, + sub_domain: name, + "login_token": token, + "format": "json", + }); + const res = await instance.post(api, data); + return res.data; +} + +async function del_record(domain, record) { + const {id} = record; + const api = "/Record.Remove"; + const data = querystring.stringify({ + domain, + record_id: id, + "login_token": token, + "format": "json", + }); + const res = await instance.post(api, data); + return res.data; +} + +module.exports = {get_record, update_record, add_record, del_record}; + +if (!module.parent) { + get_record("home999.cc", "gd").then(res => { + console.log(res); + for (const record of res) { + if (record.line !== "默认") { + del_record("home999.cc", record).then(res => { + console.log(res); + }); + } + } + }); +} diff --git a/schedule/schedule.js b/schedule/schedule.js index b0adb99..42ff4f0 100644 --- a/schedule/schedule.js +++ b/schedule/schedule.js @@ -2,24 +2,38 @@ const cron = require("node-cron"); const leancloud = require("../utils/leancloud"); const chai = require("chai"); const chaiHttp = require("chai-http"); -const app = require("../app"); +const cf2dns = require("./cf2dns"); -function subcache() { +function subcache(app) { console.log("Running Cron Job:subcache"); chai.use(chaiHttp); chai.request(app) .get("/sub/cache") .end((err, res) => { - leancloud.add("Schedule", {name: "subcache", result: res.text}); + leancloud.add("Schedule", { + name: "subcache", + result: res.text + }); console.log(res.text); }); } -cron.schedule("0 * * * *", () => { - subcache(); -}); -//TODO 添加自动删除一个月前的日志 -console.log("schedule.js loaded"); -// if (!module.parent) { -// subcache(); -// } +module.exports = (app) => { + //TODO 添加自动删除一个月前的日志 + console.log("schedule.js loaded"); + cron.schedule("0 */8 * * *", () => { + subcache(app); + }); + cron.schedule("*/15 * * * *", () => { + cf2dns().then((result)=>{ + leancloud.add("Schedule", { + name: "cf2dns", + result + }); + }); + }); +}; + +if (!module.parent) { + cf2dns(); +}