o
    �õ±j°4  ã                   @  sb  d Z ddlmZ ddlZddlZddlZddlmZ ddlmZm	Z	 ddl
mZmZmZ ddlmZmZ ddlmZ dd	lmZ dd
lmZmZ ddlmZmZ ddlmZ ddlmZ 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)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1 dgZ2d%dd„Z3d&dd„Z4G dd„ dƒZ5G d d!„ d!ƒZ6d'd"d#„Z7G d$d„ dƒZ8dS )(z
Telnet server.
é    )ÚannotationsN)Úget_running_loop)ÚCallableÚ	Coroutine)ÚAnyÚTextIOÚcast)Úcreate_app_sessionÚget_app)Úrun_in_terminal)ÚSize)ÚAnyFormattedTextÚto_formatted_text)Ú	PipeInputÚcreate_pipe_input)ÚVt100_Output)Úprint_formatted_text)Ú	BaseStyleÚ
DummyStyleé   )Úlogger)ÚDOÚECHOÚIACÚLINEMODEÚMODEÚNAWSÚSBÚSEÚSENDÚSUPPRESS_GO_AHEADÚTTYPEÚWILLÚTelnetProtocolParserÚTelnetServerÚnumberÚintÚreturnÚbytesc                 C  s
   t | fƒS ©N)r(   )r%   © r*   ú—/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/prompt_toolkit/contrib/telnet/server.pyÚint2byte-   ó   
r,   Ú
connectionúsocket.socketÚNonec                 C  s¬   t  d¡ |  tt t ¡ |  tt t ¡ |  tt t t	 t
dƒ t t ¡ |  tt t ¡ |  tt t ¡ |  tt t ¡ |  tt t t t t ¡ d S )NzInitializing telnet connectionr   )r   ÚinfoÚsendr   r   r   r"   r    r   r   r,   r   r   r   r!   r   ©r.   r*   r*   r+   Ú_initialize_telnet1   s   
&"r4   c                   @  s^   e Zd ZdZddd	„Zddd„Zddd„Zddd„Zddd„Ze	ddd„ƒZ
e	ddd„ƒZdS )Ú_ConnectionStdoutzk
    Wrapper around socket which provides `write` and `flush` methods for the
    Vt100_Output output.
    r.   r/   ÚencodingÚstrr'   r0   c                 C  s"   || _ || _d| _g | _d| _d S )NÚstrictF)Ú	_encodingÚ_connectionÚ_errorsÚ_bufferÚ_closed)Úselfr.   r6   r*   r*   r+   Ú__init__T   s
   
