HEX
Server: LiteSpeed
System: Linux baran.dnsprotection.me 4.18.0-553.83.1.lve.el8.x86_64 #1 SMP Wed Nov 12 10:04:12 UTC 2025 x86_64
User: ddir12 (3618)
PHP: 8.3.31
Disabled: NONE
Upload Files
File: //lib/python3.6/site-packages/glances/plugins/__pycache__/glances_ports.cpython-36.opt-1.pyc
3

Eo�cS-�@s�dZddlZddlZddlZddlZddlZddlZddlmZm	Z	m
Z
ddlmZddl
mZddlmZddlmZddlmZdd	lmZyddlZd
ZWn6ek
r�ZzdZejdje��WYddZ[XnXGd
d�de�ZGdd�dej�ZdS)zPorts scanner plugin.�N)�WINDOWS�MACOS�BSD)�GlancesPortsList)�GlancesWebList)�Counter)�	bool_type)�logger)�
GlancesPluginTFzAMissing Python Lib ({}), Ports plugin is limited to port scanningcsheZdZdZd�fdd�	Z�fdd�Zejejdd���Z	d	d
�Z
dd
d�Zddd�Zddd�Z
�ZS)�PluginzGlances ports scanner plugin.NcsTtt|�j||gd�||_||_d|_t||d�j�t||d�j	�|_
d|_dS)zInit the plugin.)�args�configZstats_init_valueT)r
rN)�superr�__init__rr
Z
display_curserZget_ports_listrZget_web_list�stats�_thread)�selfrr
)�	__class__��#/usr/lib/python3.6/glances_ports.pyr'szPlugin.__init__cs&|jdk	r|jj�tt|�j�dS)z+Overwrite the exit method to close threads.N)r�stoprr�exit)r)rrrr9s

zPlugin.exitcCsF|jdkr@|jdkrd}n
|jj�}|s@t|j�|_|jj�n|jS)zUpdate the ports list.ZlocalNF)Zinput_methodrZis_alive�
ThreadScannerr�start)rZthread_is_runningrrr�update@s



z
Plugin.updatecCsdS)zReturn the key of the list.�indicer)rrrr�get_key[szPlugin.get_key�FcCs�d}|ddkrd}nD|ddkr(d}n2t|dttf�rZ|ddk	rZ|d|dkrZd}|j|d	�}|j||�|j||j�|||j��|S)
z?Return the alert status relative to the port scan return value.�OK�statusN�CAREFULr�CRITICAL�rtt_warning�WARNING)�header)�
isinstance�float�int�
get_stat_name�manage_threshold�
manage_action�lowerr)r�portr$�log�ret�	stat_namerrr�get_ports_alert_szPlugin.get_ports_alertcCs�d}|ddkrd}n2|dd
kr(d}n |d	dk	rH|d
|d	krHd}|j|d�}|j||�|j||j�|||j��|S)zBReturn the alert status relative to the web/url scan return value.rrNr ���-�.r!r"�elapsedr#)r$)r1r2r3)r(r)r*r+r)r�webr$r-r.r/rrr�
get_web_alertxszPlugin.get_web_alertcCs�g}|js|jr|S|d}�x�|jD�]�}d|k�r|ddkrJd}nV|ddkr\d}nDt|dt�r||ddkr|d}n$|dd	kr�d
}ndj|dd�}d
j|dd	|�|d�}|j|j|��dj|�}|j|j||j||ddd���|j|j��q(d|kr(d
j|dd	|�|d�}|j|j|��t|dt	j
��r\dj|d�}n|ddk�rpd}n|d}dj|�}|j|j||j||ddd���|j|j��q(Wy|j�Wnt
k
�r�YnX|S)z2Return the dict to display in the curse interface.��hostN�NonerZScanningTZOpenrZTimeoutz	{0:.0f}msg@�@z
{:{width}}�description)�widthz{:>9}rZ_rtt)r$�urlzCode {})rZ
disable_portsr%r�format�appendZcurse_add_liner0Zcurse_new_line�numbers�Numberr6�pop�
IndexError)rrZ	max_widthr.Zname_max_width�pr�msgrrr�	msg_curse�sH

$
$zPlugin.msg_curse)NN)rF)rF)NN)�__name__�
__module__�__qualname__�__doc__rrr
Z_check_decoratorZ_log_result_decoratorrrr0r6rE�
__classcell__rr)rrr$s

