redefine protocol constants in protocol.py (prevent unnecessary duplication)

pull/1099/head
sebres 10 years ago
parent 17502bd818
commit 3e47ce7f2a

@ -26,15 +26,12 @@ __license__ = "GPL"
#from cPickle import dumps, loads, HIGHEST_PROTOCOL
from pickle import dumps, loads, HIGHEST_PROTOCOL
from ..protocol import CSPROTO
import socket
import sys
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"):
# Create an INET, STREAMing socket
#self.csock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
@ -48,21 +45,21 @@ class CSocket:
def send(self, msg):
# Convert every list member to string
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)
def close(self, sendEnd=True):
if not self.__csock:
return
if sendEnd:
self.__csock.sendall(CSocket.CLOSE_STRING + CSocket.END_STRING)
self.__csock.sendall(CSPROTO.CLOSE + CSPROTO.END)
self.__csock.close()
self.__csock = None
@staticmethod
def receive(sock):
msg = CSocket.EMPTY_BYTES
while msg.rfind(CSocket.END_STRING) == -1:
msg = CSPROTO.EMPTY
while msg.rfind(CSPROTO.END) == -1:
chunk = sock.recv(6)
if chunk == '':
raise RuntimeError("socket connection broken")

@ -29,6 +29,16 @@ import textwrap
##
# 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 = [
['', "BASIC", ""],
["start", "starts the server and the jails"],

@ -33,6 +33,7 @@ import socket
import sys
import traceback
from ..protocol import CSPROTO
from ..helpers import getLogger,formatExceptionInfo
# Gets the instance of the logger.
@ -46,17 +47,12 @@ logSys = getLogger(__name__)
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):
asynchat.async_chat.__init__(self, conn)
self.__transmitter = transmitter
self.__buffer = []
# Sets the terminator.
self.set_terminator(RequestHandler.END_STRING)
self.set_terminator(CSPROTO.END)
def collect_incoming_data(self, data):
#logSys.debug("Received raw data: " + str(data))
@ -72,9 +68,9 @@ class RequestHandler(asynchat.async_chat):
buf = self.__buffer
self.__buffer = []
# Joins the buffer items.
message = loads(RequestHandler.EMPTY_BYTES.join(buf))
message = loads(CSPROTO.EMPTY.join(buf))
# Closes the channel if close was received
if message == RequestHandler.CLOSE_STRING:
if message == CSPROTO.CLOSE:
self.close_when_done()
return
# Gives the message to the transmitter.
@ -82,7 +78,7 @@ class RequestHandler(asynchat.async_chat):
# Serializes the response.
message = dumps(message, HIGHEST_PROTOCOL)
# Sends the response to the client.
self.push(message + RequestHandler.END_STRING)
self.push(message + CSPROTO.END)
def handle_error(self):
e1, e2 = formatExceptionInfo()

Loading…
Cancel
Save