From e63111e64fbec61943e9bd691adc8363d14503f8 Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Sun, 30 Jan 2022 00:51:28 +0000 Subject: [PATCH] Add based query response system, info broker The new InfoBroker class' job is to fetch system information and return it in a standardised format. --- src/lib/SystemQuery.mjs | 11 +++++++++++ src/lib/core/InfoBroker.mjs | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 src/lib/core/InfoBroker.mjs diff --git a/src/lib/SystemQuery.mjs b/src/lib/SystemQuery.mjs index 8409940..3d20295 100644 --- a/src/lib/SystemQuery.mjs +++ b/src/lib/SystemQuery.mjs @@ -3,10 +3,12 @@ import sysinfo from 'systeminformation'; import Agent from './agent/Agent.mjs'; +import InfoBroker from './core/InfoBroker.mjs'; class SystemQuery { constructor(config) { this.config = config; + this.info = new InfoBroker(); } async init() { @@ -24,7 +26,16 @@ class SystemQuery { } async handle_query(peer, msg) { + // 1: Validate input + if(typeof msg.table !== "string" + || !this.info.is_valid_table(msg.table)) return; + // 2: Fetch system info + let table = await this.info.fetch_table(msg.table); + if(table === null) return; + + // 3: Return to requester + await peer.send("query-response", { result: table }); } async handle_query_response(peer, msg) { diff --git a/src/lib/core/InfoBroker.mjs b/src/lib/core/InfoBroker.mjs new file mode 100644 index 0000000..6df6744 --- /dev/null +++ b/src/lib/core/InfoBroker.mjs @@ -0,0 +1,32 @@ +"use strict"; + +import sysinfo from 'systeminformation'; + +import log from '../../lib/io/NamespacedLog.mjs'; const l = log("infobroker"); + +class InfoBroker { + constructor() { + this.allowed_tables = { + // name → sysinfo name + cpu: "cpu", + }; + } + + is_valid_table(name) { + return Object.keys(this.allowed_tables).includes(name); + } + + async fetch_table(name) { + if(!(typeof name === "string")) + throw new Exception(`Error: Expected name to be of type string, but received value of type ${typeof name} instead.`); + + if(!this.is_valid_table(name)) { + l.warn(`Unrecognised table '${name}' requested, returning null`); + return null; + } + + return await sysinfo[this.allowed_tables[name]](); + } +} + +export default InfoBroker;