ip-device-parser
v1.0.3
Published
Express middleware to parse ip address and device information from request
Downloads
195
Maintainers
Readme
ip-device-parser
Exress middleware to Parse IP Adress
and Client Device Information
from the request and it will hold following data.
Installation
$ npm install ip-device-parser
API
const express = require('express');
const ipAndDeviceParser = require('ip-device-parser');
const app = express();
app.use(ipAndDeviceParser());
You can fetch ip and device infor form req.clientInfo
where req is express request object
// req.clientInfo
{
agent: {
browser: {
name: 'Chrome Mobile',
version: '67.0.3396'
},
device: {
name: 'OnePlus ONEPLUS A5000',
version: '0.0.0'
},
os: {
name: 'Android',
version: '8.1.0'
}
},
ip: '2xx5:2x5:1xx6:2xx3:2xxc:dxx3:7xx2:6xxa'
}
How It Works
It looks for specific headers in the request and falls back to some defaults if they do not exist.
The following is the order we use to determine the user ip from the request.
X-Client-IP
X-Forwarded-For
(Header may return multiple IP addresses in the format: "client IP, proxy 1 IP, proxy 2 IP", so we take the the first one.)CF-Connecting-IP
(Cloudflare)True-Client-Ip
(Akamai and Cloudflare)X-Real-IP
(Nginx proxy/FastCGI)X-Cluster-Client-IP
(Rackspace LB, Riverbed Stingray)X-Forwarded
,Forwarded-For
andForwarded
(Variations of #2)req.connection.remoteAddress
req.socket.remoteAddress
req.connection.socket.remoteAddress
req.info.remoteAddress
If an IP address cannot be found, it will return null
.
References
http://docs.aws.amazon.com/elasticloadbalancing/latest/classic/x-forwarded-headers.html http://stackoverflow.com/a/11285650 http://www.squid-cache.org/Doc/config/forwarded_for/ https://support.cloudflare.com/hc/en-us/articles/200170986-How-does-Cloudflare-handle-HTTP-Request-headers- http://www.rackspace.com/knowledge_center/article/controlling-access-to-linux-cloud-sites-based-on-the-client-ip-address https://splash.riverbed.com/docs/DOC-1926
Thanks Note
~pbojinov's work on IP address headers and request ip package