Limit connections per IP

This commit is contained in:
calzoneman 2013-04-28 01:06:58 -05:00
parent 535b80153d
commit 7003101828
3 changed files with 23 additions and 4 deletions

View file

@ -14,3 +14,4 @@ exports.MYSQL_DB = "";
exports.MYSQL_USER = ""; exports.MYSQL_USER = "";
exports.MYSQL_PASSWORD = ""; exports.MYSQL_PASSWORD = "";
exports.IO_PORT = 1337; exports.IO_PORT = 1337;
exports.MAX_PER_IP = 10;

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.5", "version": "1.3.6",
"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.5"; const VERSION = "1.3.6";
var fs = require("fs"); var fs = require("fs");
var Logger = require("./logger.js"); var Logger = require("./logger.js");
@ -57,6 +57,7 @@ var Database = require("./database.js");
Database.init(); Database.init();
exports.channels = {}; exports.channels = {};
exports.clients = {};
fs.exists("chandump", function(exists) { fs.exists("chandump", function(exists) {
if(!exists) { if(!exists) {
@ -77,11 +78,28 @@ fs.exists("chanlogs", function(exists) {
}); });
exports.io.sockets.on("connection", function(socket) { exports.io.sockets.on("connection", function(socket) {
var user = new User(socket, socket.handshake.address.address); var ip = socket.handshake.address.address;
if(!(ip in exports.clients)) {
exports.clients[ip] = 1;
}
else {
exports.clients[ip]++;
}
if(exports.clients[ip] > Config.MAX_PER_IP) {
socket.emit("kick", {
reason: "Too many connections from your IP address"
});
exports.clients[ip]--;
socket.disconnect(true);
return;
}
socket.on("disconnect", function() {
exports.clients[ip]--;
});
var user = new User(socket, ip);
Logger.syslog.log("Accepted connection from /" + user.ip); Logger.syslog.log("Accepted connection from /" + user.ip);
}); });
process.on("uncaughtException", function(err) { process.on("uncaughtException", function(err) {
Logger.errlog.log("[SEVERE] Uncaught Exception: " + err); Logger.errlog.log("[SEVERE] Uncaught Exception: " + err);
}); });