From 3e47ce7f2a8b5e16a467a1876c89488622d1a39c Mon Sep 17 00:00:00 2001 From: sebres Date: Mon, 6 Jul 2015 17:37:12 +0200 Subject: [PATCH] redefine protocol constants in protocol.py (prevent unnecessary duplication) --- fail2ban/client/csocket.py | 13 +++++-------- fail2ban/protocol.py | 10 ++++++++++ fail2ban/server/asyncserver.py | 14 +++++--------- 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/fail2ban/client/csocket.py b/fail2ban/client/csocket.py index 171f8339..2e22e5ee 100644 --- a/fail2ban/client/csocket.py +++ b/fail2ban/client/csocket.py @@ -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"" - CLOSE_STRING = b"" - 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") diff --git a/fail2ban/protocol.py b/fail2ban/protocol.py index 9b690067..2cace91f 100644 --- a/fail2ban/protocol.py +++ b/fail2ban/protocol.py @@ -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"", + "CLOSE": b"" +}) + protocol = [ ['', "BASIC", ""], ["start", "starts the server and the jails"], diff --git a/fail2ban/server/asyncserver.py b/fail2ban/server/asyncserver.py index 29d1ccc0..4caa702f 100644 --- a/fail2ban/server/asyncserver.py +++ b/fail2ban/server/asyncserver.py @@ -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"" - CLOSE_STRING = b"" - 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()