Skip to content

Commit 981d644

Browse files
committed
Add connection package
1 parent 95cb91b commit 981d644

File tree

8 files changed

+357
-0
lines changed

8 files changed

+357
-0
lines changed

pyrogram/connection/__init__.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017 Dan Tès <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+
from .connection import Connection

pyrogram/connection/connection.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017 Dan Tès <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 logging
20+
import time
21+
22+
from .transport import *
23+
24+
log = logging.getLogger(__name__)
25+
26+
27+
class Connection:
28+
MODES = {
29+
0: TCPFull,
30+
1: TCPAbridged,
31+
2: TCPIntermediate
32+
}
33+
34+
def __init__(self, ipv4: str, mode: int = 1):
35+
self.address = (ipv4, 80)
36+
self.mode = self.MODES.get(mode, TCPAbridged)
37+
self.connection = None
38+
39+
def connect(self):
40+
while True:
41+
self.connection = self.mode()
42+
43+
try:
44+
log.info("Connecting...")
45+
self.connection.connect(self.address)
46+
except OSError:
47+
self.connection.close()
48+
time.sleep(1)
49+
else:
50+
break
51+
52+
def close(self):
53+
self.connection.close()
54+
55+
def send(self, data: bytes):
56+
self.connection.send(data)
57+
58+
def recv(self) -> bytes or None:
59+
return self.connection.recv()
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017 Dan Tès <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+
from .tcp import *
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017 Dan Tès <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+
from .tcp_abridged import TCPAbridged
20+
from .tcp_full import TCPFull
21+
from .tcp_intermediate import TCPIntermediate
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017 Dan Tès <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 logging
20+
import socket
21+
22+
log = logging.getLogger(__name__)
23+
24+
25+
class TCP(socket.socket):
26+
def __init__(self):
27+
super().__init__()
28+
29+
def send(self, *args):
30+
pass
31+
32+
def recv(self, *args):
33+
pass
34+
35+
def recvall(self, length: int) -> bytes or None:
36+
data = b""
37+
38+
while len(data) < length:
39+
try:
40+
packet = super().recv(length - len(data))
41+
except OSError:
42+
return None
43+
else:
44+
if packet:
45+
data += packet
46+
else:
47+
return None
48+
49+
return data
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017 Dan Tès <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 logging
20+
21+
from .tcp import TCP
22+
23+
log = logging.getLogger(__name__)
24+
25+
26+
class TCPAbridged(TCP):
27+
def __init__(self):
28+
super().__init__()
29+
self.is_first_packet = None
30+
31+
def connect(self, address: tuple):
32+
super().connect(address)
33+
self.is_first_packet = True
34+
log.info("Connected!")
35+
36+
def send(self, data: bytes):
37+
length = len(data) // 4
38+
39+
data = (
40+
bytes([length]) + data
41+
if length <= 126
42+
else b"\x7f" + int.to_bytes(length, 3, "little") + data
43+
)
44+
45+
if self.is_first_packet:
46+
data = b"\xef" + data
47+
self.is_first_packet = False
48+
49+
super().sendall(data)
50+
51+
def recv(self) -> bytes or None:
52+
length = self.recvall(1)
53+
54+
if length is None:
55+
return None
56+
57+
if length == b"\x7f":
58+
length = self.recvall(3)
59+
60+
if length is None:
61+
return None
62+
63+
length = int.from_bytes(length, "little") * 4
64+
65+
packet = self.recvall(length)
66+
67+
return packet
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017 Dan Tès <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 logging
20+
from binascii import crc32
21+
from struct import pack, unpack
22+
from threading import Lock
23+
24+
from .tcp import TCP
25+
26+
log = logging.getLogger(__name__)
27+
28+
29+
class TCPFull(TCP):
30+
def __init__(self):
31+
super().__init__()
32+
self.seq_no = None
33+
self.lock = Lock()
34+
35+
def connect(self, address: tuple):
36+
super().connect(address)
37+
self.seq_no = 0
38+
log.info("Connected!")
39+
40+
def send(self, data: bytes):
41+
with self.lock:
42+
# 12 = packet_length (4), seq_no (4), crc32 (4) (at the end)
43+
data = pack("<II", len(data) + 12, self.seq_no) + data
44+
data += pack("<I", crc32(data))
45+
46+
self.seq_no += 1
47+
48+
super().sendall(data)
49+
50+
def recv(self) -> bytes or None:
51+
length = self.recvall(4)
52+
53+
if length is None:
54+
return None
55+
56+
packet = self.recvall(unpack("<I", length)[0] - 4)
57+
58+
if packet is None:
59+
return None
60+
61+
packet = length + packet # Whole data + checksum
62+
checksum = packet[-4:] # Checksum is at the last 4 bytes
63+
packet = packet[:-4] # Data without checksum
64+
65+
if crc32(packet) != unpack("<I", checksum)[0]:
66+
return None
67+
68+
return packet[8:] # Skip packet_length (4) and tcp_seq_no (4)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017 Dan Tès <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 logging
20+
from struct import pack, unpack
21+
22+
from .tcp_abridged import TCP
23+
24+
log = logging.getLogger(__name__)
25+
26+
27+
class TCPIntermediate(TCP):
28+
def __init__(self):
29+
super().__init__()
30+
self.is_first_packet = None
31+
32+
def connect(self, address: tuple):
33+
super().connect(address)
34+
self.is_first_packet = True
35+
log.info("Connected!")
36+
37+
def send(self, data: bytes):
38+
length = len(data)
39+
data = pack("<i", length) + data
40+
41+
if self.is_first_packet:
42+
data = b"\xee" * 4 + data
43+
self.is_first_packet = False
44+
45+
super().sendall(data)
46+
47+
def recv(self) -> bytes or None:
48+
length = self.recvall(4)
49+
50+
if length is None:
51+
return None
52+
53+
packet = self.recvall(unpack("<I", length)[0])
54+
55+
return packet

0 commit comments

Comments
 (0)