From 82066308232992b3a0ffbac366f60ea3291ea56a Mon Sep 17 00:00:00 2001 From: "DESKTOP-JKIE562\\PC" <1013397054@qq.com> Date: Wed, 3 Jun 2026 14:46:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E4=BB=B6=E9=A2=84=E8=A7=88=E6=8A=A550?= =?UTF-8?q?0=E9=97=AE=E9=A2=98=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/api/integration.py | 4 +++ backend/app/services/integration_api.py | 41 ++++++++++++++++++++----- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/backend/app/api/integration.py b/backend/app/api/integration.py index cc4e2755..dcd3c260 100644 --- a/backend/app/api/integration.py +++ b/backend/app/api/integration.py @@ -10,6 +10,7 @@ from sqlalchemy.orm import joinedload from app import db from app.models import KbDifySyncRecord, KbDocument, KbDocumentVersion, KbGroup from app.errors import AppError, ValidationError, UnauthorizedError, ForbiddenError, NotFoundError, ConflictError, ServiceUnavailableError, raise_for_status +from app.services.integration_api import IntegrationApiService integration_bp = Blueprint('integration', __name__) @@ -120,6 +121,9 @@ def _require_integration_key(): def _integration_base_url() -> str: + request_origin = IntegrationApiService._request_origin_url() + if request_origin: + return request_origin explicit = str(current_app.config.get('WECOM_PUBLIC_BASE_URL') or '').strip().rstrip('/') if explicit: return explicit diff --git a/backend/app/services/integration_api.py b/backend/app/services/integration_api.py index af193887..ce51e8dd 100644 --- a/backend/app/services/integration_api.py +++ b/backend/app/services/integration_api.py @@ -14,6 +14,28 @@ from app.config import Config class IntegrationApiService: _cache: Dict[Tuple[str, str], Dict[str, Any]] = {} + @staticmethod + def _request_origin_url() -> str: + if not has_request_context(): + return "" + + forwarded_proto = (request.headers.get("X-Forwarded-Proto") or "").split(",")[0].strip() + forwarded_host = (request.headers.get("X-Forwarded-Host") or "").split(",")[0].strip() + if forwarded_proto and forwarded_host: + return f"{forwarded_proto}://{forwarded_host}".rstrip("/") + + origin = str(request.headers.get("Origin") or "").strip().rstrip("/") + if origin: + return origin + + referer = str(request.headers.get("Referer") or "").strip() + if referer: + parsed = urlparse(referer) + if parsed.scheme and parsed.netloc: + return f"{parsed.scheme}://{parsed.netloc}".rstrip("/") + + return request.host_url.rstrip("/") + @staticmethod def _lookup_from_dify_document_api(dataset_id: str, remote_document_id: str) -> Optional[Dict[str, Any]]: """ @@ -152,6 +174,9 @@ class IntegrationApiService: return configured if mode != "external": + request_origin = IntegrationApiService._request_origin_url() + if request_origin: + return request_origin if public_base: return public_base if has_request_context(): @@ -226,13 +251,15 @@ class IntegrationApiService: version = record.document_version document = version.document - explicit = str(current_app.config.get('WECOM_PUBLIC_BASE_URL') or '').strip().rstrip('/') - if explicit: - base_url = explicit - elif has_request_context(): - base_url = request.host_url.rstrip('/') - else: - base_url = '' + base_url = IntegrationApiService._request_origin_url() + if not base_url: + explicit = str(current_app.config.get('WECOM_PUBLIC_BASE_URL') or '').strip().rstrip('/') + if explicit: + base_url = explicit + elif has_request_context(): + base_url = request.host_url.rstrip('/') + else: + base_url = '' import time import jwt