
    i+/                     Z   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 j                  Zej        eef         Z G d d           ZdS )!    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dS )SOCKS5AuthMethodz-Enumeration of SOCKS5 authentication methods.             N)__name__
__module____qualname____doc__NO_AUTH_REQUIREDGSSAPIUSERNAME_PASSWORDNO_ACCEPTABLE_METHODS     U/home/ubuntu/.hermes/hermes-agent/venv/lib/python3.11/site-packages/socksio/socks5.pyr   r      s-        77F#r   r   c                       e Zd ZdZdZdZdZdS )SOCKS5CommandzEnumeration of SOCKS5 commands.r   r      N)r   r   r   r   CONNECTBINDUDP_ASSOCIATEr   r   r   r   r      s#        ))GDMMMr   r   c                   >    e Zd ZdZdZdZdZededd fd            Z	dS )	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'   sT    K$$$++kn$$**k&&&++r   N)
r   r   r   r   r*   r,   r.   classmethodr   r1   r   r   r   r#   r#       sU        ..LKL {  }       [     r   r#   c                   6    e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdS )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   sJ        ,,I$(/%! K#!(r   r4   c                   @    e Zd ZU dZej        e         ed<   defdZ	dS )SOCKS5AuthMethodsRequestzEncapsulates a request to the proxy for available authentication methods.

    Args:
        methods: A list of acceptable authentication methods.
    methodsr&   c                     d                     dt          | j                                      dd          d                     | 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   sS     xxDL!!**1*>>&&
 
 	
r   N)
r   r   r   r   typingListr   __annotations__bytesrN   r   r   r   rC   rC   @   sQ           [)****	
u 	
 	
 	
 	
 	
 	
r   rC   c                   >    e Zd ZU dZeed<   ededd fd            ZdS )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k    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   s{     t99>> 1222	<3.tAaCy99:::: 	< 	< 	< 122;	<s    A 
A$AA$N)	r   r   r   r   r   rQ   r2   rR   r[   r   r   r   rT   rT   U   s[           < <#4 < < < [< < <r   rT   c                   4    e Zd ZU dZeed<   eed<   defdZdS )SOCKS5UsernamePasswordRequestzLEncapsulates a username/password authentication request to the proxy server.usernamepasswordr&   c           	          d                     dt          | j                                      dd          | j        t          | 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   sl     xxDM""++A+??DM""++A+??
 
 	
r   N)r   r   r   r   rR   rQ   rN   r   r   r   r]   r]   t   sI         VVOOOOOO
u 
 
 
 
 
 
r   r]   c                   >    e Zd ZU dZeed<   ededd fd            ZdS )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     s4;.////r   N)	r   r   r   r   boolrQ   r2   rR   r[   r   r   r   rc   rc      sR         VVMMM0 0#@ 0 0 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dS )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                     t          |          \  }}t          |          \  }} | |t                              |          ||          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   sZ    & <GDD,W55|s((//	
 
 
 	
r   c                     d                     d| j        d| 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   sM     xx
 $$Q%$88	
 	
 		
r   c                 @   | 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                  }|                    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ty>>Q&&&&9Z;333ty>>R''''9^^F??1?66BBr   N)r   r   r   r   r   rQ   r#   rR   intr2   rO   Unionr   Tuplerp   rN   propertyrr   r   r   r   ri   ri      s           
KKK
III

 j&,z3*GGH
 
 	
 
 
 [
6
u 
 
 
 
" 
CU 
C 
C 
C X
C 
C 
Cr   ri   c                   \    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d	S )
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k    rt          d          	 t          |dd                   } | t          |dd                   |t          t	          j        |          |dd                   t                              |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2
	<QqS	**E3*4!955#K$A%$H$H$qQSt*UU^^DI^??	     	< 	< 	< 122;	<s   BB$ $
C.B>>CN)r   r   r   r   r4   rQ   r#   strrw   r2   rR   r[   r   r   r   r|   r|      sx            
III
III< <= < < < [< < <r   r|   c                   |    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dS )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     s          
 
KKK
III
KKKMMM$ $#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
d	S )
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0L+,(&'#JJJr   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
                    e          deddfd            Ze
                    e          deddfd	            Ze
                    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dS )SOCKS5ConnectionzEncapsulates a SOCKS5 connection.

    Packs request objects into data suitable to be send and unpacks reply
    data into their appropriate reply objects.
    r&   Nc                 t    t                      | _        t                      | _        t          j        | _        d S r(   )	bytearray_data_to_send_received_datar   r   _staterL   s    r   __init__zSOCKS5Connection.__init__9  s(    &[['kk!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                 l    | xj         |                                z  c_         t          j        | _        d S r(   )r   rN   r   r   r   r   s     r   _auth_methodszSOCKS5Connection._auth_methodsN  s,    gmmoo-!3r   c                     | j         t          j        k    rt          d          t          j        | _         | xj        |                                z  c_        d S )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  sM    ;+JJJ QRRR!Agmmoo-r   c                     | j         t          j        k     rt          d          | xj        |                                z  c_        d S )NzASOCKS5 connections must be authenticated before sending a request)r   r   r   r   r   rN   r   s     r   _commandzSOCKS5Connection._commandZ  sK    ;999S   	gmmoo-r   rV   c                    | j         t          j        k    rit                              |          }|j        t          j        k    rt          j        | _         n&|j        t          j	        k    rt          j
        | _         |S | j         t          j        k    rFt                              |          }|j        rt          j
        | _         nt          j        | _         |S | j         t          j
        k    rTt                              |          }|j        t"          j        k    rt          j        | _         n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  s    ;+777(..t44J $4$FFF)N"&6&GGG)>;+EEE&A&G&G&M&M#&. 5)>)4**;+:::%%d++E?#<<<)6)4L!###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.
        )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 7 7 7
 {    X $- $$ $ $ $ $ 
]]+,,4%= 4$ 4 4 4 -,4 
]]011./L .QU . . . 21. 
]]'((. 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           ( ( ( ( ( ( % % % % % %           $ $ $ $ $udi $ $ $    E49            %      $) ) ) ) )eTY ) ) )
 
 
 
 
v0 
 
 
*< < < < <f' < < <>
 
 
 
 
F$5 
 
 
.0 0 0 0 0&"3 0 0 0GC GC GC GC GC6, GC GC GCT&< &< &< &< &<&# &< &< &<R$ $ $ $ $V& $ $ $,	 	 	 	 	$, 	 	 	 L!9;O!OP ] ] ] ] ] ] ] ] ] ]r   