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
12 changes: 9 additions & 3 deletions src/core/engines/noweb/session.noweb.core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import {
} from '@waha/core/engines/noweb/noweb.newsletter';
import { NowebAuthFactoryCore } from '@waha/core/engines/noweb/NowebAuthFactoryCore';
import { NowebInMemoryStore } from '@waha/core/engines/noweb/store/NowebInMemoryStore';
import { resolveWaVersion } from '@waha/core/engines/noweb/utils';
import { NotImplementedByEngineError } from '@waha/core/exceptions';
import { toVcardV3 } from '@waha/core/vcard';
import { createAgentProxy } from '@waha/core/helpers.proxy';
Expand Down Expand Up @@ -355,7 +356,10 @@ export class WhatsappSessionNoWebCore extends WhatsappSession {
await this.sock?.logout();
}

getSocketConfig(agents: Agents | undefined, state): Partial<SocketConfig> {
async getSocketConfig(
agents: Agents | undefined,
state,
): Promise<Partial<SocketConfig>> {
// Detect browser
let browser = ['Ubuntu', 'Chrome', '22.04.4'] as WABrowserDescription;
let deviceName =
Expand Down Expand Up @@ -391,6 +395,7 @@ export class WhatsappSessionNoWebCore extends WhatsappSession {
if (markOnlineOnConnect == undefined) {
markOnlineOnConnect = true;
}
const version = await resolveWaVersion(this.engineLogger);
return {
agent: agents?.socket,
// Baileys media upload uses Node https.request in Node runtime.
Expand All @@ -407,6 +412,7 @@ export class WhatsappSessionNoWebCore extends WhatsappSession {
msgRetryCounterCache: this.msgRetryCounterCache,
placeholderResendCache: this.placeholderResendCache,
markOnlineOnConnect: markOnlineOnConnect,
...(version ? { version } : {}),
};
}

Expand All @@ -425,10 +431,10 @@ export class WhatsappSessionNoWebCore extends WhatsappSession {
}
const { state, saveCreds } = this.authNOWEBStore;
const agents = this.makeProxyAgents();
const socketConfig: SocketConfig = this.getSocketConfig(
const socketConfig: SocketConfig = (await this.getSocketConfig(
agents,
state,
) as SocketConfig;
)) as SocketConfig;
const sock = makeWASocket(socketConfig);
sock.ev.on('creds.update', saveCreds);
return sock;
Expand Down
68 changes: 67 additions & 1 deletion src/core/engines/noweb/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,72 @@
import type { proto } from '@adiwajshing/baileys';
import type { proto, WAVersion } from '@adiwajshing/baileys';
import { fetchLatestBaileysVersion } from '@adiwajshing/baileys';
import type { ILogger } from '@adiwajshing/baileys/lib/Utils/logger';
import esm from '@waha/vendor/esm';

/**
* The WhatsApp Web version baked into the bundled Baileys fork can go stale
* whenever WhatsApp bumps the version its servers accept, breaking every
* NOWEB session at once (connections stuck in STARTING -> FAILED). Resolve
* it in priority order so operators aren't stuck waiting for a WAHA release:
* 1. WAHA_NOWEB_WA_VERSION env var, e.g. "2,3000,1037641644"
* 2. Auto-detected latest version (cached per process; Baileys itself
* falls back to its bundled default if the fetch fails)
*/
const AUTO_VERSION_TTL_MS = 60 * 60 * 1000; // 1h
let cachedAutoVersion: { version: WAVersion; fetchedAt: number } | null =
null;

export async function resolveWaVersion(
logger: ILogger,
): Promise<WAVersion | undefined> {
const envVersion = process.env.WAHA_NOWEB_WA_VERSION;
if (envVersion) {
const parsed = envVersion.split(',').map((part) => Number(part.trim()));
if (parsed.length === 3 && parsed.every((n) => Number.isFinite(n))) {
logger.info(
{ version: parsed },
'Using WhatsApp Web version from WAHA_NOWEB_WA_VERSION',
);
return parsed as WAVersion;
}
logger.warn(
`Ignoring WAHA_NOWEB_WA_VERSION="${envVersion}" - expected "major,minor,patch", e.g. "2,3000,1037641644"`,
);
}

const now = Date.now();
if (
cachedAutoVersion &&
now - cachedAutoVersion.fetchedAt < AUTO_VERSION_TTL_MS
) {
logger.debug(
{ version: cachedAutoVersion.version },
'Using cached auto-detected WhatsApp Web version',
);
return cachedAutoVersion.version;
}

try {
const { version, isLatest } = await fetchLatestBaileysVersion();
cachedAutoVersion = { version, fetchedAt: now };
if (isLatest) {
logger.info({ version }, 'Auto-detected latest WhatsApp Web version');
} else {
logger.warn(
{ version },
'Could not auto-detect the latest WhatsApp Web version, using the bundled default - set WAHA_NOWEB_WA_VERSION to pin one explicitly',
);
}
return version;
} catch (err) {
logger.warn(
{ err },
'Failed to resolve WhatsApp Web version, falling back to the Baileys default',
);
return undefined;
}
}

export function extractMediaContent(
content: any | proto.IMessage | null | undefined,
) {
Expand Down