psnet 1.2.0

A beautiful real-time TUI network monitor for Windows — 9 tabs, GeoIP, device discovery, firewall, topology & more
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
//! Hyper-V virtual machine discovery via PowerShell cmdlets.
//!
//! Three discovery queries running in parallel:
//!   1. `Get-VM` — list all VMs with name, state, ID
//!   2. `Get-VMNetworkAdapter` — network adapter details (IPs, MACs, switches)
//!   3. `Get-VMSwitch` — virtual switch configuration
//!
//! Results are grouped by switch name into separate `RemoteNetwork` entries.
//! Silently returns empty if Hyper-V is not installed or not available.

use std::collections::HashMap;
use std::net::{IpAddr, Ipv4Addr};
use std::process::Command;
use std::sync::Mutex;
use std::thread;

use chrono::Local;

use crate::types::{LanDevice, NetworkCategory, RemoteNetwork};

/// CREATE_NO_WINDOW flag to prevent console flash on Windows.
#[cfg(target_os = "windows")]
const CREATE_NO_WINDOW: u32 = 0x08000000;

/// Create a PowerShell Command with CREATE_NO_WINDOW on Windows.
fn quiet_powershell() -> Command {
    let mut cmd = Command::new("powershell");
    #[cfg(target_os = "windows")]
    {
        use std::os::windows::process::CommandExt;
        cmd.creation_flags(CREATE_NO_WINDOW);
    }
    cmd
}

/// Run a PowerShell command and return parsed JSON, or None on failure.
fn run_ps_json(script: &str) -> Option<serde_json::Value> {
    let output = quiet_powershell()
        .args(["-NoProfile", "-NonInteractive", "-Command", script])
        .output()
        .ok()?;

    if !output.status.success() {
        return None;
    }

    let stdout = String::from_utf8_lossy(&output.stdout);
    let trimmed = stdout.trim();
    if trimmed.is_empty() {
        return None;
    }

    serde_json::from_str(trimmed).ok()
}

/// Ensure a JSON value is always an array (single objects become one-element arrays).
fn as_array(val: serde_json::Value) -> Vec<serde_json::Value> {
    match val {
        serde_json::Value::Array(arr) => arr,
        serde_json::Value::Null => Vec::new(),
        other => vec![other],
    }
}

/// Format a MAC address from PowerShell format (AABBCCDDEEFF or AA-BB-CC-DD-EE-FF)
/// into standard XX:XX:XX:XX:XX:XX notation.
fn format_mac(raw: &str) -> String {
    // Strip dashes and any whitespace
    let clean: String = raw.chars().filter(|c| c.is_ascii_hexdigit()).collect();
    if clean.len() != 12 {
        return raw.to_string();
    }
    format!(
        "{}:{}:{}:{}:{}:{}",
        &clean[0..2],
        &clean[2..4],
        &clean[4..6],
        &clean[6..8],
        &clean[8..10],
        &clean[10..12],
    )
    .to_uppercase()
}

/// VM info from Get-VM.
#[derive(Clone, Debug)]
struct VmInfo {
    name: String,
    state: String,
    #[allow(dead_code)]
    id: String,
}

/// Network adapter info from Get-VMNetworkAdapter.
#[derive(Clone, Debug)]
struct VmAdapterInfo {
    vm_name: String,
    ip_addresses: Vec<String>,
    mac_address: String,
    switch_name: String,
    #[allow(dead_code)]
    status: String,
}

/// Switch info from Get-VMSwitch.
#[derive(Clone, Debug)]
struct VmSwitchInfo {
    name: String,
    switch_type: String,
    #[allow(dead_code)]
    net_adapter_description: String,
}

