Преглед на файлове

feat: refactor admin routes and services for ACL and blacklist management

Manuel преди 9 месеца
родител
ревизия
1eb99333f0
променени са 6 файла, в които са добавени 651 реда и са изтрити 324 реда
  1. 54 324
      routes/admin_routes.py
  2. 158 0
      services/acls_service.py
  3. 59 0
      services/admin_helpers.py
  4. 84 0
      services/blacklist_service.py
  5. 139 0
      services/delay_pools_service.py
  6. 157 0
      services/http_access_service.py

+ 54 - 324
routes/admin_routes.py

@@ -23,63 +23,32 @@ admin_bp = Blueprint("admin", __name__)
 
 # Global manager instance
 config_manager = SquidConfigManager()
+from services.admin_helpers import (
+    load_env_vars,
+    save_env_vars,
+    get_table_row_count,
+    get_table_size,
+)
 
-
-def load_env_vars():
-    env_vars = {}
-    env_file = os.path.join(os.getcwd(), ".env")
-    if os.path.exists(env_file):
-        with open(env_file) as f:
-            for line in f:
-                line = line.strip()
-                if line and "=" in line:
-                    key, value = line.split("=", 1)
-                    env_vars[key] = value.strip('"')
-    return env_vars
-
-
-def save_env_vars(env_vars):
-    env_file = os.path.join(os.getcwd(), ".env")
-    with open(env_file, "w") as f:
-        for key, value in env_vars.items():
-            f.write(f'{key}="{value}"\n')
-
-
-def get_table_row_count(session, engine, table_name):
-    metadata = MetaData()
-    table = Table(table_name, metadata, autoload_with=engine)
-
-    return session.execute(select(func.count()).select_from(table)).scalar_one()
-
-
-def get_table_size(session, db_type, table_name):
-    if db_type == "SQLITE":
-        result = session.execute(
-            text("SELECT SUM(pgsize) FROM dbstat WHERE name = :name"),
-            {"name": table_name},
-        )
-        return result.scalar() or 0
-
-    if db_type in ("MYSQL", "MARIADB"):
-        result = session.execute(
-            text("""
-                SELECT data_length + index_length
-                FROM information_schema.tables
-                WHERE table_name = :name
-                AND table_schema = DATABASE()
-            """),
-            {"name": table_name},
-        )
-        return result.scalar() or 0
-
-    if db_type in ("POSTGRES", "POSTGRESQL"):
-        result = session.execute(
-            text("SELECT pg_total_relation_size(:name)"),
-            {"name": table_name},
-        )
-        return result.scalar() or 0
-
-    return 0
+from services.blacklist_service import (
+    test_pihole_connection,
+    import_domains_from_file,
+    import_domains_from_url,
+    merge_and_save_blacklist,
+    save_custom_list,
+)
+from services.acls_service import add_acl as service_add_acl, edit_acl as service_edit_acl, delete_acl as service_delete_acl
+from services.http_access_service import (
+    add_http_access as service_add_http_access,
+    edit_http_access as service_edit_http_access,
+    delete_http_access as service_delete_http_access,
+    move_http_access as service_move_http_access,
+)
+from services.delay_pools_service import (
+    add_delay_pool as service_add_delay_pool,
+    edit_delay_pool as service_edit_delay_pool,
+    delete_delay_pool as service_delete_delay_pool,
+)
 
 
 @admin_bp.route("/")
@@ -174,59 +143,11 @@ def add_acl():
     options = request.form.getlist("options[]")
     comment = request.form.get("comment", "").strip()
 
-    if not name or not acl_type or not values:
-        flash("Debe proporcionar nombre, tipo y al menos un valor para la ACL", "error")
-        return redirect(url_for("admin.manage_acls"))
-
-    # Build ACL line: acl name [options] type values...
-    acl_parts = ["acl", name]
-    if options:
-        acl_parts.extend(options)
-    acl_parts.append(acl_type)
-    acl_parts.extend(values)
-    new_acl = " ".join(acl_parts)
-
-    # Check if using modular configuration
-    if config_manager.is_modular:
-        acl_content = config_manager.read_modular_config("100_acls.conf")
-        if acl_content is not None:
-            lines = acl_content.split("\n")
-
-            # Add comment if provided
-            if comment:
-                lines.append(f"# {comment}")
-
-            # Add the new ACL
-            lines.append(new_acl)
-
-            new_content = "\n".join(lines)
-            if config_manager.save_modular_config("100_acls.conf", new_content):
-                flash(f"ACL '{name}' agregada exitosamente", "success")
-            else:
-                flash("Error al guardar la ACL", "error")
-            return redirect(url_for("admin.manage_acls"))
-
-    # Fallback to main config
-    lines = config_manager.config_content.split("\n")
-    acl_section_end = -1
-    for i, line in enumerate(lines):
-        if line.strip().startswith("acl "):
-            acl_section_end = i
-
-    if acl_section_end != -1:
-        if comment:
-            lines.insert(acl_section_end + 1, f"# {comment}")
-            lines.insert(acl_section_end + 2, new_acl)
-        else:
-            lines.insert(acl_section_end + 1, new_acl)
-    else:
-        if comment:
-            lines.append(f"# {comment}")
-        lines.append(new_acl)
-
-    new_content = "\n".join(lines)
-    config_manager.save_config(new_content)
-    flash(f"ACL '{name}' agregada exitosamente", "success")
+    success, message = service_add_acl(name, acl_type, values, options, comment, config_manager)
+    flash(message, "success" if success else "error")
+    return redirect(url_for("admin.manage_acls"))
+    success, message = service_add_acl(name, acl_type, values, options, comment, config_manager)
+    flash(message, "success" if success else "error")
     return redirect(url_for("admin.manage_acls"))
 
 
