'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, CheckCircle, AlertTriangle, RefreshCw, Trash2 } from 'lucide-react'; interface IPDetectionResult { detectedIP: string; confidence: 'high' | 'medium' | 'low'; source: string; isPublicIP: boolean; allCandidates: string[]; } interface ConnectionStatus { ip: string; connectionCount: number; connections: any[]; } export default function AutoIPTestPage() { const [ipDetection, setIpDetection] = useState(null); const [connectionStatus, setConnectionStatus] = useState(null); const [loading, setLoading] = useState(false); const [message, setMessage] = useState(''); const [error, setError] = useState(''); // 自動偵測 IP const detectIP = async () => { try { setLoading(true); const response = await fetch('/api/debug-ip'); const data = await response.json(); if (data.success && data.data.smartDetection) { setIpDetection(data.data.smartDetection); setMessage(`IP 偵測成功: ${data.data.smartDetection.detectedIP}`); setError(''); } else { setError('IP 偵測失敗'); } } catch (err) { setError('IP 偵測錯誤: ' + (err instanceof Error ? err.message : '未知錯誤')); } finally { setLoading(false); } }; // 獲取連線狀態 const getConnectionStatus = async () => { try { setLoading(true); const response = await fetch('/api/ip-cleanup?action=status'); const data = await response.json(); if (data.success) { setConnectionStatus(data.data); setMessage(`連線狀態獲取成功: ${data.data.connectionCount} 個連線`); setError(''); } else { setError('獲取連線狀態失敗'); } } catch (err) { setError('獲取連線狀態錯誤: ' + (err instanceof Error ? err.message : '未知錯誤')); } finally { setLoading(false); } }; // 清理當前 IP 連線 const cleanupConnections = async () => { try { setLoading(true); const response = await fetch('/api/ip-cleanup', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ action: 'cleanup-current' }), }); const data = await response.json(); if (data.success) { setMessage(`清理成功: ${data.message}`); setError(''); // 重新獲取連線狀態 await getConnectionStatus(); } else { setError(data.error || '清理失敗'); } } catch (err) { setError('清理錯誤: ' + (err instanceof Error ? err.message : '未知錯誤')); } finally { setLoading(false); } }; // 組件載入時自動偵測 useEffect(() => { detectIP(); }, []); return (

自動 IP 偵測測試

測試智能 IP 偵測和自動連線清理功能

{/* IP 偵測結果 */} {ipDetection && ( 智能 IP 偵測結果 系統自動偵測到的客戶端 IP 地址
{ipDetection.detectedIP} 可信度: {ipDetection.confidence} 來源: {ipDetection.source}
{ipDetection.allCandidates.length > 1 && (

所有候選 IP:

{ipDetection.allCandidates.map((ip, index) => ( {ip} ))}
)}
)} {/* 連線狀態 */} {connectionStatus && ( 連線狀態 當前 IP 的資料庫連線狀態
IP: {connectionStatus.ip} 0 ? "destructive" : "default"} className="text-lg px-3 py-1" > 連線數: {connectionStatus.connectionCount}
{connectionStatus.connections.length > 0 && (

連線詳情:

{connectionStatus.connections.map((conn, index) => (
ID: {conn.ID} HOST: {conn.HOST} 時間: {conn.TIME}s 狀態: {conn.STATE || 'Sleep'}
))}
)}
)} {/* 操作按鈕 */} 操作 測試自動 IP 偵測和連線清理功能
{/* 使用說明 */} 使用說明

自動偵測: 頁面載入時會自動偵測你的 IP 地址

智能算法: 使用多種方法偵測最準確的 IP 地址

連線檢查: 檢查你的 IP 在資料庫中的連線狀態

自動清理: 點擊「清理我的連線」來清理你的 IP 連線

關閉頁面: 關閉此頁面時會自動觸發連線清理

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