direct.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. package ray
  2. import (
  3. "context"
  4. "io"
  5. "sync"
  6. "time"
  7. "v2ray.com/core/common"
  8. "v2ray.com/core/common/buf"
  9. "v2ray.com/core/common/platform"
  10. "v2ray.com/core/common/signal"
  11. )
  12. // NewRay creates a new Ray for direct traffic transport.
  13. func NewRay(ctx context.Context) Ray {
  14. return &directRay{
  15. Input: NewStream(ctx),
  16. Output: NewStream(ctx),
  17. }
  18. }
  19. type directRay struct {
  20. Input *Stream
  21. Output *Stream
  22. }
  23. func (v *directRay) OutboundInput() InputStream {
  24. return v.Input
  25. }
  26. func (v *directRay) OutboundOutput() OutputStream {
  27. return v.Output
  28. }
  29. func (v *directRay) InboundInput() OutputStream {
  30. return v.Input
  31. }
  32. func (v *directRay) InboundOutput() InputStream {
  33. return v.Output
  34. }
  35. var streamSizeLimit uint64 = 10 * 1024 * 1024
  36. func init() {
  37. const raySizeEnvKey = "v2ray.ray.buffer.size"
  38. size := platform.EnvFlag{
  39. Name: raySizeEnvKey,
  40. AltName: platform.NormalizeEnvName(raySizeEnvKey),
  41. }.GetValueAsInt(10)
  42. streamSizeLimit = uint64(size) * 1024 * 1024
  43. }
  44. // Stream is a sequential container for data in bytes.
  45. type Stream struct {
  46. access sync.RWMutex
  47. data buf.MultiBuffer
  48. size uint64
  49. ctx context.Context
  50. readSignal *signal.Notifier
  51. writeSignal *signal.Notifier
  52. close bool
  53. err bool
  54. }
  55. // NewStream creates a new Stream.
  56. func NewStream(ctx context.Context) *Stream {
  57. return &Stream{
  58. ctx: ctx,
  59. readSignal: signal.NewNotifier(),
  60. writeSignal: signal.NewNotifier(),
  61. size: 0,
  62. }
  63. }
  64. func (s *Stream) getData() (buf.MultiBuffer, error) {
  65. s.access.Lock()
  66. defer s.access.Unlock()
  67. if s.data != nil {
  68. mb := s.data
  69. s.data = nil
  70. s.size = 0
  71. return mb, nil
  72. }
  73. if s.err {
  74. return nil, io.ErrClosedPipe
  75. }
  76. if s.close {
  77. return nil, io.EOF
  78. }
  79. return nil, nil
  80. }
  81. // Peek fills in the given buffer with data from head of the Stream.
  82. func (s *Stream) Peek(b *buf.Buffer) {
  83. s.access.RLock()
  84. defer s.access.RUnlock()
  85. common.Must(b.Reset(func(data []byte) (int, error) {
  86. return s.data.Copy(data), nil
  87. }))
  88. }
  89. // ReadMultiBuffer reads data from the Stream.
  90. func (s *Stream) ReadMultiBuffer() (buf.MultiBuffer, error) {
  91. for {
  92. mb, err := s.getData()
  93. if err != nil {
  94. return nil, err
  95. }
  96. if mb != nil {
  97. s.readSignal.Signal()
  98. return mb, nil
  99. }
  100. select {
  101. case <-s.ctx.Done():
  102. return nil, s.ctx.Err()
  103. case <-s.writeSignal.Wait():
  104. }
  105. }
  106. }
  107. // ReadTimeout reads from the Stream with a specified timeout.
  108. func (s *Stream) ReadTimeout(timeout time.Duration) (buf.MultiBuffer, error) {
  109. for {
  110. mb, err := s.getData()
  111. if err != nil {
  112. return nil, err
  113. }
  114. if mb != nil {
  115. s.readSignal.Signal()
  116. return mb, nil
  117. }
  118. select {
  119. case <-s.ctx.Done():
  120. return nil, s.ctx.Err()
  121. case <-time.After(timeout):
  122. return nil, buf.ErrReadTimeout
  123. case <-s.writeSignal.Wait():
  124. }
  125. }
  126. }
  127. // Size returns the number of bytes hold in the Stream.
  128. func (s *Stream) Size() uint64 {
  129. s.access.RLock()
  130. defer s.access.RUnlock()
  131. return s.size
  132. }
  133. // waitForStreamSize waits until the Stream has room for more data, or any error happens.
  134. func (s *Stream) waitForStreamSize() error {
  135. if streamSizeLimit == 0 {
  136. return nil
  137. }
  138. for s.Size() >= streamSizeLimit {
  139. select {
  140. case <-s.ctx.Done():
  141. return s.ctx.Err()
  142. case <-s.readSignal.Wait():
  143. if s.err || s.close {
  144. return io.ErrClosedPipe
  145. }
  146. }
  147. }
  148. return nil
  149. }
  150. // WriteMultiBuffer writes more data into the Stream.
  151. func (s *Stream) WriteMultiBuffer(data buf.MultiBuffer) error {
  152. if data.IsEmpty() {
  153. return nil
  154. }
  155. if err := s.waitForStreamSize(); err != nil {
  156. data.Release()
  157. return err
  158. }
  159. s.access.Lock()
  160. defer s.access.Unlock()
  161. if s.err || s.close {
  162. data.Release()
  163. return io.ErrClosedPipe
  164. }
  165. if s.data == nil {
  166. s.data = buf.NewMultiBufferCap(128)
  167. }
  168. s.data.AppendMulti(data)
  169. s.size += uint64(data.Len())
  170. s.writeSignal.Signal()
  171. return nil
  172. }
  173. // Close closes the stream for writing. Read() still works until EOF.
  174. func (s *Stream) Close() error {
  175. s.access.Lock()
  176. s.close = true
  177. s.readSignal.Signal()
  178. s.writeSignal.Signal()
  179. s.access.Unlock()
  180. return nil
  181. }
  182. // CloseError closes the Stream with error. Read() will return an error afterwards.
  183. func (s *Stream) CloseError() {
  184. s.access.Lock()
  185. s.err = true
  186. if s.data != nil {
  187. s.data.Release()
  188. s.data = nil
  189. s.size = 0
  190. }
  191. s.access.Unlock()
  192. s.readSignal.Signal()
  193. s.writeSignal.Signal()
  194. }