2025-09-08 13:02:57 -07:00

58 lines
1.7 KiB
Python

"""
Utility functions for voicebot.
This module provides common utility functions used throughout the application.
"""
import ssl
def http_base_url(server_url: str) -> str:
"""Convert ws:// or wss:// to http(s) and ensure no trailing slash."""
if server_url.startswith("ws://"):
return "http://" + server_url[len("ws://") :].rstrip("/")
if server_url.startswith("wss://"):
return "https://" + server_url[len("wss://") :].rstrip("/")
return server_url.rstrip("/")
def ws_url(server_url: str) -> str:
"""Convert http(s) to ws(s) if needed."""
if server_url.startswith("http://"):
return "ws://" + server_url[len("http://") :].rstrip("/")
if server_url.startswith("https://"):
return "wss://" + server_url[len("https://") :].rstrip("/")
return server_url.rstrip("/")
def create_ssl_context(insecure: bool = False) -> ssl.SSLContext | None:
"""Create SSL context for connections."""
if not insecure:
return None
ssl_ctx = ssl.create_default_context()
ssl_ctx.check_hostname = False
ssl_ctx.verify_mode = ssl.CERT_NONE
return ssl_ctx
def log_network_info():
"""Log network information for debugging."""
from shared.logger import logger
try:
import socket
import subprocess
hostname = socket.gethostname()
local_ip = socket.gethostbyname(hostname)
logger.info(f"Container hostname: {hostname}, local IP: {local_ip}")
# Get all network interfaces
result = subprocess.run(
["ip", "addr", "show"], capture_output=True, text=True
)
logger.info(f"Network interfaces:\n{result.stdout}")
except Exception as e:
logger.warning(f"Could not get network info: {e}")