o
    Ìv›a:  ã                
   @   sÆ   d Z ddlZddlmZ ddlmZmZmZ ddlm	Z	 ddl
mZ ddlmZ zddlZW n eyJ Z zdZe	 d	 e¡¡ W Y dZ[ndZ[ww d
Zg d¢ZG dd„ deƒZG dd„ deƒZdS )z
IP plugin.é    N)Úloads)ÚiterkeysÚurlopenÚqueue)Úlogger)ÚTimer)ÚGlancesPluginTz.Missing Python Lib ({}), IP plugin is disabledF))zhttps://ip.42.pl/rawFN)zhttps://httpbin.org/ipTÚorigin)zhttps://jsonip.comTÚip)z"https://api.ipify.org/?format=jsonTr
   c                       sX   e Zd ZdZd‡ fdd„	Zejejdd„ ƒƒZ‡ fdd„Z	dd	d
„Z
edd„ ƒZ‡  ZS )ÚPluginz,Glances IP Plugin.

    stats is a dict
    Nc                    s2   t t| ƒj||d d| _tstƒ  ¡ | _dS dS )zInit the plugin.)ÚargsÚconfigTN)Úsuperr   Ú__init__Zdisplay_curseÚimport_error_tagÚPublicIpAddressÚgetÚpublic_address)Úselfr   r   ©Ú	__class__© ú</usr/lib/python3/dist-packages/glances/plugins/glances_ip.pyr   8   s
   ÿzPlugin.__init__c              
   C   s2  |   ¡ }| jdkrtszt ¡ d tj }W n ttfy2 } zt 	d 
|¡¡ W Y d}~ned}~ww z=t |d ¡tj d d |d< t |d ¡tj d d	 |d
< |  |d
 ¡|d< t ¡ d tj d |d< | j|d< W n" ttfyŒ } zt 	d 
|¡¡ W Y d}~nd}~ww | jdkr“	 || _| jS )zGUpdate IP stats using the input method.

        Stats is dict
        ZlocalÚdefaultz$Cannot grab the default gateway ({})Né   r   ZaddrÚaddressZnetmaskÚmaskÚ	mask_cidrZgatewayr   zCannot grab IP information: {}Zsnmp)Zget_init_valueZinput_methodr   Ú	netifacesZgatewaysZAF_INETÚKeyErrorÚAttributeErrorr   ÚdebugÚformatZifaddressesÚ
ip_to_cidrr   Ústats)r   r$   Z
default_gwÚer   r   r   ÚupdateC   s,   €ÿ  €ÿ
zPlugin.updatec                    s0   t t| ƒ ¡  t| jƒD ]	}d| j| d< qdS )zUpdate stats views.TZoptionalN)r   r   Úupdate_viewsr   r$   Zviews)r   Úkeyr   r   r   r'   e   s   ÿzPlugin.update_viewsc              	   C   sþ   g }| j r|  ¡ str|S d}| |  |¡¡ d}| |  |d¡¡ d| j v r7d | j d ¡}| |  |¡¡ d| j v rLd | j d ¡}| |  |¡¡ z
d | j d ¡}W n ttfyb   Y |S w | j d d	ur}d
}| |  |d¡¡ | |  |¡¡ |S )z2Return the dict to display in the curse interface.z - zIP ZTITLEr   z{}r   z/{}r   Nz Pub )r$   Zis_disabledr   ÚappendZcurse_add_liner"   ÚUnicodeEncodeErrorr   )r   r   Z	max_widthÚretÚmsgZmsg_pubr   r   r   Ú	msg_curseo   s0   

÷zPlugin.msg_cursec                 C   s$   | du rdS t dd„ |  d¡D ƒƒS )zUConvert IP address to CIDR.

        Example: '255.255.255.0' will return 24
        Nr   c                 s   s"    | ]}t t|ƒƒ d ¡V  qdS )Ú1N)ÚbinÚintÚcount©Ú.0Úxr   r   r   Ú	<genexpr>œ   s   €  z$Plugin.ip_to_cidr.<locals>.<genexpr>Ú.)ÚsumÚsplit)r
   r   r   r   r#   ‘   s   zPlugin.ip_to_cidr)NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   Z_check_decoratorZ_log_result_decoratorr&   r'   r-   Ústaticmethodr#   Ú__classcell__r   r   r   r   r   2   s     

"r   c                   @   s,   e Zd ZdZddd„Zdd„ Zdd	d
„ZdS )r   z+Get public IP address from online services.é   c                 C   s
   || _ dS )zInit the class.N©Útimeout)r   rA   r   r   r   r   ¢   s   
zPublicIpAddress.__init__c                 C   sª   t  ¡ }tD ]\}}}tj| j||||fd}d|_| ¡  qt| j	ƒ}d}| 
¡ s@|du r@| ¡ dkr8| ¡ }| 
¡ s@|du s.|du rFdS d tdd„ | d¡D ƒƒ¡S )	zGGet the first public IP address returned by one of the online services.)Útargetr   TNr   z, c                 S   s   g | ]}|  ¡ ‘qS r   )Ústripr2   r   r   r   Ú
<listcomp>¸   s    z'PublicIpAddress.get.<locals>.<listcomp>ú,)r   ZQueueÚurlsÚ	threadingZThreadÚ_get_ip_publicZdaemonÚstartr   rA   ZfinishedZqsizer   ÚjoinÚsetr8   )r   ÚqÚuÚjÚkÚtZtimerr
   r   r   r   r   ¦   s   

þzPublicIpAddress.getFNc              
   C   s¬   zt || jd ¡  d¡}W n! ty/ } zt d ||¡¡ | d¡ W Y d}~dS d}~ww z|s;| |¡ W dS | t	|ƒ| ¡ W dS  t
yU   | d¡ Y dS w )z?Request the url service and put the result in the queue_target.r@   zutf-8z#IP plugin - Cannot open URL {} ({})N)r   rA   ÚreadÚdecodeÚ	Exceptionr   r!   r"   Zputr   Ú
ValueError)r   Zqueue_targetZurlÚjsonr(   Zresponser%   r   r   r   rH   º   s   €þÿzPublicIpAddress._get_ip_public)r?   )FN)r9   r:   r;   r<   r   r   rH   r   r   r   r   r   Ÿ   s
    
r   )r<   rG   rU   r   Zglances.compatr   r   r   Zglances.loggerr   Zglances.timerr   Zglances.plugins.glances_pluginr   r   ÚImportErrorr%   r   Zwarningr"   rF   r   Úobjectr   r   r   r   r   Ú<module>   s$   €þm