o
    .js(                     @  s   d dl mZ d dlZd dlZ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 dd	lmZmZmZ ejd
ejdejdejdejdejdiZdZdddZG dd dZG dd dZG dd deZdS )    )annotationsN)deque)Any   )RangeSet)Headers   )QuicFrameTypeQuicPacketTypeQuicTransportParametersinitial	handshake0RTT1RTTretryversion_negotiationz0.3databytesreturnstrc                 C  s   t | dS )Nascii)binasciihexlifydecode)r    r   D/home/thesage/.local/lib/python3.10/site-packages/qh3/quic/logger.pyhexdump   s   r   c                   @  s<  e Zd ZdZdkdd	ZdlddZdmddZdnddZdoddZdpd!d"Z	dqd$d%Z
drd&d'Zdsd)d*Zdtd/d0Zdud2d3Zdrd4d5Zdvd6d7Zdvd8d9Zdrd:d;Zdwd=d>Zdxd?d@ZdydAdBZdzdCdDZd{dFdGZd|dIdJZd}dLdMZd~dRdSZddVdWZddXdYZdd\d]Zdd_d`ZddbdcZddfdgZ ddhdiZ!djS )QuicLoggerTraceaE  
    A QUIC event trace.

    Events are logged in the format defined by qlog.

    See:
    - https://datatracker.ietf.org/doc/html/draft-ietf-quic-qlog-main-schema-02
    - https://datatracker.ietf.org/doc/html/draft-marx-quic-qlog-quic-events
    - https://datatracker.ietf.org/doc/html/draft-marx-quic-qlog-h3-events
    	is_clientboolodcidr   r   Nonec                C  s&   || _ t | _d|rdndd| _d S )Nqh3clientserver)nametype)_odcidr   _events_vantage_point)selfr   r    r   r   r   __init__-   s
   
zQuicLoggerTrace.__init__rangesr   delayfloatdictc                 C  s   |  |dd |D ddS )Nc                 S  s    g | ]}|d  |d d gqS )r   r   r   ).0xr   r   r   
<listcomp>:   s     z4QuicLoggerTrace.encode_ack_frame.<locals>.<listcomp>ack)	ack_delayacked_ranges
frame_type)encode_time)r*   r,   r-   r   r   r   encode_ack_frame7   s   z QuicLoggerTrace.encode_ack_frame
error_codeintr6   
int | Nonereason_phraser   c                 C  s0   ||d u rdndd||d}|d ur||d< |S )Napplication	transportconnection_close)r9   error_spacer6   raw_error_codereasontrigger_frame_typer   )r*   r9   r6   r<   attrsr   r   r   encode_connection_close_frame>   s   z-QuicLoggerTrace.encode_connection_close_framemaximumc                 C  s2   |t jkr
d|dS d||t jkrddS ddS )Nmax_data)r6   rF   max_streamsunidirectionalbidirectional)r6   rF   stream_type)r	   MAX_DATAMAX_STREAMS_UNI)r*   r6   rF   r   r   r   encode_connection_limit_frameM   s   


z-QuicLoggerTrace.encode_connection_limit_framer   offsetc                 C  s   dt ||dS )Ncrypto)r6   lengthrO   len)r*   r   rO   r   r   r   encode_crypto_frame[   s   z#QuicLoggerTrace.encode_crypto_framelimitc                 C  
   d|dS )Ndata_blocked)r6   rU   r   )r*   rU   r   r   r   encode_data_blocked_frameb      
