https://t.me/AnonymousX5
Server : Apache
System : Linux cvar2.toservers.com 3.10.0-962.3.2.lve1.5.73.el7.x86_64 #1 SMP Wed Aug 24 21:31:23 UTC 2022 x86_64
User : njnconst ( 1116)
PHP Version : 8.4.18
Disable Function : NONE
Directory :  /opt/alt/python37/lib/python3.7/site-packages/ssa/modules/__pycache__/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : //opt/alt/python37/lib/python3.7/site-packages/ssa/modules/__pycache__/autotracer.cpython-37.pyc
B

��c�Q�@s�dZdZddlZddlZddlmZddlmZmZm	Z	ddl
m
Z
mZddlm
Z
ddlmZmZmZmZmZdd	lmZddlZdd
lmZddlmZdd
lmZmZmZddl m!Z!m"Z"m#Z#ddl$m%Z%ddl&m'Z'ddl(m)Z)ddl*m+Z+m,Z,yddl-m.Z/Wne0k
�r"dZ/YnXeGdd�d��Z1eGdd�d��Z2eGdd�d��Z3Gdd�de�Z4e5e4d�dd�Z6Gdd �d e�Z7e8d!k�r�e,�ej9d"ej:d#�e7�Z;e;�dS)$z'
This module contains Autotracer class
zssa.modules�N)�defaultdict)�	dataclass�field�asdict)�datetime�	timedelta)�fnmatchcase)�Iterator�Optional�
NamedTuple�List�Iterable)�urlparse)�setup_database�)�Common)�iter_domains_data�iter_urls_data�get_url_durations�)�status�disabled_users�is_edition_supported)�
load_tunables)�autotracing_tunables_schema)�autotracing_stats_file)�is_xray_version_supported�sentry_initc@sJeZdZUdZeed<eed<eed<eed<eed<eed<eed<d	S)
�URLzURL data container�uri�avg_duration�max_duration�total_slow_reqs�
total_reqs�
belongs_to�belongs_to_userN)�__name__�
__module__�__qualname__�__doc__�str�__annotations__�int�r-r-�G/opt/alt/python37/lib/python3.7/site-packages/ssa/modules/autotracer.pyr.s
rc@s�eZdZUdZdZeed<dZeed<dZeed<dZ	eed<dZ
eed<dZeed<dZeed	<dZ
eed
<dZeed<dZeed<dZeed
<dS)�RulesRejectszRejects counters containerr�non_wp�	throttled�density�slowness�max_slowness�disabled�nginx�	frequency�server_limit�domain_limit�	no_domainN)r&r'r(r)r0r,r+r1r2r3r4r5r6r7r8r9r:r-r-r-r.r/:s
r/c@sDeZdZUdZeed<dZeed<dZeed<e	e
d�Ze
ed<dS)	�StatszAutoTracer statistics container�
rules_versionr�urls_processed�
urls_selected)�default_factory�rejectsN)r&r'r(r)r*r+r=r,r>rr/r@r-r-r-r.r;Js

r;c@s"eZdZUdZeed<eed<dS)�URLSz"
    Representation of an URL
    �domain_name�uri_pathN)r&r'r(r)r*r+r-r-r-r.rASs
