
    LfA                     \   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	Z	d dl
Z
d dlmZ ddlmZ d dlZd dlZej"                  j$                  Z G d de	j                        Z G d dej(                  j*                        Z G d	 d
ej.                  j0                        Z G d de      Zy)    N)BaseHTTPAdapter   )	constantsc                   H     e Zd Z fdZd Zd Zd Zd Zd Zd Z	d Z
 xZS )		SSHSocketc                 n   t         |   t        j                  t        j                         || _        d | _        d | _        d| j
                  v r(| j
                  j                  d      \  | _        | _        d| j
                  v r(| j
                  j                  d      \  | _        | _        d | _	        y )N:@)
super__init__socketAF_INETSOCK_STREAMhostportusersplitproc)selfr   	__class__s     :/usr/lib/python3/dist-packages/docker/transport/sshconn.pyr   zSSHSocket.__init__   s    NNF..	0			$))#'99??3#7 DIty$))#'99??3#7 DIty	    c                    dg}| j                   r|d| j                   gz   }| j                  r|d| j                  gz   }|d| j                  dgz   }d }t        j                  sd }|}t        t        j                        }|j                  dd        |j                  dd        t        j                  d	j                  |      |d
t        j                  t        j                  t        j                  rd n|      | _        y )Nsshz-lz-pz--docker system dial-stdioc                  h    t        j                   t         j                  t         j                         y N)signalSIGINTSIG_IGN r   r   fzSSHSocket.connect.<locals>.f.   s    fmmV^^<r   LD_LIBRARY_PATHSSL_CERT_FILE T)envshellstdoutstdin
preexec_fn)r   r   r   r   IS_WINDOWS_PLATFORMdictosenvironpop
subprocessPopenjoinPIPEr   )r   kwargsargspreexec_funcr"   r&   s         r   connectzSSHSocket.connect"   s    w994++D994++DtTYY(BCC,,=L2:: 	!4(&$$HHTN??//(<<t,P	r   c                    | j                   r | j                   j                  j                  rt        d      | j                   j                  j	                  |      }| j                   j                  j                          |S Nz<SSH subprocess not initiated.connect() must be called first.)r   r)   closed	Exceptionwriteflush)r   datawrittens      r   _writezSSHSocket._write@   s\    yyDIIOO22 > ? ?))//''-		r   c                 &    | j                  |       y r   r@   r   r>   s     r   sendallzSSHSocket.sendallH   s    Dr   c                 $    | j                  |      S r   rB   rC   s     r   sendzSSHSocket.sendK   s    {{4  r   c                 z    | j                   st        d      | j                   j                  j                  |      S r9   )r   r;   r(   read)r   ns     r   recvzSSHSocket.recvN   s6    yy > ? ?yy$$Q''r   c                     | j                   s| j                          | | j                   j                  _        | j                   j                  S r   )r   r7   r(   channel)r   modes     r   makefilezSSHSocket.makefileT   s4    yyLLN#'		 yyr   c                 $   | j                   r | j                   j                  j                  ry | j                   j                  j                  d       | j                   j                  j	                          | j                   j                          y )Ns   

)r   r)   r:   r<   r=   	terminater   s    r   closezSSHSocket.close[   sV    yyDIIOO22		g&				r   )__name__
__module____qualname__r   r7   r@   rD   rF   rJ   rN   rR   __classcell__r   s   @r   r   r      s,    P<!( r   r   c                   &     e Zd Zd fd	Zd Z xZS )SSHConnectionc                 R    t         |   d|       || _        || _        || _        y )N	localhost)timeout)r   r   ssh_transportr\   ssh_host)r   r]   r\   r   r   s       r   r   zSSHConnection.__init__d   s2     	 	
 +r   c                 F   | j                   rN| j                   j                         }|j                  | j                         |j	                  d       || _        y t        | j                        }|j                  | j                         |j                          || _        y )Nr   )	r]   open_session
settimeoutr\   exec_commandr   r^   r7   sock)r   rc   s     r   r7   zSSHConnection.connectl   sw    %%224DOODLL)89 		 T]]+DOODLL)LLN	r   )N<   N)rS   rT   rU   r   r7   rV   rW   s   @r   rY   rY   c   s    
r   rY   c                   0     e Zd ZdZd fd	Zd Zd Z xZS )SSHConnectionPoolr   c                     t         |   d||       d | _        || _        |r|j	                         | _        || _        y )Nr[   )r\   maxsize)r   r   r]   r\   get_transportr^   )r   
ssh_clientr\   rh   r   r   s        r   r   zSSHConnectionPool.__init__|   sH    ' 	 	
 "!+!9!9!;Dr   c                 X    t        | j                  | j                  | j                        S r   )rY   r]   r\   r^   rQ   s    r   	_new_connzSSHConnectionPool._new_conn   s    T//t}}MMr   c                 X   d }	 | j                   j                  | j                  |      }|xs | j                         S # t        $ r! t        j
                  j                  | d      t        j                  $ r/ | j                  r t        j
                  j                  | d      Y }w xY w)N)blockr\   zPool is closed.z>Pool reached maximum size and no more connections are allowed.)poolgetrn   AttributeErrorurllib3
