import { Button, Group, Space, TextInput, Notification, Modal, Switch, SegmentedControl } from '@mantine/core'; import { useForm } from '@mantine/hooks'; import React, { useState } from 'react'; import { okNotify, regex_ipv6_no_cidr, regex_ipv4_no_cidr } from '../../js/utils'; import { ImCross } from "react-icons/im" import { porthijack } from './utils'; import PortAndInterface from '../PortAndInterface'; type ServiceAddForm = { name:string, public_port:number, proxy_port:number, proto:string, ip_src:string, ip_dst:string, autostart: boolean, } function AddNewService({ opened, onClose }:{ opened:boolean, onClose:()=>void }) { const form = useForm({ initialValues: { name:"", public_port:80, proxy_port:8080, proto:"tcp", ip_src:"", ip_dst:"127.0.0.1", autostart: false, }, validationRules:{ name: (value) => value !== ""?true:false, public_port: (value) => value>0 && value<65536, proxy_port: (value) => value>0 && value<65536, proto: (value) => ["tcp","udp"].includes(value), ip_src: (value) => value.match(regex_ipv6_no_cidr)?true:false || value.match(regex_ipv4_no_cidr)?true:false, ip_dst: (value) => value.match(regex_ipv6_no_cidr)?true:false || value.match(regex_ipv4_no_cidr)?true:false } }) const close = () =>{ onClose() form.reset() setError(null) } const [submitLoading, setSubmitLoading] = useState(false) const [error, setError] = useState(null) const submitRequest = ({ name, proxy_port, public_port, autostart, proto, ip_src, ip_dst }:ServiceAddForm) =>{ setSubmitLoading(true) porthijack.servicesadd({name, proxy_port, public_port, proto, ip_src, ip_dst }).then( res => { if (res.status === "ok" && res.service_id){ setSubmitLoading(false) close(); if (autostart) porthijack.servicestart(res.service_id) okNotify(`Service ${name} has been added`, `Successfully added service from port ${public_port} to ${proxy_port}`) }else{ setSubmitLoading(false) setError("Invalid request! [ "+res.status+" ]") } }).catch( err => { setSubmitLoading(false) setError("Request Failed! [ "+err+" ]") }) } return
{error?<> } color="red" onClose={()=>{setError(null)}}> Error: {error} :null}
} export default AddNewService;