WHITELIST = ''
# space-separated list of IPs manually blocked
BLACKLIST = ''
+# user-agents that should be blocked from bulk mode
+# (matched with startswith)
+UA_BLOCKLIST = ()
# time before a automatically blocked IP is allowed back
BLOCKCOOLOFF_PERIOD='1 hour'
BULKLONG_LIMIT = max(BULK_LOWER, BULKLONG_LIMIT - BULK_LOADFAC * (avgload - 14))
if len(prevbulks) > MAX_BULK_IPS:
BLOCK_LIMIT = max(3600, BLOCK_LOWER - (len(prevbulks) - MAX_BULK_IPS)*10)
+# if the bulk pool is still empty, clients will be faster, avoid having
+# them blocked in this case
+if len(prevbulks) < 10:
+ BLOCK_LIMIT = 2*BLOCK_UPPER
+
# get the new block candidates
cur.execute("""
bulkips = {}
emergencyblocks = []
+useragentblocks = []
for c in cur:
if c[0] not in WHITELIST and c[0] not in BLACKLIST:
- missing_agent = not c[2] or c[2].startswith('Java/1.')
+ # check for user agents that receive an immediate block
+ missing_agent = not c[2]
+ if not missing_agent:
+ for ua in UA_BLOCKLIST:
+ if c[2].startswith(ua):
+ missing_agent = True
+ break
if (missing_agent or c[1] > BLOCK_UPPER) and c[0] not in prevblocks:
newblocks.add(c[0])
- if c[0] not in prevbulks:
+ if missing_agent:
+ useragentblocks.append(c[0])
+ else:
emergencyblocks.append(c[0])
else:
bulkips[c[0]] = c[1]
deblockcandidates.add(ip)
for ip in prevbulks:
- if ip in bulkips:
- if bulkips[ip] > BLOCK_LIMIT:
- newblocks.add(ip)
- newlyblocked.append(ip)
+ if ip not in newblocks:
+ if ip in bulkips:
+ if bulkips[ip] > BLOCK_LIMIT:
+ newblocks.add(ip)
+ newlyblocked.append(ip)
+ else:
+ newbulks.add(ip)
+ del bulkips[ip]
else:
- newbulks.add(ip)
- del bulkips[ip]
- else:
- debulkcandidates.add(ip)
+ debulkcandidates.add(ip)
# cross-check deblock candidates
if deblockcandidates:
fd.write(logstr % ('new bulks:', ', '.join(bulkips.keys())))
if emergencyblocks:
fd.write(logstr % ('dir.block:', ', '.join(emergencyblocks)))
+if useragentblocks:
+ fd.write(logstr % (' ua block:', ', '.join(useragentblocks)))
if newlyblocked:
fd.write(logstr % ('new block:', ', '.join(newlyblocked)))
fd.close()