From 5424fc3858d45e25c43f3799b60a350b36a12707 Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Wed, 9 Nov 2022 22:34:50 -0500 Subject: [PATCH] feat(striker-ui-api): add /command/inquire-peer --- .../request_handlers/command/getHostSSH.ts | 65 +++++++++++++++++++ .../src/lib/request_handlers/command/index.ts | 1 + striker-ui-api/src/routes/command.ts | 2 + 3 files changed, 68 insertions(+) create mode 100644 striker-ui-api/src/lib/request_handlers/command/getHostSSH.ts diff --git a/striker-ui-api/src/lib/request_handlers/command/getHostSSH.ts b/striker-ui-api/src/lib/request_handlers/command/getHostSSH.ts new file mode 100644 index 00000000..7a8629da --- /dev/null +++ b/striker-ui-api/src/lib/request_handlers/command/getHostSSH.ts @@ -0,0 +1,65 @@ +import { RequestHandler } from 'express'; + +import { sub } from '../../accessModule'; +import { stderr } from '../../shell'; + +export const getHostSSH: RequestHandler< + unknown, + { + hostName: string; + hostOS: string; + hostUUID: string; + isConnected: boolean; + isInetConnected: boolean; + isOSRegistered: boolean; + }, + { + password: string; + port?: number; + ipAddress: string; + } +> = (request, response) => { + const { + body: { password, port = 22, ipAddress: target }, + } = request; + + let hostName: string, + hostOS: string, + hostUUID: string, + rawIsInetConnected: string, + rawIsOSRegistered: string; + + try { + ({ + host_name: hostName, + host_os: hostOS, + host_uuid: hostUUID, + internet: rawIsInetConnected, + os_registered: rawIsOSRegistered, + } = sub('get_peer_data', { + subModuleName: 'Striker', + subParams: { password, port, target }, + }).stdout as { + host_name: string; + host_os: string; + host_uuid: string; + internet: string; + os_registered: string; + }); + } catch (subError) { + stderr(`Failed to get peer data; CAUSE: ${subError}`); + + response.status(500).send(); + + return; + } + + response.status(200).send({ + hostName, + hostOS, + hostUUID, + isConnected: hostName.length > 0, + isInetConnected: rawIsInetConnected === '1', + isOSRegistered: rawIsOSRegistered === 'yes', + }); +}; diff --git a/striker-ui-api/src/lib/request_handlers/command/index.ts b/striker-ui-api/src/lib/request_handlers/command/index.ts index 5e9c62c9..b42e11b0 100644 --- a/striker-ui-api/src/lib/request_handlers/command/index.ts +++ b/striker-ui-api/src/lib/request_handlers/command/index.ts @@ -1,3 +1,4 @@ +export * from './getHostSSH'; export * from './poweroffHost'; export * from './rebootHost'; export * from './updateSystem'; diff --git a/striker-ui-api/src/routes/command.ts b/striker-ui-api/src/routes/command.ts index 019948bf..5f7970d1 100644 --- a/striker-ui-api/src/routes/command.ts +++ b/striker-ui-api/src/routes/command.ts @@ -1,6 +1,7 @@ import express from 'express'; import { + getHostSSH, poweroffHost, rebootHost, updateSystem, @@ -9,6 +10,7 @@ import { const router = express.Router(); router + .put('/inquire-peer', getHostSSH) .put('/poweroff-host', poweroffHost) .put('/reboot-host', rebootHost) .put('/update-system', updateSystem);