etunnel 1.7.20

Expose local services to the internet in one line of code: instant public HTTPS URLs with automatic TLS, through NAT and firewalls, no signup. Webhook-friendly ngrok-style reverse tunnel, with SDKs in 20 languages and a single-binary Go server.
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
// etunnel SDK for Rust (tungstenite + ureq).
// Buffer-based protocol client (bodies buffered per stream; no WS upgrades).
use std::collections::HashMap;
use std::collections::hash_map::RandomState;
use std::hash::{BuildHasher, Hasher};
use std::io::Read;
use std::net::TcpStream;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::mpsc;
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;

use tungstenite::client::IntoClientRequest;
use tungstenite::handshake::client::Response;
use tungstenite::protocol::{Message, WebSocketConfig};
use tungstenite::stream::MaybeTlsStream;

const MAXP: usize = 32768;
/// Read tick for the socket: how long a blocking read may hold the lock
/// before the loop drains outbound messages (#53).
const READ_TICK_MS: u64 = 200;

pub struct Options {
    pub token: Option<String>,
    pub subdomain: Option<String>,
}

pub struct Tunnel {
    pub url: String,
    /// Server-signed resume token: pass it back on reconnect to reclaim the
    /// same subdomain instead of getting a fresh random one.
    pub resume_token: String,
    /// Set once the reader loop sees the connection end (error, close or bye).
    /// The CLI supervisor polls it to decide when to reconnect.
    pub closed: Arc<AtomicBool>,
}

impl Tunnel {
    /// Dropping the tunnel process / task ends the connection; the server
    /// detects the closed socket and frees the subdomain.
    pub fn close(&self) {}
}

fn frame(kind: u8, sid: u32, payload: &[u8]) -> Message {
    let mut b = Vec::with_capacity(9 + payload.len());
    b.push(kind);
    b.extend_from_slice(&sid.to_be_bytes());
    b.extend_from_slice(&(payload.len() as u32).to_be_bytes());
    b.extend_from_slice(payload);
    Message::Binary(b.into())
}

// Minimal JSON helpers (control messages are simple, hand-rolled is enough).
fn json_escape(s: &str) -> String {
    s.replace('\\', "\\\\").replace('"', "\\\"").replace('\n', "\\n")
}

fn extract_str(json: &str, key: &str) -> Option<String> {
    let pat = format!("\"{}\":", key);
    let i = json.find(&pat)? + pat.len();
    let rest = &json[i..];
    let rest = rest.trim_start();
    if !rest.starts_with('"') {
        return None;
    }
    let mut out = String::new();
    let mut chars = rest[1..].chars();
    while let Some(c) = chars.next() {
        match c {
            '"' => break,
            '\\' => match chars.next() {
                Some('n') => out.push('\n'),
                Some('t') => out.push('\t'),
                Some(o) => out.push(o),
                None => break,
            },
            o => out.push(o),
        }
    }
    Some(out)
}

fn extract_num(json: &str, key: &str) -> Option<f64> {
    let pat = format!("\"{}\":", key);
    let i = json.find(&pat)? + pat.len();
    let rest = json[i..].trim_start();
    let end = rest
        .find(|c: char| !(c.is_ascii_digit() || c == '.' || c == '-'))
        .unwrap_or(rest.len());
    rest[..end].parse().ok()
}

