HEX
Server: Apache
System: Linux zacp120.webway.host 4.18.0-553.50.1.lve.el8.x86_64 #1 SMP Thu Apr 17 19:10:24 UTC 2025 x86_64
User: govancoz (1003)
PHP: 8.3.26
Disabled: exec,system,passthru,shell_exec,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname
Upload Files
File: //lib/python3.6/site-packages/fail2ban/server/__pycache__/datedetector.cpython-36.opt-1.pyc
3

UB(dL�@s�dZdZdZddlZddlZddlmZddlmZm	Z	m
Z
mZmZm
Z
ddlmZdd	lmZd
dlmZee�ZdZejd
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��<)ZmaxCountZmaxTimecCs�|dkr|}d|kr|j�}tj|�}|s�d|kr~tj|�rNt|d|kd�}n0|dkrft|dkd�}n|dkr~t|dkd
d�}|dkr�|dkr�t|dkr�dndd�}nt|�}tj||�|S)N�%�EPOCH�LEPOCH)�pattern�longFrm�{^LN-BEG}EPOCH�^EPOCH)�
lineBeginOnly�{^LN-BEG}LEPOCH�^LEPOCHT)rr�TAI64N�{^LN-BEG}TAI64N�^TAI64N�startF)Z	wordBegin)rrr)rrr)rrr)	�upper�DD_patternCache�getr	�searchrrr�set)r�key�template�r%�"/usr/lib/python3.6/datedetector.py�_getPatternTemplate,s&

r'cCsd|S)Nz	{^LN-BEG}r%)�sr%r%r&�<lambda>Dsr)cCsb||j�}tj|�}|s^|t|d|j��}t|d�r>tj|�}|s^t|d�sVt|�}nt|�}|S)Nr)�namerr �getattr�regex�hasattrr')r$�wrapr*�	template2r,r%r%r&�_getAnchoredTemplateDs





r0c@sfeZdZdZdd�Zedd��Zdd�Zdd	d
ddd
dddddddddddgZedd��Z	dd�Z
dS)�DateDetectorCachez7Implements the caching of the default templates list.
	cCst�|_t�|_dS)N)r�_DateDetectorCache__lock�list�_DateDetectorCache__templates)�selfr%r%r&�__init__\szDateDetectorCache.__init__c	Cs8|jr|jS|j�|jr |jS|j�|jSQRXdS)z6List of template instances managed by the detector.
		N)r4r2�_addDefaultTemplate)r5r%r%r&�	templates`szDateDetectorCache.templatescCsd|j}|jd�rP|jd�rPt|d�rPt|�}|j|krPd|_|jdj|�|jdj|�dS)z&Cache Fail2Ban's default template.

		z	{^LN-BEG}�^r,gY@rrN)r*�
startswithr-r0�weight�_DateDetectorCache__tmpcache�append)r5r$r*r/r%r%r&�_cacheTemplatels"
z DateDetectorCache._cacheTemplatezF%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�r:x(ttj�D]\}}t|�}|tj|<qWtjS)Nr)�
isinstancer1�DEFAULT_TEMPLATES�str�	enumerater')r5�i�dtr%r%r&�defaultTemplates�s
z"DateDetectorCache.defaultTemplatescCsBggf|_x|jD]}|j|�qW|jd|jd|_|`dS)z<Add resp. cache Fail2Ban's default set of date templates.
		rrN)r<rEr>r4)r5rDr%r%r&r7�s

z%DateDetectorCache._addDefaultTemplateN)�__name__�
__module__�__qualname__�__doc__r6�propertyr8r>r@rEr7r%r%r%r&r1Ys.r1c@s0eZdZdZd
Zdd�Zedd	��Zd
d�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)Nri���)r$rLrMrN)r5r$r%r%r&r6�szDateDetectorTemplate.__init__cCs|j|jjtd|j�S)Nr)rLr$r;�maxrN)r5r%r%r&r;�szDateDetectorTemplate.weightcCst|j|�S)zF Returns attribute of template (called for parameters not in slots)
		)r+r$)r5r*r%r%r&�__getattr__�sz DateDetectorTemplate.__getattr__N)r$rLrMrN)	rFrGrHrI�	__slots__r6rJr;rPr%r%r%r&rK�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,r��r)rN)rSN)r3�_DateDetector__templatesr"�_DateDetector__known_names�_DateDetector__unusedTime�_DateDetector__lastPos�_DateDetector__lastEndPos�_DateDetector__lastTemplIdx�_DateDetector__firstUnusedZ_DateDetector__preMatch�_DateDetector__default_tz)r5r%r%r&r6�szDateDetector.__init__FcCsD|j}||jkr$|rdStd|��|jj|�|jjt|��dS)Nz(There is already a template with name %s)r*rU�
ValueError�addrTr=rK)r5r$�	ignoreDupr*r%r%r&�_appendTemplate�s

