Skip to content

Agent Zero AI framework

feat: Tunnel Manager

linuztx43b3af5902c9
diff --git a/python/helpers/tunnel_manager.py b/python/helpers/tunnel_manager.py
new file mode 100644
index 000000000..192249273
--- /dev/null
+++ b/python/helpers/tunnel_manager.py
@@ -0,0 +1,74 @@
+from flaredantic import FlareTunnel, FlareConfig
+import threading
+
+# Singleton to manage the tunnel instance
+class TunnelManager:
+ _instance = None
+ _lock = threading.Lock()
+
+ @classmethod
+ def get_instance(cls):
+ with cls._lock:
+ if cls._instance is None:
+ cls._instance = cls()
+ return cls._instance
+
+ def __init__(self):
+ self.tunnel = None
+ self.tunnel_url = None
+ self.is_running = False
+
+ def start_tunnel(self, port=5000):
+ """Start a new tunnel or return the existing one's URL"""
+ if self.is_running and self.tunnel_url:
+ return self.tunnel_url
+
+ # Create and start a new tunnel
+ config = FlareConfig(
+ port=port,
+ verbose=True,
+ timeout=60 # Increase timeout from default 30 to 60 seconds
+ )
+
+ try:
+ # Start tunnel in a separate thread to avoid blocking
+ def run_tunnel():
+ try:
+ self.tunnel = FlareTunnel(config)
+ self.tunnel.start()
+ self.tunnel_url = self.tunnel.tunnel_url
+ self.is_running = True
+ except Exception as e:
+ print(f"Error in tunnel thread: {str(e)}")
+
+ tunnel_thread = threading.Thread(target=run_tunnel)
+ tunnel_thread.daemon = True
+ tunnel_thread.start()
+
+ # Wait for tunnel to start (max 15 seconds instead of 5)
+ for _ in range(150): # Increased from 50 to 150 iterations
+ if self.tunnel_url:
+ break
+ import time
+ time.sleep(0.1)
+
+ return self.tunnel_url
+ except Exception as e:
+ print(f"Error starting tunnel: {str(e)}")
+ return None
+
+ def stop_tunnel(self):
+ """Stop the running tunnel"""
+ if self.tunnel and self.is_running:
+ try:
+ self.tunnel.stop()
+ self.is_running = False
+ self.tunnel_url = None
+ return True
+ except Exception:
+ return False
+ return False
+
+ def get_tunnel_url(self):
+ """Get the current tunnel URL if available"""
+ return self.tunnel_url if self.is_running else None
\ No newline at end of file