Skip to content

Commit cd3e486

Browse files
freeinternet865freeinternet865
andauthored
proxy_server: restrict SNI-rewrite dispatch to HTTPS CONNECT/SOCKS targets (#50)
dispatch_tunnel() is only used by the HTTP CONNECT and SOCKS5 listeners. It previously forced hosts matched by matches_sni_rewrite() or the hosts override map into do_sni_rewrite_tunnel_from_tcp() regardless of port. That tunnel is TLS-specific: it accepts inbound TLS from the client and opens a second TLS connection to the Google edge. For non-HTTPS targets such as :80, selecting that path makes the proxy wait for a ClientHello that will never arrive. Introduce should_use_sni_rewrite() and require port 443 before forcing the rewrite tunnel from dispatch_tunnel(). Non-HTTPS targets now remain on the normal dispatch path. The tests now cover both suffix-based and hosts-map matches on ports 443 and 80. Co-authored-by: freeinternet865 <free@internet865.com>
1 parent 72fc823 commit cd3e486

1 file changed

Lines changed: 26 additions & 2 deletions

File tree

‎src/proxy_server.rs‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,19 @@ async fn handle_socks5_client(
427427

428428
// ---------- Smart dispatch (used by both HTTP CONNECT and SOCKS5) ----------
429429

430+
fn should_use_sni_rewrite(
431+
hosts: &std::collections::HashMap<String, String>,
432+
host: &str,
433+
port: u16,
434+
) -> bool {
435+
// The SNI-rewrite path expects TLS from the client: it accepts inbound
436+
// TLS, then opens a second TLS connection to the Google edge with a front
437+
// SNI. Auto-forcing that path for non-TLS ports (for example a SOCKS5
438+
// CONNECT to google.com:80) makes the proxy wait for a ClientHello that
439+
// will never arrive.
440+
port == 443 && (matches_sni_rewrite(host) || hosts_override(hosts, host).is_some())
441+
}
442+
430443
async fn dispatch_tunnel(
431444
sock: TcpStream,
432445
host: String,
@@ -435,8 +448,9 @@ async fn dispatch_tunnel(
435448
mitm: Arc<Mutex<MitmCertManager>>,
436449
rewrite_ctx: Arc<RewriteCtx>,
437450
) -> std::io::Result<()> {
438-
// 1. Explicit hosts override or SNI-rewrite suffix: always use the tunnel.
439-
if matches_sni_rewrite(&host) || hosts_override(&rewrite_ctx.hosts, &host).is_some() {
451+
// 1. Explicit hosts override or SNI-rewrite suffix: for HTTPS targets,
452+
// always use the TLS SNI-rewrite tunnel.
453+
if should_use_sni_rewrite(&rewrite_ctx.hosts, &host, port) {
440454
tracing::info!("dispatch {}:{} -> sni-rewrite tunnel (Google edge direct)", host, port);
441455
return do_sni_rewrite_tunnel_from_tcp(sock, &host, port, mitm, rewrite_ctx).await;
442456
}
@@ -1400,4 +1414,14 @@ mod tests {
14001414
assert_eq!(body, b"hello");
14011415
}
14021416

1417+
#[test]
1418+
fn sni_rewrite_is_only_for_port_443() {
1419+
let mut hosts = std::collections::HashMap::new();
1420+
hosts.insert("example.com".to_string(), "1.2.3.4".to_string());
1421+
1422+
assert!(should_use_sni_rewrite(&hosts, "google.com", 443));
1423+
assert!(!should_use_sni_rewrite(&hosts, "google.com", 80));
1424+
assert!(should_use_sni_rewrite(&hosts, "www.example.com", 443));
1425+
assert!(!should_use_sni_rewrite(&hosts, "www.example.com", 80));
1426+
}
14031427
}

0 commit comments

Comments
 (0)