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)
|
2019-01-29 04:05:28 +00:00
|
|
|
declare "cacheip$line"="$ip"
|
|
|
|
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)
|
|
|
|
declare "cachename$line"="$name"
|
|
|
|
done <<< $(jq -r '.cache_domains | to_entries[] | .key' config.json)
|
|
|
|
|
|
|
|
rm -rf ${outputdir}
|
2021-01-15 08:01:02 +00:00
|
|
|
mkdir -p ${outputdir}/hosts
|
2020-06-24 23:15:42 +00:00
|
|
|
touch ${outputdir}/lancache.conf
|
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
|
2020-06-06 10:32:24 +00:00
|
|
|
destfilename=$(echo $filename | sed -e 's/txt/hosts/')
|
2020-10-22 08:56:07 +00:00
|
|
|
lancacheconf=${outputdir}/lancache.conf
|
2021-01-15 08:01:02 +00:00
|
|
|
outputfile=${outputdir}/hosts/${destfilename}
|
|
|
|
echo "addn-hosts=/etc/dnsmasq/hosts/${destfilename}" >> ${lancacheconf}
|
2019-10-11 19:56:40 +00:00
|
|
|
touch "$outputfile"
|
2020-10-22 08:56:07 +00:00
|
|
|
# Wildcard entries
|
|
|
|
while read -r fileentry; do
|
2020-10-29 12:05:45 +00:00
|
|
|
# Ignore comments and non-wildcards
|
|
|
|
if [[ $fileentry == \#* ]] || [[ ! $fileentry =~ ^\*\. ]]; then
|
2020-10-22 08:56:07 +00:00
|
|
|
continue
|
|
|
|
fi
|
2020-10-29 12:05:45 +00:00
|
|
|
wildcard=$(echo $fileentry | sed -e "s/^\*\.//")
|
|
|
|
if grep -qx "$wildcard" "$lancacheconf"; then
|
2020-10-22 08:56:07 +00:00
|
|
|
continue
|
|
|
|
fi
|
|
|
|
for i in ${cacheip}; do
|
|
|
|
echo "address=/${wildcard}/${i}" >> "$lancacheconf"
|
|
|
|
done
|
2020-10-29 12:05:45 +00:00
|
|
|
done <<< $(cat ${basedir}/$filename | sort);
|
2020-10-22 08:56:07 +00:00
|
|
|
# All other entries
|
2019-10-11 19:56:40 +00:00
|
|
|
while read -r fileentry; do
|
2020-10-29 12:05:45 +00:00
|
|
|
# Ignore comments and wildcards
|
|
|
|
if [[ $fileentry =~ ^(\#|\*\.) ]]; then
|
2019-01-29 04:05:28 +00:00
|
|
|
continue
|
|
|
|
fi
|
2020-10-29 12:05:45 +00:00
|
|
|
parsed=$(echo $fileentry)
|
|
|
|
if grep -qx "$parsed" "$outputfile"; then
|
2019-01-29 04:05:28 +00:00
|
|
|
continue
|
|
|
|
fi
|
2019-06-15 21:27:16 +00:00
|
|
|
for i in ${cacheip}; do
|
2020-06-06 10:32:24 +00:00
|
|
|
echo "${i} ${parsed}" >> "$outputfile"
|
2019-06-15 21:27:16 +00:00
|
|
|
done
|
2020-10-29 12:05:45 +00:00
|
|
|
done <<< $(cat ${basedir}/$filename | sort);
|
2019-01-29 04:05:28 +00:00
|
|
|
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:
|
|
|
|
- ./${outputdir}/lancache.conf to /etc/dnsmasq/dnsmasq.d/
|
|
|
|
- ./${outputdir}/hosts to /etc/dnsmasq/
|
|
|
|
EOF
|