2012-02-25 17:23:44 +01:00
|
|
|
var settings = require('../../utils/Settings');
|
2014-11-25 22:38:22 +01:00
|
|
|
var socketio = require('socket.io');
|
2012-02-25 17:23:44 +01:00
|
|
|
var socketIORouter = require("../../handler/SocketIORouter");
|
2012-03-01 19:00:58 +01:00
|
|
|
var hooks = require("ep_etherpad-lite/static/js/pluginfw/hooks");
|
2012-09-22 16:03:40 +02:00
|
|
|
var webaccess = require("ep_etherpad-lite/node/hooks/express/webaccess");
|
2012-02-25 16:44:37 +01:00
|
|
|
|
2012-02-25 17:23:44 +01:00
|
|
|
var padMessageHandler = require("../../handler/PadMessageHandler");
|
2012-02-25 16:44:37 +01:00
|
|
|
|
2015-04-07 07:55:05 -05:00
|
|
|
var cookieParser = require('cookie-parser');
|
|
|
|
var sessionModule = require('express-session');
|
2019-04-16 00:34:29 +02:00
|
|
|
|
2012-02-25 16:44:37 +01:00
|
|
|
exports.expressCreateServer = function (hook_name, args, cb) {
|
|
|
|
//init socket.io and redirect all requests to the MessageHandler
|
2014-11-25 22:38:22 +01:00
|
|
|
// there shouldn't be a browser that isn't compatible to all
|
|
|
|
// transports in this list at once
|
|
|
|
// e.g. XHR is disabled in IE by default, so in IE it should use jsonp-polling
|
|
|
|
var io = socketio({
|
|
|
|
transports: settings.socketTransportProtocols
|
2019-12-07 04:20:12 +01:00
|
|
|
}).listen(args.server, {
|
|
|
|
/*
|
|
|
|
* Do not set the "io" cookie.
|
|
|
|
*
|
|
|
|
* The "io" cookie is created by socket.io, and its purpose is to offer an
|
|
|
|
* handle to perform load balancing with session stickiness when the library
|
|
|
|
* falls back to long polling or below.
|
|
|
|
*
|
|
|
|
* In Etherpad's case, if an operator needs to load balance, he can use the
|
|
|
|
* "express_sid" cookie, and thus "io" is of no use.
|
|
|
|
*
|
|
|
|
* Moreover, socket.io API does not offer a way of setting the "secure" flag
|
|
|
|
* on it, and thus is a liability.
|
|
|
|
*
|
|
|
|
* Let's simply nuke "io".
|
|
|
|
*
|
|
|
|
* references:
|
|
|
|
* https://socket.io/docs/using-multiple-nodes/#Sticky-load-balancing
|
|
|
|
* https://github.com/socketio/socket.io/issues/2276#issuecomment-147184662 (not totally true, actually, see above)
|
|
|
|
*/
|
|
|
|
cookie: false,
|
|
|
|
});
|
2012-02-25 16:44:37 +01:00
|
|
|
|
2020-09-11 15:07:33 -04:00
|
|
|
// REQUIRE a signed express-session cookie to be present, then load the session. See
|
|
|
|
// http://www.danielbaulig.de/socket-ioexpress for more info. After the session is loaded, ensure
|
|
|
|
// that the user has authenticated (if authentication is required).
|
|
|
|
//
|
|
|
|
// !!!WARNING!!! Requests to /socket.io are NOT subject to the checkAccess middleware in
|
|
|
|
// webaccess.js. If this handler fails to check for a signed express-session cookie or fails to
|
|
|
|
// check whether the user has authenticated, then any random person on the Internet can read,
|
|
|
|
// modify, or create any pad (unless the pad is password protected or an HTTP API session is
|
|
|
|
// required).
|
2015-04-07 07:55:05 -05:00
|
|
|
var cookieParserFn = cookieParser(webaccess.secret, {});
|
2014-11-04 18:17:39 +00:00
|
|
|
io.use(function(socket, accept) {
|
|
|
|
var data = socket.request;
|
2020-09-11 15:07:33 -04:00
|
|
|
if (!data.headers.cookie && settings.loadTest) return accept(null, true);
|
|
|
|
cookieParserFn(data, {}, function(err) {
|
|
|
|
if (err) {
|
|
|
|
console.error(err);
|
|
|
|
accept("Couldn't parse request cookies.", false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
data.sessionID = data.signedCookies.express_sid;
|
|
|
|
if (!data.sessionID) return accept('Signed express_sid cookie is required', false);
|
|
|
|
args.app.sessionStore.get(data.sessionID, function(err, session) {
|
|
|
|
if (err || !session) return accept('Bad session / session has expired', false);
|
|
|
|
data.session = new sessionModule.Session(data, session);
|
|
|
|
if (settings.requireAuthentication && data.session.user == null) {
|
|
|
|
return accept('Authentication required', false);
|
2020-06-01 20:17:48 +01:00
|
|
|
}
|
2020-09-11 15:07:33 -04:00
|
|
|
accept(null, true);
|
2012-09-22 16:03:40 +02:00
|
|
|
});
|
2020-09-11 15:07:33 -04:00
|
|
|
});
|
2012-04-19 14:25:12 +02:00
|
|
|
});
|
|
|
|
|
2014-11-04 19:11:06 +00:00
|
|
|
// var socketIOLogger = log4js.getLogger("socket.io");
|
|
|
|
// Debug logging now has to be set at an environment level, this is stupid.
|
|
|
|
// https://github.com/Automattic/socket.io/wiki/Migrating-to-1.0
|
|
|
|
// This debug logging environment is set in Settings.js
|
2012-02-25 16:44:37 +01:00
|
|
|
|
|
|
|
//minify socket.io javascript
|
2014-11-04 23:25:18 +00:00
|
|
|
// Due to a shitty decision by the SocketIO team minification is
|
|
|
|
// no longer available, details available at:
|
|
|
|
// http://stackoverflow.com/questions/23981741/minify-socket-io-socket-io-js-with-1-0
|
|
|
|
// if(settings.minify) io.enable('browser client minification');
|
2019-04-16 00:34:29 +02:00
|
|
|
|
2012-02-25 16:44:37 +01:00
|
|
|
//Initalize the Socket.IO Router
|
|
|
|
socketIORouter.setSocketIO(io);
|
|
|
|
socketIORouter.addComponent("pad", padMessageHandler);
|
|
|
|
|
2012-09-21 17:12:22 +02:00
|
|
|
hooks.callAll("socketio", {"app": args.app, "io": io, "server": args.server});
|
2012-02-25 16:44:37 +01:00
|
|
|
}
|