// Very small header-map extractor for req messages: "headers":{"K":["v"],...}
fn extract_headers(json: &str) -> HashMap<String, Vec<String>> {
    let mut out = HashMap::new();
    let Some(hstart) = json.find("\"headers\":{") else { return out };
    let body = &json[hstart + 11..];
    // walk pairs until the matching close brace (naive but messages are flat).
    // Iterate chars, not bytes: `byte as char` re-encoded multi-byte UTF-8
    // header values as Latin-1 mojibake (#53 LOW).
    let chars: Vec<char> = body.chars().collect();
    let mut i = 0usize;
    let mut depth = 1usize;
    let mut cur_key = String::new();
    let mut cur_vals: Vec<String> = Vec::new();
    let mut in_key = false;
    let mut in_val = false;
    let mut buf = String::new();
    while i < chars.len() {
        let c = chars[i];
        match c {
            '"' => {
                if in_key || in_val {
                    if in_key { cur_key = buf.clone(); } else { cur_vals.push(buf.clone()); }
                    buf.clear();
                    in_key = false;
                    in_val = false;
                } else if cur_key.is_empty() {
                    in_key = true;
                } else {
                    in_val = true;
                }
            }
            '{' | '[' => depth += 1,
            '}' | ']' => {
                depth -= 1;
                if depth == 0 {
                    if !cur_key.is_empty() {
                        out.insert(cur_key.clone(), cur_vals.clone());
                    }
                    break;
                }
            }
            ',' if depth == 1 && !cur_key.is_empty() && cur_vals.is_empty() => {
                // next key after value list ends is handled by ':' ... fallthrough
            }
            ':' if depth == 1 && !cur_key.is_empty() && cur_vals.is_empty() => {}
            _ => {
                if in_key || in_val {
                    buf.push(c);
                }
            }
        }
        i += 1;
    }
    out
}

struct StreamBuf {
    method: String,
    path: String,
    headers: HashMap<String, Vec<String>>,
    body: Vec<u8>,
}

pub fn connect(server: &str, local_port: u16, token: Option<&str>, subdomain: Option<&str>) -> Result<Tunnel, String> {
    connect_full(server, &format!("127.0.0.1:{}", local_port), token, subdomain)
}

pub fn connect_full(server: &str, local_addr: &str, token: Option<&str>, subdomain: Option<&str>) -> Result<Tunnel, String> {
    connect_local(server, Local::One(local_addr.to_string()), token, subdomain, None)
}

/// Route-aware connect: several local services under one public hostname
/// dispatched by URL path prefix (see route.rs).
pub fn connect_routes(server: &str, routes: Vec<Route>, token: Option<&str>, subdomain: Option<&str>, resume: Option<&str>) -> Result<Tunnel, String> {
    let mut routes = routes;
    route::normalize(&mut routes)?;
    connect_local(server, Local::Routes(routes), token, subdomain, resume)
}

/// Like [`connect_full`], plus an optional resume token from a previous
/// session so the server hands back the same subdomain after a drop.
pub fn connect_full_resume(server: &str, local_addr: &str, token: Option<&str>, subdomain: Option<&str>, resume: Option<&str>) -> Result<Tunnel, String> {
    connect_local(server, Local::One(local_addr.to_string()), token, subdomain, resume)
}