@@ -241,94 +162,14 @@ def edit_acl():
     options = request.form.getlist("options[]")
     comment = request.form.get("comment", "").strip()
 
-    if not acl_id or not new_name or not acl_type or not values:
-        flash("Datos incompletos para editar la ACL", "error")
-        return redirect(url_for("admin.manage_acls"))
-
     try:
         acl_index = int(acl_id)
-        acls = config_manager.get_acls()
-
-        if 0 <= acl_index < len(acls):
-            target_acl = acls[acl_index]
-            # line_number from parser is 1-based, convert to 0-based index
-            target_line = target_acl["line_number"] - 1
-
-            # Build new ACL line
-            acl_parts = ["acl", new_name]
-            if options:
-                acl_parts.extend(options)
-            acl_parts.append(acl_type)
-            acl_parts.extend(values)
-            new_acl_line = " ".join(acl_parts)
-
-            # Check if using modular configuration
-            if config_manager.is_modular:
-                acl_content = config_manager.read_modular_config("100_acls.conf")
-                if acl_content is not None:
-                    lines = acl_content.split("\n")
-
-                    # Replace the ACL line (line_number is 1-based, list index is 0-based)
-                    if 0 <= target_line < len(lines):
-                        # Check if there's a comment before this ACL
-                        has_comment = target_line > 0 and lines[
-                            target_line - 1
-                        ].strip().startswith("#")
-
-                        # Replace the ACL line
-                        lines[target_line] = new_acl_line
-
-                        # Handle comment
-                        if has_comment:
-                            if comment:
-                                # Update existing comment
-                                lines[target_line - 1] = f"# {comment}"
-                            else:
-                                # Remove existing comment
-                                lines.pop(target_line - 1)
-                        else:
-                            if comment:
-                                # Insert new comment before the ACL
-                                lines.insert(target_line, f"# {comment}")
-
-                        new_content = "\n".join(lines)
-                        if config_manager.save_modular_config(
-                            "100_acls.conf", new_content
-                        ):
-                            flash(
-                                f"ACL '{new_name}' actualizada exitosamente", "success"
-                            )
-                        else:
-                            flash("Error al guardar la ACL", "error")
-                    else:
-                        flash("Línea de ACL no encontrada", "error")
-                    return redirect(url_for("admin.manage_acls"))
-
-            # Fallback to main config
-            lines = config_manager.config_content.split("\n")
-            if 0 <= target_line < len(lines):
-                lines[target_line] = new_acl_line
-
-                # Handle comment
-                if target_line > 0 and lines[target_line - 1].strip().startswith("#"):
-                    if comment:
-                        lines[target_line - 1] = f"# {comment}"
-                    else:
-                        lines.pop(target_line - 1)
-                else:
-                    if comment:
-                        lines.insert(target_line, f"# {comment}")
-
-                new_content = "\n".join(lines)
-                config_manager.save_config(new_content)
-                flash(f"ACL '{new_name}' actualizada exitosamente", "success")
-            else:
-                flash("Línea de ACL no encontrada", "error")
-        else:
-            flash("ACL no encontrada", "error")
-    except (ValueError, IndexError) as e:
-        flash(f"Error al actualizar la ACL: {str(e)}", "error")
+    except (ValueError, TypeError):
+        flash("ID de ACL inválido", "error")
+        return redirect(url_for("admin.manage_acls"))
 
+    success, message = service_edit_acl(acl_index, new_name, acl_type, values, options, comment, config_manager)
+    flash(message, "success" if success else "error")
     return redirect(url_for("admin.manage_acls"))
 
 
@@ -337,73 +178,14 @@ def edit_acl():
 def delete_acl():
     """Delete an ACL using its ID (index) and line number."""
     acl_id = request.form.get("id")
-
     try:
         acl_index = int(acl_id)
-        acls = config_manager.get_acls()
-
-        if 0 <= acl_index < len(acls):
-            acl_to_delete = acls[acl_index]
-            # line_number from parser is 1-based, convert to 0-based index
-            target_line = acl_to_delete["line_number"] - 1
-
-            # Check if using modular configuration
-            if config_manager.is_modular:
-                acl_content = config_manager.read_modular_config("100_acls.conf")
-                if acl_content is not None:
-                    lines = acl_content.split("\n")
-
-                    # Check if there's a comment before this ACL (line_number is 1-based, we need 0-based)
-                    comment_to_remove = None
-                    if target_line > 0 and lines[target_line - 1].strip().startswith(
-                        "#"
-                    ):
-                        comment_to_remove = target_line - 1
-
-                    # Remove lines (comment + ACL or just ACL)
-                    new_lines = []
-                    for i, line in enumerate(lines):
-                        if i == target_line:
-                            continue  # Skip the ACL line
-                        if comment_to_remove is not None and i == comment_to_remove:
-                            continue  # Skip the comment line
-                        new_lines.append(line)
-
-                    new_content = "\n".join(new_lines)
-                    if config_manager.save_modular_config("100_acls.conf", new_content):
-                        flash(
-                            f"ACL '{acl_to_delete['name']}' eliminada exitosamente",
-                            "success",
-                        )
-                    else:
-                        flash("Error al eliminar la ACL", "error")
-                    return redirect(url_for("admin.manage_acls"))
-
-            # Fallback to main config
-            lines = config_manager.config_content.split("\n")
-
-            # Check if there's a comment before this ACL (convert to 0-based index)
-            comment_to_remove = None
-            if target_line > 0 and lines[target_line - 1].strip().startswith("#"):
-                comment_to_remove = target_line - 1
-
-            # Remove lines (comment + ACL or just ACL)
-            new_lines = []
-            for i, line in enumerate(lines):
-                if i == target_line:
-                    continue  # Skip the ACL line
-                if comment_to_remove is not None and i == comment_to_remove:
-                    continue  # Skip the comment line
-                new_lines.append(line)
-
-            new_content = "\n".join(new_lines)
-            config_manager.save_config(new_content)
-            flash(f"ACL '{acl_to_delete['name']}' eliminada exitosamente", "success")
-        else:
-            flash("ACL no encontrada", "error")
-    except (ValueError, IndexError):
-        flash("Error al eliminar la ACL", "error")
+    except (ValueError, TypeError):
+        flash("ID de ACL inválido", "error")
+        return redirect(url_for("admin.manage_acls"))
 
+    success, message = service_delete_acl(acl_index, config_manager)
+    flash(message, "success" if success else "error")
     return redirect(url_for("admin.manage_acls"))
 
 
@@ -439,31 +221,8 @@ def blacklist_test_connection():
     if not host:
         flash("Host de Pi-hole no proporcionado", "error")
         return redirect(url_for("admin.manage_blacklist"))
-
-    import requests
-
-    try:
-        url = host
-        # Normalize host to include scheme if missing
-        if not url.startswith("http://") and not url.startswith("https://"):
-            url = f"http://{url}"
-
-        # Try a basic request to the Pi-hole admin API endpoint
-        params = {}
-        headers = {}
-        if token:
-            # many Pi-hole setups accept 'Authorization' header or 'auth' param; try both
-            headers["Authorization"] = token
-            params["auth"] = token
-
-        resp = requests.get(f"{url}/admin/api.php", params=params, headers=headers, timeout=6)
-        if resp.status_code == 200:
-            flash("Conexión a Pi-hole exitosa", "success")
-        else:
-            flash(f"Respuesta inesperada de Pi-hole: {resp.status_code}", "error")
-    except Exception as e:
-        logger.exception("Error probando conexión Pi-hole")
-        flash(f"Error al conectar con Pi-hole: {str(e)}", "error")
+    success, msg = test_pihole_connection(host, token)
+    flash(msg, "success" if success else "error")
 
     return redirect(url_for("admin.manage_blacklist"))
 
@@ -482,26 +241,14 @@ def blacklist_sync():
 def blacklist_import():
     # Import domains from uploaded file or URL and append to BLACKLIST_DOMAINS
     env_vars = load_env_vars()
-    existing = [d.strip() for d in env_vars.get("BLACKLIST_DOMAINS", "").split(",") if d.strip()]
-
-    domains = set(existing)
+    domains = set()
 
     # Handle file upload
     uploaded = request.files.get("file")
     if uploaded and uploaded.filename:
         try:
-            content = uploaded.read().decode("utf-8", errors="ignore")
-            for line in content.splitlines():
-                line = line.strip()
-                if not line or line.startswith("#"):
-                    continue
-                # support hosts-like files and plain domains
-                if " " in line:
-                    parts = line.split()
-                    domain = parts[-1]
-                else:
-                    domain = line
-                domains.add(domain)
+            file_domains = import_domains_from_file(uploaded)
+            domains.update(file_domains)
             flash("Archivo importado correctamente", "success")
         except Exception as e:
             logger.exception("Error importando archivo de blacklist")
@@ -511,31 +258,16 @@ def blacklist_import():
     # Handle URL import
     url = request.form.get("url")
     if url:
