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-17 23:46:42 +00:00
|
|
|
|
|
|
|
const app = express();
|
|
|
|
app.use(helmet());
|
|
|
|
app.use(bodyParser.json());
|
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) => {
|
|
|
|
res.send(result);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function checkAuth (cookies, callback, vmpath = null) {
|
|
|
|
if (vmpath) {}
|
|
|
|
else { // if no path is specified, then do a simple authentication
|
|
|
|
requestPVE("/version", "GET", cookies, (result) => {
|
|
|
|
callback(result);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function requestPVE (path, method, cookies, callback, body = null, auth = true) {
|
|
|
|
let prms = new URLSearchParams(body);
|
|
|
|
let content = {
|
|
|
|
hostname: "pve.tronnet.net",
|
|
|
|
port: 443,
|
|
|
|
path: `/api2/json${path}`,
|
|
|
|
method: method,
|
|
|
|
mode: "cors",
|
|
|
|
credentials: "include",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/x-www-form-urlencoded",
|
|
|
|
Cookie: `PVEAuthCookie=${cookies.PVEAuthCookie}; CSRFPreventionToken=${cookies.CSRFPreventionToken}`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (method === "POST") {
|
|
|
|
content.body = prms.toString();
|
|
|
|
content.headers.CSRFPreventionToken = cookies.CSRFPreventionToken;
|
|
|
|
}
|
|
|
|
https.request(content, (res) => {
|
|
|
|
let data = "";
|
|
|
|
res.on("data", (d) => {
|
|
|
|
data += d.toString();
|
|
|
|
});
|
|
|
|
res.on("end", () => {
|
|
|
|
try{
|
|
|
|
callback(JSON.parse(data));
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
callback(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}).on("error", (e) => {callback(e)}).end();
|
|
|
|
}
|
|
|
|
|
2023-01-17 23:46:42 +00:00
|
|
|
app.listen(80, () => {
|
2023-01-19 20:22:26 +00:00
|
|
|
console.log("listening on port 80");
|
2023-01-17 23:46:42 +00:00
|
|
|
});
|