2022-12-12 17:12:11 -08:00
|
|
|
export class ResponseError extends Error {
|
2022-12-12 16:09:06 -08:00
|
|
|
constructor(message) {
|
|
|
|
super(message);
|
2022-12-12 16:11:00 -08:00
|
|
|
this.name = "ResponseError";
|
2022-12-12 16:09:06 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-12 17:12:11 -08:00
|
|
|
export class NetworkError extends Error {
|
2022-12-12 16:09:06 -08:00
|
|
|
constructor(message) {
|
|
|
|
super(message);
|
2022-12-12 16:11:00 -08:00
|
|
|
this.name = "NetworkError";
|
2022-12-12 16:09:06 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-15 23:40:04 -08:00
|
|
|
function getCookie(cname) {
|
|
|
|
let name = cname + "=";
|
|
|
|
let decodedCookie = decodeURIComponent(document.cookie);
|
2022-12-17 16:07:18 -08:00
|
|
|
let ca = decodedCookie.split(";");
|
2023-01-08 21:46:14 -08:00
|
|
|
for(let i = 0; i < ca.length; i++) {
|
2022-12-15 23:40:04 -08:00
|
|
|
let c = ca[i];
|
2022-12-17 16:07:18 -08:00
|
|
|
while (c.charAt(0) === " ") {
|
2022-12-15 23:40:04 -08:00
|
|
|
c = c.substring(1);
|
|
|
|
}
|
2022-12-17 16:07:18 -08:00
|
|
|
if (c.indexOf(name) === 0) {
|
2022-12-15 23:40:04 -08:00
|
|
|
return c.substring(name.length, c.length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2022-12-10 22:49:10 -08:00
|
|
|
export async function requestTicket (username, password) {
|
2022-12-12 16:19:47 -08:00
|
|
|
let response = await request("/access/ticket", "POST", {username: `${username}@pve`, password: password}, false);
|
2022-12-12 16:09:06 -08:00
|
|
|
|
2022-12-12 16:21:33 -08:00
|
|
|
return response;
|
2022-12-10 22:49:10 -08:00
|
|
|
}
|
|
|
|
|
2022-12-15 23:15:52 -08:00
|
|
|
export function setTicket (ticket, csrf) {
|
2022-12-10 22:49:10 -08: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-15 23:15:52 -08:00
|
|
|
document.cookie = `CSRFPreventionToken=${csrf}; path=/; expires=${d.toUTCString()}; domain=.tronnet.net;`
|
2022-12-10 22:49:10 -08:00
|
|
|
}
|
|
|
|
|
2022-12-12 16:30:55 -08:00
|
|
|
export async function request (path, method, body = null, auth = true) {
|
2022-12-10 22:49:10 -08:00
|
|
|
let prms = new URLSearchParams(body);
|
2022-12-12 16:00:11 -08:00
|
|
|
|
|
|
|
let content = {
|
2022-12-10 22:49:10 -08:00
|
|
|
method: method,
|
|
|
|
mode: "cors",
|
|
|
|
credentials: "include",
|
|
|
|
headers: {
|
2022-12-12 16:18:40 -08:00
|
|
|
"Content-Type": "application/x-www-form-urlencoded"
|
2022-12-10 22:49:10 -08:00
|
|
|
}
|
2022-12-12 16:00:11 -08:00
|
|
|
}
|
2022-12-12 16:18:40 -08:00
|
|
|
if(method === "POST") {
|
2022-12-12 16:00:11 -08:00
|
|
|
content.body = prms.toString();
|
2022-12-15 23:44:48 -08:00
|
|
|
content.headers.CSRFPreventionToken = getCookie("CSRFPreventionToken");
|
2022-12-10 22:49:10 -08:00
|
|
|
}
|
2022-12-12 16:00:11 -08:00
|
|
|
|
|
|
|
let response = await fetch(`https://pve.tronnet.net/api2/json${path}`, content)
|
|
|
|
.then((response) => {
|
|
|
|
if (!response.ok) {
|
2022-12-12 16:09:06 -08:00
|
|
|
throw new ResponseError(`recieved response status code ${response.status}`);
|
2022-12-12 16:00:11 -08:00
|
|
|
}
|
|
|
|
return response;
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
2022-12-12 16:09:57 -08:00
|
|
|
if (error instanceof ResponseError) {
|
2022-12-12 16:09:06 -08:00
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
throw new NetworkError(error);
|
2022-12-12 16:00:11 -08:00
|
|
|
});
|
|
|
|
|
2022-12-10 22:49:10 -08:00
|
|
|
let data = await response.json();
|
|
|
|
return data;
|
2022-12-18 21:52:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function goToPage (page, data={}) {
|
2022-12-18 21:58:46 -08:00
|
|
|
let url = new URL(`https://client.tronnet.net/${page}`);
|
2022-12-18 21:52:39 -08:00
|
|
|
for(let k in data) {
|
|
|
|
url.searchParams.append(k, data[k]);
|
|
|
|
}
|
2022-12-20 15:45:43 -08:00
|
|
|
window.location.assign(url.toString());
|
2022-12-18 22:14:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getURIData () {
|
|
|
|
let url = new URL(window.location.href);
|
|
|
|
return Object.fromEntries(url.searchParams);
|
2023-01-09 15:08:45 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function deleteAllCookies () {
|
2023-01-09 15:16:24 -08:00
|
|
|
document.cookie.split(";").forEach(function(c) { document.cookie = c.replace(/^ +/, "").replace(/=.*/, "=;expires=" + new Date().toUTCString() + ";path=/;domain=.tronnet.net;"); });
|
2022-12-10 22:49:10 -08:00
|
|
|
}
|