
                             S r SSKrSSKrSSKrSSKrSSKrSSKrSSKJ	r
  SSKJr  SSKJr  SSKJr  SSKJr  SrS	rS	rS
rSrSr " S S\R.                  5      r " S S\5      rg)z6Implementation for tunneling through Security Gateway.    N)iap_tunnel_websocket_utils)sg_tunnel_utils)
exceptions)log)	transporti      zX-Resource-KeyzProxy-Authorizationz
User-Agentc                       \ rS rSrSrg)SGConnectionError)    N)__name__
__module____qualname____firstlineno____static_attributes__r       /lib/googlecloudsdk/api_lib/compute/sg_tunnel.pyr
   r
   )   s    r   r
   c                   N    \ rS rSrSr SS jrS rS rS rS r	S r
S	 rS
 rSrg)SecurityGatewayTunnel-   z1Creates the tunnel connection to the destination.c                     XPl         X l        X0l        X@l        Xl        S U l        S U l        SU l        [        R                  " 5       u  U l
        U l        g )NF)_ignore_certs_get_access_token_callback_send_local_data_callback _close_local_connection_callback_target_sock_sending_thread	_stoppingsocket
socketpair_spair_rpair)selftargetaccess_token_callbacksend_local_data_callbackclose_local_connection_callbackignore_certss         r   __init__SecurityGatewayTunnel.__init__0   sP     &&;#%=",K)LDJDDN  &002DKr   c                 $    U R                  5         g )N)Closer$   s    r   __del__SecurityGatewayTunnel.__del__H   s    JJLr   c                    [         R                  " U R                  5        [        R                  " U R
                  5      nU R
                  (       a  [        R                  " US9nO[        R                  " US9n[         R                  " U R                  R                  5      u  p4[        R                  R                  X4US9nSR                  U R                  R                  U R                  R                   5      n0 n[#        U R$                  5      (       a&  SR                  U R%                  5       5      U[&        '   U R                  R(                  (       a\  [         R*                  " U R                  R,                  U R                  R.                  U R                  R0                  5      U[2        '   O[[         R4                  " U R                  R,                  U R                  R.                  U R                  R0                  5      U[2        '   [6        R8                  " 5       U[:        '   [<        R>                  " SU5        URA                  SXgS9  [        R                  RC                  URD                  SUS9nURG                  5       u  pnU
[        R                  RH                  :w  a%  [<        RJ                  " S	X5        [M        S
U-   5      eURD                  U l'        U RN                  RQ                  S5        [<        RR                  " SU5        [T        RV                  " U RX                  S9U l-        SU RZ                  l.        U RZ                  R_                  5         g)z<Starts a tunnel to the destination through Security Gateway.)cafile)contextz{}:{}z	Bearer {}zSending headers: %sCONNECT)headers)methodurlz.Connection request status [%s] with reason: %sz7Security Gateway failed to connect to destination url: FzConnected to [%s])r%   TN)0sg_utilsValidateParametersr   	iap_utilsCheckCACertsFiler   ssl_create_unverified_contextcreate_default_contextGetProxyHostPorturl_overridehttpclientHTTPSConnectionformathostportcallabler   PROXY_AUTH_HEADERuse_dest_groupGenerateDestGroupResourcePathprojectregionsecurity_gatewayRESOURCE_KEY_HEADER#GenerateSecurityGatewayResourcePathr   MakeUserAgentStringUSER_AGENT_HEADERr   debugrequestHTTPResponsesock_read_statusOKerrorr
   r   setblockinginfo	threadingThread_RunReceiver   daemonstart)r$   ca_certsssl_ctx
proxy_host
proxy_portconndst_addrr5   resp_codereasons               r   InitiateConnection(SecurityGatewayTunnel.InitiateConnectionK   s|    -))$*<*<=H..h?g**(;g%66!!#J ;;&&zw&OD~~dll//1B1BCHG//00#.#5#5

)
)
+$-g ||""%-%K%K
,,


,,


,,
'
'&)g!" 
6
6ll""ll!!ll++- !"
 "+!>!>!@GII#W-LLHL6;;##DIIiX#ND))+Qft{{~~	ii@$O
Ch
NP P DJJJ5!HH (+$++43C3CDD"&D r   c                     U R                   $ )z?Signals to parent thread that this connection should be closed.)r   r.   s    r   
ShouldStop SecurityGatewayTunnel.ShouldStop|   s     >>r   c                    U R                   (       dF  U R                  (       a5  U R                  R                  S5        U R                  R	                  5         U R                  5         U R                  c  g U R                  R                  [        R                  5        U R                  R                  5         g! [        R                  [        4 a!  n[        R                  " SU5         SnAgSnAff = f)z8Attempts to close both the local and tunnel connections.   0Nz3Failed to close connection to remote endpoint: [%s])r   r   r"   sendjoinr   r   shutdownr    	SHUT_RDWRcloserX   EnvironmentErrorr   rR   )r$   es     r   r-   SecurityGatewayTunnel.Close   s    >>d22 kkt
!))+zzJ jj&**+
jjLL*+ J	iiEqIIJs   7AB; ;C6C11C6c                    [        U5      n[        R                  " 5       [        R                  :X  a4  [        R
                  R                  5       R                  SX!SS 4-  5        SnX2:  a&   X0R                  R                  U5      -  nX2:  a  M%  gg! [        R                  [        R                  [        4 a+    [        R                  " SU R                  /S[        5         N\f = f)z:Attempts to send all bytes in data to the remote endpoint.z'DEBUG: SEND data_len [%d] data[:20] %r
N   r   r   )lenr   GetVerbosityloggingDEBUGerrGetConsoleWriterStreamwriter   rq   r<   SSLWantWriteErrorSSLWantReadErrorBlockingIOErrorselectSEND_TIMEOUT_SECONDS)r$   datadata_len	sent_datas       r   SendSecurityGatewayTunnel.Send   s    4yH
W]]*	gg$$&,,
4s)7L
LNI

BZZ__T**	 
 ##S%9%9?K Bb4::,,@ABs   /B AC)(C)c                     U R                   (       Gd@  U R                  (       d  GO-U R                  //nU R                  R                  5       (       d3  [        R                  " U R                  U R                  /SS[
        5      nUS    H  nX R                  L a	  SU l           OX R                  L d  M*  U R                  5       u  p4[        R                  " 5       [        R                  :X  a4  [        R                  R                  5       R                  SXCSS 4-  5        US:  d  M  U R                  U5        M     U R                   (       d  GM@  SU l         g! SU l         f = f)z7Receives server data and sends to the local connection.r   r   Tz'DEBUG: RECV data_len [%d] data[:20] %r
Nrz   )r   r   pendingr   r#   RECV_TIMEOUT_SECONDS_Readr   r|   r}   r~   r   r   r   r   )r$   readysr   r   s        r   r]   !SecurityGatewayTunnel._RunReceive   s   zz
**zz!!##--T[[ 92r 46%qA++!DN**_!ZZ\ND!W]]2gg,,.44<Sb	@+ +, 1},,T2  * dntdns   B0E 6A1E +&E 	E%c                     Sn U R                   R                  [        5      nU[        U5      4$ ! [        R                  [        R
                  [        4 a    US4s $ f = f)zCReads MAX_BYTES_SOCKET_READ bytes of data from the remote endpoint.r   )r   recvMAX_BYTES_SOCKET_READr<   r   r   r   r{   )r$   r   s     r   r   SecurityGatewayTunnel._Read   s\    DZZ__23d T? !!3#7#7I 2Xos   0 -A A )
r   r   r   r#   r   r   r   r"   r   r   N)F)r   r   r   r   __doc__r*   r/   rj   rm   r-   r   r]   r   r   r   r   r   r   r   -   s5    9 30/!b
J(B4r   r   )r   http.clientrA   r}   r   r    r<   r[   googlecloudsdk.api_lib.computer   r:   r   r8   googlecloudsdk.corer   r   r   r   r   r   rN   rH   rQ   Errorr
   objectr   r   r   r   <module>r      ss    =     
  R F * # )    & )   
(( VF Vr   