tunly 0.4.0

A lightweight, self-hosted HTTP tunnel for exposing local applications to the internet.
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
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
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
use std::{
    collections::HashMap,
    net::SocketAddr,
    sync::{
        atomic::{AtomicU64, Ordering},
        Arc,
    },
    time::{Duration, Instant, SystemTime, UNIX_EPOCH},
};

use axum::{
    extract::{
        connect_info::ConnectInfo,
        ws::{Message, WebSocket, WebSocketUpgrade},
        Path, Query, State,
    },
    http::{HeaderMap, Request, StatusCode, Uri},
    response::{IntoResponse, Response},
    routing::{any, get},
    Router,
};
use base64::{engine::general_purpose, Engine as _};
use flate2::read::{ZlibDecoder, ZlibEncoder};
use flate2::Compression;
use futures::{SinkExt, StreamExt};
use jsonwebtoken::{decode, encode, DecodingKey, EncodingKey, Header, Validation};
use prometheus::{Counter, Encoder, Gauge, Histogram, HistogramOpts, Registry, TextEncoder};
use rand::Rng;
use rusqlite::{params, Connection};
use serde::{Deserialize, Serialize};
use std::io::Read;
use subtle::ConstantTimeEq;
use tokio::sync::{mpsc, oneshot, Mutex, RwLock};
use tower_http::normalize_path::NormalizePathLayer;
use tower_http::trace::TraceLayer;

// Simple per-IP rate limit for /token: 10 requests per 60 seconds
pub const RL_WINDOW_SECS: u64 = 60;
pub const RL_MAX_PER_WINDOW: u32 = 10;

// Per-IP rate limit for proxy requests: 120 requests per 60 seconds
pub const PROXY_RL_WINDOW_SECS: u64 = 60;
pub const PROXY_RL_MAX_PER_WINDOW: u32 = 120;

fn fixed_token_matches(provided: &str, expected: &str) -> bool {
    provided.as_bytes().ct_eq(expected.as_bytes()).into()
}

// Session idle TTL (seconds) before being GC-removed if no activity
pub const SESSION_IDLE_TTL_SECS: u64 = 600;

