-app.on('ready', function() {
- console.log('create server')
- http.createServer(function(request, response) {
+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",new_route.host.slice(7)).replace("$PORT",new_route.port))
+ response.end()
+ })
+ }
+}).listen(2443)