3 # Create or update the list of temporarily banned IPs.
6 BLOCKEDFILE=/home/lonvia/nominatim/settings/ip_blocks
7 LOGFILE=/home/lonvia/nominatim/log/ip_blocks.log
11 COOLOFF_PERIOD='1 hour'
16 SRCHSHORT_LIMIT='10 min'
18 PSQLCMD='psql -qtA -d nominatim'
21 $PSQLCMD > $BLOCKEDFILE.newblocks << ENDOFQUERY
23 ((SELECT ipaddress FROM
24 (SELECT ipaddress, count(*) FROM new_query_log
25 WHERE type = 'reverse' AND starttime > now() - interval '$LONG_PERIOD'
28 WHERE count > $REVLONG_LIMIT)
30 (SELECT ipaddress FROM
31 (SELECT ipaddress, count(*) FROM new_query_log
32 WHERE type = 'reverse' AND starttime > now() - interval '$SHORT_PERIOD'
35 WHERE count > $REVSHORT_LIMIT)
37 (SELECT ipaddress FROM
38 (SELECT ipaddress, count(*) FROM new_query_log
39 WHERE type = 'search' AND starttime > now() - interval '$LONG_PERIOD'
42 WHERE count > $SRCHLONG_LIMIT)
44 (SELECT ipaddress FROM
45 (SELECT ipaddress, sum(endtime-starttime) as dur FROM new_query_log
46 WHERE type = 'search' AND starttime > now() - interval '$SHORT_PERIOD'
49 WHERE dur > '$SRCHSHORT_LIMIT')
50 ) as q ORDER BY ipaddress;
53 no_newblocks=`comm $BLOCKEDFILE.newblocks $BLOCKEDFILE -23 | wc -l`
55 if [ "x$no_newblocks" != "x0" ]; then
56 date +"%x %X Newly blocked IPs: `comm $BLOCKEDFILE.newblocks $BLOCKEDFILE -23 | tr '\n' ' '`" >> $LOGFILE
60 # Deblockable candidates
61 blocked=`tr '\n' ',' < $BLOCKEDFILE | sed "s:[[:space:]]::g;s:,$::;s:,:'),(':g"`
63 if [ "x$blocked" == "x" ]; then
64 mv $BLOCKEDFILE.newblocks $BLOCKEDFILE
66 $PSQLCMD > $BLOCKEDFILE.newlifted << ENDOFQUERY
69 (SELECT DISTINCT ipaddress FROM new_query_log
70 WHERE starttime > now() - interval '$COOLOFF_PERIOD')
73 no_lifted=`cat $BLOCKEDFILE.newlifted | wc -w`
75 if [ "x$no_lifted" != "x0" ]; then
76 date +"%x %X Bans lifted: `tr '\n' ' ' < $BLOCKEDFILE.newlifted`" >> $LOGFILE
79 # Write out new blocks
80 cat $BLOCKEDFILE.newblocks $BLOCKEDFILE | sort -u | comm - $BLOCKEDFILE.newlifted -23 > $BLOCKEDFILE.new
81 mv $BLOCKEDFILE.new $BLOCKEDFILE
83 rm $BLOCKEDFILE.newblocks $BLOCKEDFILE.newlifted