z_ConnectionStdout.__init__Údatac                 C  s2   |  dd¡}| j |j| j| jd�¡ |  ¡  d S )NÚ
z
)Úerrors)Úreplacer<   ÚappendÚencoder9   r;   Úflush©r>   r@   r*   r*   r+   Úwrite[   s   z_ConnectionStdout.writeÚboolc                 C  s   dS ©NTr*   ©r>   r*   r*   r+   Úisatty`   s   z_ConnectionStdout.isattyc              
   C  s^   z| j s| j d | j¡¡ W n ty) } zt d|› �¡ W Y d }~nd }~ww g | _d S )Nó    z Couldn't send data over socket: )r=   r:   r2   Újoinr<   ÚOSErrorr   Úwarning)r>   Úer*   r*   r+   rF   c   s   €€ÿ
z_ConnectionStdout.flushc                 C  s
   d| _ d S rJ   )r=   rK   r*   r*   r+   Úclosel   r-   z_ConnectionStdout.closec                 C  ó   | j S r)   )r9   rK   r*   r*   r+   r6   o   ó   z_ConnectionStdout.encodingc                 C  rS   r)   )r;   rK   r*   r*   r+   rB   s   rT   z_ConnectionStdout.errorsN)r.   r/   r6   r7   r'   r0   )r@   r7   r'   r0   )r'   rI   ©r'   r0   )r'   r7   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r?   rH   rL   rF   rR   Úpropertyr6   rB   r*   r*   r*   r+   r5   N   s    




	r5   c                   @  sd   e Zd ZdZ	d,d-dd„Zd.dd„Zd/dd„Zd.dd„Zd0d!d"„Zd0d#d$„Z	d1d'd(„Z
d.d)d*„Zd+S )2ÚTelnetConnectionz6
    Class that represents one Telnet connection.
    TÚconnr/   Úaddrútuple[str, int]Úinteractú7Callable[[TelnetConnection], Coroutine[Any, Any, None]]Úserverr$   r6   r7   ÚstyleúBaseStyle | NoneÚvt100_inputr   Ú
enable_cprrI   r'   r0   c	                   sÄ   |ˆ_ |ˆ_|ˆ_|ˆ_|ˆ_|ˆ_dˆ_t ¡ ˆ_	|ˆ_
ˆ ˆ_d ˆ_tddd�ˆ_t|ƒ d‡fdd„‰ttt||d	�ƒˆ_d‡fdd„}	d‡fdd„}
d‡ ‡‡fdd„}t|	|
|ƒˆ_d ˆ_d S )NFé(   éO   ©ÚrowsÚcolumnsr'   r   c                     s   ˆ j S r)   )Úsizer*   rK   r*   r+   Úget_size›   s   z+TelnetConnection.__init__.<locals>.get_size)r6   r@   r(   r0   c                   s   ˆ j  | ¡ dS )z-TelnetProtocolParser 'data_received' callbackN)rd   Ú
send_bytes©r@   rK   r*   r+   Údata_received    s   z0TelnetConnection.__init__.<locals>.data_receivedri   r&   rj   c                   s:   t | |d�ˆ _ˆ jdurˆ jrˆ j dd„ ¡ dS dS dS )z-TelnetProtocolParser 'size_received' callbackrh   Nc                   S  s
   t ƒ  ¡ S r)   )r
   Ú
_on_resizer*   r*   r*   r+   Ú<lambda>¨   ó   
 zBTelnetConnection.__init__.<locals>.size_received.<locals>.<lambda>)r   rk   Úvt100_outputÚcontextÚrunrh   rK   r*   r+   Úsize_received¤   s   ÿz0TelnetConnection.__init__.<locals>.size_receivedÚttyper7   c                   s"   t ˆjˆ| ˆ d�ˆ_ˆj ¡  dS )z.TelnetProtocolParser 'ttype_received' callback)Útermre   N)r   Ústdoutrs   Ú_readyÚset)rw   ©re   rl   r>   r*   r+   Úttype_receivedª   s   
ÿz1TelnetConnection.__init__.<locals>.ttype_received)r'   r   ©r@   r(   r'   r0   )ri   r&   rj   r&   r'   r0   )rw   r7   r'   r0   )r\   r]   r_   ra   r6   rb   r=   ÚasyncioÚEventrz   rd   re   rs   r   rk   r4   r   r   r5   ry   r#   Úparserrt   )r>   r\   r]   r_   ra   r6   rb   rd   re   ro   rv   r}   r*   r|   r+   r?   }   s(   

zTelnetConnection.__init__c                 ƒ  s¦   �d‡ fdd„}t ƒ }| ˆ j|¡ z;ˆ j ¡ I dH  tˆ jˆ jd�� t 	¡ ˆ _
ˆ  ˆ ¡I dH  W d  ƒ n1 s;w   Y  W ˆ  ¡  dS W ˆ  ¡  dS ˆ  ¡  w )z"
        Run application.
        r'   r0   c                    s<   ˆ j  d¡} | rˆ  | ¡ d S t djˆ jŽ ¡ ˆ  ¡  d S )Ni   z&Connection closed by client. {!r} {!r})r\   ÚrecvÚfeedr   r1   Úformatr]   rR   rn   rK   r*   r+   Úhandle_incoming_data¹   s
   z>TelnetConnection.run_application.<locals>.handle_incoming_dataN)ÚinputÚoutputrU   )r   Ú
add_readerr\   rz   Úwaitr	   rd   rs   ÚcontextvarsÚcopy_contextrt   r_   rR   )r>   r…   Úloopr*   rK   r+   Úrun_application´   s   €

þüz TelnetConnection.run_applicationr@   r(   c                 C  s   | j  |¡ dS )zF
        Handler for incoming data. (Called by TelnetServer.)
        N)r�   rƒ   rG   r*   r*   r+   rƒ   Ï   s   zTelnetConnection.feedc                 C  s@   | j sd| _ | j ¡  tƒ  | j¡ | j ¡  | j ¡  dS dS )z#
        Closed by client.
        TN)r=   rd   rR   r   Úremove_readerr\   ry   rK   r*   r*   r+   rR   Õ   s   

úzTelnetConnection.closeÚformatted_textr   c                 C  s0   | j du rdS t|ƒ}t| j || jptƒ ƒ dS )z*
        Send text to the client.
        N)rs   r   r   rb   r   ©r>   r�   r*   r*   r+   r2   á   s   
ÿzTelnetConnection.sendc                   s   t ˆ ƒ‰ ˆ ‡ ‡fdd„¡S )z\
        Send text to the client.
        This is asynchronous, returns a `Future`.
        c                     s
   ˆ  ˆ ¡S r)   )r2   r*   ©r�   r>   r*   r+   rq   ò   rr   z4TelnetConnection.send_above_prompt.<locals>.<lambda>)r   Ú_run_in_terminalr�   r*   r‘   r+   Úsend_above_promptì   s   z"TelnetConnection.send_above_promptÚfuncúCallable[[], None]c                 C  s    | j r| j  t|¡ d S tdƒ‚)Nz2Called _run_in_terminal outside `run_application`.)rt   ru   r   ÚRuntimeError)r>   r”   r*   r*   r+   r’   ô   s   z!TelnetConnection._run_in_terminalc                 C  s4   | j du rdS | j  ¡  | j  dd¡ | j  ¡  dS )zB
        Erase the screen and move the cursor to the top.
        Nr   )rs   Úerase_screenÚcursor_gotorF   rK   r*   r*   r+   r—   ü   s
   

zTelnetConnection.erase_screenN)T)r\   r/   r]   r^   r_   r`   ra   r$   r6   r7   rb   rc   rd   r   re   rI   r'   r0   rU   r~   )r�   r   r'   r0   )r”   r•   r'   r0   )rV   rW   rX   rY   r?   r�   rƒ   rR   r2   r“   r’   r—   r*   r*   r*   r+   r[   x   s    ÷
7




