2
0
mirror of https://github.com/pyrogram/pyrogram synced 2025-08-28 21:07:59 +00:00

Add connection package

This commit is contained in:
Dan 2017-12-05 12:37:30 +01:00
parent 95cb91b778
commit 981d644288
8 changed files with 357 additions and 0 deletions

View File

@ -0,0 +1,19 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017 Dan Tès <https://github.com/delivrance>
#
# 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 <http://www.gnu.org/licenses/>.
from .connection import Connection

View File

@ -0,0 +1,59 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017 Dan Tès <https://github.com/delivrance>
#
# 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 <http://www.gnu.org/licenses/>.
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()

View File

@ -0,0 +1,19 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017 Dan Tès <https://github.com/delivrance>
#
# 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 <http://www.gnu.org/licenses/>.
from .tcp import *

View File

@ -0,0 +1,21 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017 Dan Tès <https://github.com/delivrance>
#
# 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 <http://www.gnu.org/licenses/>.
from .tcp_abridged import TCPAbridged
from .tcp_full import TCPFull
from .tcp_intermediate import TCPIntermediate

View File

@ -0,0 +1,49 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017 Dan Tès <https://github.com/delivrance>
#
# 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 <http://www.gnu.org/licenses/>.
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

View File

@ -0,0 +1,67 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017 Dan Tès <https://github.com/delivrance>
#
# 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 <http://www.gnu.org/licenses/>.
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

View File

@ -0,0 +1,68 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017 Dan Tès <https://github.com/delivrance>
#
# 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 <http://www.gnu.org/licenses/>.
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("<II", len(data) + 12, self.seq_no) + data
data += pack("<I", crc32(data))
self.seq_no += 1
super().sendall(data)
def recv(self) -> bytes or None:
length = self.recvall(4)
if length is None:
return None
packet = self.recvall(unpack("<I", length)[0] - 4)
if packet is None:
return None
packet = length + packet # Whole data + checksum
checksum = packet[-4:] # Checksum is at the last 4 bytes
packet = packet[:-4] # Data without checksum
if crc32(packet) != unpack("<I", checksum)[0]:
return None
return packet[8:] # Skip packet_length (4) and tcp_seq_no (4)

View File

@ -0,0 +1,55 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017 Dan Tès <https://github.com/delivrance>
#
# 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 <http://www.gnu.org/licenses/>.
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("<i", length) + data
if self.is_first_packet:
data = b"\xee" * 4 + data
self.is_first_packet = False
super().sendall(data)
def recv(self) -> bytes or None:
length = self.recvall(4)
if length is None:
return None
packet = self.recvall(unpack("<I", length)[0])
return packet