From 0f9f4986071848bc1dbd4bbdeedf64942a16c3e1 Mon Sep 17 00:00:00 2001 From: WEBER Antoine Date: Tue, 29 Sep 2015 23:04:17 +0200 Subject: [PATCH] ajout d'une protection par mot de passe + favicon + theme configurable --- app.js | 35 ++++++++++++++++++++++++++++------- config.js_example | 5 +++-- package.json | 5 +++-- routes/channel.js | 2 +- routes/file.js | 3 +-- routes/index.js | 2 +- routes/movie.js | 3 +-- routes/show.js | 2 +- routes/show_list.js | 2 +- routes/users.js | 1 + views/layout.jade | 2 +- 11 files changed, 42 insertions(+), 20 deletions(-) diff --git a/app.js b/app.js index 5aa5939..28cfb98 100644 --- a/app.js +++ b/app.js @@ -4,11 +4,7 @@ var favicon = require('serve-favicon'); var logger = require('morgan'); var cookieParser = require('cookie-parser'); var bodyParser = require('body-parser'); - -/*var routes = require('./routes/index'); -var users = require('./routes/users'); -var file = require('./routes/file'); -var channel = require('./routes/channel');*/ +var config = require('./config'); var app = express(); @@ -16,14 +12,39 @@ var app = express(); app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'jade'); -// uncomment after placing your favicon in /public -//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))); +//protection par mot de passe +if( typeof config.auth_user !== 'undefined' && typeof config.auth_password !== 'undefined' ){ + console.log("SAFE MODE: votre serveur est protégé par mot de passe."); + var basicAuth = require('basic-auth'); + app.use(function(req, res, next) { + var user = basicAuth(req); + if (!user || user.name !== config.auth_user || user.pass !== config.auth_password) { + res.set('WWW-Authenticate', 'Basic realm=Authorization Required'); + return res.sendStatus(401); + } + else{ + next(); + } + }); +} +else{ + console.log("UNSAFE MODE: configurez un auth_user et auth_password dans le fichier config.js"); +} + +//envoyer config a tout le monde +app.use(function(req, res, next) { + res.locals.config = config; + next(); +}); + +app.use(favicon(path.join(__dirname, 'public', 'iconarchive_plex.ico'))); app.use(logger('dev')); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: false })); app.use(cookieParser()); app.use(express.static(path.join(__dirname, 'public'))); + app.use('/', require('./routes/index')); app.use('/users', require('./routes/users')); app.use('/file', require('./routes/file')); diff --git a/config.js_example b/config.js_example index b4a443c..4942682 100644 --- a/config.js_example +++ b/config.js_example @@ -17,9 +17,10 @@ Docker module.exports = { database: "/home/antoine/plex-config/Plex Media Server/Plug-in Support/Databases/com.plexapp.plugins.library.db", - + theme: "//cdnjs.cloudflare.com/ajax/libs/bootswatch/3.3.5/darkly/bootstrap.css", presentation: "

Bienvenue sur mon serveur

Merci de respecter mon upload...

", - + //auth_user: "monUser", + //auth_password: "monPassword", init_db: function(){ var sqlite3 = require('sqlite3').verbose(); diff --git a/package.json b/package.json index 787c13c..7540096 100644 --- a/package.json +++ b/package.json @@ -6,14 +6,15 @@ "start": "node ./bin/www" }, "dependencies": { + "basic-auth": "^1.0.3", "body-parser": "~1.13.2", + "cluster": "~0.7.7", "cookie-parser": "~1.3.5", "debug": "~2.2.0", "express": "~4.13.1", "jade": "~1.11.0", "morgan": "~1.6.1", "serve-favicon": "~2.3.0", - "sqlite3": "~3", - "cluster":"~0.7.7" + "sqlite3": "~3" } } diff --git a/routes/channel.js b/routes/channel.js index 9ca0cb7..dd8675c 100644 --- a/routes/channel.js +++ b/routes/channel.js @@ -3,7 +3,7 @@ var router = express.Router(); /* GET home page. */ router.get('/:id', function(req, res, next) { - var config = require('../config'); + var config = res.locals.config; var db = config.init_db(); var data = []; diff --git a/routes/file.js b/routes/file.js index dfab0f8..345e167 100644 --- a/routes/file.js +++ b/routes/file.js @@ -3,8 +3,7 @@ var router = express.Router(); /* GET users listing. */ router.get('/:id/:filename', function (req, res, next) { - - var config = require('../config'); + var config = res.locals.config; var db = config.init_db(); //db.run("CREATE TABLE if not exists user_info (info TEXT)"); diff --git a/routes/index.js b/routes/index.js index 3c5b267..c815b37 100644 --- a/routes/index.js +++ b/routes/index.js @@ -4,7 +4,7 @@ var router = express.Router(); /* GET home page. */ router.get('/', function(req, res, next) { - var config = require('../config'); + var config = res.locals.config;//require('../config'); var db = config.init_db(); var data = []; diff --git a/routes/movie.js b/routes/movie.js index b72c0b4..368a512 100644 --- a/routes/movie.js +++ b/routes/movie.js @@ -19,8 +19,7 @@ function humanFileSize(bytes, si) { /* GET home page. */ router.get('/:id', function(req, res, next) { - - var config = require('../config'); + var config = res.locals.config; var db = config.init_db(); var data; diff --git a/routes/show.js b/routes/show.js index ee519d4..c1c2520 100644 --- a/routes/show.js +++ b/routes/show.js @@ -17,7 +17,7 @@ function addZero(v) { /* GET home page. */ router.get('/:id', function(req, res, next) { - var config = require('../config'); + var config = res.locals.config; var db = config.init_db(); var data = []; diff --git a/routes/show_list.js b/routes/show_list.js index 2a371b9..67e18e6 100644 --- a/routes/show_list.js +++ b/routes/show_list.js @@ -3,7 +3,7 @@ var router = express.Router(); /* GET home page. */ router.get('/:id', function(req, res, next) { - var config = require('../config'); + var config = res.locals.config; var db = config.init_db(); var shows = []; diff --git a/routes/users.js b/routes/users.js index 623e430..cb3f702 100644 --- a/routes/users.js +++ b/routes/users.js @@ -3,6 +3,7 @@ var router = express.Router(); /* GET users listing. */ router.get('/', function(req, res, next) { + var config = res.locals.config; res.send('respond with a resource'); }); diff --git a/views/layout.jade b/views/layout.jade index c0649c5..f88550e 100644 --- a/views/layout.jade +++ b/views/layout.jade @@ -3,7 +3,7 @@ html head title= title meta(charset='UTF8') - link(rel='stylesheet', href='//cdnjs.cloudflare.com/ajax/libs/bootswatch/3.3.5/darkly/bootstrap.css') + link(rel='stylesheet', href='#{config.theme}') link(rel='stylesheet', href='//cdn.datatables.net/1.10.9/css/dataTables.bootstrap.min.css') script(type='text/javascript', src='//code.jquery.com/jquery-1.11.3.min.js') script(type='text/javascript', src='//cdn.datatables.net/1.10.9/js/jquery.dataTables.min.js')