da32c9500491adfae47701752d381f7488fbf27d
[henge/kiak.git] / router.js
1 /**
2 * @file HTTP(S) Router that treats the first directory in a URL's path as
3 * a route to a host.
4 * @author Ken Grimes
5 * @version 0.0.1
6 * @license AGPL-3.0
7 * @copyright Strapp.io
8 */
9
10 const dlog = (msg) => console.log(msg)
11
12 exports = {
13 /** Regular expression for valid routes
14 * @prop {Object.RegEx} validRoutes - matches valid route names
15 */
16 validRoutes: /[a-zA-Z][a-zA-Z0-9\-_]*/,
17
18 /** A map of routes
19 * @prop {Object.Map} routes - all the routes!
20 */
21 routes: {},
22
23 /** Parameters set on bootup (startHttpServer)
24 * @prop {string[2]} skelPage - html document split in twain for JS injection
25 * @prop {string} clientJS - jerverscripps to inject in skelPage for clients
26 * @prop {string} hostJS - jerverscripps for the hosts
27 * @prop {string} httpdRoot - a normalized path for http-servable files
28 * @prop {string} bindJail - jail bindings to this path
29 */
30 skelPage: undefined,
31 clientJS: undefined,
32 hostJS: undefined,
33 httpdRoot: undefined,
34 bindJail: undefined,
35
36 /** @func
37 * @summary Start main HTTP server
38 * @desc starts up an HTTP or HTTPS server used for routing
39 * @arg {Object} conf - object containing configuration properties
40 * @prop {number|string} conf.port - local system port to bind to
41 * @prop {string} conf.skelFile - location of the skeleton HTML page
42 * @prop {string} conf.clientJS - client JS file
43 * @prop {string} conf.hostJS - host JS file
44 * @prop {string} [conf.httpdRoot] - root path of http-accessible files, if
45 * undefined no files are accessible
46 * @prop {Object} [conf.tls] - if present, startHttpServer will use tls
47 * @prop {string} [conf.tls.certFile] - tls certificate file
48 * @prop {string} [conf.tls.keyFile] - tls public key file
49 */
50 startHttpServer: function (conf) {
51 if ('httpd' in this)
52 throw new Error('httpd already running')
53 if (conf.tls == undefined)
54 this.httpd = require('http').createServer((req, res) =>
55 this.httpdListener(req, res))
56 else if (!('keyFile' in conf.tls) || !('certFile' in conf.tls))
57 throw new Error('HTTPS requires a valid key and cert')
58 else
59 this.syncReads([conf.tls.keyFile, conf.tls.certFile]).then((results) => {
60 Object.defineProperty(this, 'httpsOpts', {
61 value: {
62 key: results[conf.tls.keyFile],
63 cert: results[conf.tls.certFile]
64 }
65 })
66 this.httpd =
67 require('https').createServer(this.httpsOpts, (request,response) =>
68 this.httpdListener(request,response))
69 .listen(conf.port)
70 })
71 if (conf.httpdRoot) {
72 this.httpdRoot = require('path').normalize(conf.httpdRoot)
73 while (this.httpdRoot[this.httpdRoot.length - 1] == require('path').sep)
74 this.httpdRoot = this.httpdRoot.slice(0,-1)
75 }
76 this.syncReads([conf.skelFile, conf.clientJS, conf.hostJS])
77 .then((results) => {
78 this.skelPage = results[conf.skelFile].split('<!--STRAPP_SRC-->')
79 this.clientJS = results[conf.clientJS]
80 this.hostJS = results[conf.hostJS]
81 })
82 .catch((err) => {
83 console.log(err)
84 })
85 console.log(`HTTP${(conf.tls == undefined) ? '' : 'S'} ` +
86 <<<<<<< HEAD
87 `Server Started on port ${conf.port}${this.httpdRoot ?
88 =======
89 `Server Started on port ${conf.port}${this.httpdRoot ?
90 >>>>>>> 3f5fce84645ec2c59aec0908807d1e9cb3e05e2d
91 `, serving files from ${this.httpdRoot}`:''}`)
92 },
93
94 /** @func
95 * @summary Create a binding for the server
96 * @desc makes a new route which is bound to a file or a path. routes
97 * bound to files always serve that file, regardless of any
98 * additional path parameters provided by the URI
99 * @arg {string} routeName - the route to create
100 * @arg {string} path - the path to the file or directory to bind
101 */
102 createBind: function (routeName, path) {
103 dlog(`Binding ${routeName} to ${path}`)
104 if (routeName in this.routes)
105 throw new Error(`route ${routeName} already exists`)
106 path = require('path').normalize(path)
107 if (this.bindJail
108 && path.indexOf(`${this.bindJail}/`) !== 0
109 && this.bindJail != path)
110 throw new Error(`${routeName}:${path} jailed to ${this.bindJail}`)
111 if (require('fs').existsSync(path)) {
112 this.routes[routeName] = {
113 bind: {
114 path: path,
115 dir: require('fs').lstatSync(path).isDirectory()
116 }
117 }
118 }
119 else
120 throw new Error(`${path} not found, ${routeName} not bound`)
121 },
122
123 /** @func
124 * @summary Router
125 * @desc listens for http client requests and services routes/files
126 * @arg {http.ClientRequest} request
127 * @arg {http.ServerResponse} response
128 */
129 httpdListener: function (request,response) {
130 dlog(`Received request ${request.method} ${request.url}`)
131 let htArgv = request.url.slice(1).split('?')
132 const routeName = htArgv[0].split('/')[0]
133 let route = this.routes[routeName]
134 <<<<<<< HEAD
135 console.log(`route is ${route}`)
136 =======
137 >>>>>>> 3f5fce84645ec2c59aec0908807d1e9cb3e05e2d
138 /* If the route exists, check if we are a returning host or a new client */
139 if (route) {
140 if (route.bind) {
141 htArgv[0] = htArgv[0].replace(`${routeName}`,'')
142 if (htArgv[0][0] === '/')
143 htArgv[0] = htArgv[0].slice(1)
144 this.serveBind(response, route.bind, htArgv)
145 }
146 //TODO: auth better than this (ip spoofing is easy)
147 // but this will require a more involved host-creation process
148 // that isn't just "give you a route if it's available" on visit
149 /* else if (route.origin == (request.headers['x-forwarded-for'] ||
150 request.connection.remoteAddress))
151 this.serveHost(response, route, htArgv)
152 else */
153 <<<<<<< HEAD
154 else {
155 =======
156 >>>>>>> 3f5fce84645ec2c59aec0908807d1e9cb3e05e2d
157 this.serveClient(request, response, route)
158 }
159 }
160 /* If it's a valid routename that doesn't exist, make this client a host */
161 else if (this.validRoutes.test(routeName)) {
162 this.routes[routeName] = true
163 require('get-port')()
164 .then((port) => {
165 this.createHost(routeName, htArgv, port, request, response)
166 })
167 .catch((err) => {
168 delete this.routes[routeName]
169 console.log(err)
170 })
171 }
172 /* Try servicing files if we have a root directory for it */
173 else if (this.httpdRoot) {
174 let realPath = require('path').join(this.httpdRoot, htArgv[0])
175 if (realPath == this.httpdRoot)
176 realPath += '/index.html'
177 if (realPath.indexOf(`${this.httpdRoot}/`) == 0) {
178 const stat_cb = (err, stat) => {
179 if (err) {
180 response.writeHead(404)
181 response.end()
182 console.log(err)
183 }
184 else if (stat.isDirectory()) {
185 realPath += '/index.html'
186 require('fs').lstat(realPath, stat_cb)
187 }
188 else if (stat.isFile())
189 this.serveFile(response, realPath)
190 else {
191 response.writeHead(403)
192 response.end()
193 }
194 }
195 require('fs').lstat(realPath, stat_cb)
196 }
197 else {
198 response.writeHead(400)
199 response.end()
200 }
201 }
202 /* Unhandled */
203 else {
204 response.writeHead(404)
205 response.end()
206 }
207 },
208
209 /** @func
210 * @summary Serves a binding to a client
211 * @desc Resolves a binding and serves a response to the client
212 * @arg {http.ServerResponse} response - the response to use
213 * @arg {Object} bind - the binding to serve the client
214 * @arg {string[]} argv - path and arguments for the bind
215 */
216 serveBind: function (response, bind, argv) {
217 dlog(`Serving binding ${bind.path}/${argv[0]}`)
218 if (bind.dir) {
219 if (argv[0] == '')
220 argv[0] = 'index.html'
221 this.serveFile(response, require('path').join(bind.path, argv[0]))
222 }
223 else
224 this.serveFile(response, bind.path)
225 },
226
227 /** @func
228 * @summary Serve a route to an http client
229 * @desc routes may be bound to the filesystem, or to an outgoing host
230 * @arg {http.ClientRequest} request - request from the client
231 * @arg {http.ServerResponse} response - response object to use
232 * @arg {Object} route - route associated with client request
233 */
234 serveClient: function (request, response, route) {
235 const type = request.headers['x-strapp-type']
236 const pubKey = request.headers['x-strapp-pubkey']
237 dlog(`Client ${type || 'HT'} request routed to ${route.name}`)
238 switch (type) {
239 case null:
240 case undefined:
241 response.writeHead(200, { 'Content-Type': 'text/html' })
242 response.write(`${this.skelPage[0]}${this.clientJS}${this.skelPage[1]}`)
243 response.end()
244 break
245 case 'ice-candidate-request':
246 case 'ice-candidate-submission':
247 case 'client-sdp-offer':
248 let data = ''
249 if (pubKey) {
250 let data = request.headers['x-strapp-offer']
251 route.pendingResponses.addResponse(pubKey, response)
252 route.socket.send(`${pubKey} ${type} ${data}`)
253 }
254 else {
255 response.writeHead(401)
256 response.end()
257 }
258 break
259 default:
260 response.writeHead(400)
261 response.end()
262 }
263 },
264
265 <<<<<<< HEAD
266 /** @func
267 =======
268 /** @func
269 >>>>>>> 3f5fce84645ec2c59aec0908807d1e9cb3e05e2d
270 * @summary Create a new route for a host
271 * @desc makes a new route for the given route name
272 * @arg {string} routeName - name of the new route
273 * @arg {string[]} argv - Origin address from the request that made this
274 * route (for security verification on the socket)
275 * @arg {number|string} port - the port to listen on for websocket
276 * @arg {http.ClientRequest} request - host's request
277 * @arg {http.ServerResponse} response - responder
278 */
279 createHost: function (routeName, argv, port, request, response) {
280 const origin = (request.headers['x-forwarded-for'] ||
281 request.connection.remoteAddress)
282 dlog(`New ${this.httpsOpts?'TLS ':''}route ${routeName}:${port}=>${origin}`)
283 const httpd = this.httpsOpts
284 ? require('https').createServer(this.httpsOpts)
285 : require('http').createServer()
286 const route = {
287 pendingResponses: new Map([]),
288 origin: origin,
289 httpd: httpd,
290 name: routeName,
291 port: port,
292 wsd: undefined,
293 socket: undefined
294 }
295 route.httpd.listen(port)
296 route.wsd = new (require('ws').Server)({ server: httpd })
297 .on('connection', (socket) => {
298 route.socket = socket
299 socket.on('message', (msg) =>
300 this.hostMessage(msg,route))
301 })
302 route.pendingResponses.addResponse = function (key, response_p) {
303 let responses = this.get(key) || []
304 <<<<<<< HEAD
305 responses.push(response_p)
306 this.set(key, responses)
307 =======
308 this.set(key, responses.push(response_p))
309 >>>>>>> 3f5fce84645ec2c59aec0908807d1e9cb3e05e2d
310 }
311 this.routes[routeName] = route
312 this.serveHost(response, route, argv)
313 },
314
315 <<<<<<< HEAD
316 /** @func
317 =======
318 /** @func
319 >>>>>>> 3f5fce84645ec2c59aec0908807d1e9cb3e05e2d
320 * @summary Serve a route to an authorized http host
321 * @desc services host application to the client, establishing a socket
322 * @arg {http.ServerResponse} response - response object to use
323 * @arg {Object} route - the route that belongs to this host
324 * @arg {string[]} argv - vector of arguments sent to the host
325 */
326 serveHost: function (response, route, argv) {
327 dlog(`Serving host ${route.origin}`)
328 response.writeHead(200, { 'Content-Type': 'text/html' })
329 response.write(`${this.skelPage[0]}` +
330 `\tconst _strapp_port = ${route.port}\n` +
331 `\tconst _strapp_protocol = ` +
332 `'${this.httpsOpts ? 'wss' : 'ws'}'\n` +
333 `${this.hostJS}\n${this.skelPage[1]}`)
334 response.end()
335 },
336
337 /** @func
338 * @summary handle host message
339 * @desc receives a message from a host, handles the command (first character),
340 * and responds to either the host or the client, or both. Commands
341 * are whitespace separated strings.
342 * Commands:
343 * Forward Payload to Client)
344 * < clientKey payload [header]
345 * Route 'payload' to the client identified by 'clientKey'.
346 * The optional 'header' argument is a stringified JSON object,
347 * which will be written to the HTTP response header
348 * In case of multiple requests from a single client, the
349 * oldest request will be serviced on arrival of message
350 * Translate SDP and Forward to Client)
351 * ^ clientKey sdp [header]
352 * Route the JSON object 'sdp' to the client, after translating
353 * for interop between browsers using planB or Unified. Other
354 * than the interop step, this is identical to the '<' command
355 * Error)
356 * ! errorMessage errorCode [offendingMessage]
357 * Notify host that an error has occured, providing a message
358 * and error code. 'offendingMessage', if present, is the
359 * message received from the remote that triggered the error.
360 * @arg {string} message - raw string from the host
361 * @arg {Object} route - the route over
362 */
363 hostMessage: function (message, route) {
364 const argv = message.split(' ')
365 const command = argv[0][0]
366 argv = argv.slice(1)
367 dlog(`Received host message from ${route.name}: ${command}`)
368 switch (command) {
369 case '^':
370 if (argv.length < 2) {
371 dlog(`Malformed '${command}' command from ${route.origin}`)
372 route.socket.send(`! "Insufficient arguments" 0 ${message}`)
373 break
374 }
375 argv[1] = JSON.parse(argv[1])
376 //TODO: interop step
377 argv[1] = JSON.stringify(argv[1])
378 //TODO: argv[1] = encryptForClient(argv[0], argv[1])
379 /* Fallthrough to '<' behavior after translating argv[1] */
380 case '<':
381 const response = route.pendingResponses.get(argv[0]).shift()
382 if (!response)
383 route.socket.send(`! "No pending responses for client ${argv[0]}" 0 `
384 + message)
385 else if (argv.length === 2 || argv.length === 3) {
386 const header = argv.length === 3 ? JSON.parse(argv[2]) : {}
387 if (!('Content-Type' in header))
388 header['Content-Type'] = 'application/octet-stream'
389 response.writeHead(200, header)
390 response.write(argv[1])
391 response.end()
392 }
393 else
394 route.socket.send(`! "Insufficient arguments" 0 ${message}`)
395 break
396 case '!':
397 if (argv.length === 3)
398 argv[0] += `\nIn message: ${argv[2]}`
399 console.log(`Error[${route.origin}|${argv[1]}]:${argv[0]}`)
400 break
401 default:
402 route.socket.send(`! "Unknown command '${command}'" 0 ${message}`)
403 dlog(`Host ${route.origin} send unknown command: ${message}`)
404 break
405 }
406 },
407
408 /** @func
409 * @summary Serve a file to an http client after a request
410 * @desc reads files from the system to be distributed to clients, and
411 * buffers recently accessed files
412 * @arg {http.ServerResponse} response - the response object to use
413 * @arg {string} filePath - relative location of the file
414 */
415 serveFile: function (response, filePath, rootPath) {
416 //TODO: Make a buffer to hold recently used files, and only read if we
417 // have to (don't forget to preserve mimetype)
418 require('fs').readFile(filePath, { encoding: 'utf8' }, (err, data) => {
419 if (err || data == undefined)
420 response.writeHead(404)
421 else {
422 response.writeHead(200, {
423 'Content-Type': require('mime').lookup(filePath)
424 })
425 response.write(data)
426 }
427 response.end()
428 })
429 },
430
431 /** @func
432 * @summary Synchronize Reading Multiple Files
433 * @desc reads an array of files into an object, whose keys are the
434 * input filenames, and values are the data read
435 * @arg {string[]} files - array of file names to read
436 * @arg {Object} [readOpts] - options to pass to fs.readFile()
437 */
438 syncReads: (files, readOpts) => new Promise((resolve,reject) => {
439 dlog(`syncing reads from ${files}`)
440 let count = 0
441 let results = {}
442 const read_cb = (fileName) => (err, data) => {
443 if (err)
444 reject(err)
445 else
446 results[fileName] = data
447 if (++count === files.length)
448 resolve(results)
449 }
450 if (readOpts == undefined)
451 readOpts = { encoding: 'utf8' }
452 files.forEach((file) =>
453 require('fs').readFile(file, readOpts, read_cb(file)))
454 })
455 }
456
457 module.exports = exports