
) X$                 @   s  d  Z  d d l m Z y d d l Z WnA e k
 ri d d l Z d d l m Z e j d e    Yn Xd 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 y d d l Z Wn e k
 rd Z Yn XGd d   d e  Z Gd d   d e e  Z Gd d   d e  Z Gd d   d e  Z Gd d   d e  Z d S)aK  
SOCKS support for urllib3
~~~~~~~~~~~~~~~~~~~~~~~~~

This contrib module contains provisional support for SOCKS proxies from within
urllib3. This module supports SOCKS4 (specifically the SOCKS4A variant) and
SOCKS5. To enable its functionality, either install PySocks or install this
module with the ``socks`` extra.

Known Limitations:

- Currently PySocks does not support contacting remote websites via literal
  IPv6 addresses. Any such connection attempt will fail.
- Currently PySocks does not support IPv6 connections to the SOCKS proxy. Any
  such connection attempt will fail.
    )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                   s4   e  Z d  Z d Z   f d d   Z d d   Z   S)SOCKSConnectionzG
    A plain-text HTTP connection that connects via a SOCKS proxy.
    c                s/   | j  d  |  _ t t |   j | |   d  S)N_socks_options)popr   superr   __init__)selfargskwargs)	__class__ W/home/ubuntu/projects/ifolica/build/requests/requests/packages/urllib3/contrib/socks.pyr   9   s    zSOCKSConnection.__init__c             C   s  i  } |  j  r |  j  | d <n  |  j r8 |  j | d <n  yl t j |  j |  j f d |  j d d |  j d d |  j d d |  j d	 d
 |  j d d |  j | } Wnt k
 r } z$ t	 |  d |  j |  j f   WYd d } ~ Xn t j
 k
 r}} zq | j rX| j } t | t  rBt	 |  d |  j |  j f   qkt |  d |   n t |  d |   WYd d } ~ Xn8 t k
 r} z t |  d |   WYd d } ~ Xn X| S)zA
        Establish a new connection via the SOCKS proxy.
        source_addresssocket_options
proxy_typesocks_versionZ
proxy_addr
proxy_host
proxy_portZproxy_usernameusernameZproxy_passwordpasswordr   z0Connection to %s timed out. (connect timeout=%s)Nz(Failed to establish a new connection: %s)r   r   sockscreate_connectionhostportr   r   SocketTimeoutr   
ProxyErrorZ
socket_err
isinstancer   SocketError)r   Zextra_kwconner   r   r   r   	_new_conn=   sJ    		(		#zSOCKSConnection._new_conn)__name__
__module____qualname____doc__r   r,   r   r   )r   r   r   5   s   r   c               @   s   e  Z d  Z d S)SOCKSHTTPSConnectionN)r-   r.   r/   r   r   r   r   r1   z   s   r1   c               @   s   e  Z d  Z e Z d S)SOCKSHTTPConnectionPoolN)r-   r.   r/   r   ConnectionClsr   r   r   r   r2   ~   s   r2   c               @   s   e  Z d  Z e Z d S)SOCKSHTTPSConnectionPoolN)r-   r.   r/   r1   r3   r   r   r   r   r4      s   r4   c                   sH   e  Z d  Z d Z i e d 6e d 6Z d d d d   f d d  Z   S)SOCKSProxyManagerzh
    A version of the urllib3 ProxyManager that routes connections via the
    defined SOCKS proxy.
    httphttpsN
   c       
         s   t  |  } | j d k r' t j } n+ | j d k rB t j } n t d |   | |  _ i | d 6| j d 6| j d 6| d 6| d 6}	 |	 | d	 <t	 t
 |   j | | |  t
 j |  _ d  S)
NZsocks5Zsocks4z)Unable to determine SOCKS version from %sr   r   r   r    r!   r   )r   schemer"   ZPROXY_TYPE_SOCKS5ZPROXY_TYPE_SOCKS4
ValueError	proxy_urlr$   r%   r   r5   r   pool_classes_by_scheme)
r   r;   r    r!   	num_poolsheadersconnection_pool_kwparsedr   Zsocks_options)r   r   r   r      s$    	



zSOCKSProxyManager.__init__)r-   r.   r/   r0   r2   r4   r<   r   r   r   )r   r   r5      s   
r5   ) r0   
__future__r   r"   ImportErrorwarnings
exceptionsr   warnsocketr   r)   r   r&   
connectionr   r   connectionpoolr	   r
   r   r   poolmanagerr   Zutil.urlr   sslr   r1   r2   r4   r5   r   r   r   r   <module>   s2   E