mirror of https://github.com/fail2ban/fail2ban
redefine protocol constants in protocol.py (prevent unnecessary duplication)
parent
17502bd818
commit
3e47ce7f2a
|
@ -26,15 +26,12 @@ __license__ = "GPL"
|
||||||
|
|
||||||
#from cPickle import dumps, loads, HIGHEST_PROTOCOL
|
#from cPickle import dumps, loads, HIGHEST_PROTOCOL
|
||||||
from pickle import dumps, loads, HIGHEST_PROTOCOL
|
from pickle import dumps, loads, HIGHEST_PROTOCOL
|
||||||
|
from ..protocol import CSPROTO
|
||||||
import socket
|
import socket
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
class CSocket:
|
class CSocket:
|
||||||
|
|
||||||
EMPTY_BYTES = b""
|
|
||||||
END_STRING = b"<F2B_END_COMMAND>"
|
|
||||||
CLOSE_STRING = b"<F2B_CLOSE_COMMAND>"
|
|
||||||
|
|
||||||
def __init__(self, sock="/var/run/fail2ban/fail2ban.sock"):
|
def __init__(self, sock="/var/run/fail2ban/fail2ban.sock"):
|
||||||
# Create an INET, STREAMing socket
|
# Create an INET, STREAMing socket
|
||||||
#self.csock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
#self.csock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
@ -48,21 +45,21 @@ class CSocket:
|
||||||
def send(self, msg):
|
def send(self, msg):
|
||||||
# Convert every list member to string
|
# Convert every list member to string
|
||||||
obj = dumps([str(m) for m in msg], HIGHEST_PROTOCOL)
|
obj = dumps([str(m) for m in msg], HIGHEST_PROTOCOL)
|
||||||
self.__csock.send(obj + CSocket.END_STRING)
|
self.__csock.send(obj + CSPROTO.END)
|
||||||
return self.receive(self.__csock)
|
return self.receive(self.__csock)
|
||||||
|
|
||||||
def close(self, sendEnd=True):
|
def close(self, sendEnd=True):
|
||||||
if not self.__csock:
|
if not self.__csock:
|
||||||
return
|
return
|
||||||
if sendEnd:
|
if sendEnd:
|
||||||
self.__csock.sendall(CSocket.CLOSE_STRING + CSocket.END_STRING)
|
self.__csock.sendall(CSPROTO.CLOSE + CSPROTO.END)
|
||||||
self.__csock.close()
|
self.__csock.close()
|
||||||
self.__csock = None
|
self.__csock = None
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def receive(sock):
|
def receive(sock):
|
||||||
msg = CSocket.EMPTY_BYTES
|
msg = CSPROTO.EMPTY
|
||||||
while msg.rfind(CSocket.END_STRING) == -1:
|
while msg.rfind(CSPROTO.END) == -1:
|
||||||
chunk = sock.recv(6)
|
chunk = sock.recv(6)
|
||||||
if chunk == '':
|
if chunk == '':
|
||||||
raise RuntimeError("socket connection broken")
|
raise RuntimeError("socket connection broken")
|
||||||
|
|
|
@ -29,6 +29,16 @@ import textwrap
|
||||||
##
|
##
|
||||||
# Describes the protocol used to communicate with the server.
|
# Describes the protocol used to communicate with the server.
|
||||||
|
|
||||||
|
class dotdict(dict):
|
||||||
|
def __getattr__(self, name):
|
||||||
|
return self[name]
|
||||||
|
|
||||||
|
CSPROTO = dotdict({
|
||||||
|
"EMPTY": b"",
|
||||||
|
"END": b"<F2B_END_COMMAND>",
|
||||||
|
"CLOSE": b"<F2B_CLOSE_COMMAND>"
|
||||||
|
})
|
||||||
|
|
||||||
protocol = [
|
protocol = [
|
||||||
['', "BASIC", ""],
|
['', "BASIC", ""],
|
||||||
["start", "starts the server and the jails"],
|
["start", "starts the server and the jails"],
|
||||||
|
|
|
@ -33,6 +33,7 @@ import socket
|
||||||
import sys
|
import sys
|
||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
|
from ..protocol import CSPROTO
|
||||||
from ..helpers import getLogger,formatExceptionInfo
|
from ..helpers import getLogger,formatExceptionInfo
|
||||||
|
|
||||||
# Gets the instance of the logger.
|
# Gets the instance of the logger.
|
||||||
|
@ -46,17 +47,12 @@ logSys = getLogger(__name__)
|
||||||
|
|
||||||
class RequestHandler(asynchat.async_chat):
|
class RequestHandler(asynchat.async_chat):
|
||||||
|
|
||||||
# python 2.x, string type is equivalent to bytes.
|
|
||||||
EMPTY_BYTES = b""
|
|
||||||
END_STRING = b"<F2B_END_COMMAND>"
|
|
||||||
CLOSE_STRING = b"<F2B_CLOSE_COMMAND>"
|
|
||||||
|
|
||||||
def __init__(self, conn, transmitter):
|
def __init__(self, conn, transmitter):
|
||||||
asynchat.async_chat.__init__(self, conn)
|
asynchat.async_chat.__init__(self, conn)
|
||||||
self.__transmitter = transmitter
|
self.__transmitter = transmitter
|
||||||
self.__buffer = []
|
self.__buffer = []
|
||||||
# Sets the terminator.
|
# Sets the terminator.
|
||||||
self.set_terminator(RequestHandler.END_STRING)
|
self.set_terminator(CSPROTO.END)
|
||||||
|
|
||||||
def collect_incoming_data(self, data):
|
def collect_incoming_data(self, data):
|
||||||
#logSys.debug("Received raw data: " + str(data))
|
#logSys.debug("Received raw data: " + str(data))
|
||||||
|
@ -72,9 +68,9 @@ class RequestHandler(asynchat.async_chat):
|
||||||
buf = self.__buffer
|
buf = self.__buffer
|
||||||
self.__buffer = []
|
self.__buffer = []
|
||||||
# Joins the buffer items.
|
# Joins the buffer items.
|
||||||
message = loads(RequestHandler.EMPTY_BYTES.join(buf))
|
message = loads(CSPROTO.EMPTY.join(buf))
|
||||||
# Closes the channel if close was received
|
# Closes the channel if close was received
|
||||||
if message == RequestHandler.CLOSE_STRING:
|
if message == CSPROTO.CLOSE:
|
||||||
self.close_when_done()
|
self.close_when_done()
|
||||||
return
|
return
|
||||||
# Gives the message to the transmitter.
|
# Gives the message to the transmitter.
|
||||||
|
@ -82,7 +78,7 @@ class RequestHandler(asynchat.async_chat):
|
||||||
# Serializes the response.
|
# Serializes the response.
|
||||||
message = dumps(message, HIGHEST_PROTOCOL)
|
message = dumps(message, HIGHEST_PROTOCOL)
|
||||||
# Sends the response to the client.
|
# Sends the response to the client.
|
||||||
self.push(message + RequestHandler.END_STRING)
|
self.push(message + CSPROTO.END)
|
||||||
|
|
||||||
def handle_error(self):
|
def handle_error(self):
|
||||||
e1, e2 = formatExceptionInfo()
|
e1, e2 = formatExceptionInfo()
|
||||||
|
|
Loading…
Reference in New Issue