r[   c                 Ã  s   �d S r)   r*   r3   r*   r*   r+   Ú_dummy_interact  s   €r™   c                   @  s`   e Zd ZdZddedddfd$dd„Zed%dd„ƒZd&d'dd„Zd(dd„Z	d(dd „Z
d)d"d#„ZdS )*r$   a¥  
    Telnet server implementation.

    Example::

        async def interact(connection):
            connection.send("Welcome")
            session = PromptSession()
            result = await session.prompt_async(message="Say something: ")
            connection.send(f"You said: {result}
")

        async def main():
            server = TelnetServer(interact=interact, port=2323)
            await server.run()
    z	127.0.0.1é   zutf-8NTÚhostr7   Úportr&   r_   r`   r6   rb   rc   re   rI   r'   r0   c                 C  s<   || _ || _|| _|| _|| _|| _d | _g | _tƒ | _	d S r)   )
r›   rœ   r_   r6   rb   re   Ú	_run_taskÚ_application_tasksr{   Úconnections)r>   r›   rœ   r_   r6   rb   re   r*   r*   r+   r?     s   zTelnetServer.__init__r/   c                 C  s>   t   t jt j¡}| t jt jd¡ | ||f¡ | d¡ |S )Nr   é   )ÚsocketÚAF_INETÚSOCK_STREAMÚ
setsockoptÚ
SOL_SOCKETÚSO_REUSEADDRÚbindÚlisten)Úclsr›   rœ   Úsr*   r*   r+   Ú_create_socket3  s
   
zTelnetServer._create_socketÚready_cbúCallable[[], None] | Nonec                 ƒ  s  �ˆ   ˆ jˆ j¡‰t dˆ jˆ j¡ tƒ  ˆ‡ ‡fdd„¡ |r#|ƒ  z4t ¡ I dH  W tƒ  	ˆ¡ ˆ 
¡  ˆ jD ]}| ¡  q9tˆ jƒdkrVtjˆ jdtjd�I dH  dS dS tƒ  	ˆ¡ ˆ 
¡  ˆ jD ]}| ¡  qetˆ jƒdkr�tjˆ jdtjd�I dH  w w )z²
        Run the telnet server, until this gets cancelled.

        :param ready_cb: Callback that will be called at the point that we're
            actually listening.
        z.Listening for telnet connections on %s port %rc                     s
   ˆ   ˆ¡S r)   )Ú_acceptr*   ©r>   r¡   r*   r+   rq   I  rr   z"TelnetServer.run.<locals>.<lambda>Nr   )ÚtimeoutÚreturn_when)r«   r›   rœ   r   r1   r   rˆ   r   ÚFuturerŽ   rR   rž   ÚcancelÚlenr‰   ÚALL_COMPLETED)r>   r¬   Útr*   r¯   r+   ru   =  s@   €
