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