AlkantarClanX12

Your IP : 13.58.201.240


Current Path : /opt/cloudlinux/venv/lib64/python3.11/site-packages/ssa/internal/__pycache__/
Upload File :
Current File : //opt/cloudlinux/venv/lib64/python3.11/site-packages/ssa/internal/__pycache__/utils.cpython-311.pyc

�

qu�f">�
�0�dZddlZddlZddlZddlZddlZddlZddlmcm	Z
ddlmZddl
mZddlmZmZddlmZddlmZmZmZmZmZmZmZddlmZmZdd	lmZddlZdd
l m!Z!ddl"m#Z#ddl$m%Z%dd
l&m'Z'm(Z(m)Z)m*Z*ddl+m,Z,m-Z-ddl.m/Z/ddl0m1Z1ddl+m2Z2ddl3m4Z4ddl5m6Z6ej7d��Z8edddg��Z9de:de9fd�Z;dee:fd�Z<dee:fd�Z=dDd�Z>dEd e:d!e?de:fd"�Z@d#e:de:fd$�ZAd%e:dd&fd'�ZBde:fd(�ZCdFd*e:de:fd+�ZDde:fd,�ZEd-eFdeGfd.�ZHd/e:dee:fd0�ZIde?fd1�ZJde?fd2�ZKde?fd3�ZLd4e?ddfd5�ZMde?fd6�ZNde:fd7�ZOd8e:d9ee:efde?fd:�ZPd;�ZQedGd<eFddfd=���ZRe		dHd@eFdAeFd<eFddfdB���ZSd?ZTdC�ZUdS)Iz>
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�is_throttling_supported�is_wp2_environment)�get_kmodlve_module_version�get_username)�get_cl_edition_readable)�get_hostname)�get_rhn_systemid_value�)�
sentry_dsn)�SSAError�utils�URL�domain_name�uri_path�url�returnc��t|��}|jr
d|j��nd}|jr|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%�	fragments�qs�uris    �I/opt/cloudlinux/venv/lib64/python3.11/site-packages/ssa/internal/utils.py�	url_splitr34sl����
�
�I�"+�/�	9�	�Y�_�	�	�	�r�B�%.�^�
<�Y�^�
!�R�
!�
!�
!��C��y��'�'���3�3�S�9�9�9�c� �td��S)z"Get version of alt-php-ssa packagez/usr/share/clos_ssa/version��pkg_version�r4r2�ssa_versionr9@s���4�5�5�5r4c� �td��S)z#Get version of alt-php-xray packagez/usr/share/alt-php-xray/versionr6r8r4r2�xray_versionr;Ds���8�9�9�9r4c�2���dtdtdtfd�}d���fd���fd�}dd	�}t��pd
}ttjtj���}t
|���}tjt||d
||g���tj
��5}dtd��p%���pt��p
t��i|_	||��n#t$rYnwxYwddd��dS#1swxYwYdS)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&c�@�|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.4-11.el8)�update)r=r>s  r2�add_infozsentry_init.<locals>.add_infoUs&��	�g����}�l�;�<�<�<��r4c��	t|t��5}|�|df��|���dcddd��S#1swxYwYdS#t$rt
�d��YdSwxYw)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)�address_family�
private_ip�ss   r2�
try_get_ipzsentry_init.<locals>.try_get_ip_s���	6���
�3�3�
*�q��	�	�:�q�/�*�*�*��}�}���q�)�
*�
*�
*�
*�
*�
*�
*�
*�
*�
*�
*�
*����
*�
*�
*�
*�
*�
*���	6�	6�	6��K�K�4�5�5�5�5�5�5�	6���s4�A"�1A�A"�A�A"�A�A"�"$B
�	B
c�`��tdftdff}|D]\}}�||��}|r|cS�dS)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)rr
)�
ipversions�addr_fam�priv_ip�iprLs    �r2�get_ipzsentry_init.<locals>.get_ipnsZ���
�/�0�8�X�2F�F�
�!+�	�	��H�g���H�g�.�.�B��
��	�	�	�
��{r4c���t��}|r|�d��nd}|r|�d��nd}t��rdnd}dt��pdfd|fd|fd|fd	t	j��fd
t
d��fdt��fd
t
d��fd���fdt��ff
}|D]}|j	|��
dS)N�version�name�WP2zalt-php-xray�UNKNOWNzControl Panel NamezControl Panel VersionzControl Panel Product�kernelzCloudLinux version�
os_releasezCloudlinux edition�Architecture�architecture�
ip_address�username)
r�getrr;�platform�releaserrr�set_tag)�sentry_scope�cp_description�
cp_version�cp_name�
cp_product�tags�tagrRs       �r2�set_tagszsentry_init.<locals>.set_tagszs���+�-�-��6D�N�^�'�'�	�2�2�2�$�
�0>�H�.�$�$�V�,�,�,�D��0�2�2�<�U�U��
�����!<�9�=�%�w�/�(�*�5�(�*�5��8�+�-�-�.�%�'=�l�'K�'K�L�%�'>�'@�'@�A��!7��!G�!G�H��v�v�x�x�(��\�^�^�,�
���	'�	'�C� �L� �#�&�&�&�	'�	'r4Nc��dS�Nr8)�pending�timeouts  r2�nopezsentry_init.<locals>.nope�s���r4zalt-php-ssa@0.4-11.el8)�level�event_level)�callbacki')�dsn�before_sendr`�max_value_length�integrations�id�	system_id�r&N)�dictr9r�logging�INFO�WARNINGr�
sentry_sdk�initr�configure_scoperrr�userrF)	rBrirn�ssa_ver�sentry_logging�
silent_atexit�scoperRrLs	       @@r2�sentry_initr�Is��������D��T�����
6�
6�
6�
�
�
�
�
�'�'�'�'�'�,-�,�,�,��m�m�7�7�G�'�g�l�4;�O�E�E�E�N�%�t�4�4�4�M��O�
��#�%*�"0�-�!@�B�B�B�B�
�	#�	%�	%����(��5�5�e�����e�\�^�^�e�Wc�We�We�
��
�	��H�U�O�O�O�O���	�	�	��D�	����
��������������������s6�%=D�#C/�.D�/
C<�9D�;C<�<D�D�DF�fname�as_errorc�L�	tj|tjdd���	tj|d��n#t
$rYnwxYw|S#t$rL}t�|rtj	ntjdt|����Yd}~dSd}~wwxYw)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)�filenamero�format�datefmti�z$No logging configuration applied: %sN)rz�basicConfigr{�os�chmod�PermissionError�OSErrorrG�log�ERROR�str)r�r��es   r2�set_logging_into_filer��s���
���U�'�,�#<�$:�	<�	<�	<�	<�	��H�U�E�"�"�"�"���	�	�	��D�	������������
�
�H�>�7�=�=�'�,�9��q�6�6�	�	�	�	�	�	�	�	�	��������s8�"A
�;�A
�
A�A
�A�A
�
B#�AB�B#�lognamec�H�t��t|����	tjtj�|����n@#t$r3}t�dt|����Yd}~dSd}~wwxYwt|d���}|S)zP
    Configure logging
    :param logname: path to log
    :return: logpath
    NzFailed to create logdir %sr)T)r�)
