cloudflare-speed-cli 1.0.2

CLI tool for Cloudflare speed testing with TUI interface
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
mod cert;
mod cloudflare;
pub mod dns;
pub mod ip_comparison;
mod latency;
pub mod network_bind;
mod throughput;
pub mod tls;
pub mod traceroute;
mod turn_udp;

use crate::model::{
    DnsSummary, IpVersionComparison, Phase, RunConfig, RunResult, TestEvent, TlsSummary,
    TracerouteSummary,
};
use anyhow::Result;
use std::sync::{
    atomic::{AtomicBool, Ordering},
    Arc,
};
use std::time::Duration;
use tokio::sync::mpsc;

/// Check if paused, wait while paused, and return true if cancelled.
/// Returns true if the caller should break out of its loop.
pub(crate) async fn wait_if_paused_or_cancelled(paused: &AtomicBool, cancel: &AtomicBool) -> bool {
    while paused.load(Ordering::Relaxed) && !cancel.load(Ordering::Relaxed) {
        tokio::time::sleep(Duration::from_millis(50)).await;
    }
    cancel.load(Ordering::Relaxed)
}

#[derive(Debug, Clone)]
pub enum EngineControl {
    /// Pause (true) or resume (false) the running test
    Pause(bool),
    /// Cancel the test entirely
    Cancel,
}

pub struct TestEngine {
    cfg: RunConfig,
}

impl TestEngine {
    pub fn new(cfg: RunConfig) -> Self {
        Self { cfg }
    }