ÿ

ýÿõ

ýÿzTelnetServer.runc                 C  s$   | j durdS tƒ  |  ¡ ¡| _ dS )z}
        Deprecated: Use `.run()` instead.

        Start the telnet server (stop by calling and awaiting `stop()`).
        N)r�   r   Úcreate_taskru   rK   r*   r*   r+   Ústartd  s   
zTelnetServer.startc                 Ã  sD   �| j dur | j  ¡  z	| j I dH  W dS  tjy   Y dS w dS )z¦
        Deprecated: Use `.run()` instead.

        Stop a telnet server that was started using `.start()` and wait for the
        cancellation to complete.
        N)r�   r³   r   ÚCancelledErrorrK   r*   r*   r+   Ústopp  s   €

ÿüzTelnetServer.stopÚlisten_socketc                   sP   |  ¡ \‰‰ tjdgˆ ¢R Ž  d‡ ‡‡‡fdd„}tƒ  |ƒ ¡‰ˆj ˆ¡ dS )z1
        Accept new incoming connection.
        zNew connection %r %rr'   r0   c                  “  s”  �zÁz]t ƒ �P} tˆˆ ˆjˆˆjˆj| ˆjd�}ˆj |¡ tj	dgˆ ¢R Ž  z| 
¡ I d H  W ˆj |¡ tj	dgˆ ¢R Ž  nˆj |¡ tj	dgˆ ¢R Ž  w W d   ƒ n1 sYw   Y  W n? tym   t 	d¡ Y n; tyz   t 	d¡ Y n7 tyž } ztdt|ƒj› �|ƒ dd l}| ¡  W Y d }~n d }~ww W ˆj ˆ¡ d S W ˆj ˆ¡ d S W ˆj ˆ¡ d S W ˆj ˆ¡ d S ˆj ˆ¡ w )N)r6   rb   rd   re   zStarting interaction %r %rzStopping interaction %r %rz)Unhandled EOFError in telnet application.z2Unhandled KeyboardInterrupt in telnet application.zGot r   )r   r[   r_   r6   rb   re   rŸ   Úaddr   r1   r�   ÚremoveÚEOFErrorÚKeyboardInterruptÚBaseExceptionÚprintÚtyperV   Ú	tracebackÚ	print_excrž   )rd   r.   rQ   rÃ   ©r]   r\   r>   Útaskr*   r+   ru   †  sR   €ø
ÿî€€ü€ö
ùþz!TelnetServer._accept.<locals>.runNrU   )Úacceptr   r1   r   r·   rž   rD   )r>   r»   ru   r*   rÅ   r+   r®   ~  s
   &zTelnetServer._accept)r›   r7   rœ   r&   r_   r`   r6   r7   rb   rc   re   rI   r'   r0   )r›   r7   rœ   r&   r'   r/   r)   )r¬   r­   r'   r0   rU   )r»   r/   r'   r0   )rV   rW   rX   rY   r™   r?   Úclassmethodr«   ru   r¸   rº   r®   r*   r*   r*   r+   r$     s    ÷	
'
)r%   r&   r'   r(   )r.   r/   r'   r0   )r.   r[   r'   r0   )9rY   Ú
__future__r   r   rŠ   r¡   r   Úcollections.abcr   r   Útypingr   r   r   Ú"prompt_toolkit.application.currentr	   r
   Ú*prompt_toolkit.application.run_in_terminalr   Úprompt_toolkit.data_structuresr   Úprompt_toolkit.formatted_textr   r   Úprompt_toolkit.inputr   r   Úprompt_toolkit.output.vt100r   Úprompt_toolkit.rendererr   Úprompt_toolkit.stylesr   r   Úlogr   Úprotocolr   r   r   r   r   r   r   r   r   r    r!   r"   r#   Ú__all__r,   r4   r5   r[   r™   r$   r*   r*   r*   r+   Ú<module>   s6    <ÿ

* 
