o
    Òó·iG  ã                   @   s¼  U d dl Z d dl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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mZ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 m!Z! G d
d„ deƒZ"da#ee$d< da%d a&da'd a(da)dd„ Z*dd„ Z+dd„ Z,d/dd„Z-dd„ Z.dd„ Z/dd„ Z0dd„ Z1dd„ Z2G d d!„ d!ƒZ3G d"d#„ d#ej4ƒZ4G d$d%„ d%ej5ƒZ6G d&d'„ d'ej7ƒZ7G d(d)„ d)ej8ƒZ8d*d+„ Z9d,d-„ Z:e;d.krÜe*ƒ  dS dS )0é    N)ÚAnyÚListÚTuple)ÚconnectÚdefault)ÚConchOptions)Ú
ConchError)ÚchannelÚcommonÚ
connectionÚ
forwardingÚsession)ÚreactorÚstdioÚtask)ÚlogÚusage)ÚioTypeÚnetworkStringc                	   @   sø   e Zd ZU dZdZg d¢g d¢g d¢gZg d¢g d¢g d¢g d	¢g d
¢g d¢gZejdgej	dd�ej	dd�dœe 
¡ ej	dd�ej	ddd�gd�Zg Zeeeeeef f  ed< g Zeeeeeef f  ed< dd„ Zdd„ Zdd„ Zdd„ ZdS ) ÚClientOptionsz(Usage:   conch [options] host [command]
zYconch is a SSHv2 client that allows logging into a remote machine and executing commands.)ÚescapeÚeú~)ÚlocalforwardÚLNz<listen-port:host:port   Forward local port to remote address)ÚremoteforwardÚRNz<listen-port:host:port   Forward remote port to local address)ÚnullÚnzRedirect input from /dev/null.)ÚforkÚfz(Fork to background after authentication.)ÚttyÚtz-Tty; allocate a tty even if command is given.)ÚnottyÚTzDo not allocate a tty.)ÚnoshellÚNz"Do not execute a shell or command.)Ú	subsystemÚsz-Invoke command (mandatory) as SSH2 subsystem.)r!   r#   zlisten-port:host:port)Údescr)r   r   ÚcommandZargumentT)r)   Úrepeat)ZmutuallyExclusiveZ
optActionsZextraActionsÚlocalForwardsÚremoteForwardsc                 C   sv   |dkr
d| d< dS |d dkr$t |ƒdkr$tt|d ƒd ƒ| d< dS t |ƒdkr0|| d< dS t d	|› d
�¡ dS )z:
        Set escape character; ``none'' = disable
        ÚnoneNr   r   ú^é   é   é@   zBad escape character 'z'.)ÚlenÚchrÚordÚsysÚexit)ÚselfÚesc© r:   úaC:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-packages\twisted/conch/scripts/conch.pyÚ
opt_escapeO   s   zClientOptions.opt_escapec                 C   ó8   |  d¡\}}}t|ƒ}t|ƒ}| j |||ff¡ dS )zH
        Forward local port to remote address (lport:host:port)
        ú:N)ÚsplitÚintr,   Úappend)r8   r    Ú	localPortZ
remoteHostÚ
remotePortr:   r:   r;   Úopt_localforward\   ó   zClientOptions.opt_localforwardc                 C   r=   )zH
        Forward remote port to local address (rport:host:port)
        r>   N)r?   r@   r-   rA   )r8   r    rC   ZconnHostZconnPortr:   r:   r;   Úopt_remoteforwarde   rE   zClientOptions.opt_remoteforwardc                 G   s   || d< d  |¡| d< d S )NÚhostú r*   )Újoin)r8   rG   r*   r:   r:   r;   Ú	parseArgsn   s   zClientOptions.parseArgsN)Ú__name__Ú
__module__Ú__qualname__ZsynopsisZlongdescZoptParametersZoptFlagsr   ZCompletionsZ	CompleterZCompleteUserAtHostZcompDatar,   r   r   r@   Ú__annotations__r-   r<   rD   rF   rJ   r:   r:   r:   r;   r      s>   
 ÿøú	

