2
0
mirror of https://github.com/pyrogram/pyrogram synced 2025-08-29 05:18:10 +00:00

Merge branch 'ipv6' into develop

# Conflicts:
#	pyrogram/client/client.py
#	pyrogram/session/session.py
This commit is contained in:
Dan 2018-08-28 12:19:37 +02:00
commit 4b3b647245
11 changed files with 57 additions and 27 deletions

View File

@ -146,6 +146,7 @@ class Client(Methods, BaseClient):
device_model: str = None, device_model: str = None,
system_version: str = None, system_version: str = None,
lang_code: str = None, lang_code: str = None,
ipv6: bool = False,
proxy: dict = None, proxy: dict = None,
test_mode: bool = False, test_mode: bool = False,
phone_number: str = None, phone_number: str = None,
@ -166,6 +167,7 @@ class Client(Methods, BaseClient):
self.device_model = device_model self.device_model = device_model
self.system_version = system_version self.system_version = system_version
self.lang_code = lang_code self.lang_code = lang_code
self.ipv6 = ipv6
# TODO: Make code consistent, use underscore for private/protected fields # TODO: Make code consistent, use underscore for private/protected fields
self._proxy = proxy self._proxy = proxy
self.test_mode = test_mode self.test_mode = test_mode
@ -388,7 +390,7 @@ class Client(Methods, BaseClient):
self.session.stop() self.session.stop()
self.dc_id = e.x self.dc_id = e.x
self.auth_key = Auth(self.dc_id, self.test_mode, self._proxy).create() self.auth_key = Auth(self.dc_id, self.test_mode, self.ipv6, self._proxy).create()
self.session = Session( self.session = Session(
self, self,
@ -433,7 +435,7 @@ class Client(Methods, BaseClient):
self.session.stop() self.session.stop()
self.dc_id = e.x self.dc_id = e.x
self.auth_key = Auth(self.dc_id, self.test_mode, self._proxy).create() self.auth_key = Auth(self.dc_id, self.test_mode, self.ipv6, self._proxy).create()
self.session = Session( self.session = Session(
self, self,
@ -936,7 +938,7 @@ class Client(Methods, BaseClient):
except FileNotFoundError: except FileNotFoundError:
self.dc_id = 1 self.dc_id = 1
self.date = 0 self.date = 0
self.auth_key = Auth(self.dc_id, self.test_mode, self._proxy).create() self.auth_key = Auth(self.dc_id, self.test_mode, self.ipv6, self._proxy).create()
else: else:
self.dc_id = s["dc_id"] self.dc_id = s["dc_id"]
self.test_mode = s["test_mode"] self.test_mode = s["test_mode"]
@ -1072,7 +1074,7 @@ class Client(Methods, BaseClient):
file_id = file_id or self.rnd_id() file_id = file_id or self.rnd_id()
md5_sum = md5() if not is_big and not is_missing_part else None md5_sum = md5() if not is_big and not is_missing_part else None
session = Session(self, self.dc_id, self.auth_key, is_media=True) session = Session(self, self.ipv6, self.dc_id, self.auth_key, is_media=True)
session.start() session.start()
try: try:

View File

@ -21,6 +21,7 @@ import threading
import time import time
from .transport import * from .transport import *
from ..session.internals import DataCenter
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -36,21 +37,24 @@ class Connection:
4: TCPIntermediateO 4: TCPIntermediateO
} }
def __init__(self, address: tuple, proxy: dict, mode: int = 1): def __init__(self, dc_id: int, test_mode: bool, ipv6: bool, proxy: dict, mode: int = 1):
self.address = address self.ipv6 = ipv6
self.proxy = proxy self.proxy = proxy
self.address = DataCenter(dc_id, test_mode, ipv6)
self.mode = self.MODES.get(mode, TCPAbridged) self.mode = self.MODES.get(mode, TCPAbridged)
self.lock = threading.Lock() self.lock = threading.Lock()
self.connection = None self.connection = None
def connect(self): def connect(self):
for i in range(Connection.MAX_RETRIES): for i in range(Connection.MAX_RETRIES):
self.connection = self.mode(self.proxy) self.connection = self.mode(self.ipv6, self.proxy)
try: try:
log.info("Connecting...") log.info("Connecting...")
self.connection.connect(self.address) self.connection.connect(self.address)
except OSError: except OSError as e:
log.warning(e) # TODO: Remove
self.connection.close() self.connection.close()
time.sleep(1) time.sleep(1)
else: else:

View File

@ -33,8 +33,9 @@ log = logging.getLogger(__name__)
class TCP(socks.socksocket): class TCP(socks.socksocket):
def __init__(self, proxy: dict): def __init__(self, ipv6: bool, proxy: dict):
super().__init__() super().__init__(family=socket.AF_INET6 if ipv6 else socket.AF_INET)
self.settimeout(10) self.settimeout(10)
self.proxy_enabled = proxy.get("enabled", False) self.proxy_enabled = proxy.get("enabled", False)

View File

@ -24,8 +24,8 @@ log = logging.getLogger(__name__)
class TCPAbridged(TCP): class TCPAbridged(TCP):
def __init__(self, proxy: dict): def __init__(self, ipv6: bool, proxy: dict):
super().__init__(proxy) super().__init__(ipv6, proxy)
def connect(self, address: tuple): def connect(self, address: tuple):
super().connect(address) super().connect(address)

View File

@ -28,8 +28,9 @@ log = logging.getLogger(__name__)
class TCPAbridgedO(TCP): class TCPAbridgedO(TCP):
RESERVED = (b"HEAD", b"POST", b"GET ", b"OPTI", b"\xee" * 4) RESERVED = (b"HEAD", b"POST", b"GET ", b"OPTI", b"\xee" * 4)
def __init__(self, proxy: dict): def __init__(self, ipv6: bool, proxy: dict):
super().__init__(proxy) super().__init__(ipv6, proxy)
self.encrypt = None self.encrypt = None
self.decrypt = None self.decrypt = None

View File

@ -26,8 +26,9 @@ log = logging.getLogger(__name__)
class TCPFull(TCP): class TCPFull(TCP):
def __init__(self, proxy: dict): def __init__(self, ipv6: bool, proxy: dict):
super().__init__(proxy) super().__init__(ipv6, proxy)
self.seq_no = None self.seq_no = None
def connect(self, address: tuple): def connect(self, address: tuple):

View File

@ -25,8 +25,8 @@ log = logging.getLogger(__name__)
class TCPIntermediate(TCP): class TCPIntermediate(TCP):
def __init__(self, proxy: dict): def __init__(self, ipv6: bool, proxy: dict):
super().__init__(proxy) super().__init__(ipv6, proxy)
def connect(self, address: tuple): def connect(self, address: tuple):
super().connect(address) super().connect(address)

View File

@ -29,8 +29,9 @@ log = logging.getLogger(__name__)
class TCPIntermediateO(TCP): class TCPIntermediateO(TCP):
RESERVED = (b"HEAD", b"POST", b"GET ", b"OPTI", b"\xee" * 4) RESERVED = (b"HEAD", b"POST", b"GET ", b"OPTI", b"\xee" * 4)
def __init__(self, proxy: dict): def __init__(self, ipv6: bool, proxy: dict):
super().__init__(proxy) super().__init__(ipv6, proxy)
self.encrypt = None self.encrypt = None
self.decrypt = None self.decrypt = None

View File

@ -26,7 +26,7 @@ from pyrogram.api import functions, types
from pyrogram.api.core import Object, Long, Int from pyrogram.api.core import Object, Long, Int
from pyrogram.connection import Connection from pyrogram.connection import Connection
from pyrogram.crypto import AES, RSA, Prime from pyrogram.crypto import AES, RSA, Prime
from .internals import MsgId, DataCenter from .internals import MsgId
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -46,9 +46,10 @@ class Auth:
16 16
) )
def __init__(self, dc_id: int, test_mode: bool, proxy: dict): def __init__(self, dc_id: int, test_mode: bool, ipv6: bool, proxy: dict):
self.dc_id = dc_id self.dc_id = dc_id
self.test_mode = test_mode self.test_mode = test_mode
self.ipv6 = ipv6
self.proxy = proxy self.proxy = proxy
self.connection = None self.connection = None
@ -84,7 +85,7 @@ class Auth:
# The server may close the connection at any time, causing the auth key creation to fail. # The server may close the connection at any time, causing the auth key creation to fail.
# If that happens, just try again up to MAX_RETRIES times. # If that happens, just try again up to MAX_RETRIES times.
while True: while True:
self.connection = Connection(DataCenter(self.dc_id, self.test_mode), self.proxy) self.connection = Connection(self.dc_id, self.test_mode, self.ipv6, self.proxy)
try: try:
log.info("Start creating a new auth key on DC{}".format(self.dc_id)) log.info("Start creating a new auth key on DC{}".format(self.dc_id))

