From af675f4dc23fecdf08659d2e4b47c0044419511d Mon Sep 17 00:00:00 2001 From: lostcontrol Date: Sun, 10 Oct 2004 13:35:11 +0000 Subject: [PATCH] - a few changes and corrections git-svn-id: https://fail2ban.svn.sourceforge.net/svnroot/fail2ban/trunk@9 a942ae1a-1317-0410-a47c-b1dcaea8d605 --- firewall/firewall.py | 38 ++++++++++++++++++++++++++++++++++---- firewall/iptables.py | 14 +++++--------- 2 files changed, 39 insertions(+), 13 deletions(-) diff --git a/firewall/firewall.py b/firewall/firewall.py index 0a937d77..76632a75 100644 --- a/firewall/firewall.py +++ b/firewall/firewall.py @@ -24,24 +24,54 @@ __date__ = "$Date$" __copyright__ = "Copyright (c) 2004 Cyril Jaquier" __license__ = "GPL" +import time + class Firewall: banList = dict() - def addBanIP(self, ip, time): - self.banList[ip] = time + def __init__(self, banTime): + self.banTime = banTime + + def addBanIP(self, ip): + if not self.inBanList(ip): + self.banList[ip] = time.time() + self.executeCmd(self.banIP(ip)) + else: + print ip, "already in ban list" def delBanIP(self, ip): - del self.banList[ip] + if self.inBanList(ip): + del self.banList[ip] + self.executeCmd(self.unBanIP(ip)) + else: + print ip, "not in ban list" + + def inBanList(self, ip): + return self.banList.has_key(ip) + + def checkForUnBan(self): + """ Check for user to remove from ban list. + """ + banListTemp = self.banList.copy() + iterBanList = banListTemp.iteritems() + for i in range(len(self.banList)): + element = iterBanList.next() + ip = element[0] + btime = element[1] + if btime < time.time()-self.banTime: + self.delBanIP(ip) + print '`->', time.time() def flushBanList(self): iterBanList = self.banList.iteritems() for i in range(len(self.banList)): element = iterBanList.next() ip = element[0] - self.unBanIP(ip) + self.delBanIP(ip) def executeCmd(self, cmd): + print cmd return #os.system(cmd) def viewBanList(self): diff --git a/firewall/iptables.py b/firewall/iptables.py index fcee9652..8206e631 100644 --- a/firewall/iptables.py +++ b/firewall/iptables.py @@ -28,14 +28,10 @@ from firewall import Firewall class Iptables(Firewall): - def banIP(self, ip, time): - query = 'iptables -I INPUT 1 -i eth0 -s '+str(ip)+' -j DROP' - self.addBanIP(ip, time) - self.executeCmd(query) - print query + def banIP(self, ip): + query = 'iptables -I INPUT 1 -i eth0 -s '+ip+' -j DROP' + return query def unBanIP(self, ip): - query = 'iptables -D INPUT -i eth0 -s '+str(ip)+' -j DROP' - self.delBanIP(ip) - self.executeCmd(query) - print query + query = 'iptables -D INPUT -i eth0 -s '+ip+' -j DROP' + return query