Skip to content

Commit fa937dd

Browse files
authored
Merge pull request uklans#178 from uklans/squid
Add script to create the acl files for squid
2 parents 0cf9233 + 6730915 commit fa937dd

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

scripts/create-squid.sh

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/bin/bash
2+
basedir=".."
3+
outputdir="output/squid"
4+
path="${basedir}/cache_domains.json"
5+
REGEX="^\\*\\.(.*)$"
6+
7+
export IFS=' '
8+
9+
test=$(which jq);
10+
out=$?
11+
if [ $out -gt 0 ] ; then
12+
echo "This script requires jq to be installed."
13+
echo "Your package manager should be able to find it"
14+
exit 1
15+
fi
16+
17+
cachenamedefault="disabled"
18+
19+
while read -r line; do
20+
name=$(jq -r ".cache_domains[\"${line}\"]" config.json)
21+
declare "cachename${line}"="${name}"
22+
done <<< $(jq -r '.cache_domains | to_entries[] | .key' config.json)
23+
24+
rm -rf ${outputdir}
25+
mkdir -p ${outputdir}
26+
while read -r entry; do
27+
unset cachename
28+
key=$(jq -r ".cache_domains[$entry].name" $path)
29+
cachename="cachename${key}"
30+
if [ -z "${!cachename}" ]; then
31+
cachename="cachenamedefault"
32+
fi
33+
if [[ ${!cachename} == "disabled" ]]; then
34+
continue;
35+
fi
36+
while read -r fileid; do
37+
while read -r filename; do
38+
destfilename=$(echo ${!cachename}.txt)
39+
outputfile=${outputdir}/${destfilename}
40+
touch ${outputfile}
41+
while read -r fileentry; do
42+
# Ignore comments
43+
if [[ ${fileentry} == \#* ]] || [[ -z ${fileentry} ]]; then
44+
continue
45+
fi
46+
# Handle wildcards to squid wildcards
47+
parsed=$(echo ${fileentry} | sed -e "s/^\*\./\./")
48+
# If we have cdn.thing and *.cdn.thing in cache_domains
49+
# Squid requires ONLY cdn.thing
50+
if [[ ${fileentry} =~ $REGEX ]]; then
51+
grep "${BASH_REMATCH[1]}" ${basedir}/${filename} | grep -v "${fileentry}" > /dev/null
52+
if [[ $? -eq 0 ]]; then
53+
continue
54+
fi
55+
fi
56+
57+
echo "${parsed}" >> "${outputfile}"
58+
done <<< $(cat ${basedir}/${filename} | sort);
59+
done <<< $(jq -r ".cache_domains[${entry}].domain_files[$fileid]" ${path})
60+
done <<< $(jq -r ".cache_domains[${entry}].domain_files | to_entries[] | .key" ${path})
61+
done <<< $(jq -r '.cache_domains | to_entries[] | .key' ${path})
62+
63+
cat << EOF
64+
Configuration generation completed.
65+
66+
Please copy the following files:
67+
- ./${outputdir}/*.txt to /etc/squid/domains/
68+
EOF

0 commit comments

Comments
 (0)