X-Git-Url: https://git.kengrimes.com/?p=henge%2Fkiak.git;a=blobdiff_plain;f=router.js;h=2085e9b166e411f9e46e4adadde35c8fe8fa1aaa;hp=0da13e6e1d55602f170639ad1512ea7336167e40;hb=d17ac76d8256b00eb253138a554c7d3d51139b9e;hpb=ac8d4e6df37f875e7f2b7e6d56cc9c3dab8c23a5 diff --git a/router.js b/router.js index 0da13e6..2085e9b 100644 --- a/router.js +++ b/router.js @@ -33,7 +33,7 @@ exports = { httpdRoot: undefined, bindJail: undefined, - /** @func + /** @func * @summary Start main HTTP server * @desc starts up an HTTP or HTTPS server used for routing * @arg {Object} conf - object containing configuration properties @@ -116,7 +116,7 @@ exports = { throw new Error(`${path} not found, ${routeName} not bound`) }, - /** @func + /** @func * @summary Router * @desc listens for http client requests and services routes/files * @arg {http.ClientRequest} request @@ -136,9 +136,11 @@ exports = { this.serveBind(response, route.bind, htArgv) } //TODO: auth better than this (ip spoofing is easy) - else if (route.origin == (request.headers['x-forwarded-for'] || + // but this will require a more involved host-creation process + // that isn't just "give you a route if it's available" on visit + /* else if (route.origin == (request.headers['x-forwarded-for'] || request.connection.remoteAddress)) - this.serveHost(response, route, htArgv) + this.serveHost(response, route, htArgv) */ else this.serveClient(request, response, route) } @@ -209,7 +211,7 @@ exports = { this.serveFile(response, bind.path) }, - /** @func + /** @func * @summary Serve a route to an http client * @desc routes may be bound to the filesystem, or to an outgoing host * @arg {http.ClientRequest} request - request from the client @@ -219,7 +221,7 @@ exports = { serveClient: function (request, response, route) { const type = request.headers['x-strapp-type'] const pubKey = request.headers['x-strapp-pubkey'] - dlog(`Client ${type || 'HT'} request routed to ${route.name}`) + dlog(`Client ${type || 'HT GET'} request routed to ${route.name}`) switch (type) { case null: case undefined: @@ -232,9 +234,11 @@ exports = { case 'client-sdp-offer': let data = '' if (pubKey) { + let data = request.headers['x-strapp-offer'] route.pendingResponses.addResponse(pubKey, response) - request.on('data', (chunk) => data += chunk) - request.on('end', () => route.socket.send(`${pubKey} ${type} ${data}`)) + dlog(`${route.origin}=>\n${pubKey}\n${type}`) + dlog(JSON.parse(data)) + route.socket.send(`${pubKey} ${type} ${data}`) } else { response.writeHead(401) @@ -247,7 +251,7 @@ exports = { } }, - /** @func + /** @func * @summary Create a new route for a host * @desc makes a new route for the given route name * @arg {string} routeName - name of the new route @@ -274,25 +278,22 @@ exports = { socket: undefined } route.httpd.listen(port) - route.wsd = new (require('ws').Server)({ - server: httpd - //verifyClient: (info) => - //info.origin == origin && (info.secure || !this.httpsOpts) - }) - route.wsd.on('connection', (socket) => { - route.socket = socket - socket.on('message', (msg) => - this.hostMessage(msg,route)) - }) + route.wsd = new (require('ws').Server)({ server: httpd }) + .on('connection', (socket) => { + route.socket = socket + socket.on('message', (msg) => + this.hostMessage(msg,route)) + }) route.pendingResponses.addResponse = function (key, response_p) { let responses = this.get(key) || [] - this.set(key, responses.push(response_p)) + responses.push(response_p) + this.set(key, responses) } this.routes[routeName] = route this.serveHost(response, route, argv) }, - /** @func + /** @Func * @summary Serve a route to an authorized http host * @desc services host application to the client, establishing a socket * @arg {http.ServerResponse} response - response object to use @@ -337,7 +338,7 @@ exports = { * @arg {Object} route - the route over */ hostMessage: function (message, route) { - const argv = message.split(' ') + let argv = message.split(' ') const command = argv[0][0] argv = argv.slice(1) dlog(`Received host message from ${route.name}: ${command}`) @@ -381,7 +382,7 @@ exports = { } }, - /** @func + /** @func * @summary Serve a file to an http client after a request * @desc reads files from the system to be distributed to clients, and * buffers recently accessed files @@ -403,8 +404,8 @@ exports = { response.end() }) }, - - /** @func + + /** @func * @summary Synchronize Reading Multiple Files * @desc reads an array of files into an object, whose keys are the * input filenames, and values are the data read