diff --git a/kiauh/components/webui_client/base_data.py b/kiauh/components/webui_client/base_data.py index f46798b..e3afa4e 100644 --- a/kiauh/components/webui_client/base_data.py +++ b/kiauh/components/webui_client/base_data.py @@ -37,6 +37,7 @@ class BaseWebClient(ABC): backup_dir: Path repo_path: str download_url: str + nginx_config: Path nginx_access_log: Path nginx_error_log: Path client_config: BaseWebClientConfig diff --git a/kiauh/components/webui_client/client_utils.py b/kiauh/components/webui_client/client_utils.py index 0141160..56699d1 100644 --- a/kiauh/components/webui_client/client_utils.py +++ b/kiauh/components/webui_client/client_utils.py @@ -338,36 +338,62 @@ def create_nginx_cfg( raise +def get_nginx_config_list() -> List[Path]: + """ + Get a list of all NGINX config files in /etc/nginx/sites-enabled + :return: List of NGINX config files + """ + configs: List[Path] = [] + for config in NGINX_SITES_ENABLED.iterdir(): + if not config.is_file(): + continue + configs.append(config) + return configs + + +def get_nginx_listen_port(config: Path) -> int | None: + """ + Get the listen port from an NGINX config file + :param config: The NGINX config file to read the port from + :return: The listen port as int or None if not found/parsable + """ + + # noinspection HttpUrlsUsage + pattern = r"default_server|http://|https://|[;\[\]]" + port = "" + with open(config, "r") as cfg: + for line in cfg.readlines(): + line = re.sub(pattern, "", line.strip()) + if line.startswith("listen"): + if ":" not in line: + port = line.split()[-1] + else: + port = line.split(":")[-1] + try: + return int(port) + except ValueError: + Logger.print_error( + f"Unable to parse listen port {port} from {config.name}!" + ) + return None + + def read_ports_from_nginx_configs() -> List[int]: """ - Helper function to iterate over all NGINX configs and read all ports defined for listen + Helper function to iterate over all NGINX configs + and read all ports defined for listen :return: A sorted list of listen ports """ if not NGINX_SITES_ENABLED.exists(): return [] - port_list = [] - for config in NGINX_SITES_ENABLED.iterdir(): - if not config.is_file(): - continue + port_list: List[int] = [] + for config in get_nginx_config_list(): + port = get_nginx_listen_port(config) + if port is not None: + port_list.append(port) - with open(config, "r") as cfg: - lines = cfg.readlines() - - for line in lines: - line = re.sub( - r"default_server|http://|https://|[;\[\]]", - "", - line.strip(), - ) - if line.startswith("listen"): - if ":" not in line: - port_list.append(line.split()[-1]) - else: - port_list.append(line.split(":")[-1]) - - ports_to_ints_list = [int(port) for port in port_list] - return sorted(ports_to_ints_list, key=lambda x: int(x)) + return sorted(port_list, key=lambda x: int(x)) def get_client_port_selection( diff --git a/kiauh/components/webui_client/fluidd_data.py b/kiauh/components/webui_client/fluidd_data.py index b499351..79c23b1 100644 --- a/kiauh/components/webui_client/fluidd_data.py +++ b/kiauh/components/webui_client/fluidd_data.py @@ -19,6 +19,7 @@ from components.webui_client.base_data import ( WebClientType, ) from core.backup_manager import BACKUP_ROOT_DIR +from core.constants import NGINX_SITES_AVAILABLE @dataclass() @@ -44,6 +45,7 @@ class FluiddData(BaseWebClient): config_file: Path = client_dir.joinpath("config.json") backup_dir: Path = BACKUP_ROOT_DIR.joinpath("fluidd-backups") repo_path: str = "fluidd-core/fluidd" + nginx_config: Path = NGINX_SITES_AVAILABLE.joinpath("fluidd") nginx_access_log: Path = Path("/var/log/nginx/fluidd-access.log") nginx_error_log: Path = Path("/var/log/nginx/fluidd-error.log") client_config: BaseWebClientConfig = None diff --git a/kiauh/components/webui_client/mainsail_data.py b/kiauh/components/webui_client/mainsail_data.py index 1d520a1..aa2030d 100644 --- a/kiauh/components/webui_client/mainsail_data.py +++ b/kiauh/components/webui_client/mainsail_data.py @@ -19,6 +19,7 @@ from components.webui_client.base_data import ( WebClientType, ) from core.backup_manager import BACKUP_ROOT_DIR +from core.constants import NGINX_SITES_AVAILABLE @dataclass() @@ -44,6 +45,7 @@ class MainsailData(BaseWebClient): config_file: Path = client_dir.joinpath("config.json") backup_dir: Path = BACKUP_ROOT_DIR.joinpath("mainsail-backups") repo_path: str = "mainsail-crew/mainsail" + nginx_config: Path = NGINX_SITES_AVAILABLE.joinpath("mainsail") nginx_access_log: Path = Path("/var/log/nginx/mainsail-access.log") nginx_error_log: Path = Path("/var/log/nginx/mainsail-error.log") client_config: BaseWebClientConfig = None diff --git a/kiauh/components/webui_client/menus/client_install_menu.py b/kiauh/components/webui_client/menus/client_install_menu.py index a741888..ae3ad42 100644 --- a/kiauh/components/webui_client/menus/client_install_menu.py +++ b/kiauh/components/webui_client/menus/client_install_menu.py @@ -15,6 +15,7 @@ from components.webui_client.base_data import BaseWebClient from components.webui_client.client_setup import install_client from components.webui_client.client_utils import ( get_client_port_selection, + get_nginx_listen_port, set_listen_port, ) from core.constants import COLOR_CYAN, COLOR_GREEN, RESET_FORMAT @@ -53,7 +54,7 @@ class ClientInstallMenu(BaseMenu): header = f" [ Installation Menu > {client_name} ] " color = COLOR_GREEN count = 62 - len(color) - len(RESET_FORMAT) - port = f"(Current: {COLOR_CYAN}{int(self.client_settings.port)}{RESET_FORMAT})" + port = f"(Current: {COLOR_CYAN}{self._get_current_port()}{RESET_FORMAT})" menu = textwrap.dedent( f""" ╔═══════════════════════════════════════════════════════╗ @@ -70,7 +71,7 @@ class ClientInstallMenu(BaseMenu): install_client(self.client, settings=self.settings, reinstall=True) def change_listen_port(self, **kwargs) -> None: - curr_port = int(self.client_settings.port) + curr_port = self._get_current_port() new_port = get_client_port_selection( self.client, self.settings, @@ -99,6 +100,14 @@ class ClientInstallMenu(BaseMenu): ], ) + def _get_current_port(self) -> int: + curr_port = get_nginx_listen_port(self.client.nginx_config) + if curr_port is None: + # if the port is not found in the config file we use + # the default port from the kiauh settings as fallback + return int(self.client_settings.port) + return curr_port + def _go_back(self, **kwargs) -> None: if self.previous_menu is not None: self.previous_menu().run()