mirror of https://github.com/fail2ban/fail2ban
- Moved hardcoded socket path to a class variable
git-svn-id: https://fail2ban.svn.sourceforge.net/svnroot/fail2ban/trunk@309 a942ae1a-1317-0410-a47c-b1dcaea8d6050.x
parent
cc25b78687
commit
ce5ea1fa23
|
@ -28,25 +28,27 @@ import socket, pickle
|
|||
|
||||
class CSocket:
|
||||
|
||||
END_STRING = "<F2B_END_COMMAND>"
|
||||
SOCKET_FILE = "/tmp/fail2ban.sock"
|
||||
|
||||
def __init__(self):
|
||||
self.socketFile = "/tmp/fail2ban.sock"
|
||||
# Create an INET, STREAMing socket
|
||||
#self.csock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
self.csock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||
#self.csock.connect(("localhost", 2222))
|
||||
self.csock.connect(self.socketFile)
|
||||
self.csock.connect(CSocket.SOCKET_FILE)
|
||||
|
||||
def send(self, msg):
|
||||
# Convert every list member to string
|
||||
obj = pickle.dumps(map(str, msg))
|
||||
self.csock.send(obj + "<F2B_END_COMMAND>")
|
||||
self.csock.send(obj + CSocket.END_STRING)
|
||||
ret = self.receive(self.csock)
|
||||
self.csock.close()
|
||||
return ret
|
||||
|
||||
def receive(self, socket):
|
||||
msg = ''
|
||||
while msg.rfind("<F2B_END_COMMAND>") == -1:
|
||||
while msg.rfind(CSocket.END_STRING) == -1:
|
||||
chunk = socket.recv(6)
|
||||
if chunk == '':
|
||||
raise RuntimeError, "socket connection broken"
|
||||
|
|
|
@ -32,20 +32,22 @@ logSys = logging.getLogger("fail2ban.comm")
|
|||
|
||||
class SSocket(Thread):
|
||||
|
||||
END_STRING = "<F2B_END_COMMAND>"
|
||||
SOCKET_FILE = "/tmp/fail2ban.sock"
|
||||
|
||||
def __init__(self, transmitter):
|
||||
Thread.__init__(self)
|
||||
self.socketFile = "/tmp/fail2ban.sock"
|
||||
self.transmit = transmitter
|
||||
self.isRunning = False
|
||||
logSys.debug("Created SSocket")
|
||||
|
||||
def initialize(self, force = False):
|
||||
# Remove socket
|
||||
if os.path.exists(self.socketFile):
|
||||
if os.path.exists(SSocket.SOCKET_FILE):
|
||||
logSys.error("Fail2ban seems to be already running")
|
||||
if force:
|
||||
logSys.warn("Forcing execution of the server")
|
||||
os.remove(self.socketFile)
|
||||
os.remove(SSocket.SOCKET_FILE)
|
||||
else:
|
||||
raise SSocketErrorException("Server already running")
|
||||
# Create an INET, STREAMing socket
|
||||
|
@ -55,7 +57,7 @@ class SSocket(Thread):
|
|||
self.ssock.setblocking(False)
|
||||
# Bind the socket to a public host and a well-known port
|
||||
#self.ssock.bind(("localhost", 2222))
|
||||
self.ssock.bind(self.socketFile)
|
||||
self.ssock.bind(SSocket.SOCKET_FILE)
|
||||
# Become a server socket
|
||||
self.ssock.listen(1)
|
||||
|
||||
|
@ -79,9 +81,9 @@ class SSocket(Thread):
|
|||
stime = 1.0
|
||||
self.ssock.close()
|
||||
# Remove socket
|
||||
if os.path.exists(self.socketFile):
|
||||
logSys.debug("Removed socket file " + self.socketFile)
|
||||
os.remove(self.socketFile)
|
||||
if os.path.exists(SSocket.SOCKET_FILE):
|
||||
logSys.debug("Removed socket file " + SSocket.SOCKET_FILE)
|
||||
os.remove(SSocket.SOCKET_FILE)
|
||||
logSys.debug("Socket shutdown")
|
||||
return True
|
||||
|
||||
|
@ -96,11 +98,11 @@ class SSocket(Thread):
|
|||
|
||||
def send(self, socket, msg):
|
||||
obj = pickle.dumps(msg)
|
||||
socket.send(obj + "<F2B_END_COMMAND>")
|
||||
socket.send(obj + SSocket.END_STRING)
|
||||
|
||||
def receive(self, socket):
|
||||
msg = ''
|
||||
while msg.rfind("<F2B_END_COMMAND>") == -1:
|
||||
while msg.rfind(SSocket.END_STRING) == -1:
|
||||
chunk = socket.recv(6)
|
||||
if chunk == '':
|
||||
raise RuntimeError, "socket connection broken"
|
||||
|
|
Loading…
Reference in New Issue