2024-07-06 12:31:26 +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.
|
|
|
|
|
|
|
|
import pika
|
|
|
|
import ssl
|
2024-07-06 12:51:00 +00:00
|
|
|
import base64
|
2024-07-06 13:40:04 +00:00
|
|
|
import config
|
|
|
|
import log
|
|
|
|
import datetime
|
2024-07-08 13:23:16 +00:00
|
|
|
from retrying import retry
|
2024-07-06 12:31:26 +00:00
|
|
|
|
|
|
|
class rabbitMQClient:
|
|
|
|
|
|
|
|
def __init__(self, ca, cacert, cakey, rabbitmqHost, rabbitmqPort, routingKey):
|
|
|
|
|
|
|
|
self.ca = ca
|
|
|
|
self.cacert = cacert
|
|
|
|
self.cakey = cakey
|
|
|
|
self.rabbitmqHost = rabbitmqHost
|
|
|
|
self.rabbitmqPort = rabbitmqPort
|
|
|
|
self.routingKey = routingKey
|
|
|
|
|
2024-08-17 09:43:49 +00:00
|
|
|
@retry(stop_max_attempt_number=120, wait_fixed=1000)
|
2024-07-06 12:31:26 +00:00
|
|
|
def publish(self, message):
|
|
|
|
|
2024-07-08 13:23:16 +00:00
|
|
|
def publishMessage(conn):
|
|
|
|
|
|
|
|
ch = conn.channel()
|
|
|
|
|
|
|
|
ch.basic_publish(exchange='', routing_key=self.routingKey, body=message, properties=pika.BasicProperties(delivery_mode=pika.spec.PERSISTENT_DELIVERY_MODE))
|
|
|
|
|
2024-07-06 12:51:00 +00:00
|
|
|
message = base64.b64encode(message.encode('utf-8'))
|
|
|
|
|
2024-07-06 12:31:26 +00:00
|
|
|
context = ssl.create_default_context(
|
|
|
|
cafile=self.ca)
|
|
|
|
context.verify_mode = ssl.CERT_REQUIRED
|
|
|
|
context.load_cert_chain(self.cacert, self.cakey)
|
|
|
|
|
|
|
|
sslOptions = pika.SSLOptions(context, self.rabbitmqHost)
|
|
|
|
|
|
|
|
connParams = pika.ConnectionParameters(host=self.rabbitmqHost,port=self.rabbitmqPort, ssl_options=sslOptions, credentials=pika.credentials.ExternalCredentials())
|
|
|
|
|
|
|
|
with pika.BlockingConnection(connParams) as conn:
|
|
|
|
|
2024-07-08 13:23:16 +00:00
|
|
|
publishMessage(conn)
|
2024-07-06 13:40:04 +00:00
|
|
|
|
2024-08-16 12:21:42 +00:00
|
|
|
@retry(stop_max_attempt_number=120, wait_fixed=1000)
|
2024-07-06 13:40:04 +00:00
|
|
|
def retrieve(self):
|
|
|
|
|
|
|
|
def callback(ch, method, properties, body):
|
|
|
|
|
|
|
|
body = base64.b64decode(body)
|
|
|
|
body = body.decode('utf-8')
|
|
|
|
result = body.split('|')
|
|
|
|
|
|
|
|
if result[3] == 'cpumem':
|
2024-08-19 17:56:56 +00:00
|
|
|
|
2024-08-19 11:25:10 +00:00
|
|
|
result[2] = datetime.datetime.fromisoformat(result[2])
|
2024-07-06 13:40:04 +00:00
|
|
|
manager = log.logsManager(config.sqlServer, config.sqlDatabase, config.sqlUsername, config.sqlPassword)
|
|
|
|
manager.insertHostLog(result[0], result[1], result[2], result[4], result[5])
|
|
|
|
ch.basic_ack(delivery_tag=method.delivery_tag)
|
|
|
|
|
|
|
|
if result[3] == 'url':
|
2024-08-19 17:56:56 +00:00
|
|
|
|
2024-08-19 11:25:10 +00:00
|
|
|
result[2] = datetime.datetime.fromisoformat(result[2])
|
2024-07-06 13:40:04 +00:00
|
|
|
manager = log.logsManager(config.sqlServer, config.sqlDatabase, config.sqlUsername, config.sqlPassword)
|
|
|
|
manager.insertURLLog(result[0], result[1], result[2], result[4], result[5])
|
|
|
|
ch.basic_ack(delivery_tag=method.delivery_tag)
|
|
|
|
|
2024-08-19 17:08:36 +00:00
|
|
|
if result[3] == 'ipBlock':
|
2024-08-19 17:56:56 +00:00
|
|
|
|
2024-08-19 17:08:36 +00:00
|
|
|
result[2] = datetime.datetime.fromisoformat(result[2])
|
2024-08-19 17:56:56 +00:00
|
|
|
manager = log.logsManager(config.sqlServer, config.sqlDatabase, config.sqlUsername, config.sqlPassword)
|
2024-08-19 17:08:36 +00:00
|
|
|
manager.insertIPBlock(result[0], result[1], result[4], result[5], result[6], result[2], config.ipinfoAPIToken)
|
2024-08-19 22:41:41 +00:00
|
|
|
ch.basic_ack(delivery_tag=method.delivery_tag)
|
2024-08-19 17:08:36 +00:00
|
|
|
|
2024-07-06 13:40:04 +00:00
|
|
|
context = ssl.create_default_context(
|
|
|
|
cafile=self.ca)
|
|
|
|
context.verify_mode = ssl.CERT_REQUIRED
|
|
|
|
context.load_cert_chain(self.cacert, self.cakey)
|
|
|
|
|
|
|
|
sslOptions = pika.SSLOptions(context, self.rabbitmqHost)
|
|
|
|
|
|
|
|
connParams = pika.ConnectionParameters(host=self.rabbitmqHost,port=self.rabbitmqPort, ssl_options=sslOptions, credentials=pika.credentials.ExternalCredentials())
|
|
|
|
|
|
|
|
connection = pika.BlockingConnection(connParams)
|
|
|
|
|
|
|
|
channel = connection.channel()
|
|
|
|
|
|
|
|
channel.basic_consume(queue='monutil', on_message_callback=callback, auto_ack=False)
|
|
|
|
|
|
|
|
channel.start_consuming()
|