fix(webui): make headless login self-explanatory (#5735)
* fix(webui): guide headless login * fix(webui): correct SSH handoff and password fallback
This commit is contained in:
1 parent
7cede64f07
commit
f8c1f2ede0
15 files changed
+265
-86
No files matched your search
@@ -1,6 +1,7 @@
|
||||
"""Shared WebUI setup, URL, health, and browser helpers."""
|
||||
|
||||
import os
|
||||
import shutil
|
||||
import sys
|
||||
import time
|
||||
import webbrowser
|
||||
@@ -47,6 +48,7 @@ __all__ = [
|
||||
"_open_webui_browser",
|
||||
"_prepare_webui_bundle_for_gateway",
|
||||
"_print_foreground_port_conflict",
|
||||
"_print_webui_manual_access",
|
||||
"_print_webui_foreground_lifecycle",
|
||||
"_resolve_webui_config_path",
|
||||
"_tcp_endpoint_reachable",
|
||||
@@ -62,6 +64,8 @@ __all__ = [
|
||||
|
||||
console = Console()
|
||||
|
||||
_TEXT_ONLY_BROWSERS = frozenset({"elinks", "links", "links2", "lynx", "w3m"})
|
||||
|
||||
|
||||
def _launch_browser(url: str) -> bool:
|
||||
"""Open *url* and request a foreground browser window."""
|
||||
@@ -74,6 +78,36 @@ def _launch_browser(url: str) -> bool:
|
||||
return bool(webbrowser.open(url, new=2, autoraise=True))
|
||||
|
||||
|
||||
def _text_only_browser_name() -> str | None:
|
||||
"""Return the configured text browser name when it cannot run the WebUI."""
|
||||
if sys.platform in {"darwin", "win32"}:
|
||||
return None
|
||||
|
||||
try:
|
||||
browser = webbrowser.get()
|
||||
except webbrowser.Error:
|
||||
return None
|
||||
|
||||
command = str(getattr(browser, "name", "") or "").strip()
|
||||
if not command:
|
||||
return None
|
||||
|
||||
import shlex
|
||||
|
||||
try:
|
||||
executable = Path(shlex.split(command)[0])
|
||||
except (IndexError, ValueError):
|
||||
return None
|
||||
|
||||
names = {executable.name.lower()}
|
||||
resolved_executable = Path(shutil.which(str(executable)) or executable)
|
||||
try:
|
||||
names.add(resolved_executable.resolve(strict=False).name.lower())
|
||||
except OSError:
|
||||
pass
|
||||
return next((name for name in names if name in _TEXT_ONLY_BROWSERS), None)
|
||||
|
||||
|
||||
def _launch_macos_browser(url: str) -> bool:
|
||||
"""Deliver URLs through Launch Services, never a credential-bearing argv."""
|
||||
import ctypes
|
||||
@@ -209,8 +243,8 @@ def _validate_gateway_startup(config: Config) -> str | None:
|
||||
)
|
||||
console.print(
|
||||
Text(
|
||||
f"If prompted, enter the configured channels.websocket.{secret_key} "
|
||||
f"value (see {config_path}).",
|
||||
f"If prompted, enter the WebUI password from "
|
||||
f"channels.websocket.{secret_key} in {config_path}.",
|
||||
style="dim",
|
||||
)
|
||||
)
|
||||
@@ -309,27 +343,29 @@ def _ensure_local_webui_channel(
|
||||
*,
|
||||
port: int | None,
|
||||
yes: bool,
|
||||
) -> tuple[bool, bool]:
|
||||
) -> bool:
|
||||
"""Enable the local WebUI channel with safe localhost defaults."""
|
||||
from nanobot.channels.websocket.runtime import WebSocketConfig
|
||||
|
||||
current: Any = getattr(config.channels, "websocket", None) or {}
|
||||
model = WebSocketConfig.model_validate(current)
|
||||
changed = False
|
||||
generated_secret = False
|
||||
|
||||
needs_enable = not model.enabled
|
||||
needs_port = port is not None and model.port != port
|
||||
needs_secret = not model.token_issue_secret.strip() and not model.token.strip()
|
||||
if not needs_enable and not needs_port and not needs_secret:
|
||||
return False, False
|
||||
return False
|
||||
|
||||
target_port = port if port is not None else model.port
|
||||
console.print()
|
||||
console.print("[bold]Local WebUI setup[/bold]")
|
||||
console.print(f" URL: [cyan]http://127.0.0.1:{target_port}[/cyan]")
|
||||
console.print(" Bind: [cyan]127.0.0.1 only[/cyan] (not exposed to your LAN)")
|
||||
console.print(" Auth: generated WebUI bootstrap secret stored in config")
|
||||
if needs_secret:
|
||||
console.print(" WebUI password: will be generated and stored in config")
|
||||
else:
|
||||
console.print(" WebUI password: already stored in config")
|
||||
console.print(
|
||||
" LAN access requires an explicit host change plus a WebUI password in config."
|
||||
)
|
||||
@@ -352,10 +388,9 @@ def _ensure_local_webui_channel(
|
||||
|
||||
model.token_issue_secret = secrets.token_urlsafe(32)
|
||||
changed = True
|
||||
generated_secret = True
|
||||
|
||||
setattr(config.channels, "websocket", model.model_dump(by_alias=True, exclude_none=True))
|
||||
return changed, generated_secret
|
||||
return changed
|
||||
|
||||
|
||||
def _warn_webui_bind_scope(config: Config) -> None:
|
||||
@@ -464,18 +499,57 @@ def _print_foreground_port_conflict(
|
||||
)
|
||||
|
||||
|
||||
def _open_webui_browser(url: str, *, wait: bool = True) -> None:
|
||||
def _open_webui_browser(url: str, *, wait: bool = True) -> bool:
|
||||
"""Open the WebUI in the user's default browser, with a copyable fallback."""
|
||||
if wait:
|
||||
_wait_for_webui(url)
|
||||
display_url = _webui_display_url(url)
|
||||
text_browser = _text_only_browser_name()
|
||||
if text_browser:
|
||||
console.print(
|
||||
f"[yellow]The configured browser ({escape(text_browser)}) cannot run the WebUI "
|
||||
"because it does not support JavaScript.[/yellow]"
|
||||
)
|
||||
return False
|
||||
try:
|
||||
if _launch_browser(url):
|
||||
console.print(f"[green]✓[/green] Opened WebUI: [cyan]{display_url}[/cyan]")
|
||||
return True
|
||||
else:
|
||||
console.print(f"[yellow]Could not open browser; visit {display_url}[/yellow]")
|
||||
console.print("[yellow]Could not open a browser automatically.[/yellow]")
|
||||
except Exception as exc:
|
||||
console.print(f"[yellow]Could not open browser ({exc}); visit {display_url}[/yellow]")
|
||||
console.print(f"[yellow]Could not open a browser automatically ({escape(str(exc))}).[/yellow]")
|
||||
return False
|
||||
|
||||
|
||||
def _print_webui_manual_access(config: Config, config_path: Path, url: str) -> None:
|
||||
"""Print a complete local or SSH-tunnel browser handoff without exposing credentials."""
|
||||
from urllib.parse import urlparse
|
||||
|
||||
browser_url = url.split("/#/", 1)[0]
|
||||
parsed = urlparse(browser_url)
|
||||
port = parsed.port or (443 if parsed.scheme == "https" else 80)
|
||||
tunnel_host = _host_for_local_browser(parsed.hostname or "127.0.0.1")
|
||||
tunnel_url = f"{parsed.scheme}://127.0.0.1:{port}"
|
||||
ws_cfg = _webui_config_dict(config)
|
||||
password_key = (
|
||||
"tokenIssueSecret" if str(ws_cfg.get("tokenIssueSecret") or "").strip() else "token"
|
||||
)
|
||||
|
||||
console.print()
|
||||
console.print("[bold]Open the WebUI manually[/bold]")
|
||||
console.print(f" WebUI: [cyan]{browser_url}[/cyan]")
|
||||
console.print(
|
||||
" WebUI password: "
|
||||
f"[cyan]channels.websocket.{password_key}[/cyan] in [cyan]{config_path}[/cyan]"
|
||||
)
|
||||
console.print()
|
||||
console.print("If nanobot is running on another machine, create an SSH tunnel from yours:")
|
||||
console.print(
|
||||
f" [cyan]ssh -N -L {port}:{tunnel_host}:{port} <user>@<server>[/cyan]"
|
||||
)
|
||||
console.print("Replace [cyan]<user>[/cyan] and [cyan]<server>[/cyan] and keep the tunnel open.")
|
||||
console.print(f"Then open [cyan]{tunnel_url}[/cyan] on your computer.")
|
||||
|
||||
|
||||
def _print_webui_foreground_lifecycle(*, attached: bool) -> None:
|
||||
|
||||
Reference in new issue
Block a user