3 # Create or update the list of temporarily banned IPs.
6 BASEDIR="$( cd "$( dirname "$0" )" && cd .. && pwd )"
7 if [ "x$BASEDIR" == "x" ]; then
8 echo "Could not determine base dir."
12 BLOCKEDFILE=$BASEDIR/settings/ip_blocks
13 LOGFILE=$BASEDIR/log/ip_blocks.log
17 COOLOFF_PERIOD='1 hour'
22 SRCHSHORT_LIMIT='10 min'
24 PSQLCMD='psql -qtA -d nominatim'
26 curload=`cat /proc/loadavg | sed 's:[. ].*::'`
27 if [ "$curload" -gt "15" ]; then
28 REVSHORT_LIMIT=$((REVSHORT_LIMIT/2))
32 $PSQLCMD > $BLOCKEDFILE.newblocks << ENDOFQUERY
34 ((SELECT ipaddress FROM
35 (SELECT ipaddress, count(*) FROM new_query_log
36 WHERE type = 'reverse' AND starttime > now() - interval '$LONG_PERIOD'
39 WHERE count > $REVLONG_LIMIT)
41 (SELECT ipaddress FROM
42 (SELECT ipaddress, count(*) FROM new_query_log
43 WHERE type = 'reverse' AND starttime > now() - interval '$SHORT_PERIOD'
46 WHERE count > $REVSHORT_LIMIT)
48 (SELECT ipaddress FROM
49 (SELECT ipaddress, count(*) FROM new_query_log
50 WHERE type = 'search' AND starttime > now() - interval '$LONG_PERIOD'
53 WHERE count > $SRCHLONG_LIMIT)
55 (SELECT ipaddress FROM
56 (SELECT ipaddress, sum(endtime-starttime) as dur FROM new_query_log
57 WHERE type = 'search' AND starttime > now() - interval '$SHORT_PERIOD'
60 WHERE dur > '$SRCHSHORT_LIMIT')
61 ) as q ORDER BY ipaddress;
64 no_newblocks=`comm $BLOCKEDFILE.newblocks $BLOCKEDFILE -23 | wc -l`
66 if [ "x$no_newblocks" != "x0" ]; then
67 date +"%x %X Newly blocked IPs: `comm $BLOCKEDFILE.newblocks $BLOCKEDFILE -23 | tr '\n' ' '`" >> $LOGFILE
71 # Deblockable candidates
72 blocked=`tr '\n' ',' < $BLOCKEDFILE | sed "s:[[:space:]]::g;s:,$::;s:,:'),(':g"`
74 if [ "x$blocked" == "x" ]; then
75 mv $BLOCKEDFILE.newblocks $BLOCKEDFILE
77 $PSQLCMD > $BLOCKEDFILE.newlifted << ENDOFQUERY
81 (SELECT DISTINCT ipaddress FROM new_query_log
82 WHERE starttime > now() - interval '$COOLOFF_PERIOD')
83 ) as q ORDER BY column1;
86 no_lifted=`cat $BLOCKEDFILE.newlifted | wc -w`
88 if [ "x$no_lifted" != "x0" ]; then
89 date +"%x %X Bans lifted: `tr '\n' ' ' < $BLOCKEDFILE.newlifted`" >> $LOGFILE
92 # Write out new blocks
93 cat $BLOCKEDFILE.newblocks $BLOCKEDFILE | sort -u | comm - $BLOCKEDFILE.newlifted -23 > $BLOCKEDFILE.new
94 mv $BLOCKEDFILE.new $BLOCKEDFILE
96 rm $BLOCKEDFILE.newblocks $BLOCKEDFILE.newlifted