
    \d                        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
  G d dej                  Z G d dej                  Z G d	 d
ej                  Z G d dej                  Z G d dej                  ZdS )z)
Implementation of the SOCKSv4 protocol.
    N)deferprotocolreactor)logc                   ,    e Zd Zd Zd Zd Zd Zd ZdS )SOCKSv4Outgoingc                     || _         d S N)socksselfr   s     7lib/python3.11/site-packages/twisted/protocols/socks.py__init__zSOCKSv4Outgoing.__init__   s    


    c                     | j                                         }| j                            dd|j        |j                   | | j        _        d S )NZ   r   )portip)	transportgetPeerr   	makeReplyr   host	otherConn)r   peers     r   connectionMadezSOCKSv4Outgoing.connectionMade   sH    ~%%''
RtyAAA#
r   c                 B    | j         j                                         d S r
   r   r   loseConnectionr   reasons     r   connectionLostzSOCKSv4Outgoing.connectionLost       
++-----r   c                 :    | j                             |           d S r
   r   writer   datas     r   dataReceivedzSOCKSv4Outgoing.dataReceived!       
r   c                 p    | j                             | |           | j                            |           d S r
   r   r   r   r%   r&   s     r   r%   zSOCKSv4Outgoing.write$   4    
tT"""T"""""r   N)__name__
__module____qualname__r   r   r!   r(   r%    r   r   r   r      s_          $ $ $
. . .  # # # # #r   r   c                   &    e Zd Zd Zd Zd Zd ZdS )SOCKSv4Incomingc                 ,    || _         | | j         _        d S r
   )r   r   r   s     r   r   zSOCKSv4Incoming.__init__*   s    
#
r   c                 B    | j         j                                         d S r
   r   r   s     r   r!   zSOCKSv4Incoming.connectionLost.   r"   r   c                 :    | j                             |           d S r
   r$   r&   s     r   r(   zSOCKSv4Incoming.dataReceived1   r)   r   c                 p    | j                             | |           | j                            |           d S r
   r+   r&   s     r   r%   zSOCKSv4Incoming.write4   r,   r   N)r-   r.   r/   r   r!   r(   r%   r0   r   r   r2   r2   )   sP        $ $ $. . .  # # # # #r   r2   c                   \    e Zd ZdZdefdZd Zd Zd Zd Z	d Z
d	 Zd
 ZddZd Zd ZdS )SOCKSv4a  
    An implementation of the SOCKSv4 protocol.

    @type logging: L{str} or L{None}
    @ivar logging: If not L{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: L{str}
    @ivar buf: Part of a SOCKSv4 connection request.

    @type otherConn: C{SOCKSv4Incoming}, C{SOCKSv4Outgoing} or L{None}
    @ivar otherConn: Until the connection has been established, C{otherConn} is
        L{None}. After that, it is the proxy-to-destination protocol instance
        along which the client's connection is being forwarded.
    Nc                 "    || _         || _        d S r
   )loggingr   )r   r:   r   s      r   r   zSOCKSv4.__init__M   s    r   c                 "    d| _         d | _        d S )Nr   )bufr   )r   s    r   r   zSOCKSv4.connectionMadeQ   s    r   c                     | j         r| j                             |           dS | j        |z   | _        | j        }d| j        dd         v rB| j        dd         | j        dd         c}| _        t          j        d|dd                   \  }}}| j                            dd          \  }| _        |dd         dk    r|dd         dk    rd| j        vr	|| _        dS | j                            dd          \  }| _        | j                            |          }	|	                    | j	        ||||           |	
                    | fd	           dS t          j        |dd                   }| 	                    |||||           dS dS )
z
        Called whenever data is received.

        @type data: L{bytes}
        @param data: Part or all of a SOCKSv4 packet.
        N       !BBH         s      c                 ,    |                     d          S N[   r   resultr   s     r   <lambda>z&SOCKSv4.dataReceived.<locals>.<lambda>p   s    t~~b7I7I r   )r   r%   r<   structunpacksplitr   resolveaddCallback_dataReceived2
