o
    j~                     @   s   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 d dlmZmZ G dd	 d	eZG d
d dZdS )    N)logger)connect)QuicConnectionProtocol)QuicConfiguration)ConnectionTerminated	QuicEventStreamDataReceived)Optionalcastc                       sX   e Zd Z fddZdejddf fddZdeddfd	d
Zde	ddfddZ
  ZS )QuicTransportClientc                    s.   t  j|i | d | _d | _d | _d | _d S N)super__init___ack_waiterin_queuedisconnected_evt	stream_id)selfargskwargs	__class__ /root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/asysocks/unicomm/protocol/quic.pyr      s
   
zQuicTransportClient.__init__	transportreturnNc                    s   t d t |S )Nz
Connected!)printr   connection_made)r   r   r   r   r   r      s   z#QuicTransportClient.connection_madedatac                    s:   | j d u r| j | _ d}| j| j || |   d S )NF)r   _quicget_next_available_stream_idsend_stream_datatransmit)r   r   
end_streamr   r   r   send   s   
zQuicTransportClient.sendeventc                 C   s:   t |tr| j|jd f t |tr| jd d S d S )NNN)
isinstancer   r   
put_nowaitr   r   )r   r%   r   r   r   quic_event_received$   s
   

z'QuicTransportClient.quic_event_received)__name__
__module____qualname__r   asyncioBaseTransportr   bytesr$   r   r)   __classcell__r   r   r   r   r      s
    
r   c                   @   s2   e Zd ZdZdddZdd Zdd Zd	d
 ZdS )
QUICSocketz
	Generic asynchronous TCP socket class, nothing SMB related.
	Creates the connection and channels incoming/outgoing bytes via asynchonous queues.
	Nc                 C   s.   || _ || _t | _t | _t | _d S r   )settingssocketr-   Queue	out_queuer   Eventr   )r   r3   targetr   r   r   r   1   s
   

zQUICSocket.__init__c                    s   | j  rdS | j   dS )zI
		Disconnects from the socket.
		Stops the reader and writer streams.
		N)disconnectedis_setset)r   r   r   r   
disconnect:   s   
zQUICSocket.disconnectc              
      s   zSt | jj| jj| jtd4 I d H 4}tt|}| j|_| j|_| j	
 s;| j I d H }||I d H  | j	
 r&W d   I d H  W d S 1 I d H sMw   Y  W d S  tjy_   Y d S  ty} } ztd |  I d H  W Y d }~d S d }~ww )N)configurationcreate_protocolz[QUICSocket] handle_outgoing)r   r2   hostnameportr<   r   r
   r   r   r8   r9   r5   getr$   r-   CancelledError	Exceptionr   	exceptionr;   )r   clientr   er   r   r   	run_innerC   s$   $

2
zQUICSocket.run_innerc              
      sf   zt dgddtjd| _t|  | _W dS  ty2 } zt	
d d|fW  Y d}~S d}~ww )	zk
		Main function to be called, connects to the target specified in settings, and starts reading/writing.
		smbTi   )alpn_protocols	is_clientmax_datagram_frame_sizeverify_mode)TNz[QUICSocket] mainFN)r   ssl	CERT_NONEr<   r-   create_taskrF   rD   rB   r   rC   )r   rE   r   r   r   r   W   s   
zQUICSocket.connectr&   )r*   r+   r,   __doc__r   r;   rF   r   r   r   r   r   r1   ,   s    
		r1   )enumr-   rL   asysocks.unicommr   aioquic.asyncio.clientr   aioquic.asyncio.protocolr   aioquic.quic.configurationr   aioquic.quic.eventsr   r   r   typingr	   r
   r   r1   r   r   r   r   <module>   s    