Skip to content

Instantly share code, notes, and snippets.

@pawnhearts
Created September 20, 2017 13:51
Show Gist options
  • Save pawnhearts/93ab18722c02ce6b3b8b7f9193c6e28f to your computer and use it in GitHub Desktop.
Save pawnhearts/93ab18722c02ce6b3b8b7f9193c6e28f to your computer and use it in GitHub Desktop.
'use strict';
var fs = require('fs');
var user_db = require('../models/user');
var ban_db = require('../models/ban');
var get_user_ip = require('./get-user-ip');
var path = require('path');
var config = require('../../config');
var root = path.join(__dirname, '../..');
var no_limit_cookie_file = path.join(root, config.no_limit_cookie_file);
var no_limit_cookie_string = '';
/* check_ip_validity:
- checks if ip is banned
- checks session validity
- checks cool down
calls callback(err) on completion
*/
module.exports = function(req, callback) {
/* get IP */
var user_ip = get_user_ip(req);
ban_db
.find({
})
.exec(function(e, d) {
for(var i=0; i<d.length; i++){
//console.log(user_ip+' '+d[i].ip+' '+user_ip.indexOf(d[i].ip));
if(user_ip.indexOf(d[i].ip)==0){
return callback(new Error('ban_violation'));
}
}
})
/* lookup IP */
user_db
.find({
ip: user_ip
})
.sort({
last_post: -1
})
.exec(function(e, d) {
for(var i=0; i<d.length; i++){
if (d[i].banned_rooms.indexOf(req.params.id) > -1) {
//console.log(d[i]);
return callback(new Error('ban_violation'));
}
}
if (e || !d[0] || (req.cookies.password_livechan !== d[0].session_key)) {
return callback(new Error('session_expiry'));
} else if ((d[0].last_post.getTime() + 3000) > (new Date()).getTime()) {
var now = new Date();
return user_db.update({
_id: d[0]._id
}, {
$set: {
last_post: now.setTime(now.getTime() + 10000)
}
},
function(err) {
if (err) return callback(new Error('database_error'));
return callback(new Error('countdown_violation'));
}
);
return;
} else if ((d[0].banned_rooms.indexOf(req.params.id) > -1) ||
(d[1] && d[1].banned_rooms && d[1].banned_rooms.indexOf(req.params.id) > -1)) {
return callback(new Error('ban_violation'));
} else {
var now = new Date();
return user_db.update({
_id: d[0]._id
}, {
$set: {
last_post: now
}
},
function(err) {
if (err) return callback(new Error('database_error'));
return callback();
}
);
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment