import { ActionIcon, Badge, Box, Code, LoadingOverlay, Space, ThemeIcon, Title, Tooltip } from '@mantine/core'; import { useEffect, useState } from 'react'; import { BsPlusLg } from "react-icons/bs"; import { useNavigate, useParams } from 'react-router'; import ServiceRow from '../../components/NFProxy/ServiceRow'; import { errorNotify, getErrorMessage, isMediumScreen } from '../../js/utils'; import AddEditService from '../../components/NFProxy/AddEditService'; import { useQueryClient } from '@tanstack/react-query'; import { TbPlugConnected, TbReload } from 'react-icons/tb'; import { EXAMPLE_PYFILTER, nfproxy, nfproxyServiceQuery } from '../../components/NFProxy/utils'; import { FaFilter, FaPencilAlt, FaServer } from 'react-icons/fa'; import { MdUploadFile } from "react-icons/md"; import { notifications } from '@mantine/notifications'; import { useFileDialog } from '@mantine/hooks'; import { CodeHighlight } from '@mantine/code-highlight'; import { DocsButton } from '../../components/DocsButton'; export default function NFProxy({ children }: { children: any }) { const navigator = useNavigate() const [open, setOpen] = useState(false); const {srv} = useParams() const queryClient = useQueryClient() const isMedium = isMediumScreen() const services = nfproxyServiceQuery() const fileDialog = useFileDialog({ accept: ".py", multiple: false, resetOnOpen: true, onChange: (files) => { if (files?.length??0 > 0) setFile(files![0]) } }); const [file, setFile] = useState(null); useEffect(() => { if (!srv) return const service = services.data?.find(s => s.service_id === srv) if (!service) return if (file){ console.log("Uploading code") const notify_id = notifications.show( { title: "Uploading code", message: `Uploading code for service ${service.name}`, color: "blue", icon: , autoClose: false, loading: true, } ) file.text() .then( code => nfproxy.setpyfilterscode(service?.service_id??"",code.toString())) .then( res => { if (!res){ notifications.update({ id: notify_id, title: "Code uploaded", message: `Successfully uploaded code for service ${service.name}`, color: "green", icon: , autoClose: 5000, loading: false, }) }else{ notifications.update({ id: notify_id, title: "Code upload failed", message: `Error: ${res}`, color: "red", icon: , autoClose: 5000, loading: false, }) } }).catch( err => { notifications.update({ id: notify_id, title: "Code upload failed", message: `Error: ${err}`, color: "red", icon: , autoClose: 5000, loading: false, }) }).finally(()=>{setFile(null)}) } }, [file]) useEffect(()=> { if(services.isError) errorNotify("NFProxy Update failed!", getErrorMessage(services.error)) },[services.isError]) const closeModal = () => {setOpen(false);} return <> <ThemeIcon radius="md" size="md" variant='filled' color='lime' ><TbPlugConnected size={20} /></ThemeIcon><Space w="xs" />Netfilter Proxy {isMedium?:} {isMedium?"General stats:":null} Services: {services.isLoading?0:services.data?.length} {services.isLoading?0:services.data?.reduce((acc, s)=> acc+=s.blocked_packets, 0)} {services.isLoading?0:services.data?.reduce((acc, s)=> acc+=s.edited_packets, 0)} {services.isLoading?0:services.data?.reduce((acc, s)=> acc+=s.n_filters, 0)} {isMedium?null:} { srv? : setOpen(true)} size="lg" radius="md" variant="filled"> } queryClient.invalidateQueries(["nfproxy"])} size="lg" radius="md" variant="filled" loading={services.isFetching}> {srv?null:<> {(services.data && services.data?.length > 0)?services.data.map( srv => { navigator("/nfproxy/"+srv.service_id) }} />):<> Netfilter proxy is a simulated proxy written using python with a c++ core Filters are created using a simple python syntax, infact the first you need to do is to install the firegex lib:<Space w="xs" /><Code mb={-4} >pip install -U fgex</Code> Then you can create a new service and write custom filters for the service setOpen(true)} size="xl" radius="md" variant="filled"> } } {srv?children:null} {!srv? :null } }