Skip to content

Commit 3fcb7d8

Browse files
authored
fix(proxy): preserve SOCKS5 remote DNS semantics (#1369)
Preserve SOCKS5 domain-name resolution semantics through raw passthrough paths and fail closed instead of leaking DNS to the local resolver.
1 parent 640bc06 commit 3fcb7d8

3 files changed

Lines changed: 104 additions & 9 deletions

File tree

‎docs/guide.fa.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ upstream_socks5 = "127.0.0.1:50529"
206206

207207
HTTP / HTTPS مثل قبل از Apps Script می‌رود (تغییری نمی‌کند)، تونل بازنویسی SNI برای `google.com` / `youtube.com` همچنان از هر دو دور می‌زند — یوتیوب به سرعت قبل می‌ماند و تلگرام هم تونل واقعی پیدا می‌کند.
208208

209+
هدف‌های دامنه‌ای SOCKS5 معنای remote-DNS را حفظ می‌کنند: اگر پروکسی برای TCP خام مجبور شود hostname را محلی resolve کند و نه Full Tunnel و نه `upstream_socks5` در دسترس باشد، به‌جای نشت DNS plaintext، اتصال را fail-closed می‌کند.
210+
209211
## حالت تونل کامل
210212

211213
`"mode": "full"` **تمام** ترافیک را end-to-end از Apps Script و یک [tunnel-node](../tunnel-node/) راه دور رد می‌کند — بدون نیاز به نصب گواهی MITM. TCP به‌صورت سشن‌های پایدار تونل، و UDP از کلاینت‌های اندروید / TUN از طریق SOCKS5 `UDP ASSOCIATE` به tunnel-node که UDP واقعی را از سمت سرور منتشر می‌کند. مبادله: تأخیر بیشتر هر درخواست (هر بایت Apps Script → tunnel-node → مقصد می‌رود)، اما برای هر پروتکل و هر برنامه‌ای بدون نصب CA کار می‌کند.

‎docs/guide.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ upstream_socks5 = "127.0.0.1:50529"
206206

207207
HTTP / HTTPS keeps going through Apps Script (no change), and the SNI-rewrite tunnel for `google.com` / `youtube.com` keeps bypassing both — YouTube stays as fast as before while Telegram gets a real tunnel.
208208

209+
SOCKS5 domain targets preserve remote-DNS semantics: if the proxy would have to resolve a hostname locally for raw TCP and neither Full Tunnel nor `upstream_socks5` is available, it fails closed instead of leaking a plaintext DNS lookup.
210+
209211
## Full Tunnel mode
210212

211213
`"mode": "full"` routes **all** traffic end-to-end through Apps Script and a remote [tunnel-node](../tunnel-node/) — no MITM certificate needed. TCP carried as persistent tunnel sessions, UDP from Android / TUN clients via SOCKS5 `UDP ASSOCIATE` to the tunnel-node which emits real UDP server-side. Trade-off: higher per-request latency (every byte goes Apps Script → tunnel-node → destination), but works for any protocol and any app, no CA install required.

‎src/proxy_server.rs‎

Lines changed: 100 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -889,7 +889,17 @@ async fn handle_http_client(
889889
sock.write_all(b"HTTP/1.1 200 Connection Established\r\n\r\n")
890890
.await?;
891891
sock.flush().await?;
892-
dispatch_tunnel(sock, host, port, fronter, mitm, rewrite_ctx, tunnel_mux).await
892+
dispatch_tunnel(
893+
sock,
894+
host,
895+
port,
896+
fronter,
897+
mitm,
898+
rewrite_ctx,
899+
tunnel_mux,
900+
false,
901+
)
902+
.await
893903
} else {
894904
// Plain HTTP proxy request (e.g. `GET http://…`).
895905
//
@@ -1017,7 +1027,18 @@ async fn handle_socks5_client(
10171027
.await?;
10181028
sock.flush().await?;
10191029

1020-
dispatch_tunnel(sock, host, port, fronter, mitm, rewrite_ctx, tunnel_mux).await
1030+
let require_remote_dns = atyp == 0x03;
1031+
dispatch_tunnel(
1032+
sock,
1033+
host,
1034+
port,
1035+
fronter,
1036+
mitm,
1037+
rewrite_ctx,
1038+
tunnel_mux,
1039+
require_remote_dns,
1040+
)
1041+
.await
10211042
}
10221043

10231044
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
@@ -1683,6 +1704,7 @@ async fn dispatch_tunnel(
16831704
mitm: Arc<Mutex<MitmCertManager>>,
16841705
rewrite_ctx: Arc<RewriteCtx>,
16851706
tunnel_mux: Option<Arc<TunnelMux>>,
1707+
require_remote_dns: bool,
16861708
) -> std::io::Result<()> {
16871709
// 0. User-configured passthrough list wins over every other path.
16881710
// If the host matches `passthrough_hosts`, we raw-TCP it (through
@@ -1698,7 +1720,7 @@ async fn dispatch_tunnel(
16981720
port,
16991721
via.unwrap_or("direct")
17001722
);
1701-
plain_tcp_passthrough(sock, &host, port, via).await;
1723+
plain_tcp_passthrough(sock, &host, port, via, require_remote_dns).await;
17021724
return Ok(());
17031725
}
17041726

@@ -1732,7 +1754,7 @@ async fn dispatch_tunnel(
17321754
port,
17331755
via.unwrap_or("direct")
17341756
);
1735-
plain_tcp_passthrough(sock, &host, port, via).await;
1757+
plain_tcp_passthrough(sock, &host, port, via, require_remote_dns).await;
17361758
return Ok(());
17371759
}
17381760

@@ -1818,7 +1840,7 @@ async fn dispatch_tunnel(
18181840
port,
18191841
via.unwrap_or("direct")
18201842
);
1821-
plain_tcp_passthrough(sock, &host, port, via).await;
1843+
plain_tcp_passthrough(sock, &host, port, via, require_remote_dns).await;
18221844
return Ok(());
18231845
}
18241846

