From 620cc1e11f7a901353fb9e0dd67d08eb6b8de186 Mon Sep 17 00:00:00 2001 From: SunBK201 Date: Wed, 5 Nov 2025 02:39:57 +0800 Subject: [PATCH] feat: buffer improve --- src/internal/rewrite/rewriter.go | 6 ++++-- src/internal/server/utils/tcp.go | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/internal/rewrite/rewriter.go b/src/internal/rewrite/rewriter.go index 0b96716..ac486db 100644 --- a/src/internal/rewrite/rewriter.go +++ b/src/internal/rewrite/rewriter.go @@ -19,6 +19,8 @@ import ( "github.com/sunbk201/ua3f/internal/statistics" ) +var one = make([]byte, 1) + // Rewriter encapsulates HTTP UA rewrite behavior and pass-through cache. type Rewriter struct { payloadUA string @@ -143,13 +145,13 @@ func (r *Rewriter) Forward(dst net.Conn, req *http.Request) error { // Process handles the proxying with UA rewriting logic. func (r *Rewriter) Process(dst net.Conn, src net.Conn, destAddr string, srcAddr string) (err error) { - reader := bufio.NewReader(src) + reader := bufio.NewReaderSize(src, 64*1024) defer func() { if err != nil { log.LogDebugWithAddr(srcAddr, destAddr, fmt.Sprintf("Process: %s", err.Error())) } - if _, err = io.Copy(dst, reader); err != nil { + if _, err = io.CopyBuffer(dst, reader, one); err != nil { log.LogErrorWithAddr(srcAddr, destAddr, fmt.Sprintf("Process io.Copy: %s", err.Error())) } }() diff --git a/src/internal/server/utils/tcp.go b/src/internal/server/utils/tcp.go index 63c64df..232ea7f 100644 --- a/src/internal/server/utils/tcp.go +++ b/src/internal/server/utils/tcp.go @@ -10,6 +10,8 @@ import ( "github.com/sunbk201/ua3f/internal/rewrite" ) +var one = make([]byte, 1) + // Connect dials the target address and returns the connection. func Connect(addr string) (target net.Conn, err error) { logrus.Debugf("Connecting %s", addr) @@ -36,7 +38,7 @@ func CopyHalf(dst, src net.Conn) { } log.LogDebugWithAddr(src.RemoteAddr().String(), dst.RemoteAddr().String(), "Connections half-closed") }() - _, _ = io.Copy(dst, src) + io.CopyBuffer(dst, src, one) } // ProxyHalf runs the rewriter proxy on src->dst and then half-closes both sides. @@ -59,7 +61,7 @@ func ProxyHalf(dst, src net.Conn, rw *rewrite.Rewriter, destAddr string) { srcAddr := src.RemoteAddr().String() if rw.Cache.Contains(destAddr) { log.LogInfoWithAddr(srcAddr, destAddr, fmt.Sprintf("destination (%s) in cache, passing through", destAddr)) - io.Copy(dst, src) + io.CopyBuffer(dst, src, one) return } _ = rw.Process(dst, src, destAddr, srcAddr)