tr300 3.15.0

Cross-platform system information report
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
//! Network information collector

#[cfg(any(target_os = "linux", target_os = "macos"))]
use crate::collectors::command::run_stdout;
use crate::collectors::command::CommandTimeout;
use crate::collectors::CollectMode;
use crate::error::Result;
use std::env;

/// Network information for TR-300
#[derive(Debug, Clone)]
pub struct NetworkInfo {
    /// Machine's primary IP address (None if skipped in fast mode)
    pub machine_ip: Option<String>,
    /// Client IP (SSH_CLIENT if in SSH session)
    pub client_ip: Option<String>,
    /// DNS server addresses
    pub dns_servers: Vec<String>,
}

/// Collect network information
pub fn collect_network_info(mode: CollectMode) -> Result<NetworkInfo> {
    let should_skip_slow = mode == CollectMode::Fast && should_skip_network_on_platform();

    let machine_ip = if should_skip_slow {
        None
    } else {
        Some(get_machine_ip())
    };

    let client_ip = get_client_ip();

    let dns_servers = if should_skip_slow {
        Vec::new()
    } else {
        get_dns_servers()
    };

    Ok(NetworkInfo {
        machine_ip,
        client_ip,
        dns_servers,
    })
}

/// Whether to skip network collection in fast mode on this platform.
/// Windows and macOS use subprocess calls (slow). Linux reads /proc (fast).
fn should_skip_network_on_platform() -> bool {
    #[cfg(target_os = "linux")]
    {
        false
    }

    #[cfg(target_os = "windows")]
    {
        true
    }

    #[cfg(target_os = "macos")]
    {
        true
    }

    #[cfg(not(any(target_os = "linux", target_os = "windows", target_os = "macos")))]
    {
        true
    }
}

/// Get the machine's primary IP address
fn get_machine_ip() -> String {
    #[cfg(target_os = "windows")]
    {
        get_machine_ip_windows()
    }

    #[cfg(target_os = "linux")]
    {
        get_machine_ip_linux()
    }

    #[cfg(target_os = "macos")]
    {
        get_machine_ip_macos()
    }

    #[cfg(not(any(target_os = "windows", target_os = "linux", target_os = "macos")))]
    {
        "Unknown".to_string()
    }
}

#[cfg(target_os = "windows")]
fn get_machine_ip_windows() -> String {
    // Use WMI for IP (no PowerShell subprocess)
    let (ip, _dns) = crate::collectors::platform::windows::get_network_info_wmi();
    if let Some(ip) = ip {
        return ip;
    }

    // Fallback: try ipconfig
    if let Some(output) = crate::collectors::command::run_output(
        "ipconfig",
        std::iter::empty::<&str>(),
        CommandTimeout::Normal,
    ) {
        let stdout = String::from_utf8_lossy(&output.stdout);
        for line in stdout.lines() {
            if line.contains("IPv4 Address") {
                if let Some(ip) = line.split(':').next_back() {
                    let ip = ip.trim();
                    if !ip.is_empty() && ip != "127.0.0.1" {
                        return ip.to_string();
                    }
                }
            }
        }
    }

    "Unknown".to_string()
}

#[cfg(target_os = "linux")]
fn get_machine_ip_linux() -> String {
    // Ask the kernel which source IP it would use for the default route.
    if let Some(stdout) = run_stdout("ip", ["route", "get", "1.1.1.1"], CommandTimeout::Normal) {
        if let Some(ip) = parse_route_src_ip(&stdout) {
            return ip;
        }
    }

    // Fallback: hostname -I
    if let Some(stdout) = run_stdout("hostname", ["-I"], CommandTimeout::Normal) {
        if let Some(ip) = stdout.split_whitespace().next() {
            if !ip.is_empty() && ip != "127.0.0.1" {
                return ip.to_string();
            }
        }
    }

    "Unknown".to_string()
}

#[cfg(target_os = "macos")]
fn get_machine_ip_macos() -> String {
    if let Some(stdout) = run_stdout("scutil", ["--nwi"], CommandTimeout::Normal) {
        if let Some(iface) = parse_scutil_nwi_primary_interface(&stdout) {
            if let Some(ip) = run_stdout(
                "ipconfig",
                ["getifaddr", iface.as_str()],
                CommandTimeout::Normal,
            ) {
                let ip = ip.trim().to_string();
                if !ip.is_empty() && ip != "127.0.0.1" {
                    return ip;
                }
            }
        }
    }

    // Try ipconfig getifaddr en0 (WiFi) or en1 (Ethernet)
    for iface in &["en0", "en1", "en2"] {
        if let Some(output) = run_stdout("ipconfig", ["getifaddr", iface], CommandTimeout::Normal) {
            let ip = output.trim().to_string();
            if !ip.is_empty() && ip != "127.0.0.1" {
                return ip;
            }
        }
    }

    // Fallback: try route
    if let Some(stdout) = run_stdout("route", ["get", "default"], CommandTimeout::Normal) {
        for line in stdout.lines() {
            if line.trim().starts_with("interface:") {
                if let Some(iface) = line.split(':').next_back() {
                    let iface = iface.trim();
                    if let Some(output) =
                        run_stdout("ipconfig", ["getifaddr", iface], CommandTimeout::Normal)
                    {
                        let ip = output.trim().to_string();
                        if !ip.is_empty() && ip != "127.0.0.1" {
                            return ip;
                        }
                    }
                }
            }
        }
    }

    "Unknown".to_string()
}

