admin.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import os
  2. import shutil
  3. from datetime import datetime
  4. # Configuración para Squid
  5. SQUID_CONFIG_PATH = "/etc/squid/squid.conf"
  6. BACKUP_DIR = "/etc/squid/backups"
  7. ACL_FILES_DIR = "/etc/squid/acls"
  8. class SquidConfigManager:
  9. def __init__(self, config_path=SQUID_CONFIG_PATH):
  10. self.config_path = config_path
  11. self.config_content = ""
  12. self.load_config()
  13. def load_config(self):
  14. """Cargar el archivo de configuración"""
  15. try:
  16. with open(self.config_path, encoding="utf-8") as f:
  17. self.config_content = f.read()
  18. except FileNotFoundError:
  19. self.config_content = ""
  20. def save_config(self, content):
  21. """Guardar la configuración con backup"""
  22. # Crear backup
  23. self.create_backup()
  24. # Guardar nueva configuración
  25. with open(self.config_path, "w", encoding="utf-8") as f:
  26. f.write(content)
  27. self.config_content = content
  28. def create_backup(self):
  29. """Crear backup de la configuración actual"""
  30. if not os.path.exists(BACKUP_DIR):
  31. os.makedirs(BACKUP_DIR)
  32. timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
  33. backup_path = os.path.join(BACKUP_DIR, f"squid.conf.backup_{timestamp}")
  34. shutil.copy2(self.config_path, backup_path)
  35. def get_acls(self):
  36. """Extraer ACLs del archivo de configuración"""
  37. acls = []
  38. lines = self.config_content.split("\n")
  39. for line in lines:
  40. line = line.strip()
  41. if line.startswith("acl ") and not line.startswith("#"):
  42. parts = line.split()
  43. if len(parts) >= 3:
  44. acl_name = parts[1]
  45. acl_type = parts[2]
  46. acl_value = " ".join(parts[3:]) if len(parts) > 3 else ""
  47. acls.append(
  48. {
  49. "name": acl_name,
  50. "type": acl_type,
  51. "value": acl_value,
  52. "full_line": line,
  53. }
  54. )
  55. return acls
  56. def get_delay_pools(self):
  57. """Extraer configuración de delay pools"""
  58. delay_pools = []
  59. lines = self.config_content.split("\n")
  60. for line in lines:
  61. line = line.strip()
  62. if line.startswith("delay_pools "):
  63. parts = line.split()
  64. if len(parts) >= 2:
  65. delay_pools.append({"directive": "delay_pools", "value": parts[1]})
  66. elif line.startswith("delay_class "):
  67. parts = line.split()
  68. if len(parts) >= 3:
  69. delay_pools.append(
  70. {
  71. "directive": "delay_class",
  72. "pool": parts[1],
  73. "class": parts[2],
  74. }
  75. )
  76. elif line.startswith("delay_parameters "):
  77. parts = line.split()
  78. if len(parts) >= 3:
  79. delay_pools.append(
  80. {
  81. "directive": "delay_parameters",
  82. "pool": parts[1],
  83. "parameters": " ".join(parts[2:]),
  84. }
  85. )
  86. elif line.startswith("delay_access "):
  87. parts = line.split()
  88. if len(parts) >= 4:
  89. delay_pools.append(
  90. {
  91. "directive": "delay_access",
  92. "pool": parts[1],
  93. "action": parts[2],
  94. "acl": " ".join(parts[3:]),
  95. }
  96. )
  97. return delay_pools
  98. def get_http_access_rules(self):
  99. """Extraer reglas de acceso HTTP"""
  100. rules = []
  101. lines = self.config_content.split("\n")
  102. for line in lines:
  103. line = line.strip()
  104. if line.startswith("http_access ") and not line.startswith("#"):
  105. parts = line.split()
  106. if len(parts) >= 3:
  107. rules.append({"action": parts[1], "acl": " ".join(parts[2:])})
  108. return rules