a
    kÄ#d
   ã                   @   sF  d Z ddlmZ ddl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mZmZ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 dd	lmZ ddlm  mZ G d
d„ deƒZG dd„ deƒZ G dd„ dej!ƒZ"d$dd„Z#d%dd„Z$G dd„ dej%ƒZ&dd„ Z'dd„ Z(dd„ Z)dd„ Z*dd„ Z+d d!„ Z,d"d#„ Z-dS )&z1
Run commands when the Scapy interpreter starts.
é    )Úprint_functionN)Úconf)ÚNoThemeÚDefaultThemeÚ
HTMLTheme2ÚLatexTheme2)Ú	log_scapyÚScapy_Exception)Ú
tex_escape)ÚAnyÚOptionalÚTextIOÚDictÚTuple)Úqueuec                   @   s   e Zd ZdZdS )ÚStopAutorunÚ N)Ú__name__Ú
__module__Ú__qualname__Úcode_run© r   r   úD/var/www/html/gsl.tools/lib/python3.9/site-packages/scapy/autorun.pyr   &   s   r   c                   @   s   e Zd ZdS )ÚStopAutorunTimeoutN)r   r   r   r   r   r   r   r   *   s   r   c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚScapyAutorunInterpreterc                 O   s   t jj| g|¢R i |¤Ž d S ©N)ÚcodeÚInteractiveInterpreterÚ__init__)ÚselfÚargsÚkargsr   r   r   r   /   s    z ScapyAutorunInterpreter.__init__c                 C   s   d S r   r   )r   Údatar   r   r   Úwrite3   s    zScapyAutorunInterpreter.writeN)r   r   r   r   r#   r   r   r   r   r   .   s   r   c           	      C   sž  t j}�zL�z*|d u r(ddlm} |ƒ }t|d�}ztjjjd d= W n t	yX   Y n0 |d urh|t _d}|  
¡ }| d¡ | ¡  |r¢tj tj dd¡¡ ntj tj d	d
¡¡ | ¡ }t|ƒ |d| 7 }d t_| |¡ræq†tj�rtjtjtjtjjtjd� d t_W W |t _dS d}t|ƒdkr†�q4q†W n t�yJ   Y n0 W |t _n|t _0 ztjjjd d W S  t	�y˜   tjjj dd ¡ Y S 0 d S )Nr   )Ú_scapy_builtins)ÚlocalsZscapy_sessionÚ_r   Zps2z... Úps1z>>> Ú
)ÚfileFé   )r   ÚverbZ
scapy.mainr$   r   ÚsixÚmovesÚbuiltinsÚ__dict__ÚKeyErrorÚ
splitlinesÚappendÚreverseÚsysÚstderrr#   ÚgetÚpopÚprintÚ
last_valueZ	runsourceÚ	tracebackÚprint_exceptionÚ	last_typeÚlast_tracebackÚtb_nextÚstdoutÚlenÚ
SystemExit)	Z_cmdsZ
my_globalsr+   Úsvr$   ZinterpÚcmdÚcmdsÚliner   r   r   Úautorun_commands8   s\    


