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/ssa/internal/__pycache__/utils.cpython-37.pyc
B

�BgdL8�@s�dZddlZddlZddlZddlZddlZddlZddlmm	Z
ddlmZddl
mZddlmZmZddlmZddlmZmZmZmZmZmZmZddlmZmZdd	lmZddlZdd
l m!Z!ddl"m#Z#ddl$m%Z%dd
l&m'Z'ddl(m)Z)ddl*m+Z+m,Z,ddl-m.Z.ddl/m0Z0e�1d�Z2edddg�Z3e4e3d�dd�Z5dd�dd�Z6dVe4e7e4d�dd �Z8e4e4d!�d"d#�Z9e4d$d%�d&d'�Z:e4d�d(d)�Z;dWe4e4d+�d,d-�Z<e4d�d.d/�Z=e>e?d0�d1d2�Z@e4ee4d3�d4d5�ZAee4d�d6d7�ZBee4d�d8d9�ZCe7d�d:d;�ZDe7d�d<d=�ZEe7d�d>d?�ZFe7dd@�dAdB�ZGe7d�dCdD�ZHe4d�dEdF�ZIe4ee4efe7dG�dHdI�ZJdJdK�ZKedXe>ddL�dMdN��ZLedYe>e>e>ddQ�dRdS��ZMdPZNdTdU�ZOdS)Zz>
This module contains helpful utility functions for SSA Agent
�N)�
namedtuple)�contextmanager)�date�	timedelta)�LooseVersion)�socket�fromfd�AF_UNIX�SOCK_STREAM�AF_INET�AF_INET6�
SOCK_DGRAM)�Optional�Union)�urlparse)�AtexitIntegration)�LoggingIntegration)�Feature)�is_panel_feature_supported)�get_kmodlve_module_version)�is_cl_solo_edition�is_container�)�
sentry_dsn)�SSAError�utils�URLZdomain_nameZuri_path)�url�returncCsJt|�}|jrd|j��nd}|jr2|j�|��nd}t|j�dd�|�S)z�
    Split URL into domain_name and uripath including query string
    :param url: URL of format protocol://domain/path;parameters?query#fragment
    :return: namedtuple URL(domain_name, uripath)
    �?��/zwww.)r�query�pathr�netloc�replace)rZ	fragments�qs�uri�r(�C/opt/alt/python37/lib/python3.7/site-packages/ssa/internal/utils.py�	url_split0sr*)rc	s�tttd�dd�}dd���fdd�}dd	�d
d�}ttjtjd�}t|d
�}tjt|d||gd�t�	��}d|�i|_
WdQRXdS)u�
    Initialize Sentry client
    shutdown_timeout=0 disables Atexit integration as stated in docs:
    'it’s easier to disable it by setting the shutdown_timeout to 0'
    https://docs.sentry.io/platforms/python/default-integrations/#atexit
    On the other hand, docs say, that
    'Setting this value too low will most likely cause problems
    for sending events from command line applications'
    https://docs.sentry.io/error-reporting/configuration/?platform=python#shutdown-timeout
    )�event�hintrcSs|d�ddi�|S)z�
        Add extra data into sentry event
        :param event: original event
        :param hint: additional data caught
        :return: updated event
        �extrazssa.versionz
0.3-11.el9)�update)r+r,r(r(r)�add_infoIszsentry_init.<locals>.add_infoc	SsTy0t|t��}|�|df�|��dSQRXWntk
rNt�d�YnXdS)aI
        address_family - we can choose constants represent the address
                           (and protocol) families
                           (AF_INET for ipv4 and AF_INET6 for ipv6)
        private_ip - specify some private ip address. For instance:
                     ipv4 -> 10.255.255.255 or ipv6 -> fc00::
        rrNzCannot retrieve IP address)rr
�connect�getsockname�	Exception�logger�info)Zaddress_familyZ
private_ip�sr(r(r)�
try_get_ipSszsentry_init.<locals>.try_get_ipcs8tdftdff}x"|D]\}}�||�}|r|SqWdS)zq
        We are trying to get an IPv4 or IPv6 address.
        In case of failure we'll return 127.0.0.1
        z10.255.255.255zfc00::z	127.0.0.1)rr)Z
ipversionsZaddr_famZpriv_ip�ip)r6r(r)�get_ipbs
zsentry_init.<locals>.get_ipN)rcSsdS)Nr()�pending�timeoutr(r(r)�nopen�zsentry_init.<locals>.nope)�levelZevent_level)�callbackzalt-php-ssa@0.3-11.el9)ZdsnZbefore_send�releaseZintegrationsZ
ip_address)�dictr�logging�INFO�WARNINGr�
sentry_sdk�initr�configure_scope�user)r/r8r;Zsentry_loggingZ
silent_atexit�scoper()r6r)�sentry_init=s



