From cf283a2eaaf6732623d03302ccaa4c06f884be5e Mon Sep 17 00:00:00 2001 From: _ <_@_> Date: Wed, 8 Dec 2021 16:04:45 -0600 Subject: [PATCH] :heavy_plus_sign: add `my-ips` subcommand, currently for Windows only --- src/main.rs | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/src/main.rs b/src/main.rs index 3d75f20..218f06e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -39,6 +39,8 @@ enum AppError { Message (#[from] message::MessageError), #[error (transparent)] Tlv (#[from] tlv::TlvError), + #[error (transparent)] + Utf8 (#[from] std::str::Utf8Error), } #[derive (Debug, Error)] @@ -88,6 +90,7 @@ fn main () -> Result <(), AppError> { match subcommand.as_ref ().map (|x| &x[..]) { None => return Err (CliArgError::MissingSubcommand.into ()), Some ("client") => client (args)?, + Some ("my-ips") => my_ips ()?, Some ("server") => server (args)?, Some (x) => return Err (CliArgError::UnknownSubcommand (x.to_string ()).into ()), } @@ -95,6 +98,61 @@ fn main () -> Result <(), AppError> { Ok (()) } +#[cfg(target_os = "linux")] +fn my_ips () -> Result <(), AppError> { + println! ("my-ips subcommand not implemented for linux yet"); + 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> { + use std::process::Command; + + let output = Command::new ("ipconfig") + .output ()?; + let output = output.stdout.as_slice (); + let output = std::str::from_utf8 (output)?; + + for addr in parse_ip_config_output (output) { + println! ("{:?}", addr); + } + + Ok (()) +} + +fn parse_ip_config_output (output: &str) -> Vec { + let mut addrs = vec! []; + + for line in output.lines () { + let line = line.trim_start (); + + // Maybe only works on English locales? + if ! line.starts_with ("IPv4 Address") { + continue; + } + let colon_pos = match line.find (":") { + None => continue, + Some (x) => x, + }; + let line = &line [colon_pos + 2..]; + + let addr = match Ipv4Addr::from_str (line) { + Err (_) => continue, + Ok (x) => x, + }; + + addrs.push (addr); + } + + addrs +} + struct ServerResponse { mac: Option <[u8; 6]>, nickname: Option ,