o
    g>                     @   s4   d dl Zd dlZd dlZdd Zdd Zdd ZdS )    Nc              
   C   sl   z
| | }|j jW S  tjjy   Y dS  ty   Y dS  ty5 } zd| W  Y d }~S d }~ww )NUnknown
Invalid IPzError: )countrynamegeoip2errorsZAddressNotFoundError
ValueError	Exception)ipreaderresponsee r   Z   C:\xampp\htdocs\Programación de servicios y procesos\jocarsa-ivory-main\funciones\pais.pyget_country   s   

r   c              
   C   s:  d}d}t j|r$t|d}| }W d   n1 sw   Y  ng }t }d}d}d}	t|D ]4\}
}| |krBd}|
}q3| |krNd}|
}	 n|rg| drg| 	dd  }|
| q3t| | }|svtd	 dS g }|dur|	dur|D ]}|d
| d q|d|	 | ||	d  }tdt| d n2|d ddg}|D ]}|d
| d q|d ||d  |dg | }tdt| d z%t|d}|| W d   n1 sw   Y  td| d W dS  ty } ztd| d|  W Y d}~dS d}~ww )z
    Adds the blocked IPs to the .htaccess file within specific markers using Apache 2.4+ syntax.
    Only adds new IPs that are not already present to prevent duplication.
    z# BEGIN Blocked IPs by Countryz# END Blocked IPs by CountryrNFTzRequire not ipz-No new IPs to add to the country-based block.z    Require not ip 
zAdding z; new IPs to the existing country-based blocked IPs section.z<RequireAll>
z    Require all granted
z</RequireAll>
z6Creating a new country-based blocked IPs section with z IPs.wzSuccessfully updated z! with new blocked IPs by country.zError writing to : )ospathexistsopen	readlinesset	enumeratestrip
startswithsplitaddprintappendlen
writelinesr	   )blacklisted_ipshtaccess_pathZstart_markerZ
end_markerflinesZexisting_ipsZwithin_blockZblock_start_indexZblock_end_indexindexliner
   Znew_ipsZblock_rulesZ	new_linesr   r   r   r   block_ips_htaccess   sh   


"r+   c               
   C   s  d} d}g d}d}zDt j|rM|d }t|d$}t|d}||  W d    n1 s2w   Y  W d    n1 sAw   Y  td|  W n tyh } ztd	|  W Y d }~d S d }~ww zt| d}| }	W d    n1 s~w   Y  W n+ t	y   td
|  d Y d S  ty } ztd|  W Y d }~d S d }~ww i }
|	D ]-}z!|
dd  }t| ||
vrd|
|< n|
|  d7  < W q tjy   Y qw ztj|}W n, t	y   td| d Y d S  ty } ztd|  W Y d }~d S d }~ww i }g }i }|
 D ]4\}}t||}|dvrO||v r:|||< q!||vrE|||< q!||  |7  < q!|| q!|  tt| dd dd}tt| dd dd}td | D ]\}}t| d|  q|td | D ]\}}t| d|  qtdt|  z*tdd}|D ]}|| d qW d    n	1 sw   Y  td W n ty } ztd|  W Y d }~nd }~ww z1td d}| D ]\}}|| d!| d qW d    n	1 sw   Y  td" W n ty; } ztd#|  W Y d }~nd }~ww |rHt| | d S td$ d S )%Nz/var/log/apache2/access.logzGeoLite2-Country.mmdb)ZChinaZUkraineZ	Singaporez/var/www/html/.htaccessz.backup_countryr   r   zBackup of .htaccess created at z$Error creating backup of .htaccess: zError: The file z does not exist.z*An error occurred while reading the file: -r      z"Error: The GeoLite2 database file z7An error occurred while opening the GeoLite2 database: )r   r   Errorc                 S      | d S Nr-   r   itemr   r   r   <lambda>       zmain.<locals>.<lambda>T)keyreversec                 S   r/   r0   r   r1   r   r   r   r3      r4   zDesired Countries:r   z
Blacklisted Countries:z
Unknown/Invalid: zunknown_ips_country.logr   z-Unknown IPs logged to unknown_ips_country.logz#Error writing unknown IPs to file: zblacklisted_ips_country.logz
 - Count: z@Blacklisted IPs by country logged to blacklisted_ips_country.logz'Error writing blacklisted IPs to file: z'No blacklisted IPs by country to block.)r   r   r   r   writereadr!   r	   r   FileNotFoundErrorr   r   	ipaddressZIPv4AddressZAddressValueErrorr   ZdatabaseReaderitemsr   r"   closedictsortedr#   r+   keys)Zlog_file_pathZ	mmdb_pathZnon_desired_countriesr&   Zbackup_pathZoriginalZbackupr   ZarchivoZlineasZdiccionario_ipsZlinear
   r   Zdiccionario_paisesZunknown_ipsr%   countr   Zordenado_paisesZordenado_blacklistedr'   r   r   r   mainY   s    






		rB   )Zgeoip2.databaser   r:   r   r   r+   rB   r   r   r   r   <module>   s   E