-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
253 lines (218 loc) · 9.49 KB
/
main.js
File metadata and controls
253 lines (218 loc) · 9.49 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/**
* BrailleCrypt for Discord — Main Entry Point
*
* Orchestrates: Launch Discord → Connect CDP → Inject Encryption Script
*
* Usage:
* node main.js (prompts for key)
* node main.js --key "mySecret" (uses provided key)
* node main.js --no-launch (connects to already-running Discord)
*/
const readline = require('readline');
const CDPConnector = require('./src/cdp');
const { launchDiscord } = require('./src/launcher');
const { buildInjectionScript } = require('./src/discord-inject');
const config = require('./config.json');
// ═══════════════════════════════════════════════════════════════
// CLI ARGUMENT PARSING
// ═══════════════════════════════════════════════════════════════
function parseArgs() {
const args = process.argv.slice(2);
const options = {
key: config.encryptionKey || '',
port: config.debugPort || 0,
noLaunch: false,
help: false
};
for (let i = 0; i < args.length; i++) {
switch (args[i]) {
case '--key':
case '-k':
options.key = args[++i] || '';
break;
case '--port':
case '-p':
options.port = parseInt(args[++i]) || 0;
break;
case '--no-launch':
options.noLaunch = true;
break;
case '--help':
case '-h':
options.help = true;
break;
}
}
return options;
}
function printHelp() {
console.log(`
╔══════════════════════════════════════════════════════════════╗
║ 🔐 BrailleCrypt for Discord ║
╚══════════════════════════════════════════════════════════════╝
Usage: node main.js [options]
Options:
--key, -k <passphrase> Set the encryption passphrase
--port, -p <port> Set the CDP debug port (0 = random)
--no-launch Don't restart Discord (attach to existing)
--help, -h Show this help message
Examples:
node main.js --key "my-secret-key"
node main.js --key "shared-key" --no-launch --port 9222
`);
}
// ═══════════════════════════════════════════════════════════════
// PROMPT FOR KEY
// ═══════════════════════════════════════════════════════════════
function promptForKey() {
return new Promise((resolve) => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
console.log('');
rl.question('🔑 Enter encryption passphrase: ', (answer) => {
rl.close();
resolve(answer.trim());
});
});
}
// ═══════════════════════════════════════════════════════════════
// MAIN
// ═══════════════════════════════════════════════════════════════
async function main() {
console.log('');
console.log('╔══════════════════════════════════════════════════════════════╗');
console.log('║ 🔐 BrailleCrypt for Discord ║');
console.log('║ Automatic end-to-end encrypted chat ║');
console.log('╚══════════════════════════════════════════════════════════════╝');
console.log('');
const options = parseArgs();
if (options.help) {
printHelp();
process.exit(0);
}
// Get the encryption key
let passphrase = options.key;
if (!passphrase) {
passphrase = await promptForKey();
}
if (!passphrase || passphrase.length < 4) {
console.error('❌ Passphrase must be at least 4 characters long');
process.exit(1);
}
console.log(`🔑 Passphrase set: ${passphrase.substring(0, 3)}${'*'.repeat(passphrase.length - 3)}`);
console.log('');
let port = options.port;
// Step 1: Launch Discord (unless --no-launch)
if (!options.noLaunch) {
console.log('🚀 Step 1: Launching Discord with debug port...');
try {
const result = await launchDiscord(port);
port = result.port;
} catch (err) {
console.error(`❌ Failed to launch Discord: ${err.message}`);
console.log('');
console.log('💡 Tips:');
console.log(' - Make sure Discord desktop is installed');
console.log(' - Try running with --no-launch if Discord is already open');
console.log(' - Manually launch Discord with:');
console.log(` Discord.exe --remote-debugging-port=9222`);
process.exit(1);
}
} else {
if (port === 0) port = 9222; // Default when not launching
console.log(`🔌 Connecting to existing Discord on port ${port}...`);
}
// Wait a bit for Discord to fully initialize the UI
console.log('');
console.log('⏳ Waiting for Discord to fully load...');
await new Promise(r => setTimeout(r, 5000));
// Step 2: Connect via CDP
console.log('');
console.log('🔌 Step 2: Connecting to Discord via Chrome DevTools Protocol...');
const cdp = new CDPConnector(port, '127.0.0.1');
let retries = 0;
const maxRetries = 10;
while (retries < maxRetries) {
try {
await cdp.connect();
break;
} catch (err) {
retries++;
if (retries >= maxRetries) {
console.error(`❌ Failed to connect after ${maxRetries} attempts: ${err.message}`);
process.exit(1);
}
console.log(` Retry ${retries}/${maxRetries}...`);
await new Promise(r => setTimeout(r, 3000));
}
}
await cdp.enableRuntime();
// Step 3: Inject the encryption script
console.log('');
console.log('💉 Step 3: Injecting E2E encryption into Discord...');
const script = buildInjectionScript(
passphrase,
config.messagePrefix || '⠓⠑',
config.pbkdf2Iterations || 600000
);
try {
await cdp.evaluate(script);
console.log('');
console.log('═══════════════════════════════════════════════════════════════');
console.log(' ✅ E2E ENCRYPTION IS NOW ACTIVE');
console.log('');
console.log(' • Your messages will be automatically encrypted');
console.log(' • Encrypted messages from others with the same key');
console.log(' will be automatically decrypted');
console.log(' • Messages appear as Braille characters to anyone');
console.log(' without the key');
console.log(' • A 🔒 icon indicates successfully decrypted messages');
console.log('');
console.log(' Press Ctrl+C to stop');
console.log('═══════════════════════════════════════════════════════════════');
console.log('');
} catch (err) {
console.error(`❌ Failed to inject encryption: ${err.message}`);
cdp.disconnect();
process.exit(1);
}
// Handle reconnection — re-inject the script
cdp.on('reconnected', async () => {
console.log('[Main] Reconnected to Discord, re-injecting encryption...');
try {
await cdp.enableRuntime();
await cdp.evaluate(script);
console.log('[Main] ✅ Re-injection successful');
} catch (err) {
console.error('[Main] ❌ Re-injection failed:', err.message);
}
});
cdp.on('failed', () => {
console.error('[Main] ❌ Lost connection to Discord permanently');
process.exit(1);
});
// Graceful shutdown
process.on('SIGINT', () => {
console.log('');
console.log('🛑 Shutting down E2E encryption...');
cdp.disconnect();
console.log('👋 Goodbye! Discord will continue running without encryption.');
process.exit(0);
});
process.on('SIGTERM', () => {
cdp.disconnect();
process.exit(0);
});
// Keep the process alive
setInterval(() => {
if (!cdp.connected) {
console.log('[Main] Waiting for reconnection...');
}
}, 30000);
}
main().catch(err => {
console.error('Fatal error:', err);
process.exit(1);
});