2019-01-29 04:05:28 +00:00
|
|
|
#!/bin/bash
|
|
|
|
basedir=".."
|
|
|
|
outputdir="output/dnsmasq"
|
|
|
|
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"
|
|
|
|
|
2019-10-11 19:56:40 +00:00
|
|
|
while read -r line; do
|
2019-06-15 21:27:16 +00:00
|
|
|
ip=$(jq ".ips[\"${line}\"]" config.json)
|
2021-09-12 00:15:11 +00:00
|
|
|
declare "cacheip${line}"="${ip}"
|
2019-01-29 04:05:28 +00:00
|
|
|
done <<< $(jq -r '.ips | to_entries[] | .key' config.json)
|
|
|
|
|
2019-10-11 19:56:40 +00:00
|
|
|
while read -r line; do
|
2019-01-29 04:05:28 +00:00
|
|
|
name=$(jq -r ".cache_domains[\"${line}\"]" config.json)
|
2021-09-12 00:15:11 +00:00
|
|
|
declare "cachename${line}"="${name}"
|
2019-01-29 04:05:28 +00:00
|
|
|
done <<< $(jq -r '.cache_domains | to_entries[] | .key' config.json)
|
|
|
|
|
|
|
|
rm -rf ${outputdir}
|
2021-09-12 00:15:11 +00:00
|
|
|
mkdir -p ${outputdir}
|
2019-10-11 19:56:40 +00:00
|
|
|
while read -r entry; do
|
2019-01-29 04:05:28 +00:00
|
|
|
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}"
|
2019-06-15 21:27:16 +00:00
|
|
|
cacheip=$(jq -r 'if type == "array" then .[] else . end' <<< ${!cacheipname} | xargs)
|
2019-10-11 19:56:40 +00:00
|
|
|
while read -r fileid; do
|
|
|
|
while read -r filename; do
|
2021-09-12 00:15:11 +00:00
|
|
|
destfilename=$(echo $filename | sed -e 's/txt/conf/')
|
|
|
|
outputfile=${outputdir}/${destfilename}
|
|
|
|
touch ${outputfile}
|
2019-10-11 19:56:40 +00:00
|
|
|
while read -r fileentry; do
|
2021-01-27 23:16:44 +00:00
|
|
|
# Ignore comments, newlines and wildcards
|
2021-09-12 00:15:11 +00:00
|
|
|
if [[ ${fileentry} == \#* ]] || [[ -z ${fileentry} ]]; then
|
2019-01-29 04:05:28 +00:00
|
|
|
continue
|
|
|
|
fi
|
2021-09-12 00:15:11 +00:00
|
|
|
parsed=$(echo ${fileentry} | sed -e "s/^\*\.//")
|
2019-06-15 21:27:16 +00:00
|
|
|
for i in ${cacheip}; do
|
2023-03-28 10:36:46 +00:00
|
|
|
if ! grep -qx "address=/${parsed}/${i}" "${outputfile}"; then
|
|
|
|
echo "address=/${parsed}/${i}" >> "${outputfile}"
|
|
|
|
fi
|
|
|
|
if ! grep -qx "local=/${parsed}/" "${outputfile}"; then
|
|
|
|
echo "local=/${parsed}/" >> "${outputfile}"
|
2021-09-12 00:15:11 +00:00
|
|
|
fi
|
2019-06-15 21:27:16 +00:00
|
|
|
done
|
2021-09-12 00:15:11 +00:00
|
|
|
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})
|
2021-01-15 09:09:32 +00:00
|
|
|
|
2021-01-15 10:43:21 +00:00
|
|
|
cat << EOF
|
|
|
|
Configuration generation completed.
|
|
|
|
|
|
|
|
Please copy the following files:
|
2021-09-12 00:15:11 +00:00
|
|
|
- ./${outputdir}/*.conf to /etc/dnsmasq/dnsmasq.d/
|
2021-01-15 10:43:21 +00:00
|
|
|
EOF
|