パッケージの詳細

request-ip

pbojinov8.2mMIT3.3.0

A small Node.js module to retrieve the request's IP address

request ip, ip, address, request

readme

request-ip

A tiny Node.js module for retrieving a request's IP address.

Coverage Status npm version

Installation

Yarn

yarn add request-ip

npm

npm install request-ip --save

Getting Started

const requestIp = require('request-ip');

// inside middleware handler
const ipMiddleware = function(req, res, next) {
    const clientIp = requestIp.getClientIp(req); 
    next();
};

// on localhost you'll see 127.0.0.1 if you're using IPv4 
// or ::1, ::ffff:127.0.0.1 if you're using IPv6

As Connect Middleware

const requestIp = require('request-ip');
app.use(requestIp.mw())

app.use(function(req, res) {
    const ip = req.clientIp;
    res.end(ip);
});

To see a full working code for the middleware, check out the examples folder.

The connect-middleware also supports retrieving the ip address under a custom attribute name, which also works as a container for any future settings.

How It Works

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

The user ip is determined by the following order:

  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 first one.)
  3. CF-Connecting-IP (Cloudflare)
  4. Fastly-Client-Ip (Fastly CDN and Firebase hosting header when forwared to a cloud function)
  5. True-Client-Ip (Akamai and Cloudflare)
  6. X-Real-IP (Nginx proxy/FastCGI)
  7. X-Cluster-Client-IP (Rackspace LB, Riverbed Stingray)
  8. X-Forwarded, Forwarded-For and Forwarded (Variations of #2)
  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)

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

Samples Use Cases

  • Getting a user's IP for geolocation.

Running the Tests

Make sure you have the necessary dev dependencies needed to run the tests:

npm install

Run the integration tests

npm test

Building

Compiles the current ES6 code to ES5 using Babel.

npm build

Release Notes

See the wonderful changelog

To generate a new changelog, install github-changelog-generator then run npm run changelog. This will require being on Ruby >= 3

Contributors

Thank you to all the contributors!

License

The MIT License (MIT) - 2022

更新履歴

Changelog

Unreleased

Full Changelog

Merged pull requests:

3.2.0 (2022-07-07)

Full Changelog

Closed issues:

  • Incompatible with Next.js middleware/Vercel edge functions #63

Merged pull requests:

3.1.0 (2022-07-06)

Full Changelog

v4.0.0 (2022-07-06)

Full Changelog

Merged pull requests:

3.0.2 (2022-06-30)

Full Changelog

v3.0.2 (2022-06-30)

Full Changelog

Closed issues:

  • Bad extract ip from x-forwarded-for #60
  • Wrong Client Ip address been picked in your recent version #57

3.0.1 (2022-06-30)

Full Changelog

3.0.0 (2022-06-30)

Full Changelog

Closed issues:

  • Does it support AdonisJS? #58
  • ::ffff:127.0.0.1 Address from client on same VPN as application #56

Merged pull requests:

v2.2.0 (2022-06-01)

Full Changelog

Closed issues:

  • Add support for Cloudflare's "Cf-Pseudo-IPv4" header (as a last resort) #46
  • Mac address instead of IP #43
  • Add support for fastify #41
  • Client IP not getting captured #36
  • All my ips looks like ::ffff:127.0.0.1 #31

Merged pull requests:

2.1.3 (2018-10-29)

Full Changelog

2.1.2 (2018-10-29)

Full Changelog

Closed issues:

  • Handle Firebase hosting header fastly-client-ip #37

Merged pull requests:

2.1.1 (2018-07-03)

Full Changelog

2.1.0 (2018-07-03)

Full Changelog

Closed issues:

  • Not getting the right ip first time #28
  • Allow using node > 6 #27

Merged pull requests:

2.0.2 (2017-06-26)

Full Changelog

Closed issues:

  • azure web app adds port to x-forwarded-for #29

Merged pull requests:

2.0.1 (2017-03-09)

Full Changelog

Implemented enhancements:

  • ES2015 Support #22

2.0.0 (2017-03-07)

Full Changelog

Closed issues:

  • optimized your code a bit (no need to evalutate every option before choosing first one that matches. just evaluate then return on first match) #15

Merged pull requests:

1.3.0 (2017-03-03)

Full Changelog

Closed issues:

  • Support Cloudflare? #20
  • How to receive IP in client #17

Merged pull requests:

  • Adding support for CF-Connecting-IP and True-Client-IP #21 (fluxsauce)
  • Return once we find something and don't crash if req.headers is undefined #19 (rokob)
  • Ignore 'unknown' ip addresses in X-Forwarded-For header #18 (raunc)

1.2.3 (2016-11-02)

Full Changelog

Closed issues:

  • Are there any security concerns when saving the IP directly to a database? #16
  • I'm not getting local host ip address 127.0.0.1 #14

1.2.2 (2016-01-27)

Full Changelog

1.2.1 (2016-01-27)

Full Changelog

Merged pull requests:

1.2.0 (2016-01-27)

Full Changelog

Merged pull requests:

1.1.4 (2015-07-23)

Full Changelog

Merged pull requests:

  • Add case management where you can not find the IP address #10 (sitexw)

1.1.3 (2015-04-20)

Full Changelog

1.1.2 (2015-04-04)

Full Changelog

1.1.1 (2015-04-04)

Full Changelog

Closed issues:

  • needs semver #7

1.1.0 (2015-04-04)

Full Changelog

Merged pull requests:

v0.0.4 (2015-01-16)

Full Changelog

Closed issues:

  • Invalid header #5
  • replace req.header('X-Forwarded-For') for req.header('X-Forwarder-For')); #3
  • Nginx problems #2

Merged pull requests:

* This Changelog was automatically generated by github_changelog_generator