3 Commits

Author SHA1 Message Date
Alexander Bersenev
0614c35020 implement read timeout, if server sends nothing for 1 min, connection drops 2026-02-18 02:37:36 +05:00
Alexander Bersenev
368669a546 don't prefer ipv6 if using middle proxies, they are unstable 2026-02-18 02:10:55 +05:00
Alexander Bersenev
949ee12ed2 use utc timezone when getting the server time 2026-02-18 01:30:46 +05:00

View File

@@ -139,8 +139,8 @@ def init_config():
# use middle proxy, necessary to show ad
conf_dict.setdefault("USE_MIDDLE_PROXY", len(conf_dict["AD_TAG"]) == 16)
# if IPv6 available, use it by default
conf_dict.setdefault("PREFER_IPV6", socket.has_ipv6)
# if IPv6 available, use it by default, IPv6 with middle proxies is unstable now
conf_dict.setdefault("PREFER_IPV6", socket.has_ipv6 and not conf_dict["USE_MIDDLE_PROXY"])
# disables tg->client traffic reencryption, faster but less secure
conf_dict.setdefault("FAST_MODE", True)
@@ -264,6 +264,9 @@ def init_config():
# telegram servers connect timeout in seconds
conf_dict.setdefault("TG_CONNECT_TIMEOUT", 10)
# drop connection if no data from telegram server for this many seconds
conf_dict.setdefault("TG_READ_TIMEOUT", 60)
# listen address for IPv4
conf_dict.setdefault("LISTEN_ADDR_IPV4", "0.0.0.0")
@@ -1584,7 +1587,11 @@ async def do_middleproxy_handshake(proto_tag, dc_idx, cl_ip, cl_port):
async def tg_connect_reader_to_writer(rd, wr, user, rd_buf_size, is_upstream):
try:
while True:
data = await rd.read(rd_buf_size)
if not is_upstream:
data = await asyncio.wait_for(rd.read(rd_buf_size),
timeout=config.TG_READ_TIMEOUT)
else:
data = await rd.read(rd_buf_size)
if isinstance(data, tuple):
data, extra = data
else:
@@ -1605,7 +1612,7 @@ async def tg_connect_reader_to_writer(rd, wr, user, rd_buf_size, is_upstream):
wr.write(data, extra)
await wr.drain()
except (OSError, asyncio.IncompleteReadError) as e:
except (OSError, asyncio.IncompleteReadError, asyncio.TimeoutError) as e:
# print_err(e)
pass
@@ -2011,6 +2018,7 @@ async def get_srv_time():
continue
line = line[len("Date: "):].decode()
srv_time = datetime.datetime.strptime(line, "%a, %d %b %Y %H:%M:%S %Z")
srv_time = srv_time.replace(tzinfo=datetime.timezone.utc)
now_time = datetime.datetime.now(datetime.timezone.utc)
is_time_skewed = (now_time-srv_time).total_seconds() > MAX_TIME_SKEW
if is_time_skewed and config.USE_MIDDLE_PROXY and not disable_middle_proxy: