Created
October 30, 2017 22:25
-
-
Save parvez/41eb672967eb8b4c9aeb37e4870a0d82 to your computer and use it in GitHub Desktop.
Sniff NodeJS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.use((req, res, next) => { | |
try { | |
var sniff_data = {} | |
sniff_data.host = req.headers && req.headers.host | |
sniff_data.url = req.url | |
sniff_data.method = req.method | |
sniff_data.user_agent = req.headers && req.headers['user-agent'] | |
sniff_data.ip_address = {} | |
if (req.connection && req.connection.socket && req.connection.socket.remoteAddress) { | |
sniff_data.ip_address.ip = sniff_data.ip_address.socketRemoteAddress = req.connection.socket.remoteAddress | |
} | |
if (req.socket && req.socket.remoteAddress) { | |
sniff_data.ip_address.ip = sniff_data.ip_address.remoteAddress = req.socket.remoteAddress | |
} | |
if (req.connection && req.connection.remoteAddress) { | |
sniff_data.ip_address.ip = sniff_data.ip_address.remoteAddress = req.connection.remoteAddress | |
} | |
if (req.headers['x-forwarded-for']) { | |
sniff_data.ip_address.ip = sniff_data.ip_address['x-forwarded-for'] = req.headers['x-forwarded-for'] | |
} | |
logger.info('SNIFF', JSON.stringify(sniff_data)) | |
next() | |
} catch (err) { | |
logger.error('Fatal', JSON.stringify(err.stack)) | |
res.status(500).json( {'error': 'INTERNAL_SERVER_ERROR'} ) | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment