diff --git a/src/main.js b/src/main.js index 48209a8..b5d4eef 100644 --- a/src/main.js +++ b/src/main.js @@ -7,7 +7,7 @@ import morgan from "morgan"; import api from "./package.js"; import * as pve from "./pve.js"; import * as utils from "./utils.js"; -import db from "./db.js"; +import LocalDB from "./db.js"; import parseArgs from "minimist"; global.argv = parseArgs(process.argv.slice(2), { @@ -19,16 +19,16 @@ global.argv = parseArgs(process.argv.slice(2), { global.api = api; global.pve = pve; global.utils = utils; -global.db = new db(global.argv.localdb); +global.db = new LocalDB(global.argv.localdb); const app = express(); global.app = app; app.use(bodyParser.urlencoded({ extended: true })); app.use(cookieParser()); -app.use(cors({ origin: db.hostname })); +app.use(cors({ origin: global.db.hostname })); app.use(morgan("combined")); -global.server = app.listen(db.listenPort, () => { +global.server = app.listen(global.db.listenPort, () => { console.log(`proxmoxaas-api v${api.version} listening on port ${global.db.listenPort}`); }); diff --git a/src/pve.js b/src/pve.js index f99934c..3e10a27 100644 --- a/src/pve.js +++ b/src/pve.js @@ -1,7 +1,4 @@ import axios from "axios"; -import db from "./db.js"; -const pveAPI = db.pveAPI; -const pveAPIToken = db.pveAPIToken; /** * Send HTTP request to proxmox API. Allows requests to be made with user cookie credentials or an API token for controlled priviledge elevation. @@ -13,6 +10,7 @@ const pveAPIToken = db.pveAPIToken; * @returns {Obejct} HTTP response object or HTTP error object */ export async function requestPVE (path, method, cookies, body = null, token = null) { + const pveAPI = global.db.pveAPI; const url = `${pveAPI}${path}`; const content = { method, @@ -53,6 +51,7 @@ export async function requestPVE (path, method, cookies, body = null, token = nu * @param {Object} res response object of ProxmoxAAS API call. */ export async function handleResponse (node, result, res) { + const pveAPIToken = global.db.pveAPIToken; const waitFor = delay => new Promise(resolve => setTimeout(resolve, delay)); if (result.data.data && typeof (result.data.data) === "string" && result.data.data.startsWith("UPID:")) { const upid = result.data.data; @@ -144,6 +143,7 @@ export async function getUsedResources (req, resourceMeta) { * @returns {Objetc} k-v pairs of specific disk data, including storage and size of unused disks. */ export async function getDiskInfo (node, type, vmid, disk) { + const pveAPIToken = global.db.pveAPIToken; try { const config = await requestPVE(`/nodes/${node}/${type}/${vmid}/config`, "GET", null, null, pveAPIToken); const storageID = config.data.data[disk].split(":")[0]; @@ -166,6 +166,7 @@ export async function getDiskInfo (node, type, vmid, disk) { * @returns {Object} k-v pairs of specific device data, including device name and manufacturer. */ export async function getDeviceInfo (node, type, vmid, qid) { + const pveAPIToken = global.db.pveAPIToken; try { const result = (await requestPVE(`/nodes/${node}/hardware/pci`, "GET", null, null, pveAPIToken)).data.data; const deviceData = []; @@ -193,6 +194,7 @@ export async function getDeviceInfo (node, type, vmid, qid) { * @returns {Array.} array of k-v pairs of specific device data, including device name and manufacturer, which are available on the specified node. */ export async function getNodeAvailDevices (node, cookies) { + const pveAPIToken = global.db.pveAPIToken; // get node pci devices let nodeAvailPci = (await requestPVE(`/nodes/${node}/hardware/pci`, "GET", cookies, null, pveAPIToken)).data.data; // for each node container, get its config and remove devices which are already used diff --git a/src/routes/proxmox.js b/src/routes/proxmox.js index 6437ff9..3e1aedf 100644 --- a/src/routes/proxmox.js +++ b/src/routes/proxmox.js @@ -8,6 +8,7 @@ const requestPVE = global.pve.requestPVE; * request and responses passed through to/from proxmox */ router.get("/*", async (req, res) => { // proxy endpoint for GET proxmox api with no token + console.log(req.url); const path = req.url.replace("/api/proxmox", ""); const result = await requestPVE(path, "GET", req.cookies); res.status(result.status).send(result.data); diff --git a/src/utils.js b/src/utils.js index 055c13f..eca8251 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,7 +1,6 @@ import { createHash } from "crypto"; import { getUsedResources, requestPVE } from "./pve.js"; -import db from "./db.js"; /** * Check if a user is authorized to access a specified vm, or the cluster in general. @@ -11,6 +10,7 @@ import db from "./db.js"; * @returns {boolean} true if the user is authorized to access the specific vm or cluster in general, false otheriwse. */ export async function checkAuth (cookies, res, vmpath = null) { + const db = global.db; let auth = false; if (db.getUserConfig(cookies.username) === null) { @@ -43,6 +43,7 @@ export async function checkAuth (cookies, res, vmpath = null) { * @returns {{used: Object, avail: Object, max: Object, resources: Object}} used, available, maximum, and resource metadata for the specified user. */ export async function getUserResources (req, username) { + const db = global.db; const dbResources = db.getGlobalConfig().resources; const used = await getUsedResources(req, dbResources); const max = db.getUserConfig(username).resources.max;