Browse Source

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).
Bobby Wibowo 1 year ago
parent
commit
0d38995b2b
10 changed files with 88 additions and 21 deletions
  1. 15 0
      config.sample.js
  2. 26 13
      lolisafe.js
  3. 0 1
      pages/error/404.html
  4. 17 0
      pages/error/404.html
  5. 0 1
      pages/error/500.html
  6. 17 0
      pages/error/500.html
  7. 0 5
      pages/error/README.md
  8. 5 1
      routes/nojs.js
  9. 4 0
      views/home.njk
  10. 4 0
      views/nojs.njk

+ 15 - 0
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.
   */

+ 26 - 13
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)

+ 0 - 1
pages/error/404.html

@@ -1 +0,0 @@
-../../../HttpErrorPages/dist/HTTP404.html

File diff suppressed because it is too large
+ 17 - 0
pages/error/404.html


+ 0 - 1
pages/error/500.html

@@ -1 +0,0 @@
-../../../HttpErrorPages/dist/HTTP500.html

File diff suppressed because it is too large
+ 17 - 0
pages/error/500.html


+ 0 - 5
pages/error/README.md

@@ -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).

+ 5 - 1
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 || [{}]

+ 4 - 0
views/home.njk

@@ -115,6 +115,10 @@
       </h3>
 
       {% include "_partial/links.njk" %}
+
+      {% if gitHash -%}
+      <p>Git commit: <a href="https://github.com/BobbyWibowo/lolisafe/commit/{{ gitHash }}" target="_blank" rel="noopener">{{ gitHash }}</a></p>
+      {%- endif %}
     </div>
   </div>
 </section>

+ 4 - 0
views/nojs.njk

@@ -60,6 +60,10 @@
 
       {% set plain_links = true %}
       {% include "_partial/links.njk" %}
+
+      {% if gitHash -%}
+      <p>Git commit: <a href="https://github.com/BobbyWibowo/lolisafe/commit/{{ gitHash }}" target="_blank" rel="noopener">{{ gitHash }}</a></p>
+      {%- endif %}
     </div>
   </div>
 </section>