mirror of
https://github.com/XTLS/Xray-core.git
synced 2026-10-22 15:41:22 +00:00
Some checks failed
Build and Release for Windows 7 / check-assets (push) Has been cancelled
Build and Release / check-assets (push) Has been cancelled
Tests and Checkings / check-assets (push) Has been cancelled
Tests and Checkings / check-proto (push) Has been cancelled
Tests and Checkings / check-format (push) Has been cancelled
Build and Release for Windows 7 / build (win7-32, 386, windows) (push) Has been cancelled
Build and Release for Windows 7 / build (win7-64, amd64, windows) (push) Has been cancelled
Build and Release / build (386, freebsd, ) (push) Has been cancelled
Build and Release / build (386, linux, ) (push) Has been cancelled
Build and Release / build (386, openbsd, ) (push) Has been cancelled
Build and Release / build (386, windows, ) (push) Has been cancelled
Build and Release / build (amd64, android, android-amd64) (push) Has been cancelled
Build and Release / build (amd64, darwin, ) (push) Has been cancelled
Build and Release / build (amd64, freebsd, ) (push) Has been cancelled
Build and Release / build (amd64, linux, ) (push) Has been cancelled
Build and Release / build (amd64, openbsd, ) (push) Has been cancelled
Build and Release / build (amd64, windows, ) (push) Has been cancelled
Build and Release / build (arm, 5, linux) (push) Has been cancelled
Build and Release / build (arm, 6, linux) (push) Has been cancelled
Build and Release / build (arm, 7, freebsd) (push) Has been cancelled
Build and Release / build (arm, 7, linux) (push) Has been cancelled
Build and Release / build (arm, 7, openbsd) (push) Has been cancelled
Build and Release / build (arm64, android) (push) Has been cancelled
Build and Release / build (arm64, darwin) (push) Has been cancelled
Build and Release / build (arm64, freebsd) (push) Has been cancelled
Build and Release / build (arm64, linux) (push) Has been cancelled
Build and Release / build (arm64, openbsd) (push) Has been cancelled
Build and Release / build (arm64, windows) (push) Has been cancelled
Build and Release / build (loong64, linux) (push) Has been cancelled
Build and Release / build (mips, linux) (push) Has been cancelled
Build and Release / build (mips64, linux) (push) Has been cancelled
Build and Release / build (mips64le, linux) (push) Has been cancelled
Build and Release / build (mipsle, linux) (push) Has been cancelled
Build and Release / build (ppc64, linux) (push) Has been cancelled
Build and Release / build (ppc64le, linux) (push) Has been cancelled
Build and Release / build (riscv64, linux) (push) Has been cancelled
Build and Release / build (s390x, linux) (push) Has been cancelled
Tests and Checkings / test (macos-latest) (push) Has been cancelled
Tests and Checkings / test (ubuntu-latest) (push) Has been cancelled
Tests and Checkings / test (windows-latest) (push) Has been cancelled
https://github.com/XTLS/Xray-core/pull/6822#issuecomment-5993165787
60 lines
1.4 KiB
Go
60 lines
1.4 KiB
Go
package mux
|
|
|
|
import (
|
|
"io"
|
|
|
|
"github.com/xtls/xray-core/common/buf"
|
|
"github.com/xtls/xray-core/common/crypto"
|
|
"github.com/xtls/xray-core/common/errors"
|
|
"github.com/xtls/xray-core/common/net"
|
|
"github.com/xtls/xray-core/common/serial"
|
|
)
|
|
|
|
// PacketReader is an io.Reader that reads whole chunk of Mux frames every time.
|
|
type PacketReader struct {
|
|
reader io.Reader
|
|
eof bool
|
|
dest net.Destination
|
|
}
|
|
|
|
// NewPacketReader creates a new PacketReader.
|
|
// dest is copied because the caller reuses it for the next frame.
|
|
func NewPacketReader(reader io.Reader, dest *net.Destination) *PacketReader {
|
|
return &PacketReader{
|
|
reader: reader,
|
|
eof: false,
|
|
dest: *dest,
|
|
}
|
|
}
|
|
|
|
// ReadMultiBuffer implements buf.Reader.
|
|
func (r *PacketReader) ReadMultiBuffer() (buf.MultiBuffer, error) {
|
|
if r.eof {
|
|
return nil, io.EOF
|
|
}
|
|
|
|
size, err := serial.ReadUint16(r.reader)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if size > buf.Size {
|
|
return nil, errors.New("packet size too large: ", size)
|
|
}
|
|
|
|
b := buf.New()
|
|
if _, err := b.ReadFullFrom(r.reader, int32(size)); err != nil {
|
|
b.Release()
|
|
return nil, err
|
|
}
|
|
r.eof = true
|
|
if r.dest.Network == net.Network_UDP {
|
|
b.UDP = &r.dest // only one packet is read, so b owns r.dest
|
|
}
|
|
return buf.MultiBuffer{b}, nil
|
|
}
|
|
|
|
// NewStreamReader creates a new StreamReader.
|
|
func NewStreamReader(reader *buf.BufferedReader) buf.Reader {
|
|
return crypto.NewChunkStreamReaderWithChunkCount(crypto.PlainChunkSizeParser{}, reader, 1)
|
|
}
|