Skip to content

Commit

Permalink
Add get_primary_ip function to imix. (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
hulto authored Mar 23, 2023
1 parent 292de05 commit e3a5b2d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
3 changes: 2 additions & 1 deletion implants/imix/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ chrono = { version = "0.4.23" , features = ["serde"] }
tempfile = "3.3.0"
whoami = "1.3.0"
uuid = { version = "1.3.0", features = ["v4","fast-rng"] }
sys-info = "0.9.1"
default-net = "0.13.1"
sys-info = "0.9.1"
30 changes: 30 additions & 0 deletions implants/imix/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use eldritch::eldritch_run;
use uuid::Uuid;
use sys_info::{os_release,linux_os_release};


async fn install(config_path: String) -> Result<(), imix::Error> {
let config_file = File::open(config_path)?;
let config: imix::Config = serde_json::from_reader(config_file)?;
Expand Down Expand Up @@ -123,6 +124,22 @@ fn get_host_id(host_id_file_path: String) -> Result<String> {
Ok(host_id)
}

fn get_primary_ip() -> Result<String> {
let res = match default_net::get_default_interface() {
Ok(default_interface) => {
if default_interface.ipv4.len() > 0 {
default_interface.ipv4[0].addr.to_string()
}else{
"DANGER-UNKNOWN".to_string()
}
},
Err(e) => {
println!("Error getting primary ip address:\n{e}");
"DANGER-UNKNOWN".to_string()
},
};
Ok(res)
}

fn get_os_pretty_name() -> Result<String> {
if cfg!(target_os = "linux") {
Expand Down Expand Up @@ -311,6 +328,19 @@ mod tests {
use imix::{graphql::{GraphQLJob, GraphQLTome}};
use super::*;

#[test]
fn imix_test_default_ip(){
let primary_ip_address = match get_primary_ip() {
Ok(local_primary_ip) => local_primary_ip,
Err(local_error) => {
println!("An error occured during testing default_ip:{local_error}");
assert_eq!(false,true);
"DANGER-UNKNOWN".to_string()
},
};
assert!((primary_ip_address != "DANGER-UNKNOWN".to_string()))
}

#[test]
fn imix_test_get_os_pretty_name() {
let res = get_os_pretty_name().unwrap();
Expand Down

0 comments on commit e3a5b2d

Please sign in to comment.