HEX
Server: Apache
System: Linux server2.voipitup.com.au 4.18.0-553.104.1.lve.el8.x86_64 #1 SMP Tue Feb 10 20:07:30 UTC 2026 x86_64
User: posscale (1027)
PHP: 8.2.29
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //opt/alt/python37/lib/python3.7/site-packages/ssa/internal/__pycache__/utils.cpython-37.pyc
B

V�d�<�@s�dZddlZddlZddlZddlZddlZddlZddlmm	Z
ddlmZddl
mZddlmZmZddlmZddlmZmZmZmZmZmZmZddlmZmZdd	lmZddlZdd
l m!Z!ddl"m#Z#ddl$m%Z%dd
l&m'Z'm(Z(ddl)m*Z*ddl+m,Z,m-Z-m.Z.ddl)m/Z/ddl0m1Z1ddl2m3Z3e�4d�Z5edddg�Z6e7e6d�dd�Z8ee7d�dd�Z9ee7d�dd�Z:dd�d d!�Z;dWe7e<e7d#�d$d%�Z=e7e7d&�d'd(�Z>e7d)d*�d+d,�Z?e7d�d-d.�Z@dXe7e7d0�d1d2�ZAe7d�d3d4�ZBeCeDd5�d6d7�ZEe7ee7d8�d9d:�ZFe<d�d;d<�ZGe<d�d=d>�ZHe<d�d?d@�ZIe<ddA�dBdC�ZJe<d�dDdE�ZKe7d�dFdG�ZLe7ee7efe<dH�dIdJ�ZMdKdL�ZNedYeCddM�dNdO��ZOedZeCeCeCddR�dSdT��ZPdQZQdUdV�ZRdS)[z>
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_cp_description)�get_kmodlve_module_version)�is_cl_solo_edition�is_container�get_cl_edition_readable)�get_rhn_systemid_value�)�
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)r Z	fragmentsZqs�uri�r*�C/opt/alt/python37/lib/python3.7/site-packages/ssa/internal/utils.py�	url_split2sr,)r!cCstd�S)z"Get version of alt-php-ssa packagez/usr/share/clos_ssa/version)�pkg_versionr*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_versionBsr/c	s�tttd�dd�}dd���fdd�}dd	�}d
d�dd
�}t�pDd}ttjtjd�}t|d�}tjt	||||gd�t�
��4}d|�i|_y||�Wntk
r�YnXWd
QRXd
S)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�hintr!cSs|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-15.el8)�update)r0r1r*r*r+�add_infoSszsentry_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
�connectZgetsockname�	Exception�logger�info)Zaddress_familyZ
private_ip�sr*r*r+�
try_get_ip]s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)r:r*r+�get_ipls
zsentry_init.<locals>.get_ipc	Ss�t�}|r|�d�nd}|r&|�d�nd}dt�p4dfd|fd|fdt��fdtd	�fd
t�fdtd�ff}x|D]}|j|�qrWdS)
N�version�namezalt-php-xrayZUNKNOWNzControl Panel NamezControl Panel VersionZkernelzCloudLinux versionZ
os_releasezCloudlinux editionZArchitectureZarchitecture)r�getr/�platform�releaserr�set_tag)Zsentry_scopeZcp_descriptionZ
cp_versionZcp_name�tags�tagr*r*r+�set_tagsxs


zsentry_init.<locals>.set_tagsN)r!cSsdS)Nr*)�pending�timeoutr*r*r+�nope��zsentry_init.<locals>.nopezalt-php-ssa@0.3-15.el8)�levelZevent_level)�callback)ZdsnZbefore_sendrAZintegrationsZ
ip_address)
�dictr.r�logging�INFO�WARNINGr�
sentry_sdkZinitr�configure_scope�userr6)r4r<rErHZssa_verZsentry_loggingZ
silent_atexit�scoper*)r:r+�sentry_initGs$




rTF)�fname�as_errorr!c
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)�filenamerJ�format�datefmti�z$No logging configuration applied: %sN)rM�basicConfigrN�os�chmod�PermissionError�OSErrorr7�log�ERROR�str)rUrV�er*r*r+�set_logging_into_file�s
rc)�lognamer!c
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)rV)
rTrcr[�makedirsr&�dirnamer6r7�warningra)rdrbr*r*r+�configure_logging�srhz
socket object)�
sock_locationr!c	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�)�intr[�environr?�umask_0rr	Zbind�listenrr
)rirjZsockobjr*r*r+�
create_socket�s
rpcCst��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�srt�%d.%m.%Y)�datestrr!cCstj�||�}|�d�S)z+
    Convert date to format YYYY-mm-dd
    z%Y-%m-%d)�datetime�strptimers)rvZ	formatstrZ_dater*r*r+�format_date�sryc
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�textrPrQrB�lstripr^Z
ParseErrorr7rgra)Ztree�rootZwhole_idrSrbr*r*r+�read_sys_id�s


r�)�durationr!cCstt|dd��S)zQ
    Cast duration from microseconds to seconds leaving 2 digits after point
    i@Bz0.2f)�floatrX)r�r*r*r+�
duration_cast�sr�)�filepathr!c	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�stripr^�join�split)r�Zv_filer=r*r*r+r-�s
r-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_supportedsr�c
CsFd}ttt��.}y|�|�Wnttfk
r6dSXWdQRXdS)z Check if User Agent is listeningz$/opt/alt/php-xray/run/xray-user.sockFNT)rr	r
r5�ConnectionErrorr^)Zuser_agent_sockr9r*r*r+�is_xray_user_agent_actives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)r[r&�isfile�dbmr��len�keys�error)Zxray_tasks_storageZ
xray_tasksr*r*r+�no_xray_active_taskssr�)�enabledr!c
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)	rrZLVEr7r8r��writer^ra)r��frbr*r*r+�switch_schedstats/s

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_availableCs
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�compiler@rA�search�groupr�)�patternrAZversion_numr*r*r+r�Ps
r�)�
first_version�second_versionr!c
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�rbr*r*r+r�]s
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_supportedhsr�)�maskr!ccst�|�}dVt�|�dS)z,
    Context manager for dropping umask
    N)r[�umask)r��prevr*r*r+rnts
rnr�T)�
target_uid�
target_gidr�r!c	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)r[�getuid�getgid�statr^�st_uid�st_gidr��setegidr7�debug�getegidr�seteuid�geteuid)	r�r�Ztarget_pathr�Z
with_checkZprev_uidZprev_gid�	stat_infor�r*r*r+�set_privileges~sN










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)ru)r)NNr�NT)Sr�rwr�rMr[r@r�Zxml.etree.ElementTree�etreeZElementTreerz�collectionsr�
contextlibrrr�distutils.versionrrrr	r
rrr
�typingrr�urllib.parserrPZsentry_sdk.integrations.atexitrZsentry_sdk.integrations.loggingrZclcommon.constr�clcommon.cpapirr�clcommon.utilsr�clcommon.lib.cleditionrrrrZ	constantsr�
exceptionsr�	getLoggerr7rrar,r.r/rT�boolrcrhrprtryr�rlr�r�r-r�r�r�r�r�r�r�r�rnr�r�r�r*r*r*r+�<module>
sj$
U


	F