From 1aa1eef2c597f8072b78b94e04f1ccea15723897 Mon Sep 17 00:00:00 2001 From: pascal Date: Mon, 9 Mar 2026 16:08:30 +0100 Subject: [PATCH] account for streaming --- proxy/internal/accesslog/middleware.go | 13 +++++++++---- proxy/internal/accesslog/statuswriter.go | 14 ++++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/proxy/internal/accesslog/middleware.go b/proxy/internal/accesslog/middleware.go index 615ddd83b..7368185c0 100644 --- a/proxy/internal/accesslog/middleware.go +++ b/proxy/internal/accesslog/middleware.go @@ -32,6 +32,14 @@ func (l *Logger) Middleware(next http.Handler) http.Handler { status: http.StatusOK, } + var bytesRead int64 + if r.Body != nil { + r.Body = &bodyCounter{ + ReadCloser: r.Body, + bytesRead: &bytesRead, + } + } + // Resolve the source IP using trusted proxy configuration before passing // the request on, as the proxy will modify forwarding headers. sourceIp := extractSourceIP(r, l.trustedProxies) @@ -53,10 +61,7 @@ func (l *Logger) Middleware(next http.Handler) http.Handler { host = r.Host } - bytesUpload := r.ContentLength - if bytesUpload < 0 { - bytesUpload = 0 - } + bytesUpload := bytesRead bytesDownload := sw.bytesWritten entry := logEntry{ diff --git a/proxy/internal/accesslog/statuswriter.go b/proxy/internal/accesslog/statuswriter.go index d65bbe080..24f7b35e9 100644 --- a/proxy/internal/accesslog/statuswriter.go +++ b/proxy/internal/accesslog/statuswriter.go @@ -1,6 +1,8 @@ package accesslog import ( + "io" + "github.com/netbirdio/netbird/proxy/internal/responsewriter" ) @@ -23,3 +25,15 @@ func (w *statusWriter) Write(b []byte) (int, error) { w.bytesWritten += int64(n) return n, err } + +// bodyCounter wraps an io.ReadCloser and counts bytes read from the request body. +type bodyCounter struct { + io.ReadCloser + bytesRead *int64 +} + +func (bc *bodyCounter) Read(p []byte) (int, error) { + n, err := bc.ReadCloser.Read(p) + *bc.bytesRead += int64(n) + return n, err +}