a
    lJhn                     @   sR  d Z ddgZddlZddlZddlZz"ddlZejejejej	dZ
W n ey\   dZY n0 ddlmZmZ ddlmZ ddlmZmZmZmZmZ d	d
lmZmZmZmZmZmZ d	dlmZm Z m!Z!m"Z" ddl#m$Z$m%Z% e&e'dddZ(G dd deZ)G dd de)Z*G dd de*Z+G dd deZ,G dd de,Z-G dd de,Z.dS )zFModule implementing low-level socket communication with MySQL servers.MySQLTcpSocketMySQLUnixSocket    N)TLSv1TLSv1.1TLSv1.2TLSv1.3)ABCabstractmethod)deque)AnyDequeListOptionalTuple   )InterfaceErrorNotSupportedErrorOperationalErrorProgrammingErrorReadTimeoutErrorWriteTimeoutError)COMPRESSED_PACKET_HEADER_LENGTHMAX_PAYLOAD_LENGTHMIN_COMPRESS_LENGTHPACKET_HEADER_LENGTH   )StreamWriteropen_connection)errreturnc                 C   s    | j st| S | j  d| j S )z`Reformat the IOError error message.

    This function reformats the IOError error message.
     )errnostrstrerror)r    r$   M/var/www/shaz/venv/lib/python3.9/site-packages/mysql/connector/aio/network.py_strioerrorI   s    r&   c                
   @   s\   e Zd ZdZed	eeeee	 ee	 ee	 ddddZ
ed
ejeee	 edddZdS )NetworkBrokeraP  Broker class interface.

    The network object is a broker used as a delegate by a socket object. Whenever the
    socket wants to deliver or get packets to or from the MySQL server it needs to rely
    on its network broker (netbroker).

    The netbroker sends `payloads` and receives `packets`.

    A packet is a bytes sequence, it has a header and body (referred to as payload).
    The first `PACKET_HEADER_LENGTH` or `COMPRESSED_PACKET_HEADER_LENGTH`
    (as appropriate) bytes correspond to the `header`, the remaining ones represent the
    `payload`.

    The maximum payload length allowed to be sent per packet to the server is
    `MAX_PAYLOAD_LENGTH`. When  `send` is called with a payload whose length is greater
    than `MAX_PAYLOAD_LENGTH` the netbroker breaks it down into packets, so the caller
    of `send` can provide payloads of arbitrary length.

    Finally, data received by the netbroker comes directly from the server, expect to
    get a packet for each call to `recv`. The received packet contains a header and
    payload, the latter respecting `MAX_PAYLOAD_LENGTH`.
    Nwriteraddresspayloadpacket_numbercompressed_packet_numberwrite_timeoutr   c                    s   dS )a  Send `payload` to the MySQL server.

        If provided a payload whose length is greater than `MAX_PAYLOAD_LENGTH`, it is
        broken down into packets.

        Args:
            sock: Object holding the socket connection.
            address: Socket's location.
            payload: Packet's body to send.
            packet_number: Sequence id (packet ID) to attach to the header when sending
                           plain packets.
            compressed_packet_number: Same as `packet_number` but used when sending
                                      compressed packets.
            write_timeout: Timeout in seconds before which sending a packet to the server
                           should finish else WriteTimeoutError is raised.


        Raises:
            :class:`OperationalError`: If something goes wrong while sending packets to
                                       the MySQL server.
        Nr$   )selfr)   r*   r+   r,   r-   r.   r$   r$   r%   writei   s    
zNetworkBroker.writereaderr*   read_timeoutr   c                    s   dS )aM  Get the next available packet from the MySQL server.

        Args:
            sock: Object holding the socket connection.
            address: Socket's location.
            read_timeout: Timeout in seconds before which reading a packet from the server
                          should finish.

        Returns:
            packet: A packet from the MySQL server.

        Raises:
            :class:`OperationalError`: If something goes wrong while receiving packets
                                       from the MySQL server.
            :class:`ReadTimeoutError`: If the time to receive a packet from the server takes
                                       longer than `read_timeout`.
            :class:`InterfaceError`: If something goes wrong while receiving packets
                                     from the MySQL server.
        Nr$   )r/   r2   r*   r3   r$   r$   r%   read   s    zNetworkBroker.read)NNN)N)__name__
