This commit is contained in:
董宏凯 committed 2026-08-14 11:15:37 +08:00
commit c4d244881f
14 files changed
+1194 -107

No files matched your search

@@ -1646,6 +1646,10 @@ This gateway provides custom endpoints for models, MCP configuration, skills, an
# WeKnora's embedded frontend uses root-relative routes such as /platform,
# /assets, /locales and /api/v1/*. Register that proxy last so it stays a
# true fallback and cannot intercept DeerFlow APIs.
# Register the public-prefix routes before the unprefixed static fallback.
# Otherwise proxy_router's /{proxied_path:path} route consumes every
# /deerflow/* request before the prefixed platform/assets routes can match.
app.include_router(llmwiki.proxy_router, prefix="/deerflow")
app.include_router(llmwiki.proxy_router)
return app
@@ -9,7 +9,7 @@ import re
import time
from dataclasses import replace
from typing import Any, Literal
from urllib.parse import quote, urlencode
from urllib.parse import quote, urlencode, urlsplit
import httpx
from fastapi import APIRouter, BackgroundTasks, File, HTTPException, Query, Request, UploadFile
@@ -48,6 +48,84 @@ proxy_router = APIRouter(tags=["llmwiki-proxy"])
_MAX_UPLOAD_BYTES = 100 * 1024 * 1024
_WEKNORA_ADMIN_SESSION_CACHE: dict[str, dict[str, Any]] = {}
_WEKNORA_PUBLIC_PREFIX = "/deerflow"
_WEKNORA_PUBLIC_ROOT_PATHS = (
"/platform",
"/assets",
"/css",
"/fonts",
"/img",
"/images",
"/js",
"/locales",
"/media",
"/static",
"/tdesign-icons",
"/api/v1",
"/favicon.ico",
"/favicon.svg",
"/config.js",
"/logo.svg",
"/manifest.webmanifest",
)
_WEKNORA_PUBLIC_RELATIVE_ROOT_PATHS = (
"assets",
"css",
"fonts",
"img",
"images",
"js",
"locales",
"media",
"static",
"tdesign-icons",
)
_WEKNORA_PUBLIC_RELATIVE_FILE_PATHS = (
"config.js",
"favicon.ico",
"favicon.svg",
"logo.svg",
"manifest.webmanifest",
)
_WEKNORA_STATIC_FILE_SUFFIXES = (
".avif",
".css",
".gif",
".ico",
".jpeg",
".jpg",
".js",
".json",
".map",
".mjs",
".png",
".svg",
".ttf",
".txt",
".webmanifest",
".webp",
".woff",
".woff2",
)
_WEKNORA_STATIC_ROOT_PREFIXES = (
"/assets/",
"/css/",
"/fonts/",
"/img/",
"/images/",
"/js/",
"/locales/",
"/media/",
"/static/",
"/tdesign-icons/",
)
_WEKNORA_ROOT_ASSET_FILES = (
"/config.js",
"/favicon.ico",
"/favicon.svg",
"/logo.svg",
"/manifest.webmanifest",
)
_HOP_BY_HOP_RESPONSE_HEADERS = {
"connection",
"content-encoding",
@@ -398,6 +476,9 @@ async def _authorize_weknora_api_proxy(
return
raise HTTPException(status_code=403, detail="Only WeKnora session introspection is allowed")
if read_method and tail == ["me", "invitations", "pending-count"]:
return
if tail[:1] == ["knowledge-bases"]:
if len(tail) == 1:
if method == "GET":
@@ -501,12 +582,191 @@ def _escape_script_json(value: Any) -> str:
return json.dumps(value, ensure_ascii=False, separators=(",", ":")).replace("</", "<\\/")
def _normalize_public_prefix(value: str | None) -> str:
raw = (value or "").strip()
if not raw:
return ""
if "://" in raw:
try:
raw = urlsplit(raw).path
except ValueError:
return ""
if not raw.startswith("/"):
raw = f"/{raw}"
raw = raw.rstrip("/")
return "" if raw == "/" else raw
def _request_public_prefix(request: Request) -> str:
return _WEKNORA_PUBLIC_PREFIX
def _with_public_prefix(prefix: str, path: str) -> str:
if not path.startswith("/"):
path = f"/{path}"
return f"{prefix}{path}" if prefix else path
def _without_public_prefix(prefix: str, path: str) -> str:
if prefix and path.startswith(f"{prefix}/"):
return path[len(prefix):]
if prefix and path == prefix:
return "/"
return path
def _rewrite_weknora_html_public_paths(
html: str,
public_prefix: str,
*,
rewrite_relative: bool = True,
) -> str:
if not public_prefix:
return html
for path in _WEKNORA_PUBLIC_ROOT_PATHS:
prefixed = f"{public_prefix}{path}"
html = re.sub(
rf"(?P<prefix>[\"'`(=:\s]){re.escape(path)}(?=[/?#\"'`),;\]\}}\s>]|$)",
rf"\g<prefix>{prefixed}",
html,
)
escaped_path = rf"\\/{re.escape(path.lstrip('/'))}"
escaped_prefixed = f"\\/{prefixed.lstrip('/')}"
html = re.sub(
rf"(?<!\\/{re.escape(public_prefix.lstrip('/'))}){escaped_path}(?=[\\/?#\"'`),;\]\}}\s>]|$)",
escaped_prefixed,
html,
)
if rewrite_relative:
for path in _WEKNORA_PUBLIC_RELATIVE_ROOT_PATHS:
prefixed = f"{public_prefix}/{path}"
html = re.sub(
rf"(?P<prefix>[\"'`(=:\s])(?:\./)?{re.escape(path)}(?=/)",
rf"\g<prefix>{prefixed}",
html,
)
for path in _WEKNORA_PUBLIC_RELATIVE_FILE_PATHS:
prefixed = f"{public_prefix}/{path}"
html = re.sub(
rf"(?P<prefix>[\"'`(=:\s])(?:\./)?{re.escape(path)}(?=[?#\"'`),;\]\}}\s>]|$)",
rf"\g<prefix>{prefixed}",
html,
)
return html
def _rewrite_weknora_script_public_paths(text: str, public_prefix: str) -> str:
if not public_prefix:
return text
for path in _WEKNORA_PUBLIC_ROOT_PATHS:
prefixed = f"{public_prefix}{path}"
text = re.sub(
rf"(?P<quote>[\"'`]){re.escape(path)}(?=[/?#\"'`),;\]\}}\s]|$)",
rf"\g<quote>{prefixed}",
text,
)
return _rewrite_weknora_vite_dependency_map(text, public_prefix)
def _should_rewrite_weknora_static_response(content_type: str, upstream_path: str) -> bool:
lower_type = content_type.lower()
if any(
marker in lower_type
for marker in (
"javascript",
"text/css",
"application/json",
"manifest",
"image/svg+xml",
)
):
return True
lower_path = upstream_path.lower()
return lower_path.endswith((".js", ".css", ".json", ".webmanifest", ".svg"))
def _rewrite_weknora_vite_dependency_map(text: str, public_prefix: str) -> str:
if not public_prefix or "__vite__mapDeps" not in text:
return text
marker = "m.f||(m.f=["
start = text.find(marker)
if start < 0:
return text
end = text.find("]))", start + len(marker))
if end < 0:
return text
prefix = public_prefix.strip("/")
dependency_map = text[start:end]
dependency_map = re.sub(
r'(?P<quote>["\'])assets/',
rf"\g<quote>{prefix}/assets/",
dependency_map,
)
return f"{text[:start]}{dependency_map}{text[end:]}"
def _weknora_static_fallback_upstream_path(request: Request) -> str | None:
path = _without_public_prefix(
_request_public_prefix(request),
str(request.scope.get("path") or request.url.path),
)
if not path.startswith("/"):
path = f"/{path}"
lower_path = path.lower()
if lower_path.startswith("/api/"):
return None
if lower_path.startswith(_WEKNORA_STATIC_ROOT_PREFIXES):
return path
for root_prefix in _WEKNORA_STATIC_ROOT_PREFIXES:
index = lower_path.find(root_prefix)
if index > 0:
return path[index:]
for asset_file in _WEKNORA_ROOT_ASSET_FILES:
if lower_path.endswith(asset_file):
return asset_file
if lower_path.endswith(_WEKNORA_STATIC_FILE_SUFFIXES):
return path
return None
def _rewrite_weknora_location_header(location: str, public_prefix: str, target_base_url: str) -> str:
if not location or not public_prefix:
return location
try:
current = urlsplit(location)
upstream = urlsplit(target_base_url)
except ValueError:
return location
def proxied_path(path: str) -> str:
upstream_base_path = upstream.path.rstrip("/")
if upstream_base_path and (path == upstream_base_path or path.startswith(f"{upstream_base_path}/")):
path = path[len(upstream_base_path) :] or "/"
return _without_public_prefix(public_prefix, path)
suffix = ""
if current.query:
suffix += f"?{current.query}"
if current.fragment:
suffix += f"#{current.fragment}"
if not current.scheme and location.startswith("/"):
path = proxied_path(current.path or "/")
return f"{public_prefix}{path}{suffix}"
if current.scheme and current.netloc and current.netloc == upstream.netloc:
path = proxied_path(current.path or "/")
return f"{public_prefix}{path}{suffix}"
return location
def _weknora_embed_injection(
*,
allowed_path: str,
tab: str,
session: dict[str, Any],
ctx: WeKnoraEmbedContext,
public_prefix: str = "",
) -> str:
tenant_id = _tenant_id_from_session(session)
user = session.get("user") if isinstance(session.get("user"), dict) else {}
@@ -520,7 +780,8 @@ def _weknora_embed_injection(
"tenantId": tenant_id,
"user": user,
"tenant": tenant,
"apiPrefix": _WEKNORA_EMBED_API_PREFIX,
"publicPrefix": public_prefix,
"apiPrefix": _with_public_prefix(public_prefix, _WEKNORA_EMBED_API_PREFIX),
}
data = _escape_script_json(payload)
return f"""
@@ -658,8 +919,19 @@ html.deerflow-weknora-embed [class*=" guide__"] {{
function rewriteApiUrl(value) {{
try {{
const next = new URL(value, location.origin);
if (next.origin !== location.origin || !next.pathname.startsWith("/api/v1/")) return value;
next.pathname = cfg.apiPrefix + next.pathname;
if (next.origin !== location.origin) return value;
const publicPrefix = cfg.publicPrefix || "";
const apiPath = "/api/v1/";
const publicApiPath = publicPrefix + apiPath;
let upstreamPath = "";
if (next.pathname.startsWith(publicApiPath)) {{
upstreamPath = next.pathname.slice(publicPrefix.length);
}} else if (next.pathname.startsWith(apiPath)) {{
upstreamPath = next.pathname;
}} else {{
return value;
}}
next.pathname = cfg.apiPrefix + upstreamPath;
return next.href;
}} catch (_) {{
return value;
@@ -911,9 +1183,24 @@ html.deerflow-weknora-embed [class*=" guide__"] {{
"""
def _inject_weknora_embed_html(html: bytes, *, allowed_path: str, tab: str, session: dict[str, Any], ctx: WeKnoraEmbedContext) -> bytes:
def _inject_weknora_embed_html(
html: bytes,
*,
allowed_path: str,
tab: str,
session: dict[str, Any],
ctx: WeKnoraEmbedContext,
public_prefix: str = "",
) -> bytes:
text = html.decode("utf-8", errors="replace")
injection = _weknora_embed_injection(allowed_path=allowed_path, tab=tab, session=session, ctx=ctx)
text = _rewrite_weknora_html_public_paths(text, public_prefix)
injection = _weknora_embed_injection(
allowed_path=allowed_path,
tab=tab,
session=session,
ctx=ctx,
public_prefix=public_prefix,
)
if "</head>" in text:
text = text.replace("</head>", f"{injection}</head>", 1)
else:
@@ -972,6 +1259,7 @@ async def _proxy_weknora_request(
content = response.content
content_type = response.headers.get("content-type", "")
public_prefix = _request_public_prefix(request)
if is_api and request.method.upper() == "GET" and upstream_path.rstrip("/") == "/api/v1/knowledge-bases":
content = _filter_knowledge_base_list_response(response, ctx) or content
if is_api and ctx.is_conversation_deposit and "application/json" in content_type.lower():
@@ -986,11 +1274,48 @@ async def _proxy_weknora_request(
separators=(",", ":"),
).encode("utf-8")
if not is_api and "text/html" in content_type.lower():
allowed_path = f"/platform/knowledge-bases/{quote(ctx.weknora_id, safe='')}"
content = _inject_weknora_embed_html(content, allowed_path=allowed_path, tab=tab, session=session, ctx=ctx)
allowed_path = _with_public_prefix(
public_prefix,
f"/platform/knowledge-bases/{quote(ctx.weknora_id, safe='')}",
)
content = _inject_weknora_embed_html(
content,
allowed_path=allowed_path,
tab=tab,
session=session,
ctx=ctx,
public_prefix=public_prefix,
)
content_type = "text/html; charset=utf-8"
elif not is_api and _should_rewrite_weknora_static_response(content_type, upstream_path):
text = content.decode("utf-8", errors="replace")
lower_type = content_type.lower()
if "javascript" in lower_type or upstream_path.lower().endswith((".js", ".mjs")):
text = _rewrite_weknora_script_public_paths(text, public_prefix)
else:
text = _rewrite_weknora_html_public_paths(
text,
public_prefix,
rewrite_relative=False,
)
content = text.encode("utf-8")
headers_out = _safe_response_headers(response.headers)
if "location" in headers_out:
headers_out["location"] = _rewrite_weknora_location_header(
headers_out["location"],
public_prefix,
target_base_url,
)
if "Location" in headers_out:
headers_out["Location"] = _rewrite_weknora_location_header(
headers_out["Location"],
public_prefix,
target_base_url,
)
if not is_api:
headers_out.pop("etag", None)
headers_out.pop("ETag", None)
headers_out["Cache-Control"] = "no-store" if is_api or "text/html" in content_type.lower() else "private, max-age=3600"
return Response(
content=content,
@@ -1263,15 +1588,17 @@ def _weknora_embed_target_path(weknora_id: str, tab: str) -> str:
def _set_weknora_embed_cookie(request: Request, response: Response, payload: dict[str, Any]) -> None:
secure = is_secure_request(request)
response.set_cookie(
_WEKNORA_EMBED_COOKIE,
_sign_embed_payload(payload),
max_age=_WEKNORA_EMBED_TTL_SECONDS,
httponly=True,
secure=secure,
samesite="none" if secure else "lax",
path="/",
)
signed = _sign_embed_payload(payload)
for path in ("/", _WEKNORA_PUBLIC_PREFIX):
response.set_cookie(
_WEKNORA_EMBED_COOKIE,
signed,
max_age=_WEKNORA_EMBED_TTL_SECONDS,
httponly=True,
secure=secure,
samesite="none" if secure else "lax",
path=path,
)
response.headers["Cache-Control"] = "no-store"
@@ -1355,7 +1682,10 @@ async def create_weknora_detail_session(
"""Create a signed iframe session using the normal DeerFlow API auth path."""
weknora_id, payload = await _prepare_weknora_embed_session(request, mapping_id)
target = _weknora_embed_target_path(weknora_id, tab)
target = _with_public_prefix(
_request_public_prefix(request),
_weknora_embed_target_path(weknora_id, tab),
)
response = JSONResponse(
{
"frame_path": target,
@@ -1375,7 +1705,10 @@ async def open_weknora_detail_frame(
"""Start a short-lived, DeerFlow-authorized WeKnora detail iframe session."""
weknora_id, payload = await _prepare_weknora_embed_session(request, mapping_id)
target = _weknora_embed_target_path(weknora_id, tab)
target = _with_public_prefix(
_request_public_prefix(request),
_weknora_embed_target_path(weknora_id, tab),
)
response = RedirectResponse(target, status_code=302)
_set_weknora_embed_cookie(request, response, payload)
return response
@@ -1388,8 +1721,19 @@ async def proxy_weknora_platform_page(request: Request, proxied_path: str = "")
requested = f"/platform/{proxied_path}".rstrip("/")
allowed_path = f"/platform/knowledge-bases/{ctx.weknora_id}"
if requested != allowed_path:
static_upstream_path = _weknora_static_fallback_upstream_path(request)
if static_upstream_path is not None:
runtime = _runtime_for_iframe_proxy(request)
return await _proxy_weknora_request(
request,
target_base_url=runtime.web_base_url,
upstream_path=static_upstream_path,
ctx=ctx,
is_api=False,
)
target_query = urlencode({"tab": request.query_params.get("tab") or "wiki"})
return RedirectResponse(f"{allowed_path}?{target_query}", status_code=302)
public_target = _with_public_prefix(_request_public_prefix(request), allowed_path)
return RedirectResponse(f"{public_target}?{target_query}", status_code=302)
runtime = _runtime_for_iframe_proxy(request)
return await _proxy_weknora_request(
request,
@@ -1438,7 +1782,10 @@ async def proxy_weknora_root_asset(request: Request) -> Response:
return await _proxy_weknora_request(
request,
target_base_url=runtime.web_base_url,
upstream_path=request.url.path,
upstream_path=_without_public_prefix(
_request_public_prefix(request),
str(request.scope.get("path") or request.url.path),
),
ctx=ctx,
is_api=False,
)
@@ -1479,6 +1826,10 @@ async def proxy_weknora_api(request: Request, proxied_path: str) -> Response:
"/api/llmwiki/weknora-embed/api/v1/{proxied_path:path}",
methods=["GET", "HEAD", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"],
)
@router.api_route(
"/deerflow/api/llmwiki/weknora-embed/api/v1/{proxied_path:path}",
methods=["GET", "HEAD", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"],
)
async def proxy_weknora_embed_api(request: Request, proxied_path: str) -> Response:
"""Proxy iframe API calls under a collision-free cmzs namespace."""
@@ -1495,6 +1846,22 @@ async def proxy_weknora_embed_api(request: Request, proxied_path: str) -> Respon
)
@proxy_router.api_route("/{proxied_path:path}", methods=["GET", "HEAD"])
async def proxy_weknora_static_fallback(request: Request, proxied_path: str) -> Response:
upstream_path = _weknora_static_fallback_upstream_path(request)
if upstream_path is None:
raise HTTPException(status_code=404, detail="WeKnora proxy path not found")
ctx = await _read_embed_context(request)
runtime = _runtime_for_iframe_proxy(request)
return await _proxy_weknora_request(
request,
target_base_url=runtime.web_base_url,
upstream_path=upstream_path,
ctx=ctx,
is_api=False,
)
@router.get("/api/llmwiki/runtime")
async def get_llmwiki_runtime() -> dict[str, Any]:
return _runtime_or_legacy()
@@ -14,10 +14,11 @@ from typing import Any
from fastapi import HTTPException
from deerflow.config.runtime_paths import runtime_home
WEKNORA_EMBED_COOKIE = "deerflow_weknora_embed"
WEKNORA_EMBED_TTL_SECONDS = 15 * 60
WEKNORA_EMBED_API_PREFIX = "/api/llmwiki/weknora-embed"
WEKNORA_PROXY_SECRET = os.getenv("DEERFLOW_LLMWIKI_PROXY_SECRET") or secrets.token_urlsafe(48)
WEKNORA_ROOT_ASSET_PATHS: frozenset[str] = frozenset(
{
"/favicon.ico",
@@ -27,6 +28,43 @@ WEKNORA_ROOT_ASSET_PATHS: frozenset[str] = frozenset(
"/manifest.webmanifest",
}
)
WEKNORA_STATIC_PATH_PREFIXES: tuple[str, ...] = (
"/assets/",
"/css/",
"/fonts/",
"/img/",
"/images/",
"/js/",
"/locales/",
"/media/",
"/static/",
"/tdesign-icons/",
)
def _load_weknora_proxy_secret() -> str:
if secret := os.getenv("DEERFLOW_LLMWIKI_PROXY_SECRET"):
return secret
secret_path = runtime_home() / "weknora_embed_secret"
try:
if secret_path.is_file():
secret = secret_path.read_text(encoding="utf-8").strip()
if secret:
return secret
secret_path.parent.mkdir(parents=True, exist_ok=True)
secret = secrets.token_urlsafe(48)
try:
fd = os.open(str(secret_path), os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
except FileExistsError:
return secret_path.read_text(encoding="utf-8").strip()
with os.fdopen(fd, "w", encoding="utf-8") as handle:
handle.write(secret)
return secret
except OSError:
return secrets.token_urlsafe(48)
WEKNORA_PROXY_SECRET = _load_weknora_proxy_secret()
@dataclass(frozen=True)
@@ -41,12 +79,11 @@ class WeKnoraEmbedContext:
def is_weknora_embed_proxy_path(path: str) -> bool:
path = path.removeprefix("/deerflow")
return (
path == "/platform"
or path.startswith("/platform/")
or path.startswith("/assets/")
or path.startswith("/locales/")
or path.startswith("/tdesign-icons/")
or path.startswith(WEKNORA_STATIC_PATH_PREFIXES)
or path in WEKNORA_ROOT_ASSET_PATHS
or path.startswith(f"{WEKNORA_EMBED_API_PREFIX}/api/v1/")
or (path.startswith("/api/v1/") and not path.startswith("/api/v1/auth/"))