ProxmoxAAS-Dashboard/scripts/utils.js

162 lines
4.8 KiB
JavaScript
Raw Normal View History

import {PVE, API} from "/vars.js";
2022-12-13 01:12:11 +00:00
export class ResponseError extends Error {
constructor(message) {
super(message);
2022-12-13 00:11:00 +00:00
this.name = "ResponseError";
}
}
2022-12-13 01:12:11 +00:00
export class NetworkError extends Error {
constructor(message) {
super(message);
2022-12-13 00:11:00 +00:00
this.name = "NetworkError";
}
}
export const resources = {
disk: {
//actionBarOrder: ["config", "move", "reassign", "resize", "delete_detach_attach"],
actionBarOrder: ["config", "move", "resize", "delete_detach_attach"], // handle reassign later
lxc: {
prefixOrder: ["rootfs", "mp", "unused"],
rootfs: {name: "ROOTFS", icon: "images/resources/drive.svg", actions: ["move", "resize"]},
mp: {name: "MP", icon: "images/resources/drive.svg", actions: ["config", "detach", "move", "reassign", "resize"]},
unused: {name: "UNUSED", icon: "images/resources/drive.svg", actions: ["attach", "delete", "reassign"]}
},
qemu: {
prefixOrder: ["ide", "sata", "unused"],
ide: {name: "IDE", icon: "images/resources/disk.svg", actions: ["config", "delete"]},
sata: {name: "SATA", icon: "images/resources/drive.svg", actions: ["detach", "move", "reassign", "resize"]},
unused: {name: "UNUSED", icon: "images/resources/drive.svg", actions: ["attach", "delete", "reassign"]}
}
}
}
export const instances = {
running: {
powerButtonSrc: "images/actions/stop.svg",
powerButtonAlt: "Shutdown Instance",
configButtonSrc: "images/actions/config-inactive.svg",
configButtonAlt: "Configuration Disabled"
},
stopped: {
powerButtonSrc: "images/actions/start.svg",
powerButtonAlt: "Start Instance",
configButtonSrc: "images/actions/config-active.svg",
configButtonAlt: "Configure Instance"
},
loading: {
powerButtonSrc: "images/actions/loading.svg",
powerButtonAlt: "Loading Instance",
configButtonSrc: "images/actions/config-inactive.svg",
configButtonAlt: "Configuration Disabled"
}
}
2022-12-16 07:40:04 +00:00
function getCookie(cname) {
let name = cname + "=";
let decodedCookie = decodeURIComponent(document.cookie);
2022-12-18 00:07:18 +00:00
let ca = decodedCookie.split(";");
2023-01-09 05:46:14 +00:00
for(let i = 0; i < ca.length; i++) {
2022-12-16 07:40:04 +00:00
let c = ca[i];
2022-12-18 00:07:18 +00:00
while (c.charAt(0) === " ") {
2022-12-16 07:40:04 +00:00
c = c.substring(1);
}
2022-12-18 00:07:18 +00:00
if (c.indexOf(name) === 0) {
2022-12-16 07:40:04 +00:00
return c.substring(name.length, c.length);
}
}
return "";
}
export async function requestTicket (username, password) {
let response = await requestPVE("/access/ticket", "POST", {username: `${username}@ldap`, password: password}, false);
2022-12-13 00:21:33 +00:00
return response;
}
2022-12-16 07:15:52 +00:00
export function setTicket (ticket, csrf) {
let d = new Date();
d.setTime(d.getTime() + (2*60*60*1000));
document.cookie = `PVEAuthCookie=${ticket}; path=/; expires=${d.toUTCString()}; domain=.tronnet.net`;
2022-12-16 07:15:52 +00:00
document.cookie = `CSRFPreventionToken=${csrf}; path=/; expires=${d.toUTCString()}; domain=.tronnet.net;`
}
2023-01-25 02:06:31 +00:00
export async function requestPVE (path, method, body = null) {
let prms = new URLSearchParams(body);
2023-01-25 02:06:31 +00:00
let content = {
method: method,
mode: "cors",
credentials: "include",
headers: {
"Content-Type": "application/x-www-form-urlencoded"
}
}
if(method === "POST") {
content.body = prms.toString();
content.headers.CSRFPreventionToken = getCookie("CSRFPreventionToken");
}
let response = await request(`${PVE}${path}`, content);
2023-01-25 02:06:31 +00:00
return response;
}
2023-01-25 02:06:31 +00:00
export async function requestAPI (path, method, body = null) {
let prms = new URLSearchParams(body);
let content = {
method: method,
mode: "cors",
credentials: "include",
headers: {
2022-12-13 00:18:40 +00:00
"Content-Type": "application/x-www-form-urlencoded"
}
}
2022-12-13 00:18:40 +00:00
if(method === "POST") {
content.body = prms.toString();
2022-12-16 07:44:48 +00:00
content.headers.CSRFPreventionToken = getCookie("CSRFPreventionToken");
}
let response = await request(`${API}${path}`, content);
2023-01-25 02:06:31 +00:00
return response;
}
async function request (url, content) {
let response = await fetch(url, content)
.then((response) => {
if (!response.ok) {
throw new ResponseError(`recieved response status code ${response.status}`);
}
return response;
})
.catch((error) => {
2022-12-13 00:09:57 +00:00
if (error instanceof ResponseError) {
throw error;
}
throw new NetworkError(error);
});
let data = await response.json();
return data;
2022-12-19 05:52:39 +00:00
}
export function goToPage (page, data={}) {
2022-12-19 05:58:46 +00:00
let url = new URL(`https://client.tronnet.net/${page}`);
2022-12-19 05:52:39 +00:00
for(let k in data) {
url.searchParams.append(k, data[k]);
}
2022-12-20 23:45:43 +00:00
window.location.assign(url.toString());
}
export function getURIData () {
let url = new URL(window.location.href);
return Object.fromEntries(url.searchParams);
}
export function deleteAllCookies () {
document.cookie.split(";").forEach(function(c) { document.cookie = c.replace(/^ +/, "").replace(/=.*/, "=;expires=" + new Date().toUTCString() + ";path=/;domain=.tronnet.net;"); });
}
export function reload () {
window.location.reload();
}