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/tests/__pycache__/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

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

;s*b���@s�dZdZdZddlZddlZddlZddlZddlZddlZddl	Z	ddl
mZm
Z
mZmZmZddlmZddlmZdd	lmZmZmZdd
lmZddlmZmZmZddl m!Z!mZ"dd
l#m$Z$ddl%m%Z%ddl&m'Z'ddl(m)Z)ddl*m+Z+m,Z-m.Z.m/Z/m0Z0m1Z1m2Z3m4Z4m5Z5ddl6m7Z7e7e8�Z,dZ9dZ:ee!�;��Z<e	j=�>e	j=j?�Z?e	j=�>e	j=j@�Z@e?de_?e,e_,e_,e_,e%jAZBe%jCZDdd�ZEeEe_Fe_Fe_Fe$_Fdd�ZGe%jHZHdd�ZIe@fdd�ZJGdd�dejK�ZKGd d!�d!ejK�ZLeKZMeLZNgaOd"d#�ZPePe_Qd$e_Re_Re+jSZTd%d&�ZUd'd(�ZV		,	d?d-d.�ZWd/d0�ZXd1d2�ZYd3d4�ZZd5d6�Z[ifd7d8�Z\Gd9d:�d:e+�Z]Gd;d<�d<e]�Z^Gd=d>�d>e]�Z_dS)@zSerg BresterzICopyright (c) 2014- Serg G. Brester (sebres), 2008- Fail2Ban Contributors�GPL�N)�join�isdir�isfile�exists�dirname��wraps)�Thread�)�fail2banclient�fail2banserver�fail2bancmdline)�Fail2banCmdLine)�exec_command_line�CSocket�
VisualWait)�Fail2banServerr)�protocol)�server)�MyTime)�Utils�)	�LogCaptureTestCase�logSys�with_tmpdir�shutil�logging�STOCK�
CONFIG_DIR�TEST_NOW�tearDownMyTime)�	getLoggerzfail2ban-clientzfail2ban-servercGst�|d�dS�Nr)r�info��args�r'�G/usr/lib/python3/dist-packages/fail2ban/tests/fail2banclienttestcase.py�_test_outputJsr)cCs&t�d|�t�t��|d�dS)Nz===>>> time shift + %s min�<)r�debugr�setTime�time)�shiftr'r'r(�_time_shiftQsr/cCs.tjdurtj�t�tj�td�dSdS)z$Helper to wait observer becomes idleN�)�	Observers�Main�
wait_empty�MID_WAITTIME�	wait_idler'r'r'r(�_observer_wait_idleYs
�r6cs2tjdurtjj����fdd�}|tj_dSdS)zOHelper to block observer before increase bantime until some condition gets trueNcs:�tj_t�d�t����t�d��|i|��dS)Nz4  [Observer::banFound] *** observer blocked for testz.  [Observer::banFound] +++ observer runs again)r1r2�banFoundrr+r�wait_for�r&�kwargs��
_obs_banFound�cond�timeoutr'r(�	_banFoundds


z0_observer_wait_before_incrban.<locals>._banFound)r1r2r7)r=r>r?r'r;r(�_observer_wait_before_incrban_s

	�r@c@�eZdZdZdS)�
ExitExceptionzException upon a normal exitN��__name__�
__module__�__qualname__�__doc__r'r'r'r(rBt�rBc@rA)�FailExitExceptionzException upon abnormal exitNrCr'r'r'r(rIyrHrIcGstt�r	t�d�SdS)Nr�exit)�len�INTERACT�popr%r'r'r(�_test_input_command�s
rNFcGs2t||�}|�d�|�|rdnd�|��dS)N�
�)�open�writer�close)�fn�mode�lines�fr'r'r(�_write_file�s
rXcCs>d}zt|�}|��W|dur|��SS|dur|��ww�N)rQ�readrS)rTrWr'r'r(�
_read_file�s

�
�r[�	/dev/null�:memory:r'�rPcCs�t|d�}|dkrt|d�}d}	|r}tr}dd�}
tjt||
d�|dus,Jdt|���|dur2d	}t�d
�}tj	t|d�dd
�D]}|�
d�}|�|�rOd}t|�qAt�d�}tj	t|d�dd
�D]}|�
d�}|�|�rqd}t|�qc|rzdnd}	n*t
�|�tt|d�dddd|�dd�ddt|d�dt|d�dd|d d!d�
|	r�tt||	�dgd"dd#d$|df|�R�|r�tt|d%�dg|�R�tjjtjkr�tt|d��tt|d��|r�tt|d%��|	r�|	dkr�tt||	��|�rt�r|D]}
t
�t
j�tt|
��t||
��q�|�r%|D]
}
t|
d&|idd��qd'\}}tjjtjk�rGttjj�}tjjd(k�rGd)d*tjjf}|d+|f}d,|d-t|d�d.t|d�d/|f|d0dd1ttj�fS)2N�config�autozf2b-db.sqlite3�	jail.confcs�fdd�|D�S)z?Filters list of 'files' to contain only directories (under dir)csg|]}tt�|��r|�qSr')r�pjoin)�.0rW��dirr'r(�
<listcomp>�sz2_start_params.<locals>.ig_dirs.<locals>.<listcomp>r')re�filesr'rdr(�ig_dirs�sz_start_params.<locals>.ig_dirs)�ignorez?We are about to overload use_stock_cfg from the one provided %s)�action.dzfilter.dz^dbfile\s*=z
fail2ban.confT)�inplacerOzdbfile = :memory:z^backend\s*=�backend = pollingz
jail.localrP�w�[Definition]zloglevel = INFOzlogtarget = �%z%%zsyslogsocket = autoz	socket = �f2b.sockz
pidfile = �f2b.pidz	dbfile = zdbmaxmatches = 100zdbpurgeage = 1d�
[INCLUDES]�	[DEFAULT]ztmp = zfail2ban.local�tmp)r'�INFOr�-�vz
--loglevel�-c�-sz-p�--logtargetz--syslogsocketz	--timeout)rbrr�copytree�STOCK_CONF_DIR�repr�re�compile�	fileinput�input�rstrip�match�print�os�mkdirrX�replace�unittest�F2B�	log_levelr�DEBUG�	_out_file�symlink�path�abspathru�str�	verbosityr�MAX_WAITTIME)rt�	use_stock�
use_stock_cfg�	logtarget�db�	f2b_local�jails�create_before_start�cfg�j_confrh�r�line�n�vvv�llevr'r'r(�
_start_params�s�



�








�
���"��
��r�cCs.z||�d�ddkWStyYdSw)Nrzr�	INHERITEDF)�index�
ValueError��startparamsr'r'r(�_inherited_log�s
�r�c
CsXd}zt|�}t�d|���}t|�WSty+}zt�|�WYd}~|Sd}~ww)Nz\S+)r[r~r��group�int�	Exceptionrr+)�pidfile�pid�er'r'r(�_get_pid_from_file�s
��r�c
sft�d|t|�f�t|�r|}t|d�}t|�st|d�}tjjtj	kr:t|d�}t|�r3t
|�nt�dd|�t|�sFt�d|�dSt�d	|�t|���durVd
SzDt�d���dksg�t
��krotd
�|f��t���swWdSt
��tj�t��fdd�d�s�t
��tj�t�d�t���WSty�}zt�|�WYd}~dSd}~ww)Nzcleanup: %rrqzfail2ban.pid�f2b.logr0z
no logfile %rzcleanup: no pidfile for %rTzcleanup pidfile: %rFzcleanup pid: %rrzpid %s of %s is invalidcst���SrY)r�
pid_existsr'�r�r'r(�<lambda>+�z_kill_srv.<locals>.<lambda>r�cleanup: kill ready)rr+rrbrr�r�r�rr�r��logr�r��getpidr�rr��kill�signal�SIGTERMr8�SIGKILLr��	exception)r��piddir�logfiler�r'r�r(�	_kill_srv	sD





