request-ip
Advanced tools
Comparing version 2.1.3 to 2.2.0
"use strict"; | ||
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | ||
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } | ||
var is = require('is_js'); | ||
/** | ||
* Parse x-forwarded-for headers. | ||
* | ||
* @param {string} value - The value to be parsed. | ||
* @return {string|null} First known IP address, if any. | ||
*/ | ||
function getClientIpFromXForwardedFor(value) { | ||
@@ -21,10 +14,4 @@ if (!is.existy(value)) { | ||
throw new TypeError("Expected a string, got \"".concat(_typeof(value), "\"")); | ||
} // x-forwarded-for may return multiple IP addresses in the format: | ||
// "client IP, proxy 1 IP, proxy 2 IP" | ||
// Therefore, the right-most IP address is the IP address of the most recent proxy | ||
// and the left-most IP address is the IP address of the originating client. | ||
// source: http://docs.aws.amazon.com/elasticloadbalancing/latest/classic/x-forwarded-headers.html | ||
// Azure Web App's also adds a port for some reason, so we'll only use the first part (the IP) | ||
} | ||
var forwardedIps = value.split(',').map(function (e) { | ||
@@ -34,3 +21,3 @@ var ip = e.trim(); | ||
if (ip.includes(':')) { | ||
var splitted = ip.split(':'); // make sure we only use this if it's ipv4 (ip:port) | ||
var splitted = ip.split(':'); | ||
@@ -43,25 +30,19 @@ if (splitted.length === 2) { | ||
return ip; | ||
}); // Sometimes IP addresses in this header can be 'unknown' (http://stackoverflow.com/a/11285650). | ||
// Therefore taking the left-most IP address that is not unknown | ||
// A Squid configuration directive can also set the value to "unknown" (http://www.squid-cache.org/Doc/config/forwarded_for/) | ||
}); | ||
return forwardedIps.find(is.ip); | ||
for (var i = forwardedIps.length - 1; i >= 0; i -= 1) { | ||
if (is.ip(forwardedIps[i])) { | ||
return forwardedIps[i]; | ||
} | ||
} | ||
return null; | ||
} | ||
/** | ||
* Determine client IP address. | ||
* | ||
* @param req | ||
* @returns {string} ip - The IP address if known, defaulting to empty string if unknown. | ||
*/ | ||
function getClientIp(req) { | ||
// Server is probably behind a proxy. | ||
if (req.headers) { | ||
// Standard headers used by Amazon EC2, Heroku, and others. | ||
if (is.ip(req.headers['x-client-ip'])) { | ||
return req.headers['x-client-ip']; | ||
} // Load-balancers (AWS ELB) or proxies. | ||
} | ||
var xForwardedFor = getClientIpFromXForwardedFor(req.headers['x-forwarded-for']); | ||
@@ -71,29 +52,20 @@ | ||
return xForwardedFor; | ||
} // Cloudflare. | ||
// @see https://support.cloudflare.com/hc/en-us/articles/200170986-How-does-Cloudflare-handle-HTTP-Request-headers- | ||
// CF-Connecting-IP - applied to every request to the origin. | ||
} | ||
if (is.ip(req.headers['cf-connecting-ip'])) { | ||
return req.headers['cf-connecting-ip']; | ||
} // Fastly and Firebase hosting header (When forwared to cloud function) | ||
} | ||
if (is.ip(req.headers['fastly-client-ip'])) { | ||
return req.headers['fastly-client-ip']; | ||
} // Akamai and Cloudflare: True-Client-IP. | ||
} | ||
if (is.ip(req.headers['true-client-ip'])) { | ||
return req.headers['true-client-ip']; | ||
} // Default nginx proxy/fcgi; alternative to x-forwarded-for, used by some proxies. | ||
} | ||
if (is.ip(req.headers['x-real-ip'])) { | ||
return req.headers['x-real-ip']; | ||
} // (Rackspace LB and Riverbed's Stingray) | ||
// 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 | ||
} | ||
if (is.ip(req.headers['x-cluster-client-ip'])) { | ||
@@ -114,4 +86,7 @@ return req.headers['x-cluster-client-ip']; | ||
} | ||
} // Remote address checks. | ||
if (is.ip(req.headers['x-appengine-user-ip'])) { | ||
return req.headers['x-appengine-user-ip']; | ||
} | ||
} | ||
@@ -134,5 +109,4 @@ if (is.existy(req.connection)) { | ||
return req.info.remoteAddress; | ||
} // AWS Api Gateway + Lambda | ||
} | ||
if (is.existy(req.requestContext) && is.existy(req.requestContext.identity) && is.ip(req.requestContext.identity.sourceIp)) { | ||
@@ -142,16 +116,11 @@ return req.requestContext.identity.sourceIp; | ||
if (is.existy(req.raw)) { | ||
return getClientIp(req.raw); | ||
} | ||
return null; | ||
} | ||
/** | ||
* Expose request IP as a middleware. | ||
* | ||
* @param {object} [options] - Configuration. | ||
* @param {string} [options.attributeName] - Name of attribute to augment request object with. | ||
* @return {*} | ||
*/ | ||
function mw(options) { | ||
// Defaults. | ||
var configuration = is.not.existy(options) ? {} : options; // Validation. | ||
var configuration = is.not.existy(options) ? {} : options; | ||
@@ -158,0 +127,0 @@ if (is.not.object(configuration)) { |
The MIT License (MIT) | ||
Copyright (c) 2018 Petar Bojinov - petarbojinov@gmail.com | ||
Copyright (c) 2022 Petar Bojinov - petarbojinov+github@gmail.com | ||
@@ -5,0 +5,0 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of |
{ | ||
"name": "request-ip", | ||
"version": "2.1.3", | ||
"description": "A small node.js module to retrieve the request's IP address", | ||
"version": "2.2.0", | ||
"description": "A small Node.js module to retrieve the request's IP address", | ||
"keywords": [ | ||
@@ -27,3 +27,7 @@ "request ip", | ||
"ipv4", | ||
"ipv6" | ||
"ipv6", | ||
"fastify", | ||
"x-appengine-user-ip", | ||
"cloudflare", | ||
"Cf-Pseudo-IPv4" | ||
], | ||
@@ -39,3 +43,3 @@ "homepage": "https://github.com/pbojinov/request-ip", | ||
"license": "MIT", | ||
"author": "Petar Bojinov <petarbojinov@gmail.com>", | ||
"author": "Petar Bojinov <petarbojinov+github@gmail.com>", | ||
"contributors": [ | ||
@@ -57,2 +61,3 @@ { | ||
}, | ||
"prettier": "@shopify/prettier-config", | ||
"dependencies": { | ||
@@ -62,9 +67,9 @@ "is_js": "^0.9.0" | ||
"devDependencies": { | ||
"@babel/cli": "^7.0.0-beta.51", | ||
"@babel/core": "^7.0.0-beta.51", | ||
"@babel/preset-env": "^7.0.0-beta.51", | ||
"@babel/cli": "^7.0.0", | ||
"@babel/core": "^7.0.0", | ||
"@babel/preset-env": "^7.0.0", | ||
"@shopify/eslint-plugin": "^41.3.0", | ||
"@shopify/prettier-config": "^1.1.2", | ||
"coveralls": "^3.0.2", | ||
"eslint": "^5.8.0", | ||
"eslint-config-airbnb-base": "^13.1.0", | ||
"eslint-plugin-import": "^2.2.0", | ||
"eslint": "^8.16.0", | ||
"nyc": "^13.1.0", | ||
@@ -71,0 +76,0 @@ "request": "^2.54.0", |
@@ -5,3 +5,3 @@ # request-ip | ||
![](https://nodei.co/npm/request-ip.png?downloads=true&cacheBust=2) | ||
![](https://nodei.co/npm/request-ip.png?downloads=true&cacheBust=3) | ||
@@ -15,2 +15,8 @@ ![](https://travis-ci.org/pbojinov/request-ip.svg?branch=master) | ||
Yarn | ||
``` | ||
yarn add request-ip | ||
``` | ||
npm | ||
```bash | ||
@@ -58,3 +64,3 @@ npm install request-ip --save | ||
1. `X-Client-IP` | ||
2. `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.) | ||
2. `X-Forwarded-For` (Header may return multiple IP addresses in the format: "proxy 1 IP, proxy 2 IP, client IP, ", so we take the the last one.) | ||
3. `CF-Connecting-IP` (Cloudflare) | ||
@@ -66,6 +72,9 @@ 4. `Fastly-Client-Ip` (Fastly CDN and Firebase hosting header when forwared to a cloud function) | ||
8. `X-Forwarded`, `Forwarded-For` and `Forwarded` (Variations of #2) | ||
9. `req.connection.remoteAddress` | ||
10. `req.socket.remoteAddress` | ||
11. `req.connection.socket.remoteAddress` | ||
12. `req.info.remoteAddress` | ||
9. `appengine-user-ip` (Google App Engine) | ||
10. `req.connection.remoteAddress` | ||
11. `req.socket.remoteAddress` | ||
12. `req.connection.socket.remoteAddress` | ||
13. `req.info.remoteAddress` | ||
14. `Cf-Pseudo-IPv4` (Cloudflare fallback) | ||
15. `request.raw` (Fastify) | ||
@@ -93,2 +102,10 @@ If an IP address cannot be found, it will return `null`. | ||
## Building | ||
Compiles the current ES6 code to ES5 using Babel. | ||
``` | ||
npm build | ||
``` | ||
## Release Notes | ||
@@ -98,3 +115,3 @@ | ||
To easily generate a new changelog, install [github-changelog-generator](https://github.com/skywinder/github-changelog-generator) then run `npm run changelog`. | ||
To generate a new changelog, install [github-changelog-generator](https://github.com/skywinder/github-changelog-generator) then run `npm run changelog`. This will require being on Ruby >= 3 | ||
@@ -110,2 +127,2 @@ ## Contributors | ||
The MIT License (MIT) - 2018 | ||
The MIT License (MIT) - 2022 |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
122
0
9989
4
109