fix(whatsapp): honor env proxy during QR login

This commit is contained in:
Peter Steinberger
2026-04-27 11:30:20 +01:00
parent f0b327cf68
commit f50fb73560
7 changed files with 69 additions and 17 deletions

View File

@@ -5,6 +5,7 @@
"type": "module",
"dependencies": {
"@whiskeysockets/baileys": "7.0.0-rc.9",
"https-proxy-agent": "^9.0.0",
"jimp": "^1.6.1",
"typebox": "1.1.33",
"undici": "8.1.0"

View File

@@ -204,6 +204,33 @@ describe("web session", () => {
);
});
it("uses lowercase HTTPS proxy before uppercase for WA WebSocket connection", async () => {
vi.stubEnv("HTTPS_PROXY", "http://upper-proxy.test:8080");
vi.stubEnv("https_proxy", "http://lower-proxy.test:8080");
await createWaSocket(false, false);
const passed = (baileys.makeWASocket as ReturnType<typeof vi.fn>).mock.calls[0]?.[0] as {
agent?: { proxy?: URL };
};
expect(passed.agent).toBeDefined();
expect(passed.agent?.proxy?.href).toContain("lower-proxy.test");
});
it("skips WA WebSocket env proxy agent when NO_PROXY covers WhatsApp Web", async () => {
vi.stubEnv("HTTPS_PROXY", "http://proxy.test:8080");
vi.stubEnv("NO_PROXY", "mmg.whatsapp.net");
await createWaSocket(false, false);
const passed = (baileys.makeWASocket as ReturnType<typeof vi.fn>).mock.calls[0]?.[0] as {
agent?: unknown;
fetchAgent?: unknown;
};
expect(passed.agent).toBeUndefined();
expect(passed.fetchAgent).toBeDefined();
});
it("does not create a proxy agent when no env proxy is configured", async () => {
for (const key of [
"ALL_PROXY",

View File

@@ -1,9 +1,13 @@
import { randomUUID } from "node:crypto";
import fsSync from "node:fs";
import type { Agent } from "node:https";
import { HttpsProxyAgent } from "https-proxy-agent";
import { formatCliCommand } from "openclaw/plugin-sdk/cli-runtime";
import { VERSION } from "openclaw/plugin-sdk/cli-runtime";
import { resolveAmbientNodeProxyAgent } from "openclaw/plugin-sdk/extension-shared";
import {
resolveEnvHttpProxyUrl,
shouldUseEnvHttpProxyForUrl,
} from "openclaw/plugin-sdk/infra-runtime";
import { danger, success } from "openclaw/plugin-sdk/runtime-env";
import { getChildLogger, toPinoLikeLogger } from "openclaw/plugin-sdk/runtime-env";
import { ensureDir, resolveUserPath } from "openclaw/plugin-sdk/text-runtime";
@@ -58,6 +62,7 @@ export {
export type { CredsQueueWaitResult } from "./creds-persistence.js";
const LOGGED_OUT_STATUS = DisconnectReason?.loggedOut ?? 401;
const WHATSAPP_WEBSOCKET_PROXY_TARGET = "https://mmg.whatsapp.net/";
const CREDS_FLUSH_TIMEOUT_MESSAGE =
"Queued WhatsApp creds save did not finish before auth bootstrap; skipping repair and continuing with primary creds.";
@@ -210,17 +215,24 @@ export async function createWaSocket(
async function resolveEnvProxyAgent(
logger: ReturnType<typeof getChildLogger>,
): Promise<Agent | undefined> {
return resolveAmbientNodeProxyAgent<Agent>({
onError: (err) => {
logger.warn(
{ error: String(err) },
"Failed to initialize env proxy agent for WhatsApp WebSocket connection",
);
},
onUsingProxy: () => {
logger.info("Using ambient env proxy for WhatsApp WebSocket connection");
},
});
if (!shouldUseEnvHttpProxyForUrl(WHATSAPP_WEBSOCKET_PROXY_TARGET)) {
return undefined;
}
const proxyUrl = resolveEnvHttpProxyUrl("https");
if (!proxyUrl) {
return undefined;
}
try {
const agent = new HttpsProxyAgent(proxyUrl) as Agent;
logger.info("Using ambient env proxy for WhatsApp WebSocket connection");
return agent;
} catch (error) {
logger.warn(
{ error: String(error) },
"Failed to initialize env proxy agent for WhatsApp WebSocket connection",
);
return undefined;
}
}
async function resolveEnvFetchDispatcher(