r�r�r��makedirsr,�dirnamerFrG�warningr�)r�r�s  r2�configure_loggingr��s����M�M�M��W�%�%�-�	��K������0�0�1�1�1�1���	�	�	��N�N�7��Q���@�@�@��2�2�2�2�2�����	����(��$�?�?�?���Ns�1A�
B�(B	�	B�
sock_locationz
socket objectc��ttj�dd����}|dkrdt	��5tt��}|�|��|���ddd��n#1swxYwYn/tdtt��}|���|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
�bind�listenr	r)r�r��sockobjs   r2�
create_socketr��s����R�Z�^�^�L�!�4�4�5�5�J��Q���
�Y�Y�	�	��W�o�o�G��L�L��'�'�'��N�N����	�	�	�	�	�	�	�	�	�	�	����	�	�	�	��
��G�[�1�1���������Ns�>B�B�Bc�t�tj��td���z
}|�d��S)zC
    Returns date of previous day in a format "day.month.year"
    r)�days�%d.%m.%Y)r�todayr�strftime)�	yesterdays r2�previous_day_dater��s3���
���y�a�0�0�0�0�I����j�)�)�)r4r��datestrc�l�tj�||��}|�d��S)z+
    Convert date to format YYYY-mm-dd
    z%Y-%m-%d)�datetime�strptimer�)r��	formatstr�_dates   r2�format_dater��s.��
��&�&�w�	�:�:�E��>�>�*�%�%�%r4c���	tjd��}|���}|�d��j}tj��5}|�d|��ddd��n#1swxYwY|�d��S#ttj
f$r3}t�dt|����Yd}~dSd}~wwxYw)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/stringrwNzID-z Failed to retrieve system_id: %s)�ET�parse�getroot�find�textr}rra�lstripr��
ParseErrorrGr�r�)�tree�root�whole_idr�r�s     r2�read_sys_idr��s��
C��x�5�6�6���|�|�~�~���9�9�G�H�H�M��
�
'�
)�
)�	1�U��M�M�+�x�0�0�0�	1�	1�	1�	1�	1�	1�	1�	1�	1�	1�	1����	1�	1�	1�	1����u�%�%�%���R�]�#�C�C�C����9�3�q�6�6�B�B�B�B�B�B�B�B�B�����C���s<�AB�A:�.B�:A>�>B�A>�B�C#�0(C�C#�durationc�B�tt|dzd����S)zQ
    Cast duration from microseconds to seconds leaving 2 digits after point
    i@Bz0.2f)�floatr�)r�s r2�
