/usr/share/denyhosts/DenyHosts/purgecounter.py is in denyhosts 2.10-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 72 73 74 75 | import logging
import os
import constants
from counter import Counter, CounterRecord
error = logging.getLogger("purgecounter").error
info = logging.getLogger("purgecounter").info
class PurgeCounter(object):
def __init__(self, prefs):
self.filename = os.path.join(prefs['WORK_DIR'],
constants.PURGE_HISTORY)
self.purge_threshold = prefs['PURGE_THRESHOLD']
def get_banned_for_life(self):
banned = set()
if self.purge_threshold == 0:
return banned
try:
fp = open(self.filename, "r")
except IOError:
return banned
for line in fp:
try:
host, count, timestamp = line.strip().split(':', 2)
except Exception:
continue
if int(count) > self.purge_threshold:
banned.add(host)
fp.close()
return banned
def get_data(self):
counter = Counter()
try:
fp = open(self.filename, "r")
except IOError:
return counter
for line in fp:
try:
host, count, timestamp = line.strip().split(':', 2)
except Exception:
continue
counter[host] = CounterRecord(int(count), timestamp)
fp.close()
return counter
def write_data(self, data):
try:
fp = open(self.filename, "w")
keys = data.keys()
keys.sort()
for key in keys:
fp.write("%s:%s\n" % (key, data[key]))
fp.close()
except Exception, e:
error("error saving %s: %s", self.filename, str(e))
def increment(self, purged_hosts):
data = self.get_data()
for host in purged_hosts:
data[host] += 1
self.write_data(data)
|