From 73b442717ef3f48e9b68525f5396545804e9bb0c Mon Sep 17 00:00:00 2001 From: gitadmin Date: Fri, 10 Jul 2026 12:59:21 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20getClientIP=20?= =?UTF-8?q?=E5=AF=BC=E5=85=A5=E5=AF=BC=E8=87=B4=E7=9A=84=E6=A0=91=E6=91=87?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - scan-emails/route.ts: 直接定义 getClientIP 函数,避免从 @/lib/audit 导入 - scan-status/route.ts: 同上 - 原因: Next.js tree-shaking 在打包时未将 getClientIP 包含在模块导出中 - scan-history API: 新增扫描历史查询接口 - db-schema: 新增 scan_history 表 --- src/app/api/monitor/scan-emails/route.ts | 9 ++++++++- src/app/api/monitor/scan-status/route.ts | 9 ++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/app/api/monitor/scan-emails/route.ts b/src/app/api/monitor/scan-emails/route.ts index 13a584d..9899735 100644 --- a/src/app/api/monitor/scan-emails/route.ts +++ b/src/app/api/monitor/scan-emails/route.ts @@ -8,9 +8,16 @@ import { getMonitorConfig } from '@/lib/monitor/settings-manager' import { ImapFlow } from 'imapflow' import { simpleParser } from 'mailparser' import { formatBeijingTime } from '@/lib/monitor/types' -import { writeAuditLog, getClientIP } from '@/lib/audit' +import { writeAuditLog } from '@/lib/audit' import { getScanState, setScanState, isCancelRequested, resetCancelFlag } from '@/lib/monitor/scan-state' +// 直接定义,避免 Next.js tree-shaking 导致模块导出缺失 +function getClientIP(request: NextRequest): string | null { + const forwarded = request.headers.get('x-forwarded-for') + if (forwarded) return forwarded.split(',')[0].trim() + return request.headers.get('x-real-ip') ?? null +} + interface ScanResult { status: 'running' | 'completed' | 'error' | 'cancelling' startedAt: string diff --git a/src/app/api/monitor/scan-status/route.ts b/src/app/api/monitor/scan-status/route.ts index dd2f2c8..45ee8bb 100644 --- a/src/app/api/monitor/scan-status/route.ts +++ b/src/app/api/monitor/scan-status/route.ts @@ -3,9 +3,16 @@ import { NextRequest, NextResponse } from 'next/server' import { initDatabase } from '@/lib/db-schema' import { getCurrentUser } from '@/lib/auth' import { hasPermission } from '@/lib/permissions' -import { getClientIP, writeAuditLog } from '@/lib/audit' +import { writeAuditLog } from '@/lib/audit' import { getScanState, requestCancel } from '@/lib/monitor/scan-state' +// 直接定义,避免 Next.js tree-shaking 导致模块导出缺失 +function getClientIP(request: NextRequest): string | null { + const forwarded = request.headers.get('x-forwarded-for') + if (forwarded) return forwarded.split(',')[0].trim() + return request.headers.get('x-real-ip') ?? null +} + export async function GET(request: NextRequest) { initDatabase() const user = await getCurrentUser()