diff --git a/src/common/utils/api_models.py b/src/common/utils/api_models.py index 35c46bd4..de2d51fa 100644 --- a/src/common/utils/api_models.py +++ b/src/common/utils/api_models.py @@ -337,6 +337,8 @@ class CustomConfigServiceId(BaseModel): class PluginId(BaseModel): plugin_id: str = Field(examples=["blacklist"], description="The plugin id") +class PluginPageName(BaseModel): + plugin_page_name: str = Field(examples=["index"], description="The plugin page name") class InstanceHostname(BaseModel): instance_hostname: str = Field(examples=["bunkerweb-1"], description="The instance hostname") diff --git a/src/ui/routes/dashboard.py b/src/ui/routes/dashboard.py index b1fe0fb4..5f4d9741 100644 --- a/src/ui/routes/dashboard.py +++ b/src/ui/routes/dashboard.py @@ -73,30 +73,6 @@ def actions(): return render_template("actions.html") -@dashboard.route(f"{PREFIX}/plugins/external", methods=["GET"]) -@jwt_required() -def get_custom_page(): - args = request.args.to_dict() - plugin_id = args.get("plugin_id") or "" - if not plugin_id: - raise InternalServerError(response=Response(status=404), description="No plugin id found to get custom page.") - - # Retrieve template from CORE - try: - # page = requests.get(f"{CORE_API}/plugins/external/{plugin_id}/page") # TODO fix this - page = requests.get(f"TODO/plugins/external/{plugin_id}/page") - if not str(page.status_code).startswith("2"): - raise InternalServerError(response=Response(status=404), description=f"No custom page found for plugin {plugin_id}.") - except: - raise InternalServerError(response=Response(status=500), description=f"Error while trying to get custom page for plugin {plugin_id}.") - # Send source code as template - try: - content = page.content.decode("utf-8") - return render_template_string(content) - except: - raise InternalServerError(response=Response(status=500), description=f"Error while sending custom page for plugin {plugin_id}.") - - @dashboard.route(f"{PREFIX}/plugins") @jwt_required() def plugins(): diff --git a/src/ui/routes/external.py b/src/ui/routes/external.py index 1d2faab8..eb9dd686 100644 --- a/src/ui/routes/external.py +++ b/src/ui/routes/external.py @@ -25,6 +25,39 @@ PREFIX = "/api/external" external = Blueprint("external", __name__) +@external.route(f"{PREFIX}/page//", methods=["GET"]) +@jwt_required() +@model_validator(params={"plugin_id": "PluginId", "plugin_page_name" : "PluginPageName"}) +def get_custom_page(plugin_id, plugin_page_name): + # Check if plugin id exists + is_plugin = False + try: + plugins = get_core_format_res(f"{CORE_API}/plugins") + for item in plugins["data"]: + if plugin_id == item["id"]: + is_plugin = True + break + + if not is_plugin: + raise HTTPException(response=Response(status=500), description=f"Plugin {plugin_id} not find to execute action.") + + except: + raise HTTPException(response=Response(status=500), description=f"Error while trying to find plugin {plugin_id}.") + + # Retrieve template from CORE + try: + page = requests.get(f"{CORE_API}/plugins/external/page/{plugin_id}/{page}") + if not str(page.status_code).startswith("2"): + raise InternalServerError(response=Response(status=404), description=f"No custom page found for plugin {plugin_id}.") + except: + raise InternalServerError(response=Response(status=500), description=f"Error while trying to get custom page for plugin {plugin_id}.") + # Send source code as template + try: + content = page.content.decode("utf-8") + return render_template_string(content) + except: + raise InternalServerError(response=Response(status=500), description=f"Error while sending custom page for plugin {plugin_id}.") + # Communicate with CORE retrieving ui api file and executing a specific function (action name) @external.route(f"{PREFIX}//action", methods=["GET", "POST", "PUT", "DELETE"])