From 618d04200146373f824961b402f78d7d61003c93 Mon Sep 17 00:00:00 2001 From: ginuerzh Date: Wed, 16 Oct 2024 22:16:34 +0800 Subject: [PATCH] add input/ouput fields for log --- handler/dns/handler.go | 14 ++++++++++++-- handler/forward/local/handler.go | 14 ++++++++++++-- handler/forward/remote/handler.go | 14 ++++++++++++-- handler/http/handler.go | 5 ++++- handler/redirect/tcp/handler.go | 4 +++- handler/redirect/udp/handler.go | 4 +++- handler/relay/handler.go | 5 ++++- handler/sni/handler.go | 5 ++++- handler/socks/v4/handler.go | 5 ++++- handler/socks/v5/handler.go | 5 ++++- handler/ss/handler.go | 5 ++++- handler/sshd/handler.go | 4 +++- handler/unix/handler.go | 4 +++- 13 files changed, 72 insertions(+), 16 deletions(-) diff --git a/handler/dns/handler.go b/handler/dns/handler.go index 3004b340..547e8862 100644 --- a/handler/dns/handler.go +++ b/handler/dns/handler.go @@ -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()) }() diff --git a/handler/forward/local/handler.go b/handler/forward/local/handler.go index 241d7113..775b7e48 100644 --- a/handler/forward/local/handler.go +++ b/handler/forward/local/handler.go @@ -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()) }() diff --git a/handler/forward/remote/handler.go b/handler/forward/remote/handler.go index e6158f1d..261e156c 100644 --- a/handler/forward/remote/handler.go +++ b/handler/forward/remote/handler.go @@ -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()) }() diff --git a/handler/http/handler.go b/handler/http/handler.go index b952cb20..5012082f 100644 --- a/handler/http/handler.go +++ b/handler/http/handler.go @@ -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()) }() diff --git a/handler/redirect/tcp/handler.go b/handler/redirect/tcp/handler.go index 4f39a800..46e9171a 100644 --- a/handler/redirect/tcp/handler.go +++ b/handler/redirect/tcp/handler.go @@ -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()) }() diff --git a/handler/redirect/udp/handler.go b/handler/redirect/udp/handler.go index 0df07406..a4e4a1a0 100644 --- a/handler/redirect/udp/handler.go +++ b/handler/redirect/udp/handler.go @@ -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()) }() diff --git a/handler/relay/handler.go b/handler/relay/handler.go index 99ae2bf2..1dd6695e 100644 --- a/handler/relay/handler.go +++ b/handler/relay/handler.go @@ -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()) }() diff --git a/handler/sni/handler.go b/handler/sni/handler.go index 8df3f03c..0913796d 100644 --- a/handler/sni/handler.go +++ b/handler/sni/handler.go @@ -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()) }() diff --git a/handler/socks/v4/handler.go b/handler/socks/v4/handler.go index 44cd52cb..68bf80a9 100644 --- a/handler/socks/v4/handler.go +++ b/handler/socks/v4/handler.go @@ -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()) }() diff --git a/handler/socks/v5/handler.go b/handler/socks/v5/handler.go index 4f73464e..c79cb4b5 100644 --- a/handler/socks/v5/handler.go +++ b/handler/socks/v5/handler.go @@ -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()) }() diff --git a/handler/ss/handler.go b/handler/ss/handler.go index 5bfb8299..e482797f 100644 --- a/handler/ss/handler.go +++ b/handler/ss/handler.go @@ -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()) }() diff --git a/handler/sshd/handler.go b/handler/sshd/handler.go index 06ce5b58..93e40f84 100644 --- a/handler/sshd/handler.go +++ b/handler/sshd/handler.go @@ -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()) }() diff --git a/handler/unix/handler.go b/handler/unix/handler.go index 837c04de..a52243d2 100644 --- a/handler/unix/handler.go +++ b/handler/unix/handler.go @@ -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()) }()