��r�cst���fdd��}|S)z�Helper to decorate tests which receive in the last argument tmpdir to pass to kill_srv

	To be used in tandem with @with_tmpdir
	cs.|d}z
�|g|�R�Wt|�St|�w)N���)r�)�selfr&r��rWr'r(�wrapper:szwith_kill_srv.<locals>.wrapperr�rWr�r'r�r(�
with_kill_srv5sr�cs�fdd�}|S)z}Helper to decorate tests uses foreground server (as thread), started directly in test-cases

	To be used only in subclasses
	cstt����fdd���}|S)Nc
s�d}t��z�z|t�fddi����td�j���fd�}d|_|��ttff����fdd�	}|�_t	�
�fdd	�t�����
d
d���j�d��d�t�d��������g|�Ri|��WW|rt�d
���������
dd�r|��t�Sty�}ztd|����}|r�td|�����d}~ww|r�t�d
���������
dd�r�|��t�w)Nr�r��_TestCaseWorker)�name�targetr&Tcs�t���dd�stj�t�d��st��fdd�t���dd�sE��	|�d�t��fdd�t
�����dd���jddd	t
d
�dd��_
dS)N�endrqc���dd�duS�Nr���getr'��phaser'r(r�_�zywith_foreground_server_thread.<locals>._deco_wrapper.<locals>.wrapper.<locals>._stopAndWaitForServerEnd.<locals>.<lambda>�stopcr�r�r�r'r�r'r(r�dr��Shutdown successfulzExiting Fail2banT��all�waitc_�dSrYr'r9r'r'r(r�h�)r!r�r�r�rrbrr8r4�execCmdr��
assertTrue�assertLogged�stopAndWaitForServerEnd��code�r�r�r�rtr'r(�_stopAndWaitForServerEnd[szgwith_foreground_server_thread.<locals>._deco_wrapper.<locals>.wrapper.<locals>._stopAndWaitForServerEndcr�)N�startr�r'r�r'r(r�kr�zWwith_foreground_server_thread.<locals>._deco_wrapper.<locals>.wrapper.<locals>.<lambda>r�)r�r�z=== within server: begin ===z=== within server: end.  ===r�z=== Catch an exception: %sz#=== Error of server, log: ===
%s===)�dictr�r
�_testStartForeground�daemonr��SUCCESS�FAILEDr�rr8r�r�r��
_wait_for_srv�	DefLogSysr$�pruneLogrr!r�r��getLog)r�rtr&r:�thr�r�r�)rW�
startextrar�r(r�IsT�
	
���
zEwith_foreground_server_thread.<locals>._deco_wrapper.<locals>.wrapper)rr	r��r�r�r(�
_deco_wrapperHs:z4with_foreground_server_thread.<locals>._deco_wrapperr')r�r�r'r�r(�with_foreground_server_threadCs>r�c@speZdZejZdd�Zdd�Zdd�Ze	ddd	��Z
ddd
�Zdd�Zdd�Z
dd�Zeddid�dd��ZdS)�Fail2banClientServerBasecOr�rYr')r�r&r:r'r'r(�_setLogLevel�sz%Fail2banClientServerBase._setLogLevelcCs(t�|�dt_tjt_t|j�t	_
dS)zCall before every test case.r�N)r�setUpr�
DEF_LOGTARGETr��level�DEF_LOGLEVEL�staticmethod�
_test_exitr�_exit�r�r'r'r(r��s
zFail2banClientServerBase.setUpcCs(|jt_tt_tt_t�	|�t
�dS)zCall after every test case.N)�
_orig_exitrr��SRV_DEF_LOGTARGETrr��SRV_DEF_LOGLEVELr�r�tearDownr!r�r'r'r(r��s


z!Fail2banClientServerBase.tearDownrcCs|dkrt��t��r#)rBrIr�r'r'r(r��sz#Fail2banClientServerBase._test_exitTNcs��si�z:t|d��t���fdd�t�}|r��d�r#td|f��|r9t��fdd�t�}|s<td�|f��WdSWdSt|�rRtd������	�t|d	�}t
|�r`t|��t|�sjt�
d
|��)Nrpcs��d�pt��Sr�)r�rr')r��sockr'r(r��sz8Fail2banClientServerBase._wait_for_srv.<locals>.<lambda>r�z9Unexpected: Socket file does not exists.
Start failed: %rcsd���vS)N�Server ready)r�r'r�r'r(r��r�zBUnexpected: Server ready was not found, phase %r.
Start failed: %rz,=== Error by wait fot server, log: ===
%s===r�z*No log file %s to examine details of error)rbrr8r�r�r�r�r�r�r�rr�rr+)r�rt�readyr�r��retr�r')r�r�r�r(r��s>
�����
�z&Fail2banClientServerBase._wait_for_srvcGs*|�||jd|jdd�||�dS)Nrr)�assertRaisesr)r��exitTyper�r&r'r'r(r��s�z Fail2banClientServerBase.execCmdcGs:||�d�d}t|�}z
|�|�W|��S|��w)Nryr)r�r�sendrS)r�r�r&r��sr'r'r(�
execCmdDirect�s

z&Fail2banClientServerBase.execCmdDirectc	Csbt�d�d|d<z|�td|d�Wd|d<d|d<t�d�dSd|d<d|d<t�d�w)Nzstart of test workerTr�)z-fFr�zend of test worker)rr+r�r�)r�rtr�r�r'r'r(r��s
�z-Fail2banClientServerBase._testStartForegroundr�)z[Thread]zstacksize = 128r�cCsR|��|�t|dd�|�d�|�t|d�|�t|d�|�t|dd�dS)Nr��threadz{'stacksize': 128}�ping�~~unknown~cmd~failed~~�echo�	TEST-ECHO)r�r�r�r�r��r�rtr�r'r'r(�testStartForeground�s
z,Fail2banClientServerBase.testStartForeground)r)TNN)rDrErFrr�r�r�r�r�r�r�r�r�rr�r�rr'r'r'r(r��s	
r�c@steZdZeefZdd�Zdd�Zedd��Z	ee
dd���Zee
d	d
���Zee
dd���Z
ed
d��Zdd�ZdS)�Fail2banClientTestcCs,|�tttt���|�tttt���dSrY)r�rrb�BIN�CLIENT�SERVERr�r'r'r(�testConsistency�sz"Fail2banClientTest.testConsistencycCs�|�tdd�|�dt�|�d�|��|�tdd�|�t���|��|�tddd�|�dtj�|��|�tdd	d
�|�d�dS)Nr'�-h�Usage: �Report bugs to z-Vz-vqz	--versionz
Fail2Ban vz	--str2sec�1d12h30m�131400)r�r�r�rr�r�normVersion�versionr�r'r'r(�testClientUsage�s
z"Fail2banClientTest.testClientUsagecCsPt|d�}|�t|d�|�d�|�d�|��|�t|d�|�d�dS)NTz-vvdz
Loading filesz['set', 'logtarget',z--dp)r�r�r�r�r�r
r'r'r(�testClientDumps


z!Fail2banClientTest.testClientDumpcCst|d�}|�td|d�|j|d|d�|�d�|�d�z8|�t|dd�|�t|d	�|��|�td|d�|�d
�W|��|�t|d�|�d�|�d�n|��|�t|d�|�d�|�d�w|��|�t|d�|�d
�|�d�dS)NT�z-br�r�r��Exit with code 0rr	rzServer already runningr�r�zFailed to access socket pathzIs fail2ban running?)r�r�r�r�r�r�r�r
r'r'r(�testClientStartBackgroundInsides.



�

z2Fail2banClientTest.testClientStartBackgroundInsidecCs�t|t|d�d�}tjjr|�t|d�n0tjtt	t
�f}t�d|�||d}t
j|tddd�}|�t|�o<|d	�|j|d|d
�|�d�|��z�|�t|dd
�|�d
�|�d�|��|�t|dd�|�d�|��tt|d��}zt�|tj�t�t
j�|�t|dd�Wt�|tj�nt�|tj�w|�d�|��tgd�7a|�t|d�|�d�|�dd�|�d�|��tgd�7a|�t|d�|�d�|�d�|�d�|�d�|��tddg7a|�t|d�|�d�|��|�t|d d!�|�d�|�d"�|��W|��|�t|d#�|�d�|�d�dS|��|�t|d#�|�d�|�d�w)$Nr��r�)r��Start %s ...)�--asyncr�FT�r>�shell�outputrr�r�rr	rrz0.1zServer replied: pongrqz1e-10z	timed out)zecho INTERACT-ECHO�statusrJz-iz
INTERACT-ECHO�StatuszNumber of jail:)�reload�restartrJzReading config files:r�zreload ~~unknown~jail~fail~~rJz@Failed during configuration: No section: '~~unknown~jail~fail~~'r%z~~unknown~jail~fail~~zExit with code 255r�) r�rbr�r��fastr�r��sys�
executabler
rrr+r�
executeCmdr�r�rKr�r�r�r�r�r�r��SIGSTOPr-�sleep�DEFAULT_SHORT_INTERVALr��SIGCONTrL)r�rtr��cmdr�r�r'r'r(�testClientStartBackgroundCall2sv



 






�




�
z0Fail2banClientTest.testClientStartBackgroundCallcCs�t|dd�}|�tdddt|d�d�|�dt|d�d	�|��|�tddt|d
�dt|d�d
�|�d�|��tt|d�d���|�tdddt|d
�dt|d�d�|�d�|��t�	t|d��|�tdd�|�d�|��dS)Nr�rr'rrx�missr��Base configuration directory � does not existr_ryrpr%�Could not find server�a�LFail2ban seems to be in unexpected state (not running but the socket exists)r�
r�r�r�rbr�r�rQrSr��remover
r'r'r(�testClientFailStart�s*��
�

z&Fail2banClientTest.testClientFailStartcCsXt|dd�}|�t|dd�|�d�|��|�t|dddd�|�d�|��dS)	Nr�rr%�jailr4rz--xxxz"Unexpected argument(s) for reload:)r�r�r�r�r�r
r'r'r(�testClientFailCommands�s�
�
z)Fail2banClientTest.testClientFailCommandsc	Csnd}dD]0}d}t|d��}|r%|��|rtjjst�|�|d8}|sWd�n1s/wYqdS)Ng�Q���?)rr�r0r)r�	heartbeatr�r�r'r-r,)r��	sleeptime�verbose�cntr�visr'r'r(�testVisualWait�s
�����z!Fail2banClientTest.testVisualWaitN)rDrErF�_exec_clientrrrrrrr�rr0r9r;rBr'r'r'r(r�s$
T
rc@s�eZdZeefZdd�Zeedd���Z	eedd���Z
eedd���Zed	d
��Ze
ddid
�dd��Zejjdd�e
dddd�d
�dd���Zejjdd�e
ddddd�d
�dd ���Ze
�d!d"��Zd(S))�Fail2banServerTestcCs*|�tdd�|�dt�|�d�dS)Nr'rrr)r�r�r�rr�r'r'r(�testServerUsage�sz"Fail2banServerTest.testServerUsagecCst|t|d�d�}tjttt�f}t�d|�||d}tj	|t
ddd�}|�t|�o/|d�|j
|d|d	�|�d
�|��z'|�t|dd�|�t|d
�W|��|�t|d�|�d�|�d�dS|��|�t|d�|�d�|�d�w)Nr�rrrFTr rr�r�rr	rr�r�r)r�rbr(r)r
rrr+rr*r�r�rKr�r�r�r�r�r��r�rtr�r/r�r'r'r(�testServerStartBackground�s(

�
z,Fail2banServerTest.testServerStartBackgroundc
Cs�t|dd�}|�tddt|d��|�dt|d�d�|��tt|d�d	���|�tddt|d
�dt|d��|�d�|��t�	t|d��dS)
Nr�rr'rxr1r2r3rpr5r_ryr6r7r
r'r'r(�testServerFailStart�s
��
z&Fail2banServerTest.testServerFailStartcCs�t|dd�}t|d�}|�d�|�t|d�|�d�tt|d�dd	d
d	dd�|�d
�|�t|d�|jddddd�|�d�|�t|dd�|jddddd�dS)Nr�rr_z[test-phase 0]z--testz$OK: configuration test is successfulrar5rP�
[broken-jail]�filter = broken-jail-filter�enabled = truez[test-phase 0a]�.Unable to read the filter 'broken-jail-filter'zErrors in jail 'broken-jail'.z ERROR: test configuration failedT�r�z[test-phase 0b]z-tr�)r�rbr�r�r�r�rXr�)r�rtr�r�r'r'r(�testServerTestFailStart�s(


�
�

�z*Fail2banServerTest.testServerTestFailStartc
s
zQt�t�d�d�}tjttt�f}t�d|�||d}tj	|t
ddd�}|�t|�o0|d�|j
�d|d	�|�d
�|��t�d��W|�t���n|�t���wt��fdd
�t
�|�tt�d���|�d�|��|�t���|�d�dS)NzGf2b.log[format="SRV: %(relativeCreated)3d | %(message)s", datetime=off]rrrFTr rr�r�zKill server ... %scstt�d��S)Nrq)rrbr'�rtr'r(r�-r�z7Fail2banServerTest.testKillAfterStart.<locals>.<lambda>rqr�zcleanup: no pidfile for)r�rbr(r)r
rrr+rr*r�r�rKr�r�r�r�r8�assertFalserrFr'rOr(�testKillAfterStarts(�
 
z%Fail2banServerTest.testKillAfterStartr�r`r�cs�
t|d��t|d��t|d��t|d��t�t�d��		dȇfd	d
�	}dɇ���fdd�	}|dd�|dd�|dggd�d�tt�d�dddddd�t�dgttt����dfd�R�t�d�t�d�|�d�t	j
jtj
kr}t��|�t|d�|jdd dtd!�|�d"��|�d#�|jd$d%dd&�|jd'd(dtd!�|jd)d*d+dd&�|�d,�|dd-gd.�t�d/�t	j
jtj
kr�t��|�t|d�|jdtd0�|jd1d#dd&�|jd"�d"�dd&�|jd2d3dd&�|jd4d5dd&�|jd6d7dd&�|�d8�|�d9�|dgd.�|dd:dd;dd<dd=�|�t|d�|jdtd0�|jd1d#dd&�|�d>�|jd2d?dd&�|�d@�|�dA�|�dB�|ddCdD�|d-dgd.�|�dE�t�d/gttt����dFfdttt����dGfdttt����dHfdttt����dIfd�R�t	j
jtj
k�r�t��|jdJdKdtd!�|�t|dLdMdNdO�|jdPtd0�|jdQdRdSdTdUdVdWdd&�|jdXdYdZd[dd&�t�|�|�|d\�d]d^gd_�idMgd`�igf�|�|�|d\dadbdc�d]d^gd^dMgggf�|�|�|ddd^d\�dgd_��|�|�|dddMd\�dgd`��|�|�|ddd^d\de�dd�|�|�|ddd^d\dO�dd]�|�|�|ddd^d\dedO�ddd]g�t�d/�t�d/�|�df�|�t|dgdM�|jddhdPdtd!�|jdidjdkdld5dmdndodd&�	|jdpdqdtd!�|jdrdsdd&�|�dt�|�t|dLdMdNdu�|�t|dLdMdNdv�|jdwdxdtd!�t�|�dy�|�t|dddMdNdz�|jdbd{dudvdtd!�|�d|�|�t|ddd^dN�|jdad}dedbd{dtd!�|�d~�|�t|dgddM�|jdd5dtd!�|jdld5didjdkdd&�|jd�d�dd&�|�d��|jd�d�dd&�|jdUdVdd&�|ddCdD�|gd.�|�d��|�t|dd^�|jdtd0�|jd�d�dd&�|jd�d�d�dd&�|�d��|dgd��|�t|d�|jdtd0�|�d��|jd�dldd&�|jd��d��dd&�|�d��t�d/gttt����d�fdttt����d�fdttt����d�fd�R�t	j
jtj
k�r�t��|jd�d�dtd!�|jd�d�d�d�dd&�|�d��|�d��|�t|d�d�d�d��|jd�d�dtd!�|�d��|�t|d�d�d��|jd�d�dtd!�|�t|d�d�d�d��|jd�d�dtd!�|�d��|�t|d�dLd^dNd�d��|jd�d�dtd!�|�t|d�dLd^d�d��|jd�d�dtd!�|�d��|�t|dd�|jdtd0�|jd�d1d�dd&�|jd�d�d#dSdd&�|�d��|�t|d�d�d��|jd�d�dd&�|�d��|dgd�d��|�t|d�|jdtd0�|jd�d�dd&�|�d��|dgd��|�t|d�|jdtd0�|�d��|�t|d�ddM�|jdtd0�|�d��|��|�t|d�dd�dM�|jdtd0�|jd4d5dd&�|�d��|�t|d�dd�d��|jdtd0�|jd�d�dtd!�|�dá|�t|d�dLd^d�d�dơ|�t|d�ddd^dǡ|jd�d�dd&�dS)�Nr_�	test1.logz	test2.logz	test3.logrj�test-action1TrPcsnt�dd|�}|st�|�dSt|ddddddd	d
dd|d
|d|d|d|�tjjtjkr5t	|�dSdS)Nrj�%s.confrmrsz_exec_once = 0rPrnznorestored = %(_exec_once)sz
restore = zinfo = z<_use_flush_ = echo '[%(name)s] %(actname)s: -- flushing IPs'z6actionstart =  echo '[%(name)s] %(actname)s: ** start'z7actionreload = echo '[%(name)s] %(actname)s: .. reload'zMactionban =    echo '[%(name)s] %(actname)s: ++ ban <ip> %(restore)s%(info)s'z;actionunban =  echo '[%(name)s] %(actname)s: -- unban <ip>'z5actionstop =   echo '[%(name)s] %(actname)s: __ stop')
rbr�r8rXr�r�r�rr�r�)�actname�allowr�r%�ban�unbanr�rT�r�r'r(�_write_action_cfgEs,
��zBFail2banServerTest.testServerReloadTest.<locals>._write_action_cfg�rrr'�pollingcsPtgt�d��d�d�d�d�d�d�d�d�d	�d
�d�d�d�d
|�d�d�d|vr/dnd�d|vr7dnd�d|vr?dnd�d��d|vrMd�nd�d|vrWd�nd�d	�d|vradnd�d|vridnd�d�d�d
|�d�d�d|vr}dnd�d|vr�dnd�d��d|vr�dnd�R�tjjtjkr�tt�d��dSdS)NrarmrrrPrs�usedns = no�maxretry = 3zfindtime = 10mzBfailregex = ^\s*failure <F-ERRCODE>401|403</F-ERRCODE> from <HOST>�datepattern = {^LN-BEG}EPOCHzignoreip = 127.0.0.1/8 ::1�[test-jail1]�
backend = �filter =z	action = rz*         test-action1[name='%(__name__)s']rzj         test-action2[name='%(__name__)s', restore='restored: <restored>', info=', err-code: <F-ERRCODE>']�z�         test-action2[name='%(__name__)s', actname=test-action3, _exec_once=1, restore='restored: <restored>', actionflush=<_use_flush_>]�
logpath = z
          z@            ^\s*error <F-ERRCODE>401|403</F-ERRCODE> from <HOST>rKz[test-jail2]�rXrbr�r�r�rr�r�)�enabled�actions�backend�r��test1log�test2log�test3logr'r(�_write_jail_cfg]s�����������	�
