This commit is contained in:
pika 2025-04-03 16:58:01 +02:00
parent 2b36992be1
commit 25087d055c
16 changed files with 1394 additions and 816 deletions

View file

@ -35,62 +35,102 @@ def validate_app_data(name, server_id, existing_app_id=None):
return True, None
def is_port_in_use(port_number, protocol, server_id, exclude_app_id=None):
def is_port_in_use(port, server_id, exclude_app_id=None):
"""
Check if a port+protocol combination is already in use by any application on the server
"""
from sqlalchemy import and_
# Join App and Port models to find ports used by apps on this server
query = db.session.query(Port, App).join(App).filter(
Port.port_number == port_number,
Port.protocol == protocol,
App.server_id == server_id
)
# # Exclude the current app if editing
# if exclude_app_id:
# query = query.filter(App.id != exclude_app_id)
result = query.first()
if result:
return True, result.App.name
return False, None
def validate_port_data(port_number, protocol, description, server_id=None, app_id=None):
"""
Validate port data for an application
Check if a port is already in use on a server
Args:
port_number: The port number to validate
protocol: The protocol (TCP/UDP)
description: Port description
server_id: ID of the server
app_id: ID of the application (for excluding the current app when checking conflicts)
port: The port number to check
server_id: The ID of the server
exclude_app_id: Optional app ID to exclude from the check (for editing an app)
Returns:
bool: True if port is in use, False otherwise
"""
from app.core.models import App, Port
# Get all apps on this server
apps_on_server = App.query.filter_by(server_id=server_id).all()
for app in apps_on_server:
# Skip the app we're editing
if exclude_app_id and app.id == int(exclude_app_id):
continue
# Check if this app uses the port - use port_number rather than number
for app_port in app.ports:
if int(app_port.port_number) == int(port): # Use port_number here
return True
return False
def validate_port_data(ports, descriptions=None, server_id=None, exclude_app_id=None, protocol=None):
"""
Validate port data - works with both the API and form submissions
Args:
ports: List of port numbers or a single port number string
descriptions: List of port descriptions or a single description
server_id: The server ID
exclude_app_id: Optional app ID to exclude from port conflict check
protocol: Optional protocol (for API validation)
Returns:
Tuple of (valid, clean_port, error_message)
For form validation: Error message string or None if valid
For API validation: (valid, clean_port, error_message) tuple
"""
# Check if port number is provided
if not port_number:
return False, None, "Port number is required"
try:
clean_port = int(port_number)
except ValueError:
return False, None, "Port number must be a valid integer"
if clean_port < 1 or clean_port > 65535:
return False, None, "Port number must be between 1 and 65535"
# Always check for port conflicts
in_use, app_name = is_port_in_use(clean_port, protocol, server_id, app_id)
if in_use:
return False, clean_port, f"Port {clean_port}/{protocol} is already in use by application '{app_name}'"
return True, clean_port, None
# Handle the API call format
if protocol is not None:
# This is the API validation path
try:
port = int(ports)
if port < 1 or port > 65535:
return False, None, f"Port {port} is out of valid range (1-65535)"
# Check if port is already in use
if is_port_in_use(port, server_id, exclude_app_id):
return False, None, f"Port {port} is already in use on this server"
return True, port, None
except ValueError:
return False, None, "Invalid port number"
# Handle the form submission format (list of ports)
seen_ports = set()
# Make sure ports is a list
if not isinstance(ports, list):
ports = [ports]
# Make sure descriptions is a list (or empty list)
if descriptions is None:
descriptions = []
elif not isinstance(descriptions, list):
descriptions = [descriptions]
for i, port_str in enumerate(ports):
if not port_str: # Skip empty port entries
continue
try:
port = int(port_str)
if port < 1 or port > 65535:
return f"Port {port} is out of valid range (1-65535)"
# Check for duplicate ports in the submitted data
if port in seen_ports:
return f"Duplicate port {port} in submission"
seen_ports.add(port)
# Check if port is already in use on this server
if is_port_in_use(port, server_id, exclude_app_id):
return f"Port {port} is already in use on this server"
except ValueError:
return f"Invalid port number: {port_str}"
return None
def process_app_ports(app_id, port_data, server_id=None):
@ -128,7 +168,7 @@ def process_app_ports(app_id, port_data, server_id=None):
# Validate the port data
valid, clean_port, error = validate_port_data(
port_number, protocol, description, server_id, app_id
[port_number], [description], server_id, app_id
)
if not valid: