forked from emgram769/livechan-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.js
772 lines (660 loc) · 21.3 KB
/
web.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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
/* required modules */
var express = require("express");
var logfmt = require("logfmt");
var http = require('http');
var https = require('https');
var crypto = require('crypto');
var ipfilter = require('ipfilter');
var captcha = require('captcha');
var tripcode = require('tripcode');
var fs = require('fs');
var format = require('util').format;
var mongoose = require('mongoose');
var exec = require('child_process').exec;
var useImageMagick = true;
/* salt */
var securetrip_salt = "AVEPwfpR4K8PXQaKa4PjXYMGktC2XY4Qt59ZnERsEt5PzAxhyL";
fs.readFile('salt.txt', 'utf8', function (err, data) {
"use strict";
if (!err) {
securetrip_salt = data;
}
});
/* initialize app */
var app = express();
var port = process.env.PORT || 5000;
var secure_port = 443;
/* listen now */
var server = http.createServer(app).listen(port, function () {
"use strict";
console.log('Express server listening on port %d in %s mode', server.address().port, app.settings.env);
});
var options = {
ca: fs.readFileSync('sub.class1.server.ca.pem'),
key: fs.readFileSync('ssl.key'),
cert: fs.readFileSync('ssl.crt')
};
var secure_server = https.createServer(options, app).listen(secure_port, function () {
"use strict";
console.log('Express secure_server listening on port %d in %s mode', secure_server.address().port, app.settings.env);
});
var io = require('socket.io').listen(secure_server);
/* set up db */
mongoose.connect('mongodb://localhost/livechan_db');
var Schema = mongoose.Schema;
/* 5mb limit */
app.use(express.limit('5mb'));
/* for saving images */
app.use(express.bodyParser({
uploadDir: 'public/tmp/uploads',
keepExtensions: true
}));
/* blocked nodes. this file can include more */
fs.readFile('tor_list.txt', 'utf8', function (err, data) {
"use strict";
if (err) {
return;
}
var tor_list = data.split("\n"); // IP per line
app.use(ipfilter(tor_list));
});
/* logging */
app.use(logfmt.requestLogger());
/* serve public data (/public/*) and get cookies */
app.use(express.static(__dirname + '/public'));
app.use(express.cookieParser());
app.use(express.cookieSession({
secret: 'keyboard-cat'
}));
/* captcha security */
app.use(captcha({
url: '/captcha.jpg',
color: '#465e67',
background: '#800000'
}));
/* stored data in memory */
var count;
var hash_list = [];
var session_list = [];
var ips = {};
var boards = ['all', 'a', 'b', 'c', 'd', 'e', 'f', 'film', 'g', 'gif', 'h', 'hr', 'k', 'm', 'o', 'p', 'r', 's', 't', 'u', 'v', 'vg', 'vr', 'w', 'wg', 'i', 'ic', 'r9k', 's4s', 'cm', 'hm', 'lgbt', 'y', '3', 'adv', 'an', 'asp', 'cgl', 'ck', 'co', 'diy', 'fa', 'fit', 'gd', 'hc', 'int', 'jp', 'lit', 'mlp', 'mu', 'n', 'out', 'po', 'pol', 'sci', 'soc', 'sp', 'tg', 'toy', 'trv', 'tv', 'vp', 'waifu', 'wsg', 'x', 'dev', 'tech', 'prog','dogecoin','fedoracoin', 'coin', 'q'];
/* database fields to transmit */
var all_fields = 'chat name body convo convo_id count date trip';
var board_fields = 'chat name body convo convo_id count date image image_filename image_filesize image_width image_height thumb trip';
/* db schema */
var chat_schema = new Schema({
convo: String,
convo_id: Number,
is_convo_op: Boolean,
body: String,
name: String,
count: {
type: Number,
unique: true
},
date: {
type: Date,
default: Date.now
},
ip: String,
chat: String,
image: String,
image_filename: String,
image_filesize: Number,
image_width: Number,
image_height: Number,
thumb: String,
trip: String
});
var user_schema = new Schema({
date: { /* user session expires every 24 hours */
type: Date,
default: Date.now,
expires: '24h'
},
ip: String,
session_key: String,
last_post: {
type: Date,
default: Date.now
},
banned_rooms: Array /* list of rooms the user is banned from */
});
var chat_db = mongoose.model('chat_db', chat_schema);
var user_db = mongoose.model('user_db', user_schema);
/* set counter to newest post */
chat_db.findOne().sort({
count: -1
}).exec(function (e, d) {
"use strict";
if (d) {
count = d.count;
}
});
/* helper functions */
function get_extension(filename) {
"use strict";
var i = filename.lastIndexOf('.');
return (i < 0) ? '' : filename.substr(i + 1).toLowerCase();
}
function invalid_extension(filename) {
"use strict";
var test = ['jpg', 'jpeg', 'png', 'gif', 'ogv', 'webm'].indexOf(get_extension(filename));
if (test > -1) {
return false;
}
return true;
}
function is_video(filename) {
"use strict";
return (['ogv', 'webm'].indexOf(get_extension(filename)) > -1);
}
function delete_posts(e, ds) {
"use strict";
if (e) {
console.log("chat find error", e);
return;
}
ds.forEach(function(d) {
if (d.image) fs.unlink(d.image);
if (d.thumb) fs.unlink(d.thumb);
d.remove(function(e2) {console.log("chat removal error",e2);});
});
}
function send_data(room, event, data, fields) {
"use strict";
var fields_array = fields.split(" ");
var data2 = {};
for (var key in data) {
if (fields_array.indexOf(key) > -1) {
data2[key] = data[key];
}
}
io.sockets.in(room).emit(event, data2);
}
function add_to_chat(data) {
"use strict";
send_data(data.chat, 'chat', data, board_fields);
send_data('all', 'chat', data, all_fields);
/* store in the db */
chat_db.update({count: data.count}, data, {upsert: true}, function(err) {
if (err) {
console.log("chat save error", err);
return;
}
chat_db.find({chat: data.chat, is_convo_op: false})
.sort({count: -1})
.skip(100)
.exec(delete_posts);
chat_db.find({chat: data.chat, is_convo_op: true})
.sort({count: -1})
.skip(100)
.exec(delete_posts);
});
}
function session_exists(session) {
"use strict";
if (user_db.count({
session_key: session
})) {
return true;
}
return false;
}
/* check_ip_validity:
- checks if ip is banned
- checks session validity
- checks cool down
calls format_chat(req, res, next, add_to_chat) on success
*/
function check_ip_validity(req, res, next) {
/* get IP */
var user_ip = req.connection.remoteAddress;
var ip_addr = req.headers["x-forwarded-for"];
if (ip_addr) {
var list = ip_addr.split(",");
user_ip = list[list.length - 1];
}
/* lookup IP */
user_db.find({ip:user_ip})
.sort({last_post:-1})
.exec(function (e, d) {
console.log(d);
if(e) {
res.json({ failure: "session_expiry" });
return;
}
else if(d[0]) {
if (req.cookies.password_livechan != d[0].session_key) {
res.json({ failure: "session_expiry" });
return;
} else if ((d[0].last_post.getTime() + 5000) > (new Date).getTime()) {
var now = new Date;
user_db.update(
{ _id: d[0]._id },
{ $set: {last_post: now.setTime(now.getTime() + 10000)}},
function(){
res.json({ failure: "countdown_violation" });
return;
});
return;
} else if (d[0].banned_rooms.indexOf(req.params.id) > -1) {
res.json({ failure: "ban_violation" });
return;
} else {
var now = new Date;
user_db.update(
{ _id: d[0]._id },
{ $set: {last_post: now}},
function(){
console.log("formatting image...");
format_image(req, res, next, add_to_chat);
});
/* update cool down here */
}
} else {
res.json({ failure: "session_expiry" });
return;
}
});
}
/* format_image
- checks for image and sets data accordingly
calls generate_thumbnail(req, res, next, data, callback) in case of image/video
skips to format_post(req, res, next, data, callback) otherwise
*/
function format_image(req, res, next, callback) {
var data = {}; /* to be stored in db and passed to clients */
/* no image uploaded */
if (req.files && req.files.image &&
req.files.image.size === 0 || invalid_extension(req.files.image.path)) {
fs.unlink(req.files.image.path); /* delete blank file */
if (/^\s*$/.test(req.body.body)) {
res.json({ failure: "nothing substantial submitted" });
return;
} else {
console.log("formatting post...");
format_post(req, res, next, data, callback)
}
}
/* video uploaded */
else if (is_video(req.files.image.path)) {
var command = "ffprobe -print_format json -show_streams " + req.files.image.path;
console.log("executing", command);
exec(command, function(err, stdout, stderr) {
if (err) {
console.log("ffmpeg size error", err);
return;
}
try {
var stream_data = JSON.parse(stdout);
} catch(e) {
console.log("ffmpeg size error", stderr);
return;
}
var video_stream = null;
for (var i in stream_data.streams) {
if (stream_data.streams[i].codec_type === "video") {
video_stream = stream_data.streams[i];
break;
}
}
if (video_stream !== null && video_stream.width && video_stream.height) {
data.image = req.files.image.path;
data.image_filename = req.files.image.originalFilename;
data.image_filesize = fs.statSync(data.image).size;
data.image_width = video_stream.width;
data.image_height = video_stream.height;
console.log("generating thumbnail...");
generate_thumbnail(req, res, next, data, callback)
} else {
console.log("ffmpeg could not find video stream", stderr);
return;
}
});
}
/* image uploaded */
else {
var gm = require('gm').subClass({
imageMagick: useImageMagick // Necessary for hosted version, remove if you're using graphicmagick
});
gm(req.files.image.path).size(function (err, dimensions) {
if (err) {
console.log("gm size error", err);
return;
}
if (dimensions.height > 0 && dimensions.width > 0) {
data.image = req.files.image.path;
data.image_filename = req.files.image.originalFilename;
data.image_filesize = fs.statSync(data.image).size;
data.image_width = dimensions.width;
data.image_height = dimensions.height;
}
console.log("generating thumbnail...");
generate_thumbnail(req, res, next, data, callback)
});
}
}
/* generate_thumbnail
- generates thumbnail for image
calls format_post(req, res, next, data, callback) on completion
*/
function generate_thumbnail(req, res, next, data, callback) {
var scale = Math.min(250/data.image_width, 100/data.image_height, 1);
var thumb_width = Math.round(scale * data.image_width);
var thumb_height = Math.round(scale * data.image_height);
data.thumb = "public/tmp/thumb/" + data.image.match(/([\w\-]+)\.\w+$/)[1] + ".jpg";
if (is_video) {
var command = "ffmpeg -i "+req.files.image.path+" -s "+thumb_width+"x"+thumb_height+" -vframes 1 "+data.thumb;
console.log("executing", command);
exec(command, function(err, stdout, stderr) {
if (err) {
console.log("thumbnail creation error", err);
return;
}
console.log("formatting post...");
format_post(req, res, next, data, callback);
});
} else {
var gm = require('gm').subClass({
imageMagick: useImageMagick // Necessary for hosted version, remove if you're using graphicmagick
});
gm(data.image)
.out("-delete", "1--1") // use first frame only; only needed for ImageMagick
.thumb(thumb_width, thumb_height, data.thumb, function(err) {
if (err) {
console.log("thumbnail creation error", err);
return;
}
console.log("formatting post...");
format_post(req, res, next, data, callback);
});
}
}
/* format_post:
- checks for text violations
- generates tripcode
calls callback(data) on success
*/
function format_post(req, res, next, data, callback) {
/* length checks */
if (req.body.body.length > 400) {
req.body.body = req.body.body.substring(0, 399) + "...";
}
if (req.body.body.split("\n").length > 8) {
req.body.body = req.body.body.split("\n", 8).join("\n");
}
if (req.body.name.length > 40) {
req.body.name = req.body.name.substring(0, 39) + "...";
}
if (req.body.convo) {
if (req.body.convo.length > 40) {
req.body.convo = req.body.convo.substring(0, 39) + "...";
}
} else {
req.body.convo = 'General'; /* default conversation */
}
/* generate tripcode */
var trip_index = req.body.name.indexOf("#");
var trip;
if (trip_index > -1) {
var trip = req.body.name.substr(trip_index + 1);
var secure = trip.indexOf("#") === 0;
if (secure) {
trip = crypto.createHash('sha1').update(trip.substr(1) +
securetrip_salt).digest('base64').toString();
}
data.trip = (secure ? "!!" : "!") + tripcode(trip);
req.body.name = req.body.name.slice(0, trip_index);
}
/* everything looks good, we can safely add this chat */
count++; /* counter has been promoted */
data.count = count;
data.convo = req.body.convo;
data.body = req.body.body;
data.name = req.body.name || "Anonymous";
data.date = (new Date()).toString();
var user_ip = req.connection.remoteAddress;
var ip_addr = req.headers["x-forwarded-for"];
if (ip_addr) {
var list = ip_addr.split(",");
user_ip = list[list.length - 1];
}
data.ip = user_ip;
data.chat = req.params.id;
/* determine if OP of conversation topic */
if (data.convo && data.convo !== "General") {
chat_db.findOne({
convo: data.convo,
chat: data.chat,
is_convo_op: true
}).exec(function (err, convo_ent) {
if (!convo_ent) {
data.is_convo_op = true;
data.convo_id = data.count;
/* cool down increase */
} else {
data.is_convo_op = false;
data.convo_id = convo_ent.count;
}
callback(data);
});
} else {
callback(data);
}
console.log("everything done.");
/* give the client information about the post */
res.json({
success: "success_posting",
id: data.count
});
return;
}
/* REQUESTS */
app.all('*',function(req,res,next){
if(!req.connection.encrypted)
res.redirect('https://livechan.net'+req.url);
else
next();
});
app.get('/login', function (req, res) {
"use strict";
res.send('<html><head><meta name="viewport" content="width=device-width,user-scalable=no"><link type="text/css" rel="stylesheet" href="style.css"></head><body><div class="center container"><div>Please prove you aren\'t a robot</div><br/><img src="/captcha.jpg"/><form action="/login" method="post"><br/><input type="text" name="digits"/><input type="hidden" name="page" value="' + req.query.page + '"/></form></div></body></html>');
});
app.post('/login', function (req, res) {
"use strict";
res.type("text/plain");
var user_ip = req.connection.remoteAddress;
var ip_addr = req.headers["x-forwarded-for"];
if (ip_addr) {
var list = ip_addr.split(",");
user_ip = list[list.length - 1];
}
if (req.body.digits === req.session.captcha) {
var key = (Math.random() * 1e17).toString(36);
var info = req.headers['user-agent'] + user_ip + key;
var password = crypto.createHash('sha1').update(info).digest('base64').toString();
console.log("password", password);
res.cookie('password_livechan', password, {
maxAge: 9000000,
httpOnly: false
});
if (req.body.page) {
res.redirect(req.body.page);
} else {
res.json({
success: "captcha"
});
}
var now = new Date;
var data = {
session_key: password,
ip: user_ip,
last_post: now.setTime(now.getTime() - 6000)
};
new user_db(data).save(function () {
/*user_db.find().exec(function (e, d) {
console.log("session found", e, d);
});*/
});
return;
}
if (req.body.page) {
res.send("You mistyped the captcha!");
} else {
res.json({
failure: "You mistyped the captcha."
});
}
});
app.get('/', function (req, res) {
"use strict";
res.redirect('/chat/home');
});
app.get('/chat/:id([a-z0-9]+)', function (req, res) {
"use strict";
if (boards.indexOf(req.params.id) < 0 && req.params.id !== "home") {
res.send("Board doesn't exist :(");
return;
}
res.sendfile('pages/index.html');
return;
});
app.get('/data:ops((?:_convo)?)/:id([a-z0-9]+)', function (req, res) {
"use strict";
if (req.params.id !== "all" && boards.indexOf(req.params.id) < 0) {
res.send("Does not exist :(");
return;
}
var search = {};
var limit = 0;
var fields = "";
if (req.params.id === "all") {
limit = 30;
fields = all_fields;
} else {
search.chat = req.params.id;
limit = 150;
fields = board_fields;
}
if (req.params.ops === "_convo") {
search.is_convo_op = true;
limit = 30;
}
chat_db.find(search)
.sort({
count: -1
})
.select(fields)
.limit(limit)
.exec(function (e, d) {
if (!e) {
res.json(d);
} else {
res.send('db_error');
}
});
});
app.post('/ban/:password([a-z0-9]+)/:id([0-9]+)/:type/:board', function (req, res, next) {
"use strict";
var chat_id = req.params.id;
var password = req.params.password;
var type = req.params.text;
var board = req.params.board;
var hash_pass = crypto.createHash('sha1').update(password).digest('base64');
if (hash_pass!="Nqesm9E+3GXfOG0KgJq8YmizCho="){
console.log("ATTEMPTED PASS", password);
res.json({failure:"wrong password"});
return;
}
res.json({success:"right password"});
return;
});
app.get('/delete/:password([a-z0-9]+)/:id([0-9]+)', function (req, res, next) {
"use strict";
var chat_id = req.params.id;
var password = req.params.password;
var hash_pass = crypto.createHash('sha1').update(password).digest('base64');
if (hash_pass!="Nqesm9E+3GXfOG0KgJq8YmizCho="){
console.log("ATTEMPTED PASS", password);
res.json({failure:"wrong password"});
return;
}
chat_db.update(
{count:chat_id},
{$set:
{body:"deleted",
convo:"General",
name:"deleted",
image_filename:""}
},function(err){
if(!err) {
chat_db.find({count:chat_id},
function(e,d){
if(d[0] && d[0].image) {
fs.unlink(d[0].image);
fs.unlink(d[0].thumb);
}
d[0].image = "";
add_to_chat(d[0]);
})
}
});
res.json({success:"deleted "+chat_id});
return;
});
app.get('/set/:password([a-z0-9]+)/:id([0-9]+)/:text', function (req, res, next) {
"use strict";
var chat_id = req.params.id;
var password = req.params.password;
var text = decodeURI(req.params.text);
var hash_pass = crypto.createHash('sha1').update(password).digest('base64');
if (hash_pass!="Nqesm9E+3GXfOG0KgJq8YmizCho="){
console.log("ATTEMPTED PASS", password);
res.json({failure:"wrong password"});
return;
}
chat_db.update(
{count:chat_id},
{$set:
{body: text}
},function(err){
if(!err) {
chat_db.find({count:chat_id},
function(e,d){
add_to_chat(d[0]);
})
}
});
res.json({success:"reset "+chat_id});
return;
});
app.post('/chat/:id([a-z0-9]+)', function (req, res, next) {
"use strict";
res.type("text/plain");
if (req.params.id !== "all" && boards.indexOf(req.params.id) < 0) {
res.json({failure: "This board does not exist."});
return;
}
check_ip_validity(req, res, next);
return;
});
/* 404 */
app.get('*', function(req, res){
res.status(404).sendfile('pages/404.html');
});
/* 404 */
app.post('*', function(req, res){
res.status(404).sendfile('pages/404.html');
});
/* join a chat room */
io.sockets.on('connection', function (socket) {
"use strict";
socket.emit('request_location', "please return a chat room");
socket.on('subscribe', function (data) {
socket.join(data);
});
socket.on('unsubscribe', function (data) {
socket.leave(data);
});
});