U
    @6^                     @   s0   d dl Z d dlmZmZmZ G dd deZdS )    N)
SpiderFootSpiderFootPluginSpiderFootEventc                   @   sV   e Zd ZdZddiZddiZdZdZe fddZ	d	d
 Z
dd Zdd Zdd ZdS )
sfp_ipinfozIPInfo.io:Footprint,Investigate,Passive:Real World:apikey:Identifies the physical location of IP addresses identified using ipinfo.io.api_key zIpinfo.io access token.NFc                 C   s:   || _ |  | _d| _t| D ]}|| | j|< q"d S )NF)sftempStorageresults
errorStatelistkeysopts)selfsfcuserOptsopt r   5/var/www/spiderfoot.crq.systems/modules/sfp_ipinfo.pysetup    s
    
zsfp_ipinfo.setupc                 C   s   ddgS )N
IP_ADDRESSIPV6_ADDRESSr   r   r   r   r   watchedEvents)   s    zsfp_ipinfo.watchedEventsc                 C   s   dgS )NGEOINFOr   r   r   r   r   producedEvents/   s    zsfp_ipinfo.producedEventsc              
   C   s   dd| j d  i}| jjd| d | j d | j d |d}|d	 d
kr^| jdd d| _d S |d d kr~| jd|  d S zt|d }W n4 tk
r } z| j	d W Y d S d }~X Y nX |S )NAuthorizationzBearer r   zhttps://ipinfo.io/z/json_fetchtimeout
_useragent)timeout	useragentheaderscode429z(You are being rate-limited by ipinfo.io.FTcontentzNo GeoIP info found for zError processing JSON response.)
r   r   fetchUrlerrorr   infojsonloads	Exceptiondebug)r   ipr!   resresulter   r   r   queryIP3   s*     zsfp_ipinfo.queryIPc                 C   s
  |j }|j}|j}| jrd S | jd| d |  | jd dkrZ| jdd d| _d S || jkr|| jd| d	  d S d| j|< | 	|}|d krd S d
|krd S d
dd |d|d|d
fD }| jd| d |  td|| j|}| | d S )NzReceived event, z, from r   r   z2You enabled sfp_ipinfo but did not set an API key!FTz	Skipping z as already mapped.countryz, c                 S   s   g | ]}|r|qS r   r   ).0Z_fr   r   r   
<listcomp>l   s      z*sfp_ipinfo.handleEvent.<locals>.<listcomp>cityregionzFound GeoIP for z: r   )	eventTypemoduledatar   r   r+   r   r&   r
   r0   joingetr'   r   __name__notifyListeners)r   event	eventNamesrcModuleName	eventDatar8   locationevtr   r   r   handleEventN   s0    


,
zsfp_ipinfo.handleEvent)r;   
__module____qualname____doc__r   optdescsr
   r   dictr   r   r   r0   rC   r   r   r   r   r      s     	r   )r(   sflibr   r   r   r   r   r   r   r   <module>   s   