This repository has been archived by the owner on Aug 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathsocket.io.js
117 lines (104 loc) · 3.59 KB
/
socket.io.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
'use strict';
// Load the module dependencies
var config = require('../config'),
path = require('path'),
fs = require('fs'),
http = require('http'),
https = require('https'),
cookieParser = require('cookie-parser'),
passport = require('passport'),
socketio = require('socket.io'),
session = require('express-session'),
MongoStore = require('connect-mongo')(session);
// Define the Socket.io configuration method
module.exports = function (app, db) {
var server;
if (config.secure && config.secure.ssl === true) {
// Load SSL key and certificate
var privateKey = fs.readFileSync(path.resolve(config.secure.privateKey), 'utf8');
var certificate = fs.readFileSync(path.resolve(config.secure.certificate), 'utf8');
var caBundle;
try {
caBundle = fs.readFileSync(path.resolve(config.secure.caBundle), 'utf8');
} catch (err) {
console.log('Warning: couldn\'t find or read caBundle file');
}
var options = {
key: privateKey,
cert: certificate,
ca: caBundle,
// requestCert : true,
// rejectUnauthorized : true,
secureProtocol: 'TLSv1_method',
ciphers: [
'ECDHE-RSA-AES128-GCM-SHA256',
'ECDHE-ECDSA-AES128-GCM-SHA256',
'ECDHE-RSA-AES256-GCM-SHA384',
'ECDHE-ECDSA-AES256-GCM-SHA384',
'DHE-RSA-AES128-GCM-SHA256',
'ECDHE-RSA-AES128-SHA256',
'DHE-RSA-AES128-SHA256',
'ECDHE-RSA-AES256-SHA384',
'DHE-RSA-AES256-SHA384',
'ECDHE-RSA-AES256-SHA256',
'DHE-RSA-AES256-SHA256',
'HIGH',
'!aNULL',
'!eNULL',
'!EXPORT',
'!DES',
'!RC4',
'!MD5',
'!PSK',
'!SRP',
'!CAMELLIA'
].join(':'),
honorCipherOrder: true
};
// Create new HTTPS Server
server = https.createServer(options, app);
} else {
// Create a new HTTP server
server = http.createServer(app);
}
// Create a new Socket.io server
var io = socketio.listen(server);
// Create a MongoDB storage object
var mongoStore = new MongoStore({
db: db,
collection: config.sessionCollection
});
// Intercept Socket.io's handshake request
io.use(function (socket, next) {
// Use the 'cookie-parser' module to parse the request cookies
cookieParser(config.sessionSecret)(socket.request, {}, function (err) {
// Get the session id from the request cookies
var sessionId = socket.request.signedCookies ? socket.request.signedCookies[config.sessionKey] : undefined;
if (!sessionId) return next(new Error('sessionId was not found in socket.request'), false);
// Use the mongoStorage instance to get the Express session information
mongoStore.get(sessionId, function (err, session) {
if (err) return next(err, false);
if (!session) return next(new Error('session was not found for ' + sessionId), false);
// Set the Socket.io session information
socket.request.session = session;
// Use Passport to populate the user details
passport.initialize()(socket.request, {}, function () {
passport.session()(socket.request, {}, function () {
if (socket.request.user) {
next(null, true);
} else {
next(new Error('User is not authenticated'), false);
}
});
});
});
});
});
// Add an event listener to the 'connection' event
io.on('connection', function (socket) {
config.files.server.sockets.forEach(function (socketConfiguration) {
require(path.resolve(socketConfiguration))(io, socket);
});
});
return server;
};