1+ import { socksDispatcher } from 'fetch-socks' ;
12import { HttpsProxyAgent } from 'https-proxy-agent' ;
23import { SocksProxyAgent } from 'socks-proxy-agent' ;
34import { ProxyAgent } from 'undici' ;
@@ -46,7 +47,7 @@ export function makeProxyAgent(proxy: Proxy | string): HttpsProxyAgent<string> |
4647 return selectProxyAgent ( proxyUrl ) ;
4748}
4849
49- export function makeProxyAgentUndici ( proxy : Proxy | string ) : ProxyAgent | SocksProxyAgent {
50+ export function makeProxyAgentUndici ( proxy : Proxy | string ) {
5051 let proxyUrl : string ;
5152 let protocol : string ;
5253
@@ -57,15 +58,15 @@ export function makeProxyAgentUndici(proxy: Proxy | string): ProxyAgent | SocksP
5758 } else {
5859 const { host, password, port, protocol : proto , username } = proxy ;
5960 protocol = ( proto || 'http' ) . replace ( ':' , '' ) ;
60-
61- if ( protocol === 'socks' ) {
62- protocol = 'socks5' ;
63- }
61+ if ( protocol === 'socks' ) protocol = 'socks5' ;
6462
6563 const auth = username && password ? `${ username } :${ password } @` : '' ;
6664 proxyUrl = `${ protocol } ://${ auth } ${ host } :${ port } ` ;
6765 }
6866
67+ // Normalização
68+ protocol = protocol . toLowerCase ( ) ;
69+
6970 const PROXY_HTTP_PROTOCOL = 'http' ;
7071 const PROXY_HTTPS_PROTOCOL = 'https' ;
7172 const PROXY_SOCKS4_PROTOCOL = 'socks4' ;
@@ -74,10 +75,26 @@ export function makeProxyAgentUndici(proxy: Proxy | string): ProxyAgent | SocksP
7475 switch ( protocol ) {
7576 case PROXY_HTTP_PROTOCOL :
7677 case PROXY_HTTPS_PROTOCOL :
78+ // Proxy HTTP/HTTPS → usar ProxyAgent do Undici
7779 return new ProxyAgent ( proxyUrl ) ;
80+
7881 case PROXY_SOCKS4_PROTOCOL :
79- case PROXY_SOCKS5_PROTOCOL :
80- return new SocksProxyAgent ( proxyUrl ) ;
82+ case PROXY_SOCKS5_PROTOCOL : {
83+ let type : 4 | 5 = 5 ;
84+
85+ if ( PROXY_SOCKS4_PROTOCOL === protocol ) type = 4 ;
86+
87+ const url = new URL ( proxyUrl ) ;
88+
89+ return socksDispatcher ( {
90+ type : type ,
91+ host : url . hostname ,
92+ port : Number ( url . port ) ,
93+ userId : url . username || undefined ,
94+ password : url . password || undefined ,
95+ } ) ;
96+ }
97+
8198 default :
8299 throw new Error ( `Unsupported proxy protocol: ${ protocol } ` ) ;
83100 }
0 commit comments