mirror of
https://github.com/XTLS/Xray-core.git
synced 2026-07-03 02:08:45 +00:00
WireGuard: Use Xray's buffer (#5880)
Fixes https://github.com/XTLS/Xray-core/issues/5878
This commit is contained in:
+13
-8
@@ -10,6 +10,7 @@ import (
|
|||||||
"golang.zx2c4.com/wireguard/conn"
|
"golang.zx2c4.com/wireguard/conn"
|
||||||
"golang.zx2c4.com/wireguard/device"
|
"golang.zx2c4.com/wireguard/device"
|
||||||
|
|
||||||
|
"github.com/xtls/xray-core/common/buf"
|
||||||
"github.com/xtls/xray-core/common/errors"
|
"github.com/xtls/xray-core/common/errors"
|
||||||
"github.com/xtls/xray-core/common/net"
|
"github.com/xtls/xray-core/common/net"
|
||||||
"github.com/xtls/xray-core/features/dns"
|
"github.com/xtls/xray-core/features/dns"
|
||||||
@@ -17,7 +18,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type netReadInfo struct {
|
type netReadInfo struct {
|
||||||
buff []byte
|
buff *buf.Buffer
|
||||||
endpoint conn.Endpoint
|
endpoint conn.Endpoint
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,7 +83,8 @@ func (bind *netBind) Open(uport uint16) ([]conn.ReceiveFunc, uint16, error) {
|
|||||||
fun := func(bufs [][]byte, sizes []int, eps []conn.Endpoint) (n int, err error) {
|
fun := func(bufs [][]byte, sizes []int, eps []conn.Endpoint) (n int, err error) {
|
||||||
select {
|
select {
|
||||||
case r := <-bind.readQueue:
|
case r := <-bind.readQueue:
|
||||||
sizes[0], eps[0] = copy(bufs[0], r.buff), r.endpoint
|
sizes[0], eps[0] = copy(bufs[0], r.buff.Bytes()), r.endpoint
|
||||||
|
r.buff.Release()
|
||||||
return 1, nil
|
return 1, nil
|
||||||
case <-bind.closedCh:
|
case <-bind.closedCh:
|
||||||
errors.LogDebug(context.Background(), "recv func closed")
|
errors.LogDebug(context.Background(), "recv func closed")
|
||||||
@@ -130,27 +132,30 @@ func (bind *netBindClient) connectTo(endpoint *netEndpoint) error {
|
|||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
buff := make([]byte, device.MaxMessageSize)
|
buff := buf.NewWithSize(device.MaxMessageSize)
|
||||||
n, err := c.Read(buff)
|
n, err := buff.ReadFrom(c)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
buff.Release()
|
||||||
endpoint.conn = nil
|
endpoint.conn = nil
|
||||||
c.Close()
|
c.Close()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rawBytes := buff.Bytes()
|
||||||
if n > 3 {
|
if n > 3 {
|
||||||
buff[1] = 0
|
rawBytes[1] = 0
|
||||||
buff[2] = 0
|
rawBytes[2] = 0
|
||||||
buff[3] = 0
|
rawBytes[3] = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case bind.readQueue <- &netReadInfo{
|
case bind.readQueue <- &netReadInfo{
|
||||||
buff: buff[:n],
|
buff: buff,
|
||||||
endpoint: endpoint,
|
endpoint: endpoint,
|
||||||
}:
|
}:
|
||||||
case <-bind.closedCh:
|
case <-bind.closedCh:
|
||||||
|
buff.Release()
|
||||||
endpoint.conn = nil
|
endpoint.conn = nil
|
||||||
c.Close()
|
c.Close()
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -101,17 +101,17 @@ func (s *Server) Process(ctx context.Context, network net.Network, conn stat.Con
|
|||||||
}
|
}
|
||||||
|
|
||||||
for i, b := range mb {
|
for i, b := range mb {
|
||||||
buff := b.Bytes()
|
|
||||||
|
|
||||||
|
rawBytes := b.Bytes()
|
||||||
if b.Len() > 3 {
|
if b.Len() > 3 {
|
||||||
buff[1] = 0
|
rawBytes[1] = 0
|
||||||
buff[2] = 0
|
rawBytes[2] = 0
|
||||||
buff[3] = 0
|
rawBytes[3] = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case s.bindServer.readQueue <- &netReadInfo{
|
case s.bindServer.readQueue <- &netReadInfo{
|
||||||
buff: buff,
|
buff: b,
|
||||||
endpoint: nep,
|
endpoint: nep,
|
||||||
}:
|
}:
|
||||||
case <-s.bindServer.closedCh:
|
case <-s.bindServer.closedCh:
|
||||||
|
|||||||
Reference in New Issue
Block a user