tunnel ports fix

frdel committed May 15, 2025 at 11:00 UTC 02833a21cf07494f55952a44992212e3db7da023
8 files changed +42 -38
docker/run/Dockerfile
+1 -1
@@ -41,7 +41,7 @@ RUN bash /ins/post_install.sh $BRANCH
41 # Expose ports
42 EXPOSE 22 80
43
44 -RUN chmod +x /exe/initialize.sh /exe/run_A0.sh /exe/run_searxng.sh
44 +RUN chmod +x /exe/initialize.sh /exe/run_A0.sh /exe/run_searxng.sh /exe/run_tunnel_api.sh
45
46 # initialize runtime and switch to supervisord
47 CMD ["/exe/initialize.sh", "$BRANCH"]
python/api/tunnel.py
+3 -3
@@ -1,4 +1,5 @@
1 from flask import Request, Response
2 +from python.helpers import runtime
3 from python.helpers.api import ApiHandler
4 from python.helpers.tunnel_manager import TunnelManager
5
@@ -8,12 +9,11 @@ class Tunnel(ApiHandler):
9
10 tunnel_manager = TunnelManager.get_instance()
11
11 - if action == "verify":
12 + if action == "health":
13 return {"success": True}
14
15 if action == "create":
15 - # Get the port from the request or use default
16 - port = input.get("port", 5000)
16 + port = runtime.get_web_ui_port()
17 tunnel_url = tunnel_manager.start_tunnel(port)
18 if tunnel_url is None:
19 # Add a little delay and check again - tunnel might be starting
python/api/tunnel_proxy.py
+1 -1
@@ -17,7 +17,7 @@ class TunnelProxy(ApiHandler):
17 # first verify the service is running:
18 service_ok = False
19 try:
20 - response = requests.post(f"http://localhost:{tunnel_api_port}/", json={"action": "verify"})
20 + response = requests.post(f"http://localhost:{tunnel_api_port}/", json={"action": "health"})
21 if response.status_code == 200:
22 service_ok = True
23 except Exception as e:
python/helpers/runtime.py
+17
@@ -124,3 +124,20 @@ def call_development_function_sync(func: Union[Callable[..., T], Callable[..., A
124
125 result = result_queue.get_nowait()
126 return cast(T, result)
127 +
128 +
129 +def get_web_ui_port():
130 + web_ui_port = (
131 + get_arg("port")
132 + or int(dotenv.get_dotenv_value("WEB_UI_PORT", 0))
133 + or 5000
134 + )
135 + return web_ui_port
136 +
137 +def get_tunnel_api_port():
138 + tunnel_api_port = (
139 + get_arg("tunnel_api_port")
140 + or int(dotenv.get_dotenv_value("TUNNEL_API_PORT", 0))
141 + or 5070
142 + )
143 + return tunnel_api_port
\ No newline at end of file
python/helpers/tunnel_manager.py
+15 -13
@@ -1,35 +1,36 @@
1 from flaredantic import FlareTunnel, FlareConfig
2 import threading
3
4 +
5 # Singleton to manage the tunnel instance
6 class TunnelManager:
7 _instance = None
8 _lock = threading.Lock()
8 -
9 +
10 @classmethod
11 def get_instance(cls):
12 with cls._lock:
13 if cls._instance is None:
14 cls._instance = cls()
15 return cls._instance
15 -
16 +
17 def __init__(self):
18 self.tunnel = None
19 self.tunnel_url = None
20 self.is_running = False
20 -
21 - def start_tunnel(self, port=5000):
21 +
22 + def start_tunnel(self, port=80):
23 """Start a new tunnel or return the existing one's URL"""
24 if self.is_running and self.tunnel_url:
25 return self.tunnel_url
25 -
26 +
27 # Create and start a new tunnel
28 config = FlareConfig(
29 port=port,
30 verbose=True,
30 - timeout=60 # Increase timeout from default 30 to 60 seconds
31 + timeout=60, # Increase timeout from default 30 to 60 seconds
32 )
32 -
33 +
34 try:
35 # Start tunnel in a separate thread to avoid blocking
36 def run_tunnel():
@@ -40,23 +41,24 @@ class TunnelManager:
41 self.is_running = True
42 except Exception as e:
43 print(f"Error in tunnel thread: {str(e)}")
43 -
44 +
45 tunnel_thread = threading.Thread(target=run_tunnel)
46 tunnel_thread.daemon = True
47 tunnel_thread.start()
47 -
48 +
49 # Wait for tunnel to start (max 15 seconds instead of 5)
50 for _ in range(150): # Increased from 50 to 150 iterations
51 if self.tunnel_url:
52 break
53 import time
54 +
55 time.sleep(0.1)
54 -
56 +
57 return self.tunnel_url
58 except Exception as e:
59 print(f"Error starting tunnel: {str(e)}")
60 return None
59 -
61 +
62 def stop_tunnel(self):
63 """Stop the running tunnel"""
64 if self.tunnel and self.is_running:
@@ -68,7 +70,7 @@ class TunnelManager:
70 except Exception:
71 return False
72 return False
71 -
73 +
74 def get_tunnel_url(self):
75 """Get the current tunnel URL if available"""
74 - return self.tunnel_url if self.is_running else None
\ No newline at end of file
76 + return self.tunnel_url if self.is_running else None
run_tunnel.py
+3 -14
@@ -20,35 +20,24 @@ def run():
20 from werkzeug.serving import make_server
21
22 PrintStyle().print("Starting tunnel server...")
23 +
24 class NoRequestLoggingWSGIRequestHandler(WSGIRequestHandler):
25 def log_request(self, code="-", size="-"):
26 pass # Override to suppress request logging
27
28 # Get configuration from environment
28 - web_ui_port = (
29 - runtime.get_arg("port")
30 - or int(dotenv.get_dotenv_value("WEB_UI_PORT", 0))
31 - or 5000
32 - )
33 - # Get configuration from environment
34 - tunnel_api_port = (
35 - runtime.get_arg("tunnel_api_port")
36 - or int(dotenv.get_dotenv_value("TUNNEL_API_PORT", 0))
37 - or 5070
38 - )
29 + tunnel_api_port = runtime.get_tunnel_api_port()
30 host = (
31 runtime.get_arg("host") or dotenv.get_dotenv_value("WEB_UI_HOST") or "localhost"
32 )
33 server = None
34 lock = threading.Lock()
35 tunnel = Tunnel(app, lock)
45 -
36
37 # handle api request
38 @app.route("/", methods=["POST"])
39 async def handle_request():
50 - return await tunnel.handle_request(request=request) # type: ignore
51 -
40 + return await tunnel.handle_request(request=request) # type: ignore
41
42 try:
43 server = make_server(
run_ui.py
+1 -5
@@ -157,11 +157,7 @@ def run():
157 pass # Override to suppress request logging
158
159 # Get configuration from environment
160 - port = (
161 - runtime.get_arg("port")
162 - or int(dotenv.get_dotenv_value("WEB_UI_PORT", 0))
163 - or 5000
164 - )
160 + port = runtime.get_web_ui_port()
161 host = (
162 runtime.get_arg("host") or dotenv.get_dotenv_value("WEB_UI_HOST") or "localhost"
163 )
webui/js/tunnel.js
+1 -1
@@ -177,7 +177,7 @@ document.addEventListener('alpine:init', () => {
177 },
178 body: JSON.stringify({
179 action: 'create',
180 - port: window.location.port || (window.location.protocol === 'https:' ? 443 : 80)
180 + // port: window.location.port || (window.location.protocol === 'https:' ? 443 : 80)
181 }),
182 });
183