outbound.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package commander
  2. import (
  3. "context"
  4. "sync"
  5. "github.com/v2fly/v2ray-core/v5/common"
  6. "github.com/v2fly/v2ray-core/v5/common/net"
  7. "github.com/v2fly/v2ray-core/v5/common/signal/done"
  8. "github.com/v2fly/v2ray-core/v5/transport"
  9. )
  10. func NewOutboundListener() *OutboundListener {
  11. return &OutboundListener{
  12. buffer: make(chan net.Conn, 4),
  13. done: done.New(),
  14. }
  15. }
  16. // OutboundListener is a net.Listener for listening gRPC connections.
  17. type OutboundListener struct {
  18. buffer chan net.Conn
  19. done *done.Instance
  20. }
  21. func (l *OutboundListener) add(conn net.Conn) {
  22. select {
  23. case l.buffer <- conn:
  24. case <-l.done.Wait():
  25. conn.Close()
  26. default:
  27. conn.Close()
  28. }
  29. }
  30. // Accept implements net.Listener.
  31. func (l *OutboundListener) Accept() (net.Conn, error) {
  32. select {
  33. case <-l.done.Wait():
  34. return nil, newError("listen closed")
  35. case c := <-l.buffer:
  36. return c, nil
  37. }
  38. }
  39. // Close implement net.Listener.
  40. func (l *OutboundListener) Close() error {
  41. common.Must(l.done.Close())
  42. L:
  43. for {
  44. select {
  45. case c := <-l.buffer:
  46. c.Close()
  47. default:
  48. break L
  49. }
  50. }
  51. return nil
  52. }
  53. // Addr implements net.Listener.
  54. func (l *OutboundListener) Addr() net.Addr {
  55. return &net.TCPAddr{
  56. IP: net.IP{0, 0, 0, 0},
  57. Port: 0,
  58. }
  59. }
  60. func NewOutbound(tag string, listener *OutboundListener) *Outbound {
  61. return &Outbound{
  62. tag: tag,
  63. listener: listener,
  64. }
  65. }
  66. // Outbound is a outbound.Handler that handles gRPC connections.
  67. type Outbound struct {
  68. tag string
  69. listener *OutboundListener
  70. access sync.RWMutex
  71. closed bool
  72. }
  73. // Dispatch implements outbound.Handler.
  74. func (co *Outbound) Dispatch(ctx context.Context, link *transport.Link) {
  75. co.access.RLock()
  76. if co.closed {
  77. common.Interrupt(link.Reader)
  78. common.Interrupt(link.Writer)
  79. co.access.RUnlock()
  80. return
  81. }
  82. closeSignal := done.New()
  83. c := net.NewConnection(net.ConnectionInputMulti(link.Writer), net.ConnectionOutputMulti(link.Reader), net.ConnectionOnClose(closeSignal))
  84. co.listener.add(c)
  85. co.access.RUnlock()
  86. <-closeSignal.Wait()
  87. }
  88. // Tag implements outbound.Handler.
  89. func (co *Outbound) Tag() string {
  90. return co.tag
  91. }
  92. // Start implements common.Runnable.
  93. func (co *Outbound) Start() error {
  94. co.access.Lock()
  95. co.closed = false
  96. co.access.Unlock()
  97. return nil
  98. }
  99. // Close implements common.Closable.
  100. func (co *Outbound) Close() error {
  101. co.access.Lock()
  102. defer co.access.Unlock()
  103. co.closed = true
  104. return co.listener.Close()
  105. }