fix: Refactor IP-literal handling in ProxyServer to improve relay logic and logging

This commit is contained in:
Abolfazl
2026-05-13 05:33:29 +03:30
parent 93947f4790
commit 45c39af110
+9 -32
View File
@@ -580,42 +580,19 @@ class ProxyServer:
return return
# ── IP-literal destinations ─────────────────────────────── # ── IP-literal destinations ───────────────────────────────
# Prefer a direct tunnel first (works for unblocked IPs and keeps # Relay HTTP(S) IP literals through Apps Script (e.g. Telegram DCs on
# TLS end-to-end). If the network blocks the route (common for # 443) to bypass DPI that blocks direct routes to those IPs.
# Telegram data-centers behind DPI), fall back to: # Keep non-HTTP ports on direct tunnel because they cannot be relayed.
# • port 443 → MITM + relay through Apps Script
# • port 80 → plain-HTTP relay through Apps Script
# • other → give up (non-HTTP; can't be relayed)
# We use a shorter connect timeout for IP literals (4 s) because
# when the route is DPI-dropped, waiting longer doesn't help and
# clients like Telegram speed up DC-rotation when we fail fast.
# We remember per-IP failures for a short while so subsequent
# connects skip the doomed direct attempt.
if is_ip_literal(host): if is_ip_literal(host):
if not self._direct_temporarily_disabled(host):
log.info("Direct tunnel → %s:%d (IP literal)", host, port)
ok = await self._do_direct_tunnel(
host, port, reader, writer, timeout=4.0,
)
if ok:
return
self._remember_direct_failure(host, ttl=300)
if port not in (80, 443):
log.warning("Direct tunnel failed for %s:%d", host, port)
return
log.warning(
"Direct tunnel fallback → %s:%d (switching to relay)",
host, port,
)
else:
log.info(
"Relay fallback → %s:%d (direct temporarily disabled)",
host, port,
)
if port == 443: if port == 443:
await self._do_mitm_connect(host, port, reader, writer) await self._do_mitm_connect(host, port, reader, writer)
elif port == 80: elif port == 80:
await self._do_plain_http_tunnel(host, port, reader, writer) await self._do_plain_http_tunnel(host, port, reader, writer)
else:
log.info("Direct tunnel → %s:%d (IP literal non-HTTP port)", host, port)
ok = await self._do_direct_tunnel(host, port, reader, writer)
if not ok:
log.warning("Direct tunnel failed for %s:%d", host, port)
return return
override_ip = self._sni_rewrite_ip(host) override_ip = self._sni_rewrite_ip(host)
@@ -684,7 +661,7 @@ class ProxyServer:
h = host.lower().rstrip(".") h = host.lower().rstrip(".")
if h == "music.youtube.com" or h.endswith(".music.youtube.com"): if h == "music.youtube.com" or h.endswith(".music.youtube.com"):
return None return None
ip = self._hosts_ip(host) ip = self._hosts_ip(host)
if ip: if ip:
return ip return ip