2017 © Pedro Peláez
 

library cf-ip-rewrite

Library to rewrite CloudFlare IP Addresses to the end-user IP address

image

cloudflare/cf-ip-rewrite

Library to rewrite CloudFlare IP Addresses to the end-user IP address

  • Tuesday, October 10, 2017
  • by dfritsch
  • Repository
  • 26 Watchers
  • 6 Stars
  • 6,614 Installations
  • PHP
  • 1 Dependents
  • 1 Suggesters
  • 5 Forks
  • 2 Open issues
  • 8 Versions
  • 9 % Grown

The README.md

Cloudflare PHP IP Rewriting

This module makes it easy for developers to add rewrite Cloudflare IP Addresses for actual end-user IP Addresses at the application layer. It is recommended to either install mod_cloudflare for Apache or use nginx rewrite rules (https://support.cloudflare.com/hc/en-us/articles/200170706-Does-CloudFlare-have-an-IP-module-for-Nginx-) if possible., (*1)

For those cases, where the IP can not be guaranteed to be rewritten by one of these alternate means, this module can be used to rewrite the IP address., (*2)

How it works

    $ipRewrite = new CloudFlare\IpRewrite();
    $is_cf = $ipRewrite->isCloudFlare();
    $rewritten_ip = $ipRewrite->getRewrittenIP();
    $original_ip = $ipRewrite->getOriginalIP();

The class exposes three methods for interaction and a constructor., (*3)

Initializing IpRewrite() object will try to rewrite the IP. If the IP is rewritten, $_SERVER["REMOTE_ADDR"] will be updated to reflect the end-user's IP address., (*4)

isCloudFlare(); returns true if the CF_CONNECTING_IP header is present in the request and the request originates from a Cloudflare IP., (*5)

getRewrittenIP() Returns the rewritten ip address if a rewrite occurs, otherwise it will return null., (*6)

getOriginalIP() returns the saved original ip address from $_SERVER["REMOTE_ADDR"]., (*7)

Best Pratice

    // Initialize object to rewrite the headers
    try {
        $ipRewrite = new CloudFlare\IpRewrite();
    } catch (RuntimeException $e) {
        // PHP configurations does not support IPv6
    }

    // Check if the request is from Cloudflare
    $is_cf = $ipRewrite->isCloudFlare();
    if ($is_cf) {
        // Get original or rewritten ip
        // Order does not matter
        ...
        $rewritten_ip = $ipRewrite->getRewrittenIP();
        ...
        $original_ip = $ipRewrite->getOriginalIP();
        ...
    }

Caution

Rewrite action is triggered only once in constructor. If getRewrittenIP() or getOriginalIP() is called multiple times it'll return the first result regardless if a change happened after the first call. Since rewrite action was not triggered., (*8)

To get the newest changes a new IpRewrite object should be used., (*9)

Testing this module

This module comes with a set of tests that can be run using phpunit. To run the tests, run composer install on the package and then one of the following commands:, (*10)

Basic Tests

composer test

With code coverage report in coverage folder

vendor/bin/phpunit -c phpunit.xml.dist --coverage-html coverage

The Versions

10/10 2017

dev-master

9999999-dev https://github.com/cloudflare/cf-ip-rewrite

Library to rewrite CloudFlare IP Addresses to the end-user IP address

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

cloudflare ip rewrite

10/10 2017

1.0.4

1.0.4.0 https://github.com/cloudflare/cf-ip-rewrite

Library to rewrite CloudFlare IP Addresses to the end-user IP address

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

cloudflare ip rewrite

27/06 2017

1.0.3

1.0.3.0 https://github.com/cloudflare/cf-ip-rewrite

Library to rewrite CloudFlare IP Addresses to the end-user IP address

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

cloudflare ip rewrite

26/06 2017

dev-thellimist/PI-1193

dev-thellimist/PI-1193 https://github.com/cloudflare/cf-ip-rewrite

Library to rewrite CloudFlare IP Addresses to the end-user IP address

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

cloudflare ip rewrite

17/08 2016

1.0.2

1.0.2.0 https://github.com/cloudflare/cf-ip-rewrite

Library to rewrite CloudFlare IP Addresses to the end-user IP address

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

cloudflare ip rewrite

13/07 2016

1.0.1

1.0.1.0 https://github.com/cloudflare/cf-ip-rewrite

Library to rewrite CloudFlare IP Addresses to the end-user IP address

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

cloudflare ip rewrite

12/07 2016

1.0.0

1.0.0.0 https://github.com/cloudflare/cf-ip-rewrite

Library to rewrite CloudFlare IP Addresses to the end-user IP address

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

cloudflare ip rewrite

07/03 2015

v0.5.0

0.5.0.0

Library to rewrite CloudFlare IP Addresses to the end-user IP address

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

cloudflare ip rewrite