
    i`a                        d Z ddgZddlZddlZddlZ	 ddlZej        ej        ej        ej	        dZ
n# e$ r dZY nw xY w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 d	dlmZmZmZm Z  ddl!m"Z"m#Z# de$de%fdZ& G d de          Z' G d de'          Z( G d de(          Z) G d de          Z* G d de*          Z+ G 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)COMPRESSED_PACKET_HEADER_LENGTHMAX_PAYLOAD_LENGTHMIN_COMPRESS_LENGTHPACKET_HEADER_LENGTH   )StreamWriteropen_connectionerrreturnc                 P    | j         st          |           n| j          d| j         S )z`Reformat the IOError error message.

    This function reformats the IOError error message.
     )errnostrstrerror)r   s    U/var/www/cyber-strat/venv/lib/python3.11/site-packages/mysql/connector/aio/network.py_strioerrorr%   G   s-    
 9G3s888SY*G*G*G*GG    c                       e Zd ZdZe	 	 ddedededee	         dee	         ddfd	            Z
ed
ej        dedef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_numberr   c                 
   K   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.

        Raises:
            :class:`OperationalError`: If something goes wrong while sending packets to
                                       the MySQL server.
        N )selfr)   r*   r+   r,   r-   s         r$   writezNetworkBroker.writeg   
        r&   readerc                 
   K   dS )a)  Get the next available packet from the MySQL server.

        Args:
            sock: Object holding the socket connection.
            address: Socket's location.

        Returns:
            packet: A packet from the MySQL server.

        Raises:
            :class:`OperationalError`: If something goes wrong while receiving packets
                                       from the MySQL server.
            :class:`InterfaceError`: If something goes wrong while receiving packets
                                     from the MySQL server.
        Nr/   )r0   r3   r*   s      r$   readzNetworkBroker.read   r2   r&   NN)__name__
__module____qualname____doc__r
   r   r"   bytesr   intr1   asyncioStreamReader	bytearrayr5   r/   r&   r$   r(   r(   O   s         .  (,26   	
  } #+3- 
   ^6 !5  	    ^  r&   r(   c                       e Zd ZdZddZededeeef         fd            Z	dde
e         ddfdZd	ed
ededdfdZ	 ddej        dedefdZ	 	 dd	ed
edede
e         de
e         ddfdZdej        d
edefdZdS )NetworkBrokerPlain,Broker class for MySQL socket communication.r   Nc                     d| _         d S N_pktnrr0   s    r$   __init__zNetworkBrokerPlain.__init__   s    r&   pktc                     t          |           t          k     rt          d          t          j        d| dd         dz             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)rJ   pllseqids      r$   
get_headerzNetworkBrokerPlain.get_header   sb     s88***RSSS M$AaC7 233A6F 
 Ezr&   next_idc                 Z    || xj         dz  c_         n|| _         | xj         dz  c_         dS z:Set the given packet id, if any, else increment packet id.Nr      rF   r0   rX   s     r$   _set_next_pktnrz"NetworkBrokerPlain._set_next_pktnr   s4    ?KK1KKK!DKsr&   r)   r*   c                   K   	 |                     |           |                                 d{V  dS # t          $ r&}t          d|t	          |          f          |d}~wt
          $ r}t          d          |d}~ww xY w)z!Write packet to the comm channel.N  r!   valuesi  r!   )r1   drainIOErrorr   r%   AttributeError)r0   r)   r*   rJ   r   s        r$   
_write_pktzNetworkBrokerPlain._write_pkt   s      	8LL,,..          	 	 	"G[-=-=#>    	8 	8 	8"...C7	8s    /5 
B!A  B-A>>Br   r3   sizec                    K   t          d          }t          |          |k     rU|                    |t          |          z
             d{V }|st          d          ||z  }t          |          |k     U|S )z(Read `size` bytes from the comm channel.r&   Ni  rb   )r?   rQ   r5   r   )r0   r3   rg   rJ   chunks        r$   _read_chunkzNetworkBrokerPlain._read_chunk   s       nn#hhoo ++dSXXo66666666E 1$400005LC	 #hhoo
 
