├── .coveragerc ├── .flake8 ├── .github └── workflows │ └── ci.yml ├── .gitignore ├── LICENSE.txt ├── MANIFEST.in ├── README.md ├── aiohttp_socks ├── __init__.py ├── _deprecated.py ├── connector.py ├── py.typed └── utils.py ├── pyproject.toml ├── requirements-dev.txt └── tests ├── __init__.py ├── config.py ├── conftest.py ├── http_app.py ├── http_server.py ├── mocks.py ├── proxy_server.py ├── test_connector.py └── utils.py /.coveragerc: -------------------------------------------------------------------------------- 1 | [run] 2 | omit = 3 | # */_proxy_chain_*.py 4 | aiohttp_socks/core_socks/_basic_auth.py 5 | aiohttp_socks/_deprecated.py 6 | [report] 7 | # Regexes for lines to exclude from consideration 8 | exclude_lines = 9 | pragma: no cover 10 | def __repr__ 11 | if self.debug: 12 | raise NotImplementedError 13 | raise ValueError 14 | -------------------------------------------------------------------------------- /.flake8: -------------------------------------------------------------------------------- 1 | [flake8] 2 | ignore = N805,W503 3 | max-line-length = 99 4 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | branches: ["master"] 6 | pull_request: 7 | branches: ["master"] 8 | 9 | jobs: 10 | build: 11 | name: "Python ${{ matrix.python-version }} ${{ matrix.os }}" 12 | runs-on: "${{ matrix.os }}" 13 | strategy: 14 | matrix: 15 | python-version: ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13"] 16 | os: [ubuntu-latest] 17 | steps: 18 | - name: Checkout 19 | uses: actions/checkout@v4 20 | - name: Setup Python ${{ matrix.python-version }} 21 | uses: actions/setup-python@v5 22 | with: 23 | python-version: ${{ matrix.python-version }} 24 | - name: Install dependencies 25 | run: | 26 | python -m pip install --upgrade pip setuptools 27 | pip install -r requirements-dev.txt 28 | - name: Lint with flake8 29 | run: | 30 | python -m flake8 aiohttp_socks tests 31 | continue-on-error: true 32 | - name: Run tests 33 | # run: python -m pytest tests --cov=./aiohttp_socks --cov-report term-missing -s 34 | run: python -m pytest tests --cov=./aiohttp_socks --cov-report xml 35 | - name: Upload coverage 36 | uses: codecov/codecov-action@v5 37 | with: 38 | token: ${{ secrets.CODECOV_TOKEN }} 39 | slug: romis2012/aiohttp-socks 40 | file: ./coverage.xml 41 | flags: unit 42 | fail_ci_if_error: false -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.bak 2 | *.egg 3 | *.egg-info 4 | *.eggs 5 | *.pyc 6 | *.pyd 7 | *.pyo 8 | *.so 9 | *.tar.gz 10 | *~ 11 | .DS_Store 12 | .Python 13 | .cache 14 | .coverage 15 | .coverage.* 16 | .idea 17 | .installed.cfg 18 | .noseids 19 | .tox 20 | .vimrc 21 | # bin 22 | build 23 | cover 24 | coverage 25 | develop-eggs 26 | dist 27 | docs/_build/ 28 | eggs 29 | include/ 30 | lib/ 31 | man/ 32 | nosetests.xml 33 | parts 34 | pyvenv 35 | sources 36 | var/* 37 | venv 38 | virtualenv.py 39 | .install-deps 40 | .develop 41 | .idea/ 42 | .vscode/ 43 | usage*.py -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | # Include the license file 2 | include LICENSE.txt 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## aiohttp-socks 2 | 3 | [![CI](https://github.com/romis2012/aiohttp-socks/actions/workflows/ci.yml/badge.svg)](https://github.com/romis2012/aiohttp-socks/actions/workflows/ci.yml) 4 | [![Coverage Status](https://codecov.io/gh/romis2012/aiohttp-socks/branch/master/graph/badge.svg)](https://codecov.io/gh/romis2012/aiohttp-socks) 5 | [![PyPI version](https://badge.fury.io/py/aiohttp-socks.svg)](https://pypi.python.org/pypi/aiohttp-socks) 6 | 9 | The `aiohttp-socks` package provides a proxy connector for [aiohttp](https://github.com/aio-libs/aiohttp). 10 | Supports SOCKS4(a), SOCKS5(h), HTTP (CONNECT) as well as Proxy chains. 11 | It uses [python-socks](https://github.com/romis2012/python-socks) for core proxy functionality. 12 | 13 | 14 | ## Requirements 15 | - Python >= 3.8 16 | - aiohttp >= 3.10.0 17 | - python-socks[asyncio] >= 2.4.3 18 | 19 | ## Installation 20 | ``` 21 | pip install aiohttp_socks 22 | ``` 23 | 24 | ## Usage 25 | 26 | #### Simple usage 27 | ```python 28 | import aiohttp 29 | from aiohttp_socks import ProxyType, ProxyConnector, ChainProxyConnector 30 | 31 | 32 | async def fetch(url): 33 | connector = ProxyConnector.from_url('socks5://user:password@127.0.0.1:1080') 34 | 35 | ### or use ProxyConnector constructor 36 | # connector = ProxyConnector( 37 | # proxy_type=ProxyType.SOCKS5, 38 | # host='127.0.0.1', 39 | # port=1080, 40 | # username='user', 41 | # password='password', 42 | # rdns=True # default is True for socks5 43 | # ) 44 | 45 | ### proxy chaining (since ver 0.3.3) 46 | # connector = ChainProxyConnector.from_urls([ 47 | # 'socks5://user:password@127.0.0.1:1080', 48 | # 'socks4://127.0.0.1:1081', 49 | # 'http://user:password@127.0.0.1:3128', 50 | # ]) 51 | async with aiohttp.ClientSession(connector=connector) as session: 52 | async with session.get(url) as response: 53 | return await response.text() 54 | ``` 55 | #### Exception handling recommendations 56 | 57 | Since the latest versions of aiohttp do not respect exceptions raised in the custom `TCPConnector` (see issue [#52](https://github.com/romis2012/aiohttp-socks/issues/52)), the following pattern can be used to handle `ProxyConnectionError` and `ProxyTimeoutError` exceptions 58 | 59 | ```python 60 | from aiohttp_socks import ( 61 | ProxyConnectionError, 62 | ProxyTimeoutError, 63 | ) 64 | 65 | def is_proxy_connection_error(e: Exception): 66 | return isinstance(e, ProxyConnectionError) or isinstance( 67 | e.__cause__, ProxyConnectionError 68 | ) 69 | 70 | 71 | def is_proxy_timeout_error(e: Exception): 72 | return isinstance(e, ProxyTimeoutError) or isinstance( 73 | e.__cause__, ProxyTimeoutError 74 | ) 75 | 76 | 77 | try: 78 | await fetch(...) 79 | except Exception as e: 80 | if is_proxy_connection_error(e): 81 | ...do something useful... 82 | 83 | ``` 84 | 85 | ## Why yet another SOCKS connector for aiohttp 86 | 87 | Unlike [aiosocksy](https://github.com/romis2012/aiosocksy), aiohttp_socks has only single point of integration with aiohttp. 88 | This makes it easier to maintain compatibility with new aiohttp versions. 89 | 90 | 91 | -------------------------------------------------------------------------------- /aiohttp_socks/__init__.py: -------------------------------------------------------------------------------- 1 | __title__ = 'aiohttp-socks' 2 | __version__ = '0.10.1' 3 | 4 | from python_socks import ( 5 | ProxyError, 6 | ProxyTimeoutError, 7 | ProxyConnectionError, 8 | ProxyType 9 | ) 10 | 11 | from .connector import ( 12 | ProxyConnector, 13 | ChainProxyConnector, 14 | ProxyInfo 15 | ) 16 | from .utils import open_connection, create_connection 17 | 18 | from ._deprecated import ( 19 | SocksVer, 20 | SocksConnector, 21 | SocksConnectionError, 22 | SocksError 23 | ) 24 | 25 | __all__ = ( 26 | '__title__', 27 | '__version__', 28 | 'ProxyConnector', 29 | 'ChainProxyConnector', 30 | 'ProxyInfo', 31 | 'ProxyType', 32 | 'ProxyError', 33 | 'ProxyConnectionError', 34 | 'ProxyTimeoutError', 35 | 'open_connection', 36 | 'create_connection', 37 | 38 | 'SocksVer', 39 | 'SocksConnector', 40 | 'SocksError', 41 | 'SocksConnectionError', 42 | ) 43 | -------------------------------------------------------------------------------- /aiohttp_socks/_deprecated.py: -------------------------------------------------------------------------------- 1 | import warnings 2 | 3 | from python_socks import ( 4 | ProxyError, 5 | ProxyConnectionError, 6 | ProxyType 7 | ) 8 | 9 | from .connector import ProxyConnector 10 | 11 | 12 | class SocksVer(object): 13 | SOCKS4 = 1 14 | SOCKS5 = 2 15 | 16 | 17 | def _warn_about_connector(): 18 | warnings.warn('SocksConnector is deprecated. ' 19 | 'Use ProxyConnector instead.', DeprecationWarning, 20 | stacklevel=3) 21 | 22 | 23 | class SocksConnector(ProxyConnector): 24 | def __init__(self, socks_ver=SocksVer.SOCKS5, **kwargs): 25 | _warn_about_connector() # noqa 26 | 27 | if 'proxy_type' in kwargs: # from_url 28 | super().__init__(**kwargs) 29 | else: 30 | super().__init__(proxy_type=ProxyType(socks_ver), **kwargs) 31 | 32 | @classmethod 33 | def from_url(cls, url, **kwargs): 34 | _warn_about_connector() # noqa 35 | return super().from_url(url, **kwargs) 36 | 37 | 38 | SocksError = ProxyError 39 | SocksConnectionError = ProxyConnectionError 40 | -------------------------------------------------------------------------------- /aiohttp_socks/connector.py: -------------------------------------------------------------------------------- 1 | import asyncio 2 | import socket 3 | from ssl import SSLContext 4 | from typing import Any, Iterable, NamedTuple, Optional, List, Tuple 5 | 6 | from aiohttp import ClientConnectorError, TCPConnector 7 | from aiohttp.abc import AbstractResolver, ResolveResult 8 | from aiohttp.client_proto import ResponseHandler 9 | from python_socks import ProxyType, parse_proxy_url 10 | from python_socks.async_.asyncio.v2 import Proxy 11 | 12 | 13 | class NoResolver(AbstractResolver): 14 | async def resolve( 15 | self, 16 | host: str, 17 | port: int = 0, 18 | family: socket.AddressFamily = socket.AF_INET, # pylint: disable=no-member 19 | ) -> List[ResolveResult]: 20 | return [ 21 | { 22 | 'hostname': host, 23 | 'host': host, 24 | 'port': port, 25 | 'family': family, 26 | 'proto': 0, 27 | 'flags': 0, 28 | } 29 | ] 30 | 31 | async def close(self): 32 | pass # pragma: no cover 33 | 34 | 35 | class _ResponseHandler(ResponseHandler): 36 | """ 37 | To fix issue https://github.com/romis2012/aiohttp-socks/issues/27 38 | In Python>=3.11.5 we need to keep a reference to the StreamWriter 39 | so that the underlying transport is not closed during garbage collection. 40 | See StreamWriter.__del__ method (was added in Python 3.11.5) 41 | """ 42 | 43 | def __init__(self, loop: asyncio.AbstractEventLoop, writer: asyncio.StreamWriter) -> None: 44 | super().__init__(loop) 45 | self._writer = writer 46 | 47 | 48 | class _BaseProxyConnector(TCPConnector): 49 | async def _wrap_create_connection( 50 | self, 51 | *args, 52 | addr_infos, 53 | req, 54 | timeout, 55 | client_error=ClientConnectorError, 56 | **kwargs, 57 | ) -> Tuple[asyncio.Transport, ResponseHandler]: 58 | try: 59 | host: str = addr_infos[0][4][0] 60 | port: int = addr_infos[0][4][1] 61 | except IndexError as e: # pragma: no cover 62 | raise ValueError('Invalid arg: `addr_infos`') from e 63 | 64 | ssl: Optional[SSLContext] = kwargs.get('ssl') # type: ignore 65 | 66 | return await self._connect_via_proxy( 67 | host=host, 68 | port=port, 69 | ssl=ssl, 70 | timeout=timeout.sock_connect, 71 | ) 72 | 73 | async def _connect_via_proxy( 74 | self, 75 | host: str, 76 | port: int, 77 | ssl: Optional[SSLContext] = None, 78 | timeout: Optional[float] = None, 79 | ) -> Tuple[asyncio.Transport, ResponseHandler]: 80 | raise NotImplementedError 81 | 82 | 83 | class ProxyConnector(_BaseProxyConnector): 84 | def __init__( 85 | self, 86 | host: str, 87 | port: int, 88 | proxy_type: ProxyType = ProxyType.SOCKS5, 89 | username: Optional[str] = None, 90 | password: Optional[str] = None, 91 | rdns: Optional[bool] = None, 92 | proxy_ssl: Optional[SSLContext] = None, 93 | **kwargs: Any, 94 | ) -> None: 95 | kwargs['resolver'] = NoResolver() 96 | super().__init__(**kwargs) 97 | 98 | self._proxy_type = proxy_type 99 | self._proxy_host = host 100 | self._proxy_port = port 101 | self._proxy_username = username 102 | self._proxy_password = password 103 | self._rdns = rdns 104 | self._proxy_ssl = proxy_ssl 105 | 106 | async def _connect_via_proxy( 107 | self, 108 | host: str, 109 | port: int, 110 | ssl: Optional[SSLContext] = None, 111 | timeout: Optional[float] = None, 112 | ) -> Tuple[asyncio.Transport, ResponseHandler]: 113 | proxy = Proxy( 114 | proxy_type=self._proxy_type, 115 | host=self._proxy_host, 116 | port=self._proxy_port, 117 | username=self._proxy_username, 118 | password=self._proxy_password, 119 | rdns=self._rdns, 120 | proxy_ssl=self._proxy_ssl, 121 | ) 122 | 123 | stream = await proxy.connect( 124 | dest_host=host, 125 | dest_port=port, 126 | dest_ssl=ssl, 127 | timeout=timeout, 128 | ) 129 | 130 | transport: asyncio.Transport = stream.writer.transport 131 | protocol: ResponseHandler = _ResponseHandler( 132 | loop=self._loop, 133 | writer=stream.writer, 134 | ) 135 | 136 | transport.set_protocol(protocol) 137 | protocol.connection_made(transport) 138 | 139 | return transport, protocol 140 | 141 | @classmethod 142 | def from_url(cls, url: str, **kwargs: Any) -> 'ProxyConnector': 143 | proxy_type, host, port, username, password = parse_proxy_url(url) 144 | return cls( 145 | proxy_type=proxy_type, 146 | host=host, 147 | port=port, 148 | username=username, 149 | password=password, 150 | **kwargs, 151 | ) 152 | 153 | 154 | class ProxyInfo(NamedTuple): 155 | proxy_type: ProxyType 156 | host: str 157 | port: int 158 | username: Optional[str] = None 159 | password: Optional[str] = None 160 | rdns: Optional[bool] = None 161 | 162 | 163 | class ChainProxyConnector(_BaseProxyConnector): 164 | def __init__(self, proxy_infos: Iterable[ProxyInfo], **kwargs): 165 | kwargs['resolver'] = NoResolver() 166 | super().__init__(**kwargs) 167 | 168 | self._proxy_infos = proxy_infos 169 | 170 | async def _connect_via_proxy( 171 | self, 172 | host: str, 173 | port: int, 174 | ssl: Optional[SSLContext] = None, 175 | timeout: Optional[float] = None, 176 | ) -> Tuple[asyncio.Transport, ResponseHandler]: 177 | forward = None 178 | proxy = None 179 | for info in self._proxy_infos: 180 | proxy = Proxy( 181 | proxy_type=info.proxy_type, 182 | host=info.host, 183 | port=info.port, 184 | username=info.username, 185 | password=info.password, 186 | rdns=info.rdns, 187 | forward=forward, 188 | ) 189 | forward = proxy 190 | 191 | assert proxy is not None 192 | 193 | stream = await proxy.connect( 194 | dest_host=host, 195 | dest_port=port, 196 | dest_ssl=ssl, 197 | timeout=timeout, 198 | ) 199 | 200 | transport: asyncio.Transport = stream.writer.transport 201 | protocol: ResponseHandler = _ResponseHandler( 202 | loop=self._loop, 203 | writer=stream.writer, 204 | ) 205 | 206 | transport.set_protocol(protocol) 207 | protocol.connection_made(transport) 208 | 209 | return transport, protocol 210 | 211 | @classmethod 212 | def from_urls(cls, urls: Iterable[str], **kwargs: Any) -> 'ChainProxyConnector': 213 | infos = [] 214 | for url in urls: 215 | proxy_type, host, port, username, password = parse_proxy_url(url) 216 | proxy_info = ProxyInfo( 217 | proxy_type=proxy_type, 218 | host=host, 219 | port=port, 220 | username=username, 221 | password=password, 222 | ) 223 | infos.append(proxy_info) 224 | 225 | return cls(infos, **kwargs) 226 | -------------------------------------------------------------------------------- /aiohttp_socks/py.typed: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/romis2012/aiohttp-socks/a0bb32b924e2d622543b8d3d9e44d0c248e9b62f/aiohttp_socks/py.typed -------------------------------------------------------------------------------- /aiohttp_socks/utils.py: -------------------------------------------------------------------------------- 1 | import asyncio 2 | import warnings 3 | 4 | from python_socks import ProxyType, parse_proxy_url 5 | from python_socks.async_.asyncio import Proxy 6 | 7 | 8 | async def open_connection( 9 | proxy_url=None, 10 | host=None, 11 | port=None, 12 | *, 13 | proxy_type=ProxyType.SOCKS5, 14 | proxy_host='127.0.0.1', 15 | proxy_port=1080, 16 | username=None, 17 | password=None, 18 | rdns=True, 19 | loop=None, 20 | **kwargs, 21 | ): 22 | warnings.warn( 23 | 'open_connection is deprecated. ' 24 | 'Use https://github.com/romis2012/python-socks directly instead.', 25 | DeprecationWarning, 26 | stacklevel=2, 27 | ) 28 | 29 | if host is None or port is None: 30 | raise ValueError('host and port must be specified') # pragma: no cover 31 | 32 | if loop is None: 33 | loop = asyncio.get_event_loop() 34 | 35 | if proxy_url is not None: 36 | proxy_type, proxy_host, proxy_port, username, password = parse_proxy_url(proxy_url) 37 | 38 | proxy = Proxy.create( 39 | proxy_type=proxy_type, 40 | host=proxy_host, 41 | port=proxy_port, 42 | username=username, 43 | password=password, 44 | rdns=rdns, 45 | loop=loop, 46 | ) 47 | 48 | sock = await proxy.connect(host, port) 49 | 50 | # noinspection PyTypeChecker 51 | return await asyncio.open_connection(host=None, port=None, sock=sock, **kwargs) 52 | 53 | 54 | async def create_connection( 55 | proxy_url=None, 56 | protocol_factory=None, 57 | host=None, 58 | port=None, 59 | *, 60 | proxy_type=ProxyType.SOCKS5, 61 | proxy_host='127.0.0.1', 62 | proxy_port=1080, 63 | username=None, 64 | password=None, 65 | rdns=True, 66 | loop=None, 67 | **kwargs, 68 | ): 69 | warnings.warn( 70 | 'create_connection is deprecated. ' 71 | 'Use https://github.com/romis2012/python-socks directly instead.', 72 | DeprecationWarning, 73 | stacklevel=2, 74 | ) 75 | 76 | if protocol_factory is None: 77 | raise ValueError('protocol_factory must be specified') # pragma: no cover 78 | 79 | if host is None or port is None: 80 | raise ValueError('host and port must be specified') # pragma: no cover 81 | 82 | if loop is None: 83 | loop = asyncio.get_event_loop() 84 | 85 | if proxy_url is not None: 86 | proxy_type, proxy_host, proxy_port, username, password = parse_proxy_url(proxy_url) 87 | 88 | proxy = Proxy.create( 89 | proxy_type=proxy_type, 90 | host=proxy_host, 91 | port=proxy_port, 92 | username=username, 93 | password=password, 94 | rdns=rdns, 95 | loop=loop, 96 | ) 97 | 98 | sock = await proxy.connect(host, port) 99 | 100 | return await loop.create_connection( 101 | protocol_factory=protocol_factory, host=None, port=None, sock=sock, **kwargs 102 | ) 103 | -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- 1 | [build-system] 2 | requires = ['setuptools'] 3 | build-backend = 'setuptools.build_meta' 4 | 5 | [project] 6 | name = 'aiohttp_socks' 7 | license = { text = 'Apache-2.0' } 8 | description = 'Proxy connector for aiohttp' 9 | readme = 'README.md' 10 | authors = [{ name = 'Roman Snegirev', email = 'snegiryev@gmail.com' }] 11 | keywords = ['asyncio', 'aiohttp', 'socks', 'socks5', 'socks4', 'http', 'proxy'] 12 | requires-python = ">=3.8.0" 13 | dependencies = ['aiohttp>=3.10.0', 'python-socks[asyncio]>=2.4.3,<3.0.0'] 14 | dynamic = ['version'] 15 | classifiers = [ 16 | "Development Status :: 4 - Beta", 17 | "Programming Language :: Python", 18 | "Programming Language :: Python :: 3", 19 | "Programming Language :: Python :: 3 :: Only", 20 | "Programming Language :: Python :: 3.8", 21 | "Programming Language :: Python :: 3.9", 22 | "Programming Language :: Python :: 3.10", 23 | "Programming Language :: Python :: 3.11", 24 | "Programming Language :: Python :: 3.12", 25 | "Programming Language :: Python :: 3.13", 26 | "Operating System :: MacOS", 27 | "Operating System :: Microsoft", 28 | "Operating System :: POSIX :: Linux", 29 | "Topic :: Internet :: WWW/HTTP", 30 | "Intended Audience :: Developers", 31 | "Framework :: AsyncIO", 32 | "License :: OSI Approved :: Apache Software License", 33 | ] 34 | 35 | [project.urls] 36 | homepage = 'https://github.com/romis2012/aiohttp-socks' 37 | repository = 'https://github.com/romis2012/aiohttp-socks' 38 | 39 | [tool.setuptools.dynamic] 40 | version = { attr = 'aiohttp_socks.__version__' } 41 | 42 | [tool.setuptools.packages.find] 43 | include = ['aiohttp_socks*'] 44 | 45 | [tool.black] 46 | line-length = 89 47 | target-version = ['py38', 'py39', 'py310', 'py311', 'py312'] 48 | skip-string-normalization = true 49 | preview = true 50 | verbose = true 51 | 52 | [tool.pytest.ini_options] 53 | asyncio_mode = 'strict' 54 | -------------------------------------------------------------------------------- /requirements-dev.txt: -------------------------------------------------------------------------------- 1 | -e . 2 | flake8>=3.9.1 3 | pytest==8.0.2 4 | pytest-cov==4.1.0 5 | pytest-asyncio==0.23.5 6 | trustme==0.9.0 7 | attrs>=19.3.0 8 | yarl>=1.4.2 9 | flask>=1.1.2 10 | anyio>=3.3.4,<5.0.0 11 | tiny-proxy>=0.1.1 12 | -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | -------------------------------------------------------------------------------- /tests/config.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | LOGIN = 'admin' 4 | PASSWORD = 'admin' 5 | 6 | PROXY_HOST_IPV4 = '127.0.0.1' 7 | PROXY_HOST_IPV6 = '::1' 8 | 9 | PROXY_HOST_NAME_IPV4 = 'ip4.proxy.example.com' 10 | PROXY_HOST_NAME_IPV6 = 'ip6.proxy.example.com' 11 | 12 | SOCKS5_PROXY_PORT = 7780 13 | SOCKS5_PROXY_PORT_NO_AUTH = 7781 14 | 15 | SOCKS4_PROXY_PORT = 7782 16 | SOCKS4_PORT_NO_AUTH = 7783 17 | 18 | HTTP_PROXY_PORT = 7784 19 | 20 | SKIP_IPV6_TESTS = 'SKIP_IPV6_TESTS' in os.environ 21 | 22 | SOCKS5_IPV4_URL = 'socks5://{login}:{password}@{host}:{port}'.format( 23 | host=PROXY_HOST_IPV4, 24 | port=SOCKS5_PROXY_PORT, 25 | login=LOGIN, 26 | password=PASSWORD, 27 | ) 28 | 29 | SOCKS5_IPV6_URL = 'socks5://{login}:{password}@{host}:{port}'.format( 30 | host='[%s]' % PROXY_HOST_IPV6, 31 | port=SOCKS5_PROXY_PORT, 32 | login=LOGIN, 33 | password=PASSWORD, 34 | ) 35 | 36 | SOCKS5_IPV4_HOSTNAME_URL = 'socks5://{login}:{password}@{host}:{port}'.format( 37 | host=PROXY_HOST_NAME_IPV4, 38 | port=SOCKS5_PROXY_PORT, 39 | login=LOGIN, 40 | password=PASSWORD, 41 | ) 42 | 43 | SOCKS5_IPV4_URL_WO_AUTH = 'socks5://{host}:{port}'.format( 44 | host=PROXY_HOST_IPV4, port=SOCKS5_PROXY_PORT_NO_AUTH 45 | ) 46 | 47 | SOCKS4_URL = 'socks4://{login}:{password}@{host}:{port}'.format( 48 | host=PROXY_HOST_IPV4, 49 | port=SOCKS4_PROXY_PORT, 50 | login=LOGIN, 51 | password='', 52 | ) 53 | 54 | HTTP_PROXY_URL = 'http://{login}:{password}@{host}:{port}'.format( 55 | host=PROXY_HOST_IPV4, 56 | port=HTTP_PROXY_PORT, 57 | login=LOGIN, 58 | password=PASSWORD, 59 | ) 60 | 61 | TEST_HOST_IPV4 = '127.0.0.1' 62 | TEST_HOST_IPV6 = '::1' 63 | 64 | TEST_HOST_NAME_IPV4 = 'ip4.target.example.com' 65 | TEST_HOST_NAME_IPV6 = 'ip6.target.example.com' 66 | 67 | TEST_PORT_IPV4 = 8889 68 | TEST_PORT_IPV6 = 8889 69 | 70 | TEST_PORT_IPV4_HTTPS = 8890 71 | 72 | TEST_URL_IPV4 = 'http://{host}:{port}/ip'.format(host=TEST_HOST_NAME_IPV4, port=TEST_PORT_IPV4) 73 | 74 | TEST_URL_IPv6 = 'http://{host}:{port}/ip'.format(host=TEST_HOST_NAME_IPV6, port=TEST_PORT_IPV6) 75 | 76 | TEST_URL_IPV4_DELAY = 'http://{host}:{port}/delay/2'.format( 77 | host=TEST_HOST_NAME_IPV4, port=TEST_PORT_IPV4 78 | ) 79 | 80 | TEST_URL_IPV4_HTTPS = 'https://{host}:{port}/ip'.format( 81 | host=TEST_HOST_NAME_IPV4, port=TEST_PORT_IPV4_HTTPS 82 | ) 83 | 84 | 85 | def resolve_path(path): 86 | return os.path.normpath(os.path.join(os.path.dirname(os.path.realpath(__file__)), path)) 87 | -------------------------------------------------------------------------------- /tests/conftest.py: -------------------------------------------------------------------------------- 1 | import ssl 2 | from unittest import mock 3 | 4 | import pytest # noqa 5 | import trustme # noqa 6 | from python_socks.async_.asyncio._resolver import Resolver as AsyncioResolver # noqa 7 | 8 | from tests.config import ( 9 | PROXY_HOST_IPV4, 10 | PROXY_HOST_IPV6, 11 | SOCKS5_PROXY_PORT, 12 | LOGIN, 13 | PASSWORD, 14 | SKIP_IPV6_TESTS, 15 | HTTP_PROXY_PORT, 16 | SOCKS4_PORT_NO_AUTH, 17 | SOCKS4_PROXY_PORT, 18 | SOCKS5_PROXY_PORT_NO_AUTH, 19 | TEST_PORT_IPV4, 20 | TEST_PORT_IPV6, 21 | TEST_HOST_IPV4, 22 | TEST_HOST_IPV6, 23 | TEST_PORT_IPV4_HTTPS, 24 | TEST_HOST_NAME_IPV4, 25 | TEST_HOST_NAME_IPV6, 26 | ) 27 | from tests.http_server import HttpServer, HttpServerConfig 28 | from tests.mocks import async_resolve_factory 29 | from tests.proxy_server import ProxyConfig, ProxyServer 30 | from tests.utils import wait_until_connectable 31 | 32 | 33 | @pytest.fixture(scope='session') 34 | def target_ssl_ca() -> trustme.CA: 35 | return trustme.CA() 36 | 37 | 38 | @pytest.fixture(scope='session') 39 | def target_ssl_cert(target_ssl_ca) -> trustme.LeafCert: 40 | return target_ssl_ca.issue_cert( 41 | 'localhost', 42 | TEST_HOST_IPV4, 43 | TEST_HOST_IPV6, 44 | TEST_HOST_NAME_IPV4, 45 | TEST_HOST_NAME_IPV6, 46 | ) 47 | 48 | 49 | @pytest.fixture(scope='session') 50 | def target_ssl_certfile(target_ssl_cert): 51 | with target_ssl_cert.cert_chain_pems[0].tempfile() as cert_path: 52 | yield cert_path 53 | 54 | 55 | @pytest.fixture(scope='session') 56 | def target_ssl_keyfile(target_ssl_cert): 57 | with target_ssl_cert.private_key_pem.tempfile() as private_key_path: 58 | yield private_key_path 59 | 60 | 61 | @pytest.fixture(scope='session') 62 | def target_ssl_context(target_ssl_ca) -> ssl.SSLContext: 63 | ssl_ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) 64 | ssl_ctx.verify_mode = ssl.CERT_REQUIRED 65 | ssl_ctx.check_hostname = True 66 | target_ssl_ca.configure_trust(ssl_ctx) 67 | return ssl_ctx 68 | 69 | 70 | @pytest.fixture(scope='session', autouse=True) 71 | def patch_resolvers(): 72 | with mock.patch.object( 73 | AsyncioResolver, attribute='resolve', new=async_resolve_factory(AsyncioResolver) 74 | ): 75 | yield None 76 | 77 | 78 | @pytest.fixture(scope='session', autouse=True) 79 | def proxy_server(): 80 | config = [ 81 | ProxyConfig( 82 | proxy_type='http', 83 | host=PROXY_HOST_IPV4, 84 | port=HTTP_PROXY_PORT, 85 | username=LOGIN, 86 | password=PASSWORD, 87 | ), 88 | ProxyConfig( 89 | proxy_type='socks4', 90 | host=PROXY_HOST_IPV4, 91 | port=SOCKS4_PROXY_PORT, 92 | username=LOGIN, 93 | password=None, 94 | ), 95 | ProxyConfig( 96 | proxy_type='socks4', 97 | host=PROXY_HOST_IPV4, 98 | port=SOCKS4_PORT_NO_AUTH, 99 | username=None, 100 | password=None, 101 | ), 102 | ProxyConfig( 103 | proxy_type='socks5', 104 | host=PROXY_HOST_IPV4, 105 | port=SOCKS5_PROXY_PORT, 106 | username=LOGIN, 107 | password=PASSWORD, 108 | ), 109 | ProxyConfig( 110 | proxy_type='socks5', 111 | host=PROXY_HOST_IPV4, 112 | port=SOCKS5_PROXY_PORT_NO_AUTH, 113 | username=None, 114 | password=None, 115 | ), 116 | ] 117 | 118 | if not SKIP_IPV6_TESTS: 119 | config.append( 120 | ProxyConfig( 121 | proxy_type='socks5', 122 | host=PROXY_HOST_IPV6, 123 | port=SOCKS5_PROXY_PORT, 124 | username=LOGIN, 125 | password=PASSWORD, 126 | ), 127 | ) 128 | 129 | server = ProxyServer(config=config) 130 | server.start() 131 | for cfg in config: 132 | wait_until_connectable(host=cfg.host, port=cfg.port, timeout=10) 133 | 134 | yield None 135 | 136 | server.terminate() 137 | 138 | 139 | @pytest.fixture(scope='session', autouse=True) 140 | def web_server(target_ssl_certfile, target_ssl_keyfile): 141 | config = [ 142 | HttpServerConfig(host=TEST_HOST_IPV4, port=TEST_PORT_IPV4), 143 | HttpServerConfig( 144 | host=TEST_HOST_IPV4, 145 | port=TEST_PORT_IPV4_HTTPS, 146 | certfile=target_ssl_certfile, 147 | keyfile=target_ssl_keyfile, 148 | ), 149 | ] 150 | 151 | if not SKIP_IPV6_TESTS: 152 | config.append(HttpServerConfig(host=TEST_HOST_IPV6, port=TEST_PORT_IPV6)) 153 | 154 | server = HttpServer(config=config) 155 | server.start() 156 | for cfg in config: 157 | server.wait_until_connectable(host=cfg.host, port=cfg.port) 158 | 159 | yield None 160 | 161 | server.terminate() 162 | -------------------------------------------------------------------------------- /tests/http_app.py: -------------------------------------------------------------------------------- 1 | import ssl 2 | import time 3 | 4 | import flask # noqa 5 | from flask import request # noqa 6 | 7 | app = flask.Flask(__name__) 8 | 9 | 10 | @app.route('/ip') 11 | def ip(): 12 | return request.remote_addr 13 | 14 | 15 | @app.route('/delay/') 16 | def delay(seconds): 17 | time.sleep(seconds) 18 | return 'ok' 19 | 20 | 21 | def run_app(host: str, port: int, certfile: str = None, keyfile: str = None): 22 | if certfile and keyfile: 23 | ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS) 24 | ssl_context.load_cert_chain(certfile, keyfile) 25 | else: 26 | ssl_context = None 27 | 28 | print('Starting http server on {}:{}...'.format(host, port)) 29 | app.run(debug=False, host=host, port=port, threaded=True, 30 | ssl_context=ssl_context) 31 | -------------------------------------------------------------------------------- /tests/http_server.py: -------------------------------------------------------------------------------- 1 | import typing 2 | import time 3 | from multiprocessing import Process 4 | 5 | from tests.utils import is_connectable 6 | from tests.http_app import run_app 7 | 8 | 9 | class HttpServerConfig(typing.NamedTuple): 10 | host: str 11 | port: int 12 | certfile: str = None 13 | keyfile: str = None 14 | 15 | def to_dict(self): 16 | d = {} 17 | for key, val in self._asdict().items(): 18 | if val is not None: 19 | d[key] = val 20 | return d 21 | 22 | 23 | class HttpServer: 24 | def __init__(self, config: typing.Iterable[HttpServerConfig]): 25 | self.config = config 26 | self.workers = [] 27 | 28 | def start(self): 29 | for cfg in self.config: 30 | p = Process(target=run_app, kwargs=cfg.to_dict()) 31 | self.workers.append(p) 32 | 33 | for p in self.workers: 34 | p.start() 35 | 36 | def terminate(self): 37 | for p in self.workers: 38 | p.terminate() 39 | 40 | def wait_until_connectable(self, host, port, timeout=10): 41 | count = 0 42 | while not is_connectable(host=host, port=port): 43 | if count >= timeout: 44 | self.terminate() 45 | raise Exception( 46 | 'The http server has not available ' 47 | 'by (%s, %s) in %d seconds' 48 | % (host, port, timeout)) 49 | count += 1 50 | time.sleep(1) 51 | return True 52 | -------------------------------------------------------------------------------- /tests/mocks.py: -------------------------------------------------------------------------------- 1 | import socket 2 | 3 | from tests.config import ( 4 | TEST_HOST_NAME_IPV4, 5 | PROXY_HOST_NAME_IPV4, 6 | TEST_HOST_NAME_IPV6, 7 | PROXY_HOST_NAME_IPV6, 8 | ) 9 | 10 | 11 | def getaddrinfo_sync_mock(): 12 | _orig_getaddrinfo = socket.getaddrinfo 13 | 14 | def getaddrinfo(host, port, family=0, type=0, proto=0, flags=0): 15 | if host in (TEST_HOST_NAME_IPV4, PROXY_HOST_NAME_IPV4): 16 | return [(socket.AF_INET, socket.SOCK_STREAM, 6, '', ('127.0.0.1', port))] 17 | 18 | if host in (TEST_HOST_NAME_IPV6, PROXY_HOST_NAME_IPV6): 19 | return [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('::1', port, 0, 0))] 20 | 21 | return _orig_getaddrinfo(host, port, family, type, proto, flags) 22 | 23 | return getaddrinfo 24 | 25 | 26 | def getaddrinfo_async_mock(origin_getaddrinfo): 27 | async def getaddrinfo(host, port, family=0, type=0, proto=0, flags=0): 28 | if host in (TEST_HOST_NAME_IPV4, PROXY_HOST_NAME_IPV4): 29 | return [(socket.AF_INET, socket.SOCK_STREAM, 6, '', ('127.0.0.1', port))] 30 | 31 | if host in (TEST_HOST_NAME_IPV6, PROXY_HOST_NAME_IPV6): 32 | return [(socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('::1', port, 0, 0))] 33 | 34 | return await origin_getaddrinfo( 35 | host, 36 | port, 37 | family=family, 38 | type=type, 39 | proto=proto, 40 | flags=flags, 41 | ) 42 | 43 | return getaddrinfo 44 | 45 | 46 | def _resolve_local(host): 47 | if host in (TEST_HOST_NAME_IPV4, PROXY_HOST_NAME_IPV4): 48 | return socket.AF_INET, '127.0.0.1' 49 | 50 | if host in (TEST_HOST_NAME_IPV6, PROXY_HOST_NAME_IPV6): 51 | return socket.AF_INET6, '::1' 52 | 53 | return None 54 | 55 | 56 | def sync_resolve_factory(cls): 57 | original_resolver = cls.resolve 58 | 59 | def new_resolver(self, host, port=0, family=socket.AF_UNSPEC): 60 | res = _resolve_local(host) 61 | 62 | if res is not None: 63 | return res 64 | 65 | return original_resolver(self, host=host, port=port, family=family) 66 | 67 | return new_resolver 68 | 69 | 70 | def async_resolve_factory(cls): 71 | original_resolver = cls.resolve 72 | 73 | async def new_resolver(self, host, port=0, family=socket.AF_UNSPEC): 74 | res = _resolve_local(host) 75 | 76 | if res is not None: 77 | return res 78 | 79 | return await original_resolver(self, host=host, port=port, family=family) 80 | 81 | return new_resolver 82 | -------------------------------------------------------------------------------- /tests/proxy_server.py: -------------------------------------------------------------------------------- 1 | import ssl 2 | import typing 3 | from multiprocessing import Process 4 | from unittest import mock 5 | 6 | import anyio 7 | from anyio import create_tcp_listener 8 | from anyio.streams.tls import TLSListener 9 | from tiny_proxy import ( 10 | HttpProxyHandler, 11 | Socks5ProxyHandler, 12 | Socks4ProxyHandler, 13 | HttpProxy, 14 | Socks4Proxy, 15 | Socks5Proxy, 16 | AbstractProxy, 17 | ) 18 | 19 | from tests.mocks import getaddrinfo_async_mock 20 | 21 | 22 | class ProxyConfig(typing.NamedTuple): 23 | proxy_type: str 24 | host: str 25 | port: int 26 | username: typing.Optional[str] = None 27 | password: typing.Optional[str] = None 28 | ssl_certfile: typing.Optional[str] = None 29 | ssl_keyfile: typing.Optional[str] = None 30 | 31 | def to_dict(self): 32 | d = {} 33 | for key, val in self._asdict().items(): 34 | if val is not None: 35 | d[key] = val 36 | return d 37 | 38 | 39 | cls_map = { 40 | 'http': HttpProxyHandler, 41 | 'socks4': Socks4ProxyHandler, 42 | 'socks5': Socks5ProxyHandler, 43 | } 44 | 45 | 46 | def connect_to_remote_factory(cls: typing.Type[AbstractProxy]): 47 | """ 48 | simulate target host connection timeout 49 | """ 50 | origin_connect_to_remote = cls.connect_to_remote 51 | 52 | async def new_connect_to_remote(self): 53 | await anyio.sleep(0.01) 54 | return await origin_connect_to_remote(self) 55 | 56 | return new_connect_to_remote 57 | 58 | 59 | @mock.patch.object( 60 | HttpProxy, 61 | attribute='connect_to_remote', 62 | new=connect_to_remote_factory(HttpProxy), 63 | ) 64 | @mock.patch.object( 65 | Socks4Proxy, 66 | attribute='connect_to_remote', 67 | new=connect_to_remote_factory(Socks4Proxy), 68 | ) 69 | @mock.patch.object( 70 | Socks5Proxy, 71 | attribute='connect_to_remote', 72 | new=connect_to_remote_factory(Socks5Proxy), 73 | ) 74 | @mock.patch('anyio._core._sockets.getaddrinfo', new=getaddrinfo_async_mock(anyio.getaddrinfo)) 75 | def start( 76 | proxy_type, 77 | host, 78 | port, 79 | ssl_certfile=None, 80 | ssl_keyfile=None, 81 | **kwargs, 82 | ): 83 | handler_cls = cls_map.get(proxy_type) 84 | if not handler_cls: 85 | raise RuntimeError(f'Unsupported type: {proxy_type}') 86 | 87 | if ssl_certfile and ssl_keyfile: 88 | ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) 89 | ssl_context.load_cert_chain(ssl_certfile, ssl_keyfile) 90 | else: 91 | ssl_context = None 92 | 93 | print(f'Starting {proxy_type} proxy on {host}:{port}...') 94 | 95 | handler = handler_cls(**kwargs) 96 | 97 | async def serve(): 98 | listener = await create_tcp_listener(local_host=host, local_port=port) 99 | if ssl_context is not None: 100 | listener = TLSListener(listener=listener, ssl_context=ssl_context) 101 | 102 | async with listener: 103 | await listener.serve(handler.handle) 104 | 105 | anyio.run(serve) 106 | 107 | 108 | class ProxyServer: 109 | workers: typing.List[Process] 110 | 111 | def __init__(self, config: typing.Iterable[ProxyConfig]): 112 | self.config = config 113 | self.workers = [] 114 | 115 | def start(self): 116 | for cfg in self.config: 117 | print( 118 | 'Starting {} proxy on {}:{}; certfile={}, keyfile={}...'.format( 119 | cfg.proxy_type, 120 | cfg.host, 121 | cfg.port, 122 | cfg.ssl_certfile, 123 | cfg.ssl_keyfile, 124 | ) 125 | ) 126 | 127 | p = Process(target=start, kwargs=cfg.to_dict(), daemon=True) 128 | self.workers.append(p) 129 | 130 | for p in self.workers: 131 | p.start() 132 | 133 | def terminate(self): 134 | for p in self.workers: 135 | p.terminate() 136 | -------------------------------------------------------------------------------- /tests/test_connector.py: -------------------------------------------------------------------------------- 1 | import asyncio 2 | import ssl 3 | 4 | import aiohttp 5 | import pytest # noqa 6 | from aiohttp import ClientResponse, TCPConnector 7 | from yarl import URL # noqa 8 | 9 | from aiohttp_socks import ( 10 | ProxyType, 11 | ProxyConnector, 12 | ChainProxyConnector, 13 | ProxyInfo, 14 | ProxyError, 15 | ProxyConnectionError, 16 | ProxyTimeoutError, 17 | open_connection, 18 | create_connection, 19 | ) 20 | from tests.config import ( 21 | TEST_URL_IPV4, 22 | SOCKS5_IPV4_URL, 23 | PROXY_HOST_IPV4, 24 | SOCKS5_PROXY_PORT, 25 | LOGIN, 26 | PASSWORD, 27 | TEST_URL_IPV4_DELAY, 28 | SKIP_IPV6_TESTS, 29 | SOCKS5_IPV6_URL, 30 | SOCKS4_URL, 31 | HTTP_PROXY_URL, 32 | SOCKS4_PROXY_PORT, 33 | HTTP_PROXY_PORT, 34 | TEST_URL_IPV4_HTTPS, 35 | ) 36 | 37 | 38 | def is_proxy_connection_error(e: Exception): 39 | return isinstance(e, ProxyConnectionError) or isinstance( 40 | e.__cause__, ProxyConnectionError 41 | ) 42 | 43 | 44 | def is_proxy_timeout_error(e: Exception): 45 | return isinstance(e, ProxyTimeoutError) or isinstance(e.__cause__, ProxyTimeoutError) 46 | 47 | 48 | async def fetch( 49 | connector: TCPConnector, 50 | url: str, 51 | timeout=None, 52 | ssl_context=None, 53 | ) -> ClientResponse: 54 | url = URL(url) 55 | 56 | if url.scheme == 'https': 57 | dest_ssl = ssl_context 58 | else: 59 | dest_ssl = None 60 | 61 | async with aiohttp.ClientSession(connector=connector) as session: 62 | async with session.get(url, ssl=dest_ssl, timeout=timeout) as resp: 63 | return resp 64 | 65 | 66 | @pytest.mark.parametrize('url', (TEST_URL_IPV4, TEST_URL_IPV4_HTTPS)) 67 | @pytest.mark.parametrize('rdns', (True, False)) 68 | @pytest.mark.asyncio 69 | async def test_socks5_proxy_ipv4(url, rdns, target_ssl_context): 70 | connector = ProxyConnector.from_url(SOCKS5_IPV4_URL, rdns=rdns) 71 | res = await fetch( 72 | connector=connector, 73 | url=url, 74 | ssl_context=target_ssl_context, 75 | ) 76 | assert res.status == 200 77 | 78 | 79 | @pytest.mark.asyncio 80 | async def test_socks5_proxy_with_invalid_credentials(target_ssl_context): 81 | connector = ProxyConnector( 82 | proxy_type=ProxyType.SOCKS5, 83 | host=PROXY_HOST_IPV4, 84 | port=SOCKS5_PROXY_PORT, 85 | username=LOGIN, 86 | password=PASSWORD + 'aaa', 87 | ) 88 | with pytest.raises(ProxyError): 89 | await fetch( 90 | connector=connector, 91 | url=TEST_URL_IPV4, 92 | ssl_context=target_ssl_context, 93 | ) 94 | 95 | 96 | @pytest.mark.asyncio 97 | async def test_socks5_proxy_with_timeout(target_ssl_context): 98 | connector = ProxyConnector( 99 | proxy_type=ProxyType.SOCKS5, 100 | host=PROXY_HOST_IPV4, 101 | port=SOCKS5_PROXY_PORT, 102 | username=LOGIN, 103 | password=PASSWORD, 104 | ) 105 | with pytest.raises(asyncio.TimeoutError): 106 | await fetch( 107 | connector=connector, 108 | url=TEST_URL_IPV4_DELAY, 109 | timeout=1, 110 | ssl_context=target_ssl_context, 111 | ) 112 | 113 | 114 | @pytest.mark.asyncio 115 | async def test_socks5_proxy_with_proxy_connect_timeout(target_ssl_context): 116 | connector = ProxyConnector.from_url(SOCKS5_IPV4_URL) 117 | timeout = aiohttp.ClientTimeout(total=32, sock_connect=0.001) 118 | # with pytest.raises(ProxyTimeoutError): 119 | with pytest.raises(Exception) as exc_info: 120 | await fetch( 121 | connector=connector, 122 | url=TEST_URL_IPV4, 123 | timeout=timeout, 124 | ssl_context=target_ssl_context, 125 | ) 126 | assert is_proxy_timeout_error(exc_info.value) 127 | 128 | 129 | @pytest.mark.asyncio 130 | async def test_socks5_proxy_with_invalid_proxy_port(unused_tcp_port, target_ssl_context): 131 | connector = ProxyConnector( 132 | proxy_type=ProxyType.SOCKS5, 133 | host=PROXY_HOST_IPV4, 134 | port=unused_tcp_port, 135 | username=LOGIN, 136 | password=PASSWORD, 137 | ) 138 | # with pytest.raises(ProxyConnectionError): 139 | with pytest.raises(Exception) as exc_info: 140 | await fetch( 141 | connector=connector, 142 | url=TEST_URL_IPV4, 143 | ssl_context=target_ssl_context, 144 | ) 145 | assert is_proxy_connection_error(exc_info.value) 146 | 147 | 148 | @pytest.mark.parametrize('url', (TEST_URL_IPV4, TEST_URL_IPV4_HTTPS)) 149 | @pytest.mark.skipif(SKIP_IPV6_TESTS, reason="TravisCI doesn't support ipv6") 150 | @pytest.mark.asyncio 151 | async def test_socks5_proxy_ipv6(url, target_ssl_context): 152 | connector = ProxyConnector.from_url(SOCKS5_IPV6_URL) 153 | res = await fetch( 154 | connector=connector, 155 | url=url, 156 | ssl_context=target_ssl_context, 157 | ) 158 | assert res.status == 200 159 | 160 | 161 | @pytest.mark.parametrize('url', (TEST_URL_IPV4, TEST_URL_IPV4_HTTPS)) 162 | @pytest.mark.parametrize('rdns', (True, False)) 163 | @pytest.mark.asyncio 164 | async def test_socks4_proxy(url, rdns, target_ssl_context): 165 | connector = ProxyConnector.from_url( 166 | SOCKS4_URL, 167 | rdns=rdns, 168 | ) 169 | res = await fetch( 170 | connector=connector, 171 | url=url, 172 | ssl_context=target_ssl_context, 173 | ) 174 | assert res.status == 200 175 | 176 | 177 | @pytest.mark.parametrize('url', (TEST_URL_IPV4, TEST_URL_IPV4_HTTPS)) 178 | @pytest.mark.asyncio 179 | async def test_http_proxy(url, target_ssl_context): 180 | connector = ProxyConnector.from_url(HTTP_PROXY_URL) 181 | res = await fetch( 182 | connector=connector, 183 | url=url, 184 | ssl_context=target_ssl_context, 185 | ) 186 | assert res.status == 200 187 | 188 | 189 | @pytest.mark.parametrize('url', (TEST_URL_IPV4, TEST_URL_IPV4_HTTPS)) 190 | @pytest.mark.asyncio 191 | async def test_chain_proxy_from_url(url, target_ssl_context): 192 | connector = ChainProxyConnector.from_urls( 193 | [SOCKS5_IPV4_URL, SOCKS4_URL, HTTP_PROXY_URL] 194 | ) 195 | res = await fetch( 196 | connector=connector, 197 | url=url, 198 | ssl_context=target_ssl_context, 199 | ) 200 | assert res.status == 200 201 | 202 | 203 | @pytest.mark.parametrize('url', (TEST_URL_IPV4, TEST_URL_IPV4_HTTPS)) 204 | @pytest.mark.parametrize('rdns', (True, False)) 205 | @pytest.mark.asyncio 206 | async def test_chain_proxy_ctor(url, rdns, target_ssl_context): 207 | connector = ChainProxyConnector( 208 | [ 209 | ProxyInfo( 210 | proxy_type=ProxyType.SOCKS5, 211 | host=PROXY_HOST_IPV4, 212 | port=SOCKS5_PROXY_PORT, 213 | username=LOGIN, 214 | password=PASSWORD, 215 | rdns=rdns, 216 | ), 217 | ProxyInfo( 218 | proxy_type=ProxyType.SOCKS4, 219 | host=PROXY_HOST_IPV4, 220 | port=SOCKS4_PROXY_PORT, 221 | username=LOGIN, 222 | rdns=rdns, 223 | ), 224 | ProxyInfo( 225 | proxy_type=ProxyType.HTTP, 226 | host=PROXY_HOST_IPV4, 227 | port=HTTP_PROXY_PORT, 228 | username=LOGIN, 229 | password=PASSWORD, 230 | ), 231 | ] 232 | ) 233 | res = await fetch( 234 | connector=connector, 235 | url=url, 236 | ssl_context=target_ssl_context, 237 | ) 238 | assert res.status == 200 239 | 240 | 241 | @pytest.mark.parametrize('url', (TEST_URL_IPV4, TEST_URL_IPV4_HTTPS)) 242 | @pytest.mark.parametrize('rdns', (True, False)) 243 | @pytest.mark.asyncio 244 | async def test_socks5_open_connection(url, rdns, target_ssl_context): 245 | url = URL(url) 246 | 247 | ssl_context = None 248 | if url.scheme == 'https': 249 | ssl_context = target_ssl_context 250 | 251 | reader, writer = await open_connection( 252 | proxy_url=SOCKS5_IPV4_URL, 253 | host=url.host, 254 | port=url.port, 255 | ssl=ssl_context, 256 | server_hostname=url.host if ssl_context else None, 257 | rdns=rdns, 258 | ) 259 | request = "GET %s HTTP/1.1\r\nHost: %s\r\nConnection: close\r\n\r\n" % ( 260 | url.path_qs, 261 | url.host, 262 | ) 263 | 264 | writer.write(request.encode()) 265 | response = await reader.read(-1) 266 | assert b'200 OK' in response 267 | 268 | 269 | @pytest.mark.parametrize('url', (TEST_URL_IPV4, TEST_URL_IPV4_HTTPS)) 270 | @pytest.mark.parametrize('rdns', (True, False)) 271 | @pytest.mark.asyncio 272 | async def test_socks5_http_create_connection( 273 | url: str, 274 | rdns: bool, 275 | event_loop: asyncio.AbstractEventLoop, 276 | target_ssl_context: ssl.SSLContext, 277 | ): 278 | url = URL(url) 279 | 280 | ssl_context = None 281 | if url.scheme == 'https': 282 | ssl_context = target_ssl_context 283 | 284 | reader = asyncio.StreamReader(loop=event_loop) 285 | protocol = asyncio.StreamReaderProtocol(reader, loop=event_loop) 286 | 287 | transport, _ = await create_connection( 288 | proxy_url=SOCKS5_IPV4_URL, 289 | protocol_factory=lambda: protocol, 290 | host=url.host, 291 | port=url.port, 292 | ssl=ssl_context, 293 | server_hostname=url.host if ssl_context else None, 294 | rdns=rdns, 295 | ) 296 | 297 | writer = asyncio.StreamWriter(transport, protocol, reader, event_loop) 298 | 299 | request = "GET %s HTTP/1.1\r\nHost: %s\r\nConnection: close\r\n\r\n" % ( 300 | url.path_qs, 301 | url.host, 302 | ) 303 | 304 | writer.write(request.encode()) 305 | response = await reader.read(-1) 306 | assert b'200 OK' in response 307 | -------------------------------------------------------------------------------- /tests/utils.py: -------------------------------------------------------------------------------- 1 | import socket 2 | import time 3 | 4 | 5 | def is_connectable(host, port): 6 | try: 7 | sock = socket.create_connection((host, port), 1) 8 | except socket.error: 9 | return False 10 | else: 11 | sock.close() 12 | return True 13 | 14 | 15 | def wait_until_connectable(host, port, timeout=10): 16 | count = 0 17 | while not is_connectable(host=host, port=port): 18 | if count >= timeout: 19 | raise Exception( 20 | f'The proxy server has not available by ({host}, {port}) in {timeout:d} seconds' 21 | ) 22 | count += 1 23 | time.sleep(1) 24 | return True 25 | --------------------------------------------------------------------------------