This commit is contained in:
Jeferson Ramos 2025-11-19 16:51:59 -03:00
parent d3e3c458a0
commit ea88edd512
3 changed files with 36 additions and 7 deletions

11
package-lock.json generated
View File

@ -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",

View File

@ -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",

View File

@ -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<string> |
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}`);
}