direct.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package ray
  2. import (
  3. "errors"
  4. "io"
  5. "time"
  6. "context"
  7. "v2ray.com/core/common/buf"
  8. )
  9. const (
  10. bufferSize = 512
  11. )
  12. var ErrReadTimeout = errors.New("Ray: timeout.")
  13. // NewRay creates a new Ray for direct traffic transport.
  14. func NewRay(ctx context.Context) Ray {
  15. return &directRay{
  16. Input: NewStream(ctx),
  17. Output: NewStream(ctx),
  18. }
  19. }
  20. type directRay struct {
  21. Input *Stream
  22. Output *Stream
  23. }
  24. func (v *directRay) OutboundInput() InputStream {
  25. return v.Input
  26. }
  27. func (v *directRay) OutboundOutput() OutputStream {
  28. return v.Output
  29. }
  30. func (v *directRay) InboundInput() OutputStream {
  31. return v.Input
  32. }
  33. func (v *directRay) InboundOutput() InputStream {
  34. return v.Output
  35. }
  36. func (v *directRay) AddInspector(inspector Inspector) {
  37. if inspector == nil {
  38. return
  39. }
  40. v.Input.inspector.AddInspector(inspector)
  41. v.Output.inspector.AddInspector(inspector)
  42. }
  43. type Stream struct {
  44. buffer chan *buf.Buffer
  45. ctx context.Context
  46. close chan bool
  47. err chan bool
  48. inspector *InspectorChain
  49. }
  50. func NewStream(ctx context.Context) *Stream {
  51. return &Stream{
  52. ctx: ctx,
  53. buffer: make(chan *buf.Buffer, bufferSize),
  54. close: make(chan bool),
  55. err: make(chan bool),
  56. inspector: &InspectorChain{},
  57. }
  58. }
  59. func (v *Stream) Read() (*buf.Buffer, error) {
  60. select {
  61. case <-v.ctx.Done():
  62. return nil, io.ErrClosedPipe
  63. case <-v.err:
  64. return nil, io.ErrClosedPipe
  65. case b := <-v.buffer:
  66. return b, nil
  67. default:
  68. select {
  69. case <-v.ctx.Done():
  70. return nil, io.ErrClosedPipe
  71. case b := <-v.buffer:
  72. return b, nil
  73. case <-v.close:
  74. return nil, io.EOF
  75. case <-v.err:
  76. return nil, io.ErrClosedPipe
  77. }
  78. }
  79. }
  80. func (v *Stream) ReadTimeout(timeout time.Duration) (*buf.Buffer, error) {
  81. select {
  82. case <-v.ctx.Done():
  83. return nil, io.ErrClosedPipe
  84. case <-v.err:
  85. return nil, io.ErrClosedPipe
  86. case b := <-v.buffer:
  87. return b, nil
  88. default:
  89. select {
  90. case <-v.ctx.Done():
  91. return nil, io.ErrClosedPipe
  92. case b := <-v.buffer:
  93. return b, nil
  94. case <-v.close:
  95. return nil, io.EOF
  96. case <-v.err:
  97. return nil, io.ErrClosedPipe
  98. case <-time.After(timeout):
  99. return nil, ErrReadTimeout
  100. }
  101. }
  102. }
  103. func (v *Stream) Write(data *buf.Buffer) (err error) {
  104. if data.IsEmpty() {
  105. return
  106. }
  107. select {
  108. case <-v.ctx.Done():
  109. return io.ErrClosedPipe
  110. case <-v.err:
  111. return io.ErrClosedPipe
  112. case <-v.close:
  113. return io.ErrClosedPipe
  114. default:
  115. select {
  116. case <-v.ctx.Done():
  117. return io.ErrClosedPipe
  118. case <-v.err:
  119. return io.ErrClosedPipe
  120. case <-v.close:
  121. return io.ErrClosedPipe
  122. case v.buffer <- data:
  123. v.inspector.Input(data)
  124. return nil
  125. }
  126. }
  127. }
  128. func (v *Stream) Close() {
  129. defer swallowPanic()
  130. close(v.close)
  131. }
  132. func (v *Stream) CloseError() {
  133. defer swallowPanic()
  134. close(v.err)
  135. n := len(v.buffer)
  136. for i := 0; i < n; i++ {
  137. select {
  138. case b := <-v.buffer:
  139. b.Release()
  140. default:
  141. return
  142. }
  143. }
  144. }
  145. func (v *Stream) Release() {}
  146. func swallowPanic() {
  147. recover()
  148. }