/// Get client IP from SSH_CLIENT environment variable
fn get_client_ip() -> Option<String> {
    // Check SSH_CLIENT env var
    if let Ok(ssh_client) = env::var("SSH_CLIENT") {
        // Format: "IP PORT LOCAL_PORT"
        if let Some(ip) = ssh_client.split_whitespace().next() {
            return Some(ip.to_string());
        }
    }

    // Check SSH_CONNECTION env var
    if let Ok(ssh_conn) = env::var("SSH_CONNECTION") {
        // Format: "CLIENT_IP CLIENT_PORT SERVER_IP SERVER_PORT"
        if let Some(ip) = ssh_conn.split_whitespace().next() {
            return Some(ip.to_string());
        }
    }

    None
}

/// Get DNS server addresses
fn get_dns_servers() -> Vec<String> {
    #[cfg(target_os = "windows")]
    {
        get_dns_servers_windows()
    }

    #[cfg(target_os = "linux")]
    {
        get_dns_servers_linux()
    }

    #[cfg(target_os = "macos")]
    {
        get_dns_servers_macos()
    }

    #[cfg(not(any(target_os = "windows", target_os = "linux", target_os = "macos")))]
    {
        Vec::new()
    }
}

#[cfg(target_os = "windows")]
fn get_dns_servers_windows() -> Vec<String> {
    // Use WMI for DNS servers (no PowerShell subprocess)
    let (_ip, servers) = crate::collectors::platform::windows::get_network_info_wmi();
    if !servers.is_empty() {
        return servers;
    }

    // Fallback: parse ipconfig /all
    let mut servers = Vec::new();
    if let Some(output) =
        crate::collectors::command::run_output("ipconfig", ["/all"], CommandTimeout::Normal)
    {
        let stdout = String::from_utf8_lossy(&output.stdout);
        let mut in_dns_section = false;
        for line in stdout.lines() {
            if line.contains("DNS Servers") {
                in_dns_section = true;
                if let Some(ip) = line.split(':').next_back() {
                    let ip = ip.trim();
                    if !ip.is_empty() && !servers.contains(&ip.to_string()) {
                        servers.push(ip.to_string());
                    }
                }
            } else if in_dns_section {
                let trimmed = line.trim();
                if trimmed.contains('.') && !trimmed.contains(':') {
                    if !servers.contains(&trimmed.to_string()) {
                        servers.push(trimmed.to_string());
                    }
                } else if !trimmed.is_empty() {
                    in_dns_section = false;
                }
            }
            if servers.len() >= 5 {
                break;
            }
        }
    }

    servers
}

#[cfg(target_os = "linux")]
fn get_dns_servers_linux() -> Vec<String> {
    use std::fs;

    for path in [
        "/run/systemd/resolve/resolv.conf",
        "/run/NetworkManager/resolv.conf",
        "/etc/resolv.conf",
    ] {
        if let Ok(content) = fs::read_to_string(path) {
            let servers = parse_resolv_conf_servers(&content, path != "/etc/resolv.conf");
            if !servers.is_empty() {
                return servers;
            }
        }
    }

    // Try systemd-resolved if no servers found
    let mut servers = Vec::new();
    if let Some(stdout) = run_stdout("resolvectl", ["status"], CommandTimeout::Normal) {
        for line in stdout.lines() {
            if line.contains("DNS Servers:") {
                if let Some(ips) = line.split(':').next_back() {
                    for ip in ips.split_whitespace() {
                        if !servers.contains(&ip.to_string()) {
                            servers.push(ip.to_string());
                            if servers.len() >= 5 {
                                break;
                            }
                        }
                    }
                }
            }
        }
    }

    servers
}

