
    i                     &   d dl Z d dlZddlmZ ddlmZmZ ddlmZm	Z	m
Z
mZ  G d dee j                  Z G d d	ee j                  Z G d
 dej                  Z G d dej                  Z G d dej                  Z G d d          ZdS )    N   )
StrOrBytes)ProtocolError
SOCKSError)AddressTypedecode_addressencode_address#get_address_port_tuple_from_addressc                   "    e Zd ZdZdZdZdZdZdS )SOCKS4ReplyCodez"Enumeration of SOCKS4 reply codes.   Z   [   \   ]N)__name__
__module____qualname____doc__REQUEST_GRANTEDREQUEST_REJECTED_OR_FAILEDCONNECTION_FAILEDAUTHENTICATION_FAILED     U/home/ubuntu/.hermes/hermes-agent/venv/lib/python3.11/site-packages/socksio/socks4.pyr   r      s-        ,,O!(#r   r   c                       e Zd ZdZdZdZdS )SOCKS4Commandz$Enumeration of SOCKS4 command codes.      N)r   r   r   r   CONNECTBINDr   r   r   r   r      s        ..GDDDr   r   c            
           e Zd ZU dZeed<   eed<   eed<   dZe	j
        e         ed<   e	 ddede	j        ee	j        eef         f         de	j
        e         dd fd	            Zdde	j
        e         defd
ZdS )SOCKS4Requestaj  Encapsulates a request to the SOCKS4 proxy server

    Args:
        command: The command to request.
        port: The port number to connect to on the target host.
        addr: IP address of the target host.
        user_id: Optional user ID to be included in the request, if not supplied
            the user *must* provide one in the packing operation.
    commandportaddrNuser_idaddressreturnc                     t          |          \  }}t          |          \  }}|t          j        k    rt	          d           | ||||          S )a  Convenience class method to build an instance from command and address.

        Args:
            command: The command to request.
            address: A string in the form 'HOST:PORT' or a tuple of ip address string
                and port number.
            user_id: Optional user ID.

        Returns:
            A SOCKS4Request instance.

        Raises:
            SOCKSError: If a domain name or IPv6 address was supplied.
        z;IPv6 addresses and domain names are not supported by SOCKS4r$   r&   r%   r'   )r
   r	   r   IPV4r   clsr$   r(   r'   r%   atypeencoded_addrs          r   from_addresszSOCKS4Request.from_address.   se    * <GDD,W55|K$$$M   s7D'RRRRr   c                     |p| j         }|t          d          d                    d| j        | j                            dd          | j        |dg          S )	  Packs the instance into a raw binary in the appropriate form.

        Args:
            user_id: Optional user ID as an override, if not provided the instance's
                will be used, if none was provided at initialization an error is raised.

        Returns:
            The packed request.

        Raises:
            SOCKSError: If no user was specified in this call or on initialization.
        N-SOCKS4 requires a user_id, none was specifiedr         big	byteorder    r'   r   joinr$   r%   to_bytesr&   selfr'   s     r   dumpszSOCKS4Request.dumpsK   sj     )T\?LMMMxx$$Q%$88		
 	
 		
r   Nr   r   r   r   r   __annotations__intbytesr'   typingOptionalclassmethodUnionr   Tupler1   r@   r   r   r   r#   r#      s           
III
KKK&*GV_U#***
 +/	S SS j&,z3*GGHS '	S
 
S S S [S8
 
V_U3 
u 
 
 
 
 
 
r   r#   c            
           e Zd ZU dZeed<   eed<   eed<   dZe	j
        e         ed<   e	 ddede	j        ee	j        eef         f         de	j
        e         dd fd	            Zdde	j
        e         defd
ZdS )SOCKS4ARequestak  Encapsulates a request to the SOCKS4A proxy server

    Args:
        command: The command to request.
        port: The port number to connect to on the target host.
        addr: IP address of the target host.
        user_id: Optional user ID to be included in the request, if not supplied
            the user *must* provide one in the packing operation.
    r$   r%   r&   Nr'   r(   r)   c                 h    t          |          \  }}t          |          \  }} | ||||          S )ac  Convenience class method to build an instance from command and address.

        Args:
            command: The command to request.
            address: A string in the form 'HOST:PORT' or a tuple of ip address string
                and port number.
            user_id: Optional user ID.

        Returns:
            A SOCKS4ARequest instance.
        r+   )r
   r	   r-   s          r   r1   zSOCKS4ARequest.from_addressx   s@    $ <GDD,W55|s7D'RRRRr   c           
          |p| j         }|t          d          d                    d| j        | j                            dd          d|d	| j        d	g          S )
r3   Nr4   r   r5   r6   r7   r8   s      r:   r;   r>   s     r   r@   zSOCKS4ARequest.dumps   sp     )T\?LMMMxx$$Q%$88#		
 
 	
r   rA   rB   r   r   r   rL   rL   h   s           
III
KKK&*GV_U#***
 +/	S SS j&,z3*GGHS '	S
 
S S S [S*
 
V_U3 
u 
 
 
 
 
 
r   rL   c                   h    e Zd ZU dZeed<   eed<   ej        e	         ed<   e
dedd fd            ZdS )	SOCKS4ReplyzEncapsulates a reply from the SOCKS4 proxy server

    Args:
        reply_code: The code representing the type of reply.
        port: The port number returned.
        addr: Optional IP address returned.
    
reply_coder%   r&   datar)   c           
      r   t          |          dk    s|dd         dk    rt          d          	  | t          |dd                   t                              |dd         d	          t          t          j        |dd                   
          S # t          $ r}t          d          |d}~ww xY w)zUnpacks the reply data into an instance.

        Returns:
            The unpacked reply instance.

        Raises:
            ProtocolError: If the data does not match the spec.
           r   r   r:   zMalformed replyr6      r7   r8   )rQ   r%   r&   N)	lenr   r   rD   
from_bytesr   r   r,   
ValueError)r.   rR   excs      r   loadszSOCKS4Reply.loads   s     t99>>T!A#Y'11 1222	<3*4!955^^D1I^??#K$4d1Q3i@@   
  	< 	< 	< 122;	<s   A$B 
B6!B11B6N)r   r   r   r   r   rC   rD   rF   rG   strrH   rE   rZ   r   r   r   rP   rP      sv            
III
/#
< <= < < < [< < <r   rP   c                   d    e Zd ZdZdefdZdej        ee	f         ddfdZ
dedefd	Zdefd
ZdS )SOCKS4ConnectionzEncapsulates a SOCKS4 and SOCKS4A connection.

    Packs request objects into data suitable to be send and unpacks reply
    data into their appropriate reply objects.

    Args:
        user_id: The user ID to be sent as part of the requests.
    r'   c                 `    || _         t                      | _        t                      | _        d S rA   )r'   	bytearray_data_to_send_received_datar>   s     r   __init__zSOCKS4Connection.__init__   s'    &[['kkr   requestr)   Nc                 j    |j         p| j         }| xj        |                    |          z  c_        dS )zPacks a request object and adds it to the send data buffer.

        Args:
            request: The request instance to be packed.
        )r'   N)r'   r`   r@   )r?   rc   r'   s      r   sendzSOCKS4Connection.send   s:     /1T\gmmGm<<<r   rR   c                 z    | xj         |z  c_         t                              t          | j                             S )zUnpacks response data into a reply object.

        Args:
            data: The raw response data from the proxy server.

        Returns:
            The appropriate reply object.
        )ra   rP   rZ   rE   r?   rR   s     r   receive_datazSOCKS4Connection.receive_data   s7     	t#  t':!;!;<<<r   c                 T    t          | j                  }t                      | _        |S )zqReturns the data to be sent via the I/O library of choice.

        Also clears the connection's buffer.
        )rE   r`   r_   rg   s     r   data_to_sendzSOCKS4Connection.data_to_send   s%    
 T'((&[[r   )r   r   r   r   rE   rb   rF   rI   r#   rL   re   rP   rh   rj   r   r   r   r]   r]      s         * * * * *=FL)FG =D = = = =
= 
=; 
= 
= 
= 
=e      r   r]   )enumrF   _typesr   
exceptionsr   r   utilsr   r   r	   r
   rE   Enumr   r   
NamedTupler#   rL   rP   r]   r   r   r   <module>rq      s           1 1 1 1 1 1 1 1           $ $ $ $ $eTY $ $ $    E49   G
 G
 G
 G
 G
F% G
 G
 G
TB
 B
 B
 B
 B
V& B
 B
 B
J!< !< !< !< !<&# !< !< !<H, , , , , , , , , ,r   