�
�
��
��������������������!��"�#�%�z@Fail2banServerTest.testServerReloadTest.<locals>._write_jail_cfg)rU�test-action2r)rrrc)rfrgrar5rIrJrKrmz# failure 401 from 192.0.2.1: test 1rcz[test-phase 1a]r%�Reload finished.z1 ticket(s) in 'test-jail1r�zAdded logfile: %rz[test-jail1] Ban 192.0.2.1z-stdout: '[test-jail1] test-action1: ** start'z-stdout: '[test-jail1] test-action2: ** start'rMzPstdout: '[test-jail1] test-action2: ++ ban 192.0.2.1 restored: 0, err-code: 401'zAstdout: '[test-jail1] test-action3: ++ ban 192.0.2.1 restored: 0'rLz)Errors in jail 'broken-jail'. Skipping...z:Jail 'broken-jail' skipped, because of wrong configurationz[test-phase 1b]r)rg�w+�r�z[test-jail1] Unban 192.0.2.1z.stdout: '[test-jail1] test-action1: .. reload'z.stdout: '[test-jail1] test-action2: .. reload'zCreating new jail 'test-jail2'zJail 'test-jail2' startedz4stdout: '[test-jail1] test-action3: -- flushing IPs'z,stdout: '[test-jail1] test-action3: __ stop'z7stdout: '[test-jail1] test-action3: -- unban 192.0.2.1'z[test-phase 2a]z+               echo '[<name>] %s: started.'z,               echo '[<name>] %s: reloaded.'z+               echo '[<name>] %s: stopped.')rUr�r%r�zAdded logfile:z.stdout: '[test-jail1] test-action1: reloaded.'z7stdout: '[test-jail1] test-action2: -- unban 192.0.2.1'z,stdout: '[test-jail1] test-action2: __ stop'z7stdout: '[test-jail1] test-action1: -- unban 192.0.2.1'F)rUrVz[test-phase 2b]z#   error 403 from 192.0.2.2: test 2z#   error 403 from 192.0.2.3: test 2z# failure 401 from 192.0.2.4: test 2z# failure 401 from 192.0.2.8: test 2z2 ticket(s) in 'test-jail2z5 ticket(s) in 'test-jail1�setz
test-jail2�banip�	192.0.2.9z3 ticket(s) in 'test-jail2z[test-jail1] Ban 192.0.2.2z[test-jail1] Ban 192.0.2.3z[test-jail1] Ban 192.0.2.4z[test-jail1] Ban 192.0.2.8z[test-jail2] Ban 192.0.2.4z[test-jail2] Ban 192.0.2.8z[test-jail2] Ban 192.0.2.9z[test-jail2] Found 192.0.2.2z[test-jail2] Ban 192.0.2.2z[test-jail2] Found 192.0.2.3z[test-jail2] Ban 192.0.2.3�bannedr�
test-jail1)�	192.0.2.4�	192.0.2.1�	192.0.2.8�	192.0.2.3�	192.0.2.2)rwrtryrxrwz192.0.2.222r�rzz[test-phase 2c]r&zRestore Banz[test-jail2] Unban 192.0.2.4z[test-jail2] Unban 192.0.2.8z[test-jail2] Unban 192.0.2.9zJail 'test-jail2' stoppedz"[test-jail2] Restore Ban 192.0.2.4z"[test-jail2] Restore Ban 192.0.2.8z"[test-jail2] Restore Ban 192.0.2.9zPstdout: '[test-jail2] test-action2: ++ ban 192.0.2.4 restored: 1, err-code: 401'zPstdout: '[test-jail2] test-action2: ++ ban 192.0.2.8 restored: 1, err-code: 401'zAstdout: '[test-jail2] test-action3: ++ ban 192.0.2.4 restored: 1'zAstdout: '[test-jail2] test-action3: ++ ban 192.0.2.8 restored: 1'z[test-phase 2d]z
192.0.2.21z
192.0.2.22z5stdout: '[test-jail2] test-action3: ++ ban 192.0.2.22z6stdout: '[test-jail2] test-action3: ++ ban 192.0.2.22 z[test-phase 2d.1]rOryz[test-phase 2d.2]r{z[test-phase 2e]z--unbanz7stdout: '[test-jail2] test-action2: -- unban 192.0.2.21z8stdout: '[test-jail2] test-action2: -- unban 192.0.2.22'z4stdout: '[test-jail2] test-action3: -- flushing IPs'z8stdout: '[test-jail2] test-action3: -- unban 192.0.2.21'z8stdout: '[test-jail2] test-action3: -- unban 192.0.2.22'z[test-phase 3]zReload jail 'test-jail1'zJail 'test-jail1' reloadedzReload jail 'test-jail2'zJail 'test-jail2' reloadedzJail 'test-jail1' startedz[test-phase 4])rfzStopping jail 'test-jail2'zRemoved logfile: %rz[test-phase 5]z# failure 401 from 192.0.2.1: test 5z#   error 403 from 192.0.2.5: test 5z# failure 401 from 192.0.2.6: test 5z6 ticket(s) in 'test-jail1z%[test-jail1] 192.0.2.1 already bannedz[test-jail1] Found 192.0.2.1z[test-jail1] Found 192.0.2.6z[test-jail1] Ban 192.0.2.6z[test-jail1] Found 192.0.2.5z[test-phase 6a]rrXz	192.0.2.5z	192.0.2.6z192.0.2.5 is not bannedz[test-jail1] Unban 192.0.2.6z[test-phase 6b]z192.0.2.2/31z[test-jail1] Unban 192.0.2.2z[test-jail1] Unban 192.0.2.3z192.0.2.8/31z192.0.2.100/31z[test-jail1] Unban 192.0.2.8z192.0.2.100/31 is not bannedz[test-phase 6c]z
192.0.2.96/28z192.0.2.112/28z[test-jail1] Ban 192.0.2.96/28z[test-jail1] Ban 192.0.2.112/28�unbanipz
192.0.2.64/26z [test-jail1] Unban 192.0.2.96/28z![test-jail1] Unban 192.0.2.112/28z[test-phase 7]z[test-jail1] Unban 192.0.2.4zJail 'test-jail1' stoppedz[test-phase 7b]�--allzFlush ban listz'Unbanned 0, 0 ticket(s) in 'test-jail1'z[test-phase 8a]zxxx-unknown-backend-zzz)rfrhz0Restart jail 'test-jail1' (reason: 'polling' != zUnknown backend z[test-phase 8b]z[test-phase end-1]z$the jail 'test-jail2' does not existz--if-existsz[test-phase end-2]�	--restartz[test-phase end-3]�addignoreipz192.0.2.1/32z2001:DB8::1/96�ignoreip)rSTrPrPrPrPrP)r[r'r\)rbr�r�rXr�r�rr-r�r�r�r�rr�r�r�r�r�r4�assertNotLoggedr6�assertSortedEqualr�assertEqualr�)r�rtr�rZrmr'rir(�testServerReloadTest5sZ




