
    .Kf                     2   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y)    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y)SOCKS4ReplyCodez"Enumeration of SOCKS4 reply codes.   Z   [   \   ]N)__name__
__module____qualname____doc__REQUEST_GRANTEDREQUEST_REJECTED_OR_FAILEDCONNECTION_FAILEDAUTHENTICATION_FAILED     I/var/www/html/Backend/venv/lib/python3.12/site-packages/socksio/socks4.pyr   r      s    ,O!(#r   r   c                       e Zd ZdZdZdZy)SOCKS4Commandz$Enumeration of SOCKS4 command codes.      N)r   r   r   r   CONNECTBINDr   r   r   r   r      s    .GDr   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y)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                  k7  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.   sR    * <GD,W5|K$$$M  7D'RRr   c                     |xs | j                   }|t        d      dj                  d| j                  | 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.
        -SOCKS4 requires a user_id, none was specifiedr         big	byteorder    r'   r   joinr$   r%   to_bytesr&   selfr'   s     r   dumpszSOCKS4Request.dumpsK   sg     )T\\?LMMxx$$Q%$8			
 		
r   Nr   r   r   r   r   __annotations__intbytesr'   typingOptionalclassmethodUnionr   Tupler1   r@   r   r   r   r#   r#      s     
I
K&*GV__U#*
 +/	SS j&,,z3*GGH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y)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                 R    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   s2    $ <GD,W5|7D'RRr   c           
          |xs | j                   }|t        d      dj                  d| j                  | j                  j                  dd      d|d| j                  dg      S )	r3   r4   r   r5   r6   r7   r8   s      r:   r;   r>   s     r   r@   zSOCKS4ARequest.dumps   sm     )T\\?LMMxx$$Q%$8#			
 	
r   rA   rB   r   r   r   rL   rL   h   s     
I
K&*GV__U#*
 +/	SS j&,,z3*GGHS '	S
 
S S*
V__U3 
u 
r   rL   c                   d    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y)	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           
         t        |      dk7  s|dd dk7  rt        d      	  | t        |dd       t        j	                  |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     t9>T!AY'1 122	<*4!95^^D1I^?#K$4$4d1Qi@ 
  	< 12;	<s   A	A- -	B6BBN)r   r   r   r   r   rC   rD   rF   rG   strrH   rE   rZ   r   r   r   rP   rP      sC      
I
//#
< <= < <r   rP   c                   f    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y)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                 N    || _         t               | _        t               | _        y rA   )r'   	bytearray_data_to_send_received_datar>   s     r   __init__zSOCKS4Connection.__init__   s    &['kr   requestr)   Nc                     |j                   xs | j                   }| xj                  |j                  |      z  c_        y)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   s2     //1T\\gmmGm<<r   rR   c                 |    | xj                   |z  c_         t        j                  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   s1     	t#  t':':!;<<r   c                 N    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]      sS    * *=FLL)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 $eTYY $E499 G
F%% G
TB
V&& B
J!<&## !<H, ,r   