add clientIP for handler recorder object

This commit is contained in:
ginuerzh
2024-09-15 18:28:24 +08:00
parent f681f7aa03
commit b39aa63f75
12 changed files with 77 additions and 8 deletions
+7 -1
View File
@@ -29,6 +29,7 @@ import (
ctxvalue "github.com/go-gost/x/ctx"
xio "github.com/go-gost/x/internal/io"
netpkg "github.com/go-gost/x/internal/net"
xhttp "github.com/go-gost/x/internal/net/http"
limiter_util "github.com/go-gost/x/internal/util/limiter"
stats_util "github.com/go-gost/x/internal/util/stats"
rate_limiter "github.com/go-gost/x/limiter/rate"
@@ -101,6 +102,7 @@ func (h *httpHandler) Handle(ctx context.Context, conn net.Conn, opts ...handler
Time: start,
SID: string(ctxvalue.SidFromContext(ctx)),
}
ro.ClientIP, _, _ = net.SplitHostPort(conn.RemoteAddr().String())
log := h.options.Logger.WithFields(map[string]any{
"remote": conn.RemoteAddr().String(),
@@ -133,6 +135,10 @@ func (h *httpHandler) Handle(ctx context.Context, conn net.Conn, opts ...handler
}
defer req.Body.Close()
if clientIP := xhttp.GetClientIP(req); clientIP != nil {
ro.ClientIP = clientIP.String()
}
return h.handleRequest(ctx, conn, req, ro, log)
}
@@ -179,7 +185,7 @@ func (h *httpHandler) handleRequest(ctx context.Context, conn net.Conn, req *htt
if u, _, _ := h.basicProxyAuth(req.Header.Get("Proxy-Authorization")); u != "" {
fields["user"] = u
ro.Client = u
ro.ClientID = u
}
log = log.WithFields(fields)