__module____qualname____doc__r	   r   r"   bytesr   intr0   asyncioStreamReader	bytearrayr4   r$   r$   r$   r%   r'   Q   s,       r'   c                	   @   s   e Zd ZdZddddZeeeeef dddZ	de
e dd	d
dZeeeddddZdejee
e edddZdeeee
e e
e e
e ddddZdejee
e edddZdS )NetworkBrokerPlain,Broker class for MySQL socket communication.Nr   c                 C   s
   d| _ d S N_pktnrr/   r$   r$   r%   __init__   s    zNetworkBrokerPlain.__init__pktr   c                 C   sB   t | tk rtdtd| dd d d | d  }}||fS )-Recover the header information from a packet.3Can't recover header info from an incomplete packet<Ir          )lenr   
ValueErrorstructunpack)rH   pllseqidr$   r$   r%   
get_header   s    zNetworkBrokerPlain.get_headernext_idr   c                 C   s0   |du r|  j d7  _ n|| _ |  j d;  _ dS z:Set the given packet id, if any, else increment packet id.Nr      rC   r/   rV   r$   r$   r%   _set_next_pktnr   s    z"NetworkBrokerPlain._set_next_pktnrr)   r*   rH   r   c              
      s   z| | | I dH  W nh tyV } z"td|t|fd|W Y d}~n8d}~0  ty } ztdd|W Y d}~n
d}~0 0 dS )z!Write packet to the comm channel.N  r!   valuesi  r!   )r0   ZdrainIOErrorr   r&   AttributeError)r/   r)   r*   rH   r   r$   r$   r%   
_write_pkt   s    
zNetworkBrokerPlain._write_pktr   )r2   sizer3   r   c              
      s   zRt d}t||k rNt||t| |I dH }|sDtdd||7 }q
|W S  tjtjfy } ztdd|W Y d}~n
d}~0 0 dS )z(Read `size` bytes from the comm channel.    Ni  r_     )	r=   rN   r;   wait_forr4   r   CancelledErrorTimeoutErrorr   )r/   r2   rc   r3   rH   chunkr   r$   r$   r%   _read_chunk   s    


zNetworkBrokerPlain._read_chunkr(   c           
         s   |  | d}ztt|t D ]N}t| ||dtd| j	 |||t   |I dH  |    |t7 }q t| ||tdt|| dd td| j	 ||d  |I dH  W n8 tj
tjfy }	 ztdd|	W Y d}	~	n
d}	~	0 0 dS )	zSend payload to the MySQL server.

        If provided a payload whose length is greater than `MAX_PAYLOAD_LENGTH`, it is
        broken down into packets.
        r      <BNrK   rL   re   r_   )rZ   rangerN   r   r;   rf   rb   rP   packrD   rg   rh   r   )
r/   r)   r*   r+   r,   r-   r.   offset_r   r$   r$   r%   r0      s@    





zNetworkBrokerPlain.writer1   c              
      s~   z>|  |t|I dH }| |\}| _||  |||I dH  W S  tyx } z"td|t|fd|W Y d}~n
