Initial upload: API Mock CLI v0.1.0
Some checks failed
CI / test (3.10) (push) Has been cancelled
CI / test (3.11) (push) Has been cancelled
CI / test (3.12) (push) Has been cancelled
CI / test (3.9) (push) Has been cancelled
CI / lint (push) Has been cancelled
CI / type-check (push) Has been cancelled
CI / build (push) Has been cancelled

This commit is contained in:
2026-01-29 13:53:46 +00:00
parent adbd253773
commit ff09c2d5f6

42
src/core/tunnel.py Normal file
View File

@@ -0,0 +1,42 @@
import os
import time
from typing import Optional, Dict, Any
from pyngrok import ngrok
class TunnelError(Exception):
pass
class Tunnel:
def __init__(self, authtoken: Optional[str] = None):
self.authtoken = authtoken or os.environ.get("NGROK_AUTHTOKEN")
self.tunnel: Optional[ngrok.HTTPEndpoint] = None
self.url: Optional[str] = None
def start(self, port: int, region: str = "us") -> str:
try:
if self.authtoken:
ngrok.set_auth_token(self.authtoken)
self.tunnel = ngrok.connect(port, "http")
self.url = self.tunnel.public_url if self.tunnel else None
if not self.url:
raise TunnelError("Failed to get tunnel URL")
return self.url
except Exception as e:
raise TunnelError(f"Failed to create tunnel: {e}")
def stop(self) -> None:
try:
if self.tunnel:
ngrok.disconnect(self.tunnel.public_url)
self.tunnel = None
self.url = None
except Exception:
pass
def get_url(self) -> Optional[str]:
return self.url
def is_active(self) -> bool:
return self.tunnel is not None and self.url is not None