v0.0.1
authorken <ken@kengrimes.com>
Mon, 26 Jun 2017 21:51:18 +0000 (21:51 +0000)
committerken <ken@kengrimes.com>
Mon, 26 Jun 2017 21:51:18 +0000 (21:51 +0000)
.gitignore
host.js
main.js
opts.js [new file with mode: 0644]
package.json
skel.html
usage [new file with mode: 0644]

index 2bd1f87..60e2017 100644 (file)
@@ -1,2 +1,3 @@
 node_modules/
 .tern-project
+*~
diff --git a/host.js b/host.js
index 2700506..939c8f2 100644 (file)
--- a/host.js
+++ b/host.js
@@ -2,9 +2,9 @@ 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)
+  const wsock = new WebSocket("wss://" + window.location.host + ":" +  _strapp_port)
   wsock.onopen = () => {
-    console.log("Strapped to wss://" + _strapp_host + ":" +  _strapp_port)
+    console.log("Strapped to wss://" + window.location.host + ":" +  _strapp_port)
   }
   wsock.onmessage = (evt) => {
     console.log("Incoming connection from " + evt.data)
diff --git a/main.js b/main.js
index b24e9f3..c7d5bd7 100644 (file)
--- a/main.js
+++ b/main.js
@@ -1,81 +1,93 @@
+/**
+ * @file      Node entry and main driver
+ * @author    Jordan Lavatai, Ken Grimes
+ * @version   0.0.1
+ * @license   AGPL-3.0
+ * @copyright jk software 2017
+ * @summary   HTTP(S) Router that uses the first directory in the requested URL
+ *            as the route name
+ */
 const fs = require('fs')
 const ws = require('ws')
+const http = require('http')
 const https = require('https')
 const getport = require('get-port')
 const mime = require('mime')
+const opts = require('./opts.js')
 
-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()
+/* const routeConnection = (request,response) => {
+  const htArgv = request.url.slice(1).split("?")
+  let   routeName = /[^\/]*/.match(htArgv[0])[0]
+  if (routeName === '')
+    routeName = opts['index']
+  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()
   }
-})
-
-//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)
+  else if (routeName in routes) {
+    const route = routes[routeName]
+    response.writeHead(200, { 'Content-Type': 'text/html' })
+    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) => {
+      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(() => {
+      response.writeHead(200, { 'Content-Type': 'text/html' })
+      response.write(skelPage[0] + 'const _strapp_port = \'' + newRoute.port + '\'\n' + hostJS + skelPage[1])
+      response.end()
+    })
+  }
+}
 
-const routerOpts = {
-  key: fs.readFileSync(argv['ca-key']),
-  cert: fs.readFileSync(argv['ca-cert'])
+if (!opts['no-tls']) {
+  router.httpd = http.createServer(router.listener)
 }
-const skelPage = String(fs.readFileSync('skel.html')).split("<!--STRAPP-->")
-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)
-                   }
+else {
+  /* Setup TLS */
+  if (!fs.existsSync(opts['ca-key'])) {
+    console.log(`ERR: Key ${opts['ca-key']} inaccessible (required for tls)`)
+    process.exit()
+  }
+  if (!fs.existsSync(opts['ca-cert'])) {
+    console.log(`ERR: Cert ${opts['ca-cert']} inaccessible (required for tls)`)
+    process.exit()
+  }
+  router.opts = {
+    key: fs.readFileSync(opts['ca-key']),
+    cert: fs.readFileSync(opts['ca-cert'])
   }
-})
+}
 
+const skelPage = String(fs.readFileSync('skel.html')).split("<!--STRAPP_SRC-->")
+const clientJS = fs.readFileSync(opts['client-js'])
+const hostJS   = fs.readFileSync(opts['host-js'])
+const routes   = {}
+/*
 const router = https.createServer(routerOpts, (request, response) => {
   const htArgv = request.url.slice(1).split("?")
   let   routeName = htArgv[0].split('/')[0]
@@ -119,11 +131,11 @@ const router = https.createServer(routerOpts, (request, response) => {
       routes[htArgv[0]] = newRoute
     }).then(() => {
       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.write(skelPage[0] + 'const _strapp_port = \'' + newRoute.port + '\'\n' + hostJS + skelPage[1])
       response.end()
     })
   }
 }).listen(argv['port'])
-
+*/
 //TODO: if ("electron" in process.versions) open a local renderwindow, and route to it
 
