Author |
Message |
rovshan
Hangin' Around
Joined: Nov 26, 2005
Posts: 40
|
Posted:
Tue Apr 23, 2019 10:00 am |
|
Here is what I found in:
1. RavenNuke-2.5.1 (not php7) distribution => nukesentinel.php =>
// Clearing of expired blocks
// CAUTION: This function can slow your sites load time
$clearedtime = strtotime(date('Y-m-d 23:59:59', $nsnst_const['ban_time']));
$cleartime = strtotime(date('Y-m-d 23:59:59', $nsnst_const['ban_time'])) - 86400;
if( $ab_config['self_expire'] == 1 AND $ab_config['blocked_clear'] < $cleartime) {
does not found this value $ab_config['blocked_clear'] in config table => even not in INSTALL SQL file // seems missed
BUT....
2. RavenNuke-master (php7) distribution => nukesentinel.php =>
// Clearing of expired blocks
// CAUTION: This function can slow your sites load time
$clearedtime = strtotime(date('Y-m-d 23:59:59', $nsnst_const['ban_time']));
$cleartime = strtotime(date('Y-m-d 23:59:59', $nsnst_const['ban_time'])) - 86400;
if( $ab_config['self_expire'] == 1 AND $clearedtime < $cleartime) {
Which one is correct ?!
I just remarked that Blocked IPs for limited time (1,2 days) still in DB... need to be AUTO removed... |
|
|
|
|
neralex
Site Admin
Joined: Aug 22, 2007
Posts: 1775
|
Posted:
Tue Apr 23, 2019 10:28 am |
|
That is a very good question, because I found the same strange situation while patching it for php7 and I fixed only the PHP-error, which was caused by the undefined index 'blocked_clear'. Maybe it was an option of prior versions. So I replaced only $ab_config['blocked_clear'] with $clearedtime in the if-statemanet, which you mentioned.
But the db-field: 'self_expire' exists in the _nsnst_config db-table. It is set to '0' per default. You can try to set it from '0' to '1' and see what happend. I never used this feature - feel free to test it and let me know when you get php-errors.
Edit: I tested again and maybe I made a mistake in the if-statement and this is right way because $cleartime is smaller than $clearedtime:
php Code:if($cleartime < $clearedtime) {
|
|
_________________ Only registered users can see links on this board! Get registered or login! |
|
|
|
rovshan
|
Posted:
Wed Apr 24, 2019 4:04 pm |
|
Here is how I fixed it :
1. Added $result = $db->sql_query("INSERT INTO `".$prefix."_nsnst_config` VALUES ('blocked_clear', '0')");
2. Modified code in // Clearing of expired blocks part :
php Code:
// Clearing of expired blocks
// CAUTION: This function can slow your sites load time
$clearedtime = strtotime(date('Y-m-d 23:59:59', $nsnst_const['ban_time']));
$cleartime = strtotime(date('Y-m-d 23:59:59', $nsnst_const['ban_time'])) - 86400;
if( $ab_config['self_expire'] == 1 AND $ab_config['blocked_clear'] < $cleartime) {
// check if minimum one blocker configured for save in htaccess file
$htnum = $db->sql_numrows($db->sql_query('SELECT * FROM `' . $prefix . '_nsnst_blockers` WHERE `htaccess`!="0" '));
$clearresult = $db->sql_query('SELECT * FROM `' . $prefix. '_nsnst_blocked_ips` WHERE (`expires` < "' . $clearedtime . '" AND `expires`!="0")');
while($clearblock = $db->sql_fetchrow($clearresult)) {
$a_ip = $clearblock['ip_addr'] ;
if(!empty($ab_config['htaccess_path']) AND $htnum > 0 ) {
$ipfile = file($ab_config['htaccess_path']);
$ipfile = implode('', $ipfile);
$i = 1;
while ($i <= 3) {
$tip = substr($clearblock['ip_addr'], -2);
if($tip == '.*') { $clearblock['ip_addr'] = substr($clearblock['ip_addr'], 0, -2); }
$i++;
}
$testip = 'deny from ' . $clearblock['ip_addr'] . "\n";
$ipfile = str_replace($testip, '', $ipfile);
$doit = @fopen($ab_config['htaccess_path'], 'w');
@fwrite($doit, $ipfile);
@fclose($doit);
}
$db->sql_query('DELETE FROM `' . $prefix . '_nsnst_blocked_ips` WHERE `ip_addr`="' . $a_ip . '"');
$db->sql_query('OPTIMIZE TABLE `' . $prefix . '_nsnst_blocked_ips`');
}
$clearresult = $db->sql_query('SELECT * FROM `' . $prefix . '_nsnst_blocked_ranges` WHERE (`expires`<"' . $clearedtime . '" AND `expires`!="0")');
while($clearblock = $db->sql_fetchrow($clearresult)) {
$old_masscidr = ABGetCIDRs($clearblock['ip_lo'], $clearblock['ip_hi']);
if(!empty($ab_config['htaccess_path']) AND $htnum > 0) {
$old_masscidr = explode('||', $old_masscidr);
for ($i=0, $maxi=sizeof($old_masscidr); $i < $maxi; $i++) {
if(!empty($old_masscidr[$i])) {
$old_masscidr[$i] = 'deny from ' . $old_masscidr[$i] . "\n";
}
}
$ipfile = file($ab_config['htaccess_path']);
$ipfile = implode('', $ipfile);
$ipfile = str_replace($old_masscidr, '', $ipfile);
$ipfile = $ipfile;
$doit = @fopen($ab_config['htaccess_path'], 'w');
@fwrite($doit, $ipfile);
@fclose($doit);
}
$db->sql_query('DELETE FROM `' . $prefix . '_nsnst_blocked_ranges` WHERE `ip_lo`="' . $clearblock['ip_lo'] . '" AND `ip_hi`="' . $clearblock['ip_hi'] . '"');
$db->sql_query('OPTIMIZE TABLE `' . $prefix . '_nsnst_blocked_ranges`');
}
$db->sql_query('UPDATE `' . $prefix . '_nsnst_config` SET `config_value`="' . $clearedtime . '" WHERE `config_name`="blocked_clear"');
}
|
I am not sure that OPTIMIZE query should be in loop function ?! |
|
|
|
|
neralex
|
Posted:
Thu Apr 25, 2019 11:57 am |
|
rovshan, let me say thank you!
After checking the installation-files, I found the entry, which is adding the field blocked_clear into the _nsnst_config table inside the rndb_upgrade_nukesentinel.php. I never noticed it but I added this field also into the ns_core.sql, which is used while a fresh installation and I added this into the database-upgrade function of the rndb_upgrade.php. So there are existing now two ways to add this field into the config-table of NukeSentinel, when RN is already installed.
My suggestion for your question about the placing of the OPTIMIZE-queries: You are right, to let run this query after each record could be end in a very high usage. I would count the records inside the while-loops and if the count of records are > 0, then I would run the query only once outside of the while-loops like this:
php Code:// Clearing of expired blocks
// CAUTION: This function can slow your sites load time
$clearedtime = strtotime(date('Y-m-d 23:59:59', $nsnst_const['ban_time']));
$cleartime = strtotime(date('Y-m-d 23:59:59', $nsnst_const['ban_time'])) - 86400;
if( $ab_config['self_expire'] == 1 AND $ab_config['blocked_clear'] < $clearedtime) {
// check if minimum one blocker configured for save in htaccess file
$htnum = $db->sql_numrows($db->sql_query('SELECT * FROM `' . $prefix . '_nsnst_blockers` WHERE `htaccess`!="0" '));
// if the value are 0, there is no need to optimize the tables
$optimize_blocked_ips = 0;
$optimize_blocked_ranges = 0;
$clearresult = $db->sql_query('SELECT * FROM `' . $prefix. '_nsnst_blocked_ips` WHERE (`expires` < "' . $clearedtime . '" AND `expires`!="0")');
while($clearblock = $db->sql_fetchrow($clearresult)) {
if(!empty($ab_config['htaccess_path']) AND $htnum > 0) {
$ipfile = file($ab_config['htaccess_path']);
$ipfile = implode('', $ipfile);
$i = 1;
while ($i <= 3) {
$tip = substr($clearblock['ip_addr'], -2);
if($tip == '.*') { $clearblock['ip_addr'] = substr($clearblock['ip_addr'], 0, -2); }
$i++;
}
$testip = 'deny from ' . $clearblock['ip_addr'] . "\n";
$ipfile = str_replace($testip, '', $ipfile);
$doit = @fopen($ab_config['htaccess_path'], 'w');
@fwrite($doit, $ipfile);
@fclose($doit);
// count the records, to the check if the table should be optimized
$optimize_blocked_ips++;
}
$db->sql_query('DELETE FROM `' . $prefix . '_nsnst_blocked_ips` WHERE `ip_addr`="' . $clearblock['ip_addr'] . '"');
}
if ($optimize_blocked_ips > 0) {
$db->sql_query('OPTIMIZE TABLE `' . $prefix . '_nsnst_blocked_ips`');
}
$clearresult = $db->sql_query('SELECT * FROM `' . $prefix . '_nsnst_blocked_ranges` WHERE (`expires`<"' . $clearedtime . '" AND `expires`!="0")');
while($clearblock = $db->sql_fetchrow($clearresult)) {
$old_masscidr = ABGetCIDRs($clearblock['ip_lo'], $clearblock['ip_hi']);
if(!empty($ab_config['htaccess_path']) AND $htnum > 0) {
$old_masscidr = explode('||', $old_masscidr);
for ($i=0, $maxi=sizeof($old_masscidr); $i < $maxi; $i++) {
if(!empty($old_masscidr[$i])) {
$old_masscidr[$i] = 'deny from ' . $old_masscidr[$i] . "\n";
}
}
$ipfile = file($ab_config['htaccess_path']);
$ipfile = implode('', $ipfile);
$ipfile = str_replace($old_masscidr, '', $ipfile);
$ipfile = $ipfile;
$doit = @fopen($ab_config['htaccess_path'], 'w');
@fwrite($doit, $ipfile);
@fclose($doit);
// count the records, to the check if the table should be optimized
$optimize_blocked_ranges++;
}
$db->sql_query('DELETE FROM `' . $prefix . '_nsnst_blocked_ranges` WHERE `ip_lo`="' . $clearblock['ip_lo'] . '" AND `ip_hi`="' . $clearblock['ip_hi'] . '"');
}
if ($optimize_blocked_ranges > 0) {
$db->sql_query('OPTIMIZE TABLE `' . $prefix . '_nsnst_blocked_ranges`');
}
$db->sql_query('UPDATE `' . $prefix . '_nsnst_config` SET `config_value`="' . $clearedtime . '" WHERE `config_name`="blocked_clear"');
}
|
Added to the Github-repo: https://github.com/neralex/RavenNuke/commit/de00827df6fe9ca6f2116b150b1030b28601a2ad
Any suggestion? |
|
|
|
|
|