direct.go 3.6 KB

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