fix: fix middle proxy mode without AD_TAG and retry another endpoint when one fails

This commit is contained in:
Kirill Yakunin authored and Alexander Bersenev committed 2026-05-31 00:35:59 +05:00
1 parent 0614c35020
commit bd5b03cd99
1 file changed
+19 -11
+19 -11
View File
@@ -949,7 +949,7 @@ class ProxyReqStreamWriter(LayeredStreamWriterBase):
print_err("BUG: attempted to send msg with len %d" % len(msg))
return 0
flags = FLAG_HAS_AD_TAG | FLAG_MAGIC | FLAG_EXTMODE2
flags = FLAG_MAGIC | FLAG_EXTMODE2
if self.proto_tag == PROTO_TAG_ABRIDGED:
flags |= FLAG_ABRIDGED
@@ -964,9 +964,14 @@ class ProxyReqStreamWriter(LayeredStreamWriterBase):
if msg.startswith(b"\x00" * 8):
flags |= FLAG_NOT_ENCRYPTED
if len(config.AD_TAG) == 16:
flags |= FLAG_HAS_AD_TAG
full_msg = bytearray()
full_msg += RPC_PROXY_REQ + int.to_bytes(flags, 4, "little") + self.out_conn_id
full_msg += self.remote_ip_port + self.our_ip_port + EXTRA_SIZE + PROXY_TAG
full_msg += self.remote_ip_port + self.our_ip_port
if len(config.AD_TAG) == 16:
full_msg += EXTRA_SIZE + PROXY_TAG
full_msg += bytes([len(config.AD_TAG)]) + config.AD_TAG + FOUR_BYTES_ALIGNER
full_msg += msg
@@ -1559,23 +1564,26 @@ async def do_middleproxy_handshake(proto_tag, dc_idx, cl_ip, cl_port):
if use_ipv6_tg:
if dc_idx not in TG_MIDDLE_PROXIES_V6:
return False
addr, port = myrandom.choice(TG_MIDDLE_PROXIES_V6[dc_idx])
proxies = list(TG_MIDDLE_PROXIES_V6[dc_idx])
else:
if dc_idx not in TG_MIDDLE_PROXIES_V4:
return False
addr, port = myrandom.choice(TG_MIDDLE_PROXIES_V4[dc_idx])
proxies = list(TG_MIDDLE_PROXIES_V4[dc_idx])
myrandom.shuffle(proxies)
for addr, port in proxies:
try:
ret = await tg_connection_pool.get_connection(addr, port, middleproxy_handshake)
reader_tgt, writer_tgt, my_ip, my_port = ret
reader_tgt, writer_tgt, my_ip, my_port = await tg_connection_pool.get_connection(
addr, port, middleproxy_handshake)
break
except ConnectionRefusedError as E:
print_err("The Telegram server %d (%s %s) is refusing connections" % (dc_idx, addr, port))
return False
print_err("The Telegram server %d (%s %s) is refusing connections, trying next" % (dc_idx, addr, port))
except ConnectionAbortedError as E:
print_err("The Telegram server connection is bad: %d (%s %s) %s" % (dc_idx, addr, port, E))
return False
print_err("The Telegram server connection is bad: %d (%s %s) %s, trying next" % (dc_idx, addr, port, E))
except (OSError, asyncio.TimeoutError) as E:
print_err("Unable to connect to the Telegram server %d (%s %s)" % (dc_idx, addr, port))
print_err("Unable to connect to the Telegram server %d (%s %s), trying next" % (dc_idx, addr, port))
else:
return False
writer_tgt = ProxyReqStreamWriter(writer_tgt, cl_ip, cl_port, my_ip, my_port, proto_tag)