U
    ÓZj(  ã                
   @   sœ   d dl Z d dlZd dlZd dlmZ d dlZd dlZd\ZZzd dlZW n& ek
rn Z	 ze	ZW 5 dZ	[	X Y nX d dl
mZ d dlmZ G dd„ deƒZdS )é    N)Úselect)NN)ÚProxyCommandFailure)ÚClosingContextManagerc                   @   sP   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zed
d„ ƒZ	edd„ ƒZ
dd„ ZdS )ÚProxyCommandaª  
    Wraps a subprocess running ProxyCommand-driven programs.

    This class implements a the socket-like interface needed by the
    `.Transport` and `.Packetizer` classes. Using this class instead of a
    regular socket makes it possible to talk with a Popen'd command that will
    proxy traffic between the client and a server hosted in another machine.

    Instances of this class may be used as context managers.
    c                 C   s@   t dkrt‚t |¡| _t j| jt jt jt jdd�| _d| _dS )a  
        Create a new CommandProxy instance. The instance created by this
        class can be passed as an argument to the `.Transport` class.

        :param str command_line:
            the command that should be executed and used as the proxy.
        Nr   )ÚstdinÚstdoutÚstderrÚbufsize)	Ú
subprocessÚsubprocess_import_errorÚshlexÚsplitÚcmdÚPopenÚPIPEÚprocessÚtimeout)ÚselfZcommand_line© r   úQ/var/www/html/TRUCKING_PROJECT/venv/lib/python3.8/site-packages/paramiko/proxy.pyÚ__init__3   s    ûzProxyCommand.__init__c              
   C   sR   z| j j |¡ W n6 tk
rH } ztd | j¡|jƒ‚W 5 d}~X Y nX t|ƒS )zº
        Write the content received from the SSH client to the standard
        input of the forked command.

        :param str content: string to be sent to the forked command
        ú N)	r   r   ÚwriteÚIOErrorr   Újoinr   ÚstrerrorÚlen)r   ÚcontentÚer   r   r   ÚsendG   s
    &zProxyCommand.sendc           
   
   C   s  z¤d}t   ¡ }t|ƒ|k r d}| jdk	rPt   ¡ | }|| jkrFt ¡ ‚| j| }t| jjgg g |ƒ\}}}|r|d | jjkr|t | jj 	¡ |t|ƒ ¡7 }q|W S  tjk
rÈ   |rÂ| Y S ‚ Y n8 t
k
� rþ }	 ztd | j¡|	jƒ‚W 5 d}	~	X Y nX dS )zÎ
        Read from the standard output of the forked program.

        :param int size: how many chars should be read

        :return: the string of bytes read, which may be shorter than requested
        ó    Nr   r   )Útimer   r   Úsocketr   r   r   ÚosÚreadÚfilenor   r   r   r   r   )
r   ÚsizeÚbufferÚstartZselect_timeoutÚelapsedÚrÚwÚxr   r   r   r   ÚrecvX   s.    



 
ÿzProxyCommand.recvc                 C   s   t  | jjtj¡ d S ©N)r#   Úkillr   ÚpidÚsignalÚSIGTERM©r   r   r   r   Úclosey   s    zProxyCommand.closec                 C   s   | j jd k	S r.   )r   Ú
returncoder3   r   r   r   Úclosed|   s    zProxyCommand.closedc                 C   s   | j S r.   )r6   r3   r   r   r   Ú_closed€   s    zProxyCommand._closedc                 C   s
   || _ d S r.   )r   )r   r   r   r   r   Ú
settimeout…   s    zProxyCommand.settimeoutN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r-   r4   Úpropertyr6   r7   r8   r   r   r   r   r   '   s   !

r   )r#   r   r1   r   r"   r!   r
   r   ÚImportErrorr   Zparamiko.ssh_exceptionr   Zparamiko.utilr   r   r   r   r   r   Ú<module>   s   