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