
                         0   d Z ddlmZ ddlmZ ddlmZ ddlZddlZddlmZ ddlm	Z	 ddlm
Z
 dd	lmZ dd
lmZ ddlmZ ddlmZ ddlZddlZddlmZ ddlmZ ddlZdZ G d dej.                        Zd Z G d dej4                        Zd Zd Zd Zy)z:A module for diagnosing common network and proxy problems.    )absolute_import)division)unicode_literalsN)config)http)
properties)requests)
check_base)diagnostic_base)http_proxy_setup)http_client)urllib<   c                   ,     e Zd ZdZ fdZ fdZ xZS )NetworkDiagnosticz1Diagnose and fix local network connection issues.c                 H    d}t         t        |   |dt               g       y )NzENetwork diagnostic detects and fixes local network connection issues.zNetwork diagnostic)introtitle	checklist)superr   __init__ReachabilityChecker)selfr   	__class__s     :lib/googlecloudsdk/core/diagnostics/network_diagnostics.pyr   zNetworkDiagnostic.__init__-   s.    E	
T+/&() , +    c                 J    t        j                         syt        |          S NT)r   IsDefaultUniverser   	RunChecks)r   r   s    r   r    zNetworkDiagnostic.RunChecks4   s     '')7r   )__name__
__module____qualname____doc__r   r    __classcell__)r   s   @r   r   r   *   s    9+ r   r   c                      g d} t         j                  j                  j                  j	                         xs t
        j                  j                  }| j                  d |j                  d      D               | S )zReturns a list of hosts whose reachability is essential for the Cloud SDK.

  Returns:
    A list of urls (str) to check reachability for.
  )zhttps://accounts.google.comz<https://cloudresourcemanager.googleapis.com/v1beta1/projectsz.https://www.googleapis.com/auth/cloud-platformc              3   v   K   | ]1  }t         j                  j                  |      j                  d v r| 3 yw))r   httpsN)r   parseurlparsescheme).0us     r   	<genexpr>zDefaultUrls.<locals>.<genexpr>G   s7      G1A&&q)004EE 1s   79,)	r   VALUEScomponent_managersnapshot_urlGetr   INSTALLATION_CONFIGextendsplit)urlsdownload_urlss     r   DefaultUrlsr9   ;   sm    
<$ $$66CCGGI ;--:: ++ G,,S1 G G	+r   c                   (    e Zd ZdZed        ZddZy)r   z5Checks whether the hosts of given urls are reachable.c                      y)Nznetwork connection )r   s    r   issuezReachabilityChecker.issueO   s    r   Nc                    |
t               }g }|D ]!  }t        |      }|s|j                  |       # |D ]!  }t        |      }|s|j                  |       # |r8t	        ||      }t        j                  d||      }t        j                  }||fS dj                  |rdnd      }	t        j                  d|sdn|		      }|dfS )
a  Run reachability check.

    Args:
      urls: iterable(str), The list of urls to check connection to. Defaults to
        DefaultUrls() (above) if not supplied.
      first_run: bool, True if first time this has been run this invocation.

    Returns:
      A tuple of (check_base.Result, fixer) where fixer is a function that can
        be used to fix a failed check, or  None if the check passed or failed
        with no applicable fix.
    NF)passedmessagefailureszReachability Check {0}.r?   z
now passesTzNo URLs to check.)r?   r@   )
r9   CheckURLHttplib2appendCheckURLRequestsConstructMessageFromFailuresr
   Resultr   ChangeGcloudProxySettingsformat)
r   r7   	first_runrA   urlfailfail_messageresultfixerpass_messages
             r   CheckzReachabilityChecker.CheckS   s     |]dHc"d	  c"d	 
 1(IFl  |*24f88eU],33	H4@BLd&* 5H0<>F4<r   r   )r!   r"   r#   r$   propertyr=   rP   r<   r   r   r   r   L   s    =   (r   r   c                 h   	 t        j                  t              j                  | d       y # t        j
                  t        j                  t        j                  t        j                  t        j                  f$ r3}dj                  | |      }t        j                   ||      cY d }~S d }~ww xY w)NtimeoutGET)methodzhttplib2 cannot reach {0}:
{1}
r@   	exception)r   Http_NETWORK_TIMEOUTrequestr   HTTPExceptionsocketerrorsslSSLErrorhttplib2HttpLib2Errorsocks	HTTPErrorrH   r
   FailurerJ   errmsgs      r   rB   rB   ~   s    :II&'//E/B

#
#V\\3<<

 
 %//
3 :
-
4
4SCcS99	:s   +. AB1>(B,&B1,B1c                     	 t        j                  t              j                  d|        y # t        j
                  j                  $ r3}dj                  | |      }t        j                  ||      cY d }~S d }~ww xY w)NrS   rU   zrequests cannot reach {0}:
{1}
rW   )
core_requests
GetSessionrZ   r[   r	   
exceptionsRequestExceptionrH   r
   re   rf   s      r   rD   rD      sg    :%56>>ucJ				-	- :
-
4
4SCcS99:s   *- A=
(A82A=8A=c                     dj                  |rdnd      }| D ]   }|dj                  |j                        z  }" |r|dz  }|S )z<Constructs error messages along with diagnostic information.zReachability Check {0}.
failedzstill does not passz    {0}
zFNetwork connection problems may be due to proxy or firewall settings.
)rH   r@   )rA   rI   r@   failures       r   rE   rE      sV    '..9x/DF'g{!!'//22G  ' (G 
.r   )r$   
__future__r   r   r   r]   r_   googlecloudsdk.corer   r   r   r	   rj   googlecloudsdk.core.diagnosticsr
   r   r   ra   	six.movesr   r   rc   rZ   
Diagnosticr   r9   Checkerr   rB   rD   rE   r<   r   r   <module>rw      s}     A &  '  
 & $ * 9 6 ; <   !   22 ""/*,, /d::
r   