Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

fix: use IPv4 loopback for frontend proxy (#784)

s4kura110dd9027488
diff --git a/deeptutor/runtime/launcher.py b/deeptutor/runtime/launcher.py
index b25e84b58..9f055ce41 100644
--- a/deeptutor/runtime/launcher.py
+++ b/deeptutor/runtime/launcher.py
@@ -955,7 +955,7 @@ def start(home: str | Path | None = None, *, dev: bool = False) -> None:
backend_port = settings.backend_port
frontend_port = settings.frontend_port
- backend_url = f"http://localhost:{backend_port}"
+ backend_url = f"http://127.0.0.1:{backend_port}"
api_base = (
runtime_env.get("NEXT_PUBLIC_API_BASE_EXTERNAL")
or runtime_env.get("NEXT_PUBLIC_API_BASE")
@@ -991,7 +991,7 @@ def start(home: str | Path | None = None, *, dev: bool = False) -> None:
if (resolved_backend, resolved_frontend) != (backend_port, frontend_port):
backend_port, frontend_port = resolved_backend, resolved_frontend
runtime_env = export_runtime_settings_to_env(overwrite=True)
- backend_url = f"http://localhost:{backend_port}"
+ backend_url = f"http://127.0.0.1:{backend_port}"
api_base = (
runtime_env.get("NEXT_PUBLIC_API_BASE_EXTERNAL")
or runtime_env.get("NEXT_PUBLIC_API_BASE")
@@ -1032,7 +1032,7 @@ def start(home: str | Path | None = None, *, dev: bool = False) -> None:
# The Next.js middleware (web/proxy.ts) runs in the frontend's Node runtime
# and reads these at request time to forward /api/* and /ws/* to the backend
# and to gate the login redirect. The browser uses relative paths, so the
- # frontend server reaches the backend on localhost at the resolved port —
+ # frontend server reaches the backend on the IPv4 loopback at the resolved port —
# use backend_url (not api_base, which may be an external browser URL).
common_env["DEEPTUTOR_API_BASE_URL"] = backend_url
common_env["DEEPTUTOR_AUTH_ENABLED"] = "true" if auth_enabled else "false"
diff --git a/tests/runtime/test_launcher.py b/tests/runtime/test_launcher.py
index 910228eae..a306e3014 100644
--- a/tests/runtime/test_launcher.py
+++ b/tests/runtime/test_launcher.py
@@ -401,3 +401,71 @@ def test_source_production_build_is_reused_until_an_input_changes(
(["npm", "run", "build"], source, launcher.SOURCE_PRODUCTION_DIST_DIR),
]
assert next_env.read_text(encoding="utf-8") == "// developer dist types\n"
+
+
[email protected]("resolved_backend_port", [8001, 8123])
+def test_start_uses_ipv4_loopback_for_frontend_proxy(
+ tmp_path: Path,
+ monkeypatch: pytest.MonkeyPatch,
+ resolved_backend_port: int,
+) -> None:
+ from deeptutor.services import config as config_module
+ from deeptutor.services import setup as setup_module
+
+ settings_dir = tmp_path / "data" / "user" / "settings"
+ settings = config_module.LaunchSettings(
+ backend_port=8001,
+ frontend_port=3782,
+ language="en",
+ source="test",
+ settings_dir=settings_dir,
+ interface_json_path=settings_dir / "interface.json",
+ system_json_path=settings_dir / "system.json",
+ )
+ captured_env: dict[str, str] = {}
+
+ monkeypatch.setattr(launcher, "_relax_console_encoding", lambda: None)
+ monkeypatch.setattr(launcher, "_reset_runtime_singletons", lambda: None)
+ monkeypatch.setattr(config_module, "ensure_runtime_settings_files", lambda: None)
+ monkeypatch.setattr(config_module, "load_launch_settings", lambda _home: settings)
+ monkeypatch.setattr(
+ config_module,
+ "export_runtime_settings_to_env",
+ lambda **_kwargs: {},
+ )
+ monkeypatch.setattr(config_module, "load_auth_settings", lambda: {"enabled": False})
+ monkeypatch.setattr(config_module, "get_ws_max_size", lambda: 1024)
+ monkeypatch.setattr(setup_module, "init_user_directories", lambda _home: None)
+ monkeypatch.setattr(launcher, "resolve_language", lambda: "en")
+ monkeypatch.setattr(launcher, "print_banner", lambda **_kwargs: None)
+ monkeypatch.setattr(launcher, "_log", lambda _message: None)
+ monkeypatch.setattr(
+ launcher,
+ "_resolve_frontend",
+ lambda *_args, **_kwargs: launcher.FrontendRuntime("source", ["npm"], tmp_path),
+ )
+ monkeypatch.setattr(launcher, "_detect_existing_source_frontend", lambda _runtime: None)
+ monkeypatch.setattr(
+ launcher,
+ "_resolve_port_conflicts",
+ lambda **_kwargs: (resolved_backend_port, 3782),
+ )
+ monkeypatch.setattr(launcher, "_install_signal_handlers", lambda _callback: None)
+ monkeypatch.setattr(launcher.atexit, "register", lambda _callback: None)
+ monkeypatch.setattr(launcher, "_wait_for_http", lambda **_kwargs: None)
+ monkeypatch.setattr(launcher, "_terminate", lambda _process: None)
+
+ def _capture_spawn(_command, *, cwd, env, name):
+ assert cwd == tmp_path
+ if name == "backend":
+ return launcher.ManagedProcess("backend", object(), None)
+ assert name == "frontend"
+ captured_env.update(env)
+ raise RuntimeError("captured launch environment")
+
+ monkeypatch.setattr(launcher, "_spawn", _capture_spawn)
+
+ with pytest.raises(RuntimeError, match="captured launch environment"):
+ launcher.start(tmp_path)
+
+ assert captured_env["DEEPTUTOR_API_BASE_URL"] == (f"http://127.0.0.1:{resolved_backend_port}")