mirror of
https://github.com/TeamPGM/pyrogram.git
synced 2024-11-18 21:44:22 +00:00
Implement async TCP protocol
This commit is contained in:
parent
de39c181ef
commit
7a6d7d0037
@ -16,6 +16,7 @@
|
|||||||
# You should have received a copy of the GNU Lesser General Public License
|
# You should have received a copy of the GNU Lesser General Public License
|
||||||
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
import asyncio
|
||||||
import logging
|
import logging
|
||||||
import socket
|
import socket
|
||||||
|
|
||||||
@ -32,14 +33,18 @@ except ImportError as e:
|
|||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class TCP(socks.socksocket):
|
class TCP:
|
||||||
def __init__(self, proxy: dict):
|
def __init__(self, proxy: dict):
|
||||||
super().__init__()
|
self.proxy = proxy
|
||||||
self.settimeout(10)
|
|
||||||
|
self.socket = socks.socksocket()
|
||||||
|
self.reader = None # type: asyncio.StreamReader
|
||||||
|
self.writer = None # type: asyncio.StreamWriter
|
||||||
|
|
||||||
self.proxy_enabled = proxy.get("enabled", False)
|
self.proxy_enabled = proxy.get("enabled", False)
|
||||||
|
|
||||||
if proxy and self.proxy_enabled:
|
if proxy and self.proxy_enabled:
|
||||||
self.set_proxy(
|
self.socket.set_proxy(
|
||||||
proxy_type=socks.SOCKS5,
|
proxy_type=socks.SOCKS5,
|
||||||
addr=proxy.get("hostname", None),
|
addr=proxy.get("hostname", None),
|
||||||
port=proxy.get("port", None),
|
port=proxy.get("port", None),
|
||||||
@ -52,26 +57,81 @@ class TCP(socks.socksocket):
|
|||||||
proxy.get("port", None)
|
proxy.get("port", None)
|
||||||
))
|
))
|
||||||
|
|
||||||
|
async def connect(self, address: tuple):
|
||||||
|
self.socket.connect(address)
|
||||||
|
self.reader, self.writer = await asyncio.open_connection(sock=self.socket)
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
try:
|
try:
|
||||||
self.shutdown(socket.SHUT_RDWR)
|
self.writer.close()
|
||||||
except OSError:
|
except AttributeError:
|
||||||
pass
|
try:
|
||||||
finally:
|
self.socket.shutdown(socket.SHUT_RDWR)
|
||||||
super().close()
|
except OSError:
|
||||||
|
pass
|
||||||
|
finally:
|
||||||
|
self.socket.close()
|
||||||
|
|
||||||
def recvall(self, length: int) -> bytes or None:
|
async def send(self, data: bytes):
|
||||||
|
self.writer.write(data)
|
||||||
|
await self.writer.drain()
|
||||||
|
|
||||||
|
async def recv(self, length: int = 0):
|
||||||
data = b""
|
data = b""
|
||||||
|
|
||||||
while len(data) < length:
|
while len(data) < length:
|
||||||
try:
|
try:
|
||||||
packet = super().recv(length - len(data))
|
chunk = await self.reader.read(length - len(data))
|
||||||
except OSError:
|
except OSError:
|
||||||
return None
|
return None
|
||||||
else:
|
else:
|
||||||
if packet:
|
if chunk:
|
||||||
data += packet
|
data += chunk
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
# class TCP(socks.socksocket):
|
||||||
|
# def __init__(self, proxy: dict):
|
||||||
|
# super().__init__()
|
||||||
|
# self.settimeout(10)
|
||||||
|
# self.proxy_enabled = proxy.get("enabled", False)
|
||||||
|
#
|
||||||
|
# if proxy and self.proxy_enabled:
|
||||||
|
# self.set_proxy(
|
||||||
|
# proxy_type=socks.SOCKS5,
|
||||||
|
# addr=proxy.get("hostname", None),
|
||||||
|
# port=proxy.get("port", None),
|
||||||
|
# username=proxy.get("username", None),
|
||||||
|
# password=proxy.get("password", None)
|
||||||
|
# )
|
||||||
|
#
|
||||||
|
# log.info("Using proxy {}:{}".format(
|
||||||
|
# proxy.get("hostname", None),
|
||||||
|
# proxy.get("port", None)
|
||||||
|
# ))
|
||||||
|
#
|
||||||
|
# def close(self):
|
||||||
|
# try:
|
||||||
|
# self.shutdown(socket.SHUT_RDWR)
|
||||||
|
# except OSError:
|
||||||
|
# pass
|
||||||
|
# finally:
|
||||||
|
# super().close()
|
||||||
|
#
|
||||||
|
# def recvall(self, length: int) -> bytes or None:
|
||||||
|
# data = b""
|
||||||
|
#
|
||||||
|
# while len(data) < length:
|
||||||
|
# try:
|
||||||
|
# packet = super().recv(length - len(data))
|
||||||
|
# except OSError:
|
||||||
|
# return None
|
||||||
|
# else:
|
||||||
|
# if packet:
|
||||||
|
# data += packet
|
||||||
|
# else:
|
||||||
|
# return None
|
||||||
|
#
|
||||||
|
# return data
|
||||||
|
Loading…
Reference in New Issue
Block a user