This commit is contained in:
pika 2025-03-31 00:35:42 +02:00
parent 30e9c9328e
commit 254593d260
20 changed files with 156 additions and 65 deletions

View file

@ -1,4 +1,13 @@
from flask import Blueprint, jsonify, request, abort, current_app, render_template, redirect, url_for
from flask import (
Blueprint,
jsonify,
request,
abort,
current_app,
render_template,
redirect,
url_for,
)
from flask_login import login_required
from app.core.models import Subnet, Server, App, Port
from app.core.extensions import db
@ -277,59 +286,77 @@ def suggest_port(server_id):
def add_app_port(app_id):
"""Add a port to an application"""
app = App.query.get_or_404(app_id)
try:
port_number = request.form.get("port_number")
protocol = request.form.get("protocol", "TCP")
description = request.form.get("description", "")
# Validate port data
valid, clean_port, error = validate_port_data(port_number, protocol, description)
valid, clean_port, error = validate_port_data(
port_number, protocol, description
)
if not valid:
flash(error, "danger")
return redirect(url_for("dashboard.app_view", app_id=app_id)) if not request.is_xhr else jsonify({"success": False, "error": error}), 400
return (
redirect(url_for("dashboard.app_view", app_id=app_id))
if not request.is_xhr
else jsonify({"success": False, "error": error})
), 400
# Check if port already exists
existing_port = Port.query.filter_by(app_id=app_id, port_number=clean_port).first()
existing_port = Port.query.filter_by(
app_id=app_id, port_number=clean_port
).first()
if existing_port:
error_msg = f"Port {clean_port} already exists for this application"
flash(error_msg, "warning")
return redirect(url_for("dashboard.app_view", app_id=app_id)) if not request.is_xhr else jsonify({"success": False, "error": error_msg}), 400
return (
redirect(url_for("dashboard.app_view", app_id=app_id))
if not request.is_xhr
else jsonify({"success": False, "error": error_msg})
), 400
# Create new port
new_port = Port(
app_id=app_id,
port_number=clean_port,
protocol=protocol,
description=description
description=description,
)
db.session.add(new_port)
db.session.commit()
success_msg = f"Port {clean_port}/{protocol} added successfully"
flash(success_msg, "success")
# If it's a regular form submission (not AJAX), redirect
if not request.is_xhr and not request.is_json:
return redirect(url_for("dashboard.app_view", app_id=app_id))
# Otherwise return JSON for API/AJAX calls
return jsonify({
"success": True,
"message": success_msg,
"port": {
"id": new_port.id,
"number": new_port.port_number,
"protocol": new_port.protocol,
"description": new_port.description
return jsonify(
{
"success": True,
"message": success_msg,
"port": {
"id": new_port.id,
"number": new_port.port_number,
"protocol": new_port.protocol,
"description": new_port.description,
},
}
})
)
except Exception as e:
db.session.rollback()
flash(f"Error: {str(e)}", "danger")
return redirect(url_for("dashboard.app_view", app_id=app_id)) if not request.is_xhr else jsonify({"success": False, "error": str(e)}), 500
return (
redirect(url_for("dashboard.app_view", app_id=app_id))
if not request.is_xhr
else jsonify({"success": False, "error": str(e)})
), 500
@bp.route("/app/<int:app_id>/ports", methods=["GET"])
@ -362,19 +389,21 @@ def delete_app_port(app_id, port_id):
"""Delete a port from an application"""
app = App.query.get_or_404(app_id)
port = Port.query.get_or_404(port_id)
if port.app_id != app.id:
flash("Port does not belong to this application", "danger")
return redirect(url_for("dashboard.app_view", app_id=app_id))
try:
db.session.delete(port)
db.session.commit()
flash(f"Port {port.port_number}/{port.protocol} deleted successfully", "success")
flash(
f"Port {port.port_number}/{port.protocol} deleted successfully", "success"
)
except Exception as e:
db.session.rollback()
flash(f"Error deleting port: {str(e)}", "danger")
return redirect(url_for("dashboard.app_view", app_id=app_id))