mirror of https://github.com/fail2ban/fail2ban
- Moved __executeCmd to utils/process.py
- Removed interface git-svn-id: https://fail2ban.svn.sourceforge.net/svnroot/fail2ban/branches/FAIL2BAN-0_5@119 a942ae1a-1317-0410-a47c-b1dcaea8d6050.5
parent
65fef83e56
commit
3df3c510f4
|
@ -26,6 +26,8 @@ __license__ = "GPL"
|
||||||
|
|
||||||
import time, os, log4py, re
|
import time, os, log4py, re
|
||||||
|
|
||||||
|
from utils.process import executeCmd
|
||||||
|
|
||||||
# Gets the instance of log4py.
|
# Gets the instance of log4py.
|
||||||
logSys = log4py.Logger().get_instance()
|
logSys = log4py.Logger().get_instance()
|
||||||
|
|
||||||
|
@ -36,11 +38,10 @@ class Firewall:
|
||||||
|
|
||||||
banList = dict()
|
banList = dict()
|
||||||
|
|
||||||
def __init__(self, banRule, unBanRule, banTime, interface):
|
def __init__(self, banRule, unBanRule, banTime):
|
||||||
self.banRule = banRule
|
self.banRule = banRule
|
||||||
self.unBanRule = unBanRule
|
self.unBanRule = unBanRule
|
||||||
self.banTime = banTime
|
self.banTime = banTime
|
||||||
self.interface = interface
|
|
||||||
|
|
||||||
def addBanIP(self, ip, debug):
|
def addBanIP(self, ip, debug):
|
||||||
""" Bans an IP.
|
""" Bans an IP.
|
||||||
|
@ -48,7 +49,7 @@ class Firewall:
|
||||||
if not self.inBanList(ip):
|
if not self.inBanList(ip):
|
||||||
logSys.warn("Ban "+ip)
|
logSys.warn("Ban "+ip)
|
||||||
self.banList[ip] = time.time()
|
self.banList[ip] = time.time()
|
||||||
self.__executeCmd(self.banIP(ip), debug)
|
executeCmd(self.banIP(ip), debug)
|
||||||
else:
|
else:
|
||||||
logSys.error(ip+" already in ban list")
|
logSys.error(ip+" already in ban list")
|
||||||
|
|
||||||
|
@ -58,7 +59,7 @@ class Firewall:
|
||||||
if self.inBanList(ip):
|
if self.inBanList(ip):
|
||||||
logSys.warn("Unban "+ip)
|
logSys.warn("Unban "+ip)
|
||||||
del self.banList[ip]
|
del self.banList[ip]
|
||||||
self.__executeCmd(self.unBanIP(ip), debug)
|
executeCmd(self.unBanIP(ip), debug)
|
||||||
else:
|
else:
|
||||||
logSys.error(ip+" not in ban list")
|
logSys.error(ip+" not in ban list")
|
||||||
|
|
||||||
|
@ -86,31 +87,23 @@ class Firewall:
|
||||||
ip = element[0]
|
ip = element[0]
|
||||||
self.delBanIP(ip, debug)
|
self.delBanIP(ip, debug)
|
||||||
|
|
||||||
def __executeCmd(self, cmd, debug):
|
|
||||||
""" Executes an OS command.
|
|
||||||
"""
|
|
||||||
logSys.debug(cmd)
|
|
||||||
if not debug:
|
|
||||||
return os.system(cmd)
|
|
||||||
else:
|
|
||||||
return None
|
|
||||||
|
|
||||||
def banIP(self, ip):
|
def banIP(self, ip):
|
||||||
""" Returns query to ban IP.
|
""" Returns query to ban IP.
|
||||||
"""
|
"""
|
||||||
query = self.replaceTag(self.banRule, ip, self.interface)
|
query = self.replaceTag(self.banRule, ip)
|
||||||
return query
|
return query
|
||||||
|
|
||||||
def unBanIP(self, ip):
|
def unBanIP(self, ip):
|
||||||
""" Returns query to unban IP.
|
""" Returns query to unban IP.
|
||||||
"""
|
"""
|
||||||
query = self.replaceTag(self.unBanRule, ip, self.interface)
|
query = self.replaceTag(self.unBanRule, ip)
|
||||||
return query
|
return query
|
||||||
|
|
||||||
def replaceTag(self, query, ip, interface):
|
def replaceTag(self, query, ip):
|
||||||
|
""" Replace tag in query
|
||||||
|
"""
|
||||||
string = query
|
string = query
|
||||||
string = string.replace("<ip>", ip)
|
string = string.replace("<ip>", ip)
|
||||||
string = string.replace("<if>", interface)
|
|
||||||
return string
|
return string
|
||||||
|
|
||||||
def viewBanList(self):
|
def viewBanList(self):
|
||||||
|
|
Loading…
Reference in New Issue