Skip to content

Commit

Permalink
Fix compatibility with aiohttp v3.10 (issue #43)
Browse files Browse the repository at this point in the history
  • Loading branch information
romis2012 committed Jul 31, 2024
1 parent adb9bb0 commit 9671c3e
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
runs-on: "${{ matrix.os }}"
strategy:
matrix:
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11"]
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
os: [ubuntu-latest]
steps:
- name: Checkout
Expand Down
72 changes: 53 additions & 19 deletions aiohttp_socks/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from asyncio import BaseTransport, StreamWriter
from typing import Iterable

from aiohttp import TCPConnector
from aiohttp import TCPConnector, ClientConnectorError
from aiohttp.abc import AbstractResolver
from aiohttp.client_proto import ResponseHandler
from python_socks import ProxyType, parse_proxy_url
Expand Down Expand Up @@ -64,8 +64,7 @@ def __init__(
self._rdns = rdns
self._proxy_ssl = proxy_ssl

# noinspection PyMethodOverriding
async def _wrap_create_connection(self, protocol_factory, host, port, *, ssl, **kwargs):
async def _connect_via_proxy(self, host, port, ssl=None, timeout=None):
proxy = Proxy(
proxy_type=self._proxy_type,
host=self._proxy_host,
Expand All @@ -76,17 +75,11 @@ async def _wrap_create_connection(self, protocol_factory, host, port, *, ssl, **
proxy_ssl=self._proxy_ssl,
)

connect_timeout = None

timeout = kwargs.get('timeout')
if timeout is not None:
connect_timeout = getattr(timeout, 'sock_connect', None)

stream = await proxy.connect(
dest_host=host,
dest_port=port,
dest_ssl=ssl,
timeout=connect_timeout,
timeout=timeout,
)

transport: BaseTransport = stream.writer.transport
Expand All @@ -100,6 +93,30 @@ async def _wrap_create_connection(self, protocol_factory, host, port, *, ssl, **

return transport, protocol

async def _wrap_create_connection(
self,
*args,
addr_infos,
req,
timeout,
client_error=ClientConnectorError,
**kwargs,
):
try:
host = addr_infos[0][4][0]
port = addr_infos[0][4][1]
except IndexError:
raise ValueError('Invalid arg: `addr_infos`')

ssl = kwargs.get('ssl')

return await self._connect_via_proxy(
host=host,
port=port,
ssl=ssl,
timeout=timeout.sock_connect,
)

@classmethod
def from_url(cls, url, **kwargs):
proxy_type, host, port, username, password = parse_proxy_url(url)
Expand Down Expand Up @@ -129,8 +146,7 @@ def __init__(self, proxy_infos: Iterable[ProxyInfo], **kwargs):

self._proxy_infos = proxy_infos

# noinspection PyMethodOverriding
async def _wrap_create_connection(self, protocol_factory, host, port, *, ssl, **kwargs):
async def _connect_via_proxy(self, host, port, ssl=None, timeout=None):
forward = None
proxy = None
for info in self._proxy_infos:
Expand All @@ -145,17 +161,11 @@ async def _wrap_create_connection(self, protocol_factory, host, port, *, ssl, **
)
forward = proxy

connect_timeout = None

timeout = kwargs.get('timeout')
if timeout is not None:
connect_timeout = getattr(timeout, 'sock_connect', None)

stream = await proxy.connect(
dest_host=host,
dest_port=port,
dest_ssl=ssl,
timeout=connect_timeout,
timeout=timeout,
)

transport: BaseTransport = stream.writer.transport
Expand All @@ -169,6 +179,30 @@ async def _wrap_create_connection(self, protocol_factory, host, port, *, ssl, **

return transport, protocol

async def _wrap_create_connection(
self,
*args,
addr_infos,
req,
timeout,
client_error=ClientConnectorError,
**kwargs,
):
try:
host = addr_infos[0][4][0]
port = addr_infos[0][4][1]
except IndexError:
raise ValueError('Invalid arg: `addr_infos`')

ssl = kwargs.get('ssl')

return await self._connect_via_proxy(
host=host,
port=port,
ssl=ssl,
timeout=timeout.sock_connect,
)

@classmethod
def from_urls(cls, urls: Iterable[str], **kwargs):
infos = []
Expand Down
15 changes: 6 additions & 9 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
-e .
importlib_metadata==4.12.0; python_version == "3.7"
flake8==3.9.1
pytest==7.0.1
pytest-cov==3.0.0
# coveralls==3.3.1
pytest-asyncio==0.16.0; python_version < "3.7"
pytest-asyncio==0.18.3; python_version >= "3.7"
#-e .
flake8>=3.9.1
pytest==8.0.2
pytest-cov==4.1.0
pytest-asyncio==0.23.5
trustme==0.9.0
attrs>=19.3.0
yarl>=1.4.2
flask>=1.1.2
anyio>=3.3.4,<4.0.0
anyio>=3.3.4,<5.0.0
tiny-proxy>=0.1.1
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

from setuptools import setup

if sys.version_info < (3, 6, 0):
raise RuntimeError('aiohttp-socks requires Python 3.6+')
if sys.version_info < (3, 8, 0):
raise RuntimeError('aiohttp-socks requires Python 3.8+')


def get_version():
Expand Down Expand Up @@ -35,7 +35,7 @@ def get_long_description():
packages=['aiohttp_socks'],
keywords='asyncio aiohttp socks socks5 socks4 http proxy',
install_requires=[
'aiohttp>=2.3.2',
'aiohttp>=3.10.0',
'python-socks[asyncio]>=2.4.3,<3.0.0',
],
)

0 comments on commit 9671c3e

Please sign in to comment.