
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
3
;j                 @   s   d dl Z d dlZd dlZd dlZddlmZmZmZmZm	Z	m
Z
 ddlmZ d ZdZdZejjejdZdZG d	d
 d
eZe Zdd Zdd Zdd Zdd Zdd ZdS )    N   )config	constants
http_utils	log_utilsserveridutils)json_loads_nstr   zipv6_preference.json   <   c               @   s,   e Zd Zdd Zedd Zedd ZdS )IPProtoSelectorc             C   sb  t jrtjd dS t jjds.tjd dS t jrBtjd dS t }|dk	rltjdj|r`dnd	 |S d}| j	t j
stjd
 d}n2| j	t jstjd d}ntj rtjd d}|dk	rt| |S | jdd}| jdd}|tkrtjd d}nR|tkrtjd d}n8|tkr8tjd d}n|tkrRtjd d}nd}t| |S )ak  
        Choose ipv6 if it is more suitable.
        Checks order:
        - check config values (it is faster) - eportal setup and FORCE_IPVx
        - then check each proto availability using HEAD requests
        - then check if we have server_id, it means we don't expect an ip license
        - and finally we need to check if there is an ip license
        z,decided to use ipv4 because of config valuesFzkernelcare.comz,decided to use ipv6 because of config valuesTNz%decided to use {0} from on-disk cacheipv6Zipv4z1decided to use ipv4 because ipv6 is not availablez1decided to use ipv6 because ipv4 is not availablez/decided to use ipv4 because server id was found)r   z2decided to use ipv4 because ipv4 license was foundz2decided to use ipv6 because ipv6 license was foundz8decided to use ipv4 because ipv4 trial license was foundz8decided to use ipv6 because ipv6 trial license was found)r   
FORCE_IPV4r   logdebugPATCH_SERVERendswith
FORCE_IPV6_read_cacheformat_is_url_reachablePATCH_SERVER_IPV6r   get_serverid_write_cache_get_cln_licenseCLN_VALID_LICENSECLN_TRIAL_ACTIVE_LICENSE)selfcachedresultZipv4_licenseZipv6_license r    2/usr/libexec/kcare/python/kcarectl/ipv6_support.pyis_ipv6_preferred   sV    













z!IPProtoSelector.is_ipv6_preferredc             C   sb   t j| dd d}yt j|dddd dS  tk
r\ } ztjdj| t| dS d }~X nX d S )	NHEAD)methodauth_string
   F   )timeoutretry_on_500retry_countTz%error during HEAD request to {0}: {1})r   http_requesturlopen	Exceptionr   r   r   str)urlrequester    r    r!   r   `   s    z!IPProtoSelector._is_url_reachablec             C   s   | r
t jnt j}|d }tjtj|ddj }tj|}| sL|j	d rbt
jjdj| tS yt|d S  tk
r   tS X d S )Nz/check.plainF)r)   codezUnexpected CLN response: {0})r   REGISTRATION_URL_IPV6REGISTRATION_URLr   nstrr   r,   readdata_as_dictgetr   kcarelogerrorr   CLN_NO_LICENSEint
ValueError)r   Zbase_urlr/   contentinfor    r    r!   r   k   s    
z IPProtoSelector._get_cln_licenseN)__name__
__module____qualname__r"   staticmethodr   r   r    r    r    r!   r      s   Ar   c              C   s  t jt} | sd S yt| }W n, ttfk
rJ   tjdj| dd d S X t	|t
sntjdj|dd d S |jd}t	|tstjdj|dd d S |jd}t	|tj st	|trtjdj|dd d S tj t| }|d	k p|tkrtjd
j|t d S |S )Nz+ipv6 preference cache: malformed json {0!r}F)	print_msgz/ipv6 preference cache: unexpected payload {0!r}prefer_ipv6z3ipv6 preference cache: unexpected prefer_ipv6 {0!r}tsz)ipv6 preference cache: malformed ts {0!r}r   z;ipv6 preference cache: stale entry (age={0:.0f}s, ttl={1}s))r   try_to_read
CACHE_FILEr	   r=   	TypeErrorr   logwarnr   
isinstancedictr8   boolnumbersZIntegraltimer<   CACHE_TTL_SECONDSr   )r>   datarE   Z	cached_tsZager    r    r!   r      s0    




r   c             C   sl   | t tj d}ytjttj|dd W n: ttfk
rf } zt	j
dj|dd W Y d d }~X nX d S )N)rE   rF   T)
ensure_dirz*failed to write ipv6 preference cache: {0}F)rD   )r<   rO   r   atomic_writerH   jsondumpsOSErrorIOErrorr   rJ   r   )r   rQ   r1   r    r    r!   r      s    r   c               C   s(   yt jt W n tk
r"   Y nX dS )z'Drop the on-disk ipv6 preference cache.N)osunlinkrH   rV   r    r    r    r!   clear_cache   s    rZ   c               C   s   t j rtjS tjS )N)ip_proto_selectorr"   r   r   r   r    r    r    r!   get_patch_server   s    r\   c               C   s   t j rtjS tjS )N)r[   r"   r   r3   r4   r    r    r    r!   get_registration_url   s    r]   i  iQ )rT   rN   rX   rO    r   r   r   r   r   r   py23r	   r   r   r;   pathjoinPATCH_CACHErH   rP   objectr   r[   r   r   rZ   r\   r]   r    r    r    r!   <module>   s"    b'	