-let https_routes = {}
-const https_router = https.createServer(https_router_opts, (request, response) => {
- let ht_argv = request.url.slice(1).split("?")
- console.log(ht_argv)
- if (ht_argv[0] in https_routes) {
- response.writeHead(200, { 'Content-Type': 'text/plain' })
- response.write('You are a remote client\r\n')
- let route = https_routes[ht_argv[0]]
- response.write('You should connect to ' + route.host + "\r\nOn port: " + route.port + "\r\n")
- response.end()
- }
- else if (ht_argv[0].indexOf(".") == -1) {
- https_routes[ht_argv[0]] = 'true'
- response.writeHead(200, { 'Content-Type': 'text/html' })
- let new_route = {}
- new_route.host = request.headers['x-forwarded-for'] || request.connection.remoteAddress
- getport().then( (port) => {
- new_route.port = port
- new_route.httpd = https.createServer(https_router_opts, (request, response) => {
- }).listen(port)
- new_route.ws = new ws.Server( { server: new_route.httpd } )
- new_route.ws.on('connection', (ws) => { console.log("socket connected"); ws.send("CONNECTED") } )
- new_route.ws.on('message', (msg) => { console.log("Received message" + msg) })
- console.log("Listening for websocket socket " + new_route.port + " on " + new_route.host)
- console.log(new_route)
- https_routes[ht_argv[0]] = new_route
- }).then(() => {
- let str = String(fs.readFileSync('remote-server.html'))
- response.write(str.replace("$HOST","wss://www.strapp.io").replace("$PORT",new_route.port))
- response.end()
+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) => {
+ 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}`)