2011-06-20 12:44:04 +02:00
|
|
|
/**
|
2019-02-08 23:20:57 +01:00
|
|
|
* This is the Socket.IO Router. It routes the Messages between the
|
2011-06-20 12:44:04 +02:00
|
|
|
* components of the Server. The components are at the moment: pad and timeslider
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2011-08-11 16:26:41 +02:00
|
|
|
* 2011 Peter 'Pita' Martischka (Primary Technology Ltd)
|
2011-06-20 12:44:04 +02:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS-IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2011-07-31 19:25:51 +02:00
|
|
|
var log4js = require('log4js');
|
|
|
|
var messageLogger = log4js.getLogger("message");
|
2011-08-16 16:53:09 +02:00
|
|
|
var securityManager = require("../db/SecurityManager");
|
2013-10-13 22:31:41 +02:00
|
|
|
var readOnlyManager = require("../db/ReadOnlyManager");
|
2014-11-05 00:25:18 +01:00
|
|
|
var remoteAddress = require("../utils/RemoteAddress").remoteAddress;
|
2013-04-24 12:19:41 +02:00
|
|
|
var settings = require('../utils/Settings');
|
2011-07-31 19:25:51 +02:00
|
|
|
|
2011-06-20 12:44:04 +02:00
|
|
|
/**
|
|
|
|
* Saves all components
|
|
|
|
* key is the component name
|
|
|
|
* value is the component module
|
2019-02-08 23:20:57 +01:00
|
|
|
*/
|
2011-06-20 12:44:04 +02:00
|
|
|
var components = {};
|
|
|
|
|
|
|
|
var socket;
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2011-06-20 12:44:04 +02:00
|
|
|
/**
|
|
|
|
* adds a component
|
|
|
|
*/
|
|
|
|
exports.addComponent = function(moduleName, module)
|
|
|
|
{
|
2019-02-08 23:20:57 +01:00
|
|
|
// save the component
|
2011-06-20 12:44:04 +02:00
|
|
|
components[moduleName] = module;
|
2019-02-08 23:20:57 +01:00
|
|
|
|
|
|
|
// give the module the socket
|
2011-06-20 12:44:04 +02:00
|
|
|
module.setSocketIO(socket);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sets the socket.io and adds event functions for routing
|
|
|
|
*/
|
2013-04-15 20:29:06 +02:00
|
|
|
exports.setSocketIO = function(_socket) {
|
2019-02-08 23:20:57 +01:00
|
|
|
// save this socket internaly
|
2011-06-20 12:44:04 +02:00
|
|
|
socket = _socket;
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2011-06-23 15:08:18 +02:00
|
|
|
socket.sockets.on('connection', function(client)
|
2011-06-20 12:44:04 +02:00
|
|
|
{
|
2014-11-05 00:25:18 +01:00
|
|
|
// Broken: See http://stackoverflow.com/questions/4647348/send-message-to-specific-client-with-socket-io-and-node-js
|
|
|
|
// Fixed by having a persistant object, ideally this would actually be in the database layer
|
|
|
|
// TODO move to database layer
|
2019-02-08 23:20:57 +01:00
|
|
|
if (settings.trustProxy && client.handshake.headers['x-forwarded-for'] !== undefined) {
|
2014-11-05 00:25:18 +01:00
|
|
|
remoteAddress[client.id] = client.handshake.headers['x-forwarded-for'];
|
2019-02-08 23:20:57 +01:00
|
|
|
} else {
|
2014-11-05 00:25:18 +01:00
|
|
|
remoteAddress[client.id] = client.handshake.address;
|
2013-12-05 08:41:29 +01:00
|
|
|
}
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2011-08-16 16:53:09 +02:00
|
|
|
var clientAuthorized = false;
|
2019-02-08 23:20:57 +01:00
|
|
|
|
|
|
|
// wrap the original send function to log the messages
|
2011-07-11 15:56:45 +02:00
|
|
|
client._send = client.send;
|
2013-04-15 20:29:06 +02:00
|
|
|
client.send = function(message) {
|
2013-02-10 16:03:49 +01:00
|
|
|
messageLogger.debug("to " + client.id + ": " + stringifyWithoutPassword(message));
|
2011-07-11 15:56:45 +02:00
|
|
|
client._send(message);
|
|
|
|
}
|
2019-02-08 23:20:57 +01:00
|
|
|
|
|
|
|
// tell all components about this connect
|
2019-01-30 11:25:01 +01:00
|
|
|
for (let i in components) {
|
2011-06-20 12:44:04 +02:00
|
|
|
components[i].handleConnect(client);
|
2019-02-08 23:20:57 +01:00
|
|
|
}
|
2013-04-15 20:29:06 +02:00
|
|
|
|
2019-01-28 14:13:24 +01:00
|
|
|
client.on('message', async function(message) {
|
2019-02-08 23:20:57 +01:00
|
|
|
if (message.protocolVersion && message.protocolVersion != 2) {
|
2011-08-16 16:53:09 +02:00
|
|
|
messageLogger.warn("Protocolversion header is not correct:" + stringifyWithoutPassword(message));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-08 23:20:57 +01:00
|
|
|
if (clientAuthorized) {
|
|
|
|
// client is authorized, everything ok
|
2013-04-15 20:29:06 +02:00
|
|
|
handleMessage(client, message);
|
2019-02-08 23:20:57 +01:00
|
|
|
} else {
|
|
|
|
// try to authorize the client
|
|
|
|
if (message.padId !== undefined && message.sessionID !== undefined && message.token !== undefined && message.password !== undefined) {
|
2019-01-28 14:13:24 +01:00
|
|
|
// check for read-only pads
|
|
|
|
let padId = message.padId;
|
|
|
|
if (padId.indexOf("r.") === 0) {
|
|
|
|
padId = await readOnlyManager.getPadId(message.padId);
|
|
|
|
}
|
|
|
|
|
|
|
|
let { accessStatus } = await securityManager.checkAccess(padId, message.sessionID, message.token, message.password);
|
|
|
|
|
|
|
|
if (accessStatus === "grant") {
|
|
|
|
// access was granted, mark the client as authorized and handle the message
|
|
|
|
clientAuthorized = true;
|
|
|
|
handleMessage(client, message);
|
2013-10-13 22:31:41 +02:00
|
|
|
} else {
|
2019-01-28 14:13:24 +01:00
|
|
|
// no access, send the client a message that tells him why
|
|
|
|
messageLogger.warn("Authentication try failed:" + stringifyWithoutPassword(message));
|
|
|
|
client.json.send({ accessStatus });
|
2013-10-13 22:31:41 +02:00
|
|
|
}
|
2019-02-08 23:20:57 +01:00
|
|
|
} else {
|
|
|
|
// drop message
|
2019-02-15 22:52:53 +01:00
|
|
|
messageLogger.warn("Dropped message because of bad permissions:" + stringifyWithoutPassword(message));
|
2011-08-16 16:53:09 +02:00
|
|
|
}
|
2011-06-20 12:44:04 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-02-08 23:20:57 +01:00
|
|
|
client.on('disconnect', function() {
|
|
|
|
// tell all components about this disconnect
|
2019-01-30 11:25:01 +01:00
|
|
|
for (let i in components) {
|
2011-06-20 12:44:04 +02:00
|
|
|
components[i].handleDisconnect(client);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2011-08-16 16:53:09 +02:00
|
|
|
|
2019-02-08 23:20:57 +01:00
|
|
|
// try to handle the message of this client
|
2013-04-15 20:29:06 +02:00
|
|
|
function handleMessage(client, message)
|
|
|
|
{
|
2019-02-08 23:20:57 +01:00
|
|
|
if (message.component && components[message.component]) {
|
|
|
|
// check if component is registered in the components array
|
|
|
|
if (components[message.component]) {
|
2013-04-15 20:29:06 +02:00
|
|
|
messageLogger.debug("from " + client.id + ": " + stringifyWithoutPassword(message));
|
|
|
|
components[message.component].handleMessage(client, message);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
messageLogger.error("Can't route the message:" + stringifyWithoutPassword(message));
|
|
|
|
}
|
2019-02-08 23:20:57 +01:00
|
|
|
}
|
2013-04-15 20:29:06 +02:00
|
|
|
|
2019-02-08 23:20:57 +01:00
|
|
|
// returns a stringified representation of a message, removes the password
|
|
|
|
// this ensures there are no passwords in the log
|
2011-08-16 16:53:09 +02:00
|
|
|
function stringifyWithoutPassword(message)
|
|
|
|
{
|
2019-01-30 11:25:01 +01:00
|
|
|
let newMessage = Object.assign({}, message);
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2019-01-30 11:25:01 +01:00
|
|
|
if (newMessage.password != null) {
|
|
|
|
newMessage.password = "xxx";
|
2011-08-16 16:53:09 +02:00
|
|
|
}
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2011-08-16 16:53:09 +02:00
|
|
|
return JSON.stringify(newMessage);
|
|
|
|
}
|