d}~0 0 dS )z+Receive `one` packet from the MySQL server.Nr\   r]   )rj   r   rT   rD   r`   r   r&   )r/   r2   r*   r3   headerZpayload_lenr   r$   r$   r%   r4     s    zNetworkBrokerPlain.read)N)r   N)NNN)N)r5   r6   r7   r8   rF   staticmethodr9   r   r:   rT   r   rZ   r   r"   rb   r;   r<   r=   rj   r0   r4   r$   r$   r$   r%   r>      sJ   
     3 r>   c                	       s   e Zd ZdZdd fddZeeeee dddZ	eee
eeef d	d
dZdee ddddZeeedd fddZdeeeee ee ee dd fddZdejeee dd fddZdejeee ed fddZ  ZS )NetworkBrokerCompressedr?   Nr@   c                    s   t    d| _t | _d S rA   )superrF   _compressed_pktnrr
   _queue_readrE   	__class__r$   r%   rF   -  s    
z NetworkBrokerCompressed.__init__)r+   pktnrr   c                 C   s   d}g }t t| t D ]>}|dtd| | ||t    |d d }|t7 }q|tdt| | dd td| | |d   |S )	z2Prepare a payload for sending to the MySQL server.r   rk   rl   r   rX   rK   rL   N)rm   rN   r   appendrP   rn   )r+   ry   ro   Zpktsrp   r$   r$   r%   _prepare_packets2  s(    



z(NetworkBrokerCompressed._prepare_packetsrG   c                 C   sb   t | tk rtdtd| dd d d | d td| dd d d   }}}|||fS )rI   rJ   rK   r   rL   rM         )rN   r   rO   rP   rQ   )rH   compressed_pllrS   uncompressed_pllr$   r$   r%   rT   K  s    
z"NetworkBrokerCompressed.get_headerrU   c                 C   s0   |du r|  j d7  _ n|| _ |  j d;  _ dS rW   )ru   rY   r$   r$   r%   _set_next_compressed_pktnrY  s    z2NetworkBrokerCompressed._set_next_compressed_pktnrr[   c                    sb   t |}tdt|dd td| j tdt|dd  | }t |||I dH S )z1Compress packet and write it to the comm channel.rK   r   rL   rl   N)zlibcompressrP   rn   rN   ru   rt   rb   )r/   r)   r*   rH   compressed_pktrw   r$   r%   rb   a  s    
z"NetworkBrokerCompressed._write_pktr(   c                    s|  |  | | | z&td| || j}t|tt krd}t	t|t D ]<}	t
| |||||t  |I dH  |   |t7 }qTt
| ||||d |I dH  nt|tkrt
| ||||I dH  nXt
t ||tdt|dd td| j tdddd  | |I dH  W n: t
jt
jfyv }
 ztdd|
W Y d}
~
n
d}
~
0 0 dS )	zSend `payload` as compressed packets to the MySQL server.

        If provided a payload whose length is greater than `MAX_PAYLOAD_LENGTH`, it is
        broken down into packets.
        rd   r   NrK   rL   rl   re   r_   )rZ   r   r=   joinr{   rD   rN   r   r   rm   r;   rf   rb   r   rt   rP   rn   ru   rg   rh   r   )r/   r)   r*   r+   r,   r-   r.   Zpayload_prepro   rp   r   rw   r$   r%   r0   q  sV    



	zNetworkBrokerCompressed.write)r2   r~   r3   r   c           
         s   t tt |||I dH }d}|t|k rtd|||t d  d d }t| t|| krt |t	|I dH }| 
|\}| _}t |||I dH }	||dkr|	nt|	7 }| j|||t |   |t| 7 }q$dS )z&Handle reading of a compressed packet.Nr   rK   r   rM   )r=   r   
decompressrt   rj   rN   rP   rQ   r   r   rT   ru   rv   rz   )
r/   r2   r~   r3   rH   ro   rR   rq   r   r   rw   r$   r%   _read_compressed_pkt  s>    
	
z,NetworkBrokerCompressed._read_compressed_pktr1   c           	   
      s   | j szht |t|I dH }| |\}| _}|dkrX| j t |||I dH  n| |||I dH  W n: ty } z"t	d|t
|fd|W Y d}~n
d}~0 0 | j sdS | j  }|d | _|S )z{Receive `one` or `several` packets from the MySQL server, enqueue them, and
        return the packet at the head.
        Nr   r\   r]   rL   )rv   rt   rj   r   rT   ru   rz   r   r`   r   r&   popleftrD   )	r/   r2   r*   r3   rq   r~   r   r   rH   rw   r$   r%   r4     s8    

	

