B
    wZ[t                 @   s   d dl Z d dlZd dlmZ ejZejZejjZejj	Z
dgZdddge Zdadadd	d
ZdddZdddZdddZe jdddZdS )    N)urllibzwww.github.iozdata.astropy.orgzastropy.stsci.eduzwww.astropy.orgFP   c             C   sL   yt dd t| |D }W n tjk
r<   t g }Y nX ||  |S )z_
    Obtain all the IPs, including aliases, in a way that supports
    IPv4/v6 dual stack.
    c             S   s   g | ]}|d  d qS )r    ).0sr   r   Alib/python3.7/site-packages/pytest_remotedata/disable_internet.py
<listcomp>!   s    z%_resolve_host_ips.<locals>.<listcomp>)setsocketZgetaddrinfoZgaierroradd)hostnameZportZipsr   r   r   _resolve_host_ips   s    
r   c                s    fdd}|S )z
    Wraps ``original_function``, which in most cases is assumed
    to be a `socket.socket` method, to raise an `IOError` for any operations
    on non-local AF_INET sockets.

    Allowing Astropy data also automatically allow GitHub data.
    c        
         sl  t | d tjrP| d jtjtjfkr0| |S | d d }d}tdddg}nDt | d trnt| d dksx| |S | d d }d}tddg} rx<tD ]}|	t
|}qW n rxtD ]}|	t
|}qW t }t }|||fkr6d}t|g}|| | d f}	| d | |	f | |d d   } nt
|}t||@ dkrZ| |S td|d S )Nr      Z	localhostz	127.0.0.1z::1   zwAn attempt was made to connect to the internet by a test that was not marked `remote_data`. The requested host was: {0})
isinstancer   ZfamilyZAF_INETZAF_INET6r
   tuplelenASTROPY_HOSTSunionr   GITHUB_HOSTSZgethostnameZgetfqdnIOErrorformat)
argskwargsZhostZaddr_argZvalid_hostsZ
valid_hostr   ZfqdnZhost_ipsZnew_addr)allow_astropy_dataallow_github_dataoriginal_functionr   r   new_function6   s:    




$
z(check_internet_off.<locals>.new_functionr   )r   r   r   r   r   )r   r   r   r   check_internet_off,   s    
0r   c             C   s   t rdS da d}| rtd tj atji }tj|}tj| tt	||dt
_tt||dt
j
_tt||dt
j
_t
S )a1  
    Disable internet access via python by preventing connections from being
    created using the socket module.  Presumably this could be worked around by
    using some other means of accessing the internet, but all default python
    modules (urllib, requests, etc.) use socket [citation needed].
    NTzInternet access disabled)r   r   )INTERNET_OFFprintr   requestZbuild_opener_orig_openerZProxyHandlerinstall_openerr   socket_create_connectionr   create_connectionsocket_bindbindsocket_connectconnect)verboser   r   Z__tracebackhide__Zno_proxy_handleropenerr   r   r   turn_off_interneti   s(    

r-   c             C   s>   t sdS da | rtd tjt tt_t	tj_
ttj_tS )zL
    Restore internet access.  Not used, but kept in case it is needed.
    NFzInternet access enabled)r    r!   r   r"   r$   r#   r%   r   r&   r'   r(   r)   r*   )r+   r   r   r   turn_on_internet   s    r.   c          	   c   s.   t }t| d z
dV  W d|s(t| d X dS )a7  Context manager to temporarily disable internet access (if not already
    disabled).  If it was already disabled before entering the context manager
    (i.e. `turn_off_internet` was called previously) then this is a no-op and
    leaves internet access disabled until a manual call to `turn_on_internet`.
    )r+   N)r    r-   r.   )r+   Zalready_disabledr   r   r   no_internet   s    

r/   )r   )FF)FFF)F)F)
contextlibr   Z	six.movesr   Zsocket_originalr&   r%   r(   r'   r*   r)   r   r   r    r#   r   r   r-   r.   contextmanagerr/   r   r   r   r   <module>   s&   
 
< 
)
