mirror of https://github.com/fail2ban/fail2ban
- Change executeCmd to private
- Code comments git-svn-id: https://fail2ban.svn.sourceforge.net/svnroot/fail2ban/trunk@28 a942ae1a-1317-0410-a47c-b1dcaea8d6050.6
parent
18029d4426
commit
3331c9dccb
|
@ -27,6 +27,9 @@ __license__ = "GPL"
|
||||||
import time, os
|
import time, os
|
||||||
|
|
||||||
class Firewall:
|
class Firewall:
|
||||||
|
""" Manages the ban list and executes the command that ban
|
||||||
|
the IP.
|
||||||
|
"""
|
||||||
|
|
||||||
banList = dict()
|
banList = dict()
|
||||||
|
|
||||||
|
@ -35,26 +38,32 @@ class Firewall:
|
||||||
self.logSys = logSys
|
self.logSys = logSys
|
||||||
|
|
||||||
def addBanIP(self, ip, debug):
|
def addBanIP(self, ip, debug):
|
||||||
|
""" Bans an IP.
|
||||||
|
"""
|
||||||
if not self.inBanList(ip):
|
if not self.inBanList(ip):
|
||||||
self.logSys.info("Ban "+ip)
|
self.logSys.info("Ban "+ip)
|
||||||
self.banList[ip] = time.time()
|
self.banList[ip] = time.time()
|
||||||
self.executeCmd(self.banIP(ip), debug)
|
self.__executeCmd(self.banIP(ip), debug)
|
||||||
else:
|
else:
|
||||||
self.logSys.info(ip+" already in ban list")
|
self.logSys.info(ip+" already in ban list")
|
||||||
|
|
||||||
def delBanIP(self, ip, debug):
|
def delBanIP(self, ip, debug):
|
||||||
|
""" Unban an IP.
|
||||||
|
"""
|
||||||
if self.inBanList(ip):
|
if self.inBanList(ip):
|
||||||
self.logSys.info("Unban "+ip)
|
self.logSys.info("Unban "+ip)
|
||||||
del self.banList[ip]
|
del self.banList[ip]
|
||||||
self.executeCmd(self.unBanIP(ip), debug)
|
self.__executeCmd(self.unBanIP(ip), debug)
|
||||||
else:
|
else:
|
||||||
self.logSys.info(ip+" not in ban list")
|
self.logSys.info(ip+" not in ban list")
|
||||||
|
|
||||||
def inBanList(self, ip):
|
def inBanList(self, ip):
|
||||||
|
""" Checks if IP is in ban list.
|
||||||
|
"""
|
||||||
return self.banList.has_key(ip)
|
return self.banList.has_key(ip)
|
||||||
|
|
||||||
def checkForUnBan(self, debug):
|
def checkForUnBan(self, debug):
|
||||||
""" Check for user to remove from ban list.
|
""" Check for IP to remove from ban list.
|
||||||
"""
|
"""
|
||||||
banListTemp = self.banList.copy()
|
banListTemp = self.banList.copy()
|
||||||
iterBanList = banListTemp.iteritems()
|
iterBanList = banListTemp.iteritems()
|
||||||
|
@ -66,13 +75,18 @@ class Firewall:
|
||||||
self.delBanIP(ip, debug)
|
self.delBanIP(ip, debug)
|
||||||
|
|
||||||
def flushBanList(self, debug):
|
def flushBanList(self, debug):
|
||||||
|
""" Flushes the ban list and of course the firewall rules.
|
||||||
|
Called when fail2ban exits.
|
||||||
|
"""
|
||||||
iterBanList = self.banList.iteritems()
|
iterBanList = self.banList.iteritems()
|
||||||
for i in range(len(self.banList)):
|
for i in range(len(self.banList)):
|
||||||
element = iterBanList.next()
|
element = iterBanList.next()
|
||||||
ip = element[0]
|
ip = element[0]
|
||||||
self.delBanIP(ip, debug)
|
self.delBanIP(ip, debug)
|
||||||
|
|
||||||
def executeCmd(self, cmd, debug):
|
def __executeCmd(self, cmd, debug):
|
||||||
|
""" Executes an OS command.
|
||||||
|
"""
|
||||||
self.logSys.debug(cmd)
|
self.logSys.debug(cmd)
|
||||||
if not debug:
|
if not debug:
|
||||||
return os.system(cmd)
|
return os.system(cmd)
|
||||||
|
@ -80,6 +94,8 @@ class Firewall:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def viewBanList(self):
|
def viewBanList(self):
|
||||||
|
""" Prints the ban list on screen. Usefull for debugging.
|
||||||
|
"""
|
||||||
iterBanList = self.banList.iteritems()
|
iterBanList = self.banList.iteritems()
|
||||||
for i in range(len(self.banList)):
|
for i in range(len(self.banList)):
|
||||||
element = iterBanList.next()
|
element = iterBanList.next()
|
||||||
|
|
Loading…
Reference in New Issue