|
|
|
@ -7,14 +7,17 @@ var url = require('url');
|
|
|
|
|
var cookieParser = require('cookie-parser')(nconf.get('secret'));
|
|
|
|
|
|
|
|
|
|
var db = require('../database');
|
|
|
|
|
var user = require('../user');
|
|
|
|
|
var logger = require('../logger');
|
|
|
|
|
var ratelimit = require('../middleware/ratelimit');
|
|
|
|
|
|
|
|
|
|
(function (Sockets) {
|
|
|
|
|
var Namespaces = {};
|
|
|
|
|
var io;
|
|
|
|
|
|
|
|
|
|
Sockets.init = function (server) {
|
|
|
|
|
var Namespaces = {};
|
|
|
|
|
var io;
|
|
|
|
|
|
|
|
|
|
var Sockets = module.exports;
|
|
|
|
|
|
|
|
|
|
Sockets.init = function (server) {
|
|
|
|
|
requireModules();
|
|
|
|
|
|
|
|
|
|
var SocketIO = require('socket.io');
|
|
|
|
@ -35,9 +38,9 @@ var ratelimit = require('../middleware/ratelimit');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Sockets.server = io;
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function onConnection(socket) {
|
|
|
|
|
function onConnection(socket) {
|
|
|
|
|
socket.ip = socket.request.headers['x-forwarded-for'] || socket.request.connection.remoteAddress;
|
|
|
|
|
|
|
|
|
|
logger.io_one(socket, socket.uid);
|
|
|
|
@ -47,9 +50,9 @@ var ratelimit = require('../middleware/ratelimit');
|
|
|
|
|
socket.on('*', function (payload) {
|
|
|
|
|
onMessage(socket, payload);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function onConnect(socket) {
|
|
|
|
|
function onConnect(socket) {
|
|
|
|
|
if (socket.uid) {
|
|
|
|
|
socket.join('uid_' + socket.uid);
|
|
|
|
|
socket.join('online_users');
|
|
|
|
@ -59,9 +62,9 @@ var ratelimit = require('../middleware/ratelimit');
|
|
|
|
|
|
|
|
|
|
socket.join('sess_' + socket.request.signedCookies[nconf.get('sessionKey')]);
|
|
|
|
|
io.sockets.sockets[socket.id].emit('checkSession', socket.uid);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function onMessage(socket, payload) {
|
|
|
|
|
function onMessage(socket, payload) {
|
|
|
|
|
if (!payload.data.length) {
|
|
|
|
|
return winston.warn('[socket.io] Empty payload');
|
|
|
|
|
}
|
|
|
|
@ -104,6 +107,9 @@ var ratelimit = require('../middleware/ratelimit');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
checkMaintenance(socket, next);
|
|
|
|
|
},
|
|
|
|
|
function (next) {
|
|
|
|
|
validateSession(socket, next);
|
|
|
|
|
},
|
|
|
|
@ -120,9 +126,9 @@ var ratelimit = require('../middleware/ratelimit');
|
|
|
|
|
], function (err, result) {
|
|
|
|
|
callback(err ? {message: err.message} : null, result);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function requireModules() {
|
|
|
|
|
function requireModules() {
|
|
|
|
|
var modules = ['admin', 'categories', 'groups', 'meta', 'modules',
|
|
|
|
|
'notifications', 'plugins', 'posts', 'topics', 'user', 'blacklist'
|
|
|
|
|
];
|
|
|
|
@ -130,9 +136,21 @@ var ratelimit = require('../middleware/ratelimit');
|
|
|
|
|
modules.forEach(function (module) {
|
|
|
|
|
Namespaces[module] = require('./' + module);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function checkMaintenance(socket, callback) {
|
|
|
|
|
var meta = require('../meta');
|
|
|
|
|
if (parseInt(meta.config.maintenanceMode, 10) !== 1) {
|
|
|
|
|
return setImmediate(callback);
|
|
|
|
|
}
|
|
|
|
|
user.isAdministrator(socket.uid, function (err, isAdmin) {
|
|
|
|
|
if (err || isAdmin) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function validateSession(socket, callback) {
|
|
|
|
|
function validateSession(socket, callback) {
|
|
|
|
|
var req = socket.request;
|
|
|
|
|
if (!req.signedCookies || !req.signedCookies[nconf.get('sessionKey')]) {
|
|
|
|
|
return callback(new Error('[[error:invalid-session]]'));
|
|
|
|
@ -144,9 +162,9 @@ var ratelimit = require('../middleware/ratelimit');
|
|
|
|
|
|
|
|
|
|
callback();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function authorize(socket, callback) {
|
|
|
|
|
function authorize(socket, callback) {
|
|
|
|
|
var request = socket.request;
|
|
|
|
|
|
|
|
|
|
if (!request) {
|
|
|
|
@ -172,9 +190,9 @@ var ratelimit = require('../middleware/ratelimit');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
], callback);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function addRedisAdapter(io) {
|
|
|
|
|
function addRedisAdapter(io) {
|
|
|
|
|
if (nconf.get('redis')) {
|
|
|
|
|
var redisAdapter = require('socket.io-redis');
|
|
|
|
|
var redis = require('../database/redis');
|
|
|
|
@ -184,23 +202,23 @@ var ratelimit = require('../middleware/ratelimit');
|
|
|
|
|
} else if (nconf.get('isCluster') === 'true') {
|
|
|
|
|
winston.warn('[socket.io] Clustering detected, you are advised to configure Redis as a websocket store.');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Sockets.in = function (room) {
|
|
|
|
|
Sockets.in = function (room) {
|
|
|
|
|
return io.in(room);
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Sockets.getUserSocketCount = function (uid) {
|
|
|
|
|
Sockets.getUserSocketCount = function (uid) {
|
|
|
|
|
if (!io) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var room = io.sockets.adapter.rooms['uid_' + uid];
|
|
|
|
|
return room ? room.length : 0;
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Sockets.reqFromSocket = function (socket, payload, event) {
|
|
|
|
|
Sockets.reqFromSocket = function (socket, payload, event) {
|
|
|
|
|
var headers = socket.request ? socket.request.headers : {};
|
|
|
|
|
var encrypted = socket.request ? !!socket.request.connection.encrypted : false;
|
|
|
|
|
var host = headers.host;
|
|
|
|
@ -224,6 +242,6 @@ var ratelimit = require('../middleware/ratelimit');
|
|
|
|
|
path: referer.substr(referer.indexOf(host) + host.length),
|
|
|
|
|
headers: headers
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}(exports));
|
|
|
|
|