From 8ac48c555c027f76978c5339b19be48f48518aa5 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Wed, 13 Jun 2018 13:34:56 +0200 Subject: [PATCH 01/22] Add ipv6 data center addresses --- pyrogram/session/internals/data_center.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/pyrogram/session/internals/data_center.py b/pyrogram/session/internals/data_center.py index 232ca13b..154cd9e0 100644 --- a/pyrogram/session/internals/data_center.py +++ b/pyrogram/session/internals/data_center.py @@ -34,5 +34,24 @@ class DataCenter: 121: "95.213.217.195" } - def __new__(cls, dc_id: int, test_mode: bool): - return (cls.TEST[dc_id], 80) if test_mode else (cls.PROD[dc_id], 443) + TEST_IPV6 = { + 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) From efe26bcb196f4fe6a9a6c4db385d48eba47c6593 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Wed, 13 Jun 2018 13:35:41 +0200 Subject: [PATCH 02/22] Allow Connection to connect to ipv6 addresses --- pyrogram/connection/connection.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/pyrogram/connection/connection.py b/pyrogram/connection/connection.py index a53295ce..043f7cb7 100644 --- a/pyrogram/connection/connection.py +++ b/pyrogram/connection/connection.py @@ -21,6 +21,7 @@ import threading import time from .transport import * +from ..session.internals import DataCenter log = logging.getLogger(__name__) @@ -36,16 +37,18 @@ class Connection: 4: TCPIntermediateO } - def __init__(self, address: tuple, proxy: dict, mode: int = 1): - self.address = address + def __init__(self, dc_id: int, test_mode: bool, ipv6: bool, proxy: dict, mode: int = 1): + self.ipv6 = ipv6 self.proxy = proxy + self.address = DataCenter(dc_id, test_mode, ipv6) self.mode = self.MODES.get(mode, TCPAbridged) + self.lock = threading.Lock() self.connection = None def connect(self): for i in range(Connection.MAX_RETRIES): - self.connection = self.mode(self.proxy) + self.connection = self.mode(self.ipv6, self.proxy) try: log.info("Connecting...") From 56748ff39075b41c427987127f975a9d672ee999 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Wed, 13 Jun 2018 13:36:26 +0200 Subject: [PATCH 03/22] Make the underlying TCP protocol accept ipv6 addresses --- pyrogram/connection/transport/tcp/tcp.py | 5 +++-- pyrogram/connection/transport/tcp/tcp_abridged.py | 4 ++-- pyrogram/connection/transport/tcp/tcp_abridged_o.py | 5 +++-- pyrogram/connection/transport/tcp/tcp_full.py | 5 +++-- pyrogram/connection/transport/tcp/tcp_intermediate.py | 4 ++-- pyrogram/connection/transport/tcp/tcp_intermediate_o.py | 5 +++-- 6 files changed, 16 insertions(+), 12 deletions(-) diff --git a/pyrogram/connection/transport/tcp/tcp.py b/pyrogram/connection/transport/tcp/tcp.py index 5df8aacb..8560604f 100644 --- a/pyrogram/connection/transport/tcp/tcp.py +++ b/pyrogram/connection/transport/tcp/tcp.py @@ -33,8 +33,9 @@ log = logging.getLogger(__name__) class TCP(socks.socksocket): - def __init__(self, proxy: dict): - super().__init__() + def __init__(self, ipv6: bool, proxy: dict): + super().__init__(family=socket.AF_INET6 if ipv6 else socket.AF_INET) + self.settimeout(10) self.proxy_enabled = proxy.get("enabled", False) diff --git a/pyrogram/connection/transport/tcp/tcp_abridged.py b/pyrogram/connection/transport/tcp/tcp_abridged.py index 472f4799..d89421f5 100644 --- a/pyrogram/connection/transport/tcp/tcp_abridged.py +++ b/pyrogram/connection/transport/tcp/tcp_abridged.py @@ -24,8 +24,8 @@ log = logging.getLogger(__name__) class TCPAbridged(TCP): - def __init__(self, proxy: dict): - super().__init__(proxy) + def __init__(self, ipv6: bool, proxy: dict): + super().__init__(ipv6, proxy) def connect(self, address: tuple): super().connect(address) diff --git a/pyrogram/connection/transport/tcp/tcp_abridged_o.py b/pyrogram/connection/transport/tcp/tcp_abridged_o.py index bba88e34..57cc0336 100644 --- a/pyrogram/connection/transport/tcp/tcp_abridged_o.py +++ b/pyrogram/connection/transport/tcp/tcp_abridged_o.py @@ -28,8 +28,9 @@ log = logging.getLogger(__name__) class TCPAbridgedO(TCP): RESERVED = (b"HEAD", b"POST", b"GET ", b"OPTI", b"\xee" * 4) - def __init__(self, proxy: dict): - super().__init__(proxy) + def __init__(self, ipv6: bool, proxy: dict): + super().__init__(ipv6, proxy) + self.encrypt = None self.decrypt = None diff --git a/pyrogram/connection/transport/tcp/tcp_full.py b/pyrogram/connection/transport/tcp/tcp_full.py index 1b131678..0aac1a14 100644 --- a/pyrogram/connection/transport/tcp/tcp_full.py +++ b/pyrogram/connection/transport/tcp/tcp_full.py @@ -26,8 +26,9 @@ log = logging.getLogger(__name__) class TCPFull(TCP): - def __init__(self, proxy: dict): - super().__init__(proxy) + def __init__(self, ipv6: bool, proxy: dict): + super().__init__(ipv6, proxy) + self.seq_no = None def connect(self, address: tuple): diff --git a/pyrogram/connection/transport/tcp/tcp_intermediate.py b/pyrogram/connection/transport/tcp/tcp_intermediate.py index 4b2e2596..b33fe466 100644 --- a/pyrogram/connection/transport/tcp/tcp_intermediate.py +++ b/pyrogram/connection/transport/tcp/tcp_intermediate.py @@ -25,8 +25,8 @@ log = logging.getLogger(__name__) class TCPIntermediate(TCP): - def __init__(self, proxy: dict): - super().__init__(proxy) + def __init__(self, ipv6: bool, proxy: dict): + super().__init__(ipv6, proxy) def connect(self, address: tuple): super().connect(address) diff --git a/pyrogram/connection/transport/tcp/tcp_intermediate_o.py b/pyrogram/connection/transport/tcp/tcp_intermediate_o.py index df79352a..e8c96ebc 100644 --- a/pyrogram/connection/transport/tcp/tcp_intermediate_o.py +++ b/pyrogram/connection/transport/tcp/tcp_intermediate_o.py @@ -29,8 +29,9 @@ log = logging.getLogger(__name__) class TCPIntermediateO(TCP): RESERVED = (b"HEAD", b"POST", b"GET ", b"OPTI", b"\xee" * 4) - def __init__(self, proxy: dict): - super().__init__(proxy) + def __init__(self, ipv6: bool, proxy: dict): + super().__init__(ipv6, proxy) + self.encrypt = None self.decrypt = None From c9469ed5428ed0a6495a986a9f9afd26b214001a Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Wed, 13 Jun 2018 13:37:12 +0200 Subject: [PATCH 04/22] Allow auth to use ipv6 --- pyrogram/session/auth.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pyrogram/session/auth.py b/pyrogram/session/auth.py index 80956187..2142be59 100644 --- a/pyrogram/session/auth.py +++ b/pyrogram/session/auth.py @@ -26,7 +26,7 @@ from pyrogram.api import functions, types from pyrogram.api.core import Object, Long, Int from pyrogram.connection import Connection from pyrogram.crypto import AES, RSA, Prime -from .internals import MsgId, DataCenter +from .internals import MsgId log = logging.getLogger(__name__) @@ -46,9 +46,10 @@ class Auth: 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.test_mode = test_mode + self.ipv6 = ipv6 self.proxy = proxy 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. # If that happens, just try again up to MAX_RETRIES times. 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: log.info("Start creating a new auth key on DC{}".format(self.dc_id)) From b804709c6c6a88f7b95d3db62c2a231ec7c9591d Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Wed, 13 Jun 2018 13:37:35 +0200 Subject: [PATCH 05/22] Allow session to use ipv6 --- pyrogram/session/session.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pyrogram/session/session.py b/pyrogram/session/session.py index 7e90cfff..82751f21 100644 --- a/pyrogram/session/session.py +++ b/pyrogram/session/session.py @@ -35,7 +35,7 @@ from pyrogram.api.core import Message, Object, MsgContainer, Long, FutureSalt, I from pyrogram.api.errors import Error, InternalServerError from pyrogram.connection import Connection from pyrogram.crypto import AES, KDF -from .internals import MsgId, MsgFactory, DataCenter +from .internals import MsgId, MsgFactory log = logging.getLogger(__name__) @@ -86,6 +86,7 @@ class Session: def __init__(self, dc_id: int, test_mode: bool, + ipv6: bool, proxy: dict, auth_key: bytes, api_id: int, @@ -98,6 +99,7 @@ class Session: self.dc_id = dc_id self.test_mode = test_mode + self.ipv6 = ipv6 self.proxy = proxy self.api_id = api_id self.is_cdn = is_cdn @@ -130,7 +132,7 @@ class Session: def start(self): 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: self.connection.connect() From ade1c2f377902c3c2b6c1f4e406bc21a3acd7e4f Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Wed, 13 Jun 2018 13:38:14 +0200 Subject: [PATCH 06/22] Accommodate ipv6 in the Client class --- pyrogram/client/client.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/pyrogram/client/client.py b/pyrogram/client/client.py index 73f45966..65490a55 100644 --- a/pyrogram/client/client.py +++ b/pyrogram/client/client.py @@ -129,6 +129,7 @@ class Client(Methods, BaseClient): session_name: str, api_id: int or str = None, api_hash: str = None, + ipv6: bool = False, proxy: dict = None, test_mode: bool = False, phone_number: str = None, @@ -145,6 +146,7 @@ class Client(Methods, BaseClient): self.session_name = session_name self.api_id = int(api_id) if api_id else None self.api_hash = api_hash + self.ipv6 = ipv6 # TODO: Make code consistent, use underscore for private/protected fields self._proxy = proxy self.test_mode = test_mode @@ -194,6 +196,7 @@ class Client(Methods, BaseClient): self.session = Session( self.dc_id, self.test_mode, + self.ipv6, self._proxy, self.auth_key, self.api_id, @@ -342,11 +345,12 @@ class Client(Methods, BaseClient): self.session.stop() 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.dc_id, self.test_mode, + self.ipv6, self._proxy, self.auth_key, self.api_id, @@ -390,11 +394,12 @@ class Client(Methods, BaseClient): self.session.stop() 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.dc_id, self.test_mode, + self.ipv6, self._proxy, self.auth_key, self.api_id, @@ -871,7 +876,7 @@ class Client(Methods, BaseClient): except FileNotFoundError: self.dc_id = 1 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: self.dc_id = s["dc_id"] self.test_mode = s["test_mode"] @@ -1022,7 +1027,7 @@ class Client(Methods, BaseClient): file_id = file_id or self.rnd_id() md5_sum = md5() if not is_big and not is_missing_part else None - session = Session(self.dc_id, self.test_mode, self._proxy, self.auth_key, self.api_id) + session = Session(self.dc_id, self.test_mode, self.ipv6, self._proxy, self.auth_key, self.api_id) session.start() try: @@ -1108,8 +1113,9 @@ class Client(Methods, BaseClient): session = Session( dc_id, self.test_mode, + self.ipv6, self._proxy, - Auth(dc_id, self.test_mode, self._proxy).create(), + Auth(dc_id, self.test_mode, self.ipv6, self._proxy).create(), self.api_id ) @@ -1127,6 +1133,7 @@ class Client(Methods, BaseClient): session = Session( dc_id, self.test_mode, + self.ipv6, self._proxy, self.auth_key, self.api_id @@ -1197,8 +1204,9 @@ class Client(Methods, BaseClient): cdn_session = Session( r.dc_id, self.test_mode, + self.ipv6, self._proxy, - Auth(r.dc_id, self.test_mode, self._proxy).create(), + Auth(r.dc_id, self.test_mode, self.ipv6, self._proxy).create(), self.api_id, is_cdn=True ) From d38d23f46df7fb03aeefff9938c0b80e3000a065 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Wed, 13 Jun 2018 13:39:06 +0200 Subject: [PATCH 07/22] Log in case connection fails (to test ipv6) --- pyrogram/connection/connection.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pyrogram/connection/connection.py b/pyrogram/connection/connection.py index 043f7cb7..ca9bd96c 100644 --- a/pyrogram/connection/connection.py +++ b/pyrogram/connection/connection.py @@ -53,7 +53,8 @@ class Connection: try: log.info("Connecting...") self.connection.connect(self.address) - except OSError: + except OSError as e: + log.warning(e) # TODO: Remove self.connection.close() time.sleep(1) else: From 2779e33d1351a461ce3fc1d0f8a692360e4cc8e1 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Mon, 27 Aug 2018 02:11:07 +0200 Subject: [PATCH 08/22] Rename "token" to "bot_token" --- pyrogram/client/client.py | 8 ++++---- pyrogram/client/ext/base_client.py | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pyrogram/client/client.py b/pyrogram/client/client.py index 3af60931..dba204fd 100644 --- a/pyrogram/client/client.py +++ b/pyrogram/client/client.py @@ -201,7 +201,7 @@ class Client(Methods, BaseClient): raise ConnectionError("Client has already been started") if self.BOT_TOKEN_RE.match(self.session_name): - self.token = self.session_name + self.bot_token = self.session_name self.session_name = self.session_name.split(":")[0] self.load_config() @@ -217,14 +217,14 @@ class Client(Methods, BaseClient): self.is_started = True if self.user_id is None: - if self.token is None: + if self.bot_token is None: self.authorize_user() else: self.authorize_bot() self.save_session() - if self.token is None: + if self.bot_token is None: now = time.time() if abs(now - self.date) > Client.OFFLINE_SLEEP: @@ -381,7 +381,7 @@ class Client(Methods, BaseClient): flags=0, api_id=self.api_id, api_hash=self.api_hash, - bot_auth_token=self.token + bot_auth_token=self.bot_token ) ) except UserMigrate as e: diff --git a/pyrogram/client/ext/base_client.py b/pyrogram/client/ext/base_client.py index c78997c1..fa96e1db 100644 --- a/pyrogram/client/ext/base_client.py +++ b/pyrogram/client/ext/base_client.py @@ -64,7 +64,7 @@ class BaseClient: } def __init__(self): - self.token = None + self.bot_token = None self.dc_id = None self.auth_key = None self.user_id = None From 973ccfcd033b4d11c7fe18a2698da0f064a3306b Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Tue, 28 Aug 2018 12:23:22 +0200 Subject: [PATCH 09/22] Use compact IPv6 addresses --- pyrogram/session/internals/data_center.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/pyrogram/session/internals/data_center.py b/pyrogram/session/internals/data_center.py index 154cd9e0..154ba4e5 100644 --- a/pyrogram/session/internals/data_center.py +++ b/pyrogram/session/internals/data_center.py @@ -35,19 +35,19 @@ class DataCenter: } TEST_IPV6 = { - 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" + 1: "2001:b28:f23d:f001::e", + 2: "2001:67c:4e8:f002::e", + 3: "2001:b28:f23d:f003::e", + 121: "2a03:b0c0:3:d0::114: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" + 1: "2001:b28:f23d:f001::a", + 2: "2001:67c:4e8:f002::a", + 3: "2001:b28:f23d:f003::a", + 4: "2001:67c:4e8:f004::a", + 5: "2001:b28:f23f:f005::a", + 121: "2a03:b0c0:3:d0::114:d001" } def __new__(cls, dc_id: int, test_mode: bool, ipv6: bool): From a0c1018a1bcf63201fb3b0676b6e3aea2eeca4c3 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Tue, 28 Aug 2018 12:25:08 +0200 Subject: [PATCH 10/22] Minor style fix --- pyrogram/session/internals/data_center.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/pyrogram/session/internals/data_center.py b/pyrogram/session/internals/data_center.py index 154ba4e5..d36e0613 100644 --- a/pyrogram/session/internals/data_center.py +++ b/pyrogram/session/internals/data_center.py @@ -52,6 +52,14 @@ class DataCenter: 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) + 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) + return ( + (cls.TEST[dc_id], 80) + if test_mode + else (cls.PROD[dc_id], 443) + ) From f50638772cebc5bd1dad2b01de8ae6a1b58382e6 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Tue, 28 Aug 2018 12:38:02 +0200 Subject: [PATCH 11/22] Minor fix: update info log --- pyrogram/session/session.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyrogram/session/session.py b/pyrogram/session/session.py index c87f0698..f0387002 100644 --- a/pyrogram/session/session.py +++ b/pyrogram/session/session.py @@ -156,7 +156,7 @@ class Session: self.ping_thread = Thread(target=self.ping, name="PingThread") self.ping_thread.start() - log.info("Connection inited: Layer {}".format(layer)) + log.info("Session initialized: Layer {}".format(layer)) except AuthKeyDuplicated as e: self.stop() raise e From b845544e638eb7b6e96c74154af5052d1300a51d Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Tue, 28 Aug 2018 12:39:14 +0200 Subject: [PATCH 12/22] Info log device and system parameters --- pyrogram/session/session.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pyrogram/session/session.py b/pyrogram/session/session.py index f0387002..4ca32733 100644 --- a/pyrogram/session/session.py +++ b/pyrogram/session/session.py @@ -157,6 +157,9 @@ class Session: self.ping_thread.start() log.info("Session initialized: Layer {}".format(layer)) + log.info("Device: {} - {}".format(self.client.device_model, self.client.app_version)) + log.info("System: {} ({})".format(self.client.system_version, self.client.lang_code.upper())) + except AuthKeyDuplicated as e: self.stop() raise e From 49f2cbe04f1d41d8a62cc6d1981c28109080f574 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Wed, 29 Aug 2018 22:04:04 +0200 Subject: [PATCH 13/22] Move CURRENT_DH_PRIME into Prime namespace --- pyrogram/crypto/prime.py | 12 ++++++++++++ pyrogram/session/auth.py | 14 +------------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/pyrogram/crypto/prime.py b/pyrogram/crypto/prime.py index 9338c206..8e9426ca 100644 --- a/pyrogram/crypto/prime.py +++ b/pyrogram/crypto/prime.py @@ -20,6 +20,18 @@ from random import randint class Prime: + CURRENT_DH_PRIME = int( + "C71CAEB9C6B1C9048E6C522F70F13F73980D40238E3E21C14934D037563D930F" + "48198A0AA7C14058229493D22530F4DBFA336F6E0AC925139543AED44CCE7C37" + "20FD51F69458705AC68CD4FE6B6B13ABDC9746512969328454F18FAF8C595F64" + "2477FE96BB2A941D5BCD1D4AC8CC49880708FA9B378E3C4F3A9060BEE67CF9A4" + "A4A695811051907E162753B56B0F6B410DBA74D8A84B2A14B3144E0EF1284754" + "FD17ED950D5965B4B9DD46582DB1178D169C6BC465B0D6FF9CA3928FEF5B9AE4" + "E418FC15E83EBEA0F87FA9FF5EED70050DED2849F47BF959D956850CE929851F" + "0D8115F635B105EE2E4E15D04B2454BF6F4FADF034B10403119CD8E3B92FCC5B", + 16 + ) + # Recursive variant # @classmethod # def gcd(cls, a: int, b: int) -> int: diff --git a/pyrogram/session/auth.py b/pyrogram/session/auth.py index 2142be59..a545a642 100644 --- a/pyrogram/session/auth.py +++ b/pyrogram/session/auth.py @@ -34,18 +34,6 @@ log = logging.getLogger(__name__) class Auth: MAX_RETRIES = 5 - CURRENT_DH_PRIME = int( - "C71CAEB9C6B1C9048E6C522F70F13F73980D40238E3E21C14934D037563D930F" - "48198A0AA7C14058229493D22530F4DBFA336F6E0AC925139543AED44CCE7C37" - "20FD51F69458705AC68CD4FE6B6B13ABDC9746512969328454F18FAF8C595F64" - "2477FE96BB2A941D5BCD1D4AC8CC49880708FA9B378E3C4F3A9060BEE67CF9A4" - "A4A695811051907E162753B56B0F6B410DBA74D8A84B2A14B3144E0EF1284754" - "FD17ED950D5965B4B9DD46582DB1178D169C6BC465B0D6FF9CA3928FEF5B9AE4" - "E418FC15E83EBEA0F87FA9FF5EED70050DED2849F47BF959D956850CE929851F" - "0D8115F635B105EE2E4E15D04B2454BF6F4FADF034B10403119CD8E3B92FCC5B", - 16 - ) - def __init__(self, dc_id: int, test_mode: bool, ipv6: bool, proxy: dict): self.dc_id = dc_id self.test_mode = test_mode @@ -220,7 +208,7 @@ class Auth: # Security checks ####################### - assert dh_prime == self.CURRENT_DH_PRIME + assert dh_prime == Prime.CURRENT_DH_PRIME log.debug("DH parameters check: OK") # https://core.telegram.org/mtproto/security_guidelines#g-a-and-g-b-validation From ef9fc969d3334c8c97c1cdd203055e00093c3707 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Wed, 29 Aug 2018 22:20:00 +0200 Subject: [PATCH 14/22] Info log connection settings --- pyrogram/connection/connection.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pyrogram/connection/connection.py b/pyrogram/connection/connection.py index ca9bd96c..b9d4cc87 100644 --- a/pyrogram/connection/connection.py +++ b/pyrogram/connection/connection.py @@ -58,6 +58,10 @@ class Connection: self.connection.close() time.sleep(1) else: + log.info("Connected! IPv{} - {}".format( + "6" if self.ipv6 else "4", + self.mode.__name__ + )) break else: raise TimeoutError From 43483a1ccdf46644901a8795dd2d03c6ed55e442 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Wed, 29 Aug 2018 22:20:32 +0200 Subject: [PATCH 15/22] Remove info logs from protocol impls --- pyrogram/connection/transport/tcp/tcp_abridged.py | 2 -- pyrogram/connection/transport/tcp/tcp_abridged_o.py | 2 -- pyrogram/connection/transport/tcp/tcp_full.py | 1 - pyrogram/connection/transport/tcp/tcp_intermediate.py | 2 -- pyrogram/connection/transport/tcp/tcp_intermediate_o.py | 2 -- 5 files changed, 9 deletions(-) diff --git a/pyrogram/connection/transport/tcp/tcp_abridged.py b/pyrogram/connection/transport/tcp/tcp_abridged.py index d89421f5..5566b179 100644 --- a/pyrogram/connection/transport/tcp/tcp_abridged.py +++ b/pyrogram/connection/transport/tcp/tcp_abridged.py @@ -31,8 +31,6 @@ class TCPAbridged(TCP): super().connect(address) super().sendall(b"\xef") - log.info("Connected{}!".format(" with proxy" if self.proxy_enabled else "")) - def sendall(self, data: bytes, *args): length = len(data) // 4 diff --git a/pyrogram/connection/transport/tcp/tcp_abridged_o.py b/pyrogram/connection/transport/tcp/tcp_abridged_o.py index 57cc0336..91ee8375 100644 --- a/pyrogram/connection/transport/tcp/tcp_abridged_o.py +++ b/pyrogram/connection/transport/tcp/tcp_abridged_o.py @@ -55,8 +55,6 @@ class TCPAbridgedO(TCP): super().sendall(nonce) - log.info("Connected{}!".format(" with proxy" if self.proxy_enabled else "")) - def sendall(self, data: bytes, *args): length = len(data) // 4 diff --git a/pyrogram/connection/transport/tcp/tcp_full.py b/pyrogram/connection/transport/tcp/tcp_full.py index 0aac1a14..8704247b 100644 --- a/pyrogram/connection/transport/tcp/tcp_full.py +++ b/pyrogram/connection/transport/tcp/tcp_full.py @@ -34,7 +34,6 @@ class TCPFull(TCP): def connect(self, address: tuple): super().connect(address) self.seq_no = 0 - log.info("Connected{}!".format(" with proxy" if self.proxy_enabled else "")) def sendall(self, data: bytes, *args): # 12 = packet_length (4), seq_no (4), crc32 (4) (at the end) diff --git a/pyrogram/connection/transport/tcp/tcp_intermediate.py b/pyrogram/connection/transport/tcp/tcp_intermediate.py index b33fe466..aa198db7 100644 --- a/pyrogram/connection/transport/tcp/tcp_intermediate.py +++ b/pyrogram/connection/transport/tcp/tcp_intermediate.py @@ -32,8 +32,6 @@ class TCPIntermediate(TCP): super().connect(address) super().sendall(b"\xee" * 4) - log.info("Connected{}!".format(" with proxy" if self.proxy_enabled else "")) - def sendall(self, data: bytes, *args): super().sendall(pack(" Date: Thu, 30 Aug 2018 01:17:13 +0200 Subject: [PATCH 16/22] Rename "build" to "generate" It was interfering with the built-in command "install" --- setup.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/setup.py b/setup.py index 5df52747..a7a1458a 100644 --- a/setup.py +++ b/setup.py @@ -114,12 +114,12 @@ class Clean(Command): print("removing {}".format(path)) -class Build(Command): - description = "Build Pyrogram files" +class Generate(Command): + description = "Generate Pyrogram files" user_options = [ - ("api", None, "Build API files"), - ("docs", None, "Build docs files"), + ("api", None, "Generate API files"), + ("docs", None, "Generate docs files") ] def __init__(self, dist, **kw): @@ -191,6 +191,6 @@ setup( extras_require={"tgcrypto": ["tgcrypto>=1.0.4"]}, cmdclass={ "clean": Clean, - "build": Build, + "generate": Generate } ) From ca15778ac893214db59d521381364e5a5fdb3515 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Thu, 30 Aug 2018 01:17:35 +0200 Subject: [PATCH 17/22] Make clean default to all --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index a7a1458a..76f1f7a9 100644 --- a/setup.py +++ b/setup.py @@ -102,7 +102,7 @@ class Clean(Command): if self.docs: paths.update(Clean.DOCS) - if self.all: + if self.all or not paths: paths.update(Clean.ALL) for path in sorted(list(paths)): From 2a78dff79cb75769465fe2e10541e4377fc28789 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Thu, 30 Aug 2018 01:17:51 +0200 Subject: [PATCH 18/22] Minor style fixes --- setup.py | 22 +++------------------- 1 file changed, 3 insertions(+), 19 deletions(-) diff --git a/setup.py b/setup.py index 76f1f7a9..b8d5c274 100644 --- a/setup.py +++ b/setup.py @@ -46,25 +46,9 @@ def get_readme(): class Clean(Command): - DIST = [ - "./build", - "./dist", - "./Pyrogram.egg-info" - ] - - API = [ - "pyrogram/api/errors/exceptions", - "pyrogram/api/functions", - "pyrogram/api/types", - "pyrogram/api/all.py", - ] - - DOCS = [ - "docs/source/functions", - "docs/source/types", - "docs/build" - ] - + DIST = ["./build", "./dist", "./Pyrogram.egg-info"] + API = ["pyrogram/api/errors/exceptions", "pyrogram/api/functions", "pyrogram/api/types", "pyrogram/api/all.py"] + DOCS = ["docs/source/functions", "docs/source/types", "docs/build"] ALL = DIST + API + DOCS description = "Clean generated files" From 9f4d7854e8f91ef79562e6566085e3ea6bc0e735 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Thu, 30 Aug 2018 01:23:19 +0200 Subject: [PATCH 19/22] Fix ipv6 branch merge mess --- pyrogram/client/client.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pyrogram/client/client.py b/pyrogram/client/client.py index 7d195e01..e9be7a4e 100644 --- a/pyrogram/client/client.py +++ b/pyrogram/client/client.py @@ -1074,7 +1074,7 @@ class Client(Methods, BaseClient): file_id = file_id or self.rnd_id() md5_sum = md5() if not is_big and not is_missing_part else None - session = Session(self, self.ipv6, self.dc_id, self.auth_key, is_media=True) + session = Session(self, self.dc_id, self.auth_key, is_media=True) session.start() try: @@ -1160,7 +1160,7 @@ class Client(Methods, BaseClient): session = Session( self, dc_id, - Auth(dc_id, self.test_mode, self._proxy).create(), + Auth(dc_id, self.test_mode, self.ipv6, self._proxy).create(), is_media=True ) @@ -1245,7 +1245,7 @@ class Client(Methods, BaseClient): cdn_session = Session( self, r.dc_id, - Auth(r.dc_id, self.test_mode, self._proxy).create(), + Auth(r.dc_id, self.test_mode, self.ipv6, self._proxy).create(), is_media=True, is_cdn=True ) From a5979a3ac7b1a3bef249ab111642dafe1c57a026 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Fri, 31 Aug 2018 12:56:05 +0200 Subject: [PATCH 20/22] Update tgcrypto function names --- pyrogram/crypto/aes.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pyrogram/crypto/aes.py b/pyrogram/crypto/aes.py index e4fb94b1..065a5b4d 100644 --- a/pyrogram/crypto/aes.py +++ b/pyrogram/crypto/aes.py @@ -30,19 +30,19 @@ try: # TODO: Use new tgcrypto function names @classmethod def ige256_encrypt(cls, data: bytes, key: bytes, iv: bytes) -> bytes: - return tgcrypto.ige_encrypt(data, key, iv) + return tgcrypto.ige256_encrypt(data, key, iv) @classmethod def ige256_decrypt(cls, data: bytes, key: bytes, iv: bytes) -> bytes: - return tgcrypto.ige_decrypt(data, key, iv) + return tgcrypto.ige256_decrypt(data, key, iv) @staticmethod def ctr256_encrypt(data: bytes, key: bytes, iv: bytearray, state: bytearray = None) -> bytes: - return tgcrypto.ctr_encrypt(data, key, iv, state or bytearray(1)) + return tgcrypto.ctr256_encrypt(data, key, iv, state or bytearray(1)) @staticmethod def ctr256_decrypt(data: bytes, key: bytes, iv: bytearray, state: bytearray = None) -> bytes: - return tgcrypto.ctr_decrypt(data, key, iv, state or bytearray(1)) + return tgcrypto.ctr256_decrypt(data, key, iv, state or bytearray(1)) @staticmethod def xor(a: bytes, b: bytes) -> bytes: From f576fc899dc5765822a01d2af1696db648256c82 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Fri, 31 Aug 2018 13:12:13 +0200 Subject: [PATCH 21/22] Remove TODO --- pyrogram/crypto/aes.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pyrogram/crypto/aes.py b/pyrogram/crypto/aes.py index 065a5b4d..f16688c4 100644 --- a/pyrogram/crypto/aes.py +++ b/pyrogram/crypto/aes.py @@ -27,7 +27,6 @@ try: class AES: - # TODO: Use new tgcrypto function names @classmethod def ige256_encrypt(cls, data: bytes, key: bytes, iv: bytes) -> bytes: return tgcrypto.ige256_encrypt(data, key, iv) From 07a9cce8efe1b2353556298e3351611eaa0007b8 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sat, 1 Sep 2018 00:58:44 +0200 Subject: [PATCH 22/22] Add missing InputMedia types to docs --- docs/source/pyrogram/Types.rst | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/docs/source/pyrogram/Types.rst b/docs/source/pyrogram/Types.rst index 92093b11..e8dc709c 100644 --- a/docs/source/pyrogram/Types.rst +++ b/docs/source/pyrogram/Types.rst @@ -62,6 +62,9 @@ Input Media InputMediaPhoto InputMediaVideo + InputMediaAudio + InputMediaAnimation + InputMediaDocument InputPhoneContact .. User & Chats @@ -172,5 +175,14 @@ Input Media .. autoclass:: InputMediaVideo :members: +.. autoclass:: InputMediaAudio + :members: + +.. autoclass:: InputMediaAnimation + :members: + +.. autoclass:: InputMediaDocument + :members: + .. autoclass:: InputPhoneContact :members: