o
    gn                     @   s$   d dl Z d dlZdd Zdd ZdS )    Nc              
   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 User Agentz# END Blocked IPs by User AgentrNFTzRequire not ipz0No new IPs to add to the user-agent-based block.z    Require not ip 
zAdding z> new IPs to the existing user-agent-based blocked IPs section.z<RequireAll>
z    Require all granted
z</RequireAll>
z9Creating a new user-agent-based blocked IPs section with z IPs.wzSuccessfully updated z$ with new blocked IPs by user agent.zError writing to z: )ospathexistsopen	readlinesset	enumeratestrip
startswithsplitaddprintappendlen
writelines	Exception)blacklisted_ipshtaccess_pathstart_marker
end_markerflinesexisting_ipswithin_blockblock_start_indexblock_end_indexindexlineipnew_ipsblock_rules	new_linese r'   f   C:\xampp\htdocs\Programación de servicios y procesos\jocarsa-ivory-main\funciones\user_agent_block.pyblock_ips_htaccess   sh   


"r)   c               
   C   s  d} d}zDt j|rG|d }t|d$}t|d}||  W d    n1 s,w   Y  W d    n1 s;w   Y  td|  W n tyb } ztd|  W Y d }~d S d }~ww zt| d}| }W d    n1 sxw   Y  W n+ t	y   td|  d	 Y d S  ty } ztd
|  W Y d }~d S d }~ww t
 }|D ];}	z'|	d}
t|
dkr|
d  }|dkr|	 d }t| || W q tjy   Y q ty   Y qw t|}td |D ]}t| qz*tdd}|D ]}|| d qW d    n	1 sw   Y  td W n ty@ } ztd|  W Y d }~nd }~ww |rKt|| d S td d S )Nz/var/log/apache2/access.logz/var/www/html/.htaccessz.backup_user_agentr   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Blacklisted IPs by User Agent:zblacklisted_ips_user_agent.logr   zFBlacklisted IPs by user agent logged to blacklisted_ips_user_agent.logz'Error writing blacklisted IPs to file: z*No blacklisted IPs by user agent to block.)r   r   r   r	   writereadr   r   r
   FileNotFoundErrorr   r   r   r   	ipaddressIPv4Addressr   AddressValueError
IndexErrorsortedr)   )log_file_pathr   backup_pathoriginalbackupr&   archivolineasr   lineapartsZ
user_agentr"   ordenado_blacklistedr   r'   r'   r(   mainK   s    




r?   )r1   r   r)   r?   r'   r'   r'   r(   <module>   s   E