
    .Kf+/                        d dl Z d dlZddlmZ ddlmZ ddl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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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 j6                        Zej:                  eef   Z G d d       Zy)!    N   )
StrOrBytes)singledispatchmethod)ProtocolError)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)SOCKS5AuthMethodz-Enumeration of SOCKS5 authentication methods.             N)__name__
__module____qualname____doc__NO_AUTH_REQUIREDGSSAPIUSERNAME_PASSWORDNO_ACCEPTABLE_METHODS     I/var/www/html/Backend/venv/lib/python3.12/site-packages/socksio/socks5.pyr   r      s    7F#r   r   c                       e Zd ZdZdZdZdZy)SOCKS5CommandzEnumeration of SOCKS5 commands.r   r      N)r   r   r   r   CONNECTBINDUDP_ASSOCIATEr   r   r   r   r      s    )GDMr   r   c                   6    e Zd ZdZdZdZdZededd fd       Z	y)	SOCKS5ATypez$Enumeration of SOCKS5 address types.r   r      atypereturnc                     |t         j                  k(  rt        j                  S |t         j                  k(  rt        j
                  S |t         j                  k(  rt        j                  S t        |      N)	r   IPV4r#   IPV4_ADDRESSDNDOMAIN_NAMEIPV6IPV6_ADDRESS
ValueError)clsr%   s     r   
from_atypezSOCKS5AType.from_atype'   sY    K$$$+++knn$***k&&&+++r   N)
r   r   r   r   r*   r,   r.   classmethodr   r1   r   r   r   r#   r#       s4    .LKL {  }    r   r#   c                   4    e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
Zy)SOCKS5ReplyCodez"Enumeration of SOCKS5 reply codes.r   r   r   r   r$               N)r   r   r   r   	SUCCEEDEDGENERAL_SERVER_FAILURE!CONNECTION_NOT_ALLOWED_BY_RULESETNETWORK_UNREACHABLEHOST_UNREACHABLECONNECTION_REFUSEDTTL_EXPIREDCOMMAND_NOT_SUPPORTEDADDRESS_TYPE_NOT_SUPPORTEDr   r   r   r4   r4   2   s9    ,I$(/%! K#!(r   r4   c                   B    e Zd ZU dZej
                  e   ed<   defdZ	y)SOCKS5AuthMethodsRequestzEncapsulates a request to the proxy for available authentication methods.

    Args:
        methods: A list of acceptable authentication methods.
    methodsr&   c                     dj                  dt        | j                        j                  dd      dj                  | j                        g      S )z=Packs the instance into a raw binary in the appropriate form.r   r5   r   big	byteorder)joinlenrD   to_bytesselfs    r   dumpszSOCKS5AuthMethodsRequest.dumpsI   sI     xxDLL!**1*>&
 	
r   N)
r   r   r   r   typingListr   __annotations__bytesrN   r   r   r   rC   rC   @   s%     [[)**	
u 	
r   rC   c                   6    e Zd ZU dZeed<   ededd fd       Zy)SOCKS5AuthReplyzEncapsulates a reply from the proxy with the authentication method to be used.

    Args:
        method: The authentication method to be used.

    Raises:
        ProtocolError: If the data does not conform with the expected structure.
    methoddatar&   c                     t        |      dk7  rt        d      	  | t        |dd             S # t        $ r}t        d      |d}~ww xY w)zUnpacks the authentication reply data into an instance.

        Returns:
            The unpacked authentication reply instance.

        Raises:
            ProtocolError: If the data does not match the spec.
           Malformed replyr   )rU   N)rJ   r   r   r/   )r0   rV   excs      r   loadszSOCKS5AuthReply.loadsa   sU     t9> 122	<.tAay9:: 	< 12;	<s   0 	A
AA
N)	r   r   r   r   r   rQ   r2   rR   r[   r   r   r   rT   rT   U   s0     < <#4 < <r   rT   c                   2    e Zd ZU dZeed<   eed<   defdZy)SOCKS5UsernamePasswordRequestzLEncapsulates a username/password authentication request to the proxy server.usernamepasswordr&   c           	          dj                  dt        | j                        j                  dd      | j                  t        | j                        j                  dd      | j                  g      S )xPacks the instance into a raw binary in the appropriate form.

        Returns:
            The packed request.
        r   r   r   rF   rG   )rI   rJ   r^   rK   r_   rL   s    r   rN   z#SOCKS5UsernamePasswordRequest.dumpsz   sc     xxDMM"++A+?DMM"++A+?
 	
