U
    Vh                     @   s   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 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ZdS )    N   )
StrOrBytes)ProtocolError
SOCKSError)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 )SOCKS4ReplyCodez"Enumeration of SOCKS4 reply codes.   Z   [   \   ]N)__name__
__module____qualname____doc__REQUEST_GRANTEDREQUEST_REJECTED_OR_FAILEDCONNECTION_FAILEDAUTHENTICATION_FAILED r   r   P/var/www/html/arya_register/venvv2/lib/python3.8/site-packages/socksio/socks4.pyr
      s
   r
   c                   @   s   e Zd ZdZdZdZdS )SOCKS4Commandz$Enumeration of SOCKS4 command codes.      N)r   r   r   r   CONNECTBINDr   r   r   r   r      s   r   c                   @   s   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ee	jee	jeef f e	j
e d ddd	Zde	j
e ed
ddZdS )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r   addressr"   returnc                 C   s:   t |\}}t|\}}|tjkr*td| ||||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    ZatypeZencoded_addrr   r   r   from_address.   s    
zSOCKS4Request.from_addressr"   r%   c                 C   s@   |p| j }|dkrtddd| j| jjd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.
        N-SOCKS4 requires a user_id, none was specified          big	byteorder    r"   r   joinr   r    to_bytesr!   selfr"   r   r   r   dumpsK   s    
zSOCKS4Request.dumps)N)Nr   r   r   r   r   __annotations__intbytesr"   typingOptionalclassmethodUnionr   Tupler*   r:   r   r   r   r   r      s   

 r   c                   @   s   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ee	jee	jeef f e	j
e d ddd	Zde	j
e ed
ddZdS )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#   c                 C   s(   t |\}}t|\}}| ||||d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(   r   r   r   r*   x   s    zSOCKS4ARequest.from_addressr+   c              
   C   sD   |p| j }|dkrtddd| j| jjdddd|d	| jd	gS )
r,   Nr-   r.   r/   r0   r1   r2   s      r4   r5   r8   r   r   r   r:      s    
zSOCKS4ARequest.dumps)N)Nr;   r   r   r   r   rD   h   s   

 rD   c                   @   sD   e Zd ZU dZeed< eed< eje	 ed< e
ed dddZdS )	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              
   C   s   t |dks|dd dkr$tdz>| t|dd tj|dd dd	ttj|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   r4   zMalformed replyr0      r1   r2   )rF   r    r!   N)	lenr   r
   r=   
from_bytesr   r   r'   
ValueError)r)   rH   excr   r   r   loads   s    
zSOCKS4Reply.loadsN)r   r   r   r   r
   r<   r=   r?   r@   strrA   r>   rO   r   r   r   r   rE      s   
rE   c                   @   sV   e Zd ZdZedddZejee	f ddddZ
eed	d
dZedddZdS )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                 C   s   || _ t | _t | _d S )N)r"   	bytearray_data_to_send_received_datar8   r   r   r   __init__   s    zSOCKS4Connection.__init__N)requestr%   c                 C   s&   |j p
| j }|  j|j|d7  _dS )zPacks a request object and adds it to the send data buffer.

        Args:
            request: The request instance to be packed.
        rR   N)r"   rT   r:   )r9   rW   r"   r   r   r   send   s    zSOCKS4Connection.sendrG   c                 C   s   |  j |7  _ t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.
        )rU   rE   rO   r>   r9   rH   r   r   r   receive_data   s    	zSOCKS4Connection.receive_data)r%   c                 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.
        )r>   rT   rS   rY   r   r   r   data_to_send   s    
zSOCKS4Connection.data_to_send)r   r   r   r   r>   rV   r?   rB   r   rD   rX   rE   rZ   r[   r   r   r   r   rQ      s
   		rQ   )enumr?   _typesr   
exceptionsr   r   utilsr   r   r   r	   r>   Enumr
   r   
NamedTupler   rD   rE   rQ   r   r   r   r   <module>   s   	JE$