mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Defer middleware HTML file reads
This commit is contained in:
@@ -10,13 +10,13 @@ import { getConfig, getConfigValue, safeReadFileSync } from '../util.js';
|
|||||||
const PER_USER_BASIC_AUTH = getConfigValue('perUserBasicAuth', false);
|
const PER_USER_BASIC_AUTH = getConfigValue('perUserBasicAuth', false);
|
||||||
const ENABLE_ACCOUNTS = getConfigValue('enableUserAccounts', false);
|
const ENABLE_ACCOUNTS = getConfigValue('enableUserAccounts', false);
|
||||||
|
|
||||||
const unauthorizedWebpage = safeReadFileSync('./public/error/unauthorized.html') ?? '';
|
|
||||||
const unauthorizedResponse = (res) => {
|
|
||||||
res.set('WWW-Authenticate', 'Basic realm="SillyTavern", charset="UTF-8"');
|
|
||||||
return res.status(401).send(unauthorizedWebpage);
|
|
||||||
};
|
|
||||||
|
|
||||||
const basicAuthMiddleware = async function (request, response, callback) {
|
const basicAuthMiddleware = async function (request, response, callback) {
|
||||||
|
const unauthorizedWebpage = safeReadFileSync('./public/error/unauthorized.html') ?? '';
|
||||||
|
const unauthorizedResponse = (res) => {
|
||||||
|
res.set('WWW-Authenticate', 'Basic realm="SillyTavern", charset="UTF-8"');
|
||||||
|
return res.status(401).send(unauthorizedWebpage);
|
||||||
|
};
|
||||||
|
|
||||||
const config = getConfig();
|
const config = getConfig();
|
||||||
const authHeader = request.headers.authorization;
|
const authHeader = request.headers.authorization;
|
||||||
|
|
||||||
|
@@ -11,9 +11,6 @@ const whitelistPath = path.join(process.cwd(), './whitelist.txt');
|
|||||||
const enableForwardedWhitelist = getConfigValue('enableForwardedWhitelist', false);
|
const enableForwardedWhitelist = getConfigValue('enableForwardedWhitelist', false);
|
||||||
let whitelist = getConfigValue('whitelist', []);
|
let whitelist = getConfigValue('whitelist', []);
|
||||||
let knownIPs = new Set();
|
let knownIPs = new Set();
|
||||||
const forbiddenWebpage = Handlebars.compile(
|
|
||||||
safeReadFileSync('./public/error/forbidden-by-whitelist.html') ?? '',
|
|
||||||
);
|
|
||||||
|
|
||||||
if (fs.existsSync(whitelistPath)) {
|
if (fs.existsSync(whitelistPath)) {
|
||||||
try {
|
try {
|
||||||
@@ -56,6 +53,10 @@ function getForwardedIp(req) {
|
|||||||
* @returns {import('express').RequestHandler} The middleware function
|
* @returns {import('express').RequestHandler} The middleware function
|
||||||
*/
|
*/
|
||||||
export default function whitelistMiddleware(whitelistMode, listen) {
|
export default function whitelistMiddleware(whitelistMode, listen) {
|
||||||
|
const forbiddenWebpage = Handlebars.compile(
|
||||||
|
safeReadFileSync('./public/error/forbidden-by-whitelist.html') ?? '',
|
||||||
|
);
|
||||||
|
|
||||||
return function (req, res, next) {
|
return function (req, res, next) {
|
||||||
const clientIp = getIpFromRequest(req);
|
const clientIp = getIpFromRequest(req);
|
||||||
const forwardedIp = getForwardedIp(req);
|
const forwardedIp = getForwardedIp(req);
|
||||||
|
Reference in New Issue
Block a user