Skip to content

Commit 45c39af

Browse files
committed
fix: Refactor IP-literal handling in ProxyServer to improve relay logic and logging
1 parent 93947f4 commit 45c39af

1 file changed

Lines changed: 9 additions & 32 deletions

File tree

src/proxy/proxy_server.py

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -580,42 +580,19 @@ async def _handle_target_tunnel(self, host: str, port: int,
580580
return
581581

582582
# ── IP-literal destinations ───────────────────────────────
583-
# Prefer a direct tunnel first (works for unblocked IPs and keeps
584-
# TLS end-to-end). If the network blocks the route (common for
585-
# Telegram data-centers behind DPI), fall back to:
586-
# • port 443 → MITM + relay through Apps Script
587-
# • port 80 → plain-HTTP relay through Apps Script
588-
# • other → give up (non-HTTP; can't be relayed)
589-
# We use a shorter connect timeout for IP literals (4 s) because
590-
# when the route is DPI-dropped, waiting longer doesn't help and
591-
# clients like Telegram speed up DC-rotation when we fail fast.
592-
# We remember per-IP failures for a short while so subsequent
593-
# connects skip the doomed direct attempt.
583+
# Relay HTTP(S) IP literals through Apps Script (e.g. Telegram DCs on
584+
# 443) to bypass DPI that blocks direct routes to those IPs.
585+
# Keep non-HTTP ports on direct tunnel because they cannot be relayed.
594586
if is_ip_literal(host):
595-
if not self._direct_temporarily_disabled(host):
596-
log.info("Direct tunnel → %s:%d (IP literal)", host, port)
597-
ok = await self._do_direct_tunnel(
598-
host, port, reader, writer, timeout=4.0,
599-
)
600-
if ok:
601-
return
602-
self._remember_direct_failure(host, ttl=300)
603-
if port not in (80, 443):
604-
log.warning("Direct tunnel failed for %s:%d", host, port)
605-
return
606-
log.warning(
607-
"Direct tunnel fallback → %s:%d (switching to relay)",
608-
host, port,
609-
)
610-
else:
611-
log.info(
612-
"Relay fallback → %s:%d (direct temporarily disabled)",
613-
host, port,
614-
)
615587
if port == 443:
616588
await self._do_mitm_connect(host, port, reader, writer)
617589
elif port == 80:
618590
await self._do_plain_http_tunnel(host, port, reader, writer)
591+
else:
592+
log.info("Direct tunnel → %s:%d (IP literal non-HTTP port)", host, port)
593+
ok = await self._do_direct_tunnel(host, port, reader, writer)
594+
if not ok:
595+
log.warning("Direct tunnel failed for %s:%d", host, port)
619596
return
620597

621598
override_ip = self._sni_rewrite_ip(host)
@@ -684,7 +661,7 @@ def _sni_rewrite_ip(self, host: str) -> str | None:
684661
h = host.lower().rstrip(".")
685662
if h == "music.youtube.com" or h.endswith(".music.youtube.com"):
686663
return None
687-
664+
688665
ip = self._hosts_ip(host)
689666
if ip:
690667
return ip

0 commit comments

Comments
 (0)