Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Betterer IP address detection. #449

Merged
merged 1 commit into from Mar 21, 2015
Merged

Betterer IP address detection. #449

merged 1 commit into from Mar 21, 2015

Conversation

@jaswrks
Copy link

@jaswrks jaswrks commented Mar 21, 2015

  • Improves IP address detection.

  • Supports CloudFlare IP forwarding.

  • Supports multiple IPs in a single header.

  • The following lookup order is used by ZenCache. We use the first public IP address found in these headers, starting from the top and working down.

    'HTTP_CF_CONNECTING_IP',
    'HTTP_CLIENT_IP',
    'HTTP_X_FORWARDED_FOR',
    'HTTP_X_FORWARDED',
    'HTTP_X_CLUSTER_CLIENT_IP',
    'HTTP_FORWARDED_FOR',
    'HTTP_FORWARDED',
    'HTTP_VIA',
    'REMOTE_ADDR',
    
  • Supports a filter in case a site owner would like to revert to the old behavior.

    Create this directory and file:
    /wp-content/mu-plugins/zc-prioritize-remote-addr.php

    <?php
    add_filter('zencache\\share::current_ip_prioritize_remote_addr', '__return_true');
@jaswrks
Copy link
Author

@jaswrks jaswrks commented Mar 21, 2015

There is also a filter that can be used to customize the lookup order and/or add/remove sources that are searched when looking for the current IP address.

From the ZenCache source code we find this:

$sources = array(
  'HTTP_CF_CONNECTING_IP',
  'HTTP_CLIENT_IP',
  'HTTP_X_FORWARDED_FOR',
  'HTTP_X_FORWARDED',
  'HTTP_X_CLUSTER_CLIENT_IP',
  'HTTP_FORWARDED_FOR',
  'HTTP_FORWARDED',
  'HTTP_VIA',
  'REMOTE_ADDR',
);
$sources = $this->apply_filters(__METHOD__.'_sources', $sources);

So to modify the list of sources create this directory and file:

/wp-content/mu-plugins/zc-ip-sources.php

<?php
add_filter('zencache\\share::current_ip_sources', function($sources)
{
    // A source is an array key in the `$_SERVER` superglobal.
    return array(
     'HTTP_CF_CONNECTING_IP',
     'HTTP_CLIENT_IP',
     'HTTP_X_FORWARDED_FOR',
     'HTTP_X_FORWARDED',
     'HTTP_X_CLUSTER_CLIENT_IP',
     'HTTP_FORWARDED_FOR',
     'HTTP_FORWARDED',
     'HTTP_VIA',
     'REMOTE_ADDR',
    );
});
@raamdev raamdev added this to the Next Release milestone Mar 21, 2015
@raamdev raamdev merged commit 2bd1f78 into 000000-dev Mar 21, 2015
@raamdev
Copy link
Contributor

@raamdev raamdev commented Mar 21, 2015

Merged. Thank you!

@raamdev raamdev deleted the feature/#429 branch Mar 21, 2015
@raamdev
Copy link
Contributor

@raamdev raamdev commented Mar 21, 2015

Supports a filter in case a site owner would like to revert to the old behavior.

@jaswsinc What is the old behavior?

There is also a filter that can be used to customize the lookup order

Thank you! I'll create a KB Article to document this.

@jaswrks
Copy link
Author

@jaswrks jaswrks commented Mar 21, 2015

@raamdev

What is the old behavior?

Old Behavior

To use $_SERVER['REMOTE_ADDR'], and only that value.

New Behavior w/ Filter to Revert

The filter I provided above will not replicate that exactly, but it is very very close.

<?php
add_filter('zencache\\share::current_ip_prioritize_remote_addr', '__return_true');

With this filter in place, if $_SERVER['REMOTE_ADDR'] is set and it's a public IP, we will use that without checking anything else; i.e., attempting to replicate the old behavior.

However, if $_SERVER['REMOTE_ADDR'] is empty, or it's not a public IP, we will still skip it and continue searching through the new list of sources. So the filter is very very close to the old behavior.

@raamdev
Copy link
Contributor

@raamdev raamdev commented Mar 21, 2015

@jaswsinc Perfect. Thanks for explaining that! I document that in a KB Article.

@raamdev
Copy link
Contributor

@raamdev raamdev commented Mar 24, 2015

Next release changelog:

  • Enhancement: Improvements to IP address detection, including added support for CloudFlare IP forwarding, multiple IPs in a single header, and the ability to customize the lookup order and/or add/remove sources that are searched when looking for the current IP address. It's also possible to revert to the old IP address detection behavior (see How do I customize remote IP detection?). Issue #449
@raamdev
Copy link
Contributor

@raamdev raamdev commented Mar 24, 2015

@jaswsinc I wrote a KB Article to document your notes above. Thanks for explaining so clearly! :-) http://zencache.com/kb-article/how-do-i-customize-ip-address-detection/

@jaswrks
Copy link
Author

@jaswrks jaswrks commented Mar 24, 2015

@raamdev writes...

I wrote a KB Article to document your notes above. Thanks for explaining so clearly! :-) http://zencache.com/kb-article/how-do-i-customize-ip-address-detection/

Cool! Great :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

2 participants