mirror of
https://github.com/jokob-sk/NetAlertX.git
synced 2026-04-10 20:22:02 -07:00
Coderabit changes
This commit is contained in:
@@ -76,9 +76,6 @@ from .mcp_routes import mcp_bp # noqa: E402 [flake8 lint suppression]
|
|||||||
|
|
||||||
# Flask application
|
# Flask application
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
# ... (CORS settings) ...
|
|
||||||
|
|
||||||
# ... (rest of file) ...
|
|
||||||
|
|
||||||
# Register Blueprints
|
# Register Blueprints
|
||||||
app.register_blueprint(tools_bp, url_prefix='/api/tools')
|
app.register_blueprint(tools_bp, url_prefix='/api/tools')
|
||||||
@@ -111,12 +108,14 @@ CORS(
|
|||||||
def log_request_info():
|
def log_request_info():
|
||||||
"""Log details of every incoming request."""
|
"""Log details of every incoming request."""
|
||||||
# Filter out noisy requests if needed, but user asked for drastic logging
|
# Filter out noisy requests if needed, but user asked for drastic logging
|
||||||
mylog("none", [f"[HTTP] {request.method} {request.path} from {request.remote_addr}"])
|
mylog("verbose", [f"[HTTP] {request.method} {request.path} from {request.remote_addr}"])
|
||||||
mylog("none", [f"[HTTP] Headers: {dict(request.headers)}"])
|
# Filter sensitive headers before logging
|
||||||
|
safe_headers = {k: v for k, v in request.headers if k.lower() not in ('authorization', 'cookie', 'x-api-key')}
|
||||||
|
mylog("debug", [f"[HTTP] Headers: {safe_headers}"])
|
||||||
if request.method == "POST":
|
if request.method == "POST":
|
||||||
# Be careful with large bodies, but log first 1000 chars
|
# Be careful with large bodies, but log first 1000 chars
|
||||||
data = request.get_data(as_text=True)
|
data = request.get_data(as_text=True)
|
||||||
mylog("none", [f"[HTTP] Body: {data[:1000]}"])
|
mylog("debug", [f"[HTTP] Body length: {len(data)} chars"])
|
||||||
|
|
||||||
|
|
||||||
@app.errorhandler(404)
|
@app.errorhandler(404)
|
||||||
|
|||||||
@@ -5,7 +5,9 @@ import uuid
|
|||||||
import queue
|
import queue
|
||||||
import requests
|
import requests
|
||||||
import threading
|
import threading
|
||||||
|
import logging
|
||||||
from flask import Blueprint, request, Response, stream_with_context, jsonify
|
from flask import Blueprint, request, Response, stream_with_context, jsonify
|
||||||
|
from helper import get_setting_value
|
||||||
|
|
||||||
mcp_bp = Blueprint('mcp', __name__)
|
mcp_bp = Blueprint('mcp', __name__)
|
||||||
|
|
||||||
@@ -16,7 +18,9 @@ sessions_lock = threading.Lock()
|
|||||||
# Cache for OpenAPI spec to avoid fetching on every request
|
# Cache for OpenAPI spec to avoid fetching on every request
|
||||||
openapi_spec_cache = None
|
openapi_spec_cache = None
|
||||||
|
|
||||||
API_BASE_URL = "http://localhost:20212/api/tools"
|
BACKEND_PORT = get_setting_value("GRAPHQL_PORT")
|
||||||
|
|
||||||
|
API_BASE_URL = f"http://localhost:{BACKEND_PORT}/api/tools"
|
||||||
|
|
||||||
|
|
||||||
def get_openapi_spec():
|
def get_openapi_spec():
|
||||||
@@ -28,7 +32,7 @@ def get_openapi_spec():
|
|||||||
try:
|
try:
|
||||||
# Fetch from local server
|
# Fetch from local server
|
||||||
# We use localhost because this code runs on the server
|
# We use localhost because this code runs on the server
|
||||||
response = requests.get(f"{API_BASE_URL}/openapi.json")
|
response = requests.get(f"{API_BASE_URL}/openapi.json", timeout=10)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
openapi_spec_cache = response.json()
|
openapi_spec_cache = response.json()
|
||||||
return openapi_spec_cache
|
return openapi_spec_cache
|
||||||
@@ -61,7 +65,11 @@ def map_openapi_to_mcp_tools(spec):
|
|||||||
content = details["requestBody"].get("content", {})
|
content = details["requestBody"].get("content", {})
|
||||||
if "application/json" in content:
|
if "application/json" in content:
|
||||||
schema = content["application/json"].get("schema", {})
|
schema = content["application/json"].get("schema", {})
|
||||||
tool["inputSchema"] = schema
|
tool["inputSchema"] = schema.copy()
|
||||||
|
if "properties" not in tool["inputSchema"]:
|
||||||
|
tool["inputSchema"]["properties"] = {}
|
||||||
|
if "required" not in tool["inputSchema"]:
|
||||||
|
tool["inputSchema"]["required"] = []
|
||||||
|
|
||||||
# Extract parameters from 'parameters' list (query/path params) - simplistic support
|
# Extract parameters from 'parameters' list (query/path params) - simplistic support
|
||||||
if "parameters" in details:
|
if "parameters" in details:
|
||||||
@@ -145,15 +153,16 @@ def process_mcp_request(data):
|
|||||||
headers = {
|
headers = {
|
||||||
"Content-Type": "application/json"
|
"Content-Type": "application/json"
|
||||||
}
|
}
|
||||||
|
|
||||||
if "Authorization" in request.headers:
|
if "Authorization" in request.headers:
|
||||||
headers["Authorization"] = request.headers["Authorization"]
|
headers["Authorization"] = request.headers["Authorization"]
|
||||||
|
|
||||||
url = f"{API_BASE_URL}{target_path}"
|
url = f"{API_BASE_URL}{target_path}"
|
||||||
|
|
||||||
if target_method == "POST":
|
if target_method == "POST":
|
||||||
api_res = requests.post(url, json=tool_args, headers=headers)
|
api_res = requests.post(url, json=tool_args, headers=headers, timeout=30)
|
||||||
elif target_method == "GET":
|
elif target_method == "GET":
|
||||||
api_res = requests.get(url, params=tool_args, headers=headers)
|
api_res = requests.get(url, params=tool_args, headers=headers, timeout=30)
|
||||||
else:
|
else:
|
||||||
api_res = None
|
api_res = None
|
||||||
|
|
||||||
@@ -236,8 +245,9 @@ def handle_sse():
|
|||||||
else:
|
else:
|
||||||
# Notification or no response needed
|
# Notification or no response needed
|
||||||
return "", 202
|
return "", 202
|
||||||
except Exception:
|
except Exception as e:
|
||||||
pass
|
# Log but don't fail - malformed requests shouldn't crash the endpoint
|
||||||
|
logging.getLogger(__name__).debug(f"SSE POST processing error: {e}")
|
||||||
|
|
||||||
return jsonify({"status": "ok", "message": "MCP SSE endpoint active"}), 200
|
return jsonify({"status": "ok", "message": "MCP SSE endpoint active"}), 200
|
||||||
|
|
||||||
|
|||||||
@@ -208,7 +208,8 @@ def get_open_ports():
|
|||||||
cmd,
|
cmd,
|
||||||
capture_output=True,
|
capture_output=True,
|
||||||
text=True,
|
text=True,
|
||||||
check=True
|
check=True,
|
||||||
|
timeout=120
|
||||||
)
|
)
|
||||||
|
|
||||||
# Parse output for open ports
|
# Parse output for open ports
|
||||||
@@ -388,7 +389,7 @@ def wol_wake_device():
|
|||||||
try:
|
try:
|
||||||
# Using wakeonlan command
|
# Using wakeonlan command
|
||||||
result = subprocess.run(
|
result = subprocess.run(
|
||||||
["wakeonlan", mac], capture_output=True, text=True, check=True
|
["wakeonlan", mac], capture_output=True, text=True, check=True, timeout=10
|
||||||
)
|
)
|
||||||
return jsonify(
|
return jsonify(
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user