-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathnginx.sh
executable file
·36 lines (30 loc) · 1.07 KB
/
nginx.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/env sh
LOC="$(dirname ${0})"
. "${LOC}"/common.sh
LIST='/etc/nginx/conf.d/govblock.conf'
BLOCKER='/etc/nginx/block_gov'
REDIRECT_URL="${REDIRECT_URL:-http://pastebin.com/raw.php?i=9pabJfqB}"
SITES=/etc/nginx/sites-available/*
# create config file with rules
LIST_PARENT=`dirname ${LIST}`
mkdir -p "${LIST_PARENT}"
echo '# WARNING! This file was generated. Do not change!' > "${LIST}"
echo 'geo $gov_user {' >> "${LIST}"
echo 'default 0;' >> "${LIST}"
getblacklist_all | while read net; do
echo "${net} 1;" >> "${LIST}"
done
echo '}' >> "${LIST}"
# create config file which does actual blocking
# visitors are redirected to $REDIRECT_URL
echo '# WARNING! This file was generated. Do not change!' > "${BLOCKER}"
echo 'if ($gov_user = 1) {' >> "${BLOCKER}"
echo "rewrite ^ ${REDIRECT_URL};" >> "${BLOCKER}"
echo '}' >> "${BLOCKER}"
# include blocker configuration file from all sites
# finds only '^server {' server declarations
for site in $SITES; do
if (! grep -q "${BLOCKER}" "${site}" ); then
sed "/^server {/ainclude ${BLOCKER};" -i "${site}";
fi
done