fix(extensions): IP 归属地管理端测试可绕过启用开关
后台测试此前受 ip_geo_enabled 短路,未启用时结果恒为空;强制 live lookup 并返回可读失败原因。
This commit is contained in:
1 parent
5c84d259e7
commit
3675b7dcef
4 files changed
+78
-21
No files matched your search
@@ -21,33 +21,43 @@ class AKSRT_IP_Geo {
|
||||
/**
|
||||
* 查询 IP 归属地
|
||||
*
|
||||
* @param string $ip IPv4 or IPv6
|
||||
* @return array{country: string, province: string, city: string, raw: array}
|
||||
* @param string $ip IPv4 or IPv6
|
||||
* @param bool $force 管理端测试用:跳过「启用」开关与缓存,仍拒绝私有 IP
|
||||
* @return array{country: string, province: string, city: string, raw: array, reason?: string}
|
||||
*/
|
||||
public function lookup(string $ip): array {
|
||||
public function lookup(string $ip, bool $force = false): array {
|
||||
$config = $this->get_config();
|
||||
|
||||
if (empty($config['ip_geo_enabled'])) {
|
||||
return $this->empty_result();
|
||||
if (!$force && empty($config['ip_geo_enabled'])) {
|
||||
return $this->empty_result('disabled');
|
||||
}
|
||||
|
||||
// 本地/私有 IP 跳过查询
|
||||
if ($this->is_private_ip($ip)) {
|
||||
return $this->empty_result();
|
||||
return $this->empty_result('private_ip');
|
||||
}
|
||||
|
||||
$cache_key = 'aksrt_ip_geo_' . md5($ip);
|
||||
$ttl = max(3600, (int) ($config['ip_geo_cache_ttl'] ?? 86400));
|
||||
$cached = get_transient($cache_key);
|
||||
if ($cached !== false && is_array($cached)) {
|
||||
return $cached;
|
||||
if (!$force) {
|
||||
$cached = get_transient($cache_key);
|
||||
if ($cached !== false && is_array($cached)) {
|
||||
return $cached;
|
||||
}
|
||||
}
|
||||
|
||||
$provider = sanitize_key((string) ($config['ip_addr_sdk'] ?? ($config['ip_geo_provider'] ?? 'null')));
|
||||
$result = $this->query_provider($provider, $ip, $config);
|
||||
|
||||
if (!empty($result['province']) || !empty($result['city'])) {
|
||||
set_transient($cache_key, $result, $ttl);
|
||||
if (!empty($result['province']) || !empty($result['city']) || !empty($result['country'])) {
|
||||
if (!$force) {
|
||||
set_transient($cache_key, $result, $ttl);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
if (empty($result['reason'])) {
|
||||
$result['reason'] = 'provider_empty';
|
||||
}
|
||||
|
||||
return $result;
|
||||
@@ -211,7 +221,11 @@ class AKSRT_IP_Geo {
|
||||
return !filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE);
|
||||
}
|
||||
|
||||
private function empty_result(): array {
|
||||
return ['country' => '', 'province' => '', 'city' => '', 'raw' => []];
|
||||
private function empty_result(string $reason = ''): array {
|
||||
$out = ['country' => '', 'province' => '', 'city' => '', 'raw' => []];
|
||||
if ($reason !== '') {
|
||||
$out['reason'] = $reason;
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user