Files
firegex-traffic-viewer/frontend/src/pages/ServiceDetails.tsx

112 lines
4.6 KiB
TypeScript
Raw Normal View History

2022-06-13 10:59:05 +02:00
import { ActionIcon, Grid, LoadingOverlay, Space, Title } from '@mantine/core';
2022-06-12 12:09:50 +02:00
import React, { useEffect, useState } from 'react';
2022-06-12 14:40:48 +02:00
import { BsTrashFill } from 'react-icons/bs';
2022-06-12 16:44:54 +02:00
import { useNavigate, useParams } from 'react-router-dom';
2022-06-12 12:09:50 +02:00
import RegexView from '../components/RegexView';
import ServiceRow from '../components/ServiceRow';
2022-06-12 16:44:54 +02:00
import YesNoModal from '../components/YesNoModal';
import { RegexFilter, Service, update_freq } from '../js/models';
2022-06-13 10:59:05 +02:00
import { deleteservice, errorNotify, okNotify, regenport, serviceinfo, serviceregexlist } from '../js/utils';
import { BsArrowRepeat } from "react-icons/bs"
2022-06-12 12:09:50 +02:00
function ServiceDetails() {
const {srv_id} = useParams()
const [serviceInfo, setServiceInfo] = useState<Service>({
id:srv_id?srv_id:"",
internal_port:0,
n_packets:0,
n_regex:0,
name:srv_id?srv_id:"",
public_port:0,
status:"🤔"
})
2022-06-12 16:44:54 +02:00
const [regexesList, setRegexesList] = useState<RegexFilter[]>([])
2022-06-13 10:59:05 +02:00
const [loader, setLoader] = useState(true);
2022-06-12 16:44:54 +02:00
const navigator = useNavigate()
2022-06-12 12:09:50 +02:00
const updateInfo = async () => {
if (!srv_id) return
let error = false;
await serviceinfo(srv_id).then(res => {
setServiceInfo(res)
}).catch(
err =>{
2022-06-12 16:44:54 +02:00
error = true;
navigator("/")
2022-06-12 12:09:50 +02:00
})
if (error) return
await serviceregexlist(srv_id).then(res => {
setRegexesList(res)
}).catch(
2022-06-12 22:28:16 +02:00
err => errorNotify(`Updater for ${srv_id} service failed [Regex list]!`, err.toString())
)
2022-06-13 10:59:05 +02:00
setLoader(false)
2022-06-12 12:09:50 +02:00
}
2022-06-12 22:28:16 +02:00
2022-06-12 12:09:50 +02:00
useEffect(()=>{
updateInfo()
const updater = setInterval(updateInfo, update_freq)
return () => { clearInterval(updater) }
2022-06-13 10:59:05 +02:00
},[]);
2022-06-12 16:44:54 +02:00
const [deleteModal, setDeleteModal] = useState(false)
2022-06-13 10:59:05 +02:00
const [changePortModal, setChangePortModal] = useState(false)
2022-06-12 12:09:50 +02:00
2022-06-13 10:59:05 +02:00
const deleteService = () => {
deleteservice(serviceInfo.id).then(res => {
if (!res)
okNotify("Service delete complete!",`The service ${serviceInfo.id} has been deleted!`)
else
errorNotify("An error occurred while deleting a service",`Error: ${res}`)
}).catch(err => {
errorNotify("An error occurred while deleting a service",`Error: ${err}`)
})
}
const changePort = () => {
regenport(serviceInfo.id).then(res => {
if (!res)
okNotify("Service port regeneration completed!",`The service ${serviceInfo.id} has changed the internal port!`)
else
errorNotify("An error occurred while changing the internal service port",`Error: ${res}`)
}).catch(err => {
errorNotify("An error occurred while changing the internal service port",`Error: ${err}`)
})
}
return <div>
<LoadingOverlay visible={loader} />
2022-06-12 14:40:48 +02:00
<ServiceRow service={serviceInfo} additional_buttons={<>
2022-06-12 16:44:54 +02:00
<ActionIcon color="red" onClick={()=>setDeleteModal(true)} size="xl" radius="md" variant="filled"><BsTrashFill size={22} /></ActionIcon>
2022-06-12 14:40:48 +02:00
<Space w="md"/>
2022-06-13 10:59:05 +02:00
<ActionIcon color="blue" onClick={()=>setChangePortModal(true)} size="xl" radius="md" variant="filled"><BsArrowRepeat size={28} /></ActionIcon>
<Space w="md"/>
2022-06-12 14:40:48 +02:00
</>}></ServiceRow>
2022-06-12 12:09:50 +02:00
{regexesList.length === 0?
2022-06-12 19:07:23 +02:00
<><Space h="xl" /> <Title className='center-flex' order={3}>No regex found for this service! Add one clicking the add button above</Title></>:
2022-06-12 12:09:50 +02:00
<Grid>
{regexesList.map( (regexInfo) => <Grid.Col key={regexInfo.id} span={6}><RegexView regexInfo={regexInfo}/></Grid.Col>)}
</Grid>
}
2022-06-12 16:44:54 +02:00
<YesNoModal
title='Are you sure to delete this service?'
description={`You are going to delete the service '${serviceInfo.id}', causing the stopping of the firewall and deleting all the regex associated. This will cause the shutdown of your service ⚠️!`}
onClose={()=>setDeleteModal(false)}
2022-06-13 10:59:05 +02:00
action={deleteService}
2022-06-12 16:44:54 +02:00
opened={deleteModal}
/>
2022-06-13 10:59:05 +02:00
<YesNoModal
title='Are you sure to change the proxy internal port?'
description={`You are going to change the proxy port '${serviceInfo.internal_port}'. This will cause the shutdown of your service temporarily ⚠️!`}
onClose={()=>setChangePortModal(false)}
action={changePort}
opened={changePortModal}
/>
</div>
2022-06-12 12:09:50 +02:00
}
export default ServiceDetails;