This is really great, many many thanks!
So ideally:
I would like to order the IPs by those present in the HTTP_X_FORWARDED_FOR header, failing which it should use REMOTE_ADDR.
I would like to prefer the first public IP, alternatively the last valid private IP (closest to the client).
Your loop is much cleaner and easier to understand, avoids the double negative that I was doing (unshifting REMOTE_ADDR to the beginning of the array and then selecting the last entry). The problem that you high-lighted with my loop also explains the odd case where some small WISPs were transparently natting HTTP behind fake public IPs in their network.
use Data::Validate::IP qw(is_ip is_public_ip is_loopback_ip);
my($valid_ip, $valid_public);
my @IPs;
if (defined $ENV{HTTP_X_FORWARDED_FOR}) {
@IPs=split /,/, $ENV{HTTP_X_FORWARDED_FOR};
};
push(@IPs, $ENV{REMOTE_ADDR});
foreach my $check (@IPs) {
# trim whitespaces
$check =~ s/\s//g;
# skip if not a valid IP
next unless is_ip($check);
# overwrite with last valid IP
#$valid_ip //= $check; # select first valid ip
$valid_ip = $check;
# skip unless it's a public, loopback or proxy IP
next if (not is_public_ip($check))
or (is_loopback_ip($check))
or ($check eq "192.0.2.90");
# overwrite with the last valid public IP
$valid_public = $check;
# select first public IP
last;
}
# select public IP, if we found one, else last valid IP
my $ip = $valid_public // $valid_ip;
print "Content-type: text/html\nCache-Control: max-age=0,no-cache,no-s
+tore,post-check=0,pre-check=0\n\n<html><head><title>Current IP Check<
+/title></head><body>Current IP Address: $ip</body></html>";
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.