'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 } from 'lucide-react'; interface IPInfo { smartDetection?: { detectedIP: string; confidence: 'high' | 'medium' | 'low'; source: string; allCandidates: string[]; isPublicIP: boolean; }; headers: Record; nextRequestIP: string | undefined; env: Record; allHeaders: Record; } export default function DebugIPPage() { const [ipInfo, setIpInfo] = useState(null); const [loading, setLoading] = useState(false); const [message, setMessage] = useState(''); const [error, setError] = useState(''); const [recommendedIP, setRecommendedIP] = 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; setRecommendedIP(smartDetection.detectedIP); setMessage(`智能偵測結果: ${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 getRecommendedIP = () => { if (!ipInfo) return 'unknown'; // 優先使用智能偵測結果 if (ipInfo.smartDetection) { return ipInfo.smartDetection.detectedIP; } // 回退到舊的邏輯 const { headers, nextRequestIP } = ipInfo; // 優先順序檢查 if (headers['cf-connecting-ip']) return headers['cf-connecting-ip']; if (headers['x-real-ip']) return headers['x-real-ip']; if (headers['x-client-ip']) return headers['x-client-ip']; if (headers['x-cluster-client-ip']) return headers['x-cluster-client-ip']; if (headers['x-forwarded']) return headers['x-forwarded']; if (headers['x-forwarded-for']) { const ips = headers['x-forwarded-for']!.split(',').map(ip => ip.trim()); const publicIPs = ips.filter(ip => ip && ip !== '127.0.0.1' && ip !== '::1' && !ip.startsWith('192.168.') && !ip.startsWith('10.') && !ip.startsWith('172.') ); if (publicIPs.length > 0) return publicIPs[0]; return ips[0]; } if (nextRequestIP && nextRequestIP !== '::1' && nextRequestIP !== '127.0.0.1') { return nextRequestIP; } return 'unknown'; }; return (

IP 調試工具

調試和檢查客戶端 IP 地址獲取

{/* 智能偵測結果 */} {ipInfo?.smartDetection && ( 智能 IP 偵測結果 使用智能算法偵測的客戶端 IP 地址
{ipInfo.smartDetection.detectedIP} 可信度: {ipInfo.smartDetection.confidence} 來源: {ipInfo.smartDetection.source}
{ipInfo.smartDetection.allCandidates.length > 1 && (

所有候選 IP:

{ipInfo.smartDetection.allCandidates.map((ip, index) => ( {ip} ))}
)}
)} {/* 推薦的 IP (回退顯示) */} {ipInfo && !ipInfo.smartDetection && ( 推薦的 IP 地址 系統推薦使用的客戶端 IP 地址
{getRecommendedIP()} 狀態: {getRecommendedIP() === 'unknown' ? "無法識別" : "已識別"}
)} {/* 請求標頭 */} {ipInfo && ( 請求標頭 從 HTTP 請求標頭中獲取的 IP 相關信息
{Object.entries(ipInfo.headers).map(([key, value]) => (
{key}: {value || 'null'}
))}
)} {/* NextRequest IP */} {ipInfo && ( NextRequest IP Next.js 框架提供的 IP 地址
{ipInfo.nextRequestIP || 'undefined'}
)} {/* 環境信息 */} {ipInfo && ( 環境信息 部署環境相關信息
{Object.entries(ipInfo.env).map(([key, value]) => (
{key}: {value || 'undefined'}
))}
)} {/* 所有標頭(用於調試) */} {ipInfo && ( 所有請求標頭 完整的 HTTP 請求標頭信息
{Object.entries(ipInfo.allHeaders).map(([key, value]) => (
{key}: {value}
))}
)} {/* 訊息顯示 */} {message && ( {message} )} {error && ( {error} )} {/* 使用說明 */} 使用說明

推薦的 IP: 系統根據優先順序推薦使用的 IP 地址

請求標頭: 檢查各種代理和負載均衡器設置的 IP 標頭

NextRequest IP: Next.js 框架直接提供的 IP 地址

環境信息: 部署環境相關的配置信息

• 如果推薦的 IP 是 "unknown",請檢查代理設置或網路配置

); }