Very much appreciate all the tips!
We had an issue with some transparent proxies (the URL is served as http://), where those sometimes add HTTP_X_FORWARDED_FOR but then detail internal IPs. We subsequently may still wish to refer to the remote_addr if all the IPs in the http_x_forwarded_for list are all not valid. The following appears to be working perfectly now:
use Data::Validate::IP qw(is_ip is_public_ip is_loopback_ip);
my $ip;
my @IPs;
if (defined $ENV{HTTP_X_FORWARDED_FOR}) {
@IPs=split /,/, $ENV{HTTP_X_FORWARDED_FOR};
};
unshift (@IPs, $ENV{REMOTE_ADDR});
foreach $check (@IPs) {
$check =~ s/^\s*(.*?)\s*$/\1/g;
if (is_ip($check)) {
if (defined $ip) {
if ( (not is_public_ip($check))
or (is_loopback_ip($check))
or ($check eq "41.79.21.90") ) {
$check = $ip;
}
}
$ip = $check;
}
}
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.