-        import requests
-        try:
-            resp = requests.get(url, timeout=8)
-            if resp.status_code == 200:
-                for line in resp.text.splitlines():
-                    line = line.strip()
-                    if not line or line.startswith("#"):
-                        continue
-                    if " " in line:
-                        parts = line.split()
-                        domain = parts[-1]
-                    else:
-                        domain = line
-                    domains.add(domain)
-                flash("Lista importada desde URL correctamente", "success")
-            else:
-                flash(f"Error al descargar la lista: {resp.status_code}", "error")
-        except Exception as e:
-            logger.exception("Error descargando lista desde URL")
-            flash(f"Error descargando la lista: {str(e)}", "error")
+        ok, url_domains, err = import_domains_from_url(url)
+        if ok:
+            domains.update(url_domains)
+            flash("Lista importada desde URL correctamente", "success")
+        else:
+            flash(f"Error importando desde URL: {err}", "error")
 
-    # Save merged domains back to .env
+    # Merge with existing and save
     try:
-        env_vars["BLACKLIST_DOMAINS"] = ",".join(sorted(domains))
-        save_env_vars(env_vars)
+        merge_and_save_blacklist(env_vars, domains)
         flash("Blacklist actualizada exitosamente", "success")
     except Exception as e:
         logger.exception("Error guardando BLACKLIST_DOMAINS")
@@ -561,9 +293,7 @@ def blacklist_save_custom():
                 items.append(d)
 
     try:
-        env_vars = load_env_vars()
-        env_vars["BLACKLIST_DOMAINS"] = ",".join(sorted(set(items)))
-        save_env_vars(env_vars)
+        save_custom_list(items)
         flash("Lista personalizada guardada en BLACKLIST_DOMAINS", "success")
     except Exception as e:
         logger.exception("Error guardando lista personalizada")

+ 158 - 0
services/acls_service.py

@@ -0,0 +1,158 @@
+from loguru import logger
+
+
+def add_acl(name: str, acl_type: str, values: list, options: list, comment: str, config_manager) -> tuple[bool, str]:
+    if not name or not acl_type or not values:
+        return False, "Debe proporcionar nombre, tipo y al menos un valor para la ACL"
+
+    acl_parts = ["acl", name]
+    if options:
+        acl_parts.extend(options)
+    acl_parts.append(acl_type)
+    acl_parts.extend(values)
+    new_acl = " ".join(acl_parts)
+
+    try:
+        if config_manager.is_modular:
+            acl_content = config_manager.read_modular_config("100_acls.conf")
+            if acl_content is not None:
+                lines = acl_content.split("\n")
+                if comment:
+                    lines.append(f"# {comment}")
+                lines.append(new_acl)
+                new_content = "\n".join(lines)
+                if config_manager.save_modular_config("100_acls.conf", new_content):
+                    return True, f"ACL '{name}' agregada exitosamente"
+                else:
+                    return False, "Error al guardar la ACL en modular"
+
+        # Fallback to main config
+        lines = config_manager.config_content.split("\n")
+        acl_section_end = -1
+        for i, line in enumerate(lines):
+            if line.strip().startswith("acl "):
+                acl_section_end = i
+
+        if acl_section_end != -1:
+            if comment:
+                lines.insert(acl_section_end + 1, f"# {comment}")
+                lines.insert(acl_section_end + 2, new_acl)
+            else:
+                lines.insert(acl_section_end + 1, new_acl)
+        else:
+            if comment:
+                lines.append(f"# {comment}")
+            lines.append(new_acl)
+
+        new_content = "\n".join(lines)
+        config_manager.save_config(new_content)
+        return True, f"ACL '{name}' agregada exitosamente"
+    except Exception as e:
+        logger.exception("Error agregando ACL")
+        return False, str(e)
+
+
+def edit_acl(acl_index: int, new_name: str, acl_type: str, values: list, options: list, comment: str, config_manager) -> tuple[bool, str]:
+    try:
+        acls = config_manager.get_acls()
+        if not (0 <= acl_index < len(acls)):
+            return False, "ACL no encontrada"
+
+        target_acl = acls[acl_index]
+        target_line = target_acl["line_number"] - 1
+
+        acl_parts = ["acl", new_name]
+        if options:
+            acl_parts.extend(options)
+        acl_parts.append(acl_type)
+        acl_parts.extend(values)
+        new_acl_line = " ".join(acl_parts)
+
+        if config_manager.is_modular:
+            acl_content = config_manager.read_modular_config("100_acls.conf")
+            if acl_content is not None:
+                lines = acl_content.split("\n")
+                if 0 <= target_line < len(lines):
+                    has_comment = target_line > 0 and lines[target_line - 1].strip().startswith("#")
+                    lines[target_line] = new_acl_line
+                    if has_comment:
+                        if comment:
+                            lines[target_line - 1] = f"# {comment}"
+                        else:
+                            lines.pop(target_line - 1)
+                    else:
+                        if comment:
+                            lines.insert(target_line, f"# {comment}")
+                    new_content = "\n".join(lines)
+                    if config_manager.save_modular_config("100_acls.conf", new_content):
+                        return True, f"ACL '{new_name}' actualizada exitosamente"
+                    else:
+                        return False, "Error guardando ACL modular"
+
+        # Fallback to main config
+        lines = config_manager.config_content.split("\n")
+        if 0 <= target_line < len(lines):
+            lines[target_line] = new_acl_line
+            if target_line > 0 and lines[target_line - 1].strip().startswith("#"):
+                if comment:
+                    lines[target_line - 1] = f"# {comment}"
+                else:
+                    lines.pop(target_line - 1)
+            else:
+                if comment:
+                    lines.insert(target_line, f"# {comment}")
+            new_content = "\n".join(lines)
+            config_manager.save_config(new_content)
+            return True, f"ACL '{new_name}' actualizada exitosamente"
+        return False, "Línea de ACL no encontrada"
+    except Exception as e:
+        logger.exception("Error editando ACL")
+        return False, str(e)
+
+
+def delete_acl(acl_index: int, config_manager) -> tuple[bool, str]:
+    try:
+        acls = config_manager.get_acls()
+        if not (0 <= acl_index < len(acls)):
+            return False, "ACL no encontrada"
+
+        acl_to_delete = acls[acl_index]
+        target_line = acl_to_delete["line_number"] - 1
+
+        if config_manager.is_modular:
+            acl_content = config_manager.read_modular_config("100_acls.conf")
+            if acl_content is not None:
+                lines = acl_content.split("\n")
+                comment_to_remove = None
+                if target_line > 0 and lines[target_line - 1].strip().startswith("#"):
+                    comment_to_remove = target_line - 1
+                new_lines = []
+                for i, line in enumerate(lines):
+                    if i == target_line:
+                        continue
+                    if comment_to_remove is not None and i == comment_to_remove:
+                        continue
+                    new_lines.append(line)
+                new_content = "\n".join(new_lines)
+                if config_manager.save_modular_config("100_acls.conf", new_content):
+                    return True, f"ACL '{acl_to_delete['name']}' eliminada exitosamente"
+                else:
+                    return False, "Error al eliminar ACL modular"
+
+        lines = config_manager.config_content.split("\n")
+        comment_to_remove = None
+        if target_line > 0 and lines[target_line - 1].strip().startswith("#"):
+            comment_to_remove = target_line - 1
+        new_lines = []
+        for i, line in enumerate(lines):
+            if i == target_line:
+                continue
+            if comment_to_remove is not None and i == comment_to_remove:
+                continue
+            new_lines.append(line)
+        new_content = "\n".join(new_lines)
+        config_manager.save_config(new_content)
+        return True, f"ACL '{acl_to_delete['name']}' eliminada exitosamente"
+    except Exception as e:
+        logger.exception("Error eliminando ACL")
+        return False, str(e)

