# Pyrogram - Telegram MTProto API Client Library for Python # Copyright (C) 2017-present Dan # # This file is part of Pyrogram. # # Pyrogram is free software: you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as published # by the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # Pyrogram is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . import asyncio import ipaddress import logging import socket import time from typing import Optional try: import socks except ImportError as e: e.msg = ( "PySocks is missing and Pyrogram can't run without. " "Please install it using \"pip3 install pysocks\"." ) raise e log = logging.getLogger(__name__) class TCP: TIMEOUT = 10 def __init__(self, ipv6: bool, proxy: dict): self.socket = None self.reader = None self.writer = None self.send_queue = asyncio.Queue() self.send_task = None self.loop = asyncio.get_event_loop() if proxy: hostname = proxy.get("hostname") try: ip_address = ipaddress.ip_address(hostname) except ValueError: self.socket = socks.socksocket(socket.AF_INET) else: if isinstance(ip_address, ipaddress.IPv6Address): self.socket = socks.socksocket(socket.AF_INET6) else: self.socket = socks.socksocket(socket.AF_INET) self.socket.set_proxy( proxy_type=getattr(socks, proxy.get("scheme").upper()), addr=hostname, port=proxy.get("port", None), username=proxy.get("username", None), password=proxy.get("password", None) ) log.info(f"Using proxy {hostname}") else: self.socket = socket.socket( socket.AF_INET6 if ipv6 else socket.AF_INET ) self.socket.setblocking(False) async def connect(self, address: tuple): try: await asyncio.wait_for(asyncio.get_event_loop().sock_connect(self.socket, address), TCP.TIMEOUT) except asyncio.TimeoutError: # Re-raise as TimeoutError. asyncio.TimeoutError is deprecated in 3.11 raise TimeoutError("Connection timed out") self.reader, self.writer = await asyncio.open_connection(sock=self.socket) self.send_task = asyncio.create_task(self.send_worker()) async def close(self): await self.send_queue.put(None) if self.send_task is not None: await self.send_task try: if self.writer is not None: self.writer.close() await asyncio.wait_for(self.writer.wait_closed(), TCP.TIMEOUT) except Exception as e: log.info("Close exception: %s %s", type(e).__name__, e) async def send(self, data: bytes): await self.send_queue.put(data) async def send_worker(self): while True: data = await self.send_queue.get() if data is None: break try: self.writer.write(data) await self.writer.drain() except Exception as e: log.info("Send exception: %s %s", type(e).__name__, e) async def recv(self, length: int = 0): data = b"" while len(data) < length: try: chunk = await asyncio.wait_for( self.reader.read(length - len(data)), TCP.TIMEOUT ) except (OSError, asyncio.TimeoutError): return None else: if chunk: data += chunk else: return None return data