cache-domains/scripts/create-dnsmasq.sh
Gerard Ryan 70437ee7e7 Revert Dnsmasq script breaking change
In https://github.com/uklans/cache-domains/pull/174 the output changed
from a single `lancache.conf` to many variously named `.conf` files.
Previously, It was only necessary to configure Dnsmasq with a single
`lancache.conf` as the (also generated) `.hosts` files were referenced
within. Since said pull request it is now necessary to configure Dnsmasq
with N `.conf` files.

This commit reverts this to only needing a single `lancache.conf` file.

I also can't see anything that the multiple `.conf` files afford us.
I could see it providing an additional way of selectively disabling the
caching of domain groups. But I'd argue that using the,
`"cache_domains": { "steam": "disabled" }` way, or not having a
`default` entry is still a better way. And was previously the only way
for disabling wildcard domain groups.

Signed-off-by: Gerard Ryan <G.M0N3Y.2503@gmail.com>
2021-09-18 12:57:31 +10:00

69 lines
2.5 KiB
Bash
Executable File

#!/bin/bash
basedir=".."
outputdir="output/dnsmasq"
outputfile="${outputdir}/lancache.conf"
path="${basedir}/cache_domains.json"
export IFS=' '
test=$(which jq);
out=$?
if [ $out -gt 0 ] ; then
echo "This script requires jq to be installed."
echo "Your package manager should be able to find it"
exit 1
fi
cachenamedefault="disabled"
while read -r line; do
ip=$(jq ".ips[\"${line}\"]" config.json)
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}"
done <<< $(jq -r '.cache_domains | to_entries[] | .key' config.json)
rm -rf ${outputdir}
mkdir -p ${outputdir}
touch ${outputfile}
while read -r entry; do
unset cacheip
unset cachename
key=$(jq -r ".cache_domains[$entry].name" $path)
cachename="cachename${key}"
if [ -z "${!cachename}" ]; then
cachename="cachenamedefault"
fi
if [[ ${!cachename} == "disabled" ]]; then
continue;
fi
cacheipname="cacheip${!cachename}"
cacheip=$(jq -r 'if type == "array" then .[] else . end' <<< ${!cacheipname} | xargs)
while read -r fileid; do
while read -r filename; do
while read -r fileentry; do
# Ignore comments and newlines
if [[ ${fileentry} == \#* ]] || [[ -z ${fileentry} ]]; then
continue
fi
parsed=$(echo ${fileentry} | sed -e "s/^\*\.//")
for i in ${cacheip}; do
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})
cat << EOF
Configuration generation completed.
Please copy ./${outputfile} to /etc/dnsmasq/dnsmasq.d/
EOF