ó
ì2;Zc        
   @   s‹  d  Z  d d l m Z d d l m Z d 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 dD 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/ 6d0 d1 6d2 d3 6Z2 i d4 e 6d4 e 6d5 e 6Z3 d d d e5 d d d6 „ Z6 d7 „  Z7 d8 „  Z8 e8 Z9 d9 „  Z: e: Z; d d d d d e5 d d d d: „	 Z< d; e j f d< „  ƒ  YZ= d= „  Z> xX dE D]P Z e? e= e d ƒ Z@ eA e@ e ƒ se= jB jC e ƒ eD e= e e> e ƒ ƒ qqWdB e= f dC „  ƒ  YZE d S(F   sŠ
  SocksiPy - Python SOCKS module.

Copyright 2006 Dan-Haim. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.
3. Neither the name of Dan Haim nor the names of his contributors may be used
   to endorse or promote products derived from this software without specific
   prior written permission.

THIS SOFTWARE IS PROVIDED BY DAN HAIM "AS IS" AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
EVENT SHALL DAN HAIM OR HIS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


This module provides a standard socket-like interface for Python
for tunneling connections through SOCKS proxies.

===============================================================================

Minor modifications made by Christopher Gilbert (http://motomastyle.com/)
for use in PyLoris (http://pyloris.sourceforge.net/)

Minor modifications made by Mario Vilas (http://breakingcode.wordpress.com/)
mainly to merge bug fixes found in Sourceforge

Modifications made by Anorov (https://github.com/Anorov)
-Forked and renamed to PySocks
-Fixed issue with HTTP proxy failure checking (same bug that was in the
 old ___recvall() method)
-Included SocksiPyHandler (sockshandler.py), to be used as a urllib2 handler,
 courtesy of e000 (https://github.com/e000):
 https://gist.github.com/869791#file_socksipyhandler.py
-Re-styled code to make it readable
    -Aliased PROXY_TYPE_SOCKS5 -> SOCKS5 etc.
    -Improved exception handling and output
    -Removed irritating use of sequence indexes, replaced with tuple unpacked
     variables
    -Fixed up Python 3 bytestring handling - chr(0x03).encode() -> b""
    -Other general fixes
-Added clarification that the HTTP proxy connection method only supports
 CONNECT-style tunneling HTTP proxies
-Various small bug fixes
iÿÿÿÿ(   t	   b64encode(   t   Callable(   t
   EOPNOTSUPPt   EINVALt   EAGAINN(   t   BytesIO(   t   SEEK_CURs   1.6.7t   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)   /usr/lib/python2.7/dist-packages/socks.pyt   wrapper]   s    
 (   t	   functoolst   wraps(   R   R   (    (   R   s)   /usr/lib/python2.7/dist-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   : {0}(   t   msgt
   socket_errt   format(   R   R   R   (    (    s)   /usr/lib/python2.7/dist-packages/socks.pyt   __init__p   s    		c         C   s   |  j  S(   N(   R   (   R   (    (    s)   /usr/lib/python2.7/dist-packages/socks.pyt   __str__w   s    N(   t   __name__t
   __module__t   __doc__t   NoneR   R   (    (    (    s)   /usr/lib/python2.7/dist-packages/socks.pyR   n   s   t   GeneralProxyErrorc           B   s   e  Z RS(    (   R    R!   (    (    (    s)   /usr/lib/python2.7/dist-packages/socks.pyR$   {   s   t   ProxyConnectionErrorc           B   s   e  Z RS(    (   R    R!   (    (    (    s)   /usr/lib/python2.7/dist-packages/socks.pyR%      s   t   SOCKS5AuthErrorc           B   s   e  Z RS(    (   R    R!   (    (    (    s)   /usr/lib/python2.7/dist-packages/socks.pyR&   ƒ   s   t   SOCKS5Errorc           B   s   e  Z RS(    (   R    R!   (    (    (    s)   /usr/lib/python2.7/dist-packages/socks.pyR'   ‡   s   t   SOCKS4Errorc           B   s   e  Z RS(    (   R    R!   (    (    (    s)   /usr/lib/python2.7/dist-packages/socks.pyR(   ‹   s   t	   HTTPErrorc           B   s   e  Z RS(    (   R    R!   (    (    (    s)   /usr/lib/python2.7/dist-packages/socks.pyR)      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)   /usr/lib/python2.7/dist-packages/socks.pyt   set_default_proxy¨   s    c          O   s/   d | k r" | j  d ƒ | d <n  t |  | Ž  S(   Nt	   proxytypeR-   (   t   popR3   (   R   R   (    (    s)   /usr/lib/python2.7/dist-packages/socks.pyt   setdefaultproxy³   s    c           C   s   t  j S(   s4   Returns the default proxy, set by set_default_proxy.(   R+   R,   (    (    (    s)   /usr/lib/python2.7/dist-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)   /usr/lib/python2.7/dist-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)   /usr/lib/python2.7/dist-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)   /usr/lib/python2.7/dist-packages/socks.pyR     s
    (   R    R!   R"   R   t   listRc   (    (    (    s)   /usr/lib/python2.7/dist-packages/socks.pyR_     s   	c            s   ‡  f d †  S(   Nc            s   |  j  ˆ  | | Ž  S(   N(   Rb   (   R   Rf   Rg   (   Rh   (    s)   /usr/lib/python2.7/dist-packages/socks.pyt   <lambda>  t    (    (   Rh   (    (   Rh   s)   /usr/lib/python2.7/dist-packages/socks.pyt   _makemethod  s    t   sendtot   sendt   recvfromt   recvR+   c           B   sF  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 „  ƒ 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)   /usr/lib/python2.7/dist-packages/socks.pyR   1  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)   /usr/lib/python2.7/dist-packages/socks.pyt   _readallC  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)   /usr/lib/python2.7/dist-packages/socks.pyRD   O  s    	c         C   s   |  j  S(   N(   Rx   (   R   (    (    s)   /usr/lib/python2.7/dist-packages/socks.pyR   X  s    c         C   s'   | r |  j  d  ƒ n |  j  d ƒ d  S(   Ng        (   RD   R#   (   R   t   v(    (    s)   /usr/lib/python2.7/dist-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)   /usr/lib/python2.7/dist-packages/socks.pyRE   a  s    c         O   s2   d | k r" | j  d ƒ | d <n  |  j | | Ž  S(   NR4   R-   (   R5   RE   (   R   R   R   (    (    s)   /usr/lib/python2.7/dist-packages/socks.pyt   setproxyv  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)   /usr/lib/python2.7/dist-packages/socks.pyRF   {  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)   /usr/lib/python2.7/dist-packages/socks.pyRm      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)   /usr/lib/python2.7/dist-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)   /usr/lib/python2.7/dist-packages/socks.pyRo   º  s    	%	c         O   s   |  j  | | Ž  \ } } | S(   N(   Ro   (   R   Rf   Rg   R’   R‰   (    (    s)   /usr/lib/python2.7/dist-packages/socks.pyRp   Î  s    c         C   s,   |  j  r |  j  j ƒ  n  t t |  ƒ j ƒ  S(   N(   Rt   RI   Rs   R+   (   R   (    (    s)   /usr/lib/python2.7/dist-packages/socks.pyRI   Ò  s    	c         C   s   |  j  S(   s:   Returns the bound IP address and port number at the proxy.(   Rv   (   R   (    (    s)   /usr/lib/python2.7/dist-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)   /usr/lib/python2.7/dist-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)   /usr/lib/python2.7/dist-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)   /usr/lib/python2.7/dist-packages/socks.pyt   _negotiate_SOCKS5í  s    c         C   s¡  |  j  \ } } } } } }	 | j d ƒ }
 | j d d ƒ } zI| r[ |	 r[ |
 j d ƒ n |
 j d ƒ |
 j ƒ  |  j | d ƒ } | d d !d k r¦ t d	 ƒ ‚ n  | d d !d
 k r]|
 j d t t | ƒ ƒ j ƒ  | t t |	 ƒ ƒ j ƒ  |	 ƒ |
 j ƒ  |  j | d ƒ } | d d !d k r8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 rR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   s   s    s   SOCKS5 authentication faileds   ÿs7   All offered SOCKS5 authentication methods were rejectedi   s   Unknown errors   {0:#04x}: {1}N(   Ru   t   makefileRŽ   t   flushR€   R$   t   chrRz   R*   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)   /usr/lib/python2.7/dist-packages/socks.pyRˆ   ó  sX    
;


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)   /usr/lib/python2.7/dist-packages/socks.pyR   M  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)   /usr/lib/python2.7/dist-packages/socks.pyRœ   z  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   {0:#04x}: {1}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)   /usr/lib/python2.7/dist-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   {0}: {1}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)   /usr/lib/python2.7/dist-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 ƒ  | \ } } d	 j | | ƒ } t | } d
 j | | ƒ } t j d | |
 ƒ t | |
 ƒ ‚ nq Xy! |  j | } | |  | | ƒ WnL t j k
 r€}
 |  j ƒ  t d |
 ƒ ‚ 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   {0}:{1}s!   Error connecting to {0} proxy {1}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   R§   RÑ   R-   RM   RN   R0   R1   R2   RH   t   proxy_servert   printable_typeR   t	   negotiate(    (    s)   /usr/lib/python2.7/dist-packages/socks.pyRG     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)   /usr/lib/python2.7/dist-packages/socks.pyR‡   ]  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‡   (    (    (    s)   /usr/lib/python2.7/dist-packages/socks.pyR+   &  sF   							%								Z	-		<	;
W(   i   i    (   Rm   Rn   Ro   Rp   (F   R"   t   base64R    t   collectionsR   t   errnoR   R   R   R   t   ioR   t   loggingt   osR   R8   RÀ   t   syst   __version__Rh   t   version_infot   win_inet_ptont   ImportErrort	   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)   /usr/lib/python2.7/dist-packages/socks.pyt   <module>7   s†   


!	


			8	