index.js 11 KB

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