HEX
Server: LiteSpeed
System: Linux ws4.angoweb.net 5.14.0-611.13.1.el9_7.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Dec 11 04:57:59 EST 2025 x86_64
User: tswangoe (2287)
PHP: 8.1.33
Disabled: show_source, system, shell_exec, passthru, exec, phpinfo, popen, proc_open
Upload Files
File: //opt/alt/python37/lib/python3.7/site-packages/xray/internal/__pycache__/nginx_utils.cpython-37.pyc
B

�*Ud7�@s�dZddlZddlZddlZddlZddlmZddlm	Z	ddl
mZddlm
Z
mZddlmZdd	lmZe�d
�ZGdd�de	�ZeGd
d�d��ZeGdd�d��ZdS)zL
This module contains utilities for ea-nginx cache disabling during tracing
�N)�	dataclass)�Enum)�Optional�)�nginx_cache_stat_storage�allow_disable_nginx_cache)�	XRayError)�dbm_storage�nginx_utilsc@seZdZdZdZdZdS)�CachezCache statuses enumrrN)�__name__�
__module__�__qualname__�__doc__�ENABLED�DISABLED�rr�J/opt/alt/python37/lib/python3.7/site-packages/xray/internal/nginx_utils.pyrsrc@s&eZdZUdZeed<dZeed<dS)�
SavedState��initialr�active_tasksN)rr
rrr�__annotations__r�intrrrrr s
rc@s�eZdZUdZeed<eed�dd��Zee	d�dd��Z
eed�dd	��Zej
ed
d�dd	��Zejd
d�d
d	��Zed
d�dd�Zeeee	d�dd��Zd
d�dd�Zd
d�dd�Zd
S)�NginxUserCachezc
    Class which implements reading/writing the user's cache status
    from the local storage
    �username)�returncCsjd}d|j�d�}tj�|�s$tjStj�|�rJ|�|�}|dk	rJt|�S|�|�}|dk	rdt|�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.jsonN)r�os�path�isfilerr�get_cache_status_from_filer)�selfZglobal_fileZ	user_fileZuser_settingZglobal_settingrrr�current_status/s

zNginxUserCache.current_statuscCs|jtjkS)z"Check if cache is enabled for user)r"rr)r!rrr�
is_enabledJszNginxUserCache.is_enabledc
Cs�y"ttdd��}||jSQRXWnRtk
rP}zt�d|�Wdd}~XYn&tk
rtt�d|j���YnXtt	j
�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�RuntimeError�logger�warning�KeyError�inforrr)r!�_nginx_cache_stat�errr�saved_stateOs
zNginxUserCache.saved_stateN)�valuerc
CsZy&ttdd��}|||j<WdQRXWn.tk
rT}zt�d|�Wdd}~XYnXdS)zq
        Save state of nginx cache manipulations -- initial cache setting
        and active tasks count
        T)r$Nz+Failed to save state of nginx cache with %s)r	rrr%r&r')r!r-r*r+rrrr,bs
c
CsXy$ttdd��}||j=WdQRXWn.tk
rR}zt�d|�Wdd}~XYnXdS)z�
        Delete the record of state of nginx cache manipulations --
        initial cache setting and active tasks count
        T)r$Nz-Failed to delete state of nginx cache with %s)r	rrr%r&r')r!r*r+rrrr,os
)�enum_memberrc
Cs�d|j�d|j��}ytj|��dddd�Wn�tjk
r�}z:t�d|j|j	|j
|j�tt
|�d|j
|jd�d��Wd	d	}~XYnDtttjfk
r�}ztd
|j�d|��dd��Wd	d	}~XYnXd	S)
z Enable or disable cache for userz)/usr/local/cpanel/scripts/ea-nginx cache z --enabled=T)�check�text�capture_outputz<Failed command %s details: exitcode %i, stdout %s, stderr %sr')�out�err)�flag�extraNzFailed to set nginx cache as z with )r4)rr-�
subprocess�run�split�CalledProcessErrorr&r)�args�
returncode�stdout�stderrr�str�OSError�
ValueError�SubprocessError�name)r!r.�cmdr+rrr�set|s
"zNginxUserCache.set)�filenamercCs�ytt|��b}yt�|�}WnBtjk
r\}z"tjd|t|�d|��id�Wdd}~XYnX|�d�SWdQRXWn8t	t
fk
r�}zt�d|t|��Wdd}~XYnXdS)z%Get the 'enabled' key from given filezInvalid JSON from %s: %s�contents)r5N�enabledzFailed to read file %s with %s)�open�json�load�JSONDecodeErrorr&�errorr>�read�get�IOErrorr?)rEZ_f�datar+rrrr �s
"z)NginxUserCache.get_cache_status_from_filecCsftsdS|j}|jrBy|�tj�Wntk
r8Yn
Xtj|_|jtjkr\|j	d7_	||_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)
rr,r#rDrrrrrr)r!�_staterrr�disable�szNginxUserCache.disablecCsb|j}|jtjkrX|jd8_|jdkrXy|�tj�Wntk
rNYnX|`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,rrrrrDr)r!rQrrr�restore�s

zNginxUserCache.restore)rr
rrr>r�propertyrr"�boolr#rr,�setter�deleterrD�staticmethodrr rRrSrrrrr's"
r)rrI�logging�os.pathrr6�dataclassesr�enumr�typingr�	constantsrr�
exceptionsr�utilsr	�	getLoggerr&rrrrrrr�<module>
s