strapp.io hostname hardcoded
[henge/kiak.git] / main.js
1 const fs = require('fs')
2 const ws = require('ws')
3 const https = require('https')
4 const http = require('http')
5 const getport = require('get-port')
6 const argv = require('minimist')(process.argv.slice(2))
7
8 const https_router_opts = {
9 key: fs.readFileSync('stunnel.key'),
10 cert: fs.readFileSync('stunnel.cert')
11 }
12
13 let https_routes = {}
14 const https_router = https.createServer(https_router_opts, (request, response) => {
15 let ht_argv = request.url.slice(1).split("?")
16 console.log(ht_argv)
17 if (ht_argv[0] in https_routes) {
18 response.writeHead(200, { 'Content-Type': 'text/plain' })
19 response.write('You are a remote client\r\n')
20 let route = https_routes[ht_argv[0]]
21 response.write('You should connect to ' + route.host + "\r\nOn port: " + route.port + "\r\n")
22 response.end()
23 }
24 else if (ht_argv[0].indexOf(".") == -1) {
25 https_routes[ht_argv[0]] = 'true'
26 response.writeHead(200, { 'Content-Type': 'text/html' })
27 let new_route = {}
28 new_route.host = request.headers['x-forwarded-for'] || request.connection.remoteAddress
29 getport().then( (port) => {
30 new_route.port = port
31 new_route.httpd = https.createServer(https_router_opts, (request, response) => {
32 }).listen(port)
33 new_route.ws = new ws.Server( { server: new_route.httpd } )
34 new_route.ws.on('connection', (ws) => { console.log("socket connected"); ws.send("CONNECTED") } )
35 new_route.ws.on('message', (msg) => { console.log("Received message" + msg) })
36 console.log("Listening for websocket socket " + new_route.port + " on " + new_route.host)
37 console.log(new_route)
38 https_routes[ht_argv[0]] = new_route
39 }).then(() => {
40 let str = String(fs.readFileSync('remote-server.html'))
41 response.write(str.replace("$HOST","wss://www.strapp.io").replace("$PORT",new_route.port))
42 response.end()
43 })
44 }
45 }).listen(2443)
46
47
48
49 if ("electron" in process.versions);
50