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] 请求头
3438 */
3539
3640const { request : http_request } = require ( 'http' ) ;
41+ const { HttpProxyAgent } = require ( 'http-proxy-agent' ) ;
3742const { request : https_request } = require ( 'https' ) ;
43+ const { HttpsProxyAgent } = require ( 'https-proxy-agent' ) ;
3844const { 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 = '' ;
0 commit comments