From: ken Date: Sun, 25 Jun 2017 06:48:17 +0000 (+0000) Subject: improved structure/functionality X-Git-Url: https://git.kengrimes.com/?p=henge%2Fkiak.git;a=commitdiff_plain;h=69adc9a3512752d627e126dde85c859fcba9fb63 improved structure/functionality --- diff --git a/client.js b/client.js new file mode 100644 index 0000000..3f0aeec --- /dev/null +++ b/client.js @@ -0,0 +1,5 @@ +const body = document.createElement('body') +const root = document.createElement('div') +document.title = "Strapp.io Client" +body.appendChild(root) +document.body = body diff --git a/host.js b/host.js new file mode 100644 index 0000000..2700506 --- /dev/null +++ b/host.js @@ -0,0 +1,17 @@ +const body = document.createElement('body') +const root = document.createElement('div') +document.title = "Strapp.io Host" +if ("WebSocket" in window) { + const wsock = new WebSocket("wss://" + _strapp_host + ":" + _strapp_port) + wsock.onopen = () => { + console.log("Strapped to wss://" + _strapp_host + ":" + _strapp_port) + } + wsock.onmessage = (evt) => { + console.log("Incoming connection from " + evt.data) + console.log("TODO: Open a socket to this client") + } +} else { + root.appendChild(document.createTextNode("Your browser does not support Strapp")) +} +body.appendChild(root) +document.body = body diff --git a/main.js b/main.js index 2db2240..b24e9f3 100644 --- a/main.js +++ b/main.js @@ -1,50 +1,129 @@ const fs = require('fs') const ws = require('ws') const https = require('https') -const http = require('http') const getport = require('get-port') -const argv = require('minimist')(process.argv.slice(2)) +const mime = require('mime') -const https_router_opts = { - key: fs.readFileSync('stunnel.key'), - cert: fs.readFileSync('stunnel.cert') +const argv = require('minimist')(process.argv.slice(2), { + string: [ 'ca-cert', 'ca-key', 'config', 'client-js', 'host-js', 'electron', 'port' ], + boolean: [ 'remote-host' ], + alias: { c: 'config', + j: 'client-js', + J: 'host-js', + C: 'ca-cert', + K: 'ca-key', + e: 'electron', + r: 'remote-host', + p: 'port' + }, + default: { config: undefined, + 'client-js': 'client.js', + 'host-js': 'host.js', + 'ca-cert': 'stunnel.cert', + 'ca-key': 'stunnel.key', + 'remote-host': true, + 'port': 2443 + }, + stopEarly: true, + unknown: (opt) => { + console.log(process.argv.join(" ") + '\nUnknown operator: ' + opt + ` +Usage: strapp [OPTION]... +Route https connections from a hardware port to a remote host, and initiate +peer-to-peer connection with clients. + +CONFIG + -c, --config=path Configuration file to use (/etc/strapp.conf) + - overridden by command line opts + -j, --client-js=path Path to the client Strapp code (./strapp-client.js) + -J, --host-js=path Path to the host Strapp code (./strapp-host.js) + -C, --ca-cert=path Accessible location of the CA Cert (./stunnel.cert) + -K, --ca-key=path Accessible location of the CA Key (./stunnel.key) + -p, --port=number The local port to bind HTTPS listener to (2443) + +ROUTING + -e, --electron=route Route to the local electron user (nil) + - enables optional electron dependency + -d, --dedicated=route Route all incoming connections to this route (nil) + - used in conjunction with '-e=my_route' + +(c)2017 jk software +`) + process.exit() + } +}) + +//TODO: if (argv['config'] !== undefined), read and apply to argv without overwrite +if (argv['www-path'] == undefined) + argv['www-path'] = 'www' +else if (String(argv['www-path']).endsWith('/')) + argv['www-path'] = argv['www-path'].slice(0,-1) + +const routerOpts = { + key: fs.readFileSync(argv['ca-key']), + cert: fs.readFileSync(argv['ca-cert']) } +const skelPage = String(fs.readFileSync('skel.html')).split("") +const clientJS = fs.readFileSync(argv['client-js']) +const hostJS = fs.readFileSync(argv['host-js']) +const routes = {} +const fileBuf = {} +let fbSize = 0 +fs.readdirSync(argv['www-path']).forEach((file) => { + if (fbSize++ < 50) { + fileBuf[file] = { mime: mime.lookup(argv['www-path'] + '/' + file), + data: fs.readFileSync(argv['www-path'] + '/' + file) + } + } +}) -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") +const router = https.createServer(routerOpts, (request, response) => { + const htArgv = request.url.slice(1).split("?") + let routeName = htArgv[0].split('/')[0] + if (routeName === '') + routeName = 'index.html' + if (routeName.indexOf('.') != -1) { + if (routeName in fileBuf) { + response.writeHead(200, { 'Content-Type': fileBuf[routeName].mime }) + response.write(fileBuf[routeName].data) + } + else { + if ('404.html' in fileBuf) { + response.writeHead(404, fileBuf['404.html'].mime) + response.write(fileBuf['404.html'].data) + } + else + response.writeHead(404) + } response.end() } - else if (ht_argv[0].indexOf(".") == -1) { - https_routes[ht_argv[0]] = 'true' + else if (routeName in routes) { + const route = routes[routeName] response.writeHead(200, { 'Content-Type': 'text/html' }) - let new_route = {} - new_route.host = request.headers['x-forwarded-for'] || request.connection.remoteAddress + response.write(skelPage[0] + clientJS + skelPage[1]) + response.end() + route.socket.send(request.headers['x-forwarded-for'] || request.connection.remoteAddress) + } + else { + routes[htArgv[0]] = true + const newRoute = {} + newRoute.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 + newRoute.port = port + newRoute.httpd = https.createServer(routerOpts, (request, response) => { + }).listen(newRoute.port) + + newRoute.ws = new ws.Server( { server: newRoute.httpd } ) + newRoute.ws.on('connection', (ws) => { console.log("socket connected"); newRoute.socket = ws; ws.send("CONNECTED") } ) + newRoute.ws.on('message', (msg) => { console.log("Received message" + msg) }) + console.log("Listening for websocket " + newRoute.host + " on port " + newRoute.port) + routes[htArgv[0]] = newRoute }).then(() => { - let str = String(fs.readFileSync('remote-server.html')) - response.write(str.replace("$HOST","www.strapp.io").replace("$PORT",new_route.port)) + response.writeHead(200, { 'Content-Type': 'text/html' }) + response.write(skelPage[0] + 'const _strapp_host = \'www.strapp.io\'\n\tconst _strapp_port = \'' + newRoute.port + '\'\n' + hostJS + skelPage[1]) response.end() }) } -}).listen(2443) - - +}).listen(argv['port']) -if ("electron" in process.versions); +//TODO: if ("electron" in process.versions) open a local renderwindow, and route to it diff --git a/package.json b/package.json index b03b4c4..b041e15 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ "dependencies": { "electron": "^1.6.11", "get-port": "^3.1.0", + "mime": "^1.3.6", "minimist": "^1.2.0", "ws": "^3.0.0" } diff --git a/remote-server.html b/remote-server.html index 3d4ee3c..2b65a20 100644 --- a/remote-server.html +++ b/remote-server.html @@ -1,28 +1,24 @@ - + strappDiv.innerHTML = "Sent test" + } + wsock.onmessage = (evt) => { + strappDiv.innerHTML = "Got Response
" + evt.data + } + } else { + strappDiv.innerHTML = "Your browser does not support Strapp" + } + - -
- Unterminated -
- diff --git a/skel.html b/skel.html new file mode 100644 index 0000000..0342496 --- /dev/null +++ b/skel.html @@ -0,0 +1,10 @@ + + + + + Strapp.io + + + diff --git a/stunnel.cert b/stunnel.cert new file mode 100644 index 0000000..6ae9532 --- /dev/null +++ b/stunnel.cert @@ -0,0 +1,29 @@ +-----BEGIN CERTIFICATE----- +MIIE/TCCA+WgAwIBAgISA8tDqABqbcv2/gK8/q12IhazMA0GCSqGSIb3DQEBCwUA +MEoxCzAJBgNVBAYTAlVTMRYwFAYDVQQKEw1MZXQncyBFbmNyeXB0MSMwIQYDVQQD +ExpMZXQncyBFbmNyeXB0IEF1dGhvcml0eSBYMzAeFw0xNzA2MjMwMjI1MDBaFw0x +NzA5MjEwMjI1MDBaMBgxFjAUBgNVBAMTDXd3dy5zdHJhcHAuaW8wggEiMA0GCSqG +SIb3DQEBAQUAA4IBDwAwggEKAoIBAQCh1tSTfA1JAxsNJTwhCgwPMeY9Lib0PjiO +/EOy0optOeDqOJwB1C0HRnTg4NSoj9q+B0joKuvjvLhYBob7sRdjqiDPsq78aESC +ENP5rz1+O73wEtcv3G9z5IvCcnicYrWdRYipBgFiV6rxkzwZFBsxxy6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hD5rbSJGvkbXOI9nyTBcXF3t0rsKS+GJACaZkA0w9e+kADhJ0eGrbf30KT +l5D9B7qX2LJaM+zcDY8gj3+tDj2hBK07JMekjC1C0y+Tq0WnvZZ3dw2YD4Jd8Ofc +OmR20LGv4xz4wCJ7E3rqMqpUVgAfyM7yiQrnLcggIiMQRqACqJk4yn0+agsM6P2n +Ru+x1nbXw87XsGwno5dYnqlonbUMrDBOFKX/jtoXjy+/ +-----END CERTIFICATE----- diff --git a/stunnel.key b/stunnel.key new file mode 100644 index 0000000..99661cb --- /dev/null +++ b/stunnel.key @@ -0,0 +1,28 @@ +-----BEGIN PRIVATE KEY----- +MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCh1tSTfA1JAxsN +JTwhCgwPMeY9Lib0PjiO/EOy0optOeDqOJwB1C0HRnTg4NSoj9q+B0joKuvjvLhY +Bob7sRdjqiDPsq78aESCENP5rz1+O73wEtcv3G9z5IvCcnicYrWdRYipBgFiV6rx +kzwZFBsxxy6W47/jjadXG1nhWTKy1Vkd2BjnB/B138qhJe0z45pRCvPVIaPlalQv +SEiqrLhIOfmc1apFjBiscjI57bQ6AswgoELY5SP+edLVy3ZM/XAS+6aPVJumzQ+X +Cy3vCvOh0aJfoDa0EwneDeMcTU9G3YicPOHwR5HT3Cd7OK+o7y4Aib0LJn1B1wbE +wE8btkTRAgMBAAECggEAZYNvi6NAYg/EpBY7BS5cKhvOo4mTfZiZuXaasExH8ANi +BQjFMpB4Phv/rB7axXCWcKP5I0fo9sylhPFzMeT/GtDYC++0uzZb4onTrMYy+D9Z +AIGQ4MCQWE1/LNTIuq9f4+p9sifGLn13CFimVcS62hpqKtNoxb9Qu964HhXpKyqQ +MeSZhJsN9/ixdAVW/JWMEcOZLtttaznJ/JPZu5cvt0cis86Iir4I89fowdFO3V+h +WI03Ax/aafcthS1zOg4QLl/mCL69yURQPC1dYhDZ7pH/ZEqgNxp1B3+Sio37fe2A +EJFx8kyb4+Jku7KkIYw8L+OelYxLlwEYjHkuQ+puwQKBgQDWHSA7COEvJxRScq0G +07TaCmHMvhG+GtCDSLiST8pip64RDN/u3OpMn2MlLYQMSQB4XGdklBfb3IesbpLl +DWIdMxQ4H1GFgfSQYiN5WzF2U6A1OG7IaWGc17hrB6hcFKrW6K8hstaboMEgtUnV +87N9T4sr4G7B38bxI2f+VYi+jQKBgQDBf8d2yGBhfDBruNrTx+eN4ywccC+uWaOS +vEcV1qf5hQDS2PA/7AQkxMofDDNZFak7baTbFAKhH+sUKriTyth2vgakHVS5xxJH +WvuGJhttHheh35B1lGKVvYsJqpMG5oPpySqLLu1/gBzw0H+tnbdCA/iF//qodLD0 +vUIg6ksAVQKBgGZjz0Hr4SOXYJR82lllbBrI1DQAwKNUV1owsHLnTTUYUpk8uumT +I0g5AESLG5Z0YpEsPxVN7IrrTsYaqox2dfDYuFaOQ4HAv588DxK9lU2sd/R7ZB7a +ph9G2Z31L4G5MGlNJFLNHuuVWEW36/Mwcyj9nOQCk3SK+qvd7qElbhsRAoGAXlzK +EQs2tVOy6VWEobLjITaf1F6BYi0kr/JRWD0OPYi3FhvlTcbzXlr5lpqEj/UYSnr1 +4kCn55uBMJyE1s4dMgsJgg8ruk1yhInCD3GV/mimbbJw6GYsjYmZvGUKvlq8Cc6o +iKeh+oOnmWiUWz6GmBMHp5Fl07DUewWwsgyP9uECgYEAsdIajEA77zX0Zi/58DwC +5OBpM5UhLBqjYPIFZgE+iowMRc79A484IBxVXnoYdGeDE1qvu4RTCggpCSmVuOFM +kd+HY6zQNv1cVn/VXyJ2p1OK3J2gxY4nSrmy7zlIj3e7AOSswK0arenkuUlpBo7o +G4kQUXI2Zjzw98EmRT60Yrg= +-----END PRIVATE KEY----- diff --git a/www/android-icon-144x144.png b/www/android-icon-144x144.png new file mode 100644 index 0000000..bcc35dc Binary files /dev/null and b/www/android-icon-144x144.png differ diff --git a/www/android-icon-192x192.png b/www/android-icon-192x192.png new file mode 100644 index 0000000..9084c43 Binary files /dev/null and b/www/android-icon-192x192.png differ diff --git a/www/android-icon-36x36.png b/www/android-icon-36x36.png new file mode 100644 index 0000000..5ac6cb4 Binary files /dev/null and b/www/android-icon-36x36.png differ diff --git a/www/android-icon-48x48.png b/www/android-icon-48x48.png new file mode 100644 index 0000000..7dcb424 Binary files /dev/null and b/www/android-icon-48x48.png differ diff --git a/www/android-icon-72x72.png b/www/android-icon-72x72.png new file mode 100644 index 0000000..98b51d1 Binary files /dev/null and b/www/android-icon-72x72.png differ diff --git a/www/android-icon-96x96.png b/www/android-icon-96x96.png new file mode 100644 index 0000000..94175e2 Binary files /dev/null and b/www/android-icon-96x96.png differ diff --git a/www/apple-icon-114x114.png b/www/apple-icon-114x114.png new file mode 100644 index 0000000..2ffae55 Binary files /dev/null and b/www/apple-icon-114x114.png differ diff --git a/www/apple-icon-120x120.png b/www/apple-icon-120x120.png new file mode 100644 index 0000000..47121e6 Binary files /dev/null and b/www/apple-icon-120x120.png differ diff --git a/www/apple-icon-144x144.png b/www/apple-icon-144x144.png new file mode 100644 index 0000000..bcc35dc Binary files /dev/null and b/www/apple-icon-144x144.png differ diff --git a/www/apple-icon-152x152.png b/www/apple-icon-152x152.png new file mode 100644 index 0000000..9355b93 Binary files /dev/null and b/www/apple-icon-152x152.png differ diff --git a/www/apple-icon-180x180.png b/www/apple-icon-180x180.png new file mode 100644 index 0000000..df3bf03 Binary files /dev/null and b/www/apple-icon-180x180.png differ diff --git a/www/apple-icon-57x57.png b/www/apple-icon-57x57.png new file mode 100644 index 0000000..971538b Binary files /dev/null and b/www/apple-icon-57x57.png differ diff --git a/www/apple-icon-60x60.png b/www/apple-icon-60x60.png new file mode 100644 index 0000000..051fc8e Binary files /dev/null and b/www/apple-icon-60x60.png differ diff --git a/www/apple-icon-72x72.png b/www/apple-icon-72x72.png new file mode 100644 index 0000000..98b51d1 Binary files /dev/null and b/www/apple-icon-72x72.png differ diff --git a/www/apple-icon-76x76.png b/www/apple-icon-76x76.png new file mode 100644 index 0000000..52ca277 Binary files /dev/null and b/www/apple-icon-76x76.png differ diff --git a/www/apple-icon-precomposed.png b/www/apple-icon-precomposed.png new file mode 100644 index 0000000..dbdb0bb Binary files /dev/null and b/www/apple-icon-precomposed.png differ diff --git a/www/apple-icon.png b/www/apple-icon.png new file mode 100644 index 0000000..dbdb0bb Binary files /dev/null and b/www/apple-icon.png differ diff --git a/www/browserconfig.xml b/www/browserconfig.xml new file mode 100644 index 0000000..c554148 --- /dev/null +++ b/www/browserconfig.xml @@ -0,0 +1,2 @@ + +#ffffff \ No newline at end of file diff --git a/www/favicon-16x16.png b/www/favicon-16x16.png new file mode 100644 index 0000000..0c0b724 Binary files /dev/null and b/www/favicon-16x16.png differ diff --git a/www/favicon-32x32.png b/www/favicon-32x32.png new file mode 100644 index 0000000..8cff0a4 Binary files /dev/null and b/www/favicon-32x32.png differ diff --git a/www/favicon-96x96.png b/www/favicon-96x96.png new file mode 100644 index 0000000..94175e2 Binary files /dev/null and b/www/favicon-96x96.png differ diff --git a/www/favicon.ico b/www/favicon.ico new file mode 100644 index 0000000..7f7bb17 Binary files /dev/null and b/www/favicon.ico differ diff --git a/www/manifest.json b/www/manifest.json new file mode 100644 index 0000000..013d4a6 --- /dev/null +++ b/www/manifest.json @@ -0,0 +1,41 @@ +{ + "name": "App", + "icons": [ + { + "src": "\/android-icon-36x36.png", + "sizes": "36x36", + "type": "image\/png", + "density": "0.75" + }, + { + "src": "\/android-icon-48x48.png", + "sizes": "48x48", + "type": "image\/png", + "density": "1.0" + }, + { + "src": "\/android-icon-72x72.png", + "sizes": "72x72", + "type": "image\/png", + "density": "1.5" + }, + { + "src": "\/android-icon-96x96.png", + "sizes": "96x96", + "type": "image\/png", + "density": "2.0" + }, + { + "src": "\/android-icon-144x144.png", + "sizes": "144x144", + "type": "image\/png", + "density": "3.0" + }, + { + "src": "\/android-icon-192x192.png", + "sizes": "192x192", + "type": "image\/png", + "density": "4.0" + } + ] +} \ No newline at end of file diff --git a/www/ms-icon-144x144.png b/www/ms-icon-144x144.png new file mode 100644 index 0000000..bcc35dc Binary files /dev/null and b/www/ms-icon-144x144.png differ diff --git a/www/ms-icon-150x150.png b/www/ms-icon-150x150.png new file mode 100644 index 0000000..c640ed3 Binary files /dev/null and b/www/ms-icon-150x150.png differ diff --git a/www/ms-icon-310x310.png b/www/ms-icon-310x310.png new file mode 100644 index 0000000..70ba1ef Binary files /dev/null and b/www/ms-icon-310x310.png differ diff --git a/www/ms-icon-70x70.png b/www/ms-icon-70x70.png new file mode 100644 index 0000000..36f8176 Binary files /dev/null and b/www/ms-icon-70x70.png differ