diff --git a/backend/app/Controllers/AuthController.php b/backend/app/Controllers/AuthController.php index 4d5b380..b4a8e30 100644 --- a/backend/app/Controllers/AuthController.php +++ b/backend/app/Controllers/AuthController.php @@ -53,9 +53,10 @@ class AuthController extends Controller $this->error('请输入用户名或邮箱'); } - // Cloudflare Turnstile 人机验证 + // Cloudflare Turnstile(按系统配置开关与入口白名单) + $portalKey = $portal === 'agent' ? 'agent_login' : 'admin_login'; $turnstileToken = trim((string) ($input['turnstile_token'] ?? '')); - if (!TurnstileService::verify($turnstileToken !== '' ? $turnstileToken : null, clientIp())) { + if (!TurnstileService::verifyFor($portalKey, $turnstileToken !== '' ? $turnstileToken : null, clientIp())) { $this->error('人机验证失败,请完成验证后重试'); } @@ -132,9 +133,9 @@ class AuthController extends Controller $this->error('两次输入的密码不一致'); } - // Cloudflare Turnstile 人机验证 + // Cloudflare Turnstile(按系统配置开关与入口白名单) $turnstileToken = trim((string) ($input['turnstile_token'] ?? '')); - if (!TurnstileService::verify($turnstileToken !== '' ? $turnstileToken : null, clientIp())) { + if (!TurnstileService::verifyFor('agent_register', $turnstileToken !== '' ? $turnstileToken : null, clientIp())) { $this->error('人机验证失败,请完成验证后重试'); } diff --git a/backend/app/Controllers/PublicController.php b/backend/app/Controllers/PublicController.php index 7e22c9f..c69704c 100644 --- a/backend/app/Controllers/PublicController.php +++ b/backend/app/Controllers/PublicController.php @@ -6,6 +6,7 @@ use App\Core\Database; use App\Core\Validator; use App\Services\InstallationService; use App\Services\VersionService; +use App\Services\TurnstileService; /** * 公开接口控制器 @@ -746,6 +747,21 @@ class PublicController extends Controller $this->success(['valid' => false, 'message' => '卡密状态异常'], 'ok'); } + /** + * 人机验证公开配置(前端登录页判断是否展示组件) + * GET /api/public/turnstile-config + * 注意:客户端 verify/activate/check-update 等接口从不校验 Turnstile + */ + public function turnstileConfig(): void + { + $c = \App\Services\TurnstileService::config(); + $this->success([ + 'enabled' => $c['enabled'] === 1 && $c['site_key'] !== '', + 'site_key' => $c['site_key'], + 'endpoints' => $c['endpoints'], + ], 'ok'); + } + /** * 获取公告列表(公开/客户端) * GET /api/public/announcements?target=client&project_id=1&limit=10 diff --git a/backend/app/Services/TurnstileService.php b/backend/app/Services/TurnstileService.php index cd8a634..2923c77 100644 --- a/backend/app/Services/TurnstileService.php +++ b/backend/app/Services/TurnstileService.php @@ -1,13 +1,99 @@ '管理员登录', + 'agent_login' => '代理登录', + 'agent_register'=> '代理注册', + ]; + + /** + * 读取配置缓存 + * @return array{enabled:int, endpoints:array, secret:string, site_key:string} + */ + public static function config(): array + { + static $cache = null; + if ($cache !== null) { + return $cache; + } + + $enabled = true; + $endpoints = array_keys(self::ENDPOINTS); + try { + $db = Database::getInstance(); + $rows = $db->fetchAll( + "SELECT `key`, `value` FROM {$db->table('configs')} WHERE `key` IN ('turnstile_enabled','turnstile_endpoints')" + ); + $map = []; + foreach ($rows as $r) { + $map[$r['key']] = $r['value']; + } + if (isset($map['turnstile_enabled'])) { + $enabled = (string) $map['turnstile_enabled'] === '1'; + } + if (isset($map['turnstile_endpoints']) && trim($map['turnstile_endpoints']) !== '') { + $parts = array_filter(array_map('trim', explode(',', $map['turnstile_endpoints']))); + $endpoints = array_values(array_intersect(array_keys(self::ENDPOINTS), $parts)); + } + } catch (\Throwable $e) { + // 配置表不可用时保持默认:启用且仅登录注册 + } + + $cache = [ + 'enabled' => $enabled ? 1 : 0, + 'endpoints' => $endpoints, + 'secret' => (string) config('app.turnstile.secret_key', ''), + 'site_key' => (string) config('app.turnstile.site_key', ''), + ]; + return $cache; + } + + /** + * 全局是否启用人机验证 + */ + public static function isEnabled(): bool + { + $c = self::config(); + return $c['enabled'] === 1 && $c['secret'] !== ''; + } + + /** + * 指定入口是否需要人机验证 + * @param string $endpoint admin_login|agent_login|agent_register + */ + public static function requiredFor(string $endpoint): bool + { + if (!self::isEnabled()) { + return false; + } + $c = self::config(); + return in_array($endpoint, $c['endpoints'], true); + } + /** * 校验 Turnstile token + * @param string $endpoint 入口标识 + */ + public static function verifyFor(string $endpoint, ?string $token, ?string $remoteIp = null): bool + { + if (!self::requiredFor($endpoint)) { + return true; + } + return self::verify($token, $remoteIp); + } + + /** + * 校验 Turnstile token(强制) * 未配置 secret 时跳过校验(便于本地开发) */ public static function verify(?string $token, ?string $remoteIp = null): bool diff --git a/backend/routes/api.php b/backend/routes/api.php index cdb9b58..d72f216 100644 --- a/backend/routes/api.php +++ b/backend/routes/api.php @@ -47,6 +47,9 @@ $router->group('api/public', function ($router) { // 公告推送(客户端/商城) $router->get('announcements', [PublicController::class, 'announcements']); + // 人机验证公开配置(登录页用;客户端激活/验证接口从不校验) + $router->get('turnstile-config', [PublicController::class, 'turnstileConfig']); + // 支付方式(前台展示用) $router->get('payment/options', [PublicController::class, 'paymentOptions']); diff --git a/frontend/src/components/TurnstileWidget.vue b/frontend/src/components/TurnstileWidget.vue index 154d51e..c33e6b9 100644 --- a/frontend/src/components/TurnstileWidget.vue +++ b/frontend/src/components/TurnstileWidget.vue @@ -6,10 +6,12 @@