Skip to content

Commit cfcdf1b

Browse files
authored
Merge pull request uklans#174 from nightah/update-dnsmasq-roundrobin
Simplify Dnsmasq script
2 parents 50e5d28 + 9ea5624 commit cfcdf1b

File tree

2 files changed

+18
-44
lines changed

2 files changed

+18
-44
lines changed

scripts/README.md

+1-8
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,4 @@ configuration which can be utilised with:
3939

4040
**This also applies to users utilising the script alongside Pi-hole.**
4141

42-
If utilising the `create-dnsmasq.sh` the generation script will create a `lancache.conf` which also loads in the respective `*.hosts` files.
43-
44-
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.
45-
46-
You can copy the `*.hosts` file to any location other than `/etc/dnsmasq.d/` as this location is utilised only for `*.conf` files.
47-
48-
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/`:
49-
`sed -i 's/dnsmasq\/hosts/pihole/g' output/dnsmasq/lancache.conf`
42+
Multi-IP Lancache setups are only supported with Dnsmasq or Pi-hole versions >= 2.86 or 2021.09 respectively.

scripts/create-dnsmasq.sh

+17-36
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,16 @@ cachenamedefault="disabled"
1717

1818
while read -r line; do
1919
ip=$(jq ".ips[\"${line}\"]" config.json)
20-
declare "cacheip$line"="$ip"
20+
declare "cacheip${line}"="${ip}"
2121
done <<< $(jq -r '.ips | to_entries[] | .key' config.json)
2222

2323
while read -r line; do
2424
name=$(jq -r ".cache_domains[\"${line}\"]" config.json)
25-
declare "cachename$line"="$name"
25+
declare "cachename${line}"="${name}"
2626
done <<< $(jq -r '.cache_domains | to_entries[] | .key' config.json)
2727

2828
rm -rf ${outputdir}
29-
mkdir -p ${outputdir}/hosts
30-
touch ${outputdir}/lancache.conf
29+
mkdir -p ${outputdir}
3130
while read -r entry; do
3231
unset cacheip
3332
unset cachename
@@ -43,47 +42,29 @@ while read -r entry; do
4342
cacheip=$(jq -r 'if type == "array" then .[] else . end' <<< ${!cacheipname} | xargs)
4443
while read -r fileid; do
4544
while read -r filename; do
46-
destfilename=$(echo $filename | sed -e 's/txt/hosts/')
47-
lancacheconf=${outputdir}/lancache.conf
48-
outputfile=${outputdir}/hosts/${destfilename}
49-
echo "addn-hosts=/etc/dnsmasq/hosts/${destfilename}" >> ${lancacheconf}
50-
touch "$outputfile"
51-
# Wildcard entries
52-
while read -r fileentry; do
53-
# Ignore comments and non-wildcards
54-
if [[ $fileentry == \#* ]] || [[ ! $fileentry =~ ^\*\. ]]; then
55-
continue
56-
fi
57-
wildcard=$(echo $fileentry | sed -e "s/^\*\.//")
58-
if grep -qx "$wildcard" "$lancacheconf"; then
59-
continue
60-
fi
61-
for i in ${cacheip}; do
62-
echo "address=/${wildcard}/${i}" >> "$lancacheconf"
63-
done
64-
done <<< $(cat ${basedir}/$filename | sort);
65-
# All other entries
45+
destfilename=$(echo $filename | sed -e 's/txt/conf/')
46+
outputfile=${outputdir}/${destfilename}
47+
touch ${outputfile}
6648
while read -r fileentry; do
6749
# Ignore comments, newlines and wildcards
68-
if [[ $fileentry =~ ^(\#|\*\.) ]] || [[ -z $fileentry ]]; then
69-
continue
70-
fi
71-
parsed=$(echo $fileentry)
72-
if grep -qx "$parsed" "$outputfile"; then
50+
if [[ ${fileentry} == \#* ]] || [[ -z ${fileentry} ]]; then
7351
continue
7452
fi
53+
parsed=$(echo ${fileentry} | sed -e "s/^\*\.//")
7554
for i in ${cacheip}; do
76-
echo "${i} ${parsed}" >> "$outputfile"
55+
if grep -qx "address=/${parsed}/${i}" "${outputfile}"; then
56+
continue
57+
fi
58+
echo "address=/${parsed}/${i}" >> "${outputfile}"
7759
done
78-
done <<< $(cat ${basedir}/$filename | sort);
79-
done <<< $(jq -r ".cache_domains[$entry].domain_files[$fileid]" $path)
80-
done <<< $(jq -r ".cache_domains[$entry].domain_files | to_entries[] | .key" $path)
81-
done <<< $(jq -r '.cache_domains | to_entries[] | .key' $path)
60+
done <<< $(cat ${basedir}/${filename} | sort);
61+
done <<< $(jq -r ".cache_domains[${entry}].domain_files[$fileid]" ${path})
62+
done <<< $(jq -r ".cache_domains[${entry}].domain_files | to_entries[] | .key" ${path})
63+
done <<< $(jq -r '.cache_domains | to_entries[] | .key' ${path})
8264

8365
cat << EOF
8466
Configuration generation completed.
8567
8668
Please copy the following files:
87-
- ./${outputdir}/lancache.conf to /etc/dnsmasq/dnsmasq.d/
88-
- ./${outputdir}/hosts to /etc/dnsmasq/
69+
- ./${outputdir}/*.conf to /etc/dnsmasq/dnsmasq.d/
8970
EOF

0 commit comments

Comments
 (0)