
    ^i+/                        S SK r S SKrSSKJr  SSKJr  SSKJr  SSKJ	r	J
r
JrJr   " S S\\ R                  5      r " S	 S
\\ R                  5      r " S S\\ R                  5      r " S S\\ R                  5      r " S S\R&                  5      r " S S\R&                  5      r " S S\R&                  5      r " S S\R&                  5      r " S S\R&                  5      r " S S\R&                  5      r " S S\R&                  5      r " S S\ R6                  5      r\R:                  \\4   r " S S 5      rg)!    N   )
StrOrBytes)singledispatchmethod)ProtocolError)AddressTypedecode_addressencode_address#get_address_port_tuple_from_addressc                   (    \ rS rSrSrSrSrSrSrSr	g)	SOCKS5AuthMethod   z-Enumeration of SOCKS5 authentication methods.              N)
__name__
__module____qualname____firstlineno____doc__NO_AUTH_REQUIREDGSSAPIUSERNAME_PASSWORDNO_ACCEPTABLE_METHODS__static_attributes__r       H/home/james-whalen/.local/lib/python3.13/site-packages/socksio/socks5.pyr   r      s    7F#r   r   c                   $    \ rS rSrSrSrSrSrSrg)SOCKS5Command   zEnumeration of SOCKS5 commands.r   r      r   N)	r   r   r   r   r   CONNECTBINDUDP_ASSOCIATEr   r   r   r   r    r       s    )GDMr   r    c                   @    \ rS rSrSrSrSrSr\S\	SS 4S j5       r
S	rg
)SOCKS5AType    z$Enumeration of SOCKS5 address types.r   r"      atypereturnc                     U[         R                  :X  a  [        R                  $ U[         R                  :X  a  [        R
                  $ U[         R                  :X  a  [        R                  $ [        U5      eN)	r   IPV4r'   IPV4_ADDRESSDNDOMAIN_NAMEIPV6IPV6_ADDRESS
ValueError)clsr*   s     r   
from_atypeSOCKS5AType.from_atype'   sY    K$$$+++knn$***k&&&+++r   r   N)r   r   r   r   r   r/   r1   r3   classmethodr   r6   r   r   r   r   r'   r'       s4    .LKL {  }    r   r'   c                   <    \ rS rSrSrSrSrSrSrSr	Sr
S	rS
rSrSrg)SOCKS5ReplyCode2   z"Enumeration of SOCKS5 reply codes.r   r   r   r"   r)               r   N)r   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   r   r:   r:   2   s9    ,I$(/%! K#!(r   r:   c                   L    \ rS rSr% Sr\R                  \   \S'   S\	4S jr
Srg)SOCKS5AuthMethodsRequest@   zEncapsulates a request to the proxy for available authentication methods.

Args:
    methods: A list of acceptable authentication methods.
methodsr+   c                     SR                  S[        U R                  5      R                  SSS9SR                  U R                  5      /5      $ )z=Packs the instance into a raw binary in the appropriate form.r   r<   r   big	byteorder)joinlenrL   to_bytesselfs    r   dumpsSOCKS5AuthMethodsRequest.dumpsI   sI     xxDLL!**1*>&
 	