z)QuicLoggerTrace.encode_data_blocked_framerQ   c                 C  rV   )Ndatagram)r6   rQ   r   )r*   rQ   r   r   r   encode_datagram_framee   rY   z%QuicLoggerTrace.encode_datagram_framec                 C     ddiS )Nr6   handshake_doner   r*   r   r   r   encode_handshake_done_frameh      z+QuicLoggerTrace.encode_handshake_done_frame	stream_idc                 C     d||dS )Nmax_stream_data)r6   rF   ra   r   )r*   rF   ra   r   r   r   encode_max_stream_data_framek      z,QuicLoggerTrace.encode_max_stream_data_frameconnection_idretire_prior_tosequence_numberstateless_reset_tokenc                 C  s   t |dt|t |||dS )Nnew_connection_id)rf   r6   rQ   reset_tokenrg   rh   )r   rS   )r*   rf   rg   rh   ri   r   r   r   encode_new_connection_id_framer   s   z.QuicLoggerTrace.encode_new_connection_id_frametokenc                 C  s   dt |t|dS )N	new_token)r6   rQ   rm   )rS   r   )r*   rm   r   r   r   encode_new_token_frame   s   z&QuicLoggerTrace.encode_new_token_framec                 C  r\   )Nr6   paddingr   r^   r   r   r   encode_padding_frame   r`   z$QuicLoggerTrace.encode_padding_framec                 C     t |ddS )Npath_challenger   r6   r   r*   r   r   r   r   encode_path_challenge_frame      z+QuicLoggerTrace.encode_path_challenge_framec                 C  rr   )Npath_responsert   ru   rv   r   r   r   encode_path_response_frame   rx   z*QuicLoggerTrace.encode_path_response_framec                 C  r\   )Nr6   pingr   r^   r   r   r   encode_ping_frame   r`   z!QuicLoggerTrace.encode_ping_frame
final_sizec                 C  s   ||d|dS )Nreset_stream)r9   r}   r6   ra   r   )r*   r9   r}   ra   r   r   r   encode_reset_stream_frame   s
   z)QuicLoggerTrace.encode_reset_stream_framec                 C  rV   )Nretire_connection_id)r6   rh   r   )r*   rh   r   r   r   !encode_retire_connection_id_frame   s   z1QuicLoggerTrace.encode_retire_connection_id_framec                 C  rb   )Nstream_data_blocked)r6   rU   ra   r   )r*   rU   ra   r   r   r    encode_stream_data_blocked_frame   re   z0QuicLoggerTrace.encode_stream_data_blocked_framec                 C  rb   )Nstop_sending)r6   r9   ra   r   )r*   r9   ra   r   r   r   encode_stop_sending_frame   re   z)QuicLoggerTrace.encode_stop_sending_framefinc                 C  s   |dt |||dS )Nstream)r   r6   rQ   rO   ra   rR   )r*   r   r   rO   ra   r   r   r   encode_stream_frame   s   z#QuicLoggerTrace.encode_stream_frameis_unidirectionalc                 C  s   d||rddS ddS )Nstreams_blockedrI   rJ   )r6   rU   rK   r   )r*   r   rU   r   r   r   encode_streams_blocked_frame   s   z,QuicLoggerTrace.encode_streams_blocked_framesecondsc                 C  s   |d S )z1
        Convert a time to milliseconds.
        i  r   )r*   r   r   r   r   r7      s   zQuicLoggerTrace.encode_timeowner
parametersr   dict[str, Any]c                 C  s^   d|i}|j  D ]#\}}t|tr|||< q	t|tr#t|||< q	t|tr,|||< q	|S )Nr   )__dict__items
isinstancer   r   r   r:   )r*   r   r   r   
param_nameparam_valuer   r   r   encode_transport_parameters   s   



