import { ActionIcon, Box, Button, Checkbox, Code, Collapse, Group, Modal, NumberInput, Paper, Space, Table, Text, TextInput, Textarea, Title, Tooltip, FileButton } from "@mantine/core"; import { useForm } from "@mantine/form"; import { useDisclosure } from "@mantine/hooks"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import { useEffect, useState } from "react"; import { FaPlay, FaStop, FaTrash, FaPencilAlt } from "react-icons/fa"; import { MdAdd } from "react-icons/md"; import { getapi, postapi, putapi, deleteapi, errorNotify, okNotify } from "../../js/utils"; import PortAndInterface from "../../components/PortAndInterface"; import { TbShieldLock } from "react-icons/tb"; export const tlsStreamsQueryKey = ["tls_streams"]; interface TLSStream { id: string; name: string; ip_int: string; port: number; cert: string; key: string; status: string; ssl_port: number; clear_port: number; } const StreamFormModal = ({ opened, close, editStream }: { opened: boolean, close: () => void, editStream?: TLSStream | null }) => { const queryClient = useQueryClient(); const [loading, setLoading] = useState(false); const isEdit = !!editStream; const form = useForm({ initialValues: { name: "", ip_int: "", port: 443, cert: "", key: "" }, validate: { name: (val) => val.length > 0 ? null : "Name is required", ip_int: (val) => val.length > 0 ? null : "Target IP is required", port: (val) => val > 0 && val <= 65535 ? null : "Invalid port", cert: (val) => val.length > 0 ? null : "Certificate is required", key: (val) => val.length > 0 ? null : "Private Key is required" } }); useEffect(() => { if (opened) { if (editStream) { form.setValues({ name: editStream.name, ip_int: editStream.ip_int, port: editStream.port, cert: editStream.cert, key: editStream.key }); } else { form.reset(); } } // eslint-disable-next-line react-hooks/exhaustive-deps }, [opened, editStream?.id]); const mutation = useMutation({ mutationFn: (values: typeof form.values) => { return isEdit ? putapi(`tls/streams/${editStream!.id}`, values) : postapi("tls/streams", values); }, onSuccess: () => { okNotify(isEdit ? "Stream updated" : "Stream created", `The TLS stream was successfully ${isEdit ? "updated" : "created"}`); queryClient.invalidateQueries({ queryKey: tlsStreamsQueryKey }); form.reset(); close(); }, onError: (err: any) => { errorNotify(`Error ${isEdit ? "updating" : "creating"} stream`, err.toString()); }, onSettled: () => setLoading(false) }); const handleFileUpload = (file: File | null, fieldName: string) => { if (!file) return; const reader = new FileReader(); reader.onload = (e) => { const text = e.target?.result as string; form.setFieldValue(fieldName, text); }; reader.readAsText(file); }; return ( {isEdit ? "Edit TLS Decrypt Stream" : "Add New TLS Decrypt Stream"}} centered styles={{ body: { maxHeight: 'calc(100vh - 150px)', overflowY: 'auto' } }}>
{ setLoading(true); mutation.mutate(v); })}> TLS Certificate (PEM format) * handleFileUpload(file, 'cert')} accept=".pem,.crt,.cer,.txt,*"> {(props) => }