1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 18:17:52 -05:00

fix: read logic of PacketConnectionReader

This commit is contained in:
秋のかえで 2022-12-21 17:15:42 +08:00 committed by Shelikhoo
parent c10e16cb5e
commit 1bd1de5f52
No known key found for this signature in database
GPG Key ID: C4D5E79D22B25316
2 changed files with 19 additions and 10 deletions

View File

@ -2,6 +2,7 @@ package trojan
import ( import (
"context" "context"
sync "sync"
core "github.com/v2fly/v2ray-core/v5" core "github.com/v2fly/v2ray-core/v5"
"github.com/v2fly/v2ray-core/v5/common" "github.com/v2fly/v2ray-core/v5/common"
@ -119,7 +120,10 @@ func (c *Client) Process(ctx context.Context, link *transport.Link, dialer inter
defer timer.SetTimeout(sessionPolicy.Timeouts.UplinkOnly) defer timer.SetTimeout(sessionPolicy.Timeouts.UplinkOnly)
packetReader := &PacketReader{Reader: conn} packetReader := &PacketReader{Reader: conn}
splitReader := &PacketSplitReader{Reader: packetReader} splitReader := &PacketConnectionReader{
readerAccess: &sync.Mutex{},
reader: packetReader,
}
return udp.CopyPacketConn(packetConn, splitReader, udp.UpdateActivity(timer)) return udp.CopyPacketConn(packetConn, splitReader, udp.UpdateActivity(timer))
} }

View File

@ -4,6 +4,7 @@ import (
"encoding/binary" "encoding/binary"
"io" "io"
gonet "net" gonet "net"
sync "sync"
"github.com/v2fly/v2ray-core/v5/common/buf" "github.com/v2fly/v2ray-core/v5/common/buf"
"github.com/v2fly/v2ray-core/v5/common/net" "github.com/v2fly/v2ray-core/v5/common/net"
@ -287,25 +288,29 @@ func (r *PacketReader) ReadMultiBufferWithMetadata() (*PacketPayload, error) {
return &PacketPayload{Target: dest, Buffer: mb}, nil return &PacketPayload{Target: dest, Buffer: mb}, nil
} }
type PacketSplitReader struct { type PacketConnectionReader struct {
Reader *PacketReader readerAccess *sync.Mutex
Payload *PacketPayload reader *PacketReader
payload *PacketPayload
} }
func (r *PacketSplitReader) ReadFrom(p []byte) (n int, addr gonet.Addr, err error) { func (r *PacketConnectionReader) ReadFrom(p []byte) (n int, addr gonet.Addr, err error) {
if r.Payload == nil || r.Payload.Buffer.IsEmpty() { r.readerAccess.Lock()
r.Payload, err = r.Reader.ReadMultiBufferWithMetadata() defer r.readerAccess.Unlock()
if r.payload == nil || r.payload.Buffer.IsEmpty() {
r.payload, err = r.reader.ReadMultiBufferWithMetadata()
if err != nil { if err != nil {
return return
} }
} }
addr = &gonet.UDPAddr{ addr = &gonet.UDPAddr{
IP: r.Payload.Target.Address.IP(), IP: r.payload.Target.Address.IP(),
Port: int(r.Payload.Target.Port), Port: int(r.payload.Target.Port),
} }
r.Payload.Buffer, n = buf.SplitBytes(r.Payload.Buffer, p) r.payload.Buffer, n = buf.SplitFirstBytes(r.payload.Buffer, p)
return return
} }