HOME


Mini Shell 1.0
Redirecting to https://devs.lapieza.net/iniciar-sesion Redirecting to https://devs.lapieza.net/iniciar-sesion.
DIR: /lib/python3/dist-packages/fail2ban/server/__pycache__/
Upload File :
Current File : //lib/python3/dist-packages/fail2ban/server/__pycache__/observer.cpython-311.pyc
�

G�kc�D���dZdZdZddlZddlmZddlmZddlZddl	Z	ddl
Z
ddlZddlZddl
Z
ddlZddlZdd	lmZdd
lmZddlmZee��ZGd�d
e��ZGd�d��Ze��ZdS)zSerg G. Brester (sebres)z"Copyright (c) 2014 Serg G. Brester�GPL�N�)�
JailThread)�FailManagerEmpty�)�	getLogger)�MyTime)�Utilsc�`��eZdZdZejdzZ�fd�Zd�Zd�Zd�Z	d�Z
d�Zd	�Zd
�Z
d�Zd�Zd
�Zd�Zd�Zd�Zd�Zd�Zd(d�Z�fd�Zd)d�Zed���Zd(d�Zd(d�Zed���Zejd���Zed���Zd�Z d �Z!d!�Z"Gd"�d#��Z#d$�Z$d%�Z%d&�Z&d'�Z'�xZ(S)*�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.
	�
c�*��tt|���d���d|_t	j��|_g|_t	j��|_	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__s ��:/usr/lib/python3/dist-packages/fail2ban/server/observer.pyrzObserverThread.__init__?s���������&�&�N�&�;�;�;��$�)��_�&�&�$���$�+���"�"�$�,��$�.��$�,��$�,��$�)�"�$���$�+�+�+�c�^�	|j|S#t$rtd|z���wxYw)NzInvalid event index : %s�r�KeyError�r!�is  r#�__getitem__zObserverThread.__getitem__TsB��2�
�+�a�.���	�2�2�2�	�,�q�0�	1�	1�1�2���s��,c�X�	|j|=dS#t$rtd|z���wxYw)NzInvalid event index: %sr&r(s  r#�__delitem__zObserverThread.__delitem__ZsC��1��{�1�~�~�~��	�1�1�1�	�+�a�/�	0�	0�0�1���s��)c�*�t|j��S�N)�iterr�r!s r#�__iter__zObserverThread.__iter__`s��	
�d�k�	�	�r$c�*�t|j��Sr.)�lenrr0s r#�__len__zObserverThread.__len__cs��	�T�[�	�	�r$c��dS)NF�)r!�others  r#�__eq__zObserverThread.__eq__fs��	�r$c� �t|��Sr.)�idr0s r#�__hash__zObserverThread.__hash__is
��	�D���/r$c���|j�|d��}|�|���tj||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)r!r�	starttime�event�ts     r#�add_named_timerzObserverThread.add_named_timerls_���l���t�T�"�"�!��]��8�8�:�:�:��o�i���5�1�1�!��$�,�t���'�'�)�)�)�)�)r$c�L�tj�h|rftjtj|jtj��|ztj��|z|f��}|���dStj||j	|��}|���dS)zJAdd a timer event to queue will start (and wake) in 'starttime' seconds
		N)
r	�myTimerr?r
�DEFAULT_SLEEP_INTERVAL�
_delayedEvent�timerAr@)r!rBrCrDs    r#�	add_timerzObserverThread.add_timerys����]��9����u�3�T�5G��[�]�]�Y���	���i� 7��?���1��7�7�9�9�9�	�6��o�i���5�1�1�!��'�'�)�)�)�)�)r$c��tj��|kstj��|kr|jdg|�R�dStjt
j|j|||f��}|���dS)Nr)	r	rJrKrr?r
rHrIrA)r!�	endMyTime�endTimerCrDs     r#rIzObserverThread._delayedEvent�sz���[�]�]�i���4�9�;�;�'�#9�#9��4�>�!��e�����	�6��o�e�2�D�4F�
�w�����!��'�'�)�)�)�)�)r$c�V�|js|j}|r|���dSdSdS)z1Notify wakeup (sets /and resets/ notify event)
		N)rr�set)r!�ns  r#�pulse_notifyzObserverThread.pulse_notify�sB��
����|�1����E�E�G�G�G�G�G����r$c��|j5|j�|��ddd��n#1swxYwY|���dS)z5Add a event to queue and notify thread to wake up.
		N)rr�appendrR�r!rCs  r#r@zObserverThread.add�s��������;���e�����������������������������s�/�3�3c�z�|j5|j�|��ddd��dS#1swxYwYdS)z=Add a event to queue withouth notifying thread to wake up.
		N)rrrTrUs  r#�add_wnzObserverThread.add_wn�s��������;���e������������������������s�0�4�4c��||�dSr.r6)r!�l�argss   r#�call_lambdazObserverThread.call_lambda�s���!�T�(�(�(�(r$c