+ 59 - 0
services/admin_helpers.py

@@ -0,0 +1,59 @@
+import os
+from sqlalchemy import MetaData, Table, func, select, text
+
+
+def load_env_vars():
+    env_vars = {}
+    env_file = os.path.join(os.getcwd(), ".env")
+    if os.path.exists(env_file):
+        with open(env_file) as f:
+            for line in f:
+                line = line.strip()
+                if line and "=" in line:
+                    key, value = line.split("=", 1)
+                    env_vars[key] = value.strip('"')
+    return env_vars
+
+
+def save_env_vars(env_vars):
+    env_file = os.path.join(os.getcwd(), ".env")
+    with open(env_file, "w") as f:
+        for key, value in env_vars.items():
+            f.write(f'{key}="{value}"\n')
+
+
+def get_table_row_count(session, engine, table_name):
+    metadata = MetaData()
+    table = Table(table_name, metadata, autoload_with=engine)
+
+    return session.execute(select(func.count()).select_from(table)).scalar_one()
+
+
+def get_table_size(session, db_type, table_name):
+    if db_type == "SQLITE":
+        result = session.execute(
+            text("SELECT SUM(pgsize) FROM dbstat WHERE name = :name"),
+            {"name": table_name},
+        )
+        return result.scalar() or 0
+
+    if db_type in ("MYSQL", "MARIADB"):
+        result = session.execute(
+            text("""
+                SELECT data_length + index_length
+                FROM information_schema.tables
+                WHERE table_name = :name
+                AND table_schema = DATABASE()
+            """),
+            {"name": table_name},
+        )
+        return result.scalar() or 0
+
+    if db_type in ("POSTGRES", "POSTGRESQL"):
+        result = session.execute(
+            text("SELECT pg_total_relation_size(:name)"),
+            {"name": table_name},
+        )
+        return result.scalar() or 0
+
+    return 0

+ 84 - 0
services/blacklist_service.py

