+const opts = require('./opts.js')
+
+const router = {
+ skelPage: fs.readFileSync('./skel.html', { encoding: 'utf8' }).split('<!--STRAPP_SRC-->'),
+ clientJS: fs.readFileSync(opts['client-js']),
+ hostJS: fs.readFileSync(opts['host-js']),
+ routes: {},
+ httpsOpt: undefined,
+ httpd: undefined,
+ wsProtocol: opts['no-tls'] ? 'ws' : 'wss',
+ respond: (request,response) => {
+ console.log('server handling request')
+ 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()
+ }
+ })
+ }
+ 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']) {
+ 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`)
+ })
+ }
+ else {
+ console.log(`SEC: ${localPath} references files not in route`)
+ }
+ }
+ /* 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 (headerData === undefined) {
+ console.log('client init GET')
+ 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 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)
+ }