�
*
�(


�
���

������
��
����
��������	�
�

��
���
���
���
���
���

���


�����
���
�
�
�������

��
���
�����

��
����
��
��
���
��
�

�
��
��
��z'Fail2banServerTest.testServerReloadTestznginx-block-map)�action)�%(tmp)s/blck-failures.log)rj)z[nginx-blck-lst]rlr]z#logpath = %(tmp)s/blck-failures.logzRaction = nginx-block-map[blck_lst_reload="", blck_lst_file="%(tmp)s/blck-lst.map"]z�         blocklist_de[actionban='curl() { echo "*** curl" "$*";}; <Definition/actionban>', email="Fail2Ban <fail2ban@localhost>", apikey="TEST-API-KEY", agent="fail2ban-test-agent", service=<name>]rb�datepattern = ^Epochz3failregex = ^ failure "<F-ID>[^"]+</F-ID>" - <ADDR>zmaxretry = 1rK)r�r�r�cCs�t|d�}dd|i}dd|i}t|dttt����dttt����dttt����dttt����d	ttt����d
�|jddd
ddddtd�t|�t	|�}|�
d|�|�
d|�|�
d|�|�
d|�|�
d|�|jdddtd�|�t|dddd�t|�t	|�}|�
d|�|�
d|�|�
d|�|�
d|�|�
d|�|�t�|�d�t|�t	|�}|�|d�dS) Nr_r�rtz%(tmp)s/blck-lst.maprpz" failure "125-000-001" - 192.0.2.1z" failure "125-000-002" - 192.0.2.1u1 failure "125-000-003" - 192.0.2.1 (òðåòèé)u1 failure "125-000-004" - 192.0.2.1 (òðåòèé)z" failure "125-000-005" - 192.0.2.1z [nginx-blck-lst] Ban 125-000-001z [nginx-blck-lst] Ban 125-000-002z [nginx-blck-lst] Ban 125-000-003z [nginx-blck-lst] Ban 125-000-004z [nginx-blck-lst] Ban 125-000-005z5 ticket(s)Tr�z\125-000-001 1;
z\125-000-002 1;
z\125-000-003 1;
z\125-000-004 1;
z\125-000-005 1;
z�stdout: '*** curl --fail --data-urlencode server=Fail2Ban <fail2ban@localhost> --data apikey=TEST-API-KEY --data service=nginx-blck-lst z=stdout: ' --data format=text --user-agent fail2ban-test-agentrXz125-000-001z125-000-002z125-000-005z5[nginx-blck-lst] Flush ticket(s) with nginx-block-maprP)rbrXr�r�rr-r�r4r�r[�assertInr�r��assertNotInr�r�)r�rtr�r��lgfn�mpfn�mpr'r'r(�testServerActions_NginxBlockMap'sX
��	�

z2Fail2banServerTest.testServerActions_NginxBlockMapz
sendmail-auth)�filter)�%(tmp)s/test.logT)rszdbmaxmatches = 1)z�test_action = dummy[actionstart_on_demand=1, init="start: %(__name__)s", target="%(tmp)s/test.txt",
      actionban='<known/actionban>;
        echo "<matches>"; printf "=====\n%%b\n=====\n\n" "<matches>" >> <target>']z[sendmail-auth]rlr]�logpath = %(tmp)s/test.log�action = %(test_action)sz%filter = sendmail-auth[logtype=short]r�r^zmaxmatches = 2rKz[sendmail-reject]rlr]r�r�z'filter = sendmail-reject[logtype=short]r�r^rK)r�r�r�r�c
