direct.go 3.4 KB

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