X-Git-Url: https://git.kengrimes.com/?p=henge%2Fkiak.git;a=blobdiff_plain;f=main.js;h=321af8873c2ab3d5a2a683bac4ad8085be83b525;hp=f123a413fd73593d94bbbce8705586395cac3c90;hb=bf0ef83964741dcd64d27a8da651e65beba7e451;hpb=d33866cf8b1a188ad9ab23a934986bfd1b4726cb diff --git a/main.js b/main.js index f123a41..321af88 100644 --- a/main.js +++ b/main.js @@ -25,15 +25,6 @@ const router = { httpd: undefined, wsProtocol: opts['no-tls'] ? 'ws' : 'wss', respond: (request,response) => { - let body = [] - request.on('error', function(err) { - console.error(`error is ${err}`); - }).on('data', function(chunk) { - console.log(`chunk is ${chunk}`) - body.push(chunk); - }).on('end', function() { - console.log(`body is ${body}`) - }) console.log('server handling request') const serveFile = (fPath) => { fs.readFile(fPath, { encoding: 'utf8' }, (err, data) => { @@ -51,8 +42,10 @@ const router = { 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])) { @@ -69,11 +62,15 @@ const router = { } /* TODO: Handle reconnecting host */ else if (routeName in router.routes) { - const route = router.routes[routeName] + const clients = route['clients'] + const headerData = request.headers['x-strapp-type'] + + + /* Client is INIT GET */ - if (request.headers['x-strapp-type'] == undefined) { + if (headerData === undefined) { console.log('client init GET') response.writeHead(200, { 'Content-Type': 'text/html' }) response.write(`${router.skelPage[0]}${router.clientJS}${router.skelPage[1]}`) @@ -81,17 +78,39 @@ const router = { //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 */ - console.log(JSON.parse(request.headers['x-strapp-type'])) - route.socket.on('message', (hostResponse) => { - console.log(hostResponse) + else if (headerData.localeCompare('ice-candidate-request') === 0) { + console.log('Server: received ice-candidate-request from Client') + let pubKey = request.headers['x-client-pubkey'] + clients.set(pubKey, response) + pubKey = '{ "pubKey": "' + pubKey + '" }' + route.socket.send(pubKey) + } + else if (headerData.localeCompare('ice-candidate-submission') === 0) { + console.log('Server: recieved ice-candidate-submission from Client') + let data = [] + request.on('data', (chunk) => { + data.push(chunk) + }).on('end', () => { + console.log('Sending ice-candidate-submission to Host') + data = Buffer.concat(data).toString(); + clients.set(JSON.parse(data)['pubKey'], response) + route.socket.send(data) }) } + else if (headerData.localeCompare('client-sdp-offer') === 0){ /* Client sent offer, waiting for answer */ + console.log('Server: Sending client offer to host') + clients.set(JSON.parse(request.headers['x-client-offer'])['pubKey'], response) + route.socket.send(request.headers['x-client-offer']) + } else { + console.log('Unhandled stuff') + console.log(request.headers) + } } else { router.routes[routeName] = true const newRoute = {} + newRoute.clients = new Map([]) newRoute.host = request.headers['x-forwarded-for'] || request.connection.remoteAddress getport().then( (port) => { newRoute.port = port @@ -102,9 +121,53 @@ const router = { newRoute.httpd.listen(newRoute.port) newRoute.wsd = new ws.Server( { server: newRoute.httpd } ) newRoute.wsd.on('connection', (sock) => { + console.log(`${routeName} server has been established`) newRoute.socket = sock - sock.on('message', (msg) => { console.log(`[${newRoute.host}] ${msg}`) }) + + /* Handle all messages from host */ + sock.on('message', (hostMessage) => { + hostMessage = JSON.parse(hostMessage) + response = newRoute.clients.get(hostMessage['clientPubKey']) + + /* If the host response is a answer */ + if (hostMessage['cmd'].localeCompare('< sdp pubKey') === 0) { + console.log('Server: Sending host answer to client') + response.writeHead(200, { 'Content-Type': 'application/json' }) + response.write(JSON.stringify(hostMessage)) + response.end() + } + else if (hostMessage['cmd'].localeCompare('< ice pubKey') === 0){ + /* if the host response is an ice candidate */ + console.log('Server: Handling host ICE message') + let iceState = hostMessage['iceState'] + /* If there are any ice candidates, send them back */ + switch(iceState) { + case "a": + response.writeHead('200', {'x-strapp-type': 'ice-candidate-available'}) + response.write(JSON.stringify(hostMessage)) + response.end() + break + case "g": + console.log('Server: Host is still gathering candidates, keep trying') + response.writeHead('200', {'x-strapp-type': 'ice-state-gathering'}) + response.write(JSON.stringify(hostMessage)) + response.end() + break + case "c": + console.log('Server: Host has completed gathering candidates') + response.writeHead('200', {'x-strapp-type': 'ice-state-complete'}) + response.write(JSON.stringify(hostMessage)) + response.end() + break + default: + console.log('unhandled iceState from host') + break + } + } + + }) }) + console.log(`Listening for websocket ${newRoute.host} on port ${newRoute.port}`) router.routes[routeName] = newRoute }).then(() => { @@ -117,6 +180,7 @@ const router = { }) } + } }