Add global bans

This commit is contained in:
calzoneman 2013-04-28 01:30:18 -05:00
parent 7003101828
commit 3730ae0066
3 changed files with 50 additions and 6 deletions

View File

@ -74,11 +74,49 @@ exports.init = function() {
return false; return false;
} }
var query = "CREATE TABLE IF NOT EXISTS `global_bans` \
(`ip` VARCHAR( 15 ) NOT NULL ,\
`note` VARCHAR( 255 ) NOT NULL ,\
PRIMARY KEY (`ip`)\
) ENGINE = MYISAM";
var results = db.querySync(query);
if(!results) {
Logger.errlog.log("database.init: global ban table init failed!");
return false;
}
initialized = true; initialized = true;
db.closeSync(); db.closeSync();
return true; return true;
} }
var gbanTime = 0;
var gbans = {};
exports.checkGlobalBan = function(ip) {
// Check database at most once per 5 minutes
if(new Date().getTime() > gbanTime + 300000) {
var db = exports.getConnection();
if(!db) {
return [];
}
// Check if channel exists
var query = "SELECT * FROM global_bans WHERE 1";
var results = db.querySync(query);
if(!results) {
Logger.errlog.log("loadGlobalBans: query failed");
return [];
}
var rows = results.fetchAllSync();
gbans = {};
for(var i = 0; i < rows.length; i++) {
gbans[rows[i].ip] = true;
}
db.closeSync();
gbanTime = new Date().getTime();
}
return (ip in gbans);
}
exports.loadChannel = function(chan) { exports.loadChannel = function(chan) {
var db = exports.getConnection(); var db = exports.getConnection();
if(!db) { if(!db) {

View File

@ -2,7 +2,7 @@
"author": "Calvin Montgomery", "author": "Calvin Montgomery",
"name": "CyTube", "name": "CyTube",
"description": "Online media synchronizer and chat", "description": "Online media synchronizer and chat",
"version": "1.3.6", "version": "1.4.0",
"repository": { "repository": {
"url": "http://github.com/calzoneman/sync" "url": "http://github.com/calzoneman/sync"
}, },

View File

@ -9,7 +9,7 @@ The above copyright notice and this permission notice shall be included in all c
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
const VERSION = "1.3.6"; const VERSION = "1.4.0";
var fs = require("fs"); var fs = require("fs");
var Logger = require("./logger.js"); var Logger = require("./logger.js");
@ -79,6 +79,16 @@ fs.exists("chanlogs", function(exists) {
exports.io.sockets.on("connection", function(socket) { exports.io.sockets.on("connection", function(socket) {
var ip = socket.handshake.address.address; var ip = socket.handshake.address.address;
if(Database.checkGlobalBan(ip)) {
socket.emit("kick", {
reason: "You're globally banned!"
});
socket.disconnect(true);
return;
}
socket.on("disconnect", function() {
exports.clients[ip]--;
});
if(!(ip in exports.clients)) { if(!(ip in exports.clients)) {
exports.clients[ip] = 1; exports.clients[ip] = 1;
} }
@ -89,13 +99,9 @@ exports.io.sockets.on("connection", function(socket) {
socket.emit("kick", { socket.emit("kick", {
reason: "Too many connections from your IP address" reason: "Too many connections from your IP address"
}); });
exports.clients[ip]--;
socket.disconnect(true); socket.disconnect(true);
return; return;
} }
socket.on("disconnect", function() {
exports.clients[ip]--;
});
var user = new User(socket, ip); var user = new User(socket, ip);
Logger.syslog.log("Accepted connection from /" + user.ip); Logger.syslog.log("Accepted connection from /" + user.ip);
}); });