@@ -79,9 +79,6 @@ export DNS_RATE_LIMIT
79
79
export LOG_FILE
80
80
export DNS_MAX_RETRIES
81
81
82
- # Global variables for statistics
83
- # declare -i TOTAL_DOMAINS=0
84
-
85
82
# Enable debugging
86
83
# exec 2>"${WORK_DIR}/debug.log"
87
84
@@ -283,13 +280,11 @@ cleanup() {
283
280
284
281
cleanup_invalid_cache () {
285
282
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
293
288
}
294
289
295
290
log_cache_stats () {
@@ -356,7 +351,8 @@ save_state() {
356
351
fi
357
352
358
353
# 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
360
356
log " ERROR: Failed to calculate MD5 for main list"
361
357
return 1
362
358
fi
@@ -585,7 +581,8 @@ export -f validate_domain
585
581
extract_domains () {
586
582
local input=$1
587
583
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"
589
586
590
587
log " Extracting domains from: $input "
591
588
@@ -677,7 +674,8 @@ extract_domains() {
677
674
initial_filter () {
678
675
local input=$1
679
676
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"
681
679
682
680
log " Initial filtering of: $input "
683
681
@@ -706,7 +704,6 @@ initial_filter() {
706
704
grep -v ' ^#' | \
707
705
grep -v ' ^$' | \
708
706
tr ' [:upper:]' ' [:lower:]' | \
709
- tr -d ' ' | \
710
707
awk ' length <= 253' > " $temp_output " ; then
711
708
log " ERROR: Domain filtering failed"
712
709
rm -f " $temp_output "
@@ -960,8 +957,10 @@ apply_whitelist() {
960
957
local input=$1
961
958
local whitelist=$2
962
959
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"
965
964
966
965
log " Applying whitelist to: $input "
967
966
@@ -1410,7 +1409,7 @@ check_updates_needed() {
1410
1409
1411
1410
if [[ " $process_failed " == " true" ]]; then
1412
1411
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 || :
1414
1413
return 1
1415
1414
fi
1416
1415
@@ -1419,14 +1418,14 @@ check_updates_needed() {
1419
1418
local changed=false
1420
1419
while IFS=' ' read -r source md5; do
1421
1420
local prev_line
1422
- prev_line=$( grep " ^${source} " " $previous_md5 " || echo " " )
1421
+ prev_line=$( grep " ^${source} [[:space:]] " " $previous_md5 " || echo " " )
1423
1422
1424
1423
if [[ -z " $prev_line " ]]; then
1425
1424
changed=true
1426
- log " Content changed for source: $source "
1425
+ log " Content changed for source: $source (no previous entry) "
1427
1426
else
1428
1427
local prev_md5
1429
- prev_md5=$( echo " $prev_line " | cut -d ' ' -f2 )
1428
+ prev_md5=$( echo " $prev_line " | awk ' {print $2} ' )
1430
1429
1431
1430
if [[ " $md5 " != " $prev_md5 " ]]; then
1432
1431
changed=true
@@ -1437,21 +1436,42 @@ check_updates_needed() {
1437
1436
fi
1438
1437
done < " $current_md5 "
1439
1438
1439
+ # Set update_needed based on changes
1440
1440
if [[ " $changed " == " true" ]]; then
1441
1441
update_needed=true
1442
+ else
1443
+ update_needed=false
1442
1444
fi
1443
1445
else
1444
1446
update_needed=true
1445
1447
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
1446
1465
fi
1447
1466
1467
+ # Update MD5 checksums and state file if update is needed
1448
1468
if [[ " $update_needed " == " true" ]]; then
1449
1469
if ! cp " $current_md5 " " $previous_md5 " ; then
1450
1470
log " ERROR: Failed to update MD5 checksums"
1451
1471
return 1
1452
1472
fi
1453
1473
1454
- if ! date +%s > " $state_file " ; then
1474
+ if ! echo " $( date +%s) " > " $state_file " ; then
1455
1475
log " WARNING: Failed to save update state"
1456
1476
fi
1457
1477
@@ -1461,26 +1481,25 @@ check_updates_needed() {
1461
1481
1462
1482
log " Update state saved"
1463
1483
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"
1475
1485
fi
1476
1486
1477
1487
# Cleanup but preserve MD5 files
1478
1488
if ! find " $temp_dir " -type f ! -name " *md5" -delete 2> /dev/null; then
1479
1489
log " WARNING: Failed to clean temporary files"
1480
1490
fi
1481
1491
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
+
1484
1503
1485
1504
# Helper function to restore backups
1486
1505
restore_backups () {
@@ -1536,6 +1555,8 @@ main() {
1536
1555
return 0
1537
1556
fi
1538
1557
1558
+ log " Updates needed, proceeding with processing..."
1559
+
1539
1560
# Clean temporary files
1540
1561
if ! cleanup; then
1541
1562
log " ERROR: Cleanup failed"
0 commit comments