/// Discover Hyper-V VMs and group them by virtual switch into RemoteNetworks.
pub fn discover() -> Vec<RemoteNetwork> {
    // Run all three PowerShell queries in parallel
    let vms_result: Mutex<Vec<VmInfo>> = Mutex::new(Vec::new());
    let adapters_result: Mutex<Vec<VmAdapterInfo>> = Mutex::new(Vec::new());
    let switches_result: Mutex<Vec<VmSwitchInfo>> = Mutex::new(Vec::new());

    thread::scope(|s| {
        let vms_ref = &vms_result;
        s.spawn(move || {
            *vms_ref.lock().unwrap() = query_vms();
        });

        let adapters_ref = &adapters_result;
        s.spawn(move || {
            *adapters_ref.lock().unwrap() = query_adapters();
        });

        let switches_ref = &switches_result;
        s.spawn(move || {
            *switches_ref.lock().unwrap() = query_switches();
        });
    });

    let vms = vms_result.into_inner().unwrap();
    let adapters = adapters_result.into_inner().unwrap();
    let switches = switches_result.into_inner().unwrap();

    if vms.is_empty() {
        return Vec::new();
    }

    // Build lookup: VM name → VmInfo
    let vm_map: HashMap<String, &VmInfo> = vms.iter().map(|v| (v.name.clone(), v)).collect();

    // Build lookup: switch name → VmSwitchInfo
    let switch_map: HashMap<String, &VmSwitchInfo> =
        switches.iter().map(|sw| (sw.name.clone(), sw)).collect();

    // Group adapters by switch name
    let mut by_switch: HashMap<String, Vec<&VmAdapterInfo>> = HashMap::new();
    for adapter in &adapters {
        let key = if adapter.switch_name.is_empty() {
            "(No Switch)".to_string()
        } else {
            adapter.switch_name.clone()
        };
        by_switch.entry(key).or_default().push(adapter);
    }

    // Also include VMs that have no adapter entries
    let vms_with_adapters: std::collections::HashSet<String> =
        adapters.iter().map(|a| a.vm_name.clone()).collect();
    for vm in &vms {
        if !vms_with_adapters.contains(&vm.name) {
            by_switch
                .entry("(No Switch)".to_string())
                .or_default();
            // We'll handle orphan VMs below
        }
    }

    let now = Local::now().time();
    let mut networks = Vec::new();

    for (switch_name, switch_adapters) in &by_switch {
        let mut devices = Vec::new();
        let mut seen_vms: std::collections::HashSet<String> = std::collections::HashSet::new();

        for adapter in switch_adapters {
            seen_vms.insert(adapter.vm_name.clone());

            let vm_state = vm_map
                .get(&adapter.vm_name)
                .map(|v| v.state.as_str())
                .unwrap_or("Unknown");
            let is_online = vm_state.eq_ignore_ascii_case("Running");

            // Pick the first IPv4 address, or fallback to 0.0.0.0
            let ip = adapter
                .ip_addresses
                .iter()
                .filter_map(|s| s.parse::<Ipv4Addr>().ok())
                .next()
                .unwrap_or(Ipv4Addr::UNSPECIFIED);

            let mac = format_mac(&adapter.mac_address);

            let discovery_info = format!(
                "State:{}  Switch:{}",
                vm_state,
                if adapter.switch_name.is_empty() {
                    "None"
                } else {
                    &adapter.switch_name
                }
            );

            devices.push(LanDevice {
                ip: IpAddr::V4(ip),
                mac,
                hostname: Some(adapter.vm_name.clone()),
                vendor: Some("Hyper-V VM".to_string()),
                first_seen: now,
                last_seen: now,
                is_online,
                custom_name: None,
                discovery_info,
                open_ports: String::new(),
                bytes_sent: 0,
                bytes_received: 0,
                tick_sent: 0,
                tick_received: 0,
                speed_sent: 0.0,
                speed_received: 0.0,
            });
        }

        // Add orphan VMs (no adapters) to "(No Switch)" group
        if switch_name == "(No Switch)" {
            for vm in &vms {
                if !vms_with_adapters.contains(&vm.name) && !seen_vms.contains(&vm.name) {
                    let is_online = vm.state.eq_ignore_ascii_case("Running");
                    devices.push(LanDevice {
                        ip: IpAddr::V4(Ipv4Addr::UNSPECIFIED),
                        mac: String::new(),
                        hostname: Some(vm.name.clone()),
                        vendor: Some("Hyper-V VM".to_string()),
                        first_seen: now,
                        last_seen: now,
                        is_online,
                        custom_name: None,
                        discovery_info: format!("State:{}  Switch:None", vm.state),
                        open_ports: String::new(),
                        bytes_sent: 0,
                        bytes_received: 0,
                        tick_sent: 0,
                        tick_received: 0,
                        speed_sent: 0.0,
                        speed_received: 0.0,
                    });
                }
            }
        }

        if devices.is_empty() {
            continue;
        }

        // Determine switch type for the network name
        let switch_type_str = switch_map
            .get(switch_name.as_str())
            .map(|sw| sw.switch_type.as_str())
            .unwrap_or("Unknown");

        let network_name = if switch_name == "(No Switch)" {
            "Hyper-V: (No Switch)".to_string()
        } else {
            format!("Hyper-V: {} ({})", switch_name, switch_type_str)
        };

        // Use a default internal subnet for Hyper-V VMs
        // Try to derive from first device with a real IP
        let first_ip = devices
            .iter()
            .map(|d| match d.ip {
                IpAddr::V4(v4) => v4,
                _ => Ipv4Addr::UNSPECIFIED,
            })
            .find(|ip| !ip.is_unspecified())
            .unwrap_or(Ipv4Addr::new(172, 16, 0, 0));

        let net_u32 = u32::from(first_ip) & 0xFFFFFF00;
        let local_ip = Ipv4Addr::from(net_u32 | 1);
        let subnet_mask = Ipv4Addr::new(255, 255, 255, 0);
        let subnet_cidr = format!("{}/24", Ipv4Addr::from(net_u32));

        networks.push(RemoteNetwork {
            name: network_name,
            category: NetworkCategory::HyperV,
            adapter_name: switch_name.clone(),
            local_ip,
            subnet_mask,
            subnet_cidr,
            gateway: None,
            devices,
        });
    }

    networks
}