r&   r+   r,   r-   c           
      4  K   |                      |           d}t          t          |          t          z            D ]k}|                     ||dt          j        d| j                  z   |||t          z            z              d{V  |                                   |t          z  }l|                     ||t          j        dt          |          |z
            dd         t          j        d| j                  z   ||d         z              d{V  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      <BNrN   rO   )r]   rangerQ   r   rf   rS   packrG   )r0   r)   r*   r+   r,   r-   offset_s           r$   r1   zNetworkBrokerPlain.write   sa      	]+++
 s7||'99:: 
	) 
	)A//+dDK001&6,>#>>?@           """((FFooKc'llV344QqS9k$,,-fgg
 
 	
 	
 	
 	
 	
 	
 	
 	
 	
r&   c                 ,  K   	 |                      |t                     d{V }|                     |          \  }| _        ||                      ||           d{V z   S # t          $ r&}t          d|t          |          f          |d}~ww xY w)z+Receive `one` packet from the MySQL server.rg   Nr_   r`   )rj   r   rW   rG   rd   r   r%   )r0   r3   r*   headerpayload_lenr   s         r$   r5   zNetworkBrokerPlain.read   s      	++F9M+NNNNNNNNF (,v'>'>$K $"2"26"2"L"LLLLLLLLL 	 	 	"G[-=-=#>  	s   AA# #
B-!BBr   NN)r   r6   )r7   r8   r9   r:   rI   staticmethodr;   r   r<   rW   r   r]   r   r"   rf   r=   r>   r?   rj   r1   r5   r/   r&   r$   rA   rA      s       66    
 
%S/ 
 
 
 \
 x}     
8| 
8c 
8 
8RV 
8 
8 
8 
8 9:
 
*
25
	
 
 
 
" (,26$
 $
$
 $
 	$

  }$
 #+3-$
 
$
 $
 $
 $
L!5  	      r&   rA   c                   F    e Zd ZdZd fdZedededee         fd            Z	edede
eeef         fd	            Zdd
ee         ddfdZdedededdf fdZ	 	 ddedededee         dee         ddf fdZdej        deddf fdZdej        dedef fdZ xZS )NetworkBrokerCompressedrB   r   Nc                 |    t                                                       d| _        t                      | _        d S rD   )superrI   _compressed_pktnrr   _queue_read)r0   	__class__s    r$   rI   z NetworkBrokerCompressed.__init__  s3    !#-2WWr&   r+   pktnrc                    d}g }t          t          |           t          z            D ]R}|                    dt	          j        d|          z   | ||t          z            z              |dz   dz  }|t          z  }S|                    t	          j        dt          |           |z
            dd         t	          j        d|          z   | |d         z              |S )	z2Prepare a payload for sending to the MySQL server.r   rl   rm   r   r[   rN   rO   N)rn   rQ   r   appendrS   ro   )r+   r   rp   pktsrq   s        r$   _prepare_packetsz(NetworkBrokerCompressed._prepare_packets  s     
 s7||'99:: 	) 	)AKK+dE**+&6,>#>>?@  
 QY#%E((FFKc'llV344QqS9k$&&'fgg	
 	
 	

 r&   rJ   c                    t          |           t          k     rt          d          t          j        d| dd         dz             d         | d         t          j        d| dd         dz             d         }}}|||fS )rL   rM   rN   r   rO   rP         )rQ   r   rR   rS   rT   )rJ   compressed_pllrV   uncompressed_plls       r$   rW   z"NetworkBrokerCompressed.get_header!  s     s88555RSSS M$AaC7 233A6FM$AaC7 233A6  0 u&666r&   rX   c                 Z    || xj         dz  c_         n|| _         | xj         dz  c_         dS rZ   )r}   r\   s     r$   _set_next_compressed_pktnrz2NetworkBrokerCompressed._set_next_compressed_pktnr/  s@    ?""a'"""%,D"#%r&   r)   r*   c                 h  K   t          j        |          }t          j        dt	          |                    dd         t          j        d| j                  z   t          j        dt	          |                    dd         z   |z   }t                                          |||           d{V S )z1Compress packet and write it to the comm channel.rN   r   rO   rm   N)zlibcompressrS   ro   rQ   r}   r|   rf   )r0   r)   r*   rJ   compressed_pktr   s        r$   rf   z"NetworkBrokerCompressed._write_pkt7  s      s++Kc.11221Q37k$ 6778k$C))!A#./  	 WW''=========r&   r,   r-   c           
        K   |                      |           |                     |           t          d                              |                     || j                            }t          |          t          t          z
  k    rd}t          t          |          t          z            D ]M}| 
                    |||||t          z                       d{V  |                                  |t          z  }N| 
                    ||||d                    d{V  dS t          |          t          k    r| 
                    |||           d{V  dS t                      
                    ||t          j        dt          |                    dd         t          j        d| j                  z   t          j        dd          dd         z   |z              d{V  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.
        r&   r   NrN   rO   rm   )r]   r   r?   joinr   rG   rQ   r   r   rn   rf   r   r|   rS   ro   r}   )