fn connect_local(server: &str, local: Local, token: Option<&str>, subdomain: Option<&str>, resume: Option<&str>) -> Result<Tunnel, String> {
    let mut request = server
        .into_client_request()
        .map_err(|e| format!("bad url: {}", e))?;
    request.headers_mut().insert(
        "Sec-WebSocket-Protocol",
        "etunnel-v1".parse().unwrap(),
    );
    let cfg = WebSocketConfig::default();

    let _ = cfg;
    let (ws, _resp) = tungstenite::connect(request)
        .map_err(|e| format!("connect: {}", e))?;

    // Bound every blocking read so the reader periodically releases the
    // socket lock and drains the outbound queue: holding the lock across an
    // unbounded read() blocked every sender (serve threads, pong) until the
    // next inbound frame arrived (#53 MED). MaybeTlsStream has no blanket
    // timeout setter, so drill into the concrete stream it wraps.
    //
    // #242: the tick is applied AFTER the welcome exchange. The handshake
    // loop below does not treat WouldBlock/TimedOut as retryable, so a 200ms
    // tick during the handshake turned any welcome that arrived later than
    // 200ms (cross-geo latency, busy server) into "read welcome: TimedOut" -
    // the Rust CLI was effectively unusable off localhost. During the
    // handshake we want no tick at all; the steady-state reader spawned
    // further down DOES handle the tick (it drops the lock and flushes), so
    // it keeps the #53 guarantee.
    fn apply_tick(ws: &tungstenite::WebSocket<MaybeTlsStream<TcpStream>>, tick: Option<Duration>) {
        let res = match ws.get_ref() {
            MaybeTlsStream::Plain(s) => s.set_read_timeout(tick),
            MaybeTlsStream::Rustls(s) => s.get_ref().set_read_timeout(tick),
            // other TLS backends: best-effort, the queue still drains between
            // frames whenever the peer is active
            #[allow(unreachable_patterns)]
            _ => Ok(()),
        };
        if let Err(e) = res {
            eprintln!("etunnel: read timeout not applied: {}", e);
        }
    }
    apply_tick(&ws, None); // handshake phase: no tick (#242)

    let ws = Arc::new(Mutex::new(ws));
    // Outbound relay: senders hand messages to the reader loop, which flushes
    // them between reads - nobody but the reader ever locks the socket for
    // writing, so frames also cannot interleave (#53).
    let (out_tx, out_rx) = mpsc::channel::<Message>();

    let mut hello = String::from("{\"type\":\"hello\",\"proto\":1");
    if let Some(s) = subdomain {
        hello.push_str(&format!(",\"subdomain\":\"{}\"", json_escape(s)));
    }
    if let Some(t) = token {
        hello.push_str(&format!(",\"token\":\"{}\"", json_escape(t)));
    }
    if let Some(r) = resume {
        if !r.is_empty() {
            hello.push_str(&format!(",\"resume\":\"{}\"", json_escape(r)));
        }
    }
    hello.push_str(",\"client\":\"etunnel-rust/1.0\"}");
    ws.lock().unwrap().send(Message::Text(hello.into())).ok();

    let mut url = String::new();
    let mut resume_token = String::new();
    loop {
        let msg = ws.lock().unwrap().read().map_err(|e| format!("read welcome: {}", e))?;
        if let Message::Text(t) = msg {
            let t = t.as_str();
            if let Some(u) = extract_str(t, "url") {
                if t.contains("\"type\":\"welcome\"") {
                    url = u;
                    resume_token = extract_str(t, "resumeToken").unwrap_or_default();
                    break;
                }
            }
            if t.contains("\"type\":\"error\"") {
                let code = extract_str(t, "code").unwrap_or_default();
                return Err(format!("etunnel: {}", code));
            }
        }
    }

    let closed = Arc::new(AtomicBool::new(false));
    let ws2 = Arc::clone(&ws);
    let local = local; // Local (single or routed); serve() resolves per request
    let closed2 = Arc::clone(&closed);
    // Welcome received: arm the read tick for the steady-state reader (#242).
    // It handles WouldBlock/TimedOut by design (drops the lock, flushes), so
    // the #53 anti-head-of-line guarantee is preserved from here on.
    apply_tick(&ws2.lock().unwrap(), Some(Duration::from_millis(READ_TICK_MS)));
    thread::spawn(move || {
        let mut streams: HashMap<u32, StreamBuf> = HashMap::new();
        let mut out_rx = out_rx;
        loop {
            let msg = {
                let mut w = ws2.lock().unwrap_or_else(|p| p.into_inner());
                match w.read() {
                    Ok(m) => Some(m),
                    Err(tungstenite::Error::Io(ref e))
                        if e.kind() == std::io::ErrorKind::WouldBlock
                            || e.kind() == std::io::ErrorKind::TimedOut => {
                        // read tick elapsed: drop the lock and flush senders
                        None
                    }
                    Err(_) => break,
                }
            };
            drain_outbox(&ws2, &mut out_rx);
            let Some(msg) = msg else { continue };
            match msg {
                Message::Text(t) => {
                    let t = t.as_str().to_string();
                    if t.contains("\"type\":\"req\"") {
                        let sid = extract_num(&t, "streamId").unwrap_or(0.0) as u32;
                        streams.insert(sid, StreamBuf {
                            method: extract_str(&t, "method").unwrap_or_else(|| "GET".into()),
                            path: extract_str(&t, "path").unwrap_or_else(|| "/".into()),
                            headers: extract_headers(&t),
                            body: Vec::new(),
                        });
                    } else if t.contains("\"type\":\"ping\"") {
                        let ts = extract_num(&t, "ts").unwrap_or(0.0) as i64;
                        out_tx.send(Message::Text(format!("{{\"type\":\"pong\",\"ts\":{}}}", ts).into())).ok();
                    } else if t.contains("\"type\":\"bye\"") {
                        break;
                    }
                }
                Message::Binary(b) => {
                    if b.len() < 9 { drain_outbox(&ws2, &mut out_rx); continue; }
                    let kind = b[0];
                    let sid = u32::from_be_bytes([b[1], b[2], b[3], b[4]]);
                    let n = u32::from_be_bytes([b[5], b[6], b[7], b[8]]) as usize;
                    let payload = &b[9..(9 + n).min(b.len())];
                    let Some(st) = streams.get_mut(&sid) else { continue };
                    match kind {
                        0x01 => st.body.extend_from_slice(payload),
                        0x02 => {
                            let st = streams.remove(&sid).unwrap();
                            let tx3 = out_tx.clone();
                            let local3 = local.clone();
                            thread::spawn(move || serve(tx3, local3, sid, st));
                        }
                        0x03 => { streams.remove(&sid); }
                        _ => {}
                    }
                }
                Message::Close(_) => break,
                _ => {}
            }
        }
        closed2.store(true, Ordering::SeqCst);
    });

    Ok(Tunnel { url, resume_token, closed })
}

