a íÕ;jÃã@s®ddlZddlZddlZddlZddlmZmZmZmZm Z m Z ddl m Z dZdZdZej ejd¡ZdZGdd „d eƒZeƒZd d „Zd d „Zdd„Zdd„Zdd„ZdS)éNé)ÚconfigÚ constantsÚ http_utilsÚ log_utilsÚserveridÚutils)Újson_loads_nstrézipv6_preference.jsoni€Qc@s,eZdZdd„Zedd„ƒZedd„ƒZdS)ÚIPProtoSelectorcCsbtjrt d¡dStj d¡s.t d¡dStjrBt d¡dStƒ}|durlt d |r`dnd ¡¡|Sd}|  tj ¡sŒt d ¡d}n2|  tj¡s¨t d ¡d}nt   ¡r¾t d ¡d}|durÒt |ƒ|S|jdd }|jdd }|tkrt d¡d}nR|tkrt d¡d}n8|tkr8t d¡d}n|tkrRt d¡d}nd}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 valuesFzkernelcare.comz,decided to use ipv6 because of config valuesTNz%decided to use {0} from on-disk cacheÚipv6Zipv4z1decided 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_preferredsV                    z!IPProtoSelector.is_ipv6_preferredc Csjtj|ddd}ztj|ddddWdStyd}z$t d |t|ƒ¡¡WYd}~dSd}~00dS) NÚHEAD)ÚmethodÚ auth_stringé Fé)ÚtimeoutÚ retry_on_500Ú retry_countTz%error during HEAD request to {0}: {1})rÚ http_requestÚurlopenÚ ExceptionrrrÚstr)ÚurlÚrequestÚerrrr`sz!IPProtoSelector._is_url_reachablecCsˆ|r tjntj}|d}t tj|dd ¡¡}t |¡}|rH|  d¡s^t j   d  |¡¡tSzt|dƒWSty‚tYS0dS)Nz /check.plainF)r'ÚcodezUnexpected CLN response: {0})rÚREGISTRATION_URL_IPV6ÚREGISTRATION_URLrÚnstrrr*ÚreadÚ data_as_dictÚgetrÚkcarelogÚerrorrÚCLN_NO_LICENSEÚintÚ ValueError)r Zbase_urlr-ÚcontentÚinforrrrks  z IPProtoSelector._get_cln_licenseN)Ú__name__Ú __module__Ú __qualname__r Ú staticmethodrrrrrrr s A r c Cst t¡}|sdSz t|ƒ}Wn,ttfyJtjd |¡ddYdS0t |t ƒsntjd |¡dddS|  d¡}t |t ƒsštjd |¡dddS|  d¡}t |t jƒrºt |t ƒrÒtjd |¡dddSt ¡t|ƒ}|d ksô|tkr t d  |t¡¡dS|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}rz;ipv6 preference cache: stale entry (age={0:.0f}s, ttl={1}s))rÚ try_to_readÚ CACHE_FILEr r;Ú TypeErrorrÚlogwarnrÚ isinstanceÚdictr6ÚboolÚnumbersZIntegralÚtimer:ÚCACHE_TTL_SECONDSr)r<ÚdatarDZ cached_tsZagerrrrƒs0      rc Csn|tt ¡ƒdœ}ztjtt |¡ddWn<ttfyh}z t j d  |¡ddWYd}~n d}~00dS)N)rDrET)Ú ensure_dirz*failed to write ipv6 preference cache: {0}FrB) r:rNrÚ atomic_writerGÚjsonÚdumpsÚOSErrorÚIOErrorrrIr)rrPr/rrrrªs þrcCs&zt t¡Wnty Yn0dS)z'Drop the on-disk ipv6 preference cache.N)ÚosÚunlinkrGrUrrrrÚ clear_cache¶s rYcCst ¡rtjStjS©N)Úip_proto_selectorr rrrrrrrÚget_patch_server¿sr\cCst ¡rtjStjSrZ)r[r rr1r2rrrrÚget_registration_urlÄsr])rSrMrWrNÚrrrrrrÚpy23r ÚOptionalrrr9ÚpathÚjoinÚ PATCH_CACHErGrOÚobjectr r[rrrYr\r]rrrrÚs"  b'