index.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. 'use strict';
  2. /**
  3. * Module dependencies.
  4. */
  5. var http = require('http');
  6. var read = require('fs').readFileSync;
  7. var path = require('path');
  8. var exists = require('fs').existsSync;
  9. var engine = require('engine.io');
  10. var clientVersion = require('socket.io-client/package.json').version;
  11. var Client = require('./client');
  12. var Emitter = require('events').EventEmitter;
  13. var Namespace = require('./namespace');
  14. var ParentNamespace = require('./parent-namespace');
  15. var Adapter = require('socket.io-adapter');
  16. var parser = require('socket.io-parser');
  17. var debug = require('debug')('socket.io:server');
  18. var url = require('url');
  19. /**
  20. * Module exports.
  21. */
  22. module.exports = Server;
  23. /**
  24. * Socket.IO client source.
  25. */
  26. var clientSource = undefined;
  27. var clientSourceMap = undefined;
  28. /**
  29. * Server constructor.
  30. *
  31. * @param {http.Server|Number|Object} srv http server, port or options
  32. * @param {Object} [opts]
  33. * @api public
  34. */
  35. function Server(srv, opts){
  36. if (!(this instanceof Server)) return new Server(srv, opts);
  37. if ('object' == typeof srv && srv instanceof Object && !srv.listen) {
  38. opts = srv;
  39. srv = null;
  40. }
  41. opts = opts || {};
  42. this.nsps = {};
  43. this.parentNsps = new Map();
  44. this.path(opts.path || '/socket.io');
  45. this.serveClient(false !== opts.serveClient);
  46. this.parser = opts.parser || parser;
  47. this.encoder = new this.parser.Encoder();
  48. this.adapter(opts.adapter || Adapter);
  49. this.origins(opts.origins || '*:*');
  50. this.sockets = this.of('/');
  51. if (srv) this.attach(srv, opts);
  52. }
  53. /**
  54. * Server request verification function, that checks for allowed origins
  55. *
  56. * @param {http.IncomingMessage} req request
  57. * @param {Function} fn callback to be called with the result: `fn(err, success)`
  58. */
  59. Server.prototype.checkRequest = function(req, fn) {
  60. var origin = req.headers.origin || req.headers.referer;
  61. // file:// URLs produce a null Origin which can't be authorized via echo-back
  62. if ('null' == origin || null == origin) origin = '*';
  63. if (!!origin && typeof(this._origins) == 'function') return this._origins(origin, fn);
  64. if (this._origins.indexOf('*:*') !== -1) return fn(null, true);
  65. if (origin) {
  66. try {
  67. var parts = url.parse(origin);
  68. var defaultPort = 'https:' == parts.protocol ? 443 : 80;
  69. parts.port = parts.port != null
  70. ? parts.port
  71. : defaultPort;
  72. var ok =
  73. ~this._origins.indexOf(parts.protocol + '//' + parts.hostname + ':' + parts.port) ||
  74. ~this._origins.indexOf(parts.hostname + ':' + parts.port) ||
  75. ~this._origins.indexOf(parts.hostname + ':*') ||
  76. ~this._origins.indexOf('*:' + parts.port);
  77. debug('origin %s is %svalid', origin, !!ok ? '' : 'not ');
  78. return fn(null, !!ok);
  79. } catch (ex) {
  80. }
  81. }
  82. fn(null, false);
  83. };
  84. /**
  85. * Sets/gets whether client code is being served.
  86. *
  87. * @param {Boolean} v whether to serve client code
  88. * @return {Server|Boolean} self when setting or value when getting
  89. * @api public
  90. */
  91. Server.prototype.serveClient = function(v){
  92. if (!arguments.length) return this._serveClient;
  93. this._serveClient = v;
  94. var resolvePath = function(file){
  95. var filepath = path.resolve(__dirname, './../../', file);
  96. if (exists(filepath)) {
  97. return filepath;
  98. }
  99. return require.resolve(file);
  100. };
  101. if (v && !clientSource) {
  102. clientSource = read(resolvePath( 'socket.io-client/dist/socket.io.js'), 'utf-8');
  103. try {
  104. clientSourceMap = read(resolvePath( 'socket.io-client/dist/socket.io.js.map'), 'utf-8');
  105. } catch(err) {
  106. debug('could not load sourcemap file');
  107. }
  108. }
  109. return this;
  110. };
  111. /**
  112. * Old settings for backwards compatibility
  113. */
  114. var oldSettings = {
  115. "transports": "transports",
  116. "heartbeat timeout": "pingTimeout",
  117. "heartbeat interval": "pingInterval",
  118. "destroy buffer size": "maxHttpBufferSize"
  119. };
  120. /**
  121. * Backwards compatibility.
  122. *
  123. * @api public
  124. */
  125. Server.prototype.set = function(key, val){
  126. if ('authorization' == key && val) {
  127. this.use(function(socket, next) {
  128. val(socket.request, function(err, authorized) {
  129. if (err) return next(new Error(err));
  130. if (!authorized) return next(new Error('Not authorized'));
  131. next();
  132. });
  133. });
  134. } else if ('origins' == key && val) {
  135. this.origins(val);
  136. } else if ('resource' == key) {
  137. this.path(val);
  138. } else if (oldSettings[key] && this.eio[oldSettings[key]]) {
  139. this.eio[oldSettings[key]] = val;
  140. } else {
  141. console.error('Option %s is not valid. Please refer to the README.', key);
  142. }
  143. return this;
  144. };
  145. /**
  146. * Executes the middleware for an incoming namespace not already created on the server.
  147. *
  148. * @param {String} name name of incoming namespace
  149. * @param {Object} query the query parameters
  150. * @param {Function} fn callback
  151. * @api private
  152. */
  153. Server.prototype.checkNamespace = function(name, query, fn){
  154. if (this.parentNsps.size === 0) return fn(false);
  155. const keysIterator = this.parentNsps.keys();
  156. const run = () => {
  157. let nextFn = keysIterator.next();
  158. if (nextFn.done) {
  159. return fn(false);
  160. }
  161. nextFn.value(name, query, (err, allow) => {
  162. if (err || !allow) {
  163. run();
  164. } else {
  165. fn(this.parentNsps.get(nextFn.value).createChild(name));
  166. }
  167. });
  168. };
  169. run();
  170. };
  171. /**
  172. * Sets the client serving path.
  173. *
  174. * @param {String} v pathname
  175. * @return {Server|String} self when setting or value when getting
  176. * @api public
  177. */
  178. Server.prototype.path = function(v){
  179. if (!arguments.length) return this._path;
  180. this._path = v.replace(/\/$/, '');
  181. return this;
  182. };
  183. /**
  184. * Sets the adapter for rooms.
  185. *
  186. * @param {Adapter} v pathname
  187. * @return {Server|Adapter} self when setting or value when getting
  188. * @api public
  189. */
  190. Server.prototype.adapter = function(v){
  191. if (!arguments.length) return this._adapter;
  192. this._adapter = v;
  193. for (var i in this.nsps) {
  194. if (this.nsps.hasOwnProperty(i)) {
  195. this.nsps[i].initAdapter();
  196. }
  197. }
  198. return this;
  199. };
  200. /**
  201. * Sets the allowed origins for requests.
  202. *
  203. * @param {String|String[]} v origins
  204. * @return {Server|Adapter} self when setting or value when getting
  205. * @api public
  206. */
  207. Server.prototype.origins = function(v){
  208. if (!arguments.length) return this._origins;
  209. this._origins = v;
  210. return this;
  211. };
  212. /**
  213. * Attaches socket.io to a server or port.
  214. *
  215. * @param {http.Server|Number} server or port
  216. * @param {Object} options passed to engine.io
  217. * @return {Server} self
  218. * @api public
  219. */
  220. Server.prototype.listen =
  221. Server.prototype.attach = function(srv, opts){
  222. if ('function' == typeof srv) {
  223. var msg = 'You are trying to attach socket.io to an express ' +
  224. 'request handler function. Please pass a http.Server instance.';
  225. throw new Error(msg);
  226. }
  227. // handle a port as a string
  228. if (Number(srv) == srv) {
  229. srv = Number(srv);
  230. }
  231. if ('number' == typeof srv) {
  232. debug('creating http server and binding to %d', srv);
  233. var port = srv;
  234. srv = http.Server(function(req, res){
  235. res.writeHead(404);
  236. res.end();
  237. });
  238. srv.listen(port);
  239. }
  240. // set engine.io path to `/socket.io`
  241. opts = opts || {};
  242. opts.path = opts.path || this.path();
  243. // set origins verification
  244. opts.allowRequest = opts.allowRequest || this.checkRequest.bind(this);
  245. if (this.sockets.fns.length > 0) {
  246. this.initEngine(srv, opts);
  247. return this;
  248. }
  249. var self = this;
  250. var connectPacket = { type: parser.CONNECT, nsp: '/' };
  251. this.encoder.encode(connectPacket, function (encodedPacket){
  252. // the CONNECT packet will be merged with Engine.IO handshake,
  253. // to reduce the number of round trips
  254. opts.initialPacket = encodedPacket;
  255. self.initEngine(srv, opts);
  256. });
  257. return this;
  258. };
  259. /**
  260. * Initialize engine
  261. *
  262. * @param {Object} options passed to engine.io
  263. * @api private
  264. */
  265. Server.prototype.initEngine = function(srv, opts){
  266. // initialize engine
  267. debug('creating engine.io instance with opts %j', opts);
  268. this.eio = engine.attach(srv, opts);
  269. // attach static file serving
  270. if (this._serveClient) this.attachServe(srv);
  271. // Export http server
  272. this.httpServer = srv;
  273. // bind to engine events
  274. this.bind(this.eio);
  275. };
  276. /**
  277. * Attaches the static file serving.
  278. *
  279. * @param {Function|http.Server} srv http server
  280. * @api private
  281. */
  282. Server.prototype.attachServe = function(srv){
  283. debug('attaching client serving req handler');
  284. var url = this._path + '/socket.io.js';
  285. var urlMap = this._path + '/socket.io.js.map';
  286. var evs = srv.listeners('request').slice(0);
  287. var self = this;
  288. srv.removeAllListeners('request');
  289. srv.on('request', function(req, res) {
  290. if (0 === req.url.indexOf(urlMap)) {
  291. self.serveMap(req, res);
  292. } else if (0 === req.url.indexOf(url)) {
  293. self.serve(req, res);
  294. } else {
  295. for (var i = 0; i < evs.length; i++) {
  296. evs[i].call(srv, req, res);
  297. }
  298. }
  299. });
  300. };
  301. /**
  302. * Handles a request serving `/socket.io.js`
  303. *
  304. * @param {http.Request} req
  305. * @param {http.Response} res
  306. * @api private
  307. */
  308. Server.prototype.serve = function(req, res){
  309. // Per the standard, ETags must be quoted:
  310. // https://tools.ietf.org/html/rfc7232#section-2.3
  311. var expectedEtag = '"' + clientVersion + '"';
  312. var etag = req.headers['if-none-match'];
  313. if (etag) {
  314. if (expectedEtag == etag) {
  315. debug('serve client 304');
  316. res.writeHead(304);
  317. res.end();
  318. return;
  319. }
  320. }
  321. debug('serve client source');
  322. res.setHeader("Cache-Control", "public, max-age=0");
  323. res.setHeader('Content-Type', 'application/javascript');
  324. res.setHeader('ETag', expectedEtag);
  325. res.writeHead(200);
  326. res.end(clientSource);
  327. };
  328. /**
  329. * Handles a request serving `/socket.io.js.map`
  330. *
  331. * @param {http.Request} req
  332. * @param {http.Response} res
  333. * @api private
  334. */
  335. Server.prototype.serveMap = function(req, res){
  336. // Per the standard, ETags must be quoted:
  337. // https://tools.ietf.org/html/rfc7232#section-2.3
  338. var expectedEtag = '"' + clientVersion + '"';
  339. var etag = req.headers['if-none-match'];
  340. if (etag) {
  341. if (expectedEtag == etag) {
  342. debug('serve client 304');
  343. res.writeHead(304);
  344. res.end();
  345. return;
  346. }
  347. }
  348. debug('serve client sourcemap');
  349. res.setHeader('Content-Type', 'application/json');
  350. res.setHeader('ETag', expectedEtag);
  351. res.writeHead(200);
  352. res.end(clientSourceMap);
  353. };
  354. /**
  355. * Binds socket.io to an engine.io instance.
  356. *
  357. * @param {engine.Server} engine engine.io (or compatible) server
  358. * @return {Server} self
  359. * @api public
  360. */
  361. Server.prototype.bind = function(engine){
  362. this.engine = engine;
  363. this.engine.on('connection', this.onconnection.bind(this));
  364. return this;
  365. };
  366. /**
  367. * Called with each incoming transport connection.
  368. *
  369. * @param {engine.Socket} conn
  370. * @return {Server} self
  371. * @api public
  372. */
  373. Server.prototype.onconnection = function(conn){
  374. debug('incoming connection with id %s', conn.id);
  375. var client = new Client(this, conn);
  376. client.connect('/');
  377. return this;
  378. };
  379. /**
  380. * Looks up a namespace.
  381. *
  382. * @param {String|RegExp|Function} name nsp name
  383. * @param {Function} [fn] optional, nsp `connection` ev handler
  384. * @api public
  385. */
  386. Server.prototype.of = function(name, fn){
  387. if (typeof name === 'function' || name instanceof RegExp) {
  388. const parentNsp = new ParentNamespace(this);
  389. debug('initializing parent namespace %s', parentNsp.name);
  390. if (typeof name === 'function') {
  391. this.parentNsps.set(name, parentNsp);
  392. } else {
  393. this.parentNsps.set((nsp, conn, next) => next(null, name.test(nsp)), parentNsp);
  394. }
  395. if (fn) parentNsp.on('connect', fn);
  396. return parentNsp;
  397. }
  398. if (String(name)[0] !== '/') name = '/' + name;
  399. var nsp = this.nsps[name];
  400. if (!nsp) {
  401. debug('initializing namespace %s', name);
  402. nsp = new Namespace(this, name);
  403. this.nsps[name] = nsp;
  404. }
  405. if (fn) nsp.on('connect', fn);
  406. return nsp;
  407. };
  408. /**
  409. * Closes server connection
  410. *
  411. * @param {Function} [fn] optional, called as `fn([err])` on error OR all conns closed
  412. * @api public
  413. */
  414. Server.prototype.close = function(fn){
  415. for (var id in this.nsps['/'].sockets) {
  416. if (this.nsps['/'].sockets.hasOwnProperty(id)) {
  417. this.nsps['/'].sockets[id].onclose();
  418. }
  419. }
  420. this.engine.close();
  421. if (this.httpServer) {
  422. this.httpServer.close(fn);
  423. } else {
  424. fn && fn();
  425. }
  426. };
  427. /**
  428. * Expose main namespace (/).
  429. */
  430. var emitterMethods = Object.keys(Emitter.prototype).filter(function(key){
  431. return typeof Emitter.prototype[key] === 'function';
  432. });
  433. emitterMethods.concat(['to', 'in', 'use', 'send', 'write', 'clients', 'compress', 'binary']).forEach(function(fn){
  434. Server.prototype[fn] = function(){
  435. return this.sockets[fn].apply(this.sockets, arguments);
  436. };
  437. });
  438. Namespace.flags.forEach(function(flag){
  439. Object.defineProperty(Server.prototype, flag, {
  440. get: function() {
  441. this.sockets.flags = this.sockets.flags || {};
  442. this.sockets.flags[flag] = true;
  443. return this;
  444. }
  445. });
  446. });
  447. /**
  448. * BC with `io.listen`
  449. */
  450. Server.listen = Server;