HEX
Server: LiteSpeed
System: Linux 112.webhostingindonesia.co.id 5.14.0-570.62.1.el9_6.x86_64 #1 SMP PREEMPT_DYNAMIC Tue Nov 11 10:10:59 EST 2025 x86_64
User: iyfwylsv (10313)
PHP: 8.2.30
Disabled: NONE
Upload Files
File: //lib/python3.9/site-packages/fail2ban/server/__pycache__/utils.cpython-39.opt-2.pyc
a

늠����@s�dZdZdZddlZddlZddlZddlZddlZddlZddl	m
Z
ddlZddlm
Z
mZmZddlmZddlZe
e�Zd	d
iZedd�ej��D��ZGd
d�d�ZdS)z2Serg G. Brester (sebres) and Fail2Ban ContributorszYCopyright (c) 2004 Cyril Jaquier, 2011-2012 Yaroslav Halchenko, 2012-2015 Serg G. BresterZGPL�N)�Lock�)�	getLogger�_merge_dicts�
uni_decode)�OrderedDict�am"Command not found".  Make sure that all commands in %(realCmd)r are in the PATH of fail2ban-server process (grep -a PATH= /proc/`pidof -x fail2ban-server`/environ). You may want to start "fail2ban-server -f" separately, initiate it with "fail2ban-client reload" in another shell session and observe if additional informative error messages appear in the terminals.ccs$|]\}}|�d�r||fVqdS)ZSIGN)�
startswith)�.0�nameZnum�r�9/usr/lib/python3.9/site-packages/fail2ban/server/utils.py�	<genexpr>4s�rc@s�eZdZdZdZdZdZedZGdd�de�Z	e
dd	��Ze
d
d��Ze
ddd��Z
e
ddd��Zejdkr|e
dd��Zne
dd��Ze
dd��ZdS)�UtilszPUtilities provide diverse static methods like executes OS shell commands, etc.
	rg�������?g����MbP?�dc@sLeZdZdZdd�Zddd�Zdd	�Zddd�Zd
d�Zdd�Z	dd�Z
d
S)zUtils.Cachez.A simple cache with a TTL and limit on size
		cOs$|j|i|��t�|_t�|_dS�N)�
setOptionsr�_cacher�_Cache__lock)�self�args�kwargsrrr
�__init__EszUtils.Cache.__init__���<cCs||_||_dSr)�maxCount�maxTime)rrrrrr
rJszUtils.Cache.setOptionscCs
t|j�Sr)�lenr�rrrr
�__len__NszUtils.Cache.__len__NcCs6|j�|�}|r2|dt��kr(|dS|�|�|S)N�r)r�get�time�unset)r�kZdefv�vrrr
r!Qs
zUtils.Cache.getcCs�t��}|j}|j�bt|�|jkrV|rV|jdd�\}}|d|kr$t|�|jkr$qVq$|||jf||<Wd�n1s|0YdS)NF)Zlastr )r"rrrr�popitemr)rr$r%�t�cacheZckZcvrrr
�setYszUtils.Cache.setcCs8|j�|j�|d�Wd�n1s*0YdSr)rr�pop)rr$rrr
r#iszUtils.Cache.unsetcCs4|j�|j��Wd�n1s&0YdSr)rr�clearrrrr
r+mszUtils.Cache.clear)rr)N)�__name__�
__module__�__qualname__�__doc__rrrr!r)r#r+rrrr
�CacheAs

r0cCs>t�|tj�}|s|tjO}n|tjM}t�|tj|�|Sr)�fcntlZF_GETFL�os�
O_NONBLOCKZF_SETFL)Zfhandle�value�flagsrrr
�
setFBlockModerszUtils.setFBlockModecCsld}t|t�s|g}t|�d}|��D]*\}}|d||f7}|�|�|d7}q(|d|d|d<|S)a�Generates new shell command as array, contains map as variables to
		arguments statement (varsStat), the command (realCmd) used this variables and
		the list of the arguments, mapped from varsDict

		Example:
			buildShellCmd('echo "V2: $v2, V1: $v1"', {"v1": "val 1", "v2": "val 2", "vUnused": "unused var"})
		returns:
			['v1=$0 v2=$1 vUnused=$2 
echo "V2: $v2, V1: $v1"', 'val 1', 'val 2', 'unused var']
		�r z%s=$%s �
r)�
isinstance�listr�items�append)�realCmd�varsDictZvarsStat�ir$r%rrr
�
buildShellCmd|s


