| 1 | from helpers.api import ApiHandler, Request, Response |
| 2 | from helpers import runtime |
| 3 | from helpers.tunnel_manager import TunnelManager |
| 4 | |
| 5 | class Tunnel(ApiHandler): |
| 6 | async def process(self, input: dict, request: Request) -> dict | Response: |
| 7 | return await process(input) |
| 8 | |
| 9 | async def process(input: dict) -> dict | Response: |
| 10 | action = input.get("action", "get") |
| 11 | |
| 12 | tunnel_manager = TunnelManager.get_instance() |
| 13 | |
| 14 | if action == "health": |
| 15 | return {"success": True} |
| 16 | |
| 17 | if action == "create": |
| 18 | port = runtime.get_web_ui_port() |
| 19 | provider = input.get("provider", "serveo") # Default to serveo |
| 20 | tunnel_url = tunnel_manager.start_tunnel(port, provider) |
| 21 | error = tunnel_manager.get_last_error() |
| 22 | if error: |
| 23 | return { |
| 24 | "success": False, |
| 25 | "tunnel_url": None, |
| 26 | "message": error, |
| 27 | "notifications": tunnel_manager.get_notifications() |
| 28 | } |
| 29 | |
| 30 | return { |
| 31 | "success": tunnel_url is not None, |
| 32 | "tunnel_url": tunnel_url, |
| 33 | "notifications": tunnel_manager.get_notifications() |
| 34 | } |
| 35 | |
| 36 | elif action == "stop": |
| 37 | return stop() |
| 38 | |
| 39 | elif action == "get": |
| 40 | tunnel_url = tunnel_manager.get_tunnel_url() |
| 41 | return { |
| 42 | "success": tunnel_url is not None, |
| 43 | "tunnel_url": tunnel_url, |
| 44 | "is_running": tunnel_manager.is_running |
| 45 | } |
| 46 | |
| 47 | elif action == "notifications": |
| 48 | return { |
| 49 | "success": True, |
| 50 | "notifications": tunnel_manager.get_notifications(), |
| 51 | "tunnel_url": tunnel_manager.get_tunnel_url(), |
| 52 | "is_running": tunnel_manager.is_running |
| 53 | } |
| 54 | |
| 55 | return { |
| 56 | "success": False, |
| 57 | "error": "Invalid action. Use 'create', 'stop', 'get', or 'notifications'." |
| 58 | } |
| 59 | |
| 60 | def stop(): |
| 61 | tunnel_manager = TunnelManager.get_instance() |
| 62 | tunnel_manager.stop_tunnel() |
| 63 | return { |
| 64 | "success": True |
| 65 | } |