@@ -0,0 +1,84 @@
+import requests
+from loguru import logger
+
+from services.admin_helpers import load_env_vars, save_env_vars
+
+
+def test_pihole_connection(host: str, token: str | None = None) -> tuple[bool, str]:
+    if not host:
+        return False, "Host no proporcionado"
+
+    url = host
+    if not url.startswith("http://") and not url.startswith("https://"):
+        url = f"http://{url}"
+
+    params = {}
+    headers = {}
+    if token:
+        headers["Authorization"] = token
+        params["auth"] = token
+
+    try:
+        resp = requests.get(f"{url}/admin/api.php", params=params, headers=headers, timeout=6)
+        if resp.status_code == 200:
+            return True, "Conexión a Pi-hole exitosa"
+        return False, f"Respuesta inesperada de Pi-hole: {resp.status_code}"
+    except Exception as e:
+        logger.exception("Error probando conexión Pi-hole")
+        return False, f"Error al conectar con Pi-hole: {str(e)}"
+
+
+def import_domains_from_file(file_storage) -> set:
+    domains = set()
+    if not file_storage:
+        return domains
+
+    content = file_storage.read().decode("utf-8", errors="ignore")
+    for line in content.splitlines():
+        line = line.strip()
+        if not line or line.startswith("#"):
+            continue
+        if " " in line:
+            parts = line.split()
+            domain = parts[-1]
+        else:
+            domain = line
+        domains.add(domain)
+    return domains
+
+
+def import_domains_from_url(url: str) -> tuple[bool, set, str]:
+    domains = set()
+    try:
+        resp = requests.get(url, timeout=8)
+        if resp.status_code != 200:
+            return False, domains, f"Error al descargar la lista: {resp.status_code}"
+
+        for line in resp.text.splitlines():
+            line = line.strip()
+            if not line or line.startswith("#"):
+                continue
+            if " " in line:
+                parts = line.split()
+                domain = parts[-1]
+            else:
+                domain = line
+            domains.add(domain)
+        return True, domains, ""
+    except Exception as e:
+        logger.exception("Error descargando lista desde URL")
+        return False, domains, str(e)
+
+
+def merge_and_save_blacklist(existing_env: dict, new_domains: set) -> None:
+    existing = [d.strip() for d in existing_env.get("BLACKLIST_DOMAINS", "").split(",") if d.strip()]
+    merged = set(existing)
+    merged.update(new_domains)
+    existing_env["BLACKLIST_DOMAINS"] = ",".join(sorted(merged))
+    save_env_vars(existing_env)
+
+
+def save_custom_list(items: list) -> None:
+    env_vars = load_env_vars()
+    env_vars["BLACKLIST_DOMAINS"] = ",".join(sorted(set(items)))
+    save_env_vars(env_vars)

+ 139 - 0
services/delay_pools_service.py

