Implement tunnel API endpoints
linuztx committed
May 15, 2025 at 09:06 UTC
a4d80ecb4f2545e6a8d6f59a2b7190bf51639055
1 file changed
+44
python/api/tunnel.py
new
+44
@@ -0,0 +1,44 @@
1
+from flask import Request, Response
2
+from python.helpers.api import ApiHandler
3
+from python.helpers.tunnel_manager import TunnelManager
4
+
5
+class Tunnel(ApiHandler):
6
+ async def process(self, input: dict, request: Request) -> dict | Response:
7
+ action = input.get("action", "get")
8
+
9
+ tunnel_manager = TunnelManager.get_instance()
10
+
11
+ if action == "create":
12
+ # Get the port from the request or use default
13
+ port = input.get("port", 5000)
14
+ tunnel_url = tunnel_manager.start_tunnel(port)
15
+ if tunnel_url is None:
16
+ # Add a little delay and check again - tunnel might be starting
17
+ import time
18
+ time.sleep(2)
19
+ tunnel_url = tunnel_manager.get_tunnel_url()
20
+
21
+ return {
22
+ "success": tunnel_url is not None,
23
+ "tunnel_url": tunnel_url,
24
+ "message": "Tunnel creation in progress" if tunnel_url is None else "Tunnel created successfully"
25
+ }
26
+
27
+ elif action == "stop":
28
+ success = tunnel_manager.stop_tunnel()
29
+ return {
30
+ "success": success
31
+ }
32
+
33
+ elif action == "get":
34
+ tunnel_url = tunnel_manager.get_tunnel_url()
35
+ return {
36
+ "success": tunnel_url is not None,
37
+ "tunnel_url": tunnel_url,
38
+ "is_running": tunnel_manager.is_running
39
+ }
40
+
41
+ return {
42
+ "success": False,
43
+ "error": "Invalid action. Use 'create', 'stop', or 'get'."
44
+ }