1+ import { parse } from 'dotenv' ;
2+
13/* eslint-disable no-undef */
24const isQX = typeof $task !== 'undefined' ;
35const isLoon = typeof $loon !== 'undefined' ;
@@ -18,6 +20,26 @@ function isPlainObject(obj) {
1820 ) ;
1921}
2022
23+ function parseSocks5Uri ( uri ) {
24+ // eslint-disable-next-line no-unused-vars
25+ let [ __ , username , password , server , port , query , name ] = uri . match (
26+ / ^ s o c k s 5 : \/ \/ (?: ( .* ?) : ( .* ?) @ ) ? ( .* ?) (?: : ( \d + ?) ) ? ( \? .* ?) ? (?: # ( .* ?) ) ? $ / ,
27+ ) ;
28+ if ( port ) {
29+ port = parseInt ( port , 10 ) ;
30+ } else {
31+ $ . error ( `port is not present in line: ${ uri } ` ) ;
32+ throw new Error ( `port is not present in line: ${ uri } ` ) ;
33+ }
34+ return {
35+ type : 5 ,
36+ host : server ,
37+ port,
38+
39+ userId : username != null ? decodeURIComponent ( username ) : undefined ,
40+ password : password != null ? decodeURIComponent ( password ) : undefined ,
41+ } ;
42+ }
2143export class OpenAPI {
2244 constructor ( name = 'untitled' , debug = false ) {
2345 this . name = name ;
@@ -393,6 +415,7 @@ export function HTTP(defaultOptions = { baseURL: '' }) {
393415 }
394416 if ( isNode ) {
395417 const undici = eval ( "require('undici')" ) ;
418+ const { socksDispatcher } = eval ( "require('fetch-socks')" ) ;
396419 const {
397420 ProxyAgent,
398421 EnvHttpProxyAgent,
@@ -422,16 +445,34 @@ export function HTTP(defaultOptions = { baseURL: '' }) {
422445 ) . toString ( 'base64' ) } `,
423446 } ;
424447 }
448+ let dispatcher ;
449+ if ( ! opts . proxy ) {
450+ const allProxy =
451+ eval ( 'process.env.all_proxy' ) ||
452+ eval ( 'process.env.ALL_PROXY' ) ;
453+ if ( allProxy && / ^ s o c k s 5 : \/ \/ / . test ( allProxy ) ) {
454+ opts . proxy = allProxy ;
455+ }
456+ }
457+ if ( opts . proxy ) {
458+ if ( / ^ s o c k s 5 : \/ \/ / . test ( opts . proxy ) ) {
459+ dispatcher = socksDispatcher (
460+ parseSocks5Uri ( opts . proxy ) ,
461+ agentOpts ,
462+ ) ;
463+ } else {
464+ dispatcher = new ProxyAgent ( {
465+ ...agentOpts ,
466+ uri : opts . proxy ,
467+ } ) ;
468+ }
469+ } else {
470+ dispatcher = new EnvHttpProxyAgent ( agentOpts ) ;
471+ }
425472 const response = await request ( opts . url , {
426473 ...opts ,
427474 method : method . toUpperCase ( ) ,
428- dispatcher : ( opts . proxy
429- ? new ProxyAgent ( {
430- ...agentOpts ,
431- uri : opts . proxy ,
432- } )
433- : new EnvHttpProxyAgent ( agentOpts )
434- ) . compose (
475+ dispatcher : dispatcher . compose (
435476 interceptors . redirect ( {
436477 maxRedirections : 3 ,
437478 throwOnMaxRedirects : true ,
0 commit comments