@@ -1833,7 +1855,14 @@ async fn dispatch_tunnel(
18331855
host,
18341856
port
18351857
);
1836-
plain_tcp_passthrough(sock, &host, port, rewrite_ctx.upstream_socks5.as_deref()).await;
1858+
plain_tcp_passthrough(
1859+
sock,
1860+
&host,
1861+
port,
1862+
rewrite_ctx.upstream_socks5.as_deref(),
1863+
require_remote_dns,
1864+
)
1865+
.await;
18371866
return Ok(());
18381867
}
18391868
};
@@ -1862,7 +1891,7 @@ async fn dispatch_tunnel(
18621891
port,
18631892
via.unwrap_or("direct")
18641893
);
1865-
plain_tcp_passthrough(sock, &host, port, via).await;
1894+
plain_tcp_passthrough(sock, &host, port, via, require_remote_dns).await;
18661895
return Ok(());
18671896
}
18681897
};
@@ -1903,7 +1932,7 @@ async fn dispatch_tunnel(
19031932
port,
19041933
via.unwrap_or("direct")
19051934
);
1906-
plain_tcp_passthrough(sock, &host, port, via).await;
1935+
plain_tcp_passthrough(sock, &host, port, via, require_remote_dns).await;
19071936
Ok(())
19081937
}
19091938

