-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathblock_referer_varnish.sh
executable file
·65 lines (58 loc) · 1.81 KB
/
block_referer_varnish.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/bin/sh
VARNISH_DIR="/opt/varnish"
BLACKLIST=$VARNISH_DIR"/blacklist"
VARNISH_CONF=$VARNISH_DIR"/etc/default.vcl"
REASON="no reason"
TMP="/tmp/.varnish.tmp"
reload(){
if [ -s $BLACKLIST ];then
LINE=""
STRING=""
sed -r -i "/^$/d" $BLACKLIST
while read bad;do
mark=`echo "$bad"|awk '{print $1}'`
bad=`echo "$bad"|awk '{print $2}'`
case $mark in
ref)
LINE="\t\t|| req.http.referer ~ \"$bad\" \t\t# blacklist\n";;
ip)
LINE="\t\t|| client.ip == \"$bad\" \t\t# blacklist\n";;
agt)
LINE="\t\t|| req.http.user-agent ~ \"$bad\" \t# blacklist\n";;
url)
LINE="\t\t|| req.url ~ \"$bad\" \t\t\t# blacklist\n";;
esac
STRING=$STRING$LINE
done < $BLACKLIST
STRING="\tif ( req.http.user-agent ~ \"^$\" \t\t# blacklist\n$STRING\t) {\t\t\t\t\t\t# blacklist\n\t\terror 403 \"Not Allowed.\";\t\t# blacklist\n\t}\t\t\t\t\t\t# blacklist\n"
echo -en $STRING > $TMP
sed -r -i "/blacklist/d" $VARNISH_CONF
sed -r -i "/vcl_recv/r $TMP" $VARNISH_CONF
else
echo "blacklist is empty or not exist."
sed -r -i "/blacklist/d" $VARNISH_CONF
fi
rm -rf $TMP
exit 0
}
case $1 in
ref)
STRING="ref\t$2";;
ip)
STRING="ip\t$2";;
agt)
STRING="agt\t$2";;
url)
STRING="url\t$2";;
reload)
reload;;
*)
echo "input ref,ip,agt,url,reload"
exit 0;;
esac
[ -z "$2" ] && (echo "Usage: $0 (ref|ip|agt|url|reload) param some-reson") && exit 0
[ ! -z "$3" ] && REASON=$2
[ -s $BLACKLIST ] || touch $BLACKLIST
sed -r -i "/$2/d" $BLACKLIST
echo -e "$STRING\t#`date`\t\"$REASON\"\n" >> $BLACKLIST
reload