diff --git a/apphub/src/api/v1/routers/proxy.py b/apphub/src/api/v1/routers/proxy.py index 7074caa1..2a787d33 100755 --- a/apphub/src/api/v1/routers/proxy.py +++ b/apphub/src/api/v1/routers/proxy.py @@ -44,7 +44,7 @@ def create_proxys( return AppManger().create_proxy_by_app(app_id,domain_names.domain_names,endpointId) @router.put( - "/proxys/{app_id}}", + "/proxys/{proxy_id}", summary="Update Proxys", description="Update proxys by app", responses={ @@ -54,11 +54,11 @@ def create_proxys( } ) def update_proxys( - proxyHost:ProxyHost = Body(..., description="Proxy host to update proxys from"), - app_id: str = Path(..., description="App ID to create proxys from"), + domain_names: DomainNames = Body(..., description="Domain names to update proxys from", example={"domain_names": ["example1.com","example2.com"]}), + proxy_id: int = Path(..., description="Proxy ID to update proxys from"), endpointId: int = Query(None, description="Endpoint ID to create proxys from. If not set, create proxys from the local endpoint"), ): - return AppManger().update_proxy_by_app(app_id,proxyHost,endpointId) + return AppManger().update_proxy_by_app(proxy_id,domain_names.domain_names,endpointId) @router.delete( "/proxys/app/{app_id}", diff --git a/apphub/src/external/nginx_proxy_manager_api.py b/apphub/src/external/nginx_proxy_manager_api.py index 688ce076..88e37cf0 100755 --- a/apphub/src/external/nginx_proxy_manager_api.py +++ b/apphub/src/external/nginx_proxy_manager_api.py @@ -113,48 +113,22 @@ class NginxProxyManagerAPI: ) def update_proxy_host( - self, - proxy_id: int, - domain_names: List[str], - forward_scheme: str, - forward_host: str, - forward_port: int, - advanced_config: str, + self,proxy_id :int,json: dict ): """ Update an existing proxy host Args: proxy_id (int): Proxy ID - domain_names (List[str]): Domain names - forward_scheme (str): Forward scheme - forward_host (str): Forward host - forward_port (int): Forward port - advanced_config (str): Advanced config + json (dict): Proxy host data Returns: Response: Response from Nginx Proxy Manager API """ + return self.api.put( path=f"nginx/proxy-hosts/{proxy_id}", - json={ - "domain_names": domain_names, - "forward_scheme": forward_scheme, - "forward_host": forward_host, - "forward_port": forward_port, - "access_list_id": "0", - "certificate_id": 0, - "meta": {"letsencrypt_agree": False, "dns_challenge": False}, - "advanced_config": advanced_config, - "block_exploits": False, - "caching_enabled": False, - "allow_websocket_upgrade": False, - "http2_support": False, - "hsts_enabled": False, - "hsts_subdomains": False, - "ssl_forced": False, - "locations": [], - }, + json=json, ) def delete_proxy_host(self, proxy_id: int): diff --git a/apphub/src/services/app_manager.py b/apphub/src/services/app_manager.py index 6d6605b9..2fd1f449 100755 --- a/apphub/src/services/app_manager.py +++ b/apphub/src/services/app_manager.py @@ -289,7 +289,7 @@ class AppManger: "APP_VERSION": app_version, "POWER_PASSWORD": PasswordGenerator.generate_strong_password(), "APP_URL": domain_names[0] - } + } EnvHelper(env_file_path).modify_env_values(new_env_values) # Commit and push to remote repo @@ -318,14 +318,16 @@ class AppManger: if proxy_enabled and domain_names: # Get the forward port form env file forward_port = EnvHelper(env_file_path).get_env_value_by_key("APP_HTTP_PORT") + forward_scheme = "https" if EnvHelper(env_file_path).get_env_value_by_key("APP_HTTPS_ACCESS") else "http" + # Get the nginx proxy config path nginx_proxy_path = f"{app_tmp_dir_path}/src/nginx-proxy.conf" if os.path.exists(nginx_proxy_path): # Get the advanced config advanced_config = FileHelper.read_file(nginx_proxy_path) - ProxyManager().create_proxy_by_app(domain_names,app_id,forward_port,advanced_config) + ProxyManager().create_proxy_by_app(domain_names,app_id,forward_port,advanced_config,forward_scheme) else: - ProxyManager().create_proxy_by_app(domain_names,app_id,forward_port) + ProxyManager().create_proxy_by_app(domain_names,app_id,forward_port,forward_scheme=forward_scheme) except (CustomException,Exception) as e: # Rollback-1: remove repo in gitea giteaManager.remove_repo(app_id) @@ -620,24 +622,20 @@ class AppManger: # Get the forward port stack_env = self.get_app_by_id(app_id,endpointId).env if stack_env: - for item in stack_env: - key, value = item.split("=", 1) - if key == "APP_HTTP_PORT": - forward_port = value - break + forward_port = stack_env.get("APP_HTTP_PORT",None) # Create proxy if forward_port: proxy_host = proxyManager.create_proxy_by_app(domain_names,app_id,forward_port) if proxy_host: - return ProxyHost( - proxy_id=proxy_host.get("id"), - domain_names=proxy_host.get("domain_names"), - ) + return proxy_host else: + logger.error(f"Create app:{app_id} proxy error") raise CustomException() else: + logger.error(f"Get app:{app_id} forward_port error") raise CustomException() else: + logger.error(f"Get app:{app_id} env error") raise CustomException() def remove_proxy_by_app(self,app_id:str,endpointId:int = None): @@ -678,50 +676,26 @@ class AppManger: ) ProxyManager().remove_proxy_host_by_id(proxy_id) - def update_proxy_by_app(self,app_id:str,proxyHost:ProxyHost,endpointId:int = None): + def update_proxy_by_app(self,proxy_id:str,domain_names:list[str],endpointId:int = None): proxyManager = ProxyManager() portainerManager = PortainerManager() # Check the endpointId is exists. endpointId = check_endpointId(endpointId, portainerManager) - # Check the app_id is exists - stack_info = portainerManager.get_stack_by_name(app_id,endpointId) - if stack_info is None: - raise CustomException( - status_code=400, - message="Invalid Request", - details=f"{app_id} Not Found" - ) - # Check the proxy id is exists - host = proxyManager.get_proxy_host_by_id(proxyHost.proxy_id) + host = proxyManager.get_proxy_host_by_id(proxy_id) if host is None: raise CustomException( status_code=400, message="Invalid Request", - details=f"Proxy ID:{proxyHost.proxy_id} Not Found" + details=f"Proxy ID:{proxy_id} Not Found" ) - - # Check the domain_names + + # check_domain_names(domain_names) # Update proxy - proxy_host = proxyManager.update_proxy_by_app( - proxyHost.proxy_id, - proxyHost.domain_names, - host.get("forward_host"), - host.get("forward_port"), - host.get("advanced_config"), - host.get("forward_scheme") - ) - - if proxy_host: - return ProxyHost( - proxy_id=proxy_host.get("id"), - domain_names=proxy_host.get("domain_names"), - ) - else: - raise CustomException() + return proxyManager.update_proxy_by_app(proxy_id,domain_names) def _init_local_repo_and_push_to_remote(self,local_path:str,repo_url:str): """ diff --git a/apphub/src/services/proxy_manager.py b/apphub/src/services/proxy_manager.py index 04886250..571dee74 100755 --- a/apphub/src/services/proxy_manager.py +++ b/apphub/src/services/proxy_manager.py @@ -128,26 +128,36 @@ class ProxyManager: else: return response.json() - def update_proxy_by_app(self,proxy_id:int,domain_names: list[str],forward_host: str,forward_port: int,advanced_config: str = "",forward_scheme: str = "http"): - response = self.nginx.update_proxy_host( - proxy_id=proxy_id, - domain_names=domain_names, - forward_scheme=forward_scheme, - forward_host=forward_host, - forward_port=forward_port, - advanced_config=advanced_config, - ) - if response.status_code == 200: - return response.json() - elif response.status_code == 500: - logger.error(f"Update proxy for app:{forward_host} error:{response.status_code}:{response.text}") - raise CustomException() - else: - logger.error(f"Update proxy for app:{forward_host} error:{response.status_code}:{response.text}") + def update_proxy_by_app(self,proxy_id:int,domain_names: list[str]): + # Get proxy host by id + req_json = self.get_proxy_host_by_id(proxy_id) + if req_json is None: raise CustomException( status_code=400, message=f"Invalid Request", - details=f"{json.loads(response.text).get('error',{}).get('message')}" + details=f"Proxy host:{proxy_id} not found" + ) + # update domain_names + req_json["domain_names"] = domain_names + keys_to_delete = ["id","created_on","modified_on","owner_user_id","enabled","certificate","owner","access_list","use_default_location","ipv6"] + for key in keys_to_delete: + req_json.pop(key, None) + + response = self.nginx.update_proxy_host(proxy_id=proxy_id, json=req_json) + if response.status_code == 200: + return response.json() + elif response.status_code == 500: + logger.error(f"Update proxy for app:{req_json['forward_host']} error:{response.status_code}:{response.text}") + raise CustomException() + else: + logger.error(f"Update proxy for app:{req_json['forward_host']} error:{response.status_code}:{response.text}") + response_dict = json.loads(response.text) + error_dict = response_dict.get('error', {}) + details = error_dict.get('message') + raise CustomException( + status_code=400, + message=f"Invalid Request", + details=details ) def get_proxy_host_by_app(self,app_id:str):