mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Refactor whitelist middleware to return a promise and update server.js to handle async initialization
This commit is contained in:
@ -41,7 +41,7 @@ import {
|
|||||||
|
|
||||||
import getWebpackServeMiddleware from './src/middleware/webpack-serve.js';
|
import getWebpackServeMiddleware from './src/middleware/webpack-serve.js';
|
||||||
import basicAuthMiddleware from './src/middleware/basicAuth.js';
|
import basicAuthMiddleware from './src/middleware/basicAuth.js';
|
||||||
import whitelistMiddleware from './src/middleware/whitelist.js';
|
import getWhitelistMiddleware from './src/middleware/whitelist.js';
|
||||||
import accessLoggerMiddleware, { getAccessLogPath, migrateAccessLog } from './src/middleware/accessLogWriter.js';
|
import accessLoggerMiddleware, { getAccessLogPath, migrateAccessLog } from './src/middleware/accessLogWriter.js';
|
||||||
import multerMonkeyPatch from './src/middleware/multerMonkeyPatch.js';
|
import multerMonkeyPatch from './src/middleware/multerMonkeyPatch.js';
|
||||||
import initRequestProxy from './src/request-proxy.js';
|
import initRequestProxy from './src/request-proxy.js';
|
||||||
@ -125,7 +125,8 @@ if (cliArgs.listen && cliArgs.basicAuthMode) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (cliArgs.whitelistMode) {
|
if (cliArgs.whitelistMode) {
|
||||||
app.use(whitelistMiddleware());
|
const whitelistMiddleware = await getWhitelistMiddleware();
|
||||||
|
app.use(whitelistMiddleware);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cliArgs.listen) {
|
if (cliArgs.listen) {
|
||||||
|
@ -22,8 +22,6 @@ if (fs.existsSync(whitelistPath)) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await resolveHostnames();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the client IP address from the request headers.
|
* Get the client IP address from the request headers.
|
||||||
* @param {import('express').Request} req Express request object
|
* @param {import('express').Request} req Express request object
|
||||||
@ -110,9 +108,9 @@ async function resolveHostnames() {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a middleware function that checks if the client IP is in the whitelist.
|
* Returns a middleware function that checks if the client IP is in the whitelist.
|
||||||
* @returns {import('express').RequestHandler} The middleware function
|
* @returns {Promise<import('express').RequestHandler>} Promise that resolves to the middleware function
|
||||||
*/
|
*/
|
||||||
export default function whitelistMiddleware() {
|
export default async function getWhitelistMiddleware() {
|
||||||
const forbiddenWebpage = Handlebars.compile(
|
const forbiddenWebpage = Handlebars.compile(
|
||||||
safeReadFileSync('./public/error/forbidden-by-whitelist.html') ?? '',
|
safeReadFileSync('./public/error/forbidden-by-whitelist.html') ?? '',
|
||||||
);
|
);
|
||||||
@ -121,6 +119,8 @@ export default function whitelistMiddleware() {
|
|||||||
'/favicon.ico',
|
'/favicon.ico',
|
||||||
];
|
];
|
||||||
|
|
||||||
|
await resolveHostnames();
|
||||||
|
|
||||||
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