þ
ýú  		r   Úoptionsc                  C   sº  t jdd … } d| v r#|  d¡}| ||d … |  } | |d |d …= | d d … D ],}z!|  |¡}|d d… dkrK| |d  d dkrKg | ||d …< W q) tyU   Y q)w tƒ azt | ¡ W n# tjyƒ } zt	d|› �ƒ t 
¡  t  d¡ W Y d }~nd }~ww td	 r­td
 ržtd
 dkr–t j}nttd
 dƒ}nt j}t j}t |¡ |t _nt ¡  tƒ  t j ¡ }zt |¡aW n tyË   d aY nw zt tjdd„ ¡}W n tyâ   d }Y nw z0t ¡  W trót |tjt¡ |rüt tj|¡ td �rtd �std �st tjtj ¡ n.t�rt |tjt¡ |�r)t tj|¡ td �r3td �s8td �sAt tjtj ¡ w w t j !¡ �rVtd �sVt	d "td ¡ƒ t  t#¡ d S )Nr1   z-lr0   é   z-or   ú-zERROR: r   Úlogfileza+c                  W   s   t  dt¡S ©Nr   )r   Ú	callLaterÚ	reConnect)Úar:   r:   r;   Ú<lambda>¦   s    zrun.<locals>.<lambda>r*   r!   r#   zConnection to {} closed.rG   )$r6   ÚargvÚindexÚ
ValueErrorr   rO   ZparseOptionsr   Z
UsageErrorÚprintZopt_helpr7   ÚstdoutÚopenÚstderrr   ZstartLoggingZdiscardLogsÚ	doConnectÚstdinÚfilenor!   Ú	tcgetattrÚoldÚBaseExceptionÚsignalZSIGUSR1r   ÚrunÚ	tcsetattrÚTCSANOWÚSIGWINCHÚSIG_DFLÚisattyÚformatÚ
exitStatus)ÚargsÚiÚargÚur    ZrealoutÚfdZoldUSR1r:   r:   r;   rf   |   s„   

$€ÿ€ý

ÿ
ÿÿ
€ûÿrf   c                  C   s,   ddl m}  dat dt¡ t |  ¡ ¡ ‚ )Nr   ©Úfailurer0   g{®Gáz„?)	Útwisted.pythonrt   rm   r   rT   Ú_stopReactorr   ÚerrZFailurers   r:   r:   r;   ÚhandleError¸   s
   rx   c                   C   s$   zt  ¡  W d S  ty   Y d S w ©N)r   Ústoprd   r:   r:   r:   r;   rv   Â   s
   ÿrv   Úreturnc                  C   sÌ   dt d v rt d  dd¡\t d< t d< t jsddgt _t d s&t ¡ t d< t d s/dt d< ntt d ƒt d< t d } t d }tj}t d	 sNt | t ¡t d	< t 	t d t t
ƒ ¡}t | |t ||¡ t¡ d S )
Nú@rG   r1   Úuserz~/.ssh/id_rsaz~/.ssh/id_dsaÚporté   zhost-key-algorithms)rO   r?   Z	identitysÚgetpassÚgetuserr@   r   ZverifyHostKeyZgetHostKeyAlgorithmsZSSHUserAuthClientÚSSHConnectionr   Ú
addErrbackÚ_ebExit)rG   r~   ZvhkZuaor:   r:   r;   r_   É   s    

r_   c                 C   s   d| › �a t dt¡ d S )Nzconch: exiting with error çš™™™™™¹?)rm   r   rT   rv   )r    r:   r:   r;   r„   ß   s   
r„   c                  C   s.  t tjdƒr
ttƒ tjr&tjD ]\} }t | t 	t|t
¡¡}tj |¡ qtjrItjD ]\}}t d|› d|› �¡ t ||¡ q,t ddt¡ td rQtd rWt tƒ ¡ td r“t ¡ rdt d	¡ t ¡  td
ƒD ](}zt |¡ W ql ty’ } zd	d l}|j|jkrˆ‚ W Y d }~qld }~ww d S d S )NZ
sendIgnorez!asking for remote forwarding for r>   ÚbeforeÚshutdownr%   Úagentr   r   é   )ÚhasattrÚconnÚ	transportÚ
_KeepAliverO   r,   r   Z	listenTCPr   ZSSHListenForwardingFactoryÚ SSHListenClientForwardingChannelrA   r-   r   ÚmsgÚrequestRemoteForwardingZaddSystemEventTriggerÚbeforeShutdownZopenChannelÚ
SSHSessionÚosr   Ú_exitZsetsidÚrangeÚcloseÚOSErrorÚerrnoÚEBADF)rB   Úhostportr(   rC   ro   r   r˜   r:   r:   r;   Ú	onConnectå   sD   ÿþ
ÿ€ýùr›   c                   C   s   t ƒ  tjj ¡  d S ry   )r‘   r‹   rŒ   ÚloseConnectionr:   r:   r:   r;   rU   	  s   rU   c                  C   s8   t j} | D ]\}}t d|› d|› �¡ t |¡ qd S )Nzcancelling r>   )rO   r-   r   r�   r‹   ÚcancelRemoteForwarding)r-   rC   rš   r:   r:   r;   r‘     s
   þr‘   c                   C   s   t d st dt¡ d S d S )NZ	reconnectr…   )rO   r   rT   rv   r:   r:   r:   r;   ÚstopConnection  s   ÿrž   c                   @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
