diff --git a/app/functions/utils.py b/app/functions/utils.py index ce58b7e..841bf6b 100644 --- a/app/functions/utils.py +++ b/app/functions/utils.py @@ -64,6 +64,14 @@ def send_webhook(webhook_url, text_data): response.raise_for_status() +# Get client IP address +@exception_handler +def get_client_ip(request): + if not request.headers.getlist("X-Forwarded-For"): + return request.remote_addr + return request.headers.getlist("X-Forwarded-For")[0] + + # Establish connection to network device def establish_connection(device_config): try: diff --git a/app/views/main.py b/app/views/main.py index e0c585c..c1583d5 100644 --- a/app/views/main.py +++ b/app/views/main.py @@ -3,7 +3,7 @@ from flask import ( ) import logging -from app.functions.utils import exception_handler, load_yaml, execute_command, send_webhook +from app.functions.utils import exception_handler, load_yaml, send_webhook, get_client_ip, execute_command, logger = logging.getLogger(__name__) @@ -58,6 +58,7 @@ def execute(): # Send a webhook notification with client IP and command output if not result['error'] and webhook: - send_webhook(webhook['url'], f"Client IP: `{request.remote_addr}`\nDevice: `{input_device}`\nCommand: `{input_command} -{ip_version} {input_target}`") + client_ip = get_client_ip(request) + send_webhook(webhook['url'], f"Client IP: `{client_ip}`\nDevice: `{input_device}`\nCommand: `{input_command} -{ip_version} {input_target}`") return result