U
    `'^|                     @   s(  d Z ddlmZ zddlZW n6 ek
rR   ddlZddlmZ ede  Y nX 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mZ ddlmZ ddlmZ zddlZW n ek
r   dZY nX 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::

    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::

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

    )absolute_importN   )DependencyWarningzSOCKS support in urllib3 requires the installation of optional dependencies: specifically, PySocks.  For more information, see https://urllib3.readthedocs.io/en/latest/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|| d S )N_socks_options)popr   superr   __init__)selfargskwargs	__class__ 7/usr/lib/python3/dist-packages/urllib3/contrib/socks.pyr   J   s    zSOCKSConnection.__init__c              
   C   sH  i }| j r| j |d< | jr$| j|d< zTtj| j| jff| jd | jd | jd | jd | jd | jd | jd	|}W n tk
r   t	| d
| j| jf Y n tj
k
r } zP|jr|j}t|trt	| d
| j| jf nt| d| nt| d| W 5 d}~X Y n2 tk
rB } zt| d| W 5 d}~X Y nX |S )zA
        Establish a new connection via the SOCKS proxy.
        source_addresssocket_optionssocks_version
proxy_host
proxy_portusernamepasswordrdns)Z
proxy_typeZ
proxy_addrr   Zproxy_usernameZproxy_passwordZ
proxy_rdnsr   z0Connection to %s timed out. (connect timeout=%s)z(Failed to establish a new connection: %sN)r   r   socksZcreate_connectionhostportr   r   SocketTimeoutr   Z
ProxyErrorZ
socket_err
isinstancer   SocketError)r   Zextra_kwZconner   r   r   r   	_new_connN   sd    


	



   zSOCKSConnection._new_conn)__name__
__module____qualname____doc__r   r*   __classcell__r   r   r   r   r   E   s   r   c                   @   s   e Zd ZdS )SOCKSHTTPSConnectionN)r+   r,   r-   r   r   r   r   r0      s   r0   c                   @   s   e Zd ZeZdS )SOCKSHTTPConnectionPoolN)r+   r,   r-   r   ConnectionClsr   r   r   r   r1      s   r1   c                   @   s   e Zd ZeZdS )SOCKSHTTPSConnectionPoolN)r+   r,   r-   r0   r2   r   r   r   r   r3      s   r3   c                       s,   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.
    )ZhttpZhttpsN
   c                    s   t |}|d krB|d krB|jd k	rB|jd}t|dkrB|\}}|jdkrXtj}	d}
nN|jdkrntj}	d}
n8|jdkrtj}	d}
n"|jdkrtj}	d}
ntd	| || _	|	|j
|j|||
d
}||d< tt| j||f| tj| _d S )N:r   Zsocks5FZsocks5hTZsocks4Zsocks4az)Unable to determine SOCKS version from %s)r   r   r   r    r!   r"   r   )r   ZauthsplitlenZschemer#   ZPROXY_TYPE_SOCKS5ZPROXY_TYPE_SOCKS4
ValueError	proxy_urlr$   r%   r   r4   r   pool_classes_by_scheme)r   r:   r    r!   Z	num_poolsZheadersZconnection_pool_kwZparsedr7   r   r"   Zsocks_optionsr   r   r   r      sD    	




 zSOCKSProxyManager.__init__)NNr5   N)	r+   r,   r-   r.   r1   r3   r;   r   r/   r   r   r   r   r4      s       r4   ) r.   Z
__future__r   r#   ImportErrorwarnings
exceptionsr   warnZsocketr   r(   r   r&   Z
connectionr   r   Zconnectionpoolr	   r
   r   r   Zpoolmanagerr   Zutil.urlr   Zsslr   r0   r1   r3   r4   r   r   r   r   <module>   s4   #
H