��t�d��|�d|jd��|j|j|j|j|j|j	|j
d�d�d�	}	|�d��|j�rbd|_
|js�	d	}|j5t!|j��r|j�d
��}d	d	d	��n#1swxYwY|�n�|d
}t'|d
��s%|�|��pt+||��}||dd	��n4#t,$r'}t�d|d
���Yd	}~nd	}~wwxYw|j��|j}|r?d
|_
|�|j��|���|jr��;n&t9jt<j��|j sn|j��bt�dt!|j����d	|_n4#t,$r'}t�d|d
���Yd	}~nd	}~wwxYw|j5g|_d	d	d	��n#1swxYwYd
|_
d
S)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_purgec��dS�Nr6r6r6r$r#�<lambda>z$ObserverThread.run.<locals>.<lambda>�s���r$c��dSr`r6r6r$r#raz$ObserverThread.run.<locals>.<lambda>�s��b�r$)	�call�db_setr^�is_alive�	is_activerA�stop�nop�shutdownreFNrr�%sT��exc_infoz&Observer stopped, %s events remaining.z Observer stopped after error: %s)!�logSys�inforErr[rdr^�isAlive�isActiverArgr@�activerrrr3r�pop�callabler=�getattr�	Exception�errorr�waitr�clearrJ�sleeprrH�is_full)r!�_ObserverThread__meth�ev�meth�erQs      r#�runzObserverThread.run�s:��	�+�+�!�"�"�"����z�4�#;�Z�H�H�H���
�[��}����
��J��9�
�)��y�
�
�&�,F��8�8�J����	
��"��D�I��l�+�+��b��� � �

�d�k�
�
� �
�[�_�_�Q�
�
�� � � � � � � � � � � ���� � � � �
���
��U�d�
�b��e�_�_�L�V�Z�Z��%5�%5�%L���t�9L�9L�d�
�d�B�q�r�r�F�m�m�m���+�+�+��l�l�4��T�l�*�*�*�*�*�*�*�*�����+�����l�+�$	
��A����T�Y��V�V�D�N�����W�W�Y�Y�Y������
�Z��5�6�6�6��L���E
��"�H
�;�;�7��T�[�9I�9I�J�J�J��4�<�<��	�F�F�F�	�<�<�2�A��<�E�E�E�E�E�E�E�E�����F���������4�;��������������������$�)�	
�s��)+H�	D7�/C�
D7�C�D7� C�!D7�&H�'AD7�6H�7
E(�E#�H�#E(�(B4H�
I�'I	�	I�I,�,I0�3I0c��dS)NTr6r0s r#rozObserverThread.isAlive�s��	
�r$Nc��|jSr.)rq)r!�fromStrs  r#rpzObserverThread.isActives��
��r$c���|j5|js'tt|�����ddd��dS#1swxYwYdSr.)rrqrrrAr s �r#rAzObserverThread.starts������(�(�
�+�(�	�.�$���%�%�'�'�'�(�(�(�(�(�(�(�(�(�(�(�(����(�(�(�(�(�(s�/A�A	�A	�Tc���|jr�|jr�t�d|��|j5|�d��|j}|j���d|_ddd��n#1swxYwY|�|��s|r*|���d|_d|_	d|_
n||_|�t|d����o|j
SdS)Nz-Observer stop ... try to end queue %s secondsriFTg�?)rqrrmrnrrWrP�
wait_emptyrxrr�	wait_idle�minrz)r!�wtime�	forceQuitrQs    r#rgzObserverThread.stops/��	�[�?�T�\�?�	�;�;�>��F�F�F�	
�����K�K�
������A��L�������D�L�
�������������������o�o�e����	���G�G�I�I�I��D�K��D�L��D�I�I��D�L�
�.�.��U�C���
)�
)�
>�$�,�.>�>�	
�s�=A:�:A>�A>c�t�|j5t|j��rdndcddd��S#1swxYwYdS)NTF)rr3rr0s r#rzzObserverThread.is_full#s�����.�.��d�k�"�"�
-�$�$��.�.�.�.�.�.�.�.�.�.�.�.����.�.�.�.�.�.s�-�1�1c��tjtj��|�t	j��|z}|j�7|�d��|jr|jr|�	��|jr?|�t	j��|krn%tjtj��|j�?|�
d��|jS)zWWait observer is running and returns if observer has no more events (queue is empty)
		Nrhg����MbP?)rJryrrHr	rrWrzrrRr��r!rr~s   r#r�zObserverThread.wait_empty(s����*�^�
