| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328 |
- import logging
- import os
- from dotenv import load_dotenv
- logger = logging.getLogger(__name__)
- load_dotenv()
- SQUID_CONFIG_PATH = os.getenv("SQUID_CONFIG_PATH", "/etc/squid/squid.conf")
- ACL_FILES_DIR = os.getenv("ACL_FILES_DIR", "/etc/squid/acls")
- def validate_paths():
- errors = []
- cfg = os.path.abspath(os.path.expanduser(SQUID_CONFIG_PATH))
- if os.path.isdir(cfg):
- squid_conf = os.path.join(cfg, "squid.conf")
- if not os.path.exists(squid_conf):
- errors.append(f"'squid.conf' not found in directory: {cfg}")
- else:
- if not os.path.isfile(squid_conf):
- errors.append(f"Found but not a regular file: {squid_conf}")
- else:
- if not os.access(squid_conf, os.R_OK):
- errors.append(f"No read permissions for: {squid_conf}")
- if not os.access(squid_conf, os.W_OK):
- errors.append(f"No write permissions for: {squid_conf}")
- else:
- squid_conf = cfg
- if not os.path.exists(squid_conf):
- errors.append(f"Configuration file not found: {squid_conf}")
- else:
- if not os.path.isfile(squid_conf):
- errors.append(f"Not a regular file: {squid_conf}")
- if os.path.basename(squid_conf) != "squid.conf":
- errors.append(
- f"Expected file named 'squid.conf', got: {os.path.basename(squid_conf)}"
- )
- if os.path.isfile(squid_conf):
- if not os.access(squid_conf, os.R_OK):
- errors.append(f"No read permissions for: {squid_conf}")
- if not os.access(squid_conf, os.W_OK):
- errors.append(f"No write permissions for: {squid_conf}")
- acl_dir = os.path.abspath(os.path.expanduser(ACL_FILES_DIR))
- if not os.path.exists(acl_dir):
- errors.append(f"ACL directory not found: {acl_dir}")
- elif not os.path.isdir(acl_dir):
- errors.append(f"ACL path is not a directory: {acl_dir}")
- elif not os.access(acl_dir, os.W_OK):
- errors.append(f"No write permissions in ACL directory: {acl_dir}")
- return errors
- class SquidConfigManager:
- def __init__(self, config_path=SQUID_CONFIG_PATH):
- self.config_path = config_path
- self.config_content = ""
- self.is_valid = False
- self.errors = []
- self._validate_environment()
- if self.is_valid:
- self.load_config()
- def _validate_environment(self):
- self.errors = validate_paths()
- if self.errors:
- for error in self.errors:
- logger.error(error)
- self.is_valid = False
- logger.error(
- "SquidConfigManager cannot be initialized due to configuration errors"
- )
- else:
- self.is_valid = True
- logger.info("SquidConfigManager initialized successfully")
- def load_config(self):
- if not self.is_valid:
- logger.error("Cannot load configuration: invalid environment")
- return False
- try:
- with open(self.config_path, encoding="utf-8") as f:
- self.config_content = f.read()
- logger.debug(f"Configuration loaded from: {self.config_path}")
- return True
- except FileNotFoundError:
- logger.error(f"Configuration file not found: {self.config_path}")
- self.config_content = ""
- return False
- except PermissionError:
- logger.error(f"No read permissions for: {self.config_path}")
- self.config_content = ""
- return False
- except UnicodeDecodeError as e:
- logger.error(f"File encoding error: {e}")
- self.config_content = ""
- return False
- except Exception as e:
- logger.error(f"Unexpected error loading configuration: {e}")
- self.config_content = ""
- return False
- def save_config(self, content):
- if not self.is_valid:
- logger.error("Cannot save configuration: invalid environment")
- return False
- try:
- backup_created = self.create_backup()
- if not backup_created:
- logger.warning("Could not create backup, but continuing with save...")
- with open(self.config_path, "w", encoding="utf-8") as f:
- f.write(content)
- self.config_content = content
- logger.info(f"Configuration saved successfully to: {self.config_path}")
- return True
- except PermissionError:
- logger.error(f"No write permissions for: {self.config_path}")
- return False
- except OSError as e:
- logger.error(f"System error saving configuration: {e}")
- return False
- except Exception as e:
- logger.error(f"Unexpected error saving configuration: {e}")
- return False
- def create_backup(self):
- if not self.is_valid:
- logger.error("Cannot create backup: invalid environment")
- return False
- """ try:
- if not os.path.exists(BACKUP_DIR):
- logger.error(f"Backup directory does not exist: {BACKUP_DIR}")
- return False
- if not os.access(BACKUP_DIR, os.W_OK):
- logger.error(f"No write permissions in backup directory: {BACKUP_DIR}")
- return False
- if not os.path.exists(self.config_path):
- logger.warning("No configuration file exists to backup")
- return False
- timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
- backup_filename = f"squid.conf.backup_{timestamp}"
- backup_path = os.path.join(BACKUP_DIR, backup_filename)
- shutil.copy2(self.config_path, backup_path)
- logger.info(f"Backup created: {backup_path}")
- return True
- except PermissionError:
- logger.error(f"No permissions to create backup in: {BACKUP_DIR}")
- return False
- except OSError as e:
- logger.error(f"System error creating backup: {e}")
- return False
- except Exception as e:
- logger.error(f"Unexpected error creating backup: {e}")
- return False """
- def get_acls(self):
- if not self.is_valid:
- logger.error("Cannot get ACLs: invalid environment")
- return []
- if not self.config_content:
- logger.warning("No configuration content available")
- return []
- try:
- acls = []
- lines = self.config_content.split("\n")
- acl_index = 0
- for line_num, line in enumerate(lines, 1):
- try:
- line = line.strip()
- if line.startswith("acl ") and not line.startswith("#"):
- parts = line.split()
- if len(parts) >= 3:
- acl_name = parts[1]
- acl_type = parts[2]
- acl_value = " ".join(parts[3:]) if len(parts) > 3 else ""
- acls.append(
- {
- "id": acl_index,
- "name": acl_name,
- "type": acl_type,
- "value": acl_value,
- "full_line": line,
- }
- )
- acl_index += 1
- except Exception as e:
- logger.warning(f"Error processing ACL at line {line_num}: {e}")
- continue
- logger.debug(f"Found {len(acls)} ACLs")
- return acls
- except Exception as e:
- logger.error(f"Unexpected error extracting ACLs: {e}")
- return []
- def get_delay_pools(self):
- if not self.is_valid:
- logger.error("Cannot get delay pools: invalid environment")
- return []
- if not self.config_content:
- logger.warning("No configuration content available")
- return []
- try:
- delay_pools = []
- lines = self.config_content.split("\n")
- for line_num, line in enumerate(lines, 1):
- try:
- line = line.strip()
- if line.startswith("delay_pools "):
- parts = line.split()
- if len(parts) >= 2:
- delay_pools.append(
- {"directive": "delay_pools", "value": parts[1]}
- )
- elif line.startswith("delay_class "):
- parts = line.split()
- if len(parts) >= 3:
- delay_pools.append(
- {
- "directive": "delay_class",
- "pool": parts[1],
- "class": parts[2],
- }
- )
- elif line.startswith("delay_parameters "):
- parts = line.split()
- if len(parts) >= 3:
- delay_pools.append(
- {
- "directive": "delay_parameters",
- "pool": parts[1],
- "parameters": " ".join(parts[2:]),
- }
- )
- elif line.startswith("delay_access "):
- parts = line.split()
- if len(parts) >= 4:
- delay_pools.append(
- {
- "directive": "delay_access",
- "pool": parts[1],
- "action": parts[2],
- "acl": " ".join(parts[3:]),
- }
- )
- except Exception as e:
- logger.warning(
- f"Error processing delay pool at line {line_num}: {e}"
- )
- continue
- logger.debug(f"Found {len(delay_pools)} delay pool configurations")
- return delay_pools
- except Exception as e:
- logger.error(f"Unexpected error extracting delay pools: {e}")
- return []
- def get_http_access_rules(self):
- if not self.is_valid:
- logger.error("Cannot get HTTP rules: invalid environment")
- return []
- if not self.config_content:
- logger.warning("No configuration content available")
- return []
- try:
- rules = []
- lines = self.config_content.split("\n")
- for line_num, line in enumerate(lines, 1):
- try:
- line = line.strip()
- if line.startswith("http_access ") and not line.startswith("#"):
- parts = line.split()
- if len(parts) >= 3:
- rules.append(
- {"action": parts[1], "acl": " ".join(parts[2:])}
- )
- except Exception as e:
- logger.warning(
- f"Error processing HTTP rule at line {line_num}: {e}"
- )
- continue
- logger.debug(f"Found {len(rules)} HTTP access rules")
- return rules
- except Exception as e:
- logger.error(f"Unexpected error extracting HTTP rules: {e}")
- return []
- def get_status(self):
- return {
- "is_valid": self.is_valid,
- "errors": self.errors,
- "config_loaded": bool(self.config_content),
- "config_path": self.config_path,
- # "backup_dir": BACKUP_DIR,
- "acl_files_dir": ACL_FILES_DIR,
- }
|