Files
firegex-traffic-viewer/frontend/src/App.tsx

171 lines
6.1 KiB
TypeScript
Raw Normal View History

import { Button, Group, Loader, LoadingOverlay, Notification, Space, PasswordInput, Title } from '@mantine/core';
2023-06-05 00:55:38 +02:00
import { useForm } from '@mantine/form';
2022-06-13 16:12:52 +02:00
import React, { useEffect, useState } from 'react';
import { ImCross } from 'react-icons/im';
2022-07-22 00:34:57 +02:00
import { Outlet, Route, Routes } from 'react-router-dom';
2022-06-11 21:57:50 +02:00
import MainLayout from './components/MainLayout';
2022-06-15 19:44:41 +02:00
import { PasswordSend, ServerStatusResponse } from './js/models';
2023-06-29 13:44:12 +02:00
import { errorNotify, fireUpdateRequest, getstatus, HomeRedirector, IS_DEV, login, setpassword } from './js/utils';
import NFRegex from './pages/NFRegex';
2022-07-08 15:13:46 +02:00
import io from 'socket.io-client';
2022-07-22 00:34:57 +02:00
import RegexProxy from './pages/RegexProxy';
import ServiceDetailsNFRegex from './pages/NFRegex/ServiceDetails';
2022-07-22 00:34:57 +02:00
import ServiceDetailsProxyRegex from './pages/RegexProxy/ServiceDetails';
import PortHijack from './pages/PortHijack';
2022-07-08 15:13:46 +02:00
2023-06-29 13:44:12 +02:00
const socket = io({transports: ["websocket", "polling"], path:"/sock", host:IS_DEV?"127.0.0.1:4444":undefined });
2022-06-11 21:57:50 +02:00
function App() {
2022-06-13 16:12:52 +02:00
const [loading, setLoading] = useState(true);
const [systemStatus, setSystemStatus] = useState<ServerStatusResponse>({status:"", loggined:false})
const [reqError, setReqError] = useState<undefined|string>()
const [error, setError] = useState<string|null>()
const [loadinBtn, setLoadingBtn] = useState(false);
const getStatus = () =>{
getstatus().then( res =>{
setSystemStatus(res)
setReqError(undefined)
setLoading(false)
}).catch(err=>{
setReqError(err.toString())
setLoading(false)
2022-06-15 19:44:41 +02:00
setTimeout(getStatus, 500)
2022-06-13 16:12:52 +02:00
})
}
2022-06-15 19:44:41 +02:00
useEffect(()=>{
2022-07-08 15:13:46 +02:00
getStatus()
socket.on("update", () => {
fireUpdateRequest()
})
socket.on("connect_error", (err) => {
errorNotify("Socket.Io connection failed! ",`Error message: [${err.message}]`)
getStatus()
});
return () => {
socket.off("update")
socket.off("connect_error")
}
2022-06-15 19:44:41 +02:00
},[])
2022-07-18 23:01:24 +02:00
useEffect(()=>{
const updater = setInterval(fireUpdateRequest,6000)
return () => clearInterval(updater)
},[])
2022-06-13 16:12:52 +02:00
const form = useForm({
initialValues: {
password:"",
},
2023-06-05 00:55:38 +02:00
validate:{
password: (value) => value !== "" ? null : "Password is required",
2022-06-13 16:12:52 +02:00
}
})
if (loading){
return <LoadingOverlay visible/>
}else if (reqError){
return <div className='center-flex-row' style={{padding:"100px"}}>
2022-06-14 12:09:17 +02:00
<Title order={1} align="center">Error launching Firegex! 🔥</Title>
2022-06-13 16:12:52 +02:00
<Space h="md" />
2022-06-14 12:09:17 +02:00
<Title order={4} align="center">Error communicating with backend</Title>
2022-06-13 16:12:52 +02:00
<Space h="md" />
Errore: {reqError}
<Space h="xl" />
<Loader />
</div>
}else if (systemStatus.status === "init"){
const submitRequest = async (values:PasswordSend) => {
setLoadingBtn(true)
await setpassword(values).then(res => {
if(!res){
setSystemStatus({loggined:true, status:"run"})
}else{
setError(res)
}
}).catch( err => setError(err.toString()))
setLoadingBtn(false)
}
return <div className='center-flex-row' style={{padding:"100px"}}>
2022-06-14 08:31:25 +02:00
<Title order={3} align="center">Setup: Choose the password for access to the firewall 🔒</Title>
2022-06-13 16:12:52 +02:00
<Space h="xl" />
<form onSubmit={form.onSubmit(submitRequest)} style={{width:"80%"}}>
<PasswordInput
2022-06-13 16:12:52 +02:00
label="Password"
placeholder="$3cr3t"
{...form.getInputProps('password')}
/>
<Group position="right" mt="md">
<Button loading={loadinBtn} type="submit">Set Password</Button>
</Group>
</form>
<Space h="xl" />
{error?<>
<Notification icon={<ImCross size={14} />} color="red" onClose={()=>{setError(null)}}>
Error: {error}
</Notification><Space h="md" /></>:null}
</div>
}else if (systemStatus.status === "run" && !systemStatus.loggined){
const submitRequest = async (values:PasswordSend) => {
setLoadingBtn(true)
await login(values).then(res => {
if(!res){
setSystemStatus({...systemStatus, loggined:true})
}else{
2022-06-28 21:49:03 +02:00
setError("Login failed")
2022-06-13 16:12:52 +02:00
}
}).catch( err => setError(err.toString()))
setLoadingBtn(false)
}
return <div className='center-flex-row' style={{padding:"100px"}}>
<Title order={2} align="center">Welcome to Firegex 🔥</Title>
<Space h="xl" />
2022-06-14 08:31:25 +02:00
<Title order={2} align="center">Before you use the firewall, insert the password 🔒</Title>
2022-06-13 16:12:52 +02:00
<Space h="xl" />
<form onSubmit={form.onSubmit(submitRequest)} style={{width:"80%"}}>
<PasswordInput
2022-06-13 16:12:52 +02:00
label="Password"
placeholder="$3cr3t"
{...form.getInputProps('password')}
/>
<Group position="right" mt="md">
<Button loading={loadinBtn} type="submit">Login</Button>
</Group>
</form>
<Space h="xl" />
{error?<>
<Notification icon={<ImCross size={14} />} color="red" onClose={()=>{setError(null)}}>
Error: {error}
</Notification><Space h="md" /></>:null}
</div>
}else if (systemStatus.status === "run" && systemStatus.loggined){
return <Routes>
<Route element={<MainLayout><Outlet /></MainLayout>}>
2022-07-21 10:20:54 +02:00
<Route path="nfregex" element={<NFRegex><Outlet /></NFRegex>} >
2022-07-22 00:34:57 +02:00
<Route path=":srv" element={<ServiceDetailsNFRegex />} />
2022-07-21 10:20:54 +02:00
</Route>
2022-07-22 00:34:57 +02:00
<Route path="regexproxy" element={<RegexProxy><Outlet /></RegexProxy>} >
<Route path=":srv" element={<ServiceDetailsProxyRegex />} />
</Route>
<Route path="porthijack" element={<PortHijack />} />
2022-07-22 00:34:57 +02:00
<Route path="*" element={<HomeRedirector />} />
2022-06-13 16:12:52 +02:00
</Route>
</Routes>
}else{
return <div className='center-flex-row' style={{padding:"100px"}}>
2022-06-14 12:09:17 +02:00
<Title order={1} align="center">Error launching Firegex! 🔥</Title>
2022-06-13 16:12:52 +02:00
<Space h="md" />
2022-06-14 12:09:17 +02:00
<Title order={4} align="center">Error communicating with backend</Title>
2022-06-13 16:12:52 +02:00
</div>
}
2022-06-11 21:57:50 +02:00
}
export default App;