feat: buffer improve

This commit is contained in:
SunBK201 2025-11-05 02:39:57 +08:00
parent 076cfd8ce3
commit 620cc1e11f
2 changed files with 8 additions and 4 deletions

View File

@ -19,6 +19,8 @@ import (
"github.com/sunbk201/ua3f/internal/statistics" "github.com/sunbk201/ua3f/internal/statistics"
) )
var one = make([]byte, 1)
// Rewriter encapsulates HTTP UA rewrite behavior and pass-through cache. // Rewriter encapsulates HTTP UA rewrite behavior and pass-through cache.
type Rewriter struct { type Rewriter struct {
payloadUA string 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. // Process handles the proxying with UA rewriting logic.
func (r *Rewriter) Process(dst net.Conn, src net.Conn, destAddr string, srcAddr string) (err error) { 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() { defer func() {
if err != nil { if err != nil {
log.LogDebugWithAddr(srcAddr, destAddr, fmt.Sprintf("Process: %s", err.Error())) 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())) log.LogErrorWithAddr(srcAddr, destAddr, fmt.Sprintf("Process io.Copy: %s", err.Error()))
} }
}() }()

View File

@ -10,6 +10,8 @@ import (
"github.com/sunbk201/ua3f/internal/rewrite" "github.com/sunbk201/ua3f/internal/rewrite"
) )
var one = make([]byte, 1)
// Connect dials the target address and returns the connection. // Connect dials the target address and returns the connection.
func Connect(addr string) (target net.Conn, err error) { func Connect(addr string) (target net.Conn, err error) {
logrus.Debugf("Connecting %s", addr) 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") 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. // 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() srcAddr := src.RemoteAddr().String()
if rw.Cache.Contains(destAddr) { if rw.Cache.Contains(destAddr) {
log.LogInfoWithAddr(srcAddr, destAddr, fmt.Sprintf("destination (%s) in cache, passing through", destAddr)) log.LogInfoWithAddr(srcAddr, destAddr, fmt.Sprintf("destination (%s) in cache, passing through", destAddr))
io.Copy(dst, src) io.CopyBuffer(dst, src, one)
return return
} }
_ = rw.Process(dst, src, destAddr, srcAddr) _ = rw.Process(dst, src, destAddr, srcAddr)