Skip to content

Commit 3515fa1

Browse files
committed
added: proxy support
Proxy: (http|https|sock\d{1})\n\nProxy usado pelo WebSocket\nWS_PROXY_URL=\nProxy usado para upload/download de mídia\nFETCH_PROXY_URL=\n\n⚠ **As duas variáveis podem conter as mesmas informações**
1 parent 72a4cc3 commit 3515fa1

File tree

5 files changed

+77
-0
lines changed

5 files changed

+77
-0
lines changed

.env.dev

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,13 @@ PROVIDER_HOST=127.0.0.1
8989
PROVIDER_PORT=5656
9090
PROVIDER_PREFIX=codechat
9191

92+
# Proxy: (http|https|sock\d{1})
93+
#
94+
# Proxy usado pelo WebSocket
95+
WS_PROXY_URL=
96+
# Proxy usado para upload/download de mídia
97+
FETCH_PROXY_URL=
98+
9299
# URL em que a documetação será exibida
93100
# EX.: v1.dodmain.com
94101
API_BACKEND=

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"fluent-ffmpeg": "^2.1.3",
5858
"form-data": "^4.0.4",
5959
"hbs": "^4.2.0",
60+
"https-proxy-agent": "^7.0.6",
6061
"js-yaml": "^4.1.0",
6162
"jsonschema": "^1.4.1",
6263
"jsonwebtoken": "^9.0.2",
@@ -71,8 +72,10 @@
7172
"qrcode": "^1.5.1",
7273
"qrcode-terminal": "^0.12.0",
7374
"sharp": "^0.34.2",
75+
"socks-proxy-agent": "^8.0.5",
7476
"swagger-ui-express": "^5.0.1",
7577
"ulid": "^3.0.1",
78+
"undici": "^7.16.0",
7679
"ws": "^8.18.3",
7780
"yamljs": "^0.3.0"
7881
},

src/config/env.config.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ export type GlobalWebhook = { URL: string; ENABLED: boolean };
111111
export type ConfigSessionPhone = { CLIENT: string; NAME: string };
112112
export type QrCodLimit = number;
113113

114+
export type EnvProxy = { WS?: string; FETCH?: string };
115+
114116
export interface Env {
115117
SERVER: HttpServer;
116118
STORE: StoreConf;
@@ -127,6 +129,7 @@ export interface Env {
127129
SESSION_SECRET: string;
128130
S3?: Bucket;
129131
WA_VERSION: string;
132+
PROXY: EnvProxy;
130133
}
131134

132135
export type Key = keyof Env;
@@ -245,6 +248,10 @@ export class ConfigService {
245248
USE_SSL: process.env?.S3_USE_SSL === 'true',
246249
},
247250
WA_VERSION: process.env?.WA_VERSION,
251+
PROXY: {
252+
WS: process.env?.WS_PROXY_URL || null,
253+
FETCH: process.env?.FETCH_PROXY_URL || null,
254+
},
248255
};
249256
}
250257
}

src/utils/proxy.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { HttpsProxyAgent } from 'https-proxy-agent';
2+
import { SocksProxyAgent } from 'socks-proxy-agent';
3+
import { ProxyAgent as UndiciProxyAgent } from 'undici';
4+
5+
export interface ProxyAgents {
6+
wsAgent?: any;
7+
fetchAgent?: any;
8+
}
9+
10+
export function createProxyAgents(
11+
wsProxyUrl?: string,
12+
fetchProxyUrl?: string,
13+
): ProxyAgents {
14+
const agents: ProxyAgents = {};
15+
16+
const detectProtocol = (url: string) => url.split(':')[0].toLowerCase();
17+
18+
// WS
19+
if (wsProxyUrl) {
20+
const proto = detectProtocol(wsProxyUrl);
21+
switch (proto) {
22+
case 'http':
23+
case 'https':
24+
agents.wsAgent = new HttpsProxyAgent(wsProxyUrl);
25+
break;
26+
case 'socks':
27+
case 'socks4':
28+
case 'socks5':
29+
agents.wsAgent = new SocksProxyAgent(wsProxyUrl);
30+
break;
31+
default:
32+
console.warn(`[Proxy] Protocolo desconhecido para WS: ${proto}`);
33+
break;
34+
}
35+
}
36+
37+
// FETCH
38+
if (fetchProxyUrl) {
39+
const proto = detectProtocol(fetchProxyUrl);
40+
switch (proto) {
41+
case 'http':
42+
case 'https':
43+
case 'socks':
44+
case 'socks4':
45+
case 'socks5':
46+
agents.fetchAgent = new UndiciProxyAgent(fetchProxyUrl);
47+
break;
48+
default:
49+
console.warn(`[Proxy] Protocolo desconhecido para Fetch: ${proto}`);
50+
break;
51+
}
52+
}
53+
54+
return agents;
55+
}

src/whatsapp/services/whatsapp.service.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,9 @@ export class WAStartupService {
522522
const { EXPIRATION_TIME } = this.configService.get<QrCode>('QRCODE');
523523
const CONNECTION_TIMEOUT = this.configService.get<number>('CONNECTION_TIMEOUT');
524524

525+
const proxy = this.configService.get<EnvProxy>('PROXY');
526+
const agents = createProxyAgents(proxy?.WS, proxy?.FETCH);
527+
525528
const socketConfig: UserFacingSocketConfig = {
526529
auth: {
527530
creds: this.authState.state.creds,
@@ -530,6 +533,8 @@ export class WAStartupService {
530533
P({ level: 'silent' }) as any,
531534
),
532535
},
536+
agent: agents?.wsAgent,
537+
fetchAgent: agents?.fetchAgent,
533538
logger: P({ level: 'silent' }) as any,
534539
printQRInTerminal: false,
535540
browser,

0 commit comments

Comments
 (0)