direct.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. readSignal chan bool
  51. writeSignal chan bool
  52. close bool
  53. err bool
  54. }
  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. func (s *Stream) Peek(b *buf.Buffer) {
  81. s.access.RLock()
  82. defer s.access.RUnlock()
  83. b.Reset(func(data []byte) (int, error) {
  84. return s.data.Copy(data), nil
  85. })
  86. }
  87. func (s *Stream) Read() (buf.MultiBuffer, error) {
  88. for {
  89. mb, err := s.getData()
  90. if err != nil {
  91. return nil, err
  92. }
  93. if mb != nil {
  94. s.notifyRead()
  95. return mb, nil
  96. }
  97. select {
  98. case <-s.ctx.Done():
  99. return nil, io.EOF
  100. case <-s.writeSignal:
  101. }
  102. }
  103. }
  104. func (s *Stream) ReadTimeout(timeout time.Duration) (buf.MultiBuffer, error) {
  105. for {
  106. mb, err := s.getData()
  107. if err != nil {
  108. return nil, err
  109. }
  110. if mb != nil {
  111. s.notifyRead()
  112. return mb, nil
  113. }
  114. select {
  115. case <-s.ctx.Done():
  116. return nil, io.EOF
  117. case <-time.After(timeout):
  118. return nil, buf.ErrReadTimeout
  119. case <-s.writeSignal:
  120. }
  121. }
  122. }
  123. func (s *Stream) Write(data buf.MultiBuffer) error {
  124. if data.IsEmpty() {
  125. return nil
  126. }
  127. for streamSizeLimit > 0 && s.size >= streamSizeLimit {
  128. select {
  129. case <-s.ctx.Done():
  130. return io.ErrClosedPipe
  131. case <-s.readSignal:
  132. s.access.RLock()
  133. if s.err || s.close {
  134. data.Release()
  135. s.access.RUnlock()
  136. return io.ErrClosedPipe
  137. }
  138. s.access.RUnlock()
  139. }
  140. }
  141. s.access.Lock()
  142. defer s.access.Unlock()
  143. if s.err || s.close {
  144. data.Release()
  145. return io.ErrClosedPipe
  146. }
  147. if s.data == nil {
  148. s.data = data
  149. } else {
  150. s.data.AppendMulti(data)
  151. }
  152. s.size += uint64(data.Len())
  153. s.notifyWrite()
  154. return nil
  155. }
  156. func (s *Stream) notifyRead() {
  157. select {
  158. case s.readSignal <- true:
  159. default:
  160. }
  161. }
  162. func (s *Stream) notifyWrite() {
  163. select {
  164. case s.writeSignal <- true:
  165. default:
  166. }
  167. }
  168. func (s *Stream) Close() {
  169. s.access.Lock()
  170. s.close = true
  171. s.notifyRead()
  172. s.notifyWrite()
  173. s.access.Unlock()
  174. }
  175. func (s *Stream) CloseError() {
  176. s.access.Lock()
  177. s.err = true
  178. if s.data != nil {
  179. s.data.Release()
  180. s.data = nil
  181. s.size = 0
  182. }
  183. s.notifyRead()
  184. s.notifyWrite()
  185. s.access.Unlock()
  186. }