2�3�3�3���
�{�}�}�y� �1�	�\���;�;�u����
�l��t�y����������5�����
�
�� 1� 1�	��:�n�3�4�4�4�	
��5�
�.�.�����
�\�	�r$c��tjtj��|jrdS|�tj��|z}|js?|�tj��|krn%tjtj��|j�?|jS)zJWait observer is running and returns if observer idle (observer sleeps)
		T)rJryrrHrr	r�s   r#r�zObserverThread.wait_idle<s����*�^�
2�3�3�3�	�Y��
�$���
�{�}�}�y� �1��I�5�����
�
�� 1� 1�	��:�n�3�4�4�4�
�I�5�
��r$c��|jSr.)rr0s r#�pausedzObserverThread.pausedJs
��	
��r$c�V�|j|krdS||_|���dSr.)rrR)r!�pauses  r#r�zObserverThread.pausedNs3��	�\�U���	�6��$�,��������r$c��dS)z/Status of observer to be implemented. [TODO]
		)�r�r6r0s r#�statuszObserverThread.statusWs	��
�r$c��||_dSr.)r)r!�dbs  r#rdzObserverThread.db_setas
���$�)�)�)r$c��t�d��|j�|j���|�d|jd��dS)NzPurge database event occurredr]r^)rm�debugr�purgerErr0s r#r^zObserverThread.db_purgedsP���,�,�.�/�/�/�	�Y���9�?�?�������z�4�#;�Z�H�H�H�H�Hr$c
�`�|���r|�d��sdS|���}|���}t�d|j|��d}d}d}	|jj�	��}|j
}	|	��|	�||��D];\}}}
t||�
����}d|dkr|ndzdzdz}t||��}|�+||kr%t�d|j|||��dS|dkrdSt�d	|j|t!j|��||||krd
nd��|jj�||dz
d��}|�|��||kr|j�|��dSdS#t*$rM}t�d
|t���t0jk���Yd}~dSd}~wwxYw)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�Trjrk)ro�getBanTimeExtra�getID�getTimermr�r�filter�failManager�getMaxRetry�database�getBan�max�getBanCountr�rnr	�time2str�
addFailure�setBanCount�
performBanrurv�getEffectiveLevel�logging�DEBUG)r!�jail�ticket�ip�unixTime�banCount�
retryCount�	timeOfBan�maxRetryr��lastBanTimer~s            r#�failureFoundzObserverThread.failureFoundos\��
�����
�t�3�3�K�@�@�
�	�6�
�|�|�~�~�"�
�^�^�
�
�(��,�,�0�$�)�R�@�@�@�
�(��*��)� M��k�%�1�1�3�3�8��
�2��n�,.�I�I�b�$�,?�,?���(��)�[��H�f�0�0�2�2�3�3�X��h��m�m�(�(��<�a�?�!�C�Z���Z��*�*�J����Y�!6�!6��\�\�L��	�2�x��,�,�,��V��A�o�o�
�F�	�;�;�6��	�2�
�O�H���x���h�&�&�W�W�B�1�1�1���'�2�2�6�:��>�4�P�P�:�	���h�����H����K���2���������
�M�M�M�	�<�<��a�&�":�":�"<�"<�g�m�"K�<�L�L�L�L�L�L�L�L�L�����M���s'�<B4G�2G�:BG�
H-� AH(�(H-c��eZdZd�ZdS)�ObserverThread.BanTimeIncrc�"�||_||_dSr.)�Time�Count)r!�banTimer�s   r#rz#ObserverThread.BanTimeIncr.__init__�s���4�9��4�:�:�:r$N��__name__�
__module__�__qualname__rr6r$r#�BanTimeIncrr��s#����������r$r�c�t�|���}|d|�||����S)N�	evformula)r�r�)r!r�r�r��bes     r#�calcBanTimezObserverThread.calcBanTime�s7�������"�	��K���)�)�'�8�<�<�	=�	=�=r$c� �|���r|js|S|���}|���}|}	|dk�rd|�dd���rM|j�|||�dd�����D�]\}}}	||���kr|�|dz��t�	d|||��|dkr%|d|�
||����}|�|��|���|krht�
d	|j�d
|�d|�dtj|���d
tj|���dtj|������nd|_nY#t&$rL}
t�d|
t���t,jk���Yd}
~
nd}
~
wwxYw|S)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] IP z	 is bad: z # last z - incr z to TrjrkN)ror�r�r�r=r�r�r�rmr�r��
setBanTimer�rnrr	r��seconds2str�restoredrurvr�r�r�)r!r�r�r�r�r��
orgBanTimer�r�r�r~s           r#�incrBanTimezObserverThread.incrBanTime�s)��
������t�}��
�>������"�
�|�|�~�~�"��*�M�
��k�k�b�f�f�[�%�0�0�k�
�]���"�d������1N�1N��O�O���(��)�[�	�F�&�&�(�(�(�(�����!��$�$�$��\�\�6��H�i�P�P�P��1�����;��� 0� 0��(� C� C�D�D�g����w���������9�$�$��k�k�k�t�y�y�y�RT�RT�RT�V^�V^�V^�
��y�!�!�!�!�
��*�%�%�%�%�v�'9�'�'B�'B�'B�D�E�E�E�E��f�o�
���	�M�M�M�	�<�<��a�&�":�":�"<�"<�g�m�"K�<�L�L�L�L�L�L�L�L�����M����	�.s�	E+F5�5
H�?AH�Hc
�l�|jrdS	|}|���}t�d|j||��|dkrL|����8|�|||��}|dks||kr|�|��|dkrz|���|z}tj
|��tj|��f}|tj��kr#t�d|d��dSnd}||kr�tj
d|j||���g|�R�t�d	d
|j|||f��|�t#dt%d||z
d	z
����|j||��|j�$|js|j�||��dSdSdS#t,$rM}t�d
|t���t2jk���Yd}~dSd}~wwxYw)�� 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���zIgnore old bantime %srF)�	permanent�infinitez$[%s] Increase Ban %s (%d # %s -> %s)r�z[%s] Observer: prolong %s in %sr
rrjrk)r�r�rmr�r�
getBanTimer�r�r�r	r�r�rJ�noticer��logrKr�r��
prolongBanr��addBanrurvr�r�r�)	r!r�r��btime�oldbtimer��bendtime�logtimer~s	         r#�banFoundzObserverThread.banFound�sS���_�
�	�6� M��8������2�	�<�<�1�4�9�b�%�H�H�H��r�k�k�f�'�'�)�)�1����T�5�&�1�1�E���{�{�e�h�&�&����u�����r�k�k��~�~���%�'�H��!�%�(�(�&�/�(�*C�*C�D�G��&�+�-�-����\�\�)�7�1�:�6�6�6��E� �(�G��x���
�M�8�$�)���	�	�	�	�)� '�)�)�)�)��J�J�q�3�T�Y��U�H�DU�V�V�V��N�N�3�r�3�q�%�(�"2�Q�"6�7�7�8�8�$�/�6�SW�X�X�X�
�m������M����v�&�&�&�&�&� �����
�M�M�M�	�<�<��a�&�":�":�"<�"<�g�m�"K�<�L�L�L�L�L�L�L�L�L�����M���s �DG�CG�
H3�&AH.�.H3c��	|���}|���}t�d|j||��|j�|��dS#t$rM}t�d|t�	��tjk���Yd}~dSd}~wwxYw)r�z[%s] Observer: prolong %s, %srjrkN)r�r�rmr�r�actions�_prolongBanrurvr�r�r�)r!r�r�r�r�r~s      r#r�zObserverThread.prolongBan�s���M������5������2�	�<�<�/���B��F�F�F��<���F�#�#�#�#�#��	�M�M�M�	�<�<��a�&�":�":�"<�"<�g�m�"K�<�L�L�L�L�L�L�L�L�L�����M���s�A$A(�(
B?�2AB:�:B?r.)r�T))r�r�r��__doc__r
rHrr*r,r1r4r8r;rErKrIrRr@rWr[rrorprArg�propertyrzr�r�r��setterr�rdr^r�r�r�r�r�r��
__classcell__)r"s@r#rr(s����������( �6��;�������*2�2�2�1�1�1����������������
�
�
����������������N�N�N�`�������(�(�(�(�(�
����.�.�.��(�.�����(��������(��	�-����-������(�����I�I�I�/M�/M�/M�d��������
>�>�>�'�'�'�R(M�(M�(M�T
M�
M�
M�
M�
M�
M�
Mr$rc��eZdZd�ZdS)�
_Observersc��d|_dSr.)�Mainr0s r#rz_Observers.__init__s
���$�)�)�)r$Nr�r6r$r#r�r�
s#����������r$r�)�
__author__�
__copyright__�__license__r�
jailthreadr�failmanagerr�osr�rJ�datetime�math�json�random�sys�helpersr�mytimer	�utilsr
r�rmrr��	Observersr6r$r#�<module>r�s^��0(�
�4�
�������"�"�"�"�"�"�)�)�)�)�)�)�6�6�6�6�6�6�6�6�6�6�6�6�6�6�6�6�6�6�6�6�6�6�6�6�6�6�6�6�
�
�
�
�������������������
��8�	�	��bM�bM�bM�bM�bM�Z�bM�bM�bM�J��������
�J�L�L�	�	�	r$