mirror of
https://github.com/TeamPGM/pyrogram.git
synced 2024-11-23 23:34:28 +00:00
Handle proxy socket connections using thread executors
This commit is contained in:
parent
1fa6f3b924
commit
2dca5aeac2
@ -20,6 +20,7 @@ import asyncio
|
||||
import ipaddress
|
||||
import logging
|
||||
import socket
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
|
||||
import socks
|
||||
|
||||
@ -38,6 +39,8 @@ class TCP:
|
||||
self.lock = asyncio.Lock()
|
||||
self.loop = asyncio.get_event_loop()
|
||||
|
||||
self.proxy = proxy
|
||||
|
||||
if proxy:
|
||||
hostname = proxy.get("hostname")
|
||||
|
||||
@ -71,10 +74,14 @@ class TCP:
|
||||
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")
|
||||
if self.proxy:
|
||||
with ThreadPoolExecutor(1) as executor:
|
||||
await self.loop.run_in_executor(executor, self.socket.connect, address)
|
||||
else:
|
||||
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)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user