direct.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. // Size returns the number of bytes hold in the Stream.
  121. func (s *Stream) Size() uint64 {
  122. s.access.RLock()
  123. defer s.access.RUnlock()
  124. return s.size
  125. }
  126. func (s *Stream) waitForStreamSize() error {
  127. if streamSizeLimit == 0 {
  128. return nil
  129. }
  130. for s.Size() >= streamSizeLimit {
  131. select {
  132. case <-s.ctx.Done():
  133. return io.ErrClosedPipe
  134. case <-s.readSignal:
  135. if s.err || s.close {
  136. return io.ErrClosedPipe
  137. }
  138. }
  139. }
  140. return nil
  141. }
  142. func (s *Stream) Write(data buf.MultiBuffer) error {
  143. if data.IsEmpty() {
  144. return nil
  145. }
  146. if err := s.waitForStreamSize(); err != nil {
  147. data.Release()
  148. return err
  149. }
  150. s.access.Lock()
  151. defer s.access.Unlock()
  152. if s.err || s.close {
  153. data.Release()
  154. return io.ErrClosedPipe
  155. }
  156. if s.data == nil {
  157. s.data = data
  158. } else {
  159. s.data.AppendMulti(data)
  160. }
  161. s.size += uint64(data.Len())
  162. s.notifyWrite()
  163. return nil
  164. }
  165. func (s *Stream) notifyRead() {
  166. select {
  167. case s.readSignal <- true:
  168. default:
  169. }
  170. }
  171. func (s *Stream) notifyWrite() {
  172. select {
  173. case s.writeSignal <- true:
  174. default:
  175. }
  176. }
  177. func (s *Stream) Close() {
  178. s.access.Lock()
  179. s.close = true
  180. s.notifyRead()
  181. s.notifyWrite()
  182. s.access.Unlock()
  183. }
  184. func (s *Stream) CloseError() {
  185. s.access.Lock()
  186. s.err = true
  187. if s.data != nil {
  188. s.data.Release()
  189. s.data = nil
  190. s.size = 0
  191. }
  192. s.notifyRead()
  193. s.notifyWrite()
  194. s.access.Unlock()
  195. }