From 4a38e505f4936e87d624ac9ca5159e099c5e3792 Mon Sep 17 00:00:00 2001 From: Jeferson Ramos Date: Mon, 27 Oct 2025 10:20:34 -0300 Subject: [PATCH 1/2] =?UTF-8?q?Esse=20erro=20acontece=20porque=20o=20Node.?= =?UTF-8?q?js=20(a=20partir=20da=20vers=C3=A3o=2018)=20usa=20o=20Undici=20?= =?UTF-8?q?como=20implementa=C3=A7=C3=A3o=20nativa=20de=20fetch(),=20e=20o?= =?UTF-8?q?=20Undici=20n=C3=A3o=20aceita=20mais=20objetos=20agent=20tradic?= =?UTF-8?q?ionais=20(como=20os=20criados=20por=20https-proxy-agent=20ou=20?= =?UTF-8?q?socks-proxy-agent).=20Ele=20espera=20objetos=20compat=C3=ADveis?= =?UTF-8?q?=20com=20a=20interface=20moderna=20Dispatcher,=20que=20possuem?= =?UTF-8?q?=20o=20m=C3=A9todo=20dispatch().?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ou seja, o Baileys estava recebendo um tipo de agente incompatível com o novo sistema de rede do Node. Foi criada uma nova função makeProxyAgentUndici() para gerar agentes de proxy compatíveis com o Undici, mantendo a versão antiga (makeProxyAgent()) inalterada para compatibilidade com bibliotecas como Axios. A nova função substitui os antigos HttpsProxyAgent e SocksProxyAgent por ProxyAgent da biblioteca undici, garantindo compatibilidade total com o Baileys e com qualquer uso de fetch() moderno. --- package-lock.json | 10 +++++ package.json | 1 + .../whatsapp/whatsapp.baileys.service.ts | 6 +-- src/utils/makeProxyAgent.ts | 39 +++++++++++++++++++ 4 files changed, 53 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index c9c50513a..c1c6d1809 100644 --- a/package-lock.json +++ b/package-lock.json @@ -65,6 +65,7 @@ "socks-proxy-agent": "^8.0.5", "swagger-ui-express": "^5.0.1", "tsup": "^8.3.5", + "undici": "^7.16.0", "uuid": "^13.0.0" }, "devDependencies": { @@ -15480,6 +15481,15 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/undici": { + "version": "7.16.0", + "resolved": "https://registry.npmjs.org/undici/-/undici-7.16.0.tgz", + "integrity": "sha512-QEg3HPMll0o3t2ourKwOeUAZ159Kn9mx5pnzHRQO8+Wixmh88YdZRiIwat0iNzNNXn0yoEtXJqFpyW7eM8BV7g==", + "license": "MIT", + "engines": { + "node": ">=20.18.1" + } + }, "node_modules/undici-types": { "version": "7.16.0", "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-7.16.0.tgz", diff --git a/package.json b/package.json index 009782b7c..aa7e508fc 100644 --- a/package.json +++ b/package.json @@ -121,6 +121,7 @@ "socks-proxy-agent": "^8.0.5", "swagger-ui-express": "^5.0.1", "tsup": "^8.3.5", + "undici": "^7.16.0", "uuid": "^13.0.0" }, "devDependencies": { diff --git a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts index 1e3bdcf13..2636adbda 100644 --- a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts +++ b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts @@ -82,7 +82,7 @@ import { createId as cuid } from '@paralleldrive/cuid2'; import { Instance, Message } from '@prisma/client'; import { createJid } from '@utils/createJid'; import { fetchLatestWaWebVersion } from '@utils/fetchLatestWaWebVersion'; -import { makeProxyAgent } from '@utils/makeProxyAgent'; +import {makeProxyAgent, makeProxyAgentUndici} from '@utils/makeProxyAgent'; import { getOnWhatsappCache, saveOnWhatsappCache } from '@utils/onWhatsappCache'; import { status } from '@utils/renderStatus'; import { sendTelemetry } from '@utils/sendTelemetry'; @@ -594,7 +594,7 @@ export class BaileysStartupService extends ChannelStartupService { const proxyUrls = text.split('\r\n'); const rand = Math.floor(Math.random() * Math.floor(proxyUrls.length)); const proxyUrl = 'http://' + proxyUrls[rand]; - options = { agent: makeProxyAgent(proxyUrl), fetchAgent: makeProxyAgent(proxyUrl) }; + options = { agent: makeProxyAgent(proxyUrl), fetchAgent: makeProxyAgentUndici(proxyUrl) }; } catch { this.localProxy.enabled = false; } @@ -607,7 +607,7 @@ export class BaileysStartupService extends ChannelStartupService { username: this.localProxy.username, password: this.localProxy.password, }), - fetchAgent: makeProxyAgent({ + fetchAgent: makeProxyAgentUndici({ host: this.localProxy.host, port: this.localProxy.port, protocol: this.localProxy.protocol, diff --git a/src/utils/makeProxyAgent.ts b/src/utils/makeProxyAgent.ts index 3b1379bde..88fe32a95 100644 --- a/src/utils/makeProxyAgent.ts +++ b/src/utils/makeProxyAgent.ts @@ -1,6 +1,8 @@ import { HttpsProxyAgent } from 'https-proxy-agent'; import { SocksProxyAgent } from 'socks-proxy-agent'; +import { ProxyAgent } from 'undici' + type Proxy = { host: string; password?: string; @@ -42,3 +44,40 @@ export function makeProxyAgent(proxy: Proxy | string): HttpsProxyAgent | return selectProxyAgent(proxyUrl); } + +export function makeProxyAgentUndici(proxy: Proxy | string): ProxyAgent { + let proxyUrl: string + let protocol: string + + if (typeof proxy === 'string') { + const url = new URL(proxy) + protocol = url.protocol.replace(':', '') + proxyUrl = proxy + } else { + const { host, password, port, protocol: proto, username } = proxy + protocol = proto.replace(':', '') + + if (protocol === 'socks') { + protocol = 'socks5' + } + + const auth = username && password ? `${username}:${password}@` : '' + proxyUrl = `${protocol}://${auth}${host}:${port}` + } + + const PROXY_HTTP_PROTOCOL = 'http' + const PROXY_HTTPS_PROTOCOL = 'https' + const PROXY_SOCKS4_PROTOCOL = 'socks4' + const PROXY_SOCKS5_PROTOCOL = 'socks5' + + switch (protocol) { + case PROXY_HTTP_PROTOCOL: + case PROXY_HTTPS_PROTOCOL: + case PROXY_SOCKS4_PROTOCOL: + case PROXY_SOCKS5_PROTOCOL: + return new ProxyAgent(proxyUrl) + + default: + throw new Error(`Unsupported proxy protocol: ${protocol}`) + } +} From 3818313161e26912a227989353fb703544661409 Mon Sep 17 00:00:00 2001 From: Jeferson Ramos Date: Mon, 27 Oct 2025 10:40:10 -0300 Subject: [PATCH 2/2] fix: handle undefined protocol in makeProxyAgent --- src/utils/makeProxyAgent.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/makeProxyAgent.ts b/src/utils/makeProxyAgent.ts index 88fe32a95..e882876ef 100644 --- a/src/utils/makeProxyAgent.ts +++ b/src/utils/makeProxyAgent.ts @@ -55,7 +55,7 @@ export function makeProxyAgentUndici(proxy: Proxy | string): ProxyAgent { proxyUrl = proxy } else { const { host, password, port, protocol: proto, username } = proxy - protocol = proto.replace(':', '') + protocol = (proto || 'http').replace(':', '') if (protocol === 'socks') { protocol = 'socks5'