2023-01-19 20:22:26 +00:00
|
|
|
const express = require("express");
|
|
|
|
const bodyParser = require("body-parser");
|
|
|
|
const cookieParser = require("cookie-parser")
|
|
|
|
const cors = require("cors");
|
|
|
|
const helmet = require("helmet");
|
|
|
|
const morgan = require("morgan");
|
2023-02-13 02:11:55 +00:00
|
|
|
const axios = require('axios');
|
|
|
|
var api = require("./package.json");
|
2023-01-25 00:28:44 +00:00
|
|
|
|
2023-02-13 02:11:55 +00:00
|
|
|
const {pveAPI, pveAPIToken, listenPort} = require("./vars.js");
|
|
|
|
const { token } = require("morgan");
|
2023-02-21 22:35:09 +00:00
|
|
|
const { response } = require("express");
|
2023-01-17 23:46:42 +00:00
|
|
|
|
|
|
|
const app = express();
|
|
|
|
app.use(helmet());
|
2023-01-25 02:01:47 +00:00
|
|
|
app.use(bodyParser.urlencoded({extended: true}));
|
2023-01-19 20:22:26 +00:00
|
|
|
app.use(cookieParser())
|
2023-01-17 23:46:42 +00:00
|
|
|
app.use(cors());
|
2023-01-19 20:22:26 +00:00
|
|
|
app.use(morgan("combined"));
|
2023-01-17 23:46:42 +00:00
|
|
|
|
|
|
|
|
2023-01-19 20:22:26 +00:00
|
|
|
app.get("/api/version", (req, res) => {
|
2023-02-13 02:11:55 +00:00
|
|
|
res.send({version: api.version});
|
2023-01-17 23:46:42 +00:00
|
|
|
});
|
|
|
|
|
2023-01-19 20:22:26 +00:00
|
|
|
app.get("/api/echo", (req, res) => {
|
2023-01-20 06:40:21 +00:00
|
|
|
res.send({body: req.body, cookies: req.cookies});
|
2023-01-19 20:22:26 +00:00
|
|
|
});
|
|
|
|
|
2023-02-13 02:11:55 +00:00
|
|
|
app.get("/api/auth", async (req, res) => {
|
|
|
|
let result = await checkAuth(req.cookies);
|
|
|
|
res.send({auth: result});
|
2023-01-20 06:40:21 +00:00
|
|
|
});
|
|
|
|
|
2023-02-24 22:21:21 +00:00
|
|
|
app.get("/api/proxmox/*", async (req, res) => { // proxy endpoint for GET proxmox api with no token
|
2023-02-24 22:19:30 +00:00
|
|
|
path = req.url.replace("/api/proxmox", "");
|
|
|
|
let result = await requestPVE(path, "GET", req.cookies);
|
|
|
|
res.send(result.data, result.status);
|
|
|
|
});
|
|
|
|
|
2023-02-24 22:21:21 +00:00
|
|
|
app.post("/api/proxmox/*", async (req, res) => { // proxy endpoint for POST proxmox api with no token
|
|
|
|
path = req.url.replace("/api/proxmox", "");
|
|
|
|
let result = await requestPVE(path, "POST", req.cookies, req.body);
|
|
|
|
res.send(result.data, result.status);
|
|
|
|
});
|
|
|
|
|
2023-02-13 02:11:55 +00:00
|
|
|
app.post("/api/disk/detach", async (req, res) => {
|
2023-02-10 21:41:36 +00:00
|
|
|
let vmpath = `/nodes/${req.body.node}/${req.body.type}/${req.body.vmid}`;
|
2023-02-13 02:11:55 +00:00
|
|
|
|
|
|
|
let auth = await checkAuth(req.cookies, vmpath);
|
2023-02-24 00:17:28 +00:00
|
|
|
if (!auth) {
|
2023-02-13 02:11:55 +00:00
|
|
|
res.send({auth: auth});
|
2023-02-24 00:17:28 +00:00
|
|
|
return;
|
2023-02-13 02:11:55 +00:00
|
|
|
}
|
2023-02-24 00:17:28 +00:00
|
|
|
|
|
|
|
let method = req.body.type === "qemu" ? "POST" : "PUT";
|
|
|
|
let result = await requestPVE(`${vmpath}/config`, method, req.cookies, req.body.action, pveAPIToken);
|
|
|
|
result = await handleResponse(req.body.node, result);
|
|
|
|
res.send({auth: auth, status: result.status, data: result.data.data});
|
2023-02-10 21:41:36 +00:00
|
|
|
});
|
|
|
|
|
2023-02-13 02:11:55 +00:00
|
|
|
app.post("/api/disk/attach", async (req, res) => {
|
2023-02-10 21:41:36 +00:00
|
|
|
let vmpath = `/nodes/${req.body.node}/${req.body.type}/${req.body.vmid}`;
|
2023-02-13 02:11:55 +00:00
|
|
|
|
|
|
|
let auth = await checkAuth(req.cookies, vmpath);
|
2023-02-24 00:17:28 +00:00
|
|
|
if (!auth) {
|
2023-02-13 02:11:55 +00:00
|
|
|
res.send({auth: auth});
|
2023-02-24 00:17:28 +00:00
|
|
|
return;
|
2023-02-13 02:11:55 +00:00
|
|
|
}
|
2023-02-24 00:17:28 +00:00
|
|
|
|
|
|
|
let method = req.body.type === "qemu" ? "POST" : "PUT";
|
|
|
|
let result = await requestPVE(`${vmpath}/config`, method, req.cookies, req.body.action, pveAPIToken);
|
|
|
|
result = await handleResponse(req.body.node, result);
|
|
|
|
res.send({auth: auth, status: result.status, data: result.data.data});
|
2023-01-25 00:48:15 +00:00
|
|
|
});
|
|
|
|
|
2023-02-13 02:11:55 +00:00
|
|
|
app.post("/api/disk/resize", async (req, res) => {
|
2023-02-10 21:41:36 +00:00
|
|
|
let vmpath = `/nodes/${req.body.node}/${req.body.type}/${req.body.vmid}`;
|
2023-02-13 02:11:55 +00:00
|
|
|
|
|
|
|
let auth = await checkAuth(req.cookies, vmpath);
|
2023-02-24 00:17:28 +00:00
|
|
|
if (!auth) {
|
2023-02-13 02:11:55 +00:00
|
|
|
res.send({auth: auth});
|
2023-02-24 00:17:28 +00:00
|
|
|
return;
|
2023-02-13 02:11:55 +00:00
|
|
|
}
|
2023-02-24 00:17:28 +00:00
|
|
|
|
|
|
|
let method = "PUT";
|
|
|
|
let result = await requestPVE(`${vmpath}/resize`, method, req.cookies, req.body.action, pveAPIToken);
|
|
|
|
result = await handleResponse(req.body.node, result);
|
|
|
|
res.send({auth: auth, status: result.status, data: result.data.data});
|
2023-02-02 19:34:51 +00:00
|
|
|
});
|
|
|
|
|
2023-02-13 02:11:55 +00:00
|
|
|
app.post("/api/disk/move", async (req, res) => {
|
2023-02-10 21:41:36 +00:00
|
|
|
let vmpath = `/nodes/${req.body.node}/${req.body.type}/${req.body.vmid}`;
|
2023-02-02 19:34:51 +00:00
|
|
|
let route = req.body.type === "qemu" ? "move_disk" : "move_volume";
|
2023-02-13 02:11:55 +00:00
|
|
|
|
|
|
|
let auth = await checkAuth(req.cookies, vmpath);
|
2023-02-24 00:17:28 +00:00
|
|
|
if (!auth) {
|
2023-02-13 02:11:55 +00:00
|
|
|
res.send({auth: auth});
|
2023-02-24 00:17:28 +00:00
|
|
|
return;
|
2023-02-13 02:11:55 +00:00
|
|
|
}
|
2023-02-24 00:17:28 +00:00
|
|
|
|
|
|
|
let method = "POST";
|
|
|
|
let result = await requestPVE(`${vmpath}/${route}`, method, req.cookies, req.body.action, pveAPIToken);
|
|
|
|
result = await handleResponse(req.body.node, result);
|
|
|
|
res.send({auth: auth, status: result.status, data: result.data.data});
|
2023-02-10 21:41:36 +00:00
|
|
|
});
|
|
|
|
|
2023-02-13 02:11:55 +00:00
|
|
|
app.post("/api/disk/delete", async (req, res) => {
|
2023-02-10 21:41:36 +00:00
|
|
|
let vmpath = `/nodes/${req.body.node}/${req.body.type}/${req.body.vmid}`;
|
2023-02-13 02:11:55 +00:00
|
|
|
|
|
|
|
let auth = await checkAuth(req.cookies, vmpath);
|
2023-02-24 00:17:28 +00:00
|
|
|
if (!auth) {
|
2023-02-17 22:17:00 +00:00
|
|
|
res.send({auth: auth});
|
2023-02-24 00:17:28 +00:00
|
|
|
return;
|
2023-02-17 22:17:00 +00:00
|
|
|
}
|
2023-02-24 00:17:28 +00:00
|
|
|
|
|
|
|
let method = req.body.type === "qemu" ? "POST" : "PUT";
|
|
|
|
let result = await requestPVE(`${vmpath}/config`, method, req.cookies, req.body.action, pveAPIToken);
|
|
|
|
result = await handleResponse(req.body.node, result);
|
|
|
|
res.send({auth: auth, status: result.status, data: result.data.data});
|
2023-02-17 22:17:00 +00:00
|
|
|
});
|
2023-02-23 20:39:33 +00:00
|
|
|
|
|
|
|
app.post("/api/disk/create", async (req, res) => {
|
|
|
|
let vmpath = `/nodes/${req.body.node}/${req.body.type}/${req.body.vmid}`;
|
|
|
|
|
|
|
|
let auth = await checkAuth(req.cookies, vmpath);
|
2023-02-24 00:17:28 +00:00
|
|
|
if (!auth) {
|
2023-02-23 20:39:33 +00:00
|
|
|
res.send({auth: auth});
|
2023-02-24 00:17:28 +00:00
|
|
|
return;
|
2023-02-23 20:39:33 +00:00
|
|
|
}
|
2023-02-24 00:17:28 +00:00
|
|
|
|
|
|
|
let method = req.body.type === "qemu" ? "POST" : "PUT";
|
|
|
|
let result = await requestPVE(`${vmpath}/config`, method, req.cookies, req.body.action, pveAPIToken);
|
|
|
|
result = await handleResponse(req.body.node, result);
|
|
|
|
res.send({auth: auth, status: result.status, data: result.data.data});
|
2023-02-23 20:39:33 +00:00
|
|
|
});
|
2023-02-17 22:17:00 +00:00
|
|
|
|
|
|
|
app.post("/api/resources", async (req, res) => {
|
|
|
|
let vmpath = `/nodes/${req.body.node}/${req.body.type}/${req.body.vmid}`;
|
|
|
|
|
|
|
|
let auth = await checkAuth(req.cookies, vmpath);
|
2023-02-24 00:17:28 +00:00
|
|
|
if (!auth) {
|
2023-02-21 22:35:09 +00:00
|
|
|
res.send({auth: auth});
|
2023-02-24 00:17:28 +00:00
|
|
|
return;
|
2023-02-21 22:35:09 +00:00
|
|
|
}
|
2023-02-24 00:17:28 +00:00
|
|
|
|
|
|
|
let method = req.body.type === "qemu" ? "POST" : "PUT";
|
|
|
|
action = JSON.stringify({cores: req.body.cores, memory: req.body.memory});
|
|
|
|
let result = await requestPVE(`${vmpath}/config`, method, req.cookies, action, pveAPIToken);
|
|
|
|
result = await handleResponse(req.body.node, result);
|
|
|
|
res.send({auth: auth, status: result.status, data: result.data.data});
|
2023-02-21 22:35:09 +00:00
|
|
|
});
|
|
|
|
|
2023-02-24 00:17:28 +00:00
|
|
|
app.post("/api/instance", async (req, res) => {
|
|
|
|
let auth = await checkAuth(req.cookies);
|
|
|
|
if (!auth) {
|
|
|
|
res.send({auth: auth});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// do stuff
|
|
|
|
});
|
2023-02-21 22:35:09 +00:00
|
|
|
|
|
|
|
app.delete("/api/instance", async (req, res) => {
|
|
|
|
let vmpath = `/nodes/${req.body.node}/${req.body.type}/${req.body.vmid}`;
|
|
|
|
|
|
|
|
let auth = await checkAuth(req.cookies, vmpath);
|
2023-02-24 00:17:28 +00:00
|
|
|
if (!auth) {
|
2023-02-13 02:11:55 +00:00
|
|
|
res.send({auth: auth});
|
2023-02-24 00:17:28 +00:00
|
|
|
return;
|
2023-02-13 02:11:55 +00:00
|
|
|
}
|
2023-02-24 00:17:28 +00:00
|
|
|
|
|
|
|
let result = await requestPVE(`${vmpath}`, "DELETE", req.cookies, null, pveAPIToken);
|
|
|
|
result = await handleResponse(req.body.node, result);
|
|
|
|
res.send({auth: auth, status: result.status, data: result.data.data});
|
2023-02-13 02:11:55 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
async function checkAuth (cookies, vmpath = null) {
|
|
|
|
if (vmpath) {
|
|
|
|
let result = await requestPVE(`/${vmpath}/config`, "GET", cookies);
|
2023-02-24 00:17:28 +00:00
|
|
|
return result.status === 200;
|
2023-01-24 20:18:29 +00:00
|
|
|
}
|
2023-01-20 06:40:21 +00:00
|
|
|
else { // if no path is specified, then do a simple authentication
|
2023-02-13 02:11:55 +00:00
|
|
|
let result = await requestPVE("/version", "GET", cookies);
|
|
|
|
return result.status === 200;
|
2023-01-20 06:40:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-13 02:11:55 +00:00
|
|
|
async function requestPVE (path, method, cookies, body = null, token = null) {
|
2023-01-25 00:28:44 +00:00
|
|
|
let url = `${pveAPI}${path}`;
|
2023-01-20 06:40:21 +00:00
|
|
|
let content = {
|
|
|
|
method: method,
|
|
|
|
mode: "cors",
|
|
|
|
credentials: "include",
|
|
|
|
headers: {
|
2023-02-13 02:11:55 +00:00
|
|
|
"Content-Type": "application/x-www-form-urlencoded"
|
|
|
|
},
|
2023-01-20 06:40:21 +00:00
|
|
|
}
|
2023-01-25 02:01:47 +00:00
|
|
|
|
|
|
|
if (token) {
|
2023-01-25 00:48:15 +00:00
|
|
|
content.headers.Authorization = `PVEAPIToken=${token.user}@${token.realm}!${token.id}=${token.uuid}`;
|
2023-01-24 20:18:29 +00:00
|
|
|
}
|
2023-02-24 22:19:30 +00:00
|
|
|
else if (cookies) {
|
2023-02-13 02:11:55 +00:00
|
|
|
content.headers.CSRFPreventionToken = cookies.CSRFPreventionToken;
|
2023-01-24 20:18:29 +00:00
|
|
|
content.headers.Cookie = `PVEAuthCookie=${cookies.PVEAuthCookie}; CSRFPreventionToken=${cookies.CSRFPreventionToken}`;
|
|
|
|
}
|
2023-01-22 23:17:38 +00:00
|
|
|
|
2023-02-13 02:11:55 +00:00
|
|
|
if (body) {
|
|
|
|
content.data = JSON.parse(body);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
let response = await axios.request(url, content);
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
catch (error) {
|
2023-02-24 22:19:30 +00:00
|
|
|
return error.response;
|
2023-02-13 02:11:55 +00:00
|
|
|
}
|
2023-01-20 06:40:21 +00:00
|
|
|
}
|
|
|
|
|
2023-02-13 04:30:41 +00:00
|
|
|
async function handleResponse (node, response) {
|
|
|
|
const waitFor = delay => new Promise(resolve => setTimeout(resolve, delay));
|
|
|
|
if (response.data.data) {
|
|
|
|
let upid = response.data.data;
|
|
|
|
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-02-13 04:34:07 +00:00
|
|
|
return taskStatus;
|
2023-02-13 04:30:41 +00:00
|
|
|
}
|
|
|
|
else if (taskStatus.data.data.status === "stopped") {
|
2023-02-13 04:34:07 +00:00
|
|
|
return taskStatus;
|
2023-02-13 04:30:41 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
await waitFor(1000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2023-02-13 04:34:07 +00:00
|
|
|
return response;
|
2023-02-13 04:30:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-25 00:28:44 +00:00
|
|
|
app.listen(listenPort, () => {
|
2023-02-13 02:11:55 +00:00
|
|
|
console.log(`proxmoxaas-api v${api.version} listening on port ${listenPort}`);
|
2023-01-17 23:46:42 +00:00
|
|
|
});
|