Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | "use client"; import { useState, useEffect, useCallback } from "react"; import { Icon } from "@/components/ui/icons/Icon"; interface BlockedIp { id: string; ipAddress: string; reason: string; expiresAt: Date; createdAt: Date; } export function BlockedIpList() { const [blockedIps, setBlockedIps] = useState<BlockedIp[]>([]); const [loading, setLoading] = useState(true); const [error, setError] = useState<string | null>(null); const [showAddForm, setShowAddForm] = useState(false); const [formData, setFormData] = useState({ ipAddress: "", reason: "", durationHours: 24, }); const [submitting, setSubmitting] = useState(false); const fetchBlockedIps = useCallback(async () => { try { const response = await fetch("/api/admin/security/blocked-ips"); if (!response.ok) throw new Error("Failed to fetch blocked IPs"); const result = await response.json(); setBlockedIps(result.data.blockedIps); } catch (err) { setError(err instanceof Error ? err.message : "An error occurred"); } finally { setLoading(false); } }, []); useEffect(() => { fetchBlockedIps(); }, [fetchBlockedIps]); const handleBlock = async (e: React.FormEvent) => { e.preventDefault(); setSubmitting(true); try { const response = await fetch("/api/admin/security/blocked-ips", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(formData), }); if (!response.ok) throw new Error("Failed to block IP"); setFormData({ ipAddress: "", reason: "", durationHours: 24 }); setShowAddForm(false); fetchBlockedIps(); } catch (err) { setError(err instanceof Error ? err.message : "Failed to block IP"); } finally { setSubmitting(false); } }; const handleUnblock = async (ipAddress: string) => { if (!confirm(`Are you sure you want to unblock ${ipAddress}?`)) return; try { const response = await fetch("/api/admin/security/blocked-ips", { method: "DELETE", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ ipAddress }), }); if (!response.ok) throw new Error("Failed to unblock IP"); fetchBlockedIps(); } catch (err) { setError(err instanceof Error ? err.message : "Failed to unblock IP"); } }; if (error) { return ( <div className="bg-red-50 dark:bg-red-900/20 border border-red-200 dark:border-red-800 rounded-lg p-4"> <p className="text-red-700 dark:text-red-400">{error}</p> <button onClick={() => { setError(null); fetchBlockedIps(); }} className="mt-2 text-sm text-red-600 dark:text-red-400 hover:underline" > Try again </button> </div> ); } return ( <div className="space-y-4"> {/* Header with Add button */} <div className="flex items-center justify-between"> <h2 className="text-lg font-semibold text-gray-900 dark:text-gray-100"> Blocked IP Addresses </h2> <button onClick={() => setShowAddForm(!showAddForm)} className="flex items-center gap-2 px-4 py-2 bg-red-600 hover:bg-red-700 text-white rounded-lg transition-colors" > <Icon name={showAddForm ? "x" : "plus"} size={18} /> {showAddForm ? "Cancel" : "Block IP"} </button> </div> {/* Add Form */} {showAddForm && ( <form onSubmit={handleBlock} className="bg-white dark:bg-gray-800 rounded-lg shadow-sm border border-gray-200 dark:border-gray-700 p-4 space-y-4" > <div className="grid grid-cols-1 md:grid-cols-3 gap-4"> <div> <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1"> IP Address </label> <input type="text" value={formData.ipAddress} onChange={(e) => setFormData((prev) => ({ ...prev, ipAddress: e.target.value, })) } placeholder="e.g., 192.168.1.1" required className="w-full px-3 py-2 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-lg text-sm text-gray-900 dark:text-gray-100 placeholder-gray-500 dark:placeholder-gray-400" /> </div> <div> <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1"> Reason </label> <input type="text" value={formData.reason} onChange={(e) => setFormData((prev) => ({ ...prev, reason: e.target.value })) } placeholder="Reason for blocking" required className="w-full px-3 py-2 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-lg text-sm text-gray-900 dark:text-gray-100 placeholder-gray-500 dark:placeholder-gray-400" /> </div> <div> <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1"> Duration </label> <select value={formData.durationHours} onChange={(e) => setFormData((prev) => ({ ...prev, durationHours: Number(e.target.value), })) } className="w-full px-3 py-2 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-lg text-sm text-gray-900 dark:text-gray-100" > <option value={1}>1 hour</option> <option value={6}>6 hours</option> <option value={24}>24 hours</option> <option value={72}>3 days</option> <option value={168}>7 days</option> <option value={720}>30 days</option> <option value={8760}>1 year</option> </select> </div> </div> <div className="flex justify-end"> <button type="submit" disabled={submitting} className="flex items-center gap-2 px-4 py-2 bg-red-600 hover:bg-red-700 disabled:bg-red-400 text-white rounded-lg transition-colors" > {submitting ? ( <> <div className="animate-spin h-4 w-4 border-2 border-white border-t-transparent rounded-full" /> Blocking... </> ) : ( <> <Icon name="ban" size={18} /> Block IP </> )} </button> </div> </form> )} {/* Blocked IPs List */} <div className="bg-white dark:bg-gray-800 rounded-lg shadow-sm border border-gray-200 dark:border-gray-700 overflow-hidden"> {loading ? ( <div className="p-8 text-center"> <div className="animate-spin h-8 w-8 border-4 border-blue-500 border-t-transparent rounded-full mx-auto" /> <p className="mt-2 text-gray-500 dark:text-gray-400"> Loading blocked IPs... </p> </div> ) : blockedIps.length === 0 ? ( <div className="p-8 text-center"> <Icon name="check-circle" size={48} className="mx-auto text-green-500" /> <p className="mt-2 text-gray-500 dark:text-gray-400"> No blocked IP addresses </p> </div> ) : ( <div className="overflow-x-auto"> <table className="w-full"> <thead className="bg-gray-50 dark:bg-gray-700"> <tr> <th className="px-4 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider"> IP Address </th> <th className="px-4 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider"> Reason </th> <th className="px-4 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider"> Blocked At </th> <th className="px-4 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider"> Expires At </th> <th className="px-4 py-3 text-right text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider"> Actions </th> </tr> </thead> <tbody className="divide-y divide-gray-200 dark:divide-gray-700"> {blockedIps.map((ip) => ( <tr key={ip.id} className="hover:bg-gray-50 dark:hover:bg-gray-700/50" > <td className="px-4 py-3 text-sm font-mono text-gray-900 dark:text-gray-100"> {ip.ipAddress} </td> <td className="px-4 py-3 text-sm text-gray-900 dark:text-gray-100"> {ip.reason} </td> <td className="px-4 py-3 text-sm text-gray-500 dark:text-gray-400"> {new Date(ip.createdAt).toLocaleString()} </td> <td className="px-4 py-3 text-sm text-gray-500 dark:text-gray-400"> {new Date(ip.expiresAt).toLocaleString()} </td> <td className="px-4 py-3 text-right"> <button onClick={() => handleUnblock(ip.ipAddress)} className="inline-flex items-center gap-1 px-3 py-1 text-sm text-green-600 dark:text-green-400 hover:bg-green-50 dark:hover:bg-green-900/20 rounded-lg transition-colors" > <Icon name="lock-outline" size={16} /> Unblock </button> </td> </tr> ))} </tbody> </table> </div> )} </div> </div> ); } export default BlockedIpList; |