Skip to content

Commit ef8366b

Browse files
committed
fix: tg推送代理设置出错 (#298)
Fixed #289
1 parent a7e31b4 commit ef8366b

3 files changed

Lines changed: 23 additions & 8 deletions

File tree

lib/helper/notify.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -495,8 +495,8 @@ function tgBotNotify(text, desp) {
495495
}
496496
if (TG_PROXY_HOST && TG_PROXY_PORT) {
497497
options.proxy = {
498-
hostname: TG_PROXY_HOST,
499-
port: TG_PROXY_PORT * 1
498+
url: "http://" + TG_PROXY_HOST + ":" + TG_PROXY_PORT,
499+
auth_headers: []
500500
}
501501
}
502502
send(options)

lib/net/http.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,16 @@
2020
* @property {boolean} [redirect] 是否重定向
2121
* @property {number} [retry_times] 重试次数
2222
*
23+
* @typedef ProxyConfig
24+
* @property {string} url https?://{IP}:{PORT}
25+
* @property {[[string, string]]} auth_headers [[k,v],[k,v]]
26+
*
2327
* @typedef {object} RequestOptions Http请求选项
2428
* @property {string} [method] 请求方法
2529
* @property {string} url 完整链接
2630
* @property {boolean} [stream] 是否流式数据
2731
* @property {RequestConfig} [config] 设置
28-
* @property {string} [proxy] HTTP代理 IP:PORT
32+
* @property {ProxyConfig} [proxy] HTTP代理 IP:PORT
2933
* @property {Object.<string, string|number>} [query] 查询选项
3034
* @property {Object.<string, string|number> | string} [contents] 内容
3135
* @property {HttpHeaders} [headers] 请求头
@@ -34,7 +38,9 @@
3438
*/
3539

3640
const { request: http_request } = require('http');
41+
const { HttpProxyAgent } = require('http-proxy-agent');
3742
const { request: https_request } = require('https');
43+
const { HttpsProxyAgent } = require('https-proxy-agent');
3844
const { stringify } = require('querystring');
3945

4046
/**默认编码 */
@@ -64,7 +70,10 @@ function send(detail) {
6470
const { timeout, wait, retry, redirect, retry_times } = config;
6571
const thisURL = new URL(url)
6672
, content = formatContents(headers["content-type"], contents)
67-
, request = (thisURL.protocol === 'https:') && !proxy ? https_request : http_request;
73+
, request = (thisURL.protocol === 'https:') ? https_request : http_request;
74+
/**
75+
* @type {import("https").RequestOptions}
76+
*/
6877
let options = {
6978
timeout,
7079
method: method.toUpperCase(),
@@ -80,9 +89,13 @@ function send(detail) {
8089
options.headers['content-length'] = Buffer.byteLength(content, 'utf-8').toString();
8190
}
8291
if (proxy) {
83-
options.headers.host = thisURL.host;
84-
options.path = thisURL.href;
85-
[options.host, options.port] = proxy.split(':');
92+
options.agent = (thisURL.protocol === 'https:')
93+
? new HttpsProxyAgent(proxy.url)
94+
: new HttpProxyAgent(proxy.url);
95+
for (const ah of proxy.auth_headers) {
96+
options.headers[ah[0]] = ah[1]
97+
}
98+
options.rejectUnauthorized = false
8699
}
87100
const req = request(options, res => {
88101
let protodata = '';

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
},
4343
"dependencies": {
4444
"chalk": "^4.1.2",
45+
"http-proxy-agent": "^7.0.0",
46+
"https-proxy-agent": "^7.0.0",
4547
"nodemailer": "^6.7.0"
4648
}
47-
}
49+
}

0 commit comments

Comments
 (0)