rIF)�fname�as_errorrc
Cs�y>tj|tjddd�yt�|d�Wntk
r:YnX|Stk
r~}z"t�|r^tj	ntjdt
|��Wdd}~XYnXdS)zz
    Try to configure logging into given fname
    If as_error True, log the exception as ERROR, otherwise -- as INFO
    z%(asctime)s %(message)sz%m/%d/%Y %I:%M:%S %p)�filenamer=�format�datefmti�z$No logging configuration applied: %sN)rA�basicConfigrB�os�chmod�PermissionError�OSErrorr3�log�ERROR�str)rJrK�er(r(r)�set_logging_into_filezs
rX)�lognamerc
Cslt�t|�dkrhyt�tj�|��Wn2tk
rZ}zt�dt	|��dSd}~XYnXt|dd�}|S)zP
    Configure logging
    :param logname: path to log
    :return: logpath
    NzFailed to create logdir %sr T)rK)
rIrXrP�makedirsr#�dirnamer2r3�warningrV)rYrWr(r(r)�configure_logging�sr]z
socket object)�
sock_locationrc	Cs`ttj�dd��}|dkrHt�� tt�}|�|�|��WdQRXnt	dtt
�}|��|S)z�
    Create world-writable socket in given sock_location
    or reuse existing one
    :param sock_location: socket address
    :return: socket object
    �
LISTEN_FDSrN�)�intrP�environ�get�umask_0rr	�bind�listenrr
)r^r_Zsockobjr(r(r)�
create_socket�s
rgcCst��tdd�}|�d�S)zC
    Returns date of previous day in a format "day.month.year"
    r)�daysz%d.%m.%Y)r�todayr�strftime)Z	yesterdayr(r(r)�previous_day_date�srk�%d.%m.%Y)�datestrrcCstj�||�}|�d�S)z+
    Convert date to format YYYY-mm-dd
    z%Y-%m-%d)�datetime�strptimerj)rmZ	formatstrZ_dater(r(r)�format_date�srpc
Cs�yHt�d�}|��}|�d�j}t���}|�d|�WdQRX|�d�St	tj
fk
r�}zt�dt
|��Wdd}~XYnXdS)ze
    Obtain system ID from /etc/sysconfig/rhn/systemid
    :return: system ID without ID- prefix
    z/etc/sysconfig/rhn/systemidz(.//member[name='system_id']/value/stringZ	system_idNzID-z Failed to retrieve system_id: %s)�ET�parseZgetroot�find�textrDrFZset_tag�lstriprSZ
ParseErrorr3r\rV)Ztree�rootZwhole_idrHrWr(r(r)�read_sys_id�s


rw)�durationrcCstt|dd��S)zQ
    Cast duration from microseconds to seconds leaving 2 digits after point
    i@Bz0.2f)�floatrM)rxr(r(r)�
duration_cast�srz)�filepathrc	CsVy$t|��}|����}WdQRXWntk
r8dSXd�|�d�dd��pTdS)zDGet version of package from file. alt-php-ssa/alt-php-xray supportedN�.�z0.0-0)�open�read�striprS�join�split)r{Zv_file�versionr(r(r)�pkg_version�s
r�cCstd�S)z"Get version of alt-php-ssa packagez/usr/share/clos_ssa/version)r�r(r(r(r)�ssa_version�sr�cCstd�S)z#Get version of alt-php-xray packagez/usr/share/alt-php-xray/version)r�r(r(r(r)�xray_version�sr�c	Cs@t�}|dkrdSyt|�td�kSttfk
r:dSXdS)zb
    Check version of alt-php-xray package.
    Autotracing in X-Ray is supported since 0.4-1
    NFz0.4-1)r�r�	TypeError�AttributeError)Zversion_numberr(r(r)�is_xray_version_supported�sr�c
CsFd}ttt��.}y|�|�Wnttfk
r6dSXWdQRXdS)z Check if User Agent is listeningz$/opt/alt/php-xray/run/xray-user.sockFNT)rr	r
r0�ConnectionErrorrS)Zuser_agent_sockr5r(r(r)�is_xray_user_agent_active�sr�c	CsXd}tj�|�sdSy(t�|d��}t|���dkSQRXWntjk
rRdSXdS)z@Check if there are no active X-Ray tasks (== empty task storage)z/usr/share/alt-php-xray/tasksT�crNF)rPr#�isfile�dbmr~�len�keys�error)Zxray_tasks_storageZ
xray_tasksr(r(r)�no_xray_active_taskssr�)�enabledrc
Cs�ttj�sdSt�d|�y:tdddd��"}|�|r8dnd�t�d	�WdQRXWn4tk
r�}zt�d
|t|��Wdd}~XYnXdS)zb
    Switch on/off throttle statistics gathering by kmodlve
    :param enabled: True or False
    NzSwitching schedstats: %sz!/proc/sys/kernel/sched_schedstats�wbr)�mode�	buffering�1�0zDone OKz(Failed to set sched_schedstats to %s: %s)	rrZLVEr3r4r~�writerSrV)r��frWr(r(r)�switch_schedstatss

r�cCs0td�}td�}t�}|r$t||�Stt�|�S)z�
    Check kmodlve module version or kernel version in order to determine
    if it provides the detection of IO throttling
    z2.0-23z1.5.58)rr�compare_versions_ge�extract_kernel_version)Zkmod_min_versionZkernel_min_versionZkmod_currentr(r(r)�$is_io_throttling_detection_available,s
r�cCs@t�d�}t��}y|�|��d�}Wntk
r:dSX|S)zO
    Get kernel version in the form of "major-minor" from current platform
    zlve([.0-9]*).elrz0.0.0)�re�compile�platformr?�search�groupr�)�patternr?Zversion_numr(r(r)r�9s
r�)�
first_version�second_versionrc
CsJyt|�|kStk
rD}ztd|�d|�d���Wdd}~XYnXdS)zE
    Comparing two versions using the greater or equal operator.
    zAUnable to compare required versions: unexpected versions format "z" and "�"N)rr�r)r�r�rWr(r(r)r�Fs
r�cCstdd�st�rdSt�S)zR
    General check of kernel support (IO throttling availability is required)
    T)�skip_jwt_check)rrr�r(r(r(r)�is_kernel_version_supportedQsr�)�maskrccst�|�}dVt�|�dS)z,
    Context manager for dropping umask
    N)rP�umask)r��prevr(r(r)rd]s
rdr|T)�
target_uid�
target_gidr�rc	csdt��}t��}yt�|�}Wntk
r6d}YnX|dkrT|dkrN|}n|j}|dkrp|dkrj|}n|j}|dk	r�t�|�}||kr�t�|�t	�
d|�|r�t��|kr�td��||k�rt�
|�t	�
d|�|�rt��|k�r||kr�t�|�td��dV||k�r,t�
|�t	�
d|�||k�rLt�|�t	�
d|�|dk	�r`t�|�dS)aH
    Context manager to drop privileges during some operation
    and then restore them back.
    If target_uid or target_gid are given, use input values.
    Otherwise, stat target_uid and target_gid from given target_path.
    If no target_path given, use current directory.
    Use mask if given.
    :param target_uid: uid to set
    :param target_gid: gid to set
    :param target_path: directory or file to stat for privileges,
                       default -- current directory
    :param mask: umask to use
    :param with_check: check the result of switching privileges
    NzDropped GID privs to %sz6Unable to execute required operation: permission issuezDropped UID privs to %szRestored UID privs to %szRestored GID privs to %s)rP�getuid�getgid�statrS�st_uid�st_gidr��setegidr3�debug�getegidr�seteuid�geteuid)	r�r�Ztarget_pathr�Z
