cloudflare-quick-tunnel 0.3.1

Pure-Rust client for Cloudflare quick tunnels (https://*.trycloudflare.com) — no cloudflared subprocess. Speaks QUIC + capnp-RPC to the argotunnel edge directly. HTTP/1.1 + WebSocket Upgrade, HA pool, TCP keep-alive.
Documentation
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
//! Top-level orchestrator. `QuickTunnelManager::start()` runs:
//!
//!   1. POST `/tunnel`               → `api::request_tunnel`
//!   2. Edge discovery               → `edge::discover`
//!   3. QUIC dial + register         → `connect_cycle` (helper)
//!   4. Spawn reactor task           → owns the QUIC connection,
//!      runs the supervisor, and reconnects on edge drop.
//!   5. Return handle holding `url` + shutdown channel.
//!
//! The reactor task is the long-lived owner: it cycles between
//! "supervise current connection" and "reconnect with backoff +
//! `replace_existing=true`" until the operator signals shutdown or
//! the reconnect attempt count exhausts.

use std::sync::Arc;
use std::time::Duration;

use quinn::Endpoint;
use tokio::sync::oneshot;
use tracing::{debug, info, warn};
use uuid::Uuid;

use crate::api::{request_tunnel, DEFAULT_SERVICE_URL, DEFAULT_USER_AGENT};
use crate::edge::{discover, IpVersionFilter};
use crate::error::TunnelError;
use crate::pool::Pool;
use crate::quic_dial::{build_endpoint, dial_any};
use crate::rpc::{register_connection, ConnectionOptions, ControlSession, TunnelAuth};
use crate::supervisor::{self, SupervisorExit, SupervisorMetrics};

/// Default budget for POST + discovery + handshake + register.
pub const DEFAULT_HANDSHAKE_TIMEOUT: Duration = Duration::from_secs(30);

/// Default budget for the `unregisterConnection` RPC on shutdown.
pub const DEFAULT_GRACE_PERIOD: Duration = Duration::from_secs(30);

/// Hard cap on consecutive reconnect failures before the reactor
/// gives up. Each failure widens the backoff (1s → 30s, exponential
/// with a cap), so 10 attempts spans roughly 90s of trying.
pub const MAX_RECONNECT_ATTEMPTS: u32 = 10;

/// How many parallel QUIC connections to register, each on a
/// distinct `conn_index`. Higher = more resilient to a single edge
/// POP dropping. cloudflared uses 4 for named tunnels and 1 for
/// quick tunnels; 2 is a happy middle that masks a single-POP
/// reconnect (~5s gap) without doubling traffic to localhost.
pub const DEFAULT_HA_CONNECTIONS: u8 = 2;

/// Hard ceiling — matches cloudflared. Going higher hits diminishing
/// returns and the edge may rate-limit registrations from one source.
pub const MAX_HA_CONNECTIONS: u8 = 4;

/// Crate version, baked into `ConnectionOptions.client.version`.
pub const CLIENT_VERSION: &str = concat!("cloudflare-quick-tunnel/", env!("CARGO_PKG_VERSION"));

#[derive(Debug, Clone, Default)]
pub struct TunnelMetrics {
    pub streams_total: u64,
    pub bytes_in: u64,
    pub bytes_out: u64,
    pub reconnects: u64,
}

pub struct QuickTunnelHandle {
    pub url: String,
    pub tunnel_id: Uuid,
    pub account_tag: String,
    /// Edge POP location of the first registered connection (e.g.
    /// `bog01`). Additional HA connections may land in different
    /// POPs; their locations are logged but not surfaced here.
    pub location: String,
    /// Fires the shutdown signal to every HA reactor at once.
    /// `Notify` is shared across N reactors via `Arc`; we don't
    /// use `oneshot` because we'd need one per reactor.
    shutdown: Arc<tokio::sync::Notify>,
    reactors: Vec<tokio::task::JoinHandle<()>>,
    metrics_view: SupervisorMetrics,
    reconnects: Arc<std::sync::atomic::AtomicU64>,
}

impl QuickTunnelHandle {
    pub fn metrics(&self) -> TunnelMetrics {
        let (s, i, o) = self.metrics_view.snapshot();
        TunnelMetrics {
            streams_total: s,
            bytes_in: i,
            bytes_out: o,
            reconnects: self.reconnects.load(std::sync::atomic::Ordering::Relaxed),
        }
    }

    /// Signal every HA reactor to drain + unregister + close, then
    /// await them all.
    pub async fn shutdown_with(mut self, _grace: Duration) -> Result<(), TunnelError> {
        // `_grace` is honoured inside each reactor — they call
        // `ControlSession::shutdown_graceful(DEFAULT_GRACE_PERIOD)`
        // unconditionally on the way out. We keep the grace param
        // in the API for forward compatibility; once it matters,
        // pass it through via a richer shutdown command.
        self.shutdown.notify_waiters();
        for j in self.reactors.drain(..) {
            j.await
                .map_err(|e| TunnelError::Internal(format!("reactor join: {e}")))?;
        }
        Ok(())
    }

    pub async fn shutdown(self) -> Result<(), TunnelError> {
        self.shutdown_with(DEFAULT_GRACE_PERIOD).await
    }
}

impl Drop for QuickTunnelHandle {
    fn drop(&mut self) {
        // Notify all reactors; the detached tasks wind down on
        // their own. Drop is sync so we can't join them here.
        self.shutdown.notify_waiters();
    }
}

pub struct QuickTunnelManager {
    pub local_port: u16,
    pub discovery_timeout: Duration,
    pub service_url: String,
    pub user_agent: String,
    pub ha_connections: u8,
}

impl QuickTunnelManager {
    pub fn new(local_port: u16) -> Self {
        Self {
            local_port,
            discovery_timeout: DEFAULT_HANDSHAKE_TIMEOUT,
            service_url: DEFAULT_SERVICE_URL.into(),
            user_agent: DEFAULT_USER_AGENT.into(),
            ha_connections: DEFAULT_HA_CONNECTIONS,
        }
    }

    pub fn with_timeout(mut self, d: Duration) -> Self {
        self.discovery_timeout = d;
        self
    }

    pub fn with_service_url(mut self, url: impl Into<String>) -> Self {
        self.service_url = url.into();
        self
    }

    pub fn with_user_agent(mut self, ua: impl Into<String>) -> Self {
        self.user_agent = ua.into();
        self
    }

    /// Number of parallel QUIC connections to keep registered with
    /// the edge. Clamped to `1..=MAX_HA_CONNECTIONS`. Default is
    /// [`DEFAULT_HA_CONNECTIONS`] (2) — masks a single-POP drop
    /// without doubling tunnel cost.
    pub fn with_ha_connections(mut self, n: u8) -> Self {
        self.ha_connections = n.clamp(1, MAX_HA_CONNECTIONS);
        self
    }

    pub async fn start(self) -> Result<QuickTunnelHandle, TunnelError> {
        // Only the first connect cycle is bounded by `discovery_timeout`.
        // Subsequent reconnects from the reactor have their own backoff.
        tokio::time::timeout(self.discovery_timeout, self.start_inner())
            .await
            .map_err(|_| TunnelError::Internal("start() exceeded discovery_timeout".into()))?
    }

    async fn start_inner(self) -> Result<QuickTunnelHandle, TunnelError> {
        // 1. POST /tunnel — single call, the same credentials are
        //    reused on every connection + reconnect (the edge
        //    keys routing off `account_tag + tunnel_id`).
        let tunnel = request_tunnel(&self.service_url, &self.user_agent).await?;
        info!(hostname = %tunnel.hostname, id = %tunnel.id, ha = self.ha_connections, "got quick tunnel");
        let tunnel_id = Uuid::parse_str(&tunnel.id)
            .map_err(|e| TunnelError::Internal(format!("tunnel.id is not a uuid: {e}")))?;
        let url = if tunnel.hostname.starts_with("https://") {
            tunnel.hostname.clone()
        } else {
            format!("https://{}", tunnel.hostname)
        };

        let auth = TunnelAuth {
            account_tag: tunnel.account_tag.clone(),
            tunnel_secret: tunnel.secret.clone(),
        };

        // 2. Build the long-lived QUIC client endpoint. Endpoints
        //    are Clone — all HA reactors share the same UDP socket.
        let endpoint = build_endpoint()?;

        // 3. First connect cycle on `conn_index=0` —
        //    `replace_existing=false`. Synchronous so we have a
        //    URL + location to return immediately.
        let (conn0, control0, location0) =
            connect_cycle(&endpoint, &auth, tunnel_id, CLIENT_VERSION, 0, false).await?;
        info!(%location0, conn_index = 0, "first registration succeeded");

        let metrics = SupervisorMetrics::default();
        let reconnects = Arc::new(std::sync::atomic::AtomicU64::new(0));
        let shutdown = Arc::new(tokio::sync::Notify::new());
        // One TCP keep-alive pool shared by every HA reactor —
        // they all proxy to the same `127.0.0.1:<local_port>` so a
        // single LIFO cache of idle sockets serves the whole tunnel.
        let pool = Arc::new(Pool::new(self.local_port));

        // Reactor for conn_index=0 (the one whose location we
        // already know).
        let mut reactors = Vec::with_capacity(self.ha_connections as usize);
        reactors.push(tokio::spawn(reactor_loop(
            self.local_port,
            endpoint.clone(),
            auth.clone(),
            tunnel_id,
            0,
            metrics.clone(),
            reconnects.clone(),
            pool.clone(),
            conn0,
            control0,
            shutdown.clone(),
        )));

        // 4. Spawn the remaining HA reactors in background — each
        //    does its own connect_cycle, on a distinct conn_index,
        //    with `replace_existing=false`. They keep retrying via
        //    their own reactor loop if the initial register fails.
        for idx in 1..self.ha_connections {
            let endpoint = endpoint.clone();
            let auth = auth.clone();
            let metrics = metrics.clone();
            let reconnects = reconnects.clone();
            let shutdown = shutdown.clone();
            let pool = pool.clone();
            let local_port = self.local_port;
            reactors.push(tokio::spawn(async move {
                match connect_cycle(&endpoint, &auth, tunnel_id, CLIENT_VERSION, idx, false).await {
                    Ok((conn, control, location)) => {
                        info!(%location, conn_index = idx, "HA registration succeeded");
                        reactor_loop(
                            local_port, endpoint, auth, tunnel_id, idx, metrics, reconnects, pool,
                            conn, control, shutdown,
                        )
                        .await;
                    }
                    Err(e) => {
                        warn!(error = %e, conn_index = idx, "HA registration failed; will retry");
                        reactor_loop_after_failure(
                            local_port, endpoint, auth, tunnel_id, idx, metrics, reconnects, pool,
                            shutdown,
                        )
                        .await;
                    }
                }
            }));
        }

        Ok(QuickTunnelHandle {
            url,
            tunnel_id,
            account_tag: tunnel.account_tag,
            location: location0,
            shutdown,
            reactors,
            metrics_view: metrics,
            reconnects,
        })
    }
}

/// Single attempt: dial the next edge, send `RegisterConnection`
/// on the supplied `conn_index`. `replace_existing=true` on
/// reconnects so the edge accepts the new conn for the same index
/// (the previous one was dropped); `false` on the very first
/// registration of each HA leg.
async fn connect_cycle(
    endpoint: &Endpoint,
    auth: &TunnelAuth,
    tunnel_id: Uuid,
    client_version: &str,
    conn_index: u8,
    replace_existing: bool,
) -> Result<(quinn::Connection, ControlSession, String), TunnelError> {
    let edges = discover(IpVersionFilter::Auto).await?;
    let cap = edges.len().min(5);
    let conn = dial_any(endpoint, &edges[..cap]).await?;

    let mut options = ConnectionOptions::default_for_quick_tunnel(client_version);
    options.replace_existing = replace_existing;

    let (details, control) =
        register_connection(&conn, auth, tunnel_id, conn_index, &options).await?;
    Ok((conn, control, details.location))
}

#[allow(clippy::too_many_arguments)]
async fn reactor_loop(
    local_port: u16,
    endpoint: Endpoint,
    auth: TunnelAuth,
    tunnel_id: Uuid,
    conn_index: u8,
    metrics: SupervisorMetrics,
    reconnects: Arc<std::sync::atomic::AtomicU64>,
    pool: Arc<Pool>,
    mut conn: quinn::Connection,
    mut control: ControlSession,
    shutdown: Arc<tokio::sync::Notify>,
) {
    debug!(conn_index, "reactor loop started");
    loop {
        // ── Supervise current connection ─────────────────────────────────────
        let (sup_tx, sup_rx) = oneshot::channel();
        let metrics_for_cycle = metrics.clone();
        let shutdown_wait = shutdown.notified();
        tokio::pin!(shutdown_wait);
        let exit = tokio::select! {
            biased;
            _ = &mut shutdown_wait => {
                // Caller wants us out. Forward to the supervisor
                // so its accept loop sees the shutdown branch and
                // closes the QUIC connection cleanly.
                let _ = sup_tx.send(());
                SupervisorExit::Shutdown
            }
            exit = supervisor::run(conn, local_port, metrics_for_cycle, pool.clone(), sup_rx) => exit,
        };

        match exit {
            SupervisorExit::Shutdown => {
                control.shutdown_graceful(DEFAULT_GRACE_PERIOD).await;
                debug!(conn_index, "reactor: clean shutdown");
                return;
            }
            SupervisorExit::ConnectionLost => {
                drop(control);

                let mut attempt = 0u32;
                loop {
                    attempt += 1;
                    if attempt > MAX_RECONNECT_ATTEMPTS {
                        warn!(
                            conn_index,
                            "reactor: giving up after {} reconnect attempts",
                            MAX_RECONNECT_ATTEMPTS
                        );
                        return;
                    }
                    let delay = backoff(attempt);
                    warn!(conn_index, attempt, ?delay, "reactor: scheduling reconnect");
                    let shutdown_wait = shutdown.notified();
                    tokio::pin!(shutdown_wait);
                    tokio::select! {
                        biased;
                        _ = shutdown_wait => {
                            debug!(conn_index, "reactor: shutdown during reconnect backoff");
                            return;
                        }
                        _ = tokio::time::sleep(delay) => {}
                    }
                    match connect_cycle(
                        &endpoint,
                        &auth,
                        tunnel_id,
                        CLIENT_VERSION,
                        conn_index,
                        true,
                    )
                    .await
                    {
                        Ok((new_conn, new_control, new_loc)) => {
                            info!(conn_index, attempt, location = %new_loc, "reactor: reconnect succeeded");
                            reconnects.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
                            conn = new_conn;
                            control = new_control;
                            break;
                        }
                        Err(e) => {
                            warn!(attempt, error = %e, "reactor: reconnect failed");
                            // continue inner loop with bigger backoff
                        }
                    }
                }
            }
        }
    }
}

/// Entry point for an HA reactor whose initial register failed.
/// Loops with exp backoff trying to register; once one attempt
/// succeeds, hands off to the regular `reactor_loop`.
#[allow(clippy::too_many_arguments)]
async fn reactor_loop_after_failure(
    local_port: u16,
    endpoint: Endpoint,
    auth: TunnelAuth,
    tunnel_id: Uuid,
    conn_index: u8,
    metrics: SupervisorMetrics,
    reconnects: Arc<std::sync::atomic::AtomicU64>,
    pool: Arc<Pool>,
    shutdown: Arc<tokio::sync::Notify>,
) {
    let mut attempt = 0u32;
    loop {
        attempt += 1;
        if attempt > MAX_RECONNECT_ATTEMPTS {
            warn!(
                conn_index,
                "HA reactor: giving up after {} initial-register attempts", MAX_RECONNECT_ATTEMPTS
            );
            return;
        }
        let delay = backoff(attempt);
        warn!(
            conn_index,
            attempt,
            ?delay,
            "HA reactor: scheduling initial register retry"
        );
        let shutdown_wait = shutdown.notified();
        tokio::pin!(shutdown_wait);
        tokio::select! {
            biased;
            _ = shutdown_wait => return,
            _ = tokio::time::sleep(delay) => {}
        }
        // First success of an HA leg's lifetime → replace_existing=false.
        let result = connect_cycle(
            &endpoint,
            &auth,
            tunnel_id,
            CLIENT_VERSION,
            conn_index,
            false,
        )
        .await;
        match result {
            Ok((conn, control, location)) => {
                info!(conn_index, %location, "HA leg eventually registered after {attempt} retries");
                // Hand off to the regular reactor for accept loop +
                // future reconnects.
                let shutdown = shutdown.clone();
                reactor_loop(
                    local_port, endpoint, auth, tunnel_id, conn_index, metrics, reconnects, pool,
                    conn, control, shutdown,
                )
                .await;
                return;
            }
            Err(e) => warn!(conn_index, attempt, error = %e, "HA register retry failed"),
        }
    }
}

/// Exponential backoff with a 30s ceiling: 1s, 2s, 4s, 8s, 16s, 30s, 30s, …
fn backoff(attempt: u32) -> Duration {
    let secs = 1u64.checked_shl(attempt.saturating_sub(1)).unwrap_or(30);
    Duration::from_secs(secs.min(30))
}

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

    #[test]
    fn backoff_curve() {
        assert_eq!(backoff(1), Duration::from_secs(1));
        assert_eq!(backoff(2), Duration::from_secs(2));
        assert_eq!(backoff(3), Duration::from_secs(4));
        assert_eq!(backoff(4), Duration::from_secs(8));
        assert_eq!(backoff(5), Duration::from_secs(16));
        assert_eq!(backoff(6), Duration::from_secs(30));
        assert_eq!(backoff(20), Duration::from_secs(30));
    }
}