#!/usr/bin/env python import json import subprocess import os import sys import hashlib import hmac # Register NetAlertX directories INSTALL_PATH = os.getenv('NETALERTX_APP', '/app') sys.path.extend([f"{INSTALL_PATH}/front/plugins", f"{INSTALL_PATH}/server"]) import conf from const import logPath, confFileName from plugin_helper import Plugin_Objects, handleEmpty from logger import mylog, Logger from helper import timeNowTZ, get_setting_value, write_file from models.notification_instance import NotificationInstance from database import DB from pytz import timezone # Make sure the TIMEZONE for logging is correct conf.tz = timezone(get_setting_value('TIMEZONE')) # Make sure log level is initialized correctly Logger(get_setting_value('LOG_LEVEL')) pluginName = 'WEBHOOK' LOG_PATH = logPath + '/plugins' RESULT_FILE = os.path.join(LOG_PATH, f'last_result.{pluginName}.log') def main(): mylog('verbose', [f'[{pluginName}](publisher) In script']) # Check if basic config settings supplied if check_config() == False: mylog('none', [f'[{pluginName}] ⚠ ERROR: Publisher notification gateway not set up correctly. Check your {confFileName} {pluginName}_* variables.']) return # Create a database connection db = DB() # instance of class DB db.open() # Initialize the Plugin obj output file plugin_objects = Plugin_Objects(RESULT_FILE) # Create a NotificationInstance instance notifications = NotificationInstance(db) # Retrieve new notifications new_notifications = notifications.getNew() # Process the new notifications (see the Notifications DB table for structure or check the /php/server/query_json.php?file=table_notifications.json endpoint) for notification in new_notifications: # Send notification response_stdout, response_stderr = send(notification["Text"], notification["HTML"], notification["JSON"]) # Log result plugin_objects.add_object( primaryId = pluginName, secondaryId = timeNowTZ(), watched1 = notification["GUID"], watched2 = handleEmpty(response_stdout), watched3 = handleEmpty(response_stderr), watched4 = 'null', extra = 'null', foreignKey = notification["GUID"] ) plugin_objects.write_result_file() #------------------------------------------------------------------------------- def check_config(): if get_setting_value('WEBHOOK_URL') == '': return False else: return True #------------------------------------------------------------------------------- def send (text_data, html_data, json_data): response_stderr = '' response_stdout = '' # limit = 1024 * 1024 # 1MB limit (1024 bytes * 1024 bytes = 1MB) limit = get_setting_value('WEBHOOK_SIZE') payloadType = get_setting_value('WEBHOOK_PAYLOAD') endpointUrl = get_setting_value('WEBHOOK_URL') secret = get_setting_value('WEBHOOK_SECRET') requestMethod = get_setting_value('WEBHOOK_REQUEST_METHOD') # use data type based on specified payload type if payloadType == 'json': # In this code, the truncate_json function is used to recursively traverse the JSON object # and remove nodes that exceed the size limit. It checks the size of each node's JSON representation # using json.dumps and includes only the nodes that are within the limit. json_str = json.dumps(json_data) if len(json_str) <= limit: payloadData = json_data else: def truncate_json(obj): if isinstance(obj, dict): return { key: truncate_json(value) for key, value in obj.items() if len(json.dumps(value)) <= limit } elif isinstance(obj, list): return [ truncate_json(item) for item in obj if len(json.dumps(item)) <= limit ] else: return obj payloadData = truncate_json(json_data) if payloadType == 'html': if len(html_data) > limit: payloadData = html_data[:limit] + "