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-21 20:25:39 +02:00
|
|
|
import { nfregex } from "../components/NFRegex/utils";
|
|
|
|
|
import { ChangePassword, IpInterface, LoginResponse, PasswordSend, ServerResponse, ServerResponseToken, ServerStatusResponse } from "./models";
|
2022-06-12 16:44:54 +02:00
|
|
|
var Buffer = require('buffer').Buffer
|
2022-06-11 21:57:50 +02:00
|
|
|
|
2022-07-21 20:25:39 +02:00
|
|
|
|
2022-06-15 19:44:41 +02:00
|
|
|
export const eventUpdateName = "update-info"
|
|
|
|
|
|
2022-07-11 19:56:05 +02:00
|
|
|
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})|((:[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*(\\/([0-9]|[1-9][0-9]|1[0-1][0-9]|12[0-8]))?$";
|
|
|
|
|
export const regex_ipv4 = "^([0-9]{1,3}\\.){3}[0-9]{1,3}(\\/([0-9]|[1-2][0-9]|3[0-2]))?$"
|
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 => {
|
2022-06-15 09:11:27 +02:00
|
|
|
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-07-21 20:25:39 +02:00
|
|
|
export function gatmainpath(){
|
|
|
|
|
const paths = window.location.pathname.split("/")
|
|
|
|
|
if (paths.length > 1) return paths[1]
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-15 19:44:41 +02:00
|
|
|
export function fireUpdateRequest(){
|
|
|
|
|
window.dispatchEvent(new Event(eventUpdateName))
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-21 10:20:54 +02:00
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-20 00:21:22 +02:00
|
|
|
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) {
|
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-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()
|
|
|
|
|
}
|