From 39bcea54d4310b28923ae6fdd0ee2f72b1662517 Mon Sep 17 00:00:00 2001 From: _ <_@_> Date: Thu, 9 Dec 2021 02:06:01 +0000 Subject: [PATCH] :recycle: refactor: make the self-IP code available within the program --- ideas.md | 1 + src/ip.rs | 21 +++++++++++++++++++++ src/main.rs | 23 +---------------------- 3 files changed, 23 insertions(+), 22 deletions(-) diff --git a/ideas.md b/ideas.md index 1df52d0..e0ed73c 100644 --- a/ideas.md +++ b/ideas.md @@ -3,3 +3,4 @@ Cool ideas that can be done but probably won't be. - Command for shell substituting IPs into commands - Arbitrary TCP forwarding of (stdin? stdout? TCP?) - Netcat replacement "Just send a file" _including filename_ +- Public-key crypto for trusting peers on first use (Hard cause it requires mutable disk state) diff --git a/src/ip.rs b/src/ip.rs index 8c5f3df..b93b77c 100644 --- a/src/ip.rs +++ b/src/ip.rs @@ -10,6 +10,27 @@ pub enum IpError { Io (#[from] std::io::Error), #[error (transparent)] FromUtf8 (#[from] std::string::FromUtf8Error), + #[error ("Self-IP detection is not implemented on Mac OS")] + NotImplementedOnMac, +} + +#[cfg(target_os = "linux")] +pub fn get_ips () -> Result , IpError> { + let output = linux::get_ip_addr_output ()?; + + Ok (linux::parse_ip_addr_output (&output)) +} + +#[cfg(target_os = "macos")] +pub fn get_ips () -> Result , IpError> { + Err (IpError::NotImplementedOnMac) +} + +#[cfg(target_os = "windows")] +pub fn get_ips () -> Result , IpError> { + let output = windows::get_ip_config_output ()?; + + Ok (windows::parse_ip_config_output (&output)) } #[cfg(target_os = "linux")] diff --git a/src/main.rs b/src/main.rs index f7930e5..415ca8e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -45,11 +45,8 @@ async fn async_main () -> Result <(), AppError> { Ok (()) } -#[cfg(target_os = "linux")] fn my_ips () -> Result <(), AppError> { - let output = ip::linux::get_ip_addr_output ()?; - - for addr in ip::linux::parse_ip_addr_output (&output) + for addr in ip::get_ips ()? .iter () .filter (|a| ! a.is_loopback ()) { @@ -58,21 +55,3 @@ fn my_ips () -> Result <(), AppError> { Ok (()) } - -#[cfg(target_os = "macos")] -fn my_ips () -> Result <(), AppError> { - println! ("my-ips subcommand not implemented for macos"); - Ok (()) -} - -#[cfg(target_os = "windows")] -fn my_ips () -> Result <(), AppError> { - let output = ip::windows::get_ip_config_output ()?; - - for addr in ip::windows::parse_ip_config_output (&output) { - println! ("{:?}", addr); - } - - Ok (()) -} -