3

Z'íc¹ã@s0dZddlmZyddlZWn6ek
rRddlZddlmZejdeƒ‚YnXddl	m
Zddl	mZ
dd	lmZmZdd
lmZmZddlmZmZddlmZdd
lmZyddlZWnek
rØdZYnXGdd„deƒZGdd„deeƒZGdd„deƒZGdd„deƒZGdd„deƒZdS)a‰
This module contains provisional support for SOCKS proxies from within
urllib3. This module supports SOCKS4, SOCKS4A (an extension of SOCKS4), and
SOCKS5. To enable its functionality, either install PySocks or install this
module with the ``socks`` extra.

The SOCKS implementation supports the full range of urllib3 features. It also
supports the following SOCKS features:

- SOCKS4A (``proxy_url='socks4a://...``)
- SOCKS4 (``proxy_url='socks4://...``)
- SOCKS5 with remote DNS (``proxy_url='socks5h://...``)
- SOCKS5 with local DNS (``proxy_url='socks5://...``)
- Usernames and passwords for the SOCKS proxy

.. note::
   It is recommended to use ``socks5h://`` or ``socks4a://`` schemes in
   your ``proxy_url`` to ensure that DNS resolution is done from the remote
   server instead of client-side when connecting to a domain name.

SOCKS4 supports IPv4 and domain names with the SOCKS4A extension. SOCKS5
supports IPv4, IPv6, and domain names.

When connecting to a SOCKS4 proxy the ``username`` portion of the ``proxy_url``
will be sent as the ``userid`` section of the SOCKS request:

.. code-block:: python

    proxy_url="socks4a://<userid>@proxy-host"

When connecting to a SOCKS5 proxy the ``username`` and ``password`` portion
of the ``proxy_url`` will be sent as the username/password to authenticate
with the proxy:

.. code-block:: python

    proxy_url="socks5h://<username>:<password>@proxy-host"

é)Úabsolute_importNé)ÚDependencyWarningzÂSOCKS support in urllib3 requires the installation of optional dependencies: specifically, PySocks.  For more information, see https://urllib3.readthedocs.io/en/1.26.x/contrib.html#socks-proxies)Úerror)Útimeout)ÚHTTPConnectionÚHTTPSConnection)ÚHTTPConnectionPoolÚHTTPSConnectionPool)ÚConnectTimeoutErrorÚNewConnectionError)ÚPoolManager)Ú	parse_urlcs(eZdZdZ‡fdd„Zdd„Z‡ZS)ÚSOCKSConnectionzG
    A plain-text HTTP connection that connects via a SOCKS proxy.
    cs"|jdƒ|_tt|ƒj||ŽdS)NÚ_socks_options)ÚpoprÚsuperrÚ__init__)ÚselfÚargsÚkwargs)Ú	__class__©ú8/tmp/pip-build-gk9425m9/urllib3/urllib3/contrib/socks.pyrPszSOCKSConnection.__init__cCsHi}|jr|j|d<|jr$|j|d<yTtj|j|jff|jd|jd|jd|jd|jd|jd|jd	œ|—Ž}WnÊtk
r¢t	|d
|j|jfƒ‚Yn¢tj
k
r}zR|jrô|j}t|tƒrät	|d
|j|jfƒ‚nt
|d|ƒ‚nt
|d|ƒ‚WYdd}~Xn2tk
rB}zt
|d|ƒ‚WYdd}~XnX|S)
zA
        Establish a new connection via the SOCKS proxy.
        Úsource_addressÚsocket_optionsÚ
socks_versionÚ
proxy_hostÚ
proxy_portÚusernameÚpasswordÚrdns)Ú
proxy_typeZ
proxy_addrrZproxy_usernameZproxy_passwordZ
proxy_rdnsrz0Connection to %s timed out. (connect timeout=%s)z(Failed to establish a new connection: %sN)rrÚsocksÚcreate_connectionÚhostÚportrrÚ
SocketTimeoutrÚ
ProxyErrorZ
socket_errÚ
isinstancerÚSocketError)rÚextra_kwÚconnÚerrrrÚ	_new_connTsJ


zSOCKSConnection._new_conn)Ú__name__Ú
__module__Ú__qualname__Ú__doc__rr.Ú
__classcell__rr)rrrKsrc@seZdZdS)ÚSOCKSHTTPSConnectionN)r/r0r1rrrrr4“sr4c@seZdZeZdS)ÚSOCKSHTTPConnectionPoolN)r/r0r1rÚ
ConnectionClsrrrrr5—sr5c@seZdZeZdS)ÚSOCKSHTTPSConnectionPoolN)r/r0r1r4r6rrrrr7›sr7cs,eZdZdZeedœZd‡fdd„	Z‡ZS)ÚSOCKSProxyManagerzh
    A version of the urllib3 ProxyManager that routes connections via the
    defined SOCKS proxy.
    )ÚhttpÚhttpsNé
csìt|ƒ}|dkrB|dkrB|jdk	rB|jjdƒ}t|ƒdkrB|\}}|jdkrXtj}	d}
nN|jdkrntj}	d}
n8|jdkr„tj}	d}
n"|jdkrštj}	d}
ntd	|ƒ‚||_	|	|j
|j|||
d
œ}||d<tt
|ƒj||f|Žt
j|_dS)Nú:rZsocks5FZsocks5hTZsocks4Zsocks4az)Unable to determine SOCKS version from %s)rrrrr r!r)rÚauthÚsplitÚlenÚschemer#ZPROXY_TYPE_SOCKS5ZPROXY_TYPE_SOCKS4Ú
ValueErrorÚ	proxy_urlr%r&rr8rÚpool_classes_by_scheme)rrBrr Ú	num_poolsÚheadersÚconnection_pool_kwÚparsedr>rr!Z
socks_options)rrrrªs:	




zSOCKSProxyManager.__init__)NNr;N)	r/r0r1r2r5r7rCrr3rr)rrr8Ÿsr8) r2Ú
__future__rr#ÚImportErrorÚwarningsÚ
exceptionsrÚwarnÚsocketrr*rr'Ú
connectionrrÚconnectionpoolr	r
rrÚpoolmanagerr
Zutil.urlrÚsslrr4r5r7r8rrrrÚ<module>(s4
H