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.
// Safe to run repeatedly (e.g. via cron) — uses ON DUPLICATE KEY UPDATE.
set_time_limit(0);
date_default_timezone_set("America/Montreal");
include('conn.php');
include('functions.php');
@@ -59,14 +60,14 @@ 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) {
$src_ip = $entry['src_ip'];
$src_ip = $entry['src_ip'];
$last_seen_str = $entry['last_seen'];
// last_seen format: "2026-03-14 10:14:37.000000" — strip microseconds
@@ -82,21 +83,22 @@ foreach ($data['ips'] as $entry) {
$src_ip .= '/32';
}
$ip_safe = mysqli_real_escape_string($con, $src_ip);
$reason_safe = mysqli_real_escape_string($con, $reason);
$ip_safe = mysqli_real_escape_string($con, $src_ip);
$values[] = "('" . $ip_safe . "', " . $pull_type . ", " . $now . ", " . $enddate . ", '" . $reason_safe . "')";
}
$SQL = "INSERT INTO blacklist (ip, type, adddate, enddate, reason)"
. " VALUES ('" . $ip_safe . "', " . $pull_type . ", " . $now . ", " . $enddate . ", '" . $reason_safe . "')"
$processed = 0;
$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 . "'";
if ($con->query($SQL) === TRUE) {
if ($con->affected_rows == 1) {
$inserted++;
} else {
$updated++;
}
$processed += count($chunk);
} else {
echo "Error on " . $src_ip . ": " . $con->error . "\n";
echo "Error: " . $con->error . "\n";
}
}
@@ -109,12 +111,12 @@ $SQL_pull = "INSERT INTO info (list, last) VALUES (" . $info_list . ", " . $now
$con->query($SQL_pull);
// 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";
$con->query($SQL_bl);
}
echo "Done. Inserted: " . $inserted . ", Updated: " . $updated . ", Skipped: " . $skipped . "\n";
echo "Done. Processed: " . $processed . ", Skipped: " . $skipped . "\n";
$con->close();
?>