Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions scripts/whatsapp-bridge/bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { execSync } from 'child_process';
import { tmpdir } from 'os';
import qrcode from 'qrcode-terminal';
import { matchesAllowedUser, parseAllowedUsers } from './allowlist.js';
import { buildSocketProxyOptions } from './proxy.js';

// Parse CLI args
const args = process.argv.slice(2);
Expand Down Expand Up @@ -196,6 +197,7 @@ async function startSocket() {
// This is enough for Baileys to complete the retry handshake.
return { conversation: '' };
},
...buildSocketProxyOptions(),
});

sock.ev.on('creds.update', () => { saveCreds(); lidToPhone = buildLidMap(); });
Expand Down
46 changes: 46 additions & 0 deletions scripts/whatsapp-bridge/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions scripts/whatsapp-bridge/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"dependencies": {
"@whiskeysockets/baileys": "WhiskeySockets/Baileys#01047debd81beb20da7b7779b08edcb06aa03770",
"express": "^4.21.0",
"https-proxy-agent": "^7.0.6",
"qrcode-terminal": "^0.12.0",
"pino": "^9.0.0"
},
Expand Down
38 changes: 38 additions & 0 deletions scripts/whatsapp-bridge/proxy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { createRequire } from 'node:module';

const require = createRequire(import.meta.url);

const PROXY_ENV_KEYS = [
'HTTPS_PROXY',
'https_proxy',
'HTTP_PROXY',
'http_proxy',
];

export function resolveProxyUrl(env = process.env) {
for (const key of PROXY_ENV_KEYS) {
const value = env?.[key];
if (typeof value === 'string' && value.trim()) {
return value.trim();
}
}
return '';
}

function loadHttpsProxyAgent() {
return require('https-proxy-agent').HttpsProxyAgent;
}

export function createProxyAgent(env = process.env, ProxyAgentCtor = undefined) {
const proxyUrl = resolveProxyUrl(env);
if (!proxyUrl) {
return undefined;
}
const AgentCtor = ProxyAgentCtor || loadHttpsProxyAgent();
return new AgentCtor(proxyUrl);
}

export function buildSocketProxyOptions(env = process.env, ProxyAgentCtor = undefined) {
const agent = createProxyAgent(env, ProxyAgentCtor);
return agent ? { agent } : {};
}
43 changes: 43 additions & 0 deletions scripts/whatsapp-bridge/proxy.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import test from 'node:test';
import assert from 'node:assert/strict';

import { buildSocketProxyOptions, createProxyAgent, resolveProxyUrl } from './proxy.js';

class FakeProxyAgent {
constructor(proxyUrl) {
this.proxyUrl = proxyUrl;
}
}

test('resolveProxyUrl prefers HTTPS proxy variables first', () => {
const env = {
HTTP_PROXY: 'http://http-proxy.example:8080',
HTTPS_PROXY: 'http://https-proxy.example:8443',
};

assert.equal(resolveProxyUrl(env), 'http://https-proxy.example:8443');
});

test('resolveProxyUrl falls back across lowercase and HTTP variants', () => {
assert.equal(
resolveProxyUrl({ http_proxy: 'http://lower-http.example:8080' }),
'http://lower-http.example:8080',
);
assert.equal(
resolveProxyUrl({ HTTP_PROXY: 'http://upper-http.example:8080' }),
'http://upper-http.example:8080',
);
assert.equal(resolveProxyUrl({ HTTPS_PROXY: ' ' }), '');
});

test('createProxyAgent returns undefined when no proxy is configured', () => {
assert.equal(createProxyAgent({}), undefined);
});

test('buildSocketProxyOptions wires an HttpsProxyAgent for Baileys', () => {
const proxyUrl = 'http://proxy.example:3128';
const options = buildSocketProxyOptions({ HTTPS_PROXY: proxyUrl }, FakeProxyAgent);

assert.ok(options.agent instanceof FakeProxyAgent);
assert.equal(options.agent.proxyUrl, proxyUrl);
});
Loading