r0   r)   r*   r+   r,   r-   payload_preprp   rq   r   s
            r$   r1   zNetworkBrokerCompressed.writeB  s3      	]+++''(@AAA ~~**4+@+@$++V+VWWw<<-0DDDD F 3|,,0BBCC - -ooG\&6DV;V2V%W         //111,,//&'<3HIIIIIIIIIII 7||111oofg|DDDDDDDDDDD gg((Kc,&7&7881=k$(>??@k$**1Q3/0 ##          r&   r3   r   c                 .  K   t          t          j        t                                          ||           d{V                     }d}|t          |          k     r3t          j        d|||t          z   dz
           dz             d         }t          |z   t          |          |z
  k    rt                                          |t                     d{V }| 
                    |          \  }| _        }t                                          ||           d{V }||dk    r|nt          j        |          z  }| j                            |||t          z   |z                       |t          |z   z  }|t          |          k     1dS dS )z&Handle reading of a compressed packet.rs   Nr   rN   r   rP   )r?   r   
decompressr|   rj   rQ   rS   rT   r   r   rW   r}   r~   r   )
r0   r3   r   rJ   rp   rU   rt   r   r   r   s
            r$   _read_compressed_pktz,NetworkBrokerCompressed._read_compressed_pkty  s     
 O%''"5"5f>"5"R"RRRRRRRSS
 
 s3xx-c&6,@#@1#DDEO C $c)CHHv,===  %ww22!@  3           OOF++	"*$',ww':':6':'W'W!W!W!W!W!W!W '1,, #N88 ##C:N1NQT1T(T$UVVV*S00F; s3xxr&   c                 B  K   | j         s	 t                                          |t                     d{V }|                     |          \  }| _        }|dk    rB| j                             t                                          ||           d{V            n|                     ||           d{V  n3# t          $ r&}t          d|t          |          f          |d}~ww xY w| j         sdS | j                                         }|d         | _        |S )z{Receive `one` or `several` packets from the MySQL server, enqueue them, and
        return the packet at the head.
        rs   Nr   r_   r`   rO   )r~   r|   rj   r   rW   r}   r   r   rd   r   r%   popleftrG   )	r0   r3   r*   rt   r   r   r   rJ   r   s	           r$   r5   zNetworkBrokerCompressed.read  s       	$ww22!@  3           OOF++	"*$ $q(($++#gg11&~1NNNNNNNN   
 33FNKKKKKKKKK   &S1A1A'B  
  	4&&((!f
s   B0B= =
C-!C((C-rv   rw   r6   )r7   r8   r9   r:   rI   rx   r;   r<   r   r   r   rW   r   r   r   r"   rf   r1   r=   r>   r   r?   r5   __classcell__r   s   @r$   rz   rz      s       665 5 5 5 5 5
 %  U    \0 7 7%S#"6 7 7 7 \7& &(3- &4 & & & &	>| 	>c 	> 	>RV 	> 	> 	> 	> 	> 	>  (,265 55 5 	5
  }5 #+3-5 
5 5 5 5 5 5n&1*&1<?&1	&1 &1 &1 &1 &1 &1P%!5 % %	 % % % % % % % % % %r&   rz   c                      e Zd ZdZddZedefd            Zede	ddfd            Z
ddZdefd	Zd
eddfdZddZdej        ddfdZ	 	 ddedee         dee         ddfdZdefdZdddddg g fdee         dee         dee         dee         dee         deee                  deee                  dej        fdZdS )MySQLSocketzMySQL socket communication interface.

    Examples:
        Subclasses: network.MySQLTCPSocket and network.MySQLUnixSocket.
    r   Nc                 r    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_addressrA   
_netbroker_is_connectedrH   s    r$   rI   zMySQLSocket.__init__  s<     8</326 '+);)=)=#(r&   c                     | j         S )zSocket location.)r   rH   s    r$   r*   zMySQLSocket.address  s     }r&   kwargsc                 
   K   dS )zOpen the socket.Nr/   r0   r   s     r$   r   zMySQLSocket.open_connection  r2   r&   c                    K   | j         rb| j                                          | j         j        | j         j                                         | j                                          d{V  d| _        dS )zClose the connection.NF)r   close	transportabortwait_closedr   rH   s    r$   close_connectionzMySQLSocket.close_connection  sz      < 	-L   |%1&,,...,**,,,,,,,,,"r&   c                     | j         S )zCheck if the socket is connected.

        Return:
            bool: Returns `True` if the socket is connected to MySQL server.
        )r   rH   s    r$   is_connectedzMySQLSocket.is_connected  s     !!r&   timeoutc                     || _         dS )zSet the connection timeout.N)r   )r0   r   s     r$   set_connection_timeoutz"MySQLSocket.set_connection_timeout  s    #*   r&   c                 ,    t                      | _        dS )zIEnable network layer where transactions are made with compressed packets.N)rz   r   rH   s    r$   switch_to_compressed_modez%MySQLSocket.switch_to_compressed_mode  s    133r&   ssl_contextc                    K   | j         J | j         j                            d          }|j        dk    rt	          d          | j                             |           d{V  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   get_extra_infofamilyr   	start_tls)r0   r   r   s      r$   switch_to_sslzMySQLSocket.switch_to_ssl  sx      " |''''66x@@=A"#QRRRl$$[11111111111r&   r+   r,   r-   c                 h   K   | j                             | j        | j        |||           d{V  dS )z!Send packets to the MySQL server.)r,   r-   N)r   r1   r   r*   )r0   r+   r,   r-   s       r$   r1   zMySQLSocket.write  sb       o##LL'%= $ 
 
 	
 	
 	
 	
 	
 	
 	
 	
 	
r&   c                 \   K   | j                             | j        | j                   d{V S )z#Read packets from the MySQL server.N)r   r5   r   r*   rH   s    r$   r5   zMySQLSocket.read&  s4      _))$,EEEEEEEEEr&   Fssl_cassl_certssl_keyssl_verify_certssl_verify_identitytls_versionstls_cipher_suitesc                    | j         st          d          t          t          d          	 |r|                    d           |d         }t
          |         }	t          j        |	          }
|dk    rZd	|vr|
xj        t          j        z  c_        d
|vr|
xj        t          j	        z  c_        d|vr|
xj        t          j
        z  c_        nt          j                    }
||
_        |rt          j        |
_        n%|rt          j        |
_        nt          j        |
_        |
                                 |rH	 |
                    |           n1# t&          t          j        f$ r}t          d|           |d}~ww xY w|rI	 |
                    ||           n1# t&          t          j        f$ r}t          d|           |d}~ww xY w|r(|
                    d                    |                     |
S # t0          $ r}t3          d          |d}~wt&          t4          t          j        t          j        f$ r"}t          t9          |                    |d}~ww xY w)zBuild a SSLContext.i   rb   Nz&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_locationsrd   SSLErrorload_cert_chainset_ciphersr   	NameErrorr   NotImplementedErrorCertificateErrorr"   )r0   r   r   r   r   r   r   r   tls_versionssl_protocolcontextr   s               r$   build_ssl_contextzMySQLSocket.build_ssl_context*  s    | 	- t,,,,;GHHH3	4 7!!$!///*1o+K8.66)++ 443+<< 443+<<l223?:466%8G" 4&)&7##$ 4&)&7##&)m#&&((( TT11&9999. T T T()IC)I)IJJPSST UU++Hg>>>>. U U U()JS)J)JKKQTTU ! A##CHH->$?$?@@@N 	W 	W 	W#$LMMSVV L	
 	4 	4 	4 !S**3	4sl   DG9 ;E G9 E?'E::E??G9 F G9 G
2GG

.G9 9
IH*I=IIrv   r6   )r7   r8   r9   r:   rI   propertyr"   r*   r
   r   r   r   boolr   r<   r   r   r   r   r   r;   r   r1   r?   r5   r   r   r/   r&   r$   r   r     s4        	) 	) 	) 	)     X c d    ^# # # #"d " " " "+c +d + + + +4 4 4 42s~ 2$ 2 2 2 28 (,26	
 

  }
 #+3-	

 

 
 
 
FI F F F F !%"&!%*/.3,.13D4 D4D4 3-D4 #	D4
 "$D4 &d^D4 tCy)D4 $DI.D4 
D4 D4 D4 D4 D4 D4r&   r   c                   D     e Zd ZdZ	 ddededef fdZd	ed
dfdZ	 x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                     t                                                       || _        || _        || _        | d| | _        d S )Nr   )r|   rI   _host_port_force_ipv6r   )r0   r   r   r   r   s       r$   rI   zMySQLTcpSocket.__init__z  sJ     	

!+ $--t--r&   r   r   Nc                 p   K   t          d| j        | j        d| d{V \  | _        | _        d| _        dS )zOpen TCP/IP connection.)r   r   NTr/   )r   r   r   r   r   r   r   s     r$   r   zMySQLTcpSocket.open_connection  sg      +: ,
$*,
 ,
06,
 ,
 &
 &
 &
 &
 &
 &
"dl "r&   )r   r   F)r7   r8   r9   r:   r"   r<   r   rI   r   r   r   r   s   @r$   r   r   q  s          MR. ..-0.EI. . . . . ."c "d " " " " " " " "r&   c                   :     e Zd ZdZd	def fdZdeddfdZ xZS )
r   zbMySQL socket class using UNIX sockets.

    Args:
        unix_socket: UNIX socket file path.
    /tmp/mysql.sockunix_socketc                 V    t                                                       || _        d S rw   )r|   rI   r   )r0   r   r   s     r$   rI   zMySQLUnixSocket.__init__  s$    (r&   r   r   Nc                 n   K   t          j        dd| j        i| d{V \  | _        | _        d| _        dS )zOpen UNIX socket connection.pathNTr/   )r=   open_unix_connectionr   r   r   r   r   s     r$   r   zMySQLUnixSocket.open_connection  sl      
 . 
 

"(
 
 
 
 
 
 
 
	
LL "r&   )r   )	r7   r8   r9   r:   r"   rI   r   r   r   r   s   @r$   r   r     su         ) )C ) ) ) ) ) )"c "d " " " " " " " "r&   )-r:   __all__r=   rS   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   networkr   r   r   r   utilsr   r   rd   r"   r%   r(   rA   rz   r   r   r   r/   r&   r$   <module>r     s  > M L .
/   
JJJ #''#	 LL    
CCC $ # # # # # # #       4 4 4 4 4 4 4 4 4 4 4 4 4 4                       1 0 0 0 0 0 0 0HW H H H H HD D D D DC D D DNg g g g g g g gTF F F F F0 F F FRe4 e4 e4 e4 e4# e4 e4 e4P" " " " "[ " " "4" " " " "k " " " " "s   4 >>