From b2ef65dbb89db335768a8c1c3bc038fbfddebd5e Mon Sep 17 00:00:00 2001 From: Mathias Wagner Date: Wed, 5 Apr 2023 13:51:47 +0200 Subject: [PATCH] Migrated to axios & added the checkNode & proxyRequest methods to the node controller --- server/controller/node.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/server/controller/node.js b/server/controller/node.js index 56a880fb..9464ecbe 100644 --- a/server/controller/node.js +++ b/server/controller/node.js @@ -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); } \ No newline at end of file