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
58import getWebpackServeMiddleware from './src/middleware/webpack-serve.js';58import getWebpackServeMiddleware from './src/middleware/webpack-serve.js';
59import basicAuthMiddleware from './src/middleware/basicAuth.js';59import basicAuthMiddleware from './src/middleware/basicAuth.js';
60import whitelistMiddleware from './src/middleware/whitelist.js';60import whitelistMiddleware, { getAccessLogPath, migrateAccessLog } from './src/middleware/whitelist.js';
61import multerMonkeyPatch from './src/middleware/multerMonkeyPatch.js';61import multerMonkeyPatch from './src/middleware/multerMonkeyPatch.js';
62import initRequestProxy from './src/request-proxy.js';62import initRequestProxy from './src/request-proxy.js';
63import getCacheBusterMiddleware from './src/middleware/cacheBuster.js';63import getCacheBusterMiddleware from './src/middleware/cacheBuster.js';
@@ -754,6 +754,7 @@ const preSetupTasks = async function () {
754 await checkForNewContent(directories);754 await checkForNewContent(directories);
755 await ensureThumbnailCache();755 await ensureThumbnailCache();
756 cleanUploads();756 cleanUploads();
757 migrateAccessLog();
757758
758 await settingsInit();759 await settingsInit();
759 await statsInit();760 await statsInit();
@@ -856,7 +857,7 @@ const postSetupTasks = async function (v6Failed, v4Failed, useIPv6, useIPv4) {
856 if (listen) {857 if (listen) {
857 console.log();858 console.log();
858 console.log('To limit connections to internal localhost only ([::1] or 127.0.0.1), change the setting in config.yaml to "listen: false".');859 console.log('To limit connections to internal localhost only ([::1] or 127.0.0.1), change the setting in config.yaml to "listen: false".');
859 console.log('Check the "access.log" file in the SillyTavern directory to inspect incoming connections.');860 console.log('Check the "access.log" file in the data directory to inspect incoming connections:', color.green(getAccessLogPath()));
860 }861 }
861 console.log('\n' + getSeparator(plainGoToLog.length) + '\n');862 console.log('\n' + getSeparator(plainGoToLog.length) + '\n');
862 console.log(goToLog);863 console.log(goToLog);
src/middleware/whitelist.js+21 -1
@@ -12,6 +12,8 @@ const enableForwardedWhitelist = getConfigValue('enableForwardedWhitelist', fals
12let whitelist = getConfigValue('whitelist', []);12let whitelist = getConfigValue('whitelist', []);
13let knownIPs = new Set();13let knownIPs = new Set();
1414
15export const getAccessLogPath = () => path.join(globalThis.DATA_ROOT, 'access.log');
16
15if (fs.existsSync(whitelistPath)) {17if (fs.existsSync(whitelistPath)) {
16 try {18 try {
17 let whitelistTxt = fs.readFileSync(whitelistPath, 'utf-8');19 let whitelistTxt = fs.readFileSync(whitelistPath, 'utf-8');
@@ -46,6 +48,23 @@ function getForwardedIp(req) {
46 return undefined;48 return undefined;
47}49}
4850
51export 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
49/**68/**
50 * Returns a middleware function that checks if the client IP is in the whitelist.69 * Returns a middleware function that checks if the client IP is in the whitelist.
51 * @param {boolean} whitelistMode If whitelist mode is enabled via config or command line70 * @param {boolean} whitelistMode If whitelist mode is enabled via config or command line
@@ -67,9 +86,10 @@ export default function whitelistMiddleware(whitelistMode, listen) {
67 knownIPs.add(clientIp);86 knownIPs.add(clientIp);
6887
69 // Write access log88 // Write access log
89 const logPath = getAccessLogPath();
70 const timestamp = new Date().toISOString();90 const timestamp = new Date().toISOString();
71 const log = `${timestamp} ${clientIp} ${userAgent}\n`;91 const log = `${timestamp} ${clientIp} ${userAgent}\n`;
72 fs.appendFile('access.log', log, (err) => {92 fs.appendFile(logPath, log, (err) => {
73 if (err) {93 if (err) {
74 console.error('Failed to write access log:', err);94 console.error('Failed to write access log:', err);
75 }95 }