Defer middleware HTML file reads
| @@ -10,13 +10,13 @@ import { getConfig, getConfigValue, safeReadFileSync } from '../util.js'; | |||
| 10 | const PER_USER_BASIC_AUTH = getConfigValue('perUserBasicAuth', false); | 10 | const PER_USER_BASIC_AUTH = getConfigValue('perUserBasicAuth', false); |
| 11 | const ENABLE_ACCOUNTS = getConfigValue('enableUserAccounts', false); | 11 | const ENABLE_ACCOUNTS = getConfigValue('enableUserAccounts', false); |
| 12 | 12 | ||
| 13 | const basicAuthMiddleware = async function (request, response, callback) { | ||
| 13 | const unauthorizedWebpage = safeReadFileSync('./public/error/unauthorized.html') ?? ''; | 14 | const unauthorizedWebpage = safeReadFileSync('./public/error/unauthorized.html') ?? ''; |
| 14 | const unauthorizedResponse = (res) => { | 15 | const unauthorizedResponse = (res) => { |
| 15 | res.set('WWW-Authenticate', 'Basic realm="SillyTavern", charset="UTF-8"'); | 16 | res.set('WWW-Authenticate', 'Basic realm="SillyTavern", charset="UTF-8"'); |
| 16 | return res.status(401).send(unauthorizedWebpage); | 17 | return res.status(401).send(unauthorizedWebpage); |
| 17 | }; | 18 | }; |
| 18 | 19 | ||
| 19 | const basicAuthMiddleware = async function (request, response, callback) { | ||
| 20 | const config = getConfig(); | 20 | const config = getConfig(); |
| 21 | const authHeader = request.headers.authorization; | 21 | const authHeader = request.headers.authorization; |
| 22 | 22 | ||
| @@ -11,9 +11,6 @@ const whitelistPath = path.join(process.cwd(), './whitelist.txt'); | |||
| 11 | const enableForwardedWhitelist = getConfigValue('enableForwardedWhitelist', false); | 11 | const enableForwardedWhitelist = getConfigValue('enableForwardedWhitelist', false); |
| 12 | let whitelist = getConfigValue('whitelist', []); | 12 | let whitelist = getConfigValue('whitelist', []); |
| 13 | let knownIPs = new Set(); | 13 | let knownIPs = new Set(); |
| 14 | const forbiddenWebpage = Handlebars.compile( | ||
| 15 | safeReadFileSync('./public/error/forbidden-by-whitelist.html') ?? '', | ||
| 16 | ); | ||
| 17 | 14 | ||
| 18 | if (fs.existsSync(whitelistPath)) { | 15 | if (fs.existsSync(whitelistPath)) { |
| 19 | try { | 16 | try { |
| @@ -56,6 +53,10 @@ function getForwardedIp(req) { | |||
| 56 | * @returns {import('express').RequestHandler} The middleware function | 53 | * @returns {import('express').RequestHandler} The middleware function |
| 57 | */ | 54 | */ |
| 58 | export default function whitelistMiddleware(whitelistMode, listen) { | 55 | export default function whitelistMiddleware(whitelistMode, listen) { |
| 56 | const forbiddenWebpage = Handlebars.compile( | ||
| 57 | safeReadFileSync('./public/error/forbidden-by-whitelist.html') ?? '', | ||
| 58 | ); | ||
| 59 | |||
| 59 | return function (req, res, next) { | 60 | return function (req, res, next) { |
| 60 | const clientIp = getIpFromRequest(req); | 61 | const clientIp = getIpFromRequest(req); |
| 61 | const forwardedIp = getForwardedIp(req); | 62 | const forwardedIp = getForwardedIp(req); |