62 lines
1.7 KiB
PHP
62 lines
1.7 KiB
PHP
<?php
|
|
/**
|
|
* GeoIP 本地国家库冒烟测试
|
|
*
|
|
* php tools/verify_geoip.php
|
|
*/
|
|
$root = dirname(__DIR__);
|
|
require_once $root . '/cong.php';
|
|
require_once $root . '/lib/bootstrap.php';
|
|
|
|
$tests = [];
|
|
$ok = true;
|
|
|
|
$dbPath = defined('CLOAK_GEOIP_DB_PATH') ? CLOAK_GEOIP_DB_PATH : '';
|
|
$tests['db_file_exists'] = $dbPath !== '' && is_file($dbPath);
|
|
if (!$tests['db_file_exists']) {
|
|
$ok = false;
|
|
}
|
|
|
|
$tests['sdk_available'] = class_exists('GeoIp2\\Database\\Reader');
|
|
if (!$tests['sdk_available']) {
|
|
$ok = false;
|
|
}
|
|
|
|
$tests['resolver_available'] = GeoIpCountryResolver::isAvailable();
|
|
if (!$tests['resolver_available']) {
|
|
$ok = false;
|
|
}
|
|
|
|
$_SERVER['HTTP_CF_CONNECTING_IP'] = '203.0.113.50';
|
|
$resolvedIp = ClientIpResolver::resolve();
|
|
$tests['client_ip_cf'] = ($resolvedIp === '203.0.113.50');
|
|
unset($_SERVER['HTTP_CF_CONNECTING_IP']);
|
|
if (!$tests['client_ip_cf']) {
|
|
$ok = false;
|
|
}
|
|
|
|
if ($tests['resolver_available']) {
|
|
$first = GeoIpCountryResolver::resolveWithMeta('8.8.8.8');
|
|
$second = GeoIpCountryResolver::resolveWithMeta('8.8.8.8');
|
|
$tests['lookup_8_8_8_8'] = ($first['code'] === 'US');
|
|
$tests['cache_second_hit'] = ($second['source'] === 'cache' || $second['source'] === 'request');
|
|
if (!$tests['lookup_8_8_8_8']) {
|
|
$ok = false;
|
|
}
|
|
|
|
$allow = CountryAllowlist::isAllowed('US', CountryAllowlist::parse('US,GB'));
|
|
$deny = CountryAllowlist::isAllowed('CN', CountryAllowlist::parse('US,GB'));
|
|
$tests['allowlist_us'] = $allow === true;
|
|
$tests['allowlist_cn'] = $deny === false;
|
|
if (!$tests['allowlist_us'] || !$tests['allowlist_cn']) {
|
|
$ok = false;
|
|
}
|
|
}
|
|
|
|
echo json_encode([
|
|
'ok' => $ok,
|
|
'tests' => $tests,
|
|
], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) . "\n";
|
|
|
|
exit($ok ? 0 : 1);
|