fix: improve error handling in proxy server and h2 transport classes

This commit is contained in:
Abolfazl
2026-04-23 22:58:49 +03:30
parent 70dd77933d
commit 4243135c66
3 changed files with 22 additions and 4 deletions
+1 -1
View File
@@ -1752,4 +1752,4 @@ class DomainFronter:
f"Content-Length: {len(body)}\r\n"
f"\r\n"
f"{body}"
).encode()
).encode()
+9
View File
@@ -363,6 +363,15 @@ class H2Transport:
except asyncio.CancelledError:
pass
except ssl.SSLError as e:
# APPLICATION_DATA_AFTER_CLOSE_NOTIFY is raised when the server
# sends data after its TLS close_notify — technically a protocol
# violation but very common with CDNs. It just means the
# connection is closed; reconnect on the next request.
if "APPLICATION_DATA_AFTER_CLOSE_NOTIFY" in str(e):
log.debug("H2 TLS session closed by remote (close_notify): %s", e)
else:
log.error("H2 reader error: %s", e)
except Exception as e:
log.error("H2 reader error: %s", e)
finally:
+12 -3
View File
@@ -1014,7 +1014,7 @@ class ProxyServer:
headers[k.strip()] = v.strip()
# Shortening the length of X API URLs to prevent relay errors.
if host == "x.com" and re.match(r"/i/api/graphql/[^/]+/[^?]+\?variables=", path):
if host == "x.com" and re.match(r"/i/api/graphql/[^/]+/[^?]+\?variables=", path):
path = path.split("&")[0]
# MITM traffic arrives as origin-form paths; SOCKS/plain HTTP can
@@ -1062,9 +1062,18 @@ class ProxyServer:
# Relay through Apps Script
try:
response = await self._relay_smart(method, url, headers, body, writer)
except asyncio.TimeoutError:
log.warning("Relay timeout (%s)", url[:60])
response = (
b"HTTP/1.1 504 Gateway Timeout\r\n"
b"Content-Type: text/plain\r\n"
b"Content-Length: 13\r\n"
b"\r\nRelay timeout"
)
except Exception as e:
log.error("Relay error (%s): %s", url[:60], e)
err_body = f"Relay error: {e}".encode()
err_label = str(e) or type(e).__name__
log.error("Relay error (%s): %s", url[:60], err_label)
err_body = f"Relay error: {err_label}".encode()
response = (
b"HTTP/1.1 502 Bad Gateway\r\n"
b"Content-Type: text/plain\r\n"