Skip to content

Commit 39baf47

Browse files
committed
add missed file
1 parent 13a3f2b commit 39baf47

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

providers/openapi/log.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"mokapi/engine/common"
88
"mokapi/lib"
99
"mokapi/runtime/events"
10+
"net"
1011
"net/http"
1112
"net/textproto"
1213
"strings"
@@ -22,6 +23,7 @@ type HttpLog struct {
2223
Actions []*common.Action `json:"actions"`
2324
Api string `json:"api"`
2425
Path string `json:"path"`
26+
ClientIP string `json:"clientIP"`
2527
}
2628

2729
type HttpRequestLog struct {
@@ -57,6 +59,7 @@ func NewLogEventContext(r *http.Request, deprecated bool, eh events.Handler, tra
5759
Deprecated: deprecated,
5860
Api: traits.GetName(),
5961
Path: traits.Get("path"),
62+
ClientIP: clientIP(r),
6063
}
6164

6265
params, _ := FromContext(r.Context())
@@ -131,3 +134,17 @@ func getParsedHeaders(headers map[string]RequestParameterValue) map[string]bool
131134
}
132135
return result
133136
}
137+
138+
func clientIP(r *http.Request) string {
139+
if xff := r.Header.Get("X-Forwarded-For"); xff != "" {
140+
parts := strings.Split(xff, ",")
141+
return strings.TrimSpace(parts[0])
142+
}
143+
144+
if realIP := r.Header.Get("X-Real-IP"); realIP != "" {
145+
return realIP
146+
}
147+
148+
host, _, _ := net.SplitHostPort(r.RemoteAddr)
149+
return host
150+
}

0 commit comments

Comments
 (0)