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);
|
|
|
|
let ca = decodedCookie.split(';');
|
|
|
|
for(let i = 0; i <ca.length; i++) {
|
|
|
|
let c = ca[i];
|
|
|
|
while (c.charAt(0) == ' ') {
|
|
|
|
c = c.substring(1);
|
|
|
|
}
|
|
|
|
if (c.indexOf(name) == 0) {
|
|
|
|
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:18:40 +00:00
|
|
|
if(auth) {
|
|
|
|
content.headers.Cookie = document.cookie;
|
|
|
|
}
|
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;
|
|
|
|
}
|