Skip to content

Commit d11f976

Browse files
author
SMKRV
committed
refactor: Enhanced MD5 source validation for more reliable change detection, and upgraded filtration mechanisms
fix: shellcheck errors
1 parent 6faabd1 commit d11f976

File tree

1 file changed

+55
-34
lines changed

1 file changed

+55
-34
lines changed

scripts/mikrotik-domain-filter-bash.sh

Lines changed: 55 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,6 @@ export DNS_RATE_LIMIT
7979
export LOG_FILE
8080
export DNS_MAX_RETRIES
8181

82-
# Global variables for statistics
83-
# declare -i TOTAL_DOMAINS=0
84-
8582
# Enable debugging
8683
# exec 2>"${WORK_DIR}/debug.log"
8784

@@ -283,13 +280,11 @@ cleanup() {
283280

284281
cleanup_invalid_cache() {
285282
log "Cleaning invalid cache entries..."
286-
find "$CACHE_DIR" -type f -name "*.cache" -exec sh -c '
287-
for f; do
288-
if ! grep -qE "^(valid|invalid)$" "$f"; then
289-
rm -f "$f"
290-
fi
291-
done
292-
' sh {} +
283+
find "$CACHE_DIR" -type f -name "*.cache" -print0 | while IFS= read -r -d '' f; do
284+
if ! grep -qE "^(valid|invalid)$" "$f"; then
285+
rm -f "$f"
286+
fi
287+
done
293288
}
294289

295290
log_cache_stats() {
@@ -356,7 +351,8 @@ save_state() {
356351
fi
357352

358353
# Calculate MD5 sums with error checking
359-
if ! main_md5=$(md5sum "$OUTPUT_FILE" 2>/dev/null); then
354+
main_md5=$(md5sum "$OUTPUT_FILE" 2>/dev/null)
355+
if [[ $? -ne 0 ]]; then
360356
log "ERROR: Failed to calculate MD5 for main list"
361357
return 1
362358
fi
@@ -585,7 +581,8 @@ export -f validate_domain
585581
extract_domains() {
586582
local input=$1
587583
local output=$2
588-
local temp_output="${TMP_DIR}/extracted_$(date +%s).tmp"
584+
local temp_output
585+
temp_output="${TMP_DIR}/extracted_$(date +%s).tmp"
589586

590587
log "Extracting domains from: $input"
591588

@@ -677,7 +674,8 @@ extract_domains() {
677674
initial_filter() {
678675
local input=$1
679676
local output=$2
680-
local temp_output="${TMP_DIR}/filtered_$(date +%s).tmp"
677+
local temp_output
678+
temp_output="${TMP_DIR}/filtered_$(date +%s).tmp"
681679

682680
log "Initial filtering of: $input"
683681

@@ -706,7 +704,6 @@ initial_filter() {
706704
grep -v '^#' | \
707705
grep -v '^$' | \
708706
tr '[:upper:]' '[:lower:]' | \
709-
tr -d ' ' | \
710707
awk 'length <= 253' > "$temp_output"; then
711708
log "ERROR: Domain filtering failed"
712709
rm -f "$temp_output"
@@ -960,8 +957,10 @@ apply_whitelist() {
960957
local input=$1
961958
local whitelist=$2
962959
local output=$3
963-
local temp_pattern="${TMP_DIR}/whitelist_pattern_$(date +%s).tmp"
964-
local temp_output="${TMP_DIR}/whitelist_filtered_$(date +%s).tmp"
960+
local temp_pattern
961+
temp_pattern="${TMP_DIR}/whitelist_pattern_$(date +%s).tmp"
962+
local temp_output
963+
temp_output="${TMP_DIR}/whitelist_filtered_$(date +%s).tmp"
965964

966965
log "Applying whitelist to: $input"
967966

@@ -1410,7 +1409,7 @@ check_updates_needed() {
14101409

14111410
if [[ "$process_failed" == "true" ]]; then
14121411
log "ERROR: Failed to process one or more sources"
1413-
find "$temp_dir" -type f ! -name "*md5" -delete 2>/dev/null || true
1412+
find "$temp_dir" -type f ! -name "*md5" -delete 2>/dev/null || :
14141413
return 1
14151414
fi
14161415

@@ -1419,14 +1418,14 @@ check_updates_needed() {
14191418
local changed=false
14201419
while IFS=' ' read -r source md5; do
14211420
local prev_line
1422-
prev_line=$(grep "^${source}" "$previous_md5" || echo "")
1421+
prev_line=$(grep "^${source}[[:space:]]" "$previous_md5" || echo "")
14231422

14241423
if [[ -z "$prev_line" ]]; then
14251424
changed=true
1426-
log "Content changed for source: $source"
1425+
log "Content changed for source: $source (no previous entry)"
14271426
else
14281427
local prev_md5
1429-
prev_md5=$(echo "$prev_line" | cut -d' ' -f2)
1428+
prev_md5=$(echo "$prev_line" | awk '{print $2}')
14301429

14311430
if [[ "$md5" != "$prev_md5" ]]; then
14321431
changed=true
@@ -1437,21 +1436,42 @@ check_updates_needed() {
14371436
fi
14381437
done < "$current_md5"
14391438

1439+
# Set update_needed based on changes
14401440
if [[ "$changed" == "true" ]]; then
14411441
update_needed=true
1442+
else
1443+
update_needed=false
14421444
fi
14431445
else
14441446
update_needed=true
14451447
log "No previous state found, update needed"
1448+
# Create initial state file
1449+
if ! cp "$current_md5" "$previous_md5"; then
1450+
log "ERROR: Failed to create initial state file"
1451+
return 1
1452+
fi
1453+
fi
1454+
1455+
# Force update if older than 24 hours
1456+
if [[ -f "$state_file" ]]; then
1457+
local last_update current_time
1458+
last_update=$(<"$state_file")
1459+
current_time=$(date +%s)
1460+
1461+
if (( current_time - last_update > 86400 )); then
1462+
log "Forced update due to time threshold"
1463+
update_needed=true
1464+
fi
14461465
fi
14471466

1467+
# Update MD5 checksums and state file if update is needed
14481468
if [[ "$update_needed" == "true" ]]; then
14491469
if ! cp "$current_md5" "$previous_md5"; then
14501470
log "ERROR: Failed to update MD5 checksums"
14511471
return 1
14521472
fi
14531473

1454-
if ! date +%s > "$state_file"; then
1474+
if ! echo "$(date +%s)" > "$state_file"; then
14551475
log "WARNING: Failed to save update state"
14561476
fi
14571477

@@ -1461,26 +1481,25 @@ check_updates_needed() {
14611481

14621482
log "Update state saved"
14631483
else
1464-
# Force update if older than 24 hours
1465-
if [[ -f "$state_file" ]]; then
1466-
local last_update current_time
1467-
last_update=$(cat "$state_file")
1468-
current_time=$(date +%s)
1469-
1470-
if (( current_time - last_update > 86400 )); then
1471-
log "Forced update due to time threshold"
1472-
update_needed=true
1473-
fi
1474-
fi
1484+
log "No updates needed based on MD5 comparison"
14751485
fi
14761486

14771487
# Cleanup but preserve MD5 files
14781488
if ! find "$temp_dir" -type f ! -name "*md5" -delete 2>/dev/null; then
14791489
log "WARNING: Failed to clean temporary files"
14801490
fi
14811491

1482-
return $(( update_needed == 1 ? 0 : 1 ))
1483-
}
1492+
# Return 0 if update is needed, 1 if not
1493+
if [[ "$update_needed" == "true" ]]; then
1494+
log "Updates needed"
1495+
return 0
1496+
else
1497+
log "No updates needed"
1498+
return 1
1499+
fi
1500+
1501+
}
1502+
14841503

14851504
# Helper function to restore backups
14861505
restore_backups() {
@@ -1536,6 +1555,8 @@ main() {
15361555
return 0
15371556
fi
15381557

1558+
log "Updates needed, proceeding with processing..."
1559+
15391560
# Clean temporary files
15401561
if ! cleanup; then
15411562
log "ERROR: Cleanup failed"

0 commit comments

Comments
 (0)