View File

@ -34,5 +34,24 @@ class DataCenter:
121: "95.213.217.195" 121: "95.213.217.195"
} }
def __new__(cls, dc_id: int, test_mode: bool): TEST_IPV6 = {
return (cls.TEST[dc_id], 80) if test_mode else (cls.PROD[dc_id], 443) 1: "2001:0b28:f23d:f001:0000:0000:0000:000e",
2: "2001:067c:04e8:f002:0000:0000:0000:000e",
3: "2001:0b28:f23d:f003:0000:0000:0000:000e",
121: "2a03:b0c0:0003:00d0:0000:0000:0114:d001"
}
PROD_IPV6 = {
1: "2001:0b28:f23d:f001:0000:0000:0000:000a",
2: "2001:067c:04e8:f002:0000:0000:0000:000a",
3: "2001:0b28:f23d:f003:0000:0000:0000:000a",
4: "2001:067c:04e8:f004:0000:0000:0000:000a",
5: "2001:0b28:f23f:f005:0000:0000:0000:000a",
121: "2a03:b0c0:0003:00d0:0000:0000:0114:d001"
}
def __new__(cls, dc_id: int, test_mode: bool, ipv6: bool):
if ipv6:
return (cls.TEST_IPV6[dc_id], 80) if test_mode else (cls.PROD_IPV6[dc_id], 443)
else:
return (cls.TEST[dc_id], 80) if test_mode else (cls.PROD[dc_id], 443)

View File

@ -34,7 +34,7 @@ from pyrogram.api.core import Message, Object, MsgContainer, Long, FutureSalt, I
from pyrogram.api.errors import Error, InternalServerError, AuthKeyDuplicated from pyrogram.api.errors import Error, InternalServerError, AuthKeyDuplicated
from pyrogram.connection import Connection from pyrogram.connection import Connection
from pyrogram.crypto import AES, KDF from pyrogram.crypto import AES, KDF
from .internals import MsgId, MsgFactory, DataCenter from .internals import MsgId, MsgFactory
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -112,7 +112,7 @@ class Session:
def start(self): def start(self):
while True: while True:
self.connection = Connection(DataCenter(self.dc_id, self.client.test_mode), self.client.proxy) self.connection = Connection(self.dc_id, self.client.test_mode, self.client.ipv6, self.client.proxy)
try: try:
self.connection.connect() self.connection.connect()