From: ken Date: Tue, 27 Jun 2017 03:22:00 +0000 (+0000) Subject: bugfixes X-Git-Url: https://www.kengrimes.com/gitweb/?p=henge%2Fkiak.git;a=commitdiff_plain;h=981f7c49567699addfa54664745b71fab2531b21 bugfixes --- diff --git a/main.js b/main.js index 6a631af..15bdde9 100644 --- a/main.js +++ b/main.js @@ -16,11 +16,10 @@ const getport = require('get-port') const mime = require('mime') const opts = require('./opts.js') -const skelPage = fs.readFileSync('./skel.html', { encoding: 'utf8' }).split('') -const clientJS = fs.readFileSync(opts['client-js']) -const hostJS = fs.readFileSync(opts['host-js']) -const routes = {} - +const skelPage = fs.readFileSync('./skel.html', { encoding: 'utf8' }).split('') +const clientJS = fs.readFileSync(opts['client-js']) +const hostJS = fs.readFileSync(opts['host-js']) +const routes = {} const httpsOpts = (opts['no-tls'] ? undefined : { @@ -32,45 +31,35 @@ const routeConnection = (request,response) => { const serveFile = (fPath) => { fs.readFile(fPath, { encoding: 'utf8' }, (err, data) => { if (err || data == undefined) { - response.write(404) + response.writeHead(404) response.end() } else { - response.write(200, mime.lookup(fPath)) + response.writeHead(200, { 'Content-Type': mime.lookup(fPath) }) response.write(data) response.end() } }) } const htArgv = request.url.slice(1).split("?") - console.log(htArgv) - let routePath = htArgv[0].split(path.sep) + let routePath = htArgv[0].split('/') let routeName = routePath[0] - if (routeName === '') - routeName = opts['index'] - if (routeName in opts['bindings']) { - const followBind = (dir, fPaths) => { - fs.readdir(dir, (err, file) => { + 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))) + console.log(localPath) + if (localPath.includes(opts['bindings'][routeName])) { + fs.readdir(localPath, (err, files) => { if (err) - serveFile(dir) - else if (fPaths.length == 0) - serveFile(`${dir}/index.html`) - else if (fPaths[0] !== '..' || path.normalize(`${dir}${path.sep}..`).includes(path.normalize(opts['bindings'][routeName]))) { - let nextFPath = fPaths.shift() - if (nextFPath in fPaths) - followBind(`${dir}${path.sep}${nextFPath}`, fPaths) - else { - response.writeHead(404) - response.end() - } - } - else { - console.log(`SEC: Ignored '..' in URL ${request.url}`) - console.log(request) - } + serveFile(localPath) + else + serveFile(`${localPath}/index.html`) }) } - followBind(opts['bindings'][routeName], htArgv[0].split(path.sep).slice(1)) + else { + console.log(`SEC: ${localPath} references files not in route`) + } } else if (routeName in routes) { const route = routes[routeName] diff --git a/opts.js b/opts.js index 25332b0..0a2c7ad 100644 --- a/opts.js +++ b/opts.js @@ -48,7 +48,7 @@ exports['defaults'] = { 'ca-cert': '../certs/cert.pem', 'ca-key': '../certs/key.pem', port: 2443, - index: './www/index.html', + index: 'www/index.html', bind: 'www:./www', electron: undefined, dedicated: undefined, @@ -97,7 +97,7 @@ exports['bind'].replace(/\s/g,'').split(',').forEach((kvp) => { 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] + exports['bindings'][kv[0]] = path.normalize(kv[1]) else console.log(`WARN: Invalid binding: ${kvp}`) } diff --git a/usage b/usage index 7aa10d9..3390c49 100644 --- a/usage +++ b/usage @@ -5,34 +5,41 @@ 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) + -K, --ca-key=path Accessible location of the CA Key (../certs/key.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) + -i, --index=path File serviced at the root domain (./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) + + Bindings of routes to paths. Any route bound to a + path will, instead of distributing the strapp client, + service the file at that path. If the file is a + directory, it will be mounted at that route. 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 -bjs:./js,html:./html,css:./css e.g. Route-based http hosting -buser1:/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. Create a dedicated electron listener