Merge pull request #3499 from SillyTavern/accesslog-dataroot Move access.log to data root

826e4f6d1640032acc59a3eddf36f3bedac73557

Cohee <18619528+Cohee1207@users.noreply.github.com>

Signed
2 files changed, +24 -3Ignore whitespace
server.js+3 -2
@@ -57,7 +57,7 @@ import {
5757
5858import getWebpackServeMiddleware from './src/middleware/webpack-serve.js';
5959import basicAuthMiddleware from './src/middleware/basicAuth.js';
6060import whitelistMiddleware, { getAccessLogPath, migrateAccessLog } from './src/middleware/whitelist.js';
6161import multerMonkeyPatch from './src/middleware/multerMonkeyPatch.js';
6262import initRequestProxy from './src/request-proxy.js';
6363import getCacheBusterMiddleware from './src/middleware/cacheBuster.js';
@@ -754,6 +754,7 @@ const preSetupTasks = async function () {
754754 await checkForNewContent(directories);
755755 await ensureThumbnailCache();
756756 cleanUploads();
757+ migrateAccessLog();
757758
758759 await settingsInit();
759760 await statsInit();
@@ -856,7 +857,7 @@ const postSetupTasks = async function (v6Failed, v4Failed, useIPv6, useIPv4) {
856857 if (listen) {
857858 console.log();
858859 console.log('To limit connections to internal localhost only ([::1] or 127.0.0.1), change the setting in config.yaml to "listen: false".');
859860 console.log('Check the "access.log" file in the SillyTaverndata directory to inspect incoming connections.:', color.green(getAccessLogPath()));
860861 }
861862 console.log('\n' + getSeparator(plainGoToLog.length) + '\n');
862863 console.log(goToLog);
src/middleware/whitelist.js+21 -1
@@ -12,6 +12,8 @@ const enableForwardedWhitelist = getConfigValue('enableForwardedWhitelist', fals
1212let whitelist = getConfigValue('whitelist', []);
1313let knownIPs = new Set();
1414
15+export const getAccessLogPath = () => path.join(globalThis.DATA_ROOT, 'access.log');
16+
1517if (fs.existsSync(whitelistPath)) {
1618 try {
1719 let whitelistTxt = fs.readFileSync(whitelistPath, 'utf-8');
@@ -46,6 +48,23 @@ function getForwardedIp(req) {
4648 return undefined;
4749}
4850
51+export function migrateAccessLog() {
52+ try {
53+ if (!fs.existsSync('access.log')) {
54+ return;
55+ }
56+ const logPath = getAccessLogPath();
57+ if (fs.existsSync(logPath)) {
58+ return;
59+ }
60+ fs.renameSync('access.log', logPath);
61+ console.log(color.yellow('Migrated access.log to new location:'), logPath);
62+ } catch (e) {
63+ console.error('Failed to migrate access log:', e);
64+ console.info('Please move access.log to the data directory manually.');
65+ }
66+}
67+
4968/**
5069 * Returns a middleware function that checks if the client IP is in the whitelist.
5170 * @param {boolean} whitelistMode If whitelist mode is enabled via config or command line
@@ -67,9 +86,10 @@ export default function whitelistMiddleware(whitelistMode, listen) {
6786 knownIPs.add(clientIp);
6887
6988 // Write access log
89+ const logPath = getAccessLogPath();
7090 const timestamp = new Date().toISOString();
7191 const log = `${timestamp} ${clientIp} ${userAgent}\n`;
7292 fs.appendFile('access.log'logPath, log, (err) => {
7393 if (err) {
7494 console.error('Failed to write access log:', err);
7595 }