From 1d33735e2f443835e5b461ff2c8c8f90e15c059c Mon Sep 17 00:00:00 2001 From: Fusion Date: Wed, 20 May 2026 15:23:39 +0200 Subject: [PATCH] fix(cloudflare): use correct container port in tunnel config Cloudflared was hardcoded to route to port 8080, but containers listen on different ports (8443 for code-server, 8888 for jupyter). - Add instance_port parameter to create_tunnel and update_cloudflared_config - Fetch tool type default_port when creating tunnels - Route to correct internal port instead of hardcoded 8080 --- apps/api/src/api/tool_instances.py | 10 ++++++++++ apps/api/src/services/cloudflare_tunnel.py | 6 +++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/apps/api/src/api/tool_instances.py b/apps/api/src/api/tool_instances.py index 899b173..0c56ed0 100644 --- a/apps/api/src/api/tool_instances.py +++ b/apps/api/src/api/tool_instances.py @@ -402,11 +402,16 @@ async def start_instance( instance.last_started_at = datetime.now() await session.commit() + # Get tool type for default port + tool_type = await session.get(ToolType, instance.tool_type_id) + instance_port = tool_type.default_port if tool_type and tool_type.default_port else 8080 + # Create Cloudflare tunnel for public access try: tunnel_info = await create_tunnel( instance_name=instance.name, instance_id=str(instance.id), + instance_port=instance_port, ) instance.tunnel_id = tunnel_info["tunnel_id"] instance.public_url = tunnel_info["public_url"] @@ -539,11 +544,16 @@ async def restart_instance( instance.status = "running" instance.last_started_at = datetime.now() + # Get tool type for default port + tool_type = await session.get(ToolType, instance.tool_type_id) + instance_port = tool_type.default_port if tool_type and tool_type.default_port else 8080 + # Create new tunnel try: tunnel_info = await create_tunnel( instance_name=instance.name, instance_id=str(instance.id), + instance_port=instance_port, ) instance.tunnel_id = tunnel_info["tunnel_id"] instance.public_url = tunnel_info["public_url"] diff --git a/apps/api/src/services/cloudflare_tunnel.py b/apps/api/src/services/cloudflare_tunnel.py index 8d99e83..0810dec 100644 --- a/apps/api/src/services/cloudflare_tunnel.py +++ b/apps/api/src/services/cloudflare_tunnel.py @@ -25,6 +25,7 @@ def _get_headers(settings: Settings) -> dict[str, str]: async def create_tunnel( instance_name: str, instance_id: str, + instance_port: int = 8080, settings: Settings | None = None, ) -> dict[str, str]: """Create a Cloudflare tunnel for an instance. @@ -32,6 +33,7 @@ async def create_tunnel( Args: instance_name: Name of the instance (used for tunnel name) instance_id: UUID of the instance + instance_port: Internal port the container listens on settings: Optional settings override Returns: @@ -97,6 +99,7 @@ async def create_tunnel( tunnel_token=tunnel_token, hostname=hostname, instance_name=instance_name, + instance_port=instance_port, settings=settings, ) @@ -161,6 +164,7 @@ async def update_cloudflared_config( tunnel_token: str, hostname: str, instance_name: str, + instance_port: int = 8080, settings: Settings | None = None, ) -> None: """Update the cloudflared config.yml with a new tunnel. @@ -206,7 +210,7 @@ async def update_cloudflared_config( # Add ingress rule for this instance ingress_rule = { "hostname": hostname, - "service": f"http://{instance_name}:8080", + "service": f"http://{instance_name}:{instance_port}", } # Remove existing rule for this hostname if present