direct.go 4.2 KB

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