
    3gR                     .    d dl Zd dlZd dlZd Zd Zd Zy)    Nc                     	 |j                  |       }|j                   j                  S # t        j                  j                  $ r Y yt
        $ r Y yt        $ r}d| cY d }~S d }~ww xY w)NUnknown
Invalid IPzError: )countrynamegeoip2errorsAddressNotFoundError
ValueError	Exception)ipreaderresponsees       %/var/www/html/ivory/funciones/pais.pyget_countryr      sc    	>>"%$$$==--   }s'   &) A)A)A)A$A)$A)c                    d}d}t         j                  j                  |      r&t        |d      5 }|j	                         }ddd       ng }t               }d}d}d}	t              D ]  \  }
}|j                         |k(  rd}|
}|j                         |k(  rd}|
}	 nf|s:|j                         j                  d      sZ|j                         j                  d      d   j                         }|j                  |        t        |       |z
  }|st        d	       yg }|E|	C|D ]  }|j                  d
| d        |d|	 |z   ||	d z   }t        dt        |       d       nj|dz   ddg}|D ]  }|j                  d
| d        |j                  d       |j                  |dz          |dgz   |z   }t        dt        |       d       	 t        |d      5 }|j                  |       ddd       t        d| d       y# 1 sw Y   xY w# 1 sw Y   &xY w# t        $ r}t        d| d|        Y d}~yd}~ww xY 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_pathstart_marker
end_markerflinesexisting_ipswithin_blockblock_start_indexblock_end_indexindexliner   new_ipsblock_rules	new_linesr   s                    r   block_ips_htaccessr7      s    4L/J 
ww~~m$-% 	"KKME	" 	"  5LLO ' %t::<<'L %::<:% L#Ozz|&&'78ZZ\''(89"=CCE  $% /"\1G=> K$)D 	=B!4RD;<	= *?+k9E/BR<SS	G~%`ab $d*,<>YZ 	=B!4RD;<	=,-:,-TFN[0	Fs7|nTYZ[8-% 	$LL#	$%m_4UVWm	" 	"h	$ 	$  8!-1#6778s;   H
H#  H2H# 
HH H# #	I,IIc                     d} d}g d}d}	 t         j                  j                  |      r\|dz   }t        |d      5 }t        |d      5 }|j	                  |j                                d d d        d d d        t        d|        	 t        | d      5 }|j                         }	d d d        i }
	D ]Q  }	 |j                  d      d   j                         }t        j                  |       ||
vrd|
|<   n|
|xx   dz  cc<   S 	 t        j                   j#                  |      }i }g }i }|
j%                         D ]H  \  }}t'        ||      }|dvr"||v r|||<    ||vr|||<   *||xx   |z  cc<   8|j)                  |       J |j+                          t-        t/        |j%                         d d            }t-        t/        |j%                         d d            }t        d       |j%                         D ]  \  }}t        | d|         t        d       |j%                         D ]  \  }}t        | d|         t        dt1        |              	 t        dd      5 }|D ]  }|j	                  | d        	 d d d        t        d       	 t        dd      5 }|j%                         D ]  \  }}|j	                  | d | d        	 d d d        t        d!       |rt3        |j5                         |       y t        d#       y # 1 sw Y   xY w# 1 sw Y   xY w# t        $ r}t        d	|        Y d }~y d }~ww xY w# 1 sw Y   xY w# t        $ r t        d
|  d       Y y t        $ r}t        d|        Y d }~y d }~ww xY w# t        j                  $ r Y w xY w# t        $ r t        d| d       Y y t        $ r}t        d|        Y d }~y d }~ww xY w# 1 sw Y   nxY w# t        $ r}t        d|        Y d }~d }~ww xY w# 1 sw Y   OxY w# t        $ r}t        d"|        Y d }~dd }~ww xY w)$Nz/var/log/apache2/access.logz)/var/www/html/ivory/GeoLite2-Country.mmdb)ChinaUkraine	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                     | d   S Nr=    items    r   <lambda>zmain.<locals>.<lambda>   
    T!W     T)keyreversec                     | d   S r@   rA   rB   s    r   rD   zmain.<locals>.<lambda>   rE   rF   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    	ipaddressIPv4AddressAddressValueErrorr   databaseReaderitemsr   r%   closedictsortedr&   r7   keys)log_file_path	mmdb_pathnon_desired_countriesr)   backup_pathoriginalbackupr   archivolineasdiccionario_ipslinear   r   diccionario_paisesunknown_ipsr(   countr   ordenado_paisesordenado_blacklistedr,   s                         r   mainrf   Y   s   1M <I .M77>>-('*;;KmS) .XtK7M .QWX]]_-. .3K=AB-% 	)&&(F	) O 	S!!$**,B!!"%(&'##q(#''	2 KO %**, #	Eb&)<<//&+# "4427&w/&w/58/r"#  LLN $$&$	
O  !!#$	
 

)//1 %	E7#$% 

$%)//1  	EBug  
K 01
239+S1 	#Q! #2$b	"#	# 	=>
=/5 	41779 4	E2$jr234	4 	PQ
 /446F78s. . . .  4QC89	) 	)  /?@A :1#>?  ** 		  29+=MNO GsKLz	# 	#  93A37889
	4 	4  =7s;<<=s  0L0 L#	 L)L#1L0 M! M%M! 5ANN8  P ,O5	P P4 )0P'P4 L 	L##L-(L0 0	M9MMMM! !N;NNNN54N58O2O2O--O25O?:P 	P$PP$'P1,P4 4	Q=QQ)geoip2.databaser   rM   r   r   r7   rf   rA   rF   r   <module>rh      s"      	
C8JN9rF   