
    bi                     \   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Zd dlZddlmZ ddlm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   )	constants   )BaseHTTPAdapterc                   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     S/home/cdr/jupyterlab/.venv/lib/python3.12/site-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                  ||t        j                  t        j                  |	      | _        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)envstdoutstdin
preexec_fn)r   r   r   r   IS_WINDOWS_PLATFORMdictosenvironpop
subprocessPopen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(&$$??//#%	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.sendallG   s    Dr   c                 $    | j                  |      S r   r@   rA   s     r   sendzSSHSocket.sendJ   s    {{4  r   c                 z    | j                   st        d      | j                   j                  j                  |      S r7   )r   r9   r'   read)r   ns     r   recvzSSHSocket.recvM   s6    yy > ? ?yy$$Q''r   c                     | j                   s| j                          | | j                   j                  _        | j                   j                  S r   )r   r5   r'   channel)r   modes     r   makefilezSSHSocket.makefileS   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(   r8   r:   r;   	terminater   s    r   closezSSHSocket.closeZ   sV    yyDIIOO22		g&				r   )__name__
__module____qualname__r   r5   r>   rB   rD   rH   rL   rP   __classcell__r   s   @r   r   r      s+    %:!( 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_transportrZ   ssh_host)r   r[   rZ   r   r   s       r   r   zSSHConnection.__init__c   s2     	 	
 +r   c                 F   | j                   rN| j                   j                         }|j                  | j                         |j	                  d       || _        y t        | j                        }|j                  | j                         |j                          || _        y )Nr   )	r[   open_session
settimeoutrZ   exec_commandr   r\   r5   sock)r   ra   s     r   r5   zSSHConnection.connectk   sw    %%224DOODLL)89 		 T]]+DOODLL)LLN	r   )N<   N)rQ   rR   rS   r   r5   rT   rU   s   @r   rW   rW   b   s    
r   rW   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 )NrY   )rZ   maxsize)r   r   r[   rZ   get_transportr\   )r   
ssh_clientrZ   rf   r   r   s        r   r   zSSHConnectionPool.__init__{   sH    ' 	 	
 "!+!9!9!;Dr   c                 X    t        | j                  | j                  | j                        S r   )rW   r[   rZ   r\   rO   s    r   	_new_connzSSHConnectionPool._new_conn   s    T//t}}MMr   c                 d   d }	 | j                   j                  | j                  |      }|xs | j                         S # t        $ r&}t        j
                  j                  | d      |d }~wt        j                  $ r0 | j                  r!t        j
                  j                  | d      d Y w xY w)N)blockrZ   zPool is closed.z>Pool reached maximum size and no more connections are allowed.)poolgetrl   AttributeErrorurllib3
exceptionsClosedPoolErrorqueueEmptyEmptyPoolErrorrj   )r   rZ   connaes       r   	_get_connzSSHConnectionPool._get_conn   s    	99==tzz7=CD 't~~''  	V$$44T;LMSUU{{ 	zz((77/ 	 	s   '? 	B/!A))AB/.B/)Nrb   
   N)rQ   rR   rS   schemer   rj   rx   rT   rU   s   @r   rd   rd   x   s    FN(r   rd   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)poolsrZ   rh   
ssh_paramsmax_pool_sizerb   Fc                    d | _         |s!| j                  |       | j                          || _        |j	                  d      r|t        d      d  | _        || _        || _        t        |d       | _	        t        | -          y )Nzssh://c                 "    | j                         S r   )rP   )ps    r   <lambda>z)SSHHTTPAdapter.__init__.<locals>.<lambda>   s    QWWY r   )dispose_func)rh   _create_paramiko_client_connectr\   
startswithlenrZ   r   RecentlyUsedContainerr}   r   r   )r   base_urlrZ   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*                  |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                  j/                  t	        j0                                y # 1 sw Y   xY w)Nparamiko)hostnamer   usernamez~/.ssh/configproxycommandra   r   r   r   r   identityfilekey_filename)logging	getLoggersetLevelWARNINGr   	SSHClientrh   urllibparseurlparser   r   r   r~   r,   path
expanduserexists	SSHConfigopenlookupProxyCommandload_system_host_keysset_missing_host_key_policyRejectPolicy)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*?*?/+' [(.9*.E
+}}$;)>*5f*='  (V{-B.9&.A
+,2=n2M/--/33H4I4I4KL# s   1HHc                 j    | j                   r' | j                   j                  di | j                   y y )Nr"   )rh   r5   r~   rO   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)rh   rZ   rf   r   )
rh   rd   rZ   r   r\   r}   lockrn   rg   r   )r   urlproxiesrm   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   rP   rh   )r   r   s    r   rP   zSSHHTTPAdapter.close   s(    ??OO!!# r   r   )rQ   rR   rS   requestsadaptersHTTPAdapter	__attrs__r   DEFAULT_NUM_POOLSDEFAULT_MAX_POOL_SIZEr   r   r   r   rP   rT   rU   s   @r   r|   r|      s]    !!--77 ; I *,"+"="=(>> (M>76$ $r   r|   )r   r,   rs   r   r   r/   urllib.parser   r   requests.adaptersr   rp   urllib3.connection r   basehttpadapterr   _collectionsr   r   
connectionHTTPConnectionrW   connectionpoolHTTPConnectionPoolrd   r|   r"   r   r   <module>r      s     	           ,,,BB K K\G&&55 ,$(..AA $(N[$_ [$r   