From 37ee5f2c5517e7c4ad269db44f77f596acabed5a Mon Sep 17 00:00:00 2001 From: easongoodale Date: Sun, 9 Jul 2017 13:03:06 -0700 Subject: [PATCH 1/3] add optional basic http auth --- lib/auth.js | 40 ++++++++++++++++++++++++++++++++++++++++ lib/index.js | 19 ++++++++++++++++++- 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 lib/auth.js diff --git a/lib/auth.js b/lib/auth.js new file mode 100644 index 0000000..440bba1 --- /dev/null +++ b/lib/auth.js @@ -0,0 +1,40 @@ +module.exports = function (app, user, password) { + app.use(function* (next) { + const challenge = `Basic realm="Node File Manager"` + let authorization = this.header['authorization'] + if (authorization != null && authorization.slice(0, 6) === 'Basic ') { + authorization = new Buffer(authorization.slice(6), 'base64').toString('utf8') + const splitIndex = authorization.indexOf(':') + if (splitIndex > -1) { + const user = authorization.slice(0, splitIndex) + const password = authorization.slice(splitIndex + 1) + this.request.auth = { + user: user, + password: password + } + } + } + + yield next + + if (this.request.auth == null) { + this.status = 401 + this.response.set('WWW-Authenticate', challenge) + } + }) + + app.use(function* (next) { + if (!this.request.auth) { + this.body = 'Please log in.' + return // 401 response + } + + if (this.request.auth.user !== user || this.request.auth.password !== password) { + this.body = 'Invalid user.' + delete this.request.auth + return // 401 response + } + + yield next + }) +} \ No newline at end of file diff --git a/lib/index.js b/lib/index.js index 94c6a8f..001a4f1 100644 --- a/lib/index.js +++ b/lib/index.js @@ -10,7 +10,7 @@ var koaStatic = require('koa-static'); // Config var argv = require('optimist') .usage([ - 'USAGE: $0 [-p ] [-d ]'] + 'USAGE: $0 [-p ] [-d ] [--user ] [--password ]'] ) .option('port', { alias: 'p', @@ -29,6 +29,12 @@ var argv = require('optimist') alias: 'h', description: "Display This Help Message" }) + .option('user', { + description: "Username for basic http auth" + }) + .option('password', { + description: "Password for basic http auth" + }) .argv; if (argv.help) { @@ -41,6 +47,11 @@ if (argv.version) { process.exit(0); } +if ((argv.user && !argv.password) || (argv.password && !argv.user)) { + console.log('Both username and password are required to enable http auth') + process.exit(0); +} + global.C = { data: { root: argv.directory || path.dirname('.') @@ -58,6 +69,12 @@ var startServer = function (app, port) { }; var app = koa(); + +if (argv.user && argv.password) { + var enableAuth = require('./auth') + enableAuth(app, argv.user, argv.password) +} + app.proxy = true; app.use(Tools.handelError); app.use(Tools.realIp); From d375d30df0a297e120c9f0564de7148d9e300a3a Mon Sep 17 00:00:00 2001 From: Corning Sun Date: Thu, 22 Feb 2018 11:19:28 +0800 Subject: [PATCH 2/3] =?UTF-8?q?bugfix=EF=BC=9Aupload=20large=20file=20time?= =?UTF-8?q?out?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/public/js/app.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/public/js/app.js b/lib/public/js/app.js index 8875c6e..ad428f8 100644 --- a/lib/public/js/app.js +++ b/lib/public/js/app.js @@ -117,7 +117,7 @@ FMApp.controller('FileManagerCtr', ['$scope', '$http', '$location', url: url, params: params, data: data, - timeout: 10000 + timeout: 1000000 }; for (var k in config) { if (config.hasOwnProperty(k)) { From 909cbf2927e1a515ab31f66ecbc9e85551501da9 Mon Sep 17 00:00:00 2001 From: Corning Sun Date: Tue, 6 Mar 2018 17:37:22 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E5=90=88=E5=B9=B6=20=E7=99=BB=E9=99=86?= =?UTF-8?q?=E9=AA=8C=E8=AF=81=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 011cbcc..5c84802 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,7 @@ ```sh npm install -g node-file-manager node-file-manager -p 8080 -d /path/to/ + node-file-manager -p 8080 -d /path/to/ --user yourname --password yourpwd ``` Or @@ -15,7 +16,7 @@ Or cd node-file-manager npm i cd lib - node --harmony index.js -p 8080 -d /path/to + node --harmony index.js -p 8080 -d /path/to --user yourname --password yourpwd ``` We can run node-file-manager in terminal directly. We can specify prot add data root dir by `-p` and `-d`, default with 5000 and scripts directory.