2015-10-14 09:07:13 -04:00
|
|
|
package ray
|
|
|
|
|
|
|
|
import (
|
2017-02-07 11:37:26 -05:00
|
|
|
"context"
|
2016-04-18 12:44:10 -04:00
|
|
|
"io"
|
2017-04-16 03:57:28 -04:00
|
|
|
"sync"
|
2017-01-04 06:34:01 -05:00
|
|
|
"time"
|
|
|
|
|
2017-10-23 08:15:16 -04:00
|
|
|
"v2ray.com/core/common"
|
2016-12-09 05:35:27 -05:00
|
|
|
"v2ray.com/core/common/buf"
|
2017-05-25 19:11:38 -04:00
|
|
|
"v2ray.com/core/common/platform"
|
2017-12-27 15:33:42 -05:00
|
|
|
"v2ray.com/core/common/signal"
|
2015-10-14 09:07:13 -04:00
|
|
|
)
|
|
|
|
|
2015-10-15 07:15:59 -04:00
|
|
|
// NewRay creates a new Ray for direct traffic transport.
|
2017-01-26 14:46:44 -05:00
|
|
|
func NewRay(ctx context.Context) Ray {
|
2015-10-14 09:07:13 -04:00
|
|
|
return &directRay{
|
2017-01-26 14:46:44 -05:00
|
|
|
Input: NewStream(ctx),
|
|
|
|
Output: NewStream(ctx),
|
2015-10-14 09:07:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type directRay struct {
|
2016-04-18 12:44:10 -04:00
|
|
|
Input *Stream
|
|
|
|
Output *Stream
|
2015-10-14 09:07:13 -04:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *directRay) OutboundInput() InputStream {
|
|
|
|
return v.Input
|
2015-10-14 09:07:13 -04:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *directRay) OutboundOutput() OutputStream {
|
|
|
|
return v.Output
|
2015-10-14 09:07:13 -04:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *directRay) InboundInput() OutputStream {
|
|
|
|
return v.Input
|
2015-10-14 09:07:13 -04:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *directRay) InboundOutput() InputStream {
|
|
|
|
return v.Output
|
2015-10-14 09:07:13 -04:00
|
|
|
}
|
2016-04-18 12:44:10 -04:00
|
|
|
|
2017-05-18 18:21:48 -04:00
|
|
|
var streamSizeLimit uint64 = 10 * 1024 * 1024
|
2017-05-15 14:31:52 -04:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
const raySizeEnvKey = "v2ray.ray.buffer.size"
|
2017-05-25 19:11:38 -04:00
|
|
|
size := platform.EnvFlag{
|
|
|
|
Name: raySizeEnvKey,
|
|
|
|
AltName: platform.NormalizeEnvName(raySizeEnvKey),
|
|
|
|
}.GetValueAsInt(10)
|
|
|
|
streamSizeLimit = uint64(size) * 1024 * 1024
|
2017-05-15 14:31:52 -04:00
|
|
|
}
|
|
|
|
|
2017-10-23 08:15:16 -04:00
|
|
|
// Stream is a sequential container for data in bytes.
|
2016-04-18 12:44:10 -04:00
|
|
|
type Stream struct {
|
2017-05-15 14:42:10 -04:00
|
|
|
access sync.RWMutex
|
|
|
|
data buf.MultiBuffer
|
|
|
|
size uint64
|
|
|
|
ctx context.Context
|
2017-12-27 15:33:42 -05:00
|
|
|
readSignal *signal.Notifier
|
|
|
|
writeSignal *signal.Notifier
|
2017-05-15 14:42:10 -04:00
|
|
|
close bool
|
|
|
|
err bool
|
2016-04-18 12:44:10 -04:00
|
|
|
}
|
|
|
|
|
2017-10-23 08:15:16 -04:00
|
|
|
// NewStream creates a new Stream.
|
2017-01-26 14:46:44 -05:00
|
|
|
func NewStream(ctx context.Context) *Stream {
|
2016-04-18 12:44:10 -04:00
|
|
|
return &Stream{
|
2017-05-15 14:42:10 -04:00
|
|
|
ctx: ctx,
|
2017-12-27 15:33:42 -05:00
|
|
|
readSignal: signal.NewNotifier(),
|
|
|
|
writeSignal: signal.NewNotifier(),
|
2017-05-15 14:42:10 -04:00
|
|
|
size: 0,
|
2016-04-18 12:44:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-16 03:57:28 -04:00
|
|
|
func (s *Stream) getData() (buf.MultiBuffer, error) {
|
|
|
|
s.access.Lock()
|
|
|
|
defer s.access.Unlock()
|
|
|
|
|
|
|
|
if s.data != nil {
|
|
|
|
mb := s.data
|
|
|
|
s.data = nil
|
2017-05-15 14:31:52 -04:00
|
|
|
s.size = 0
|
2017-04-16 03:57:28 -04:00
|
|
|
return mb, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.close {
|
|
|
|
return nil, io.EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.err {
|
2016-12-24 18:42:03 -05:00
|
|
|
return nil, io.ErrClosedPipe
|
2017-04-16 03:57:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2017-10-23 08:15:16 -04:00
|
|
|
// Peek fills in the given buffer with data from head of the Stream.
|
2017-04-25 04:48:06 -04:00
|
|
|
func (s *Stream) Peek(b *buf.Buffer) {
|
2017-04-24 18:19:22 -04:00
|
|
|
s.access.RLock()
|
|
|
|
defer s.access.RUnlock()
|
|
|
|
|
2017-10-23 08:15:16 -04:00
|
|
|
common.Must(b.Reset(func(data []byte) (int, error) {
|
2017-04-25 04:48:06 -04:00
|
|
|
return s.data.Copy(data), nil
|
2017-10-23 08:15:16 -04:00
|
|
|
}))
|
2017-04-24 18:19:22 -04:00
|
|
|
}
|
|
|
|
|
2017-12-27 15:33:42 -05:00
|
|
|
// ReadMultiBuffer reads data from the Stream.
|
2017-11-09 16:33:15 -05:00
|
|
|
func (s *Stream) ReadMultiBuffer() (buf.MultiBuffer, error) {
|
2017-04-16 03:57:28 -04:00
|
|
|
for {
|
|
|
|
mb, err := s.getData()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if mb != nil {
|
2017-12-27 15:33:42 -05:00
|
|
|
s.readSignal.Signal()
|
2017-04-16 03:57:28 -04:00
|
|
|
return mb, nil
|
|
|
|
}
|
|
|
|
|
2016-12-24 18:42:03 -05:00
|
|
|
select {
|
2017-04-16 03:57:28 -04:00
|
|
|
case <-s.ctx.Done():
|
2017-04-16 15:31:16 -04:00
|
|
|
return nil, io.EOF
|
2017-12-27 15:33:42 -05:00
|
|
|
case <-s.writeSignal.Wait():
|
2016-12-24 18:42:03 -05:00
|
|
|
}
|
2016-04-18 12:44:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-23 08:15:16 -04:00
|
|
|
// ReadTimeout reads from the Stream with a specified timeout.
|
2017-04-16 03:57:28 -04:00
|
|
|
func (s *Stream) ReadTimeout(timeout time.Duration) (buf.MultiBuffer, error) {
|
|
|
|
for {
|
|
|
|
mb, err := s.getData()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if mb != nil {
|
2017-12-27 15:33:42 -05:00
|
|
|
s.readSignal.Signal()
|
2017-04-16 03:57:28 -04:00
|
|
|
return mb, nil
|
2017-03-27 02:56:16 -04:00
|
|
|
}
|
|
|
|
|
2017-01-04 06:34:01 -05:00
|
|
|
select {
|
2017-04-16 03:57:28 -04:00
|
|
|
case <-s.ctx.Done():
|
2017-04-16 15:31:16 -04:00
|
|
|
return nil, io.EOF
|
2017-01-04 06:34:01 -05:00
|
|
|
case <-time.After(timeout):
|
2017-03-27 05:12:34 -04:00
|
|
|
return nil, buf.ErrReadTimeout
|
2017-12-27 15:33:42 -05:00
|
|
|
case <-s.writeSignal.Wait():
|
2017-01-04 06:34:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-23 08:09:14 -04:00
|
|
|
// Size returns the number of bytes hold in the Stream.
|
|
|
|
func (s *Stream) Size() uint64 {
|
|
|
|
s.access.RLock()
|
|
|
|
defer s.access.RUnlock()
|
|
|
|
|
|
|
|
return s.size
|
|
|
|
}
|
|
|
|
|
2017-10-23 08:15:16 -04:00
|
|
|
// waitForStreamSize waits until the Stream has room for more data, or any error happens.
|
2017-10-20 17:23:29 -04:00
|
|
|
func (s *Stream) waitForStreamSize() error {
|
|
|
|
if streamSizeLimit == 0 {
|
2017-04-19 05:20:08 -04:00
|
|
|
return nil
|
2016-12-26 18:44:11 -05:00
|
|
|
}
|
|
|
|
|
2017-10-23 08:09:14 -04:00
|
|
|
for s.Size() >= streamSizeLimit {
|
2017-05-15 14:31:52 -04:00
|
|
|
select {
|
|
|
|
case <-s.ctx.Done():
|
|
|
|
return io.ErrClosedPipe
|
2017-12-27 15:33:42 -05:00
|
|
|
case <-s.readSignal.Wait():
|
2017-05-15 14:42:10 -04:00
|
|
|
if s.err || s.close {
|
|
|
|
return io.ErrClosedPipe
|
|
|
|
}
|
2017-05-15 14:31:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-20 17:23:29 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-27 15:33:42 -05:00
|
|
|
// WriteMultiBuffer writes more data into the Stream.
|
2017-11-09 16:33:15 -05:00
|
|
|
func (s *Stream) WriteMultiBuffer(data buf.MultiBuffer) error {
|
2017-10-20 17:23:29 -04:00
|
|
|
if data.IsEmpty() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.waitForStreamSize(); err != nil {
|
|
|
|
data.Release()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-04-16 03:57:28 -04:00
|
|
|
s.access.Lock()
|
|
|
|
defer s.access.Unlock()
|
|
|
|
|
2017-04-19 05:20:08 -04:00
|
|
|
if s.err || s.close {
|
2017-04-16 03:57:28 -04:00
|
|
|
data.Release()
|
2016-12-24 18:42:03 -05:00
|
|
|
return io.ErrClosedPipe
|
|
|
|
}
|
2016-04-18 12:44:10 -04:00
|
|
|
|
2017-04-16 03:57:28 -04:00
|
|
|
if s.data == nil {
|
2017-11-08 18:55:28 -05:00
|
|
|
s.data = buf.NewMultiBufferCap(128)
|
2017-04-16 03:57:28 -04:00
|
|
|
}
|
2017-11-08 18:55:28 -05:00
|
|
|
|
|
|
|
s.data.AppendMulti(data)
|
2017-05-15 14:31:52 -04:00
|
|
|
s.size += uint64(data.Len())
|
2017-12-27 15:33:42 -05:00
|
|
|
s.writeSignal.Signal()
|
2016-12-22 11:28:06 -05:00
|
|
|
|
2017-04-16 03:57:28 -04:00
|
|
|
return nil
|
2016-04-18 12:44:10 -04:00
|
|
|
}
|
|
|
|
|
2017-10-23 08:15:16 -04:00
|
|
|
// Close closes the stream for writing. Read() still works until EOF.
|
2017-04-16 03:57:28 -04:00
|
|
|
func (s *Stream) Close() {
|
|
|
|
s.access.Lock()
|
|
|
|
s.close = true
|
2017-12-27 15:33:42 -05:00
|
|
|
s.readSignal.Signal()
|
|
|
|
s.writeSignal.Signal()
|
2017-04-16 03:57:28 -04:00
|
|
|
s.access.Unlock()
|
|
|
|
}
|
2016-12-22 11:28:06 -05:00
|
|
|
|
2017-10-23 08:15:16 -04:00
|
|
|
// CloseError closes the Stream with error. Read() will return an error afterwards.
|
2017-04-16 03:57:28 -04:00
|
|
|
func (s *Stream) CloseError() {
|
|
|
|
s.access.Lock()
|
|
|
|
s.err = true
|
|
|
|
if s.data != nil {
|
|
|
|
s.data.Release()
|
|
|
|
s.data = nil
|
2017-05-15 14:42:10 -04:00
|
|
|
s.size = 0
|
2016-04-18 12:44:10 -04:00
|
|
|
}
|
2017-12-27 15:33:42 -05:00
|
|
|
s.readSignal.Signal()
|
|
|
|
s.writeSignal.Signal()
|
2017-04-16 03:57:28 -04:00
|
|
|
s.access.Unlock()
|
2016-12-22 11:28:06 -05:00
|
|
|
}
|