Files
firegex-traffic-viewer/frontend/src/js/utils.tsx

197 lines
9.0 KiB
TypeScript
Raw Normal View History

2022-06-12 16:44:54 +02:00
import { showNotification } from "@mantine/notifications";
import { ImCross } from "react-icons/im";
import { TiTick } from "react-icons/ti"
2022-07-22 00:34:57 +02:00
import { Navigate } from "react-router-dom";
import { nfregex } from "../components/NFRegex/utils";
import { ChangePassword, IpInterface, LoginResponse, PasswordSend, ServerResponse, ServerResponseToken, ServerStatusResponse } from "./models";
2023-06-05 00:55:38 +02:00
import { Buffer } from "buffer"
import { QueryClient, useQuery } from "@tanstack/react-query";
2023-10-12 20:01:48 +02:00
import { useMediaQuery } from "@mantine/hooks";
import { nfproxy } from "../components/NFProxy/utils";
2022-06-11 21:57:50 +02:00
2023-06-29 13:44:12 +02:00
export const IS_DEV = import.meta.env.DEV
export const regex_ipv6 = "^s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5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|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:)))(%.+)?s*$";
2022-08-12 18:51:50 +00:00
export const regex_ipv4 = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\\/(3[0-2]|[1-2][0-9]|[0-9]))?$"
export const regex_ipv4_no_cidr = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$"
2023-09-25 18:10:12 +02:00
export const regex_port = "^([1-9]|[1-9][0-9]{1,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])?$"
export const regex_range_port = "^(([1-9]|[1-9][0-9]{1,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])(-([1-9]|[1-9][0-9]{1,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])?)?)?$"
2025-02-02 22:27:12 +01:00
export const DEV_IP_BACKEND = "127.0.0.1:4444"
2023-09-24 05:48:54 +02:00
export const queryClient = new QueryClient({ defaultOptions: { queries: {
staleTime: Infinity
2023-09-24 05:48:54 +02:00
} }})
2023-09-24 05:48:54 +02:00
export function getErrorMessage(e: any) {
let error = "Unknown error";
2023-09-25 18:10:12 +02:00
if(typeof e == "string") return e
2023-09-24 05:48:54 +02:00
if (e.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
error = e.response.data.error;
} else {
// Something happened in setting up the request that triggered an Error
error = e.message || e.error;
}
return error;
}
2023-09-26 21:16:03 +02:00
export function getErrorMessageFromServerResponse(e: any, def:string = "Unknown error") {
if (e.status){
return e.status
}
if (e.detail){
if (typeof e.detail == "string")
return e.detail
if (e.detail[0] && e.detail[0].msg)
return e.detail[0].msg
}
if (e.error){
return e.error
}
return def
}
2025-02-11 19:11:30 +01:00
export async function genericapi(method:string,path:string,data:any = undefined, is_form:boolean=false):Promise<any>{
2022-06-28 21:49:03 +02:00
return await new Promise((resolve, reject) => {
fetch(`${IS_DEV?`http://${DEV_IP_BACKEND}`:""}/api/${path}`, {
2025-02-11 19:11:30 +01:00
method: method,
2022-06-28 21:49:03 +02:00
credentials: "same-origin",
cache: 'no-cache',
headers: {
2025-02-11 19:11:30 +01:00
...(data?{'Content-Type': is_form ? 'application/x-www-form-urlencoded' : 'application/json'}:{}),
2022-06-28 21:49:03 +02:00
"Authorization" : "Bearer " + window.localStorage.getItem("access_token")
},
2025-02-11 19:11:30 +01:00
body: data? (is_form ? (new URLSearchParams(data)).toString() : JSON.stringify(data)) : undefined
2022-06-28 21:49:03 +02:00
}).then(res => {
if(res.status === 401) window.location.reload()
if(res.status === 406) resolve({status:"Wrong Password"})
2023-09-26 21:16:03 +02:00
if(!res.ok){
const errorDefault = res.statusText
return res.json().then( res => reject(getErrorMessageFromServerResponse(res, errorDefault)) ).catch( _err => reject(errorDefault))
}
2022-06-28 21:49:03 +02:00
res.json().then( res => resolve(res) ).catch( err => reject(err))
})
.catch(err => {
reject(err)
})
});
}
2025-02-11 19:11:30 +01:00
export async function getapi(path:string):Promise<any>{
return await genericapi("GET",path)
}
export async function postapi(path:string,data:any=undefined,is_form:boolean=false):Promise<any>{
return await genericapi("POST",path,data,is_form)
}
export async function deleteapi(path:string):Promise<any>{
return await genericapi("DELETE",path)
}
export async function putapi(path:string,data:any):Promise<any>{
return await genericapi("PUT",path,data)
}
2024-10-13 01:50:52 +02:00
export function getMainPath(){
const paths = window.location.pathname.split("/")
if (paths.length > 1) return paths[1]
return ""
}
2022-07-22 00:34:57 +02:00
export function HomeRedirector(){
const section = sessionStorage.getItem("home_section")
2023-10-01 02:09:07 +02:00
const path = section?`/${section}`:`/nfregex`
2023-09-24 17:05:55 +02:00
return <Navigate to={path} replace/>
}
2022-07-20 21:19:22 +02:00
export async function resetfiregex(delete_data:boolean = false){
const { status } = await postapi("reset",{delete:delete_data}) as ServerResponse;
return (status === "ok"?undefined:status)
}
export const ipInterfacesQuery = () => useQuery(["ipinterfaces"], getipinterfaces)
export async function getipinterfaces(){
return await getapi("interfaces") as IpInterface[];
}
2022-06-13 16:12:52 +02:00
export async function getstatus(){
return await getapi(`status`) as ServerStatusResponse;
}
2022-06-11 21:57:50 +02:00
2022-06-13 16:12:52 +02:00
export async function logout(){
2022-06-28 21:49:03 +02:00
window.localStorage.removeItem("access_token")
2022-06-13 16:12:52 +02:00
}
export async function setpassword(data:PasswordSend) {
2022-06-28 21:49:03 +02:00
const { status, access_token } = await postapi("set-password",data) as ServerResponseToken;
if (access_token)
window.localStorage.setItem("access_token", access_token);
2022-06-13 16:12:52 +02:00
return status === "ok"?undefined:status
}
export async function changepassword(data:ChangePassword) {
2022-06-28 21:49:03 +02:00
const { status, access_token } = await postapi("change-password",data) as ServerResponseToken;
if (access_token)
window.localStorage.setItem("access_token", access_token);
return status === "ok"?undefined:status
2022-06-13 16:12:52 +02:00
}
export async function login(data:PasswordSend) {
const from = {username: "login", password: data.password};
2022-06-28 21:49:03 +02:00
const { status, access_token } = await postapi("login",from,true) as LoginResponse;
window.localStorage.setItem("access_token", access_token);
return status;
2022-06-13 16:12:52 +02:00
}
2022-06-12 16:44:54 +02:00
export function errorNotify(title:string, description:string ){
showNotification({
2022-06-15 19:44:41 +02:00
autoClose: 2000,
2022-06-12 16:44:54 +02:00
title: title,
message: description,
color: 'red',
icon: <ImCross />,
});
}
export function okNotify(title:string, description:string ){
showNotification({
2022-06-15 19:44:41 +02:00
autoClose: 2000,
2022-06-12 16:44:54 +02:00
title: title,
message: description,
color: 'teal',
icon: <TiTick />,
});
}
2023-09-25 18:10:12 +02:00
export const makeid = (length:number) => {
let result = '';
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
const charactersLength = characters.length;
let counter = 0;
while (counter < length) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
counter += 1;
}
return result;
}
2022-06-22 01:31:46 +02:00
export function b64encode(data:number[]|string){
2022-06-12 16:44:54 +02:00
return Buffer.from(data).toString('base64')
2022-06-15 01:23:54 +02:00
}
2022-07-01 02:29:28 +02:00
export function b64decode(regexB64:string){
return Buffer.from(regexB64, "base64").toString()
2023-06-05 00:55:38 +02:00
}
2023-10-12 20:01:48 +02:00
export function isMediumScreen(){
return useMediaQuery('(min-width: 600px)');
}
export function isLargeScreen(){
return useMediaQuery('(min-width: 992px)');
}