diff --git a/package-lock.json b/package-lock.json index c1c6d180..a4481cdc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -31,6 +31,7 @@ "eventemitter2": "^6.4.9", "express": "^4.21.2", "express-async-errors": "^3.1.1", + "fetch-socks": "^1.3.2", "fluent-ffmpeg": "^2.1.3", "form-data": "^4.0.1", "https-proxy-agent": "^7.0.6", @@ -8628,6 +8629,16 @@ "reusify": "^1.0.4" } }, + "node_modules/fetch-socks": { + "version": "1.3.2", + "resolved": "https://registry.npmjs.org/fetch-socks/-/fetch-socks-1.3.2.tgz", + "integrity": "sha512-vkH5+Zgj2yEbU57Cei0iyLgTZ4OkEKJj56Xu3ViB5dpsl599JgEooQ3x6NVagIFRHWnWJ+7K0MO0aIV1TMgvnw==", + "license": "MIT", + "dependencies": { + "socks": "^2.8.2", + "undici": ">=6" + } + }, "node_modules/fflate": { "version": "0.8.2", "resolved": "https://registry.npmjs.org/fflate/-/fflate-0.8.2.tgz", diff --git a/package.json b/package.json index aa7e508f..2e1f239c 100644 --- a/package.json +++ b/package.json @@ -90,6 +90,7 @@ "fluent-ffmpeg": "^2.1.3", "form-data": "^4.0.1", "https-proxy-agent": "^7.0.6", + "fetch-socks": "^1.3.2", "i18next": "^23.7.19", "jimp": "^1.6.0", "json-schema": "^0.4.0", diff --git a/src/utils/makeProxyAgent.ts b/src/utils/makeProxyAgent.ts index c2848292..c2f5affa 100644 --- a/src/utils/makeProxyAgent.ts +++ b/src/utils/makeProxyAgent.ts @@ -1,3 +1,4 @@ +import { socksDispatcher } from 'fetch-socks'; import { HttpsProxyAgent } from 'https-proxy-agent'; import { SocksProxyAgent } from 'socks-proxy-agent'; import { ProxyAgent } from 'undici'; @@ -46,7 +47,7 @@ export function makeProxyAgent(proxy: Proxy | string): HttpsProxyAgent | return selectProxyAgent(proxyUrl); } -export function makeProxyAgentUndici(proxy: Proxy | string): ProxyAgent | SocksProxyAgent { +export function makeProxyAgentUndici(proxy: Proxy | string) { let proxyUrl: string; let protocol: string; @@ -57,15 +58,15 @@ export function makeProxyAgentUndici(proxy: Proxy | string): ProxyAgent | SocksP } else { const { host, password, port, protocol: proto, username } = proxy; protocol = (proto || 'http').replace(':', ''); - - if (protocol === 'socks') { - protocol = 'socks5'; - } + if (protocol === 'socks') protocol = 'socks5'; const auth = username && password ? `${username}:${password}@` : ''; proxyUrl = `${protocol}://${auth}${host}:${port}`; } + // Normalização + protocol = protocol.toLowerCase(); + const PROXY_HTTP_PROTOCOL = 'http'; const PROXY_HTTPS_PROTOCOL = 'https'; const PROXY_SOCKS4_PROTOCOL = 'socks4'; @@ -74,10 +75,26 @@ export function makeProxyAgentUndici(proxy: Proxy | string): ProxyAgent | SocksP switch (protocol) { case PROXY_HTTP_PROTOCOL: case PROXY_HTTPS_PROTOCOL: + // Proxy HTTP/HTTPS → usar ProxyAgent do Undici return new ProxyAgent(proxyUrl); + case PROXY_SOCKS4_PROTOCOL: - case PROXY_SOCKS5_PROTOCOL: - return new SocksProxyAgent(proxyUrl); + case PROXY_SOCKS5_PROTOCOL: { + let type: 4 | 5 = 5; + + if (PROXY_SOCKS4_PROTOCOL === protocol) type = 4; + + const url = new URL(proxyUrl); + + return socksDispatcher({ + type: type, + host: url.hostname, + port: Number(url.port), + userId: url.username || undefined, + password: url.password || undefined, + }); + } + default: throw new Error(`Unsupported proxy protocol: ${protocol}`); }