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'
27 $PSQLCMD > $BLOCKEDFILE.newblocks << ENDOFQUERY
29 ((SELECT ipaddress FROM
30 (SELECT ipaddress, count(*) FROM new_query_log
31 WHERE type = 'reverse' AND starttime > now() - interval '$LONG_PERIOD'
34 WHERE count > $REVLONG_LIMIT)
36 (SELECT ipaddress FROM
37 (SELECT ipaddress, count(*) FROM new_query_log
38 WHERE type = 'reverse' AND starttime > now() - interval '$SHORT_PERIOD'
41 WHERE count > $REVSHORT_LIMIT)
43 (SELECT ipaddress FROM
44 (SELECT ipaddress, count(*) FROM new_query_log
45 WHERE type = 'search' AND starttime > now() - interval '$LONG_PERIOD'
48 WHERE count > $SRCHLONG_LIMIT)
50 (SELECT ipaddress FROM
51 (SELECT ipaddress, sum(endtime-starttime) as dur FROM new_query_log
52 WHERE type = 'search' AND starttime > now() - interval '$SHORT_PERIOD'
55 WHERE dur > '$SRCHSHORT_LIMIT')
56 ) as q ORDER BY ipaddress;
59 no_newblocks=`comm $BLOCKEDFILE.newblocks $BLOCKEDFILE -23 | wc -l`
61 if [ "x$no_newblocks" != "x0" ]; then
62 date +"%x %X Newly blocked IPs: `comm $BLOCKEDFILE.newblocks $BLOCKEDFILE -23 | tr '\n' ' '`" >> $LOGFILE
66 # Deblockable candidates
67 blocked=`tr '\n' ',' < $BLOCKEDFILE | sed "s:[[:space:]]::g;s:,$::;s:,:'),(':g"`
69 if [ "x$blocked" == "x" ]; then
70 mv $BLOCKEDFILE.newblocks $BLOCKEDFILE
72 $PSQLCMD > $BLOCKEDFILE.newlifted << ENDOFQUERY
75 (SELECT DISTINCT ipaddress FROM new_query_log
76 WHERE starttime > now() - interval '$COOLOFF_PERIOD')
79 no_lifted=`cat $BLOCKEDFILE.newlifted | wc -w`
81 if [ "x$no_lifted" != "x0" ]; then
82 date +"%x %X Bans lifted: `tr '\n' ' ' < $BLOCKEDFILE.newlifted`" >> $LOGFILE
85 # Write out new blocks
86 cat $BLOCKEDFILE.newblocks $BLOCKEDFILE | sort -u | comm - $BLOCKEDFILE.newlifted -23 > $BLOCKEDFILE.new
87 mv $BLOCKEDFILE.new $BLOCKEDFILE
89 rm $BLOCKEDFILE.newblocks $BLOCKEDFILE.newlifted