diff --git a/app.js b/app.js index 6a790ad..fdaac82 100644 --- a/app.js +++ b/app.js @@ -232,7 +232,7 @@ function startServices() { // We're good to connect them! connectedClients += 1; - logger.log('info', '%s connected, total clients: %d', client.request.connection.remoteAddress, connectedClients); + logger.log('info', '%s connected, total clients: %d', util.getRemoteAddr(client.request), connectedClients); // We send the boot time (also sent in publicConfig.json) to the frontend to validate they have the same config. // If so, they'll send back "requestListing" event, otherwise they will pull the new config and retry. @@ -242,7 +242,7 @@ function startServices() { client.on('disconnect', function() { connectedClients -= 1; - logger.log('info', '%s disconnected, total clients: %d', client.request.connection.remoteAddress, connectedClients); + logger.log('info', '%s disconnected, total clients: %d', util.getRemoteAddr(client.request), connectedClients); }); client.on('requestHistoryGraph', function() { diff --git a/lib/server.js b/lib/server.js index 5606fdb..e867c79 100644 --- a/lib/server.js +++ b/lib/server.js @@ -27,12 +27,7 @@ function setupRoutes() { function handleRequest(req, res) { var requestUrl = url.parse(req.url).pathname; - let remoteAddress = req.connection.remoteAddress; - if (req.headers && (req.headers['cf-connecting-ip'] || req.headers['x-forwarded-for'])) { - remoteAddress = req.headers['cf-connecting-ip'] || req.headers['x-forwarded-for']; - } - - logger.log('info', '%s requested: %s', remoteAddress, requestUrl); + logger.log('info', '%s requested: %s', util.getRemoteAddr(req), requestUrl); if (requestUrl === '/publicConfig.json') { res.setHeader('Content-Type', 'application/javascript'); diff --git a/lib/util.js b/lib/util.js index d186e55..27ef08c 100644 --- a/lib/util.js +++ b/lib/util.js @@ -174,3 +174,8 @@ exports.unfurlSRV = function(hostname, port, callback) { callback(records[0].name, records[0].port); }) }; + +exports.getRemoteAddr = function(req) { + let remoteAddress = req.headers['cf-connecting-ip'] || req.headers['x-forwarded-for'] || req.connection.remoteAddress; + return remoteAddress; +}; \ No newline at end of file