Skip to content

Commit 8d5c7d8

Browse files
Merge pull request #2240 from JefersonRamos/bugfix/media-upload-failed-on-all-hosts
Bugfix/media upload failed on all hosts
2 parents 13f96a3 + 5c58cb7 commit 8d5c7d8

File tree

3 files changed

+52
-8
lines changed

3 files changed

+52
-8
lines changed

package-lock.json

Lines changed: 17 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: 34 additions & 8 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';
@@ -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

Comments
 (0)