feat: buffer improve

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

View File

@ -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()))
}
}()

View File

@ -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)