2 * @file HTTP(S) Router that treats the first directory in a URL's path as
10 const dlog
= (msg
) => console
.log(msg
)
13 /** Regular expression for valid routes
14 * @prop {Object.RegEx} validRoutes - matches valid route names
16 validRoutes
: /[a-zA-Z][a-zA-Z0-9\-_]*/,
19 * @prop {Object.Map} routes - all the routes!
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
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
50 startHttpServer: function (conf
) {
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')
59 this.syncReads([conf
.tls
.keyFile
, conf
.tls
.certFile
]).then((results
) => {
60 Object
.defineProperty(this, 'httpsOpts', {
62 key
: results
[conf
.tls
.keyFile
],
63 cert
: results
[conf
.tls
.certFile
]
67 require('https').createServer(this.httpsOpts
, (request
,response
) =>
68 this.httpdListener(request
,response
))
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)
76 this.syncReads([conf
.skelFile
, conf
.clientJS
, conf
.hostJS
])
78 this.skelPage
= results
[conf
.skelFile
].split('<!--STRAPP_SRC-->')
79 this.clientJS
= results
[conf
.clientJS
]
80 this.hostJS
= results
[conf
.hostJS
]
85 console
.log(`HTTP${(conf.tls == undefined) ? '' : 'S'} ` +
86 `Server Started on port ${conf.port}${this.httpdRoot ?
87 `, serving files
from ${this.httpdRoot}
`:''}`)
91 * @summary Create a binding for the server
92 * @desc makes a new route which is bound to a file or a path. routes
93 * bound to files always serve that file, regardless of any
94 * additional path parameters provided by the URI
95 * @arg {string} routeName - the route to create
96 * @arg {string} path - the path to the file or directory to bind
98 createBind: function (routeName
, path
) {
99 dlog(`Binding ${routeName} to ${path}`)
100 if (routeName
in this.routes
)
101 throw new Error(`route ${routeName} already exists`)
102 path
= require('path').normalize(path
)
104 && path
.indexOf(`${this.bindJail}/`) !== 0
105 && this.bindJail
!= path
)
106 throw new Error(`${routeName}:${path} jailed to ${this.bindJail}`)
107 if (require('fs').existsSync(path
)) {
108 this.routes
[routeName
] = {
111 dir
: require('fs').lstatSync(path
).isDirectory()
116 throw new Error(`${path} not found, ${routeName} not bound`)
121 * @desc listens for http client requests and services routes/files
122 * @arg {http.ClientRequest} request
123 * @arg {http.ServerResponse} response
125 httpdListener: function (request
,response
) {
126 dlog(`Received request ${request.method} ${request.url}`)
127 let htArgv
= request
.url
.slice(1).split('?')
128 const routeName
= htArgv
[0].split('/')[0]
129 let route
= this.routes
[routeName
]
130 /* If the route exists, check if we are a returning host or a new client */
133 htArgv
[0] = htArgv
[0].replace(`${routeName}`,'')
134 if (htArgv
[0][0] === '/')
135 htArgv
[0] = htArgv
[0].slice(1)
136 this.serveBind(response
, route
.bind
, htArgv
)
138 //TODO: auth better than this (ip spoofing is easy)
139 // but this will require a more involved host-creation process
140 // that isn't just "give you a route if it's available" on visit
141 /* else if (route.origin == (request.headers['x-forwarded-for'] ||
142 request.connection.remoteAddress))
143 this.serveHost(response, route, htArgv) */
145 this.serveClient(request
, response
, route
)
147 /* If it's a valid routename that doesn't exist, make this client a host */
148 else if (this.validRoutes
.test(routeName
)) {
149 this.routes
[routeName
] = true
150 require('get-port')()
152 this.createHost(routeName
, htArgv
, port
, request
, response
)
155 delete this.routes
[routeName
]
159 /* Try servicing files if we have a root directory for it */
160 else if (this.httpdRoot
) {
161 let realPath
= require('path').join(this.httpdRoot
, htArgv
[0])
162 if (realPath
== this.httpdRoot
)
163 realPath
+= '/index.html'
164 if (realPath
.indexOf(`${this.httpdRoot}/`) == 0) {
165 const stat_cb
= (err
, stat
) => {
167 response
.writeHead(404)
171 else if (stat
.isDirectory()) {
172 realPath
+= '/index.html'
173 require('fs').lstat(realPath
, stat_cb
)
175 else if (stat
.isFile())
176 this.serveFile(response
, realPath
)
178 response
.writeHead(403)
182 require('fs').lstat(realPath
, stat_cb
)
185 response
.writeHead(400)
191 response
.writeHead(404)
197 * @summary Serves a binding to a client
198 * @desc Resolves a binding and serves a response to the client
199 * @arg {http.ServerResponse} response - the response to use
200 * @arg {Object} bind - the binding to serve the client
201 * @arg {string[]} argv - path and arguments for the bind
203 serveBind: function (response
, bind
, argv
) {
204 dlog(`Serving binding ${bind.path}/${argv[0]}`)
207 argv
[0] = 'index.html'
208 this.serveFile(response
, require('path').join(bind
.path
, argv
[0]))
211 this.serveFile(response
, bind
.path
)
215 * @summary Serve a route to an http client
216 * @desc routes may be bound to the filesystem, or to an outgoing host
217 * @arg {http.ClientRequest} request - request from the client
218 * @arg {http.ServerResponse} response - response object to use
219 * @arg {Object} route - route associated with client request
221 serveClient: function (request
, response
, route
) {
222 const type
= request
.headers
['x-strapp-type']
223 const pubKey
= request
.headers
['x-strapp-pubkey']
224 dlog(`Client ${type || 'HT GET'} request routed to ${route.name}`)
228 response
.writeHead(200, { 'Content-Type': 'text/html' })
229 response
.write(`${this.skelPage[0]}${this.clientJS}${this.skelPage[1]}`)
232 case 'ice-candidate-request':
233 case 'ice-candidate-submission':
234 case 'client-sdp-offer':
237 let data
= request
.headers
['x-strapp-offer']
238 route
.pendingResponses
.addResponse(pubKey
, response
)
239 dlog(`${route.origin}=>\n${pubKey}\n${type}`)
240 dlog(JSON
.parse(data
))
241 route
.socket
.send(`${pubKey} ${type} ${data}`)
244 response
.writeHead(401)
249 response
.writeHead(400)
255 * @summary Create a new route for a host
256 * @desc makes a new route for the given route name
257 * @arg {string} routeName - name of the new route
258 * @arg {string[]} argv - Origin address from the request that made this
259 * route (for security verification on the socket)
260 * @arg {number|string} port - the port to listen on for websocket
261 * @arg {http.ClientRequest} request - host's request
262 * @arg {http.ServerResponse} response - responder
264 createHost: function (routeName
, argv
, port
, request
, response
) {
265 const origin
= (request
.headers
['x-forwarded-for'] ||
266 request
.connection
.remoteAddress
)
267 dlog(`New ${this.httpsOpts?'TLS ':''}route ${routeName}:${port}=>${origin}`)
268 const httpd
= this.httpsOpts
269 ? require('https').createServer(this.httpsOpts
)
270 : require('http').createServer()
272 pendingResponses
: new Map([]),
280 route
.httpd
.listen(port
)
281 route
.wsd
= new (require('ws').Server
)({ server
: httpd
})
282 .on('connection', (socket
) => {
283 route
.socket
= socket
284 socket
.on('message', (msg
) =>
285 this.hostMessage(msg
,route
))
287 route
.pendingResponses
.addResponse = function (key
, response_p
) {
288 let responses
= this.get(key
) || []
289 responses
.push(response_p
)
290 this.set(key
, responses
)
292 this.routes
[routeName
] = route
293 this.serveHost(response
, route
, argv
)
297 * @summary Serve a route to an authorized http host
298 * @desc services host application to the client, establishing a socket
299 * @arg {http.ServerResponse} response - response object to use
300 * @arg {Object} route - the route that belongs to this host
301 * @arg {string[]} argv - vector of arguments sent to the host
303 serveHost: function (response
, route
, argv
) {
304 dlog(`Serving host ${route.origin}`)
305 response
.writeHead(200, { 'Content-Type': 'text/html' })
306 response
.write(`${this.skelPage[0]}` +
307 `\tconst _strapp_port = ${route.port}\n` +
308 `\tconst _strapp_protocol = ` +
309 `'${this.httpsOpts ? 'wss' : 'ws'}'\n` +
310 `${this.hostJS}\n${this.skelPage[1]}`)
315 * @summary handle host message
316 * @desc receives a message from a host, handles the command (first character),
317 * and responds to either the host or the client, or both. Commands
318 * are whitespace separated strings.
320 * Forward Payload to Client)
321 * < clientKey payload [header]
322 * Route 'payload' to the client identified by 'clientKey'.
323 * The optional 'header' argument is a stringified JSON object,
324 * which will be written to the HTTP response header
325 * In case of multiple requests from a single client, the
326 * oldest request will be serviced on arrival of message
327 * Translate SDP and Forward to Client)
328 * ^ clientKey sdp [header]
329 * Route the JSON object 'sdp' to the client, after translating
330 * for interop between browsers using planB or Unified. Other
331 * than the interop step, this is identical to the '<' command
333 * ! errorMessage errorCode [offendingMessage]
334 * Notify host that an error has occured, providing a message
335 * and error code. 'offendingMessage', if present, is the
336 * message received from the remote that triggered the error.
337 * @arg {string} message - raw string from the host
338 * @arg {Object} route - the route over
340 hostMessage: function (message
, route
) {
341 let argv
= message
.split(' ')
342 const command
= argv
[0][0]
344 dlog(`Received host message from ${route.name}: ${command}`)
347 if (argv
.length
< 2) {
348 dlog(`Malformed '${command}' command from ${route.origin}`)
349 route
.socket
.send(`! "Insufficient arguments" 0 ${message}`)
352 argv
[1] = JSON
.parse(argv
[1])
354 argv
[1] = JSON
.stringify(argv
[1])
355 //TODO: argv[1] = encryptForClient(argv[0], argv[1])
356 /* Fallthrough to '<' behavior after translating argv[1] */
358 const response
= route
.pendingResponses
.get(argv
[0]).shift()
360 route
.socket
.send(`! "No pending responses for client ${argv[0]}" 0 `
362 else if (argv
.length
=== 2 || argv
.length
=== 3) {
363 const header
= argv
.length
=== 3 ? JSON
.parse(argv
[2]) : {}
364 if (!('Content-Type' in header
))
365 header
['Content-Type'] = 'application/octet-stream'
366 response
.writeHead(200, header
)
367 response
.write(argv
[1])
371 route
.socket
.send(`! "Insufficient arguments" 0 ${message}`)
374 if (argv
.length
=== 3)
375 argv
[0] += `\nIn message: ${argv[2]}`
376 console
.log(`Error[${route.origin}|${argv[1]}]:${argv[0]}`)
379 route
.socket
.send(`! "Unknown command '${command}'" 0 ${message}`)
380 dlog(`Host ${route.origin} send unknown command: ${message}`)
386 * @summary Serve a file to an http client after a request
387 * @desc reads files from the system to be distributed to clients, and
388 * buffers recently accessed files
389 * @arg {http.ServerResponse} response - the response object to use
390 * @arg {string} filePath - relative location of the file
392 serveFile: function (response
, filePath
, rootPath
) {
393 //TODO: Make a buffer to hold recently used files, and only read if we
394 // have to (don't forget to preserve mimetype)
395 require('fs').readFile(filePath
, { encoding
: 'utf8' }, (err
, data
) => {
396 if (err
|| data
== undefined)
397 response
.writeHead(404)
399 response
.writeHead(200, {
400 'Content-Type': require('mime').lookup(filePath
)
409 * @summary Synchronize Reading Multiple Files
410 * @desc reads an array of files into an object, whose keys are the
411 * input filenames, and values are the data read
412 * @arg {string[]} files - array of file names to read
413 * @arg {Object} [readOpts] - options to pass to fs.readFile()
415 syncReads
: (files
, readOpts
) => new Promise((resolve
,reject
) => {
416 dlog(`syncing reads from ${files}`)
419 const read_cb
= (fileName
) => (err
, data
) => {
423 results
[fileName
] = data
424 if (++count
=== files
.length
)
427 if (readOpts
== undefined)
428 readOpts
= { encoding
: 'utf8' }
429 files
.forEach((file
) =>
430 require('fs').readFile(file
, readOpts
, read_cb(file
)))
434 module
.exports
= exports