HEX
Server: Apache/2.4.65 (Debian)
System: Linux kubikelcreative 5.10.0-35-amd64 #1 SMP Debian 5.10.237-1 (2025-05-19) x86_64
User: www-data (33)
PHP: 8.4.13
Disabled: NONE
Upload Files
File: //usr/lib/python3/dist-packages/fail2ban/server/__pycache__/datedetector.cpython-39.pyc
a

��_
L�@s�dZdZdZddlZddlZddlmZddlmZm	Z	m
Z
mZmZm
Z
ddlmZdd	lmZd
dlmZee�ZdZe�d
ej�Zejddd�Zddd�Zdd�fdd�ZGdd�de�ZGdd�de�Z Gdd�de�Z!dS)z'Cyril Jaquier and Fail2Ban Contributorsz Copyright (c) 2004 Cyril JaquierZGPL�N)�Lock�)�re�DateTemplate�DatePatternRegex�
DateTai64n�	DateEpoch�RE_EPOCH_PATTERN)�validateTimeZone)�Utils�)�	getLogger�z(?<!\\)\{DATE\}i�i)ZmaxCountZmaxTimecCs�|dur|}d|vr|��}t�|�}|s�d|vr~t�|�rNt|d|vd�}n0|dvrft|dkd�}n|dvr~t|dkdd	�}|dur�|d
vr�t|dkr�dnd
d�}nt|�}t�||�|S)N�%�EPOCH�LEPOCH)�pattern�longFrm)rz{^LN-BEG}EPOCHz^EPOCH)�
lineBeginOnly)rz{^LN-BEG}LEPOCHz^LEPOCHT)rr)�TAI64Nz{^LN-BEG}TAI64Nz^TAI64Nr�startF)Z	wordBegin)	�upper�DD_patternCache�getr	�searchrrr�set)r�key�template�r�>/usr/lib/python3/dist-packages/fail2ban/server/datedetector.py�_getPatternTemplate,s&

r cCsd|S�N�	{^LN-BEG}r��srrr�<lambda>D�r%cCsb||j�}t�|�}|s^|t|d|j��}t|d�r>t�|�}|s^t|d�sVt|�}nt|�}|S)Nr)�namerr�getattr�regex�hasattrr )r�wrapr'�	template2r)rrr�_getAnchoredTemplateDs





r-c@sHeZdZdZdd�Zedd��Zdd�Zgd�Zed	d
��Z	dd�Z
d
S)�DateDetectorCachez7Implements the caching of the default templates list.
	cCst�|_t�|_dS�N)r�_DateDetectorCache__lock�list�_DateDetectorCache__templates��selfrrr�__init__\szDateDetectorCache.__init__cCs^|jr|jS|j�8|jr.|jWd�S|��|jWd�S1sP0YdS)�6List of template instances managed by the detector.
		N)r2r0�_addDefaultTemplater3rrr�	templates`szDateDetectorCache.templatescCs`|j}|�d�sL|�d�sLt|d�rLt|�}|j|krLd|_|jd�|�|jd�|�dS)z&Cache Fail2Ban's default template.

		r"�^r)gY@rrN)r'�
startswithr*r-�weight�_DateDetectorCache__tmpcache�append)r4rr'r,rrr�_cacheTemplatels
z DateDetectorCache._cacheTemplate)zF%ExY(?P<_sep>[-/.])%m(?P=_sep)%d(?:T|  ?)%H:%M:%S(?:[.,]%f)?(?:\s*%z)?z)(?:%a )?%b %d %k:%M:%S(?:\.%f)?(?: %ExY)?z$(?:%a )?%b %d %ExY %k:%M:%S(?:\.%f)?z1%d(?P<_sep>[-/])%m(?P=_sep)(?:%ExY|%Exy) %k:%M:%Sz=%d(?P<_sep>[-/])%b(?P=_sep)%ExY[ :]?%H:%M:%S(?:\.%f)?(?: %z)?z%m/%d/%ExY:%H:%M:%Sz%m-%d-%ExY %k:%M:%S(?:\.%f)?rz{^LN-BEG}%H:%M:%Sz^<%m/%d/%Exy@%H:%M:%S>z%Exy%Exm%Exd  ?%H:%M:%Sz%b %d, %ExY %I:%M:%S %pz^%b-%d-%Exy %k:%M:%Sz6%ExY%Exm%Exd(?:T|  ?)%ExH%ExM%ExS(?:[.,]%f)?(?:\s*%z)?z1(?:%Z )?(?:%a )?%b %d %k:%M:%S(?:\.%f)?(?: %ExY)?z1(?:%z )?(?:%a )?%b %d %k:%M:%S(?:\.%f)?(?: %ExY)?rcCs<ttjdt�r6ttj�D]\}}t|�}|tj|<qtjS)Nr)�
isinstancer.�DEFAULT_TEMPLATES�str�	enumerater )r4�i�dtrrr�defaultTemplates�s
z"DateDetectorCache.defaultTemplatescCs>ggf|_|jD]}|�|�q|jd|jd|_|`dS)z<Add resp. cache Fail2Ban's default set of date templates.
		rrN)r<rEr>r2)r4rDrrrr7�s