@@ -0,0 +1,139 @@
+from loguru import logger
+
+
+def delete_delay_pool(pool_number: str, config_manager) -> tuple[bool, str]:
+    try:
+        if config_manager.is_modular:
+            delay_content = config_manager.read_modular_config("110_delay_pools.conf")
+            if delay_content is not None:
+                lines = delay_content.split("\n")
+                new_lines = []
+                for line in lines:
+                    stripped = line.strip()
+                    if (
+                        stripped.startswith(f"delay_class {pool_number} ")
+                        or stripped.startswith(f"delay_parameters {pool_number} ")
+                        or stripped.startswith(f"delay_access {pool_number} ")
+                    ):
+                        continue
+                    new_lines.append(line)
+                new_content = "\n".join(new_lines)
+                if config_manager.save_modular_config("110_delay_pools.conf", new_content):
+                    return True, f"Delay Pool #{pool_number} eliminado exitosamente"
+                else:
+                    return False, "Error al eliminar delay pool modular"
+
+        # Fallback to main config
+        lines = config_manager.config_content.split("\n")
+        new_lines = []
+        for line in lines:
+            stripped = line.strip()
+            if (
+                stripped.startswith(f"delay_class {pool_number} ")
+                or stripped.startswith(f"delay_parameters {pool_number} ")
+                or stripped.startswith(f"delay_access {pool_number} ")
+            ):
+                continue
+            new_lines.append(line)
+        new_content = "\n".join(new_lines)
+        config_manager.save_config(new_content)
+        return True, f"Delay Pool #{pool_number} eliminado exitosamente"
+    except Exception as e:
+        logger.exception("Error eliminando delay pool")
+        return False, str(e)
+
+
+def edit_delay_pool(pool_number: str, pool_class: str, parameters: str, access_actions: list, access_acls: list, config_manager) -> tuple[bool, str]:
+    try:
+        new_directives = []
+        new_directives.append(f"delay_class {pool_number} {pool_class}")
+        new_directives.append(f"delay_parameters {pool_number} {parameters}")
+        for action, acl in zip(access_actions, access_acls):
+            if acl.strip():
+                new_directives.append(f"delay_access {pool_number} {action} {acl}")
+
+        if config_manager.is_modular:
+            delay_content = config_manager.read_modular_config("110_delay_pools.conf")
+            if delay_content is not None:
+                lines = delay_content.split("\n")
+                new_lines = []
+                pool_found = False
+                insert_index = -1
+                for i, line in enumerate(lines):
+                    if line.strip().startswith(f"delay_class {pool_number} "):
+                        pool_found = True
+                        insert_index = i
+                        # skip existing block (we will rebuild)
+                        continue
+                    if pool_found and (line.strip().startswith("delay_class ") or line.strip() == ""):
+                        pool_found = False
+                    if not pool_found:
+                        new_lines.append(line)
+
+                if insert_index >= 0:
+                    # insert our directives at the insertion point
+                    new_lines[insert_index:insert_index] = new_directives
+                else:
+                    new_lines.extend(new_directives)
+                new_content = "\n".join(new_lines)
+                if config_manager.save_modular_config("110_delay_pools.conf", new_content):
+                    return True, f"Delay Pool #{pool_number} actualizado exitosamente"
+                else:
+                    return False, "Error al guardar delay pool modular"
+
+        # Fallback to main config: naive append/update
+        lines = config_manager.config_content.split("\n")
+        # Try to find existing pool and replace, else append
+        pool_found = False
+        insert_index = -1
+        for i, line in enumerate(lines):
+            if line.strip().startswith(f"delay_class {pool_number} "):
+                pool_found = True
+                insert_index = i
+                break
+
+        if insert_index >= 0:
+            # remove existing block lines starting at insert_index until next blank or next delay_class
+            j = insert_index
+            while j < len(lines) and not (lines[j].strip().startswith("delay_class ") and j != insert_index):
+                j += 1
+            new_lines = lines[:insert_index] + new_directives + lines[j:]
+        else:
+            new_lines = lines + new_directives
+
+        new_content = "\n".join(new_lines)
+        config_manager.save_config(new_content)
+        return True, f"Delay Pool #{pool_number} actualizado exitosamente"
+    except Exception as e:
+        logger.exception("Error actualizando delay pool")
+        return False, str(e)
+
+
+def add_delay_pool(pool_number: str, pool_class: str, parameters: str, access_actions: list, access_acls: list, config_manager) -> tuple[bool, str]:
+    try:
+        new_directives = []
+        new_directives.append(f"delay_class {pool_number} {pool_class}")
+        new_directives.append(f"delay_parameters {pool_number} {parameters}")
+        for action, acl in zip(access_actions, access_acls):
+            if acl.strip():
+                new_directives.append(f"delay_access {pool_number} {action} {acl}")
+
+        if config_manager.is_modular:
+            delay_content = config_manager.read_modular_config("110_delay_pools.conf")
+            if delay_content is not None:
+                lines = delay_content.split("\n")
+                lines.extend(new_directives)
+                new_content = "\n".join(lines)
+                if config_manager.save_modular_config("110_delay_pools.conf", new_content):
+                    return True, f"Delay Pool #{pool_number} creado exitosamente"
+                else:
+                    return False, "Error al crear delay pool modular"
+
+        lines = config_manager.config_content.split("\n")
+        lines.extend(new_directives)
+        new_content = "\n".join(lines)
+        config_manager.save_config(new_content)
+        return True, f"Delay Pool #{pool_number} creado exitosamente"
+    except Exception as e:
+        logger.exception("Error creando delay pool")
+        return False, str(e)

+ 157 - 0
services/http_access_service.py

