Skip to content

Commit cbe06b5

Browse files
committed
port upstream connection-quality fixes (masterking32/MasterHttpRelayVPN)
Three ports from the upstream Python repo — all straightforward wins for user-facing connection reliability: - plain-tcp: 4s connect timeout for IP literals (10s for hostnames). Ported from upstream 7b1812c. When Telegram MTProto (or any protocol that CONNECTs to a raw IP) hits a DPI-dropped DC, failing fast lets the client rotate to the next DC roughly twice as quickly. Users previously sat on "connecting..." for nearly a minute walking DC1→DC3. - SNI rotation pool: add maps/chat/translate/play/lens.google.com to both the Rust DEFAULT_GOOGLE_SNI_POOL and the Android DEFAULT_SNI_POOL. Ported from upstream 57738ec. Extra fingerprint spread plus a couple of SNIs (maps, play) that reliably pass DPI where shorter *.google.com names don't. - x.com GraphQL URL truncation: when the path matches /i/api/graphql/<hash>/<op>?variables=..., drop everything from the first `&` onward. The combined variables+features+fieldToggles query string regularly exceeds Apps Script's URL length cap and returns a generic relay error; `variables=` alone is enough for x.com's timeline to render. Ported from upstream 2d959d4. No version bump — these are low-risk infrastructure patches; they'll fold into the next release.
1 parent 63cfb4b commit cbe06b5

3 files changed

Lines changed: 67 additions & 3 deletions

File tree

‎android/app/src/main/java/com/therealaleph/mhrv/ConfigStore.kt‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,4 +291,12 @@ val DEFAULT_SNI_POOL: List<String> = listOf(
291291
"accounts.googl.com",
292292
// Issue #47: same DPI-passing behaviour on MCI / Samantel.
293293
"scholar.google.com",
294+
// Ported from upstream Python FRONT_SNI_POOL_GOOGLE (commit 57738ec);
295+
// more rotation material for DPI-fingerprint spread and a couple of
296+
// SNIs (maps/play) that pass DPI where shorter *.google.com names don't.
297+
"maps.google.com",
298+
"chat.google.com",
299+
"translate.google.com",
300+
"play.google.com",
301+
"lens.google.com",
294302
)

‎src/domain_fronter.rs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,18 @@ pub const DEFAULT_GOOGLE_SNI_POOL: &[&str] = &[
10941094
// core *.google.com cert so it handshakes normally against
10951095
// google_ip:443.
10961096
"scholar.google.com",
1097+
// Additional Google properties for rotation. Ported from upstream
1098+
// Python `FRONT_SNI_POOL_GOOGLE` (masterking32/MasterHttpRelayVPN
1099+
// commit 57738ec, "Add additional Google services to exclusion
1100+
// lists"). All served off the same GFE IP range, all covered by the
1101+
// wildcard cert, all give the DPI-fingerprint spread without extra
1102+
// config. A few of these (maps.google.com, play.google.com) reliably
1103+
// pass DPI on carriers where the shorter `*.google.com` names don't.
1104+
"maps.google.com",
1105+
"chat.google.com",
1106+
"translate.google.com",
1107+
"play.google.com",
1108+
"lens.google.com",
10971109
];
10981110

10991111
/// Build the pool of SNI hosts used for outbound connections to the Google

‎src/proxy_server.rs‎

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,22 @@ async fn plain_tcp_passthrough(
604604
upstream_socks5: Option<&str>,
605605
) {
606606
let target_host = host.trim_start_matches('[').trim_end_matches(']');
607+
// Shorter connect timeout for IP literals (4s vs 10s for hostnames).
608+
// Ported from upstream Python 7b1812c: when the target is an IP (i.e.
609+
// a raw Telegram DC, or an IP someone hardcoded), and that route is
610+
// DPI-dropped, the client speeds up its own DC-rotation / fallback if
611+
// we fail fast. Ten seconds of "waiting for a dead IP" translates
612+
// directly into Telegram's 10s-per-DC rotation delay — users see the
613+
// app sit on "connecting..." for nearly a minute as it walks through
614+
// DC1, DC2, DC3. At 4s we cut that in roughly half.
615+
// Hostnames still get 10s because DNS + first-hop TCP genuinely can
616+
// take that long on flaky links, and the resolver fallbacks already
617+
// trim the worst case.
618+
let connect_timeout = if looks_like_ip(target_host) {
619+
std::time::Duration::from_secs(4)
620+
} else {
621+
std::time::Duration::from_secs(10)
622+
};
607623
let upstream = if let Some(proxy) = upstream_socks5 {
608624
match socks5_connect_via(proxy, target_host, port).await {
609625
Ok(s) => {
@@ -619,7 +635,7 @@ async fn plain_tcp_passthrough(
619635
e
620636
);
621637
match tokio::time::timeout(
622-
std::time::Duration::from_secs(10),
638+
connect_timeout,
623639
TcpStream::connect((target_host, port)),
624640
)
625641
.await
@@ -631,7 +647,7 @@ async fn plain_tcp_passthrough(
631647
}
632648
} else {
633649
match tokio::time::timeout(
634-
std::time::Duration::from_secs(10),
650+
connect_timeout,
635651
TcpStream::connect((target_host, port)),
636652
)
637653
.await
@@ -645,7 +661,10 @@ async fn plain_tcp_passthrough(
645661
return;
646662
}
647663
Err(_) => {
648-
tracing::debug!("plain-tcp connect {}:{} timeout", host, port);
664+
tracing::debug!(
665+
"plain-tcp connect {}:{} timeout (likely blocked; client should rotate)",
666+
host, port
667+
);
649668
return;
650669
}
651670
}
@@ -1104,6 +1123,31 @@ where
11041123

11051124
let body = read_body(stream, &leftover, &headers).await?;
11061125

1126+
// ── Per-host URL fix-ups ──────────────────────────────────────────
1127+
// x.com's GraphQL endpoints concatenate three huge JSON blobs into
1128+
// the query string: `?variables=<json>&features=<json>&fieldToggles=<json>`.
1129+
// The combined URL regularly exceeds Apps Script's URL length limit
1130+
// (it returns a generic "relay error" with no useful detail). The
1131+
// `variables=` portion alone is enough for x.com to serve the
1132+
// timeline — `features` / `fieldToggles` are client-capability
1133+
// hints it tolerates being absent. Truncating at the first `&`
1134+
// after `?variables=` ships a working request that fits under the
1135+
// limit. Ported from upstream Python 2d959d4 (p0u1ya's fix).
1136+
let path = if host.eq_ignore_ascii_case("x.com")
1137+
&& path.starts_with("/i/api/graphql/")
1138+
&& path.contains("?variables=")
1139+
{
1140+
match path.split_once('&') {
1141+
Some((short, _)) => {
1142+
tracing::debug!("x.com graphql URL truncated: {} chars -> {}", path.len(), short.len());
1143+
short.to_string()
1144+
}
1145+
None => path,
1146+
}
1147+
} else {
1148+
path
1149+
};
1150+
11071151
let default_port = if scheme == "https" { 443 } else { 80 };
11081152
let url = if port == default_port {
11091153
format!("{}://{}{}", scheme, host, path)

0 commit comments

Comments
 (0)