'use client' import { useState, useEffect } from 'react' import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card' import { Badge } from '@/components/ui/badge' import { Button } from '@/components/ui/button' import { Alert, AlertDescription } from '@/components/ui/alert' import { Globe, RefreshCw, CheckCircle, AlertCircle, Info } from 'lucide-react' interface IpTestResult { originalIp: string cleanedIp: string isIPv4: boolean isIPv6: boolean isLocalhost: boolean description: string } export default function IpFormatTestPage() { const [testResults, setTestResults] = useState([]) const [loading, setLoading] = useState(false) // 測試IP地址清理函數 function cleanIpForDisplay(ip: string): string { if (!ip) return '127.0.0.1'; // 移除空白字符 ip = ip.trim(); // 處理IPv6格式的IPv4地址 (例如: ::ffff:192.168.1.1) if (ip.startsWith('::ffff:')) { return ip.substring(7); } // 處理純IPv6本地回環地址 if (ip === '::1') { return '127.0.0.1'; } // 驗證是否為有效的IPv4地址 const ipv4Regex = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/; if (ipv4Regex.test(ip)) { return ip; } // 如果不是有效的IPv4,返回默認值 return '127.0.0.1'; } // 檢查IP類型 function analyzeIp(ip: string): { isIPv4: boolean; isIPv6: boolean; isLocalhost: boolean; description: string } { const ipv4Regex = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/; const ipv6Regex = /^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$/; const isIPv4 = ipv4Regex.test(ip); const isIPv6 = ipv6Regex.test(ip) || ip.startsWith('::ffff:') || ip === '::1'; const isLocalhost = ip === '127.0.0.1' || ip === '::1' || ip === 'localhost'; let description = ''; if (ip.startsWith('::ffff:')) { description = 'IPv6格式的IPv4地址'; } else if (ip === '::1') { description = 'IPv6本地回環地址'; } else if (isIPv4) { description = '標準IPv4地址'; } else if (isIPv6) { description = 'IPv6地址'; } else { description = '無效的IP地址格式'; } return { isIPv4, isIPv6, isLocalhost, description }; } const runTests = () => { setLoading(true); const testIps = [ '::ffff:127.0.0.1', '::1', '127.0.0.1', '192.168.1.1', '::ffff:192.168.1.100', '2001:db8::1', 'invalid-ip', 'localhost', '::ffff:203.0.113.1', '10.0.0.1' ]; const results: IpTestResult[] = testIps.map(originalIp => { const cleanedIp = cleanIpForDisplay(originalIp); const analysis = analyzeIp(originalIp); return { originalIp, cleanedIp, ...analysis }; }); setTestResults(results); setLoading(false); }; useEffect(() => { runTests(); }, []); return (

IPv4 格式測試

測試IP地址清理和IPv4格式轉換功能

{/* 說明 */}
測試目的
• 驗證IPv6格式的IPv4地址能正確轉換為IPv4
• 確保所有IP地址都顯示為標準IPv4格式
• 測試各種IP地址格式的處理邏輯
• 驗證本地回環地址的正確處理
{/* 測試結果 */} IP格式轉換測試結果 各種IP地址格式的清理和轉換結果
{testResults.map((result, index) => (
原始IP: {result.originalIp}
清理後: {result.cleanedIp}
{result.isIPv4 ? ( ) : ( )} IPv4: {result.isIPv4 ? '是' : '否'}
{result.isIPv6 ? ( ) : ( - )} IPv6: {result.isIPv6 ? '是' : '否'}
{result.isLocalhost ? ( ) : ( - )} 本地: {result.isLocalhost ? '是' : '否'}
{result.description}
))}
{/* 操作按鈕 */}
{/* 總結 */} 測試總結 IPv4格式轉換的關鍵點

✅ 正確處理的格式

  • ::ffff:127.0.0.1127.0.0.1
  • ::1127.0.0.1
  • 127.0.0.1127.0.0.1 (保持不變)
  • 192.168.1.1192.168.1.1 (保持不變)

⚠️ 無效格式處理

  • • 無效IP地址 → 127.0.0.1 (默認值)
  • • 空值或null → 127.0.0.1 (默認值)

🎯 目標

確保所有顯示的IP地址都是標準的IPv4格式,提供一致且易於理解的用戶體驗。

) }