-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
378 lines (351 loc) · 10.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
var config = require('./config.json');
var express = require('express');
var app = express();
var http = require('http');
var server = http.createServer(app);
var io = require('socket.io')(server);
var port = config.port || 4200;
var mysql = require('mysql');
var fs = require('fs');
var crypto = require('crypto');
var bodyParser = require('body-parser');
var path = require('path');
server.listen(port, function()
{
console.log('Server listening at port %d', port);
});
var connection = mysql.createConnection(config.mysql);
connection.connect();
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.urlencoded({extended: true}));
app.get('/logs/:channel/:year/:month/:day', function(req, res)
{
var ts = new Date(req.params.year, req.params.month-1, req.params.day, 0, 0, 0, 0).getTime()/1000;
connection.query('SELECT * FROM irc_lines WHERE channel = ? AND time >= ? AND time < ? ORDER BY line_number ASC', [req.params.channel, ts, ts+86400], function(err, rows, fields)
{
res.render('logviewer.ejs', {date: ts, lines: rows});
});
});
app.post('/admin', function(req, res)
{
var auth = {"nick":req.body.nick,"signature":req.body.signature,"uid":req.body.uid};
if(isAuthed(auth))
{
/*var opreq = http.request('http:'+config.checkLoginURL+"?op&u="+auth.uid+"&nick="+base64_encode(auth.nick), function(opres)
{
opres.on('data', function(chunk)
{*/
var group = req.body.group;
if(config.admins.indexOf(group) != -1)
res.render('admin.ejs', {config: config});
else
{
res.status(403);
//res.send("Wrong group: "+group);
res.sendFile(__dirname + '/public/403.html');
}
/*});
});
opreq.on('error', function(e)
{
res.status(503);
res.sendFile(__dirname + '/public/503.html');
});*/
}
else
{
res.status(403);
//res.send("Bad auth: "+auth);
res.sendFile(__dirname + '/public/403.html');
}
});
app.get('*', function(req, res)
{
res.status(404);
res.sendFile(__dirname + '/public/404.html');
});
app.use(function(err, req, res, next)
{
res.status(500);
res.render('500.ejs', {error: err, stack: err.stack});
});
var linenum = fs.readFileSync(config.curid, "utf-8");
function base64_encode(str)
{
return new Buffer(str, 'utf8').toString('base64').replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, ",");
}
function base64_decode(str)
{
return new Buffer(str.replace(/-/g, "+").replace(/_/g, "/").replace(/,/g, "="), 'base64').toString('utf8');
}
function isAuthed(auth)
{
try
{
var sig = auth.signature.split("|");
if(sig[0] > getTime()-86400)
{
var hash = crypto.createHmac('sha512', [config.network,config.key,sig[0]].join(""));
hash.update(auth.nick);
var digest = hash.digest('hex');
return digest === sig[1];
}
else return false;
}
catch(e)
{
return false;
}
}
function getTime()
{
return Math.floor(new Date().getTime()/1000);
}
io.on('connection', function(socket)
{
var ip = socket.request.headers['x-forwarded-for'] || socket.request.connection.remoteAddress;
var referer = socket.request.headers['referer'] || "THE GAME";
socket.on('settings', function(data)
{
connection.query('SELECT * FROM irc_vars', function(err, rows, fields)
{
if(err) return;
var settings = {};
if(rows.length > 0)
{
for(i=0;i<rows.length;i++)
{
switch(rows[i].type)
{
case 3:
case 5:
settings[rows[i].name] = JSON.parse(rows[i].value);
break;
default:
settings[rows[i].name] = rows[i].value;
}
}
}
settings.fileLimit = config.fileLimit;
settings.checkLoginURL = config.checkLoginURL;
settings.network = config.network;
socket.emit('settings',settings);
});
});
socket.on('lastlines', function(data)
{
connection.query('SELECT * FROM irc_lines WHERE channel = ? AND time >= ? ORDER BY line_number DESC LIMIT ?', [config.channel, Math.floor(new Date().getTime()/1000)-config.maxBacklog, data.lines], function(err, rows, fields)
{
if(err) return;
if(rows.length > 0)
{
for(i=rows.length-1;i>=0;i--)
{
var row = rows[i];
row.scroll = '';
if(rows.length == 1)
{
row.scroll = 'normal';
}
else
{
row.scroll = 'no';
}
socket.emit(row.type,row);
}
if(rows.length > 1)
{
socket.emit("last_row",null);
}
}
});
connection.query('SELECT * FROM irc_channels WHERE chan = ?', [config.channel], function(err, rows, fields)
{
if(err) return;
socket.emit('topics',rows[0]);
});
});
socket.on('lastcurid', function(data)
{
connection.query('SELECT * FROM irc_lines WHERE channel = ? AND time >= ? AND line_number > ? ORDER BY line_number DESC', [config.channel, Math.floor(new Date().getTime()/1000)-config.maxBacklog, data.curid], function(err, rows, fields)
{
if(err) return;
if(rows.length > 0)
{
for(i=rows.length-1;i>=0;i--)
{
var row = rows[i];
row.scroll = '';
if(rows.length == 1)
{
row.scroll = 'normal';
}
else
{
if(i > 0)
row.scroll = 'no';
else
row.scroll = 'force';
}
socket.emit(rows[i].type,row);
}
}
});
connection.query('SELECT * FROM irc_channels WHERE chan = ?', [config.channel], function(err, rows, fields)
{
if(err) return;
socket.emit('topics',rows[0]);
});
});
socket.on('userlist', function(data)
{
connection.query('SELECT * FROM irc_users WHERE isOnline = 1 AND channel = ? ORDER BY username ASC', [config.channel], function(err, rows, fields)
{
if(err) return;
socket.emit('userlist',{users: rows});
});
});
socket.on('auth', function(data)
{
var time = Math.floor(new Date().getTime()/1000);
var hash = crypto.createHmac('sha512', [config.key,time,config.network].join(""));
hash.update(referer);
var digest = hash.digest('hex');
socket.emit('auth', {checkLoginURL: config.checkLoginURL+"?sid="+digest+"|"+time+"&network="+config.network+"&jsoncallback=?"});
});
socket.on('message', function(data)
{
if(isAuthed(data.auth) && data.message && data.message != "")
{
var messages = data.message.trim().split("\n");
for(i in messages)
{
var message = messages[i].trim();
var action = data.action==1?"action":"message";
var time = getTime();
connection.query("UPDATE `irc_users` SET lastMsg = ? WHERE username = ? AND channel = ? AND online = ?", [time, data.auth.nick, config.channel, config.network]);
connection.query("INSERT INTO `irc_outgoing_messages` (message,nick,channel,action,fromSource,type) VALUES ?", [[[message, data.auth.nick, config.channel, data.action, config.network, action]]]);
connection.query("INSERT INTO `irc_lines` (name1,message,type,channel,time,online) VALUES ?", [[[data.auth.nick, message, action, config.channel, time, config.network]]], function(err, result)
{
if(err) return;
fs.writeFileSync(config.curid, result.insertId);
});
}
}
});
socket.on('join', function(data)
{
if(isAuthed(data.auth))
{
connection.query("INSERT INTO `irc_lines` (name1,type,channel,time,online) VALUES ?", [[[data.auth.nick, "join", config.channel, getTime(), config.network]]], function(err, result)
{
if(err) return;
fs.writeFileSync(config.curid, result.insertId);
});
}
});
socket.on('part', function(data)
{
if(isAuthed(data.auth))
{
connection.query("INSERT INTO `irc_lines` (name1,type,channel,time,online) VALUES ?", [[[data.auth.nick, "part", config.channel, getTime(), config.network]]], function(err, result)
{
if(err) return;
fs.writeFileSync(config.curid, result.insertId);
});
}
});
socket.on('file', function(data)
{
if(isAuthed(data.auth))
{
//console.log(data.info);
var hash = crypto.createHash('sha1');
hash.update(data.file);
var digest = hash.digest('hex');
connection.query('SELECT * FROM irc_uploads WHERE hash = ?', [digest], function(err, rows, fields)
{
if(err) return;
if(rows.length > 0)
{
socket.emit('file',{"url": config.imgurl+rows[0].filename});
}
else
{
if(/^image\//.test(data.info.type) && data.info.size <= config.fileLimit)
{
filename = path.basename(data.info.name);
var cont = false;
// while(cont)
// {
connection.query('SELECT * FROM irc_uploads WHERE filename = ?', [filename], function(err, rows, fields)
{
if(err) return;
if(rows.length > 0)
{
filename = filename.replace(/(\D+)(-?\d*)(\.\D+)$/, function(str, m1, m2, m3)
{
if(m2)
var newstr = (Math.abs(+m2) + 1) + "";
else
var newstr = "1";
return m1 + newstr + m3;
});
}
else
{
fs.writeFile('uploads/'+filename, data.file, function(err, fdata)
{
if(err) return;
connection.query("INSERT INTO `irc_uploads` (nick,filename,hash,time) VALUES ?", [[[data.auth.nick, filename, digest, getTime()]]], function(err, result)
{
socket.emit('file',{"url": config.imgurl+filename});
});
});
//cont = true;
}
});
//}
}
}
});
}
});
});
fs.watch(config.curid, function(event, filename)
{
var line = fs.readFileSync(config.curid, "utf-8");
if(event == 'change' && line != linenum && line != "")
{
connection.query('SELECT * FROM irc_lines WHERE channel = ? AND line_number > ? ORDER BY line_number ASC', [config.channel, linenum], function(err, rows, fields) {
if(err) return;
if(rows.length > 0)
{
for(i=0;i<rows.length;i++)
{
var row = rows[i];
row.scroll = '';
if(rows.length == 1)
{
row.scroll = 'normal';
}
else
{
if(i > 0)
row.scroll = 'no';
else
row.scroll = 'force';
}
io.sockets.emit(rows[i].type,row);
}
}
});
linenum = line;
}
});
process.on('SIGINT', function()
{
console.log("\nGracefully shutting down from SIGINT (Ctrl-C)");
connection.end();
process.exit();
});