
    3gn                          d dl Z d dlZd Zd Zy)    Nc                    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 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es                    1/var/www/html/ivory/funciones/user_agent_block.pyblock_ips_htaccessr*      s    7L2J 
ww~~m$-% 	"KKME	" 	"  5LLO ' %t::<<'L %::<:% L#Ozz|&&'78ZZ\''(89"=CCE  $% /"\1G@A K$)D 	=B!4RD;<	= *?+k9E/BR<SS	G~%cde $d*,<>YZ 	=B!4RD;<	=,-:,-TFN[0	I#g,W\]^8-% 	$LL#	$%m_4XYZm	" 	"h	$ 	$  8!-1#6778s;   H
H#  H2H# 
HH H# #	I,IIc                     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        t               }D ]s  }		 |	j                  d      }
t        |
      dk\  rQ|
d   j                         }|dk(  r9|	j                         d   }t        j                  |       |j!                  |       u t'        |      }t        d       |D ]  }t        |        	 t        dd      5 }|D ]  }|j	                  | d        	 d d d        t        d       |rt)        ||       y t        d       y # 1 sw Y   HxY w# 1 sw Y   MxY w# t        $ r}t        d|        Y d }~y d }~ww xY w# 1 sw Y   Jx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 t$        $ r Y w xY w# 1 sw Y   xY w# t        $ r}t        d|        Y d }~d }~ww xY w)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parts
user_agentr$   ordenado_blacklistedr   s                  r)   mainrB   K   s   1M .M77>>-('*>>KmS) .XtK7M .QWX]]_-. .3K=AB-% 	)&&(F	) eO 	 KK$E5zQ"1X^^-
$q)B))"-#''+* "/2 

*+" b	=2C8 	#A* #2$b	"#	# 	VW
 /?:;{. . . .  4QC89	) 	)  /?@A :1#>?$ ** 	 			# 	#  =7s;<<=s   0G F6 F)#F6+G G4 G'G4 7A0H1I" I;I" )F3	.F66G ;G 	G$GG$'G1,G4 4H.H.H))H.1IIIII" "	J+I>>J)r3   r   r*   rB        r)   <module>rE      s     	C8JH<rD   