Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(baileys): connect through a SOCKS proxy at an IPv6 literal

Yudhi Armyndharis3a6e6a3119c4
diff --git a/src/engine/adapters/baileys-lifecycle.ts b/src/engine/adapters/baileys-lifecycle.ts
index 065810a62..3eb9782d8 100644
--- a/src/engine/adapters/baileys-lifecycle.ts
+++ b/src/engine/adapters/baileys-lifecycle.ts
@@ -105,7 +105,11 @@ export function createProxyAgent(proxyUrl: string, connectTimeoutMs = PROXY_CONN
return new AbortableHttpsProxyAgent(proxyUrl, connectTimeoutMs);
}
if (protocol === 'socks4:' || protocol === 'socks5:') {
- return new SocksProxyAgent(proxyUrl);
+ const agent = new SocksProxyAgent(proxyUrl);
+ // The library passes URL.hostname through, so an IPv6 literal keeps its brackets and fails as a
+ // DNS lookup of "[::1]". Mutated in place: userId and password are non-enumerable on this object.
+ agent.proxy.host = agent.proxy.host?.replace(/^\[|\]$/g, '');
+ return agent;
}
throw new Error(`Unsupported proxy protocol for the baileys engine: ${protocol}`);
}
diff --git a/src/engine/adapters/baileys.adapter.spec.ts b/src/engine/adapters/baileys.adapter.spec.ts
index 7e181e07d..57ad5d364 100644
--- a/src/engine/adapters/baileys.adapter.spec.ts
+++ b/src/engine/adapters/baileys.adapter.spec.ts
@@ -5730,6 +5730,14 @@ describe('BaileysAdapter proxy support', () => {
expect(createProxyAgent('socks4://proxy.example:1080')).toBeInstanceOf(SocksProxyAgent);
});
+ it('hands the SOCKS client an IPv6 proxy address without its URL brackets', () => {
+ expect((createProxyAgent('socks5://user:pass@[2001:db8::10]:1080') as SocksProxyAgent).proxy.host).toBe(
+ '2001:db8::10',
+ );
+ expect((createProxyAgent('socks4://[::1]:1080') as SocksProxyAgent).proxy.host).toBe('::1');
+ expect((createProxyAgent('socks5://proxy.example:1080') as SocksProxyAgent).proxy.host).toBe('proxy.example');
+ });
+
it('throws on an unsupported proxy scheme', () => {
expect(() => createProxyAgent('ftp://proxy.example:21')).toThrow(/unsupported proxy/i);
});
diff --git a/test/__mocks__/socks-proxy-agent.ts b/test/__mocks__/socks-proxy-agent.ts
index 27e63d6ce..f139bade8 100644
--- a/test/__mocks__/socks-proxy-agent.ts
+++ b/test/__mocks__/socks-proxy-agent.ts
@@ -5,15 +5,19 @@
* graph. The unit suites never open a real proxied socket (the e2e config exercises the real module
* via transformIgnorePatterns), so a class that records the proxy URL and satisfies the `Agent`
* return type of createProxyAgent is sufficient — and because the spec's `instanceof` checks import
- * the same mapped module, they run against this very class.
+ * the same mapped module, they run against this very class. `proxy` mirrors the library's parsed
+ * object (parseSocksURL: `host` is URL.hostname, brackets included for an IPv6 literal).
*/
import { Agent } from 'node:https';
export class SocksProxyAgent extends Agent {
- readonly proxy: string;
+ readonly proxy: { host?: string; port: number; type: 4 | 5 };
+ readonly proxyUrl: string;
constructor(proxy: string | URL) {
super();
- this.proxy = String(proxy);
+ const url = typeof proxy === 'string' ? new URL(proxy) : proxy;
+ this.proxy = { host: url.hostname, port: Number(url.port) || 1080, type: url.protocol === 'socks4:' ? 4 : 5 };
+ this.proxyUrl = url.href;
}
}