r�   c                 C   s*   || _ d | _t | j¡| _| j d¡ d S )Ni,  )r‹   ÚglobalTimeoutr   ZLoopingCallÚ
sendGlobalÚlcÚstart)r8   r‹   r:   r:   r;   Ú__init__  s   z_KeepAlive.__init__c                 C   s2   | j jdddd�}| | j¡ t d| j¡| _d S )Ns"   conch-keep-alive@twistedmatrix.comó    r1   ©Z	wantReplyé   )r‹   ÚsendGlobalRequestÚaddBothÚ	_cbGlobalr   rT   Ú	_ebGlobalrŸ   )r8   Údr:   r:   r;   r    !  s
   ÿz_KeepAlive.sendGlobalc                 C   s   | j r| j  ¡  d | _ d S d S ry   )rŸ   Úcancel)r8   Úresr:   r:   r;   r©   (  s   

þz_KeepAlive._cbGlobalc                 C   s    | j rd | _ | jj ¡  d S d S ry   )rŸ   r‹   rŒ   rœ   ©r8   r:   r:   r;   rª   -  s   þz_KeepAlive._ebGlobalN)rK   rL   rM   r£   r    r©   rª   r:   r:   r:   r;   r�     s
    r�   c                   @   sL   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dS )r‚   c                 C   s   | a g | _i | _tƒ  d S ry   )r‹   r,   r-   r›   r®   r:   r:   r;   ÚserviceStarted4  s   
zSSHConnection.serviceStartedc                 C   s(   | j }g | _ |D ]}| ¡  qtƒ  d S ry   )r,   rœ   rž   )r8   Úlfr(   r:   r:   r;   ÚserviceStopped;  s
   

zSSHConnection.serviceStoppedc                 C   sX   t  d|f¡}| jd|dd�}t d|› d|› �¡ | | j||¡ | | j||¡ d S )Nú0.0.0.0s   tcpip-forwardr1   r¥   zrequesting remote forwarding r>   )	r   ÚpackGlobal_tcpip_forwardr§   r   r�   ZaddCallbackÚ_cbRemoteForwardingrƒ   Ú_ebRemoteForwarding)r8   rC   rš   Údatar«   r:   r:   r;   r�   B  s
   z%SSHConnection.requestRemoteForwardingc                 C   s4   t  d|› d|› �¡ || j|< t  t| jƒ¡ d S )Nzaccepted remote forwarding r>   )r   r�   r-   Úrepr)r8   ÚresultrC   rš   r:   r:   r;   r´   I  s   
z!SSHConnection._cbRemoteForwardingc                 C   s&   t  d|› d|› d�¡ t  |¡ d S )Nzremote forwarding r>   z failed©r   r�   )r8   r    rC   rš   r:   r:   r;   rµ   N  s   z!SSHConnection._ebRemoteForwardingc                 C   s^   t  d|f¡}|  d|¡ t d|› �¡ z| j|= W n	 ty$   Y nw t t| jƒ¡ d S )Nr²   s   cancel-tcpip-forwardzcancelling remote forwarding )r   r³   r§   r   r�   r-   Ú	Exceptionr·   )r8   rC   r¶   r:   r:   r;   r�   R  s   ÿz$SSHConnection.cancelRemoteForwardingc                 C   s|   t  d|›�¡ t |¡\}}t  | j¡ t  |¡ |d | jv r8| j|d  }t  d|› �¡ t|||| d�S ttjdƒ‚)NzFTCP r1   zconnect forwarding )ZremoteWindowZremoteMaxPacketr‹   zdon't know about that port)	r   r�   r   ZunpackOpen_forwarded_tcpipr-   ÚSSHConnectForwardingChannelr   r   ZOPEN_CONNECT_FAILED)r8   Z
windowSizeZ	maxPacketr¶   ZremoteHPZorigHPZ	connectHPr:   r:   r;   Úchannel_forwarded_tcpip\  s   
ÿÿz%SSHConnection.channel_forwarded_tcpipc                 C   sV   t  d|› �¡ t  | j¡ t| jƒdkrt  d¡ tƒ  d S | jjd  | |¡ d S )Nzconnection closing r1   zstopping connectionr   )r   r�   Úchannelsr3   rž   Ú	__class__Ú	__bases__ÚchannelClosed)r8   r	   r:   r:   r;   rÀ   l  s   

