1+ import { socksDispatcher } from 'fetch-socks' ;
12import { HttpsProxyAgent } from 'https-proxy-agent' ;
23import { SocksProxyAgent } from 'socks-proxy-agent' ;
34import { ProxyAgent } from 'undici' ;
@@ -18,12 +19,23 @@ function selectProxyAgent(proxyUrl: string): HttpsProxyAgent<string> | SocksProx
1819 // the end so, we add the protocol constants without the `:` to avoid confusion.
1920 const PROXY_HTTP_PROTOCOL = 'http:' ;
2021 const PROXY_SOCKS_PROTOCOL = 'socks:' ;
22+ const PROXY_SOCKS5_PROTOCOL = 'socks5:' ;
2123
2224 switch ( url . protocol ) {
2325 case PROXY_HTTP_PROTOCOL :
2426 return new HttpsProxyAgent ( url ) ;
2527 case PROXY_SOCKS_PROTOCOL :
26- return new SocksProxyAgent ( url ) ;
28+ case PROXY_SOCKS5_PROTOCOL : {
29+ let urlSocks = '' ;
30+
31+ if ( url . username && url . password ) {
32+ urlSocks = `socks://${ url . username } :${ url . password } @${ url . hostname } :${ url . port } ` ;
33+ } else {
34+ urlSocks = `socks://${ url . hostname } :${ url . port } ` ;
35+ }
36+
37+ return new SocksProxyAgent ( urlSocks ) ;
38+ }
2739 default :
2840 throw new Error ( `Unsupported proxy protocol: ${ url . protocol } ` ) ;
2941 }
@@ -44,7 +56,7 @@ export function makeProxyAgent(proxy: Proxy | string): HttpsProxyAgent<string> |
4456 return selectProxyAgent ( proxyUrl ) ;
4557}
4658
47- export function makeProxyAgentUndici ( proxy : Proxy | string ) : ProxyAgent {
59+ export function makeProxyAgentUndici ( proxy : Proxy | string ) {
4860 let proxyUrl : string ;
4961 let protocol : string ;
5062
@@ -55,15 +67,14 @@ export function makeProxyAgentUndici(proxy: Proxy | string): ProxyAgent {
5567 } else {
5668 const { host, password, port, protocol : proto , username } = proxy ;
5769 protocol = ( proto || 'http' ) . replace ( ':' , '' ) ;
58-
59- if ( protocol === 'socks' ) {
60- protocol = 'socks5' ;
61- }
70+ if ( protocol === 'socks' ) protocol = 'socks5' ;
6271
6372 const auth = username && password ? `${ username } :${ password } @` : '' ;
6473 proxyUrl = `${ protocol } ://${ auth } ${ host } :${ port } ` ;
6574 }
6675
76+ protocol = protocol . toLowerCase ( ) ;
77+
6778 const PROXY_HTTP_PROTOCOL = 'http' ;
6879 const PROXY_HTTPS_PROTOCOL = 'https' ;
6980 const PROXY_SOCKS4_PROTOCOL = 'socks4' ;
@@ -72,10 +83,25 @@ export function makeProxyAgentUndici(proxy: Proxy | string): ProxyAgent {
7283 switch ( protocol ) {
7384 case PROXY_HTTP_PROTOCOL :
7485 case PROXY_HTTPS_PROTOCOL :
75- case PROXY_SOCKS4_PROTOCOL :
76- case PROXY_SOCKS5_PROTOCOL :
7786 return new ProxyAgent ( proxyUrl ) ;
7887
88+ case PROXY_SOCKS4_PROTOCOL :
89+ case PROXY_SOCKS5_PROTOCOL : {
90+ let type : 4 | 5 = 5 ;
91+
92+ if ( PROXY_SOCKS4_PROTOCOL === protocol ) type = 4 ;
93+
94+ const url = new URL ( proxyUrl ) ;
95+
96+ return socksDispatcher ( {
97+ type : type ,
98+ host : url . hostname ,
99+ port : Number ( url . port ) ,
100+ userId : url . username || undefined ,
101+ password : url . password || undefined ,
102+ } ) ;
103+ }
104+
79105 default :
80106 throw new Error ( `Unsupported proxy protocol: ${ protocol } ` ) ;
81107 }
0 commit comments