diff --git a/opts.js b/opts.js
new file mode 100644 (file)
index 0000000..e97d84e
--- /dev/null
+++ b/opts.js
@@ -0,0 +1,108 @@
+/**
+ * @file      Establishes runtime options from args and config files
+ * @author    Ken Grimes
+ * @version   0.0.1
+ * @license   AGPL-3.0
+ * @copyright jk software 2017
+ */
+const fs = require('fs')
+const path = require('path')
+
+/**
+ * @summary Parse arguments with minimist
+ */
+exports = require('minimist')(process.argv.slice(2), {
+  string:  [ 'config', 'client-js', 'host-js', 'ca-cert', 'ca-key',
+            'port', 'index', 'bind', 'electron', 'dedicated'
+          ],
+  boolean: [ 'no-tls', 'legacy-socket' ],
+  alias:   { c: 'config',
+            j: 'client-js',
+            J: 'host-js',
+            T: 'no-tls',
+            C: 'ca-cert',
+            K: 'ca-key',
+            p: 'port',
+            i: 'index',
+            b: 'bind',
+            e: 'electron',
+            d: 'dedicated'
+          },
+  stopEarly: true,
+  unknown: (opt) => {
+    console.log('Unknown operator: ' + opt)
+    console.log(process.argv.join(' '))
+    console.log(fs.readFileSync('./usage'))
+    process.exit()
+  }
+})
+
+/**
+ * @summary Defaults
+ */
+exports['defaults'] = {
+  config:      '/etc/strapp.conf:~/.strapp/strapp.conf:./strapp.conf',
+  'client-js': './client.js',
+  'host-js':   './host.js',
+  tls:         true,
+  'ca-cert':   '../certs/cert.pem',
+  'ca-key':    '../certs/key.pem',
+  port:        2443,
+  index:       './www/index.html',
+  bind:        'www:./www',
+  electron:    undefined,
+  dedicated:   undefined,
+  'legacy-socket': false
+}
+
+exports = Object.assign({}, exports['defaults'], exports)
+
+/**
+ * @summary Parse config files
+ */
+exports['conf'] = {}
+exports['config'].split(':').forEach((fileName) => {
+  let filePath = path.resolve(fileName)
+  if (fs.existsSync(filePath)){
+    let lineNo = 0;
+    String(fs.readFileSync(path.resolve(fileName))).split('\n').forEach((line) => {
+      lineNo++
+      /* Skip comments */
+      if (!line.match(/[\s]*#/)) {
+       let kv = line.replace(/#.*/g,'').replace(/\s/g,'').split('=')
+       if (kv.length == 2 && kv[0] in exports['defaults']) {
+         /* Multiple "bind=..." lines in are additive */
+         if (kv[0] === 'bind' && 'bind' in exports['conf']) {
+             kv[1] = exports['conf']['bind'] + ',' + kv[1]
+         }
+         exports['conf'][kv[0]] = kv[1]
+       }
+       else
+         console.log(`WARN: Bad option ${line} in ${fileName}:${lineNo}`)
+      }
+    })
+  }
+  else if (exports['config'] !== exports['defaults']['config'])
+    console.log(`WARN: opt '-c${exports['config']}', ${fileName} inaccessible`)
+})
+
+exports = Object.assign({}, exports['defaults'], exports['conf'], exports)
+
+/**
+ * @summary Parse bindings
+ */
+exports['bindings'] = {}
+exports['bind'].replace(/\s/g,'').split(',').forEach((kvp) => {
+  let kv = kvp.split(':')
+  kv[1] = path.resolve(kv[1])
+  if (fs.existsSync(kv[1])) {
+    if (kv.length == 2 && /^[-_.A-Za-z0-9]/g.test(kv[0]))
+      exports['bindings'][kv[0]] = kv[1]
+    else
+      console.log(`WARN: Invalid binding: ${kvp}`)
+  }
+  else
+    console.log(`WARN: Binding not made, directory ${kvp} inaccessible`)
+})
+
+module.exports = exports
index b041e15..cb89904 100644 (file)
@@ -21,7 +21,7 @@
     "Jordan Lavatai",
     "Ken Grimes"
   ],
-  "license": "GPL-3.0",
+  "license": "AGPL-3.0",
   "devDependencies": {},
   "dependencies": {
     "electron": "^1.6.11",
index 0342496..967e8c1 100644 (file)
--- a/skel.html
+++ b/skel.html
@@ -3,8 +3,10 @@
   <head>
     <meta charset="utf-8" />
     <title>Strapp.io</title>
-    <script type="text/javascript">
-    <!--STRAPP-->
-    </script>
+    <!--STRAPP_FAVICO-->
+    <link rel=icon href="./www/favicon-96x96.png">    
+    <!--STRAPP_MANIFEST-->
+    <link rel=manifest href="./www/manifest.json">
+    <script type="text/javascript" <!--STRAPP_SRC-->></script>
   </head>
 </html>
diff --git a/usage b/usage
new file mode 100644 (file)
index 0000000..3d3eeee
--- /dev/null
+++ b/usage
@@ -0,0 +1,50 @@
+Usage: strapp [OPTION]... 
+Route https connections from a hardware port to a remote host, and initiate
+peer-to-peer connection with clients.  A 'route' is established from the first
+directory specified in the requested URL.
+
+CONFIG
+  -c, --config=path[:path]...
+                          Configuration files to use.  Each file in the sequence
+                          can override any of the previous file's settings.
+                          (/etc/strapp.conf:~/.strapp/strapp.conf:./strapp.conf)
+                          - config settings are overridden by command line opts
+                            except where noted
+  -j, --client-js=path    Path to the client Strapp code (./client.js)
+  -J, --host-js=path      Path to the host Strapp code (./host.js)
+  -T, --no-tls=bool       Don't use HTTPS and WSS protocols (false)
+                          - makes 'ca-cert' and 'ca-key' unnecessary
+  -C, --ca-cert=path      Accessible location of the CA Cert (../certs/cert.pem)
+  -K, --ca-key=path       Accessible location of the CA Key (../certs/cert.pem)
+  -p, --port=number       The local port to bind HTTPS listener to (2443)
+  -i, --index=path        File serviced at the root domain (./www/index.html)
+
+ROUTING
+  -b, --bind=[string:path[,string:path]]...
+                          Bindings of routes to directories.  Any route bound to
+                          a path will, instead of distributing the strapp client,
+                          service files in that path.  Multiple bindings may be
+                          established by separating the key:value pairs with ','
+                          commas. (www:./www)
+                          e.g. Service a typical frontpage
+                            -i./html/index.html -b js:./js,html:./html,css:./css
+                          e.g. Route-based http hosting
+                            -b user1:/home/user1/www,user2:/home/user2/www
+  -e, --electron=string   Specify a route name for the local electron render
+                          window which will be launched on execution (nil)
+                          - enables optional electron dependency
+  -d, --dedicated=string  Route all incoming connections to this route (nil)
+                          - can be used in conjunction with '-e' for single-user
+                          e.g. Set the electron window to use route 'default',
+                               Set the dedicated route to 'default'
+                          $ node strapp.js -e default -d default
+                            - All routes point to 'default' which is bound to
+                              the local electron window.  No other hosts may
+                              be established, so no remote hosts may exist
+
+COMPATIBILITY
+  --legacy-socket=bool    Use Socket.io compatibility layer to enable 
+                          long-polling and AJAX fallbacks (false)
+                          - enables optional socket.io dependency
+
+(c)2017 jk software