add input/ouput fields for log

This commit is contained in:
ginuerzh
2024-10-16 22:16:34 +08:00
parent 24547b4332
commit 618d042001
13 changed files with 72 additions and 16 deletions
+12 -2
View File
@@ -145,12 +145,20 @@ func (h *dnsHandler) Handle(ctx context.Context, conn net.Conn, opts ...handler.
Time: start,
SID: string(ctxvalue.SidFromContext(ctx)),
}
ro.ClientIP, _, _ = net.SplitHostPort(conn.RemoteAddr().String())
ro.ClientIP = conn.RemoteAddr().String()
if clientAddr := ctxvalue.ClientAddrFromContext(ctx); clientAddr != "" {
ro.ClientIP = string(clientAddr)
}
if h, _, _ := net.SplitHostPort(ro.ClientIP); h != "" {
ro.ClientIP = h
}
log := h.options.Logger.WithFields(map[string]any{
"remote": conn.RemoteAddr().String(),
"local": conn.LocalAddr().String(),
"sid": ctxvalue.SidFromContext(ctx),
"client": ro.ClientIP,
})
log.Infof("%s <> %s", conn.RemoteAddr(), conn.LocalAddr())
@@ -170,7 +178,9 @@ func (h *dnsHandler) Handle(ctx context.Context, conn net.Conn, opts ...handler.
}
log.WithFields(map[string]any{
"duration": time.Since(start),
"duration": time.Since(start),
"inputBytes": ro.InputBytes,
"outputBytes": ro.OutputBytes,
}).Infof("%s >< %s", conn.RemoteAddr(), conn.LocalAddr())
}()
+12 -2
View File
@@ -88,12 +88,20 @@ func (h *forwardHandler) Handle(ctx context.Context, conn net.Conn, opts ...hand
Time: start,
SID: string(ctxvalue.SidFromContext(ctx)),
}
ro.ClientIP, _, _ = net.SplitHostPort(conn.RemoteAddr().String())
ro.ClientIP = conn.RemoteAddr().String()
if clientAddr := ctxvalue.ClientAddrFromContext(ctx); clientAddr != "" {
ro.ClientIP = string(clientAddr)
}
if h, _, _ := net.SplitHostPort(ro.ClientIP); h != "" {
ro.ClientIP = h
}
log := h.options.Logger.WithFields(map[string]any{
"remote": conn.RemoteAddr().String(),
"local": conn.LocalAddr().String(),
"sid": ctxvalue.SidFromContext(ctx),
"client": ro.ClientIP,
})
log.Infof("%s <> %s", conn.RemoteAddr(), conn.LocalAddr())
@@ -112,7 +120,9 @@ func (h *forwardHandler) Handle(ctx context.Context, conn net.Conn, opts ...hand
}
log.WithFields(map[string]any{
"duration": time.Since(start),
"duration": time.Since(start),
"inputBytes": ro.InputBytes,
"outputBytes": ro.OutputBytes,
}).Infof("%s >< %s", conn.RemoteAddr(), conn.LocalAddr())
}()
+12 -2
View File
@@ -89,12 +89,20 @@ func (h *forwardHandler) Handle(ctx context.Context, conn net.Conn, opts ...hand
Time: start,
SID: string(ctxvalue.SidFromContext(ctx)),
}
ro.ClientIP, _, _ = net.SplitHostPort(conn.RemoteAddr().String())
ro.ClientIP = conn.RemoteAddr().String()
if clientAddr := ctxvalue.ClientAddrFromContext(ctx); clientAddr != "" {
ro.ClientIP = string(clientAddr)
}
if h, _, _ := net.SplitHostPort(ro.ClientIP); h != "" {
ro.ClientIP = h
}
log := h.options.Logger.WithFields(map[string]any{
"remote": conn.RemoteAddr().String(),
"local": conn.LocalAddr().String(),
"sid": ctxvalue.SidFromContext(ctx),
"client": ro.ClientIP,
})
log.Infof("%s <> %s", conn.RemoteAddr(), conn.LocalAddr())
@@ -113,7 +121,9 @@ func (h *forwardHandler) Handle(ctx context.Context, conn net.Conn, opts ...hand
}
log.WithFields(map[string]any{
"duration": time.Since(start),
"duration": time.Since(start),
"inputBytes": ro.InputBytes,
"outputBytes": ro.OutputBytes,
}).Infof("%s >< %s", conn.RemoteAddr(), conn.LocalAddr())
}()
+4 -1
View File
@@ -124,6 +124,7 @@ func (h *httpHandler) Handle(ctx context.Context, conn net.Conn, opts ...handler
"remote": conn.RemoteAddr().String(),
"local": conn.LocalAddr().String(),
"sid": ctxvalue.SidFromContext(ctx),
"client": ro.ClientIP,
})
log.Infof("%s <> %s", conn.RemoteAddr(), conn.LocalAddr())
@@ -142,7 +143,9 @@ func (h *httpHandler) Handle(ctx context.Context, conn net.Conn, opts ...handler
}
log.WithFields(map[string]any{
"duration": time.Since(start),
"duration": time.Since(start),
"inputBytes": ro.InputBytes,
"outputBytes": ro.OutputBytes,
}).Infof("%s >< %s", conn.RemoteAddr(), conn.LocalAddr())
}()
+3 -1
View File
@@ -105,7 +105,9 @@ func (h *redirectHandler) Handle(ctx context.Context, conn net.Conn, opts ...han
}
log.WithFields(map[string]any{
"duration": time.Since(start),
"duration": time.Since(start),
"inputBytes": ro.InputBytes,
"outputBytes": ro.OutputBytes,
}).Infof("%s >< %s", conn.RemoteAddr(), conn.LocalAddr())
}()
+3 -1
View File
@@ -93,7 +93,9 @@ func (h *redirectHandler) Handle(ctx context.Context, conn net.Conn, opts ...han
}
log.WithFields(map[string]any{
"duration": time.Since(start),
"duration": time.Since(start),
"inputBytes": ro.InputBytes,
"outputBytes": ro.OutputBytes,
}).Infof("%s >< %s", conn.RemoteAddr(), conn.LocalAddr())
}()
+4 -1
View File
@@ -117,6 +117,7 @@ func (h *relayHandler) Handle(ctx context.Context, conn net.Conn, opts ...handle
"remote": conn.RemoteAddr().String(),
"local": conn.LocalAddr().String(),
"sid": ctxvalue.SidFromContext(ctx),
"client": ro.ClientIP,
})
log.Infof("%s <> %s", conn.RemoteAddr(), conn.LocalAddr())
@@ -136,7 +137,9 @@ func (h *relayHandler) Handle(ctx context.Context, conn net.Conn, opts ...handle
}
log.WithFields(map[string]any{
"duration": time.Since(start),
"duration": time.Since(start),
"inputBytes": ro.InputBytes,
"outputBytes": ro.OutputBytes,
}).Infof("%s >< %s", conn.RemoteAddr(), conn.LocalAddr())
}()
+4 -1
View File
@@ -92,6 +92,7 @@ func (h *sniHandler) Handle(ctx context.Context, conn net.Conn, opts ...handler.
"remote": conn.RemoteAddr().String(),
"local": conn.LocalAddr().String(),
"sid": ctxvalue.SidFromContext(ctx),
"client": ro.ClientIP,
})
log.Infof("%s <> %s", conn.RemoteAddr(), conn.LocalAddr())
@@ -110,7 +111,9 @@ func (h *sniHandler) Handle(ctx context.Context, conn net.Conn, opts ...handler.
}
log.WithFields(map[string]any{
"duration": time.Since(start),
"duration": time.Since(start),
"inputBytes": ro.InputBytes,
"outputBytes": ro.OutputBytes,
}).Infof("%s >< %s", conn.RemoteAddr(), conn.LocalAddr())
}()
+4 -1
View File
@@ -118,6 +118,7 @@ func (h *socks4Handler) Handle(ctx context.Context, conn net.Conn, opts ...handl
"remote": conn.RemoteAddr().String(),
"local": conn.LocalAddr().String(),
"sid": ctxvalue.SidFromContext(ctx),
"client": ro.ClientIP,
})
log.Infof("%s <> %s", conn.RemoteAddr(), conn.LocalAddr())
@@ -136,7 +137,9 @@ func (h *socks4Handler) Handle(ctx context.Context, conn net.Conn, opts ...handl
}
log.WithFields(map[string]any{
"duration": time.Since(start),
"duration": time.Since(start),
"inputBytes": ro.InputBytes,
"outputBytes": ro.OutputBytes,
}).Infof("%s >< %s", conn.RemoteAddr(), conn.LocalAddr())
}()
+4 -1
View File
@@ -118,6 +118,7 @@ func (h *socks5Handler) Handle(ctx context.Context, conn net.Conn, opts ...handl
"remote": conn.RemoteAddr().String(),
"local": conn.LocalAddr().String(),
"sid": ctxvalue.SidFromContext(ctx),
"client": ro.ClientIP,
})
log.Infof("%s <> %s", conn.RemoteAddr(), conn.LocalAddr())
@@ -136,7 +137,9 @@ func (h *socks5Handler) Handle(ctx context.Context, conn net.Conn, opts ...handl
}
log.WithFields(map[string]any{
"duration": time.Since(start),
"duration": time.Since(start),
"inputBytes": ro.InputBytes,
"outputBytes": ro.OutputBytes,
}).Infof("%s >< %s", conn.RemoteAddr(), conn.LocalAddr())
}()
+4 -1
View File
@@ -102,6 +102,7 @@ func (h *ssHandler) Handle(ctx context.Context, conn net.Conn, opts ...handler.H
"remote": conn.RemoteAddr().String(),
"local": conn.LocalAddr().String(),
"sid": ctxvalue.SidFromContext(ctx),
"client": ro.ClientIP,
})
log.Infof("%s <> %s", conn.RemoteAddr(), conn.LocalAddr())
@@ -120,7 +121,9 @@ func (h *ssHandler) Handle(ctx context.Context, conn net.Conn, opts ...handler.H
}
log.WithFields(map[string]any{
"duration": time.Since(start),
"duration": time.Since(start),
"inputBytes": ro.InputBytes,
"outputBytes": ro.OutputBytes,
}).Infof("%s >< %s", conn.RemoteAddr(), conn.LocalAddr())
}()
+3 -1
View File
@@ -113,7 +113,9 @@ func (h *forwardHandler) Handle(ctx context.Context, conn net.Conn, opts ...hand
}
log.WithFields(map[string]any{
"duration": time.Since(start),
"duration": time.Since(start),
"inputBytes": ro.InputBytes,
"outputBytes": ro.OutputBytes,
}).Infof("%s >< %s", conn.RemoteAddr(), conn.LocalAddr())
}()
+3 -1
View File
@@ -110,7 +110,9 @@ func (h *unixHandler) Handle(ctx context.Context, conn net.Conn, opts ...handler
}
log.WithFields(map[string]any{
"duration": time.Since(start),
"duration": time.Since(start),
"inputBytes": ro.InputBytes,
"outputBytes": ro.OutputBytes,
}).Infof("%s >< %s", conn.RemoteAddr(), conn.LocalAddr())
}()