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

169 lines
5.7 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-07 21:08:02 +02:00
import { GeneralStats, Service, ServiceAddForm, ServerResponse, RegexFilter, RegexAddForm, ServerStatusResponse, PasswordSend, ChangePassword, LoginResponse, ServerResponseToken } from "./models";
2022-06-12 16:44:54 +02:00
var Buffer = require('buffer').Buffer
2022-06-11 21:57:50 +02:00
2022-06-15 19:44:41 +02:00
export const eventUpdateName = "update-info"
2022-06-13 10:59:05 +02:00
2022-06-11 21:57:50 +02:00
export async function getapi(path:string):Promise<any>{
2022-06-28 21:49:03 +02:00
2022-06-13 16:12:52 +02:00
return await new Promise((resolve, reject) => {
2022-06-28 21:49:03 +02:00
fetch(`/api/${path}`,{
credentials: "same-origin",
headers: { "Authorization" : "Bearer " + window.localStorage.getItem("access_token")}
}).then(res => {
if(res.status === 401) window.location.reload()
2022-06-13 16:12:52 +02:00
if(!res.ok) reject(res.statusText)
res.json().then( res => resolve(res) ).catch( err => reject(err))
})
.catch(err => {
reject(err)
})
});
2022-06-11 21:57:50 +02:00
}
2022-06-28 21:49:03 +02:00
export async function postapi(path:string,data:any,is_form:boolean=false):Promise<any>{
return await new Promise((resolve, reject) => {
fetch(`/api/${path}`, {
method: 'POST',
credentials: "same-origin",
cache: 'no-cache',
headers: {
'Content-Type': is_form ? 'application/x-www-form-urlencoded' : 'application/json',
"Authorization" : "Bearer " + window.localStorage.getItem("access_token")
},
body: is_form ? data : JSON.stringify(data)
}).then(res => {
if(res.status === 401) window.location.reload()
if(res.status === 406) resolve({status:"Wrong Password"})
if(!res.ok) reject(res.statusText)
res.json().then( res => resolve(res) ).catch( err => reject(err))
})
.catch(err => {
reject(err)
})
});
}
2022-06-15 19:44:41 +02:00
export function fireUpdateRequest(){
window.dispatchEvent(new Event(eventUpdateName))
}
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-12 12:09:50 +02:00
export async function generalstats(){
2022-06-11 21:57:50 +02:00
return await getapi("general-stats") as GeneralStats;
}
2022-06-12 12:09:50 +02:00
export async function servicelist(){
2022-06-11 21:57:50 +02:00
return await getapi("services") as Service[];
2022-06-12 12:09:50 +02:00
}
2022-07-07 21:08:02 +02:00
export async function serviceinfo(service_port:number){
return await getapi(`service/${service_port}`) as Service;
2022-06-12 12:09: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;
console.log(access_token)
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) {
2022-06-28 21:49:03 +02:00
const from = "username=login&password=" + encodeURI(data.password);
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-13 10:59:05 +02:00
export async function deleteregex(regex_id:number){
const { status } = await getapi(`regex/${regex_id}/delete`) as ServerResponse;
return status === "ok"?undefined:status
}
2022-06-30 15:58:03 +02:00
export async function activateregex(regex_id:number){
const { status } = await getapi(`regex/${regex_id}/enable`) as ServerResponse;
return status === "ok"?undefined:status
}
export async function deactivateregex(regex_id:number){
const { status } = await getapi(`regex/${regex_id}/disable`) as ServerResponse;
return status === "ok"?undefined:status
}
2022-07-07 21:08:02 +02:00
export async function startservice(service_port:number){
const { status } = await getapi(`service/${service_port}/start`) as ServerResponse;
2022-06-13 10:59:05 +02:00
return status === "ok"?undefined:status
}
2022-07-07 21:08:02 +02:00
export async function stopservice(service_port:number){
const { status } = await getapi(`service/${service_port}/stop`) as ServerResponse;
2022-06-30 17:48:04 +02:00
return status === "ok"?undefined:status
}
2022-06-12 12:09:50 +02:00
export async function addservice(data:ServiceAddForm) {
2022-07-07 21:08:02 +02:00
return await postapi("services/add",data) as ServerResponse;
2022-06-12 12:09:50 +02:00
}
2022-07-07 21:08:02 +02:00
export async function deleteservice(service_port:number) {
const { status } = await getapi(`service/${service_port}/delete`) as ServerResponse;
2022-06-13 10:59:05 +02:00
return status === "ok"?undefined:status
}
2022-06-30 15:58:03 +02:00
2022-06-12 16:44:54 +02:00
export async function addregex(data:RegexAddForm) {
const { status } = await postapi("regexes/add",data) as ServerResponse;
return status === "ok"?undefined:status
}
2022-07-07 21:08:02 +02:00
export async function serviceregexlist(service_port:number){
return await getapi(`service/${service_port}/regexes`) as RegexFilter[];
2022-06-12 12:09:50 +02:00
}
2022-07-01 02:29:28 +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 />,
});
}
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()
}