U
    Vh+/                     @   sL  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 dee jZG d	d
 d
ee jZG dd dee jZG dd dee jZG dd dejZG dd dejZG dd dejZG dd dejZG dd dejZG dd dejZG dd dejZG dd de jZejeef ZG dd  d ZdS )!    N   )
StrOrBytes)singledispatchmethod)ProtocolError)AddressTypedecode_addressencode_address#get_address_port_tuple_from_addressc                   @   s    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_PASSWORDZNO_ACCEPTABLE_METHODS r   r   P/var/www/html/arya_register/venvv2/lib/python3.8/site-packages/socksio/socks5.pyr
      s
   r
   c                   @   s   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   r   c                   @   s0   e Zd ZdZdZdZdZeed dddZ	dS )	SOCKS5ATypez$Enumeration of SOCKS5 address types.r   r      )atypereturnc                 C   s<   |t jkrtjS |t jkr tjS |t jkr0tjS t|d S N)	r   IPV4r   IPV4_ADDRESSDNDOMAIN_NAMEIPV6IPV6_ADDRESS
ValueError)clsr   r   r   r   
from_atype'   s    


zSOCKS5AType.from_atypeN)
r   r   r   r   r#   r%   r'   classmethodr   r*   r   r   r   r   r       s   r   c                   @   s4   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   	SUCCEEDEDZGENERAL_SERVER_FAILUREZ!CONNECTION_NOT_ALLOWED_BY_RULESETNETWORK_UNREACHABLEHOST_UNREACHABLECONNECTION_REFUSEDTTL_EXPIREDCOMMAND_NOT_SUPPORTEDADDRESS_TYPE_NOT_SUPPORTEDr   r   r   r   r,   2   s   r,   c                   @   s.   e Zd ZU dZeje ed< edddZ	dS )SOCKS5AuthMethodsRequestzEncapsulates a request to the proxy for available authentication methods.

    Args:
        methods: A list of acceptable authentication methods.
    methodsr    c                 C   s(   d dt| jjdddd | jgS )z=Packs the instance into a raw binary in the appropriate form.    r-   r   big	byteorder)joinlenr9   to_bytesselfr   r   r   dumpsI   s    
zSOCKS5AuthMethodsRequest.dumpsN)
r   r   r   r   typingListr
   __annotations__bytesrD   r   r   r   r   r8   @   s   
r8   c                   @   s.   e Zd ZU dZeed< eed dd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              
   C   s^   t |dkrtdz| t|dd dW S  tk
rX } ztd|W 5 d}~X Y nX dS )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   )rJ   N)r@   r   r
   r(   )r)   rL   excr   r   r   loadsa   s    
zSOCKS5AuthReply.loadsN)	r   r   r   r   r
   rG   r+   rH   rP   r   r   r   r   rI   U   s   
	rI   c                   @   s0   e Zd ZU dZeed< eed< edddZdS )SOCKS5UsernamePasswordRequestzLEncapsulates a username/password authentication request to the proxy server.usernamepasswordr:   c              	   C   s8   d dt| jjddd| jt| jjddd| jgS )xPacks the instance into a raw binary in the appropriate form.

        Returns:
            The packed request.
        r;   r   r   r<   r=   )r?   r@   rR   rA   rS   rB   r   r   r   rD   z   s    z#SOCKS5UsernamePasswordRequest.dumpsN)r   r   r   r   rH   rG   rD   r   r   r   r   rQ   t   s   
rQ   c                   @   s.   e Zd ZU dZeed< eed dddZdS )SOCKS5UsernamePasswordReplyzLEncapsulates a username/password authentication reply from the proxy server.successrK   c                 C   s   | |dkdS )zUnpacks the reply authentication data into an instance.

        Returns:
            The unpacked authentication reply instance.
        s    )rV   r   r)   rL   r   r   r   rP      s    z!SOCKS5UsernamePasswordReply.loadsN)	r   r   r   r   boolrG   r+   rH   rP   r   r   r   r   rU      s   
rU   c                   @   s|   e Zd ZU dZeed< eed< eed< eed< e	ee
jee
jeef f d dddZed	d
dZeed	d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)rZ   addressr    c                 C   s.   t |\}}t|\}}| |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. 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   r*   )r)   rZ   r]   r\   r   encoded_addrr   r   r   from_address   s    z!SOCKS5CommandRequest.from_addressr:   c                 C   s(   d d| jd| j| j| jjdddgS )rT   r;   r-   r   rM   r<   r=   )r?   rZ   r   packed_addrr\   rA   rB   r   r   r   rD      s    zSOCKS5CommandRequest.dumpsc                 C   sj   | j tjkr$t| jdkst| jS | j tjkrHt| jdksBt| jS t| j}|jddd| j S dS )zGProperty returning the packed address in the correct form for its type.      r   r<   r=   N)r   r   r#   r@   r[   AssertionErrorr'   rA   )rC   lengthr   r   r   r`      s    
z SOCKS5CommandRequest.packed_addrN)r   r   r   r   r   rG   r   rH   intr+   rE   Unionr   Tupler_   rD   propertyr`   r   r   r   r   rY      s   
	rY   c                   @   sF   e Zd ZU dZeed< eed< eed< eed< e	e
d dd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   r[   r\   rK   c              
   C   s   |dd dkrt dzTt|dd }| t|dd |tt||dd tj|dd	 d
ddW S  tk
r } zt d|W 5 d	}~X Y nX d	S )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-   rN      ra   rM   Nr<   r=   )rj   r   r[   r\   )	r   r   r,   r   r   from_socks5_atypere   
from_bytesr(   )r)   rL   r   rO   r   r   r   rP      s    
zSOCKS5Reply.loadsN)r   r   r   r   r,   rG   r   strre   r+   rH   rP   r   r   r   r   ri      s   
	ri   c                   @   sd   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	ed dd	d
Z
edddZdS )SOCKS5DatagramzXEncapsulates a SOCKS5 datagram for UDP connections.

    Currently not implemented.
    r   r[   r\   rL   fragmentZlast_fragmentrK   c                 C   s
   t  d S r!   NotImplementedErrorrW   r   r   r   rP     s    zSOCKS5Datagram.loadsr:   c                 C   s
   t  d S r!   rr   rB   r   r   r   rD     s    zSOCKS5Datagram.dumpsN)r   r   r   r   r   rG   rH   re   rX   r+   rP   rD   r   r   r   r   rp     s   
rp   c                   @   s,   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   rM   rk   ra            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   rt   #  s   rt   c                   @   s   e Zd ZdZddddZeedddZee	ddd	d
Z
e
eeddddZe
eeddddZe
eeddddZeejeeef dddZedd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.
    Nr:   c                 C   s   t  | _t  | _tj| _d S r!   )	bytearray_data_to_send_received_datart   rx   _staterB   r   r   r   __init__9  s    zSOCKS5Connection.__init__c                 C   s   | j S )z*Returns the current state of the protocol.)r   rB   r   r   r   state>  s    zSOCKS5Connection.state)requestr    c                 C   s
   t  dS )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.
        Nrr   rC   r   r   r   r   sendC  s    	zSOCKS5Connection.sendc                 C   s   |  j | 7  _ tj| _d S r!   )r   rD   rt   ry   r   r   r   r   r   _auth_methodsN  s    zSOCKS5Connection._auth_methodsc                 C   s2   | j tjkrtdtj| _ |  j| 7  _d S )Nz/Not currently waiting for username and password)r   rt   r|   r   r}   r   rD   r   r   r   r   _auth_username_passwordS  s    z(SOCKS5Connection._auth_username_passwordc                 C   s*   | j tjk rtd|  j| 7  _d S )NzASOCKS5 connections must be authenticated before sending a request)r   rt   rz   r   r   rD   r   r   r   r   _commandZ  s
    zSOCKS5Connection._commandrK   c                 C   s   | j tjkrDt|}|jtjkr,tj| _ n|jtj	kr@tj
| _ |S | j tjkrvt|}|jrjtj
| _ ntj| _ |S | j tj
krt|}|jtjkrtj| _ ntj| _ |S t dS )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.
        N)r   rt   ry   rI   rP   rJ   r
   r   r|   r   rz   r}   rU   rV   r~   ri   rj   r,   r1   r{   rs   )rC   rL   Z
auth_replyZusername_password_replyreplyr   r   r   receive_datab  s(    





zSOCKS5Connection.receive_datac                 C   s   t | j}t | _|S )zqReturns the data to be sent via the I/O library of choice.

        Also clears the connection's buffer.
        )rH   r   r   )rC   rL   r   r   r   data_to_send  s    
zSOCKS5Connection.data_to_send)r   r   r   r   r   rh   rt   r   r   SOCKS5RequestTyper   registerr8   r   rQ   r   rY   r   rH   rE   rf   rI   ri   rU   r   r   r   r   r   r   r   2  s    
&r   ) enumrE   _typesr   compatr   
exceptionsr   utilsr   r   r   r	   rH   Enumr
   r   r   r,   
NamedTupler8   rI   rQ   rU   rY   ri   rp   IntEnumrt   rf   r   r   r   r   r   r   <module>   s&   	J)