r   N)r   r   r   r   rR   rQ   rN   r   r   r   r]   r]   t   s    VOO
u 
r   r]   c                   6    e Zd ZU dZeed<   ededd fd       Zy)SOCKS5UsernamePasswordReplyzLEncapsulates a username/password authentication reply from the proxy server.successrV   r&   c                      | |dk(        S )zUnpacks the reply authentication data into an instance.

        Returns:
            The unpacked authentication reply instance.
        s    )rd   r   r0   rV   s     r   r[   z!SOCKS5UsernamePasswordReply.loads   s     4;.//r   N)	r   r   r   r   boolrQ   r2   rR   r[   r   r   r   rc   rc      s+    VM0 0#@ 0 0r   rc   c            	           e Zd ZU dZeed<   eed<   eed<   eed<   e	dede
j                  ee
j                  eef   f   dd fd       Zdefd	Zedefd
       Zy)SOCKS5CommandRequesta	  Encapsulates a command request to the proxy server.

    Args:
        command: The command to request.
        atype: The address type of the addr field.
        addr: Address of the target host.
        port: The port number to connect to on the target host.
    commandr%   addrportaddressr&   c                 x    t        |      \  }}t        |      \  }} | |t        j                  |      ||      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. The address type will be inferred.

        Returns:
            A SOCKS5CommandRequest instance.

        Raises:
            SOCKSError: If a domain name or IPv6 address was supplied.
        )rj   r%   rk   rl   )r
   r	   r#   r1   )r0   rj   rm   rl   r%   encoded_addrs         r   from_addressz!SOCKS5CommandRequest.from_address   sE    & <GD,W5|((/	
 	
r   c                     dj                  d| j                  d| j                  | j                  | j                  j                  dd      g      S )ra   r   r5   r   rX   rF   rG   )rI   rj   r%   packed_addrrl   rK   rL   s    r   rN   zSOCKS5CommandRequest.dumps   sN     xx

  $$Q%$8	
 		
r   c                 x   | j                   t        j                  k(  r&t        | j                        dk(  sJ | j                  S | j                   t        j
                  k(  r&t        | j                        dk(  sJ | j                  S t        | j                        }|j                  dd      | j                  z   S )zGProperty returning the packed address in the correct form for its type.      r   rF   rG   )r%   r#   r*   rJ   rk   r.   rK   )rM   lengths     r   rr   z SOCKS5CommandRequest.packed_addr   s     ::111tyy>Q&&&99ZZ;333tyy>R'''99^F??1?6BBr   N)r   r   r   r   r   rQ   r#   rR   intr2   rO   Unionr   Tuplerp   rN   propertyrr   r   r   r   ri   ri      s     
K
I

 j&,,z3*GGH
 
 	
 
6
u 
" 
CU 
C 
Cr   ri   c                   T    e Zd ZU dZeed<   eed<   eed<   eed<   e	de
dd fd       Zy	)
SOCKS5Replya  Encapsulates a reply from the SOCKS5 proxy server

    Args:
        reply_code: The code representing the type of reply.
        atype: The address type of the addr field.
        addr: Optional IP address returned.
        port: The port number returned.
    
reply_coder%   rk   rl   rV   r&   c           
          |dd dk7  rt        d      	 t        |dd       } | t        |dd       |t        t	        j
                  |      |dd       t        j                  |d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   r5   rY      rt   rX   NrF   rG   )r}   r%   rk   rl   )	r   r#   r4   r   r   from_socks5_atyperw   
