Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
陈曦
sub2api
Commits
4bfa69bf
Commit
4bfa69bf
authored
Feb 14, 2026
by
yangjianbo
Browse files
fix(openai): 仅记录 codex_cli_only 拒绝日志并输出详细 User-Agent
parent
f96acf6e
Changes
2
Hide whitespace changes
Inline
Side-by-side
backend/internal/service/openai_gateway_service.go
View file @
4bfa69bf
...
...
@@ -313,7 +313,6 @@ func logCodexCLIOnlyDetection(ctx context.Context, c *gin.Context, account *Acco
}
log
:=
logger
.
FromContext
(
ctx
)
.
With
(
fields
...
)
if
result
.
Matched
{
log
.
Warn
(
"OpenAI codex_cli_only 允许官方客户端请求"
)
return
}
log
.
Warn
(
"OpenAI codex_cli_only 拒绝非官方客户端请求"
)
...
...
@@ -333,7 +332,7 @@ func appendCodexCLIOnlyRejectedRequestFields(fields []zap.Field, c *gin.Context,
zap
.
String
(
"request_host"
,
strings
.
TrimSpace
(
req
.
Host
)),
zap
.
String
(
"request_client_ip"
,
strings
.
TrimSpace
(
c
.
ClientIP
())),
zap
.
String
(
"request_remote_addr"
,
strings
.
TrimSpace
(
req
.
RemoteAddr
)),
zap
.
String
(
"request_user_agent"
,
strings
.
TrimSpace
(
req
.
Header
.
Get
(
"User-Agent"
))),
zap
.
String
(
"request_user_agent"
,
buildDetailedUserAgent
(
req
.
Header
.
Values
(
"User-Agent"
))),
zap
.
String
(
"request_content_type"
,
strings
.
TrimSpace
(
req
.
Header
.
Get
(
"Content-Type"
))),
zap
.
Int64
(
"request_content_length"
,
req
.
ContentLength
),
zap
.
Bool
(
"request_stream"
,
requestStream
),
...
...
@@ -352,6 +351,21 @@ func appendCodexCLIOnlyRejectedRequestFields(fields []zap.Field, c *gin.Context,
return
fields
}
func
buildDetailedUserAgent
(
values
[]
string
)
string
{
if
len
(
values
)
==
0
{
return
""
}
result
:=
make
([]
string
,
0
,
len
(
values
))
for
_
,
value
:=
range
values
{
v
:=
strings
.
TrimSpace
(
value
)
if
v
==
""
{
continue
}
result
=
append
(
result
,
v
)
}
return
strings
.
Join
(
result
,
" | "
)
}
func
snapshotCodexCLIOnlyHeaders
(
header
http
.
Header
)
map
[
string
]
string
{
if
len
(
header
)
==
0
{
return
nil
...
...
backend/internal/service/openai_gateway_service_codex_cli_only_test.go
View file @
4bfa69bf
...
...
@@ -103,7 +103,7 @@ func TestLogCodexCLIOnlyDetection_NilSafety(t *testing.T) {
})
}
func
TestLogCodexCLIOnlyDetection_Logs
BothMatchedAnd
Rejected
(
t
*
testing
.
T
)
{
func
TestLogCodexCLIOnlyDetection_
Only
LogsRejected
(
t
*
testing
.
T
)
{
logSink
,
restore
:=
captureStructuredLog
(
t
)
defer
restore
()
...
...
@@ -119,7 +119,7 @@ func TestLogCodexCLIOnlyDetection_LogsBothMatchedAndRejected(t *testing.T) {
Reason
:
CodexClientRestrictionReasonNotMatchedUA
,
},
nil
)
require
.
Tru
e
(
t
,
logSink
.
ContainsMessage
(
"OpenAI codex_cli_only 允许官方客户端请求"
))
require
.
Fals
e
(
t
,
logSink
.
ContainsMessage
(
"OpenAI codex_cli_only 允许官方客户端请求"
))
require
.
True
(
t
,
logSink
.
ContainsMessage
(
"OpenAI codex_cli_only 拒绝非官方客户端请求"
))
}
...
...
@@ -131,7 +131,8 @@ func TestLogCodexCLIOnlyDetection_RejectedIncludesRequestDetails(t *testing.T) {
rec
:=
httptest
.
NewRecorder
()
c
,
_
:=
gin
.
CreateTestContext
(
rec
)
c
.
Request
=
httptest
.
NewRequest
(
http
.
MethodPost
,
"/v1/responses?trace=1"
,
bytes
.
NewReader
(
nil
))
c
.
Request
.
Header
.
Set
(
"User-Agent"
,
"curl/8.0"
)
c
.
Request
.
Header
.
Add
(
"User-Agent"
,
"curl/8.0"
)
c
.
Request
.
Header
.
Add
(
"User-Agent"
,
"Codex/1.2.3 (cli)"
)
c
.
Request
.
Header
.
Set
(
"Content-Type"
,
"application/json"
)
c
.
Request
.
Header
.
Set
(
"OpenAI-Beta"
,
"assistants=v2"
)
...
...
@@ -143,7 +144,7 @@ func TestLogCodexCLIOnlyDetection_RejectedIncludesRequestDetails(t *testing.T) {
Reason
:
CodexClientRestrictionReasonNotMatchedUA
,
},
body
)
require
.
True
(
t
,
logSink
.
ContainsFieldValue
(
"request_user_agent"
,
"curl/8.0"
))
require
.
True
(
t
,
logSink
.
ContainsFieldValue
(
"request_user_agent"
,
"curl/8.0
| Codex/1.2.3 (cli)
"
))
require
.
True
(
t
,
logSink
.
ContainsFieldValue
(
"request_model"
,
"gpt-5.2"
))
require
.
True
(
t
,
logSink
.
ContainsFieldValue
(
"request_query"
,
"trace=1"
))
require
.
True
(
t
,
logSink
.
ContainsFieldValue
(
"request_prompt_cache_key_sha256"
,
hashSensitiveValueForLog
(
"pc-123"
)))
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment