Some change

This commit is contained in:
xteamlyer
2026-04-30 19:04:28 +03:00
parent b99a41e040
commit 63b9203be4
4 changed files with 165 additions and 113 deletions
+34 -9
View File
@@ -19,8 +19,24 @@ import (
const (
COPY_BUF = 128 * 1024
BAD_REQ_MSG = "Bad Request\n"
// Reduced idle pool: the proxy handler makes upstream connections per request,
// not persistent keep-alive sessions. 10 total / 2 per host is plenty and
// avoids leaking hundreds of idle goroutines/sockets under bursty traffic.
TRANSPORT_MAX_IDLE_CONNS = 10
TRANSPORT_MAX_IDLE_CONNS_PER_HOST = 2
TRANSPORT_IDLE_CONN_TIMEOUT = 60 * time.Second
)
// copyBufPool reuses 128 KiB buffers for bidirectional data relay,
// avoiding per-connection heap allocations.
var copyBufPool = sync.Pool{
New: func() any {
b := make([]byte, COPY_BUF)
return &b
},
}
type ProxyHandler struct {
logger *clog.CondLogger
dialer dialer.ContextDialer
@@ -28,17 +44,18 @@ type ProxyHandler struct {
fakeSNI string
}
func NewProxyHandler(dialer dialer.ContextDialer, logger *clog.CondLogger, fakeSNI string) *ProxyHandler {
func NewProxyHandler(d dialer.ContextDialer, logger *clog.CondLogger, fakeSNI string) *ProxyHandler {
httptransport := &http.Transport{
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
MaxIdleConns: TRANSPORT_MAX_IDLE_CONNS,
MaxIdleConnsPerHost: TRANSPORT_MAX_IDLE_CONNS_PER_HOST,
IdleConnTimeout: TRANSPORT_IDLE_CONN_TIMEOUT,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
DialContext: dialer.DialContext,
DialContext: d.DialContext,
}
return &ProxyHandler{
logger: logger,
dialer: dialer,
dialer: d,
httptransport: httptransport,
fakeSNI: fakeSNI,
}
@@ -130,7 +147,10 @@ func proxy(ctx context.Context, left net.Conn, leftReader io.Reader, right net.C
}
rtl := func(dst, src net.Conn) {
defer wg.Done()
io.Copy(dst, src)
// Grab a pooled buffer for this copy direction.
bufp := copyBufPool.Get().(*[]byte)
defer copyBufPool.Put(bufp)
io.CopyBuffer(dst, src, *bufp)
dst.Close()
}
wg.Add(2)
@@ -156,6 +176,9 @@ func proxyh2(ctx context.Context, leftreader io.ReadCloser, leftwriter io.Writer
wg := sync.WaitGroup{}
ltr := func(dst net.Conn, src io.Reader) {
defer wg.Done()
bufp := copyBufPool.Get().(*[]byte)
defer copyBufPool.Put(bufp)
io.CopyBuffer(dst, src, *bufp)
copyWithSNIRewrite(dst, src, fakeSNI)
dst.Close()
}
@@ -237,12 +260,14 @@ func flush(flusher interface{}) bool {
}
func copyBody(wr io.Writer, body io.Reader) {
buf := make([]byte, COPY_BUF)
// Use pooled buffer to avoid per-call allocation.
bufp := copyBufPool.Get().(*[]byte)
defer copyBufPool.Put(bufp)
for {
bread, read_err := body.Read(buf)
bread, read_err := body.Read(*bufp)
var write_err error
if bread > 0 {
_, write_err = wr.Write(buf[:bread])
_, write_err = wr.Write((*bufp)[:bread])
flush(wr)
}
if read_err != nil || write_err != nil {