Skip to content
Open
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
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,12 @@ docker compose down
#### Web Interface

```bash
# Terminal 1: Start backend
# Terminal 1.1: Start backend (WhatsApp only)
npm run start:server

# Terminal 1.2: Start backend (WhatsApp + Signal) Docker needed
npm run start:server:signal

# Terminal 2: Start frontend
npm run start:client
```
Expand Down
2 changes: 1 addition & 1 deletion client/src/components/ContactCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ export function ContactCard({
<XAxis dataKey="timestamp" hide />
<YAxis domain={['auto', 'auto']} />
<Tooltip
labelFormatter={(t: number) => new Date(t).toLocaleTimeString()}
labelFormatter={(t: any) => new Date(t).toLocaleTimeString()}
contentStyle={{ borderRadius: '8px', border: 'none', boxShadow: '0 4px 6px -1px rgb(0 0 0 / 0.1)' }}
/>
<Line type="monotone" dataKey="avg" stroke="#3b82f6" strokeWidth={2} dot={false} name="Avg RTT" isAnimationActive={false} />
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"scripts": {
"build": "tsc",
"start": "npx tsx src/index.ts",
"start:server": "npm run start:signal-api && npx tsx src/server.ts",
"start:server": "npx tsx src/server.ts",
"start:server:signal": "npm run start:signal-api && npx tsx src/server.ts",
"start:signal-api": "npx tsx src/scripts/init-signal.ts",
"start:client": "cd client && npm start",
"dev": "npm run start:server",
Expand All @@ -54,4 +55,4 @@
"typescript": "^5.7.2"
},
"private": true
}
}
47 changes: 28 additions & 19 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ const originalStdoutWrite = process.stdout.write.bind(process.stdout);
// prevents Baileys from spamming the console
const shouldSuppressOutput = (message: string): boolean => {
return message.includes('Closing session:') ||
message.includes('SessionEntry') ||
message.includes('_chains') ||
message.includes('registrationId') ||
message.includes('currentRatchet') ||
message.includes('ephemeralKeyPair') ||
message.includes('pendingPreKey') ||
message.includes('indexInfo') ||
message.includes('baseKey') ||
message.includes('remoteIdentityKey') ||
message.includes('lastRemoteEphemeralKey') ||
message.includes('previousCounter') ||
message.includes('rootKey') ||
message.includes('signedKeyId') ||
message.includes('preKeyId') ||
message.includes('<Buffer');
message.includes('SessionEntry') ||
message.includes('_chains') ||
message.includes('registrationId') ||
message.includes('currentRatchet') ||
message.includes('ephemeralKeyPair') ||
message.includes('pendingPreKey') ||
message.includes('indexInfo') ||
message.includes('baseKey') ||
message.includes('remoteIdentityKey') ||
message.includes('lastRemoteEphemeralKey') ||
message.includes('previousCounter') ||
message.includes('rootKey') ||
message.includes('signedKeyId') ||
message.includes('preKeyId') ||
message.includes('<Buffer');
};

if (!debugMode) {
Expand Down Expand Up @@ -58,7 +58,7 @@ if (!debugMode) {

// Now safe to import modules
import '@whiskeysockets/baileys';
import makeWASocket, { DisconnectReason, useMultiFileAuthState } from '@whiskeysockets/baileys';
import makeWASocket, { DisconnectReason, useMultiFileAuthState, fetchLatestBaileysVersion } from '@whiskeysockets/baileys';
import { pino } from 'pino';
import { Boom } from '@hapi/boom';
import qrcode from 'qrcode-terminal';
Expand All @@ -77,11 +77,17 @@ let currentTracker: WhatsAppTracker | null = null;

async function connectToWhatsApp() {
const { state, saveCreds } = await useMultiFileAuthState('auth_info_baileys');
const { version, isLatest } = await fetchLatestBaileysVersion();
if (debugMode) {
originalConsoleLog(`Using WA v${version.join('.')}, isLatest: ${isLatest}`);
}

const sock = makeWASocket({
version,
auth: state,
logger: pino({ level: 'silent' }),
markOnlineOnConnect: true,
browser: ['Windows', 'Chrome', '120.0.0'],
});

originalConsoleLog('🔌 Connecting to WhatsApp... (use the --debug flag for more details)');
Expand All @@ -103,12 +109,15 @@ async function connectToWhatsApp() {
currentTracker = null;
}

const shouldReconnect = (lastDisconnect?.error as Boom)?.output?.statusCode !== DisconnectReason.loggedOut;
const statusCode = (lastDisconnect?.error as Boom)?.output?.statusCode;
const shouldReconnect = statusCode !== DisconnectReason.loggedOut && statusCode !== 405;
if (debugMode) {
originalConsoleLog('connection closed due to ', lastDisconnect?.error, ', reconnecting ', shouldReconnect);
originalConsoleLog(`connection closed due to error (Status: ${statusCode}): `, lastDisconnect?.error, ', reconnecting ', shouldReconnect);
}
if (shouldReconnect) {
connectToWhatsApp();
setTimeout(() => connectToWhatsApp(), 3000);
} else {
originalConsoleLog('⚠️ Session logged out. Please completely stop the process, delete auth_info_baileys, and restart.');
}
} else if (connection === 'open') {
originalConsoleLog('✅ Connected to WhatsApp');
Expand Down
15 changes: 11 additions & 4 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import express from 'express';
import { createServer } from 'http';
import { Server } from 'socket.io';
import cors from 'cors';
import makeWASocket, { DisconnectReason, useMultiFileAuthState } from '@whiskeysockets/baileys';
import makeWASocket, { DisconnectReason, useMultiFileAuthState, fetchLatestBaileysVersion } from '@whiskeysockets/baileys';
import { pino } from 'pino';
import { Boom } from '@hapi/boom';
import { WhatsAppTracker, ProbeMethod } from './tracker.js';
Expand Down Expand Up @@ -50,12 +50,16 @@ const trackers: Map<string, TrackerEntry> = new Map(); // JID/Number -> Tracker

async function connectToWhatsApp() {
const { state, saveCreds } = await useMultiFileAuthState('auth_info_baileys');
const { version, isLatest } = await fetchLatestBaileysVersion();
console.log(`Using WA v${version.join('.')}, isLatest: ${isLatest}`);

sock = makeWASocket({
version,
auth: state,
logger: pino({ level: 'debug' }),
markOnlineOnConnect: true,
printQRInTerminal: false,
browser: ['Windows', 'Chrome', '120.0.0'],
});

sock.ev.on('connection.update', async (update: any) => {
Expand All @@ -70,10 +74,13 @@ async function connectToWhatsApp() {
if (connection === 'close') {
isWhatsAppConnected = false;
currentWhatsAppQr = null; // Clear QR on close
const shouldReconnect = (lastDisconnect?.error as Boom)?.output?.statusCode !== DisconnectReason.loggedOut;
console.log('connection closed, reconnecting ', shouldReconnect);
const statusCode = (lastDisconnect?.error as Boom)?.output?.statusCode;
const shouldReconnect = statusCode !== DisconnectReason.loggedOut && statusCode !== 405;
console.log(`Connection closed (Status: ${statusCode}). Reconnecting: ${shouldReconnect}`);
if (shouldReconnect) {
connectToWhatsApp();
setTimeout(() => connectToWhatsApp(), 3000);
} else {
console.log('⚠️ Session logged out. Please completely stop the server, delete auth_info_baileys, and restart.');
}
} else if (connection === 'open') {
isWhatsAppConnected = true;
Expand Down