o
    	þðc¹  ã                   @   s(  d Z ddlmZ zddlZW n ey&   ddlZddlmZ e de¡ ‚ w ddl	m
Z ddl	mZ dd	lmZmZ dd
lmZmZ ddlmZmZ ddlmZ ddlmZ zddlZW n eyh   dZY nw G dd„ deƒZG dd„ deeƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZdS )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_urlc                       s(   e Zd ZdZ‡ fdd„Zdd„ Z‡  ZS )ÚSOCKSConnectionzG
    A plain-text HTTP connection that connects via a SOCKS proxy.
    c                    s&   |  d¡| _tt| ƒj|i |¤Ž d S )NÚ_socks_options)Úpopr   Úsuperr   Ú__init__)ÚselfÚargsÚkwargs©Ú	__class__© úB/opt/certbot/lib/python3.10/site-packages/urllib3/contrib/socks.pyr   P   s   zSOCKSConnection.__init__c              
   C   s$  i }| j r
| j |d< | jr| j|d< z+tj| j| jff| jd | jd | jd | jd | jd | jd | jd	œ|¤Ž}W |S  tyO   t	| d
| j| jf ƒ‚ tj
y } z$|jrt|j}t|tƒrmt	| d
| j| jf ƒ‚t| d| ƒ‚t| d| ƒ‚d}~w ty‘ } zt| d| ƒ‚d}~ww )zA
        Establish a new connection via the SOCKS proxy.
        Úsource_addressÚsocket_optionsÚsocks_versionÚ
proxy_hostÚ
proxy_portÚusernameÚpasswordÚrdns)Ú
proxy_typeÚ
proxy_addrr   Úproxy_usernameÚproxy_passwordÚ
proxy_rdnsr   z0Connection to %s timed out. (connect timeout=%s)z(Failed to establish a new connection: %sN)r   r   ÚsocksÚcreate_connectionÚhostÚportr   r   ÚSocketTimeoutr   Ú
ProxyErrorÚ
socket_errÚ
isinstancer   ÚSocketError)r   Úextra_kwÚconnÚer   r   r   r   Ú	_new_connT   sd   


ÿø	÷,à
ÿþ

ÿþÿÿ€ÿ€ÿzSOCKSConnection._new_conn)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r4   Ú__classcell__r   r   r   r   r   K   s    r   c                   @   s   e Zd ZdS )ÚSOCKSHTTPSConnectionN)r5   r6   r7   r   r   r   r   r:   “   s    r:   c                   @   ó   e Zd ZeZdS )ÚSOCKSHTTPConnectionPoolN)r5   r6   r7   r   ÚConnectionClsr   r   r   r   r<   —   ó    r<   c                   @   r;   )ÚSOCKSHTTPSConnectionPoolN)r5   r6   r7   r:   r=   r   r   r   r   r?   ›   r>   r?   c                       s4   e Zd ZdZeedœZ				d‡ fdd„	Z‡  ZS )ÚSOCKSProxyManagerzh
    A version of the urllib3 ProxyManager that routes connections via the
    defined SOCKS proxy.
    )ÚhttpÚhttpsNé
   c                    sð   t |ƒ}|d u r!|d u r!|jd ur!|j d¡}t|ƒdkr!|\}}|jdkr,tj}	d}
n'|jdkr7tj}	d}
n|jdkrBtj}	d}
n|jdkrMtj}	d}
ntd	| ƒ‚|| _	|	|j
|j|||
d
œ}||d< tt| ƒj||fi |¤Ž tj| _d S )Nú:r   Úsocks5FÚsocks5hTÚsocks4Úsocks4az)Unable to determine SOCKS version from %s)r   r   r   r    r!   r"   r   )r   ÚauthÚsplitÚlenÚschemer(   ÚPROXY_TYPE_SOCKS5ÚPROXY_TYPE_SOCKS4Ú
ValueErrorÚ	proxy_urlr*   r+   r   r@   r   Úpool_classes_by_scheme)r   rP   r    r!   Ú	num_poolsÚheadersÚconnection_pool_kwÚparsedrJ   r   r"   Úsocks_optionsr   r   r   r   ª   sB   	



ú
ÿÿzSOCKSProxyManager.__init__)NNrC   N)	r5   r6   r7   r8   r<   r?   rQ   r   r9   r   r   r   r   r@   Ÿ   s    þúr@   ) r8   Ú
__future__r   r(   ÚImportErrorÚwarningsÚ
exceptionsr   ÚwarnÚsocketr   r0   r   r,   Ú
connectionr   r   Úconnectionpoolr	   r
   r   r   Úpoolmanagerr   Úutil.urlr   Ússlr   r:   r<   r?   r@   r   r   r   r   Ú<module>   s<   'úóÿH