Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
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.

websocket-server.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. 'use strict';
  2. const EventEmitter = require('events');
  3. const { createHash } = require('crypto');
  4. const { createServer, STATUS_CODES } = require('http');
  5. const PerMessageDeflate = require('./permessage-deflate');
  6. const WebSocket = require('./websocket');
  7. const { format, parse } = require('./extension');
  8. const { GUID, kWebSocket } = require('./constants');
  9. const keyRegex = /^[+/0-9A-Za-z]{22}==$/;
  10. /**
  11. * Class representing a WebSocket server.
  12. *
  13. * @extends EventEmitter
  14. */
  15. class WebSocketServer extends EventEmitter {
  16. /**
  17. * Create a `WebSocketServer` instance.
  18. *
  19. * @param {Object} options Configuration options
  20. * @param {Number} [options.backlog=511] The maximum length of the queue of
  21. * pending connections
  22. * @param {Boolean} [options.clientTracking=true] Specifies whether or not to
  23. * track clients
  24. * @param {Function} [options.handleProtocols] A hook to handle protocols
  25. * @param {String} [options.host] The hostname where to bind the server
  26. * @param {Number} [options.maxPayload=104857600] The maximum allowed message
  27. * size
  28. * @param {Boolean} [options.noServer=false] Enable no server mode
  29. * @param {String} [options.path] Accept only connections matching this path
  30. * @param {(Boolean|Object)} [options.perMessageDeflate=false] Enable/disable
  31. * permessage-deflate
  32. * @param {Number} [options.port] The port where to bind the server
  33. * @param {http.Server} [options.server] A pre-created HTTP/S server to use
  34. * @param {Function} [options.verifyClient] A hook to reject connections
  35. * @param {Function} [callback] A listener for the `listening` event
  36. */
  37. constructor(options, callback) {
  38. super();
  39. options = {
  40. maxPayload: 100 * 1024 * 1024,
  41. perMessageDeflate: false,
  42. handleProtocols: null,
  43. clientTracking: true,
  44. verifyClient: null,
  45. noServer: false,
  46. backlog: null, // use default (511 as implemented in net.js)
  47. server: null,
  48. host: null,
  49. path: null,
  50. port: null,
  51. ...options
  52. };
  53. if (options.port == null && !options.server && !options.noServer) {
  54. throw new TypeError(
  55. 'One of the "port", "server", or "noServer" options must be specified'
  56. );
  57. }
  58. if (options.port != null) {
  59. this._server = createServer((req, res) => {
  60. const body = STATUS_CODES[426];
  61. res.writeHead(426, {
  62. 'Content-Length': body.length,
  63. 'Content-Type': 'text/plain'
  64. });
  65. res.end(body);
  66. });
  67. this._server.listen(
  68. options.port,
  69. options.host,
  70. options.backlog,
  71. callback
  72. );
  73. } else if (options.server) {
  74. this._server = options.server;
  75. }
  76. if (this._server) {
  77. const emitConnection = this.emit.bind(this, 'connection');
  78. this._removeListeners = addListeners(this._server, {
  79. listening: this.emit.bind(this, 'listening'),
  80. error: this.emit.bind(this, 'error'),
  81. upgrade: (req, socket, head) => {
  82. this.handleUpgrade(req, socket, head, emitConnection);
  83. }
  84. });
  85. }
  86. if (options.perMessageDeflate === true) options.perMessageDeflate = {};
  87. if (options.clientTracking) this.clients = new Set();
  88. this.options = options;
  89. }
  90. /**
  91. * Returns the bound address, the address family name, and port of the server
  92. * as reported by the operating system if listening on an IP socket.
  93. * If the server is listening on a pipe or UNIX domain socket, the name is
  94. * returned as a string.
  95. *
  96. * @return {(Object|String|null)} The address of the server
  97. * @public
  98. */
  99. address() {
  100. if (this.options.noServer) {
  101. throw new Error('The server is operating in "noServer" mode');
  102. }
  103. if (!this._server) return null;
  104. return this._server.address();
  105. }
  106. /**
  107. * Close the server.
  108. *
  109. * @param {Function} [cb] Callback
  110. * @public
  111. */
  112. close(cb) {
  113. if (cb) this.once('close', cb);
  114. //
  115. // Terminate all associated clients.
  116. //
  117. if (this.clients) {
  118. for (const client of this.clients) client.terminate();
  119. }
  120. const server = this._server;
  121. if (server) {
  122. this._removeListeners();
  123. this._removeListeners = this._server = null;
  124. //
  125. // Close the http server if it was internally created.
  126. //
  127. if (this.options.port != null) {
  128. server.close(() => this.emit('close'));
  129. return;
  130. }
  131. }
  132. process.nextTick(emitClose, this);
  133. }
  134. /**
  135. * See if a given request should be handled by this server instance.
  136. *
  137. * @param {http.IncomingMessage} req Request object to inspect
  138. * @return {Boolean} `true` if the request is valid, else `false`
  139. * @public
  140. */
  141. shouldHandle(req) {
  142. if (this.options.path) {
  143. const index = req.url.indexOf('?');
  144. const pathname = index !== -1 ? req.url.slice(0, index) : req.url;
  145. if (pathname !== this.options.path) return false;
  146. }
  147. return true;
  148. }
  149. /**
  150. * Handle a HTTP Upgrade request.
  151. *
  152. * @param {http.IncomingMessage} req The request object
  153. * @param {net.Socket} socket The network socket between the server and client
  154. * @param {Buffer} head The first packet of the upgraded stream
  155. * @param {Function} cb Callback
  156. * @public
  157. */
  158. handleUpgrade(req, socket, head, cb) {
  159. socket.on('error', socketOnError);
  160. const key =
  161. req.headers['sec-websocket-key'] !== undefined
  162. ? req.headers['sec-websocket-key'].trim()
  163. : false;
  164. const version = +req.headers['sec-websocket-version'];
  165. const extensions = {};
  166. if (
  167. req.method !== 'GET' ||
  168. req.headers.upgrade.toLowerCase() !== 'websocket' ||
  169. !key ||
  170. !keyRegex.test(key) ||
  171. (version !== 8 && version !== 13) ||
  172. !this.shouldHandle(req)
  173. ) {
  174. return abortHandshake(socket, 400);
  175. }
  176. if (this.options.perMessageDeflate) {
  177. const perMessageDeflate = new PerMessageDeflate(
  178. this.options.perMessageDeflate,
  179. true,
  180. this.options.maxPayload
  181. );
  182. try {
  183. const offers = parse(req.headers['sec-websocket-extensions']);
  184. if (offers[PerMessageDeflate.extensionName]) {
  185. perMessageDeflate.accept(offers[PerMessageDeflate.extensionName]);
  186. extensions[PerMessageDeflate.extensionName] = perMessageDeflate;
  187. }
  188. } catch (err) {
  189. return abortHandshake(socket, 400);
  190. }
  191. }
  192. //
  193. // Optionally call external client verification handler.
  194. //
  195. if (this.options.verifyClient) {
  196. const info = {
  197. origin:
  198. req.headers[`${version === 8 ? 'sec-websocket-origin' : 'origin'}`],
  199. secure: !!(req.socket.authorized || req.socket.encrypted),
  200. req
  201. };
  202. if (this.options.verifyClient.length === 2) {
  203. this.options.verifyClient(info, (verified, code, message, headers) => {
  204. if (!verified) {
  205. return abortHandshake(socket, code || 401, message, headers);
  206. }
  207. this.completeUpgrade(key, extensions, req, socket, head, cb);
  208. });
  209. return;
  210. }
  211. if (!this.options.verifyClient(info)) return abortHandshake(socket, 401);
  212. }
  213. this.completeUpgrade(key, extensions, req, socket, head, cb);
  214. }
  215. /**
  216. * Upgrade the connection to WebSocket.
  217. *
  218. * @param {String} key The value of the `Sec-WebSocket-Key` header
  219. * @param {Object} extensions The accepted extensions
  220. * @param {http.IncomingMessage} req The request object
  221. * @param {net.Socket} socket The network socket between the server and client
  222. * @param {Buffer} head The first packet of the upgraded stream
  223. * @param {Function} cb Callback
  224. * @throws {Error} If called more than once with the same socket
  225. * @private
  226. */
  227. completeUpgrade(key, extensions, req, socket, head, cb) {
  228. //
  229. // Destroy the socket if the client has already sent a FIN packet.
  230. //
  231. if (!socket.readable || !socket.writable) return socket.destroy();
  232. if (socket[kWebSocket]) {
  233. throw new Error(
  234. 'server.handleUpgrade() was called more than once with the same ' +
  235. 'socket, possibly due to a misconfiguration'
  236. );
  237. }
  238. const digest = createHash('sha1')
  239. .update(key + GUID)
  240. .digest('base64');
  241. const headers = [
  242. 'HTTP/1.1 101 Switching Protocols',
  243. 'Upgrade: websocket',
  244. 'Connection: Upgrade',
  245. `Sec-WebSocket-Accept: ${digest}`
  246. ];
  247. const ws = new WebSocket(null);
  248. let protocol = req.headers['sec-websocket-protocol'];
  249. if (protocol) {
  250. protocol = protocol.split(',').map(trim);
  251. //
  252. // Optionally call external protocol selection handler.
  253. //
  254. if (this.options.handleProtocols) {
  255. protocol = this.options.handleProtocols(protocol, req);
  256. } else {
  257. protocol = protocol[0];
  258. }
  259. if (protocol) {
  260. headers.push(`Sec-WebSocket-Protocol: ${protocol}`);
  261. ws._protocol = protocol;
  262. }
  263. }
  264. if (extensions[PerMessageDeflate.extensionName]) {
  265. const params = extensions[PerMessageDeflate.extensionName].params;
  266. const value = format({
  267. [PerMessageDeflate.extensionName]: [params]
  268. });
  269. headers.push(`Sec-WebSocket-Extensions: ${value}`);
  270. ws._extensions = extensions;
  271. }
  272. //
  273. // Allow external modification/inspection of handshake headers.
  274. //
  275. this.emit('headers', headers, req);
  276. socket.write(headers.concat('\r\n').join('\r\n'));
  277. socket.removeListener('error', socketOnError);
  278. ws.setSocket(socket, head, this.options.maxPayload);
  279. if (this.clients) {
  280. this.clients.add(ws);
  281. ws.on('close', () => this.clients.delete(ws));
  282. }
  283. cb(ws, req);
  284. }
  285. }
  286. module.exports = WebSocketServer;
  287. /**
  288. * Add event listeners on an `EventEmitter` using a map of <event, listener>
  289. * pairs.
  290. *
  291. * @param {EventEmitter} server The event emitter
  292. * @param {Object.<String, Function>} map The listeners to add
  293. * @return {Function} A function that will remove the added listeners when
  294. * called
  295. * @private
  296. */
  297. function addListeners(server, map) {
  298. for (const event of Object.keys(map)) server.on(event, map[event]);
  299. return function removeListeners() {
  300. for (const event of Object.keys(map)) {
  301. server.removeListener(event, map[event]);
  302. }
  303. };
  304. }
  305. /**
  306. * Emit a `'close'` event on an `EventEmitter`.
  307. *
  308. * @param {EventEmitter} server The event emitter
  309. * @private
  310. */
  311. function emitClose(server) {
  312. server.emit('close');
  313. }
  314. /**
  315. * Handle premature socket errors.
  316. *
  317. * @private
  318. */
  319. function socketOnError() {
  320. this.destroy();
  321. }
  322. /**
  323. * Close the connection when preconditions are not fulfilled.
  324. *
  325. * @param {net.Socket} socket The socket of the upgrade request
  326. * @param {Number} code The HTTP response status code
  327. * @param {String} [message] The HTTP response body
  328. * @param {Object} [headers] Additional HTTP response headers
  329. * @private
  330. */
  331. function abortHandshake(socket, code, message, headers) {
  332. if (socket.writable) {
  333. message = message || STATUS_CODES[code];
  334. headers = {
  335. Connection: 'close',
  336. 'Content-Type': 'text/html',
  337. 'Content-Length': Buffer.byteLength(message),
  338. ...headers
  339. };
  340. socket.write(
  341. `HTTP/1.1 ${code} ${STATUS_CODES[code]}\r\n` +
  342. Object.keys(headers)
  343. .map((h) => `${h}: ${headers[h]}`)
  344. .join('\r\n') +
  345. '\r\n\r\n' +
  346. message
  347. );
  348. }
  349. socket.removeListener('error', socketOnError);
  350. socket.destroy();
  351. }
  352. /**
  353. * Remove whitespace characters from both ends of a string.
  354. *
  355. * @param {String} str The string
  356. * @return {String} A new string representing `str` stripped of whitespace
  357. * characters from both its beginning and end
  358. * @private
  359. */
  360. function trim(str) {
  361. return str.trim();
  362. }