Files
fail2ban-p2p/fail2ban-p2p/server.py
2026-04-22 23:24:29 +02:00

41 lines
1.4 KiB
Python

from parser import parse
import customexceptions
import log
logger = log.initialize_logging("fail2ban-p2p." + __name__)
def serve(n, connection, address):
"""Starts the server listener socket to receive messages."""
data = connection.recv(1024)
logger.debug("Parsing message: %s", data)
command = parse(data)
if command:
logger.debug("command syntax verified")
try:
n.verifyMessage(command)
logger.debug("command signature verified")
n.addMessage(command)
if command.msgType == 2:
timeframe = int(command.parameter['TimeFrame'])
logger.debug("Requested Timeframe is: %s", timeframe)
connection.sendall(n.dumpBanlist(timeframe).encode("utf-8"))
else:
connection.sendall(b"OK\n")
except customexceptions.InvalidMessage:
connection.sendall(b"ERROR Invalid message\n")
logger.warning("This message made no sense.")
except customexceptions.InvalidSignature:
connection.sendall(b"ERROR invalid signature\n")
logger.warning("The Signature could not be verified")
except customexceptions.InvalidProtocolVersion:
connection.sendall(b"ERROR invalid protocol version\n")
else:
connection.sendall(b"Error\n")
logger.warning('invalid message')
connection.close()