2020-11-25 13:01:53 +02:00
|
|
|
package buf
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
2021-09-20 15:11:21 +03:00
|
|
|
|
|
|
|
"github.com/xtls/xray-core/features/stats"
|
|
|
|
"github.com/xtls/xray-core/transport/internet/stat"
|
2020-11-25 13:01:53 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Reader extends io.Reader with MultiBuffer.
|
|
|
|
type Reader interface {
|
|
|
|
// ReadMultiBuffer reads content from underlying reader, and put it into a MultiBuffer.
|
|
|
|
ReadMultiBuffer() (MultiBuffer, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ErrReadTimeout is an error that happens with IO timeout.
|
|
|
|
var ErrReadTimeout = newError("IO timeout")
|
|
|
|
|
|
|
|
// TimeoutReader is a reader that returns error if Read() operation takes longer than the given timeout.
|
|
|
|
type TimeoutReader interface {
|
|
|
|
ReadMultiBufferTimeout(time.Duration) (MultiBuffer, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Writer extends io.Writer with MultiBuffer.
|
|
|
|
type Writer interface {
|
|
|
|
// WriteMultiBuffer writes a MultiBuffer into underlying writer.
|
|
|
|
WriteMultiBuffer(MultiBuffer) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteAllBytes ensures all bytes are written into the given writer.
|
2021-09-20 15:11:21 +03:00
|
|
|
func WriteAllBytes(writer io.Writer, payload []byte, c stats.Counter) error {
|
|
|
|
wc := 0
|
|
|
|
defer func() {
|
|
|
|
if c != nil {
|
|
|
|
c.Add(int64(wc))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-11-25 13:01:53 +02:00
|
|
|
for len(payload) > 0 {
|
|
|
|
n, err := writer.Write(payload)
|
2021-09-20 15:11:21 +03:00
|
|
|
wc += n
|
2020-11-25 13:01:53 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
payload = payload[n:]
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func isPacketReader(reader io.Reader) bool {
|
|
|
|
_, ok := reader.(net.PacketConn)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewReader creates a new Reader.
|
|
|
|
// The Reader instance doesn't take the ownership of reader.
|
|
|
|
func NewReader(reader io.Reader) Reader {
|
|
|
|
if mr, ok := reader.(Reader); ok {
|
|
|
|
return mr
|
|
|
|
}
|
|
|
|
|
|
|
|
if isPacketReader(reader) {
|
|
|
|
return &PacketReader{
|
|
|
|
Reader: reader,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_, isFile := reader.(*os.File)
|
|
|
|
if !isFile && useReadv {
|
|
|
|
if sc, ok := reader.(syscall.Conn); ok {
|
|
|
|
rawConn, err := sc.SyscallConn()
|
|
|
|
if err != nil {
|
|
|
|
newError("failed to get sysconn").Base(err).WriteToLog()
|
|
|
|
} else {
|
2021-09-23 06:37:46 +03:00
|
|
|
var counter stats.Counter
|
|
|
|
|
|
|
|
if statConn, ok := reader.(*stat.CounterConnection); ok {
|
|
|
|
reader = statConn.Connection
|
|
|
|
counter = statConn.ReadCounter
|
|
|
|
}
|
2021-09-20 15:11:21 +03:00
|
|
|
return NewReadVReader(reader, rawConn, counter)
|
2020-11-25 13:01:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &SingleReader{
|
|
|
|
Reader: reader,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewPacketReader creates a new PacketReader based on the given reader.
|
|
|
|
func NewPacketReader(reader io.Reader) Reader {
|
|
|
|
if mr, ok := reader.(Reader); ok {
|
|
|
|
return mr
|
|
|
|
}
|
|
|
|
|
|
|
|
return &PacketReader{
|
|
|
|
Reader: reader,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func isPacketWriter(writer io.Writer) bool {
|
|
|
|
if _, ok := writer.(net.PacketConn); ok {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the writer doesn't implement syscall.Conn, it is probably not a TCP connection.
|
|
|
|
if _, ok := writer.(syscall.Conn); !ok {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewWriter creates a new Writer.
|
|
|
|
func NewWriter(writer io.Writer) Writer {
|
|
|
|
if mw, ok := writer.(Writer); ok {
|
|
|
|
return mw
|
|
|
|
}
|
|
|
|
|
2021-10-19 19:57:14 +03:00
|
|
|
iConn := writer
|
2021-09-20 15:11:21 +03:00
|
|
|
if statConn, ok := writer.(*stat.CounterConnection); ok {
|
|
|
|
iConn = statConn.Connection
|
|
|
|
}
|
|
|
|
|
|
|
|
if isPacketWriter(iConn) {
|
2020-11-25 13:01:53 +02:00
|
|
|
return &SequentialWriter{
|
|
|
|
Writer: writer,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-20 15:11:21 +03:00
|
|
|
var counter stats.Counter
|
|
|
|
|
|
|
|
if statConn, ok := writer.(*stat.CounterConnection); ok {
|
|
|
|
counter = statConn.WriteCounter
|
|
|
|
}
|
2020-11-25 13:01:53 +02:00
|
|
|
return &BufferToBytesWriter{
|
2021-09-20 15:11:21 +03:00
|
|
|
Writer: iConn,
|
|
|
|
counter: counter,
|
2020-11-25 13:01:53 +02:00
|
|
|
}
|
|
|
|
}
|