You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

481 lines
12 KiB

7 years ago
  1. /**
  2. * Module dependencies.
  3. */
  4. var qs = require('querystring');
  5. var parse = require('url').parse;
  6. var base64id = require('base64id');
  7. var transports = require('./transports');
  8. var EventEmitter = require('events').EventEmitter;
  9. var Socket = require('./socket');
  10. var util = require('util');
  11. var debug = require('debug')('engine');
  12. var cookieMod = require('cookie');
  13. /**
  14. * Module exports.
  15. */
  16. module.exports = Server;
  17. /**
  18. * Server constructor.
  19. *
  20. * @param {Object} options
  21. * @api public
  22. */
  23. function Server (opts) {
  24. if (!(this instanceof Server)) {
  25. return new Server(opts);
  26. }
  27. this.clients = {};
  28. this.clientsCount = 0;
  29. opts = opts || {};
  30. this.wsEngine = opts.wsEngine || process.env.EIO_WS_ENGINE;
  31. this.pingTimeout = opts.pingTimeout || 60000;
  32. this.pingInterval = opts.pingInterval || 25000;
  33. this.upgradeTimeout = opts.upgradeTimeout || 10000;
  34. this.maxHttpBufferSize = opts.maxHttpBufferSize || 10E7;
  35. this.transports = opts.transports || Object.keys(transports);
  36. this.allowUpgrades = false !== opts.allowUpgrades;
  37. this.allowRequest = opts.allowRequest;
  38. this.cookie = false !== opts.cookie ? (opts.cookie || 'io') : false;
  39. this.cookiePath = false !== opts.cookiePath ? (opts.cookiePath || '/') : false;
  40. this.cookieHttpOnly = false !== opts.cookieHttpOnly;
  41. this.perMessageDeflate = false !== opts.perMessageDeflate ? (opts.perMessageDeflate || true) : false;
  42. this.httpCompression = false !== opts.httpCompression ? (opts.httpCompression || {}) : false;
  43. var self = this;
  44. // initialize compression options
  45. ['perMessageDeflate', 'httpCompression'].forEach(function (type) {
  46. var compression = self[type];
  47. if (true === compression) self[type] = compression = {};
  48. if (compression && null == compression.threshold) {
  49. compression.threshold = 1024;
  50. }
  51. });
  52. // initialize websocket server
  53. if (~this.transports.indexOf('websocket')) {
  54. // keep require('ws') as separate expression for packers (browserify, etc)
  55. var WebSocketServer = (this.wsEngine ? require(this.wsEngine) : require('ws')).Server;
  56. this.ws = new WebSocketServer({
  57. noServer: true,
  58. clientTracking: false,
  59. perMessageDeflate: this.perMessageDeflate,
  60. maxPayload: this.maxHttpBufferSize
  61. });
  62. }
  63. }
  64. /**
  65. * Protocol errors mappings.
  66. */
  67. Server.errors = {
  68. UNKNOWN_TRANSPORT: 0,
  69. UNKNOWN_SID: 1,
  70. BAD_HANDSHAKE_METHOD: 2,
  71. BAD_REQUEST: 3
  72. };
  73. Server.errorMessages = {
  74. 0: 'Transport unknown',
  75. 1: 'Session ID unknown',
  76. 2: 'Bad handshake method',
  77. 3: 'Bad request'
  78. };
  79. /**
  80. * Inherits from EventEmitter.
  81. */
  82. util.inherits(Server, EventEmitter);
  83. /**
  84. * Hash of open clients.
  85. *
  86. * @api public
  87. */
  88. Server.prototype.clients;
  89. /**
  90. * Returns a list of available transports for upgrade given a certain transport.
  91. *
  92. * @return {Array}
  93. * @api public
  94. */
  95. Server.prototype.upgrades = function (transport) {
  96. if (!this.allowUpgrades) return [];
  97. return transports[transport].upgradesTo || [];
  98. };
  99. /**
  100. * Verifies a request.
  101. *
  102. * @param {http.IncomingMessage}
  103. * @return {Boolean} whether the request is valid
  104. * @api private
  105. */
  106. Server.prototype.verify = function (req, upgrade, fn) {
  107. // transport check
  108. var transport = req._query.transport;
  109. if (!~this.transports.indexOf(transport)) {
  110. debug('unknown transport "%s"', transport);
  111. return fn(Server.errors.UNKNOWN_TRANSPORT, false);
  112. }
  113. // sid check
  114. var sid = req._query.sid;
  115. if (sid) {
  116. if (!this.clients.hasOwnProperty(sid)) {
  117. return fn(Server.errors.UNKNOWN_SID, false);
  118. }
  119. if (!upgrade && this.clients[sid].transport.name !== transport) {
  120. debug('bad request: unexpected transport without upgrade');
  121. return fn(Server.errors.BAD_REQUEST, false);
  122. }
  123. } else {
  124. // handshake is GET only
  125. if ('GET' !== req.method) return fn(Server.errors.BAD_HANDSHAKE_METHOD, false);
  126. if (!this.allowRequest) return fn(null, true);
  127. return this.allowRequest(req, fn);
  128. }
  129. fn(null, true);
  130. };
  131. /**
  132. * Prepares a request by processing the query string.
  133. *
  134. * @api private
  135. */
  136. Server.prototype.prepare = function (req) {
  137. // try to leverage pre-existing `req._query` (e.g: from connect)
  138. if (!req._query) {
  139. req._query = ~req.url.indexOf('?') ? qs.parse(parse(req.url).query) : {};
  140. }
  141. };
  142. /**
  143. * Closes all clients.
  144. *
  145. * @api public
  146. */
  147. Server.prototype.close = function () {
  148. debug('closing all open clients');
  149. for (var i in this.clients) {
  150. if (this.clients.hasOwnProperty(i)) {
  151. this.clients[i].close(true);
  152. }
  153. }
  154. if (this.ws) {
  155. debug('closing webSocketServer');
  156. this.ws.close();
  157. // don't delete this.ws because it can be used again if the http server starts listening again
  158. }
  159. return this;
  160. };
  161. /**
  162. * Handles an Engine.IO HTTP request.
  163. *
  164. * @param {http.IncomingMessage} request
  165. * @param {http.ServerResponse|http.OutgoingMessage} response
  166. * @api public
  167. */
  168. Server.prototype.handleRequest = function (req, res) {
  169. debug('handling "%s" http request "%s"', req.method, req.url);
  170. this.prepare(req);
  171. req.res = res;
  172. var self = this;
  173. this.verify(req, false, function (err, success) {
  174. if (!success) {
  175. sendErrorMessage(req, res, err);
  176. return;
  177. }
  178. if (req._query.sid) {
  179. debug('setting new request for existing client');
  180. self.clients[req._query.sid].transport.onRequest(req);
  181. } else {
  182. self.handshake(req._query.transport, req);
  183. }
  184. });
  185. };
  186. /**
  187. * Sends an Engine.IO Error Message
  188. *
  189. * @param {http.ServerResponse} response
  190. * @param {code} error code
  191. * @api private
  192. */
  193. function sendErrorMessage (req, res, code) {
  194. var headers = { 'Content-Type': 'application/json' };
  195. if (req.headers.origin) {
  196. headers['Access-Control-Allow-Credentials'] = 'true';
  197. headers['Access-Control-Allow-Origin'] = req.headers.origin;
  198. } else {
  199. headers['Access-Control-Allow-Origin'] = '*';
  200. }
  201. res.writeHead(400, headers);
  202. res.end(JSON.stringify({
  203. code: code,
  204. message: Server.errorMessages[code]
  205. }));
  206. }
  207. /**
  208. * generate a socket id.
  209. * Overwrite this method to generate your custom socket id
  210. *
  211. * @param {Object} request object
  212. * @api public
  213. */
  214. Server.prototype.generateId = function (req) {
  215. return base64id.generateId();
  216. };
  217. /**
  218. * Handshakes a new client.
  219. *
  220. * @param {String} transport name
  221. * @param {Object} request object
  222. * @api private
  223. */
  224. Server.prototype.handshake = function (transportName, req) {
  225. var id = this.generateId(req);
  226. debug('handshaking client "%s"', id);
  227. try {
  228. var transport = new transports[transportName](req);
  229. if ('polling' === transportName) {
  230. transport.maxHttpBufferSize = this.maxHttpBufferSize;
  231. transport.httpCompression = this.httpCompression;
  232. } else if ('websocket' === transportName) {
  233. transport.perMessageDeflate = this.perMessageDeflate;
  234. }
  235. if (req._query && req._query.b64) {
  236. transport.supportsBinary = false;
  237. } else {
  238. transport.supportsBinary = true;
  239. }
  240. } catch (e) {
  241. sendErrorMessage(req, req.res, Server.errors.BAD_REQUEST);
  242. return;
  243. }
  244. var socket = new Socket(id, this, transport, req);
  245. var self = this;
  246. if (false !== this.cookie) {
  247. transport.on('headers', function (headers) {
  248. headers['Set-Cookie'] = cookieMod.serialize(self.cookie, id,
  249. {
  250. path: self.cookiePath,
  251. httpOnly: self.cookiePath ? self.cookieHttpOnly : false
  252. });
  253. });
  254. }
  255. transport.onRequest(req);
  256. this.clients[id] = socket;
  257. this.clientsCount++;
  258. socket.once('close', function () {
  259. delete self.clients[id];
  260. self.clientsCount--;
  261. });
  262. this.emit('connection', socket);
  263. };
  264. /**
  265. * Handles an Engine.IO HTTP Upgrade.
  266. *
  267. * @api public
  268. */
  269. Server.prototype.handleUpgrade = function (req, socket, upgradeHead) {
  270. this.prepare(req);
  271. var self = this;
  272. this.verify(req, true, function (err, success) {
  273. if (!success) {
  274. abortConnection(socket, err);
  275. return;
  276. }
  277. var head = new Buffer(upgradeHead.length);
  278. upgradeHead.copy(head);
  279. upgradeHead = null;
  280. // delegate to ws
  281. self.ws.handleUpgrade(req, socket, head, function (conn) {
  282. self.onWebSocket(req, conn);
  283. });
  284. });
  285. };
  286. /**
  287. * Called upon a ws.io connection.
  288. *
  289. * @param {ws.Socket} websocket
  290. * @api private
  291. */
  292. Server.prototype.onWebSocket = function (req, socket) {
  293. socket.on('error', onUpgradeError);
  294. if (!transports[req._query.transport].prototype.handlesUpgrades) {
  295. debug('transport doesnt handle upgraded requests');
  296. socket.close();
  297. return;
  298. }
  299. // get client id
  300. var id = req._query.sid;
  301. // keep a reference to the ws.Socket
  302. req.websocket = socket;
  303. if (id) {
  304. var client = this.clients[id];
  305. if (!client) {
  306. debug('upgrade attempt for closed client');
  307. socket.close();
  308. } else if (client.upgrading) {
  309. debug('transport has already been trying to upgrade');
  310. socket.close();
  311. } else if (client.upgraded) {
  312. debug('transport had already been upgraded');
  313. socket.close();
  314. } else {
  315. debug('upgrading existing transport');
  316. // transport error handling takes over
  317. socket.removeListener('error', onUpgradeError);
  318. var transport = new transports[req._query.transport](req);
  319. if (req._query && req._query.b64) {
  320. transport.supportsBinary = false;
  321. } else {
  322. transport.supportsBinary = true;
  323. }
  324. transport.perMessageDeflate = this.perMessageDeflate;
  325. client.maybeUpgrade(transport);
  326. }
  327. } else {
  328. // transport error handling takes over
  329. socket.removeListener('error', onUpgradeError);
  330. this.handshake(req._query.transport, req);
  331. }
  332. function onUpgradeError () {
  333. debug('websocket error before upgrade');
  334. // socket.close() not needed
  335. }
  336. };
  337. /**
  338. * Captures upgrade requests for a http.Server.
  339. *
  340. * @param {http.Server} server
  341. * @param {Object} options
  342. * @api public
  343. */
  344. Server.prototype.attach = function (server, options) {
  345. var self = this;
  346. options = options || {};
  347. var path = (options.path || '/engine.io').replace(/\/$/, '');
  348. var destroyUpgradeTimeout = options.destroyUpgradeTimeout || 1000;
  349. // normalize path
  350. path += '/';
  351. function check (req) {
  352. return path === req.url.substr(0, path.length);
  353. }
  354. // cache and clean up listeners
  355. var listeners = server.listeners('request').slice(0);
  356. server.removeAllListeners('request');
  357. server.on('close', self.close.bind(self));
  358. // add request handler
  359. server.on('request', function (req, res) {
  360. if (check(req)) {
  361. debug('intercepting request for path "%s"', path);
  362. self.handleRequest(req, res);
  363. } else {
  364. for (var i = 0, l = listeners.length; i < l; i++) {
  365. listeners[i].call(server, req, res);
  366. }
  367. }
  368. });
  369. if (~self.transports.indexOf('websocket')) {
  370. server.on('upgrade', function (req, socket, head) {
  371. if (check(req)) {
  372. self.handleUpgrade(req, socket, head);
  373. } else if (false !== options.destroyUpgrade) {
  374. // default node behavior is to disconnect when no handlers
  375. // but by adding a handler, we prevent that
  376. // and if no eio thing handles the upgrade
  377. // then the socket needs to die!
  378. setTimeout(function () {
  379. if (socket.writable && socket.bytesWritten <= 0) {
  380. return socket.end();
  381. }
  382. }, destroyUpgradeTimeout);
  383. }
  384. });
  385. }
  386. };
  387. /**
  388. * Closes the connection
  389. *
  390. * @param {net.Socket} socket
  391. * @param {code} error code
  392. * @api private
  393. */
  394. function abortConnection (socket, code) {
  395. if (socket.writable) {
  396. var message = Server.errorMessages.hasOwnProperty(code) ? Server.errorMessages[code] : code;
  397. var length = Buffer.byteLength(message);
  398. socket.write(
  399. 'HTTP/1.1 400 Bad Request\r\n' +
  400. 'Connection: close\r\n' +
  401. 'Content-type: text/html\r\n' +
  402. 'Content-Length: ' + length + '\r\n' +
  403. '\r\n' +
  404. message
  405. );
  406. }
  407. socket.destroy();
  408. }