zNetworkBrokerCompressed.read)N)NNN)N)N)r5   r6   r7   r8   rF   rr   r9   r:   r   r{   r   rT   r   r   r   r"   rb   r0   r;   r<   r   r=   r4   __classcell__r$   r$   rw   r%   rs   *  sL   
   M 3 rs   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
ddddZedddZeddddZddddZejddddZd!eee ee ee ddddZd"ee edddZdddddg g fee ee ee ee ee eee  eee  ejddd ZdS )#MySQLSocketzMySQL socket communication interface.

    Examples:
        Subclasses: network.MySQLTCPSocket and network.MySQLUnixSocket.
    Nr@   c                 C   s*   d| _ d| _d| _d| _t | _d| _dS )zsNetwork layer where transactions are made with plain (uncompressed) packets
        is enabled by default.
        NF)_reader_writer_connection_timeout_addressr>   
_netbroker_is_connectedrE   r$   r$   r%   rF      s    zMySQLSocket.__init__c                 C   s   | j S )zSocket location.)r   rE   r$   r$   r%   r*   +  s    zMySQLSocket.addresskwargsr   c                    s   dS )zOpen the socket.Nr$   r/   r   r$   r$   r%   r   0  s    zMySQLSocket.open_connectionc              
      sl   | j rbz6| j   | j jdur*| j j  | j  I dH  W n$ ty` } zW Y d}~n
d}~0 0 d| _dS )zClose the connection.NF)r   close	transportabortZwait_closed	Exceptionr   )r/   rp   r$   r$   r%   close_connection4  s    
zMySQLSocket.close_connectionc                 C   s   | j S )zCheck if the socket is connected.

        Return:
            bool: Returns `True` if the socket is connected to MySQL server.
        )r   rE   r$   r$   r%   is_connectedC  s    zMySQLSocket.is_connected)timeoutr   c                 C   s
   || _ dS )zSet the connection timeout.N)r   )r/   r   r$   r$   r%   set_connection_timeoutK  s    z"MySQLSocket.set_connection_timeoutc                 C   s   t  | _dS )zIEnable network layer where transactions are made with compressed packets.N)rs   r   rE   r$   r$   r%   switch_to_compressed_modeO  s    z%MySQLSocket.switch_to_compressed_mode)ssl_contextr   c                    sD   | j dusJ | j jd}|jdkr.td| j |I dH  dS )a?  Upgrade an existing stream-based connection to TLS.

        The `start_tls()` method from `asyncio.streams.StreamWriter` is only available
        in Python 3.11. This method is used as a workaround.

        The MySQL TLS negotiation happens in the middle of the TCP connection.
        Therefore, passing a socket to open connection will cause it to negotiate
        TLS on an existing connection.

        Args:
            ssl_context: The SSL Context to be used.

        Raises:
            RuntimeError: If the transport does not expose the socket instance.
        Nsocketr   z,SSL is not supported when using Unix sockets)r   r   Zget_extra_infofamilyr   Z	start_tls)r/   r   r   r$   r$   r%   switch_to_sslS  s
    
zMySQLSocket.switch_to_ssl)r+   r,   r-   r.   r   c                    s&   | j j| j| j||||dI dH  dS )z!Send packets to the MySQL server.)r,   r-   r.   N)r   r0   r   r*   )r/   r+   r,   r-   r.   r$   r$   r%   r0   l  s    zMySQLSocket.write)r3   r   c                    s   | j | j| j|I dH S )z#Read packets from the MySQL server.N)r   r4   r   r*   )r/   r3   r$   r$   r%   r4   }  s    zMySQLSocket.readF)ssl_cassl_certssl_keyssl_verify_certssl_verify_identitytls_versionstls_cipher_suitesr   c                 C   s  d}| j stddtdu r$tdz|r|jdd |d }t| }	t|	}
|dkrd	|vrr|
 jtjO  _d
|vr|
 jtj	O  _d|vr|
 jtj
O  _nt }
||
_|rtj|
_n|rtj|
_ntj|
_|
  |r0z|
| W n< ttjfy. } ztd| |W Y d}~n
d}~0 0 |rz|
|| W n< ttjfy } ztd| |W Y d}~n
d}~0 0 |r|d	kr|
d| |
W S  ty } ztd|W Y d}~nHd}~0  tttjtjfy } ztt||W Y d}~n
d}~0 0 dS )zBuild a SSLContext.Ni   r_   z&Python installation has no SSL supportT)reverser   r   r   r   r   zInvalid CA Certificate: zInvalid Certificate/Key: :)r   r   sslRuntimeErrorsortTLS_VERSIONS
SSLContextoptionsOP_NO_TLSv1_2OP_NO_TLSv1_1OP_NO_TLSv1create_default_contextcheck_hostnameCERT_REQUIREDverify_modeCERT_OPTIONAL	CERT_NONEload_default_certsload_verify_locationsr`   SSLErrorload_cert_chainset_ciphersr   	NameErrorr   NotImplementedErrorCertificateErrorr"   )r/   r   r   r   r   r   r   r   Ztls_versionZssl_protocolcontextr   r$   r$   r%   build_ssl_context  s`    



&&zMySQLSocket.build_ssl_context)NNN)N)r5   r6   r7   r8   rF   propertyr"   r*   r	   r   r   r   boolr   r:   r   r   r   r   r   r9   r   r0   r=   r4   r   r   r$   r$   r$   r%   r     sN      

r   c                       s<   e Zd ZdZdeeed fddZedd	d
dZ	  Z
S )r   zMySQL socket class using TCP/IP.

    Args:
        host: MySQL host name.
        port: MySQL port.
        force_ipv6: Force IPv6 usage.
    	127.0.0.1  F)hostport
force_ipv6c                    s0   t    || _|| _|| _| d| | _d S )Nr   )rt   rF   _host_portZ_force_ipv6r   )r/   r   r   r   rw   r$   r%   rF     s
    
zMySQLTcpSocket.__init__Nr   c                    s0   t f | j| jd|I dH \| _| _d| _dS )zOpen TCP/IP connection.)r   r   NT)r   r   r   r   r   r   r   r$   r$   r%   r     s    zMySQLTcpSocket.open_connection)r   r   F)r5   r6   r7   r8   r"   r:   r   rF   r   r   r   r$   r$   rw   r%   r     s   	 	c                       s8   e Zd ZdZd
ed fddZedddd	Z  ZS )r   zbMySQL socket class using UNIX sockets.

    Args:
        unix_socket: UNIX socket file path.
    /tmp/mysql.sock)unix_socketc                    s   t    || _d S )N)rt   rF   r   )r/   r   rw   r$   r%   rF     s    
zMySQLUnixSocket.__init__Nr   c                    s.   t jf d| ji|I dH \| _| _d| _dS )zOpen UNIX socket connection.pathNT)r;   Zopen_unix_connectionr   r   r   r   r   r$   r$   r%   r     s    
zMySQLUnixSocket.open_connection)r   )	r5   r6   r7   r8   r"   rF   r   r   r   r$   r$   rw   r%   r     s   )/r8   __all__r;   rP   r   r   PROTOCOL_TLSv1PROTOCOL_TLSv1_1PROTOCOL_TLSv1_2PROTOCOL_TLSr   ImportErrorabcr   r	   collectionsr
   typingr   r   r   r   r   errorsr   r   r   r   r   r   networkr   r   r   r   utilsr   r   r`   r"   r&   r'   r>   rs   r   r   r   r$   r$   r$   r%   <module>   s:   

 T  p 4