zUtils.buildShellCmdrTF�rNc
s�d}}d}	d�}
|r6|r*t��|��nttj|�}
t�����fdd�}�ztj�tjtj||
tj	d����
�}	|	dur��fdd�}t�||tj�}	|	r�|	d}	|	du�rX|r�|t
j�d}t�d�|f�t��j�}
t�|
tj�t�tj���
�}	|	du�s|�r<t�|
tj�t�tj�|	du�r<��
�}	|	du�rXt�|
��sXtj}	Wnxt�y�}z^|�r�|t
j�d}d	�|f}t�|���s�|�s�d
n
d
|||	fWYd}~SWYd}~n
d}~00|	|v�r�t
jnt
j}|t��k�r |�r ||t
jk�r|dnt
j�d}|�s4|t��k�r��j�r�z0|	du�sR|	dk�r`t��jd
��j� �}Wn2t!�y�}zt�d|�WYd}~n
d}~00|du�r�|d
k�r�|t��k�r�|�"�D]}t�#|d�t$|���qʈj%�r�z0|	du�s|	dk�rt��j%d
��j%� �}Wn2t!�yP}zt�d|�WYd}~n
d}~00|du�r�|d
k�r�|t��k�r�|�"�D]}t�#|d�t$|���q|�j�r��j�&��j%�r��j%�&�d
}|	|v�r�t�'d�|	�d}n�|	du�r�t�d��j�n~|	dk�s|	dk�rD|	dk�r|	n|	d}t�d�t(�)|d|�|	�n4t*�)|	d�}t�d�|	�|�rxt�+d|	|t,��|�r�||||	fSt-|�dk�r�|S||	fS)aExecutes a command.

		Parameters
		----------
		realCmd : str
			The command to execute.
		timeout : int
			The time out in seconds for the command.
		shell : bool
			If shell is True (default), the specified command (may be a string) will be 
			executed through the shell.
		output : bool
			If output is True, the function returns tuple (success, stdoutdata, stderrdata, returncode).
			If False, just indication of success is returned
		varsDict: dict
			variables supplied to the command (or to the shell script)

		Returns
		-------
		bool or (bool, str, str, int)
			True if the command succeeded and with stdout, stderr, returncode if output was set to True

		Raises
		------
		OSError
			If command fails to be executed.
		RuntimeError
			If command execution times out.
		Ncst�|d���S)Nz%x -- exec: %s)�logSys�log)�level)r=�	realCmdIdrr
�<lambda>��z"Utils.executeCmd.<locals>.<lambda>)�stdout�stderr�shell�env�
preexec_fncs���}|durd|fSdS)NT)�poll)�retcode)�popenrr
�_popen_wait_end�sz)Utils.executeCmd.<locals>._popen_wait_endr z!%x -- timed out after %s seconds.z%s -- failed with %sFrz  ... -- failed to read stdout %sr7z%x -- stdout: %rz  ... -- failed to read stderr %sz%x -- stderr: %rz%x -- returned successfully %iTz%x -- unable to kill PID %i�z&%x -- killed with %s (return code: %s)z	signal %iz%x -- returned %izHINT on %i: %s).rr@rr2�environ�id�
subprocess�Popen�PIPE�setsidrM�wait_for�DEFAULT_SHORTEST_INTERVAL�logging�ERRORrB�error�getpgid�pid�killpg�signal�SIGTERMr"�sleep�DEFAULT_SLEEP_INTERVAL�SIGKILL�
pid_exists�OSError�DEBUGZgetEffectiveLevelrHr6�read�IOError�
splitlinesrCrrI�close�debug�signamer!�_RETCODE_HINTS�info�localsr)r=�timeoutrJ�outputZtout_kill_treeZ
success_codesr>rHrIrNrKZlogCmdrPZpgid�eZ	std_level�lZsuccessZsigcode�msgr)rOr=rEr
�
executeCmd�s� �
�


8(""""

�zUtils.executeCmdcsrd}|�}|r|S|rDd}}t|�s@t��|��fdd�}n|}|�rLqnt||pXtjtj�}t�|�q|S)a5Wait until condition expression `cond` is True, up to `timeout` sec

		Parameters
		----------
		cond : callable
			The expression to check condition 
			(should return equivalent to bool True if wait successful).
		timeout : float or callable
			The time out for end of wait
			(in seconds or callable that returns True if timeout occurred).
		interval : float (optional)
			Polling start interval for wait cycle in seconds.

		Returns
		-------
		variable
			The return value of the last call of `cond`, 
			logical False (or None, 0, etc) if timeout occurred.
		r rcst���kSr)r"r�Ztime0rr
rF9rGz Utils.wait_for.<locals>.<lambda>)�callabler"�minrrc�DEFAULT_SLEEP_TIMErb)Zcondrq�intervalZiniZretZstmZtimeout_exprrrwr
rXszUtils.wait_for�posixc
Cs^ddl}|dkrdSzt�|d�Wn0tyT}z|j|jkWYd}~Sd}~00dSdS)z6Check whether pid exists in the current process table.rNFT)�errnor2�killrfZEPERM)r^r}rsrrr
reEs"zUtils.pid_existscCs@ddl}|jj}d}|�|d|�}|dkr8|�|�dSdSdS)NriTF)�ctypesZwindll�kernel32ZOpenProcessZCloseHandle)r^rr�ZSYNCHRONIZE�processrrr
reRs
cCs.tj�tj�|��d}tj�||���}|S)Nr)r2�path�splitext�basename�	importlib�	machinery�SourceFileLoader�load_module)ZpythonModuleZpythonModuleName�modrrr
�load_python_module_s
���zUtils.load_python_module)rTFTrAN)N)r,r-r.r/rzrcZDEFAULT_SHORT_INTERVALrY�objectr0�staticmethodr6r@rvrXr2rrer�rrrr
r7s01
	
�)

r)�
__author__Z
__copyright__Z__license__r1rZr2r`rT�sys�	threadingrr"Zhelpersrrr�collectionsrZimportlib.machineryr�r,rBrn�dict�__dict__r;rmrrrrr
�<module>s(��