2023-02-28 23:24:49 +00:00
|
|
|
const axios = require('axios');
|
|
|
|
const {pveAPI, pveAPIToken} = require("./vars.js");
|
|
|
|
|
2023-04-19 01:03:55 +00:00
|
|
|
async function checkAuth (cookies, res, vmpath = null) {
|
|
|
|
let auth = false;
|
2023-02-28 23:24:49 +00:00
|
|
|
if (vmpath) {
|
|
|
|
let result = await requestPVE(`/${vmpath}/config`, "GET", cookies);
|
2023-04-19 01:03:55 +00:00
|
|
|
auth = result.status === 200;
|
2023-02-28 23:24:49 +00:00
|
|
|
}
|
|
|
|
else { // if no path is specified, then do a simple authentication
|
|
|
|
let result = await requestPVE("/version", "GET", cookies);
|
2023-04-19 01:03:55 +00:00
|
|
|
auth = result.status === 200;
|
|
|
|
}
|
|
|
|
if (!auth) {
|
|
|
|
res.status(401).send({auth: auth});
|
|
|
|
res.end();
|
|
|
|
return;
|
2023-02-28 23:24:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function requestPVE (path, method, cookies, body = null, token = null) {
|
|
|
|
let url = `${pveAPI}${path}`;
|
|
|
|
let content = {
|
|
|
|
method: method,
|
|
|
|
mode: "cors",
|
|
|
|
credentials: "include",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/x-www-form-urlencoded"
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if (token) {
|
|
|
|
content.headers.Authorization = `PVEAPIToken=${token.user}@${token.realm}!${token.id}=${token.uuid}`;
|
|
|
|
}
|
|
|
|
else if (cookies) {
|
|
|
|
content.headers.CSRFPreventionToken = cookies.CSRFPreventionToken;
|
|
|
|
content.headers.Cookie = `PVEAuthCookie=${cookies.PVEAuthCookie}; CSRFPreventionToken=${cookies.CSRFPreventionToken}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (body) {
|
|
|
|
content.data = JSON.parse(body);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
let response = await axios.request(url, content);
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
return error.response;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-19 01:03:55 +00:00
|
|
|
async function handleResponse (node, result, res) {
|
2023-02-28 23:24:49 +00:00
|
|
|
const waitFor = delay => new Promise(resolve => setTimeout(resolve, delay));
|
2023-04-19 01:03:55 +00:00
|
|
|
if (result.data.data) {
|
|
|
|
let upid = result.data.data;
|
2023-02-28 23:24:49 +00:00
|
|
|
while (true) {
|
|
|
|
let taskStatus = await requestPVE(`/nodes/${node}/tasks/${upid}/status`, "GET", null, null, pveAPIToken);
|
|
|
|
if (taskStatus.data.data.status === "stopped" && taskStatus.data.data.exitstatus === "OK") {
|
2023-04-19 01:03:55 +00:00
|
|
|
res.status(200).send(taskStatus.data.data);
|
|
|
|
res.end();
|
|
|
|
return;
|
2023-02-28 23:24:49 +00:00
|
|
|
}
|
|
|
|
else if (taskStatus.data.data.status === "stopped") {
|
2023-04-19 01:03:55 +00:00
|
|
|
res.status(500).send(taskStatus.data.data);
|
|
|
|
res.end();
|
|
|
|
return;
|
2023-02-28 23:24:49 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
await waitFor(1000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2023-04-19 01:03:55 +00:00
|
|
|
res.status(result.status).send(result.data);
|
|
|
|
res.end();
|
|
|
|
return;
|
2023-02-28 23:24:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-19 01:03:55 +00:00
|
|
|
async function getUsedResources (req, resourceMeta) {
|
|
|
|
let response = await requestPVE("/cluster/resources", "GET", req.cookies);
|
|
|
|
let used = {};
|
|
|
|
let diskprefixes = [];
|
|
|
|
for (let resourceName of Object.keys(resourceMeta)) {
|
2023-04-20 20:43:05 +00:00
|
|
|
used[resourceName] = 0;
|
|
|
|
if (resourceMeta[resourceName].type === "storage") {
|
|
|
|
for (let diskPrefix of resourceMeta[resourceName].disks) {
|
|
|
|
diskprefixes.push(diskPrefix);
|
|
|
|
}
|
2023-04-19 01:03:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for (instance of response.data.data) {
|
|
|
|
if (instance.type === "lxc" || instance.type === "qemu") {
|
|
|
|
let config = await requestPVE(`/nodes/${instance.node}/${instance.type}/${instance.vmid}/config`, "GET", req.cookies);
|
|
|
|
config = config.data.data;
|
|
|
|
for (key of Object.keys(config)) {
|
|
|
|
if (Object.keys(used).includes(key) && resourceMeta[key].type === "numeric") {
|
|
|
|
used[key] += config[key];
|
|
|
|
}
|
|
|
|
else if (diskprefixes.some(prefix => key.startsWith(prefix))) {
|
|
|
|
let diskInfo = await getDiskInfo(instance.node, instance.type, instance.vmid, key);
|
|
|
|
used[diskInfo.storage] += diskInfo.size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return used;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getDiskInfo (node, type, vmid, disk) {
|
|
|
|
let config = await requestPVE(`/nodes/${node}/${type}/${vmid}/config`, "GET", null, null, pveAPIToken);
|
|
|
|
let storageID = config.data.data[disk].split(":")[0];
|
2023-04-19 02:42:35 +00:00
|
|
|
let volID = config.data.data[disk].split(",")[0];
|
|
|
|
let volInfo = await requestPVE(`/nodes/${node}/storage/${storageID}/content/${volID}`, "GET", null, null, pveAPIToken);
|
|
|
|
volInfo.data.data.storage = storageID;
|
|
|
|
return volInfo.data.data;
|
2023-02-28 23:24:49 +00:00
|
|
|
}
|
|
|
|
|
2023-04-19 02:42:35 +00:00
|
|
|
module.exports = {checkAuth, requestPVE, handleResponse, getUsedResources, getDiskInfo};
|