新增maxmind数据库本地判断国家,并且将优先级调整至最高

This commit is contained in:
root
2026-06-14 15:29:29 +08:00
parent 72d388f642
commit eab59c1c18
17 changed files with 912 additions and 38 deletions
+8
View File
@@ -248,6 +248,14 @@ $cfDomainOut = shell_exec(escapeshellarg(PHP_BINARY) . ' ' . escapeshellarg($roo
$cfDomainJson = json_decode((string) $cfDomainOut, true);
reg_assert('Cloudflare 域名自动化 Mock 流转', ($cfDomainJson['ok'] ?? false) === true, (string) $cfDomainOut);
$geoOut = shell_exec(escapeshellarg(PHP_BINARY) . ' ' . escapeshellarg($root . '/tools/verify_geoip.php') . ' 2>/dev/null');
$geoJson = json_decode((string) $geoOut, true);
if (($geoJson['tests']['db_file_exists'] ?? false) === true) {
reg_assert('GeoIP 本地国家库', ($geoJson['ok'] ?? false) === true, (string) $geoOut);
} else {
reg_skip('GeoIP 本地国家库', 'mmdb 未安装,运行 php tools/setup_geoip_db.php');
}
// ── 9. 数据库连通(可选) ─────────────────────────────────────
echo "[9] 数据库\n";
if (defined('DB_NAME')) {
+58
View File
@@ -0,0 +1,58 @@
<?php
/**
* 将根目录 GeoLite2-Country.mmdb 移动并重命名到隐蔽路径
*
* php tools/setup_geoip_db.php
*/
$root = dirname(__DIR__);
require_once $root . '/cong.php';
$source = $root . '/GeoLite2-Country.mmdb';
$target = defined('CLOAK_GEOIP_DB_PATH') ? CLOAK_GEOIP_DB_PATH : $root . '/storage/runtime/c7f2a9e1/cc_idx.dat';
$targetDir = dirname($target);
$result = [
'ok' => false,
'source' => $source,
'target' => $target,
'msg' => '',
];
if (is_file($target)) {
$result['ok'] = true;
$result['msg'] = '目标文件已存在,跳过';
echo json_encode($result, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) . "\n";
exit(0);
}
if (!is_file($source)) {
$result['msg'] = '源文件不存在:' . $source;
echo json_encode($result, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) . "\n";
exit(1);
}
if (!is_dir($targetDir)) {
if (!@mkdir($targetDir, 0755, true) && !is_dir($targetDir)) {
$result['msg'] = '无法创建目录:' . $targetDir;
echo json_encode($result, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) . "\n";
exit(1);
}
}
if (!@rename($source, $target)) {
if (!@copy($source, $target)) {
$result['msg'] = '移动/复制失败';
echo json_encode($result, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) . "\n";
exit(1);
}
@unlink($source);
}
$htaccess = $targetDir . '/.htaccess';
if (!is_file($htaccess)) {
@file_put_contents($htaccess, "Deny from all\n");
}
$result['ok'] = true;
$result['msg'] = 'GeoIP 数据库已就绪';
echo json_encode($result, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) . "\n";
+61
View File
@@ -0,0 +1,61 @@
<?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);