woohoo

90459116e36997ab75384af55b5c224520745173

Spappz <34202141+Spappz@users.noreply.github.com>

Signed
2 files changed, +22 -4Showing whitespace changes
default/config.yaml+2 -0
@@ -31,6 +31,8 @@ enableForwardedWhitelist: true
3131whitelist:
3232 - ::1
3333 - 127.0.0.1
34+# HTML displayed when a connection is blocked. Use "{{ipDetails}}" to print the client's IP.
35+whitelistErrorMessage: "<h1>Forbidden</h1><p>If you are the system administrator, add your IP address to the whitelist or disable whitelist mode by editing <code>config.yaml</code> in the root directory of your installation.</p><hr /><p><em>Connection from {{ipDetails}} has been blocked. This attempt has been logged.</em></p>"
3436# Toggle basic authentication for endpoints
3537basicAuthMode: false
3638# Basic authentication credentials
src/middleware/whitelist.js+20 -4
@@ -1,6 +1,7 @@
11import path from 'node:path';
22import fs from 'node:fs';
33import process from 'node:process';
4+import Handlebars from 'handlebars';
45import ipMatching from 'ip-matching';
56
67import { getIpFromRequest } from '../express-common.js';
@@ -11,6 +12,9 @@ const enableForwardedWhitelist = getConfigValue('enableForwardedWhitelist', fals
1112let whitelist = getConfigValue('whitelist', []);
1213let knownIPs = new Set();
1314
15+const DEFAULT_WHITELIST_ERROR_MESSAGE =
16+ '<h1>Forbidden</h1><p>If you are the system administrator, add your IP address to the whitelist or disable whitelist mode by editing <code>config.yaml</code> in the root directory of your installation.</p><hr /><p><em>Connection from {{ipDetails}} has been blocked. This attempt has been logged.</em></p>';
17+
1418if (fs.existsSync(whitelistPath)) {
1519 try {
1620 let whitelistTxt = fs.readFileSync(whitelistPath, 'utf-8');
@@ -55,9 +59,9 @@ export default function whitelistMiddleware(whitelistMode, listen) {
5559 return function (req, res, next) {
5660 const clientIp = getIpFromRequest(req);
5761 const forwardedIp = getForwardedIp(req);
62+ const userAgent = req.headers['user-agent'];
5863
5964 if (listen && !knownIPs.has(clientIp)) {
60- const userAgent = req.headers['user-agent'];
6165 console.log(color.yellow(`New connection from ${clientIp}; User Agent: ${userAgent}\n`));
6266 knownIPs.add(clientIp);
6367
@@ -76,9 +80,21 @@ export default function whitelistMiddleware(whitelistMode, listen) {
7680 || forwardedIp && whitelistMode === true && !whitelist.some(x => ipMatching.matches(forwardedIp, ipMatching.getMatch(x)))
7781 ) {
7882 // Log the connection attempt with real IP address
79- const ipDetails = forwardedIp ? `${clientIp} (forwarded from ${forwardedIp})` : clientIp;
83+ const ipDetails = forwardedIp
80- console.log(color.red('Forbidden: Connection attempt from ' + ipDetails + '. If you are attempting to connect, please add your IP address in whitelist or disable whitelist mode in config.yaml in root of SillyTavern folder.\n'));
84+ ? `${clientIp} (forwarded from ${forwardedIp})`
81- return res.status(403).send('<b>Forbidden</b>: Connection attempt from <b>' + ipDetails + '</b>. If you are attempting to connect, please add your IP address in whitelist or disable whitelist mode in config.yaml in root of SillyTavern folder.');
85+ : clientIp;
86+ const errorMessage = Handlebars.compile(
87+ getConfigValue(
88+ 'whitelistErrorMessage',
89+ DEFAULT_WHITELIST_ERROR_MESSAGE,
90+ ),
91+ );
92+ console.log(
93+ color.red(
94+ `Blocked connection from ${clientIp}; User Agent: ${userAgent}\n\tTo allow this connection, add its IP address to the whitelist or disable whitelist mode by editing config.yaml in the root directory of your SillyTavern installation.\n`,
95+ ),
96+ );
97+ return res.status(403).send(errorMessage({ ipDetails }));
8298 }
8399 next();
84100 };