// ─── PowerShell query functions ──────────────────────────────────────────────

fn query_vms() -> Vec<VmInfo> {
    let json = match run_ps_json(
        "Get-VM | Select-Object Name, State, Id | ConvertTo-Json",
    ) {
        Some(v) => v,
        None => return Vec::new(),
    };

    as_array(json)
        .into_iter()
        .filter_map(|obj| {
            let name = obj.get("Name")?.as_str()?.to_string();
            // State can be a string or integer depending on PS version
            let state = match obj.get("State") {
                Some(serde_json::Value::String(s)) => s.clone(),
                Some(serde_json::Value::Number(n)) => {
                    // Hyper-V VM state enum: 2=Running, 3=Off, 6=Saved, etc.
                    match n.as_u64() {
                        Some(2) => "Running".to_string(),
                        Some(3) => "Off".to_string(),
                        Some(6) => "Saved".to_string(),
                        Some(9) => "Paused".to_string(),
                        Some(v) => format!("State({})", v),
                        None => "Unknown".to_string(),
                    }
                }
                _ => "Unknown".to_string(),
            };
            let id = obj
                .get("Id")
                .and_then(|v| v.as_str())
                .unwrap_or("")
                .to_string();
            Some(VmInfo { name, state, id })
        })
        .collect()
}

fn query_adapters() -> Vec<VmAdapterInfo> {
    let json = match run_ps_json(
        "Get-VMNetworkAdapter -All | Select-Object VMName, IPAddresses, MacAddress, SwitchName, Status | ConvertTo-Json",
    ) {
        Some(v) => v,
        None => return Vec::new(),
    };

    as_array(json)
        .into_iter()
        .filter_map(|obj| {
            let vm_name = obj.get("VMName")?.as_str()?.to_string();
            // Skip management OS adapters (VMName is null/empty)
            if vm_name.is_empty() {
                return None;
            }

            let ip_addresses = match obj.get("IPAddresses") {
                Some(serde_json::Value::Array(arr)) => arr
                    .iter()
                    .filter_map(|v| v.as_str().map(String::from))
                    .collect(),
                Some(serde_json::Value::String(s)) => vec![s.clone()],
                _ => Vec::new(),
            };

            let mac_address = obj
                .get("MacAddress")
                .and_then(|v| v.as_str())
                .unwrap_or("")
                .to_string();

            let switch_name = obj
                .get("SwitchName")
                .and_then(|v| v.as_str())
                .unwrap_or("")
                .to_string();

            let status = obj
                .get("Status")
                .and_then(|v| v.as_str())
                .unwrap_or("")
                .to_string();

            Some(VmAdapterInfo {
                vm_name,
                ip_addresses,
                mac_address,
                switch_name,
                status,
            })
        })
        .collect()
}

fn query_switches() -> Vec<VmSwitchInfo> {
    let json = match run_ps_json(
        "Get-VMSwitch | Select-Object Name, SwitchType, NetAdapterInterfaceDescription | ConvertTo-Json",
    ) {
        Some(v) => v,
        None => return Vec::new(),
    };

    as_array(json)
        .into_iter()
        .filter_map(|obj| {
            let name = obj.get("Name")?.as_str()?.to_string();

            let switch_type = match obj.get("SwitchType") {
                Some(serde_json::Value::String(s)) => s.clone(),
                Some(serde_json::Value::Number(n)) => {
                    // SwitchType enum: 0=Private, 1=Internal, 2=External
                    match n.as_u64() {
                        Some(0) => "Private".to_string(),
                        Some(1) => "Internal".to_string(),
                        Some(2) => "External".to_string(),
                        Some(v) => format!("Type({})", v),
                        None => "Unknown".to_string(),
                    }
                }
                _ => "Unknown".to_string(),
            };

            let net_adapter_description = obj
                .get("NetAdapterInterfaceDescription")
                .and_then(|v| v.as_str())
                .unwrap_or("")
                .to_string();

            Some(VmSwitchInfo {
                name,
                switch_type,
                net_adapter_description,
            })
        })
        .collect()
}