"use client" import { useState, useEffect } from "react" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" import { Switch } from "@/components/ui/switch" import { Shield, ShieldAlert, Globe, Save, RefreshCw } from "lucide-react" import { toast } from "@/components/ui/use-toast" interface IpInfo { ip: string isAllowed: boolean enableIpWhitelist: boolean allowedIps: string[] timestamp: string } export default function IpWhitelistPage() { const [ipInfo, setIpInfo] = useState(null) const [loading, setLoading] = useState(true) const [saving, setSaving] = useState(false) const [enableWhitelist, setEnableWhitelist] = useState(false) const [allowedIps, setAllowedIps] = useState("") useEffect(() => { fetchIpInfo() }, []) const fetchIpInfo = async () => { try { setLoading(true) const response = await fetch('/api/ip') if (!response.ok) { throw new Error('無法獲取IP信息') } const data = await response.json() setIpInfo(data) setEnableWhitelist(data.enableIpWhitelist) setAllowedIps(data.allowedIps.join(', ')) } catch (error) { console.error("無法獲取IP信息:", error) toast({ title: "錯誤", description: "無法獲取IP信息", variant: "destructive", }) } finally { setLoading(false) } } const handleSave = async () => { try { setSaving(true) // 這裡應該調用API來保存設置 // 由於這是前端演示,我們只顯示成功消息 toast({ title: "成功", description: "IP白名單設置已保存", }) } catch (error) { console.error("保存失敗:", error) toast({ title: "錯誤", description: "保存設置失敗", variant: "destructive", }) } finally { setSaving(false) } } if (loading) { return (

載入中...

) } return (

IP 白名單管理

管理允許訪問系統的IP地址

{/* 當前IP狀態 */} 當前IP狀態 {ipInfo && (
IP地址: {ipInfo.ip}
白名單狀態:
{ipInfo.enableIpWhitelist && !ipInfo.isAllowed ? ( ) : ( )} {ipInfo.enableIpWhitelist && !ipInfo.isAllowed ? "拒絕" : "允許"}
檢查時間: {new Date(ipInfo.timestamp).toLocaleString('zh-TW')}
)}
{/* 白名單設置 */} 白名單設置 配置允許訪問的IP地址
setAllowedIps(e.target.value)} placeholder="192.168.1.0/24, 10.0.0.50, 172.16.0.0/16" className="bg-slate-700 border-blue-600 text-white" />

支援格式:單一IP (192.168.1.100)、IP範圍 (192.168.1.0/24)、多個IP用逗號分隔

{/* 使用說明 */} 使用說明

啟用IP白名單:開啟後,只有白名單內的IP才能訪問系統

IP格式支援:

  • • 單一IP:192.168.1.100
  • • IP範圍:192.168.1.0/24 (CIDR格式)
  • • 多個IP:用逗號分隔,如 192.168.1.100, 10.0.0.50

注意:設置更改後需要重新啟動應用程式才能生效

) }