duration_castr��s!������(�&�1�1�2�2�2r4�filepathc�&�	t|��5}|������}ddd��n#1swxYwYn#t$rYdSwxYwd�|�d��dd���pdS)zDGet version of package from file. alt-php-ssa/alt-php-xray supportedN�.�z0.0-0)�open�read�stripr��join�split)r��v_filerTs   r2r7r7s����
�(�^�^�	,�v��k�k�m�m�)�)�+�+�G�	,�	,�	,�	,�	,�	,�	,�	,�	,�	,�	,����	,�	,�	,�	,���������������8�8�G�M�M�#�&�&�r��r�*�+�+�6�w�6s3�A�'A�A�A�A�A�A�
A�Ac��t��}|�dS	t|��td��kS#ttf$rYdSwxYw)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)�version_numbers r2�is_xray_version_supportedr�s_��
"�^�^�N����u���N�+�+�|�G�/D�/D�D�D���~�&�����u�u����s�4�A	�A	c���d}ttt��5}	|�|��n##tt
f$rYddd��dSwxYw	ddd��n#1swxYwYdS)z Check if User Agent is listeningz$/opt/alt/php-xray/run/xray-user.sockNFT)rr
rrD�ConnectionErrorr�)�user_agent_sockrKs  r2�is_xray_user_agent_activer�s���<�O�	���	%�	%���	�
�I�I�o�&�&�&�&����)�	�	�	��	��������	����
'�������������������
�4s1�A%�5�A%�A�A%�A�A%�%A)�,A)c��d}tj�|��sdS	tj|d��5}t|�����dkcddd��S#1swxYwYdS#tj$rYdSwxYw)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)�xray_tasks_storage�
xray_taskss  r2�no_xray_active_tasksr�)s���8��
�7�>�>�,�-�-���t��
�X�(�#�
.�
.�	/�*��z���(�(�)�)�Q�.�	/�	/�	/�	/�	/�	/�	/�	/�	/�	/�	/�	/����	/�	/�	/�	/�	/�	/���9�����u�u����s4�A9�%A,�A9�,A0�0A9�3A0�4A9�9B�B�enabledc��ttj��sdSt�d|��	tddd���5}|�|rdnd��t�d	��ddd��dS#1swxYwYdS#t$r4}t�d
|t|����Yd}~dSd}~wwxYw)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)	rr�LVErGrHr��writer�r�)r��fr�s   r2�switch_schedstatsr�9sD��
&�g�k�2�2����
�K�K�*�G�4�4�4�%�
�5�D�����	#�"#�
�G�G�G�-�D�D��.�.�.��K�K�	�"�"�"�	#�	#�	#�	#�	#�	#�	#�	#�	#�	#�	#�	#����	#�	#�	#�	#�	#�	#���%�%�%����>��S��V�V�	%�	%�	%�	%�	%�	%�	%�	%�	%�����%���s;�B�
4B�>B�B�B�B�B�
C�")C�Cc��td��}td��}t��}|rt||��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)�kmod_min_version�kernel_min_version�kmod_currents   r2�$is_io_throttling_detection_availabler�Ms]��
$�H�-�-��%�h�/�/��-�/�/�L��C�"�<�1A�B�B�B��5�7�7�9K�L�L�Lr4c���tjd��}tj��}	|�|���d��}n#t$rYdSwxYw|S)zO
    Get kernel version in the form of "major-minor" from current platform
    zlve([0-9]+(\.[0-9]+)+)\.elrz0.0.0)�re�compiler_r`�search�groupr�)�patternr`�version_nums   r2r�r�Zsp���j�6�7�7�G��� � �G���n�n�W�-�-�3�3�A�6�6���������w�w������s�(A�
A �A �
first_version�second_versionc�z�	t|��|kS#t$r}td|�d|�d����d}~wwxYw)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�r�s   r2r�r�gsk��y��M�*�*�n�<�<���y�y�y��w�P]�w�w�ft�w�w�w�y�y�	y�����y���s��
:�5�:c�>�t��sdSt��S)zR
    General check of kernel support (IO throttling availability is required)
    T)rr�r8r4r2�is_kernel_version_supportedrrs#��#�$�$���t�/�1�1�1r4�maskc#�bK�tj|��}dV�tj|��dS)z,
    Context manager for dropping umask
    N)r��umask)r�prevs  r2r�r�~s/����
