Skip to content

Commit 63d91f0

Browse files
author
SMKRV
committed
refactor: Enhanced and upgraded filtration mechanisms
1 parent 8dfe57b commit 63d91f0

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

scripts/mikrotik-domain-filter-bash.sh

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,6 +1057,7 @@ check_intersections() {
10571057
local main_list=$1
10581058
local special_list=$2
10591059
local temp_intersect="${TMP_DIR}/intersections.tmp"
1060+
local temp_main="${TMP_DIR}/main_without_duplicates.tmp"
10601061

10611062
log "Checking intersections between lists..."
10621063

@@ -1070,31 +1071,43 @@ check_intersections() {
10701071
return 1
10711072
fi
10721073

1073-
# Create temporary file for intersections
1074-
if ! : > "$temp_intersect"; then
1075-
log "ERROR: Failed to create temporary file for intersections"
1074+
# Create temporary files
1075+
if ! : > "$temp_intersect" || ! : > "$temp_main"; then
1076+
log "ERROR: Failed to create temporary files"
10761077
return 1
10771078
fi
10781079

10791080
# Find intersections using comm
10801081
if ! comm -12 <(sort "$main_list") <(sort "$special_list") > "$temp_intersect"; then
10811082
log "ERROR: Failed to check intersections"
1082-
rm -f "$temp_intersect"
1083+
rm -f "$temp_intersect" "$temp_main"
10831084
return 1
10841085
fi
10851086

1086-
# Check if we found any intersections
1087+
# If intersections found, remove them from main list
10871088
if [[ -s "$temp_intersect" ]]; then
1088-
log "WARNING: Found intersections between lists:"
1089+
log "Found intersecting domains - moving to special list:"
10891090
while IFS= read -r domain; do
1090-
log "Duplicate domain: $domain"
1091+
log "Moving domain to special list: $domain"
10911092
done < "$temp_intersect"
1092-
rm -f "$temp_intersect"
1093-
return 1
1093+
1094+
# Remove intersecting domains from main list
1095+
if ! grep -vFf "$temp_intersect" "$main_list" > "$temp_main"; then
1096+
log "ERROR: Failed to remove intersecting domains from main list"
1097+
rm -f "$temp_intersect" "$temp_main"
1098+
return 1
1099+
fi
1100+
1101+
# Update main list
1102+
if ! mv "$temp_main" "$main_list"; then
1103+
log "ERROR: Failed to update main list"
1104+
rm -f "$temp_intersect" "$temp_main"
1105+
return 1
1106+
fi
10941107
fi
10951108

1096-
rm -f "$temp_intersect"
1097-
log "No intersections found between lists"
1109+
rm -f "$temp_intersect" "$temp_main"
1110+
log "Intersection check completed - duplicates moved to special list"
10981111
return 0
10991112
}
11001113

@@ -1675,9 +1688,9 @@ main() {
16751688
fi
16761689
fi
16771690

1678-
# Check for intersections
1691+
# Check for intersections and move duplicates to special list
16791692
if ! check_intersections "${TMP_DIR}/main_filtered.txt" "${TMP_DIR}/special_filtered.txt"; then
1680-
log "ERROR: Intersections found between lists"
1693+
log "ERROR: Failed to process list intersections"
16811694
restore_backups
16821695
release_lock
16831696
return 1

0 commit comments

Comments
 (0)