2023-01-27 21:49:50 +00:00
|
|
|
import {PVE, API} from "/vars.js";
|
2023-01-19 19:58:44 +00:00
|
|
|
|
2022-12-13 01:12:11 +00:00
|
|
|
export class ResponseError extends Error {
|
2022-12-13 00:09:06 +00:00
|
|
|
constructor(message) {
|
|
|
|
super(message);
|
2022-12-13 00:11:00 +00:00
|
|
|
this.name = "ResponseError";
|
2022-12-13 00:09:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-13 01:12:11 +00:00
|
|
|
export class NetworkError extends Error {
|
2022-12-13 00:09:06 +00:00
|
|
|
constructor(message) {
|
|
|
|
super(message);
|
2022-12-13 00:11:00 +00:00
|
|
|
this.name = "NetworkError";
|
2022-12-13 00:09:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-19 20:33:05 +00:00
|
|
|
export const resources = {
|
|
|
|
disk: {
|
2023-02-10 21:36:47 +00:00
|
|
|
actionBarOrder: ["move", "resize", "detach_attach", "delete"],
|
2023-01-19 20:33:05 +00:00
|
|
|
lxc: {
|
|
|
|
prefixOrder: ["rootfs", "mp", "unused"],
|
|
|
|
rootfs: {name: "ROOTFS", icon: "images/resources/drive.svg", actions: ["move", "resize"]},
|
2023-02-02 18:26:44 +00:00
|
|
|
mp: {name: "MP", icon: "images/resources/drive.svg", actions: ["detach", "move", "reassign", "resize"]},
|
2023-01-19 20:33:05 +00:00
|
|
|
unused: {name: "UNUSED", icon: "images/resources/drive.svg", actions: ["attach", "delete", "reassign"]}
|
|
|
|
},
|
|
|
|
qemu: {
|
|
|
|
prefixOrder: ["ide", "sata", "unused"],
|
2023-02-02 18:26:44 +00:00
|
|
|
ide: {name: "IDE", icon: "images/resources/disk.svg", actions: ["delete"]},
|
2023-01-19 20:33:05 +00:00
|
|
|
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"]}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-26 20:50:15 +00:00
|
|
|
export const instances = {
|
|
|
|
running: {
|
2023-02-21 21:50:54 +00:00
|
|
|
powerButtonSrc: "images/actions/instance/stop.svg",
|
2023-02-01 00:02:10 +00:00
|
|
|
powerButtonAlt: "Shutdown Instance",
|
2023-02-21 21:50:54 +00:00
|
|
|
configButtonSrc: "images/actions/instance/config-inactive.svg",
|
2023-02-21 19:57:47 +00:00
|
|
|
configButtonAlt: "Change Configuration (Inactive)",
|
2023-02-21 21:50:54 +00:00
|
|
|
consoleButtonSrc: "images/actions/instance/console-active.svg",
|
2023-02-21 19:57:47 +00:00
|
|
|
consoleButtonAlt: "Open Console",
|
|
|
|
deleteButtonSrc: "images/actions/delete-inactive.svg",
|
|
|
|
deleteButtonAlt: "Delete Instance (Inactive)"
|
2023-01-26 20:50:15 +00:00
|
|
|
},
|
|
|
|
stopped: {
|
2023-02-21 21:50:54 +00:00
|
|
|
powerButtonSrc: "images/actions/instance/start.svg",
|
2023-02-01 00:02:10 +00:00
|
|
|
powerButtonAlt: "Start Instance",
|
2023-02-21 21:50:54 +00:00
|
|
|
configButtonSrc: "images/actions/instance/config-active.svg",
|
2023-02-21 19:57:47 +00:00
|
|
|
configButtonAlt: "Change Configuration",
|
2023-02-21 21:50:54 +00:00
|
|
|
consoleButtonSrc: "images/actions/instance/console-inactive.svg",
|
2023-02-21 19:57:47 +00:00
|
|
|
consoleButtonAlt: "Open Console (Inactive)",
|
|
|
|
deleteButtonSrc: "images/actions/delete-active.svg",
|
|
|
|
deleteButtonAlt: "Delete Instance"
|
2023-01-26 20:50:15 +00:00
|
|
|
},
|
|
|
|
loading: {
|
2023-02-21 22:50:01 +00:00
|
|
|
powerButtonSrc: "images/actions/loading.svg",
|
2023-02-01 00:02:10 +00:00
|
|
|
powerButtonAlt: "Loading Instance",
|
2023-02-21 21:50:54 +00:00
|
|
|
configButtonSrc: "images/actions/instance/config-inactive.svg",
|
2023-02-21 19:57:47 +00:00
|
|
|
configButtonAlt: "Change Configuration (Inactive)",
|
2023-02-21 21:50:54 +00:00
|
|
|
consoleButtonSrc: "images/actions/instance/console-inactive.svg",
|
2023-02-21 19:57:47 +00:00
|
|
|
consoleButtonAlt: "Open Console (Inactive)",
|
|
|
|
deleteButtonSrc: "images/actions/delete-inactive.svg",
|
|
|
|
deleteButtonAlt: "Delete Instance (Inactive)"
|
2023-01-26 20:50:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-14 05:21:45 +00:00
|
|
|
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 "";
|
|
|
|
}
|
|
|
|
|
2022-12-11 06:49:10 +00:00
|
|
|
export async function requestTicket (username, password) {
|
2023-02-01 00:02:10 +00:00
|
|
|
let response = await requestPVE("/access/ticket", "POST", {username: `${username}@ldap`, password: password}, false);
|
2022-12-13 00:09:06 +00:00
|
|
|
|
2022-12-13 00:21:33 +00:00
|
|
|
return response;
|
2022-12-11 06:49:10 +00:00
|
|
|
}
|
|
|
|
|
2023-02-14 05:21:45 +00:00
|
|
|
export function setTicket (ticket, csrf, username) {
|
2022-12-11 06:49:10 +00:00
|
|
|
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-02-14 05:21:45 +00:00
|
|
|
document.cookie = `username=${username}@ldap; path=/; expires=${d.toUTCString()}; domain=.tronnet.net;`
|
2022-12-11 06:49:10 +00:00
|
|
|
}
|
|
|
|
|
2023-01-25 02:06:31 +00:00
|
|
|
export async function requestPVE (path, method, body = null) {
|
2022-12-11 06:49:10 +00:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2023-01-27 21:49:50 +00:00
|
|
|
let response = await request(`${PVE}${path}`, content);
|
2023-01-25 02:06:31 +00:00
|
|
|
return response;
|
|
|
|
}
|
2022-12-13 00:00:11 +00:00
|
|
|
|
2023-01-25 02:06:31 +00:00
|
|
|
export async function requestAPI (path, method, body = null) {
|
|
|
|
let prms = new URLSearchParams(body);
|
2022-12-13 00:00:11 +00:00
|
|
|
let content = {
|
2022-12-11 06:49:10 +00:00
|
|
|
method: method,
|
|
|
|
mode: "cors",
|
|
|
|
credentials: "include",
|
|
|
|
headers: {
|
2022-12-13 00:18:40 +00:00
|
|
|
"Content-Type": "application/x-www-form-urlencoded"
|
2022-12-11 06:49:10 +00:00
|
|
|
}
|
2022-12-13 00:00:11 +00:00
|
|
|
}
|
2023-02-21 22:50:01 +00:00
|
|
|
if (method === "POST" || method === "DELETE") {
|
2022-12-16 07:44:48 +00:00
|
|
|
content.headers.CSRFPreventionToken = getCookie("CSRFPreventionToken");
|
2022-12-11 06:49:10 +00:00
|
|
|
}
|
2023-02-01 23:42:30 +00:00
|
|
|
if (body) {
|
|
|
|
content.body = prms.toString();
|
|
|
|
}
|
2022-12-13 00:00:11 +00:00
|
|
|
|
2023-01-27 21:49:50 +00:00
|
|
|
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)
|
2022-12-13 00:00:11 +00:00
|
|
|
.then((response) => {
|
|
|
|
if (!response.ok) {
|
2022-12-13 00:09:06 +00:00
|
|
|
throw new ResponseError(`recieved response status code ${response.status}`);
|
2022-12-13 00:00:11 +00:00
|
|
|
}
|
|
|
|
return response;
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
2022-12-13 00:09:57 +00:00
|
|
|
if (error instanceof ResponseError) {
|
2022-12-13 00:09:06 +00:00
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
throw new NetworkError(error);
|
2022-12-13 00:00:11 +00:00
|
|
|
});
|
|
|
|
|
2022-12-11 06:49:10 +00:00
|
|
|
let data = await response.json();
|
|
|
|
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());
|
|
|
|
}
|
2022-12-19 06:14:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getURIData () {
|
|
|
|
let url = new URL(window.location.href);
|
|
|
|
return Object.fromEntries(url.searchParams);
|
2023-01-09 23:08:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function deleteAllCookies () {
|
2023-01-09 23:16:24 +00:00
|
|
|
document.cookie.split(";").forEach(function(c) { document.cookie = c.replace(/^ +/, "").replace(/=.*/, "=;expires=" + new Date().toUTCString() + ";path=/;domain=.tronnet.net;"); });
|
2023-01-19 18:08:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function reload () {
|
|
|
|
window.location.reload();
|
2022-12-11 06:49:10 +00:00
|
|
|
}
|