r   r   N)r   r   r   r   r   typingListr   __annotations__bytesrV   r   r   r   r   rJ   rJ   @   s%     [[)**	
u 	
r   rJ   c                   @    \ rS rSr% Sr\\S'   \S\SS 4S j5       r	Sr
g)	SOCKS5AuthReplyU   zEncapsulates 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                     [        U5      S:w  a  [        S5      e U " [        USS 5      S9$ ! [         a  n[        S5      UeSnAff = f)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   )r_   N)rR   r   r   r4   )r5   r`   excs      r   loadsSOCKS5AuthReply.loadsa   sU     t9> 122	<.tAay9:: 	< 12;	<s   . 
A	AA	r   N)r   r   r   r   r   r   rZ   r8   r[   re   r   r   r   r   r]   r]   U   s0     < <#4 < <r   r]   c                   <    \ rS rSr% Sr\\S'   \\S'   S\4S jrSrg)	SOCKS5UsernamePasswordRequestt   zLEncapsulates a username/password authentication request to the proxy server.usernamepasswordr+   c           
          SR                  S[        U R                  5      R                  SSS9U R                  [        U R                  5      R                  SSS9U R                  /5      $ )`Packs the instance into a raw binary in the appropriate form.

Returns:
    The packed request.
r   r   r   rN   rO   )rQ   rR   rj   rS   rk   rT   s    r   rV   #SOCKS5UsernamePasswordRequest.dumpsz   sc     xxDMM"++A+?DMM"++A+?
 	
r   r   N)	r   r   r   r   r   r[   rZ   rV   r   r   r   r   rh   rh   t   s    VOO
u 
r   rh   c                   @    \ rS rSr% Sr\\S'   \S\SS 4S j5       r	Sr
g)	SOCKS5UsernamePasswordReply   zLEncapsulates a username/password authentication reply from the proxy server.successr`   r+   c                     U " US:H  S9$ )zrUnpacks the reply authentication data into an instance.

Returns:
    The unpacked authentication reply instance.
s    )rr   r   r5   r`   s     r   re   !SOCKS5UsernamePasswordReply.loads   s     4;.//r   r   N)r   r   r   r   r   boolrZ   r8   r[   re   r   r   r   r   rp   rp      s+    VM0 0#@ 0 0r   rp   c            	           \ rS rSr% Sr\\S'   \\S'   \\S'   \	\S'   \
S\S\R                  \\R                  \\	4   4   SS 4S	 j5       rS\4S
 jr\S\4S j5       rSrg)SOCKS5CommandRequest   zEncapsulates 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                 n    [        U5      u  p#[        U5      u  pEU " U[        R                  U5      UUS9$ )au  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.
)rz   r*   r{   r|   )r
   r	   r'   r6   )r5   rz   r}   r|   r*   encoded_addrs         r   from_address!SOCKS5CommandRequest.from_address   sA    & <GD,W5((/	
 	
r   c                     SR                  SU R                  SU R                  U R                  U R                  R                  SSS9/5      $ )rm   r   r<   r   rb   rN   rO   )rQ   rz   r*   packed_addrr|   rS   rT   s    r   rV   SOCKS5CommandRequest.dumps   sN     xx

  $$Q%$8	
 		
r   c                 z   U R                   [        R                  :X  a'  [        U R                  5      S:X  d   eU R                  $ U R                   [        R
                  :X  a'  [        U R                  5      S:X  d   eU R                  $ [        U R                  5      nUR                  SSS9U R                  -   $ )zGProperty returning the packed address in the correct form for its type.      r   rN   rO   )r*   r'   r/   rR   r{   r3   rS   )rU   lengths     r   r    SOCKS5CommandRequest.packed_addr   s     ::111tyy>Q&&&99ZZ;333tyy>R'''99^F??1?6BBr   r   N)r   r   r   r   r   r    rZ   r'   r[   intr8   rX   Unionr   Tupler   rV   propertyr   r   r   r   r   rx   rx      s     
K
I

 j&,,z3*GGH
 
 	
 
6
u 
" 
CU 
C 
Cr   rx   c                   ^    \ rS rSr% Sr\\S'   \\S'   \\S'   \	\S'   \
S\SS 4S	 j5       rS
rg)SOCKS5Reply   zEncapsulates 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*   r{   r|   r`   r+   c           
         USS S:w  a  [        S5      e [        USS 5      nU " [        USS 5      U[        [        R
                  " U5      USS 5      [        R                  USS	 S
S9S9$ ! [         a  n[        S5      UeS	nAff = f)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<   rc      r   rb   NrN   rO   )r   r*   r{   r|   )	r   r'   r:   r   r   from_socks5_atyper   
from_bytesr4   )r5   r`   r*   rd   s       r   re   SOCKS5Reply.loads   s     !9 122
	<Qq	*E*4!95#K$A$A%$H$qQS*U^^DI^?	   	< 12;	<s   AA/ /
B
9BB
r   N)r   r   r   r   r   r:   rZ   r'   strr   r8   r[   re   r   r   r   r   r   r      s?      
I
I< <= < <r   r   c                       \ rS rSr% Sr\\S'   \\S'   \\S'   \\S'   \\S'   \	\S'   \
S\S	S 4S
 j5       rS	\4S jrSrg)SOCKS5Datagrami  zPEncapsulates a SOCKS5 datagram for UDP connections.

Currently not implemented.
r*   r{   r|   r`   fragmentlast_fragmentr+   c                     [        5       er-   NotImplementedErrorrt   s     r   re   SOCKS5Datagram.loads  s    !##r   c                     [        5       er-   r   rT   s    r   rV   SOCKS5Datagram.dumps  s    !##r   r   N)r   r   r   r   r   r'   rZ   r[   r   rv   r8   re   rV   r   r   r   r   r   r     sV    
 
K
I
KM$ $#3 $ $$u $r   r   c                   4    \ rS rSrSrSrSrSrSrSr	Sr
S	rS
rg)SOCKS5Statei#  z&Enumeration of SOCKS5 protocol states.r   rb   r   r            r   N)r   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   r   #  s-    0L+,(&'#Jr   r   c                   D   \ rS rSrSrSS jr\S\4S j5       r\	S\
SS4S j5       r\R                  \5      S\SS4S	 j5       r\R                  \5      S\SS4S
 j5       r\R                  \5      S\SS4S j5       rS\S\R*                  \\\4   4S jrS\4S jrSrg)SOCKS5Connectioni2  zEncapsulates a SOCKS5 connection.

