Skip to content

Commit 0614c35

Browse files
committed
implement read timeout, if server sends nothing for 1 min, connection drops
1 parent 368669a commit 0614c35

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

mtprotoproxy.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,9 @@ def init_config():
264264
# telegram servers connect timeout in seconds
265265
conf_dict.setdefault("TG_CONNECT_TIMEOUT", 10)
266266

267+
# drop connection if no data from telegram server for this many seconds
268+
conf_dict.setdefault("TG_READ_TIMEOUT", 60)
269+
267270
# listen address for IPv4
268271
conf_dict.setdefault("LISTEN_ADDR_IPV4", "0.0.0.0")
269272

@@ -1584,7 +1587,11 @@ async def do_middleproxy_handshake(proto_tag, dc_idx, cl_ip, cl_port):
15841587
async def tg_connect_reader_to_writer(rd, wr, user, rd_buf_size, is_upstream):
15851588
try:
15861589
while True:
1587-
data = await rd.read(rd_buf_size)
1590+
if not is_upstream:
1591+
data = await asyncio.wait_for(rd.read(rd_buf_size),
1592+
timeout=config.TG_READ_TIMEOUT)
1593+
else:
1594+
data = await rd.read(rd_buf_size)
15881595
if isinstance(data, tuple):
15891596
data, extra = data
15901597
else:
@@ -1605,7 +1612,7 @@ async def tg_connect_reader_to_writer(rd, wr, user, rd_buf_size, is_upstream):
16051612

16061613
wr.write(data, extra)
16071614
await wr.drain()
1608-
except (OSError, asyncio.IncompleteReadError) as e:
1615+
except (OSError, asyncio.IncompleteReadError, asyncio.TimeoutError) as e:
16091616
# print_err(e)
16101617
pass
16111618

0 commit comments

Comments
 (0)