Skip to content

Commit

Permalink
fix: buffer pool redundant allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
uint48 authored and markpash committed Jan 23, 2025
1 parent 35e1db7 commit 6cc5a7b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,10 @@ require (
github.com/refraction-networking/gotapdance v1.7.10 // indirect
github.com/refraction-networking/obfs4 v0.1.2 // indirect
github.com/rogpeppe/go-internal v1.9.0 // indirect
github.com/sagernet/sing v0.5.1 // indirect
github.com/sergeyfrolov/bsbuffer v0.0.0-20180903213811-94e85abb8507 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635 // indirect
github.com/wader/filtertransport v0.0.0-20200316221534-bdd9e61eee78 // indirect
gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/goptlib v1.5.0 // indirect
Expand Down
5 changes: 4 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZV
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/ryanuber/go-glob v0.0.0-20170128012129-256dc444b735 h1:7YvPJVmEeFHR1Tj9sZEYsmarJEQfMVYpd/Vyy/A8dqE=
github.com/ryanuber/go-glob v0.0.0-20170128012129-256dc444b735/go.mod h1:807d1WSdnB0XRJzKNil9Om6lcp/3a0v4qIHxIXzX/Yc=
github.com/sagernet/sing v0.5.1 h1:mhL/MZVq0TjuvHcpYcFtmSD1BFOxZ/+8ofbNZcg1k1Y=
github.com/sagernet/sing v0.5.1/go.mod h1:ARkL0gM13/Iv5VCZmci/NuoOlePoIsW0m7BWfln/Hak=
github.com/sergeyfrolov/bsbuffer v0.0.0-20180903213811-94e85abb8507 h1:ML7ZNtcln5UBo5Wv7RIv9Xg3Pr5VuRCWLFXEwda54Y4=
github.com/sergeyfrolov/bsbuffer v0.0.0-20180903213811-94e85abb8507/go.mod h1:DbI1gxrXI2jRGw7XGEUZQOOMd6PsnKzRrCKabvvMrwM=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
Expand All @@ -202,8 +204,9 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635 h1:kdXcSzyDtseVEc4yCz2qF8ZrQvIDBJLl4S1c3GCXmoI=
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww=
github.com/things-go/go-socks5 v0.0.5 h1:qvKaGcBkfDrUL33SchHN93srAmYGzb4CxSM2DPYufe8=
Expand Down
25 changes: 16 additions & 9 deletions wiresocks/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package wiresocks
import (
"context"
"errors"
"github.com/sagernet/sing/common/buf"
"io"
"log/slog"
"net"
Expand All @@ -14,7 +15,6 @@ import (
"github.com/bepass-org/warp-plus/proxy/pkg/statute"
"github.com/bepass-org/warp-plus/wireguard/device"
"github.com/bepass-org/warp-plus/wireguard/tun/netstack"
"github.com/things-go/go-socks5/bufferpool"
)

// VirtualTun stores a reference to netstack network and DNS configuration
Expand All @@ -23,9 +23,12 @@ type VirtualTun struct {
Logger *slog.Logger
Dev *device.Device
Ctx context.Context
pool bufferpool.BufPool
pool buf.Allocator
//pool bufferpool.BufPool
}

var BuffSize = 65536

// StartProxy spawns a socks5 server.
func StartProxy(ctx context.Context, l *slog.Logger, tnet *netstack.Net, bindAddress netip.AddrPort) (netip.AddrPort, error) {
ln, err := net.Listen("tcp", bindAddress.String())
Expand All @@ -38,7 +41,7 @@ func StartProxy(ctx context.Context, l *slog.Logger, tnet *netstack.Net, bindAdd
Logger: l.With("subsystem", "vtun"),
Dev: nil,
Ctx: ctx,
pool: bufferpool.NewPool(256 * 1024),
pool: buf.DefaultAllocator,
}

proxy := mixed.NewProxy(
Expand Down Expand Up @@ -80,9 +83,11 @@ func (vt *VirtualTun) generalHandler(req *statute.ProxyRequest) error {
done := make(chan error, 1)
// Copy data from req.Conn to conn
go func() {
buf1 := vt.pool.Get()
defer vt.pool.Put(buf1)
_, err := copyConnTimeout(conn, req.Conn, buf1[:cap(buf1)], timeout)
buf1 := vt.pool.Get(BuffSize)
defer func(pool buf.Allocator, buf []byte) {
_ = pool.Put(buf)
}(vt.pool, buf1)
_, err := copyConnTimeout(conn, req.Conn, buf1, timeout)
if errors.Is(err, syscall.ECONNRESET) {
done <- nil
return
Expand All @@ -91,9 +96,11 @@ func (vt *VirtualTun) generalHandler(req *statute.ProxyRequest) error {
}()
// Copy data from conn to req.Conn
go func() {
buf2 := vt.pool.Get()
defer vt.pool.Put(buf2)
_, err := copyConnTimeout(req.Conn, conn, buf2[:cap(buf2)], timeout)
buf2 := vt.pool.Get(BuffSize)
defer func(pool buf.Allocator, buf []byte) {
_ = pool.Put(buf)
}(vt.pool, buf2)
_, err := copyConnTimeout(req.Conn, conn, buf2, timeout)
done <- err
}()
// Wait for one of the copy operations to finish
Expand Down

0 comments on commit 6cc5a7b

Please sign in to comment.