Packs request objects into data suitable to be send and unpacks reply
data into their appropriate reply objects.
r+   Nc                 j    [        5       U l        [        5       U l        [        R                  U l        g r-   )	bytearray_data_to_send_received_datar   r   _staterT   s    r   __init__SOCKS5Connection.__init__9  s#    &['k!66r   c                     U R                   $ )z*Returns the current state of the protocol.)r   rT   s    r   stateSOCKS5Connection.state>  s     {{r   requestc                     [        5       e)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   rU   r   s     r   sendSOCKS5Connection.sendC  s     "##r   c                 t    U =R                   UR                  5       -  sl         [        R                  U l        g r-   )r   rV   r   r   r   r   s     r   _auth_methodsSOCKS5Connection._auth_methodsN  s&    gmmo-!33r   c                     U R                   [        R                  :w  a  [        S5      e[        R                  U l         U =R
                  UR                  5       -  sl        g )Nz/Not currently waiting for username and password)r   r   r   r   r   r   rV   r   s     r   _auth_username_password(SOCKS5Connection._auth_username_passwordS  sD    ;;+JJJ QRR!AAgmmo-r   c                     U R                   [        R                  :  a  [        S5      eU =R                  UR                  5       -  sl        g )NzASOCKS5 connections must be authenticated before sending a request)r   r   r   r   r   rV   r   s     r   _commandSOCKS5Connection._commandZ  s=    ;;999S  	gmmo-r   r`   c                 2   U R                   [        R                  :X  a  [        R	                  U5      nUR
                  [        R                  :X  a  [        R                  U l         U$ UR
                  [        R                  :X  a  [        R                  U l         U$ U R                   [        R                  :X  aT  [        R	                  U5      nUR                  (       a  [        R                  U l         U$ [        R                  U l         U$ U R                   [        R                  :X  aa  [        R	                  U5      nUR                   ["        R$                  :X  a  [        R&                  U l         U$ [        R                  U l         U$ [)        5       e)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   r]   re   r_   r   r   r   r   r   r   rp   rr   r   r   r   r:   r@   r   r   )rU   r`   
auth_replyusername_password_replyreplys        r   receive_data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    [        U R                  5      n[        5       U l        U$ )zaReturns the data to be sent via the I/O library of choice.

Also clears the connection's buffer.
)r[   r   r   )rU   r`   s     r   data_to_sendSOCKS5Connection.data_to_send  s#    
 T''(&[r   )r   r   r   )r+   N)r   r   r   r   r   r   r   r   r   r   SOCKS5RequestTyper   registerrJ   r   rh   r   rx   r   r[   rX   r   r]   r   rp   r   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   ) enumrX   _typesr   compatr   
exceptionsr   utilsr   r   r	   r
   r[   Enumr   r    r'   r:   
NamedTuplerJ   r]   rh   rp   rx   r   r   IntEnumr   r   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   