From 0d38995b2b4a59658caa0ca825d291d44eddb115 Mon Sep 17 00:00:00 2001 From: Bobby Wibowo Date: Thu, 20 Sep 2018 18:41:17 +0700 Subject: [PATCH] Updates * When gitHash in config.js is set to true, latest commit hash of the currently enabled git repo/branch will be displayed in home and nojs uploader pages. * Error pages can now be configured with errorPages option (their root directory and their file names). --- config.sample.js | 15 +++++++++++++++ lolisafe.js | 39 ++++++++++++++++++++++++++------------- pages/error/404.html | 18 +++++++++++++++++- pages/error/500.html | 18 +++++++++++++++++- pages/error/README.md | 5 ----- routes/nojs.js | 6 +++++- views/home.njk | 4 ++++ views/nojs.njk | 4 ++++ 8 files changed, 88 insertions(+), 21 deletions(-) mode change 120000 => 100644 pages/error/404.html mode change 120000 => 100644 pages/error/500.html delete mode 100644 pages/error/README.md diff --git a/config.sample.js b/config.sample.js index 54263b4..732178c 100644 --- a/config.sample.js +++ b/config.sample.js @@ -71,6 +71,21 @@ module.exports = { '.profile' ], + /* + Show hash of the current git commit in homepage. + */ + showGitHash: false, + + /* + Path to error pages. Only 404 and 500 will be used. + Note: rootDir can either be relative or absolute path. + */ + errorPages: { + rootDir: './pages/error', + 404: '404.html', + 500: '500.html' + }, + /* Uploads config. */ diff --git a/lolisafe.js b/lolisafe.js index d885412..959374e 100644 --- a/lolisafe.js +++ b/lolisafe.js @@ -12,6 +12,14 @@ const nunjucks = require('nunjucks') const RateLimit = require('express-rate-limit') const safe = express() +// It appears to be best to catch these before doing anything else +process.on('uncaughtException', error => { + console.error('Uncaught Exception:', error) +}) +process.on('unhandledRejection', error => { + console.error('Unhandled Rejection (Promise):', error) +}) + require('./database/db.js')(db) fs.existsSync('./pages/custom') || fs.mkdirSync('./pages/custom') @@ -60,7 +68,8 @@ for (const page of config.pages) { })) } else if (page === 'home') { safe.get('/', (req, res, next) => res.render('home', { - urlMaxSize: config.uploads.urlMaxSize + urlMaxSize: config.uploads.urlMaxSize, + gitHash: safe.get('git-hash') })) } else if (page === 'faq') { const fileLength = config.uploads.fileLength @@ -78,33 +87,37 @@ for (const page of config.pages) { } safe.use((req, res, next) => { - res.status(404).sendFile('404.html', { root: './pages/error/' }) + res.status(404).sendFile(config.errorPages[404], { root: config.errorPages.rootDir }) }) safe.use((error, req, res, next) => { console.error(error) - res.status(500).sendFile('500.html', { root: './pages/error/' }) -}) - -process.on('uncaughtException', error => { - console.error('Uncaught Exception:', error) -}) - -process.on('unhandledRejection', error => { - console.error('Unhandled Rejection (Promise):', error) + res.status(500).sendFile(config.errorPages[500], { root: config.errorPages.rootDir }) }) const start = async () => { + if (config.showGitHash) { + const gitHash = await new Promise((resolve, reject) => { + require('child_process').exec('git rev-parse HEAD', (error, stdout) => { + if (error) { return reject(error) } + resolve(stdout.replace(/\n$/, '')) + }) + }).catch(console.error) + if (!gitHash) { return } + console.log(`Git commit: ${gitHash}`) + safe.set('git-hash', gitHash) + } + if (config.uploads.scan && config.uploads.scan.enabled) { const created = await new Promise(async (resolve, reject) => { if (!config.uploads.scan.ip || !config.uploads.scan.port) { - return reject(new Error('Clamd IP or Port is missing')) + return reject(new Error('clamd IP or port is missing')) } const ping = await clamd.ping(config.uploads.scan.ip, config.uploads.scan.port).catch(reject) if (!ping) { return reject(new Error('Could not ping clamd')) } const version = await clamd.version(config.uploads.scan.ip, config.uploads.scan.port).catch(reject) - console.log(`${config.uploads.scan.ip}:${config.uploads.scan.port} ${version}`) + console.log(`${config.uploads.scan.ip}:${config.uploads.scan.port} ${version}`) const scanner = clamd.createScanner(config.uploads.scan.ip, config.uploads.scan.port) safe.set('clam-scanner', scanner) return resolve(true) diff --git a/pages/error/404.html b/pages/error/404.html deleted file mode 120000 index a6c9e90..0000000 --- a/pages/error/404.html +++ /dev/null @@ -1 +0,0 @@ -../../../HttpErrorPages/dist/HTTP404.html \ No newline at end of file diff --git a/pages/error/404.html b/pages/error/404.html new file mode 100644 index 0000000..e76f357 --- /dev/null +++ b/pages/error/404.html @@ -0,0 +1,17 @@ + + + + + + + + We've got some trouble | 404 - Resource not found + + + +
+

