"use client" import { useEffect, useState } from "react" import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card" import { Badge } from "@/components/ui/badge" import { Button } from "@/components/ui/button" import { RefreshCw, Shield, AlertTriangle, CheckCircle, Lock } from "lucide-react" interface IpInfo { ip: string isAllowed: boolean enableIpWhitelist: boolean allowedIps: string[] timestamp: string } export default function IpBlockingTestPage() { const [ipInfo, setIpInfo] = useState(null) const [loading, setLoading] = useState(true) const [error, setError] = useState(null) const fetchIpInfo = async () => { setLoading(true) setError(null) try { const response = await fetch('/api/ip') if (!response.ok) { throw new Error('無法獲取IP信息') } const data = await response.json() setIpInfo(data) } catch (error) { console.error("無法獲取IP信息:", error) setError("無法獲取IP信息") } finally { setLoading(false) } } useEffect(() => { fetchIpInfo() }, []) if (loading) { return (
載入中...
) } if (error || !ipInfo) { return (
錯誤

{error}

) } return (

IP 阻擋測試

{/* IP狀態卡片 */} {ipInfo.enableIpWhitelist && !ipInfo.isAllowed ? ( <> IP 被阻擋 ) : ( <> IP 允許訪問 )}

{ipInfo.ip}

{ipInfo.isAllowed ? "允許" : "阻擋"}
{ipInfo.enableIpWhitelist ? "已啟用" : "未啟用"}
{/* 允許的IP列表 */} {ipInfo.enableIpWhitelist && ipInfo.allowedIps.length > 0 && ( 允許的IP列表
{ipInfo.allowedIps.map((ip, index) => (
{ip === ipInfo.ip ? "當前" : ""} {ip}
))}
)} {/* 阻擋測試說明 */} IP 阻擋測試說明

✅ 如果你能看到這個頁面

說明你的IP ({ipInfo.ip}) 在白名單中,可以正常訪問網站。

⚠️ 測試阻擋功能

要測試IP阻擋功能,可以:

  • 暫時從 .env.local 的 ALLOWED_IPS 中移除你的IP
  • 重啟應用後訪問網站
  • 應該會看到403禁止訪問頁面
  • 記得測試完成後將IP加回白名單

🚫 被阻擋的訪問

如果IP不在白名單中,訪問者會看到:

  • 403 Forbidden 錯誤頁面
  • 顯示被阻擋的IP地址
  • 無法訪問任何網頁內容
  • 只有IP檢測API可以訪問
{/* 當前配置 */} 當前配置
白名單狀態: {ipInfo.enableIpWhitelist ? "已啟用" : "未啟用"}
當前IP: {ipInfo.ip}
訪問權限: {ipInfo.isAllowed ? "允許" : "阻擋"}
) }