-
Notifications
You must be signed in to change notification settings - Fork 0
/
whatsapp-terminal-control.js
191 lines (179 loc) · 7.18 KB
/
whatsapp-terminal-control.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
const { default: makeWASocket, useMultiFileAuthState, DisconnectReason } = require('@whiskeysockets/baileys');
const qrcode = require('qrcode-terminal');
const { Boom } = require('@hapi/boom');
const { exec } = require('child_process');
const fs = require('fs');
const os = require('os');
const path = require('path');
const uploadDir = path.join(__dirname, 'uploads');
if (!fs.existsSync(uploadDir)) {
fs.mkdirSync(uploadDir);
}
async function connectToWhatsApp() {
const { state, saveCreds } = await useMultiFileAuthState('auth_info_baileys');
const sock = makeWASocket({
auth: state,
printQRInTerminal: true
});
sock.ev.on('creds.update', saveCreds);
sock.ev.on('connection.update', (update) => {
const { connection, qr, lastDisconnect } = update;
if (qr) {
qrcode.generate(qr, { small: true });
}
if (connection === 'close') {
const shouldReconnect = (lastDisconnect.error instanceof Boom) &&
lastDisconnect.error.output?.statusCode !== DisconnectReason.loggedOut;
console.log('Connection closed, reconnecting...', shouldReconnect);
if (shouldReconnect) {
connectToWhatsApp();
}
} else if (connection === 'open') {
console.log('Connection opened!');
}
});
sock.ev.on('messages.upsert', async (m) => {
console.log(JSON.stringify(m, undefined, 2));
const msg = m.messages[0];
if (msg.key.fromMe) return;
if (msg.message?.documentMessage) {
await handleFileUpload(msg, sock);
} else {
const messageContent = msg.message?.conversation || msg.message?.extendedTextMessage?.text;
if (messageContent) {
handleIncomingMessage(messageContent, msg.key.remoteJid, sock);
}
}
});
}
async function handleFileUpload(msg, sock) {
const fileName = msg.message.documentMessage.fileName;
const fileBuffer = await sock.downloadMediaMessage(msg);
const filePath = path.join(uploadDir, fileName);
fs.writeFile(filePath, fileBuffer, (err) => {
if (err) {
sock.sendMessage(msg.key.remoteJid, { text: `Error uploading file: ${err.message}` });
} else {
sock.sendMessage(msg.key.remoteJid, { text: `File uploaded successfully. Path: ${filePath}` });
}
});
}
function handleIncomingMessage(messageContent, remoteJid, sock) {
if (!messageContent) return;
if (messageContent.startsWith('$')) {
handleTerminalCommand(messageContent.slice(1), remoteJid, sock);
} else if (messageContent.startsWith('/')) {
handleCustomCommand(messageContent.slice(1), remoteJid, sock);
} else if (messageContent.startsWith('£')) {
handleMathOperation(messageContent.slice(1), remoteJid, sock);
} else {
sock.sendMessage(remoteJid, { text: `Unknown command format. Use $ for terminal commands, / for custom commands, or £ for math operations.` });
}
}
function handleTerminalCommand(command, remoteJid, sock) {
exec(command, (error, stdout, stderr) => {
if (error) {
sock.sendMessage(remoteJid, { text: `Error: ${error.message}` });
return;
}
if (stderr) {
sock.sendMessage(remoteJid, { text: `Stderr: ${stderr}` });
return;
}
const output = stdout.split('\n').slice(-15).join('\n');
sock.sendMessage(remoteJid, { text: `Output:\n${output}` });
});
}
function handleCustomCommand(command, remoteJid, sock) {
const [subCommand, ...args] = command.split(' ');
switch (subCommand) {
case 'sysinfo':
const sysInfo = {
platform: os.platform(),
arch: os.arch(),
release: os.release(),
uptime: os.uptime(),
totalMem: os.totalmem(),
freeMem: os.freemem()
};
sock.sendMessage(remoteJid, { text: `System Info:\n${JSON.stringify(sysInfo, null, 2)}` });
break;
case 'listfiles':
const dirPath = args[0] || '.';
fs.readdir(dirPath, (err, files) => {
if (err) {
sock.sendMessage(remoteJid, { text: `Error listing files: ${err.message}` });
} else {
sock.sendMessage(remoteJid, { text: `Files in ${dirPath}:\n${files.join('\n')}` });
}
});
break;
case 'readfile':
if (args.length === 0) {
sock.sendMessage(remoteJid, { text: 'Please provide a file path' });
return;
}
fs.readFile(args[0], 'utf8', (err, data) => {
if (err) {
sock.sendMessage(remoteJid, { text: `Error reading file: ${err.message}` });
} else {
sock.sendMessage(remoteJid, { text: `File contents:\n${data}` });
}
});
break;
case 'upload':
sock.sendMessage(remoteJid, { text: 'Please send the file you want to upload.' });
break;
case 'download':
if (args.length === 0) {
sock.sendMessage(remoteJid, { text: 'Please provide a file path to download' });
return;
}
const filePath = args[0];
if (fs.existsSync(filePath)) {
const fileName = path.basename(filePath);
sock.sendMessage(remoteJid, {
document: { url: filePath },
fileName: fileName,
mimetype: 'application/octet-stream'
});
} else {
sock.sendMessage(remoteJid, { text: `File not found: ${filePath}` });
}
break;
case 'netstat':
exec('netstat -an', (error, stdout, stderr) => {
if (error) {
sock.sendMessage(remoteJid, { text: `Error: ${error.message}` });
return;
}
const connections = stdout.split('\n').filter(line => line.includes('ESTABLISHED')).join('\n');
sock.sendMessage(remoteJid, { text: `Active connections:\n${connections}` });
});
break;
case 'ping':
if (args.length === 0) {
sock.sendMessage(remoteJid, { text: 'Please provide a host to ping' });
return;
}
exec(`ping -c 4 ${args[0]}`, (error, stdout, stderr) => {
if (error) {
sock.sendMessage(remoteJid, { text: `Error: ${error.message}` });
return;
}
sock.sendMessage(remoteJid, { text: stdout });
});
break;
default:
sock.sendMessage(remoteJid, { text: `Unknown custom command: ${subCommand}` });
}
}
function handleMathOperation(expression, remoteJid, sock) {
try {
const result = eval(expression);
sock.sendMessage(remoteJid, { text: `Result: ${result}` });
} catch (error) {
sock.sendMessage(remoteJid, { text: `Error evaluating expression: ${error.message}` });
}
}
connectToWhatsApp();