'use client'; import React, { useState, useEffect } from 'react'; import { Button } from '@/components/ui/button'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; import { Badge } from '@/components/ui/badge'; import { Alert, AlertDescription } from '@/components/ui/alert'; import { Loader2, Database, Eye, RefreshCw, AlertTriangle, CheckCircle } from 'lucide-react'; interface VercelIPInfo { smartDetection: { detectedIP: string; confidence: 'high' | 'medium' | 'low'; source: string; isPublicIP: boolean; allCandidates: string[]; isInfrastructureIP?: boolean; isUserRealIP?: boolean; infrastructureIPs?: string[]; userRealIPs?: string[]; }; headers: Record; nextRequestIP: string | undefined; env: Record; allHeaders: Record; } export default function VercelIPDebugPage() { const [ipInfo, setIpInfo] = useState(null); const [loading, setLoading] = useState(false); const [message, setMessage] = useState(''); const [error, setError] = useState(''); // 獲取 IP 調試信息 const fetchIPInfo = async () => { try { setLoading(true); const response = await fetch('/api/debug-ip'); const data = await response.json(); if (data.success) { setIpInfo(data.data); if (data.data.smartDetection) { const smartDetection = data.data.smartDetection; setMessage(`Vercel IP 偵測結果: ${smartDetection.detectedIP} (可信度: ${smartDetection.confidence}, 來源: ${smartDetection.source})`); } else { setMessage('IP 調試信息更新成功'); } setError(''); } else { setError(data.error || '獲取 IP 調試信息失敗'); } } catch (err) { setError('網路錯誤: ' + (err instanceof Error ? err.message : '未知錯誤')); } finally { setLoading(false); } }; // 組件載入時獲取 IP 信息 useEffect(() => { fetchIPInfo(); }, []); // 檢查是否為基礎設施 IP const isInfrastructureIP = (ip: string): boolean => { if (!ip) return false; return ip.includes('ec2-') && ip.includes('.amazonaws.com'); }; // 檢查是否為用戶真實 IP const isUserRealIP = (ip: string): boolean => { if (!ip || ip === 'unknown') return false; return !isInfrastructureIP(ip) && ip !== '::1' && ip !== '127.0.0.1'; }; return (

Vercel IP 調試工具

專門用於調試 Vercel 部署環境中的 IP 偵測問題

{/* 智能偵測結果 */} {ipInfo?.smartDetection && ( 智能 IP 偵測結果 系統智能偵測的客戶端 IP 地址(已過濾基礎設施 IP)
{ipInfo.smartDetection.detectedIP} 可信度: {ipInfo.smartDetection.confidence} 來源: {ipInfo.smartDetection.source}

用戶真實 IP:

{ipInfo.smartDetection.userRealIPs?.map((ip, index) => ( {ip} )) || }

基礎設施 IP (已過濾):

{ipInfo.smartDetection.infrastructureIPs?.map((ip, index) => ( {ip} )) || }
)} {/* 請求標頭分析 */} {ipInfo && ( 請求標頭分析 分析各種 HTTP 標頭中的 IP 信息
{Object.entries(ipInfo.headers).map(([key, value]) => { if (!value) return null; const isInfra = isInfrastructureIP(value); const isUser = isUserRealIP(value); return (
{key}:
{value}
{isUser && 用戶真實 IP} {isInfra && 基礎設施 IP}
); })}
)} {/* 環境信息 */} {ipInfo && ( 部署環境信息 Vercel 部署環境相關信息
{Object.entries(ipInfo.env).map(([key, value]) => (
{key}: {value || 'undefined'}
))}
)} {/* 問題診斷 */} 問題診斷 {ipInfo?.smartDetection && ( <> {isInfrastructureIP(ipInfo.smartDetection.detectedIP) && ( 問題: 偵測到的是 Vercel/AWS 基礎設施 IP,不是用戶真實 IP。
解決方案: 檢查 x-forwarded-for 標頭中是否包含用戶真實 IP。
)} {isUserRealIP(ipInfo.smartDetection.detectedIP) && ( 正常: 成功偵測到用戶真實 IP。 )} {!isUserRealIP(ipInfo.smartDetection.detectedIP) && !isInfrastructureIP(ipInfo.smartDetection.detectedIP) && ( 問題: 無法偵測到有效的用戶 IP。
可能原因: 代理配置問題或網路環境限制。
)} )}
{/* 使用說明 */} 使用說明

基礎設施 IP: Vercel/AWS 的服務器 IP,不是用戶真實 IP

用戶真實 IP: 實際訪問網站的用戶 IP 地址

x-forwarded-for: 通常包含用戶真實 IP,格式為 "用戶IP,代理IP"

問題排查: 如果偵測到基礎設施 IP,檢查代理配置

{/* 訊息顯示 */} {message && ( {message} )} {error && ( {error} )}
); }