mirror of https://github.com/fail2ban/fail2ban
- Improved server startup checks
- Added "force" option "-x" git-svn-id: https://fail2ban.svn.sourceforge.net/svnroot/fail2ban/trunk@270 a942ae1a-1317-0410-a47c-b1dcaea8d6050.x
parent
448701ad61
commit
a694df4a9a
|
@ -50,6 +50,7 @@ class Fail2banServer:
|
|||
self.argv = None
|
||||
self.conf = dict()
|
||||
self.conf["background"] = True
|
||||
self.conf["force"] = False
|
||||
|
||||
def dispUsage(self):
|
||||
""" Prints Fail2Ban command line options and exits
|
||||
|
@ -61,6 +62,7 @@ class Fail2banServer:
|
|||
print
|
||||
print " -b start in background"
|
||||
print " -f start in foreground"
|
||||
print " -x force execution of the server"
|
||||
print " -h display this help message"
|
||||
print
|
||||
print "Report bugs to <lostcontrol@users.sourceforge.net>"
|
||||
|
@ -74,6 +76,8 @@ class Fail2banServer:
|
|||
self.conf["background"] = True
|
||||
if opt[0] == "-f":
|
||||
self.conf["background"] = False
|
||||
if opt[0] == "-x":
|
||||
self.conf["force"] = True
|
||||
if opt[0] in ["-h", "--help"]:
|
||||
self.dispUsage()
|
||||
|
||||
|
@ -90,7 +94,7 @@ class Fail2banServer:
|
|||
|
||||
# Reads the command line options.
|
||||
try:
|
||||
cmdOpts = 'bfh'
|
||||
cmdOpts = 'bfxh'
|
||||
cmdLongOpts = ['help']
|
||||
optList, args = getopt.getopt(self.argv[1:], cmdOpts, cmdLongOpts)
|
||||
except getopt.GetoptError:
|
||||
|
@ -107,7 +111,7 @@ class Fail2banServer:
|
|||
|
||||
try:
|
||||
self.server = Server()
|
||||
self.server.start()
|
||||
self.server.start(self.conf["force"])
|
||||
except Exception, e:
|
||||
print e
|
||||
self.server.quit()
|
||||
|
|
|
@ -42,9 +42,9 @@ class Server:
|
|||
self.setLogLevel(self.logLevel)
|
||||
self.setLogTarget(self.logTarget)
|
||||
|
||||
def start(self):
|
||||
def start(self, force):
|
||||
# Start the communication
|
||||
self.transm.start()
|
||||
self.transm.start(force)
|
||||
|
||||
def quit(self):
|
||||
for jail in self.jails.copy():
|
||||
|
|
|
@ -39,10 +39,15 @@ class SSocket(Thread):
|
|||
self.isRunning = False
|
||||
logSys.debug("Created SSocket")
|
||||
|
||||
def initialize(self):
|
||||
def initialize(self, force = False):
|
||||
# Remove socket
|
||||
if os.path.exists(self.socketFile):
|
||||
logSys.error("Fail2ban seems to be already running")
|
||||
if force:
|
||||
logSys.warn("Forcing execution of the server")
|
||||
os.remove(self.socketFile)
|
||||
else:
|
||||
raise SSocketErrorException("Server already running")
|
||||
# Create an INET, STREAMing socket
|
||||
#self.ssock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
self.ssock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||
|
@ -101,3 +106,7 @@ class SSocket(Thread):
|
|||
raise RuntimeError, "socket connection broken"
|
||||
msg = msg + chunk
|
||||
return pickle.loads(msg)
|
||||
|
||||
|
||||
class SSocketErrorException(Exception):
|
||||
pass
|
||||
|
|
|
@ -25,6 +25,7 @@ __copyright__ = "Copyright (c) 2004 Cyril Jaquier"
|
|||
__license__ = "GPL"
|
||||
|
||||
from ssocket import SSocket
|
||||
from ssocket import SSocketErrorException
|
||||
import re, pickle, logging
|
||||
|
||||
# Gets the instance of the logger.
|
||||
|
@ -36,9 +37,12 @@ class Transmitter:
|
|||
self.server = server
|
||||
self.socket = SSocket(self)
|
||||
|
||||
def start(self):
|
||||
self.socket.initialize()
|
||||
def start(self, force):
|
||||
try:
|
||||
self.socket.initialize(force)
|
||||
self.socket.start()
|
||||
except SSocketErrorException:
|
||||
logSys.error("Could not start server")
|
||||
|
||||
##
|
||||
# Stop the transmitter.
|
||||
|
@ -78,6 +82,9 @@ class Transmitter:
|
|||
self.server.startJail(name)
|
||||
return None
|
||||
elif action[0] == "stop":
|
||||
if len(action) == 1:
|
||||
self.server.quit()
|
||||
else:
|
||||
name = action[1]
|
||||
self.server.stopJail(name)
|
||||
return None
|
||||
|
@ -91,9 +98,6 @@ class Transmitter:
|
|||
return self.actionGet(action[1:])
|
||||
elif action[0] == "status":
|
||||
return self.status(action[1:])
|
||||
elif action[0] == "quit":
|
||||
self.server.quit()
|
||||
return None
|
||||
raise Exception("Invalid command")
|
||||
|
||||
def actionSet(self, action):
|
||||
|
|
Loading…
Reference in New Issue