Files
firegex-traffic-viewer/frontend/src/components/NFRegex/ServiceRow/index.tsx

153 lines
8.2 KiB
TypeScript
Raw Normal View History

2024-10-13 01:50:52 +02:00
import { ActionIcon, Badge, Box, Divider, Grid, Menu, Space, Title, Tooltip } from '@mantine/core';
2023-09-24 05:48:54 +02:00
import { useState } from 'react';
2022-07-07 21:08:02 +02:00
import { FaPlay, FaStop } from 'react-icons/fa';
2023-09-24 05:48:54 +02:00
import { nfregex, Service, serviceQueryKey } from '../utils';
2025-02-12 22:24:59 +01:00
import { MdDoubleArrow, MdOutlineArrowForwardIos } from "react-icons/md"
2022-07-21 10:20:54 +02:00
import YesNoModal from '../../YesNoModal';
2023-10-12 20:01:48 +02:00
import { errorNotify, isMediumScreen, okNotify, regex_ipv4 } from '../../../js/utils';
2022-07-07 21:08:02 +02:00
import { BsTrashFill } from 'react-icons/bs';
2022-06-30 15:58:03 +02:00
import { BiRename } from 'react-icons/bi'
2022-07-08 13:13:30 +02:00
import RenameForm from './RenameForm';
import { MenuDropDownWithButton } from '../../MainLayout';
2023-09-24 05:48:54 +02:00
import { useQueryClient } from '@tanstack/react-query';
2025-02-12 22:24:59 +01:00
import { FaFilter } from "react-icons/fa";
import { VscRegex } from "react-icons/vsc";
2022-06-11 21:57:50 +02:00
2025-02-12 22:24:59 +01:00
export default function ServiceRow({ service, onClick }:{ service:Service, onClick?:()=>void }) {
2022-06-11 21:57:50 +02:00
let status_color = "gray";
switch(service.status){
case "stop": status_color = "red"; break;
case "active": status_color = "teal"; break;
}
2022-06-12 16:44:54 +02:00
2023-09-24 05:48:54 +02:00
const queryClient = useQueryClient()
2022-06-12 16:44:54 +02:00
const [buttonLoading, setButtonLoading] = useState(false)
2022-06-16 02:25:41 +02:00
const [tooltipStopOpened, setTooltipStopOpened] = useState(false);
2022-06-30 15:58:03 +02:00
const [deleteModal, setDeleteModal] = useState(false)
2022-07-08 13:13:30 +02:00
const [renameModal, setRenameModal] = useState(false)
2023-10-12 20:01:48 +02:00
const isMedium = isMediumScreen()
2022-06-12 16:44:54 +02:00
2022-06-13 10:59:05 +02:00
const stopService = async () => {
2022-06-12 16:44:54 +02:00
setButtonLoading(true)
2022-07-21 10:20:54 +02:00
await nfregex.servicestop(service.service_id).then(res => {
2022-06-13 10:59:05 +02:00
if(!res){
2022-07-07 22:36:56 +02:00
okNotify(`Service ${service.name} stopped successfully!`,`The service on ${service.port} has been stopped!`)
2023-09-24 05:48:54 +02:00
queryClient.invalidateQueries(serviceQueryKey)
2022-06-13 10:59:05 +02:00
}else{
2022-07-07 21:08:02 +02:00
errorNotify(`An error as occurred during the stopping of the service ${service.port}`,`Error: ${res}`)
2022-06-13 10:59:05 +02:00
}
}).catch(err => {
2022-07-07 21:08:02 +02:00
errorNotify(`An error as occurred during the stopping of the service ${service.port}`,`Error: ${err}`)
2022-06-13 10:59:05 +02:00
})
setButtonLoading(false);
2022-06-12 16:44:54 +02:00
}
2022-06-13 10:59:05 +02:00
const startService = async () => {
2022-06-12 16:44:54 +02:00
setButtonLoading(true)
2022-07-21 10:20:54 +02:00
await nfregex.servicestart(service.service_id).then(res => {
2022-06-13 10:59:05 +02:00
if(!res){
2022-07-07 22:36:56 +02:00
okNotify(`Service ${service.name} started successfully!`,`The service on ${service.port} has been started!`)
2023-09-24 05:48:54 +02:00
queryClient.invalidateQueries(serviceQueryKey)
2022-06-13 10:59:05 +02:00
}else{
2022-07-07 21:08:02 +02:00
errorNotify(`An error as occurred during the starting of the service ${service.port}`,`Error: ${res}`)
2022-06-13 10:59:05 +02:00
}
}).catch(err => {
2022-07-07 21:08:02 +02:00
errorNotify(`An error as occurred during the starting of the service ${service.port}`,`Error: ${err}`)
2022-06-13 10:59:05 +02:00
})
2022-06-12 16:44:54 +02:00
setButtonLoading(false)
}
2022-06-30 15:58:03 +02:00
const deleteService = () => {
2022-07-21 10:20:54 +02:00
nfregex.servicedelete(service.service_id).then(res => {
2022-06-30 15:58:03 +02:00
if (!res){
2022-07-07 22:36:56 +02:00
okNotify("Service delete complete!",`The service ${service.name} has been deleted!`)
2023-09-24 05:48:54 +02:00
queryClient.invalidateQueries(serviceQueryKey)
2022-06-30 15:58:03 +02:00
}else
errorNotify("An error occurred while deleting a service",`Error: ${res}`)
}).catch(err => {
errorNotify("An error occurred while deleting a service",`Error: ${err}`)
})
}
2022-06-11 21:57:50 +02:00
return <>
2024-10-13 01:50:52 +02:00
<Box className='firegex__nfregex__rowbox'>
2025-02-12 22:24:59 +01:00
<Box className="firegex__nfregex__row" style={{width:"100%", flexDirection: isMedium?"row":"column"}}>
<Box>
<Box className="center-flex" style={{ justifyContent: "flex-start" }}>
<MdDoubleArrow size={30} style={{color: "white"}}/>
<Title className="firegex__nfregex__name" ml="xs">
2024-06-28 18:03:15 +02:00
{service.name}
</Title>
2024-10-13 01:50:52 +02:00
</Box>
2025-02-12 22:24:59 +01:00
<Box className="center-flex" style={{ gap: 8, marginTop: 15, justifyContent: "flex-start" }}>
<Badge color={status_color} radius="md" size="lg" variant="filled">{service.status}</Badge>
<Badge size="lg" gradient={{ from: 'indigo', to: 'cyan' }} variant="gradient" radius="md" style={{ fontSize: "110%" }}>
:{service.port}
</Badge>
</Box>
{isMedium?null:<Space w="xl" />}
</Box>
2022-06-15 01:23:54 +02:00
2025-02-12 22:24:59 +01:00
<Box className={isMedium?"center-flex":"center-flex-row"}>
2024-10-13 01:50:52 +02:00
<Box className="center-flex-row">
<Badge color={service.ip_int.match(regex_ipv4)?"cyan":"pink"} radius="sm" size="md" variant="filled">{service.ip_int} on {service.proto}</Badge>
2025-02-12 22:24:59 +01:00
<Space h="xs" />
<Box className='center-flex'>
<Badge color="yellow" radius="sm" size="md" variant="filled"><FaFilter style={{ marginBottom: -2}} /> {service.n_packets}</Badge>
<Space w="xs" />
<Badge color="violet" radius="sm" size="md" variant="filled"><VscRegex style={{ marginBottom: -2}} size={13} /> {service.n_regex}</Badge>
</Box>
2024-10-13 01:50:52 +02:00
</Box>
2025-02-12 22:24:59 +01:00
{isMedium?<Space w="xl" />:<Space h="lg" />}
2024-10-13 01:50:52 +02:00
<Box className="center-flex">
<MenuDropDownWithButton>
<Menu.Label><b>Rename service</b></Menu.Label>
<Menu.Item leftSection={<BiRename size={18} />} onClick={()=>setRenameModal(true)}>Change service name</Menu.Item>
<Divider />
<Menu.Label><b>Danger zone</b></Menu.Label>
<Menu.Item color="red" leftSection={<BsTrashFill size={18} />} onClick={()=>setDeleteModal(true)}>Delete Service</Menu.Item>
</MenuDropDownWithButton>
<Space w="md"/>
<Tooltip label="Stop service" zIndex={0} color="red" opened={tooltipStopOpened}>
<ActionIcon color="red" loading={buttonLoading}
onClick={stopService} size="xl" radius="md" variant="filled"
disabled={service.status === "stop"}
aria-describedby="tooltip-stop-id"
onFocus={() => setTooltipStopOpened(false)} onBlur={() => setTooltipStopOpened(false)}
onMouseEnter={() => setTooltipStopOpened(true)} onMouseLeave={() => setTooltipStopOpened(false)}>
<FaStop size="20px" />
</ActionIcon>
</Tooltip>
<Space w="md"/>
<Tooltip label="Start service" zIndex={0} color="teal">
<ActionIcon color="teal" size="xl" radius="md" onClick={startService} loading={buttonLoading}
variant="filled" disabled={!["stop","pause"].includes(service.status)?true:false}>
<FaPlay size="20px" />
</ActionIcon>
</Tooltip>
{isMedium?<Space w="xl" />:<Space w="md" />}
2025-02-12 22:24:59 +01:00
{onClick?<Box className='firegex__service_forward_btn'>
2024-10-13 01:50:52 +02:00
<MdOutlineArrowForwardIos onClick={onClick} style={{cursor:"pointer"}} size={25} />
</Box>:null}
</Box>
2025-02-12 22:24:59 +01:00
</Box>
</Box>
2024-10-13 01:50:52 +02:00
</Box>
2022-06-30 15:58:03 +02:00
<YesNoModal
title='Are you sure to delete this service?'
2022-07-07 21:08:02 +02:00
description={`You are going to delete the service '${service.port}', causing the stopping of the firewall and deleting all the regex associated. This will cause the shutdown of your service! ⚠️`}
2022-06-30 15:58:03 +02:00
onClose={()=>setDeleteModal(false) }
action={deleteService}
opened={deleteModal}
/>
2022-07-08 13:13:30 +02:00
<RenameForm
onClose={()=>setRenameModal(false)}
opened={renameModal}
service={service}
/>
2022-06-11 21:57:50 +02:00
</>
}