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: //usr/lib/python3.6/site-packages/fail2ban/server/__pycache__/observer.cpython-36.opt-1.pyc
3

UB(d�D�@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. BresterZGPL�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-��ZdId.d/�ZdJd0d1�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)K�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|_tj�|_g|_tj�|_	d|_
i|_d|_d|_
d|_d|_dS)Nzf2b/observer)�nameT�<Fi)�superr	�__init__�idle�	threading�RLock�_queue_lock�_queueZEvent�_notify�	sleeptime�_timers�_paused�_ObserverThread__db�"_ObserverThread__db_purge_intervalZdaemon)�self)�	__class__��/usr/lib/python3.6/observer.pyr?s

zObserverThread.__init__cCs0y
|j|Stk
r*td|��YnXdS)NzInvalid event index : %s)r�KeyError)r�irrr�__getitem__Ts
zObserverThread.__getitem__cCs2y|j|=Wn tk
r,td|��YnXdS)NzInvalid event index: %s)rr)rrrrr�__delitem__ZszObserverThread.__delitem__cCs
t|j�S)N)�iterr)rrrr�__iter__`szObserverThread.__iter__cCs
t|j�S)N)�lenr)rrrr�__len__cszObserverThread.__len__cCsdS)NFr)r�otherrrr�__eq__fszObserverThread.__eq__cCst|�S)N)�id)rrrr�__hash__iszObserverThread.__hash__cGsD|jj|d�}|dk	r|j�tj||j|�}||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�getZcancelr�Timer�add�start)rr�	starttime�event�trrr�add_named_timerls
zObserverThread.add_named_timercGs^tjdk	rB|rBtjtj|jtj�|tj�||f�}|j�dStj||j	|�}|j�dS)zJAdd a timer event to queue will start (and wake) in 'starttime' seconds
		N)
rZmyTimerr+r�DEFAULT_SLEEP_INTERVAL�
_delayedEvent�timer-r,)rr.r/r0rrr�	add_timeryszObserverThread.add_timercCsNtj�|kstj�|kr*|jd|��dStjtj|j|||f�}|j�dS)Nr)r)	rr4r5rr+rr2r3r-)rZ	endMyTimeZendTimer/r0rrrr3�szObserverThread._delayedEventcCs|js|j}|r|j�dS)z1Notify wakeup (sets /and resets/ notify event)
		N)rr�set)r�nrrr�pulse_notify�szObserverThread.pulse_notifyc	Gs*|j�|jj|�WdQRX|j�dS)z5Add a event to queue and notify thread to wake up.
		N)rr�appendr8)rr/rrrr,�szObserverThread.addc	Gs"|j�|jj|�WdQRXdS)z=Add a event to queue withouth notifying thread to wake up.
		N)rrr9)rr/rrr�add_wn�szObserverThread.add_wncGs||�dS)Nr)r�l�argsrrr�call_lambda�szObserverThread.call_lambdacCs�tjd�|jd|jd�|j|j|j|j|j|j	|j
dd�dd�d�	}�y8|jd��x|j�rfd	|_
x�|j�syrd
}|j�t|j�r�|jjd�}Wd
QRX|d
kr�P|d}t|d�s�|j|�p�t||�}||dd
��Wqjtk
�r}ztjd
|dd�WYd
d
}~XqjXqjW|j}|�rPd|_
|j|j�|j�|j�rdqZqZtjtj�|j sZPqZWtjdt|j��d
|_Wn4tk
�r�}ztjd|dd�WYd
d
}~XnX|j�g|_Wd
QRXd|_
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_purgecSsfS)Nrrrrr�<lambda>�sz$ObserverThread.run.<locals>.<lambda>cSsfS)Nrrrrrr@�s)	Zcall�db_setr?�is_aliveZ	is_activer-�stop�nop�shutdownrBFNrrz%sT)�exc_infoz&Observer stopped, %s events remaining.z Observer stopped after error: %s)!�logSys�infor1rr=rAr?�isAlive�isActiver-rCr,�activerrrr$r�pop�callabler*�getattr�	Exception�errorr�waitr�clearr4�sleepr	r2�is_full)rZ_ObserverThread__methZev�meth�er7rrr�run�s^




&
"zObserverThread.runcCsdS)NTr)rrrrrI�szObserverThread.isAliveNcCs|jS)N)rK)rZfromStrrrrrJszObserverThread.isActivec
s*|j�|jstt|�j�WdQRXdS)N)rrKr
r	r-)r)rrrr-szObserverThread.start�TcCs�|jr�|jr�tjd|�|j�&|jd�|j}|jj�d|_WdQRX|j|�sX|rt|j�d|_d|_	d|_
n||_|jt|d��o�|j
SdS)Nz-Observer stop ... try to end queue %s secondsrEFTg�?)rKrrGrHrr:r6�
wait_emptyrRrr�	wait_idle�minrT)rZwtimeZ	forceQuitr7rrrrCs

zObserverThread.stopc	Cs$|j�t|j�rdSdSQRXdS)NTF)rr$r)rrrrrT#szObserverThread.is_fullcCs�tjtj�|dk	r tj�|}|jdk	rH|jd�|jrH|jrH|j	�x,|jrt|dk	rftj�|krfPtjtj�qJW|j
d�|jS)zWWait observer is running and returns if observer has no more events (queue is empty)
		NrDg����MbP?)r4rSr	r2rrr:rTrr8rZ)rrrVrrrrY(s


zObserverThread.wait_emptycCs^tjtj�|jrdS|dk	r*tj�|}x,|jsV|dk	rHtj�|krHPtjtj�q,W|jS)zJWait observer is running and returns if observer idle (observer sleeps)
		TN)r4rSr	r2rr)rrrVrrrrZ<szObserverThread.wait_idlecCs|jS)N)r)rrrr�pausedJszObserverThread.pausedcCs |j|krdS||_|j�dS)N)rr8)r�pauserrrr\Ns
cCsdS)z/Status of observer to be implemented. [TODO]
		�)r^r^r)rrrr�statusWszObserverThread.statuscCs
||_dS)N)r)r�dbrrrrAaszObserverThread.db_setcCs2tjd�|jdk	r|jj�|jd|jd�dS)NzPurge database event occurredr>r?)rG�debugr�purger1r)rrrrr?ds


zObserverThread.db_purgecCs�|j�s|jd�rdS|j�}|j�}tjd|j|�d}d}d}�y|jjj	�}|j
}	|	dk	r�xF|	j||�D]6\}}}
t||j
��}d|dkr�|nd>dd}PqrWt||�}|dk	r�||kr�tjd|j|||�dS|dkr�dStjd	|j|tj|�||||k�rd
nd�|jjj||dd�}|j|�||k�rL|jj|�Wn>tk
�r�}z tjd
|tj�tjkd�WYdd}~XnXdS)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^Tz%s)rF)rI�getBanTimeExtra�getID�getTimerGrar�filterZfailManagerZgetMaxRetry�database�getBan�max�getBanCountr[rHr�time2strZ
addFailure�setBanCountZ
performBanrOrP�getEffectiveLevel�logging�DEBUG)r�jail�ticket�ipZunixTime�banCountZ
retryCount�	timeOfBanZmaxRetryr`�lastBanTimerVrrr�failureFoundos@