�8�D�>�>�D�	�E�E�E��H�T�N�N�N�N�Nr4r�T�
target_uid�
target_gidc#�K�tj��}tj��}	tj|��}n#t$rd}YnwxYw|�|�|}n|j}|�|�|}n|j}|�tj|��}||krWtj|��t�
d|��|r&tj��|krtd���||krqtj
|��t�
d|��|r@tj��|kr)||krtj|��td���dV�||kr/tj
|��t�
d|��||kr/tj|��t�
d|��|�tj|��dSdS)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�setegidrG�debug�getegidr �seteuid�geteuid)	rr�target_pathr�
with_check�prev_uid�prev_gid�	stat_infors	         r2�set_privilegesr�s����"�y�{�{�H��y�{�{�H���G�K�(�(�	�	�������	�	�	���������!�J�J�"�)�J�����!�J�J�"�)�J����x��~�~���:���
�
�:�������.�
�;�;�;��	J�"�*�,�,�*�4�4��H�J�J�
J��:���
�
�:�������.�
�;�;�;��	J�"�*�,�,�*�4�4��:�%�%��
�8�$�$�$��H�J�J�
J�	�E�E�E��:���
�
�8�������/��:�:�:��:���
�
�8�������/��:�:�:���
���������s�?�A�
Ac�2�Gd�d��}||��S)Nc��eZdZdZd�Zd�ZdS)�singleton.<locals>.__Singletonzm
        A singleton wrapper class. Its instances would be created
        for each decorated class.
        c�"�||_d|_dSrk)�_wrapped�	_instance)�self�_clss  r2�__init__z'singleton.<locals>.__Singleton.__init__�s�� �D�M�!�D�N�N�Nr4c�P�|j�ts|j|i|��|_|jS)z,Returns a single instance of decorated class)r�IS_SINGLETON_ENABLEDr)r�args�kwargss   r2�__call__z'singleton.<locals>.__Singleton.__call__�s0���~�%�-A�%�!.����!?��!?�!?����>�!r4N)�__name__�
__module__�__qualname__�__doc__r r%r8r4r2�__Singletonr�s<������	�	�
	"�	"�	"�	"�	"�	"�	"�	"r4r*r8)�some_clsr*s  r2�	singletonr,�s<��"�"�"�"�"�"�"�"� �;�x� � � r4rx)F)r�)r)NNr�NT)Vr)r�r�rzr�r_r��xml.etree.ElementTree�etree�ElementTreer��collectionsr�
contextlibrrr�distutils.versionrrr	r
rrr
r�typingrr�urllib.parserr}�sentry_sdk.integrations.atexitr�sentry_sdk.integrations.loggingr�clcommon.constr�clcommon.cpapirrrr�clcommon.utilsrr�clcommon.lib.cleditionr�clcommon.lib.networkrr�	constantsr�
exceptionsr �	getLoggerrGr"r�r3r9r;r��boolr�r�r�r�r�r�r�r�r�r7r�r�r�r�r�r�r�rr�rr"r,r8r4r2�<module>r@s���������
�
�
�
�����	�	�	�	�����	�	�	�	�"�"�"�"�"�"�"�"�"�"�"�"�"�"�"�%�%�%�%�%�%�$�$�$�$�$�$�$�$�*�*�*�*�*�*�������������������"�"�"�"�"�"�"�"�!�!�!�!�!�!�����<�<�<�<�<�<�>�>�>�>�>�>�"�"�"�"�"�"�������������D�C�C�C�C�C�C�C�:�:�:�:�:�:�-�-�-�-�-�-�1�1�1�1�1�1�!�!�!�!�!�!� � � � � � �	��	�7�	#�	#���j���
�3�4�4��

:�3�
:�3�
:�
:�
:�
:�6�X�c�]�6�6�6�6�:�h�s�m�:�:�:�:�
Z�Z�Z�Z�z�����������(�s��s�����"��������&*�3�*�*�*�*�&�&��&�s�&�&�&�&�
C�S�
C�
C�
C�
C� 3�C�3�E�3�3�3�3�7�#�7�(�3�-�7�7�7�7��4������4�����
�d�
�
�
�
� %�t�%��%�%�%�%�(
M�d�
M�
M�
M�
M�
��
�
�
�
�y�s�y�E�#�|�BS�<T�y�Y]�y�y�y�y�2�2�2����#��d��������=A�AE�@�@�s�@�s�@�*-�@�JN�@�@�@���@�N��!�!�!�!�!r4