fj{dZddlZddlZddlZddlZddlmZddlm Z ddl m Z ddl m ZddlmZmZdd lmZdd lmZejd ZGd d e ZeGddZeGddZdS)zL This module contains utilities for ea-nginx cache disabling during tracing N) dataclass)Enum)Optional)gettext)nginx_cache_stat_storageallow_disable_nginx_cache) XRayError) dbm_storage nginx_utilsceZdZdZdZdZdS)CachezCache statuses enumrrN)__name__ __module__ __qualname____doc__ENABLEDDISABLEDP/opt/cloudlinux/venv/lib64/python3.11/site-packages/xray/internal/nginx_utils.pyrrsGHHHrrc,eZdZUdZeed<dZeed<dS) SavedStateinitialr active_tasksN)rrrrr__annotations__rintrrrrr"s0 F NNNL#rrc eZdZUdZeed<edefdZede fdZ ede fdZ e j de ddfd Z e jdd Z d eddfd Zed edee fdZddZddZdS)NginxUserCachezc Class which implements reading/writing the user's cache status from the local storage usernamereturnc`d}d|jd}tj|s tjStj|r&||}|t |S||}|t |StjS)z Detect if cache is enabled or disabled for user using cPanel's guidelines https://docs.cpanel.net/knowledge-base/web-services/nginx-with-reverse-proxy/#third-party-integration z/etc/nginx/ea-nginx/cache.jsonz/var/cpanel/userdata/z/nginx-cache.json)r!ospathisfilerrget_cache_status_from_filer)self global_file user_file user_settingglobal_settings rcurrent_statuszNginxUserCache.current_status1s7 LDMLLL w~~k** "> ! 7>>) $ $ +::9EEL'\***88EE  %(( (}rc(|jtjuS)z"Check if cache is enabled for user)r-rr)r(s r is_enabledzNginxUserCache.is_enabledLs"em33rcl ttd5}||jcdddS#1swxYwYn_#t$r%}td|Yd}~n5d}~wt $r%td|jYnwxYwttj S)zu Get saved state (initial cache setting and active tasks count) of nginx cache manipulations T is_shelveNz0Failed to get saved state of nginx cache with %szNo nginx cache entry found for ) r rr! RuntimeErrorloggerwarningKeyErrorinforrrr(_nginx_cache_states r saved_statezNginxUserCache.saved_stateQs+  K5'+--- 80A(7 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8    NNN         K K K KKI$-II J J J J J K%.)))s3> 2 >6>6> BA((/BBvalueNc ttd5}|||j<ddddS#1swxYwYdS#t$r&}td|Yd}~dSd}~wwxYw)zq Save state of nginx cache manipulations -- initial cache setting and active tasks count Tr1Nz+Failed to save state of nginx cache with %sr rr!r3r4r5)r(r<r9r:s rr;zNginxUserCache.saved_stateds  M5'+--- 90A38!$-0 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 M M M NNH! L L L L L L L L L Ms-= 0 =4=4= A-A((A-c ttd5}||j=ddddS#1swxYwYdS#t$r&}td|Yd}~dSd}~wwxYw)z Delete the record of state of nginx cache manipulations -- initial cache setting and active tasks count Tr1Nz-Failed to delete state of nginx cache with %sr>r8s rr;zNginxUserCache.saved_stateqs  O5'+--- 50A%dm4 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 O O O NNJA N N N N N N N N N Os-; . ;2;2; A+A&&A+ enum_memberc (dd|jd|jg} tj|dddd S#tj$rc}t d|j|j|j |j tt|d|j |j d d }~wtttjf$rV}tt!d t|jt|d d }~wwxYw) z Enable or disable cache for userz"/usr/local/cpanel/scripts/ea-nginxcachez --enabled=T)checktextcapture_outputz;+<>>@ N3d*. 0 0 0 0 0 0, F F F KKN ah : : :CFF*+(18"D"DFFF FZ%?@   ;BB3{GWCXCXZ]^_Z`Z`aabb     s"/DABD;AD  Dfilenamec   t|5} tj|}|dcdddS#tj$rI}t d|t|d|iYd}~nd}~wwxYw ddddS#1swxYwYdS#ttf$r4}t d|t|Yd}~dSd}~wwxYw)z%Get the 'enabled' key from given fileenabledNzInvalid JSON from %s: %scontents)rIzFailed to read file %s with %s) openjsonloadgetJSONDecodeErrorr4errorrQreadIOErrorrR)rZ_fdatar:s rr'z)NginxUserCache.get_cache_status_from_files +h /2/9R==D  88I.. / / / / / / / /+@@@LL!;!)3q66(2BGGII'>!@@@@@@@@@@  / / / / / / / / / / / / / / / / / /! + + + LL9!3q66 + + + + + + + + + +sgB>B1AB1 B>B ?BB1B  B1$ B>1B55B>8B59B>>D)C>>Dc tsdS|j}|jrB |tjtj|_n#t$rYnwxYw|jtjur|xj dz c_ ||_dS)z If cache is enabled for user, disable it. Save retrieved cache status. For ENABLED initial status increment number of active tasks. Skip disabling cache if it is disallowed in constants file Nr) r r;r/rYrrrrr rr(_states rdisablezNginxUserCache.disables)  F! ? / /((("'     >U] * *   1 $  !sA AAc|j}|jtjurP|xjdzc_|jdkr5 |tjn#t $rYnwxYw|`dS||_dS)a For ENABLED initial status decrement number of active tasks. Restore the cache state according to saved status: - enable if it was enabled and number of active tasks <= 0, - do nothing if it was enabled, but number of active tasks > 0, - do nothing if it was disabled. Clear the record if enable has taken place. Skip operation if cache disabling is disallowed in constants file rrN)r;rrrrrYr ris rrestorezNginxUserCache.restores! >U] * *   1 $  "a''HHU]++++ D$!sA A$#A$)r"N)rrrrrQrpropertyrr-boolr/rr;setterdeleterrY staticmethodrr'rkrmrrrr r )szMMM X44D444X4*Z***X*$ M M M M M M O O O O u     $+S+Xd^+++\+ """"4""""""rr )rr_loggingos.pathr$rJ dataclassesrenumrtypingrxrayrrU constantsrr exceptionsr utilsr getLoggerr4rrr rrrr}st !!!!!!JJJJJJJJ!!!!!!  = ) )D     h"h"h"h"h"h"h" h"h"h"r