#[derive(Debug, Serialize, Deserialize)]
pub struct Claims {
    pub sub: String, // sid
    pub ip: String,
    pub exp: usize,
    pub jti: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct TokenResponse {
    pub token: String,
    pub session: String,
    pub expires_in: u64,
}

#[derive(Debug, Clone)]
pub enum AuthMode {
    Fixed(String),
    Ephemeral,
}

#[derive(Debug, Clone)]
pub struct AccessLogEntry {
    pub method: String,
    pub uri: String,
    pub status: u16,
    pub dur_ms: u128,
    pub bytes_in: u64,  // Request body size
    pub bytes_out: u64, // Response body size
}
#[derive(Debug, Serialize, Deserialize)]
pub struct DashboardTunnelInfo {
    pub session_id: String,
    pub created_at: u64,
    pub last_seen: u64,
    pub total_requests: u64,
    pub bytes_in: u64,
    pub bytes_out: u64,
    pub status: String, // "active" or "idle"
}

#[derive(Debug, Serialize, Deserialize)]
pub struct DashboardStats {
    pub active_sessions: usize,
    pub total_requests: u64,
    pub uptime_secs: u64,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct DashboardRequest {
    pub timestamp: u64,
    pub method: String,
    pub uri: String,
    pub status: u16,
    pub latency_ms: u128,
}

#[derive(Debug)]
pub struct SessionState {
    pub outbound_tx: mpsc::Sender<ServerToClient>,
    pub pending: Mutex<HashMap<u64, oneshot::Sender<ClientToServer>>>,
    pub _created_at: Instant,
    pub last_seen: Mutex<Instant>,
    pub access_log: Mutex<Vec<AccessLogEntry>>, // ring buffer (last N)
    pub request_count: AtomicU64,               // Total requests for this tunnel
    pub bytes_in: AtomicU64,                    // Total bytes received from client
    pub bytes_out: AtomicU64,                   // Total bytes sent to client
}

pub struct Metrics {
    pub registry: Registry,
    pub proxy_requests: Counter,
    pub proxy_latency_seconds: Histogram,
    pub active_sessions: Gauge,
}

impl Default for Metrics {
    fn default() -> Self {
        Self::new()
    }
}

impl Metrics {
    pub fn new() -> Self {
        let registry = Registry::new();

        let proxy_requests =
            Counter::new("proxy_requests_total", "Total proxy requests handled").unwrap();
        let proxy_latency_seconds = Histogram::with_opts(HistogramOpts::new(
            "proxy_latency_seconds",
            "Latency of proxied requests",
        ))
        .unwrap();
        let active_sessions = Gauge::new(
            "active_sessions",
            "Current active WebSocket tunnel sessions",
        )
        .unwrap();

        registry.register(Box::new(proxy_requests.clone())).unwrap();
        registry
            .register(Box::new(proxy_latency_seconds.clone()))
            .unwrap();
        registry
            .register(Box::new(active_sessions.clone()))
            .unwrap();

        Self {
            registry,
            proxy_requests,
            proxy_latency_seconds,
            active_sessions,
        }
    }
}

pub struct AppState {
    // For Fixed mode this holds the configured token; empty string in Ephemeral mode
    pub _token: String,
    pub req_id: AtomicU64,
    // Auth
    pub auth_mode: AuthMode,
    pub jwt_secret: Vec<u8>,
    // token -> (ip, expiry, session) - used for revocation/single-use tracking in JWT mode
    pub issued_tokens: Mutex<HashMap<String, (String, Instant, String)>>,
    // session -> state
    pub sessions: RwLock<HashMap<String, Arc<SessionState>>>,
    // rate limit map: ip -> (count, window_start)
    pub rl: Mutex<HashMap<String, (u32, Instant)>>,
    // proxy rate limit map: ip -> (count, window_start)
    pub proxy_rl: Mutex<HashMap<String, (u32, Instant)>>,
    // config: allow token in query string for WS
    pub allow_token_query: bool,
    /// (Optional) Internal key to restrict /token to frontend only
    pub internal_key: Option<String>,
    pub metrics: Metrics,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ServerToClient {
    ProxyRequest(ProxyRequest),
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ClientToServer {
    ProxyResponse(ProxyResponse),
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ProxyRequest {
    pub id: u64,
    pub method: String,
    pub uri: String,
    pub headers: Vec<(String, String)>,
    pub body_b64: String,
    #[serde(default)]
    pub is_compressed: bool,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ProxyResponse {
    pub id: u64,
    pub status: u16,
    pub headers: Vec<(String, String)>,
    pub body_b64: String,
    #[serde(default)]
    pub is_compressed: bool,
}

pub fn compress_body(data: &[u8]) -> (String, bool) {
    if data.len() < 1024 {
        return (general_purpose::STANDARD.encode(data), false);
    }
    let mut encoder = ZlibEncoder::new(data, Compression::default());
    let mut compressed = Vec::new();
    if encoder.read_to_end(&mut compressed).is_ok() && compressed.len() < data.len() {
        (general_purpose::STANDARD.encode(compressed), true)
    } else {
        (general_purpose::STANDARD.encode(data), false)
    }
}

pub fn decompress_body(b64_data: &str, is_compressed: bool) -> Vec<u8> {
    let raw = general_purpose::STANDARD
        .decode(b64_data)
        .unwrap_or_default();
    if !is_compressed {
        return raw;
    }
    let mut decoder = ZlibDecoder::new(&raw[..]);
    let mut decompressed = Vec::new();
    if decoder.read_to_end(&mut decompressed).is_ok() {
        decompressed
    } else {
        raw
    }
}

pub async fn metrics_handler(State(state): State<Arc<AppState>>) -> impl IntoResponse {
    let encoder = TextEncoder::new();
    let metric_families = state.metrics.registry.gather();
    let mut buffer = vec![];
    encoder.encode(&metric_families, &mut buffer).unwrap();
    Response::builder()
        .status(StatusCode::OK)
        .header("Content-Type", encoder.format_type())
        .body(axum::body::Body::from(buffer))
        .unwrap()
}

// Initialize SQLite database for request logging (optional persistent storage)
pub fn init_db(db_path: &str) -> Result<Connection, rusqlite::Error> {
    let conn = Connection::open(db_path)?;

    // Create request_logs table with circular buffer pattern
    conn.execute(
        "CREATE TABLE IF NOT EXISTS request_logs (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            session_id TEXT NOT NULL,
            timestamp INTEGER NOT NULL,
            method TEXT NOT NULL,
            uri TEXT NOT NULL,
            status INTEGER NOT NULL,
            latency_ms INTEGER NOT NULL
        )",
        [],
    )?;

    // Create index for efficient querying
    conn.execute(
        "CREATE INDEX IF NOT EXISTS idx_session_timestamp ON request_logs(session_id, timestamp DESC)",
        [],
    )?;

    Ok(conn)
}

// Log request to database (with circular buffer cleanup to keep < 10k rows)
pub fn log_request_to_db(
    conn: &Connection,
    session_id: &str,
    method: &str,
    uri: &str,
    status: u16,
    latency_ms: u128,
) -> Result<(), rusqlite::Error> {
    let now = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap()
        .as_secs();

    conn.execute(
        "INSERT INTO request_logs (session_id, timestamp, method, uri, status, latency_ms) 
         VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
        params![session_id, now, method, uri, status, latency_ms as i64],
    )?;

    // Clean up old entries if > 10k rows
    let count: i64 = conn.query_row("SELECT COUNT(*) FROM request_logs", [], |row| row.get(0))?;

    if count > 10000 {
        conn.execute(
            "DELETE FROM request_logs WHERE id IN (
                SELECT id FROM request_logs ORDER BY id ASC LIMIT ?1
            )",
            params![count - 5000], // Keep 5000 most recent
        )?;
    }

    Ok(())
}

pub fn create_app(state: Arc<AppState>) -> Router {
    Router::new()
        .route("/metrics", get(metrics_handler))
        .route("/ws", get(ws_handler))
        .route("/token", get(token_endpoint))
        .route("/healthz", get(health))
        .route("/api/tunnels", get(api_tunnels))
        .route("/api/stats", get(api_stats))
        .route("/api/requests", get(api_requests))
        .route("/_next/{*path}", any(next_asset_redirect))
        .route("/s/{sid}/_log", get(session_log))
        .route("/s/{sid}/", any(proxy_handler_root))
        .route("/s/{sid}", any(proxy_handler_root))
        .route("/s/{sid}/{*path}", any(proxy_handler_path))
        .fallback(fallback_404)
        .layer(NormalizePathLayer::trim_trailing_slash())
        .layer(TraceLayer::new_for_http())
        .with_state(state)
}

pub fn extract_real_ip(addr: &SocketAddr, headers: &HeaderMap) -> String {
    headers
        .get("x-forwarded-for")
        .and_then(|v| v.to_str().ok())
        .and_then(|s| s.split(',').next())
        .map(|s| s.trim().to_string())
        .unwrap_or_else(|| addr.ip().to_string())
}

pub async fn ws_handler(
    ConnectInfo(addr): ConnectInfo<SocketAddr>,
    State(state): State<Arc<AppState>>,
    headers: HeaderMap,
    ws: WebSocketUpgrade,
    Query(params): Query<HashMap<String, String>>,
) -> Response {
    let sid = match params.get("sid") {
        Some(s) if !s.is_empty() => s.clone(),
        _ => return (StatusCode::BAD_REQUEST, "missing sid").into_response(),
    };

    // Extract token, prefer Authorization header; only allow query token if explicitly enabled
    let auth_header = headers
        .get(axum::http::header::AUTHORIZATION)
        .and_then(|v| v.to_str().ok())
        .unwrap_or("");
    let bearer = auth_header.strip_prefix("Bearer ");
    let token_str = if let Some(tok) = bearer {
        Some(tok.to_string())
    } else if state.allow_token_query {
        params.get("token").cloned()
    } else {
        None
    };
    let Some(token) = token_str else {
        let msg = if state.allow_token_query {
            "missing token"
        } else {
            "missing token (use Authorization: Bearer <token>)"
        };
        return (StatusCode::UNAUTHORIZED, msg).into_response();
    };

    let token_ok = match &state.auth_mode {
        AuthMode::Fixed(expected) => fixed_token_matches(&token, expected),
        AuthMode::Ephemeral => {
            let ip = extract_real_ip(&addr, &headers);
            match decode::<Claims>(
                &token,
                &DecodingKey::from_secret(&state.jwt_secret),
                &Validation::default(),
            ) {
                Ok(data) => {
                    let claims = data.claims;
                    if claims.ip != ip || claims.sub != sid {
                        false
                    } else {
                        let mut issued = state.issued_tokens.lock().await;
                        // Single-use check via JTI
                        if let Some((_, _, _)) = issued.remove(&claims.jti) {
                            true
                        } else {
                            tracing::warn!("Token JTI {} not found or already used", claims.jti);
                            false
                        }
                    }
                }
                Err(e) => {
                    tracing::error!("JWT validation failed: {}", e);
                    false
                }
            }
        }
    };

    if !token_ok {
        return (StatusCode::UNAUTHORIZED, "invalid token").into_response();
    }

    ws.on_upgrade(move |socket| client_ws(socket, state, sid))
}

pub async fn token_endpoint(
    ConnectInfo(addr): ConnectInfo<SocketAddr>,
    State(state): State<Arc<AppState>>,
    headers: HeaderMap,
) -> Response {
    // Only available in Ephemeral mode
    match &state.auth_mode {
        AuthMode::Fixed(_) => {
            return (StatusCode::FORBIDDEN, "token issuance disabled").into_response();
        }
        AuthMode::Ephemeral => {}
    }

    // (Optional) Internal Key check to restrict access (e.g., to frontend only)
    if let Some(ref required_key) = state.internal_key {
        let provided_key = headers
            .get("x-internal-key")
            .and_then(|v| v.to_str().ok())
            .unwrap_or("");

        if provided_key != required_key {
            tracing::warn!(
                "Unauthorized /token access attempt from {}",
                extract_real_ip(&addr, &headers)
            );
            return (StatusCode::UNAUTHORIZED, "unauthorized access").into_response();
        }
    }

    // Rate limiting per IP
    let ip = extract_real_ip(&addr, &headers);
    // Removed sensitive header logging for security
    let now = Instant::now();
    {
        let mut rl = state.rl.lock().await;
        use std::collections::hash_map::Entry;
        match rl.entry(ip.clone()) {
            Entry::Occupied(mut e) => {
                let (ref mut count, ref mut start) = *e.get_mut();
                let elapsed = now.duration_since(*start).as_secs();
                if elapsed >= RL_WINDOW_SECS {
                    *count = 1;
                    *start = now;
                } else if *count >= RL_MAX_PER_WINDOW {
                    let retry_after = RL_WINDOW_SECS - elapsed;
                    return axum::http::Response::builder()
                        .status(StatusCode::TOO_MANY_REQUESTS)
                        .header(
                            axum::http::header::CONTENT_TYPE,
                            "text/plain; charset=utf-8",
                        )
                        .header("retry-after", retry_after.to_string())
                        .header("cache-control", "no-store")
                        .header("x-robots-tag", "noindex, nofollow")
                        .header("referrer-policy", "no-referrer")
                        .body(axum::body::Body::from("rate limit exceeded for /token"))
                        .unwrap();
                } else {
                    *count += 1;
                }
            }
            Entry::Vacant(v) => {
                v.insert((1, now));
            }
        }
    }

    // Generate random token (jti) & session, tie to requesting IP, TTL 5 minutes
    let mut jti_bytes = [0u8; 16];
    rand::rng().fill_bytes(&mut jti_bytes);
    let jti = general_purpose::URL_SAFE_NO_PAD.encode(jti_bytes);

    let mut sid_bytes = [0u8; 16];
    rand::rng().fill_bytes(&mut sid_bytes);
    let sid = general_purpose::URL_SAFE_NO_PAD.encode(sid_bytes);

    let exp = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap()
        .as_secs()
        + 300;

    let claims = Claims {
        sub: sid.clone(),
        ip: ip.clone(),
        exp: exp as usize,
        jti: jti.clone(),
    };

    let token = match encode(
        &Header::default(),
        &claims,
        &EncodingKey::from_secret(&state.jwt_secret),
    ) {
        Ok(t) => t,
        Err(e) => {
            tracing::error!("Failed to encode JWT: {}", e);
            return (StatusCode::INTERNAL_SERVER_ERROR, "token generation failed").into_response();
        }
    };

    let expiry = Instant::now() + Duration::from_secs(300);

    {
        let mut issued = state.issued_tokens.lock().await;
        issued.insert(jti, (ip, expiry, sid.clone()));
    }

    // Return JSON with security headers
    let resp = TokenResponse {
        token,
        session: sid,
        expires_in: 300,
    };

    (
        StatusCode::OK,
        [
            (axum::http::header::CONTENT_TYPE, "application/json"),
            (axum::http::header::CACHE_CONTROL, "no-store"),
            (
                axum::http::header::HeaderName::from_static("x-robots-tag"),
                "noindex, nofollow",
            ),
            (axum::http::header::REFERRER_POLICY, "same-origin"),
        ],
        axum::Json(resp),
    )
        .into_response()
}

pub async fn client_ws(stream: WebSocket, state: Arc<AppState>, sid: String) {
    state.metrics.active_sessions.inc();
    tracing::info!("Client connected via WebSocket for session {}", sid);

    let (mut ws_tx, mut ws_rx) = stream.split();

    // Channel for outbound messages (server -> client)
    let (out_tx, mut out_rx) = mpsc::channel::<ServerToClient>(64);

    // Create session state and store
    let session_state = Arc::new(SessionState {
        outbound_tx: out_tx.clone(),
        pending: Mutex::new(HashMap::new()),
        _created_at: Instant::now(),
        last_seen: Mutex::new(Instant::now()),
        access_log: Mutex::new(Vec::new()),
        request_count: AtomicU64::new(0),
        bytes_in: AtomicU64::new(0),
        bytes_out: AtomicU64::new(0),
    });
    {
        let mut sessions = state.sessions.write().await;
        sessions.insert(sid.clone(), session_state.clone());
    }

    // Task: forward outbound messages to websocket
    let write_session = session_state.clone();
    let write_task = tokio::spawn(async move {
        while let Some(msg) = out_rx.recv().await {
            let text = serde_json::to_string(&msg).unwrap();
            if ws_tx.send(Message::Text(text.into())).await.is_err() {
                break;
            }
            // update last_seen on outbound activity
            {
                let mut ls = write_session.last_seen.lock().await;
                *ls = Instant::now();
            }
        }
    });

    // Task: read inbound messages from websocket (responses from client)
    let read_state = state.clone();
    let read_sid = sid.clone();
    let read_task = tokio::spawn(async move {
        while let Some(Ok(msg)) = ws_rx.next().await {
            // update last_seen on any inbound WS message
            if let Some(sess) = read_state.sessions.read().await.get(&read_sid).cloned() {
                let mut ls = sess.last_seen.lock().await;
                *ls = Instant::now();
            }
            if let Message::Text(txt) = msg {
                match serde_json::from_str::<ClientToServer>(&txt) {
                    Ok(ClientToServer::ProxyResponse(resp)) => {
                        let maybe_session =
                            { read_state.sessions.read().await.get(&read_sid).cloned() };
                        if let Some(sess) = maybe_session {
                            let mut pending = sess.pending.lock().await;
                            if let Some(tx) = pending.remove(&resp.id) {
                                let _ = tx.send(ClientToServer::ProxyResponse(resp));
                            }
                        }
                    }
                    Err(e) => {
                        tracing::error!("Failed to parse client message: {}", e);
                    }
                }
            }
        }
    });

    // Wait for either side to finish (disconnect)
    let _ = tokio::join!(write_task, read_task);

    // Remove session on disconnect
    {
        let mut sessions = state.sessions.write().await;
        sessions.remove(&sid);
    }
    state.metrics.active_sessions.dec();

    tracing::info!("Client disconnected for session {}", sid);
}

pub async fn health() -> &'static str {
    "ok"
}

pub async fn session_log(Path(sid): Path<String>, State(state): State<Arc<AppState>>) -> Response {
    let maybe = { state.sessions.read().await.get(&sid).cloned() };
    let Some(sess) = maybe else {
        return (StatusCode::NOT_FOUND, "session not found").into_response();
    };

    let log = sess.access_log.lock().await.clone();
    let mut html = String::from("<!doctype html><meta charset=\"utf-8\"><title>Tunly Session Log</title><style>body{font-family:system-ui,-apple-system,Segoe UI,Roboto,Ubuntu,\"Helvetica Neue\",Arial,sans-serif;padding:20px}table{border-collapse:collapse;width:100%}th,td{border:1px solid #ddd;padding:8px}th{background:#f7f7f7;text-align:left}code{background:#f3f3f3;padding:2px 4px;border-radius:3px}</style>");
    html.push_str(&format!("<h1>Session <code>{}</code></h1>", sid));
    html.push_str(&format!("<p>Quick links: <a href=\"/s/{}/\">/</a> · <a href=\"/s/{}/api\">/api</a> · <a href=\"/s/{}/blog\">/blog</a></p>", sid, sid, sid));
    html.push_str("<table><thead><tr><th>Method</th><th>URI</th><th>Status</th><th>Duration</th></tr></thead><tbody>");
    for e in log.iter().rev() {
        // newest first
        html.push_str(&format!(
            "<tr><td>{}</td><td><code>{}</code></td><td>{}</td><td>{} ms</td></tr>",
            escape_html(&e.method),
            escape_html(&e.uri),
            e.status,
            e.dur_ms
        ));
    }
    html.push_str("</tbody></table>");

    axum::http::Response::builder()
        .status(StatusCode::OK)
        .header(axum::http::header::CONTENT_TYPE, "text/html; charset=utf-8")
        .header("cache-control", "no-store")
        .header("x-robots-tag", "noindex, nofollow")
        .header("referrer-policy", "same-origin")
        .body(axum::body::Body::from(html))
        .unwrap()
}

pub fn escape_html(s: &str) -> String {
    s.replace('&', "&amp;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
}

pub async fn proxy_handler_root(
    ConnectInfo(addr): ConnectInfo<SocketAddr>,
    headers: HeaderMap,
    Path(sid): Path<String>,
    State(state): State<Arc<AppState>>,
    req: Request<axum::body::Body>,
) -> Response {
    proxy_logic(State(state), addr, headers, sid, "".to_string(), req).await
}

pub async fn proxy_handler_path(
    ConnectInfo(addr): ConnectInfo<SocketAddr>,
    headers: HeaderMap,
    Path((sid, path)): Path<(String, String)>,
    State(state): State<Arc<AppState>>,
    req: Request<axum::body::Body>,
) -> Response {
    proxy_logic(State(state), addr, headers, sid, path, req).await
}

pub async fn proxy_logic(
    State(state): State<Arc<AppState>>,
    addr: SocketAddr,
    headers: HeaderMap,
    sid: String,
    path: String,
    req: Request<axum::body::Body>,
) -> Response {
    state.metrics.proxy_requests.inc();
    let _timer = state.metrics.proxy_latency_seconds.start_timer();
    tracing::info!("-> PROXY_HANDLER: sid='{}', path='{}'", sid, path);
    let start = Instant::now();

    // Rate limiting per IP
    let ip = extract_real_ip(&addr, &headers);
    let now = Instant::now();
    {
        let mut rl = state.proxy_rl.lock().await;
        use std::collections::hash_map::Entry;
        match rl.entry(ip.clone()) {
            Entry::Occupied(mut e) => {
                let (ref mut count, ref mut start_time) = *e.get_mut();
                let elapsed = now.duration_since(*start_time).as_secs();
                if elapsed >= PROXY_RL_WINDOW_SECS {
                    *count = 1;
                    *start_time = now;
                } else if *count >= PROXY_RL_MAX_PER_WINDOW {
                    let retry_after = PROXY_RL_WINDOW_SECS - elapsed;
                    return axum::http::Response::builder()
                        .status(StatusCode::TOO_MANY_REQUESTS)
                        .header("retry-after", retry_after.to_string())
                        .body(axum::body::Body::from(
                            "rate limit exceeded for proxy requests",
                        ))
                        .unwrap()
                        .into_response();
                } else {
                    *count += 1;
                }
            }
            Entry::Vacant(v) => {
                v.insert((1, now));
            }
        }
    }

    // Prepare request snapshot pieces up front
    let method = req.method().to_string();
    let uri: Uri = req.uri().clone();
    // Build URI for client: "/" + tail + optional ?query
    let tail = path.trim_start_matches('/');
    let mut uri_str = if tail.is_empty() {
        "/".to_string()
    } else {
        format!("/{}", tail)
    };
    if let Some(query) = uri.query() {
        uri_str.push('?');
        uri_str.push_str(query);
    }

    // Lookup session
    let maybe_sess = { state.sessions.read().await.get(&sid).cloned() };
    let Some(sess) = maybe_sess else {
        return (
            StatusCode::SERVICE_UNAVAILABLE,
            "no tunnel client for session",
        )
            .into_response();
    };

    // mark activity
    {
        let mut ls = sess.last_seen.lock().await;
        *ls = Instant::now();
    }

    // Build request snapshot
    let id = state.req_id.fetch_add(1, Ordering::SeqCst);

    let headers_vec = headers_to_vec(req.headers());

    // Limit proxy request body to 2 MB to prevent memory exhaustion
    let body_bytes = match axum::body::to_bytes(req.into_body(), 2 * 1024 * 1024).await {
        Ok(bytes) => bytes,
        Err(_) => {
            return (
                StatusCode::PAYLOAD_TOO_LARGE,
                "Request body too large (2MB limit)",
            )
                .into_response();
        }
    };

    // Track request body size
    let req_body_size = body_bytes.len() as u64;
    sess.bytes_in.fetch_add(req_body_size, Ordering::SeqCst);

    let (body_b64, is_compressed) = compress_body(&body_bytes);

    let proxy_req = ProxyRequest {
        id,
        method: method.clone(),
        uri: uri_str.clone(),
        headers: headers_vec,
        body_b64,
        is_compressed,
    };

    // Prepare oneshot for the response
    let (resp_tx, resp_rx) = oneshot::channel::<ClientToServer>();
    {
        let mut pending = sess.pending.lock().await;
        pending.insert(id, resp_tx);
    }

    // Send to client
    if sess
        .outbound_tx
        .send(ServerToClient::ProxyRequest(proxy_req))
        .await
        .is_err()
    {
        let mut pending = sess.pending.lock().await;
        pending.remove(&id);
        // log failure
        let dur_ms = start.elapsed().as_millis();
        {
            let mut log = sess.access_log.lock().await;
            log.push(AccessLogEntry {
                method: method.clone(),
                uri: uri_str.clone(),
                status: StatusCode::BAD_GATEWAY.as_u16(),
                dur_ms,
                bytes_in: req_body_size,
                bytes_out: 0,
            });
            if log.len() > 50 {
                let drop_n = log.len() - 50;
                log.drain(0..drop_n);
            }
        }
        tracing::info!(
            "PROXY {} {} -> {} in {}ms (sid={})",
            method,
            uri_str,
            StatusCode::BAD_GATEWAY.as_u16(),
            dur_ms,
            sid
        );
        return (StatusCode::BAD_GATEWAY, "failed to send to tunnel client").into_response();
    }

    // Await response with timeout
    let resp = match tokio::time::timeout(std::time::Duration::from_secs(30), resp_rx).await {
        Ok(Ok(ClientToServer::ProxyResponse(r))) => r,
        Ok(Err(_)) => {
            let dur_ms = start.elapsed().as_millis();
            {
                let mut log = sess.access_log.lock().await;
                log.push(AccessLogEntry {
                    method: method.clone(),
                    uri: uri_str.clone(),
                    status: StatusCode::BAD_GATEWAY.as_u16(),
                    dur_ms,
                    bytes_in: req_body_size,
                    bytes_out: 0,
                });
                if log.len() > 50 {
                    let drop_n = log.len() - 50;
                    log.drain(0..drop_n);
                }
            }
            tracing::info!(
                "PROXY {} {} -> {} in {}ms (sid={})",
                method,
                uri_str,
                StatusCode::BAD_GATEWAY.as_u16(),
                dur_ms,
                sid
            );
            return (StatusCode::BAD_GATEWAY, "tunnel closed").into_response();
        }
        Err(_) => {
            // Timeout
            let mut pending = sess.pending.lock().await;
            pending.remove(&id);
            let dur_ms = start.elapsed().as_millis();
            {
                let mut log = sess.access_log.lock().await;
                log.push(AccessLogEntry {
                    method: method.clone(),
                    uri: uri_str.clone(),
                    status: StatusCode::GATEWAY_TIMEOUT.as_u16(),
                    dur_ms,
                    bytes_in: req_body_size,
                    bytes_out: 0,
                });
                if log.len() > 50 {
                    let drop_n = log.len() - 50;
                    log.drain(0..drop_n);
                }
            }
            tracing::info!(
                "PROXY {} {} -> {} in {}ms (sid={})",
                method,
                uri_str,
                StatusCode::GATEWAY_TIMEOUT.as_u16(),
                dur_ms,
                sid
            );
            return (StatusCode::GATEWAY_TIMEOUT, "upstream timeout").into_response();
        }
    };

    // Build response to external client
    let mut builder = axum::http::Response::builder().status(resp.status);
    for (k, v) in resp.headers.iter() {
        // Skip hop-by-hop headers
        if is_hop_by_hop(k) {
            continue;
        }

        // Rewrite relative Location headers to stay under /s/:sid/
        if k.eq_ignore_ascii_case("location") {
            // Absolute-path Location: rewrite under /s/:sid/
            if v.starts_with('/') {
                let new_loc = if v.starts_with(&format!("/s/{}/", sid)) {
                    v.clone()
                } else {
                    format!("/s/{}/{}", sid, v.trim_start_matches('/'))
                };
                if let (Ok(name), Ok(value)) = (
                    axum::http::header::HeaderName::from_bytes(k.as_bytes()),
                    axum::http::HeaderValue::from_str(&new_loc),
                ) {
                    builder = builder.header(name, value);
                }
                continue;
            }
            // Absolute-URL Location (http/https): strip scheme+host and rewrite path+query under /s/:sid/
            let lower = v.to_ascii_lowercase();
            if lower.starts_with("http://") || lower.starts_with("https://") {
                if let Some(scheme_idx) = v.find("://") {
                    let after_scheme = scheme_idx + 3;
                    if let Some(path_rel_idx) = v[after_scheme..].find('/') {
                        let path_start = after_scheme + path_rel_idx; // index of '/'
                        let path_q = &v[path_start..]; // includes leading '/'
                        let new_loc = if path_q.starts_with(&format!("/s/{}/", sid)) {
                            path_q.to_string()
                        } else {
                            format!("/s/{}/{}", sid, path_q.trim_start_matches('/'))
                        };
                        if let (Ok(name), Ok(value)) = (
                            axum::http::header::HeaderName::from_bytes(k.as_bytes()),
                            axum::http::HeaderValue::from_str(&new_loc),
                        ) {
                            builder = builder.header(name, value);
                        }
                        continue;
                    }
                }
            }
        }

        if let (Ok(name), Ok(value)) = (
            axum::http::header::HeaderName::from_bytes(k.as_bytes()),
            axum::http::HeaderValue::from_str(v),
        ) {
            builder = builder.header(name, value);
        }
    }
    // Security/cache headers to reduce leakage
    builder = builder
        .header("cache-control", "no-store")
        .header("x-robots-tag", "noindex, nofollow")
        .header("referrer-policy", "same-origin");
    // Persist session id to a cookie for asset routing (/_next/* -> /s/:sid/_next/*)
    if let Ok(cv) = axum::http::HeaderValue::from_str(&format!(
        "tunly_sid={}; Path=/; Max-Age=600; HttpOnly; SameSite=Lax",
        sid
    )) {
        builder = builder.header(axum::http::header::SET_COOKIE, cv);
    }

    let body = decompress_body(&resp.body_b64, resp.is_compressed);
    let resp_body_size = body.len() as u64;

    // Track response body size
    sess.bytes_out.fetch_add(resp_body_size, Ordering::SeqCst);

    let response = builder
        .body(axum::body::Body::from(body))
        .unwrap()
        .into_response();

    // lightweight logging
    let dur_ms = start.elapsed().as_millis();
    {
        // push to session ring buffer (keep last 50)
        let mut log = sess.access_log.lock().await;
        log.push(AccessLogEntry {
            method: method.clone(),
            uri: uri_str.clone(),
            status: response.status().as_u16(),
            dur_ms,
            bytes_in: req_body_size,
            bytes_out: resp_body_size,
        });
        if log.len() > 50 {
            let drop_n = log.len() - 50;
            log.drain(0..drop_n);
        }
    }
    // Increment request counter for this session
    sess.request_count.fetch_add(1, Ordering::SeqCst);

    tracing::info!(
        "PROXY {} {} -> {} in {}ms (sid={})",
        method,
        uri_str,
        response.status().as_u16(),
        dur_ms,
        sid
    );

    response
}

pub fn headers_to_vec(headers: &HeaderMap) -> Vec<(String, String)> {
    headers
        .iter()
        .filter_map(|(k, v)| {
            if is_hop_by_hop(k.as_str()) {
                return None;
            }
            Some((k.as_str().to_string(), v.to_str().unwrap_or("").to_string()))
        })
        .collect()
}

pub fn is_hop_by_hop(name: &str) -> bool {
    matches!(
        name.to_ascii_lowercase().as_str(),
        "connection"
            | "keep-alive"
            | "proxy-authenticate"
            | "proxy-authorization"
            | "te"
            | "trailers"
            | "transfer-encoding"
            | "upgrade"
    )
}

// Redirect root Next.js asset requests (/_next/*) to the prefixed session path (/s/:sid/_next/*)
// We infer the session id preferring Referer (so multiple sessions work), then cookie.
pub async fn next_asset_redirect(
    Path(path): Path<String>,
    headers: HeaderMap,
    uri: Uri,
) -> Response {
    let qs = uri
        .query()
        .map(|q| {
            let mut s = String::from("?");
            s.push_str(q);
            s
        })
        .unwrap_or_default();
    // 1) Prefer Referer: derive from path like https://host/s/<sid>/...
    if let Some(ref_val) = headers
        .get(axum::http::header::REFERER)
        .and_then(|v| v.to_str().ok())
    {
        if let Some(sid) = sid_from_referer(ref_val) {
            let new_loc = format!("/s/{}/_next/{}{}", sid, path, qs);
            let hv = axum::http::HeaderValue::from_str(&new_loc)
                .unwrap_or(axum::http::HeaderValue::from_static("/"));
            return axum::http::Response::builder()
                .status(StatusCode::TEMPORARY_REDIRECT)
                .header(axum::http::header::LOCATION, hv)
                .header("cache-control", "no-store")
                .body(axum::body::Body::empty())
                .unwrap()
                .into_response();
        }
    }
    // 2) Fallback to cookie
    if let Some(sid) = cookie_value(&headers, "tunly_sid") {
        let new_loc = format!("/s/{}/_next/{}{}", sid, path, qs);
        let hv = axum::http::HeaderValue::from_str(&new_loc)
            .unwrap_or(axum::http::HeaderValue::from_static("/"));
        return axum::http::Response::builder()
            .status(StatusCode::TEMPORARY_REDIRECT)
            .header(axum::http::header::LOCATION, hv)
            .header("cache-control", "no-store")
            .body(axum::body::Body::empty())
            .unwrap()
            .into_response();
    }
    (
        StatusCode::NOT_FOUND,
        "not found: /_next/* (no session context)",
    )
        .into_response()
}

pub fn sid_from_referer(referer: &str) -> Option<String> {
    // look for "/s/" then capture until next '/'
    if let Some(idx) = referer.find("/s/") {
        let after = &referer[idx + 3..];
        let end = after.find('/').unwrap_or(after.len());
        let sid = &after[..end];
        if !sid.is_empty() {
            return Some(sid.to_string());
        }
    }
    None
}

pub fn cookie_value(headers: &HeaderMap, name: &str) -> Option<String> {
    let raw = headers.get(axum::http::header::COOKIE)?.to_str().ok()?;
    for part in raw.split(';') {
        let kv = part.trim();
        if let Some(val) = kv.strip_prefix(&format!("{}=", name)) {
            return Some(val.to_string());
        }
    }
    None
}

// Fallback for unmatched routes: return 404 and include the requested URI for visibility
pub async fn fallback_404(uri: Uri) -> Response {
    let body = format!("not found: {}", uri);
    axum::http::Response::builder()
        .status(StatusCode::NOT_FOUND)
        .header("cache-control", "no-store")
        .body(axum::body::Body::from(body))
        .unwrap()
        .into_response()
}

// Dashboard API: Get list of active tunnels
pub async fn api_tunnels(
    State(state): State<Arc<AppState>>,
) -> axum::response::Json<Vec<DashboardTunnelInfo>> {
    let sessions = state.sessions.read().await;
    let now = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap()
        .as_secs();

    let tunnels: Vec<_> = sessions
        .iter()
        .map(|(sid, sess)| {
            let last_seen = futures::executor::block_on(async {
                let ls = sess.last_seen.lock().await;
                ls.elapsed().as_secs()
            });
            let is_idle = last_seen > 30; // Idle if no activity for 30 seconds
            let request_count = sess.request_count.load(Ordering::SeqCst);
            let bytes_in = sess.bytes_in.load(Ordering::SeqCst);
            let bytes_out = sess.bytes_out.load(Ordering::SeqCst);

            DashboardTunnelInfo {
                session_id: sid.clone(),
                created_at: now - sess._created_at.elapsed().as_secs(),
                last_seen: now - last_seen,
                total_requests: request_count,
                bytes_in,
                bytes_out,
                status: if is_idle {
                    "idle".to_string()
                } else {
                    "active".to_string()
                },
            }
        })
        .collect();

    axum::response::Json(tunnels)
}

// Dashboard API: Get server statistics
pub async fn api_stats(State(state): State<Arc<AppState>>) -> axum::response::Json<DashboardStats> {
    let sessions = state.sessions.read().await;
    let total_requests: u64 = sessions
        .values()
        .map(|sess| sess.request_count.load(Ordering::SeqCst))
        .sum();

    axum::response::Json(DashboardStats {
        active_sessions: sessions.len(),
        total_requests,
        uptime_secs: 0, // TODO: Track server uptime
    })
}

// Dashboard API: Get recent requests
pub async fn api_requests(
    State(state): State<Arc<AppState>>,
) -> axum::response::Json<Vec<DashboardRequest>> {
    let sessions = state.sessions.read().await;
    let mut all_requests = Vec::new();

    let now = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap()
        .as_secs();

    for sess in sessions.values() {
        let log = futures::executor::block_on(async { sess.access_log.lock().await.clone() });

        for entry in log {
            all_requests.push(DashboardRequest {
                timestamp: now, // TODO: Add timestamp to AccessLogEntry
                method: entry.method,
                uri: entry.uri,
                status: entry.status,
                latency_ms: entry.dur_ms,
            });
        }
    }

    // Sort by timestamp descending (newest first)
    all_requests.sort_by_key(|a| std::cmp::Reverse(a.timestamp));

    axum::response::Json(all_requests)
}

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

    #[test]
    fn fixed_token_matches_only_equal_tokens() {
        assert!(fixed_token_matches("secret-token", "secret-token"));
        assert!(!fixed_token_matches("secret-tokeN", "secret-token"));
        assert!(!fixed_token_matches("secret", "secret-token"));
    }
}