    pub async fn run(
        self,
        event_tx: mpsc::Sender<TestEvent>,
        mut control_rx: mpsc::Receiver<EngineControl>,
    ) -> Result<RunResult> {
        // Effective IPv4/IPv6 restriction for the whole run. Validated in
        // `build_config`, so this is expected to succeed here; recomputing
        // keeps a single source of truth rather than threading a stored value.
        let family = network_bind::resolve_ip_family(
            self.cfg.ipv4_only,
            self.cfg.ipv6_only,
            self.cfg.resolved_bind_ip,
        )?;

        let client = cloudflare::CloudflareClient::new(&self.cfg, family).await?;

        let paused = Arc::new(AtomicBool::new(false));
        let cancel = Arc::new(AtomicBool::new(false));

        // Try to get meta from multiple sources in order of preference:
        // 1. /meta endpoint (may have full details)
        // 2. /cdn-cgi/trace endpoint (reliable source for colo, ip, country)
        // 3. Response headers (fallback)
        let mut meta: Option<serde_json::Value> = match cloudflare::fetch_meta(&client).await {
            Ok(v) if !v.as_object().map(|m| m.is_empty()).unwrap_or(true) => Some(v),
            _ => None,
        };

        // If meta is empty or missing colo, try /cdn-cgi/trace
        let has_colo = meta
            .as_ref()
            .and_then(|m| m.get("colo"))
            .and_then(|v| v.as_str())
            .is_some();

        if !has_colo {
            if let Ok(trace_meta) = cloudflare::fetch_trace(&client).await {
                if !trace_meta.as_object().map(|m| m.is_empty()).unwrap_or(true) {
                    // Merge trace_meta into meta
                    if let Some(ref mut existing) = meta {
                        if let (Some(existing_map), Some(trace_map)) =
                            (existing.as_object_mut(), trace_meta.as_object())
                        {
                            for (k, v) in trace_map {
                                if !existing_map.contains_key(k) {
                                    existing_map.insert(k.clone(), v.clone());
                                }
                            }
                        }
                    } else {
                        meta = Some(trace_meta);
                    }
                }
            }
        }

        // Final fallback to response headers
        if meta.is_none() {
            meta = cloudflare::fetch_meta_from_response(&client).await.ok();
        }

        let locations = cloudflare::fetch_locations(&client).await.ok();
        let server = meta
            .as_ref()
            .and_then(|m: &serde_json::Value| {
                m.get("colo").and_then(|v: &serde_json::Value| v.as_str())
            })
            .and_then(|colo| {
                locations
                    .as_ref()
                    .and_then(|loc| cloudflare::map_colo_to_server(loc, colo))
            });

        // Send meta info early so TUI can display server/colo/ip immediately
        if let Some(ref m) = meta {
            event_tx
                .send(TestEvent::MetaInfo { meta: m.clone() })
                .await
                .ok();
        }

        // Control listener.
        let paused2 = paused.clone();
        let cancel2 = cancel.clone();
        let control_handle = tokio::spawn(async move {
            while let Some(msg) = control_rx.recv().await {
                match msg {
                    EngineControl::Pause(p) => paused2.store(p, Ordering::Relaxed),
                    EngineControl::Cancel => {
                        cancel2.store(true, Ordering::Relaxed);
                        break;
                    }
                }
            }
        });

        // Run diagnostic tests before the main speed test
        let mut dns_summary: Option<DnsSummary> = None;
        let mut tls_summary: Option<TlsSummary> = None;
        let mut ip_comparison_result: Option<IpVersionComparison> = None;
        let mut traceroute_summary: Option<TracerouteSummary> = None;
        let mut external_ipv4: Option<String> = None;
        let mut external_ipv6: Option<String> = None;

        // DNS Resolution measurement
        if self.cfg.measure_dns {
            if let Some(hostname) = dns::extract_hostname(&self.cfg.base_url) {
                event_tx
                    .send(TestEvent::Info {
                        message: format!("Measuring DNS resolution for {}...", hostname),
                    })
                    .await
                    .ok();

                match dns::measure_dns_resolution(&hostname, family).await {
                    Ok(summary) => {
                        event_tx
                            .send(TestEvent::DiagnosticDns {
                                summary: summary.clone(),
                            })
                            .await
                            .ok();
                        dns_summary = Some(summary);
                    }
                    Err(e) => {
                        event_tx
                            .send(TestEvent::Info {
                                message: format!("DNS measurement failed: {}", e),
                            })
                            .await
                            .ok();
                    }
                }
            }
        }

        // TLS Handshake measurement
        if self.cfg.measure_tls {
            if let Some((hostname, port)) = tls::extract_host_port(&self.cfg.base_url) {
                event_tx
                    .send(TestEvent::Info {
                        message: format!("Measuring TLS handshake with {}:{}...", hostname, port),
                    })
                    .await
                    .ok();

                match tls::measure_tls_handshake(
                    &hostname,
                    port,
                    self.cfg.certificate_path.as_deref(),
                    self.cfg.resolved_bind_ip,
                    family,
                )
                .await
                {
                    Ok(summary) => {
                        event_tx
                            .send(TestEvent::DiagnosticTls {
                                summary: summary.clone(),
                            })
                            .await
                            .ok();
                        tls_summary = Some(summary);
                    }
                    Err(e) => {
                        event_tx
                            .send(TestEvent::Info {
                                message: format!("TLS measurement failed: {}", e),
                            })
                            .await
                            .ok();
                    }
                }
            }
        }

        // Fetch external IPs (runs in parallel, part of default diagnostics)
        if self.cfg.measure_dns {
            let (v4, v6) = dns::fetch_external_ips(
                &self.cfg.base_url,
                self.cfg.resolved_bind_ip,
                self.cfg.certificate_path.as_deref(),
                family,
            )
            .await;
            external_ipv4 = v4.clone();
            external_ipv6 = v6.clone();
            event_tx
                .send(TestEvent::ExternalIps { ipv4: v4, ipv6: v6 })
                .await
                .ok();
        }

        // IPv4 vs IPv6 comparison
        if self.cfg.compare_ip_versions {
            event_tx
                .send(TestEvent::Info {
                    message: "Comparing IPv4 vs IPv6 performance...".to_string(),
                })
                .await
                .ok();

            match ip_comparison::compare_ip_versions(
                &self.cfg.base_url,
                &self.cfg.user_agent,
                self.cfg.resolved_bind_ip,
                self.cfg.certificate_path.as_deref(),
                family,
            )
            .await
            {
                Ok(comparison) => {
                    event_tx
                        .send(TestEvent::DiagnosticIpComparison {
                            comparison: comparison.clone(),
                        })
                        .await
                        .ok();
                    ip_comparison_result = Some(comparison);
                }
                Err(e) => {
                    event_tx
                        .send(TestEvent::Info {
                            message: format!("IP comparison failed: {}", e),
                        })
                        .await
                        .ok();
                }
            }
        }

        // Traceroute
        if self.cfg.traceroute {
            if let Some(hostname) = dns::extract_hostname(&self.cfg.base_url) {
                event_tx
                    .send(TestEvent::Info {
                        message: format!(
                            "Running traceroute to {} (max {} hops)...",
                            hostname, self.cfg.traceroute_max_hops
                        ),
                    })
                    .await
                    .ok();

                match traceroute::run_traceroute(
                    &hostname,
                    self.cfg.traceroute_max_hops,
                    &event_tx,
                    self.cfg.resolved_bind_ip,
                    self.cfg.interface.as_deref(),
                    family,
                )
                .await
                {
                    Ok(summary) => {
                        event_tx
                            .send(TestEvent::TracerouteComplete {
                                summary: summary.clone(),
                            })
                            .await
                            .ok();
                        traceroute_summary = Some(summary);
                    }
                    Err(e) => {
                        event_tx
                            .send(TestEvent::Info {
                                message: format!("Traceroute failed: {}", e),
                            })
                            .await
                            .ok();
                    }
                }
            }
        }

        event_tx
            .send(TestEvent::PhaseStarted {
                phase: Phase::IdleLatency,
            })
            .await
            .ok();

        let idle_latency = latency::run_latency_probes(
            &client,
            Phase::IdleLatency,
            None,
            self.cfg.idle_latency_duration,
            self.cfg.probe_interval_ms,
            self.cfg.probe_timeout_ms,
            &event_tx,
            paused.clone(),
            cancel.clone(),
        )
        .await?;

        event_tx
            .send(TestEvent::PhaseStarted {
                phase: Phase::Download,
            })
            .await
            .ok();

        let (download, loaded_latency_download) = throughput::run_download_with_loaded_latency(
            &client,
            &self.cfg,
            &event_tx,
            paused.clone(),
            cancel.clone(),
        )
        .await?;

        event_tx
            .send(TestEvent::PhaseStarted {
                phase: Phase::Upload,
            })
            .await
            .ok();

        // Prefetch DNS for STUN server during upload to eliminate delay before packet loss phase.
        // Collect *all* resolved addresses so the UDP probe can filter by bind-IP family
        // (binding to a v4 source IP and connecting to a v6 target fails with EAFNOSUPPORT).
        let stun_dns_handle = tokio::spawn(async move {
            tokio::net::lookup_host(("turn.cloudflare.com", 3478_u16))
                .await
                .map(|addrs| addrs.collect::<Vec<_>>())
                .unwrap_or_default()
        });

        let (upload, loaded_latency_upload) = throughput::run_upload_with_loaded_latency(
            &client,
            &self.cfg,
            &event_tx,
            paused,
            cancel.clone(),
        )
        .await?;
        // Note: PhaseStarted::PacketLoss is emitted from inside the upload function
        // the moment its tick loop ends, so the dashboard can switch immediately
        // without waiting for upload-task drain.

        let mut experimental_udp = None;
        let mut udp_error = None;

        let info = crate::model::TurnInfo {
            urls: vec!["stun:turn.cloudflare.com:3478".to_string()],
            username: None,
            credential: None,
        };

        // Use prefetched DNS if available (empty Vec means resolve inline)
        let pre_resolved: Vec<std::net::SocketAddr> = stun_dns_handle.await.unwrap_or_default();

        match turn_udp::run_udp_like_loss_probe(&info, &self.cfg, &event_tx, pre_resolved, family)
            .await
        {
            Ok(udp) => {
                experimental_udp = Some(udp);
            }
            Err(e) => {
                let msg = format!("UDP probe failed: {e:#}");
                udp_error = Some(msg.clone());
                event_tx
                    .send(TestEvent::Info { message: msg })
                    .await
                    .ok();
            }
        }

        event_tx
            .send(TestEvent::PhaseStarted {
                phase: Phase::Summary,
            })
            .await
            .ok();

        // Abort the control listener task before returning.
        // In Tokio, dropping a JoinHandle does NOT cancel the task - it continues running!
        // This was causing high CPU usage when idle because the task was still waiting
        // on control_rx.recv().await even after the test completed.
        control_handle.abort();
        // Don't await the aborted task - just let it be cleaned up

        Ok(RunResult {
            version: Some(env!("CARGO_PKG_VERSION").to_string()),
            timestamp_utc: time::OffsetDateTime::now_utc()
                .format(&time::format_description::well_known::Rfc3339)
                .unwrap_or_else(|_| "now".into()),
            base_url: self.cfg.base_url.clone(),
            meas_id: self.cfg.meas_id.clone(),
            comments: self.cfg.comments.clone(),
            meta,
            server,
            idle_latency,
            download,
            upload,
            loaded_latency_download,
            loaded_latency_upload,
            turn: None,
            experimental_udp,
            udp_error,
            // Network information - will be populated by TUI when available
            ip: None,
            colo: None,
            asn: None,
            as_org: None,
            interface_name: None,
            network_name: None,
            is_wireless: None,
            interface_mac: None,
            local_ipv4: None,
            local_ipv6: None,
            external_ipv4,
            external_ipv6,
            // Diagnostic results
            dns: dns_summary,
            tls: tls_summary,
            ip_comparison: ip_comparison_result,
            traceroute: traceroute_summary,
            connection_quality: None,
        })
    }
}