rcs|eZdZdZ�fdd�Zdd�Zedd��Zejdd��Zdd
d�Z	dd
�Z
dd�Zdd�Zdd�Z
dd�Zdd�Z�ZS)rzP
    Specific thread for the port/web scanner.

    stats is a list of dict
    cs8tjdj|��tt|�j�tj�|_||_	d|_
dS)zInit the class.z-ports plugin - Create thread for scan list {}ZportsN)r	�debugr=rrr�	threadingZEvent�_stopper�_stats�plugin_name)rr)rrrr�s

zThreadScanner.__init__cCsRxL|jD]B}|j�rPd|kr4|j|�tjd�qd|krtr|j|�qWdS)z`Grab the stats.

        Infinite loop, should be stopped by calling the stop() method.
        r,�r<N)rN�stopped�
_port_scan�timeZsleep�requests_tag�	_web_scan)rrCrrr�run�s
zThreadScanner.runcCs|jS)z
Stats getter.)rN)rrrrr�szThreadScanner.statscCs
||_dS)z
Stats setter.N)rN)r�valuerrrr�sNcCs tjdj|j��|jj�dS)zStop the thread.z,ports plugin - Close thread for scan list {}N)r	rKr=rNrM�set)r�timeoutrrrr�szThreadScanner.stopcCs
|jj�S)z%Return True is the thread is stopped.)rMZis_set)rrrrrQ�szThreadScanner.stoppedcCs�y(tj|dd|d|d|dd�}Wn<tk
rd}z tj|�d|d<d	|d
<WYdd}~XnX|j|d<|jj�|d
<|S)z3Scan the  Web/URL (dict) and update the status key.r<TZ
ssl_verify�proxiesrY)Zallow_redirectsZverifyrZrY�Errorrrr4N)�requests�head�	Exceptionr	rKZstatus_coder4Z
total_seconds)rr5Zreq�errrrU�s

zThreadScanner._web_scancCs(t|d�dkr|j|�S|j|�SdS)z9Scan the port structure (dict) and update the status key.r,rN)r'�_port_scan_icmp�_port_scan_tcp)rr,rrrrRs
zThreadScanner._port_scancCsP|}ytj|�}Wn8tk
rJ}ztjdj|j||��WYdd}~XnX|S)zConvert hostname to IP address.z({}: Cannot convert {} to IP address ({})N)�socketZ
gethostbynamer^r	rKr=rO)rZhostname�ipr_rrr�_resolv_names(zThreadScanner._resolv_namec	Csd}trd}d}ntstr$d}d}nd}d}d|d|t|j|d	��|j|d
�g}ttjd�}y:t�}t	j
|||dd
�}|dkr�|j�|d<nd|d<WnXt	jk
r�d|d<Yn<t
k
r�}z tjdj|j|d
|��WYdd}~XnX|j�|S)z@Scan the (ICMP) port structure (dict) and update the status key.Nz-wz-nz-tz-cz-WZping�1rYr8�wT)�stdout�stderrZ	close_fdsrrFz${}: Error while pinging host {} ({}))rrr�strrd�open�os�devnullr�
subprocessZ
check_call�getZCalledProcessErrorr^r	rKr=rO�close)	rr,r.Ztimeout_optZ	count_opt�cmdZfnull�counterr_rrrr`s8,zThreadScanner._port_scan_icmpc%Cs�d}y"tj|d�tjtjtj�}Wn6tk
r\}ztjdj|j|��WYdd}~XnX|j	|d�}t
�}zvy|j|t|d�f�}Wn8tk
r�}ztjdj|j||��WYdd}~Xn X|dkr�|j
�|d<nd	|d<Wd|j�X|S)
z?Scan the (TCP) port structure (dict) and update the status key.NrYz-{}: Error while creating scanning socket ({})r8r,z%{}: Error while scanning port {} ({})rrF)rbZsetdefaulttimeoutZAF_INETZSOCK_STREAMr^r	rKr=rOrdrZ
connect_exr'rnro)rr,r.Z_socketr_rcrqrrrraLs"&(
zThreadScanner._port_scan_tcp)N)rFrGrHrIrrV�propertyr�setterrrQrUrRrdr`rarJrr)rrr�s
	.r) rIrkrmrLrbrSr?Zglances.globalsrrrZglances.ports_listrZglances.web_listrZ
glances.timerrZglances.compatrZglances.loggerr	Zglances.plugins.glances_pluginr
r\rT�ImportErrorr_Zwarningr=rZThreadrrrrr�<module>
s,"