From 74cab757af50b01cbdab36572f0ef97ce1828fb7 Mon Sep 17 00:00:00 2001
From: Amir Zarrinkafsh <nightah@me.com>
Date: Thu, 29 Oct 2020 23:05:45 +1100
Subject: [PATCH] Fix generation scripts

This change leverages #130 and also applies this to the dnsmasq script.

As it currently stands both generation scripts (unbound and dnsmasq) have a condition where a domain will be skipped if it fuzzy matches a domain already parsed that is higher in the CDN domain list.

For example the latter of the below two samples would never be added.
https://github.com/uklans/cache-domains/blob/8793ce15315cac1e594f7602158c2e82f510bc91/steam.txt#L20 https://github.com/uklans/cache-domains/blob/8793ce15315cac1e594f7602158c2e82f510bc91/steam.txt#L29

I've also taken the liberty to sort the output of said scripts for readability and troubleshooting purposes.

Closes #130.
---
 scripts/create-dnsmasq.sh | 20 ++++++++++----------
 scripts/create-unbound.sh |  4 ++--
 2 files changed, 12 insertions(+), 12 deletions(-)

diff --git a/scripts/create-dnsmasq.sh b/scripts/create-dnsmasq.sh
index 559fd59..abe94b0 100755
--- a/scripts/create-dnsmasq.sh
+++ b/scripts/create-dnsmasq.sh
@@ -50,32 +50,32 @@ while read -r entry; do
                         touch "$outputfile"
                         # Wildcard entries
                         while read -r fileentry; do
-                                # Ignore comments
-                                if [[ $fileentry == \#* ]]; then
+                                # Ignore comments and non-wildcards
+                                if [[ $fileentry == \#* ]] || [[ ! $fileentry =~ ^\*\. ]]; then
                                         continue
                                 fi
-                                wildcard=$(echo $fileentry | grep "*." | sed -e "s/^\*\.//")
-                                if grep -q "$wildcard" "$lancacheconf"; then
+                                wildcard=$(echo $fileentry | sed -e "s/^\*\.//")
+                                if grep -qx "$wildcard" "$lancacheconf"; then
                                         continue
                                 fi
                                 for i in ${cacheip}; do
                                         echo "address=/${wildcard}/${i}" >> "$lancacheconf"
                                 done
-                        done <<< $(cat ${basedir}/$filename);
+                        done <<< $(cat ${basedir}/$filename | sort);
                         # All other entries
                         while read -r fileentry; do
-                                # Ignore comments
-                                if [[ $fileentry == \#* ]]; then
+                                # Ignore comments and wildcards
+                                if [[ $fileentry =~ ^(\#|\*\.) ]]; then
                                         continue
                                 fi
-                                parsed=$(echo $fileentry | sed -e "s/^\*\.//")
-                                if grep -q "$parsed" "$outputfile"; then
+                                parsed=$(echo $fileentry)
+                                if grep -qx "$parsed" "$outputfile"; then
                                         continue
                                 fi
                                 for i in ${cacheip}; do
                                         echo "${i} ${parsed}" >> "$outputfile"
                                 done
-                        done <<< $(cat ${basedir}/$filename);
+                        done <<< $(cat ${basedir}/$filename | sort);
                 done <<< $(jq -r ".cache_domains[$entry].domain_files[$fileid]" $path)
         done <<< $(jq -r ".cache_domains[$entry].domain_files | to_entries[] | .key" $path)
 done <<< $(jq -r '.cache_domains | to_entries[] | .key' $path)
diff --git a/scripts/create-unbound.sh b/scripts/create-unbound.sh
index 10ee026..1364a47 100755
--- a/scripts/create-unbound.sh
+++ b/scripts/create-unbound.sh
@@ -52,14 +52,14 @@ while read entry; do
 					continue
 				fi
 				parsed=$(echo $fileentry | sed -e "s/^\*\.//")
-				if grep -q "$parsed" $outputfile; then
+				if grep -qx "$parsed" $outputfile; then
 					continue
 				fi
 				echo "  local-zone: \"${parsed}\" redirect" >> $outputfile
 				for i in ${cacheip}; do
 					echo "  local-data: \"${parsed} 30 IN A ${i}\"" >> $outputfile
 				done
-			done <<< $(cat ${basedir}/$filename);
+			done <<< $(cat ${basedir}/$filename | sort);
 		done <<< $(jq -r ".cache_domains[$entry].domain_files[$fileid]" $path)
 	done <<< $(jq -r ".cache_domains[$entry].domain_files | to_entries[] | .key" $path)
 done <<< $(jq -r '.cache_domains | to_entries[] | .key' $path)