Files
CLOAK/tools/verify_geoip.php
T

79 lines
2.8 KiB
PHP
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?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;
}
$tests['normalize_iso2'] = CountryCodeNormalizer::toAlpha2('us') === 'US';
$tests['normalize_name'] = CountryCodeNormalizer::toAlpha2('United States') === 'US';
$tests['allowlist_name'] = CountryAllowlist::isAllowed('United Kingdom', CountryAllowlist::parse('GB,US'));
$tests['allowlist_fullwidth_comma'] = CountryAllowlist::isAllowed('BR', CountryAllowlist::parse('BRCN'));
$tests['allowlist_semicolon'] = CountryAllowlist::isAllowed('GB', CountryAllowlist::parse('US;GB;CA'));
$tests['allowlist_country_name'] = CountryAllowlist::isAllowed('BR', CountryAllowlist::parse('Brazil,China'));
if (!$tests['normalize_iso2'] || !$tests['normalize_name'] || !$tests['allowlist_name']
|| !$tests['allowlist_fullwidth_comma'] || !$tests['allowlist_semicolon'] || !$tests['allowlist_country_name']) {
$ok = false;
}
$tests['is_enabled_empty'] = CountryAllowlist::isEnabled('') === false;
$tests['is_enabled_us'] = CountryAllowlist::isEnabled('US,GB') === true;
if (!$tests['is_enabled_empty'] || !$tests['is_enabled_us']) {
$ok = false;
}
}
echo json_encode([
'ok' => $ok,
'tests' => $tests,
], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) . "\n";
exit($ok ? 0 : 1);