From 8a767de924e57ea87bc2d0d0b9cf9864e523d916 Mon Sep 17 00:00:00 2001 From: jordan lavatai Date: Thu, 29 Jun 2017 21:30:43 -0700 Subject: [PATCH] added host --- host.js | 48 +++++++++++++------ main.js | 145 ++++++++++++++++++++++++++++---------------------------- 2 files changed, 106 insertions(+), 87 deletions(-) diff --git a/host.js b/host.js index 45ea63f..e6600de 100644 --- a/host.js +++ b/host.js @@ -1,19 +1,39 @@ document.title = "Strapp.io Host" -const clients = [] +const clients = [] //TODO: Change to Map if ("WebSocket" in window) { - const wsock = new WebSocket(`${_strapp_protocol}://${window.location.hostname}:${_strapp_port}`) - wsock.onopen = () => { - console.log(`Strapped to ${_strapp_protocol}://${window.location.hostname}:${_strapp_port}`) - } - wsock.onmessage = (evt) => { - console.log("Incoming connection from " + evt.data) - console.log("TODO: Open a socket to this client") - wsock.send("Got " + evt.data) - clients.push({ - ip: evt.data, - dataChannel: undefined - }) - } + document.addEventListener('DOMContentLoaded', (event) => { + const wsock = new WebSocket(`${_strapp_protocol}://${window.location.hostname}:${_strapp_port}`) + wsock.onopen = () => { + console.log(`Strapped to ${_strapp_protocol}://${window.location.hostname}:${_strapp_port}`) + + } + wsock.onmessage = (msg) => { + /* Message is offer from client */ + console.log("Incoming connection " + msg) + + /* State machine to parse offer */ + + /* New Client Connection*/ + hpc = new RTCPeerConnection() + + hpc.createAnswer().then((offer) => { + return hpc.setLocalDescription(offer) + }).then(() => { + return hpc.setRemoteDescription(msg.sdp) + }).then(() => { + const hpk = getPublicKey() + wsock.send({ + cmd: '< sdp pubKey' + sdp: hpc.localDescription + pubKey: hpk + }) + clients.push({ + sdp: hpc.localDescription + clientPubKey: msg.pubKey + }) + }) + } + }) } else { document.addEventListener('DOMContentLoaded', () => { diff --git a/main.js b/main.js index cdf8601..75e762f 100644 --- a/main.js +++ b/main.js @@ -1,12 +1,12 @@ /** - * @file Node entry and main driver - * @author Jordan Lavatai, Ken Grimes - * @version 0.0.1 - * @license AGPL-3.0 - * @copyright loljk 2017 - * @summary HTTP(S) Router that uses the first directory in the requested URL - * as the route name - */ +* @file Node entry and main driver +* @author Jordan Lavatai, Ken Grimes +* @version 0.0.1 +* @license AGPL-3.0 +* @copyright loljk 2017 +* @summary HTTP(S) Router that uses the first directory in the requested URL +* as the route name +*/ const fs = require('fs') const ws = require('ws') const path = require('path') @@ -27,34 +27,34 @@ const router = { respond: (request,response) => { const serveFile = (fPath) => { fs.readFile(fPath, { encoding: 'utf8' }, (err, data) => { - if (err || data == undefined) { - response.writeHead(404) - response.end() - } - else { - response.writeHead(200, { 'Content-Type': mime.lookup(fPath) }) - response.write(data) - response.end() - } + if (err || data == undefined) { + response.writeHead(404) + response.end() + } + else { + response.writeHead(200, { 'Content-Type': mime.lookup(fPath) }) + response.write(data) + response.end() + } }) } const htArgv = request.url.slice(1).split("?") let routePath = htArgv[0].split('/') let routeName = routePath[0] if (routeName === '' || routeName === 'index.html') - serveFile(opts['index']) + serveFile(opts['index']) else if (routeName in opts['bindings']) { let localPath = path.normalize(opts['bindings'][routeName].concat(path.sep + routePath.slice(1).join(path.sep))) if (localPath.includes(opts['bindings'][routeName])) { - fs.readdir(localPath, (err, files) => { - if (err) - serveFile(localPath) - else - serveFile(`${localPath}/index.html`) - }) + fs.readdir(localPath, (err, files) => { + if (err) + serveFile(localPath) + else + serveFile(`${localPath}/index.html`) + }) } else { - console.log(`SEC: ${localPath} references files not in route`) + console.log(`SEC: ${localPath} references files not in route`) } } else if (routeName in router.routes) { @@ -71,63 +71,62 @@ const router = { const newRoute = {} newRoute.host = request.headers['x-forwarded-for'] || request.connection.remoteAddress getport().then( (port) => { - newRoute.port = port - if (opts['no-tls']) - newRoute.httpd = http.createServer() - else - newRoute.httpd = https.createServer(router.httpsOpts) - newRoute.httpd.listen(newRoute.port) - newRoute.wsd = new ws.Server( { server: newRoute.httpd } ) - newRoute.wsd.on('connection', (sock) => { - newRoute.socket = sock - sock.on('message', (msg) => { console.log(`[${newRoute.host}] ${msg}`) }) - }) - console.log(`Listening for websocket ${newRoute.host} on port ${newRoute.port}`) - router.routes[routeName] = newRoute + newRoute.port = port + if (opts['no-tls']) + newRoute.httpd = http.createServer() + else + newRoute.httpd = https.createServer(router.httpsOpts) + newRoute.httpd.listen(newRoute.port) + newRoute.wsd = new ws.Server( { server: newRoute.httpd } ) + newRoute.wsd.on('connection', (sock) => { + newRoute.socket = sock + sock.on('message', (msg) => { console.log(`[${newRoute.host}] ${msg}`) }) + }) + console.log(`Listening for websocket ${newRoute.host} on port ${newRoute.port}`) + router.routes[routeName] = newRoute }).then(() => { - response.writeHead(200, { 'Content-Type': 'text/html' }) - response.write(`${router.skelPage[0]}` + - `\tconst _strapp_port = ${newRoute.port}\n` + - `\tconst _strapp_protocol = '${router.wsProtocol}'\n` + - `${router.hostJS}\n${router.skelPage[1]}`) - response.end() - }) + response.writeHead(200, { 'Content-Type': 'text/html' }) + response.write(`${router.skelPage[0]}` + + `\tconst _strapp_port = ${newRoute.port}\n` + + `\tconst _strapp_protocol = '${router.wsProtocol}'\n` + + `${router.hostJS}\n${router.skelPage[1]}`) + response.end() + }) + } + } - } -} -/** - * @summary Boot up the router. With TLS, we must wait for file reads to sync. - */ -if (!opts['no-tls']) { - let filesRead = 0 - let key = undefined - let cert = undefined - const syncRead = () => { - if (++filesRead == 2) { - if (key == undefined) + /** + * @summary Boot up the router. With TLS, we must wait for file reads to sync. + */ + if (!opts['no-tls']) { + let filesRead = 0 + let key = undefined + let cert = undefined + const syncRead = () => { + if (++filesRead == 2) { + if (key == undefined) console.log(`ERR: Key ${opts['ca-key']} inaccessible, tls will fail`) - if(cert == undefined) + if(cert == undefined) console.log(`ERR: Cert ${opts['ca-cert']} inaccessible, tls will fail`) - else if (key != undefined) { - router.httpsOpts = { cert: cert, key: key} - router.httpd = https.createServer(router.httpsOpts, router.respond) + else if (key != undefined) { + router.httpsOpts = { cert: cert, key: key} + router.httpd = https.createServer(router.httpsOpts, router.respond) .listen(opts['port']) + } } } + fs.readFile(opts['ca-key'], { encoding: 'utf8' }, (err, data) => { + if (!err) key = data + syncRead() + }) + fs.readFile(opts['ca-cert'], { encoding: 'utf8' }, (err, data) => { + if (!err) cert = data + syncRead() + }) } - fs.readFile(opts['ca-key'], { encoding: 'utf8' }, (err, data) => { - if (!err) key = data - syncRead() - }) - fs.readFile(opts['ca-cert'], { encoding: 'utf8' }, (err, data) => { - if (!err) cert = data - syncRead() - }) -} -else + else router.httpd = http.createServer(router.respond).listen(opts['port']) -//TODO: if ("electron" in process.versions) open a local renderwindow, and route to it - + //TODO: if ("electron" in process.versions) open a local renderwindow, and route to it -- 2.18.0