4
0

configurator.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. for line in lines:
  38. stripped = line.strip()
  39. if not stripped or stripped.startswith("#"):
  40. continue
  41. for file, regex_list in patterns.items():
  42. if any(regex.search(stripped) for regex in regex_list):
  43. files[file].append(line)
  44. break
  45. for filename, content in files.items():
  46. with open(filename, "w") as f:
  47. f.writelines(content)
  48. print("Extraction completed. Files generated:")
  49. for f in files:
  50. print(f" - {f}")
  51. if __name__ == "__main__":
  52. extract_squid_config(input_file)