monutil/reportIPBlock.py

150 lines
4.9 KiB
Python
Raw Normal View History

2024-08-21 18:16:37 +00:00
#!/usr/bin/python3
# MIT License
# Copyright (c) 2024 Thomas Williams - https://git.server.wales/thomas
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
def main():
import socket
import os
import config
import datetime
import signal
import threading
import log
import sys
import select
2024-08-20 15:36:17 +00:00
import time
socketFile = '/etc/monutil/ip.socket'
if len(sys.argv) == 4:
hostname = socket.gethostname()
ipAddress = socket.gethostbyname(hostname)
blockedIPAddress = sys.argv[1]
jail = sys.argv[2]
live = int(sys.argv[3])
logTime = datetime.datetime.now()
clientSocket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
clientSocket.connect(socketFile)
data = f"{hostname}|{ipAddress}|{logTime}|ipBlock|{blockedIPAddress}|{jail}|{live}"
clientSocket.send(data.encode('utf-8'))
clientSocket.close()
sys.exit(0)
else:
if os.path.exists(socketFile):
os.remove(socketFile)
2024-08-20 10:04:23 +00:00
stopEvent = threading.Event()
failureEvent = threading.Event()
dataBuffer = []
2024-08-20 10:04:23 +00:00
def publishData(stopEvent):
try:
2024-08-20 10:04:23 +00:00
if config.loggingMode == 'rabbitmq':
2024-08-20 10:04:23 +00:00
import rabbitmq
rabbitmq = rabbitmq.rabbitMQClient(config.rabbitmqca,config.rabbitmqcacert,config.rabbitmqcakey,config.rabbitmqHost,config.rabbitmqPort,config.rabbitmqRoutingKey)
2024-08-20 10:04:23 +00:00
while not (stopEvent.is_set() and not failureEvent.is_set()):
2024-08-20 15:36:17 +00:00
time.sleep(1)
2024-08-20 10:04:23 +00:00
if dataBuffer:
data = dataBuffer.pop(0)
rabbitmq.publish(f"{data}")
2024-08-20 10:04:23 +00:00
2024-08-20 18:33:08 +00:00
else:
while not (stopEvent.is_set() and not failureEvent.is_set()):
time.sleep(1)
if dataBuffer:
data = dataBuffer.pop(0)
data = data.split('|')
manager = log.logsManager(config.sqlServer, config.sqlDatabase, config.sqlUsername, config.sqlPassword)
2024-08-21 09:38:13 +00:00
manager.insertIPBlock(data[0], data[1], data[4], data[5], data[6], data[2], config.ipinfoAPIToken)
2024-08-20 10:04:23 +00:00
except Exception:
failureEvent.set()
def cleanup(signum, frame):
print("Signal received, shutting down...")
2024-08-20 10:04:23 +00:00
stopEvent.set()
signal.signal(signal.SIGTERM, cleanup)
signal.signal(signal.SIGINT, cleanup)
2024-08-20 10:04:23 +00:00
def server(stopEvent):
2024-08-20 10:04:23 +00:00
try:
serverSocket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
serverSocket.bind(socketFile)
serverSocket.listen(50)
2024-08-20 10:04:23 +00:00
while not (stopEvent.is_set() and not failureEvent.is_set()):
2024-08-20 10:04:23 +00:00
readable, _, _ = select.select([serverSocket], [], [], 1.0)
2024-08-20 10:04:23 +00:00
if readable:
clientSocket, _ = serverSocket.accept()
data = clientSocket.recv(1024).decode('utf-8')
2024-08-20 10:04:23 +00:00
if data:
2024-08-20 10:04:23 +00:00
dataBuffer.append(data)
print(data)
2024-08-20 10:04:23 +00:00
clientSocket.close()
2024-08-20 10:04:23 +00:00
except Exception:
failureEvent.set()
publishThread = threading.Thread(target=publishData, args=(stopEvent,))
serverThread = threading.Thread(target=server, args=(stopEvent,))
2024-08-19 17:56:56 +00:00
publishThread.start()
serverThread.start()
2024-08-19 17:56:56 +00:00
publishThread.join()
serverThread.join()
2024-08-20 10:36:07 +00:00
if failureEvent.is_set():
2024-08-20 10:04:23 +00:00
print("One of the threads failed. Terminating")
sys.exit(1)
if __name__ == "__main__":
main()