You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
1.1 KiB

  1. const express = require('express')
  2. const router = express.Router()
  3. const _ = require('lodash')
  4. const qs = require('querystring')
  5. /* global WIKI */
  6. /**
  7. * Let's Encrypt Challenge
  8. */
  9. router.get('/.well-known/acme-challenge/:token', (req, res, next) => {
  10. res.type('text/plain')
  11. if (_.get(WIKI.config, 'letsencrypt.challenge', false)) {
  12. if (WIKI.config.letsencrypt.challenge.token === req.params.token) {
  13. res.send(WIKI.config.letsencrypt.challenge.keyAuthorization)
  14. WIKI.logger.info(`(LETSENCRYPT) Received valid challenge request. [ ACCEPTED ]`)
  15. } else {
  16. res.status(406).send('Invalid Challenge Token!')
  17. WIKI.logger.warn(`(LETSENCRYPT) Received invalid challenge request. [ REJECTED ]`)
  18. }
  19. } else {
  20. res.status(418).end()
  21. }
  22. })
  23. /**
  24. * Redirect to HTTPS if HTTP Redirection is enabled
  25. */
  26. router.all('/*', (req, res, next) => {
  27. if (WIKI.config.server.sslRedir && !req.secure && WIKI.servers.servers.https) {
  28. let query = (!_.isEmpty(req.query)) ? `?${qs.stringify(req.query)}` : ``
  29. return res.redirect(`https://${req.hostname}${req.originalUrl}${query}`)
  30. } else {
  31. next()
  32. }
  33. })
  34. module.exports = router