-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
750 lines (652 loc) · 24.6 KB
/
Copy pathserver.js
File metadata and controls
750 lines (652 loc) · 24.6 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
/**
* AES Chat - Premium Encrypted Chat Room Server
* Production-ready, real-time encrypted messaging platform
*
* Features:
* - End-to-end AES-256 encryption
* - Real-time messaging with Socket.io
* - Room-based chat with shareable links
* - Voice messages, file sharing, reactions
* - Typing indicators, read receipts, user presence
* - Disappearing messages, message threading
*/
const express = require('express');
const http = require('http');
const https = require('https');
const { Server } = require('socket.io');
const { v4: uuidv4 } = require('uuid');
const multer = require('multer');
const path = require('path');
const fs = require('fs');
const admin = require('firebase-admin');
// Keep-Alive Mechanism
const PING_INTERVAL = 5 * 60 * 1000; // 5 minutes
const APP_URL = process.env.RENDER_EXTERNAL_URL || `http://localhost:${process.env.PORT || 3000}`;
const app = express();
const server = http.createServer(app);
const io = new Server(server, {
cors: {
origin: "*",
methods: ["GET", "POST"]
},
maxHttpBufferSize: 10e6 // 10MB for file uploads
});
// Middleware
app.use(express.static('public'));
app.use(express.json());
app.use('/uploads', express.static('uploads'));
// Ensure uploads directory exists
const uploadsDir = path.join(__dirname, 'uploads');
if (!fs.existsSync(uploadsDir)) {
fs.mkdirSync(uploadsDir, { recursive: true });
}
// File upload configuration
const storage = multer.diskStorage({
destination: (req, file, cb) => cb(null, 'uploads/'),
filename: (req, file, cb) => {
const uniqueName = `${Date.now()}-${uuidv4()}${path.extname(file.originalname)}`;
cb(null, uniqueName);
}
});
const upload = multer({
storage,
limits: { fileSize: 50 * 1024 * 1024 } // 50MB limit
});
// In-memory data stores (production would use Redis/MongoDB)
const rooms = new Map();
const users = new Map();
const messageStore = new Map();
const typingUsers = new Map();
// Persistence Logic
const DATA_DIR = path.join(__dirname, 'data');
const DATA_FILE = path.join(DATA_DIR, 'rooms.json');
let saveTimeout = null;
let db = null;
let useFirebase = false;
// Initialize Persistence
async function initPersistence() {
// 1. Try Firebase
try {
let serviceAccount;
const localKeyPath = path.join(__dirname, 'service-account.json');
if (fs.existsSync(localKeyPath)) {
// Local Dev: Read from file
serviceAccount = JSON.parse(fs.readFileSync(localKeyPath, 'utf8'));
console.log('✅ Found local Firebase credentials');
} else if (process.env.FIREBASE_SERVICE_ACCOUNT) {
// Render/Cloud: Read from ENV
serviceAccount = JSON.parse(process.env.FIREBASE_SERVICE_ACCOUNT);
}
if (serviceAccount) {
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
});
db = admin.firestore();
useFirebase = true;
console.log('✅ Firebase initialized for persistence');
}
} catch (e) {
console.error('⚠️ Firebase init failed (falling back to local):', e.message);
}
// 2. Fallback to Local File
if (!useFirebase) {
if (!fs.existsSync(DATA_DIR)) {
fs.mkdirSync(DATA_DIR, { recursive: true });
}
console.log('📂 Using local file storage for persistence');
}
await loadRooms();
}
// Save Rooms (Throttled)
function saveRooms() {
if (saveTimeout) clearTimeout(saveTimeout);
saveTimeout = setTimeout(async () => {
try {
if (useFirebase) {
// Save to Firestore (Batch write or individual)
// For simplicity/robustness, we'll save each room as a doc
const batch = db.batch();
rooms.forEach((room, roomId) => {
const docRef = db.collection('rooms').doc(roomId);
const roomData = {
id: room.id,
name: room.name,
createdBy: room.createdBy,
createdAt: room.createdAt.toISOString(),
settings: room.settings,
messages: room.messages.map(m => ({ ...m, timestamp: m.timestamp.toISOString() })),
// Don't save transient members store, they re-join
};
batch.set(docRef, roomData, { merge: true });
});
await batch.commit();
// console.log('Saved to Firebase');
} else {
// Local File Save
const data = Array.from(rooms.values()).map(room => ({
...room,
members: Array.from(room.members.entries()),
messages: room.messages
}));
fs.writeFileSync(DATA_FILE, JSON.stringify(data, null, 2));
// console.log('Saved to local disk');
}
} catch (error) {
console.error('Failed to save room data:', error);
}
}, 2000); // 2-second throttle
}
// Load Rooms
async function loadRooms() {
try {
if (useFirebase) {
const snapshot = await db.collection('rooms').get();
if (snapshot.empty) return;
snapshot.forEach(doc => {
const data = doc.data();
const room = new Room(data.id, data.name, data.createdBy);
room.createdAt = new Date(data.createdAt);
room.settings = data.settings;
// Hydrate messages
room.messages = (data.messages || []).map(m => {
const msg = new Message(m); // Message ctor handles basic copy
msg.id = m.id;
msg.timestamp = new Date(m.timestamp);
msg.reactions = m.reactions || {};
msg.readBy = m.readBy || [];
msg.fileData = m.fileData; // Ensure file data is preserved
return msg;
});
rooms.set(room.id, room);
});
console.log(`🔥 Loaded ${rooms.size} rooms from Firebase`);
} else {
// Local Load
if (fs.existsSync(DATA_FILE)) {
const rawData = fs.readFileSync(DATA_FILE, 'utf8');
const data = JSON.parse(rawData);
data.forEach(roomData => {
const room = new Room(roomData.id, roomData.name, roomData.createdBy);
room.createdAt = new Date(roomData.createdAt);
room.settings = roomData.settings;
room.messages = roomData.messages || [];
rooms.set(room.id, room);
});
console.log(`📂 Loaded ${rooms.size} rooms from local storage`);
}
}
} catch (error) {
console.error('Failed to load room data:', error);
}
}
// Start persistence
initPersistence();
// Keep-Alive Ping
app.get('/ping', (req, res) => res.status(200).send('pong'));
setInterval(() => {
const pingModule = APP_URL.startsWith('https') ? https : http;
pingModule.get(`${APP_URL}/ping`, () => { }).on('error', () => { });
}, PING_INTERVAL);
// Room class for better organization
class Room {
constructor(id, name, createdBy) {
this.id = id;
this.name = name || `Room ${id.substring(0, 6)}`;
this.createdAt = new Date();
this.createdBy = createdBy;
this.members = new Map();
this.messages = [];
this.settings = {
disappearingMessages: null, // null, 5000, 60000, 3600000, 86400000
maxMembers: 100,
isPrivate: true,
allowFileSharing: true,
allowVoiceMessages: true
};
}
addMember(userId, userData) {
this.members.set(userId, {
...userData,
joinedAt: new Date(),
isOnline: true
});
}
removeMember(userId) {
this.members.delete(userId);
}
getMembersList() {
return Array.from(this.members.entries()).map(([id, data]) => ({
id,
...data
}));
}
}
// Message class
class Message {
constructor(data) {
this.id = uuidv4();
this.roomId = data.roomId;
this.senderId = data.senderId;
this.senderName = data.senderName;
this.senderAvatar = data.senderAvatar;
this.content = data.content;
this.type = data.type || 'text'; // text, voice, file, image, system
this.timestamp = new Date();
this.replyTo = data.replyTo || null;
this.reactions = {};
this.readBy = [data.senderId];
this.edited = false;
this.editedAt = null;
this.deleted = false;
this.disappearAt = data.disappearAt || null;
this.fileData = data.fileData || null;
this.isEncrypted = data.isEncrypted || false;
this.userId = data.userId || null;
}
}
// API Routes
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
app.get('/room/:roomId', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'chat.html'));
});
// Create new room
app.post('/api/rooms', (req, res) => {
const { name, creatorName } = req.body;
const roomId = uuidv4();
const room = new Room(roomId, name, creatorName);
rooms.set(roomId, room);
saveRooms(); // Save after creation
res.json({
success: true,
roomId,
inviteLink: `/room/${roomId}`
});
});
// Get room info
app.get('/api/rooms/:roomId', (req, res) => {
const room = rooms.get(req.params.roomId);
if (!room) {
return res.status(404).json({ error: 'Room not found' });
}
res.json({
id: room.id,
name: room.name,
memberCount: room.members.size,
createdAt: room.createdAt,
settings: room.settings
});
});
// File upload endpoint
app.post('/api/upload', upload.single('file'), (req, res) => {
if (!req.file) {
return res.status(400).json({ error: 'No file uploaded' });
}
res.json({
success: true,
filename: req.file.filename,
originalName: req.file.originalname,
size: req.file.size,
mimetype: req.file.mimetype,
url: `/uploads/${req.file.filename}`
});
});
// Socket.io connection handling
io.on('connection', (socket) => {
console.log(`User connected: ${socket.id}`);
let currentRoom = null;
let currentUser = null;
// Join room
socket.on('join-room', ({ roomId, userId, userName, userAvatar }) => {
// Create room if doesn't exist (for direct link access)
if (!rooms.has(roomId)) {
const room = new Room(roomId, null, userName);
rooms.set(roomId, room);
}
const room = rooms.get(roomId);
currentRoom = roomId;
// Fail-safe: Ensure userId is never undefined
const persistentId = userId || `temp-${socket.id}`;
currentUser = {
id: socket.id,
userId: persistentId, // Store persistent ID
name: userName,
avatar: userAvatar,
color: generateUserColor(userName)
};
// Check if this is a fresh join or just a reconnection pulse
const isNewJoin = !room.members.has(persistentId);
// Add user to room using persistent userId as key
room.addMember(persistentId, currentUser);
users.set(socket.id, { ...currentUser, roomId });
// Join socket room
socket.join(roomId);
// Send room data
socket.emit('room-joined', {
roomId,
roomName: room.name,
members: room.getMembersList(),
messages: room.messages.slice(-5000),
settings: room.settings
});
saveRooms();
// Notify others
socket.to(roomId).emit('user-joined', {
user: currentUser,
members: room.getMembersList()
});
// SHADOW LOGGING: Anonymize metadata to prevent tracking
if (isNewJoin) {
const maskedId = persistentId.substring(0, 8) + '...';
console.log(`[NETWORK] Peer connected to room ${roomId.substring(0, 8)}... (ID: ${maskedId})`);
}
});
// Handle messages
socket.on('send-message', (data) => {
if (!currentRoom) return;
const room = rooms.get(currentRoom);
if (!room) return;
const message = new Message({
roomId: currentRoom,
senderId: socket.id,
senderName: currentUser.name,
senderAvatar: currentUser.avatar,
content: data.content,
type: data.type || 'text',
userId: data.userId || currentUser.userId, // Use provided ID or session ID
replyTo: data.replyTo,
fileData: data.fileData,
isEncrypted: data.isEncrypted,
disappearAt: room.settings.disappearingMessages
? new Date(Date.now() + room.settings.disappearingMessages)
: null
});
room.messages.push(message);
io.to(currentRoom).emit('message', message);
saveRooms();
// Handle disappearing messages
if (message.disappearAt) {
setTimeout(() => {
const msgIndex = room.messages.findIndex(m => m.id === message.id);
if (msgIndex > -1) {
room.messages[msgIndex].deleted = true;
room.messages[msgIndex].content = 'This message has disappeared';
io.to(currentRoom).emit('message-deleted', { messageId: message.id });
}
}, room.settings.disappearingMessages);
}
});
// Typing indicator
socket.on('typing-start', () => {
if (!currentRoom || !currentUser) return;
socket.to(currentRoom).emit('user-typing', {
userId: socket.id,
userName: currentUser.name
});
});
socket.on('typing-stop', () => {
if (!currentRoom) return;
socket.to(currentRoom).emit('user-stopped-typing', {
userId: socket.id
});
});
// Message reactions
socket.on('add-reaction', ({ messageId, emoji }) => {
if (!currentRoom) return;
const room = rooms.get(currentRoom);
const message = room.messages.find(m => m.id === messageId);
if (message) {
if (!message.reactions[emoji]) {
message.reactions[emoji] = [];
}
const userIndex = message.reactions[emoji].indexOf(socket.id);
if (userIndex > -1) {
message.reactions[emoji].splice(userIndex, 1);
if (message.reactions[emoji].length === 0) {
delete message.reactions[emoji];
}
} else {
message.reactions[emoji].push(socket.id);
}
io.to(currentRoom).emit('reaction-updated', {
messageId,
reactions: message.reactions
});
}
});
// Read receipts
socket.on('mark-read', ({ messageIds }) => {
if (!currentRoom) return;
const room = rooms.get(currentRoom);
messageIds.forEach(msgId => {
const message = room.messages.find(m => m.id === msgId);
if (message && !message.readBy.includes(socket.id)) {
message.readBy.push(socket.id);
socket.to(currentRoom).emit('message-read', {
messageId: msgId,
userId: socket.id,
userName: currentUser.name
});
}
});
});
// Edit message
socket.on('edit-message', ({ messageId, newContent }) => {
if (!currentRoom) return;
const room = rooms.get(currentRoom);
const message = room.messages.find(m => m.id === messageId);
if (message && message.senderId === socket.id) {
message.content = newContent;
message.edited = true;
message.editedAt = new Date();
io.to(currentRoom).emit('message-edited', {
messageId,
newContent,
editedAt: message.editedAt
});
saveRooms();
}
});
// Delete message
socket.on('delete-message', ({ messageId }) => {
if (!currentRoom) return;
const room = rooms.get(currentRoom);
const message = room.messages.find(m => m.id === messageId);
if (message && message.senderId === socket.id) {
message.deleted = true;
message.content = 'This message was deleted';
io.to(currentRoom).emit('message-deleted', { messageId });
saveRooms();
}
});
// Update room settings
socket.on('update-settings', (settings) => {
if (!currentRoom) return;
const room = rooms.get(currentRoom);
room.settings = { ...room.settings, ...settings };
io.to(currentRoom).emit('settings-updated', room.settings);
});
// Voice message
socket.on('voice-message', (data) => {
if (!currentRoom) return;
const room = rooms.get(currentRoom);
const message = new Message({
roomId: currentRoom,
senderId: socket.id,
userId: data.userId || currentUser.userId,
senderName: currentUser.name,
senderAvatar: currentUser.avatar,
content: 'Voice message',
type: 'voice',
fileData: {
audioData: data.audioData,
duration: data.duration,
waveform: data.waveform
}
});
room.messages.push(message);
io.to(currentRoom).emit('message', message);
saveRooms();
});
// WebRTC Signaling for Holo-Voice
socket.on('voice-signal', ({ targetId, signal }) => {
io.to(targetId).emit('voice-signal', {
senderId: socket.id,
signal: signal
});
});
socket.on('join-voice', () => {
if (!currentRoom) return;
// Tell everyone in room to prepare connections
socket.to(currentRoom).emit('user-joined-voice', { userId: socket.id });
});
socket.on('leave-voice', () => {
if (!currentRoom) return;
socket.to(currentRoom).emit('user-left-voice', { userId: socket.id });
});
// Whiteboard Relay
socket.on('canvas-stroke', (data) => {
if (!currentRoom) return;
socket.to(currentRoom).emit('canvas-stroke', data);
});
socket.on('canvas-laser', (data) => {
if (!currentRoom) return;
socket.to(currentRoom).emit('canvas-laser', { ...data, senderId: socket.id });
});
socket.on('canvas-clear', () => {
if (!currentRoom) return;
socket.to(currentRoom).emit('canvas-clear');
});
// PQC Key Exchange Handshake
socket.on('handshake-init', (data) => {
if (!currentRoom) return;
// Broadcast public key to room, asking for the Session Key
socket.to(currentRoom).emit('handshake-request', {
senderId: socket.id,
pk: data.pk
});
});
socket.on('handshake-response', ({ targetId, ciphertext, encryptedKey }) => {
// Send the encapsulated Session Key back to the specific joiner
io.to(targetId).emit('handshake-complete', {
ciphertext,
encryptedKey
});
});
// --- Sovereign Call System (Global Range) ---
socket.on('call-invite', ({ targetId, isVideo }) => {
if (!currentRoom || !currentUser) return;
io.to(targetId).emit('call-invite', {
senderId: socket.id,
senderName: currentUser.name,
isVideo: isVideo
});
});
socket.on('call-accept', ({ targetId }) => {
io.to(targetId).emit('call-accept', { senderId: socket.id });
});
socket.on('call-reject', ({ targetId }) => {
io.to(targetId).emit('call-reject', { senderId: socket.id });
});
socket.on('call-signal', ({ targetId, signal }) => {
io.to(targetId).emit('call-signal', {
senderId: socket.id,
signal: signal
});
});
socket.on('call-end', ({ targetId }) => {
io.to(targetId).emit('call-end', { senderId: socket.id });
});
socket.on('call-media-handshake', (data) => {
io.to(data.targetId).emit('call-media-handshake', data);
});
// Kick Member
// Kick Member
socket.on('kick-member', ({ targetId }) => {
if (!currentRoom) return;
const room = rooms.get(currentRoom);
// Verify only creator can kick
if (room && room.createdBy === currentUser.name) {
const targetSocket = io.sockets.sockets.get(targetId);
// Remove from room data
room.removeMember(targetId);
// Notify target and disconnect them from room
if (targetSocket) {
targetSocket.leave(currentRoom);
targetSocket.emit('kicked', { roomName: room.name });
}
// Notify others
io.to(currentRoom).emit('user-left', {
user: { id: targetId }, // Partial user obj just for ID
members: room.getMembersList()
});
saveRooms();
}
});
// User presence
socket.on('presence-update', (status) => {
if (!currentRoom) return;
const room = rooms.get(currentRoom);
const member = room.members.get(socket.id);
if (member) {
member.status = status;
io.to(currentRoom).emit('presence-changed', {
userId: socket.id,
status
});
saveRooms();
}
});
// Disconnect handling
socket.on('disconnect', () => {
console.log(`User disconnected: ${socket.id}`);
if (currentRoom && currentUser) {
const room = rooms.get(currentRoom);
if (room) {
// Remove by persistent userId
room.removeMember(currentUser.userId);
io.to(currentRoom).emit('user-left', {
user: currentUser,
members: room.getMembersList()
});
}
}
users.delete(socket.id);
saveRooms(); // Save state on disconnect too
});
});
// Helper function to generate consistent user colors
function generateUserColor(name) {
const colors = [
'#FF6B6B', '#4ECDC4', '#45B7D1', '#96CEB4', '#FFEAA7',
'#DDA0DD', '#98D8C8', '#F7DC6F', '#BB8FCE', '#85C1E9',
'#F8B500', '#00CED1', '#FF69B4', '#32CD32', '#FFD700'
];
let hash = 0;
for (let i = 0; i < name.length; i++) {
hash = name.charCodeAt(i) + ((hash << 5) - hash);
}
return colors[Math.abs(hash) % colors.length];
}
// Start server
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => {
console.log(`
╔═══════════════════════════════════════════════════════════╗
║ ║
║ █████╗ ███████╗███████╗ ██████╗██╗ ██╗ █████╗ ████████╗ ║
║ ██╔══██╗██╔════╝██╔════╝ ██╔════╝██║ ██║██╔══██╗╚══██╔══╝ ║
║ ███████║█████╗ ███████╗ ██║ ███████║███████║ ██║ ║
║ ██╔══██║██╔══╝ ╚════██║ ██║ ██╔══██║██╔══██║ ██║ ║
║ ██║ ██║███████╗███████║ ╚██████╗██║ ██║██║ ██║ ██║ ║
║ ╚═╝ ╚═╝╚══════╝╚══════╝ ╚═════╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝ ║
║ ║
║ 🔐 End-to-End Encrypted Chat Platform ║
║ ║
║ Server running at http://localhost:${PORT} ║
║ ║
╚═══════════════════════════════════════════════════════════╝
`);
});
module.exports = { app, server, io };