ensure wrong socketio messages doesn't kill the server

This commit is contained in:
Peter 'Pita' Martischka 2011-08-17 17:28:30 +01:00
parent 296777ab53
commit 77ba4e3e6c
3 changed files with 63 additions and 39 deletions

View File

@ -21,9 +21,10 @@
var ueberDB = require("ueberDB"); var ueberDB = require("ueberDB");
var settings = require("../utils/Settings"); var settings = require("../utils/Settings");
var log4js = require('log4js');
//set database settings //set database settings
var db = new ueberDB.database(settings.dbType, settings.dbSettings); var db = new ueberDB.database(settings.dbType, settings.dbSettings, null, log4js.getLogger("ueberDB"));
/** /**
* The UeberDB Object that provides the database functions * The UeberDB Object that provides the database functions

View File

@ -26,6 +26,8 @@ var authorManager = require("../db/AuthorManager");
var readOnlyManager = require("../db/ReadOnlyManager"); var readOnlyManager = require("../db/ReadOnlyManager");
var settings = require('../utils/Settings'); var settings = require('../utils/Settings');
var securityManager = require("../db/SecurityManager"); var securityManager = require("../db/SecurityManager");
var log4js = require('log4js');
var messageLogger = log4js.getLogger("message");
/** /**
* A associative array that translates a session to a pad * A associative array that translates a session to a pad
@ -64,10 +66,7 @@ exports.setSocketIO = function(socket_io)
* @param client the new client * @param client the new client
*/ */
exports.handleConnect = function(client) exports.handleConnect = function(client)
{ {
//check if all ok
throwExceptionIfClientOrIOisInvalid(client);
//Initalize session2pad and sessioninfos for this new session //Initalize session2pad and sessioninfos for this new session
session2pad[client.id]=null; session2pad[client.id]=null;
sessioninfos[client.id]={}; sessioninfos[client.id]={};
@ -95,10 +94,7 @@ exports.kickSessionsFromPad = function(padID)
* @param client the client that leaves * @param client the client that leaves
*/ */
exports.handleDisconnect = function(client) exports.handleDisconnect = function(client)
{ {
//check if all ok
throwExceptionIfClientOrIOisInvalid(client);
//save the padname of this session //save the padname of this session
var sessionPad=session2pad[client.id]; var sessionPad=session2pad[client.id];
@ -156,9 +152,6 @@ exports.handleDisconnect = function(client)
*/ */
exports.handleMessage = function(client, message) exports.handleMessage = function(client, message)
{ {
//check if all ok
throwExceptionIfClientOrIOisInvalid(client);
if(message == null) if(message == null)
{ {
throw "Message is null!"; throw "Message is null!";
@ -197,7 +190,7 @@ exports.handleMessage = function(client, message)
//if the message type is unkown, throw an exception //if the message type is unkown, throw an exception
else else
{ {
throw "unkown Message Type: '" + message.type + "'"; messageLogger.warn("Droped message, unkown Message Type " + message.type);
} }
} }
@ -276,11 +269,13 @@ function handleSuggestUserName(client, message)
//check if all ok //check if all ok
if(message.data.payload.newName == null) if(message.data.payload.newName == null)
{ {
throw "suggestUserName Message has no newName!"; messageLogger.warn("Droped message, suggestUserName Message has no newName!");
return;
} }
if(message.data.payload.unnamedId == null) if(message.data.payload.unnamedId == null)
{ {
throw "suggestUserName Message has no unnamedId!"; messageLogger.warn("Droped message, suggestUserName Message has no unnamedId!");
return;
} }
var padId = session2pad[client.id]; var padId = session2pad[client.id];
@ -306,7 +301,8 @@ function handleUserInfoUpdate(client, message)
//check if all ok //check if all ok
if(message.data.userInfo.colorId == null) if(message.data.userInfo.colorId == null)
{ {
throw "USERINFO_UPDATE Message has no colorId!"; messageLogger.warn("Droped message, USERINFO_UPDATE Message has no colorId!");
return;
} }
//Find out the author name of this session //Find out the author name of this session
@ -349,15 +345,18 @@ function handleUserChanges(client, message)
//check if all ok //check if all ok
if(message.data.baseRev == null) if(message.data.baseRev == null)
{ {
throw "USER_CHANGES Message has no baseRev!"; messageLogger.warn("Droped message, USER_CHANGES Message has no baseRev!");
return;
} }
if(message.data.apool == null) if(message.data.apool == null)
{ {
throw "USER_CHANGES Message has no apool!"; messageLogger.warn("Droped message, USER_CHANGES Message has no apool!");
return;
} }
if(message.data.changeset == null) if(message.data.changeset == null)
{ {
throw "USER_CHANGES Message has no changeset!"; messageLogger.warn("Droped message, USER_CHANGES Message has no changeset!");
return;
} }
//get all Vars we need //get all Vars we need
@ -579,19 +578,23 @@ function handleClientReady(client, message)
//check if all ok //check if all ok
if(!message.token) if(!message.token)
{ {
throw "CLIENT_READY Message has no token!"; messageLogger.warn("Droped message, CLIENT_READY Message has no token!");
return;
} }
if(!message.padId) if(!message.padId)
{ {
throw "CLIENT_READY Message has no padId!"; messageLogger.warn("Droped message, CLIENT_READY Message has no padId!");
return;
} }
if(!message.protocolVersion) if(!message.protocolVersion)
{ {
throw "CLIENT_READY Message has no protocolVersion!"; messageLogger.warn("Droped message, CLIENT_READY Message has no protocolVersion!");
return;
} }
if(message.protocolVersion != 2) if(message.protocolVersion != 2)
{ {
throw "CLIENT_READY Message has a unkown protocolVersion '" + message.protocolVersion + "'!"; messageLogger.warn("Droped message, CLIENT_READY Message has a unkown protocolVersion '" + message.protocolVersion + "'!");
return;
} }
var author; var author;
@ -862,18 +865,3 @@ function handleClientReady(client, message)
if(err) throw err; if(err) throw err;
}); });
} }
/**
* A internal function that simply checks if client or socketio is null and throws a exception if yes
*/
function throwExceptionIfClientOrIOisInvalid(client)
{
if(client == null)
{
throw "Client is null!";
}
if(socketio == null)
{
throw "SocketIO is not set or null! Please use setSocketIO(io) to set it";
}
}

