Skip to content

Get Cloudflare IPs for use in trust proxy configurations

License

Notifications You must be signed in to change notification settings

sampsakuronen/cloudflare-ip-ranges

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Cloudflare IP ranges

npm CircleCI (all branches) npm Dependents (via libraries.io)

Get Cloudflare IPs for use in trust proxy configurations. You might also be interested in Cloudflare authenticated origin pulls.

This package will be updated if Cloudflare ever decides to change the endpoints for retrieving current IPs. We will most likely never see a breaking change for this package.


Install

Requires node 7 or newer.

npm install --save cloudflare-ip-ranges

Usage

For use in an Express environment please see Express documentation on trust proxies.

It is recommended to use setInterval for updating the IP list periodically.

Directly update trust proxies for an Express app

const cfIPranges = require('cloudflare-ip-ranges')

cfIPranges.updateTrustProxy(app)

setInterval(() => {
  cfIPranges.updateTrustProxy(app)
}, 1000*60*60*12)

Getting a list of IPs and updating trust proxies manually

Useful if you need to list other proxies alongside the Cloudflare ones.

const cfIPranges = require('cloudflare-ip-ranges')

cfIPranges.updateIPs()
  .then((ips) => {
    app.set('trust proxy', ['loopback', ...ips])
  })

API

Method Info
updateIPs By default returns Promise<string[]>. By specifying { versioned: true } as an argument you get an object that contains V4 and V6 separately (Promise<{ V4: string[], V6: string[] }>).
updateTrustProxy Takes an Express app instance as an argument. Returns Promise<void>.

Changelog

Version Release notes
2.1.0 Adds the possibility of directly updating trust proxies for an Express app using updateTrustProxy.
2.0.0 Change default behavior of updateIPs into returning a flat list of IPs.
1.0.1 Add info to documentation about using setInterval for keeping the IPs fresh.
1.0.0 Initial release.