import { useQuery, useMutation } from "@tanstack/react-query"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { Slider } from "@/components/ui/slider"; import { AlertTriangle, Search, Shield, Globe, MapPin, Building2, ShieldPlus } from "lucide-react"; import { format } from "date-fns"; import { useState } from "react"; import type { Detection } from "@shared/schema"; import { getFlag } from "@/lib/country-flags"; import { apiRequest, queryClient } from "@/lib/queryClient"; import { useToast } from "@/hooks/use-toast"; export default function Detections() { const [searchQuery, setSearchQuery] = useState(""); const [anomalyTypeFilter, setAnomalyTypeFilter] = useState("all"); const [minScore, setMinScore] = useState(0); const [maxScore, setMaxScore] = useState(100); const { toast } = useToast(); // Build query params const queryParams = new URLSearchParams(); queryParams.set("limit", "5000"); if (anomalyTypeFilter !== "all") { queryParams.set("anomalyType", anomalyTypeFilter); } if (minScore > 0) { queryParams.set("minScore", minScore.toString()); } if (maxScore < 100) { queryParams.set("maxScore", maxScore.toString()); } const { data: detections, isLoading } = useQuery({ queryKey: ["/api/detections", anomalyTypeFilter, minScore, maxScore], queryFn: () => fetch(`/api/detections?${queryParams.toString()}`).then(r => r.json()), refetchInterval: 5000, }); const filteredDetections = detections?.filter((d) => d.sourceIp.toLowerCase().includes(searchQuery.toLowerCase()) || d.anomalyType.toLowerCase().includes(searchQuery.toLowerCase()) ); // Mutation per aggiungere a whitelist const addToWhitelistMutation = useMutation({ mutationFn: async (detection: Detection) => { return await apiRequest("POST", "/api/whitelist", { ipAddress: detection.sourceIp, reason: `Auto-added from detection: ${detection.anomalyType} (Risk: ${parseFloat(detection.riskScore).toFixed(1)})` }); }, onSuccess: (_, detection) => { toast({ title: "IP aggiunto alla whitelist", description: `${detection.sourceIp} รจ stato aggiunto alla whitelist con successo.`, }); queryClient.invalidateQueries({ queryKey: ["/api/whitelist"] }); queryClient.invalidateQueries({ queryKey: ["/api/detections"] }); }, onError: (error: any, detection) => { toast({ title: "Errore", description: error.message || `Impossibile aggiungere ${detection.sourceIp} alla whitelist.`, variant: "destructive", }); } }); const getRiskBadge = (riskScore: string) => { const score = parseFloat(riskScore); if (score >= 85) return CRITICO; if (score >= 70) return ALTO; if (score >= 60) return MEDIO; if (score >= 40) return BASSO; return NORMALE; }; const getAnomalyTypeLabel = (type: string) => { const labels: Record = { ddos: "DDoS Attack", port_scan: "Port Scanning", brute_force: "Brute Force", botnet: "Botnet Activity", suspicious: "Suspicious Activity" }; return labels[type] || type; }; return (

Rilevamenti

Anomalie rilevate dal sistema IDS

{/* Search and Filters */}
setSearchQuery(e.target.value)} className="pl-9" data-testid="input-search" />
Risk Score: {minScore} - {maxScore}
0 { setMinScore(min); setMaxScore(max); }} className="flex-1" data-testid="slider-risk-score" /> 100
{/* Detections List */} Rilevamenti ({filteredDetections?.length || 0}) {isLoading ? (
Caricamento...
) : filteredDetections && filteredDetections.length > 0 ? (
{filteredDetections.map((detection) => (
{/* Flag Emoji */} {detection.countryCode && ( {getFlag(detection.country, detection.countryCode)} )} {detection.sourceIp} {getRiskBadge(detection.riskScore)} {getAnomalyTypeLabel(detection.anomalyType)}

{detection.reason}

{/* Geolocation Info */} {(detection.country || detection.organization || detection.asNumber) && (
{detection.country && (
{detection.city ? `${detection.city}, ${detection.country}` : detection.country}
)} {detection.organization && (
{detection.organization}
)} {detection.asNumber && (
{detection.asNumber} {detection.asName && `- ${detection.asName}`}
)}
)}

Risk Score

{parseFloat(detection.riskScore).toFixed(1)}/100

Confidence

{parseFloat(detection.confidence).toFixed(1)}%

Log Count

{detection.logCount}

Rilevato

{format(new Date(detection.detectedAt), "dd/MM HH:mm")}

Prima: {format(new Date(detection.firstSeen), "dd/MM HH:mm:ss")} Ultima: {format(new Date(detection.lastSeen), "dd/MM HH:mm:ss")}
{detection.blocked ? ( Bloccato ) : ( Attivo )}
))}
) : (

Nessun rilevamento trovato

{searchQuery && (

Prova con un altro termine di ricerca

)}
)}
); }