Resource not found 404

+

The requested resource could not be found but may be available again in the future.

+
+ + diff --git a/pages/error/500.html b/pages/error/500.html deleted file mode 120000 index 28890ae..0000000 --- a/pages/error/500.html +++ /dev/null @@ -1 +0,0 @@ -../../../HttpErrorPages/dist/HTTP500.html \ No newline at end of file diff --git a/pages/error/500.html b/pages/error/500.html new file mode 100644 index 0000000..b9bd9c8 --- /dev/null +++ b/pages/error/500.html @@ -0,0 +1,17 @@ + + + + + + + + We've got some trouble | 500 - Webservice currently unavailable + + + +
+

Webservice currently unavailable 500

+

An unexpected condition was encountered.
Our service team has been dispatched to bring it back online.

+
+ + diff --git a/pages/error/README.md b/pages/error/README.md deleted file mode 100644 index 7e8eaf5..0000000 --- a/pages/error/README.md +++ /dev/null @@ -1,5 +0,0 @@ -# Disclaimer as to why this folder only have Symlinks - -At fiery.me, we are using this: [https://github.com/BobbyWibowo/HttpErrorPages](https://github.com/BobbyWibowo/HttpErrorPages). - -It's nothing too important, but we are sharing the error pages from that directory to all other instances in the server (regular http sites with nginx and node servers). diff --git a/routes/nojs.js b/routes/nojs.js index 1bcfdba..2ca41bd 100644 --- a/routes/nojs.js +++ b/routes/nojs.js @@ -16,7 +16,10 @@ if (config.private) { } routes.get('/nojs', async (req, res, next) => { - return res.render('nojs', { renderOptions }) + const options = { renderOptions } + options.gitHash = req.app.get('git-hash') + + return res.render('nojs', options) }) routes.post('/nojs', (req, res, next) => { @@ -26,6 +29,7 @@ routes.post('/nojs', (req, res, next) => { const result = args[0] const options = { renderOptions } + options.gitHash = req.app.get('git-hash') options.errorMessage = result.success ? '' : (result.description || 'An unexpected error occurred.') options.files = result.files || [{}] diff --git a/views/home.njk b/views/home.njk index fd57dd4..ad05dc6 100644 --- a/views/home.njk +++ b/views/home.njk @@ -115,6 +115,10 @@ {% include "_partial/links.njk" %} + + {% if gitHash -%} +

Git commit: {{ gitHash }}

+ {%- endif %} diff --git a/views/nojs.njk b/views/nojs.njk index 3cfc3e1..90c6f7e 100644 --- a/views/nojs.njk +++ b/views/nojs.njk @@ -60,6 +60,10 @@ {% set plain_links = true %} {% include "_partial/links.njk" %} + + {% if gitHash -%} +

Git commit: {{ gitHash }}

+ {%- endif %}