Migrated to axios & added the checkNode & proxyRequest methods to the node controller

This commit is contained in:
Mathias Wagner
2023-04-05 13:51:47 +02:00
parent 256b258641
commit b2ef65dbb8

View File

@@ -1,3 +1,4 @@
const axios = require('axios');
const nodes = require('../models/Node');
// Gets all node entries
@@ -28,4 +29,34 @@ module.exports.updateName = async (nodeId, name) => {
// Update the password of the node entry
module.exports.updatePassword = async (nodeId, password) => {
return await nodes.update({password: password}, {where: {id: nodeId}});
}
module.exports.checkNode = async (url, password) => {
const api = await axios.get(url + "/api/config", {headers: {password: password}}).catch(() => {
return "INVALID_URL";
});
if (api === "INVALID_URL" || api.status !== 200) return "INVALID_URL";
if (api.data.viewMode) return "PASSWORD_REQUIRED";
return "NODE_VALID";
}
module.exports.proxyRequest = async (url, req, res) => {
const response = await axios(url, {
method: req.method,
headers: req.headers,
data: req.method === "GET" ? undefined : JSON.stringify(req.body),
signal: req.signal,
validateStatus: (status) => status >= 200 && status < 400
}).catch(() => "INVALID_URL");
if (response === "INVALID_URL")
return res.status(500).json({message: "Internal server error"});
if (response.headers["content-disposition"])
res.setHeader("content-disposition", response.headers["content-disposition"]);
res.status(response.status).json(response.data);
}