From 9ea56243def66f26b23be4dcfaffcae6992f2312 Mon Sep 17 00:00:00 2001
From: Amir Zarrinkafsh <nightah@me.com>
Date: Sun, 12 Sep 2021 10:15:11 +1000
Subject: [PATCH] Simplify Dnsmasq script

[Dnsmasq version >= 2.86](https://thekelleys.org.uk/dnsmasq/CHANGELOG) supports passing multiple IP addresses via the `address=` syntax now.

>Major rewrite of the DNS server and domain handling code. The change makes multiple addresses associated with a domain work address=/example.com/1.2.3.4 address=/example.com/5.6.7.8.

This allows us to simplify the script and the `.hosts` file workaround is no longer necessary.
---
 scripts/README.md         |  9 +------
 scripts/create-dnsmasq.sh | 53 +++++++++++++--------------------------
 2 files changed, 18 insertions(+), 44 deletions(-)

diff --git a/scripts/README.md b/scripts/README.md
index 96fcc19..35d2ed9 100755
--- a/scripts/README.md
+++ b/scripts/README.md
@@ -39,11 +39,4 @@ configuration which can be utilised with:
 
 **This also applies to users utilising the script alongside Pi-hole.**
 
-If utilising the `create-dnsmasq.sh` the generation script will create a `lancache.conf` which also loads in the respective `*.hosts` files.
-
-The `lancache.conf` should be copied into the `/etc/dnsmasq.d/` location but also will need to be modified to point to the respective location of the `*.hosts` files.
-
-You can copy the `*.hosts` file to any location other than `/etc/dnsmasq.d/` as this location is utilised only for `*.conf` files.
-
-For example if utilising Pi-hole a user can copy the `*.hosts` files to `/etc/pihole/` and modify the `lancache.conf` with the following command, prior to copying it to `/etc/dnsmasq.d/`:
-`sed -i 's/dnsmasq\/hosts/pihole/g' output/dnsmasq/lancache.conf`
\ No newline at end of file
+Multi-IP Lancache setups are only supported with Dnsmasq or Pi-hole versions >= 2.86 or 2021.09 respectively.
\ No newline at end of file
diff --git a/scripts/create-dnsmasq.sh b/scripts/create-dnsmasq.sh
index 334f690..756cf2a 100755
--- a/scripts/create-dnsmasq.sh
+++ b/scripts/create-dnsmasq.sh
@@ -17,17 +17,16 @@ cachenamedefault="disabled"
 
 while read -r line; do
         ip=$(jq ".ips[\"${line}\"]" config.json)
-        declare "cacheip$line"="$ip"
+        declare "cacheip${line}"="${ip}"
 done <<< $(jq -r '.ips | to_entries[] | .key' config.json)
 
 while read -r line; do
         name=$(jq -r ".cache_domains[\"${line}\"]" config.json)
-        declare "cachename$line"="$name"
+        declare "cachename${line}"="${name}"
 done <<< $(jq -r '.cache_domains | to_entries[] | .key' config.json)
 
 rm -rf ${outputdir}
-mkdir -p ${outputdir}/hosts
-touch ${outputdir}/lancache.conf
+mkdir -p ${outputdir}
 while read -r entry; do
         unset cacheip
         unset cachename
@@ -43,47 +42,29 @@ while read -r entry; do
         cacheip=$(jq -r 'if type == "array" then .[] else . end' <<< ${!cacheipname} | xargs)
         while read -r fileid; do
                 while read -r filename; do
-                        destfilename=$(echo $filename | sed -e 's/txt/hosts/')
-                        lancacheconf=${outputdir}/lancache.conf
-                        outputfile=${outputdir}/hosts/${destfilename}
-                        echo "addn-hosts=/etc/dnsmasq/hosts/${destfilename}" >> ${lancacheconf}
-                        touch "$outputfile"
-                        # Wildcard entries
-                        while read -r fileentry; do
-                                # Ignore comments and non-wildcards
-                                if [[ $fileentry == \#* ]] || [[ ! $fileentry =~ ^\*\. ]]; then
-                                        continue
-                                fi
-                                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 | sort);
-                        # All other entries
+                        destfilename=$(echo $filename | sed -e 's/txt/conf/')
+                        outputfile=${outputdir}/${destfilename}
+                        touch ${outputfile}
                         while read -r fileentry; do
                                 # Ignore comments, newlines and wildcards
-                                if [[ $fileentry =~ ^(\#|\*\.) ]] || [[ -z $fileentry ]]; then
-                                        continue
-                                fi
-                                parsed=$(echo $fileentry)
-                                if grep -qx "$parsed" "$outputfile"; then
+                                if [[ ${fileentry} == \#* ]] || [[ -z ${fileentry} ]]; then
                                         continue
                                 fi
+                                parsed=$(echo ${fileentry} | sed -e "s/^\*\.//")
                                 for i in ${cacheip}; do
-                                        echo "${i} ${parsed}" >> "$outputfile"
+                                        if grep -qx "address=/${parsed}/${i}" "${outputfile}"; then
+                                                continue
+                                        fi
+                                        echo "address=/${parsed}/${i}" >> "${outputfile}"
                                 done
-                        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)
+                        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})
 
 cat << EOF
 Configuration generation completed.
 
 Please copy the following files:
-- ./${outputdir}/lancache.conf to /etc/dnsmasq/dnsmasq.d/
-- ./${outputdir}/hosts to /etc/dnsmasq/
+- ./${outputdir}/*.conf to /etc/dnsmasq/dnsmasq.d/
 EOF