2020-01-12 03:33:19 +00:00
|
|
|
const express = require('express')
|
|
|
|
const router = express.Router()
|
|
|
|
const _ = require('lodash')
|
2020-01-20 02:30:25 +00:00
|
|
|
const qs = require('querystring')
|
2020-01-12 03:33:19 +00:00
|
|
|
|
|
|
|
/* global WIKI */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Let's Encrypt Challenge
|
|
|
|
*/
|
|
|
|
router.get('/.well-known/acme-challenge/:token', (req, res, next) => {
|
|
|
|
res.type('text/plain')
|
|
|
|
if (_.get(WIKI.config, 'letsencrypt.challenge', false)) {
|
|
|
|
if (WIKI.config.letsencrypt.challenge.token === req.params.token) {
|
|
|
|
res.send(WIKI.config.letsencrypt.challenge.keyAuthorization)
|
|
|
|
WIKI.logger.info(`(LETSENCRYPT) Received valid challenge request. [ ACCEPTED ]`)
|
|
|
|
} else {
|
|
|
|
res.status(406).send('Invalid Challenge Token!')
|
|
|
|
WIKI.logger.warn(`(LETSENCRYPT) Received invalid challenge request. [ REJECTED ]`)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
res.status(418).end()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-01-20 02:30:25 +00:00
|
|
|
/**
|
|
|
|
* Redirect to HTTPS if HTTP Redirection is enabled
|
|
|
|
*/
|
|
|
|
router.all('/*', (req, res, next) => {
|
|
|
|
if (WIKI.config.server.sslRedir && !req.secure && WIKI.servers.servers.https) {
|
2023-06-04 03:19:01 +00:00
|
|
|
return res.redirect(`https://${req.hostname}${req.originalUrl}`)
|
2020-01-20 02:30:25 +00:00
|
|
|
} else {
|
|
|
|
next()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-01-12 03:33:19 +00:00
|
|
|
module.exports = router
|