2017-11-26 19:14:28 +03:00
|
|
|
"""
|
|
|
|
This module holds a rough implementation of the C# TCP client.
|
2018-06-06 21:41:01 +03:00
|
|
|
|
|
|
|
This class is **not** safe across several tasks since partial reads
|
|
|
|
may be ``await``'ed before being able to return the exact byte count.
|
|
|
|
|
|
|
|
This class is also not concerned about disconnections or retries of
|
|
|
|
any sort, nor any other kind of errors such as connecting twice.
|
2017-11-26 19:14:28 +03:00
|
|
|
"""
|
2018-06-06 21:41:01 +03:00
|
|
|
import asyncio
|
2018-02-26 15:41:07 +03:00
|
|
|
import logging
|
2016-08-26 13:58:53 +03:00
|
|
|
import socket
|
2018-06-06 21:41:01 +03:00
|
|
|
from io import BytesIO
|
2016-08-26 13:58:53 +03:00
|
|
|
|
2018-02-20 17:58:51 +03:00
|
|
|
try:
|
|
|
|
import socks
|
|
|
|
except ImportError:
|
|
|
|
socks = None
|
|
|
|
|
2018-01-05 20:31:48 +03:00
|
|
|
|
2018-02-26 15:41:07 +03:00
|
|
|
__log__ = logging.getLogger(__name__)
|
|
|
|
|
2016-08-26 13:58:53 +03:00
|
|
|
|
2018-06-06 21:41:01 +03:00
|
|
|
# TODO Except asyncio.TimeoutError, ConnectionError, OSError...
|
|
|
|
# ...for connect, write and read (in the upper levels, not here)
|
2016-08-26 13:58:53 +03:00
|
|
|
class TcpClient:
|
2017-11-26 19:14:28 +03:00
|
|
|
"""A simple TCP client to ease the work with sockets and proxies."""
|
2018-06-06 21:41:01 +03:00
|
|
|
def __init__(self, proxy=None, timeout=5):
|
2017-11-26 19:14:28 +03:00
|
|
|
"""
|
|
|
|
Initializes the TCP client.
|
|
|
|
|
|
|
|
:param proxy: the proxy to be used, if any.
|
|
|
|
:param timeout: the timeout for connect, read and write operations.
|
|
|
|
"""
|
2017-09-22 17:11:07 +03:00
|
|
|
self.proxy = proxy
|
2017-08-29 14:35:56 +03:00
|
|
|
self._socket = None
|
2018-06-06 21:41:01 +03:00
|
|
|
self._loop = asyncio.get_event_loop()
|
2017-09-13 10:44:58 +03:00
|
|
|
|
2018-06-06 21:41:01 +03:00
|
|
|
if isinstance(timeout, (int, float)):
|
2017-09-21 14:43:33 +03:00
|
|
|
self.timeout = float(timeout)
|
2018-06-06 21:41:01 +03:00
|
|
|
elif hasattr(timeout, 'seconds'):
|
|
|
|
self.timeout = float(timeout.seconds)
|
2017-09-02 20:33:42 +03:00
|
|
|
else:
|
2017-12-28 02:22:28 +03:00
|
|
|
raise TypeError('Invalid timeout type: {}'.format(type(timeout)))
|
2017-05-09 20:05:14 +03:00
|
|
|
|
2018-06-06 21:41:01 +03:00
|
|
|
@staticmethod
|
|
|
|
def _create_socket(mode, proxy):
|
|
|
|
if proxy is None:
|
|
|
|
s = socket.socket(mode, socket.SOCK_STREAM)
|
2017-05-30 12:42:14 +03:00
|
|
|
else:
|
2017-05-30 11:40:33 +03:00
|
|
|
import socks
|
2018-06-06 21:41:01 +03:00
|
|
|
s = socks.socksocket(mode, socket.SOCK_STREAM)
|
|
|
|
if isinstance(proxy, dict):
|
|
|
|
s.set_proxy(**proxy)
|
2017-06-05 07:04:01 +03:00
|
|
|
else: # tuple, list, etc.
|
2018-06-06 21:41:01 +03:00
|
|
|
s.set_proxy(*proxy)
|
|
|
|
s.setblocking(False)
|
|
|
|
return s
|
2017-05-30 11:40:33 +03:00
|
|
|
|
2018-06-06 21:41:01 +03:00
|
|
|
async def connect(self, ip, port):
|
2017-11-26 19:14:28 +03:00
|
|
|
"""
|
2018-06-06 21:41:01 +03:00
|
|
|
Tries connecting to IP:port.
|
2017-11-26 19:14:28 +03:00
|
|
|
|
|
|
|
:param ip: the IP to connect to.
|
|
|
|
:param port: the port to connect to.
|
2017-06-22 20:21:33 +03:00
|
|
|
"""
|
2017-09-21 13:37:05 +03:00
|
|
|
if ':' in ip: # IPv6
|
2018-01-26 16:38:13 +03:00
|
|
|
ip = ip.replace('[', '').replace(']', '')
|
2017-09-21 13:37:05 +03:00
|
|
|
mode, address = socket.AF_INET6, (ip, port, 0, 0)
|
|
|
|
else:
|
|
|
|
mode, address = socket.AF_INET, (ip, port)
|
|
|
|
|
2018-06-06 21:41:01 +03:00
|
|
|
if self._socket is None:
|
|
|
|
self._socket = self._create_socket(mode, self.proxy)
|
|
|
|
|
|
|
|
asyncio.wait_for(self._loop.sock_connect(self._socket, address),
|
|
|
|
self.timeout, loop=self._loop)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_connected(self):
|
2017-11-26 19:14:28 +03:00
|
|
|
"""Determines whether the client is connected or not."""
|
2017-11-14 14:01:33 +03:00
|
|
|
return self._socket is not None and self._socket.fileno() >= 0
|
2017-08-29 14:35:56 +03:00
|
|
|
|
2016-08-26 13:58:53 +03:00
|
|
|
def close(self):
|
2017-11-26 19:14:28 +03:00
|
|
|
"""Closes the connection."""
|
2018-06-06 21:41:01 +03:00
|
|
|
if self._socket is not None:
|
2017-09-13 10:44:58 +03:00
|
|
|
try:
|
2018-06-06 21:41:01 +03:00
|
|
|
self._socket.shutdown(socket.SHUT_RDWR)
|
|
|
|
self._socket.close()
|
2017-09-13 10:44:58 +03:00
|
|
|
finally:
|
|
|
|
self._socket = None
|
2016-08-26 13:58:53 +03:00
|
|
|
|
2018-06-06 21:41:01 +03:00
|
|
|
async def write(self, data):
|
2017-11-26 19:14:28 +03:00
|
|
|
"""
|
|
|
|
Writes (sends) the specified bytes to the connected peer.
|
|
|
|
|
|
|
|
:param data: the data to send.
|
|
|
|
"""
|
2018-06-06 21:41:01 +03:00
|
|
|
if not self.is_connected:
|
|
|
|
raise ConnectionError()
|
2016-09-10 11:17:15 +03:00
|
|
|
|
2018-06-06 21:41:01 +03:00
|
|
|
await asyncio.wait_for(
|
|
|
|
self.sock_sendall(data),
|
|
|
|
timeout=self.timeout,
|
|
|
|
loop=self._loop
|
|
|
|
)
|
|
|
|
|
|
|
|
async def read(self, size):
|
2017-11-26 19:14:28 +03:00
|
|
|
"""
|
|
|
|
Reads (receives) a whole block of size bytes from the connected peer.
|
2017-06-09 12:42:39 +03:00
|
|
|
|
2017-11-26 19:14:28 +03:00
|
|
|
:param size: the size of the block to be read.
|
|
|
|
:return: the read data with len(data) == size.
|
2017-06-09 12:42:39 +03:00
|
|
|
"""
|
2018-06-06 21:41:01 +03:00
|
|
|
if not self.is_connected:
|
|
|
|
raise ConnectionError()
|
2017-09-21 14:43:33 +03:00
|
|
|
|
2018-06-06 21:41:01 +03:00
|
|
|
with BytesIO() as buffer:
|
2017-09-02 19:49:29 +03:00
|
|
|
bytes_left = size
|
|
|
|
while bytes_left != 0:
|
2018-06-06 21:41:01 +03:00
|
|
|
partial = await asyncio.wait_for(
|
|
|
|
self.sock_recv(bytes_left),
|
|
|
|
timeout=self.timeout,
|
|
|
|
loop=self._loop
|
|
|
|
)
|
|
|
|
if not partial == 0:
|
|
|
|
raise ConnectionResetError()
|
2017-09-02 19:49:29 +03:00
|
|
|
|
2017-09-02 20:14:11 +03:00
|
|
|
buffer.write(partial)
|
|
|
|
bytes_left -= len(partial)
|
2017-09-02 19:49:29 +03:00
|
|
|
|
2018-06-06 21:41:01 +03:00
|
|
|
return buffer.getvalue()
|
2017-09-03 12:54:26 +03:00
|
|
|
|
2018-06-06 21:41:01 +03:00
|
|
|
# Due to recent https://github.com/python/cpython/pull/4386
|
|
|
|
# Credit to @andr-04 for his original implementation
|
|
|
|
def sock_recv(self, n):
|
|
|
|
fut = self._loop.create_future()
|
|
|
|
self._sock_recv(fut, None, n)
|
|
|
|
return fut
|
|
|
|
|
|
|
|
def _sock_recv(self, fut, registered_fd, n):
|
|
|
|
if registered_fd is not None:
|
|
|
|
self._loop.remove_reader(registered_fd)
|
|
|
|
if fut.cancelled():
|
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
|
|
|
data = self._socket.recv(n)
|
|
|
|
except (BlockingIOError, InterruptedError):
|
|
|
|
fd = self._socket.fileno()
|
|
|
|
self._loop.add_reader(fd, self._sock_recv, fut, fd, n)
|
|
|
|
except Exception as exc:
|
|
|
|
fut.set_exception(exc)
|
|
|
|
else:
|
|
|
|
fut.set_result(data)
|
|
|
|
|
|
|
|
def sock_sendall(self, data):
|
|
|
|
fut = self._loop.create_future()
|
|
|
|
if data:
|
|
|
|
self._sock_sendall(fut, None, data)
|
|
|
|
else:
|
|
|
|
fut.set_result(None)
|
|
|
|
return fut
|
|
|
|
|
|
|
|
def _sock_sendall(self, fut, registered_fd, data):
|
|
|
|
if registered_fd:
|
|
|
|
self._loop.remove_writer(registered_fd)
|
|
|
|
if fut.cancelled():
|
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
|
|
|
n = self._socket.send(data)
|
|
|
|
except (BlockingIOError, InterruptedError):
|
|
|
|
n = 0
|
|
|
|
except Exception as exc:
|
|
|
|
fut.set_exception(exc)
|
|
|
|
return
|
|
|
|
|
|
|
|
if n == len(data):
|
|
|
|
fut.set_result(None)
|
|
|
|
else:
|
|
|
|
if n:
|
|
|
|
data = data[n:]
|
|
|
|
fd = self._socket.fileno()
|
|
|
|
self._loop.add_writer(fd, self._sock_sendall, fut, fd, data)
|