fix: tg推送代理设置出错 (#298)

Fixed #289
This commit is contained in:
shanmite 2023-07-10 10:15:33 +08:00
parent a7e31b484e
commit ef8366be66
3 changed files with 23 additions and 8 deletions

View File

@ -495,8 +495,8 @@ function tgBotNotify(text, desp) {
} }
if (TG_PROXY_HOST && TG_PROXY_PORT) { if (TG_PROXY_HOST && TG_PROXY_PORT) {
options.proxy = { options.proxy = {
hostname: TG_PROXY_HOST, url: "http://" + TG_PROXY_HOST + ":" + TG_PROXY_PORT,
port: TG_PROXY_PORT * 1 auth_headers: []
} }
} }
send(options) send(options)

View File

@ -20,12 +20,16 @@
* @property {boolean} [redirect] 是否重定向 * @property {boolean} [redirect] 是否重定向
* @property {number} [retry_times] 重试次数 * @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请求选项 * @typedef {object} RequestOptions Http请求选项
* @property {string} [method] 请求方法 * @property {string} [method] 请求方法
* @property {string} url 完整链接 * @property {string} url 完整链接
* @property {boolean} [stream] 是否流式数据 * @property {boolean} [stream] 是否流式数据
* @property {RequestConfig} [config] 设置 * @property {RequestConfig} [config] 设置
* @property {string} [proxy] HTTP代理 IP:PORT * @property {ProxyConfig} [proxy] HTTP代理 IP:PORT
* @property {Object.<string, string|number>} [query] 查询选项 * @property {Object.<string, string|number>} [query] 查询选项
* @property {Object.<string, string|number> | string} [contents] 内容 * @property {Object.<string, string|number> | string} [contents] 内容
* @property {HttpHeaders} [headers] 请求头 * @property {HttpHeaders} [headers] 请求头
@ -34,7 +38,9 @@
*/ */
const { request: http_request } = require('http'); const { request: http_request } = require('http');
const { HttpProxyAgent } = require('http-proxy-agent');
const { request: https_request } = require('https'); const { request: https_request } = require('https');
const { HttpsProxyAgent } = require('https-proxy-agent');
const { stringify } = require('querystring'); const { stringify } = require('querystring');
/**默认编码 */ /**默认编码 */
@ -64,7 +70,10 @@ function send(detail) {
const { timeout, wait, retry, redirect, retry_times } = config; const { timeout, wait, retry, redirect, retry_times } = config;
const thisURL = new URL(url) const thisURL = new URL(url)
, content = formatContents(headers["content-type"], contents) , 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 = { let options = {
timeout, timeout,
method: method.toUpperCase(), method: method.toUpperCase(),
@ -80,9 +89,13 @@ function send(detail) {
options.headers['content-length'] = Buffer.byteLength(content, 'utf-8').toString(); options.headers['content-length'] = Buffer.byteLength(content, 'utf-8').toString();
} }
if (proxy) { if (proxy) {
options.headers.host = thisURL.host; options.agent = (thisURL.protocol === 'https:')
options.path = thisURL.href; ? new HttpsProxyAgent(proxy.url)
[options.host, options.port] = proxy.split(':'); : 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 => { const req = request(options, res => {
let protodata = ''; let protodata = '';

View File

@ -42,6 +42,8 @@
}, },
"dependencies": { "dependencies": {
"chalk": "^4.1.2", "chalk": "^4.1.2",
"http-proxy-agent": "^7.0.0",
"https-proxy-agent": "^7.0.0",
"nodemailer": "^6.7.0" "nodemailer": "^6.7.0"
} }
} }