Move access.log to data root
| @@ -57,7 +57,7 @@ import { | ||
| 57 | 57 | |
| 58 | 58 | import getWebpackServeMiddleware from './src/middleware/webpack-serve.js'; |
| 59 | 59 | import basicAuthMiddleware from './src/middleware/basicAuth.js'; |
| 60 | 60 | import whitelistMiddleware, { getAccessLogPath, migrateAccessLog } from './src/middleware/whitelist.js'; |
| 61 | 61 | import multerMonkeyPatch from './src/middleware/multerMonkeyPatch.js'; |
| 62 | 62 | import initRequestProxy from './src/request-proxy.js'; |
| 63 | 63 | import getCacheBusterMiddleware from './src/middleware/cacheBuster.js'; |
| @@ -754,6 +754,7 @@ const preSetupTasks = async function () { | ||
| 754 | 754 | await checkForNewContent(directories); |
| 755 | 755 | await ensureThumbnailCache(); |
| 756 | 756 | cleanUploads(); |
| 757 | + migrateAccessLog(); | |
| 757 | 758 | |
| 758 | 759 | await settingsInit(); |
| 759 | 760 | await statsInit(); |
| @@ -856,7 +857,7 @@ const postSetupTasks = async function (v6Failed, v4Failed, useIPv6, useIPv4) { | ||
| 856 | 857 | if (listen) { |
| 857 | 858 | console.log(); |
| 858 | 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 | 860 | console.log('Check the "access.log" file in the SillyTaverndata directory to inspect incoming connections.:', color.green(getAccessLogPath())); |
| 860 | 861 | } |
| 861 | 862 | console.log('\n' + getSeparator(plainGoToLog.length) + '\n'); |
| 862 | 863 | console.log(goToLog); |
| @@ -12,6 +12,8 @@ const enableForwardedWhitelist = getConfigValue('enableForwardedWhitelist', fals | ||
| 12 | 12 | let whitelist = getConfigValue('whitelist', []); |
| 13 | 13 | let knownIPs = new Set(); |
| 14 | 14 | |
| 15 | +export const getAccessLogPath = () => path.join(globalThis.DATA_ROOT, 'access.log'); | |
| 16 | + | |
| 15 | 17 | if (fs.existsSync(whitelistPath)) { |
| 16 | 18 | try { |
| 17 | 19 | let whitelistTxt = fs.readFileSync(whitelistPath, 'utf-8'); |
| @@ -46,6 +48,23 @@ function getForwardedIp(req) { | ||
| 46 | 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 | 69 | * Returns a middleware function that checks if the client IP is in the whitelist. |
| 51 | 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 | 86 | knownIPs.add(clientIp); |
| 68 | 87 | |
| 69 | 88 | // Write access log |
| 89 | + const logPath = getAccessLogPath(); | |
| 70 | 90 | const timestamp = new Date().toISOString(); |
| 71 | 91 | const log = `${timestamp} ${clientIp} ${userAgent}\n`; |
| 72 | 92 | fs.appendFile('access.log'logPath, log, (err) => { |
| 73 | 93 | if (err) { |
| 74 | 94 | console.error('Failed to write access log:', err); |
| 75 | 95 | } |