File size: 9,694 Bytes
743c605
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86da7cd
 
 
 
743c605
86da7cd
743c605
 
 
 
 
 
 
 
 
86da7cd
 
 
 
743c605
 
 
 
86da7cd
 
743c605
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86da7cd
 
 
 
743c605
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86da7cd
743c605
 
 
 
 
86da7cd
 
 
 
 
743c605
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const express = require('express');
const http = require('http');
const { Server } = require('socket.io');
const crypto = require('crypto');

const app = express();
const server = http.createServer(app);
const io = new Server(server, {
  maxHttpBufferSize: 1e7, // 10 MB limit for WebSocket messages
});

app.use(express.static('public'));

// In-Memory Data Stores
const usersBySocket = new Map();
const usersById = new Map();
const groups = new Map();

io.on('connection', (socket) => {
  // Rate limiting map per socket
  const rateLimit = new Map();

  const isRateLimited = (eventName) => {
    const now = Date.now();
    const lastTime = rateLimit.get(eventName) || 0;
    if (now - lastTime < 100) { // 100ms between same events
      return true;
    }
    rateLimit.set(eventName, now);
    return false;
  };

  socket.on('register', ({ displayName, avatarColor, publicKey }) => {
    if (isRateLimited('register')) return;

    const name = (displayName || '').trim().substring(0, 30).replace(/</g, "&lt;").replace(/>/g, "&gt;");
    const sanitizedColor = sanitizeColor(avatarColor);
    if (!name || !publicKey) return;

    const userId = crypto.randomUUID();
    const user = {
      userId,
      displayName: name,
      avatarColor: sanitizedColor,
      socketId: socket.id,
      publicKey,
      status: 'online',
      joinedAt: Date.now()
    };

    usersBySocket.set(socket.id, user);
    usersById.set(userId, user);

    socket.emit('registered', { userId, displayName: name });
    io.emit('user_joined', getSafeUserRecord(user));
    
    socket.emit('active_users', Array.from(usersById.values()).map(getSafeUserRecord));
  });

  socket.on('resume_session', ({ userId, displayName, publicKey }) => {
     if (isRateLimited('resume_session')) return;
     const name = (displayName || '').trim().substring(0, 30).replace(/</g, "&lt;").replace(/>/g, "&gt;");
     if (!name || !publicKey) return;

     // Security: Generate new userId on resume to prevent hijacking
     // Client-provided userId is ignored for security
     const newUserId = crypto.randomUUID();
     
     const user = {
        userId: newUserId,
        displayName: name,
        avatarColor: '#6366f1',
        socketId: socket.id,
        publicKey,
        status: 'online',
        joinedAt: Date.now()
      };
      
      usersBySocket.set(socket.id, user);
      usersById.set(newUserId, user);
      
      // Emit the NEW userId back to client
      socket.emit('registered', { userId: newUserId, displayName: name });
      
      io.emit('user_joined', getSafeUserRecord(user));
      socket.emit('active_users', Array.from(usersById.values()).map(getSafeUserRecord));
      
      // Note: Groups are lost on session resume due to ephemeral design
      // Client will need to be re-added to groups by other members
      const myGroups = [];
      for (const group of groups.values()) {
          if (group.memberIds.has(userId)) {
              myGroups.push({ ...group, memberIds: Array.from(group.memberIds), adminIds: Array.from(group.adminIds) });
          }
      }
      socket.emit('active_groups', myGroups);

      // Re-join socket rooms for groups
      for (const group of groups.values()) {
          if (group.memberIds.has(userId)) {
              socket.join(`group_${group.id}`);
          }
      }
  });

  socket.on('update_status', ({ status }) => {
    if (isRateLimited('update_status')) return;
    const user = usersBySocket.get(socket.id);
    if (!user) return;
    
    const validStatuses = ['online', 'away', 'dnd'];
    if (!validStatuses.includes(status)) return;
    
    user.status = status;
    io.emit('user_status_changed', { userId: user.userId, status });
  });

  socket.on('send_dm', ({ toUserId, message }) => {
    if (isRateLimited('send_dm')) return;
    if (Buffer.byteLength(JSON.stringify(message)) > 10 * 1024 * 1024) return; // double check size

    const sender = usersBySocket.get(socket.id);
    const recipient = usersById.get(toUserId);
    
    if (!sender || !recipient) return;
    
    io.to(recipient.socketId).emit('receive_dm', {
      fromUserId: sender.userId,
      message,
      timestamp: Date.now()
    });
    
    socket.emit('dm_sent_receipt', { toUserId, tempId: message.tempId, timestamp: Date.now() });
  });
  
  socket.on('delete_message', ({ targetId, isGroup, messageId }) => {
     if (isRateLimited('delete_message')) return;
     const sender = usersBySocket.get(socket.id);
     if (!sender) return;

     if (isGroup) {
         socket.to(`group_${targetId}`).emit('message_deleted', { targetId, isGroup: true, messageId });
     } else {
         const recipient = usersById.get(targetId);
         if (recipient) {
             io.to(recipient.socketId).emit('message_deleted', { targetId: sender.userId, isGroup: false, messageId });
         }
     }
  });

  socket.on('create_group', ({ name, avatar, memberIds, encryptedKeys }) => {
    if (isRateLimited('create_group')) return;
    const sender = usersBySocket.get(socket.id);
    if (!sender) return;

    const groupId = crypto.randomUUID();
    const sanitizedName = (name || '').trim().substring(0, 30).replace(/</g, "&lt;").replace(/>/g, "&gt;");
    const sanitizedAvatar = sanitizeColor(avatar);

    if (!Array.isArray(memberIds) || memberIds.length === 0 || memberIds.length > 100) return;
    
    const validMemberIds = new Set();
    for (const mId of memberIds) {
      if (typeof mId === 'string' && usersById.has(mId)) {
        validMemberIds.add(mId);
      }
    }
    validMemberIds.add(sender.userId);

    const group = {
       id: groupId,
       name: sanitizedName, 
       avatar: sanitizedAvatar,
       adminIds: new Set([sender.userId]),
       memberIds: validMemberIds
    };
    groups.set(groupId, group);

    for (const mId of validMemberIds) {
       const m = usersById.get(mId);
       if (m) {
          const encKey = encryptedKeys[mId];
          if (!encKey) {
             // Skip members without encrypted keys - they can't decrypt anyway
             continue;
          }
          const s = io.sockets.sockets.get(m.socketId);
          if (s) s.join(`group_${groupId}`);

          io.to(m.socketId).emit('group_created', {
             id: groupId,
             name: group.name,
             avatar: group.avatar,
             memberIds: Array.from(group.memberIds),
             adminIds: Array.from(group.adminIds),
             groupKeyEncrypted: encKey,
             createdBy: sender.userId
          });
       }
    }
  });

  socket.on('send_group_msg', ({ groupId, message }) => {
      if (isRateLimited('send_group_msg')) return;
      if (Buffer.byteLength(JSON.stringify(message)) > 10 * 1024 * 1024) return;

      const sender = usersBySocket.get(socket.id);
      if (!sender) return;
      const group = groups.get(groupId);
      if (!group || !group.memberIds.has(sender.userId)) return;

      socket.to(`group_${groupId}`).emit('receive_group_msg', {
          groupId,
          fromUserId: sender.userId,
          message,
          timestamp: Date.now()
      });
      
      socket.emit('group_msg_sent_receipt', { groupId, tempId: message.tempId, timestamp: Date.now() });
  });

  socket.on('typing', ({ targetId, isGroup }) => {
     if (isRateLimited('typing')) return;
     const sender = usersBySocket.get(socket.id);
     if (!sender) return;
     if (isGroup) {
        socket.to(`group_${targetId}`).emit('user_typing', { userId: sender.userId, targetId, isGroup: true });
     } else {
        const recipient = usersById.get(targetId);
        if (recipient) {
           io.to(recipient.socketId).emit('user_typing', { userId: sender.userId, targetId, isGroup: false });
        }
     }
  });

  socket.on('stop_typing', ({ targetId, isGroup }) => {
     if (isRateLimited('stop_typing')) return;
     const sender = usersBySocket.get(socket.id);
     if (!sender) return;
     if (isGroup) {
        socket.to(`group_${targetId}`).emit('user_stop_typing', { userId: sender.userId, targetId, isGroup: true });
     } else {
        const recipient = usersById.get(targetId);
        if (recipient) {
           io.to(recipient.socketId).emit('user_stop_typing', { userId: sender.userId, targetId, isGroup: false });
        }
     }
  });

  socket.on('disconnect', () => {
    const user = usersBySocket.get(socket.id);
    if (user) {
      const disconnectedSocketId = socket.id;
      user.status = 'offline';
      user.lastSeen = Date.now();
      usersBySocket.delete(socket.id);
      
      setTimeout(() => {
          // Check if user reconnected by verifying their userId still exists with a different socketId
          const currentUser = usersById.get(user.userId);
          if (!currentUser || currentUser.socketId === disconnectedSocketId) {
             // User didn't reconnect or is still on the same (now disconnected) socket
             usersById.delete(user.userId);
             io.emit('user_left', { userId: user.userId, lastSeen: user.lastSeen });
          }
      }, 5000);
    }
  });
});

function getSafeUserRecord(user) {
    return {
       userId: user.userId,
       displayName: user.displayName,
       avatarColor: user.avatarColor,
       publicKey: user.publicKey,
       status: user.status,
       lastSeen: user.lastSeen || null
    };
}

function sanitizeColor(color) {
    // Only allow valid hex colors
    if (!color || typeof color !== 'string') return '#6366f1';
    const hexPattern = /^#[0-9A-Fa-f]{6}$/;
    return hexPattern.test(color) ? color : '#6366f1';
}

const PORT = process.env.PORT || 3000;
server.listen(PORT, () => {
  console.log(`Server listening on port ${PORT}`);
});