-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.js
397 lines (362 loc) · 12.9 KB
/
index.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
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
const process = require('process')
// Handle SIGINT
process.on('SIGINT', () => {
console.info("SIGINT Received, exiting...")
process.exit(0)
})
// Handle SIGTERM
process.on('SIGTERM', () => {
console.info("SIGTERM Received, exiting...")
process.exit(0)
})
// Handle APP ERRORS
process.on('uncaughtException', (error, origin) => {
console.log('----- Uncaught exception -----')
console.log(error)
console.log('----- Exception origin -----')
console.log(origin)
})
process.on('unhandledRejection', (reason, promise) => {
console.log('----- Unhandled Rejection at -----')
console.log(promise)
console.log('----- Reason -----')
console.log(reason)
})
function isObjNull(obj) {
for (const key in obj) {
return false;
};
return true;
}
const express = require('express');
const http = require('http');
const path = require("path");
const fs = require("fs");
const parser = require('ua-parser-js');
const app = express();
const port = 3001;
const publicRun = process.argv[2];
const nameGenerator = require('./name-generator.js');
app.get('*', (req, res) => {
const file = path.join(__dirname, 'public', req.url);
fs.stat(file, function (err, stat) {
if (!err && stat.isFile()) { //是文件
res.sendFile(file);
return;
}
if (/^\/[^\/]*\//.test(req.url)) { //如果字符串中包含了两个或以上的斜杆
req.url = req.url.match(/^\/[^\/]*/)[0]; //截取第一个二个斜杠之间的字符串
res.redirect(req.url);
} else {
res.sendFile(path.join(__dirname, 'public', '/index.html'));
}
});
});
const server = http.createServer(app);
(!publicRun == "public") ? server.listen(port) : server.listen(port, '0.0.0.0');
console.log(new Date().toISOString(), ' Snapchat is running on port', port);
class SnapchatServer {
constructor() {
const WebSocket = require('ws');
this._wss = new WebSocket.Server({ server });
this._wss.on('connection', (socket, request) => this._onConnection(new Peer(socket, request)));
this._wss.on('headers', (headers, response) => this._onHeaders(headers, response));
this._rooms = {};
this._msgs = {};
this._timer = {};
this._timeDelMsgs = 259200000;
}
_onConnection(peer) {
this._joinRoom(peer);
peer.socket.on('message', message => this._onMessage(peer, message));
this._keepAlive(peer);
this._sendDispName(peer);
}
_onHeaders(headers, response) {
let match;
if (response.headers.cookie) match = response.headers.cookie.match(/peerid=([0-9a-zA-Z]{8}-[0-9a-zA-Z]{4}-4[0-9a-zA-Z]{3}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{12})/);
if (match) {
response.peerId = match[1];
} else {
response.peerId = Peer.uuid();
}
headers.push('Set-Cookie: peerid=' + response.peerId + "; SameSite=Strict; Max-Age=259200"); //; Secure
}
_onMessage(sender, message) {
// Try to parse message
try {
message = JSON.parse(message);
} catch (e) {
return; // TODO: handle malformed JSON
}
switch (message.type) {
case 'disconnect':
this._leaveRoom(sender);
break;
case 'pong':
sender.lastBeat = Date.now();
break;
case 'getmsgs':
this._sendHistoryMsgs(sender, message.time);
break;
case 'pub':
this._pubMsg(sender, message);
break;
}
// relay message to recipient
if (message.to && this._rooms[sender.room]) {
const recipientId = message.to; // TODO: sanitize
const recipient = this._rooms[sender.room][recipientId];
delete message.to;
// add sender id
message.sender = sender.id;
this._send(recipient, message);
return;
}
}
_sendDispName(peer) {
this._send(peer, {
type: 'display-name',
dispName: peer.name.displayName,
peerId: peer.id
});
}
_pubMsg(sender, message) {
const serverTime = Date.now();
this._send(sender, {
type: 'msg-received',
id: message.time,
time: serverTime
});
message.time = serverTime; //将消息的时间戳替换成服务器时间
message.sender = sender.id;
for (const peerId in this._rooms[sender.room]) { //群发消息给其他群成员
if (sender.id != peerId) this._send(this._rooms[sender.room][peerId], message);
};
for (const timeId in this._msgs[sender.room]) { //删除72小时之前的记录
if (Date.now() - timeId > this._timeDelMsgs) {
delete this._msgs[sender.room][timeId];
} else {
break;
}
};
let delCmd = message.text.match(/^\/del:(.+)/);
if (delCmd) { //如果为删除指令
if (delCmd[1] == 'all') {
for (const timeId in this._msgs[sender.room]) { //删除所有记录
if (this._msgs[sender.room][timeId].sender == sender.id) {
this._msgs[sender.room][timeId].text = '<span class="chat-room-msg-text-del">[此消息已被删除]</span>';
}
}
} else {
for (const timeId in this._msgs[sender.room]) { //删除单条记录
if (timeId == delCmd[1] && sender.id == this._msgs[sender.room][timeId].sender) {
this._msgs[sender.room][timeId].text = '<span class="chat-room-msg-text-del">[此消息已被删除]</span>';
break;
}
}
}
} else { //如果不是指令,则添加到对象:_msgs[roomID],然后添加到数据库
if (!this._msgs[sender.room]) this._msgs[sender.room] = {};
this._msgs[sender.room][message.time] = {
sender: sender.id,
name: message.name,
text: message.text
}
}
}
_sendHistoryMsgs(peer, time) {
for (const timeId in this._msgs[peer.room]) {
if (Date.now() - timeId > this._timeDelMsgs) {
delete this._msgs[peer.room][timeId];
} else {
break;
}
}
if (time > 0) {
let msgs = {};
for (const timeId in this._msgs[peer.room]) {
if (timeId > time) {
msgs[timeId] = this._msgs[peer.room][timeId];
}
}
this._send(peer, {
type: 'history-msgs',
time: Date.now(),
msgs: msgs
});
} else {
this._send(peer, {
type: 'history-msgs',
time: Date.now(),
msgs: this._msgs[peer.room]
});
}
}
_joinRoom(peer) {
// if room doesn't exist, create it
if (!this._rooms[peer.room]) {
this._rooms[peer.room] = {};
//this._msgs[peer.room] = {};
this._send(peer, {
type: 'peers',
peers: []
});
} else {
// notify all other peers
const otherPeers = [];
const count = Object.keys(this._rooms[peer.room]).length + 1;
for (const peerId in this._rooms[peer.room]) {
if (peerId != peer.id) {
otherPeers.push(this._rooms[peer.room][peerId].getInfo());
this._send(this._rooms[peer.room][peerId], {
type: 'peer-joined',
peer: peer.getInfo(),
count: count
});
}
}
// notify peer about the other peers
this._send(peer, {
type: 'peers',
peers: otherPeers
});
}
// add peer to room
this._rooms[peer.room][peer.id] = peer;
if (this._timer[peer.room]) clearTimeout(this._timer[peer.room]); //有成员进群则取消清空消息定时器
}
_leaveRoom(peer) {
if (!this._rooms[peer.room] || !this._rooms[peer.room][peer.id]) return;
this._cancelKeepAlive(this._rooms[peer.room][peer.id]);
// delete the peer
delete this._rooms[peer.room][peer.id];
peer.socket.terminate();
//if room is empty, delete the room
if (isObjNull(this._rooms[peer.room])) { //最后一个群成员退出
if (isObjNull(this._msgs[peer.room])) { //如果消息记录为空,最后一个成员退出群聊之后直接删除房间和消息对象
delete this._rooms[peer.room];
delete this._msgs[peer.room];
} else { //如果消息记录不为空,最后一个成员退出房间之后设置定时器延时72小时删除房间和消息对象
this._timer[peer.room] = setTimeout(() => this._delMsgs(peer.room), this._timeDelMsgs);
}
} else {
const count = Object.keys(this._rooms[peer.room]).length;
// notify all other peers
for (const otherPeerId in this._rooms[peer.room]) {
const otherPeer = this._rooms[peer.room][otherPeerId];
this._send(otherPeer, {
type: 'peer-left',
peerId: peer.id,
peerName: peer.name.displayName,
count: count
});
}
}
}
_delMsgs(room) {
delete this._rooms[room];
delete this._msgs[room];
delete this._timer[room];
}
_send(peer, message) {
if (!peer) return;
if (this._wss.readyState !== this._wss.OPEN) return;
message = JSON.stringify(message);
peer.socket.send(message, error => '');
}
_keepAlive(peer) {
this._cancelKeepAlive(peer);
const timeout = 30000;
if (!peer.lastBeat) {
peer.lastBeat = Date.now();
}
if (Date.now() - peer.lastBeat > 2 * timeout) {
this._leaveRoom(peer);
return;
}
this._send(peer, { type: 'ping' });
peer.timerId = setTimeout(() => this._keepAlive(peer), timeout);
}
_cancelKeepAlive(peer) {
if (peer && peer.timerId) {
clearTimeout(peer.timerId);
}
}
}
class Peer {
constructor(socket, request) {
this.socket = socket;
this._setRoom(request);
this.id = request.peerId;
this.rtcSupported = request.url.lastIndexOf('/false') < 1;
this._setName(request);
this.timerId = 0;
this.lastBeat = Date.now();
//console.log(new Date().toISOString(), this.room, this.name.deviceName, 'RTC:', this.rtcSupported);
}
_setRoom(request) {
let match = request.url.match(/@([^\/]+)\//);
if (match) {
this.room = decodeURIComponent(match[1]);
} else {
this.room = '未命名';
}
}
toString() {
return `<Peer id=${this.id} ip=${this.room} rtcSupported=${this.rtcSupported}>`;
}
_setName(request) {
let ua = parser(request.headers['user-agent']);
let deviceName = '';
if (ua.os && ua.os.name) deviceName = ua.os.name.replace('Mac OS', 'Mac') + ' ';
if (ua.browser.name) deviceName += ua.browser.name;
if (!deviceName) deviceName = 'Unknown Device';
let displayName = '';
let match = request.url.match(/\/([^@]*)@/);
if (match) displayName = decodeURIComponent(match[1]);
if (!displayName) {
displayName = nameGenerator.getName(this.id);
}
this.name = {
model: ua.device.model,
os: ua.os.name,
browser: ua.browser.name,
type: ua.device.type,
deviceName,
displayName
}
}
getInfo() {
return {
id: this.id,
name: this.name,
rtcSupported: this.rtcSupported
}
}
// return uuid of form xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
static uuid() {
let uuid = '';
for (let i = 0; i < 32; i += 1) {
switch (i) {
case 8:
case 20:
uuid += '-';
uuid += (Math.random() * 16 | 0).toString(16);
break;
case 12:
uuid += '-';
uuid += '4';
break;
case 16:
uuid += '-';
uuid += (Math.random() * 4 | 8).toString(16);
break;
default:
uuid += (Math.random() * 16 | 0).toString(16);
}
}
return uuid;
}
}
const snapchatServer = new SnapchatServer();