zSSHConnection.channelClosedN)rK   rL   rM   r¯   r±   r�   r´   rµ   r�   r¼   rÀ   r:   r:   r:   r;   r‚   3  s    
r‚   c                   @   sp   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )r’   s   sessionc           	         sÎ  t  dˆ j› d�¡ td rˆ jjˆ dddd�}| dd	„ ¡ td
 r%d S td r-td s1td s4tƒ  t 	¡ }td rHtd sHdˆ _
ˆ j|_nˆ j|_‡ fdd	„|_t |¡ˆ _d}td rnˆ j ˆ dt td ¡¡ d S td r¯td r tjd }t |tjd¡}t d|¡}t ||d¡}ˆ j ˆ d|¡ t tjˆ j¡ ˆ j ˆ dt td ¡¡ d S td sÝtjd }t |tjd¡}t d|¡}t ||d¡}ˆ j ˆ d|¡ t tjˆ j¡ ˆ j ˆ dd¡ d S )Nzsession z openrˆ   s   auth-agent-req@openssh.comr¤   r1   r¥   c                 S   s
   t  | ¡S ry   r¹   ©Úxr:   r:   r;   rW   €  s   
 z(SSHSession.channelOpen.<locals>.<lambda>r%   r*   r!   r#   r   c                    s   ˆ   ¡ S ry   )ÚsendEOFrÁ   r®   r:   r;   rW   ‹  s    r   r'   s	   subsystemÚTERMÚ12345678Ú4HÚ s   pty-reqs   execs   shell)r   r�   ÚidrO   r‹   ÚsendRequestr¨   Ú_enterRawModer   ZSSHSessionClientÚ
escapeModeÚhandleInputÚdataReceivedÚwriteZconnectionLostr   Z
StandardIOr
   ZNSr“   ÚenvironÚfcntlÚioctlr!   Ú
TIOCGWINSZÚstructÚunpackZpackRequest_pty_reqre   ri   Ú_windowResized)	r8   Zfoor«   Úcrr   ZtermÚwinszÚwinSizeZ
ptyReqDatar:   r®   r;   ÚchannelOpenz  sJ   ÿ


