ó
Ý‰]c        
   @   s­  d  d l  m Z y d  d l m Z Wn! e k
 rG d  d l m Z n Xd  d l m Z m Z m	 Z	 d  d l
 Z
 d  d l m Z d  d l Z d  d l Z d  d l m Z d  d l Z d  d l Z d  d l Z d Z e j d k r#e j dC k  r#y d  d l Z Wq#e k
 re d ƒ ‚ q#Xn  e j e ƒ Z d Z Z d Z Z d	 Z Z i e d 6e d 6e d 6Z  e! e" e  j# ƒ  e  j$ ƒ  ƒ ƒ Z% e j Z& Z' d „  Z( d e) f d „  ƒ  YZ* d e* f d „  ƒ  YZ+ d e* f d „  ƒ  YZ, d e* f d „  ƒ  YZ- d e* f d „  ƒ  YZ. d e* f d „  ƒ  YZ/ d e* f d „  ƒ  YZ0 i d  d! 6d" d# 6d$ d% 6Z1 i d& d 6d' d 6d( d	 6d) d* 6d+ d, 6d- d. 6d/ d0 6d1 d2 6Z2 i d3 e 6d3 e 6d4 e 6Z3 d d d e5 d d d5 „ Z6 d6 „  Z7 d7 „  Z8 e8 Z9 d8 „  Z: e: Z; d d d d d e5 d d d d9 „	 Z< d: e j f d; „  ƒ  YZ= d< „  Z> xX dD D]P Z e? e= e d ƒ Z@ eA e@ e ƒ s?e= jB jC e ƒ eD e= e e> e ƒ ƒ q?q?WdA e= f dB „  ƒ  YZE d S(E   iÿÿÿÿ(   t	   b64encode(   t   Callable(   t
   EOPNOTSUPPt   EINVALt   EAGAINN(   t   BytesIO(   t   SEEK_CURs   1.7.1t   nti   i    s8   To run PySocks on Windows you must install win_inet_ptoni   i   t   SOCKS4t   SOCKS5t   HTTPc            s"   t  j ˆ  ƒ ‡  f d †  ƒ } | S(   Nc             s„   |  d } zV y9 | j  ƒ  } | d k r8 | j t ƒ n  ˆ  |  | Ž  SWn t k
 r^ } ‚  n XWd  | d k r | j t ƒ n  Xd  S(   Ni    (   t
   gettimeoutt   setblockingt   Truet	   Exceptiont   False(   t   argst   kwargst   selft   _is_blockingt   e(   t   function(    s$   lib/python2.7/site-packages/socks.pyt   wrapper(   s    
 (   t	   functoolst   wraps(   R   R   (    (   R   s$   lib/python2.7/site-packages/socks.pyt   set_self_blocking&   s    t
   ProxyErrorc           B   s#   e  Z d  Z d d „ Z d „  Z RS(   s4   Socket_err contains original socket.error exception.c         C   s7   | |  _  | |  _ | r3 |  j  d j | ƒ 7_  n  d  S(   Ns   : {}(   t   msgt
   socket_errt   format(   R   R   R   (    (    s$   lib/python2.7/site-packages/socks.pyt   __init__;   s    		c         C   s   |  j  S(   N(   R   (   R   (    (    s$   lib/python2.7/site-packages/socks.pyt   __str__B   s    N(   t   __name__t
   __module__t   __doc__t   NoneR   R   (    (    (    s$   lib/python2.7/site-packages/socks.pyR   9   s   t   GeneralProxyErrorc           B   s   e  Z RS(    (   R    R!   (    (    (    s$   lib/python2.7/site-packages/socks.pyR$   F   s   t   ProxyConnectionErrorc           B   s   e  Z RS(    (   R    R!   (    (    (    s$   lib/python2.7/site-packages/socks.pyR%   J   s   t   SOCKS5AuthErrorc           B   s   e  Z RS(    (   R    R!   (    (    (    s$   lib/python2.7/site-packages/socks.pyR&   N   s   t   SOCKS5Errorc           B   s   e  Z RS(    (   R    R!   (    (    (    s$   lib/python2.7/site-packages/socks.pyR'   R   s   t   SOCKS4Errorc           B   s   e  Z RS(    (   R    R!   (    (    (    s$   lib/python2.7/site-packages/socks.pyR(   V   s   t	   HTTPErrorc           B   s   e  Z RS(    (   R    R!   (    (    (    s$   lib/python2.7/site-packages/socks.pyR)   Z   s   s   Request rejected or failedi[   sL   Request rejected because SOCKS server cannot connect to identd on the clienti\   sP   Request rejected because the client program and identd report different user-idsi]   s   General SOCKS server failures!   Connection not allowed by rulesets   Network unreachables   Host unreachablei   s   Connection refusedi   s   TTL expiredi   s(   Command not supported, or protocol errori   s   Address type not supportedi   i8  i  c         C   sC   |  | | | | r | j  ƒ  n d | r3 | j  ƒ  n d f t _ d S(   s›   Sets a default proxy.

    All further socksocket objects will use the default unless explicitly
    changed. All parameters are as for socket.set_proxy().N(   t   encodeR#   t
   socksockett   default_proxy(   t
   proxy_typet   addrt   portt   rdnst   usernamet   password(    (    s$   lib/python2.7/site-packages/socks.pyt   set_default_proxys   s    c          O   s/   d | k r" | j  d ƒ | d <n  t |  | Ž  S(   Nt	   proxytypeR-   (   t   popR3   (   R   R   (    (    s$   lib/python2.7/site-packages/socks.pyt   setdefaultproxy~   s    c           C   s   t  j S(   s4   Returns the default proxy, set by set_default_proxy.(   R+   R,   (    (    (    s$   lib/python2.7/site-packages/socks.pyt   get_default_proxy„   s    c         C   s(   t  j r t  |  j _ n t d ƒ ‚ d S(   s  Attempts to replace a module's socket library with a SOCKS socket.

    Must set a default proxy using set_default_proxy(...) first. This will
    only work on modules that import socket directly into the namespace;
    most of the Python Standard Library falls into this category.s   No default proxy specifiedN(   R+   R,   t   socketR$   (   t   module(    (    s$   lib/python2.7/site-packages/socks.pyt   wrap_module‹   s    	c
         C   s¯  |  \ }
 } |
 j  d ƒ r- |
 j d ƒ }
 n  | rT | j  d ƒ rT | j d ƒ } n  d } x0t j | | d t j ƒ D]} | \ } } } } } d } y´ t | | | ƒ } |	 rÓ x |	 D] } | j | Œ  q¹ Wn  t | t	 t
 f ƒ rø | j | ƒ n  | r| j | | | | | | ƒ n  | r3| j | ƒ n  | j |
 | f ƒ | SWqv t j t f k
 rˆ} | } | r‰| j ƒ  d } q‰qv Xqv W| rœ| ‚ n  t j d ƒ ‚ d S(   sí  create_connection(dest_pair, *[, timeout], **proxy_args) -> socket object

    Like socket.create_connection(), but connects to proxy
    before returning the socket object.

    dest_pair - 2-tuple of (IP/hostname, port).
    **proxy_args - Same args passed to socksocket.set_proxy() if present.
    timeout - Optional socket timeout value, in seconds.
    source_address - tuple (host, port) for the socket to bind to as its source
    address before connecting (only for compatibility)
    t   [s   []i    s   gai returned empty list.N(   t
   startswitht   stripR#   R8   t   getaddrinfot   SOCK_STREAMR+   t
   setsockoptt
   isinstancet   intt   floatt
   settimeoutt	   set_proxyt   bindt   connectt   errorR   t   close(   t	   dest_pairt   timeoutt   source_addressR-   t
   proxy_addrt
   proxy_portt
   proxy_rdnst   proxy_usernamet   proxy_passwordt   socket_optionst   remote_hostt   remote_portt   errt   rt   familyt   socket_typet   protot	   canonnamet   sat   sockt   optR   (    (    s$   lib/python2.7/site-packages/socks.pyt   create_connection™   s>    "
	t   _BaseSocketc           B   s    e  Z d  Z d „  Z e ƒ  Z RS(   sB   Allows Python 2 delegated methods such as send() to be overridden.c         O   sZ   t  j |  | | Ž t ƒ  |  _ x4 |  j D]) } t |  | ƒ |  j | <t |  | ƒ q) Wd  S(   N(   t   _orig_socketR   t   dictt   _savedmethodst
   _savenamest   getattrt   delattr(   R   t   post   kwt   name(    (    s$   lib/python2.7/site-packages/socks.pyR   Ø   s
    (   R    R!   R"   R   t   listRc   (    (    (    s$   lib/python2.7/site-packages/socks.pyR_   Ö   s   	c            s   ‡  f d †  S(   Nc            s   |  j  ˆ  | | Ž  S(   N(   Rb   (   R   Rf   Rg   (   Rh   (    s$   lib/python2.7/site-packages/socks.pyt   <lambda>ä   t    (    (   Rh   (    (   Rh   s$   lib/python2.7/site-packages/socks.pyt   _makemethodã   s    t   sendtot   sendt   recvfromt   recvR+   c           B   sX  e  Z d  Z d Z e j e j d d „ Z d „  Z	 d „  Z
 d „  Z d „  Z d d d e d d d „ Z d „  Z d	 „  Z d
 „  Z d d „ Z d d „ Z d „  Z d „  Z d „  Z e Z d „  Z e Z d „  Z e Z d „  Z d „  Z d „  Z d „  Z d „  Z  d „  Z! i e  e" 6e e# 6e! e$ 6Z% e& d d „ ƒ Z' e& d „  ƒ Z( d „  Z) RS(   s2  socksocket([family[, type[, proto]]]) -> socket object

    Open a SOCKS enabled socket. The parameters are the same as
    those of the standard socket init. In order for SOCKS to work,
    you must specify family=AF_INET and proto=0.
    The "type" argument must be either SOCK_STREAM or SOCK_DGRAM.
    i    c         O   s¡   | t  j t  j f k r6 d } t | j | ƒ ƒ ‚ n  t t |  ƒ j | | | | | Ž d  |  _	 |  j
 ry |  j
 |  _ n	 d |  _ d  |  _ d  |  _ d  |  _ d  S(   Ns0   Socket type must be stream or datagram, not {!r}(   NNNNNN(   R8   R?   t
   SOCK_DGRAMt
   ValueErrorR   t   superR+   R   R#   t
   _proxyconnR,   t   proxyt   proxy_socknamet   proxy_peernamet   _timeout(   R   RW   t   typeRY   R   R   R   (    (    s$   lib/python2.7/site-packages/socks.pyR   ü   s    "					c         C   s[   d } xN t  | ƒ | k  rV | j | t  | ƒ ƒ } | sI t d ƒ ‚ n  | | 7} q	 W| S(   sŠ   Receive EXACTLY the number of bytes requested from the file object.

        Blocks until the required number of bytes have been received.Rk   s   Connection closed unexpectedly(   t   lent   readR$   (   R   t   filet   countt   datat   d(    (    s$   lib/python2.7/site-packages/socks.pyt   _readall  s    c         C   sM   | |  _  y) |  j ƒ  } t t |  ƒ j |  j  ƒ Wn t j k
 rH n Xd  S(   N(   Rx   t   get_proxy_peernameRs   R+   RD   R8   RH   (   R   RK   t   peer(    (    s$   lib/python2.7/site-packages/socks.pyRD     s    	c         C   s   |  j  S(   N(   Rx   (   R   (    (    s$   lib/python2.7/site-packages/socks.pyR   #  s    c         C   s'   | r |  j  d  ƒ n |  j  d ƒ d  S(   Ng        (   RD   R#   (   R   t   v(    (    s$   lib/python2.7/site-packages/socks.pyR   &  s    c         C   sC   | | | | | r | j  ƒ  n d | r3 | j  ƒ  n d f |  _ d S(   s   Sets the proxy to be used.

        proxy_type -  The type of the proxy to be used. Three types
                        are supported: PROXY_TYPE_SOCKS4 (including socks4a),
                        PROXY_TYPE_SOCKS5 and PROXY_TYPE_HTTP
        addr -        The address of the server (IP or DNS).
        port -        The port of the server. Defaults to 1080 for SOCKS
                        servers and 8080 for HTTP proxy servers.
        rdns -        Should DNS queries be performed on the remote side
                       (rather than the local side). The default is True.
                       Note: This has no effect with SOCKS4 servers.
        username -    Username to authenticate with to the server.
                       The default is no authentication.
        password -    Password to authenticate with to the server.
                       Only relevant when username is also provided.N(   R*   R#   Ru   (   R   R-   R.   R/   R0   R1   R2   (    (    s$   lib/python2.7/site-packages/socks.pyRE   ,  s    c         O   s2   d | k r" | j  d ƒ | d <n  |  j | | Ž  S(   NR4   R-   (   R5   RE   (   R   R   R   (    (    s$   lib/python2.7/site-packages/socks.pyt   setproxyA  s    c         O   si  |  j  \ } } } } } } | s4 |  j t j k rG t j |  | | Ž S|  j re t j t d ƒ ‚ n  | t	 k rŒ d }	 t j t
 |	 ƒ ‚ n  t t |  ƒ j | | Ž  |  j ƒ  \ }
 } d | f } t ƒ  |  _ |  j ƒ  } |  j j | ƒ d } |  j |  j | | ƒ \ }
 } | \ } }
 | \ }
 } t t |  ƒ j | | f ƒ t t |  ƒ j |  j ƒ d |  _ d S(	   sV   Implements proxy connection for UDP sockets.

        Happens during the bind() phase.s"   Socket already bound to an addresss'   UDP only supported by SOCKS5 proxy typet   0s   s   0.0.0.0i    N(   s   0.0.0.0i    (   Ru   Ry   R8   Rq   R`   RF   Rt   RH   R   R	   R   Rs   R+   t   getsocknamet   _proxy_addrRG   t   _SOCKS5_requestRD   Rx   Rv   (   R   Rf   Rg   R-   RM   RN   R0   R1   R2   R   t   _R/   t   dstRu   t   UDP_ASSOCIATEt   relayt   host(    (    s$   lib/python2.7/site-packages/socks.pyRF   F  s*    	c   
      O   sÐ   |  j  t j k r. t t |  ƒ j | | | Ž S|  j sG |  j d ƒ n  | d } | d  } t ƒ  } d } | j	 | ƒ d } | j	 | ƒ |  j
 | | ƒ t t |  ƒ j | j ƒ  | | | Ž }	 |	 | j ƒ  S(   NRk   i    iÿÿÿÿs     s    (   Rk   i    (   Ry   R8   Rq   Rs   R+   Rm   Rt   RF   R   t   writet   _write_SOCKS5_addressRn   t   getvaluet   tell(
   R   t   bytesR   R   t   addresst   flagst   headert   RSVt
   STANDALONEt   sent(    (    s$   lib/python2.7/site-packages/socks.pyRm   k  s    	

		c         K   sK   |  j  t j k r+ |  j | | |  j |  St t |  ƒ j | | |  Sd  S(   N(   Ry   R8   Rq   Rm   Rw   Rs   R+   Rn   (   R   R’   R”   R   (    (    s$   lib/python2.7/site-packages/socks.pyRn     s    c   	      C   s  |  j  t j k r+ t t |  ƒ j | | ƒ S|  j sD |  j d ƒ n  t t t |  ƒ j	 | d | ƒ ƒ } | j
 d t ƒ | j d ƒ } t | ƒ r£ t d ƒ ‚ n  |  j | ƒ \ } } |  j r|  j \ } } | | k sî | d | f k rt j t d ƒ ‚ qn  | j | ƒ | | f f S(	   NRk   i    i   i   i   s   Received UDP packet fragments   Packet filtered(   Rk   i    (   Ry   R8   Rq   Rs   R+   Ro   Rt   RF   R   Rp   t   seekR   R{   t   ordt   NotImplementedErrort   _read_SOCKS5_addressRw   RH   R   (	   R   t   bufsizeR”   t   buft   fragt   fromhostt   fromportt   peerhostt   peerport(    (    s$   lib/python2.7/site-packages/socks.pyRo   …  s    	%	c         O   s   |  j  | | Ž  \ } } | S(   N(   Ro   (   R   Rf   Rg   R’   R‰   (    (    s$   lib/python2.7/site-packages/socks.pyRp   ™  s    c         C   s,   |  j  r |  j  j ƒ  n  t t |  ƒ j ƒ  S(   N(   Rt   RI   Rs   R+   (   R   (    (    s$   lib/python2.7/site-packages/socks.pyRI     s    	c         C   s   |  j  S(   s:   Returns the bound IP address and port number at the proxy.(   Rv   (   R   (    (    s$   lib/python2.7/site-packages/socks.pyt   get_proxy_sockname¢  s    c         C   s
   |  j  ƒ  S(   s>   
        Returns the IP and port number of the proxy.
        (   t   getpeername(   R   (    (    s$   lib/python2.7/site-packages/socks.pyR   ¨  s    c         C   s   |  j  S(   sw   Returns the IP address and port number of the destination machine.

        Note: get_proxy_peername returns the proxy.(   Rw   (   R   (    (    s$   lib/python2.7/site-packages/socks.pyt   get_peername°  s    c         G   s+   d } |  j  |  | | ƒ \ |  _ |  _ d S(   s7   Negotiates a stream connection through a SOCKS5 server.s   N(   Rˆ   Rw   Rv   (   R   t	   dest_addrt   CONNECT(    (    s$   lib/python2.7/site-packages/socks.pyt   _negotiate_SOCKS5¸  s    c         C   s¼  |  j  \ } } } } } }	 | j d ƒ }
 | j d d ƒ } zd| r[ |	 r[ |
 j d ƒ n |
 j d ƒ |
 j ƒ  |  j | d ƒ } | d d !d k r¦ t d	 ƒ ‚ n  | d d !d
 k rx| oÂ |	 sÔ t d ƒ ‚ n  |
 j d t t | ƒ ƒ j	 ƒ  | t t |	 ƒ ƒ j	 ƒ  |	 ƒ |
 j ƒ  |  j | d ƒ } | d d !d k rSt d	 ƒ ‚ n  | d d !d k r¼t d ƒ ‚ q¼nD | d d !d k r¼| d d !d k r­t d ƒ ‚ q¼t d	 ƒ ‚ n  |
 j d | d ƒ |  j
 | |
 ƒ } |
 j ƒ  |  j | d ƒ } | d d !d k r!t d	 ƒ ‚ n  t | d d !ƒ } | d k rmt j | d ƒ } t d j | | ƒ ƒ ‚ n  |  j | ƒ } t t |  ƒ j |  j ƒ | | f SWd | j ƒ  |
 j ƒ  Xd S(   s‘   
        Send SOCKS5 request with given command (CMD field) and
        address (DST field). Returns resolved DST address that was used.
        t   wbt   rbi    s    s    i   i   s   s%   SOCKS5 proxy server sent invalid datas   sP   No username/password supplied. Server requested username/password authentications   s    s   SOCKS5 authentication faileds   ÿs7   All offered SOCKS5 authentication methods were rejectedi   s   Unknown errors   {:#04x}: {}N(   Ru   t   makefileRŽ   t   flushR€   R$   R&   t   chrRz   R*   R   Rš   t   SOCKS5_ERRORSt   getR'   R   Rœ   Rs   R+   RD   Rx   RI   (   R   t   connt   cmdRŠ   R-   R.   R/   R0   R1   R2   t   writert   readert   chosen_autht   auth_statust   resolvedt   respt   statusRH   t   bnd(    (    s$   lib/python2.7/site-packages/socks.pyRˆ   ¾  s\    
;


c         C   s¿  | \ } } |  j  \ } } } } } }	 i d t j 6d t j 6}
 x” t j t j f D]€ } y` t j | | ƒ } | j |
 | | ƒ t j | | ƒ } | j t j d | ƒ ƒ | | f SWqT t j	 k
 rÓ qT qT XqT W| r| j
 d ƒ } | j d t t | ƒ ƒ j
 ƒ  | ƒ n… t j | | t j t j t j t j ƒ } | d } | d } | d d } t j | | ƒ } | j |
 | | ƒ t j | | ƒ } | j t j d | ƒ ƒ | | f S(   s~   
        Return the host and port packed for the SOCKS5 protocol,
        and the resolved address as a tuple object.
        s   s   s   >Ht   idnas   i    i   (   Ru   R8   t   AF_INETt   AF_INET6t	   inet_ptonRŽ   t	   inet_ntopt   structt   packRH   R*   R®   Rz   R>   t	   AF_UNSPECR?   t   IPPROTO_TCPt   AI_ADDRCONFIG(   R   R.   R|   R   R/   R-   R‰   R0   R1   R2   t   family_to_byteRW   t
   addr_bytest
   host_bytest	   addressest   target_addr(    (    s$   lib/python2.7/site-packages/socks.pyR      s6    *

c         C   sÝ   |  j  | d ƒ } | d k r< t j |  j  | d ƒ ƒ } nu | d k ru |  j  | d ƒ } |  j  | t | ƒ ƒ } n< | d k r¥ t j t j |  j  | d ƒ ƒ } n t d ƒ ‚ t j d |  j  | d	 ƒ ƒ d
 } | | f S(   Ni   s   i   s   s   i   s%   SOCKS5 proxy server sent invalid datas   >Hi   i    (	   R€   R8   t	   inet_ntoaRš   R¿   R½   R$   RÀ   t   unpack(   R   R|   t   atypR.   t   lengthR/   (    (    s$   lib/python2.7/site-packages/socks.pyRœ   M  s    $"c         C   s  |  j  \ } } } } } } |  j d ƒ }	 |  j d d ƒ }
 zÀt } y t j | ƒ } WnA t j k
 r› | r€ d } t } qœ t j t j | ƒ ƒ } n X|	 j t	 j
 d d d | ƒ ƒ |	 j | ƒ | rÞ |	 j | ƒ n  |	 j d ƒ | r|	 j | j d	 ƒ d ƒ n  |	 j ƒ  |  j |
 d
 ƒ } | d d !d k rLt d ƒ ‚ n  t | d d !ƒ } | d k r˜t j | d ƒ } t d j | | ƒ ƒ ‚ n  t j | d ƒ t	 j d | d d !ƒ d f |  _ | rìt j | ƒ | f |  _ n | | f |  _ Wd |
 j ƒ  |	 j ƒ  Xd S(   s0   Negotiates a connection through a SOCKS4 server.Rª   R«   i    s      s   >BBHi   i   s    R»   i   s%   SOCKS4 proxy server sent invalid datai   iZ   s   Unknown errors   {:#04x}: {}s   >HN(   Ru   R¬   R   R8   t	   inet_atonRH   R   t   gethostbynameRŽ   RÀ   RÁ   R*   R­   R€   R$   Rš   t   SOCKS4_ERRORSR°   R(   R   RÊ   RË   Rv   Rw   RI   (   R   R§   t	   dest_portR-   R.   R/   R0   R1   R2   R³   R´   t   remote_resolveRÆ   R¸   R¹   RH   (    (    s$   lib/python2.7/site-packages/socks.pyt   _negotiate_SOCKS4\  sH    	
#
c         C   së  |  j  \ } } } } } } | r' | n t j | ƒ } d | j d ƒ d t | ƒ j ƒ  d d | j d ƒ g }	 | r¢ | r¢ |	 j d t | d | ƒ ƒ n  |	 j d ƒ |  j d j |	 ƒ ƒ |  j	 ƒ  }
 |
 j
 ƒ  } |
 j ƒ  | sü t d ƒ ‚ n  y | j d	 d
 ƒ \ } } } Wn t k
 r:t d ƒ ‚ n X| j d ƒ sYt d ƒ ‚ n  y t | ƒ } Wn t k
 rˆt d ƒ ‚ n X| d k rÏd j | | ƒ } | d k rÀ| d 7} n  t | ƒ ‚ n  d |  _ | | f |  _ d S(   sw   Negotiates a connection through an HTTP server.

        NOTE: This currently only supports HTTP CONNECT-style proxies.s   CONNECT R»   t   :s	    HTTP/1.1s   Host: s   Proxy-Authorization: basic s   
s   Connection closed unexpectedlyt    i   s'   HTTP proxy server sent invalid responses   HTTP/s0   Proxy server does not appear to be an HTTP proxys4   HTTP proxy server did not return a valid HTTP statusiÈ   s   {}: {}i  i“  i•  sa   
[*] Note: The HTTP proxy server may not be supported by PySocks (must be a CONNECT tunnel proxy)s   0.0.0.0i    N(   i  i“  i•  (   s   0.0.0.0i    (   Ru   R8   RÏ   R*   t   strt   appendR    t   sendallt   joinR¬   t   readlineRI   R$   t   splitRr   R<   RB   R)   R   Rv   Rw   (   R   R§   RÑ   R-   R.   R/   R0   R1   R2   t   http_headerst   fobjt   status_lineRY   t   status_codet
   status_msgRH   (    (    s$   lib/python2.7/site-packages/socks.pyt   _negotiate_HTTP˜  sB    (	
	c         C   sÀ  t  | ƒ d k s% | d j d ƒ rA t j d t | ƒ ƒ ‚ n  | \ } } |  j t j k r¹ |  j sx |  j d ƒ n  t j	 | ƒ } | d k r¦ | r¦ d |  _ n | | f |  _ d S|  j \ } } } } }	 }
 t | t t f ƒ st  | ƒ d k s| st | t ƒ r"t d ƒ ‚ n  t t |  ƒ j |  j ƒ | d k r‰| |  _ t t |  ƒ j |  j ƒ t t |  ƒ j | | f ƒ d S|  j ƒ  } y t t |  ƒ j | ƒ Wn‹ t j k
 r<} |  j ƒ  | s3| \ } } d	 j | | ƒ } t | } d
 j | | ƒ } t j d | | ƒ t | | ƒ ‚ q¼| ‚ n€ Xy! |  j | } | |  | | ƒ Wn[ t j k
 rž} | s•|  j ƒ  t d | ƒ ‚ q¼| ‚ n t k
 r»|  j ƒ  ‚  n Xd S(   sã   
        Connects to the specified destination through a proxy.
        Uses the same API as socket's connect().
        To select the proxy server, use set_proxy().

        dest_pair - 2-tuple of (IP/hostname, port).
        i   i    R;   s    PySocks doesn't support IPv6: %sRk   s   0.0.0.0Ns0   Invalid destination-connection (host, port) pairs   {}:{}s   Error connecting to {} proxy {}s   %s due to: %ss   Socket error(   Rk   i    (    Rz   R<   R8   RH   RÖ   Ry   Rq   Rt   RF   RÏ   R#   Rw   Ru   RA   Ri   t   tupleRB   R$   Rs   R+   RD   Rx   RG   R‡   RI   R   t   PRINTABLE_PROXY_TYPESt   logt   debugR%   t   _proxy_negotiatorsR   (   R   RJ   t   catch_errorsR§   RÑ   R-   RM   RN   R0   R1   R2   RH   t   proxy_servert   printable_typeR   t	   negotiate(    (    s$   lib/python2.7/site-packages/socks.pyRG   Ù  sd    	%			

		

	
c         C   sH   y |  j  | d t ƒd SWn& t k
 rC } | j r= | j S‚  n Xd S(   s&   https://docs.python.org/3/library/socket.html#socket.socket.connect_ex
        Like connect(address), but return an error indicator instead of raising an exception for errors returned by the C-level connect() call (other problems, such as "host not found" can still raise exceptions).
        Rç   i    N(   RG   R   t   OSErrort   errno(   R   RJ   R   (    (    s$   lib/python2.7/site-packages/socks.pyt
   connect_ex6  s    	c         C   sO   |  j  \ } } } } } } | p- t j | ƒ } | sE t d ƒ ‚ n  | | f S(   sD   
        Return proxy address to connect to as tuple object
        s   Invalid proxy type(   Ru   t   DEFAULT_PORTSR°   R$   (   R   R-   RM   RN   R0   R1   R2   (    (    s$   lib/python2.7/site-packages/socks.pyR‡   F  s
    N(*   R    R!   R"   R#   R,   R8   R¼   R?   R   R€   RD   R   R   R   RE   R„   RF   Rm   Rn   Ro   Rp   RI   R¤   t   getproxysocknameR   t   getproxypeernameR¦   R¥   R©   Rˆ   R   Rœ   RÓ   Rá   R   R	   R
   Ræ   R   RG   Rí   R‡   (    (    (    s$   lib/python2.7/site-packages/socks.pyR+   ñ   sJ   							%								b	-		<	;
\(   i   i    (   Rm   Rn   Ro   Rp   (F   t   base64R    t   collections.abcR   t   ImportErrort   collectionsRì   R   R   R   R   t   ioR   t   loggingt   osR   R8   RÀ   t   syst   __version__Rh   t   version_infot   win_inet_ptont	   getLoggerR    Rä   t   PROXY_TYPE_SOCKS4R   t   PROXY_TYPE_SOCKS5R	   t   PROXY_TYPE_HTTPR
   t   PROXY_TYPESRa   t   zipt   valuest   keysRã   t
   _orgsocketR`   R   t   IOErrorR   R$   R%   R&   R'   R(   R)   RÐ   R¯   Rî   R#   R   R3   R6   R7   t   getdefaultproxyR:   t
   wrapmoduleR^   R_   Rl   Rd   t   methodRA   Rc   R×   t   setattrR+   (    (    (    s$   lib/python2.7/site-packages/socks.pyt   <module>   sŠ   


!	


			8	