add detach disk path,
add pveAPIToken config item
This commit is contained in:
parent
85d57b361b
commit
5b5fc2b930
37
index.js
37
index.js
@ -7,7 +7,7 @@ const morgan = require("morgan");
|
|||||||
const https = require("https");
|
const https = require("https");
|
||||||
var package = require("./package.json");
|
var package = require("./package.json");
|
||||||
|
|
||||||
const {pveAPI, listenPort} = require("./vars.js")
|
const {pveAPI, pveAPIToken, listenPort} = require("./vars.js")
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
app.use(helmet());
|
app.use(helmet());
|
||||||
@ -31,9 +31,22 @@ app.get("/api/auth", (req, res) => {
|
|||||||
}, req.body.vmpath);
|
}, req.body.vmpath);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
app.get("/api/disk/detach", (req, res) => {
|
||||||
|
checkAuth(req.cookies, (result) => {
|
||||||
|
if (result) {
|
||||||
|
requestPVE(`${body.vmpath}/config`, "PUT", null, (result) => {
|
||||||
|
res.send(result);
|
||||||
|
}, body = req.body.action, token = pveAPIToken);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
res.send({auth: result});
|
||||||
|
}
|
||||||
|
}, req.body.vmpath);
|
||||||
|
});
|
||||||
|
|
||||||
function checkAuth (cookies, callback, vmpath = null) {
|
function checkAuth (cookies, callback, vmpath = null) {
|
||||||
if (vmpath) {
|
if (vmpath) {
|
||||||
requestPVE(vmpath, "GET", cookies, (result) => {
|
requestPVE(`${vmpath}/config`, "GET", cookies, (result) => {
|
||||||
if(result.status === 200){
|
if(result.status === 200){
|
||||||
callback(true);
|
callback(true);
|
||||||
}
|
}
|
||||||
@ -70,7 +83,7 @@ function requestPVE (path, method, cookies, callback, body = null, token = null)
|
|||||||
content.headers.CSRFPreventionToken = cookies.CSRFPreventionToken;
|
content.headers.CSRFPreventionToken = cookies.CSRFPreventionToken;
|
||||||
}
|
}
|
||||||
if(token) {
|
if(token) {
|
||||||
content.headers.Authorization = `PVEAPIToken=${token.user}@${token.realm}!${TOKENID}=${token.uuid}`;
|
content.headers.Authorization = `PVEAPIToken=${token.user}@${token.realm}!${token.id}=${token.uuid}`;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
content.headers.Cookie = `PVEAuthCookie=${cookies.PVEAuthCookie}; CSRFPreventionToken=${cookies.CSRFPreventionToken}`;
|
content.headers.Cookie = `PVEAuthCookie=${cookies.PVEAuthCookie}; CSRFPreventionToken=${cookies.CSRFPreventionToken}`;
|
||||||
@ -78,20 +91,20 @@ function requestPVE (path, method, cookies, callback, body = null, token = null)
|
|||||||
|
|
||||||
const promiseResponse = new Promise((resolve, reject) => {
|
const promiseResponse = new Promise((resolve, reject) => {
|
||||||
const fullResponse = {
|
const fullResponse = {
|
||||||
status: '',
|
status: "",
|
||||||
body: '',
|
body: "",
|
||||||
headers: ''
|
headers: ""
|
||||||
};
|
};
|
||||||
|
|
||||||
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");
|
||||||
fullResponse.status = response.statusCode;
|
fullResponse.status = response.statusCode;
|
||||||
fullResponse.headers = response.headers;
|
fullResponse.headers = response.headers;
|
||||||
response.on('data', chunk => { fullResponse.body += chunk; });
|
response.on("data", chunk => { fullResponse.body += chunk; });
|
||||||
response.on('end', () => {
|
response.on("end", () => {
|
||||||
if(fullResponse.body){
|
if(fullResponse.body){
|
||||||
fullResponse.body = JSON.parse(fullResponse.body);
|
fullResponse.body = JSON.parse(fullResponse.body);
|
||||||
}
|
}
|
||||||
@ -109,5 +122,5 @@ function requestPVE (path, method, cookies, callback, body = null, token = null)
|
|||||||
}
|
}
|
||||||
|
|
||||||
app.listen(listenPort, () => {
|
app.listen(listenPort, () => {
|
||||||
console.log("listening on port 80");
|
console.log(`listening on port ${listenPort}`);
|
||||||
});
|
});
|
@ -1,4 +1,10 @@
|
|||||||
const pveAPI = "";
|
const pveAPI = "";
|
||||||
|
const pveAPIToken = {
|
||||||
|
user: "",
|
||||||
|
realm: "",
|
||||||
|
id: "",
|
||||||
|
uuid: ""
|
||||||
|
};
|
||||||
const listenPort = 80;
|
const listenPort = 80;
|
||||||
|
|
||||||
module.exports = {pveAPI, listenPort};
|
module.exports = {pveAPI, listenPort, pveAPIToken};
|
Loading…
Reference in New Issue
Block a user