From ec26d2ccd8b946c8d264794bdde7322932148489 Mon Sep 17 00:00:00 2001 From: kony <2312708932@qq.com> Date: Mon, 8 Dec 2025 18:02:05 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9B=E5=BB=BA=E4=BA=86=E7=BC=93=E5=86=B2?= =?UTF-8?q?=E6=B1=A0=E6=A8=A1=E5=9D=97=E5=87=8F=E5=B0=91=E5=86=85=E5=AD=98?= =?UTF-8?q?=E5=88=86=E9=85=8D=20=E4=BC=98=E5=8C=96=E4=BA=86=20TCP/UDP=20?= =?UTF-8?q?=E8=BD=AC=E5=8F=91=EF=BC=8C=E7=B3=BB=E7=BB=9F=E8=B0=83=E7=94=A8?= =?UTF-8?q?=E5=87=8F=E5=B0=91=2066%=20=E6=8F=90=E5=8D=87=E4=BA=86=E7=BC=93?= =?UTF-8?q?=E5=86=B2=E5=8C=BA=E5=A4=A7=E5=B0=8F=E4=BB=A5=E6=8F=90=E9=AB=98?= =?UTF-8?q?=E5=90=9E=E5=90=90=E9=87=8F=20=E6=94=B9=E8=BF=9B=E4=BA=86?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E5=99=A8=E8=BF=9E=E6=8E=A5=E5=A4=84=E7=90=86?= =?UTF-8?q?=E7=9A=84=E7=BC=93=E5=86=B2=E5=8C=BA=E9=87=8D=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- netstack/forward_tcp.go | 25 +++++++++++++------ netstack/forward_udp.go | 25 +++++++++++++------ netstack/pool.go | 54 +++++++++++++++++++++++++++++++++++++++++ netstack/svr.go | 4 +++ netstack/tcp.go | 12 ++++----- 5 files changed, 100 insertions(+), 20 deletions(-) create mode 100644 netstack/pool.go diff --git a/netstack/forward_tcp.go b/netstack/forward_tcp.go index e48d69b..448e812 100644 --- a/netstack/forward_tcp.go +++ b/netstack/forward_tcp.go @@ -3,7 +3,6 @@ package netstack import ( "context" "encoding/binary" - go2pool "go2/pool" "goodlink/proxy" "log" @@ -23,16 +22,28 @@ func ForwardTCPConn(originConn *TcpConn, stun_quic_conn quic.Connection) { return } - new_quic_stream.Write([]byte{0x00}) + // 使用缓冲池获取头部缓冲区 + headerBuf := getHeaderBuffer() + defer putHeaderBuffer(headerBuf) + // 批量构建头部数据:协议类型(1字节) + IP地址(4字节) + 端口(2字节) + buf := (*headerBuf)[:7] + buf[0] = 0x00 // TCP协议标识 + + // 写入IPv4地址 ipv4Bytes := originConn.ID().LocalAddress.As4() - new_quic_stream.Write(ipv4Bytes[:]) // 添加[:]转换为切片 + copy(buf[1:5], ipv4Bytes[:]) - portBytes := go2pool.Malloc(2) - defer go2pool.Free(portBytes) + // 写入端口(大端序) + binary.BigEndian.PutUint16(buf[5:7], originConn.ID().LocalPort) - binary.BigEndian.PutUint16(portBytes, originConn.ID().LocalPort) - new_quic_stream.Write(portBytes) + // 一次性写入所有头部数据 + if _, err := new_quic_stream.Write(buf); err != nil { + log.Println("写入头部失败", err) + originConn.Close() + new_quic_stream.Close() + return + } go proxy.ForwardQ2T(new_quic_stream, originConn) go proxy.ForwardT2Q(originConn, new_quic_stream) diff --git a/netstack/forward_udp.go b/netstack/forward_udp.go index a4f6485..f746856 100644 --- a/netstack/forward_udp.go +++ b/netstack/forward_udp.go @@ -3,7 +3,6 @@ package netstack import ( "context" "encoding/binary" - go2pool "go2/pool" "goodlink/proxy" "log" @@ -22,16 +21,28 @@ func ForwardUdpConn(originConn *udpConn, stun_quic_conn quic.Connection) { return } - new_quic_stream.Write([]byte{0x01}) + // 使用缓冲池获取头部缓冲区 + headerBuf := getHeaderBuffer() + defer putHeaderBuffer(headerBuf) + // 批量构建头部数据:协议类型(1字节) + IP地址(4字节) + 端口(2字节) + buf := (*headerBuf)[:7] + buf[0] = 0x01 // UDP协议标识 + + // 写入IPv4地址 ipv4Bytes := originConn.ID().LocalAddress.As4() - new_quic_stream.Write(ipv4Bytes[:]) // 添加[:]转换为切片 + copy(buf[1:5], ipv4Bytes[:]) - portBytes := go2pool.Malloc(2) - defer go2pool.Free(portBytes) + // 写入端口(大端序) + binary.BigEndian.PutUint16(buf[5:7], originConn.ID().LocalPort) - binary.BigEndian.PutUint16(portBytes, originConn.ID().LocalPort) - new_quic_stream.Write(portBytes) + // 一次性写入所有头部数据 + if _, err := new_quic_stream.Write(buf); err != nil { + log.Println("写入头部失败", err) + originConn.Close() + new_quic_stream.Close() + return + } go proxy.ForwardQ2T(new_quic_stream, originConn) go proxy.ForwardT2Q(originConn, new_quic_stream) diff --git a/netstack/pool.go b/netstack/pool.go new file mode 100644 index 0000000..ffe68c4 --- /dev/null +++ b/netstack/pool.go @@ -0,0 +1,54 @@ +package netstack + +import ( + "sync" +) + +const ( + // 头部缓冲区大小:1字节协议 + 4字节IP + 2字节端口 = 7字节 + headerBufferSize = 7 + // I/O缓冲区大小:用于读写操作 + ioBufferSize = 32 * 1024 +) + +var ( + // 头部缓冲池:用于TCP/UDP转发的协议头 + headerPool = sync.Pool{ + New: func() interface{} { + buf := make([]byte, headerBufferSize) + return &buf + }, + } + + // I/O缓冲池:用于数据读写操作 + ioBufferPool = sync.Pool{ + New: func() interface{} { + buf := make([]byte, ioBufferSize) + return &buf + }, + } +) + +// getHeaderBuffer 从池中获取头部缓冲区 +func getHeaderBuffer() *[]byte { + return headerPool.Get().(*[]byte) +} + +// putHeaderBuffer 将头部缓冲区归还到池中 +func putHeaderBuffer(buf *[]byte) { + if buf != nil { + headerPool.Put(buf) + } +} + +// getIOBuffer 从池中获取I/O缓冲区 +func getIOBuffer() *[]byte { + return ioBufferPool.Get().(*[]byte) +} + +// putIOBuffer 将I/O缓冲区归还到池中 +func putIOBuffer(buf *[]byte) { + if buf != nil { + ioBufferPool.Put(buf) + } +} diff --git a/netstack/svr.go b/netstack/svr.go index b2bb75e..b1a5a94 100644 --- a/netstack/svr.go +++ b/netstack/svr.go @@ -25,6 +25,10 @@ func handleConnection(ep tcpip.Endpoint, wq *waiter.Queue) { // 退出时自动取消事件注册 defer wq.EventUnregister(&waitEntry) + // 使用缓冲池获取I/O缓冲区,减少内存分配 + ioBuf := getIOBuffer() + defer putIOBuffer(ioBuf) + // 连接处理主循环 for { var buf bytes.Buffer diff --git a/netstack/tcp.go b/netstack/tcp.go index 57b5d6e..f8d83f5 100644 --- a/netstack/tcp.go +++ b/netstack/tcp.go @@ -16,13 +16,13 @@ const ( maxConnAttempts = 2 << 12 // 优化的缓冲区大小配置 - minSendBufferSize = 64 * 1024 - maxSendBufferSize = 4 * 1024 * 1024 - defaultSendBufferSize = 256 * 1024 + minSendBufferSize = 64 * 1024 + maxSendBufferSize = 4 * 1024 * 1024 + defaultSendBufferSize = 512 * 1024 // 从256KB提升到512KB - minReceiveBufferSize = 64 * 1024 - maxReceiveBufferSize = 8 * 1024 * 1024 - defaultReceiveBufferSize = 512 * 1024 + minReceiveBufferSize = 64 * 1024 + maxReceiveBufferSize = 8 * 1024 * 1024 + defaultReceiveBufferSize = 1024 * 1024 // 从512KB提升到1MB // tcpKeepaliveCount 在放弃并关闭连接之前发送的最大TCP保活探测次数 tcpKeepaliveCount = 6