WhatsApp/Signal/Formstack/admin updates
This commit is contained in:
parent
bcecf61a46
commit
d0cc5a21de
451 changed files with 16139 additions and 39623 deletions
|
|
@ -9,6 +9,9 @@ import {
|
|||
SendMessageRoute,
|
||||
ReceiveMessageRoute,
|
||||
} from "./routes.js";
|
||||
import { createLogger } from "@link-stack/logger";
|
||||
|
||||
const logger = createLogger('bridge-whatsapp-index');
|
||||
|
||||
const server = Hapi.server({ port: 5000 });
|
||||
|
||||
|
|
@ -34,6 +37,6 @@ const main = async () => {
|
|||
};
|
||||
|
||||
main().catch((err) => {
|
||||
console.error(err);
|
||||
logger.error(err);
|
||||
process.exit(1);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ const getService = (request: Hapi.Request): WhatsappService => {
|
|||
interface MessageRequest {
|
||||
phoneNumber: string;
|
||||
message: string;
|
||||
attachments?: Array<{ data: string; filename: string; mime_type: string }>;
|
||||
}
|
||||
|
||||
export const SendMessageRoute = withDefaults({
|
||||
|
|
@ -26,11 +27,23 @@ export const SendMessageRoute = withDefaults({
|
|||
description: "Send a message",
|
||||
async handler(request: Hapi.Request, _h: Hapi.ResponseToolkit) {
|
||||
const { id } = request.params;
|
||||
console.log({ payload: request.payload });
|
||||
const { phoneNumber, message } = request.payload as MessageRequest;
|
||||
const { phoneNumber, message, attachments } =
|
||||
request.payload as MessageRequest;
|
||||
const whatsappService = getService(request);
|
||||
await whatsappService.send(id, phoneNumber, message as string);
|
||||
request.logger.info({ id }, "Sent a message at %s", new Date());
|
||||
await whatsappService.send(
|
||||
id,
|
||||
phoneNumber,
|
||||
message as string,
|
||||
attachments,
|
||||
);
|
||||
request.logger.info(
|
||||
{
|
||||
id,
|
||||
attachmentCount: attachments?.length || 0,
|
||||
},
|
||||
"Sent a message at %s",
|
||||
new Date().toISOString(),
|
||||
);
|
||||
|
||||
return _h
|
||||
.response({
|
||||
|
|
@ -56,7 +69,7 @@ export const ReceiveMessageRoute = withDefaults({
|
|||
const date = new Date();
|
||||
const twoDaysAgo = new Date(date.getTime());
|
||||
twoDaysAgo.setDate(date.getDate() - 2);
|
||||
request.logger.info({ id }, "Received messages at %s", new Date());
|
||||
request.logger.info({ id }, "Received messages at %s", new Date().toISOString());
|
||||
|
||||
return whatsappService.receive(id, twoDaysAgo);
|
||||
},
|
||||
|
|
|
|||
|
|
@ -11,6 +11,14 @@ import makeWASocket, {
|
|||
useMultiFileAuthState,
|
||||
} from "@whiskeysockets/baileys";
|
||||
import fs from "fs";
|
||||
import { createLogger } from "@link-stack/logger";
|
||||
import {
|
||||
getMaxAttachmentSize,
|
||||
getMaxTotalAttachmentSize,
|
||||
MAX_ATTACHMENTS,
|
||||
} from "@link-stack/bridge-common";
|
||||
|
||||
const logger = createLogger("bridge-whatsapp-service");
|
||||
|
||||
export type AuthCompleteCallback = (error?: string) => void;
|
||||
|
||||
|
|
@ -33,7 +41,24 @@ export default class WhatsappService extends Service {
|
|||
}
|
||||
|
||||
getBotDirectory(id: string): string {
|
||||
return `${this.getBaseDirectory()}/${id}`;
|
||||
// Validate that ID contains only safe characters (alphanumeric, dash, underscore)
|
||||
if (!/^[a-zA-Z0-9_-]+$/.test(id)) {
|
||||
throw new Error(`Invalid bot ID format: ${id}`);
|
||||
}
|
||||
|
||||
// Prevent path traversal by checking for suspicious patterns
|
||||
if (id.includes('..') || id.includes('/') || id.includes('\\')) {
|
||||
throw new Error(`Path traversal detected in bot ID: ${id}`);
|
||||
}
|
||||
|
||||
const botPath = `${this.getBaseDirectory()}/${id}`;
|
||||
|
||||
// Ensure the resolved path is still within the base directory
|
||||
if (!botPath.startsWith(this.getBaseDirectory())) {
|
||||
throw new Error(`Invalid bot path: ${botPath}`);
|
||||
}
|
||||
|
||||
return botPath;
|
||||
}
|
||||
|
||||
getAuthDirectory(id: string): string {
|
||||
|
|
@ -57,7 +82,7 @@ export default class WhatsappService extends Service {
|
|||
try {
|
||||
connection.end(null);
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
logger.error({ error }, "Connection reset error");
|
||||
}
|
||||
}
|
||||
this.connections = {};
|
||||
|
|
@ -92,27 +117,27 @@ export default class WhatsappService extends Service {
|
|||
isNewLogin,
|
||||
} = update;
|
||||
if (qr) {
|
||||
console.log("got qr code");
|
||||
logger.info("got qr code");
|
||||
const botDirectory = this.getBotDirectory(botID);
|
||||
const qrPath = `${botDirectory}/qr.txt`;
|
||||
fs.writeFileSync(qrPath, qr, "utf8");
|
||||
} else if (isNewLogin) {
|
||||
console.log("got new login");
|
||||
logger.info("got new login");
|
||||
const botDirectory = this.getBotDirectory(botID);
|
||||
const verifiedFile = `${botDirectory}/verified`;
|
||||
fs.writeFileSync(verifiedFile, "");
|
||||
} else if (connectionState === "open") {
|
||||
console.log("opened connection");
|
||||
logger.info("opened connection");
|
||||
} else if (connectionState === "close") {
|
||||
console.log("connection closed due to ", lastDisconnect?.error);
|
||||
logger.info({ lastDisconnect }, "connection closed");
|
||||
const disconnectStatusCode = (lastDisconnect?.error as any)?.output
|
||||
?.statusCode;
|
||||
if (disconnectStatusCode === DisconnectReason.restartRequired) {
|
||||
console.log("reconnecting after got new login");
|
||||
logger.info("reconnecting after got new login");
|
||||
await this.createConnection(botID, server, options);
|
||||
authCompleteCallback?.();
|
||||
} else if (disconnectStatusCode !== DisconnectReason.loggedOut) {
|
||||
console.log("reconnecting");
|
||||
logger.info("reconnecting");
|
||||
await this.sleep(pause);
|
||||
pause *= 2;
|
||||
this.createConnection(botID, server, options);
|
||||
|
|
@ -121,12 +146,12 @@ export default class WhatsappService extends Service {
|
|||
}
|
||||
|
||||
if (events["creds.update"]) {
|
||||
console.log("creds update");
|
||||
logger.info("creds update");
|
||||
await saveCreds();
|
||||
}
|
||||
|
||||
if (events["messages.upsert"]) {
|
||||
console.log("messages upsert");
|
||||
logger.info("messages upsert");
|
||||
const upsert = events["messages.upsert"];
|
||||
const { messages } = upsert;
|
||||
if (messages) {
|
||||
|
|
@ -143,13 +168,16 @@ export default class WhatsappService extends Service {
|
|||
|
||||
const baseDirectory = this.getBaseDirectory();
|
||||
const botIDs = fs.readdirSync(baseDirectory);
|
||||
console.log({ botIDs });
|
||||
|
||||
for await (const botID of botIDs) {
|
||||
const directory = this.getBotDirectory(botID);
|
||||
const verifiedFile = `${directory}/verified`;
|
||||
if (fs.existsSync(verifiedFile)) {
|
||||
const { version, isLatest } = await fetchLatestBaileysVersion();
|
||||
console.log(`using WA v${version.join(".")}, isLatest: ${isLatest}`);
|
||||
logger.info(
|
||||
{ version: version.join("."), isLatest },
|
||||
"using WA version",
|
||||
);
|
||||
|
||||
await this.createConnection(botID, this.server, {
|
||||
browser: WhatsappService.browserDescription,
|
||||
|
|
@ -169,7 +197,13 @@ export default class WhatsappService extends Service {
|
|||
message,
|
||||
messageTimestamp,
|
||||
} = webMessageInfo;
|
||||
console.log(webMessageInfo);
|
||||
logger.info("Message type debug");
|
||||
for (const key in message) {
|
||||
logger.info(
|
||||
{ key, exists: !!message[key as keyof proto.IMessage] },
|
||||
"Message field",
|
||||
);
|
||||
}
|
||||
const isValidMessage =
|
||||
message && remoteJid !== "status@broadcast" && !fromMe;
|
||||
if (isValidMessage) {
|
||||
|
|
@ -186,19 +220,22 @@ export default class WhatsappService extends Service {
|
|||
if (isMediaMessage) {
|
||||
if (audioMessage) {
|
||||
messageType = "audio";
|
||||
filename = id + "." + audioMessage.mimetype?.split("/").pop();
|
||||
const extension = audioMessage.mimetype?.split("/").pop() || "audio";
|
||||
filename = `${id}.${extension}`;
|
||||
mimeType = audioMessage.mimetype;
|
||||
} else if (documentMessage) {
|
||||
messageType = "document";
|
||||
filename = documentMessage.fileName;
|
||||
filename = documentMessage.fileName || `${id}.bin`;
|
||||
mimeType = documentMessage.mimetype;
|
||||
} else if (imageMessage) {
|
||||
messageType = "image";
|
||||
filename = id + "." + imageMessage.mimetype?.split("/").pop();
|
||||
const extension = imageMessage.mimetype?.split("/").pop() || "jpg";
|
||||
filename = `${id}.${extension}`;
|
||||
mimeType = imageMessage.mimetype;
|
||||
} else if (videoMessage) {
|
||||
messageType = "video";
|
||||
filename = id + "." + videoMessage.mimetype?.split("/").pop();
|
||||
const extension = videoMessage.mimetype?.split("/").pop() || "mp4";
|
||||
filename = `${id}.${extension}`;
|
||||
mimeType = videoMessage.mimetype;
|
||||
}
|
||||
|
||||
|
|
@ -271,8 +308,30 @@ export default class WhatsappService extends Service {
|
|||
}
|
||||
|
||||
async unverify(botID: string): Promise<void> {
|
||||
// Step 1: Close and remove the active connection if it exists
|
||||
const connection = this.connections[botID];
|
||||
if (connection?.socket) {
|
||||
try {
|
||||
// Properly close the WebSocket connection
|
||||
await connection.socket.logout();
|
||||
} catch (error) {
|
||||
logger.warn({ botID, error }, "Error during logout, forcing disconnect");
|
||||
try {
|
||||
connection.socket.end(undefined);
|
||||
} catch (endError) {
|
||||
logger.warn({ botID, endError }, "Error ending socket connection");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Step 2: Remove from in-memory connections
|
||||
delete this.connections[botID];
|
||||
|
||||
// Step 3: Remove the bot directory (auth state, QR code, verified marker)
|
||||
const botDirectory = this.getBotDirectory(botID);
|
||||
fs.rmSync(botDirectory, { recursive: true, force: true });
|
||||
if (fs.existsSync(botDirectory)) {
|
||||
fs.rmSync(botDirectory, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
async register(
|
||||
|
|
@ -293,10 +352,75 @@ export default class WhatsappService extends Service {
|
|||
botID: string,
|
||||
phoneNumber: string,
|
||||
message: string,
|
||||
attachments?: Array<{ data: string; filename: string; mime_type: string }>,
|
||||
): Promise<void> {
|
||||
const connection = this.connections[botID]?.socket;
|
||||
const recipient = `${phoneNumber.replace(/\D+/g, "")}@s.whatsapp.net`;
|
||||
await connection.sendMessage(recipient, { text: message });
|
||||
|
||||
// Send text message if provided
|
||||
if (message) {
|
||||
await connection.sendMessage(recipient, { text: message });
|
||||
}
|
||||
|
||||
// Send attachments if provided with size validation
|
||||
if (attachments && attachments.length > 0) {
|
||||
const MAX_ATTACHMENT_SIZE = getMaxAttachmentSize();
|
||||
const MAX_TOTAL_SIZE = getMaxTotalAttachmentSize();
|
||||
|
||||
if (attachments.length > MAX_ATTACHMENTS) {
|
||||
throw new Error(`Too many attachments: ${attachments.length} (max ${MAX_ATTACHMENTS})`);
|
||||
}
|
||||
|
||||
let totalSize = 0;
|
||||
|
||||
for (const attachment of attachments) {
|
||||
// Calculate size before converting to buffer
|
||||
const estimatedSize = (attachment.data.length * 3) / 4;
|
||||
|
||||
if (estimatedSize > MAX_ATTACHMENT_SIZE) {
|
||||
logger.warn({
|
||||
filename: attachment.filename,
|
||||
size: estimatedSize,
|
||||
maxSize: MAX_ATTACHMENT_SIZE
|
||||
}, 'Attachment exceeds size limit, skipping');
|
||||
continue;
|
||||
}
|
||||
|
||||
totalSize += estimatedSize;
|
||||
if (totalSize > MAX_TOTAL_SIZE) {
|
||||
logger.warn({
|
||||
totalSize,
|
||||
maxTotalSize: MAX_TOTAL_SIZE
|
||||
}, 'Total attachment size exceeds limit, skipping remaining');
|
||||
break;
|
||||
}
|
||||
|
||||
const buffer = Buffer.from(attachment.data, "base64");
|
||||
|
||||
if (attachment.mime_type.startsWith("image/")) {
|
||||
await connection.sendMessage(recipient, {
|
||||
image: buffer,
|
||||
caption: attachment.filename,
|
||||
});
|
||||
} else if (attachment.mime_type.startsWith("video/")) {
|
||||
await connection.sendMessage(recipient, {
|
||||
video: buffer,
|
||||
caption: attachment.filename,
|
||||
});
|
||||
} else if (attachment.mime_type.startsWith("audio/")) {
|
||||
await connection.sendMessage(recipient, {
|
||||
audio: buffer,
|
||||
mimetype: attachment.mime_type,
|
||||
});
|
||||
} else {
|
||||
await connection.sendMessage(recipient, {
|
||||
document: buffer,
|
||||
fileName: attachment.filename,
|
||||
mimetype: attachment.mime_type,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async receive(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue