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"
|
|
|
|
|
import { GeneralStats, Service, ServiceAddForm, ServerResponse, RegexFilter, notification_time, RegexAddForm } from "./models";
|
|
|
|
|
|
|
|
|
|
var Buffer = require('buffer').Buffer
|
2022-06-11 21:57:50 +02:00
|
|
|
|
2022-06-13 10:59:05 +02:00
|
|
|
const custom_url = ""//"http://127.0.0.1:8080"
|
|
|
|
|
|
2022-06-11 21:57:50 +02:00
|
|
|
export async function getapi(path:string):Promise<any>{
|
2022-06-13 10:59:05 +02:00
|
|
|
return await fetch(`${custom_url}/api/${path}`).then( res => res.json() )
|
2022-06-11 21:57:50 +02:00
|
|
|
}
|
|
|
|
|
|
2022-06-12 12:09:50 +02:00
|
|
|
export async function postapi(path:string,data:any):Promise<any>{
|
2022-06-13 10:59:05 +02:00
|
|
|
return await fetch(`${custom_url}/api/${path}`, {
|
2022-06-12 12:09:50 +02:00
|
|
|
method: 'POST',
|
|
|
|
|
cache: 'no-cache',
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify(data)
|
|
|
|
|
}).then(res => res.json());
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function serviceinfo(service_id:string){
|
|
|
|
|
return await getapi(`service/${service_id}`) as Service;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function startservice(service_id:string){
|
|
|
|
|
const { status } = await getapi(`service/${service_id}/start`) as ServerResponse;
|
|
|
|
|
return status === "ok"?undefined:status
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function stopservice(service_id:string){
|
|
|
|
|
const { status } = await getapi(`service/${service_id}/stop`) as ServerResponse;
|
|
|
|
|
return status === "ok"?undefined:status
|
|
|
|
|
}
|
|
|
|
|
export async function pauseservice(service_id:string){
|
|
|
|
|
const { status } = await getapi(`service/${service_id}/pause`) as ServerResponse;
|
|
|
|
|
return status === "ok"?undefined:status
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function regenport(service_id:string){
|
|
|
|
|
const { status } = await getapi(`service/${service_id}/regen-port`) as ServerResponse;
|
|
|
|
|
return status === "ok"?undefined:status
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-12 12:09:50 +02:00
|
|
|
export async function addservice(data:ServiceAddForm) {
|
|
|
|
|
const { status } = await postapi("services/add",data) as ServerResponse;
|
|
|
|
|
return status === "ok"?undefined:status
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-13 10:59:05 +02:00
|
|
|
export async function deleteservice(service_id:string) {
|
|
|
|
|
const { status } = await getapi(`service/${service_id}/delete`) as ServerResponse;
|
|
|
|
|
return status === "ok"?undefined:status
|
|
|
|
|
}
|
|
|
|
|
|
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-06-12 12:09:50 +02:00
|
|
|
export async function serviceregexlist(service_id:string){
|
|
|
|
|
return await getapi(`service/${service_id}/regexes`) as RegexFilter[];
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-12 22:28:16 +02:00
|
|
|
const unescapedChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!\"#$&'()*+,-./:;<=>?@[\\]^_`{|}~ ";
|
2022-06-12 12:09:50 +02:00
|
|
|
|
|
|
|
|
export function getHumanReadableRegex(regexB64:string){
|
|
|
|
|
const regex = Buffer.from(regexB64, "base64")
|
|
|
|
|
let res = ""
|
|
|
|
|
for (let i=0; i < regex.length; i++){
|
|
|
|
|
const byte = String.fromCharCode(regex[i]);
|
|
|
|
|
if (unescapedChars.includes(byte)){
|
|
|
|
|
res+=byte
|
|
|
|
|
}else{
|
|
|
|
|
res+="%"+regex[i].toString(16)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return res
|
2022-06-12 16:44:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function errorNotify(title:string, description:string ){
|
|
|
|
|
showNotification({
|
|
|
|
|
autoClose: notification_time,
|
|
|
|
|
title: title,
|
|
|
|
|
message: description,
|
|
|
|
|
color: 'red',
|
|
|
|
|
icon: <ImCross />,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function okNotify(title:string, description:string ){
|
|
|
|
|
showNotification({
|
|
|
|
|
autoClose: notification_time,
|
|
|
|
|
title: title,
|
|
|
|
|
message: description,
|
|
|
|
|
color: 'teal',
|
|
|
|
|
icon: <TiTick />,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function validateRegex(pattern:string) {
|
|
|
|
|
var parts = pattern.split('/'),
|
|
|
|
|
regex = pattern,
|
|
|
|
|
options = "";
|
|
|
|
|
if (parts.length > 1) {
|
|
|
|
|
regex = parts[1];
|
|
|
|
|
options = parts[2];
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
new RegExp(regex, options);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch(e) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function b64encode(data:string){
|
|
|
|
|
return Buffer.from(data).toString('base64')
|
2022-06-11 21:57:50 +02:00
|
|
|
}
|