with_checkZprev_uidZprev_gid�	stat_infor�r(r(r)�set_privilegesgsN










r�cCsGdd�d�}||�S)Nc@s eZdZdZdd�Zdd�ZdS)zsingleton.<locals>.__Singletonzm
        A singleton wrapper class. Its instances would be created
        for each decorated class.
        cSs||_d|_dS)N)�_wrapped�	_instance)�self�_clsr(r(r)�__init__�sz'singleton.<locals>.__Singleton.__init__c_s"|jdksts|j||�|_|jS)z,Returns a single instance of decorated classN)r��IS_SINGLETON_ENABLEDr�)r��args�kwargsr(r(r)�__call__�sz'singleton.<locals>.__Singleton.__call__N)�__name__�
__module__�__qualname__�__doc__r�r�r(r(r(r)�__Singleton�sr�r()Zsome_clsr�r(r(r)�	singleton�sr�)F)rl)r)NNr|NT)Pr�rnr�rArPr�r�Zxml.etree.ElementTree�etreeZElementTreerq�collectionsr�
contextlibrrr�distutils.versionrrrr	r
rrr
�typingrr�urllib.parserrDZsentry_sdk.integrations.atexitrZsentry_sdk.integrations.loggingrZclcommon.constr�clcommon.cpapir�clcommon.utilsr�clcommon.lib.cleditionrrZ	constantsr�
exceptionsr�	getLoggerr3rrVr*rI�boolrXr]rgrkrprwraryrzr�r�r�r�r�r�r�r�r�r�r�rdr�r�r�r(r(r(r)�<module>
sh$

=

	F