diff --git a/pyrogram/client/client.py b/pyrogram/client/client.py index 62b09d4e..1895a36f 100644 --- a/pyrogram/client/client.py +++ b/pyrogram/client/client.py @@ -48,6 +48,8 @@ from .methods import Methods from .storage import Storage, FileStorage, MemoryStorage from .types import User, SentCode, TermsOfService +log = logging.getLogger(__name__) + class Client(Methods, BaseClient): """Pyrogram Client, the main means for interacting with Telegram. @@ -340,7 +342,7 @@ class Client(Methods, BaseClient): if self.takeout_id: await self.send(functions.account.FinishTakeoutSession()) - logging.warning("Takeout session {} finished".format(self.takeout_id)) + log.warning("Takeout session {} finished".format(self.takeout_id)) await Syncer.remove(self) await self.dispatcher.stop() @@ -733,7 +735,7 @@ class Client(Methods, BaseClient): print(e.MESSAGE.format(x=e.x)) time.sleep(e.x) except Exception as e: - logging.error(e, exc_info=True) + log.error(e, exc_info=True) raise else: self.password = None @@ -833,7 +835,7 @@ class Client(Methods, BaseClient): if not self.storage.is_bot and self.takeout: self.takeout_id = (await self.send(functions.account.InitTakeoutSession())).id - logging.warning("Takeout session {} initiated".format(self.takeout_id)) + log.warning("Takeout session {} initiated".format(self.takeout_id)) await self.send(functions.updates.GetState()) except (Exception, KeyboardInterrupt): @@ -1273,7 +1275,7 @@ class Client(Methods, BaseClient): os.makedirs(directory, exist_ok=True) shutil.move(temp_file_path, final_file_path) except Exception as e: - logging.error(e, exc_info=True) + log.error(e, exc_info=True) try: os.remove(temp_file_path) @@ -1314,7 +1316,7 @@ class Client(Methods, BaseClient): pts_count = getattr(update, "pts_count", None) if isinstance(update, types.UpdateChannelTooLong): - logging.warning(update) + log.warning(update) if isinstance(update, types.UpdateNewChannelMessage) and is_min: message = update.message @@ -1366,9 +1368,9 @@ class Client(Methods, BaseClient): elif isinstance(updates, types.UpdateShort): self.dispatcher.updates_queue.put_nowait((updates.update, {}, {})) elif isinstance(updates, types.UpdatesTooLong): - logging.info(updates) + log.info(updates) except Exception as e: - logging.error(e, exc_info=True) + log.error(e, exc_info=True) async def send(self, data: TLObject, retries: int = Session.MAX_RETRIES, timeout: float = Session.WAIT_TIMEOUT): """Send raw Telegram queries. @@ -1543,7 +1545,7 @@ class Client(Methods, BaseClient): if isinstance(handler, Handler) and isinstance(group, int): self.add_handler(handler, group) - logging.info('[{}] [LOAD] {}("{}") in group {} from "{}"'.format( + log.info('[{}] [LOAD] {}("{}") in group {} from "{}"'.format( self.session_name, type(handler).__name__, name, group, module_path)) count += 1 @@ -1557,12 +1559,12 @@ class Client(Methods, BaseClient): try: module = import_module(module_path) except ImportError: - logging.warning('[{}] [LOAD] Ignoring non-existent module "{}"'.format( + log.warning('[{}] [LOAD] Ignoring non-existent module "{}"'.format( self.session_name, module_path)) continue if "__path__" in dir(module): - logging.warning('[{}] [LOAD] Ignoring namespace "{}"'.format( + log.warning('[{}] [LOAD] Ignoring namespace "{}"'.format( self.session_name, module_path)) continue @@ -1578,13 +1580,13 @@ class Client(Methods, BaseClient): if isinstance(handler, Handler) and isinstance(group, int): self.add_handler(handler, group) - logging.info('[{}] [LOAD] {}("{}") in group {} from "{}"'.format( + log.info('[{}] [LOAD] {}("{}") in group {} from "{}"'.format( self.session_name, type(handler).__name__, name, group, module_path)) count += 1 except Exception: if warn_non_existent_functions: - logging.warning('[{}] [LOAD] Ignoring non-existent function "{}" from "{}"'.format( + log.warning('[{}] [LOAD] Ignoring non-existent function "{}" from "{}"'.format( self.session_name, name, module_path)) if exclude: @@ -1595,12 +1597,12 @@ class Client(Methods, BaseClient): try: module = import_module(module_path) except ImportError: - logging.warning('[{}] [UNLOAD] Ignoring non-existent module "{}"'.format( + log.warning('[{}] [UNLOAD] Ignoring non-existent module "{}"'.format( self.session_name, module_path)) continue if "__path__" in dir(module): - logging.warning('[{}] [UNLOAD] Ignoring namespace "{}"'.format( + log.warning('[{}] [UNLOAD] Ignoring namespace "{}"'.format( self.session_name, module_path)) continue @@ -1616,20 +1618,20 @@ class Client(Methods, BaseClient): if isinstance(handler, Handler) and isinstance(group, int): self.remove_handler(handler, group) - logging.info('[{}] [UNLOAD] {}("{}") from group {} in "{}"'.format( + log.info('[{}] [UNLOAD] {}("{}") from group {} in "{}"'.format( self.session_name, type(handler).__name__, name, group, module_path)) count -= 1 except Exception: if warn_non_existent_functions: - logging.warning('[{}] [UNLOAD] Ignoring non-existent function "{}" from "{}"'.format( + log.warning('[{}] [UNLOAD] Ignoring non-existent function "{}" from "{}"'.format( self.session_name, name, module_path)) if count > 0: - logging.warning('[{}] Successfully loaded {} plugin{} from "{}"'.format( + log.warning('[{}] Successfully loaded {} plugin{} from "{}"'.format( self.session_name, count, "s" if count > 1 else "", root)) else: - logging.warning('[{}] No plugin loaded from "{}"'.format( + log.warning('[{}] No plugin loaded from "{}"'.format( self.session_name, root)) # def get_initial_dialogs_chunk(self, offset_date: int = 0): @@ -1646,10 +1648,10 @@ class Client(Methods, BaseClient): # ) # ) # except FloodWait as e: - # logging.warning("get_dialogs flood: waiting {} seconds".format(e.x)) + # log.warning("get_dialogs flood: waiting {} seconds".format(e.x)) # time.sleep(e.x) # else: - # logging.info("Total peers: {}".format(self.storage.peers_count)) + # log.info("Total peers: {}".format(self.storage.peers_count)) # return r # # def get_initial_dialogs(self): @@ -1885,7 +1887,7 @@ class Client(Methods, BaseClient): except Client.StopTransmission: raise except Exception as e: - logging.error(e, exc_info=True) + log.error(e, exc_info=True) else: if is_big: return types.InputFileBig( @@ -2117,7 +2119,7 @@ class Client(Methods, BaseClient): raise e except Exception as e: if not isinstance(e, Client.StopTransmission): - logging.error(e, exc_info=True) + log.error(e, exc_info=True) try: os.remove(file_name) diff --git a/pyrogram/client/ext/dispatcher.py b/pyrogram/client/ext/dispatcher.py index cd05f3f9..02654dc1 100644 --- a/pyrogram/client/ext/dispatcher.py +++ b/pyrogram/client/ext/dispatcher.py @@ -34,6 +34,8 @@ from ..handlers import ( UserStatusHandler, RawUpdateHandler, InlineQueryHandler, PollHandler ) +log = logging.getLogger(__name__) + class Dispatcher: NEW_MESSAGE_UPDATES = ( @@ -183,7 +185,7 @@ class Dispatcher: if handler.check(parsed_update): args = (parsed_update,) except Exception as e: - logging.error(e, exc_info=True) + log.error(e, exc_info=True) continue elif isinstance(handler, RawUpdateHandler): @@ -199,10 +201,10 @@ class Dispatcher: except pyrogram.ContinuePropagation: continue except Exception as e: - logging.error(e, exc_info=True) + log.error(e, exc_info=True) break except pyrogram.StopPropagation: pass except Exception as e: - logging.error(e, exc_info=True) + log.error(e, exc_info=True) diff --git a/pyrogram/client/ext/syncer.py b/pyrogram/client/ext/syncer.py index a00d2c70..8b48e6e2 100644 --- a/pyrogram/client/ext/syncer.py +++ b/pyrogram/client/ext/syncer.py @@ -20,6 +20,8 @@ import asyncio import logging import time +log = logging.getLogger(__name__) + class Syncer: INTERVAL = 20 @@ -81,9 +83,9 @@ class Syncer: start = time.time() client.storage.save() except Exception as e: - logging.critical(e, exc_info=True) + log.critical(e, exc_info=True) else: - logging.info('Synced "{}" in {:.6} ms'.format( + log.info('Synced "{}" in {:.6} ms'.format( client.storage.name, (time.time() - start) * 1000 )) diff --git a/pyrogram/client/methods/chats/get_chat_members.py b/pyrogram/client/methods/chats/get_chat_members.py index d2c31c52..63737c92 100644 --- a/pyrogram/client/methods/chats/get_chat_members.py +++ b/pyrogram/client/methods/chats/get_chat_members.py @@ -23,9 +23,10 @@ from typing import Union, List import pyrogram from pyrogram.api import functions, types from pyrogram.errors import FloodWait - from ...ext import BaseClient +log = logging.getLogger(__name__) + class Filters: ALL = "all" @@ -152,7 +153,7 @@ class GetChatMembers(BaseClient): return pyrogram.List(pyrogram.ChatMember._parse(self, member, users) for member in members) except FloodWait as e: - logging.warning("Sleeping for {}s".format(e.x)) + log.warning("Sleeping for {}s".format(e.x)) await asyncio.sleep(e.x) else: raise ValueError("The chat_id \"{}\" belongs to a user".format(chat_id)) diff --git a/pyrogram/client/methods/chats/get_dialogs.py b/pyrogram/client/methods/chats/get_dialogs.py index af703615..7c774dfc 100644 --- a/pyrogram/client/methods/chats/get_dialogs.py +++ b/pyrogram/client/methods/chats/get_dialogs.py @@ -23,9 +23,10 @@ from typing import List import pyrogram from pyrogram.api import functions, types from pyrogram.errors import FloodWait - from ...ext import BaseClient, utils +log = logging.getLogger(__name__) + class GetDialogs(BaseClient): async def get_dialogs( @@ -81,7 +82,7 @@ class GetDialogs(BaseClient): ) ) except FloodWait as e: - logging.warning("Sleeping for {}s".format(e.x)) + log.warning("Sleeping for {}s".format(e.x)) await asyncio.sleep(e.x) else: break @@ -110,6 +111,6 @@ class GetDialogs(BaseClient): if not isinstance(dialog, types.Dialog): continue - parsed_dialogs.append(pyrogram.Dialogging._parse(self, dialog, messages, users, chats)) + parsed_dialogs.append(pyrogram.Dialog._parse(self, dialog, messages, users, chats)) return pyrogram.List(parsed_dialogs) diff --git a/pyrogram/client/methods/contacts/get_contacts.py b/pyrogram/client/methods/contacts/get_contacts.py index d1ccd728..10fd9950 100644 --- a/pyrogram/client/methods/contacts/get_contacts.py +++ b/pyrogram/client/methods/contacts/get_contacts.py @@ -25,6 +25,8 @@ from pyrogram.api import functions from pyrogram.errors import FloodWait from ...ext import BaseClient +log = logging.getLogger(__name__) + class GetContacts(BaseClient): async def get_contacts(self) -> List["pyrogram.User"]: @@ -43,7 +45,7 @@ class GetContacts(BaseClient): try: contacts = await self.send(functions.contacts.GetContacts(hash=0)) except FloodWait as e: - logging.warning("get_contacts flood: waiting {} seconds".format(e.x)) + log.warning("get_contacts flood: waiting {} seconds".format(e.x)) await asyncio.sleep(e.x) else: return pyrogram.List(pyrogram.User._parse(self, user) for user in contacts.users) diff --git a/pyrogram/client/methods/messages/get_history.py b/pyrogram/client/methods/messages/get_history.py index c4eec5ce..c2893472 100644 --- a/pyrogram/client/methods/messages/get_history.py +++ b/pyrogram/client/methods/messages/get_history.py @@ -24,9 +24,10 @@ import pyrogram from pyrogram.api import functions from pyrogram.client.ext import utils from pyrogram.errors import FloodWait - from ...ext import BaseClient +log = logging.getLogger(__name__) + class GetHistory(BaseClient): async def get_history( @@ -102,7 +103,7 @@ class GetHistory(BaseClient): ) ) except FloodWait as e: - logging.warning("Sleeping for {}s".format(e.x)) + log.warning("Sleeping for {}s".format(e.x)) await asyncio.sleep(e.x) else: break diff --git a/pyrogram/client/methods/messages/get_history_count.py b/pyrogram/client/methods/messages/get_history_count.py index c496a82b..d235e79b 100644 --- a/pyrogram/client/methods/messages/get_history_count.py +++ b/pyrogram/client/methods/messages/get_history_count.py @@ -16,11 +16,14 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +import logging from typing import Union from pyrogram.api import types, functions from pyrogram.client.ext import BaseClient +log = logging.getLogger(__name__) + class GetHistoryCount(BaseClient): async def get_history_count( diff --git a/pyrogram/client/methods/messages/get_messages.py b/pyrogram/client/methods/messages/get_messages.py index 834e60c5..9f08bafb 100644 --- a/pyrogram/client/methods/messages/get_messages.py +++ b/pyrogram/client/methods/messages/get_messages.py @@ -23,9 +23,10 @@ from typing import Union, Iterable, List import pyrogram from pyrogram.api import functions, types from pyrogram.errors import FloodWait - from ...ext import BaseClient, utils +log = logging.getLogger(__name__) + # TODO: Rewrite using a flag for replied messages and have message_ids non-optional @@ -115,7 +116,7 @@ class GetMessages(BaseClient): try: r = await self.send(rpc) except FloodWait as e: - logging.warning("Sleeping for {}s".format(e.x)) + log.warning("Sleeping for {}s".format(e.x)) await asyncio.sleep(e.x) else: break diff --git a/pyrogram/client/methods/messages/send_media_group.py b/pyrogram/client/methods/messages/send_media_group.py index 8df58db9..1e16d0d3 100644 --- a/pyrogram/client/methods/messages/send_media_group.py +++ b/pyrogram/client/methods/messages/send_media_group.py @@ -26,6 +26,8 @@ from pyrogram.api import functions, types from pyrogram.client.ext import BaseClient, utils from pyrogram.errors import FloodWait +log = logging.getLogger(__name__) + class SendMediaGroup(BaseClient): # TODO: Add progress parameter @@ -87,7 +89,7 @@ class SendMediaGroup(BaseClient): ) ) except FloodWait as e: - logging.warning("Sleeping for {}s".format(e.x)) + log.warning("Sleeping for {}s".format(e.x)) await asyncio.sleep(e.x) else: break @@ -142,7 +144,7 @@ class SendMediaGroup(BaseClient): ) ) except FloodWait as e: - logging.warning("Sleeping for {}s".format(e.x)) + log.warning("Sleeping for {}s".format(e.x)) await asyncio.sleep(e.x) else: break @@ -193,7 +195,7 @@ class SendMediaGroup(BaseClient): ) ) except FloodWait as e: - logging.warning("Sleeping for {}s".format(e.x)) + log.warning("Sleeping for {}s".format(e.x)) await asyncio.sleep(e.x) else: break diff --git a/pyrogram/client/parser/html.py b/pyrogram/client/parser/html.py index 7e5a37f0..d21f024f 100644 --- a/pyrogram/client/parser/html.py +++ b/pyrogram/client/parser/html.py @@ -28,6 +28,8 @@ from pyrogram.api import types from pyrogram.errors import PeerIdInvalid from . import utils +log = logging.getLogger(__name__) + class Parser(HTMLParser): MENTION_RE = re.compile(r"tg://user\?id=(\d+)") @@ -95,7 +97,7 @@ class Parser(HTMLParser): line, offset = self.getpos() offset += 1 - logging.warning("Unmatched closing tag at line {}:{}".format(tag, line, offset)) + log.warning("Unmatched closing tag at line {}:{}".format(tag, line, offset)) else: if not self.tag_entities[tag]: self.tag_entities.pop(tag) @@ -121,7 +123,7 @@ class HTML: for tag, entities in parser.tag_entities.items(): unclosed_tags.append("<{}> (x{})".format(tag, len(entities))) - logging.warning("Unclosed tags: {}".format(", ".join(unclosed_tags))) + log.warning("Unclosed tags: {}".format(", ".join(unclosed_tags))) entities = [] diff --git a/pyrogram/client/storage/file_storage.py b/pyrogram/client/storage/file_storage.py index a24a46db..e367b447 100644 --- a/pyrogram/client/storage/file_storage.py +++ b/pyrogram/client/storage/file_storage.py @@ -26,6 +26,8 @@ from threading import Lock from .memory_storage import MemoryStorage +log = logging.getLogger(__name__) + class FileStorage(MemoryStorage): FILE_EXTENSION = ".session" @@ -81,20 +83,20 @@ class FileStorage(MemoryStorage): except ValueError: pass else: - logging.warning("JSON session storage detected! Converting it into an SQLite session storage...") + log.warning("JSON session storage detected! Converting it into an SQLite session storage...") path.rename(path.name + ".OLD") - logging.warning('The old session file has been renamed to "{}.OLD"'.format(path.name)) + log.warning('The old session file has been renamed to "{}.OLD"'.format(path.name)) self.migrate_from_json(session_json) - logging.warning("Done! The session has been successfully converted from JSON to SQLite storage") + log.warning("Done! The session has been successfully converted from JSON to SQLite storage") return if Path(path.name + ".OLD").is_file(): - logging.warning('Old session file detected: "{}.OLD". You can remove this file now'.format(path.name)) + log.warning('Old session file detected: "{}.OLD". You can remove this file now'.format(path.name)) self.conn = sqlite3.connect( str(path), diff --git a/pyrogram/client/storage/memory_storage.py b/pyrogram/client/storage/memory_storage.py index ff98fd59..b24fce38 100644 --- a/pyrogram/client/storage/memory_storage.py +++ b/pyrogram/client/storage/memory_storage.py @@ -18,6 +18,7 @@ import base64 import inspect +import logging import sqlite3 import struct import time @@ -28,6 +29,8 @@ from typing import List, Tuple from pyrogram.api import types from pyrogram.client.storage.storage import Storage +log = logging.getLogger(__name__) + class MemoryStorage(Storage): SCHEMA_VERSION = 1 diff --git a/pyrogram/connection/connection.py b/pyrogram/connection/connection.py index ef7ff39b..2cd55526 100644 --- a/pyrogram/connection/connection.py +++ b/pyrogram/connection/connection.py @@ -22,6 +22,8 @@ import logging from .transport import * from ..session.internals import DataCenter +log = logging.getLogger(__name__) + class Connection: MAX_RETRIES = 3 @@ -49,14 +51,14 @@ class Connection: self.protocol = self.mode(self.ipv6, self.proxy) try: - logging.info("Connecting...") + log.info("Connecting...") await self.protocol.connect(self.address) except OSError as e: - logging.warning(e) # TODO: Remove + log.warning(e) # TODO: Remove self.protocol.close() await asyncio.sleep(1) else: - logging.info("Connected! {} DC{} - IPv{} - {}".format( + log.info("Connected! {} DC{} - IPv{} - {}".format( "Test" if self.test_mode else "Production", self.dc_id, "6" if self.ipv6 else "4", @@ -64,12 +66,12 @@ class Connection: )) break else: - logging.warning("Connection failed! Trying again...") + log.warning("Connection failed! Trying again...") raise TimeoutError def close(self): self.protocol.close() - logging.info("Disconnected") + log.info("Disconnected") async def send(self, data: bytes): try: diff --git a/pyrogram/connection/transport/tcp/tcp.py b/pyrogram/connection/transport/tcp/tcp.py index f76cb250..0d33fcd9 100644 --- a/pyrogram/connection/transport/tcp/tcp.py +++ b/pyrogram/connection/transport/tcp/tcp.py @@ -31,6 +31,8 @@ except ImportError as e: raise e +log = logging.getLogger(__name__) + class TCP: TIMEOUT = 10 @@ -65,7 +67,7 @@ class TCP: password=proxy.get("password", None) ) - logging.info("Using proxy {}:{}".format(hostname, port)) + log.info("Using proxy {}:{}".format(hostname, port)) else: self.socket = socks.socksocket( socket.AF_INET6 if ipv6 diff --git a/pyrogram/connection/transport/tcp/tcp_abridged.py b/pyrogram/connection/transport/tcp/tcp_abridged.py index 9d460708..a155a9a2 100644 --- a/pyrogram/connection/transport/tcp/tcp_abridged.py +++ b/pyrogram/connection/transport/tcp/tcp_abridged.py @@ -16,8 +16,12 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +import logging + from .tcp import TCP +log = logging.getLogger(__name__) + class TCPAbridged(TCP): def __init__(self, ipv6: bool, proxy: dict): diff --git a/pyrogram/connection/transport/tcp/tcp_abridged_o.py b/pyrogram/connection/transport/tcp/tcp_abridged_o.py index cc5de744..3ec6a002 100644 --- a/pyrogram/connection/transport/tcp/tcp_abridged_o.py +++ b/pyrogram/connection/transport/tcp/tcp_abridged_o.py @@ -16,11 +16,14 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +import logging import os from .tcp import TCP from ....crypto.aes import AES +log = logging.getLogger(__name__) + class TCPAbridgedO(TCP): RESERVED = (b"HEAD", b"POST", b"GET ", b"OPTI", b"\xee" * 4) diff --git a/pyrogram/connection/transport/tcp/tcp_full.py b/pyrogram/connection/transport/tcp/tcp_full.py index 119adba0..9404a4aa 100644 --- a/pyrogram/connection/transport/tcp/tcp_full.py +++ b/pyrogram/connection/transport/tcp/tcp_full.py @@ -16,11 +16,14 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +import logging from binascii import crc32 from struct import pack, unpack from .tcp import TCP +log = logging.getLogger(__name__) + class TCPFull(TCP): def __init__(self, ipv6: bool, proxy: dict): diff --git a/pyrogram/connection/transport/tcp/tcp_intermediate.py b/pyrogram/connection/transport/tcp/tcp_intermediate.py index 9d833a79..b2ab88a5 100644 --- a/pyrogram/connection/transport/tcp/tcp_intermediate.py +++ b/pyrogram/connection/transport/tcp/tcp_intermediate.py @@ -16,10 +16,13 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +import logging from struct import pack, unpack from .tcp import TCP +log = logging.getLogger(__name__) + class TCPIntermediate(TCP): def __init__(self, ipv6: bool, proxy: dict): diff --git a/pyrogram/connection/transport/tcp/tcp_intermediate_o.py b/pyrogram/connection/transport/tcp/tcp_intermediate_o.py index 234b2910..8d7113a5 100644 --- a/pyrogram/connection/transport/tcp/tcp_intermediate_o.py +++ b/pyrogram/connection/transport/tcp/tcp_intermediate_o.py @@ -16,12 +16,15 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +import logging import os from struct import pack, unpack from .tcp import TCP from ....crypto.aes import AES +log = logging.getLogger(__name__) + class TCPIntermediateO(TCP): RESERVED = (b"HEAD", b"POST", b"GET ", b"OPTI", b"\xee" * 4) diff --git a/pyrogram/crypto/aes.py b/pyrogram/crypto/aes.py index 9088d57f..d603caa0 100644 --- a/pyrogram/crypto/aes.py +++ b/pyrogram/crypto/aes.py @@ -18,10 +18,12 @@ import logging +log = logging.getLogger(__name__) + try: import tgcrypto - logging.info("Using TgCrypto") + log.info("Using TgCrypto") class AES: @@ -51,7 +53,7 @@ try: except ImportError: import pyaes - logging.warning( + log.warning( "TgCrypto is missing! " "Pyrogram will work the same, but at a much slower speed. " "More info: https://docs.pyrogram.org/topics/tgcrypto" diff --git a/pyrogram/session/auth.py b/pyrogram/session/auth.py index d153716e..ba0f5aed 100644 --- a/pyrogram/session/auth.py +++ b/pyrogram/session/auth.py @@ -30,6 +30,8 @@ from pyrogram.connection import Connection from pyrogram.crypto import AES, RSA, Prime from .internals import MsgId +log = logging.getLogger(__name__) + class Auth: MAX_RETRIES = 5 @@ -76,34 +78,34 @@ class Auth: self.connection = Connection(self.dc_id, self.test_mode, self.ipv6, self.proxy) try: - logging.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)) await self.connection.connect() # Step 1; Step 2 nonce = int.from_bytes(urandom(16), "little", signed=True) - logging.debug("Send req_pq: {}".format(nonce)) + log.debug("Send req_pq: {}".format(nonce)) res_pq = await self.send(functions.ReqPqMulti(nonce=nonce)) - logging.debug("Got ResPq: {}".format(res_pq.server_nonce)) - logging.debug("Server public key fingerprints: {}".format(res_pq.server_public_key_fingerprints)) + log.debug("Got ResPq: {}".format(res_pq.server_nonce)) + log.debug("Server public key fingerprints: {}".format(res_pq.server_public_key_fingerprints)) for i in res_pq.server_public_key_fingerprints: if i in RSA.server_public_keys: - logging.debug("Using fingerprint: {}".format(i)) + log.debug("Using fingerprint: {}".format(i)) public_key_fingerprint = i break else: - logging.debug("Fingerprint unknown: {}".format(i)) + log.debug("Fingerprint unknown: {}".format(i)) else: raise Exception("Public key not found") # Step 3 pq = int.from_bytes(res_pq.pq, "big") - logging.debug("Start PQ factorization: {}".format(pq)) + log.debug("Start PQ factorization: {}".format(pq)) start = time.time() g = Prime.decompose(pq) p, q = sorted((g, pq // g)) # p < q - logging.debug("Done PQ factorization ({}s): {} {}".format(round(time.time() - start, 3), p, q)) + log.debug("Done PQ factorization ({}s): {} {}".format(round(time.time() - start, 3), p, q)) # Step 4 server_nonce = res_pq.server_nonce @@ -123,10 +125,10 @@ class Auth: data_with_hash = sha + data + padding encrypted_data = RSA.encrypt(data_with_hash, public_key_fingerprint) - logging.debug("Done encrypt data with RSA") + log.debug("Done encrypt data with RSA") # Step 5. TODO: Handle "server_DH_params_fail". Code assumes response is ok - logging.debug("Send req_DH_params") + log.debug("Send req_DH_params") server_dh_params = await self.send( functions.ReqDHParams( nonce=nonce, @@ -160,12 +162,12 @@ class Auth: server_dh_inner_data = TLObject.read(BytesIO(answer)) - logging.debug("Done decrypting answer") + log.debug("Done decrypting answer") dh_prime = int.from_bytes(server_dh_inner_data.dh_prime, "big") delta_time = server_dh_inner_data.server_time - time.time() - logging.debug("Delta time: {}".format(round(delta_time, 3))) + log.debug("Delta time: {}".format(round(delta_time, 3))) # Step 6 g = server_dh_inner_data.g @@ -186,7 +188,7 @@ class Auth: data_with_hash = sha + data + padding encrypted_data = AES.ige256_encrypt(data_with_hash, tmp_aes_key, tmp_aes_iv) - logging.debug("Send set_client_DH_params") + log.debug("Send set_client_DH_params") set_client_dh_params_answer = await self.send( functions.SetClientDHParams( nonce=nonce, @@ -209,7 +211,7 @@ class Auth: ####################### assert dh_prime == Prime.CURRENT_DH_PRIME - logging.debug("DH parameters check: OK") + log.debug("DH parameters check: OK") # https://core.telegram.org/mtproto/security_guidelines#g-a-and-g-b-validation g_b = int.from_bytes(g_b, "big") @@ -218,12 +220,12 @@ class Auth: assert 1 < g_b < dh_prime - 1 assert 2 ** (2048 - 64) < g_a < dh_prime - 2 ** (2048 - 64) assert 2 ** (2048 - 64) < g_b < dh_prime - 2 ** (2048 - 64) - logging.debug("g_a and g_b validation: OK") + log.debug("g_a and g_b validation: OK") # https://core.telegram.org/mtproto/security_guidelines#checking-sha1-hash-values answer = server_dh_inner_data.write() # Call .write() to remove padding assert answer_with_hash[:20] == sha1(answer).digest() - logging.debug("SHA1 hash values check: OK") + log.debug("SHA1 hash values check: OK") # https://core.telegram.org/mtproto/security_guidelines#checking-nonce-server-nonce-and-new-nonce-fields # 1st message @@ -236,14 +238,14 @@ class Auth: assert nonce == set_client_dh_params_answer.nonce assert server_nonce == set_client_dh_params_answer.server_nonce server_nonce = server_nonce.to_bytes(16, "little", signed=True) - logging.debug("Nonce fields check: OK") + log.debug("Nonce fields check: OK") # Step 9 server_salt = AES.xor(new_nonce[:8], server_nonce[:8]) - logging.debug("Server salt: {}".format(int.from_bytes(server_salt, "little"))) + log.debug("Server salt: {}".format(int.from_bytes(server_salt, "little"))) - logging.info( + log.info( "Done auth key exchange: {}".format( set_client_dh_params_answer.__class__.__name__ ) diff --git a/pyrogram/session/session.py b/pyrogram/session/session.py index b5f69081..ab3daef2 100644 --- a/pyrogram/session/session.py +++ b/pyrogram/session/session.py @@ -32,6 +32,8 @@ from pyrogram.crypto import MTProto from pyrogram.errors import RPCError, InternalServerError, AuthKeyDuplicated from .internals import MsgId, MsgFactory +log = logging.getLogger(__name__) + class Result: def __init__(self): @@ -156,9 +158,9 @@ class Session: self.ping_task = asyncio.ensure_future(self.ping()) - logging.info("Session initialized: Layer {}".format(layer)) - logging.info("Device: {} - {}".format(self.client.device_model, self.client.app_version)) - logging.info("System: {} ({})".format(self.client.system_version, self.client.lang_code.upper())) + 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: await self.stop() @@ -173,7 +175,7 @@ class Session: self.is_connected.set() - logging.info("Session started") + log.info("Session started") async def stop(self): self.is_connected.clear() @@ -205,9 +207,9 @@ class Session: try: await self.client.disconnect_handler(self.client) except Exception as e: - logging.error(e, exc_info=True) + log.error(e, exc_info=True) - logging.info("Session stopped") + log.info("Session stopped") async def restart(self): await self.stop() @@ -236,7 +238,7 @@ class Session: else [data] ) - logging.debug(data) + log.debug(data) for msg in messages: if msg.seq_no % 2 != 0: @@ -269,7 +271,7 @@ class Session: self.results[msg_id].event.set() if len(self.pending_acks) >= self.ACKS_THRESHOLD: - logging.info("Send {} acks".format(len(self.pending_acks))) + log.info("Send {} acks".format(len(self.pending_acks))) try: await self._send(types.MsgsAck(msg_ids=list(self.pending_acks)), False) @@ -278,12 +280,12 @@ class Session: else: self.pending_acks.clear() except Exception as e: - logging.error(e, exc_info=True) + log.error(e, exc_info=True) - logging.info("NetWorkerTask stopped") + log.info("NetWorkerTask stopped") async def ping(self): - logging.info("PingTask started") + log.info("PingTask started") while True: try: @@ -302,10 +304,10 @@ class Session: except (OSError, TimeoutError, RPCError): pass - logging.info("PingTask stopped") + log.info("PingTask stopped") async def next_salt(self): - logging.info("NextSaltTask started") + log.info("NextSaltTask started") while True: now = datetime.now() @@ -315,7 +317,7 @@ class Session: valid_until = datetime.fromtimestamp(self.current_salt.valid_until) dt = (valid_until - now).total_seconds() - 900 - logging.info("Next salt in {:.0f}m {:.0f}s ({})".format( + log.info("Next salt in {:.0f}m {:.0f}s ({})".format( dt // 60, dt % 60, now + timedelta(seconds=dt) )) @@ -333,10 +335,10 @@ class Session: self.connection.close() break - logging.info("NextSaltTask stopped") + log.info("NextSaltTask stopped") async def recv(self): - logging.info("RecvTask started") + log.info("RecvTask started") while True: packet = await self.connection.recv() @@ -345,7 +347,7 @@ class Session: self.recv_queue.put_nowait(None) if packet: - logging.warning("Server sent \"{}\"".format(Int.read(BytesIO(packet)))) + log.warning("Server sent \"{}\"".format(Int.read(BytesIO(packet)))) if self.is_connected.is_set(): asyncio.ensure_future(self.restart()) @@ -354,7 +356,7 @@ class Session: self.recv_queue.put_nowait(packet) - logging.info("RecvTask stopped") + log.info("RecvTask stopped") async def _send(self, data: TLObject, wait_response: bool = True, timeout: float = WAIT_TIMEOUT): message = self.msg_factory(data)