|
|
|
@ -2,63 +2,43 @@ use std::{ |
|
|
|
|
collections::HashMap, |
|
|
|
|
time::SystemTime |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
use serde::{ |
|
|
|
|
Deserialize, |
|
|
|
|
Serialize |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
use guess_host_triple::guess_host_triple; |
|
|
|
|
|
|
|
|
|
use crate::{ |
|
|
|
|
contracts::*, |
|
|
|
|
UID, |
|
|
|
|
Uid, |
|
|
|
|
exec_job, |
|
|
|
|
utils::vec_to_string |
|
|
|
|
}; |
|
|
|
|
use u_db::IAgent; |
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)] |
|
|
|
|
pub struct Agent { |
|
|
|
|
pub agent_id: Uid, |
|
|
|
|
pub hostname: String, |
|
|
|
|
pub is_root: bool, |
|
|
|
|
pub is_root_allowed: bool, |
|
|
|
|
pub platform: String, |
|
|
|
|
pub status: Option<String>, |
|
|
|
|
pub token: Option<String>, |
|
|
|
|
pub username: String |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl Agent { |
|
|
|
|
pub async fn gather() -> Self { |
|
|
|
|
|
|
|
|
|
async fn run_cmd_fast(cmd: String) -> String { |
|
|
|
|
let job = exec_job( |
|
|
|
|
JobMeta::from_shell_arc(cmd) |
|
|
|
|
).await; |
|
|
|
|
let job_result = match job.unwrap().data.unwrap() { |
|
|
|
|
Ok(output) => output.multiline(), |
|
|
|
|
Err(e) => e.to_string() |
|
|
|
|
}; |
|
|
|
|
JobOutput::from_multiline(&job_result) |
|
|
|
|
.map(|o| vec_to_string(&o.into_appropriate())) |
|
|
|
|
.unwrap_or(job_result) |
|
|
|
|
} |
|
|
|
|
use guess_host_triple::guess_host_triple; |
|
|
|
|
|
|
|
|
|
#[cfg(unix)] |
|
|
|
|
Agent { |
|
|
|
|
agent_id: UID.clone().to_string(), |
|
|
|
|
hostname: run_cmd_fast("hostname".to_string()).await, |
|
|
|
|
is_root: &run_cmd_fast("id -u".to_string()).await == "0", |
|
|
|
|
is_root_allowed: false, //TODO
|
|
|
|
|
platform: guess_host_triple().unwrap_or("Error").to_string(), |
|
|
|
|
status: None, //TODO
|
|
|
|
|
token: None, //TODO
|
|
|
|
|
username: run_cmd_fast("id -un".to_string()).await, |
|
|
|
|
} |
|
|
|
|
pub async fn gather() -> IAgent { |
|
|
|
|
|
|
|
|
|
async fn run_cmd_fast(cmd: String) -> String { |
|
|
|
|
let job = exec_job( |
|
|
|
|
JobMeta::from_shell_arc(cmd) |
|
|
|
|
).await; |
|
|
|
|
let job_result = match job.unwrap().data.unwrap() { |
|
|
|
|
Ok(output) => output.multiline(), |
|
|
|
|
Err(e) => e.to_string() |
|
|
|
|
}; |
|
|
|
|
JobOutput::from_multiline(&job_result) |
|
|
|
|
.map(|o| vec_to_string(&o.into_appropriate())) |
|
|
|
|
.unwrap_or(job_result) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[cfg(unix)] |
|
|
|
|
IAgent { |
|
|
|
|
alias: None, |
|
|
|
|
agent_id: UID.clone().to_string(), |
|
|
|
|
hostname: run_cmd_fast("hostname".to_string()).await, |
|
|
|
|
is_root: &run_cmd_fast("id -u".to_string()).await == "0", |
|
|
|
|
is_root_allowed: false, //TODO
|
|
|
|
|
platform: guess_host_triple().unwrap_or("Error").to_string(), |
|
|
|
|
status: None, //TODO
|
|
|
|
|
token: None, //TODO
|
|
|
|
|
username: run_cmd_fast("id -un".to_string()).await, |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -68,7 +48,7 @@ mod tests { |
|
|
|
|
|
|
|
|
|
#[tokio::test] |
|
|
|
|
async fn test_gather() { |
|
|
|
|
let cli_info = Agent::gather().await; |
|
|
|
|
let cli_info = gather().await; |
|
|
|
|
assert_eq!( |
|
|
|
|
&cli_info.username, |
|
|
|
|
"root" |
|
|
|
|