View File

@ -23,6 +23,8 @@ var padManager = require("../db/PadManager");
var Changeset = require("../utils/Changeset"); var Changeset = require("../utils/Changeset");
var AttributePoolFactory = require("../utils/AttributePoolFactory"); var AttributePoolFactory = require("../utils/AttributePoolFactory");
var authorManager = require("../db/AuthorManager"); var authorManager = require("../db/AuthorManager");
var log4js = require('log4js');
var messageLogger = log4js.getLogger("message");
/** /**
* Saves the Socket class we need to send and recieve data from the client * Saves the Socket class we need to send and recieve data from the client
@ -75,12 +77,18 @@ exports.handleMessage = function(client, message)
//if the message type is unkown, throw an exception //if the message type is unkown, throw an exception
else else
{ {
throw "unkown Message Type: '" + message.type + "'"; messageLogger.warn("Droped message, unkown Message Type: '" + message.type + "'");
} }
} }
function handleClientReady(client, message) function handleClientReady(client, message)
{ {
if(message.padId == null)
{
messageLogger.warn("Droped message, changeset request has no padId!");
return;
}
//send the timeslider client the clientVars, with this values its able to start //send the timeslider client the clientVars, with this values its able to start
createTimesliderClientVars (message.padId, function(err, clientVars) createTimesliderClientVars (message.padId, function(err, clientVars)
{ {
@ -95,6 +103,33 @@ function handleClientReady(client, message)
*/ */
function handleChangesetRequest(client, message) function handleChangesetRequest(client, message)
{ {
//check if all ok
if(message.data == null)
{
messageLogger.warn("Droped message, changeset request has no data!");
return;
}
if(message.padId == null)
{
messageLogger.warn("Droped message, changeset request has no padId!");
return;
}
if(message.data.granularity == null)
{
messageLogger.warn("Droped message, changeset request has no granularity!");
return;
}
if(message.data.start == null)
{
messageLogger.warn("Droped message, changeset request has no start!");
return;
}
if(message.data.requestID == null)
{
messageLogger.warn("Droped message, changeset request has no requestID!");
return;
}
var granularity = message.data.granularity; var granularity = message.data.granularity;
var start = message.data.start; var start = message.data.start;
var end = start + (100 * granularity); var end = start + (100 * granularity);