You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
113 lines
2.8 KiB
113 lines
2.8 KiB
use serde::{ |
|
Serialize, |
|
Deserialize |
|
}; |
|
use std::time::SystemTime; |
|
use std::fmt; |
|
use diesel::{ |
|
Queryable, |
|
Identifiable, |
|
Insertable |
|
}; |
|
|
|
use crate::{ |
|
models::*, |
|
UID, |
|
utils::*, |
|
models::schema::*, |
|
}; |
|
|
|
use guess_host_triple::guess_host_triple; |
|
use uuid::Uuid; |
|
|
|
//belongs_to |
|
#[derive(Clone, Debug, Serialize, Deserialize, Identifiable, Queryable)] |
|
#[table_name = "agents"] |
|
pub struct Agent { |
|
pub alias: Option<String>, |
|
pub hostname: String, |
|
pub id: Uuid, |
|
pub is_root: bool, |
|
pub is_root_allowed: bool, |
|
pub last_active: SystemTime, |
|
pub platform: String, |
|
pub regtime: SystemTime, |
|
pub status: Option<String>, |
|
pub token: Option<String>, |
|
pub username: String |
|
} |
|
|
|
impl fmt::Display for Agent { |
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
|
let mut out = format!("Agent {}", self.id); |
|
if self.alias.is_some() { |
|
out += &format!(" ({})", self.alias.as_ref().unwrap()) |
|
} |
|
out += &format!("\nHostname: {}", self.hostname); |
|
out += &format!("\nIs root: {}", self.is_root); |
|
out += &format!("\nRoot allowed: {}", self.is_root_allowed); |
|
out += &format!("\nLast active: {}", systime_to_string(&self.last_active)); |
|
out += &format!("\nPlatform: {}", self.platform); |
|
out += &format!("\nUsername: {}", self.username); |
|
write!(f, "{}", out) |
|
} |
|
} |
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize, Insertable)] |
|
#[table_name = "agents"] |
|
pub struct IAgent { |
|
pub alias: Option<String>, |
|
pub id: Uuid, |
|
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 |
|
} |
|
|
|
|
|
pub async fn gather() -> IAgent { |
|
async fn run_cmd_fast<S: Into<String>>(cmd: S) -> String { |
|
let jm = JobMeta::from_shell(cmd); |
|
let job_result = build_jobs(jm) |
|
.run_one_until_complete() |
|
.await |
|
.unwrap() |
|
.result |
|
.unwrap(); |
|
JobOutput::from_raw(&job_result) |
|
.map(|o| vec_to_string(&o.into_appropriate())) |
|
.unwrap_or(String::from_utf8_lossy(&job_result).to_string()) |
|
} |
|
|
|
#[cfg(unix)] |
|
IAgent { |
|
alias: None, |
|
id: UID.clone(), |
|
hostname: run_cmd_fast("hostname").await, |
|
is_root: &run_cmd_fast("id -u").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").await, |
|
} |
|
} |
|
|
|
|
|
#[cfg(test)] |
|
mod tests { |
|
use super::*; |
|
|
|
#[tokio::test] |
|
async fn test_gather() { |
|
let cli_info = gather().await; |
|
assert_eq!( |
|
&cli_info.username, |
|
"plazmoid" |
|
) |
|
} |
|
|
|
}
|
|
|