exceptionsClosedPoolErrorqueueEmptyEmptyPoolErrorrl   )r   r\   conns      r   	_get_connzSSHConnectionPool._get_conn   s    	99==tzz7=CD 't~~''  	N$$44T;LMM{{ 	zz((77/ 
 	s   '? A'B)(B))Nrd   
   N)rS   rT   rU   schemer   rl   ry   rV   rW   s   @r   rf   rf   y   s    FN(r   rf   c                        e Zd Zej                  j
                  j                  g dz   Zdej                  ej                  df fd	Z
d Zd Zd	dZ fdZ xZS )
SSHHTTPAdapter)poolsr\   rj   
ssh_paramsmax_pool_sizerd   Fc                    d | _         |s!| j                  |       | j                          || _        |j	                  d      r|t        d      d  | _        || _        || _        t        |d       | _	        t        | -          y )Nzssh://c                 "    | j                         S r   )rR   )ps    r   <lambda>z)SSHHTTPAdapter.__init__.<locals>.<lambda>   s    QWWY r   )dispose_func)rj   _create_paramiko_client_connectr^   
startswithlenr\   r   RecentlyUsedContainerr~   r   r   )r   base_urlr\   pool_connectionsr   	shell_outr   s         r   r   zSSHHTTPAdapter.__init__   s{     ((2MMO x($S]^4DM**+>

 	r   c                 <   t        j                  d      j                  t         j                         t	        j
                         | _        t        j                  j                  |      }|j                  |j                  |j                  d| _        t        j                  j!                  d      }t        j                  j#                  |      rt	        j$                         }t'        |      5 }|j                  |       d d d        |j)                  |j                        }d|v r/t	        j*                  | j,                  d         | j                  d<   d|v r|d   | j                  d<   |j                  d|v r|d   | j                  d<   |j                  d|v r|d   | j                  d	<   d
|v r|d
   | j                  d<   | j                  j/                          | j                  j1                  t	        j2                                y # 1 sw Y   xY w)Nparamiko)hostnamer   usernamez~/.ssh/configproxycommandrc   r   r   r   r   identityfilekey_filename)logging	getLoggersetLevelWARNINGr   	SSHClientrj   urllibparseurlparser   r   r   r   r-   path
expanduserexists	SSHConfigopenlookupProxyCommandssh_confload_system_host_keysset_missing_host_key_policyWarningPolicy)r   r   ssh_config_fileconfr"   host_configs         r   r   z&SSHHTTPAdapter._create_paramiko_client   s   *%..w?",,.<<((2 ))MM ))
 '',,_=77>>/*%%'Do& !

1++h&7&78K,*2*?*?MM.1+' [(.9*.E
+}}$;)>*5f*='  (V{-B.9&.A
+,2=n2M/--/33H4J4J4LM# s   1HHc                 j    | j                   r' | j                   j                  di | j                   y y )Nr!   )rj   r7   r   rQ   s    r   r   zSSHHTTPAdapter._connect   s)    ??#DOO##6doo6 r   c                 ,   | j                   s7t        | j                   | j                  | j                  | j                        S | j
                  j                  5  | j
                  j                  |      }|r|cd d d        S | j                   r*| j                   j                         s| j                          t        | j                   | j                  | j                  | j                        }|| j
                  |<   d d d        |S # 1 sw Y   S xY w)N)rj   r\   rh   r   )
rj   rf   r\   r   r^   r~   lockrp   ri   r   )r   urlproxiesro   s       r   get_connectionzSSHHTTPAdapter.get_connection   s    $??**]]	  ZZ__ 	#::>>#&D	# 	# t'D'D'F$??**]]	D #DJJsO	#" #	#" s   D	A<D		Dc                 p    t         |           | j                  r| j                  j                          y y r   )r   rR   rj   )r   r   s    r   rR   zSSHHTTPAdapter.close   s(    ??OO!!# r   r   )rS   rT   rU   requestsadaptersHTTPAdapter	__attrs__r   DEFAULT_NUM_POOLSDEFAULT_MAX_POOL_SIZEr   r   r   r   rR   rV   rW   s   @r   r}   r}      s]    !!--77 ; I *,"+"="=(>> (N>76$ $r   r}   )r   ru   urllib.parser   requests.adaptersr   r   r-   r   r   r0    docker.transport.basehttpadapterr    r   rr   urllib3.connection_collectionsr   r   
connectionHTTPConnectionrY   connectionpoolHTTPConnectionPoolrf   r}   r!   r   r   <module>r      s         	    <   ,,BB L L^G&&55 ,$(..AA $(N[$_ [$r   