#[cfg(target_os = "macos")]
fn get_dns_servers_macos() -> Vec<String> {
    let mut servers = Vec::new();

    // Try scutil --dns
    if let Some(stdout) = run_stdout("scutil", ["--dns"], CommandTimeout::Normal) {
        for line in stdout.lines() {
            let line = line.trim();
            if line.starts_with("nameserver[") {
                if let Some(ip) = line.split(':').next_back() {
                    let ip = ip.trim();
                    if !ip.is_empty() && !servers.contains(&ip.to_string()) {
                        servers.push(ip.to_string());
                        if servers.len() >= 5 {
                            break;
                        }
                    }
                }
            }
        }
    }

    servers
}

#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
fn parse_route_src_ip(route_output: &str) -> Option<String> {
    let mut parts = route_output.split_whitespace();
    while let Some(part) = parts.next() {
        if part == "src" {
            let ip = parts.next()?;
            if !ip.is_empty() && ip != "127.0.0.1" {
                return Some(ip.to_string());
            }
        }
    }
    None
}

#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
fn parse_resolv_conf_servers(content: &str, skip_loopback: bool) -> Vec<String> {
    let mut servers = Vec::new();
    for line in content.lines() {
        let line = line.split('#').next().unwrap_or("").trim();
        if !line.starts_with("nameserver") {
            continue;
        }
        let Some(ip) = line.split_whitespace().nth(1) else {
            continue;
        };
        if skip_loopback && (ip.starts_with("127.") || ip == "::1") {
            continue;
        }
        if !servers.contains(&ip.to_string()) {
            servers.push(ip.to_string());
            if servers.len() >= 5 {
                break;
            }
        }
    }
    servers
}

#[cfg_attr(not(target_os = "macos"), allow(dead_code))]
fn parse_scutil_nwi_primary_interface(output: &str) -> Option<String> {
    for line in output.lines() {
        let trimmed = line.trim();
        if let Some((iface, rest)) = trimmed.split_once(':') {
            let iface = iface.trim();
            if rest.contains("IPv4") && !iface.is_empty() {
                return Some(iface.to_string());
            }
        }
    }
    None
}

// Keep the old interface struct for backwards compatibility if needed
/// Network interface information (legacy)
#[derive(Debug, Clone)]
pub struct NetworkInterface {
    pub name: String,
    pub mac_address: String,
    pub rx_bytes: u64,
    pub tx_bytes: u64,
    pub rx_packets: u64,
    pub tx_packets: u64,
}

/// Collect network interface information (legacy)
pub fn collect() -> Result<Vec<NetworkInterface>> {
    use sysinfo::Networks;

    let networks = Networks::new_with_refreshed_list();
    let mut result = Vec::new();

    for (name, data) in networks.list() {
        let mac = data.mac_address();
        let mac_string = format!(
            "{:02X}:{:02X}:{:02X}:{:02X}:{:02X}:{:02X}",
            mac.0[0], mac.0[1], mac.0[2], mac.0[3], mac.0[4], mac.0[5]
        );

        result.push(NetworkInterface {
            name: name.clone(),
            mac_address: mac_string,
            rx_bytes: data.total_received(),
            tx_bytes: data.total_transmitted(),
            rx_packets: data.total_packets_received(),
            tx_packets: data.total_packets_transmitted(),
        });
    }

    result.sort_by(|a, b| a.name.cmp(&b.name));

    Ok(result)
}

impl NetworkInterface {
    fn format_bytes(bytes: u64) -> String {
        crate::format_bytes(bytes)
    }

    pub fn rx_formatted(&self) -> String {
        Self::format_bytes(self.rx_bytes)
    }

    pub fn tx_formatted(&self) -> String {
        Self::format_bytes(self.tx_bytes)
    }

    pub fn traffic_string(&self) -> String {
        format!("RX: {} / TX: {}", self.rx_formatted(), self.tx_formatted())
    }

    pub fn is_active(&self) -> bool {
        self.rx_bytes > 0 || self.tx_bytes > 0
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parses_linux_route_src_ip() {
        let route = "1.1.1.1 via 10.0.0.1 dev wlan0 src 10.0.0.42 uid 1000";
        assert_eq!(parse_route_src_ip(route), Some("10.0.0.42".to_string()));
    }

    #[test]
    fn resolv_conf_skips_comments_and_deduplicates() {
        let content = "\
# comment
nameserver 127.0.0.53
nameserver 1.1.1.1
nameserver 1.1.1.1
nameserver 2606:4700:4700::1111
";
        assert_eq!(
            parse_resolv_conf_servers(content, true),
            vec!["1.1.1.1".to_string(), "2606:4700:4700::1111".to_string()]
        );
    }

    #[test]
    fn parses_macos_scutil_nwi_primary_interface() {
        let nwi = "\
Network information
IPv4 network interface information
     en0 : flags      : 0x5 (IPv4,DNS)
           reach      : 0x00000002 (Reachable)
";
        assert_eq!(
            parse_scutil_nwi_primary_interface(nwi),
            Some("en0".to_string())
        );
    }
}