From: jordan lavatai Date: Fri, 30 Jun 2017 16:20:18 +0000 (-0700) Subject: client + server + host fixes X-Git-Url: https://git.kengrimes.com/?p=henge%2Fkiak.git;a=commitdiff_plain;h=10b1fcdf58cde8a6c6d5ebfa4bba76ef2adcf3dd client + server + host fixes --- diff --git a/client.js b/client.js index 1c03da4..69ac81e 100644 --- a/client.js +++ b/client.js @@ -32,7 +32,8 @@ function pollServer(url, clientPubKey, func) { func(url, clientPubKey, resolve, reject ) }) } -/* If https connection, should be already defined. If not https,*/ + +/* TODO: duplicate in both client.js and host.js */ function getPublicKey() { /* Check local storage for public key */ if (window.localStorage.getItem('public-key') === undefined) { @@ -65,8 +66,7 @@ cpc.createOffer().then((offer) => { /* Poll for answer */ return pollServer(window.location, offer, pollServerTimeout) }).then((answer) => { - /* TODO: Extract sdp from answer ?*/ console.log(answer) - /* State machine to parse answer */ + /* TODO: State machine to parse answer */ cpc.setRemoteDescription(answer.sdp) }) diff --git a/host.js b/host.js index e6600de..f70732b 100644 --- a/host.js +++ b/host.js @@ -11,7 +11,7 @@ if ("WebSocket" in window) { /* Message is offer from client */ console.log("Incoming connection " + msg) - /* State machine to parse offer */ + /* TODO: State machine to parse offer */ /* New Client Connection*/ hpc = new RTCPeerConnection() @@ -28,7 +28,8 @@ if ("WebSocket" in window) { pubKey: hpk }) clients.push({ - sdp: hpc.localDescription + host-sdp: hpc.localDescription, + client-sdp: hpc.remoteDescription, clientPubKey: msg.pubKey }) }) @@ -40,3 +41,18 @@ else { document.body.innerHTML = 'Websockets not supported in your browser' }) } +/* TODO: duplicate in both client.js and host.jhs */ +function getPublicKey() { + /* Check local storage for public key */ + if (window.localStorage.getItem('public-key') === undefined) { + /* If doesn't exist, generate public and private key pair, store in + local storage */ + crypto.subtle.generateKey({name:'RSA-OAEP', length: 192}, true, ['encrypt', 'decrypt']) + .then((keyPair) => { + /* TODO: Do we need to store the private key as well? */ + window.localStorage.setItem('public-key', keyPair.type.public.toString()) + }) + } + console.log(window.localStorage.getItem('public-key')) + return window.localStorage.getItem('public-key') +} diff --git a/main.js b/main.js index 75e762f..4ada8bb 100644 --- a/main.js +++ b/main.js @@ -25,6 +25,8 @@ const router = { httpd: undefined, wsProtocol: opts['no-tls'] ? 'ws' : 'wss', respond: (request,response) => { + console.log('server handling request') + console.log(request) const serveFile = (fPath) => { fs.readFile(fPath, { encoding: 'utf8' }, (err, data) => { if (err || data == undefined) { @@ -42,7 +44,7 @@ const router = { 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])) { @@ -57,14 +59,22 @@ const router = { console.log(`SEC: ${localPath} references files not in route`) } } + /* TODO: Handle reconnecting host */ else if (routeName in router.routes) { const route = router.routes[routeName] - response.writeHead(200, { 'Content-Type': 'text/html' }) - response.write(`${router.skelPage[0]}${router.clientJS}${router.skelPage[1]}`) - response.end() - //TODO: if route.socket == undefined: have server delay this send until host connects - // (this happens when a client connects to an active route with no currently-online host) - route.socket.send(request.headers['x-forwarded-for'] || request.connection.remoteAddress) + console.log(request) + /* Client is INIT GET */ + if (request) { + response.writeHead(200, { 'Content-Type': 'text/html' }) + response.write(`${router.skelPage[0]}${router.clientJS}${router.skelPage[1]}`) + response.end() + //TODO: if route.socket == undefined: have server delay this send until host connects + // (this happens when a client connects to an active route with no currently-online host) + } + else { /* Client sent offer, waiting for answer */ + + } + } else { router.routes[routeName] = true