-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 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}`)
+ })
+ console.log('server handling request')
+ const serveFile = (fPath) => {
+ fs.readFile(fPath, { encoding: 'utf8' }, (err, data) => {
+ if (err || data == undefined) {
+ response.writeHead(404)
+ response.end()
+ }
+ else {
+ response.writeHead(200, { 'Content-Type': mime.lookup(fPath) })
+ response.write(data)
+ response.end()
+ }
+ })
+ }
+ const htArgv = request.url.slice(1).split("?")
+ let routePath = htArgv[0].split('/')
+ let routeName = routePath[0]
+ if (routeName === '' || routeName === 'index.html')
+ serveFile(opts['index'])
+ else if (routeName in opts['bindings']) {
+ let localPath = path.normalize(opts['bindings'][routeName].concat(path.sep + routePath.slice(1).join(path.sep)))
+ if (localPath.includes(opts['bindings'][routeName])) {
+ fs.readdir(localPath, (err, files) => {
+ if (err)
+ serveFile(localPath)
+ else
+ serveFile(`${localPath}/index.html`)
+ })
+ }
+ else {
+ console.log(`SEC: ${localPath} references files not in route`)
+ }
+ }
+ /* TODO: Handle reconnecting host */
+ else if (routeName in router.routes) {