diff --git a/pyrogram/connection/__init__.py b/pyrogram/connection/__init__.py new file mode 100644 index 00000000..17f9de0e --- /dev/null +++ b/pyrogram/connection/__init__.py @@ -0,0 +1,19 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017 Dan Tès +# +# This file is part of Pyrogram. +# +# Pyrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrogram. If not, see . + +from .connection import Connection diff --git a/pyrogram/connection/connection.py b/pyrogram/connection/connection.py new file mode 100644 index 00000000..430df8fd --- /dev/null +++ b/pyrogram/connection/connection.py @@ -0,0 +1,59 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017 Dan Tès +# +# This file is part of Pyrogram. +# +# Pyrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrogram. If not, see . + +import logging +import time + +from .transport import * + +log = logging.getLogger(__name__) + + +class Connection: + MODES = { + 0: TCPFull, + 1: TCPAbridged, + 2: TCPIntermediate + } + + def __init__(self, ipv4: str, mode: int = 1): + self.address = (ipv4, 80) + self.mode = self.MODES.get(mode, TCPAbridged) + self.connection = None + + def connect(self): + while True: + self.connection = self.mode() + + try: + log.info("Connecting...") + self.connection.connect(self.address) + except OSError: + self.connection.close() + time.sleep(1) + else: + break + + def close(self): + self.connection.close() + + def send(self, data: bytes): + self.connection.send(data) + + def recv(self) -> bytes or None: + return self.connection.recv() diff --git a/pyrogram/connection/transport/__init__.py b/pyrogram/connection/transport/__init__.py new file mode 100644 index 00000000..385976a1 --- /dev/null +++ b/pyrogram/connection/transport/__init__.py @@ -0,0 +1,19 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017 Dan Tès +# +# This file is part of Pyrogram. +# +# Pyrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrogram. If not, see . + +from .tcp import * diff --git a/pyrogram/connection/transport/tcp/__init__.py b/pyrogram/connection/transport/tcp/__init__.py new file mode 100644 index 00000000..7ecf7cf6 --- /dev/null +++ b/pyrogram/connection/transport/tcp/__init__.py @@ -0,0 +1,21 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017 Dan Tès +# +# This file is part of Pyrogram. +# +# Pyrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrogram. If not, see . + +from .tcp_abridged import TCPAbridged +from .tcp_full import TCPFull +from .tcp_intermediate import TCPIntermediate diff --git a/pyrogram/connection/transport/tcp/tcp.py b/pyrogram/connection/transport/tcp/tcp.py new file mode 100644 index 00000000..17fdcba6 --- /dev/null +++ b/pyrogram/connection/transport/tcp/tcp.py @@ -0,0 +1,49 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017 Dan Tès +# +# This file is part of Pyrogram. +# +# Pyrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrogram. If not, see . + +import logging +import socket + +log = logging.getLogger(__name__) + + +class TCP(socket.socket): + def __init__(self): + super().__init__() + + def send(self, *args): + pass + + def recv(self, *args): + pass + + def recvall(self, length: int) -> bytes or None: + data = b"" + + while len(data) < length: + try: + packet = super().recv(length - len(data)) + except OSError: + return None + else: + if packet: + data += packet + else: + return None + + return data diff --git a/pyrogram/connection/transport/tcp/tcp_abridged.py b/pyrogram/connection/transport/tcp/tcp_abridged.py new file mode 100644 index 00000000..c6615b14 --- /dev/null +++ b/pyrogram/connection/transport/tcp/tcp_abridged.py @@ -0,0 +1,67 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017 Dan Tès +# +# This file is part of Pyrogram. +# +# Pyrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrogram. If not, see . + +import logging + +from .tcp import TCP + +log = logging.getLogger(__name__) + + +class TCPAbridged(TCP): + def __init__(self): + super().__init__() + self.is_first_packet = None + + def connect(self, address: tuple): + super().connect(address) + self.is_first_packet = True + log.info("Connected!") + + def send(self, data: bytes): + length = len(data) // 4 + + data = ( + bytes([length]) + data + if length <= 126 + else b"\x7f" + int.to_bytes(length, 3, "little") + data + ) + + if self.is_first_packet: + data = b"\xef" + data + self.is_first_packet = False + + super().sendall(data) + + def recv(self) -> bytes or None: + length = self.recvall(1) + + if length is None: + return None + + if length == b"\x7f": + length = self.recvall(3) + + if length is None: + return None + + length = int.from_bytes(length, "little") * 4 + + packet = self.recvall(length) + + return packet diff --git a/pyrogram/connection/transport/tcp/tcp_full.py b/pyrogram/connection/transport/tcp/tcp_full.py new file mode 100644 index 00000000..417d5d5b --- /dev/null +++ b/pyrogram/connection/transport/tcp/tcp_full.py @@ -0,0 +1,68 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017 Dan Tès +# +# This file is part of Pyrogram. +# +# Pyrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrogram. If not, see . + +import logging +from binascii import crc32 +from struct import pack, unpack +from threading import Lock + +from .tcp import TCP + +log = logging.getLogger(__name__) + + +class TCPFull(TCP): + def __init__(self): + super().__init__() + self.seq_no = None + self.lock = Lock() + + def connect(self, address: tuple): + super().connect(address) + self.seq_no = 0 + log.info("Connected!") + + def send(self, data: bytes): + with self.lock: + # 12 = packet_length (4), seq_no (4), crc32 (4) (at the end) + data = pack(" bytes or None: + length = self.recvall(4) + + if length is None: + return None + + packet = self.recvall(unpack(" +# +# This file is part of Pyrogram. +# +# Pyrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrogram. If not, see . + +import logging +from struct import pack, unpack + +from .tcp_abridged import TCP + +log = logging.getLogger(__name__) + + +class TCPIntermediate(TCP): + def __init__(self): + super().__init__() + self.is_first_packet = None + + def connect(self, address: tuple): + super().connect(address) + self.is_first_packet = True + log.info("Connected!") + + def send(self, data: bytes): + length = len(data) + data = pack(" bytes or None: + length = self.recvall(4) + + if length is None: + return None + + packet = self.recvall(unpack("