admin.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import os
  2. import shutil
  3. from datetime import datetime
  4. # Configuración para Squid
  5. SQUID_CONFIG_PATH = "/home/manuel/Desktop/config/squid.conf"
  6. BACKUP_DIR = "/home/manuel/Desktop/config/backups"
  7. ACL_FILES_DIR = "/home/manuel/Desktop/config/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. acl_index = 0
  40. for line in lines:
  41. line = line.strip()
  42. if line.startswith("acl ") and not line.startswith("#"):
  43. parts = line.split()
  44. if len(parts) >= 3:
  45. acl_name = parts[1]
  46. acl_type = parts[2]
  47. acl_value = " ".join(parts[3:]) if len(parts) > 3 else ""
  48. acls.append(
  49. {
  50. "id": acl_index,
  51. "name": acl_name,
  52. "type": acl_type,
  53. "value": acl_value,
  54. "full_line": line,
  55. }
  56. )
  57. acl_index += 1
  58. return acls
  59. def get_delay_pools(self):
  60. """Extraer configuración de delay pools"""
  61. delay_pools = []
  62. lines = self.config_content.split("\n")
  63. for line in lines:
  64. line = line.strip()
  65. if line.startswith("delay_pools "):
  66. parts = line.split()
  67. if len(parts) >= 2:
  68. delay_pools.append({"directive": "delay_pools", "value": parts[1]})
  69. elif line.startswith("delay_class "):
  70. parts = line.split()
  71. if len(parts) >= 3:
  72. delay_pools.append(
  73. {
  74. "directive": "delay_class",
  75. "pool": parts[1],
  76. "class": parts[2],
  77. }
  78. )
  79. elif line.startswith("delay_parameters "):
  80. parts = line.split()
  81. if len(parts) >= 3:
  82. delay_pools.append(
  83. {
  84. "directive": "delay_parameters",
  85. "pool": parts[1],
  86. "parameters": " ".join(parts[2:]),
  87. }
  88. )
  89. elif line.startswith("delay_access "):
  90. parts = line.split()
  91. if len(parts) >= 4:
  92. delay_pools.append(
  93. {
  94. "directive": "delay_access",
  95. "pool": parts[1],
  96. "action": parts[2],
  97. "acl": " ".join(parts[3:]),
  98. }
  99. )
  100. return delay_pools
  101. def get_http_access_rules(self):
  102. """Extraer reglas de acceso HTTP"""
  103. rules = []
  104. lines = self.config_content.split("\n")
  105. for line in lines:
  106. line = line.strip()
  107. if line.startswith("http_access ") and not line.startswith("#"):
  108. parts = line.split()
  109. if len(parts) >= 3:
  110. rules.append({"action": parts[1], "acl": " ".join(parts[2:])})
  111. return rules