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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 "";
|
|
|
|
}
|
|
|
|
|
2022-12-11 06:49:10 +00:00
|
|
|
export async function requestTicket (username, password) {
|
2022-12-13 00:19:47 +00:00
|
|
|
let response = await request("/access/ticket", "POST", {username: `${username}@pve`, 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
|
|
|
}
|
|
|
|
|
2022-12-16 07:15:52 +00:00
|
|
|
export function setTicket (ticket, csrf) {
|
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;`
|
2022-12-11 06:49:10 +00:00
|
|
|
}
|
|
|
|
|
2022-12-13 00:30:55 +00:00
|
|
|
export async function request (path, method, body = null, auth = true) {
|
2022-12-11 06:49:10 +00:00
|
|
|
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
|
|
|
}
|
2022-12-13 00:18:40 +00:00
|
|
|
if(method === "POST") {
|
2022-12-13 00:00:11 +00:00
|
|
|
content.body = prms.toString();
|
2022-12-16 07:44:48 +00:00
|
|
|
content.headers.CSRFPreventionToken = getCookie("CSRFPreventionToken");
|
2022-12-11 06:49:10 +00:00
|
|
|
}
|
2022-12-13 00:00:11 +00:00
|
|
|
|
|
|
|
let response = await fetch(`https://pve.tronnet.net/api2/json${path}`, content)
|
|
|
|
.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
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
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 () {
|
|
|
|
document.cookie.split(";").forEach(function(c) { document.cookie = c.replace(/^ +/, "").replace(/=.*/, "=;expires=" + new Date().toUTCString() + ";path=/"); });
|
2022-12-11 06:49:10 +00:00
|
|
|
}
|