@@ -0,0 +1,157 @@
+from loguru import logger
+
+
+def _find_http_lines(lines):
+    return [i for i, line in enumerate(lines) if line.strip().startswith("http_access ") and not line.strip().startswith("#")]
+
+
+def delete_http_access(rule_index: int, config_manager) -> tuple[bool, str]:
+    try:
+        if config_manager.is_modular:
+            http_content = config_manager.read_modular_config("120_http_access.conf")
+            if http_content is not None:
+                lines = http_content.split("\n")
+                http_lines_indices = _find_http_lines(lines)
+                if rule_index >= len(http_lines_indices):
+                    return False, "Índice de regla inválido"
+                # remove the line
+                del lines[http_lines_indices[rule_index]]
+                new_content = "\n".join(lines)
+                if config_manager.save_modular_config("120_http_access.conf", new_content):
+                    return True, "Regla HTTP Access eliminada exitosamente"
+                else:
+                    return False, "Error al eliminar la regla modular"
+
+        # Fallback to main config
+        lines = config_manager.config_content.split("\n")
+        http_lines_indices = _find_http_lines(lines)
+        if rule_index >= len(http_lines_indices):
+            return False, "Índice de regla inválido"
+        del lines[http_lines_indices[rule_index]]
+        new_content = "\n".join(lines)
+        config_manager.save_config(new_content)
+        return True, "Regla HTTP Access eliminada exitosamente"
+    except Exception as e:
+        logger.exception("Error eliminando http_access")
+        return False, str(e)
+
+
+def edit_http_access(rule_index: int, action: str, acls: list, description: str, config_manager) -> tuple[bool, str]:
+    try:
+        acl_string = " ".join([acl.strip() for acl in acls if acl.strip()])
+        if not acl_string:
+            return False, "Debe especificar al menos una ACL"
+
+        new_rule = f"http_access {action} {acl_string}"
+
+        if config_manager.is_modular:
+            http_content = config_manager.read_modular_config("120_http_access.conf")
+            if http_content is not None:
+                lines = http_content.split("\n")
+                http_lines_indices = _find_http_lines(lines)
+                if rule_index >= len(http_lines_indices):
+                    return False, "Índice de regla inválido"
+                i = http_lines_indices[rule_index]
+                if description:
+                    if i > 0 and lines[i - 1].strip().startswith("#"):
+                        lines[i - 1] = f"# {description}"
+                    else:
+                        lines.insert(i, f"# {description}")
+                        i += 1
+                lines[i] = new_rule
+                new_content = "\n".join(lines)
+                if config_manager.save_modular_config("120_http_access.conf", new_content):
+                    return True, "Regla HTTP Access actualizada exitosamente"
+                else:
+                    return False, "Error al guardar regla modular"
+
+        # Fallback to main config
+        lines = config_manager.config_content.split("\n")
+        http_lines_indices = _find_http_lines(lines)
+        if rule_index >= len(http_lines_indices):
+            return False, "Índice de regla inválido"
+        i = http_lines_indices[rule_index]
+        if description:
+            if i > 0 and lines[i - 1].strip().startswith("#"):
+                lines[i - 1] = f"# {description}"
+            else:
+                lines.insert(i, f"# {description}")
+                i += 1
+        lines[i] = new_rule
+        new_content = "\n".join(lines)
+        config_manager.save_config(new_content)
+        return True, "Regla HTTP Access actualizada exitosamente"
+    except Exception as e:
+        logger.exception("Error actualizando http_access")
+        return False, str(e)
+
+
+def add_http_access(action: str, acls: list, description: str, config_manager) -> tuple[bool, str]:
+    try:
+        acl_string = " ".join([acl.strip() for acl in acls if acl.strip()])
+        if not acl_string:
+            return False, "Debe especificar al menos una ACL"
+        new_rule = f"http_access {action} {acl_string}"
+        lines_to_add = []
+        if description:
+            lines_to_add.append(f"# {description}")
+        lines_to_add.append(new_rule)
+
+        if config_manager.is_modular:
+            http_content = config_manager.read_modular_config("120_http_access.conf")
+            if http_content is not None:
+                lines = http_content.split("\n")
+                lines.extend(lines_to_add)
+                new_content = "\n".join(lines)
+                if config_manager.save_modular_config("120_http_access.conf", new_content):
+                    return True, "Regla HTTP Access agregada exitosamente"
+                else:
+                    return False, "Error al guardar regla modular"
+
+        lines = config_manager.config_content.split("\n")
+        lines.extend(lines_to_add)
+        new_content = "\n".join(lines)
+        config_manager.save_config(new_content)
+        return True, "Regla HTTP Access agregada exitosamente"
+    except Exception as e:
+        logger.exception("Error agregando http_access")
+        return False, str(e)
+
+
+def move_http_access(rule_index: int, direction: str, config_manager) -> tuple[bool, str]:
+    try:
+        if config_manager.is_modular:
+            http_content = config_manager.read_modular_config("120_http_access.conf")
+            if http_content is not None:
+                lines = http_content.split("\n")
+                http_lines_indices = _find_http_lines(lines)
+                if rule_index >= len(http_lines_indices):
+                    return False, "Índice de regla inválido"
+                current = http_lines_indices[rule_index]
+                if direction == "up" and rule_index > 0:
+                    target = http_lines_indices[rule_index - 1]
+                    lines[current], lines[target] = lines[target], lines[current]
+                elif direction == "down" and rule_index < len(http_lines_indices) - 1:
+                    target = http_lines_indices[rule_index + 1]
+                    lines[current], lines[target] = lines[target], lines[current]
+                new_content = "\n".join(lines)
+                config_manager.save_modular_config("120_http_access.conf", new_content)
+                return True, "Regla movida exitosamente"
+
+        lines = config_manager.config_content.split("\n")
+        http_lines_indices = _find_http_lines(lines)
+        if rule_index >= len(http_lines_indices):
+            return False, "Índice de regla inválido"
+        current = http_lines_indices[rule_index]
+        if direction == "up" and rule_index > 0:
+            target = http_lines_indices[rule_index - 1]
+            lines[current], lines[target] = lines[target], lines[current]
+        elif direction == "down" and rule_index < len(http_lines_indices) - 1:
+            target = http_lines_indices[rule_index + 1]
+            lines[current], lines[target] = lines[target], lines[current]
+        new_content = "\n".join(lines)
+        config_manager.save_config(new_content)
+        return True, "Regla movida exitosamente"
+    except Exception as e:
+        logger.exception("Error moviendo http_access")
+        return False, str(e)