zDateDetector._appendTemplatecs�t|t�r�|�}d|kr"|j��tj��}|s��dkrR�fdd�}|j|�dSd�krl|j|dd	�dS�d
kr�td��}n
t|��}tj�|�|j|�t	j
dt|d
d�|j�t	j
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�	{^LN-BEG}�	{DEFAULT}cs�dkr|jtj@SdS)Nz	{^LN-BEG})�flagsr�
LINE_BEGIN)r$)r#r%r&r)sz-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`ra)r?rArrr �addDefaultTemplater'r"r_�logSys�infor+r*�debugr,)r5r$rZfltr%)r#r&�appendTemplates,




zDateDetector.appendTemplateNTcsnt|j�dk}xZ|rtjjntjjD]B}|dk	r<||�r<q$�dk	rXt|�fdd�d�}|j||d�q$WdS)z0Add Fail2Ban's default set of date templates.
		rNcstj�fdd���S)Ncs
tj��S)N)rZunboundPattern)�m)r(r%r&r)7szCDateDetector.addDefaultTemplate.<locals>.<lambda>.<locals>.<lambda>)�RE_DATE_PREMATCH�sub)r()rd)r(r&r)7sz1DateDetector.addDefaultTemplate.<locals>.<lambda>)r.)r^)�lenrTrR�	_defCacher8rEr0r_)r5ZfilterTemplaterdrer^r$r%)rdr&rg*szDateDetector.addDefaultTemplatecCs|jS)z6List of template instances managed by the detector.
		)rT)r5r%r%r&r8;szDateDetector.templatesc
Cs�t|j�s|j�tj�tkr$tjndd�}|tdd|�d}d}d}|j}|t|j�k�rh|j|}|j}|j	t
jt
jB@r�|tdd|�|j
|�}|}�n8|jd|jd}	}
|tdd	||	|
||	d|	�|jd||	|
�||
|
d�|jd
�
||	d|	�|jdk�sL||	|	d�|jd
k�r�|jd
j��r�||
|
d�|jd
k�s�||
d|
�|jdk�r�|jdj��r�|tdd||	|
��|j
||	|
�}n|tdd�|j
|�}|}|�r^|j�}	|j�}
t|j�dk�s4|j	t
jt
jB@�s4|	|jdk�rB|
|jdk�rB|td
|�n|td�||	|
|f}d}n
|td�|�s�|tdt|j��d}�x0|jD�]$}||k�r�|d7}�q�|tdd||j�|j}|j
|�}|�r�|j�}	|j�}
|td||	|j|jd|j�|dt|j�k�rP|j	t
jt
jB@�r.P|	dk�rX|j�rX|j|djj�rXP|	|jk�st|	|jdk�r�|td�|	|dk�r�||	|
|f}d}|d7}�q�P|d7}�q�W|�r�|d�r�|\}}	}
}|td|�|j|}|j}|�r�|jd7_tj�|_|	|_|j|k�r0|jd7_|	||	d|	�||	f|_|
||
d||
|
d�f|_|�r�||jk�r�|j|�}||_||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.
		cWsdS)Nr%)�argsr%r%r&r)Vsz(DateDetector.matchTime.<locals>.<lambda>rz"try to match time for line: %.120sN��z/  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.���)Nrrrrrs)NN)rorTrgrh�getEffectiveLevel�logLevel�logrYr$rbrrcZLINE_ENDZ	matchDaterWrX�isalnumr�endr*rNrL�timerMrZ�_reorderTemplate)r5�linerv�match�foundZignoreBySearchrC�ddtemplr$rN�endposr%r%r&�	matchTimeAs�




..
 



(


 

zDateDetector.matchTimecCs|jS)N)r[)r5r%r%r&�
default_tz�szDateDetector.default_tzcCst|�|_dS)N)r
r[)r5�valuer%r%r&r��scCs�|dkr|j|�}|d}|dk	r�yT|j||d|jd�}|dk	rttj�tkrptjtd|d|djd�|j�|SWnt	k
r�YnXdS)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\)r5r{Z	timeMatchr$Zdater%r%r&�getTime�s
zDateDetector.getTimecs��r�|j����tj�tkr0tjtd��j��j|j��j�|j	�krR|j	n�d�������fdd�}|�s���dkr��S�d�|�s��S��=�g��d�<x,|j	t
��kr؈|j	jr�|j	d7_	q�Wtj�tkr�tjtd����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c
sL��j}tj�tkr6tjtd���|�j��j��|kpJ���jkS)NzE  -> compare template #%02i & #%02i, weight %.3f > %.3f, hits %r > %r)r;rhrtrurvrLrM)Zpweight)r~�num�posr8�untimer;r%r&�	_moveable
s

z0DateDetector._reorderTemplate.<locals>._moveablerrz"  -> moved template #%02i -> #%02i)rTrhrtrurvrLrMrVr;rZro)r5r�r�r%)r~r�r�r8r�r;r&rz�s.zDateDetector._reorderTemplate)F)NNT)N)rFrGrHrIr1rpr6r_rkrgrJr8r�r��setterr�rzr%r%r%r&rR�s
	*


$rRi)N)"�
__author__Z
__copyright__Z__license__�copyryZ	threadingrZdatetemplaterrrrrr	Zstrptimer
ZutilsrZhelpersr
rFrhru�compile�
IGNORECASErmZCacherr'r0�objectr1rKrRr%r%r%r&�<module>s$ 
n