from_bytesr/   )r0   rV   r%   rZ   s       r   r[   zSOCKS5Reply.loads   s     !9 122
	<Qq	*E*4!95#K$A$A%$H$qQS*U^^DI^?	   	< 12;	<s   AA3 3	B<BBN)r   r   r   r   r4   rQ   r#   strrw   r2   rR   r[   r   r   r   r|   r|      s?      
I
I< <= < <r   r|   c                   t    e Zd ZU dZeed<   eed<   eed<   eed<   eed<   eed<   e	dedd fd	       Z
defd
Zy)SOCKS5DatagramzXEncapsulates a SOCKS5 datagram for UDP connections.

    Currently not implemented.
    r%   rk   rl   rV   fragmentlast_fragmentr&   c                     t               r(   NotImplementedErrorrf   s     r   r[   zSOCKS5Datagram.loads  s    !##r   c                     t               r(   r   rL   s    r   rN   zSOCKS5Datagram.dumps  s    !##r   N)r   r   r   r   r#   rQ   rR   rw   rg   r2   r[   rN   r   r   r   r   r     sV    
 
K
I
KM$ $#3 $ $$u $r   r   c                   ,    e Zd ZdZdZdZdZdZdZdZ	dZ
y	)
SOCKS5Statez&Enumeration of SOCKS5 protocol states.r   rX   r   rt            N)r   r   r   r   CLIENT_AUTH_REQUIREDSERVER_AUTH_REPLYCLIENT_AUTHENTICATEDTUNNEL_READY$CLIENT_WAITING_FOR_USERNAME_PASSWORDSERVER_VERIFY_USERNAME_PASSWORD
MUST_CLOSEr   r   r   r   r   #  s-    0L+,(&'#Jr   r   c                   ,   e Zd ZdZddZedefd       Zede	ddfd       Z
e
j                  e      deddfd       Ze
j                  e      deddfd	       Ze
j                  e      deddfd
       Zdedej(                  eeef   fdZdefdZy)SOCKS5ConnectionzEncapsulates a SOCKS5 connection.

    Packs request objects into data suitable to be send and unpacks reply
    data into their appropriate reply objects.
    r&   Nc                 j    t               | _        t               | _        t        j                  | _        y r(   )	bytearray_data_to_send_received_datar   r   _staterL   s    r   __init__zSOCKS5Connection.__init__9  s#    &['k!66r   c                     | j                   S )z*Returns the current state of the protocol.)r   rL   s    r   statezSOCKS5Connection.state>  s     {{r   requestc                     t               )zPacks a request object and adds it to the send data buffer.

        Also progresses the protocol state of the connection.

        Args:
            request: The request instance to be packed.
        r   rM   r   s     r   sendzSOCKS5Connection.sendC  s     "##r   c                 t    | xj                   |j                         z  c_         t        j                  | _        y r(   )r   rN   r   r   r   r   s     r   _auth_methodszSOCKS5Connection._auth_methodsN  s&    gmmo-!33r   c                     | j                   t        j                  k7  rt        d      t        j                  | _         | xj
                  |j                         z  c_        y )Nz/Not currently waiting for username and password)r   r   r   r   r   r   rN   r   s     r   _auth_username_passwordz(SOCKS5Connection._auth_username_passwordS  sD    ;;+JJJ QRR!AAgmmo-r   c                     | j                   t        j                  k  rt        d      | xj                  |j                         z  c_        y )NzASOCKS5 connections must be authenticated before sending a request)r   r   r   r   r   rN   r   s     r   _commandzSOCKS5Connection._commandZ  s=    ;;999S  	gmmo-r   rV   c                    | j                   t        j                  k(  r}t        j	                  |      }|j
                  t        j                  k(  rt        j                  | _         |S |j
                  t        j                  k(  rt        j                  | _         |S | j                   t        j                  k(  rOt        j	                  |      }|j                  rt        j                  | _         |S t        j                  | _         |S | j                   t        j                  k(  r`t        j	                  |      }|j                   t"        j$                  k(  rt        j&                  | _         |S t        j                  | _         |S t)               )zUnpacks response data into a reply object.

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

        Returns:
            A reply instance corresponding to the connection state and reply data.
        )r   r   r   rT   r[   rU   r   r   r   r   r   r   rc   rd   r   r|   r}   r4   r9   r   r   )rM   rV   
auth_replyusername_password_replyreplys        r   receive_datazSOCKS5Connection.receive_datab  s7    ;;+777(..t4J  $4$F$FF)NN  ""&6&G&GG)>>;;+EEE&A&G&G&M#&..)>> +* *44**;;+:::%%d+E?#<#<<)66 L *44L!##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.
        )rR   r   r   )rM   rV   s     r   data_to_sendzSOCKS5Connection.data_to_send  s#    
 T''(&[r   )r&   N)r   r   r   r   r   rz   r   r   r   SOCKS5RequestTyper   registerrC   r   r]   r   ri   r   rR   rO   rx   rT   r|   rc   r   r   r   r   r   r   r   2  s   7
 {   $- $$ $ $ 
]]+,4%= 4$ 4 -4 
]]01./L .QU . 2. 
]]'(. 8 .T . ).$$$$	o{4OO	P$$Le r   r   ) enumrO   _typesr   compatr   
exceptionsr   utilsr   r   r	   r
   rR   Enumr   r   r#   r4   
NamedTuplerC   rT   r]   rc   ri   r|   r   IntEnumr   rx   r   r   r   r   r   <module>r      s      ( % $udii $E499  %  $)eTYY )
v00 
*<f'' <>
F$5$5 
.0&"3"3 0GC6,, GCT&<&## &<R$V&& $,	$,, 	 LL!9;O!OP ] ]r   