From a0a25021481aa307d649342fcca26eb2fc07b1e7 Mon Sep 17 00:00:00 2001 From: shanmite Date: Thu, 29 Jun 2023 15:37:49 +0800 Subject: [PATCH 1/3] test case --- lib/net/http.js | 23 ++++++++++++++++++----- package.json | 2 ++ test/api.test.js | 2 +- test/article.test.js | 2 +- test/dynamic_card.test.js | 2 +- test/index.js | 1 + test/proxy.test.js | 38 ++++++++++++++++++++++++++++++++++++++ 7 files changed, 62 insertions(+), 8 deletions(-) create mode 100644 test/proxy.test.js diff --git a/lib/net/http.js b/lib/net/http.js index fb18b2e65..37e8e4e05 100644 --- a/lib/net/http.js +++ b/lib/net/http.js @@ -20,12 +20,16 @@ * @property {boolean} [redirect] 是否重定向 * @property {number} [retry_times] 重试次数 * + * @typedef ProxyConfig + * @property {string} url https?://{IP}:{PORT} + * @property {[[string, string]]} auth_headers [[k,v],[k,v]] + * * @typedef {object} RequestOptions Http请求选项 * @property {string} [method] 请求方法 * @property {string} url 完整链接 * @property {boolean} [stream] 是否流式数据 * @property {RequestConfig} [config] 设置 - * @property {string} [proxy] HTTP代理 IP:PORT + * @property {ProxyConfig} [proxy] HTTP代理 IP:PORT * @property {Object.} [query] 查询选项 * @property {Object. | string} [contents] 内容 * @property {HttpHeaders} [headers] 请求头 @@ -34,7 +38,9 @@ */ const { request: http_request } = require('http'); +const { HttpProxyAgent } = require('http-proxy-agent'); const { request: https_request } = require('https'); +const { HttpsProxyAgent } = require('https-proxy-agent'); const { stringify } = require('querystring'); /**默认编码 */ @@ -64,7 +70,10 @@ function send(detail) { const { timeout, wait, retry, redirect, retry_times } = config; const thisURL = new URL(url) , content = formatContents(headers["content-type"], contents) - , request = (thisURL.protocol === 'https:') && !proxy ? https_request : http_request; + , request = (thisURL.protocol === 'https:') ? https_request : http_request; + /** + * @type {import("https").RequestOptions} + */ let options = { timeout, method: method.toUpperCase(), @@ -80,9 +89,13 @@ function send(detail) { options.headers['content-length'] = Buffer.byteLength(content, 'utf-8').toString(); } if (proxy) { - options.headers.host = thisURL.host; - options.path = thisURL.href; - [options.host, options.port] = proxy.split(':'); + options.agent = (thisURL.protocol === 'https:') + ? new HttpsProxyAgent(proxy.url) + : new HttpProxyAgent(proxy.url); + for (const ah of proxy.auth_headers) { + options.headers[ah[0]] = ah[1] + } + options.rejectUnauthorized = false } const req = request(options, res => { let protodata = ''; diff --git a/package.json b/package.json index 0c3db4370..30dd4d5f5 100644 --- a/package.json +++ b/package.json @@ -42,6 +42,8 @@ }, "dependencies": { "chalk": "^4.1.2", + "http-proxy-agent": "^7.0.0", + "https-proxy-agent": "^7.0.0", "nodemailer": "^6.7.0" } } diff --git a/test/api.test.js b/test/api.test.js index 82e9e8997..113b3b29c 100644 --- a/test/api.test.js +++ b/test/api.test.js @@ -6,7 +6,7 @@ const { parseDynamicCard } = require('../lib/core/searcher'); (async () => { assert(await bili_client.getMyinfo()); - await util.par_run([0, 1, 2, 3, 4, 6], [ + await util.par_run([], [ // 0 async () => { assert.equal((await bili_client.getTopRcmd()).length, 10) diff --git a/test/article.test.js b/test/article.test.js index a43b39854..e0691e608 100644 --- a/test/article.test.js +++ b/test/article.test.js @@ -3,7 +3,7 @@ const bili_client = require("../lib/net/bili"); const util = require('./util'); (async () => { - await util.par_run([0], [ + await util.par_run([], [ // 0 async () => { let info = await bili_client.getOneArticleByCv(22112353); diff --git a/test/dynamic_card.test.js b/test/dynamic_card.test.js index 5dcb66a9a..e54b0aaab 100644 --- a/test/dynamic_card.test.js +++ b/test/dynamic_card.test.js @@ -4,7 +4,7 @@ const searcher = require("../lib/core/searcher"); const util = require('./util'); (async () => { - await util.par_run([0, 1, 2, 3, 4, 5, 6], [ + await util.par_run([], [ // 0 async () => { let info = await bili_client.getOneDynamicByDyid("728424890210713624"); diff --git a/test/index.js b/test/index.js index 19d20edf9..0ba591d94 100644 --- a/test/index.js +++ b/test/index.js @@ -7,6 +7,7 @@ log._level = 2 env.init() global_var.init(process.env["COOKIE"], 1) + fs.readdirSync(module.path) .filter(file => file.endsWith(".test.js")) .forEach(file => require(`${module.path}/${file}`)) \ No newline at end of file diff --git a/test/proxy.test.js b/test/proxy.test.js new file mode 100644 index 000000000..aa1719eef --- /dev/null +++ b/test/proxy.test.js @@ -0,0 +1,38 @@ +const util = require('./util'); +const crypto = require('crypto'); +const { send } = require('../lib/net/http'); + +(async () => { + await util.par_run([0], [ + // 0 + async () => { + let timestamp = parseInt(new Date().getTime() / 1000); + let orderno = process.env["XMDL_ORDERNNO"]; + let secret = process.env["XMDL_SECRET"]; + + if (orderno && secret) { + let txt = 'orderno=' + orderno + ',secret=' + secret + ',timestamp=' + timestamp; + let md5 = crypto.createHash('md5'); + let sign = md5.update(txt).digest('hex').toUpperCase(); + + console.log(await new Promise((resolve) => { + send({ + url: "https://api.bilibili.com/client_info", + proxy: { + url: "http://dtan.xiongmaodaili.com:8088", + auth_headers: [ + ["Proxy-Authorization", 'sign=' + sign + '&orderno=' + orderno + "×tamp=" + timestamp] + ] + }, + config: { + retry: false + }, + success: (res) => { resolve(JSON.parse(res.body)) }, + failure: resolve + }) + })); + console.log("proxy.test ... ok!"); + } + }, + ]) +})() \ No newline at end of file From c0dbdee343f6a52872ef50d97471a4f859b0e2e8 Mon Sep 17 00:00:00 2001 From: shanmite Date: Thu, 29 Jun 2023 16:40:01 +0800 Subject: [PATCH 2/3] proxy all --- env.example.js | 4 ++++ lib/helper/proxies.js | 26 ++++++++++++++++++++++++++ lib/net/bili.js | 19 ++++++++++++++----- test/api.test.js | 2 +- test/article.test.js | 2 +- test/dynamic_card.test.js | 2 +- test/proxy.test.js | 2 +- 7 files changed, 48 insertions(+), 9 deletions(-) create mode 100644 lib/helper/proxies.js diff --git a/env.example.js b/env.example.js index 241fc0517..b3b75b22a 100644 --- a/env.example.js +++ b/env.example.js @@ -23,6 +23,10 @@ module.exports = Object.freeze({ NUMBER: 1, CLEAR: true, + ENABLE_PROXY: "", + XMDL_ORDERNNO: "", + XMDL_SECRET: "", + ENABLE_MULTIPLE_ACCOUNT: false, MULTIPLE_ACCOUNT_PARM: "", diff --git a/lib/helper/proxies.js b/lib/helper/proxies.js new file mode 100644 index 000000000..afc5dfe0d --- /dev/null +++ b/lib/helper/proxies.js @@ -0,0 +1,26 @@ +const { log } = require('../utils'); +const crypto = require('crypto'); + +/** + * [熊猫代理](http://www.xiongmaodaili.com) + * @returns {import("../net/http").ProxyConfig} + */ +function xiongmaodaili() { + log.info("熊猫代理", "开始") + let timestamp = parseInt(new Date().getTime() / 1000); + let orderno = process.env["XMDL_ORDERNNO"]; + let secret = process.env["XMDL_SECRET"]; + + let txt = 'orderno=' + orderno + ',secret=' + secret + ',timestamp=' + timestamp; + let md5 = crypto.createHash('md5'); + let sign = md5.update(txt).digest('hex').toUpperCase(); + + return { + url: "http://dtan.xiongmaodaili.com:8088", + auth_headers: [ + ["Proxy-Authorization", 'sign=' + sign + '&orderno=' + orderno + "×tamp=" + timestamp] + ] + } +} + +module.exports = { xiongmaodaili } \ No newline at end of file diff --git a/lib/net/bili.js b/lib/net/bili.js index 62b257db2..53e34ca93 100644 --- a/lib/net/bili.js +++ b/lib/net/bili.js @@ -1,7 +1,8 @@ const GlobalVar = require('../data/global_var'); -const { strToJson, log } = require('../utils'); +const { strToJson, log, hasEnv } = require('../utils'); const { send } = require('./http'); const API = require('./api.bili'); +const proxies = require("../helper/proxies"); class Line { /** @@ -88,7 +89,7 @@ class Line { */ function get({ url, config, contents, query }) { return new Promise((resolve) => { - send({ + let ro = { url, method: 'GET', config, @@ -100,7 +101,11 @@ function get({ url, config, contents, query }) { contents, success: res => resolve(res.body), failure: err => resolve(err) - }) + } + if (hasEnv("ENABLE_PROXY")) { + ro.proxy = proxies.xiongmaodaili(); + } + send(ro) }) } @@ -111,7 +116,7 @@ function get({ url, config, contents, query }) { */ function post({ url, config, contents, query }) { return new Promise((resolve) => { - send({ + let ro = { url, method: 'POST', config, @@ -124,7 +129,11 @@ function post({ url, config, contents, query }) { contents, success: res => resolve(res.body), failure: err => resolve(err) - }) + }; + if (hasEnv("ENABLE_PROXY")) { + ro.proxy = proxies.xiongmaodaili(); + } + send(ro) }) } diff --git a/test/api.test.js b/test/api.test.js index 113b3b29c..6563c239b 100644 --- a/test/api.test.js +++ b/test/api.test.js @@ -6,7 +6,7 @@ const { parseDynamicCard } = require('../lib/core/searcher'); (async () => { assert(await bili_client.getMyinfo()); - await util.par_run([], [ + await util.par_run([0, 1, 2, 3, 4, 5], [ // 0 async () => { assert.equal((await bili_client.getTopRcmd()).length, 10) diff --git a/test/article.test.js b/test/article.test.js index e0691e608..a43b39854 100644 --- a/test/article.test.js +++ b/test/article.test.js @@ -3,7 +3,7 @@ const bili_client = require("../lib/net/bili"); const util = require('./util'); (async () => { - await util.par_run([], [ + await util.par_run([0], [ // 0 async () => { let info = await bili_client.getOneArticleByCv(22112353); diff --git a/test/dynamic_card.test.js b/test/dynamic_card.test.js index e54b0aaab..5dcb66a9a 100644 --- a/test/dynamic_card.test.js +++ b/test/dynamic_card.test.js @@ -4,7 +4,7 @@ const searcher = require("../lib/core/searcher"); const util = require('./util'); (async () => { - await util.par_run([], [ + await util.par_run([0, 1, 2, 3, 4, 5, 6], [ // 0 async () => { let info = await bili_client.getOneDynamicByDyid("728424890210713624"); diff --git a/test/proxy.test.js b/test/proxy.test.js index aa1719eef..fd0ef2575 100644 --- a/test/proxy.test.js +++ b/test/proxy.test.js @@ -3,7 +3,7 @@ const crypto = require('crypto'); const { send } = require('../lib/net/http'); (async () => { - await util.par_run([0], [ + await util.par_run([], [ // 0 async () => { let timestamp = parseInt(new Date().getTime() / 1000); From 77245c0391c4e3fbf7a1390daa0ddd077029cdf1 Mon Sep 17 00:00:00 2001 From: shanmite Date: Fri, 30 Jun 2023 08:44:57 +0800 Subject: [PATCH 3/3] env.js --- env.example.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/env.example.js b/env.example.js index b3b75b22a..dca73a827 100644 --- a/env.example.js +++ b/env.example.js @@ -7,6 +7,10 @@ module.exports = Object.freeze({ * - `CLEAR` 是否启用清理功能 * - `ENABLE_MULTIPLE_ACCOUNT` 是否启用多账号 * - `MULTIPLE_ACCOUNT_PARM` 多账号参数(JSON格式) + * ## 代理相关 + * - `ENABLE_PROXY` 启用代理 + * - `XMDL_ORDERNNO` 熊猫动态代理订单号 + * - `XMDL_SECRET` 熊猫动态代理密钥 * ## 调试相关 * - `LOTTERY_LOG_LEVEL` 输出日志等级 Error