Skip to content

Commit c555048

Browse files
Merge pull request #2155 from gomessguii/develop
Fix merge
2 parents d5f5b83 + 2d14c88 commit c555048

File tree

4 files changed

+53
-3
lines changed

4 files changed

+53
-3
lines changed

package-lock.json

Lines changed: 10 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
@@ -121,6 +121,7 @@
121121
"socks-proxy-agent": "^8.0.5",
122122
"swagger-ui-express": "^5.0.1",
123123
"tsup": "^8.3.5",
124+
"undici": "^7.16.0",
124125
"uuid": "^13.0.0"
125126
},
126127
"devDependencies": {

src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ import { createId as cuid } from '@paralleldrive/cuid2';
8282
import { Instance, Message } from '@prisma/client';
8383
import { createJid } from '@utils/createJid';
8484
import { fetchLatestWaWebVersion } from '@utils/fetchLatestWaWebVersion';
85-
import { makeProxyAgent } from '@utils/makeProxyAgent';
85+
import {makeProxyAgent, makeProxyAgentUndici} from '@utils/makeProxyAgent';
8686
import { getOnWhatsappCache, saveOnWhatsappCache } from '@utils/onWhatsappCache';
8787
import { status } from '@utils/renderStatus';
8888
import { sendTelemetry } from '@utils/sendTelemetry';
@@ -594,7 +594,7 @@ export class BaileysStartupService extends ChannelStartupService {
594594
const proxyUrls = text.split('\r\n');
595595
const rand = Math.floor(Math.random() * Math.floor(proxyUrls.length));
596596
const proxyUrl = 'http://' + proxyUrls[rand];
597-
options = { agent: makeProxyAgent(proxyUrl), fetchAgent: makeProxyAgent(proxyUrl) };
597+
options = { agent: makeProxyAgent(proxyUrl), fetchAgent: makeProxyAgentUndici(proxyUrl) };
598598
} catch {
599599
this.localProxy.enabled = false;
600600
}
@@ -607,7 +607,7 @@ export class BaileysStartupService extends ChannelStartupService {
607607
username: this.localProxy.username,
608608
password: this.localProxy.password,
609609
}),
610-
fetchAgent: makeProxyAgent({
610+
fetchAgent: makeProxyAgentUndici({
611611
host: this.localProxy.host,
612612
port: this.localProxy.port,
613613
protocol: this.localProxy.protocol,

src/utils/makeProxyAgent.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { HttpsProxyAgent } from 'https-proxy-agent';
22
import { SocksProxyAgent } from 'socks-proxy-agent';
33

4+
import { ProxyAgent } from 'undici'
5+
46
type Proxy = {
57
host: string;
68
password?: string;
@@ -42,3 +44,40 @@ export function makeProxyAgent(proxy: Proxy | string): HttpsProxyAgent<string> |
4244

4345
return selectProxyAgent(proxyUrl);
4446
}
47+
48+
export function makeProxyAgentUndici(proxy: Proxy | string): ProxyAgent {
49+
let proxyUrl: string
50+
let protocol: string
51+
52+
if (typeof proxy === 'string') {
53+
const url = new URL(proxy)
54+
protocol = url.protocol.replace(':', '')
55+
proxyUrl = proxy
56+
} else {
57+
const { host, password, port, protocol: proto, username } = proxy
58+
protocol = (proto || 'http').replace(':', '')
59+
60+
if (protocol === 'socks') {
61+
protocol = 'socks5'
62+
}
63+
64+
const auth = username && password ? `${username}:${password}@` : ''
65+
proxyUrl = `${protocol}://${auth}${host}:${port}`
66+
}
67+
68+
const PROXY_HTTP_PROTOCOL = 'http'
69+
const PROXY_HTTPS_PROTOCOL = 'https'
70+
const PROXY_SOCKS4_PROTOCOL = 'socks4'
71+
const PROXY_SOCKS5_PROTOCOL = 'socks5'
72+
73+
switch (protocol) {
74+
case PROXY_HTTP_PROTOCOL:
75+
case PROXY_HTTPS_PROTOCOL:
76+
case PROXY_SOCKS4_PROTOCOL:
77+
case PROXY_SOCKS5_PROTOCOL:
78+
return new ProxyAgent(proxyUrl)
79+
80+
default:
81+
throw new Error(`Unsupported proxy protocol: ${protocol}`)
82+
}
83+
}

0 commit comments

Comments
 (0)