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-01-20 06:40:21 +00:00
|
|
|
const https = require("https");
|
2023-01-19 20:22:26 +00:00
|
|
|
var package = require("./package.json");
|
2023-01-25 00:28:44 +00:00
|
|
|
|
2023-01-25 00:48:15 +00:00
|
|
|
const {pveAPI, pveAPIToken, listenPort} = require("./vars.js")
|
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-01-17 23:46:42 +00:00
|
|
|
res.send({version: package.version});
|
|
|
|
});
|
|
|
|
|
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-01-20 06:40:21 +00:00
|
|
|
app.get("/api/auth", (req, res) => {
|
|
|
|
checkAuth(req.cookies, (result) => {
|
2023-01-24 20:18:29 +00:00
|
|
|
res.send({auth: result});
|
2023-01-25 02:01:47 +00:00
|
|
|
});
|
2023-01-20 06:40:21 +00:00
|
|
|
});
|
|
|
|
|
2023-01-25 02:01:47 +00:00
|
|
|
app.post("/api/disk/detach", (req, res) => {
|
|
|
|
let vmpath = `nodes/${req.body.node}/${req.body.type}/${req.body.vmid}`;
|
2023-01-25 00:48:15 +00:00
|
|
|
checkAuth(req.cookies, (result) => {
|
|
|
|
if (result) {
|
2023-01-26 07:53:56 +00:00
|
|
|
let method = req.body.type === "qemu" ? "POST" : "PUT";
|
|
|
|
requestPVE(`/${vmpath}/config`, method, req.cookies, (result) => {
|
2023-01-25 00:48:15 +00:00
|
|
|
res.send(result);
|
|
|
|
}, body = req.body.action, token = pveAPIToken);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
res.send({auth: result});
|
|
|
|
}
|
2023-01-25 02:01:47 +00:00
|
|
|
}, vmpath);
|
2023-01-25 00:48:15 +00:00
|
|
|
});
|
|
|
|
|
2023-01-30 23:19:24 +00:00
|
|
|
app.post("/api/disk/resize", (req, res) => {
|
|
|
|
let vmpath = `nodes/${req.body.node}/${req.body.type}/${req.body.vmid}`;
|
|
|
|
checkAuth(req.cookies, (result) => {
|
|
|
|
if (result) {
|
|
|
|
let method = "PUT";
|
|
|
|
requestPVE(`/${vmpath}/resize`, method, req.cookies, (result) => {
|
|
|
|
res.send(result);
|
|
|
|
}, body = req.body.action, token = pveAPIToken);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
res.send({auth: result});
|
|
|
|
}
|
|
|
|
}, vmpath);
|
2023-02-02 19:34:51 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
app.post("/api/disk/move", (req, res) => {
|
|
|
|
let vmpath = `nodes/${req.body.node}/${req.body.type}/${req.body.vmid}`;
|
|
|
|
let route = req.body.type === "qemu" ? "move_disk" : "move_volume";
|
|
|
|
checkAuth(req.cookies, (result) => {
|
|
|
|
if (result) {
|
|
|
|
let method = "POST";
|
|
|
|
requestPVE(`/${vmpath}/${route}`, method, req.cookies, (result) => {
|
|
|
|
res.send(result);
|
|
|
|
}, body = req.body.action, token = pveAPIToken);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
res.send({auth: result});
|
|
|
|
}
|
|
|
|
}, vmpath);
|
2023-01-30 23:19:24 +00:00
|
|
|
});
|
|
|
|
|
2023-01-20 06:40:21 +00:00
|
|
|
function checkAuth (cookies, callback, vmpath = null) {
|
2023-01-24 20:18:29 +00:00
|
|
|
if (vmpath) {
|
2023-01-25 02:01:47 +00:00
|
|
|
requestPVE(`/${vmpath}/config`, "GET", cookies, (result) => {
|
2023-01-24 20:18:29 +00:00
|
|
|
if(result.status === 200){
|
|
|
|
callback(true);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
callback(false);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2023-01-20 06:40:21 +00:00
|
|
|
else { // if no path is specified, then do a simple authentication
|
|
|
|
requestPVE("/version", "GET", cookies, (result) => {
|
2023-01-24 20:18:29 +00:00
|
|
|
if(result.status === 200){
|
|
|
|
callback(true);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
callback(false);
|
|
|
|
}
|
2023-01-20 06:40:21 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-24 20:18:29 +00:00
|
|
|
function requestPVE (path, method, cookies, callback, 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-01-26 07:53:56 +00:00
|
|
|
"Content-Type": "application/x-www-form-urlencoded",
|
|
|
|
"CSRFPreventionToken": cookies.CSRFPreventionToken
|
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
|
|
|
}
|
|
|
|
else {
|
|
|
|
content.headers.Cookie = `PVEAuthCookie=${cookies.PVEAuthCookie}; CSRFPreventionToken=${cookies.CSRFPreventionToken}`;
|
|
|
|
}
|
2023-01-22 23:17:38 +00:00
|
|
|
|
|
|
|
const promiseResponse = new Promise((resolve, reject) => {
|
|
|
|
const fullResponse = {
|
2023-01-25 00:48:15 +00:00
|
|
|
status: "",
|
|
|
|
body: "",
|
|
|
|
headers: ""
|
2023-01-22 23:17:38 +00:00
|
|
|
};
|
|
|
|
|
2023-01-25 00:28:44 +00:00
|
|
|
const request = https.request(url, content);
|
2023-01-25 00:48:15 +00:00
|
|
|
request.on("error", reject);
|
|
|
|
request.on("response", response => {
|
|
|
|
response.setEncoding("utf8");
|
2023-01-22 23:17:38 +00:00
|
|
|
fullResponse.status = response.statusCode;
|
|
|
|
fullResponse.headers = response.headers;
|
2023-01-25 00:48:15 +00:00
|
|
|
response.on("data", chunk => { fullResponse.body += chunk; });
|
|
|
|
response.on("end", () => {
|
2023-01-22 23:17:38 +00:00
|
|
|
if(fullResponse.body){
|
|
|
|
fullResponse.body = JSON.parse(fullResponse.body);
|
|
|
|
}
|
|
|
|
resolve(fullResponse);
|
|
|
|
});
|
2023-01-20 06:40:21 +00:00
|
|
|
});
|
2023-01-25 02:01:47 +00:00
|
|
|
|
2023-01-26 07:53:56 +00:00
|
|
|
if (body) {
|
2023-01-30 23:19:24 +00:00
|
|
|
let prms = new URLSearchParams(JSON.parse(body));
|
|
|
|
request.write(prms.toString());
|
2023-01-25 02:01:47 +00:00
|
|
|
}
|
2023-01-22 23:17:38 +00:00
|
|
|
|
|
|
|
request.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
promiseResponse.then(
|
|
|
|
response => {callback(response);},
|
|
|
|
error => {callback(error);}
|
|
|
|
);
|
2023-01-20 06:40:21 +00:00
|
|
|
}
|
|
|
|
|
2023-01-25 00:28:44 +00:00
|
|
|
app.listen(listenPort, () => {
|
2023-01-25 00:48:15 +00:00
|
|
|
console.log(`listening on port ${listenPort}`);
|
2023-01-17 23:46:42 +00:00
|
|
|
});
|