z%DateDetectorCache._addDefaultTemplateN)�__name__�
__module__�__qualname__�__doc__r5�propertyr8r>r@rEr7rrrrr.Ys
5
r.c@s0eZdZdZdZdd�Zedd��Zdd�Zd	S)
�DateDetectorTemplateztUsed for "shallow copy" of the template object.

	Prevents collectively usage of hits/lastUsed in cached templates
	�r�hits�lastUsed�distancecCs||_d|_d|_d|_dS)Nr��rL)r4rrrrr5�szDateDetectorTemplate.__init__cCs|j|jjtd|j�S)Nr)rMrr;�maxrOr3rrrr;�szDateDetectorTemplate.weightcCst|j|�S)zF Returns attribute of template (called for parameters not in slots)
		)r(r)r4r'rrr�__getattr__�sz DateDetectorTemplate.__getattr__N)	rFrGrHrI�	__slots__r5rJr;rRrrrrrK�s
rKc@szeZdZdZe�Zdd�Zddd�Zdd�Zddd�Z	e
d
d��Zdd�Ze
dd��Z
e
jdd��Z
ddd�Zdd�Zd	S)�DateDetectorzjManages one or more date templates to find a date within a log line.

	Attributes
	----------
	templates
	cCs>t�|_t�|_d|_d|_d|_d|_d|_d|_	d|_
dS)Ni,)rN)rPNrPr)r1�_DateDetector__templatesr�_DateDetector__known_names�_DateDetector__unusedTime�_DateDetector__lastPos�_DateDetector__lastEndPos�_DateDetector__lastTemplIdx�_DateDetector__firstUnusedZ_DateDetector__preMatch�_DateDetector__default_tzr3rrrr5�szDateDetector.__init__FcCsD|j}||jvr$|rdStd|��|j�|�|j�t|��dS)Nz(There is already a template with name %s)r'rV�
ValueError�addrUr=rK)r4r�	ignoreDupr'rrr�_appendTemplate�s
�zDateDetector._appendTemplatecs�t|t�r�|�}d|vr"|���t���}|s��dvrR�fdd�}|�|�dSd�vrl|j|dd�dS�d	kr�td
��}n
t|��}t��|�|�|�t	�
dt|dd
�|j�t	�
dt|dd
�|j�dS)a�Add a date template to manage and use in search of dates.

		Parameters
		----------
		template : DateTemplate or str
			Can be either a `DateTemplate` instance, or a string which will
			be used as the pattern for the `DatePatternRegex` template. The
			template will then be added to the detector.

		Raises
		------
		ValueError
			If a template already exists with the same name.
		r)r"z	{DEFAULT}cs�dkr|jtj@SdSr!)�flagsr�
