This repository has been archived by the owner on Jan 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
/
index.js
136 lines (113 loc) · 3.18 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
'use strict';
var Hapi = require('hapi');
var nconf = require('nconf');
var SocketIO = require('socket.io');
var crypto = require('crypto');
var services = require('./lib/services');
nconf.argv().env().file({ file: 'local.json' });
var users = 0;
var server = Hapi.createServer(nconf.get('domain'), nconf.get('port'));
server.views({
engines: {
jade: require('jade')
},
isCached: process.env.node === 'production',
path: __dirname + '/views',
compileOptions: {
pretty: true
}
});
var routes = [
{
method: 'GET',
path: '/',
config: {
handler: home
}
}
];
server.route(routes);
server.route({
path: '/{path*}',
method: "GET",
config: {
handler: {
directory: {
path: './dist',
listing: false,
index: false
}
}
}
});
server.start(function () {
var io = SocketIO.listen(server.listener);
var getUserId = function (fingerprint, ip) {
return crypto.createHash('md5').update(fingerprint + ip).digest('hex');
};
var disconnectHandler = function() {
users--;
if (users < 0) {
users = 0;
}
io.emit('active', users);
};
io.on('connection', function (socket) {
socket.on('disconnect', disconnectHandler);
users++;
io.emit('active', users);
socket.on('join', function (format) {
socket.join(format);
services.recent(socket, format);
});
var ip = socket.handshake.address;
if (socket.handshake.headers['x-forwarded-for']) {
ip = socket.handshake.headers['x-forwarded-for'].split(/ *, */)[0];
}
socket.on('message', function (data) {
if (typeof data === 'string') {
// Handle legacy clients that nonsensically double-JSON-encoded
// TODO(tec27): remove this code when we're sure no clients are doing this any more
if (data.length > 1 * 1024 * 1024 /* 1MB */) {
console.log('Oversized message received: ' + (data.length / (1024 * 1024)) + 'MB');
return socket.emit('messageack', 'Message too large');
}
try {
data = JSON.parse(data);
} catch (err) {
console.log('Received malformed JSON');
return socket.emit('messageack', 'Malformed JSON');
}
}
var ackData = { key: data.key };
if (!data.fingerprint || data.fingerprint.length > 32) {
return socket.emit('messageack', 'Invalid fingerprint', ackData);
}
var userId = getUserId(data.fingerprint, ip);
ackData.userId = userId;
var payload = {
message: data.message,
media: data.media,
fingerprint: userId
};
services.addMessage(payload, function (err, chat) {
if (err) {
console.log('error ', err);
return socket.emit('messageack', 'Error adding message', ackData);
}
socket.emit('messageack', null, ackData);
var videoData = chat.media;
var formats = ['webm', 'mp4'];
formats.forEach(function (format) {
chat.media = videoData[format];
io.sockets.in(format).emit('message', chat);
});
});
});
});
});
function home(request, reply) {
reply.view('index', {
analytics: nconf.get('analytics')
});
}