新增 .dockerignore,添加 X-Forwarded-For 头以正确提供客户端 IP 信息

This commit is contained in:
2025-04-21 19:02:14 +08:00
parent 2b3f6768f0
commit 5b3de2b714
4 changed files with 24 additions and 10 deletions

View File

@@ -1,6 +1,6 @@
'use server'
import {API_BASE_URL, CLIENT_ID, CLIENT_SECRET, ApiResponse, UnauthorizedError} from '@/lib/api'
import {cookies} from 'next/headers'
import {cookies, headers} from 'next/headers'
import {redirect} from 'next/navigation'
// OAuth令牌缓存
@@ -176,24 +176,29 @@ async function callByUser<R = undefined>(
): Promise<ApiResponse<R>> {
try {
let token = await getUserToken()
const header = await headers()
// 获取客户端 IP
const clientIp = header.get('x-forwarded-for')
// 发送请求
let response: Response
const requestOptions = {
const request = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`,
},
} as Record<string, string>,
body: data ? JSON.stringify(data) : undefined,
}
if (clientIp) {
request.headers['X-Forwarded-For'] = clientIp
}
response = await fetch(`${API_BASE_URL}${endpoint}`, requestOptions)
let response = await fetch(`${API_BASE_URL}${endpoint}`, request)
if (response.status === 401) {
token = await getUserToken(true)
requestOptions.headers['Authorization'] = `Bearer ${token}`
response = await fetch(`${API_BASE_URL}${endpoint}`, requestOptions)
request.headers['Authorization'] = `Bearer ${token}`
response = await fetch(`${API_BASE_URL}${endpoint}`, request)
}
// 检查响应状态