add src field for logger and recorder

This commit is contained in:
ginuerzh
2025-07-02 21:35:02 +08:00
parent 97ed5080a6
commit d9a2f44a78
27 changed files with 217 additions and 92 deletions
+13 -4
View File
@@ -111,10 +111,11 @@ func (h *http2Handler) Handle(ctx context.Context, conn net.Conn, opts ...handle
ro.ClientIP, _, _ = net.SplitHostPort(conn.RemoteAddr().String())
log := h.options.Logger.WithFields(map[string]any{
"remote": conn.RemoteAddr().String(),
"local": conn.LocalAddr().String(),
"sid": ctxvalue.SidFromContext(ctx),
"client": ro.ClientIP,
"remote": conn.RemoteAddr().String(),
"local": conn.LocalAddr().String(),
"sid": ctxvalue.SidFromContext(ctx),
"client": ro.ClientIP,
"network": ro.Network,
})
log.Infof("%s <> %s", conn.RemoteAddr(), conn.LocalAddr())
defer func() {
@@ -230,6 +231,10 @@ func (h *http2Handler) roundTrip(ctx context.Context, w http.ResponseWriter, req
if !ok {
return errors.New("authentication failed")
}
log = log.WithFields(map[string]any{"clientID": clientID})
ro.ClientID = clientID
ctx = ctxvalue.ContextWithClientID(ctx, ctxvalue.ClientID(clientID))
if h.options.Bypass != nil && h.options.Bypass.Contains(ctx, "tcp", host) {
@@ -261,6 +266,10 @@ func (h *http2Handler) roundTrip(ctx context.Context, w http.ResponseWriter, req
}
defer cc.Close()
log = log.WithFields(map[string]any{"src": cc.LocalAddr().String(), "dst": cc.RemoteAddr().String()})
ro.Src = cc.LocalAddr().String()
ro.Dst = cc.RemoteAddr().String()
if req.Method == http.MethodConnect {
resp.StatusCode = http.StatusOK
w.WriteHeader(http.StatusOK)