ó
8úMc        
   @   sO  d  Z  d d l Z d d l Z d d l Z e e d d ƒ d k rQ e d ƒ ‚ n  d Z d Z d Z	 d a
 e j 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 d0 Z d1 Z d2 Z d3 Z d d d e d d d, „ Z d- „  Z d. e j f d/ „  ƒ  YZ d S(4   s  SocksiPy - Python SOCKS module.
Version 1.00

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 DAMANGE.


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

iÿÿÿÿNt   sockets-   socket.socket missing, proxy support unusablei   i   i   t
   ProxyErrorc           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s2   /usr/lib/python2.7/dist-packages/httplib2/socks.pyR   9   s    t   GeneralProxyErrorc           B   s   e  Z RS(    (   R   R   (    (    (    s2   /usr/lib/python2.7/dist-packages/httplib2/socks.pyR   :   s    t   Socks5AuthErrorc           B   s   e  Z RS(    (   R   R   (    (    (    s2   /usr/lib/python2.7/dist-packages/httplib2/socks.pyR   ;   s    t   Socks5Errorc           B   s   e  Z RS(    (   R   R   (    (    (    s2   /usr/lib/python2.7/dist-packages/httplib2/socks.pyR   <   s    t   Socks4Errorc           B   s   e  Z RS(    (   R   R   (    (    (    s2   /usr/lib/python2.7/dist-packages/httplib2/socks.pyR   =   s    t	   HTTPErrorc           B   s   e  Z RS(    (   R   R   (    (    (    s2   /usr/lib/python2.7/dist-packages/httplib2/socks.pyR   >   s    t   successs   invalid datas   not connecteds   not availables   bad proxy types	   bad inputt	   succeededs   general SOCKS server failures!   connection not allowed by rulesets   Network unreachables   Host unreachables   Connection refuseds   TTL expireds   Command not supporteds   Address type not supporteds   Unknown errors   authentication is requireds0   all offered authentication methods were rejecteds$   unknown username or invalid passwords   unknown errors   request granteds   request rejected or failedsL   request rejected because SOCKS server cannot connect to identd on the clientsP   request rejected because the client program and identd report different user-idsc         C   s   |  | | | | | f a  d S(   s´   setdefaultproxy(proxytype, addr[, port[, rdns[, username[, password]]]])
    Sets a default proxy which all further socksocket objects will use,
    unless explicitly changed.
    N(   t   _defaultproxy(   t	   proxytypet   addrt   portt   rdnst   usernamet   password(    (    s2   /usr/lib/python2.7/dist-packages/httplib2/socks.pyt   setdefaultproxy^   s    c         C   s+   t  d k r t |  j _ n t d ƒ ‚ d S(   s2  wrapmodule(module)
    Attempts to replace a module's socket library with a SOCKS socket. Must set
    a default proxy using setdefaultproxy(...) first.
    This will only work on modules that import socket directly into the namespace;
    most of the Python Standard Library falls into this category.
    i   s   no proxy specifiedN(   i   s   no proxy specified(   R   t   Nonet
   socksocketR    R   (   t   module(    (    s2   /usr/lib/python2.7/dist-packages/httplib2/socks.pyt
   wrapmodulef   s    R   c           B   sŒ   e  Z d  Z e j e j d d d „ Z d „  Z d d d e	 d d d „ Z
 d „  Z d „  Z d „  Z d „  Z d	 „  Z d
 „  Z d „  Z RS(   s  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, type=SOCK_STREAM and proto=0.
    i    c         C   sP   t  j |  | | | | ƒ t d  k r1 t |  _ n	 d |  _ d  |  _ d  |  _ d  S(   N(   NNNNNN(   t
   _orgsockett   __init__R   R   t   _socksocket__proxyt   _socksocket__proxysocknamet   _socksocket__proxypeername(   t   selft   familyt   typet   protot   _sock(    (    s2   /usr/lib/python2.7/dist-packages/httplib2/socks.pyR   y   s    		c         C   sd   |  j  | ƒ } xN t | ƒ | k  r_ |  j  | t | ƒ ƒ } | sR t d ƒ ‚ n  | | } q W| S(   s®   __recvall(count) -> data
        Receive EXACTLY the number of bytes requested from the socket.
        Blocks until the required number of bytes have been received.
        i    s   connection closed unexpectedly(   i    s   connection closed unexpectedly(   t   recvt   lenR   (   R   t   countt   datat   d(    (    s2   /usr/lib/python2.7/dist-packages/httplib2/socks.pyt	   __recvall‚   s     c         C   s   | | | | | | f |  _  d S(   s­  setproxy(proxytype, addr[, port[, rdns[, username[, password]]]])
        Sets the proxy to be used.
        proxytype -    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 preformed 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   R   R   R   R   R   R   (    (    s2   /usr/lib/python2.7/dist-packages/httplib2/socks.pyt   setproxyŽ   s    c   
      C   s$  |  j  d d k rK |  j  d d k rK |  j t j d d d d d ƒ ƒ n |  j t j d d d d ƒ ƒ |  j d ƒ } | d d !t d ƒ j ƒ  k r» |  j ƒ  t	 d t
 d f ƒ ‚ n  | d d !t d ƒ j ƒ  k rÝ ng| d d !t d ƒ j ƒ  k rï|  j t d ƒ j ƒ  t t |  j  d ƒ ƒ |  j  d t t |  j  d ƒ ƒ |  j  d ƒ |  j d ƒ } | d d !t d ƒ j ƒ  k rª|  j ƒ  t	 d t
 d f ƒ ‚ n  | d d !t d ƒ j ƒ  k rD|  j ƒ  t d t d f ƒ ‚ qDnU |  j ƒ  | d t d	 ƒ j ƒ  k r.t d t d f ƒ ‚ n t	 d t
 d f ƒ ‚ t j d d d d ƒ } y- t j | ƒ } | t d ƒ j ƒ  | } WnŒ t j k
 r|  j  d râd } | t d ƒ j ƒ  t t | ƒ ƒ j ƒ  | } qt j t j | ƒ ƒ } | t d ƒ j ƒ  | } n X| t j d
 | ƒ } |  j | ƒ |  j d ƒ } | d d !t d ƒ j ƒ  k rŒ|  j ƒ  t	 d t
 d f ƒ ‚ n0| d d !t d ƒ j ƒ  k r|  j ƒ  t | d d !ƒ d k rt t | d d !ƒ t t | d d !ƒ f ƒ ‚ q¼t d t d f ƒ ‚ n¢ | d d !t d ƒ j ƒ  k rK|  j d ƒ } nq | d d !t d ƒ j ƒ  k rœ| |  j d ƒ } |  j t | d d !ƒ ƒ } n  |  j ƒ  t	 d t
 d f ƒ ‚ t j d
 |  j d ƒ ƒ d }	 | |	 f |  _ | d k rt j | ƒ | f |  _ n | | f |  _ d S(   sk   __negotiatesocks5(self,destaddr,destport)
        Negotiates a connection through a SOCKS5 server.
        i   i   t   BBBBi   i    t   BBBi   i   iÿ   s   >Hi   i	   N(   R   R   t   sendallt   structt   packt   _socksocket__recvallt   chrt   encodet   closeR   t   _generalerrorsR"   R   t   _socks5autherrorsR    t	   inet_atont   errort   gethostbynamet   ordR   t   _socks5errorsR!   t   unpackR   t	   inet_ntoaR   (
   R   t   destaddrt   destportt
   chosenautht   authstatt   reqt   ipaddrt   respt	   boundaddrt	   boundport(    (    s2   /usr/lib/python2.7/dist-packages/httplib2/socks.pyt   __negotiatesocks5¡   sn    &%
]


3

3
c         C   s   |  j  S(   si   getsockname() -> address info
        Returns the bound IP address and port number at the proxy.
        (   R   (   R   (    (    s2   /usr/lib/python2.7/dist-packages/httplib2/socks.pyt   getproxysocknameý   s    c         C   s   t  j |  ƒ S(   s`   getproxypeername() -> address info
        Returns the IP and port number of the proxy.
        (   R   t   getpeername(   R   (    (    s2   /usr/lib/python2.7/dist-packages/httplib2/socks.pyt   getproxypeername  s    c         C   s   |  j  S(   s£   getpeername() -> address info
        Returns the IP address and port number of the destination
        machine (note: getproxypeername returns the proxy)
        (   R   (   R   (    (    s2   /usr/lib/python2.7/dist-packages/httplib2/socks.pyRE   	  s    c         C   s\  t  } y t j | ƒ } Wn] t j k
 rx |  j d r] t j d d d d d ƒ } t } qy t j t j | ƒ ƒ } n Xt j d d d | ƒ | } |  j d d k r¼ | |  j d } n  | t
 d ƒ j ƒ  } | rõ | | t
 d ƒ j ƒ  } n  |  j | ƒ |  j d ƒ } | d d !t
 d ƒ j ƒ  k rS|  j ƒ  t d t d f ƒ ‚ n  | d d !t
 d	 ƒ j ƒ  k rï|  j ƒ  t | d d !ƒ d k rÖ|  j ƒ  t t | d d !ƒ t t | d d !ƒ d	 f ƒ ‚ qït d t d f ƒ ‚ n  t j | d ƒ t j d | d d !ƒ d f |  _ | d k rIt j | ƒ | f |  _ n | | f |  _ d S(   sk   __negotiatesocks4(self,destaddr,destport)
        Negotiates a connection through a SOCKS4 server.
        i   R(   i    i   s   >BBHi   i   i   iZ   i[   i\   i]   i^   s   >HN(   i[   i\   i]   (   t   FalseR    R3   R4   R   R+   R,   t   TrueR5   R   R.   R/   R*   R-   R0   R   R1   R6   R   t   _socks4errorsR9   R8   R   R   (   R   R:   R;   t   rmtrslvR?   R>   R@   (    (    s2   /usr/lib/python2.7/dist-packages/httplib2/socks.pyt   __negotiatesocks4  s:    	


73c         C   s˜  |  j  d s t j | ƒ } n | } |  j d | d t | ƒ d d | d j ƒ  ƒ |  j d ƒ } x2 | j d j ƒ  ƒ d k r | |  j d ƒ } ql W| j ƒ  d	 j	 d
 j ƒ  d ƒ } | d	 d j ƒ  d j ƒ  f k r|  j
 ƒ  t d t d f ƒ ‚ n  y t | d ƒ } Wn1 t k
 rL|  j
 ƒ  t d t d f ƒ ‚ n X| d k r||  j
 ƒ  t | | d f ƒ ‚ n  d |  _ | | f |  _ d S(   sh   __negotiatehttp(self,destaddr,destport)
        Negotiates a connection through an HTTP server.
        i   s   CONNECT t   :s    HTTP/1.1
s   Host: s   

i   iÿÿÿÿi    t    i   s   HTTP/1.0s   HTTP/1.1iÈ   s   0.0.0.0N(   s   0.0.0.0i    (   R   R    R5   R*   t   strR/   R!   t   findt
   splitlinest   splitR0   R   R1   t   intt
   ValueErrorR   R   R   (   R   R:   R;   R   R@   t
   statuslinet
   statuscode(    (    s2   /usr/lib/python2.7/dist-packages/httplib2/socks.pyt   __negotiatehttp@  s*    5""


	c         C   s  t  | ƒ t t f k s\ t | ƒ d k  s\ t  | d ƒ t  d ƒ k s\ t  | d ƒ t k ru t d t d f ƒ ‚ n  |  j d t k ré |  j d d	 k r« |  j d } n d } t
 j |  |  j d | f ƒ |  j | d | d ƒ n2|  j d t k r]|  j d d	 k r|  j d } n d } t
 j |  |  j d | f ƒ |  j | d | d ƒ n¾ |  j d t k rÑ|  j d d	 k r“|  j d } n d } t
 j |  |  j d | f ƒ |  j | d | d ƒ nJ |  j d d	 k rt
 j |  | d | d f ƒ n t d t d f ƒ ‚ d	 S(
   sÿ   connect(self, despair)
        Connects to the specified destination through a proxy.
        destpar - A tuple of the IP/DNS address and the port number.
        (identical to socket's connect).
        To select the proxy server use setproxy().
        i   i    t    i   i   i8  i  i   N(   R   t   listt   tupleR"   RR   R   R1   R   t   PROXY_TYPE_SOCKS5R   R   t   connectt   _socksocket__negotiatesocks5t   PROXY_TYPE_SOCKS4t   _socksocket__negotiatesocks4t   PROXY_TYPE_HTTPt   _socksocket__negotiatehttp(   R   t   destpairt   portnum(    (    s2   /usr/lib/python2.7/dist-packages/httplib2/socks.pyR[   _  s.    \!N(   R   R   t   __doc__R    t   AF_INETt   SOCK_STREAMR   R   R-   RH   R'   R\   RD   RF   RE   R^   R`   R[   (    (    (    s2   /usr/lib/python2.7/dist-packages/httplib2/socks.pyR   r   s   			\				0	(   s   successs   invalid datas   not connecteds   not availables   bad proxy types	   bad input(
   R
   s   general SOCKS server failures!   connection not allowed by rulesets   Network unreachables   Host unreachables   Connection refuseds   TTL expireds   Command not supporteds   Address type not supporteds   Unknown error(   R
   s   authentication is requireds0   all offered authentication methods were rejecteds$   unknown username or invalid passwords   unknown error(   s   request granteds   request rejected or failedsL   request rejected because SOCKS server cannot connect to identd on the clientsP   request rejected because the client program and identd report different user-idss   unknown error(   Rc   R    R+   t   syst   getattrR   t   ImportErrorR]   RZ   R_   R   R   t	   ExceptionR   R   R   R   R   R   R1   R7   R2   RI   RH   R   R   R   (    (    (    s2   /usr/lib/python2.7/dist-packages/httplib2/socks.pyt   <module>   sZ   	                      	