Cs`t|d�}dd|i}dd|i}ttt����dttt����dttt����df}ttt����dttt����d	ttt����d
f}|�d�t|dg|�R�|jd
ddtd�t	|�t
|�}|d}	|�|	|�|dd�D]}	|�|	|�qz|�d�t|dg|�R�|jdddtd�t	|�t
|�}|D]}	|�|	|�q�|�d�|�
t|ddd�|jddddtd�t
|�}|d}	|�|	�|�|	|�|dd�D]
}	|�|	�|�|	|�q�|jdddtd�t
|�}|d}	|�|	�|�|	|�|dd�D]}	|�|	�|�|	|��q|�d�|�t�|�t|��dS)Nr_r�rtz%(tmp)s/test.txtz] smtp1 sm-mta[5133]: s1000000000001: [192.0.2.1]: possible SMTP attack: command=AUTH, count=1z] smtp1 sm-mta[5133]: s1000000000002: [192.0.2.1]: possible SMTP attack: command=AUTH, count=2z] smtp1 sm-mta[5133]: s1000000000003: [192.0.2.1]: possible SMTP attack: command=AUTH, count=3z� smtp1 sm-mta[21134]: s2000000000001: ruleset=check_rcpt, arg1=<123@example.com>, relay=xxx.dynamic.example.com [192.0.2.2], reject=550 5.7.1 <123@example.com>... Relaying denied. Proper authentication required.z� smtp1 sm-mta[21134]: s2000000000002: ruleset=check_rcpt, arg1=<345@example.com>, relay=xxx.dynamic.example.com [192.0.2.2], reject=550 5.7.1 <345@example.com>... Relaying denied. Proper authentication required.z� smtp1 sm-mta[21134]: s3000000000003: ruleset=check_rcpt, arg1=<567@example.com>, relay=xxx.dynamic.example.com [192.0.2.2], reject=550 5.7.1 <567@example.com>... Relaying denied. Proper authentication required.z[test-phase sendmail-auth]rpz[sendmail-auth] Ban 192.0.2.1z1 ticket(s) in 'sendmail-auth'Tr�rrz[test-phase sendmail-reject]z[sendmail-reject] Ban 192.0.2.2z 1 ticket(s) in 'sendmail-reject'z[test-phase restart sendmail-*]r%r~r}roz%[sendmail-auth] Restore Ban 192.0.2.1r�z'[sendmail-reject] Restore Ban 192.0.2.2z[test-phase stop server])rbr�r�rr-r�rXr�r4r�r[r�r�r�r�r�r�rPr)
r�rtr�r�r��tofn�	smaut_msg�	smrej_msg�td�mr'r'r(�testServerJails_Sendmailwsv
(��
�
�
��

�



z+Fail2banServerTest.testServerJails_Sendmailcs�t|d��t|d��t�t�d��d=�fdd�	}d>��fd	d
�	}|ddd�|d
dd�|�t�d�|�d�|�t|d�t�dgttt	�
���dfd�R�t�|jdddt
d�t�|�d�td�t�|jddddt
d�t�|�d�d�t�fdd��t�dgttt	�
���dfd �R�|jd!d"dt
d�|�d#�|�t|d$d%d&d'�|jd(d)dt
d�d�t�|�d*�td+�t�|jd,dt
d�t�|�d-�|�t|d$d%d&d'�|jd(d.dt
d�|�d/�t�d0d1i�tj���fd2d3�}��d4|���d4d5d��t��fd6d�t��j�tjj�r-d7nd8df�fd9d:�	}|�_|�t�|�d;�|��j�|��j d�d �d0<|jd;dd<���!�dS)?Nr_rRrjrSTc	sLt�dd|�}t|ddddd|rdndd	�tjjtjkr$t|�dSdS)
NrjrTrmrsrPrnzeactionban =     printf %%s "[%(name)s] %(actname)s: ++ ban <ip> -c <bancount> -t <bantime> : <F-MSG>"ziactionprolong = printf %%s "[%(name)s] %(actname)s: ++ prolong <ip> -c <bancount> -t <bantime> : <F-MSG>"zBactionunban =   printf %%b '[%(name)s] %(actname)s: -- unban <ip>')rbrXr�r�r�rr�r�)rU�prolongrTrYr'r(rZ�s��	�z@Fail2banServerTest.testServerObserver.<locals>._write_action_cfgr\csdtt�d�ddddddddd	d
dddd
|dddd�ddd�tjjtjkr0tt�d��dSdS)NrarmrrrPrsr]r^z
findtime = 1mzbantime = 5mzbantime.increment = truer_r`rarbz*action = test-action1[name='%(__name__)s']z*         test-action2[name='%(__name__)s']rdzXfailregex = ^\s*failure <F-ERRCODE>401|403</F-ERRCODE> from <HOST>:\s*<F-MSG>.*</F-MSG>$rKre)rh)r�rjr'r(rm�s*
��z>Fail2banServerTest.testServerObserver.<locals>._write_jail_cfgF)rUr�rnrmz[test-phase 0) time-0]r%rpz> failure 401 from 192.0.2.11: I'm bad "hacker" `` $(echo test)rczDstdout: '[test-jail1] test-action1: ++ ban 192.0.2.11 -c 1 -t 300 : zDstdout: '[test-jail1] test-action2: ++ ban 192.0.2.11 -c 1 -t 300 : r�z[test-phase 1) time+10m]�
z7stdout: '[test-jail1] test-action1: -- unban 192.0.2.11z7stdout: '[test-jail1] test-action2: -- unban 192.0.2.11z0 ticket(s) in 'test-jail1'z[test-phase 2) time+10m]cs�SrYr'r')�wakeObsr'r(r�9r�z7Fail2banServerTest.testServerObserver.<locals>.<lambda>zC failure 401 from 192.0.2.11: I'm very bad "hacker" `` $(echo test)rzDstdout: '[test-jail1] test-action1: ++ ban 192.0.2.11 -c 2 -t 300 : zDstdout: '[test-jail1] test-action2: ++ ban 192.0.2.11 -c 2 -t 300 : z"[test-phase 2) time+10m - get-ips]r�rvrsz--with-timez
192.0.2.11z+ 300 =z[test-phase 2) time+11m]rzHstdout: '[test-jail1] test-action2: ++ prolong 192.0.2.11 -c 2 -t 600 : z"[test-phase 2) time+11m - get-ips]z+ 600 =z'[test-phase end) stop on busy observer]�statercs<t�d�d�d<t��fdd�t����t�d�dS)Nz!++ observer enters busy state ...rr�c��ddkS)Nr�rr'r'�r5r'r(r�er�zMFail2banServerTest.testServerObserver.<locals>._long_action.<locals>.<lambda>z-- observer leaves busy state.)rr$rr8r��db_purger')r5�obsMainr'r(�_long_actionbs