z+QuicLoggerTrace.encode_transport_parameterspacket_typer
   c                 C  s   t | S N)PACKET_TYPE_NAMES)r*   r   r   r   r   r      r`   zQuicLoggerTrace.packet_typec                 C  s   ddi||dS )Nr6   r   framerQ   ra   r   )r*   rQ   ra   r   r   r   encode_http3_data_frame   s   z'QuicLoggerTrace.encode_http3_data_frameheadersr   c                 C  s   d|  |d||dS )Nr   )r6   r   r   _encode_http3_headers)r*   rQ   r   ra   r   r   r   encode_http3_headers_frame   s   z*QuicLoggerTrace.encode_http3_headers_framepush_idc                 C  s   d|  ||d||dS )Npush_promise)r6   r   r   r   r   )r*   rQ   r   r   ra   r   r   r   encode_http3_push_promise_frame   s   z/QuicLoggerTrace.encode_http3_push_promise_frame
list[dict]c                 C  s   dd |D S )Nc                 S  s*   g | ]}|d   d|d  ddqS )r   utf8r   )r%   value)r   )r0   hr   r   r   r2      s    z9QuicLoggerTrace._encode_http3_headers.<locals>.<listcomp>r   )r*   r   r   r   r   r      s   z%QuicLoggerTrace._encode_http3_headerscategoryeventc                C  s*   | j ||d | | t d d S )N:)r   r%   time)r(   appendr7   r   )r*   r   r   r   r   r   r   	log_event  s   
zQuicLoggerTrace.log_eventc                 C  s   dt | jit| j| jdS )zP
        Return the trace as a dictionary which can be written as JSON.
        ODCID)common_fieldseventsvantage_point)r   r'   listr(   r)   r^   r   r   r   to_dict  s
   
zQuicLoggerTrace.to_dictN)r   r   r    r   r   r!   )r,   r   r-   r.   r   r/   )r9   r:   r6   r;   r<   r   r   r/   )r6   r:   rF   r:   r   r/   )r   r   rO   r:   r   r/   )rU   r:   r   r/   )rQ   r:   r   r/   )r   r/   )rF   r:   ra   r:   r   r/   )
rf   r   rg   r:   rh   r:   ri   r   r   r/   )rm   r   r   r/   )r   r   r   r/   )r9   r:   r}   r:   ra   r:   r   r/   )rh   r:   r   r/   )rU   r:   ra   r:   r   r/   )r9   r:   ra   r:   r   r/   )
r   r   r   r   rO   r:   ra   r:   r   r/   )r   r   rU   r:   r   r/   )r   r.   r   r.   )r   r   r   r   r   r   )r   r
   r   r   )rQ   r:   ra   r:   r   r/   )rQ   r:   r   r   ra   r:   r   r/   )
rQ   r:   r   r   r   r:   ra   r:   r   r/   )r   r   r   r   )r   r   r   r   r   r/   r   r!   r   r   )"__name__
__module____qualname____doc__r+   r8   rE   rN   rT   rX   r[   r_   rd   rl   ro   rq   rw   rz   r|   r   r   r   r   r   r   r7   r   r   r   r   r   r   r   r   r   r   r   r   r   !   s@    






























	r   c                   @  s8   e Zd ZdZdddZdddZdddZdddZdS )
QuicLoggerz<
    A QUIC event logger which stores traces in memory.
    r   r!   c                 C  s
   g | _ d S r   _tracesr^   r   r   r   r+     rY   zQuicLogger.__init__r   r   r    r   r   c                 C  s   t ||d}| j| |S )N)r   r    )r   r   r   )r*   r   r    tracer   r   r   start_trace"  s   zQuicLogger.start_tracer   c                 C  s   || j v s	J dd S )Nz-QuicLoggerTrace does not belong to QuicLoggerr   )r*   r   r   r   r   	end_trace'  s   zQuicLogger.end_tracer   c                 C  s   dt dd | jD dS )zQ
        Return the traces as a dictionary which can be written as JSON.
        JSONc                 S  s   g | ]}|  qS r   )r   )r0   r   r   r   r   r2   1  s    z&QuicLogger.to_dict.<locals>.<listcomp>qlog_formatqlog_versiontraces)QLOG_VERSIONr   r^   r   r   r   r   *  s   zQuicLogger.to_dictN)r   r!   )r   r   r    r   r   r   r   r   r   r!   r   )r   r   r   r   r+   r   r   r   r   r   r   r   r     s    


r   c                      s,   e Zd ZdZd fddZdd
dZ  ZS )QuicFileLoggerz>
    A QUIC event logger which writes one trace per file.
    pathr   r   r!   c                   s0   t j|std| d|| _t   d S )NzQUIC log output directory 'z' does not exist)osr   isdir
ValueErrorsuperr+   )r*   r   	__class__r   r   r+   :  s   zQuicFileLogger.__init__r   r   c                 C  st   |  }tj| j|d d d }t|d}tdt|gd| W d    n1 s-w   Y  | j	| d S )Nr   r   z.qlogwr   r   )
r   r   r   joinopenjsondumpr   r   remove)r*   r   
trace_dict
trace_path	logger_fpr   r   r   r   @  s   	zQuicFileLogger.end_trace)r   r   r   r!   r   )r   r   r   r   r+   r   __classcell__r   r   r   r   r   5  s    r   )r   r   r   r   )
__future__r   r   r   r   r   collectionsr   typingr   _hazmatr   	h3.eventsr   packetr	   r
   r   INITIAL	HANDSHAKEZERO_RTTONE_RTTRETRYVERSION_NEGOTIATIONr   r   r   r   r   r   r   r   r   r   <module>   s.    
 z