Skip to content

Commit 374cb2c

Browse files
authored
feat: add TCPIntermediatePadded transport (#330)
1 parent 11439f3 commit 374cb2c

2 files changed

Lines changed: 74 additions & 0 deletions

File tree

pyrogram/connection/transport/tcp/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@
2222
from .tcp_full import TCPFull
2323
from .tcp_intermediate import TCPIntermediate
2424
from .tcp_intermediate_o import TCPIntermediateO
25+
from .tcp_intermediate_padded import TCPIntermediatePadded
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-present Dan <https://github.com/delivrance>
3+
#
4+
# This file is part of Pyrogram.
5+
#
6+
# Pyrogram is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Lesser General Public License as published
8+
# by the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# Pyrogram is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
18+
19+
import asyncio
20+
import logging
21+
import os
22+
from struct import pack, unpack
23+
from typing import Optional, Tuple, Union
24+
25+
from .tcp import TCP, ProxyDict
26+
27+
log = logging.getLogger(__name__)
28+
29+
30+
class TCPIntermediatePadded(TCP):
31+
def __init__(
32+
self,
33+
ipv6: bool = False,
34+
proxy: Optional[Union[str, ProxyDict]] = None,
35+
crypto_executor_workers: int = 1,
36+
loop: Optional[asyncio.AbstractEventLoop] = None,
37+
) -> None:
38+
super().__init__(ipv6, proxy, crypto_executor_workers, loop)
39+
40+
async def connect(self, address: Tuple[str, int]) -> None:
41+
self.marker_event.clear()
42+
await super().connect(address)
43+
await super().send(b"\xdd" * 4, wait_for_marker=False)
44+
self.marker_event.set()
45+
46+
async def send(self, data: bytes, *args) -> None:
47+
padding = os.urandom(os.urandom(1)[0] & 0x0F)
48+
await super().send(pack("<i", len(data) + len(padding)) + data + padding)
49+
50+
async def recv(self, length: int = 0) -> Optional[bytes]:
51+
length = await super().recv(4)
52+
53+
if length is None:
54+
return None
55+
56+
length = unpack("<i", length)[0]
57+
data = await super().recv(length)
58+
59+
if data is None:
60+
return None
61+
62+
if length < 24:
63+
if length >= 8 and data[:4] == b"\xff\xff\xff\xff":
64+
return data[:8]
65+
return data[:4]
66+
67+
if data[:8] != b"\x00" * 8:
68+
strip = (length - 24) % 16
69+
70+
if strip:
71+
data = data[:-strip]
72+
73+
return data

0 commit comments

Comments
 (0)