diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 6e1a8fd..b657021 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -11,8 +11,8 @@ jobs: strategy: fail-fast: false matrix: - platform: [windows-latest] - # platform: [macos-latest, ubuntu-20.04, windows-latest] + #platform: [windows-latest] + platform: [macos-latest, ubuntu-20.04, windows-latest] runs-on: ${{ matrix.platform }} steps: diff --git a/src-tauri/src/utils.rs b/src-tauri/src/utils.rs index 58b03e2..ac53125 100644 --- a/src-tauri/src/utils.rs +++ b/src-tauri/src/utils.rs @@ -27,8 +27,8 @@ pub fn ping_addr(addr: &str, timeout: u32) -> String { #[cfg(target_os = "macos")] pub fn ping_addr(addr: &str, timeout: u32) -> String { - let output = Command::new("sh") - .args(&["-c", "ping", "-c", "3", "-t", &(timeout / 1000).to_string(), addr]) + let output = Command::new("ping") + .args(&["-c", "3", "-t", &(timeout / 1000).to_string(), addr]) .output() .expect("failed to ping"); @@ -37,8 +37,8 @@ pub fn ping_addr(addr: &str, timeout: u32) -> String { #[cfg(target_os = "linux")] pub fn ping_addr(addr: &str, timeout: u32) -> String { - let output = Command::new("sh") - .args(&["-c", "ping", "-c", "3", "-t", &(timeout / 1000).to_string(), addr]) + let output = Command::new("ping") + .args(&["-c", "3", "-W", &(timeout / 1000).to_string(), addr]) .output() .expect("failed to ping"); @@ -106,30 +106,18 @@ pub fn get_ping_res_ms(res: &str) -> i16 { let mut ms = -1; if let Some(t) = res.lines().last() { - for packets_res in t.split(",") { - if packets_res.trim().is_empty() { - continue; - } - - // eg1: [Average, 23ms] - // eg2: [Lost, 3 (100% loss) ] - let mut loop_key = String::new(); - let mut loop_value = String::new(); - - for (index, kv_res) in packets_res.split("=").enumerate() { - let kv_res = kv_res.trim(); - if kv_res.is_empty() { + if t.contains("=") { + for ping_res in t.split("=") { + if ping_res.starts_with("round-trip") { continue; } - if index == 0 { - loop_key = kv_res.to_string(); - } else if index == 1 { - loop_value = kv_res.to_string(); - } - } - if loop_key == AVERAGE_KEY { - ms = loop_value.replace("ms", "").parse().unwrap(); + for (index, res_ms) in ping_res.split("/").enumerate() { + if index == 1 { + ms = res_ms.parse::().unwrap().floor() as i16; + break; + } + } } } } @@ -142,30 +130,18 @@ pub fn get_ping_res_ms(res: &str) -> i16 { let mut ms = -1; if let Some(t) = res.lines().last() { - for packets_res in t.split(",") { - if packets_res.trim().is_empty() { - continue; - } - - // eg1: [Average, 23ms] - // eg2: [Lost, 3 (100% loss) ] - let mut loop_key = String::new(); - let mut loop_value = String::new(); - - for (index, kv_res) in packets_res.split("=").enumerate() { - let kv_res = kv_res.trim(); - if kv_res.is_empty() { + if t.contains("=") { + for ping_res in t.split("=") { + if ping_res.starts_with("round-trip") { continue; } - if index == 0 { - loop_key = kv_res.to_string(); - } else if index == 1 { - loop_value = kv_res.to_string(); - } - } - if loop_key == AVERAGE_KEY { - ms = loop_value.replace("ms", "").parse().unwrap(); + for (index, res_ms) in ping_res.split("/").enumerate() { + if index == 1 { + ms = res_ms.parse::().unwrap().floor() as i16; + break; + } + } } } }