| 1 |
""" |
| 2 |
This module contains provisional support for SOCKS proxies from within |
| 3 |
urllib3. This module supports SOCKS4, SOCKS4A (an extension of SOCKS4), and |
| 4 |
SOCKS5. To enable its functionality, either install PySocks or install this |
| 5 |
module with the ``socks`` extra. |
| 6 |
|
| 7 |
The SOCKS implementation supports the full range of urllib3 features. It also |
| 8 |
supports the following SOCKS features: |
| 9 |
|
| 10 |
- SOCKS4A (``proxy_url='socks4a://...``) |
| 11 |
- SOCKS4 (``proxy_url='socks4://...``) |
| 12 |
- SOCKS5 with remote DNS (``proxy_url='socks5h://...``) |
| 13 |
- SOCKS5 with local DNS (``proxy_url='socks5://...``) |
| 14 |
- Usernames and passwords for the SOCKS proxy |
| 15 |
|
| 16 |
.. note:: |
| 17 |
It is recommended to use ``socks5h://`` or ``socks4a://`` schemes in |
| 18 |
your ``proxy_url`` to ensure that DNS resolution is done from the remote |
| 19 |
server instead of client-side when connecting to a domain name. |
| 20 |
|
| 21 |
SOCKS4 supports IPv4 and domain names with the SOCKS4A extension. SOCKS5 |
| 22 |
supports IPv4, IPv6, and domain names. |
| 23 |
|
| 24 |
When connecting to a SOCKS4 proxy the ``username`` portion of the ``proxy_url`` |
| 25 |
will be sent as the ``userid`` section of the SOCKS request: |
| 26 |
|
| 27 |
.. code-block:: python |
| 28 |
|
| 29 |
proxy_url="socks4a://<userid>@proxy-host" |
| 30 |
|
| 31 |
When connecting to a SOCKS5 proxy the ``username`` and ``password`` portion |
| 32 |
of the ``proxy_url`` will be sent as the username/password to authenticate |
| 33 |
with the proxy: |
| 34 |
|
| 35 |
.. code-block:: python |
| 36 |
|
| 37 |
proxy_url="socks5h://<username>:<password>@proxy-host" |
| 38 |
|
| 39 |
""" |
| 40 |
|
| 41 |
from __future__ import annotations |
| 42 |
|
| 43 |
try: |
| 44 |
import socks |
| 45 |
except ImportError: |
| 46 |
import warnings |
| 47 |
|
| 48 |
from ..exceptions import DependencyWarning |
| 49 |
|
| 50 |
warnings.warn( |
| 51 |
( |
| 52 |
"SOCKS support in urllib3 requires the installation of optional " |
| 53 |
"dependencies: specifically, PySocks. For more information, see " |
| 54 |
"https://urllib3.readthedocs.io/en/latest/advanced-usage.html#socks-proxies" |
| 55 |
), |
| 56 |
DependencyWarning, |
| 57 |
) |
| 58 |
raise |
| 59 |
|
| 60 |
import typing |
| 61 |
from socket import timeout as SocketTimeout |
| 62 |
|
| 63 |
from ..connection import HTTPConnection, HTTPSConnection |
| 64 |
from ..connectionpool import HTTPConnectionPool, HTTPSConnectionPool |
| 65 |
from ..exceptions import ConnectTimeoutError, NewConnectionError |
| 66 |
from ..poolmanager import PoolManager |
| 67 |
from ..util.url import parse_url |
| 68 |
|
| 69 |
try: |
| 70 |
import ssl |
| 71 |
except ImportError: |
| 72 |
ssl = None # type: ignore[assignment] |
| 73 |
|
| 74 |
|
| 75 |
class _TYPE_SOCKS_OPTIONS(typing.TypedDict): |
| 76 |
socks_version: int |
| 77 |
proxy_host: str | None |
| 78 |
proxy_port: str | None |
| 79 |
username: str | None |
| 80 |
password: str | None |
| 81 |
rdns: bool |
| 82 |
|
| 83 |
|
| 84 |
class SOCKSConnection(HTTPConnection): |
| 85 |
""" |
| 86 |
A plain-text HTTP connection that connects via a SOCKS proxy. |
| 87 |
""" |
| 88 |
|
| 89 |
def __init__( |
| 90 |
self, |
| 91 |
_socks_options: _TYPE_SOCKS_OPTIONS, |
| 92 |
*args: typing.Any, |
| 93 |
**kwargs: typing.Any, |
| 94 |
) -> None: |
| 95 |
self._socks_options = _socks_options |
| 96 |
super().__init__(*args, **kwargs) |
| 97 |
|
| 98 |
def _new_conn(self) -> socks.socksocket: |
| 99 |
""" |
| 100 |
Establish a new connection via the SOCKS proxy. |
| 101 |
""" |
| 102 |
extra_kw: dict[str, typing.Any] = {} |
| 103 |
if self.source_address: |
| 104 |
extra_kw["source_address"] = self.source_address |
| 105 |
|
| 106 |
if self.socket_options: |
| 107 |
extra_kw["socket_options"] = self.socket_options |
| 108 |
|
| 109 |
try: |
| 110 |
conn = socks.create_connection( |
| 111 |
(self.host, self.port), |
| 112 |
proxy_type=self._socks_options["socks_version"], |
| 113 |
proxy_addr=self._socks_options["proxy_host"], |
| 114 |
proxy_port=self._socks_options["proxy_port"], # type: ignore[arg-type] |
| 115 |
proxy_username=self._socks_options["username"], |
| 116 |
proxy_password=self._socks_options["password"], |
| 117 |
proxy_rdns=self._socks_options["rdns"], |
| 118 |
timeout=self.timeout, # type: ignore[arg-type] |
| 119 |
**extra_kw, |
| 120 |
) |
| 121 |
|
| 122 |
except SocketTimeout as e: |
| 123 |
raise ConnectTimeoutError( |
| 124 |
self, |
| 125 |
f"Connection to {self.host} timed out. (connect timeout={self.timeout})", |
| 126 |
) from e |
| 127 |
|
| 128 |
except socks.ProxyError as e: |
| 129 |
# This is fragile as hell, but it seems to be the only way to raise |
| 130 |
# useful errors here. |
| 131 |
if e.socket_err: |
| 132 |
error = e.socket_err |
| 133 |
if isinstance(error, SocketTimeout): |
| 134 |
raise ConnectTimeoutError( |
| 135 |
self, |
| 136 |
f"Connection to {self.host} timed out. (connect timeout={self.timeout})", |
| 137 |
) from e |
| 138 |
else: |
| 139 |
# Adding `from e` messes with coverage somehow, so it's omitted. |
| 140 |
# See #2386. |
| 141 |
raise NewConnectionError( |
| 142 |
self, f"Failed to establish a new connection: {error}" |
| 143 |
) |
| 144 |
else: # Defensive: see https://github.com/urllib3/urllib3/pull/3728#pullrequestreview-3816302703 |
| 145 |
raise NewConnectionError( |
| 146 |
self, f"Failed to establish a new connection: {e}" |
| 147 |
) from e |
| 148 |
|
| 149 |
except OSError as e: # Defensive: PySocks should catch all these. |
| 150 |
raise NewConnectionError( |
| 151 |
self, f"Failed to establish a new connection: {e}" |
| 152 |
) from e |
| 153 |
|
| 154 |
return conn |
| 155 |
|
| 156 |
|
| 157 |
# We don't need to duplicate the Verified/Unverified distinction from |
| 158 |
# urllib3/connection.py here because the HTTPSConnection will already have been |
| 159 |
# correctly set to either the Verified or Unverified form by that module. This |
| 160 |
# means the SOCKSHTTPSConnection will automatically be the correct type. |
| 161 |
class SOCKSHTTPSConnection(SOCKSConnection, HTTPSConnection): |
| 162 |
pass |
| 163 |
|
| 164 |
|
| 165 |
class SOCKSHTTPConnectionPool(HTTPConnectionPool): |
| 166 |
ConnectionCls = SOCKSConnection |
| 167 |
|
| 168 |
|
| 169 |
class SOCKSHTTPSConnectionPool(HTTPSConnectionPool): |
| 170 |
ConnectionCls = SOCKSHTTPSConnection |
| 171 |
|
| 172 |
|
| 173 |
class SOCKSProxyManager(PoolManager): |
| 174 |
""" |
| 175 |
A version of the urllib3 ProxyManager that routes connections via the |
| 176 |
defined SOCKS proxy. |
| 177 |
""" |
| 178 |
|
| 179 |
pool_classes_by_scheme = { |
| 180 |
"http": SOCKSHTTPConnectionPool, |
| 181 |
"https": SOCKSHTTPSConnectionPool, |
| 182 |
} |
| 183 |
|
| 184 |
def __init__( |
| 185 |
self, |
| 186 |
proxy_url: str, |
| 187 |
username: str | None = None, |
| 188 |
password: str | None = None, |
| 189 |
num_pools: int = 10, |
| 190 |
headers: typing.Mapping[str, str] | None = None, |
| 191 |
**connection_pool_kw: typing.Any, |
| 192 |
): |
| 193 |
parsed = parse_url(proxy_url) |
| 194 |
|
| 195 |
if username is None and password is None: |
| 196 |
username, password = parsed.auth_decoded |
| 197 |
if parsed.scheme == "socks5": |
| 198 |
socks_version = socks.PROXY_TYPE_SOCKS5 |
| 199 |
rdns = False |
| 200 |
elif parsed.scheme == "socks5h": |
| 201 |
socks_version = socks.PROXY_TYPE_SOCKS5 |
| 202 |
rdns = True |
| 203 |
elif parsed.scheme == "socks4": |
| 204 |
socks_version = socks.PROXY_TYPE_SOCKS4 |
| 205 |
rdns = False |
| 206 |
elif parsed.scheme == "socks4a": |
| 207 |
socks_version = socks.PROXY_TYPE_SOCKS4 |
| 208 |
rdns = True |
| 209 |
else: |
| 210 |
raise ValueError(f"Unable to determine SOCKS version from {proxy_url}") |
| 211 |
|
| 212 |
self.proxy_url = proxy_url |
| 213 |
|
| 214 |
socks_options = { |
| 215 |
"socks_version": socks_version, |
| 216 |
"proxy_host": parsed.host, |
| 217 |
"proxy_port": parsed.port, |
| 218 |
"username": username, |
| 219 |
"password": password, |
| 220 |
"rdns": rdns, |
| 221 |
} |
| 222 |
connection_pool_kw["_socks_options"] = socks_options |
| 223 |
|
| 224 |
super().__init__(num_pools, headers, **connection_pool_kw) |
| 225 |
|
| 226 |
self.pool_classes_by_scheme = SOCKSProxyManager.pool_classes_by_scheme |