ó
[³XMc           @   sã   d  Z  d d l Z d d l Z d d l Z d d l Z d d l m Z m Z m Z d d l	 m
 Z
 d e j f d „  ƒ  YZ d e j f d „  ƒ  YZ d	 e j f d
 „  ƒ  YZ d e j f d „  ƒ  YZ d e j f d „  ƒ  YZ d S(   s)   
Implementation of the SOCKSv4 protocol.
iÿÿÿÿN(   t   reactort   protocolt   defer(   t   logt   SOCKSv4Outgoingc           B   s5   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z RS(   c         C   s   | |  _  d  S(   N(   t   socks(   t   selfR   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/socks.pyt   __init__   s    c         C   sD   |  j  j ƒ  } |  j j d d d | j d | j ƒ|  |  j _ d  S(   NiZ   i    t   portt   ip(   t	   transportt   getPeerR   t	   makeReplyR   t   hostt	   otherConn(   R   t   peer(    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/socks.pyt   connectionMade   s    %c         C   s   |  j  j j ƒ  d  S(   N(   R   R
   t   loseConnection(   R   t   reason(    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/socks.pyt   connectionLost   s    c         C   s   |  j  j | ƒ d  S(   N(   R   t   write(   R   t   data(    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/socks.pyt   dataReceived!   s    c         C   s'   |  j  j |  | ƒ |  j j | ƒ d  S(   N(   R   R   R
   R   (   R   R   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/socks.pyR   $   s    (   t   __name__t
   __module__R   R   R   R   R   (    (    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/socks.pyR      s
   				t   SOCKSv4Incomingc           B   s,   e  Z d  „  Z d „  Z d „  Z d „  Z RS(   c         C   s   | |  _  |  |  j  _ d  S(   N(   R   R   (   R   R   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/socks.pyR   ,   s    	c         C   s   |  j  j j ƒ  d  S(   N(   R   R
   R   (   R   R   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/socks.pyR   0   s    c         C   s   |  j  j | ƒ d  S(   N(   R   R   (   R   R   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/socks.pyR   3   s    c         C   s'   |  j  j |  | ƒ |  j j | ƒ d  S(   N(   R   R   R
   R   (   R   R   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/socks.pyR   6   s    (   R   R   R   R   R   R   (    (    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/socks.pyR   *   s   			t   SOCKSv4c           B   s€   e  Z d  Z d e d „ Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d „  Z d	 d	 d
 d „ Z d „  Z d „  Z RS(   sË  
    An implementation of the SOCKSv4 protocol.

    @type logging: C{str} or C{None}
    @ivar logging: If not C{None}, the name of the logfile to which connection
        information will be written.

    @type reactor: object providing L{twisted.internet.interfaces.IReactorTCP}
    @ivar reactor: The reactor used to create connections.

    @type buf: C{str}
    @ivar buf: Part of a SOCKSv4 connection request.

    @type otherConn: C{SOCKSv4Incoming}, C{SOCKSv4Outgoing} or C{None}
    @ivar otherConn: Until the connection has been established, C{otherConn} is
        C{None}. After that, it is the proxy-to-destination protocol instance
        along which the client's connection is being forwarded.
    c         C   s   | |  _  | |  _ d  S(   N(   t   loggingR    (   R   R   R    (    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/socks.pyR   N   s    	c         C   s   d |  _  d  |  _ d  S(   Nt    (   t   buft   NoneR   (   R   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/socks.pyR   R   s    	c   
      C   s|  |  j  r |  j  j | ƒ d S|  j | |  _ |  j } d |  j d k rx|  j d  |  j d } |  _ t j d | d  ƒ \ } } } |  j j d d ƒ \ } |  _ | d d !d k rF| d d k rFd |  j k rã | |  _ d S|  j j d d ƒ \ } |  _ |  j j | ƒ }	 |	 j |  j	 | | | | ƒ |	 j
 |  d	 „ ƒ d St j | d d !ƒ } |  j	 | | | | | ƒ n  d S(
   s…   
        Called whenever data is received.

        @type data: C{str}
        @param data: Part or all of a SOCKSv4 packet.
        Nt    i   s   !BBHi   i   i   t      c         S   s   | j  d ƒ S(   Ni[   (   R   (   t   resultR   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/socks.pyt   <lambda>r   s    (   R   R   R   t   structt   unpackt   splitR    t   resolvet   addCallbackt   _dataReceived2t
   addErrbackt   sockett	   inet_ntoa(
   R   R   t   completeBuffert   headt   versiont   codeR   t   usert   servert   d(    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/socks.pyR   V   s*    		#	c         C   sì   | d k s t  d | ‚ |  j | | | | ƒ sB |  j d ƒ d S| d k r| |  j | | t |  ƒ } | j |  d „ ƒ nG | d k r¶ |  j d t |  | ƒ } | j |  d	 „ ƒ n t	 d
 | ‚ |  j
 d k sè t  d t |  j
 ƒ ‚ d S(   s3  
        The second half of the SOCKS connection setup. For a SOCKSv4 packet this
        is after the server address has been extracted from the header. For a
        SOCKSv4a packet this is after the host name has been resolved.

        @type server: C{str}
        @param server: The IP address of the destination, represented as a
            dotted quad.

        @type user: C{str}
        @param user: The username associated with the connection.

        @type version: C{int}
        @param version: The SOCKS protocol version number.

        @type code: C{int}
        @param code: The comand code. 1 means establish a TCP/IP stream
            connection, and 2 means establish a TCP/IP port binding.

        @type port: C{int}
        @param port: The port number associated with the connection.
        i   s   Bad version code: %si[   Ni   c         S   s   | j  d ƒ S(   Ni[   (   R   (   R!   R   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/socks.pyR"   –   s    i   i    c         S   s"   |  \ } } | j  d d | | ƒ S(   NiZ   i    (   R   (   t   .0R   t   ht   p(    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/socks.pyR"   ™   s    	s   Bad Connect Code: %sR   s    hmm, still stuff in buffer... %s(   t   AssertionErrort	   authorizeR   t   connectClassR   R)   t   listenClasst   SOCKSv4IncomingFactoryR'   t   RuntimeErrorR   t   repr(   R   R1   R0   R.   R/   R   R2   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/socks.pyR(   y   s    c         C   s    |  j  r |  j  j j ƒ  n  d  S(   N(   R   R
   R   (   R   R   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/socks.pyR       s    	c         C   s!   t  j d | | | | f ƒ d S(   Ns0   code %s connection to %s:%s (user %s) authorizedi   (   R   t   msg(   R   R/   R1   R   R0   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/socks.pyR7   ¤   s    c         G   s   t  j t | | Œ j | | ƒ S(   N(   R   t   ClientCreatorR    t
   connectTCP(   R   R   R   t   klasst   args(    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/socks.pyR8   ¨   s    c         G   s/   t  j | | | Œ  ƒ } t j | j ƒ  d ƒ S(   Ni   (   R    t	   listenTCPR   t   succeedt   getHost(   R   R   R@   RA   t   serv(    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/socks.pyR9   «   s    i    s   0.0.0.0c         C   sO   |  j  j t j d | | | ƒ t j | ƒ ƒ | d k rK |  j  j ƒ  n  d  S(   Ns   !BBHiZ   (   R
   R   R#   t   packR*   t	   inet_atonR   (   R   t   replyR.   R   R	   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/socks.pyR   ¯   s    / c         C   s$   |  j  |  | ƒ |  j j | ƒ d  S(   N(   R   R
   R   (   R   R   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/socks.pyR   ³   s    c      
   C   s_  |  j  s d  S|  j j ƒ  } |  j j j ƒ  } t |  j  d ƒ } | j d t j ƒ  | j | j	 | |  k rp d ps d | j | j	 f ƒ xº | rC| d  | d } } | j t
 j t d „  | ƒ d ƒ d ƒ | j d t | ƒ d d ƒ xC | D]; } t t | ƒ ƒ d k r"| j d	 ƒ qô | j | ƒ qô W| j d
 ƒ qŠ W| j d
 ƒ | j ƒ  d  S(   Nt   as   %s	%s:%d %s %s:%d
t   <t   >i   c         S   s   d t  |  ƒ S(   Ns   %02X(   t   ord(   t   x(    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/socks.pyR"   Â   s    t    i   t   .s   
(   R   R
   R   R   t   openR   t   timet   ctimeR   R   t   stringt   joint   mapt   lenR<   t   close(   R   t   protoR   R   t
   their_peert   fR5   t   c(    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/socks.pyR   ·   s(    	 	) N(   R   R   t   __doc__R   R    R   R   R   R(   R   R7   R8   R9   R   R   R   (    (    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/socks.pyR   ;   s   		#	'					t   SOCKSv4Factoryc           B   s    e  Z d  Z d „  Z d „  Z RS(   s`   
    A factory for a SOCKSv4 proxy.

    Constructor accepts one argument, a log file name.
    c         C   s   | |  _  d  S(   N(   R   (   R   R   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/socks.pyR   Ô   s    c         C   s   t  |  j t ƒ S(   N(   R   R   R    (   R   t   addr(    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/socks.pyt   buildProtocol×   s    (   R   R   R\   R   R_   (    (    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/socks.pyR]   Í   s   	R:   c           B   s    e  Z d  Z d „  Z d „  Z RS(   sJ   
    A utility class for building protocols for incoming connections.
    c         C   s   | |  _  | |  _ d  S(   N(   R   R	   (   R   R   R	   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/socks.pyR   á   s    	c         C   ss   | d |  j  k r< d |  _  |  j j d d ƒ t |  j ƒ S|  j  d k rO d  S|  j j d d ƒ d |  _  d  Sd  S(   Ni    R   iZ   i[   (   R	   R   R   R   R   (   R   R^   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/socks.pyR_   æ   s    		(   R   R   R\   R   R_   (    (    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/socks.pyR:   Ü   s   	(   R\   R#   RS   R*   RQ   t   twisted.internetR    R   R   t   twisted.pythonR   t   ProtocolR   R   R   t   FactoryR]   R:   (    (    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/socks.pyt   <module>   s   ’