ýù
rF   c                    sh   |du rt ˆ fi ˆ¤ŽS t ¡ ‰‡ ‡‡fdd„}tj|d�}d|_| ¡  | |¡ | ¡ r`t	‚ˆ 
¡ S )za
    Wraps autorun_commands with a timeout that raises StopAutorunTimeout
    on expiration.
    Nc                      s   ˆ  tˆ fi ˆ¤Ž¡ d S r   )ÚputrF   r   ©rD   ÚkwargsÚqr   r   Ú_runnerv   s    z)autorun_commands_timeout.<locals>._runner)ÚtargetT)rF   r   ÚQueueÚ	threadingÚThreadÚdaemonÚstartÚjoinÚis_aliver   r6   )rD   ÚtimeoutrI   rK   Úthr   rH   r   Úautorun_commands_timeoutk   s    
rV   c                   @   s*   e Zd ZdZd	dd„Zdd„ Zdd„ ZdS )
ÚStringWriterzPUtil to mock sys.stdout and sys.stderr, and
    store their output in a 's' var.Nc                 C   s   d| _ || _tj | ¡ d S )Nr   )ÚsÚdebugr,   ÚStringIOr   )r   rY   r   r   r   r   …   s    zStringWriter.__init__c                 C   sD   t | dd ƒr| jr| j |¡ t | dd ƒd ur<|  j|7  _t|ƒS )NrY   rX   )ÚgetattrrY   r#   rX   r@   )r   Úxr   r   r   r#   ‹   s
    zStringWriter.writec                 C   s    t | dd ƒr| jr| j ¡  d S )NrY   )r[   rY   Úflush)r   r   r   r   r]   ”   s    zStringWriter.flush)N)r   r   r   Ú__doc__r   r#   r]   r   r   r   r   rW   ‚   s   
	rW   c           	   
   K   s  t jt jt j  }}}tƒ }tjd }t |¡ t t	j
|d�¡ zŒz(| t _t _t jt _t| fi |¤Ž}W n. tyš } z|j|_‚ W Y d}~n
d}~0 0 W |||  t _t _t _t tjd ¡ t |¡ n2|||  t _t _t _t tjd ¡ t |¡ 0 |j|fS )á  Create an interactive session and execute the
    commands passed as "cmds" and return all output

    :param cmds: a list of commands to run
    :param timeout: timeout in seconds
    :returns: (output, returned) contains both sys.stdout and sys.stderr logs
    r   )ÚstreamN)r4   r?   r5   Ú
excepthookrW   r   ÚhandlersÚremoveHandlerÚ
addHandlerÚloggingÚStreamHandlerÚ__excepthook__rV   r   rX   r   )	rD   r!   ÚsstdoutÚsstderrZsexcepthookÚswZh_oldÚresÚer   r   r   Úautorun_get_interactive_sessionš   s(    	

þrm   c              
   K   s–   t jt j }}t|d�}zbz | t _t _t| fi |¤Ž}W n. tyh } z|j|_‚ W Y d}~n
d}~0 0 W || t _t _n|| t _t _0 |j|fS )r_   )rY   N)r4   r?   r5   rW   rV   r   rX   r   )rD   r!   rh   ri   rj   rk   rl   r   r   r   Ú$autorun_get_interactive_live_session·   s    	
 rn   c                 K   s>   t j}z&tƒ t _t| fi |¤Ž\}}W |t _n|t _0 ||fS r   )r   Úcolor_themer   rm   ©rD   r!   ÚctrX   rk   r   r   r   Ú$autorun_get_text_interactive_sessionÎ   s    rr   c                 K   s>   t j}z&tƒ t _t| fi |¤Ž\}}W |t _n|t _0 ||fS r   )r   ro   r   rn   rp   r   r   r   Ú$autorun_get_live_interactive_sessionÙ   s    rs   c                 K   s>   t j}z&tƒ t _t| fi |¤Ž\}}W |t _n|t _0 ||fS r   )r   ro   r   rm   rp   r   r   r   Ú$autorun_get_ansi_interactive_sessionä   s    rt   c              
   K   s‚   t j}dd„ }z^z tƒ t _t| fi |¤Ž\}}W n2 tyb } z||jƒ|_‚ W Y d }~n
d }~0 0 W |t _n|t _0 ||ƒ|fS )Nc                 S   s$   |   dd¡  dd¡  dd¡  dd¡S )Nú<z&lt;ú>z&gt;z#[#z#]#)Úreplace©rX   r   r   r   Úto_htmló   s    z5autorun_get_html_interactive_session.<locals>.to_html)r   ro   r   rm   r   r   )rD   r!   rq   ry   rX   rk   rl   r   r   r   Ú$autorun_get_html_interactive_sessionï   s    rz   c              
   K   s‚   t j}dd„ }z^z tƒ t _t| fi |¤Ž\}}W n2 tyb } z||jƒ|_‚ W Y d }~n
d }~0 0 W |t _n|t _0 ||ƒ|fS )Nc                 S   s    t | ƒ dd¡ dd¡ dd¡S )Nz@[@Ú{z@]@Ú}z@`@ú\)r
   rw   rx   r   r   r   Úto_latex  s    z7autorun_get_latex_interactive_session.<locals>.to_latex)r   ro   r   rm   r   r   )rD   r!   rq   r~   rX   rk   rl   r   r   r   Ú%autorun_get_latex_interactive_session  s    r   )NN)N).r^   Ú
__future__r   r   re   r4   rN   r:   Zscapy.configr   Zscapy.themesr   r   r   r   Zscapy.errorr   r	   Zscapy.utilsr
   Zscapy.compatr   r   r   r   r   Zscapy.libs.six.movesr   Zscapy.libs.sixZlibsr,   r   r   r   r   rF   rV   rZ   rW   rm   rn   rr   rs   rt   rz   r   r   r   r   r   Ú<module>   s4   

3
