ProxmoxAAS-Dashboard/scripts/utils.js

209 lines
6.4 KiB
JavaScript
Raw Normal View History

import {API} from "/vars.js";
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: ["move", "resize", "detach_attach", "delete"],
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: ["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: ["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: {
statusSrc: "images/status/active.svg",
statusAlt: "Instance is running",
powerButtonSrc: "images/actions/instance/stop.svg",
powerButtonAlt: "Shutdown Instance",
configButtonSrc: "images/actions/instance/config-inactive.svg",
configButtonAlt: "Change Configuration (Inactive)",
consoleButtonSrc: "images/actions/instance/console-active.svg",
consoleButtonAlt: "Open Console",
deleteButtonSrc: "images/actions/delete-inactive.svg",
deleteButtonAlt: "Delete Instance (Inactive)"
},
stopped: {
statusSrc: "images/status/inactive.svg",
statusAlt: "Instance is stopped",
powerButtonSrc: "images/actions/instance/start.svg",
powerButtonAlt: "Start Instance",
configButtonSrc: "images/actions/instance/config-active.svg",
configButtonAlt: "Change Configuration",
consoleButtonSrc: "images/actions/instance/console-inactive.svg",
consoleButtonAlt: "Open Console (Inactive)",
deleteButtonSrc: "images/actions/delete-active.svg",
deleteButtonAlt: "Delete Instance"
},
loading: {
statusSrc: "images/status/loading.svg",
statusAlt: "Instance is loading",
powerButtonSrc: "images/status/loading.svg",
powerButtonAlt: "Loading Instance",
configButtonSrc: "images/actions/instance/config-inactive.svg",
configButtonAlt: "Change Configuration (Inactive)",
consoleButtonSrc: "images/actions/instance/console-inactive.svg",
consoleButtonAlt: "Open Console (Inactive)",
deleteButtonSrc: "images/actions/delete-inactive.svg",
deleteButtonAlt: "Delete Instance (Inactive)"
}
}
export const nodes = {
online: {
statusSrc: "images/status/active.svg",
statusAlt: "Node is online",
},
offline: {
statusSrc: "images/status/inactive.svg",
statusAlt: "Node is offline",
},
uknown: {
statusSrc: "images/status/inactive.svg",
statusAlt: "Node status is unknown",
}
}
export function getCookie(cname) {
2022-12-16 07:40:04 +00:00
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, realm) {
let response = await requestPVE("/access/ticket", "POST", {username: `${username}@${realm}`, password: password}, false);
2022-12-13 00:21:33 +00:00
return response;
}
export function setTicket (ticket, csrf, username) {
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;`
document.cookie = `username=${username}@ldap; 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(`${API}/proxmox${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"
}
}
if (method === "POST" || method === "DELETE") {
2022-12-16 07:44:48 +00:00
content.headers.CSRFPreventionToken = getCookie("CSRFPreventionToken");
}
if (body) {
content.body = prms.toString();
}
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) => {
return response;
})
.catch((error) => {
return new NetworkError(error);
});
if (response instanceof NetworkError) {
return {status: 408, error: "network error"};
}
else if(!response.ok){
let data = await response.json()
return {status: response.status, error: data.error};
}
else {
let data = await response.json();
data.status = response.status;
return data;
}
2022-12-19 05:52:39 +00:00
}
2023-02-14 05:36:21 +00:00
export function goToPage (page, data={}, newwindow = false) {
2023-02-17 05:12:28 +00:00
let url = new URL(`https://${window.location.host}/${page}`);
for(let k in data) {
url.searchParams.append(k, data[k]);
}
if (newwindow) {
window.open(url, "tronnet - client", "height=480,width=848");
}
else {
window.location.assign(url.toString());
}
}
export function goToURL (href, data={}, newwindow = false) {
let url = new URL(href);
2022-12-19 05:52:39 +00:00
for(let k in data) {
url.searchParams.append(k, data[k]);
}
2023-02-14 05:36:21 +00:00
if (newwindow) {
window.open(url, "tronnet - client", "height=480,width=848");
}
else {
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;"); });
}