rA)�url�returncCs\t|�}|jrd|j��nd}|jr2|j�|��nd}|j�dd�}|�d�d}t||�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)r�query�path�netloc�replace�splitrA)rD�	fragments�qsrZ_no_www_netlocZ_no_port_netlocr-r-r.�	url_split[srQcs�eZdZdZd7�fdd�	Z�fdd�Zeed�dd	��Zee	e
ed
�dd��Ze	e
d
�dd�Zed�dd�Zd8e
eed�dd�Ze	e
d�dd�Zeee
d�dd�Zeee
d�dd�Zeee
d�dd�Ze
eee
d �d!d"�Ze
e
e
d#�d$d%�Zeed�d&d'�Zd9e
eeed(�d)d*�Zeed�d+d,�Zeed�d-d.�Z dd�d/d0�Z!dd�d1d2�Z"ed�d3d4�Z#ed�d5d6�Z$�Z%S):�
AutoTracerz0
    SSA autotracing module implementation.
    NcsNt���t�d�|_|j�dt�t�|_t�|_	d|_
|rB|nt�|_dS)N�auto_tracerzAutoTracer enabled: %s)
�super�__init__�logging�	getLogger�logger�info�__package__�listr�
tasks_list�statsr�engine)�selfr^)�	__class__r-r.rUns
zAutoTracer.__init__cst�s|j�d�dSt�s,|j�d�dStdkrD|j�d�dStj}|dk	�r|j�d�t���|j�d|j	�t
|j�|_xz|�
�D]n}|j�d|j�|jjd7_y||j|jd�Wq�tk
r�}z|j�d	t|��Wdd}~XYq�Xq�W|��dS)
Nz*AutoTracer skipped: unsupported CL editionz-AutoTracer skipped: unsupported X-Ray versionz*AutoTracer skipped: unable to import X-RayzAutoTracer startedzAutoTracer loaded config: %szStarting auto task for %sr)rDZ
tracing_countzFailed to start task: %s)rrXrYr�xray_libZstart_autotracingrTrU�debug�configr;r<r]�urls_scheduledrr>Zrequest_number�	Exception�errorr*�save_iteration_stats)r_Z
start_toolrD�e)r`r-r.�__call__xs.

(zAutoTracer.__call__)rEcCs
tdt�S)z$
        Load configuration
        zautotracing.json)rrr-r-r-r.�	load_conf�szAutoTracer.load_conf)rBrEcCstdkrdSt�|�S)z;
        Gets domain info for the specified domain
        N)ra�domain_info)rBr-r-r.�gets_domaininfo�szAutoTracer.gets_domaininfo)�	user_namerEcCs"|jrtdkrdSt�|�jSdS)z>
        Says if nginx is enabled for a specific user
        NF)Z
skip_nginxraZNginxUserCache�
is_enabled)r_rmr-r-r.�nginx_is_enabled�s
zAutoTracer.nginx_is_enabledcCs,tdd�|jD��}|j�d|�|j|S)z>Recalculate limit per server taking into account running taskscSs(g|] }|ddkr|ddkr|�qS)r�running�userz
*autotracing*r-)�.0�taskr-r-r.�
<listcomp>�sz<AutoTracer.per_server_limit_recalculated.<locals>.<listcomp>z&Number of running autotracing tasks %s)�lenr\rXrbZper_server_limit)r_Z
running_countr-r-r.�per_server_limit_recalculated�sz(AutoTracer.per_server_limit_recalculated)�	full_listrEcsD|dkrt��}|j�d|�t��t|jd���fdd�|D�S)zQ
        Excludes tasks older than N days from the general list of tasks
        NzTask list loaded %s)�dayscs8g|]0}|�d�dk	r |�d�ndt����kr|�qS)Z
createtimeNr)�getr,�	timestamp)rrrs)�
n_days_agor-r.rt�sz1AutoTracer.excludes_old_tasks.<locals>.<listcomp>)rar\rXrbr�nowrZmin_retracing_interval)r_rwr-)r{r.�excludes_old_tasks�szAutoTracer.excludes_old_tasks)�current_urlrEcCs|t|�}xn|jD]d}t|d�}|j|jko6|j|jk}t|j|j�oRt|j|j�}|s\|r|j�d||d�dSqWdS)z�
        Excludes url from the list if it completely matches the current url
        or if domain names match and "*" follows the domain name in the list
        rDz2Skipped: URL %s was traced recently. Matched by %sTF)rQr\rBrCrrXrb)r_r~�cZ	task_data�tZdirect_matchZwildcard_matchr-r-r.�exclude_thesame_urls�szAutoTracer.exclude_thesame_urls)�url_total_reqs�domain_total_reqsrEcCs4|jr0t�t�||��}|j�d|�||jkSdS)z-Check that URL density passes given thresholdzCalculated density %sT)r2�npZaminZcorrcoefrXrbZdensity_threshold)r_r�r�Zurl_densityr-r-r.�pass_by_density�s
zAutoTracer.pass_by_density)�
url_durationsrEcCs0d|j}t�||�}|j�d||�||jkS)z�
        The measure of "slowness" for URL is:
        at least N% of its requests take more than X seconds.
        N% -- self.slow_duration_percentage
        X -- self.slow_duration_threshold
        �dz.Calculated %sth percentile %s for min duration)Zslow_duration_percentager��
percentilerXrbZslow_duration_threshold)r_r�Zreversed_percentileZreversed_percentile_valuer-r-r.�pass_by_slowness_percentile�s
z&AutoTracer.pass_by_slowness_percentilecCs*t�||j�}|j�d|j|�||jkS)a
        The opposite to pass_by_slowness_percentile method.
        The measure of "much slowness" for URL is:
        at least N% of its requests must take less than X seconds.
        N% -- self.max_slow_duration_percentage
        X -- self.max_slow_duration_threshold
        z.Calculated %sth percentile %s for max duration)r�r�Zmax_slow_duration_percentagerXrbZmax_slow_duration_threshold)r_r�Zpercentile_valuer-r-r.�pass_by_max_slowness_percentile�s	z*AutoTracer.pass_by_max_slowness_percentile)�url_throttled_reqsr�rEcCs8|dkrdSt|�t|�d}|j�d|�||jkS)zY
        Check that percent of throttled requests per URL passes given threshold
        NFr�zCalculated throttled percent %s)�sumrXrbZallowed_throttling_percentage)r_r�r�Zthrottled_percentr-r-r.�pass_by_allowed_throttlings
z%AutoTracer.pass_by_allowed_throttling)�	wp_statusrEcCs|jrt|�SdS)zx
        Check that URLs of a particular domain should be analyzed.
        For now we skip non-wordpress sites
        T)Zonly_wp�bool)r_r�r-r-r.�pass_by_engineszAutoTracer.pass_by_enginec	cs�xt|j�D�]�}d}|�|j�sZ|j�d�|j}|jjj	|7_	|jj
|7_
qtt|j|j
��}�x�t|j|j
�D�]|\}}||jkr�q~|j�d|�|jj
d7_
|�|�d�|d�s�|j�d�|jjjd7_q~|�|d|j��s|j�d�|jjjd7_q~||}|�|��sP|j�d	�|jjjd7_q~|�|��s||j�d
�|jjjd7_q~|dk�r�|�|j
�}|dk�r�|j�d�|jjjd7_q~n|j}t|tt� |��t!|d�t!|d�t"|�|j
|d
�Vq~WqWdS)a
        Select all URLs suitable for auto tracing by very basic rules:
         - WP site
         - suitable throttling
         - suitable density
         - measure of "slow" URL
         ORDER OF RULES MUST NOT BE CHANGED: IT AFFECTS STATISTICS COUNTERS
        Nz Skipped by engine: non-wordpresszProcessing URL %srr�r�zSkipped by throttled percentzSkipped by densityzSkipped by slowness percentilez"Skipped by max slowness percentilez"Skipped by unavailable domain infoZ
url_slow_reqs)r r"r#r!r$r%)#rr^r�Zis_a_wordpress_domainrXrbZurls_numberr]r@r0r=�dictrrBrZnon_url_fieldsr�ryr1r�r�r2r�r3r�r4rlr:rqrr,r�Zmeanr��max)	r_�domain_data�domain_ownerZ
skipped_countZdomain_url_durationsrD�dataZ	durationsrkr-r-r.�urls_computationally_filteredsb	







z(AutoTracer.urls_computationally_filtered)�stats_collectedrEccs�|��xv|��D]j}|j|jkrF|j�d|j�|jjjd7_q|�	|j�rv|j�d|j�|jjj
d7_
q|VqWdS)z�
        From selected by computed thresholds URLs take those for which:
         - autotracing enabled
         - nginx disabled
        ORDER OF RULES MUST NOT BE CHANGED: IT AFFECTS STATISTICS COUNTERS
        z'Skipped: autotracing is disabled for %srz Skipped: nginx is enabled for %sN)�fill_in_disabled_usersr�r%rrXrbr]r@r5ror6)r_r�rDr-r-r.r>ps	zAutoTracer.urls_selectedccs�d}tt�}|��}|j�d|�|��|_|��}x�|D]�}|�|j	�r^|j
jjd7_q:||kr�||j
|jkr�|d7}||j
d7<|Vq�|j�d|j	|j�|j
jjd7_q:|j�d|j	|�|j
jjd7_q:WdS)a=
        Schedule autotracing by sorted list taking into account the limits:
         - no same task for 10 days
         - limit per server
         - limit per domain
        ORDER OF RULES MUST NOT BE CHANGED: IT AFFECTS STATISTICS COUNTERS
        Return resulting list of URLs scheduled for auto tracing
        rzSorted scheduled list %srz#Skipped URL %s by domain limit (%s)z#Skipped URL %s by server limit (%s)N)rr,�urls_sortedrXrbr}r\rvr�rr]r@r7r$Zper_domain_limitr9r8)r_Zgeneral_tasks_counterZtasks_counter_per_domainZsorted_urlsZper_server_smart_limitrDr-r-r.rd�s.


zAutoTracer.urls_scheduledcCs,tt|���dd�dd�}t|dd�dd�S)zd
        Sort URLs by total number of requests first
        and by average duration second
        cSs|jS)N)r )�ur-r-r.�<lambda>��z(AutoTracer.urls_sorted.<locals>.<lambda>T)�key�reversecSs|jS)N)r#)r�r-r-r.r��r�)�sortedr[r>)r_Zfirst_serier-r-r.r��s
zAutoTracer.urls_sortedcCst�|_dS)z6
        Fill internal list of disabled users
        N)r)r_r-r-r.r��sz!AutoTracer.fill_in_disabled_usersc
Csp|jdk	rly,ttd��}t�t|j�|�WdQRXWn4tk
rj}z|j�dt	|��Wdd}~XYnXdS)zI
        Save collected statistics for current iteration to file
        N�wz*Unable to save iteration stats to file: %s)
r]�openr�json�dumpr�OSErrorrX�warningr*)r_�
stats_filerhr-r-r.rg�s
 zAutoTracer.save_iteration_statsc
CsvyFtt��4}t�|�}t|d|d|dtf|d��}WdQRXWn&ttjtfk
rlt|j	�}YnXt
|�S)z@
        Load statistics for latest iteration from file
        r<r=r>r@N)r�rr��loadr;r/r��JSONDecodeError�KeyErrorr<r)r_r��_dataZ	stat_datar-r-r.�load_iteration_stats�s

zAutoTracer.load_iteration_statscCs*|��}|�tt�dtt��d��|S)rGr)rZdisabled_users_quantity)r��updater�rrur)r_Zstats_loadedr-r-r.�	get_stats�s
zAutoTracer.get_stats)N)N)N)&r&r'r(r)rUri�staticmethodr�rjr*r
�objectrlr�ror,rvr[r}r�r�r
r�r�r�r�r	rr�r>rdrr�r�rgr�r��
__classcell__r-r-)r`r.rRis4

	
	
W,
rR�__main__zauto_tracer_standalone.log)�filename�level)<r)rZr�rV�collectionsrZdataclassesrrrrr�fnmatchr�typingr	r
rrr
�urllib.parserZnumpyr�Zssa.dbr�commonrZstoragerrrZautotracingrrrZ
configurationrZconfiguration.schemesr�internal.constantsr�internal.utilsrrZxray.shared_libraryZshared_libraryra�ImportErrorrr/r;rAr*rQrRr&�basicConfig�DEBUGr�r-r-r-r.�<module>
sJ
	



https://t.me/AnonymousX5 - 2025