Skip to content

Commit 4b3b647

Browse files
committed
Merge branch 'ipv6' into develop
# Conflicts: # pyrogram/client/client.py # pyrogram/session/session.py
2 parents 2779e33 + d38d23f commit 4b3b647

11 files changed

Lines changed: 57 additions & 27 deletions

File tree

pyrogram/client/client.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ def __init__(self,
146146
device_model: str = None,
147147
system_version: str = None,
148148
lang_code: str = None,
149+
ipv6: bool = False,
149150
proxy: dict = None,
150151
test_mode: bool = False,
151152
phone_number: str = None,
@@ -166,6 +167,7 @@ def __init__(self,
166167
self.device_model = device_model
167168
self.system_version = system_version
168169
self.lang_code = lang_code
170+
self.ipv6 = ipv6
169171
# TODO: Make code consistent, use underscore for private/protected fields
170172
self._proxy = proxy
171173
self.test_mode = test_mode
@@ -388,7 +390,7 @@ def authorize_bot(self):
388390
self.session.stop()
389391

390392
self.dc_id = e.x
391-
self.auth_key = Auth(self.dc_id, self.test_mode, self._proxy).create()
393+
self.auth_key = Auth(self.dc_id, self.test_mode, self.ipv6, self._proxy).create()
392394

393395
self.session = Session(
394396
self,
@@ -433,7 +435,7 @@ def authorize_user(self):
433435
self.session.stop()
434436

435437
self.dc_id = e.x
436-
self.auth_key = Auth(self.dc_id, self.test_mode, self._proxy).create()
438+
self.auth_key = Auth(self.dc_id, self.test_mode, self.ipv6, self._proxy).create()
437439

438440
self.session = Session(
439441
self,
@@ -936,7 +938,7 @@ def load_session(self):
936938
except FileNotFoundError:
937939
self.dc_id = 1
938940
self.date = 0
939-
self.auth_key = Auth(self.dc_id, self.test_mode, self._proxy).create()
941+
self.auth_key = Auth(self.dc_id, self.test_mode, self.ipv6, self._proxy).create()
940942
else:
941943
self.dc_id = s["dc_id"]
942944
self.test_mode = s["test_mode"]
@@ -1072,7 +1074,7 @@ def save_file(self,
10721074
file_id = file_id or self.rnd_id()
10731075
md5_sum = md5() if not is_big and not is_missing_part else None
10741076

1075-
session = Session(self, self.dc_id, self.auth_key, is_media=True)
1077+
session = Session(self, self.ipv6, self.dc_id, self.auth_key, is_media=True)
10761078
session.start()
10771079

10781080
try:

pyrogram/connection/connection.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import time
2222

2323
from .transport import *
24+
from ..session.internals import DataCenter
2425

2526
log = logging.getLogger(__name__)
2627

@@ -36,21 +37,24 @@ class Connection:
3637
4: TCPIntermediateO
3738
}
3839

39-
def __init__(self, address: tuple, proxy: dict, mode: int = 1):
40-
self.address = address
40+
def __init__(self, dc_id: int, test_mode: bool, ipv6: bool, proxy: dict, mode: int = 1):
41+
self.ipv6 = ipv6
4142
self.proxy = proxy
43+
self.address = DataCenter(dc_id, test_mode, ipv6)
4244
self.mode = self.MODES.get(mode, TCPAbridged)
45+
4346
self.lock = threading.Lock()
4447
self.connection = None
4548

4649
def connect(self):
4750
for i in range(Connection.MAX_RETRIES):
48-
self.connection = self.mode(self.proxy)
51+
self.connection = self.mode(self.ipv6, self.proxy)
4952

5053
try:
5154
log.info("Connecting...")
5255
self.connection.connect(self.address)
53-
except OSError:
56+
except OSError as e:
57+
log.warning(e) # TODO: Remove
5458
self.connection.close()
5559
time.sleep(1)
5660
else:

pyrogram/connection/transport/tcp/tcp.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@
3333

3434

3535
class TCP(socks.socksocket):
36-
def __init__(self, proxy: dict):
37-
super().__init__()
36+
def __init__(self, ipv6: bool, proxy: dict):
37+
super().__init__(family=socket.AF_INET6 if ipv6 else socket.AF_INET)
38+
3839
self.settimeout(10)
3940
self.proxy_enabled = proxy.get("enabled", False)
4041

pyrogram/connection/transport/tcp/tcp_abridged.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424

2525

2626
class TCPAbridged(TCP):
27-
def __init__(self, proxy: dict):
28-
super().__init__(proxy)
27+
def __init__(self, ipv6: bool, proxy: dict):
28+
super().__init__(ipv6, proxy)
2929

3030
def connect(self, address: tuple):
3131
super().connect(address)

pyrogram/connection/transport/tcp/tcp_abridged_o.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@
2828
class TCPAbridgedO(TCP):
2929
RESERVED = (b"HEAD", b"POST", b"GET ", b"OPTI", b"\xee" * 4)
3030

31-
def __init__(self, proxy: dict):
32-
super().__init__(proxy)
31+
def __init__(self, ipv6: bool, proxy: dict):
32+
super().__init__(ipv6, proxy)
33+
3334
self.encrypt = None
3435
self.decrypt = None
3536

pyrogram/connection/transport/tcp/tcp_full.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@
2626

2727

2828
class TCPFull(TCP):
29-
def __init__(self, proxy: dict):
30-
super().__init__(proxy)
29+
def __init__(self, ipv6: bool, proxy: dict):
30+
super().__init__(ipv6, proxy)
31+
3132
self.seq_no = None
3233

3334
def connect(self, address: tuple):

pyrogram/connection/transport/tcp/tcp_intermediate.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525

2626

2727
class TCPIntermediate(TCP):
28-
def __init__(self, proxy: dict):
29-
super().__init__(proxy)
28+
def __init__(self, ipv6: bool, proxy: dict):
29+
super().__init__(ipv6, proxy)
3030

3131
def connect(self, address: tuple):
3232
super().connect(address)

pyrogram/connection/transport/tcp/tcp_intermediate_o.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@
2929
class TCPIntermediateO(TCP):
3030
RESERVED = (b"HEAD", b"POST", b"GET ", b"OPTI", b"\xee" * 4)
3131

32-
def __init__(self, proxy: dict):
33-
super().__init__(proxy)
32+
def __init__(self, ipv6: bool, proxy: dict):
33+
super().__init__(ipv6, proxy)
34+
3435
self.encrypt = None
3536
self.decrypt = None
3637

pyrogram/session/auth.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from pyrogram.api.core import Object, Long, Int
2727
from pyrogram.connection import Connection
2828
from pyrogram.crypto import AES, RSA, Prime
29-
from .internals import MsgId, DataCenter
29+
from .internals import MsgId
3030

3131
log = logging.getLogger(__name__)
3232

@@ -46,9 +46,10 @@ class Auth:
4646
16
4747
)
4848

49-
def __init__(self, dc_id: int, test_mode: bool, proxy: dict):
49+
def __init__(self, dc_id: int, test_mode: bool, ipv6: bool, proxy: dict):
5050
self.dc_id = dc_id
5151
self.test_mode = test_mode
52+
self.ipv6 = ipv6
5253
self.proxy = proxy
5354

5455
self.connection = None
@@ -84,7 +85,7 @@ def create(self):
8485
# The server may close the connection at any time, causing the auth key creation to fail.
8586
# If that happens, just try again up to MAX_RETRIES times.
8687
while True:
87-
self.connection = Connection(DataCenter(self.dc_id, self.test_mode), self.proxy)
88+
self.connection = Connection(self.dc_id, self.test_mode, self.ipv6, self.proxy)
8889

8990
try:
9091
log.info("Start creating a new auth key on DC{}".format(self.dc_id))

pyrogram/session/internals/data_center.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,24 @@ class DataCenter:
3434
121: "95.213.217.195"
3535
}
3636

37-
def __new__(cls, dc_id: int, test_mode: bool):
38-
return (cls.TEST[dc_id], 80) if test_mode else (cls.PROD[dc_id], 443)
37+
TEST_IPV6 = {
38+
1: "2001:0b28:f23d:f001:0000:0000:0000:000e",
39+
2: "2001:067c:04e8:f002:0000:0000:0000:000e",
40+
3: "2001:0b28:f23d:f003:0000:0000:0000:000e",
41+
121: "2a03:b0c0:0003:00d0:0000:0000:0114:d001"
42+
}
43+
44+
PROD_IPV6 = {
45+
1: "2001:0b28:f23d:f001:0000:0000:0000:000a",
46+
2: "2001:067c:04e8:f002:0000:0000:0000:000a",
47+
3: "2001:0b28:f23d:f003:0000:0000:0000:000a",
48+
4: "2001:067c:04e8:f004:0000:0000:0000:000a",
49+
5: "2001:0b28:f23f:f005:0000:0000:0000:000a",
50+
121: "2a03:b0c0:0003:00d0:0000:0000:0114:d001"
51+
}
52+
53+
def __new__(cls, dc_id: int, test_mode: bool, ipv6: bool):
54+
if ipv6:
55+
return (cls.TEST_IPV6[dc_id], 80) if test_mode else (cls.PROD_IPV6[dc_id], 443)
56+
else:
57+
return (cls.TEST[dc_id], 80) if test_mode else (cls.PROD[dc_id], 443)

0 commit comments

Comments
 (0)