| 1 |
# -*- coding: utf-8 -*- |
| 2 |
import socket |
| 3 |
|
| 4 |
from aiohttp import TCPConnector |
| 5 |
from aiohttp.abc import AbstractResolver |
| 6 |
from yarl import URL |
| 7 |
|
| 8 |
from .helpers import create_socket_wrapper, parse_proxy_url |
| 9 |
from .proto import ProxyType |
| 10 |
|
| 11 |
|
| 12 |
class NoResolver(AbstractResolver): |
| 13 |
async def resolve(self, host, port=0, family=socket.AF_INET): |
| 14 |
return [{'hostname': host, |
| 15 |
'host': host, 'port': port, |
| 16 |
'family': family, 'proto': 0, |
| 17 |
'flags': 0}] |
| 18 |
|
| 19 |
async def close(self): |
| 20 |
pass # pragma: no cover |
| 21 |
|
| 22 |
|
| 23 |
class ProxyConnector(TCPConnector): |
| 24 |
def __init__(self, proxy_type=ProxyType.HTTP, |
| 25 |
host=None, port=None, |
| 26 |
username=None, password=None, |
| 27 |
rdns=False, family=socket.AF_INET, **kwargs): |
| 28 |
|
| 29 |
if rdns: |
| 30 |
kwargs['resolver'] = NoResolver() |
| 31 |
|
| 32 |
super().__init__(**kwargs) |
| 33 |
|
| 34 |
self._proxy_type = proxy_type |
| 35 |
self._proxy_host = host |
| 36 |
self._proxy_port = port |
| 37 |
self._proxy_username = username |
| 38 |
self._proxy_password = password |
| 39 |
self._rdns = rdns |
| 40 |
self._proxy_family = family |
| 41 |
|
| 42 |
@property |
| 43 |
def proxy_url(self): |
| 44 |
if self._proxy_username: |
| 45 |
url = '{scheme}://{host}:{port}'.format( |
| 46 |
scheme=self._proxy_type, host=self._proxy_host, |
| 47 |
port=self._proxy_port) |
| 48 |
else: |
| 49 |
url = '{scheme}://{host}:{port}'.format( |
| 50 |
scheme=self._proxy_type, host=self._proxy_host, |
| 51 |
port=self._proxy_port) |
| 52 |
return URL(url) |
| 53 |
|
| 54 |
# noinspection PyMethodOverriding |
| 55 |
async def _wrap_create_connection( |
| 56 |
self, protocol_factory, host=None, port=None, *args, **kwargs): |
| 57 |
if not self._proxy_type.is_http(): |
| 58 |
sock = create_socket_wrapper( |
| 59 |
loop=self._loop, |
| 60 |
proxy_type=self._proxy_type, |
| 61 |
host=self._proxy_host, port=self._proxy_port, |
| 62 |
username=self._proxy_username, password=self._proxy_password, |
| 63 |
rdns=self._rdns, family=self._proxy_family) |
| 64 |
await sock.connect((host, port)) |
| 65 |
|
| 66 |
return await super()._wrap_create_connection( |
| 67 |
protocol_factory, None, None, *args, sock=sock.socket, |
| 68 |
**kwargs) |
| 69 |
else: |
| 70 |
return await super(ProxyConnector, self)._wrap_create_connection( |
| 71 |
protocol_factory, host, port, *args, **kwargs) |
| 72 |
|
| 73 |
async def connect(self, req, traces, timeout): |
| 74 |
if self._proxy_type.is_http(): |
| 75 |
req.update_proxy(self.proxy_url.with_scheme('http'), None, |
| 76 |
req.proxy_headers) |
| 77 |
return await super(ProxyConnector, self).connect( |
| 78 |
req=req, traces=traces, timeout=timeout) |
| 79 |
|
| 80 |
@classmethod |
| 81 |
def from_url(cls, url, **kwargs): |
| 82 |
proxy_type, host, port, username, password = parse_proxy_url(url) |
| 83 |
return cls(proxy_type=proxy_type, host=host, port=port, |
| 84 |
username=username, password=password, **kwargs) |