|
|
|
use std::{
|
|
|
|
collections::HashMap
|
|
|
|
};
|
|
|
|
|
|
|
|
use serde::{
|
|
|
|
Deserialize,
|
|
|
|
Serialize
|
|
|
|
};
|
|
|
|
use uuid::Uuid;
|
|
|
|
use crate::{contracts::*, UID, exec_job};
|
|
|
|
|
|
|
|
|
|
|
|
pub struct UClient {
|
|
|
|
pub client_info: ClientInfo,
|
|
|
|
pub jobs: JobMetaStorage,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl UClient {
|
|
|
|
pub fn new(client_info: ClientInfo) -> Self {
|
|
|
|
Self {
|
|
|
|
client_info,
|
|
|
|
jobs: HashMap::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
|
|
|
pub struct ClientInfo {
|
|
|
|
pub info: HashMap<String, String>,
|
|
|
|
pub id: Uuid,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ClientInfo {
|
|
|
|
pub async fn gather() -> Self {
|
|
|
|
let mut info: HashMap<String, String> = HashMap::new();
|
|
|
|
for job in DEFAULT_JOBS {
|
|
|
|
let job_meta = JobMeta::from_shell(job.1.into()).into_arc();
|
|
|
|
let job_result = exec_job(job_meta.clone()).await;
|
|
|
|
let job_data = match job_result.unwrap().data.unwrap() {
|
|
|
|
Ok(output) => output.multiline(),
|
|
|
|
Err(e) => e.to_string()
|
|
|
|
};
|
|
|
|
info.insert(job.0.into(), job_data);
|
|
|
|
}
|
|
|
|
ClientInfo {
|
|
|
|
info,
|
|
|
|
id: *UID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_field(&self, field: &str) -> Option<&String> {
|
|
|
|
self.info.get(field)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const DEFAULT_JOBS: &[(&str, &str)] = &[
|
|
|
|
//("local ip", "ip a"),
|
|
|
|
("hostname", "hostname"),
|
|
|
|
("username", "whoami"),
|
|
|
|
("platform", "uname -a"),
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use crate::{
|
|
|
|
utils::vec_to_string
|
|
|
|
};
|
|
|
|
use std::time::SystemTime;
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_gather() {
|
|
|
|
let cli_info = ClientInfo::gather().await;
|
|
|
|
let field = cli_info.get_field("username").unwrap();
|
|
|
|
let stdout = JobOutput::from_multiline(field).unwrap().stdout;
|
|
|
|
assert_eq!(
|
|
|
|
&vec_to_string(&stdout),
|
|
|
|
"root"
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|