Skip to content

Commit ea88edd

Browse files
committed
socks
1 parent d3e3c45 commit ea88edd

File tree

3 files changed

+36
-7
lines changed

3 files changed

+36
-7
lines changed

package-lock.json

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
"fluent-ffmpeg": "^2.1.3",
9191
"form-data": "^4.0.1",
9292
"https-proxy-agent": "^7.0.6",
93+
"fetch-socks": "^1.3.2",
9394
"i18next": "^23.7.19",
9495
"jimp": "^1.6.0",
9596
"json-schema": "^0.4.0",

src/utils/makeProxyAgent.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { socksDispatcher } from 'fetch-socks';
12
import { HttpsProxyAgent } from 'https-proxy-agent';
23
import { SocksProxyAgent } from 'socks-proxy-agent';
34
import { 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

Comments
 (0)