From 6eae454bed7201c81be05c92ccc3b2f9040a1f16 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Wed, 25 Apr 2018 01:08:31 -0600 Subject: [PATCH] update remote access example --- examples/fshare.js | 60 ------------------------- examples/remote-access.js | 95 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 60 deletions(-) delete mode 100644 examples/fshare.js create mode 100644 examples/remote-access.js diff --git a/examples/fshare.js b/examples/fshare.js deleted file mode 100644 index 55fe818..0000000 --- a/examples/fshare.js +++ /dev/null @@ -1,60 +0,0 @@ -'use strict'; - -var express = require('express'); -var basicAuth = require('express-basic-auth'); -var crypto = require('crypto'); -var serveIndex = require('serve-index'); - -var rootIndex = serveIndex('/', { hidden: true, icons: true, view: 'details' }); -var rootFs = express.static('/', { dotfiles: 'allow', redirect: true, index: false }); - -var userIndex = serveIndex(require('os').homedir(), { hidden: true, icons: true, view: 'details' }); -var userFs = express.static(require('os').homedir(), { dotfiles: 'allow', redirect: true, index: false }); - -var app = express(); -var realm = 'Login Required'; -var secret = crypto.randomBytes(16).toString('hex'); - -var myAuth = basicAuth({ - users: { 'root': secret, 'user': secret } -, challenge: true -, realm: realm -, unauthorizedResponse: function (/*req*/) { - return 'Unauthorized Home'; - } -}); - -app.get('/', function (req, res) { - res.setHeader('Content-Type', 'text/html; charset=utf-8'); - res.end( - 'View Files' - + '  |  ' - + 'Logout' - ); -}); -app.use('/logout', function (req, res) { - res.setHeader('Content-Type', 'text/html; charset=utf-8'); - res.setHeader('WWW-Authenticate', 'Basic realm="' + realm + '"'); - res.status(401); - res.statusCode = 401; - //res.setHeader('Location', '/'); - res.end('Logged out   |   Home'); -}); -app.use('/browse', function (req, res, next) { - myAuth(req, res, next); -}); -app.use('/browse', function (req, res, next) { - if ('root' === req.auth.user) { rootFs(req, res, function () { rootIndex(req, res, next); }); return; } - if ('user' === req.auth.user) { userFs(req, res, function () { userIndex(req, res, next); }); return; } - res.end('Sad Panda'); -}); - -console.log(''); -console.log(''); -console.log('\t' + secret); -console.log(''); -console.log("\tShhhh... It's a secret to everybody!"); -console.log(''); -console.log(''); - -module.exports = app; diff --git a/examples/remote-access.js b/examples/remote-access.js new file mode 100644 index 0000000..02ad963 --- /dev/null +++ b/examples/remote-access.js @@ -0,0 +1,95 @@ +'use strict'; + +// +// WARNING: Not for noobs +// Try the simple example first +// + +// +// This demo is used with tunnel-server.js and tunnel-client.js +// + +var crypto = require('crypto'); + +var email = 'john.doe@gmail.com'; +var domains = [ 'example.com' ]; +var agreeLeTos = true; +var secret = crypto.randomBytes(16).toString('hex'); + +require('../').create({ + version: 'draft-11' + + // WARNING: This runs PRODUCTION by default. + // Uncomment staging instead if you're a first-timer. +, server: 'https://acme-v02.api.letsencrypt.org/directory' // production +//, server: 'https://acme-staging-v02.api.letsencrypt.org/directory' // staging + +, email: email +, agreeTos: agreeLeTos +, approveDomains: domains +, configDir: require('path').join(require('os').homedir(), 'acme', 'etc') +, app: remoteAccess(secret) +//, debug: true +}).listen(3000, 8443); + + +function remoteAccess(secret) { + var express = require('express'); + var basicAuth = require('express-basic-auth'); + var serveIndex = require('serve-index'); + + var rootIndex = serveIndex('/', { hidden: true, icons: true, view: 'details' }); + var rootFs = express.static('/', { dotfiles: 'allow', redirect: true, index: false }); + + var userIndex = serveIndex(require('os').homedir(), { hidden: true, icons: true, view: 'details' }); + var userFs = express.static(require('os').homedir(), { dotfiles: 'allow', redirect: true, index: false }); + + var app = express(); + var realm = 'Login Required'; + + var myAuth = basicAuth({ + users: { 'root': secret, 'user': secret } + , challenge: true + , realm: realm + , unauthorizedResponse: function (/*req*/) { + return 'Unauthorized Home'; + } + }); + + app.get('/', function (req, res) { + res.setHeader('Content-Type', 'text/html; charset=utf-8'); + res.end( + 'View Files' + + '  |  ' + + 'Logout' + ); + }); + app.use('/logout', function (req, res) { + res.setHeader('Content-Type', 'text/html; charset=utf-8'); + res.setHeader('WWW-Authenticate', 'Basic realm="' + realm + '"'); + res.statusCode = 401; + //res.setHeader('Location', '/'); + res.end('Logged out   |   Home'); + }); + app.use('/browse', myAuth); + app.use('/browse', function (req, res, next) { + if ('root' === req.auth.user) { rootFs(req, res, function () { rootIndex(req, res, next); }); return; } + if ('user' === req.auth.user) { userFs(req, res, function () { userIndex(req, res, next); }); return; } + res.end('Sad Panda'); + }); + + console.log(''); + console.log(''); + console.log('Usernames are\n'); + console.log('\troot'); + console.log('\tuser'); + console.log(''); + console.log('Password (for both) is\n'); + console.log('\t' + secret); + console.log(''); + console.log("Shhhh... It's a secret to everybody!"); + console.log(''); + console.log(''); + + return app; +}