refactor(Middleware): only mount accessLogger when listen is enabled

This commit is contained in:
KevinSun
2025-02-21 23:49:15 +08:00
parent bfc609c2a8
commit 9c3e8c935b
2 changed files with 5 additions and 4 deletions

View File

@ -344,7 +344,9 @@ app.use(CORS);
if (listen && basicAuthMode) app.use(basicAuthMiddleware); if (listen && basicAuthMode) app.use(basicAuthMiddleware);
app.use(whitelistMiddleware(enableWhitelist)); app.use(whitelistMiddleware(enableWhitelist));
app.use(accessLoggerMiddleware(listen)); if (listen) {
app.use(accessLoggerMiddleware());
}
if (enableCorsProxy) { if (enableCorsProxy) {
app.use(bodyParser.json({ app.use(bodyParser.json({

View File

@ -28,15 +28,14 @@ export function migrateAccessLog() {
/** /**
* Creates middleware for logging access and new connections * Creates middleware for logging access and new connections
* @param {boolean} listen If listen mode is enabled via config or command line
* @returns {import('express').RequestHandler} * @returns {import('express').RequestHandler}
*/ */
export default function accessLoggerMiddleware(listen) { export default function accessLoggerMiddleware() {
return function (req, res, next) { return function (req, res, next) {
const clientIp = getRealIpFromHeader(req); const clientIp = getRealIpFromHeader(req);
const userAgent = req.headers['user-agent']; const userAgent = req.headers['user-agent'];
if (listen && !knownIPs.has(clientIp)) { if (!knownIPs.has(clientIp)) {
// Log new connection // Log new connection
console.info(color.yellow(`New connection from ${clientIp}; User Agent: ${userAgent}\n`)); console.info(color.yellow(`New connection from ${clientIp}; User Agent: ${userAgent}\n`));
knownIPs.add(clientIp); knownIPs.add(clientIp);