configurator.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import os
  2. import re
  3. from config import SQUID_CONFIG_PATH
  4. input_file = SQUID_CONFIG_PATH
  5. files = {
  6. "acls.conf": [],
  7. "delay_pools.conf": [],
  8. "auth.conf": [],
  9. }
  10. # Patrones por categoría
  11. patterns = {
  12. "acls.conf": [
  13. re.compile(r"^\s*acl\b"),
  14. ],
  15. "delay_pools.conf": [
  16. re.compile(r"^\s*delay_(pools|class|parameters|access)\b"),
  17. ],
  18. "auth.conf": [
  19. re.compile(r"^\s*auth_param\b"),
  20. re.compile(r"^\s*authenticate_ip_ttl\b"),
  21. re.compile(r"^\s*acl\b.*\bproxy_auth\b"),
  22. ],
  23. }
  24. def extract_squid_config(input_file):
  25. if not os.path.exists(input_file):
  26. print(f"Error: File {input_file} does not exist.")
  27. return
  28. try:
  29. with open(input_file) as f:
  30. lines = f.readlines()
  31. except PermissionError:
  32. print(f"Error: No permission to read {input_file}")
  33. return
  34. except Exception as e:
  35. print(f"Error reading file {input_file}: {e}")
  36. return
  37. lines = f.readlines()
  38. for line in lines:
  39. stripped = line.strip()
  40. if not stripped or stripped.startswith("#"):
  41. continue
  42. for file, regex_list in patterns.items():
  43. if any(regex.search(stripped) for regex in regex_list):
  44. files[file].append(line)
  45. break
  46. for filename, content in files.items():
  47. with open(filename, "w") as f:
  48. f.writelines(content)
  49. print("Extraction completed. Files generated:")
  50. for f in files:
  51. print(f" - {f}")
  52. if __name__ == "__main__":
  53. extract_squid_config(input_file)