Skip to content
This repository was archived by the owner on Feb 17, 2022. It is now read-only.

Commit cc853f5

Browse files
committed
Update README with new order of IP checks
1 parent ebe9280 commit cc853f5

File tree

3 files changed

+21
-16
lines changed

3 files changed

+21
-16
lines changed

README.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,20 @@ The connect-middleware also supports retrieving the ip address under a custom at
5050

5151
It looks for specific headers in the request and falls back to some defaults if they do not exist.
5252

53-
The following is the order we use to determine the user ip from the request.
53+
The user ip is determined by the following order:
5454

5555
1. `X-Client-IP`
5656
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.)
5757
3. `CF-Connecting-IP` (Cloudflare)
58-
4. `True-Client-Ip` (Akamai and Cloudflare)
59-
5. `X-Real-IP` (Nginx proxy/FastCGI)
60-
6. `X-Cluster-Client-IP` (Rackspace LB, Riverbed Stingray)
61-
7. `X-Forwarded`, `Forwarded-For` and `Forwarded` (Variations of #2)
62-
8. `req.connection.remoteAddress`
63-
9. `req.socket.remoteAddress`
64-
10. `req.connection.socket.remoteAddress`
65-
11. `req.info.remoteAddress`
58+
4. `Fastly-Client-Ip` (Fastly CDN and Firebase hosting header when forwared to a cloud function)
59+
5. `True-Client-Ip` (Akamai and Cloudflare)
60+
6. `X-Real-IP` (Nginx proxy/FastCGI)
61+
7. `X-Cluster-Client-IP` (Rackspace LB, Riverbed Stingray)
62+
8. `X-Forwarded`, `Forwarded-For` and `Forwarded` (Variations of #2)
63+
9. `req.connection.remoteAddress`
64+
10. `req.socket.remoteAddress`
65+
11. `req.connection.socket.remoteAddress`
66+
12. `req.info.remoteAddress`
6667

6768
If an IP address cannot be found, it will return `null`.
6869

@@ -96,7 +97,8 @@ To easily generate a new changelog, install [github-changelog-generator](https:/
9697
* Thanks to [@osherx](https://github.com/osherx) for adding the connect-middleware.
9798
* Thanks to [@raunc](https://github.com/raunc) for adding Squid proxy support.
9899
* Thanks to [@fluxsauce](https://github.com/fluxsauce) for adding `CF-Connecting-IP`, `True-Client-IP`, and ES6 support.
100+
* Thanks to [@vishalvijay](https://github.com/vishalvijay) for adding Fastly/Firebase hosting support.
99101

100102
## License
101103

102-
The MIT License (MIT) - 2017
104+
The MIT License (MIT) - 2018

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"X-Client-IP",
1414
"X-Forwarded-For",
1515
"CF-Connecting-IP",
16-
"True-Client-IP"
16+
"Fastly-Client-IP",
17+
"True-Client-IP",
1718
"X-Real-IP",
1819
"X-Cluster-Client-IP",
1920
"X-Forwarded",

src/index.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,15 @@ function getClientIpFromXForwardedFor(value) {
4646
* @returns {string} ip - The IP address if known, defaulting to empty string if unknown.
4747
*/
4848
function getClientIp(req) {
49+
4950
// Server is probably behind a proxy.
5051
if (req.headers) {
52+
5153
// Standard headers used by Amazon EC2, Heroku, and others.
5254
if (is.ip(req.headers['x-client-ip'])) {
5355
return req.headers['x-client-ip'];
5456
}
5557

56-
// Firebase hosting header (When forwared to cloud function)
57-
if (is.ip(req.headers['fastly-client-ip'])) {
58-
return req.headers['fastly-client-ip'];
59-
}
60-
6158
// Load-balancers (AWS ELB) or proxies.
6259
const xForwardedFor = getClientIpFromXForwardedFor(req.headers['x-forwarded-for']);
6360
if (is.ip(xForwardedFor)) {
@@ -71,6 +68,11 @@ function getClientIp(req) {
7168
return req.headers['cf-connecting-ip'];
7269
}
7370

71+
// Fastly and Firebase hosting header (When forwared to cloud function)
72+
if (is.ip(req.headers['fastly-client-ip'])) {
73+
return req.headers['fastly-client-ip'];
74+
}
75+
7476
// Akamai and Cloudflare: True-Client-IP.
7577
if (is.ip(req.headers['true-client-ip'])) {
7678
return req.headers['true-client-ip'];

0 commit comments

Comments
 (0)