@@ -1914,8 +1943,18 @@ async fn plain_tcp_passthrough(
19141943
host: &str,
19151944
port: u16,
19161945
upstream_socks5: Option<&str>,
1946+
require_remote_dns: bool,
19171947
) {
19181948
let target_host = host.trim_start_matches('[').trim_end_matches(']');
1949+
let target_is_ip = looks_like_ip(target_host);
1950+
if should_refuse_local_dns_fallback(require_remote_dns, target_host, upstream_socks5) {
1951+
tracing::warn!(
1952+
"refusing raw-tcp direct fallback for SOCKS5 domain target {}:{} to avoid local DNS resolution",
1953+
host,
1954+
port
1955+
);
1956+
return;
1957+
}
19191958
// Shorter connect timeout for IP literals (4s vs 10s for hostnames).
19201959
// Ported from upstream Python 7b1812c: when the target is an IP (i.e.
19211960
// a raw Telegram DC, or an IP someone hardcoded), and that route is
@@ -1927,7 +1966,7 @@ async fn plain_tcp_passthrough(
19271966
// Hostnames still get 10s because DNS + first-hop TCP genuinely can
19281967
// take that long on flaky links, and the resolver fallbacks already
19291968
// trim the worst case.
1930-
let connect_timeout = if looks_like_ip(target_host) {
1969+
let connect_timeout = if target_is_ip {
19311970
std::time::Duration::from_secs(4)
19321971
} else {
19331972
std::time::Duration::from_secs(10)
@@ -1939,6 +1978,16 @@ async fn plain_tcp_passthrough(
19391978
s
19401979
}
19411980
Err(e) => {
1981+
if require_remote_dns && !target_is_ip {
1982+
tracing::warn!(
1983+
"upstream-socks5 {} -> {}:{} failed: {}; refusing local DNS fallback",
1984+
proxy,
1985+
host,
1986+
port,
1987+
e
1988+
);
1989+
return;
1990+
}
19421991
tracing::warn!(
19431992
"upstream-socks5 {} -> {}:{} failed: {} (falling back to direct)",
19441993
proxy,
@@ -1983,6 +2032,14 @@ async fn plain_tcp_passthrough(
19832032
let _ = tokio::io::copy_bidirectional_with_sizes(&mut sock, &mut upstream, 65536, 65536).await;
19842033
}
19852034

2035+
fn should_refuse_local_dns_fallback(
2036+
require_remote_dns: bool,
2037+
target_host: &str,
2038+
upstream_socks5: Option<&str>,
2039+
) -> bool {
2040+
require_remote_dns && upstream_socks5.is_none() && !looks_like_ip(target_host)
2041+
}
2042+
19862043
/// Open a TCP stream to `(host, port)` through an upstream SOCKS5 proxy
19872044
/// (no-auth only). Returns the connected stream after SOCKS5 negotiation.
19882045
async fn socks5_connect_via(proxy: &str, host: &str, port: u16) -> std::io::Result<TcpStream> {
@@ -3279,6 +3336,40 @@ mod tests {
32793336
assert_eq!(build_socks5_udp_packet(&target, payload), raw);
32803337
}
32813338

3339+
#[test]
3340+
fn socks5_remote_dns_refuses_direct_hostname_fallback() {
3341+
assert!(should_refuse_local_dns_fallback(
3342+
true,
3343+
"example.com",
3344+
None
3345+
));
3346+
assert!(should_refuse_local_dns_fallback(
3347+
true,
3348+
"sub.example.com",
3349+
None
3350+
));
3351+
}
3352+
3353+
#[test]
3354+
fn socks5_remote_dns_allows_paths_that_do_not_need_local_dns() {
3355+
assert!(!should_refuse_local_dns_fallback(true, "1.2.3.4", None));
3356+
assert!(!should_refuse_local_dns_fallback(
3357+
true,
3358+
"2001:db8::1",
3359+
None
3360+
));
3361+
assert!(!should_refuse_local_dns_fallback(
3362+
true,
3363+
"example.com",
3364+
Some("127.0.0.1:9050")
3365+
));
3366+
assert!(!should_refuse_local_dns_fallback(
3367+
false,
3368+
"example.com",
3369+
None
3370+
));
3371+
}
3372+
32823373
#[tokio::test(flavor = "current_thread")]
32833374
async fn read_body_decodes_chunked_request() {
32843375
let (mut client, mut server) = duplex(1024);

0 commit comments

Comments
 (0)