Skip to content

fix: webui在使用反向代理时url无法正确处理#257

Open
kakaa-nju wants to merge 2 commits into
MistEO:mainfrom
kakaa-nju:main
Open

fix: webui在使用反向代理时url无法正确处理#257
kakaa-nju wants to merge 2 commits into
MistEO:mainfrom
kakaa-nju:main

Conversation

@kakaa-nju

@kakaa-nju kakaa-nju commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

bug触发条件:

  1. 在 windows 主机上开启 webui 并允许局域网访问
  2. 在另一台服务器的 nginx 上配置反向代理 “maaend.example.icu” -> "192.168.1.114:12701"
  3. 在局域网内另一台电脑访问 “maaend.example.icu”
    结果:wss 和 /api/* 的连接会请求 “maaend.example.icu:12701/{uri}”

本 pr 使用 window.location.host 获取正确的 url,并在失败时 fallback 到原本的逻辑

Sourcery 总结

修复 Web UI 中的 URL 处理问题,使在通过反向代理访问时 WebSocket 和 API 请求能够正常工作,并在直接连接后端时提供合理的回退机制。

Bug 修复:

  • 当在反向代理之后使用 Web UI 时,通过基于 window.location.hostorigin 来解析 WebSocket 和 API 端点 URL,解决端点 URL 不正确的问题。
  • 在自动主机检测不可用的情况下,通过显式的主机名/端口回退机制,确保桌面端(Tauri)和直接 LAN/后端访问路径仍然可以正常工作。
Original summary in English

Summary by Sourcery

Fix URL handling in the web UI so that WebSocket and API requests work correctly when accessed via a reverse proxy, with sensible fallbacks for direct backend connections.

Bug Fixes:

  • Resolve incorrect WebSocket and API endpoint URLs when the web UI is used behind a reverse proxy by basing them on window.location.host and origin.
  • Ensure desktop (Tauri) and direct LAN/backend access paths still function via explicit hostname/port fallbacks when automatic host detection is unavailable.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - 我在这里给出了一些整体性的反馈:

  • getWsUrlgetApiBase 中新增的 window.location.host 检查几乎总是会提前返回,从而实际忽略了在非代理或开发/Tauri 场景下的 serverPort/backendPort 回退逻辑;建议不要简单依赖 window.location.host,而是用更明确的条件来决定是否启用“代理模式”行为。
  • getWsUrlgetApiBase 中硬编码的回退端口 12701,可能会与真实配置不一致;更安全的做法是从现有的配置或状态派生这些值,而不是在此直接写死常量。
给 AI Agents 的提示
请根据这次代码审查中的评论进行修改:

## 整体点评
-`getWsUrl``getApiBase` 中新增的 `window.location.host` 检查几乎总是会提前返回,从而实际忽略了在非代理或开发/Tauri 场景下的 `serverPort`/`backendPort` 回退逻辑;建议不要简单依赖 `window.location.host`,而是用更明确的条件来决定是否启用“代理模式”行为。
-`getWsUrl``getApiBase` 中硬编码的回退端口 `12701`,可能会与真实配置不一致;更安全的做法是从现有的配置或状态派生这些值,而不是在此直接写死常量。

Sourcery 对开源项目是免费的——如果你觉得我们的审查有帮助,欢迎分享 ✨
帮我变得更有用!请在每条评论上点 👍 或 👎,我会根据你的反馈改进后续的代码审查。
Original comment in English

Hey - I've left some high level feedback:

  • The new window.location.host checks in getWsUrl and getApiBase will almost always short‑circuit the fallback branches, effectively ignoring serverPort/backendPort in non‑proxy or dev/Tauri scenarios; consider gating the proxy-style behavior on a more explicit condition instead of window.location.host.
  • Hardcoded fallback ports like 12701 in getWsUrl and getApiBase may diverge from actual configuration; it would be safer to derive these values from existing config/state rather than embedding constants here.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The new `window.location.host` checks in `getWsUrl` and `getApiBase` will almost always short‑circuit the fallback branches, effectively ignoring `serverPort`/`backendPort` in non‑proxy or dev/Tauri scenarios; consider gating the proxy-style behavior on a more explicit condition instead of `window.location.host`.
- Hardcoded fallback ports like `12701` in `getWsUrl` and `getApiBase` may diverge from actual configuration; it would be safer to derive these values from existing config/state rather than embedding constants here.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@kakaa-nju

Copy link
Copy Markdown
Contributor Author
场景 window.location.host 行为
通过 Nginx 访问 https://maaend.kakaa.fun "maaend.kakaa.fun" 使用当前 host ✅
直接访问后端 http://192.168.1.100:12701 "192.168.1.100:12701" 使用当前 host ✅
Tauri WebView 不会调用这两个函数 无关

在 WebUI 场景下,window.location.host 始终指向用户实际访问的地址,无论是通过代理还是直连,都是正确的服务端地址。

当前回退 host 和 port 均先从配置中读取,如果未设置才会回退到硬编码

Hey - 我在这里给出了一些整体性的反馈:

  • getWsUrlgetApiBase 中新增的 window.location.host 检查几乎总是会提前返回,从而实际忽略了在非代理或开发/Tauri 场景下的 serverPort/backendPort 回退逻辑;建议不要简单依赖 window.location.host,而是用更明确的条件来决定是否启用“代理模式”行为。
  • getWsUrlgetApiBase 中硬编码的回退端口 12701,可能会与真实配置不一致;更安全的做法是从现有的配置或状态派生这些值,而不是在此直接写死常量。

给 AI Agents 的提示

请根据这次代码审查中的评论进行修改:

## 整体点评
- 在 `getWsUrl` 和 `getApiBase` 中新增的 `window.location.host` 检查几乎总是会提前返回,从而实际忽略了在非代理或开发/Tauri 场景下的 `serverPort`/`backendPort` 回退逻辑;建议不要简单依赖 `window.location.host`,而是用更明确的条件来决定是否启用“代理模式”行为。
- 像 `getWsUrl` 和 `getApiBase` 中硬编码的回退端口 `12701`,可能会与真实配置不一致;更安全的做法是从现有的配置或状态派生这些值,而不是在此直接写死常量。

Sourcery 对开源项目是免费的——如果你觉得我们的审查有帮助,欢迎分享 ✨

帮我变得更有用!请在每条评论上点 👍 或 👎,我会根据你的反馈改进后续的代码审查。
Original comment in English

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant