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

106 lines
3.7 KiB
TypeScript
Raw Normal View History

2022-08-12 16:00:58 +00:00
import { Button, Group, Space, TextInput, Notification, Modal, Switch, SegmentedControl } from '@mantine/core';
2022-06-12 12:09:50 +02:00
import { useForm } from '@mantine/hooks';
2022-08-12 16:00:58 +00:00
import React, { useState } from 'react';
import { okNotify, regex_ipv4, regex_ipv6 } from '../../js/utils';
2022-06-12 12:09:50 +02:00
import { ImCross } from "react-icons/im"
import { nfregex } from './utils';
2022-08-12 16:00:58 +00:00
import PortAndInterface from '../PortAndInterface';
2022-06-12 12:09:50 +02:00
type ServiceAddForm = {
name:string,
port:number,
proto:string,
ip_int:string,
2022-06-30 17:48:04 +02:00
autostart: boolean,
}
2022-06-15 01:23:54 +02:00
function AddNewService({ opened, onClose }:{ opened:boolean, onClose:()=>void }) {
2022-06-12 12:09:50 +02:00
const form = useForm({
initialValues: {
name:"",
2022-06-30 15:58:03 +02:00
port:8080,
ip_int:"",
proto:"tcp",
autostart: true
2022-06-12 12:09:50 +02:00
},
validationRules:{
name: (value) => value !== ""?true:false,
2022-06-30 15:58:03 +02:00
port: (value) => value>0 && value<65536,
proto: (value) => ["tcp","udp"].includes(value),
ip_int: (value) => value.match(regex_ipv6)?true:false || value.match(regex_ipv4)?true:false
2022-06-12 12:09:50 +02:00
}
})
2022-06-15 01:23:54 +02:00
const close = () =>{
onClose()
form.reset()
2022-06-15 08:47:13 +02:00
setError(null)
2022-06-15 01:23:54 +02:00
}
2022-06-12 12:09:50 +02:00
const [submitLoading, setSubmitLoading] = useState(false)
const [error, setError] = useState<string|null>(null)
const submitRequest = ({ name, port, autostart, proto, ip_int }:ServiceAddForm) =>{
2022-06-12 12:09:50 +02:00
setSubmitLoading(true)
2022-07-21 10:20:54 +02:00
nfregex.servicesadd({name, port, proto, ip_int }).then( res => {
2022-07-10 15:05:56 +02:00
if (res.status === "ok" && res.service_id){
2022-06-12 12:09:50 +02:00
setSubmitLoading(false)
2022-06-15 01:23:54 +02:00
close();
2022-07-21 10:20:54 +02:00
if (autostart) nfregex.servicestart(res.service_id)
2022-07-07 21:08:02 +02:00
okNotify(`Service ${name} has been added`, `Successfully added service with port ${port}`)
2022-06-12 12:09:50 +02:00
}else{
setSubmitLoading(false)
setError("Invalid request! [ "+res.status+" ]")
2022-06-12 12:09:50 +02:00
}
}).catch( err => {
setSubmitLoading(false)
setError("Request Failed! [ "+err+" ]")
})
}
2022-06-12 12:09:50 +02:00
2022-06-15 01:23:54 +02:00
return <Modal size="xl" title="Add a new service" opened={opened} onClose={close} closeOnClickOutside={false} centered>
<form onSubmit={form.onSubmit(submitRequest)}>
<TextInput
label="Service name"
placeholder="Challenge 01"
{...form.getInputProps('name')}
/>
<Space h="md" />
2022-08-12 16:00:58 +00:00
<PortAndInterface form={form} int_name="ip_int" port_name="port" label={"Public IP Interface and port (ipv4/ipv6 + CIDR allowed)"} />
<Space h="md" />
<div className='center-flex'>
<Switch
label="Auto-Start Service"
{...form.getInputProps('autostart', { type: 'checkbox' })}
/>
<div className="flex-spacer"></div>
<SegmentedControl
data={[
{ label: 'TCP', value: 'tcp' },
{ label: 'UDP', value: 'udp' },
]}
{...form.getInputProps('proto')}
/>
</div>
2022-07-10 15:05:56 +02:00
2022-06-15 01:23:54 +02:00
<Group position="right" mt="md">
<Button loading={submitLoading} type="submit">Add Service</Button>
</Group>
2022-06-12 12:09:50 +02:00
2022-06-15 01:23:54 +02:00
<Space h="md" />
{error?<>
<Notification icon={<ImCross size={14} />} color="red" onClose={()=>{setError(null)}}>
Error: {error}
</Notification><Space h="md" /></>:null}
</form>
</Modal>
2022-06-12 12:09:50 +02:00
}
export default AddNewService;