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