Repository navigation
Expand file tree
/
Copy pathtest.js
More file actions
111 lines (91 loc) · 3.56 KB
/
Copy pathtest.js
File metadata and controls
111 lines (91 loc) · 3.56 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
#!/usr/bin/env node
import express from 'express';
import expressWs from 'express-ws';
import crypto from 'crypto';
import dotenv from 'dotenv';
dotenv.config();
const app = express();
expressWs(app);
const endpointToConnectedUsersMap = new Map();
let secretKey;
// Handle WebSocket connections for dynamic endpoints like /message1, /message2, etc.
app.ws('/:endpoint', (ws, req) => {
const username = req.query.username;
const endpoint = req.params.endpoint;
if (!username) {
sendErrorMessage(ws, 'Authentication failed. Missing username.');
return;
}
// Create a Map for the endpoint if it doesn't exist
if (!endpointToConnectedUsersMap.has(endpoint)) {
endpointToConnectedUsersMap.set(endpoint, new Map());
}
ws.on('message', (message) => {
const decryptedMessage = decryptMessage(message);
if (decryptedMessage) {
const timestamp = getCurrentTime();
console.log(`${timestamp} ${username} sent: ${decryptedMessage.content}`);
broadcastMessage({ sender: username, content: decryptedMessage.content, timestamp }, endpoint);
}
});
endpointToConnectedUsersMap.get(endpoint).set(ws, { username, socket: ws });
sendWelcomeMessage(ws, username);
});
// Reject any other WebSocket connections
app.ws('*', (ws, req) => {
ws.close(4000, 'Invalid WebSocket endpoint');
});
function sendErrorMessage(ws, errorMessage) {
ws.send(JSON.stringify({ error: errorMessage }));
ws.close();
}
function sendWelcomeMessage(ws, username) {
ws.send(JSON.stringify({ status: `Welcome, ${username}! You are now connected to the chat.` }));
}
function broadcastMessage(message, endpoint) {
const connectedUsers = endpointToConnectedUsersMap.get(endpoint);
connectedUsers.forEach((user) => {
user.socket.send(encryptMessage(message));
});
}
function encryptMessage(message) {
try {
const algorithm = 'aes-256-cbc';
const key = Buffer.from(secretKey, 'hex');
const iv = Buffer.alloc(16, 0); // Initialization vector, you may need to change this based on how you generate it
const cipher = crypto.createCipheriv(algorithm, key, iv);
let encryptedMessage = cipher.update(JSON.stringify(message), 'utf-8', 'base64');
encryptedMessage += cipher.final('base64');
return encryptedMessage;
} catch (error) {
console.error('Error encrypting message:', error);
return null;
}
}
function decryptMessage(encryptedMessage) {
try {
const algorithm = 'aes-256-cbc';
const key = Buffer.from(secretKey, 'hex');
const iv = Buffer.alloc(16, 0); // Initialization vector, you may need to change this based on how you generate it
const decipher = crypto.createDecipheriv(algorithm, key, iv);
let decryptedMessage = decipher.update(encryptedMessage, 'base64', 'utf-8');
decryptedMessage += decipher.final('utf-8');
return JSON.parse(decryptedMessage);
} catch (error) {
console.error('Error decrypting message:', error);
return null;
}
}
function getCurrentTime() {
const now = new Date();
const hours = now.getHours().toString().padStart(2, '0');
const minutes = now.getMinutes().toString().padStart(2, '0');
const seconds = now.getSeconds().toString().padStart(2, '0');
return `${hours}:${minutes}:${seconds}`;
}
const port = 6000;
app.listen(port, () => {
console.log(`Server listening on port ${port}`);
secretKey = process.env.ENCRYPTION_KEY;
});
// wscat -n -c "wss://localhost:8443/message2?username=alice"