Move access.log to data root
| @@ -57,7 +57,7 @@ import { | |||
| 57 | 57 | ||
| 58 | import getWebpackServeMiddleware from './src/middleware/webpack-serve.js'; | 58 | import getWebpackServeMiddleware from './src/middleware/webpack-serve.js'; |
| 59 | import basicAuthMiddleware from './src/middleware/basicAuth.js'; | 59 | import basicAuthMiddleware from './src/middleware/basicAuth.js'; |
| 60 | import whitelistMiddleware from './src/middleware/whitelist.js'; | 60 | import whitelistMiddleware, { getAccessLogPath, migrateAccessLog } from './src/middleware/whitelist.js'; |
| 61 | import multerMonkeyPatch from './src/middleware/multerMonkeyPatch.js'; | 61 | import multerMonkeyPatch from './src/middleware/multerMonkeyPatch.js'; |
| 62 | import initRequestProxy from './src/request-proxy.js'; | 62 | import initRequestProxy from './src/request-proxy.js'; |
| 63 | import getCacheBusterMiddleware from './src/middleware/cacheBuster.js'; | 63 | import 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(); | ||
| 757 | 758 | ||
| 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); |
| @@ -12,6 +12,8 @@ const enableForwardedWhitelist = getConfigValue('enableForwardedWhitelist', fals | |||
| 12 | let whitelist = getConfigValue('whitelist', []); | 12 | let whitelist = getConfigValue('whitelist', []); |
| 13 | let knownIPs = new Set(); | 13 | let knownIPs = new Set(); |
| 14 | 14 | ||
| 15 | export const getAccessLogPath = () => path.join(globalThis.DATA_ROOT, 'access.log'); | ||
| 16 | |||
| 15 | if (fs.existsSync(whitelistPath)) { | 17 | if (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 | } |
| 48 | 50 | ||
| 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 | |||
| 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 line | 70 | * @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); |
| 68 | 87 | ||
| 69 | // Write access log | 88 | // 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 | } |