| 1 | #![cfg_attr(not(windows), allow(dead_code))] |
| 2 | |
| 3 | use std::path::PathBuf; |
| 4 | |
| 5 | const DISTRO_SKIP_PREFIX: &str = "docker-desktop"; |
| 6 | |
| 7 | pub(super) fn decode_output(bytes: &[u8]) -> String { |
| 8 | let looks_utf16 = bytes.starts_with(&[0xff, 0xfe]) |
| 9 | || bytes |
| 10 | .iter() |
| 11 | .skip(1) |
| 12 | .step_by(2) |
| 13 | .take(8) |
| 14 | .all(|byte| *byte == 0) |
| 15 | && bytes.len() >= 2; |
| 16 | let text = if looks_utf16 { |
| 17 | let units: Vec<u16> = bytes |
| 18 | .chunks_exact(2) |
| 19 | .map(|pair| u16::from_le_bytes([pair[0], pair[1]])) |
| 20 | .collect(); |
| 21 | String::from_utf16_lossy(&units) |
| 22 | } else { |
| 23 | String::from_utf8_lossy(bytes).into_owned() |
| 24 | }; |
| 25 | text.trim_start_matches('\u{feff}').replace('\0', "") |
| 26 | } |
| 27 | |
| 28 | pub(super) fn default_distro(list_output: &str) -> Option<String> { |
| 29 | list_output |
| 30 | .lines() |
| 31 | .map(|line| line.trim().trim_end_matches("(Default)").trim()) |
| 32 | .find(|name| !name.is_empty() && !name.starts_with(DISTRO_SKIP_PREFIX)) |
| 33 | .map(str::to_string) |
| 34 | } |
| 35 | |
| 36 | pub(super) fn unc_home(distro: &str, linux_home: &str) -> PathBuf { |
| 37 | let relative = linux_home.trim().trim_matches('/').replace('/', "\\"); |
| 38 | PathBuf::from(format!("\\\\wsl.localhost\\{distro}\\{relative}")) |
| 39 | } |
| 40 | |
| 41 | pub(super) fn wslconfig_is_mirrored(text: &str) -> bool { |
| 42 | text.lines() |
| 43 | .filter_map(|line| line.split('#').next()?.split_once('=')) |
| 44 | .any(|(key, value)| { |
| 45 | key.trim().eq_ignore_ascii_case("networkingMode") |
| 46 | && value.trim().eq_ignore_ascii_case("mirrored") |
| 47 | }) |
| 48 | } |
| 49 | |
| 50 | #[cfg(windows)] |
| 51 | mod spawn { |
| 52 | use std::os::windows::process::CommandExt; |
| 53 | use std::path::PathBuf; |
| 54 | use std::process::Command; |
| 55 | |
| 56 | use super::{decode_output, default_distro, unc_home, wslconfig_is_mirrored}; |
| 57 | |
| 58 | const CREATE_NO_WINDOW: u32 = 0x0800_0000; |
| 59 | |
| 60 | fn run(arguments: &[&str]) -> Option<String> { |
| 61 | let output = Command::new("wsl.exe") |
| 62 | .args(arguments) |
| 63 | .creation_flags(CREATE_NO_WINDOW) |
| 64 | .output() |
| 65 | .ok()?; |
| 66 | output |
| 67 | .status |
| 68 | .success() |
| 69 | .then(|| decode_output(&output.stdout)) |
| 70 | } |
| 71 | |
| 72 | pub fn default_home() -> Option<(String, PathBuf)> { |
| 73 | let distro = default_distro(&run(&["-l", "-q"])?)?; |
| 74 | let linux_home = run(&["-d", &distro, "--cd", "~", "-e", "pwd"])?; |
| 75 | let home = unc_home(&distro, linux_home.trim()); |
| 76 | Some((distro, home)) |
| 77 | } |
| 78 | |
| 79 | pub fn reachable(port: u16) -> Option<bool> { |
| 80 | let probe = format!( |
| 81 | "curl -s -m 2 http://127.0.0.1:{port}/healthz || wget -qO- -T 2 http://127.0.0.1:{port}/healthz" |
| 82 | ); |
| 83 | match run(&["-e", "sh", "-c", &probe]) { |
| 84 | Some(output) => Some(output.trim() == "ok"), |
| 85 | None => mirrored_networking().then_some(true), |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | fn mirrored_networking() -> bool { |
| 90 | std::env::var_os("USERPROFILE") |
| 91 | .map(|profile| PathBuf::from(profile).join(".wslconfig")) |
| 92 | .and_then(|path| std::fs::read_to_string(path).ok()) |
| 93 | .is_some_and(|text| wslconfig_is_mirrored(&text)) |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | #[cfg(windows)] |
| 98 | pub(super) use spawn::{default_home, reachable}; |