This commit is contained in:
2026-03-16 07:42:38 -04:00
parent 73af9b52cc
commit 8b0ee04fa4

View File

@@ -4,6 +4,7 @@
// Each run logs itself in the info table using the current timestamp as list value. // Each run logs itself in the info table using the current timestamp as list value.
// Safe to run repeatedly (e.g. via cron) — uses ON DUPLICATE KEY UPDATE. // Safe to run repeatedly (e.g. via cron) — uses ON DUPLICATE KEY UPDATE.
set_time_limit(0);
date_default_timezone_set("America/Montreal"); date_default_timezone_set("America/Montreal");
include('conn.php'); include('conn.php');
include('functions.php'); include('functions.php');
@@ -59,11 +60,11 @@ if (!isset($data['ips']) || !is_array($data['ips'])) {
} }
// ------------------------------------------------------------------ // ------------------------------------------------------------------
// 3. Ingest IPs newer than last_pulled // 3. Ingest IPs newer than last_pulled (single bulk query)
// ------------------------------------------------------------------ // ------------------------------------------------------------------
$inserted = 0;
$updated = 0;
$skipped = 0; $skipped = 0;
$values = [];
$reason_safe = mysqli_real_escape_string($con, $reason);
foreach ($data['ips'] as $entry) { foreach ($data['ips'] as $entry) {
$src_ip = $entry['src_ip']; $src_ip = $entry['src_ip'];
@@ -83,20 +84,21 @@ foreach ($data['ips'] as $entry) {
} }
$ip_safe = mysqli_real_escape_string($con, $src_ip); $ip_safe = mysqli_real_escape_string($con, $src_ip);
$reason_safe = mysqli_real_escape_string($con, $reason); $values[] = "('" . $ip_safe . "', " . $pull_type . ", " . $now . ", " . $enddate . ", '" . $reason_safe . "')";
}
$SQL = "INSERT INTO blacklist (ip, type, adddate, enddate, reason)" $processed = 0;
. " VALUES ('" . $ip_safe . "', " . $pull_type . ", " . $now . ", " . $enddate . ", '" . $reason_safe . "')" $chunk_size = 100;
foreach (array_chunk($values, $chunk_size) as $chunk) {
$SQL = "INSERT INTO blacklist (ip, type, adddate, enddate, reason) VALUES "
. implode(", ", $chunk)
. " ON DUPLICATE KEY UPDATE enddate=" . $enddate . ", type=" . $pull_type . ", reason='" . $reason_safe . "'"; . " ON DUPLICATE KEY UPDATE enddate=" . $enddate . ", type=" . $pull_type . ", reason='" . $reason_safe . "'";
if ($con->query($SQL) === TRUE) { if ($con->query($SQL) === TRUE) {
if ($con->affected_rows == 1) { $processed += count($chunk);
$inserted++;
} else { } else {
$updated++; echo "Error: " . $con->error . "\n";
}
} else {
echo "Error on " . $src_ip . ": " . $con->error . "\n";
} }
} }
@@ -109,12 +111,12 @@ $SQL_pull = "INSERT INTO info (list, last) VALUES (" . $info_list . ", " . $now
$con->query($SQL_pull); $con->query($SQL_pull);
// Update blacklist last-modified only when rows were actually written // Update blacklist last-modified only when rows were actually written
if ($inserted > 0 || $updated > 0) { if ($processed > 0) {
$SQL_bl = "UPDATE info SET last=" . $now . " WHERE list=0"; $SQL_bl = "UPDATE info SET last=" . $now . " WHERE list=0";
$con->query($SQL_bl); $con->query($SQL_bl);
} }
echo "Done. Inserted: " . $inserted . ", Updated: " . $updated . ", Skipped: " . $skipped . "\n"; echo "Done. Processed: " . $processed . ", Skipped: " . $skipped . "\n";
$con->close(); $con->close();
?> ?>