implement disk detach

This commit is contained in:
Arthur Lu 2023-01-25 02:01:47 +00:00
parent 5b5fc2b930
commit 41ac95d16f

View File

@ -11,7 +11,7 @@ const {pveAPI, pveAPIToken, listenPort} = require("./vars.js")
const app = express(); const app = express();
app.use(helmet()); app.use(helmet());
app.use(bodyParser.json()); app.use(bodyParser.urlencoded({extended: true}));
app.use(cookieParser()) app.use(cookieParser())
app.use(cors()); app.use(cors());
app.use(morgan("combined")); app.use(morgan("combined"));
@ -28,25 +28,26 @@ app.get("/api/echo", (req, res) => {
app.get("/api/auth", (req, res) => { app.get("/api/auth", (req, res) => {
checkAuth(req.cookies, (result) => { checkAuth(req.cookies, (result) => {
res.send({auth: result}); res.send({auth: result});
}, req.body.vmpath); });
}); });
app.get("/api/disk/detach", (req, res) => { app.post("/api/disk/detach", (req, res) => {
let vmpath = `nodes/${req.body.node}/${req.body.type}/${req.body.vmid}`;
checkAuth(req.cookies, (result) => { checkAuth(req.cookies, (result) => {
if (result) { if (result) {
requestPVE(`${body.vmpath}/config`, "PUT", null, (result) => { requestPVE(`/${vmpath}/config`, "POST", req.cookies, (result) => {
res.send(result); res.send(result);
}, body = req.body.action, token = pveAPIToken); }, body = req.body.action, token = pveAPIToken);
} }
else { else {
res.send({auth: result}); res.send({auth: result});
} }
}, req.body.vmpath); }, vmpath);
}); });
function checkAuth (cookies, callback, vmpath = null) { function checkAuth (cookies, callback, vmpath = null) {
if (vmpath) { if (vmpath) {
requestPVE(`${vmpath}/config`, "GET", cookies, (result) => { requestPVE(`/${vmpath}/config`, "GET", cookies, (result) => {
if(result.status === 200){ if(result.status === 200){
callback(true); callback(true);
} }
@ -68,7 +69,6 @@ function checkAuth (cookies, callback, vmpath = null) {
} }
function requestPVE (path, method, cookies, callback, body = null, token = null) { function requestPVE (path, method, cookies, callback, body = null, token = null) {
let prms = new URLSearchParams(body);
let url = `${pveAPI}${path}`; let url = `${pveAPI}${path}`;
let content = { let content = {
method: method, method: method,
@ -79,10 +79,10 @@ function requestPVE (path, method, cookies, callback, body = null, token = null)
} }
} }
if (method === "POST") { if (method === "POST") {
content.body = prms.toString();
content.headers.CSRFPreventionToken = cookies.CSRFPreventionToken; content.headers.CSRFPreventionToken = cookies.CSRFPreventionToken;
} }
if(token) {
if (token) {
content.headers.Authorization = `PVEAPIToken=${token.user}@${token.realm}!${token.id}=${token.uuid}`; content.headers.Authorization = `PVEAPIToken=${token.user}@${token.realm}!${token.id}=${token.uuid}`;
} }
else { else {
@ -97,7 +97,6 @@ function requestPVE (path, method, cookies, callback, body = null, token = null)
}; };
const request = https.request(url, content); const request = https.request(url, content);
request.on("error", reject); request.on("error", reject);
request.on("response", response => { request.on("response", response => {
response.setEncoding("utf8"); response.setEncoding("utf8");
@ -112,6 +111,10 @@ function requestPVE (path, method, cookies, callback, body = null, token = null)
}); });
}); });
if (method === "POST") {
request.write(body);
}
request.end(); request.end();
}); });