z;Fail2banServerTest.testServerObserver.<locals>._long_action�callcSr�rYr'r'r'r'r(r�ir�cr�)Nr�rr'r'r�r'r(r�kr�g{�G�z�?g�������?cs
�||�SrYr')�wtime�	forceQuit)�obsMain_stopr'r(�_stopns
z4Fail2banServerTest.testServerObserver.<locals>._stopzobserver leaves busy staterq)rST)r\)"rbr�r�rXr�r�r�r�r�rr-r6r�r4r/r@r!r1r2�addrr8r�r�r�r�r'r�r�rP�idler��_ObserverThread__dbr)r�rtr�rZrmr�r�r')r5r�r�r�rjr�r(�testServerObserver�s�



��
�
��
�
�
�
 

z%Fail2banServerTest.testServerObserverFcCs|�t�dSrY)r�r�r
r'r'r(�_testServerStartStop~sz'Fail2banServerTest._testServerStartStopcCstd�D]}|��qdS)Ni�)�ranger�)r��ir'r'r(�testServerStartStop�s
�z&Fail2banServerTest.testServerStartStopN)rDrErF�_exec_serverrrrErr�rGrHrNrQr�r�r�r��skip_if_cfg_missingr�r�r�r�r�r'r'r'r(rD�sL

t�:�&L
rD)FNr\r]r'r^N)`�
__author__�
__copyright__�__license__r�r�r~r(r-r�r��os.pathrrbrrrr�	functoolsr	�	threadingr
�clientrr
r�client.fail2bancmdliner�client.fail2banclientrrCrr�client.fail2banserverrr�rPrr�
server.mytimer�server.utilsr�utilsrrr�rrrrrr|r r!�helpersr"rDrr�
getServerPathr
r��maxWaitTimer�r4r�r�r�r�r)r"r/r1r6r@rBrIr�r�rLrN�
input_command�
PRODUCTION�dumpFiler�rXr[r�r�r�r�r�r�r�rrDr'r'r'r(�<module>s�,
����

�W
,FhU

https://t.me/RX1948 - 2025