zSSHSession.channelOpenc              	   C   s&  |dv rd| _ |  |¡ d S | j dkr|td krd| _ d S | j dkr‰d| _ |dkr4t d¡ tƒ  d S |dkrDdd	„ }t d
|¡ d S |dkrUt d¡ | jj	 
¡  d S |dkr€| j d¡ | jj ¡ }| ¡  |D ]}| j td || jj| ¡ƒ¡ qkd S |  d| ¡ d S d
| _ |  |¡ d S )N)ó   
ó   r1   r   r0   ó   .zdisconnecting from escapeó   c                   S   s6   t ƒ  tj ¡  tj ¡  t t ¡ tj	¡ t
ƒ  d S ry   )Ú_leaveRawModer6   r\   Úflushr`   r“   ÚkillÚgetpidre   ZSIGTSTPrÊ   r:   r:   r:   r;   Ú_³  s
   


z!SSHSession.handleInput.<locals>._r   ó   Rzrekeying connectionó   #s'   
The following connections are open:
z
  #{} {}
ó   ~)rË   rÎ   rO   r   r�   rž   r   rT   r‹   rŒ   ZsendKexInitr   r½   ÚkeysÚsortr   rl   )r8   Úcharrâ   r½   Z	channelIdr:   r:   r;   rÌ   ¥  sF   



ÿÿÿzSSHSession.handleInputc                 C   s   | j  |¡ d S ry   )r   rÎ   ©r8   r¶   r:   r:   r;   rÍ   Ò  ó   zSSHSession.dataReceivedc                 C   sT   |t jkr(t dt|ƒ› d�¡ ttjƒtkr tjj	 
|¡ d S tj 
|¡ d S d S )Nzgot z stderr data)r   ZEXTENDED_DATA_STDERRr   r�   r3   r   r6   r^   ÚstrÚbufferrÎ   )r8   r"   r¶   r:   r:   r;   ÚextReceivedÕ  s   
ûzSSHSession.extReceivedc                 C   s   t  d¡ | j ¡  d S )Nzgot eof)r   r�   r   ZloseWriteConnectionr®   r:   r:   r;   ÚeofReceivedÝ  s   
zSSHSession.eofReceivedc                 C   s    t  d| › �¡ | j | ¡ d S )Nzremote side closed )r   r�   r‹   Z	sendCloser®   r:   r:   r;   ÚcloseReceivedá  s   zSSHSession.closeReceivedc                 C   s&   t  d| › �¡ t  t| jjƒ¡ d S )Nzclosed )r   r�   r·   r‹   r½   r®   r:   r:   r;   Úclosedå  s   zSSHSession.closedc                 C   s(   t t d|¡d ƒat dt› �¡ d S )Nz>Lr   zexit status: )r@   rÓ   rÔ   rm   r   r�   ré   r:   r:   r;   Úrequest_exit_statusê  s   zSSHSession.request_exit_statusc                 C   s   | j  | ¡ d S ry   )r‹   rÃ   r®   r:   r:   r;   rÃ   ï  rê   zSSHSession.sendEOFc                 C   ó   | j  ¡  d S ry   )r   ZpauseProducingr®   r:   r:   r;   ÚstopWritingò  ó   zSSHSession.stopWritingc                 C   rò   ry   )r   ZresumeProducingr®   r:   r:   r;   ÚstartWritingõ  rô   zSSHSession.startWritingc                 G   sZ   t  dtjd¡}t d|¡}|d |d |d |d f}| j | dtjdg|¢R Ž ¡ d S )	Nr   rÅ   rÆ   r1   r0   r‰   s   window-changez!4L)	rÐ   rÑ   r!   rÒ   rÓ   rÔ   r‹   rÉ   Úpack)r8   rn   r×   rØ   ZnewSizer:   r:   r;   rÕ   ø  s   "zSSHSession._windowResizedN)rK   rL   rM   ÚnamerÙ   rÌ   rÍ   rí   rî   rï   rð   rñ   rÃ   ró   rõ   rÕ   r:   r:   r:   r;   r’   w  s    +-r’   c                   @   ó   e Zd ZdS )rŽ   N©rK   rL   rM   r:   r:   r:   r;   rŽ   ÿ  ó    rŽ   c                   @   rø   )r»   Nrù   r:   r:   r:   r;   r»     rú   r»   c                  C   s*   t sd S tj ¡ } t | tjt¡ da d S rS   )Ú
_inRawModer6   r`   ra   r!   rg   rh   Ú_savedRawMode)rr   r:   r:   r;   rÞ     s
   
rÞ   c                  C   sV  t rd S tj ¡ } zt | ¡}|d d … }W n ty%   t d¡ Y d S w |d tj	B |d< |d tj
tjB tjB tjB tjB tjB tjB  @ |d< ttdƒrZ|d tj @ |d< |d tjtjB tjB tjB tjB tjB tjB  @ |d< ttdƒr…|d tj @ |d< |d tj @ |d< d|d tj< d|d tj< |at | tj|¡ da d S )Nznot a typewriter!r   ÚIUCLCr‰   ÚIEXTENr1   é   ) rû   r6   r`   ra   r!   rb   rd   r   r�   ZIGNPARZISTRIPZINLCRZIGNCRZICRNLZIXONZIXANYZIXOFFrŠ   rý   ZISIGZICANONÚECHOZECHOEZECHOKZECHONLrþ   ZOPOSTZVMINZVTIMErü   rg   rh   )rr   rc   Únewr:   r:   r;   rÊ     sd   

ÿÿþýüûú
ÿ
	ÿþýüûú
ÿ
	rÊ   Ú__main__)r{   N)<rÐ   r€   r“   re   rÓ   r6   r!   Útypingr   r   r   Ztwisted.conch.clientr   r   Ztwisted.conch.client.optionsr   Ztwisted.conch.errorr   Ztwisted.conch.sshr	   r
   r   r   r   Ztwisted.internetr   r   r   ru   r   r   Ztwisted.python.compatr   r   r   rO   rN   r‹   rm   rc   rû   rü   rf   rx   rv   r_   r„   r›   rU   r‘   rž   r�   r‚   Z
SSHChannelr’   rŽ   r»   rÞ   rÊ   rK   r:   r:   r:   r;   Ú<module>   sT   
V<

$D 		2
ÿ