ok

Mini Shell

Direktori : /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

�

��f$=�
� �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,ddl-m.Z.ddl+m/Z/ddl0m1Z1ddl2m3Z3ej4d��Z5edddg��Z6de7de6fd�Z8dee7fd�Z9dee7fd�Z:dCd�Z;dDde7d e<de7fd!�Z=d"e7de7fd#�Z>d$e7dd%fd&�Z?de7fd'�Z@dEd)e7de7fd*�ZAde7fd+�ZBd,eCdeDfd-�ZEd.e7dee7fd/�ZFde<fd0�ZGde<fd1�ZHde<fd2�ZId3e<ddfd4�ZJde<fd5�ZKde7fd6�ZLd7e7d8ee7efde<fd9�ZMd:�ZNedFd;eCddfd<���ZOe		dGd?eCd@eCd;eCddfdA���ZPd>ZQdB�ZRdS)Hz>
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_cl_edition_readable)�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_splitr13sl����
�
�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�r2r0�ssa_versionr7?s���4�5�5�5r2c� �td��S)z#Get version of alt-php-xray packagez/usr/share/alt-php-xray/versionr4r6r2r0�xray_versionr9Cs���8�9�9�9r2c����dtdtdtfd�}d���fd�}d�}dd	�}t��pd
}ttjtj���}t
|���}tjt||d
||g���tj
��5}d|��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-10.el8)�update)r;r<s  r0�add_infozsentry_init.<locals>.add_infoTs&��	�g����}�l�;�<�<�<��r2c��	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   r0�
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�iprJs    �r0�get_ipzsentry_init.<locals>.get_ipmsZ���
�/�0�8�X�2F�F�
�!+�	�	��H�g���H�g�.�.�B��
��	�	�	�
��{r2c��t��}|r|�d��nd}|r|�d��nd}t��rdnd}dt��pdfd|fd|fd|fd	t	j��fd
t
d��fdt��fd
t
d��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)	r�getrr9�platform�releaserr�set_tag)�sentry_scope�cp_description�
cp_version�cp_name�
cp_product�tags�tags       r0�set_tagszsentry_init.<locals>.set_tagsys���+�-�-��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�J���	'�	'�C� �L� �#�&�&�&�	'�	'r2Nc��dS�Nr6)�pending�timeouts  r0�nopezsentry_init.<locals>.nope�s���r2zalt-php-ssa@0.4-10.el8)�level�event_level)�callbacki')�dsn�before_sendr\�max_value_length�integrations�
ip_address�r$N)
�dictr7r�logging�INFO�WARNINGr�
sentry_sdk�initr�configure_scope�userrD)	r@rPrerj�ssa_ver�sentry_logging�
silent_atexit�scoperJs	        @r0�sentry_initr�Hs�������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�
�	#�	%�	%���"�F�F�H�H�-��
�	��H�U�O�O�O�O���	�	�	��D�	����	��������������������s6�"C�5C�C�
C�C�
C�C�C"�%C"F�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)�filenamerk�format�datefmti�z$No logging configuration applied: %sN)ru�basicConfigrv�os�chmod�PermissionError�OSErrorrE�log�ERROR�str)r�r��es   r0�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*�dirnamerDrE�warningr�)r�r�s  r0�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��environrZ�umask_0rr
�bind�listenr	r)r�r��sockobjs   r0�
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 r0�previous_day_dater��s3���
���y�a�0�0�0�0�I����j�)�)�)r2r��datestrc�l�tj�||��}|�d��S)z+
    Convert date to format YYYY-mm-dd
    z%Y-%m-%d)�datetime�strptimer�)r��	formatstr�_dates   r0�format_dater��s.��
��&�&�w�	�:�:�E��>�>�*�%�%�%r2c���	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/string�	system_idNzID-z Failed to retrieve system_id: %s)�ET�parse�getroot�find�textrxrzr]�lstripr��
ParseErrorrEr�r�)�tree�root�whole_idrr�s     r0�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 r0�
duration_castr��s!������(�&�1�1�2�2�2r2�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_filerRs   r0r5r5�s����
�(�^�^�	,�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)r9r�	TypeError�AttributeError)�version_numbers r0�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
rrB�ConnectionErrorr�)�user_agent_sockrIs  r0�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  r0�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�LVErErFr��writer�r�)r��fr�s   r0�switch_schedstatsr�3sD��
&�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   r0�$is_io_throttling_detection_availabler�Gs]��
$�H�-�-��%�h�/�/��-�/�/�L��C�"�<�1A�B�B�B��5�7�7�9K�L�L�Lr2c���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   r0r�r�Tsp���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   r0r�r�ask��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�r6r2r0�is_kernel_version_supportedr�ls#��#�$�$���t�/�1�1�1r2�maskc#�bK�tj|��}dV�tj|��dS)z,
    Context manager for dropping umask
    N)r��umask)r��prevs  r0r�r�xs/����
�8�D�>�>�D�	�E�E�E��H�T�N�N�N�N�Nr2r�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��setegidrE�debug�getegidr�seteuid�geteuid)	rr�target_pathr��
with_check�prev_uid�prev_gid�	stat_infors	         r0�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|_dSrg)�_wrapped�	_instance)�self�_clss  r0�__init__z'singleton.<locals>.__Singleton.__init__�s�� �D�M�!�D�N�N�Nr2c�P�|j�ts|j|i|��|_|jS)z,Returns a single instance of decorated class)r�IS_SINGLETON_ENABLEDr)r�args�kwargss   r0�__call__z'singleton.<locals>.__Singleton.__call__�s0���~�%�-A�%�!.����!?��!?�!?����>�!r2N)�__name__�
__module__�__qualname__�__doc__rr!r6r2r0�__Singletonr�s<������	�	�
	"�	"�	"�	"�	"�	"�	"�	"r2r&r6)�some_clsr&s  r0�	singletonr(�s<��"�"�"�"�"�"�"�"� �;�x� � � r2rs)F)r�)r)NNr�NT)Sr%r�r�rur�r[r��xml.etree.ElementTree�etree�ElementTreer��collectionsr�
contextlibrrr�distutils.versionrrr	r
rrr
r�typingrr�urllib.parserrx�sentry_sdk.integrations.atexitr�sentry_sdk.integrations.loggingr�clcommon.constr�clcommon.cpapirrrr�clcommon.utilsr�clcommon.lib.cleditionrr�	constantsr�
exceptionsr�	getLoggerrEr r�r1r7r9r��boolr�r�r�r�r�r�r�r�r�r5r�r�r�r�r�r�r�r�r�rrr(r6r2r0�<module>r;sj��������
�
�
�
�����	�	�	�	�����	�	�	�	�"�"�"�"�"�"�"�"�"�"�"�"�"�"�"�%�%�%�%�%�%�$�$�$�$�$�$�$�$�*�*�*�*�*�*�������������������"�"�"�"�"�"�"�"�!�!�!�!�!�!�����<�<�<�<�<�<�>�>�>�>�>�>�"�"�"�"�"�"�������������6�5�5�5�5�5�:�:�:�:�:�:�1�1�1�1�1�1�!�!�!�!�!�!� � � � � � �	��	�7�	#�	#���j���
�3�4�4��

:�3�
:�3�
:�
:�
:�
:�6�X�c�]�6�6�6�6�:�h�s�m�:�:�:�:�
U�U�U�U�p�����������(�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��!�!�!�!�!r2

Zerion Mini Shell 1.0