fix(ip-geo): harden full-chain IP geolocation reliability
Capture real client IPs behind CDN, improve provider fallbacks and negative cache, fix admin credential dependsOn for polling mode, and map country labels to ISO codes for moderation GeoIP blocks.
This commit is contained in:
+29
-4
@@ -218,11 +218,29 @@ function aksrt_get_captcha_status_summary(): array {
|
||||
// ============================================================
|
||||
|
||||
function aksrt_get_client_ip(): string {
|
||||
$keys = ['HTTP_X_FORWARDED_FOR', 'HTTP_X_REAL_IP', 'HTTP_CLIENT_IP'];
|
||||
// Prefer CDN / reverse-proxy headers (Cloudflare first), then generic proxies.
|
||||
$keys = [
|
||||
'HTTP_CF_CONNECTING_IP',
|
||||
'HTTP_TRUE_CLIENT_IP',
|
||||
'HTTP_X_FORWARDED_FOR',
|
||||
'HTTP_X_REAL_IP',
|
||||
'HTTP_CLIENT_IP',
|
||||
];
|
||||
foreach ($keys as $key) {
|
||||
if (!empty($_SERVER[$key])) {
|
||||
$parts = explode(',', (string) $_SERVER[$key]);
|
||||
$ip = trim($parts[0]);
|
||||
if (empty($_SERVER[$key])) {
|
||||
continue;
|
||||
}
|
||||
// X-Forwarded-For may be a chain: client, proxy1, proxy2 — take first valid.
|
||||
$parts = explode(',', (string) $_SERVER[$key]);
|
||||
foreach ($parts as $part) {
|
||||
$ip = trim($part);
|
||||
// Strip IPv6 brackets / port noise occasionally injected by proxies.
|
||||
if (str_starts_with($ip, '[') && str_contains($ip, ']')) {
|
||||
$ip = substr($ip, 1, strpos($ip, ']') - 1);
|
||||
} elseif (substr_count($ip, ':') === 1 && str_contains($ip, '.')) {
|
||||
// IPv4:port
|
||||
$ip = explode(':', $ip, 2)[0];
|
||||
}
|
||||
if (filter_var($ip, FILTER_VALIDATE_IP)) {
|
||||
return $ip;
|
||||
}
|
||||
@@ -231,6 +249,13 @@ function aksrt_get_client_ip(): string {
|
||||
return isset($_SERVER['REMOTE_ADDR']) ? (string) $_SERVER['REMOTE_ADDR'] : '0.0.0.0';
|
||||
}
|
||||
|
||||
// Native wp-comments-post + any path that omits comment_author_IP.
|
||||
// Ensures comment_author_IP (and thus aksrt_comment_addr) is the real client IP behind CDN.
|
||||
add_filter('pre_comment_user_ip', static function ($ip) {
|
||||
$resolved = aksrt_get_client_ip();
|
||||
return ($resolved !== '' && $resolved !== '0.0.0.0') ? $resolved : $ip;
|
||||
}, 5);
|
||||
|
||||
// ============================================================
|
||||
// 腾讯云 API 3.0 TC3-HMAC-SHA256 签名
|
||||
// ============================================================
|
||||
|
||||
Reference in New Issue
Block a user