direct.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package ray
  2. import (
  3. "context"
  4. "io"
  5. "os"
  6. "strconv"
  7. "sync"
  8. "time"
  9. "v2ray.com/core/common/buf"
  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 = 20 * 1024 * 1024
  35. func init() {
  36. const raySizeEnvKey = "v2ray.ray.buffer.size"
  37. sizeStr := os.Getenv(raySizeEnvKey)
  38. if len(sizeStr) > 0 {
  39. customSize, err := strconv.ParseUint(sizeStr, 10, 32)
  40. if err == nil {
  41. streamSizeLimit = uint64(customSize) * 1024 * 1024
  42. }
  43. }
  44. }
  45. type Stream struct {
  46. access sync.RWMutex
  47. data buf.MultiBuffer
  48. size uint64
  49. ctx context.Context
  50. wakeup chan bool
  51. close bool
  52. err bool
  53. }
  54. func NewStream(ctx context.Context) *Stream {
  55. return &Stream{
  56. ctx: ctx,
  57. wakeup: make(chan bool, 1),
  58. size: 0,
  59. }
  60. }
  61. func (s *Stream) getData() (buf.MultiBuffer, error) {
  62. s.access.Lock()
  63. defer s.access.Unlock()
  64. if s.data != nil {
  65. mb := s.data
  66. s.data = nil
  67. s.size = 0
  68. return mb, nil
  69. }
  70. if s.close {
  71. return nil, io.EOF
  72. }
  73. if s.err {
  74. return nil, io.ErrClosedPipe
  75. }
  76. return nil, nil
  77. }
  78. func (s *Stream) Peek(b *buf.Buffer) {
  79. s.access.RLock()
  80. defer s.access.RUnlock()
  81. b.Reset(func(data []byte) (int, error) {
  82. return s.data.Copy(data), nil
  83. })
  84. }
  85. func (s *Stream) Read() (buf.MultiBuffer, error) {
  86. for {
  87. mb, err := s.getData()
  88. if err != nil {
  89. return nil, err
  90. }
  91. if mb != nil {
  92. s.wakeUp()
  93. return mb, nil
  94. }
  95. select {
  96. case <-s.ctx.Done():
  97. return nil, io.EOF
  98. case <-s.wakeup:
  99. }
  100. }
  101. }
  102. func (s *Stream) ReadTimeout(timeout time.Duration) (buf.MultiBuffer, error) {
  103. for {
  104. mb, err := s.getData()
  105. if err != nil {
  106. return nil, err
  107. }
  108. if mb != nil {
  109. s.wakeUp()
  110. return mb, nil
  111. }
  112. select {
  113. case <-s.ctx.Done():
  114. return nil, io.EOF
  115. case <-time.After(timeout):
  116. return nil, buf.ErrReadTimeout
  117. case <-s.wakeup:
  118. }
  119. }
  120. }
  121. func (s *Stream) Write(data buf.MultiBuffer) error {
  122. if data.IsEmpty() {
  123. return nil
  124. }
  125. L:
  126. for streamSizeLimit > 0 && s.size >= streamSizeLimit {
  127. select {
  128. case <-s.ctx.Done():
  129. return io.ErrClosedPipe
  130. case <-s.wakeup:
  131. break L
  132. }
  133. }
  134. s.access.Lock()
  135. defer s.access.Unlock()
  136. if s.err || s.close {
  137. data.Release()
  138. return io.ErrClosedPipe
  139. }
  140. if s.data == nil {
  141. s.data = data
  142. } else {
  143. s.data.AppendMulti(data)
  144. }
  145. s.size += uint64(data.Len())
  146. s.wakeUp()
  147. return nil
  148. }
  149. func (s *Stream) wakeUp() {
  150. select {
  151. case s.wakeup <- true:
  152. default:
  153. }
  154. }
  155. func (s *Stream) Close() {
  156. s.access.Lock()
  157. s.close = true
  158. s.wakeUp()
  159. s.access.Unlock()
  160. }
  161. func (s *Stream) CloseError() {
  162. s.access.Lock()
  163. s.err = true
  164. if s.data != nil {
  165. s.data.Release()
  166. s.data = nil
  167. }
  168. s.wakeUp()
  169. s.access.Unlock()
  170. }