/// Reader-side flush of the outbound relay: sends every queued message on the
/// socket the reader owns (#53).
fn drain_outbox(ws: &Mutex<tungstenite::WebSocket<MaybeTlsStream<TcpStream>>>,
                rx: &mut mpsc::Receiver<Message>) {
    while let Ok(m) = rx.try_recv() {
        let mut w = ws.lock().unwrap_or_else(|p| p.into_inner());
        if w.send(m).is_err() {
            return;
        }
    }
}

/// Themed 502 page shown when the local app is down (same HTML as the other SDKs).
const ERR_PAGE_A: &str = r##"<!doctype html><html><head><meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>502 - local app down</title>
<style>
:root{--bg:#0b0e14;--panel:#0d1117;--line:#21262d;--fg:#e6edf3;--dim:#8b949e;--green:#3fb950}
*{box-sizing:border-box;margin:0;padding:0}
body{font-family:ui-monospace,'SF Mono',Menlo,Consolas,monospace;background:var(--bg);color:var(--fg);
display:flex;min-height:100vh;align-items:center;justify-content:center;line-height:1.6}
main{max-width:600px;padding:2.5rem 1.5rem;width:100%}
.badge{display:inline-block;border:1px solid rgba(248,81,73,.4);color:#f85149;border-radius:99px;
padding:.2rem .8rem;font-size:.72rem;letter-spacing:.08em;text-transform:uppercase;margin-bottom:1.1rem}
h1{font-size:1.6rem;letter-spacing:-.01em;margin-bottom:.4rem}
h1 .g{color:var(--green)}
p{color:var(--dim);font-size:.9rem;margin-bottom:1.4rem}
.panel{background:var(--panel);border:1px solid var(--line);border-radius:12px;padding:1.1rem 1.3rem;margin-bottom:1.1rem}
.panel .k{color:var(--dim);font-size:.72rem;text-transform:uppercase;letter-spacing:.08em;margin-bottom:.3rem}
.panel .v{font-size:.9rem;word-break:break-all}
.err{color:#f85149}
.tip{border-left:3px solid var(--green);padding:.8rem 1rem;margin-top:1.2rem;font-size:.82rem;color:var(--dim)}
.tip b{color:var(--fg)}
footer{margin-top:2rem;font-size:.75rem;color:var(--dim)}
footer a{color:#58a6ff;text-decoration:none}
.pulse{display:inline-block;width:.55em;height:.55em;border-radius:50%;background:var(--green);
margin-right:.5em;animation:p 1.2s ease-in-out infinite}
@keyframes p{0%,100%{opacity:1}50%{opacity:.25}}
@media(prefers-reduced-motion:reduce){.pulse{animation:none}}
</style></head><body><main>
<span class="badge">502 local app down</span>
<h1>tunnel up, <span class="g">local app</span> is not</h1>
<p>The tunnel connection is alive, but nothing answered when we knocked.</p>
<div class="panel">
<div class="k">error</div><div class="v err">"##;

const ERR_PAGE_B: &str = r##"</div>
</div>
<div class="tip"><span class="pulse"></span><b>Fix:</b> start your local service and reload -
the tunnel stays connected and keeps retrying for you.</div>
<footer>powered by <a href="https://github.com/topcheer/etunnel">etunnel</a></footer>
</main></body></html>"##;

fn escape_html(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    for c in s.chars() {
        match c {
            '&' => out.push_str("&amp;"),
            '<' => out.push_str("&lt;"),
            '>' => out.push_str("&gt;"),
            _ => out.push(c),
        }
    }
    out
}

fn local_error_page(msg: &str) -> String {
    format!("{}{}{}", ERR_PAGE_A, escape_html(msg), ERR_PAGE_B)
}

mod route;

use route::{parse_conf, parse_route_spec, Route};

/// Local dial target: one address (legacy) or a route table (multi-target).
#[derive(Clone)]
enum Local {
    One(String),
    Routes(Vec<Route>),
}

impl Local {
    fn resolve(&self, path: &str) -> Option<(String, String)> {
        match self {
            Local::One(a) => Some((a.clone(), path.to_string())),
            Local::Routes(rs) => route::route_for(rs, path),
        }
    }
}

fn serve(tx: mpsc::Sender<Message>, local: Local, sid: u32, st: StreamBuf) {
    let (addr, path) = match local.resolve(&st.path) {
        Some(hit) => hit,
        None => {
            // No route matches: a real 404 answered at the SDK edge.
            tx.send(Message::Text(format!(
                "{{\"type\":\"res\",\"streamId\":{},\"status\":404,\"headers\":{{\"Content-Type\":[\"text/plain; charset=utf-8\"]}}}}", sid).into())).ok();
            tx.send(frame(0x01, sid, b"404 page not found (no route matches this path)\n")).ok();
            tx.send(frame(0x02, sid, b"")).ok();
            return;
        }
    };
    let hop = |k: &str| {
        matches!(k.to_ascii_lowercase().as_str(), "connection" | "transfer-encoding" | "keep-alive"
            | "upgrade" | "content-length" | "host" | "proxy-connection" | "te" | "trailer")
    };
    let mut req = ureq::request(&st.method, &format!("http://{}{}", addr, path));
    for (k, vs) in &st.headers {
        if hop(k) { continue; }
        for v in vs {
            req = req.set(k, v);
        }
    }
    let body = st.body.clone();
    let resp = if st.method == "GET" || st.method == "HEAD" {
        req.call()
    } else {
        req.send_bytes(&body)
    };

    let (status, headers, out) = match resp {
        Ok(r) => {
            let status = r.status();
            let mut hdrs: Vec<(String, String)> = Vec::new();
            for name in r.headers_names() {
                if hop(&name) { continue; }
                if let Some(v) = r.header(&name) {
                    hdrs.push((name, v.to_string()));
                }
            }
            let mut buf = Vec::new();
            r.into_reader().take(64 * 1024 * 1024).read_to_end(&mut buf).unwrap_or(0);
            (status, hdrs, buf)
        }
        Err(ureq::Error::Status(code, r)) => {
            let mut buf = Vec::new();
            r.into_reader().read_to_end(&mut buf).unwrap_or(0);
            (code, Vec::new(), buf)
        }
        Err(e) => {
            tx.send(Message::Text(format!(
                "{{\"type\":\"res\",\"streamId\":{},\"status\":502,\"headers\":{{\"Content-Type\":[\"text/html; charset=utf-8\"]}}}}", sid).into())).ok();
            tx.send(frame(0x01, sid, local_error_page(&e.to_string()).as_bytes())).ok();
            tx.send(frame(0x02, sid, b"")).ok();
            return;
        }
    };

    let hdr_json = headers
        .iter()
        .map(|(k, v)| format!("\"{}\":[\"{}\"]", json_escape(k), json_escape(v)))
        .collect::<Vec<_>>()
        .join(",");
    tx.send(Message::Text(format!(
        "{{\"type\":\"res\",\"streamId\":{},\"status\":{},\"headers\":{{{}}}}}", sid, status, hdr_json).into())).ok();
    for chunk in out.chunks(MAXP) {
        tx.send(frame(0x01, sid, chunk)).ok();
    }
    tx.send(frame(0x02, sid, b"")).ok();
}

fn backoff_ms(attempt: u32) -> u64 {
    // 500ms, 1s, 2s, ... capped at 30s, then ±20% jitter. The jitter mixes in
    // the RandomState seed instead of subsec_nanos alone: nanos-derived
    // jitter is predictable across processes started in the same millisecond
    // (#53 LOW).
    let base = (500u64 << attempt.min(6)).min(30_000);
    let r = RandomState::new().build_hasher().finish();
    base * (800 + r % 401) / 1000
}

fn main() {
    // Args: [options] [local-port-or-addr]. Options (all optional):
    //   --route PREFIX=ADDR[,strip]   repeatable; switches to multi-target
    //   --config PATH                 key=value config with [route] blocks
    //   --server URL / --token KEY / --subdomain NAME
    // Precedence: flags > env (ETUNNEL_*) > --config file > defaults.
    let mut server_arg: Option<String> = None;
    let mut token_arg: Option<String> = None;
    let mut subdomain_arg: Option<String> = None;
    let mut route_specs: Vec<String> = Vec::new();
    let mut conf_path: Option<String> = None;
    let mut positional: Option<String> = None;
    let mut args = std::env::args().skip(1);
    while let Some(a) = args.next() {
        match a.as_str() {
            "--route" => route_specs.push(args.next().unwrap_or_else(|| { eprintln!("etunnel: --route needs PREFIX=ADDR[,strip]"); std::process::exit(2); })),
            "--config" => conf_path = Some(args.next().unwrap_or_else(|| { eprintln!("etunnel: --config needs a path"); std::process::exit(2); })),
            "--server" => server_arg = Some(args.next().unwrap_or_else(|| { eprintln!("etunnel: --server needs a URL"); std::process::exit(2); })),
            "--token" => token_arg = Some(args.next().unwrap_or_else(|| { eprintln!("etunnel: --token needs a key"); std::process::exit(2); })),
            "--subdomain" => subdomain_arg = Some(args.next().unwrap_or_else(|| { eprintln!("etunnel: --subdomain needs a name"); std::process::exit(2); })),
            "--help" | "-h" => {
                eprintln!("usage: etunnel [--server URL] [--token KEY] [--subdomain NAME] [local-port-or-addr]\n       etunnel [--config etunnel.conf] [--route PREFIX=ADDR[,strip]]...");
                std::process::exit(0);
            }
            other if other.starts_with("--") => { eprintln!("etunnel: unknown flag {}", other); std::process::exit(2); }
            other => positional = Some(other.to_string()),
        }
    }
    let conf = {
        let path = conf_path.clone().or_else(|| std::path::Path::new("etunnel.conf").exists().then(|| "etunnel.conf".to_string()));
        match path {
            Some(p) => match std::fs::read_to_string(&p) {
                Ok(text) => match parse_conf(&text) {
                    Ok(c) => c,
                    Err(e) => { eprintln!("etunnel: {}: {}", p, e); std::process::exit(2); }
                },
                Err(e) => { eprintln!("etunnel: read {}: {}", p, e); std::process::exit(2); }
            },
            None => parse_conf("").unwrap(),
        }
    };
    let server = server_arg
        .or_else(|| std::env::var("ETUNNEL_SERVER").ok())
        .or(conf.server)
        .unwrap_or_else(|| option_env!("ETUNNEL_BUILD_SERVER").unwrap_or("wss://t.etun.uk/tunnel").to_string());
    let token = token_arg
        .or_else(|| std::env::var("ETUNNEL_TOKEN").ok())
        .or(conf.token);
    let subdomain = subdomain_arg
        .or_else(|| std::env::var("ETUNNEL_SUBDOMAIN").ok())
        .or(conf.subdomain);
    // CLI --route specs win over config [route] blocks; a positional/local
    // only applies in single-target mode.
    let cli_routes: Result<Vec<Route>, String> = route_specs.iter()
        .map(|s| parse_route_spec(s)).collect();
    let cli_routes = match cli_routes {
        Ok(rs) => rs,
        Err(e) => { eprintln!("etunnel: {}", e); std::process::exit(2); }
    };
    let (local, routes) = if !cli_routes.is_empty() {
        if positional.is_some() { eprintln!("etunnel: positional <local-port-or-addr> cannot be combined with --route"); std::process::exit(2); }
        (None, cli_routes)
    } else if !conf.routes.is_empty() {
        (None, conf.routes)
    } else {
        let l = positional
            .or_else(|| std::env::var("ETUNNEL_LOCAL").ok())
            .unwrap_or_else(|| "3000".into());
        let l = if l.contains(':') { l } else { format!("127.0.0.1:{}", l) };
        let (host, port) = l.rsplit_once(':').unwrap();
        let port: u16 = port.parse().unwrap_or(3000);
        (Some(format!("{}:{}", host, port)), Vec::new())
    };
    if local.is_none() && routes.is_empty() {
        eprintln!("etunnel: nothing to expose (no --route, no config [route], no local address)");
        std::process::exit(2);
    }
    // ETUNNEL_RECONNECT=0 restores the one-shot behaviour (exit on first error).
    let reconnect = std::env::var("ETUNNEL_RECONNECT").map(|v| v != "0").unwrap_or(true);

    // SIGINT/SIGTERM keep their default disposition: the process dies at once,
    // so a stopped CLI never reconnects.
    let mut attempt: u32 = 0;
    // Carried across drops so a reconnect reclaims the same subdomain.
    let mut resume: Option<String> = None;
    loop {
        let connected = if !routes.is_empty() {
            connect_routes(&server, routes.clone(), token.as_deref(), subdomain.as_deref(), resume.as_deref())
        } else {
            connect_full_resume(&server, local.as_deref().unwrap(), token.as_deref(), subdomain.as_deref(), resume.as_deref())
        };
        match connected {
            Ok(t) => {
                println!("TUNNEL_URL: {}", t.url);
                attempt = 0;
                if !t.resume_token.is_empty() {
                    resume = Some(t.resume_token.clone());
                }
                if !reconnect {
                    loop { thread::sleep(std::time::Duration::from_secs(3600)); }
                }
                while !t.closed.load(Ordering::SeqCst) {
                    thread::sleep(std::time::Duration::from_millis(200));
                }
                eprintln!("etunnel: connection lost, reconnecting");
            }
            Err(e) => {
                eprintln!("etunnel: {}", e);
                if !reconnect {
                    std::process::exit(1);
                }
            }
        }
        let delay = backoff_ms(attempt);
        attempt = attempt.saturating_add(1);
        eprintln!("etunnel: reconnect in {}ms", delay);
        thread::sleep(std::time::Duration::from_millis(delay));
    }
}

// --- CLI loopback login (RFC 8252 style; server /auth/cli/* endpoints) ---
//   let creds = etunnel_login("https://t.etun.uk", None, 600).ok();
// Binds a random 127.0.0.1 port, opens the browser (prints the URL when the
// opener fails), waits for the one-time code (10-minute budget), exchanges
// it. Persist creds and pass it as the token option on future connects.
pub struct LoginCredentials {
    pub api_token: String,
    pub subdomains: Vec<String>,
    pub plan: String,
}

pub fn etunnel_login<F: Fn(&str) -> std::io::Result<()>>(
    server: &str,
    open_browser: Option<F>,
    timeout_secs: u64,
) -> Result<LoginCredentials, String> {
    use std::io::{Read, Write};
    let base = server.trim_end_matches('/');
    if base.is_empty() {
        return Err("etunnel: login: server required".into());
    }

    let srv = std::net::TcpListener::bind("127.0.0.1:0").map_err(|e| e.to_string())?;
    let port = srv.local_addr().map_err(|e| e.to_string())?.port();

    let start_body = format!("{{\"redirect_uri\":\"http://127.0.0.1:{port}/callback\"}}");
    let start = ureq::post(&format!("{base}/auth/cli/start"))
        .set("Content-Type", "application/json")
        .send_string(&start_body)
        .map_err(|e| format!("etunnel: login start: {e}"))?
        .into_string().map_err(|e| e.to_string())?;
    let auth_url = json_field(&start, "auth_url").ok_or("etunnel: login: empty start payload")?;
    let device_code = json_field(&start, "device_code").ok_or("etunnel: login: empty start payload")?;
    let mut opened = false;
    if let Some(opener) = &open_browser {
        if opener(&auth_url).is_ok() { opened = true; }
    }
    if !opened {
        let cmd = if cfg!(target_os = "macos") { "open" } else { "xdg-open" };
        if std::process::Command::new(cmd).arg(&auth_url).spawn().is_ok() {
            opened = true;
        }
    }
    if !opened {
        eprintln!("etunnel: authorize in your browser: {auth_url}");
    }

    srv.set_nonblocking(true).ok();
    let deadline = std::time::Instant::now() + std::time::Duration::from_secs(timeout_secs);
    let mut code = String::new();
    while code.is_empty() {
        if std::time::Instant::now() > deadline {
            return Err("etunnel: login timed out".into());
        }
        let (mut c, _) = match srv.accept() {
            Ok(x) => x,
            Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => {
                std::thread::sleep(std::time::Duration::from_millis(100));
                continue;
            }
            Err(e) => return Err(e.to_string()),
        };
        let mut req = String::new();
        let _ = c.read_to_string(&mut req);
        if let Some(q) = req.split(' ').nth(1) {
            if let Some(kv) = q.split('?').nth(1) {
                for pair in kv.split('&') {
                    if let Some(v) = pair.strip_prefix("code=") {
                        code = v.to_string();
                    }
                }
            }
        }
        let body = "<!doctype html><p>Authorized. You can close this tab and return to the terminal.</p>";
        let _ = c.write_all(format!(
            "HTTP/1.0 200 OK\r\nContent-Type: text/html; charset=utf-8\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}",
            body.len(), body).as_bytes());
    }
    drop(srv);

    let ex_body = format!("{{\"device_code\":\"{device_code}\",\"code\":\"{code}\"}}");
    let ex = ureq::post(&format!("{base}/auth/cli/exchange"))
        .set("Content-Type", "application/json")
        .send_string(&ex_body)
        .map_err(|e| format!("etunnel: login exchange: {e}"))?
        .into_string().map_err(|e| e.to_string())?;
    let api_token = json_field(&ex, "api_token").filter(|t| !t.is_empty())
        .ok_or("etunnel: login: no api_token in exchange response")?;
    let subdomains = json_string_array(&ex, "subdomains").unwrap_or_default();
    let plan = json_field(&ex, "plan").unwrap_or_default();
    Ok(LoginCredentials { api_token, subdomains, plan })
}

// Minimal extractors for the flat exchange payload (no JSON crate needed).
fn json_field(s: &str, key: &str) -> Option<String> {
    let pat = format!("\"{key}\":\"");
    let i = s.find(&pat)? + pat.len();
    let rest = &s[i..];
    let mut out = String::new();
    let mut esc = false;
    for ch in rest.chars() {
        if esc { out.push(ch); esc = false; continue; }
        match ch {
            '\\' => esc = true,
            '"' => return Some(out),
            _ => out.push(ch),
        }
    }
    None
}

fn json_string_array(s: &str, key: &str) -> Option<Vec<String>> {
    let pat = format!("\"{key}\":[");
    let i = s.find(&pat)? + pat.len();
    let rest = &s[i..];
    let end = rest.find(']')?;
    let inner = &rest[..end];
    Some(inner.split(',').map(|x| x.trim().trim_matches('"').to_string()).filter(|x| !x.is_empty()).collect())
}

#[cfg(test)]
mod login_e2e {
    use super::*;
    // E2E against sdks/testtools/loginserver (run via tests/test_login.sh,
    // which exports FX_ADDR). Skipped when the fixture is not up.
    #[test]
    fn loopback_login_start_grant_exchange() {
        let addr = match std::env::var("FX_ADDR") {
            Ok(a) if !a.is_empty() => a,
            _ => { eprintln!("SKIP: FX_ADDR not set"); return; }
        };
        let creds = etunnel_login(&format!("http://{addr}"), Some(|u: &str| {
            std::process::Command::new("curl").args(["-s", "-o", "/dev/null", u]).spawn().map(|_| ()).map_err(|e| e).ok();
            Ok(())
        }), 30).expect("login flow failed");
        assert_eq!(creds.api_token, "tok-123");
        assert_eq!(creds.subdomains, vec!["my-sub"]);
        assert_eq!(creds.plan, "pro");
    }
}