LINE_BEGIN)r�rrrr%r&z-DateDetector.appendTemplate.<locals>.<lambda>Nz{DATE}F)�preMatch�allDefaultsz{NONE}z{UNB}^z  date pattern `%r`: `%s`r�z  date pattern regex for %r: %s)r?rArrr�addDefaultTemplater rr`�logSys�infor(r'�debugr))r4rrZfltrrcr�appendTemplates2


�


��zDateDetector.appendTemplateNTcsht|j�dk}|rtjjntjjD]@}|dur8||�s8q"�durTt|�fdd�d�}|j||d�q"dS)z0Add Fail2Ban's default set of date templates.
		rNcst��fdd���S)Ncs
t���Sr/)rZunboundPattern)�mr#rrr%7r&zCDateDetector.addDefaultTemplate.<locals>.<lambda>.<locals>.<lambda>)�RE_DATE_PREMATCH�subr#�rdr#rr%7r&z1DateDetector.addDefaultTemplate.<locals>.<lambda>)r+)r_)�lenrUrT�	_defCacher8rEr-r`)r4ZfilterTemplaterdrer_rrrorrg*s�
�zDateDetector.addDefaultTemplatecCs|jS)r6)rUr3rrrr8;szDateDetector.templatesc
Cs�t|j�s|��t��tkr$tjndd�}|tdd|�d}d}d}|j}|t|j�k�rX|j|}|j}|j	t
jt
jB@r�|tdd|�|�
|�}|}�n(|jd	|jd	}	}
|tdd
||	|
||	d|	�|jd||	|
�||
|
d�|jd�
||	d|	�|jdk�sB||	|jdk�r�|jd���s�||
|
d�|jdk�s�||
d|jdk�r�|jd���s�|tdd||	|
��|�
||	|
�}n|tdd
�|�
|�}|}|�rN|��}	|��}
t|j�dk�s$|j	t
jt
jB@�s$|	|jd	k�r2|
|jd	k�r2|td|�n|td�||	|
|f}d}n
|td�|�s�|tdt|j��d	}|jD�]2}||k�r�|d7}�qz|tdd||j�|j}|�
|�}|�r�|��}	|��}
|td||	|j|jd	|j�|dt|j�k�r�q�|j	t
jt
jB@�r"�q�|	d	k�rN|j�rN|j|djj�sN�q�|	|jk�sj|	|jd	k�r�|td�|	|dk�r�||	|
|f}d}|d7}�qz�q�|d7}�qz|�s�|d	�r�|\}}	}
}|td|�|j|}|j}|�r�|jd7_t��|_|	|_|j|k�r&|jd7_|	||	d|	�||	f|_|
||
d||
|
d�f|_|�r~||jk�r~|�|�}||_||fS|td�dS)a�Attempts to find date on a log line using templates.

		This uses the templates' `matchDate` method in an attempt to find
		a date. It also increments the match hit count for the winning
		template.

		Parameters
		----------
		line : str
			Line which is searched by the date templates.

		Returns
		-------
		re.MatchObject, DateTemplate
			The regex match returned from the first successfully matched
			template.
		cWsdSr/r)�argsrrrr%Vr&z(DateDetector.matchTime.<locals>.<lambda>rz"try to match time for line: %.120sN)NrPrP���rPz/  try to match last anchored template #%02i ...rzJ  try to match last template #%02i (from %r to %r): ...%r==%r %s %r==%r...rz+  boundaries are correct, search in part %rz,  boundaries show conflict, try whole searchz"  matched last time template #%02izB  ** last pattern collision - pattern change, reserve & search ...z8  ** last pattern not found - pattern change, search ...z search template (%i) ...z  try template #%02i: %sz2  matched time template #%02i (at %r <= %r, %r) %sz1  ** distance collision - pattern change, reservez  use best time template #%02iz
 no template.)NN)rprUrgrh�getEffectiveLevel�logLevel�logrZrrarrbZLINE_ENDZ	matchDaterXrY�isalnumr�endr'rOrM�timerNr[�_reorderTemplate)r4�linerv�match�foundZignoreBySearchrC�ddtemplrrO�endposrrr�	matchTimeAs�




�������
����



�&


 

zDateDetector.matchTimecCs|jSr/)r\r3rrr�
default_tz�szDateDetector.default_tzcCst|�|_dSr/)r
r\)r4�valuerrrr��scCs�|dur|�|�}|d}|dur�zV|j||d|jd�}|durvt��tkrpt�td|d|d�d�|j�|WSWnt	y�Yn0dS)aAttempts to return the date on a log line using templates.

		This uses the templates' `getDate` method in an attempt to find
		a date. 
		For the faster usage, always specify a parameter timeMatch (the previous tuple result
		of the matchTime), then this will work without locking and without cycle over templates.

		Parameters
		----------
		line : str
			Line which is searched by the date templates.

		Returns
		-------
		float
			The Unix timestamp returned from the first successfully matched
			template or None if not found.
		Nrr)r�z&  got time %f for %r using template %s)
r�ZgetDater\rhrtrurv�groupr'r])r4r{Z	timeMatchr�daterrr�getTime�s
�
zDateDetector.getTimecs��r�|j����t��tkr0t�td��j��j|j��j�|j	�krR|j	n�d�������fdd�}|�s���dkr��S�d�|�s��S��=�g��d�<|j	t
��krֈ|j	jr�|j	d7_	q�t��tkr�t�td����S�S)z�Reorder template (bubble up) in template list if hits grows enough.

		Parameters
		----------
		num : int
			Index of template should be moved.
		z%  -> reorder template #%02i, hits: %rrcsL��j}t��tkr6t�td���|�j��j��|kpJ���jkS)NzE  -> compare template #%02i & #%02i, weight %.3f > %.3f, hits %r > %r)r;rhrtrurvrMrN)Zpweight�r~�num�posr8Zuntimer;rr�	_moveable
s
�z0DateDetector._reorderTemplate.<locals>._moveablerrz"  -> moved template #%02i -> #%02i)rUrhrtrurvrMrNrWr;r[rp)r4r�r�rr�rrz�s.zDateDetector._reorderTemplate)F)NNT)N)rFrGrHrIr.rqr5r`rkrgrJr8r�r��setterr�rzrrrrrT�s 
	*





$rT)N)"�
__author__Z
__copyright__Z__license__�copyryZ	threadingrZdatetemplaterrrrrr	�strptimer
ZutilsrZhelpersr
rFrhru�compile�
IGNORECASErmZCacherr r-�objectr.rKrTrrrr�<module>s$ 
n