9MjdZddlZddlZddlZddlZddlmZddlm 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#rrceZdZUdZeed<ddZedefdZ ede fdZ ede fdZ e jd e ddfd Z e jdd Z d eddfd Zededee fdZddZddZdS)NginxUserCachezc Class which implements reading/writing the user's cache status from the local storage usernamereturnNc~|j}t|trd|vs|dvsd|vrtd|dSdS)N/).z..z'Invalid username for nginx cache path: )r! isinstancestr ValueError)selfnames r __post_init__zNginxUserCache.__post_init__5s`} dC  QcTkkT[5H5HFVZNNOtOOPP P Q QNNrc`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)r* global_file user_file user_settingglobal_settings rcurrent_statuszNginxUserCache.current_status@s7 LDMLLL w~~k** "> ! 7>>) $ $ +::9EEL'\***88EE  %(( (}rc(|jtjuS)z"Check if cache is enabled for user)r6rr)r*s r is_enabledzNginxUserCache.is_enabled[s"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_state`s  K5FFF 8J[(7 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 R R R NNMq Q Q Q Q Q Q Q Q K K K KKI$-II J J J J J K%.)))s3> 2 >6>6> BA((/BBvaluec ttd5}|||j<ddddS#1swxYwYdS#t$r&}td|Yd}~dSd}~wwxYw)zq Save state of nginx cache manipulations -- initial cache setting and active tasks count Tr:Nz+Failed to save state of nginx cache with %sr rr!r<r=r>)r*rErBrCs rrDzNginxUserCache.saved_stateqs  M5FFF 9J[38!$-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 Tr:Nz-Failed to delete state of nginx cache with %srGrAs rrDzNginxUserCache.saved_state}s  O5FFF 5J[%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)outerr)flagextraNz'Failed to set nginx cache as {} with {})rQ)r!rE subprocessrunCalledProcessErrorr=r@args returncodestdoutstderrr r(OSErrorr)SubprocessError_formatr+)r*rIcmdrCs rsetzNginxUserCache.sets/3WdmMmZeZkMmMmn  N3dd K K K K K K, ^ ^ ^ KKNPQPVXYXdfgfnpqpx   CFF!(STS[:\:\]]] ]Z%?@   ;BB3{GWCXCXZ]^_Z`Z`aabbir  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)rRzFailed to read file %s with %s) openjsonloadgetJSONDecodeErrorr=errorr(readIOErrorrZ)r`_fdatarCs rr1z)NginxUserCache.get_cache_status_from_files Mh /2/9R==D 88I..  / / / / / / / /+nnnLL!;Xs1vvV`bdbibibkbkUlLmmmmmmmmnm  / / / / / / / / / / / / / / / / / /! M M M LL98SVV L L L L L L L L L MsgB>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 rDr8r_rrrrr 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)rDrrrrr_r ros rrestorezNginxUserCache.restores! >U] * *   1 $  "a''HHU]++++ D$!sA A$#A$)r"N)rrrrr(rr,propertyrr6boolr8rrDsetterdeleterr_ staticmethodrr1rqrsrrrr r ,s MMM Q Q Q QX44D444X4*Z***X*  M M M M M M O O O O u      MS MXd^ M M M\ M""""4""""""rr )rreloggingos.pathr.rS dataclassesrenumrtypingrxrayrr\ constantsrr exceptionsr utilsr getLoggerr=rrr rrrrst !!!!!!JJJJJJJJ!!!!!!  = ) )D   j"j"j"j"j"j"j" j"j"j"r