Some further changes to assist with bottleknecking causing upstream issues to UFW
This commit is contained in:
parent
2e04b5eeb8
commit
8ce56e19be
1 changed files with 79 additions and 18 deletions
|
@ -1,33 +1,94 @@
|
||||||
def main():
|
def main():
|
||||||
|
|
||||||
import socket
|
import socket
|
||||||
|
import os
|
||||||
import config
|
import config
|
||||||
import datetime
|
import datetime
|
||||||
|
import signal
|
||||||
|
import threading
|
||||||
from log import logsManager
|
from log import logsManager
|
||||||
import sys
|
import sys
|
||||||
|
import select
|
||||||
|
|
||||||
if len(sys.argv) != 4:
|
socketFile = '/etc/monutil/ip.socket'
|
||||||
print("Usage: python script.py <blockedIPAddress> <jail> <live>")
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
hostname = socket.gethostname()
|
if len(sys.argv) == 4:
|
||||||
ipAddress = socket.gethostbyname(hostname)
|
|
||||||
blockedIPAddress = sys.argv[1]
|
|
||||||
jail = sys.argv[2]
|
|
||||||
live = int(sys.argv[3])
|
|
||||||
logTime = datetime.datetime.now()
|
|
||||||
|
|
||||||
if not config.loggingMode == 'none' and not config.loggingMode == 'rabbitmq':
|
hostname = socket.gethostname()
|
||||||
|
ipAddress = socket.gethostbyname(hostname)
|
||||||
|
blockedIPAddress = sys.argv[1]
|
||||||
|
jail = sys.argv[2]
|
||||||
|
live = int(sys.argv[3])
|
||||||
|
logTime = datetime.datetime.now()
|
||||||
|
|
||||||
manager = logsManager(config.sqlServer, config.sqlDatabase, config.sqlUsername, config.sqlPassword)
|
clientSocket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||||
manager.insertIPBlock(hostname, ipAddress, blockedIPAddress, jail, live, logTime, token)
|
clientSocket.connect(socketFile)
|
||||||
|
data = f"{hostname}|{ipAddress}|{logTime}|ipBlock|{blockedIPAddress}|{jail}|{live}"
|
||||||
|
clientSocket.send(data.encode('utf-8'))
|
||||||
|
clientSocket.close()
|
||||||
|
sys.exit(0)
|
||||||
|
|
||||||
if config.loggingMode == 'rabbitmq':
|
else:
|
||||||
|
|
||||||
import rabbitmq
|
if os.path.exists(socketFile):
|
||||||
|
os.remove(socketFile)
|
||||||
|
|
||||||
rabbitmq = rabbitmq.rabbitMQClient(config.rabbitmqca,config.rabbitmqcacert,config.rabbitmqcakey,config.rabbitmqHost,config.rabbitmqPort,config.rabbitmqRoutingKey)
|
stop_event = threading.Event()
|
||||||
rabbitmq.publish(hostname + '|' + socket.gethostbyname(socket.gethostname()) + '|' + str(logTime) + '|' + 'ipBlock' + '|' + str(blockedIPAddress) + '|' + str(jail) + '|' + str(live))
|
dataBuffer = []
|
||||||
|
|
||||||
|
def publishData(stop_event):
|
||||||
|
|
||||||
|
if config.loggingMode = 'rabbitmq':
|
||||||
|
|
||||||
|
import rabbitmq
|
||||||
|
rabbitmq = rabbitmq.rabbitMQClient(config.rabbitmqca,config.rabbitmqcacert,config.rabbitmqcakey,config.rabbitmqHost,config.rabbitmqPort,config.rabbitmqRoutingKey)
|
||||||
|
|
||||||
|
while not (stop_event.is_set()):
|
||||||
|
if dataBuffer:
|
||||||
|
data = dataBuffer.pop(0)
|
||||||
|
|
||||||
|
if config.loggingMode = 'rabbitmq':
|
||||||
|
rabbitmq.publish(f"{data}")
|
||||||
|
else:
|
||||||
|
print("Not yet implemented")
|
||||||
|
|
||||||
|
def cleanup(signum, frame):
|
||||||
|
|
||||||
|
print("Signal received, shutting down...")
|
||||||
|
stop_event.set()
|
||||||
|
|
||||||
|
signal.signal(signal.SIGTERM, cleanup)
|
||||||
|
signal.signal(signal.SIGINT, cleanup)
|
||||||
|
|
||||||
|
def server(stop_event):
|
||||||
|
|
||||||
|
serverSocket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||||
|
serverSocket.bind(socketFile)
|
||||||
|
serverSocket.listen(50)
|
||||||
|
|
||||||
|
while not (stop_event.is_set()):
|
||||||
|
|
||||||
|
readable, _, _ = select.select([serverSocket], [], [], 1.0)
|
||||||
|
|
||||||
|
if readable:
|
||||||
|
clientSocket, _ = serverSocket.accept()
|
||||||
|
data = clientSocket.recv(1024).decode('utf-8')
|
||||||
|
|
||||||
|
if data:
|
||||||
|
|
||||||
|
dataBuffer.append(data)
|
||||||
|
print(data)
|
||||||
|
|
||||||
|
clientSocket.close()
|
||||||
|
|
||||||
|
publishThread = threading.Thread(target=publishData, args=(stop_event,))
|
||||||
|
serverThread = threading.Thread(target=server, args=(stop_event,))
|
||||||
|
|
||||||
|
publishThread.start()
|
||||||
|
serverThread.start()
|
||||||
|
|
||||||
|
publishThread.join()
|
||||||
|
serverThread.join()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
Loading…
Reference in a new issue