in reply to Re^3: mod-perl2 re-using $ENV{REMOTE_ADDR}
in thread mod-perl2 re-using $ENV{REMOTE_ADDR}
I'm glad you have something that appears to be working for you. However I find the logic somewhat confusing, and somewhat perverse.
I think the intention is to find a valid external IP, or failing that to fall back to any valid internal IP. If that is indeed the intention, I think it could be expressed more clearly with something like:
my($valid_ip, $valid_external); foreach my $check (@IPs) { # clean it up $check =~ s/^\s*(.*?)\s*$/\1/; # skip if not a valid IP next unless is_ip($check); # overwrite with the last IP we saw that's valid $valid_ip = $check; # skip unless it's an external IP next if (not is_public_ip($check)) or (is_loopback_ip($check)) or ($check eq "41.79.21.90"); # overwrite with the last valid IP that is external $valid_external = $check; } # choose last external IP if we found one, else last valid IP my $ip = $valid_external // $valid_ip;
Your current code favours the last external IP rather than the first one, and the last internal IP rather than the first one, but I cannot tell if either of those choices are intentional. My proposed code honours that preference, but if you actually want the _first_ external IP from the list you could add last; at the end of the loop to short-circuit it; if you want the first internal IP from the list, you can replace $valid_ip = $check with $valid_ip //= $check.
Side-note: the //g option on the cleanup regexp serves no useful purpose, so I removed it.
Hope this helps.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: mod-perl2 re-using $ENV{REMOTE_ADDR}
by bbs2web (Acolyte) on May 03, 2023 at 19:32 UTC |