-
Notifications
You must be signed in to change notification settings - Fork 11
/
dnsCategorifyWebGuard
executable file
·71 lines (64 loc) · 2.56 KB
/
dnsCategorifyWebGuard
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
66
67
68
69
70
71
#!/bin/bash
url=$1
host=`echo $url | sed -E 's|https*://([^/^:]+).*+$|\1|g'`
domain=`echo $host | sed -E 's|^.*\.([a-zA-Z0-9]+\.[a-zA-Z]+)$|\1|g'`
proto=$4
srcIP=$2
dstIP=$3
dns=127.0.0.1
sPort=$5
dPort=$6
LOGFILE=/var/log/webfilter-ng
CONFIGDIR=/etc/webfilter-ng
CACHEDIR=/var/cache/webfilter-ng
REDISPASS=$(cat /etc/webfilter-ng/redis-password)
[ "$host" == "ENCRYPTED-SNI" ] && echo DROP && echo `date +%Y-%m-%d\ %H:%M:%S` $url $proto $srcIP:$sPort $dstIP:$dPort DROP >>$LOGFILE && exit 0
[ "$host" == "categorify.org" ] && echo ACCEPT && exit 0
if [[ $host =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]
then
echo ACCEPT
echo `date +%Y-%m-%d\ %H:%M:%S` $url $proto $srcIP:$sPort $dstIP:$dPort ACCEPT >>$LOGFILE
exit 0
fi
if [ -f $CONFIGDIR/whitelist ]
then
grep -v "^*" $CONFIGDIR/whitelist | grep "^$host$" &>/dev/null && echo ACCEPT && echo `date +%Y-%m-%d\ %H:%M:%S` $url $proto $srcIP:$sPort $dstIP:$dPort ACCEPT >>$LOGFILE && exit 0
grep "^*" $CONFIGDIR/whitelist | sed -e 's|^*\.||g;s|^*||g' | grep "^$domain$" &>/dev/null && echo ACCEPT && echo `date +%Y-%m-%d\ %H:%M:%S` $url $proto $srcIP:$sPort $dstIP:$dPort ACCEPT >>$LOGFILE && exit 0
fi
#categorify=$(redis-cli -a "$REDISPASS" get $host:categorify && redis-cli -a "$REDISPASS" expire $host:categorify 86400 &>/dev/null)
categorify=$(redis-cli -a "$REDISPASS" get $host:categorify)
if [ "$categorify" == "" ]
then
categ_json=$(env LC_NUMERIC="en_US.UTF-8" curl --max-time 0.5 http://127.0.0.1:3000/api?website=$host -H "Host: categorify.org" 2>/dev/null)
if [ $(jq -n --argjson data "$categ_json" '$data.rating.adult') == "true" ] || [ $(jq -n --argjson data "$categ_json" '$data.rating.nudity') == "true" ]
then
categorify=1
else
categorify=0
fi
redis-cli -a "$REDISPASS" set $host:categorify $categorify &>/dev/null
redis-cli -a "$REDISPASS" expire $host:categorify 86400 &>/dev/null
fi
#delete unused cache files that was not modified/used during last five minutes
find $CACHEDIR -cmin +5 -name "*.dns" -exec rm {} \;
if [ $categorify != "1" ]
then
ipList=`dig @${dns} +noall +answer $host | awk '($4=="A") {print $5}'`
if [ -f $CACHEDIR/$host.dns ]
then
ipList=`cat $CACHEDIR/$host.dns`" "$ipList
fi
echo $ipList | sed -e 's/ /\n/g' | sort | uniq >$CACHEDIR/$host.dns
for ip in $ipList
do
if [ "$ip" == "$dstIP" ]
then
echo ACCEPT
echo `date +%Y-%m-%d\ %H:%M:%S` $url $proto $srcIP:$sPort $dstIP:$dPort ACCEPT >>$LOGFILE
exit 0
fi
done
fi
echo DROP
echo `date +%Y-%m-%d\ %H:%M:%S` $url $proto $srcIP:$sPort $dstIP:$dPort DROP >>$LOGFILE
exit 0