zObserverThread.failureFoundc@seZdZdd�ZdS)zObserverThread.BanTimeIncrcCs||_||_dS)N)ZTimeZCount)r�banTimerurrrr�sz#ObserverThread.BanTimeIncr.__init__N)�__name__�
__module__�__qualname__rrrrr�BanTimeIncr�sr}cCs|j�}|d|j||��S)N�	evformula)rer})rrrryru�berrr�calcBanTime�szObserverThread.calcBanTimecCsD|j�s|jr|S|j�}|j�}|}y�|dkr�|jdd�r�x�|jj|||jdd�d�D]�\}}}	||j�kr�|j|d�tj	d|||�|dkr�|d|j
||��}|j|�|j�|kr�tj
d	|j||tj|�tj|�tj|�f�nd
|_Pq\WWn>tk
�r>}
z tjd|
tj�tjkd�WYd
d
}
~
XnX|S)ztCheck for IP address to increment ban time (if was already banned).

		Returns
		-------
		float
			new ban time.
		rrcF�overalljails)r�rz"IP %s was already banned: %s #, %sr~z/[%s] IP %s is bad: %s # last %s - incr %s to %sTz%s)rFN)rIrirerfr*rjrlrnrGrar}�
setBanTimergrHrrrm�seconds2str�restoredrOrProrprq)rrrryrsrrtZ
orgBanTimerurvrwrVrrr�incrBanTime�s0	$

,zObserverThread.incrBanTimec	Cs�|jr
dS�y4|}|j�}tjd|j||�|dkrh|j�dkrh|j|||�}|dks^||krh|j|�|dkr�|j�|}t	j
|�t	j|�f}|t	j�kr�tjd|d�dSnd}||k�rtj
d|j||j�f|��tjd	d
|j|||f�|jtdtd||d	��|j||�|jdk	�r>|j�r>|jj||�Wn>tk
�r~}z tjd
|tj�tjkd�WYdd}~XnXdS)z� 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, %srzIgnore old bantime %sF�	permanent�infinitez$[%s] Increase Ban %s (%d # %s -> %s)rXz[%s] Observer: prolong %s in %sr
rz%s)rF���r�r�)r�r�)r�rfrGrar�
getBanTimer�r�rgrr�rmr4Znoticerl�logr5r[rk�
prolongBanriZaddBanrOrProrprq)	rrsrr�btimeZoldbtimertZbendtimeZlogtimerVrrr�banFound�s4


&zObserverThread.banFoundcCsty2|j�}|j�}tjd|j||�|jj|�Wn<tk
rn}z tjd|tj	�t
jkd�WYdd}~XnXdS)z� 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
		z[%s] Observer: prolong %s, %sz%s)rFN)r�rfrGrarZactionsZ_prolongBanrOrProrprq)rrsrrr�rtrVrrrr��szObserverThread.prolongBan)N)rXT)N)N))rzr{r|�__doc__rr2rr r!r#r%r'r)r1r5r3r8r,r:r=rWrIrJr-rC�propertyrTrYrZr\�setterr_rAr?rxr}r�r�r�r��
__classcell__rr)rrr	(sF


	P



	
2)*r	c@seZdZdd�ZdS)�
_ObserverscCs
d|_dS)N)ZMain)rrrrrsz_Observers.__init__N)rzr{r|rrrrrr�
sr�)�
__author__Z
__copyright__Z__license__rZ
jailthreadrZfailmanagerr�osrpr4ZdatetimeZmathZjsonZrandom�sysZhelpersrZmytimerZutilsrrzrGr	r�Z	Observersrrrr�<module>s"8h