addErrbacksocket	inet_ntoa)
r   r'   completeBufferheadversioncoder   userserverds
             r   r(   zSOCKSv4.dataReceivedU   s    > 	N  &&&F8d?dhqrrl""!Xbqb\48ABB<ND$("(-RaR"A"AGT4!X^^GQ77ND$(AaCyO++QqS	W0D0D
 $(**-DHF#'8>>'1#=#= L((00d14$MMMIIIJJJ)$qs)44gtTBBBBB) #"r   c                    |dk    sJ d|z              |                      ||||          s|                     d           dS |dk    r6|                     ||t          |           }|                    | fd           nN|dk    r6|                     dt          | |          }|                    | fd	           nt          d
|           | j	        dk    sJ dt          | j	                  z              dS )a4  
        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: L{str}
        @param server: The IP address of the destination, represented as a
            dotted quad.

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

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

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

        @type port: L{int}
        @param port: The port number associated with the connection.
        rA   zBad version code: %srF   NrB   c                 ,    |                     d          S rE   rG   rH   s     r   rJ   z(SOCKSv4._dataReceived2.<locals>.<lambda>   s    4>>"3E3E r      r   c                 J    |                     dd| d         | d                   S )Nr   r   rB   rG   )xr   s     r   rJ   z(SOCKSv4._dataReceived2.<locals>.<lambda>   s     t~~b!QqT1Q4/P/P r   zBad Connect Code: r   z hmm, still stuff in buffer... %s)	authorizer   connectClassr   rQ   listenClassSOCKSv4IncomingFactoryrO   RuntimeErrorr<   repr)r   rY   rX   rV   rW   r   rZ   s          r   rP   zSOCKSv4._dataReceived2w   s   . !|||3g=|||~~dFD$77 	NN2F199!!&$FFALLTEEEFFFFQYY  $:D&IIAMMPPPQQQQ:D::;;;x3 BT$(^^ Sr   c                 T    | j         r | j         j                                         d S d S r
   )r   r   r   r   s     r   r!   zSOCKSv4.connectionLost   s3    > 	6N$3355555	6 	6r   c                 H    t          j        d|d|d|d|d	           dS )Nzcode z connection to :z (user z) authorizedrB   )r   msg)r   rW   rY   r   rX   s        r   r`   zSOCKSv4.authorize   s=    ttVVVTTT444)	
 	
 	
 qr   c                 Z    t          j        t          |g|R                      ||          S r
   )r   ClientCreatorr   
connectTCP)r   r   r   klassargss        r   ra   zSOCKSv4.connectClass   s-    %gu<t<<<GGdSSSr   c                     t          j        | ||           }t          j        |                                dd                    S )NrB   )r   	listenTCPr   succeedgetHost)r   r   rm   rn   servs        r   rb   zSOCKSv4.listenClass   s;     uud|44}T\\^^ABB/000r   r   0.0.0.0c                     | j                             t          j        d|||          t	          j        |          z              |dk    r| j                                          d S d S )Nr@   r   )r   r%   rK   packrR   	inet_atonr   )r   replyrV   r   r   s        r   r   zSOCKSv4.makeReply   sk    K558H8L8LL	
 	
 	
 B;;N))+++++ ;r   c                 f    |                      | |           | j                            |           d S r
   )r   r   r%   r&   s     r   r%   zSOCKSv4.write   s2    tT"""""r   c           	      \   | j         sd S | j                                        }| j        j                                        }t	          | j         d          }|                    dt          j                    |j        |j	        || k    rdpd|j        |j	        fz             |r|d d         |dd          }}|                    t          j        t          d |          d          dz              |                    dt          |          z
  dz  dz             |D ]M}t          t          |                    dk    r|                    d	           8|                    |           N|                    d
           ||                    d
           |                                 d S )Naz%s	%s:%d %s %s:%d
<>   c                 &    dt          |           z  S )Nz%02X)ord)r_   s    r   rJ   zSOCKSv4.log.<locals>.<lambda>   s    fs1vvo r       .
)r:   r   r   r   openr%   timectimer   r   stringjoinmaplenre   close)r   protor'   r   
their_peerfpcs           r   r   zSOCKSv4.log   s   | 	F~%%''^-5577
s##	"
		4-'C/C
	
 
	
 
	
  		3B3ibcctAGGFK$=$=q A A3GG#MNNNGGR#a&&[A%+,,,  tAww<<!##GGCLLLLGGAJJJJGGDMMM  		 	
						r   )r   r   rt   )r-   r.   r/   __doc__r   r   r   r(   rP   r!   r`   ra   rb   r   r%   r   r0   r   r   r8   r8   9   s         &  $W       C  C  CD#T #T #TJ6 6 6  T T T1 1 1, , , ,# # #    r   r8   c                       e Zd ZdZd Zd ZdS )SOCKSv4Factoryz`
    A factory for a SOCKSv4 proxy.

    Constructor accepts one argument, a log file name.
    c                     || _         d S r
   )r:   )r   r   s     r   r   zSOCKSv4Factory.__init__   s    r   c                 6    t          | j        t                    S r
   )r8   r:   r   r   addrs     r   buildProtocolzSOCKSv4Factory.buildProtocol   s    t|W---r   Nr-   r.   r/   r   r   r   r0   r   r   r   r      s<           . . . . .r   r   c                       e Zd ZdZd Zd ZdS )rc   zJ
    A utility class for building protocols for incoming connections.
    c                 "    || _         || _        d S r
   )r   r   )r   r   r   s      r   r   zSOCKSv4IncomingFactory.__init__   s    
r   c                     |d         | j         k    r6d| _         | j                            dd           t          | j                  S | j         dk    rd S | j                            dd           d| _         d S )Nr    r   rF   )r   r   r   r2   r   s     r   r   z$SOCKSv4IncomingFactory.buildProtocol   sv    7dgDGJ  Q'''"4:...W]]4J  Q'''DG4r   Nr   r0   r   r   rc   rc      s<           
 
 
 
 
r   rc   )r   rR   r   rK   r   twisted.internetr   r   r   twisted.pythonr   Protocolr   r2   r8   Factoryr   rc   r0   r   r   <module>r      s]  
      6 5 5 5 5 5 5 5 5 5      # # # # #h' # # #(# # # # #h' # # # \ \ \ \ \h \ \ \~. . . . .X% . . .    X-     r   