X-Git-Url: https://git.kengrimes.com/?p=henge%2Fkiak.git;a=blobdiff_plain;f=main.js;fp=main.js;h=9187a77e78a48e2b7fc1297cb0028d834aedb1b9;hp=35cffef84fcc0969f8fd214ee7f137f565b88a12;hb=0ff5eabbfe914a66b7f6b2a7f22f6469895ab826;hpb=b5c8f98ab42b8e7128441e4a456dfd4fc02f0e86 diff --git a/main.js b/main.js index 35cffef..9187a77 100644 --- a/main.js +++ b/main.js @@ -42,6 +42,8 @@ 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']) else if (routeName in opts['bindings']) { @@ -60,11 +62,16 @@ 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'] + + if (route.socket === undefined ) { + console.log('route socket undefined') + } /* 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]}`) @@ -72,36 +79,93 @@ 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('Server: Sending client offer to host') - route.socket.send(request.headers['x-strapp-type']) - route.socket.on('message', (hostResponse) => { - console.log('Server: Sending host answer to client') - console.log(hostResponse) - response.writeHead(200, { 'Content-Type': 'application/json' }) - response.write(hostResponse) - response.end() + else if (headerData.localeCompare('ice-candidate-request') === 0){ + console.log('Server: received ice-candidate-request from Client') + let data = [] + request.on('data', (chunk) => { + data.push(chunk) + }).on('end', () => { + data = Buffer.concat(data).toString(); + console.log('Sending ice-candidate-request to Host' + data) + clients.set(data, response) + route.socket.send(data) + }) + } + 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) + 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 if (opts['no-tls']) - newRoute.httpd = http.createServer() + newRoute.httpd = http.createServer() else - newRoute.httpd = https.createServer(router.httpsOpts) + newRoute.httpd = https.createServer(router.httpsOpts) 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: Sending host ice candidate') + let iceCandidateAvailable = hostMessage['iceCandidateAvailable'] + /* If there are any ice candidates, send them back */ + if (iceCandidateAvailable) { + response.writeHead('200', {'x-strapp-type': 'ice-candidate-available'}) + response.write(JSON.stringify(hostMessage)) + response.end() + } + else { /* If not, srequest processed successfully, but there isnt anything yet*/ + console.log('Server: No ice candidate available for response') + response.writeHead('204', {'x-strapp-type': 'ice-candidate-unavailable'}) + response.end() + } + } + else { + console.log('unhandled message cmd from host') + } + + }) }) + console.log(`Listening for websocket ${newRoute.host} on port ${newRoute.port}`) router.routes[routeName] = newRoute }).then(() => { @@ -114,6 +178,7 @@ const router = { }) } + } }