whatsapp: respect no_proxy in proxy-backed web sessions
This commit is contained in:
parent
46843211c8
commit
2d4a80ccbb
@ -5,14 +5,10 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|||||||
import { resetLogger, setLoggerOverride } from "../../../src/logging.js";
|
import { resetLogger, setLoggerOverride } from "../../../src/logging.js";
|
||||||
import { baileys, getLastSocket, resetBaileysMocks, resetLoadConfigMock } from "./test-helpers.js";
|
import { baileys, getLastSocket, resetBaileysMocks, resetLoadConfigMock } from "./test-helpers.js";
|
||||||
|
|
||||||
const { httpsRequestMock, httpsProxyAgentSpy, undiciProxyAgentSpy } = vi.hoisted(() => ({
|
const { httpsProxyAgentSpy, envHttpProxyAgentSpy, undiciFetchMock } = vi.hoisted(() => ({
|
||||||
httpsRequestMock: vi.fn(),
|
|
||||||
httpsProxyAgentSpy: vi.fn(),
|
httpsProxyAgentSpy: vi.fn(),
|
||||||
undiciProxyAgentSpy: vi.fn(),
|
envHttpProxyAgentSpy: vi.fn(),
|
||||||
}));
|
undiciFetchMock: vi.fn(),
|
||||||
|
|
||||||
vi.mock("node:https", () => ({
|
|
||||||
request: httpsRequestMock,
|
|
||||||
}));
|
}));
|
||||||
|
|
||||||
vi.mock("https-proxy-agent", () => ({
|
vi.mock("https-proxy-agent", () => ({
|
||||||
@ -27,16 +23,17 @@ vi.mock("https-proxy-agent", () => ({
|
|||||||
|
|
||||||
vi.mock("undici", async (importOriginal) => {
|
vi.mock("undici", async (importOriginal) => {
|
||||||
const actual = await importOriginal<typeof import("undici")>();
|
const actual = await importOriginal<typeof import("undici")>();
|
||||||
class MockProxyAgent {
|
class MockEnvHttpProxyAgent {
|
||||||
proxyUrl: string;
|
options?: Record<string, unknown>;
|
||||||
constructor(proxyUrl: string) {
|
constructor(options?: Record<string, unknown>) {
|
||||||
this.proxyUrl = proxyUrl;
|
this.options = options;
|
||||||
undiciProxyAgentSpy(proxyUrl);
|
envHttpProxyAgentSpy(options);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
...actual,
|
...actual,
|
||||||
ProxyAgent: MockProxyAgent,
|
EnvHttpProxyAgent: MockEnvHttpProxyAgent,
|
||||||
|
fetch: undiciFetchMock,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -98,6 +95,8 @@ describe("web session", () => {
|
|||||||
delete process.env.https_proxy;
|
delete process.env.https_proxy;
|
||||||
delete process.env.HTTP_PROXY;
|
delete process.env.HTTP_PROXY;
|
||||||
delete process.env.HTTPS_PROXY;
|
delete process.env.HTTPS_PROXY;
|
||||||
|
delete process.env.no_proxy;
|
||||||
|
delete process.env.NO_PROXY;
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
@ -137,43 +136,54 @@ describe("web session", () => {
|
|||||||
expect(passed?.agent).toBeDefined();
|
expect(passed?.agent).toBeDefined();
|
||||||
expect(passed?.fetchAgent).toBeDefined();
|
expect(passed?.fetchAgent).toBeDefined();
|
||||||
expect(httpsProxyAgentSpy).toHaveBeenCalledWith("http://proxy.test:3128");
|
expect(httpsProxyAgentSpy).toHaveBeenCalledWith("http://proxy.test:3128");
|
||||||
expect(undiciProxyAgentSpy).toHaveBeenCalledWith("http://proxy.test:3128");
|
expect(envHttpProxyAgentSpy).toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("refreshes the WhatsApp Web version through the proxy when Baileys falls back", async () => {
|
it("does not force the websocket through the proxy when NO_PROXY excludes web.whatsapp.com", async () => {
|
||||||
|
process.env.HTTPS_PROXY = "http://proxy.test:3128";
|
||||||
|
process.env.NO_PROXY = "web.whatsapp.com";
|
||||||
|
|
||||||
|
await createWaSocket(false, false);
|
||||||
|
|
||||||
|
const makeWASocket = baileys.makeWASocket as ReturnType<typeof vi.fn>;
|
||||||
|
const passed = makeWASocket.mock.calls[0]?.[0] as
|
||||||
|
| { agent?: unknown; fetchAgent?: unknown }
|
||||||
|
| undefined;
|
||||||
|
|
||||||
|
expect(passed?.agent).toBeUndefined();
|
||||||
|
expect(passed?.fetchAgent).toBeDefined();
|
||||||
|
expect(httpsProxyAgentSpy).not.toHaveBeenCalled();
|
||||||
|
expect(envHttpProxyAgentSpy).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("refreshes the WhatsApp Web version with Baileys headers when Baileys falls back", async () => {
|
||||||
process.env.HTTPS_PROXY = "http://proxy.test:3128";
|
process.env.HTTPS_PROXY = "http://proxy.test:3128";
|
||||||
vi.mocked(baileys.fetchLatestBaileysVersion).mockResolvedValueOnce({
|
vi.mocked(baileys.fetchLatestBaileysVersion).mockResolvedValueOnce({
|
||||||
version: [2, 3000, 1027934701],
|
version: [2, 3000, 1027934701],
|
||||||
isLatest: false,
|
isLatest: false,
|
||||||
});
|
});
|
||||||
httpsRequestMock.mockImplementationOnce(
|
undiciFetchMock.mockResolvedValueOnce({
|
||||||
(
|
ok: true,
|
||||||
_url: string,
|
headers: new Headers(),
|
||||||
_opts: Record<string, unknown>,
|
body: null,
|
||||||
cb: (res: EventEmitter & { statusCode?: number }) => void,
|
text: vi.fn().mockResolvedValue('self.client_revision="1035441841";'),
|
||||||
) => {
|
} as unknown as Response);
|
||||||
const res = new EventEmitter() as EventEmitter & { statusCode?: number };
|
|
||||||
res.statusCode = 200;
|
|
||||||
const req = new EventEmitter() as EventEmitter & {
|
|
||||||
end: () => void;
|
|
||||||
destroy: (_err?: Error) => void;
|
|
||||||
};
|
|
||||||
req.end = () => {
|
|
||||||
cb(res);
|
|
||||||
res.emit("data", 'self.client_revision="1035441841";');
|
|
||||||
res.emit("end");
|
|
||||||
};
|
|
||||||
req.destroy = () => {};
|
|
||||||
return req;
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
await createWaSocket(false, false);
|
await createWaSocket(false, false);
|
||||||
|
|
||||||
const makeWASocket = baileys.makeWASocket as ReturnType<typeof vi.fn>;
|
const makeWASocket = baileys.makeWASocket as ReturnType<typeof vi.fn>;
|
||||||
const passed = makeWASocket.mock.calls[0]?.[0] as { version?: unknown } | undefined;
|
const passed = makeWASocket.mock.calls[0]?.[0] as { version?: unknown } | undefined;
|
||||||
expect(passed?.version).toEqual([2, 3000, 1035441841]);
|
expect(passed?.version).toEqual([2, 3000, 1035441841]);
|
||||||
expect(httpsRequestMock).toHaveBeenCalled();
|
expect(undiciFetchMock).toHaveBeenCalledWith(
|
||||||
|
"https://web.whatsapp.com/sw.js",
|
||||||
|
expect.objectContaining({
|
||||||
|
method: "GET",
|
||||||
|
headers: expect.objectContaining({
|
||||||
|
"sec-fetch-site": "none",
|
||||||
|
"user-agent": expect.stringContaining("Chrome/131.0.0.0"),
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("waits for connection open", async () => {
|
it("waits for connection open", async () => {
|
||||||
|
|||||||
@ -1,6 +1,5 @@
|
|||||||
import { randomUUID } from "node:crypto";
|
import { randomUUID } from "node:crypto";
|
||||||
import fsSync from "node:fs";
|
import fsSync from "node:fs";
|
||||||
import { request as httpsRequest } from "node:https";
|
|
||||||
import {
|
import {
|
||||||
DisconnectReason,
|
DisconnectReason,
|
||||||
fetchLatestBaileysVersion,
|
fetchLatestBaileysVersion,
|
||||||
@ -15,8 +14,9 @@ import { danger, success } from "openclaw/plugin-sdk/runtime-env";
|
|||||||
import { getChildLogger, toPinoLikeLogger } from "openclaw/plugin-sdk/runtime-env";
|
import { getChildLogger, toPinoLikeLogger } from "openclaw/plugin-sdk/runtime-env";
|
||||||
import { ensureDir, resolveUserPath } from "openclaw/plugin-sdk/text-runtime";
|
import { ensureDir, resolveUserPath } from "openclaw/plugin-sdk/text-runtime";
|
||||||
import qrcode from "qrcode-terminal";
|
import qrcode from "qrcode-terminal";
|
||||||
import { ProxyAgent } from "undici";
|
import { EnvHttpProxyAgent } from "undici";
|
||||||
import { resolveEnvHttpProxyUrl } from "../../../src/infra/net/proxy-env.js";
|
import { resolveEnvHttpProxyUrl } from "../../../src/infra/net/proxy-env.js";
|
||||||
|
import { resolveProxyFetchFromEnv } from "../../../src/infra/net/proxy-fetch.js";
|
||||||
import {
|
import {
|
||||||
maybeRestoreCredsFromBackup,
|
maybeRestoreCredsFromBackup,
|
||||||
readCredsJsonRaw,
|
readCredsJsonRaw,
|
||||||
@ -39,6 +39,13 @@ export {
|
|||||||
const credsSaveQueues = new Map<string, Promise<void>>();
|
const credsSaveQueues = new Map<string, Promise<void>>();
|
||||||
const CREDS_SAVE_FLUSH_TIMEOUT_MS = 15_000;
|
const CREDS_SAVE_FLUSH_TIMEOUT_MS = 15_000;
|
||||||
const WHATSAPP_WEB_SW_URL = "https://web.whatsapp.com/sw.js";
|
const WHATSAPP_WEB_SW_URL = "https://web.whatsapp.com/sw.js";
|
||||||
|
const WHATSAPP_WEB_SW_MAX_BYTES = 512 * 1024;
|
||||||
|
const WHATSAPP_WEB_VERSION_HEADERS = {
|
||||||
|
"sec-fetch-site": "none",
|
||||||
|
"user-agent":
|
||||||
|
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
|
||||||
|
} as const;
|
||||||
|
const WHATSAPP_WEB_SOCKET_HOST = "web.whatsapp.com";
|
||||||
|
|
||||||
function enqueueSaveCreds(
|
function enqueueSaveCreds(
|
||||||
authDir: string,
|
authDir: string,
|
||||||
@ -108,40 +115,118 @@ function extractWhatsAppWebVersion(source: string): [number, number, number] | n
|
|||||||
return [2, 3000, revision];
|
return [2, 3000, revision];
|
||||||
}
|
}
|
||||||
|
|
||||||
async function fetchLatestWhatsAppWebVersionViaProxy(
|
function parseNoProxyRules(env: NodeJS.ProcessEnv = process.env): string[] {
|
||||||
proxyUrl: string,
|
const raw = env.no_proxy ?? env.NO_PROXY;
|
||||||
): Promise<[number, number, number] | null> {
|
if (!raw) {
|
||||||
return await new Promise((resolve) => {
|
return [];
|
||||||
const agent = new HttpsProxyAgent(proxyUrl);
|
}
|
||||||
const req = httpsRequest(
|
return raw
|
||||||
WHATSAPP_WEB_SW_URL,
|
.split(",")
|
||||||
{
|
.map((value) => value.trim().toLowerCase())
|
||||||
agent,
|
.filter((value) => value.length > 0);
|
||||||
timeout: 10_000,
|
}
|
||||||
},
|
|
||||||
(res) => {
|
function stripNoProxyPort(rule: string): string {
|
||||||
const chunks: Buffer[] = [];
|
if (rule.startsWith("[") && rule.includes("]")) {
|
||||||
res.on("data", (chunk: string | Buffer) => {
|
const end = rule.indexOf("]");
|
||||||
chunks.push(typeof chunk === "string" ? Buffer.from(chunk) : chunk);
|
return end >= 0 ? rule.slice(0, end + 1) : rule;
|
||||||
});
|
}
|
||||||
res.on("end", () => {
|
const colonIndex = rule.lastIndexOf(":");
|
||||||
const statusCode = typeof res.statusCode === "number" ? res.statusCode : 0;
|
if (colonIndex <= 0 || rule.includes(".")) {
|
||||||
if (statusCode < 200 || statusCode >= 300) {
|
return colonIndex > 0 ? rule.slice(0, colonIndex) : rule;
|
||||||
resolve(null);
|
}
|
||||||
return;
|
return rule;
|
||||||
}
|
}
|
||||||
resolve(extractWhatsAppWebVersion(Buffer.concat(chunks).toString("utf8")));
|
|
||||||
});
|
function shouldBypassEnvProxyForHostname(
|
||||||
},
|
hostname: string,
|
||||||
);
|
env: NodeJS.ProcessEnv = process.env,
|
||||||
req.on("timeout", () => {
|
): boolean {
|
||||||
req.destroy(new Error("Timed out fetching WhatsApp Web version"));
|
const normalizedHost = hostname.trim().toLowerCase();
|
||||||
});
|
if (!normalizedHost) {
|
||||||
req.on("error", () => resolve(null));
|
return false;
|
||||||
req.end();
|
}
|
||||||
|
return parseNoProxyRules(env).some((rawRule) => {
|
||||||
|
if (rawRule === "*") {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
const rule = stripNoProxyPort(rawRule).replace(/^\*\./, ".").toLowerCase();
|
||||||
|
if (!rule) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (rule.startsWith(".")) {
|
||||||
|
const bareRule = rule.slice(1);
|
||||||
|
return normalizedHost === bareRule || normalizedHost.endsWith(rule);
|
||||||
|
}
|
||||||
|
return normalizedHost === rule || normalizedHost.endsWith(`.${rule}`);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function readResponseTextCapped(
|
||||||
|
response: Response,
|
||||||
|
maxBytes: number,
|
||||||
|
): Promise<string | null> {
|
||||||
|
const contentLengthHeader = response.headers.get("content-length");
|
||||||
|
if (contentLengthHeader) {
|
||||||
|
const contentLength = Number.parseInt(contentLengthHeader, 10);
|
||||||
|
if (Number.isFinite(contentLength) && contentLength > maxBytes) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!response.body) {
|
||||||
|
const text = await response.text();
|
||||||
|
return Buffer.byteLength(text, "utf8") <= maxBytes ? text : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const reader = response.body.getReader();
|
||||||
|
const decoder = new TextDecoder();
|
||||||
|
let totalBytes = 0;
|
||||||
|
let text = "";
|
||||||
|
try {
|
||||||
|
while (true) {
|
||||||
|
const { done, value } = await reader.read();
|
||||||
|
if (done) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
totalBytes += value.byteLength;
|
||||||
|
if (totalBytes > maxBytes) {
|
||||||
|
try {
|
||||||
|
await reader.cancel("WhatsApp sw.js response too large");
|
||||||
|
} catch {
|
||||||
|
// ignore reader cancellation errors
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
text += decoder.decode(value, { stream: true });
|
||||||
|
}
|
||||||
|
text += decoder.decode();
|
||||||
|
return text;
|
||||||
|
} finally {
|
||||||
|
reader.releaseLock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function fetchLatestWhatsAppWebVersion(
|
||||||
|
fetchImpl: typeof fetch,
|
||||||
|
): Promise<[number, number, number] | null> {
|
||||||
|
try {
|
||||||
|
const response = await fetchImpl(WHATSAPP_WEB_SW_URL, {
|
||||||
|
method: "GET",
|
||||||
|
headers: WHATSAPP_WEB_VERSION_HEADERS,
|
||||||
|
});
|
||||||
|
if (!response.ok) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
const source = await readResponseTextCapped(response, WHATSAPP_WEB_SW_MAX_BYTES);
|
||||||
|
if (!source) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return extractWhatsAppWebVersion(source);
|
||||||
|
} catch {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function resolveWhatsAppWebVersion(
|
async function resolveWhatsAppWebVersion(
|
||||||
sessionLogger: ReturnType<typeof getChildLogger>,
|
sessionLogger: ReturnType<typeof getChildLogger>,
|
||||||
): Promise<[number, number, number]> {
|
): Promise<[number, number, number]> {
|
||||||
@ -150,12 +235,8 @@ async function resolveWhatsAppWebVersion(
|
|||||||
return latest.version;
|
return latest.version;
|
||||||
}
|
}
|
||||||
|
|
||||||
const proxyUrl = resolveEnvHttpProxyUrl("https");
|
const fetchImpl = resolveProxyFetchFromEnv() ?? globalThis.fetch;
|
||||||
if (!proxyUrl) {
|
const proxyVersion = fetchImpl ? await fetchLatestWhatsAppWebVersion(fetchImpl) : null;
|
||||||
return latest.version;
|
|
||||||
}
|
|
||||||
|
|
||||||
const proxyVersion = await fetchLatestWhatsAppWebVersionViaProxy(proxyUrl);
|
|
||||||
if (!proxyVersion) {
|
if (!proxyVersion) {
|
||||||
return latest.version;
|
return latest.version;
|
||||||
}
|
}
|
||||||
@ -192,8 +273,11 @@ export async function createWaSocket(
|
|||||||
const { state, saveCreds } = await useMultiFileAuthState(authDir);
|
const { state, saveCreds } = await useMultiFileAuthState(authDir);
|
||||||
const version = await resolveWhatsAppWebVersion(sessionLogger);
|
const version = await resolveWhatsAppWebVersion(sessionLogger);
|
||||||
const proxyUrl = resolveEnvHttpProxyUrl("https");
|
const proxyUrl = resolveEnvHttpProxyUrl("https");
|
||||||
const wsAgent = proxyUrl ? new HttpsProxyAgent(proxyUrl) : undefined;
|
const wsAgent =
|
||||||
const fetchAgent = proxyUrl ? new ProxyAgent(proxyUrl) : undefined;
|
proxyUrl && !shouldBypassEnvProxyForHostname(WHATSAPP_WEB_SOCKET_HOST)
|
||||||
|
? new HttpsProxyAgent(proxyUrl)
|
||||||
|
: undefined;
|
||||||
|
const fetchAgent = proxyUrl ? new EnvHttpProxyAgent() : undefined;
|
||||||
const sock = makeWASocket({
|
const sock = makeWASocket({
|
||||||
auth: {
|
auth: {
|
||||||
creds: state.creds,
|
creds: state.creds,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user