
    @OOf                         d dl Z d dlmZ d dlmZ d dlmZ d dlmZ d dl	m
Z
 d dl	mZ d dlmZ d d	lmZ  G d
 d      Ze G d d             Ze G d d             Zy)    N)	dataclass)KaitaiStream)SSL)
connection)dtls_client_hello)tls_client_hello)check)contextc                       e Zd ZU dZeed<   ddedefdZddedefdZe	de
e   fd	       Ze	ded
z  fd       Ze	de
e   fd       Ze	de
eeef      fd       Zd Zy
)ClientHellozX
    A TLS ClientHello is the first message sent by the client when initiating TLS.
    
_raw_bytesraw_client_hellodtlsc                     || _         |r7t        j                  t        t	        j
                  |                  | _        yt        j                  t        t	        j
                  |                  | _        y)z/Create a TLS ClientHello object from raw bytes.N)	r   r   DtlsClientHellor   ioBytesIO_client_hellor   TlsClientHello)selfr   r   s      N/var/www/premiumrankchecker/venv/lib/python3.12/site-packages/mitmproxy/tls.py__init__zClientHello.__init__   sX    *!2!B!BRZZ(89:"D "2!@!@RZZ(89:"D    wrap_in_recordreturnc                 8   t        | j                  t        j                        rt        |rcdt        | j                        dz   j                  dd      z   dz   t        | j                        j                  dd      z   | j                  z   S | j                  S )ao  
        The raw ClientHello bytes as seen on the wire.

        If `wrap_in_record` is True, the ClientHello will be wrapped in a synthetic TLS record
        (`0x160303 + len(chm) + 0x01 + len(ch)`), which is the format expected by some tools.
        The synthetic record assumes TLS version (`0x0303`), which may be different from what has been sent over the
        wire. JA3 hashes are unaffected by this as they only use the TLS version from the ClientHello data structure.

        A future implementation may return not just the exact ClientHello, but also the exact record(s) as seen on the
        wire.
        s         big)	byteorder      )
isinstancer   r   r   NotImplementedErrorlenr   to_bytes)r   r   s     r   	raw_byteszClientHello.raw_bytes!   s     d((*;*K*KL%%  t'!+55a55IJ 	
 doo&//U/CD   ??"r   c                 B    | j                   j                  j                  S )z6The cipher suites offered by the client (as raw ints).)r   cipher_suitesr   s    r   r)   zClientHello.cipher_suites@   s     !!//===r   Nc                    t        | j                  dd      x}r|j                  D ]  }|j                  dk(  xr t	        |j
                  j                        dk(  xr^ |j
                  j                  d   j                  dk(  xr6 t        j                  |j
                  j                  d   j                        }|s|j
                  j                  d   j                  j                  d      c S  y)z
        The [Server Name Indication](https://en.wikipedia.org/wiki/Server_Name_Indication),
        which indicates which hostname the client wants to connect to.
        
extensionsNr      ascii)getattrr   r,   typer%   bodyserver_names	name_typer	   is_valid_host	host_namedecode)r   ext	extensionis_valid_sni_extensions       r   snizClientHello.sniE   s     $,,lDAA3A ^^ T	NNd* VINN778A=V!33A6@@AEV ++INN,G,G,J,T,TU	 ' *$>>66q9CCJJ7SST r   c                     t        | j                  dd      x}rH|j                  D ]9  }|j                  dk(  st	        d |j
                  j                  D              c S  g S )z
        The application layer protocols offered by the client as part of the
        [ALPN](https://en.wikipedia.org/wiki/Application-Layer_Protocol_Negotiation) TLS extension.
        r,   N   c              3   4   K   | ]  }|j                     y w)N)name).0xs     r   	<genexpr>z-ClientHello.alpn_protocols.<locals>.<genexpr>`   s     N1Ns   )r/   r   r,   r0   listr1   alpn_protocols)r   r7   r8   s      r   rC   zClientHello.alpn_protocolsW   sa     $,,lDAA3A ^^ O	>>T)N	0M0MNNNO 	r   c                     g }t        | j                  dd      x}rE|j                  D ]6  }t        |d|j                        }|j	                  |j
                  |f       8 |S )zOThe raw list of extensions in the form of `(extension_type, raw_bytes)` tuples.r,   N	_raw_body)r/   r   r,   r1   appendr0   )r   retr7   r8   r1   s        r   r,   zClientHello.extensionsc   sd     $,,lDAA3A ^^ 3	y+y~~F

INND123 
r   c                 <    d| j                    d| j                   dS )NzClientHello(sni: z, alpn_protocols: ))r:   rC   r*   s    r   __repr__zClientHello.__repr__m   s$    "488*,>t?R?R>SSTUUr   )F)T)__name__
__module____qualname____doc__bytes__annotations__boolr   r'   propertyrB   intr)   strr:   rC   tupler,   rJ    r   r   r   r      s     
 
d 
# # #> >tCy > > S4Z  " 	U 	 	 DsEz!23  Vr   r   c                   \    e Zd ZU dZej
                  ed<   	 eed<   	 dZe	ed<   	 dZ
e	ed<   y)ClientHelloDataz7
    Event data for `tls_clienthello` event hooks.
    r
   client_helloFignore_connectionestablish_server_tls_firstN)rK   rL   rM   rN   r
   ContextrP   r   rZ   rQ   r[   rV   r   r   rX   rX   q   sA     __1,#t# (-,r   rX   c                       e Zd ZU dZej
                  ed<   	 ej                  ed<   	 dZ	e
j
                  dz  ed<   	 dZeed<   y)TlsDataza
    Event data for `tls_start_client`, `tls_start_server`, and `tls_handshake` event hooks.
    connr
   Nssl_connFis_dtls)rK   rL   rM   rN   r   
ConnectionrP   r
   r\   r`   r   ra   rQ   rV   r   r   r^   r^      sN     

"__1&*Hcnnt#* GTr   r^   )r   dataclassesr   kaitaistructr   OpenSSLr   	mitmproxyr   mitmproxy.contrib.kaitaistructr   r   mitmproxy.netr	   mitmproxy.proxyr
   r   rX   r^   rV   r   r   <module>rj      sa    	 ! %    < ;  #`V `VF   (   r   