
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
from enum import StrEnum, unique
from pathlib import Path

from im360.subsys.int_config import (
    is_cloudways_hybrid,
    is_module_on_generic_panel,
)
from im360.utils import use_coraza4cpanel

_CONF_PATH = Path("/usr/share/imunify360-webshield/modularity_mode")


@unique
class Mode(StrEnum):
    STANDALONE = "standalone"
    APACHE = "apache"
    NGINX = "nginx"

    @classmethod
    def get(cls):
        try:
            mode = _CONF_PATH.read_text().strip()
            if mode == "apache":
                return cls.APACHE
            if mode == "nginx":
                return cls.NGINX
            if mode == "standalone":
                return cls.STANDALONE
            raise TypeError(f"Unknown mode: {mode}")
        except FileNotFoundError:
            return cls.STANDALONE

    @classmethod
    def wants_redirect(cls, mode) -> bool:
        if mode == cls.STANDALONE:
            return True
        # WebShield is not running in the generic-panel module mode.
        if mode == cls.APACHE and not is_module_on_generic_panel():
            return True
        return use_coraza4cpanel() or is_cloudways_hybrid()


def get_module_based_ports() -> set[int]:
    # For now - hardcoded values. In the future maybe remake to a config file.
    return {80, 443}
