hub.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package udp
  2. import (
  3. "net"
  4. "sync"
  5. "v2ray.com/core/common/alloc"
  6. "v2ray.com/core/common/dice"
  7. "v2ray.com/core/common/log"
  8. v2net "v2ray.com/core/common/net"
  9. "v2ray.com/core/common/signal"
  10. "v2ray.com/core/proxy"
  11. "v2ray.com/core/transport/internet/internal"
  12. )
  13. type UDPPayload struct {
  14. payload *alloc.Buffer
  15. session *proxy.SessionInfo
  16. }
  17. type UDPPayloadHandler func(*alloc.Buffer, *proxy.SessionInfo)
  18. type UDPPayloadQueue struct {
  19. queue []chan UDPPayload
  20. callback UDPPayloadHandler
  21. cancel *signal.CancelSignal
  22. }
  23. func NewUDPPayloadQueue(option ListenOption) *UDPPayloadQueue {
  24. queue := &UDPPayloadQueue{
  25. callback: option.Callback,
  26. cancel: signal.NewCloseSignal(),
  27. queue: make([]chan UDPPayload, option.Concurrency),
  28. }
  29. for i := range queue.queue {
  30. queue.queue[i] = make(chan UDPPayload, 64)
  31. go queue.Dequeue(queue.queue[i])
  32. }
  33. return queue
  34. }
  35. func (this *UDPPayloadQueue) Enqueue(payload UDPPayload) {
  36. size := len(this.queue)
  37. for i := 0; i < size; i++ {
  38. idx := 0
  39. if size > 1 {
  40. idx = dice.Roll(size)
  41. }
  42. select {
  43. case this.queue[idx] <- payload:
  44. return
  45. default:
  46. }
  47. }
  48. }
  49. func (this *UDPPayloadQueue) Dequeue(queue <-chan UDPPayload) {
  50. this.cancel.WaitThread()
  51. defer this.cancel.FinishThread()
  52. for !this.cancel.Cancelled() {
  53. payload, open := <-queue
  54. if !open {
  55. return
  56. }
  57. this.callback(payload.payload, payload.session)
  58. }
  59. }
  60. func (this *UDPPayloadQueue) Close() {
  61. this.cancel.Cancel()
  62. for _, queue := range this.queue {
  63. close(queue)
  64. }
  65. this.cancel.WaitForDone()
  66. }
  67. type ListenOption struct {
  68. Callback UDPPayloadHandler
  69. ReceiveOriginalDest bool
  70. Concurrency int
  71. }
  72. type UDPHub struct {
  73. sync.RWMutex
  74. conn *net.UDPConn
  75. pool *alloc.BufferPool
  76. cancel *signal.CancelSignal
  77. queue *UDPPayloadQueue
  78. option ListenOption
  79. }
  80. func ListenUDP(address v2net.Address, port v2net.Port, option ListenOption) (*UDPHub, error) {
  81. if option.Concurrency < 1 {
  82. option.Concurrency = 1
  83. }
  84. udpConn, err := net.ListenUDP("udp", &net.UDPAddr{
  85. IP: address.IP(),
  86. Port: int(port),
  87. })
  88. if err != nil {
  89. return nil, err
  90. }
  91. if option.ReceiveOriginalDest {
  92. fd, err := internal.GetSysFd(udpConn)
  93. if err != nil {
  94. log.Warning("UDP|Listener: Failed to get fd: ", err)
  95. return nil, err
  96. }
  97. err = SetOriginalDestOptions(fd)
  98. if err != nil {
  99. log.Warning("UDP|Listener: Failed to set socket options: ", err)
  100. return nil, err
  101. }
  102. }
  103. hub := &UDPHub{
  104. conn: udpConn,
  105. pool: alloc.NewBufferPool(2048, 64),
  106. queue: NewUDPPayloadQueue(option),
  107. option: option,
  108. cancel: signal.NewCloseSignal(),
  109. }
  110. go hub.start()
  111. return hub, nil
  112. }
  113. func (this *UDPHub) Close() {
  114. this.Lock()
  115. defer this.Unlock()
  116. this.cancel.Cancel()
  117. this.conn.Close()
  118. this.cancel.WaitForDone()
  119. this.queue.Close()
  120. }
  121. func (this *UDPHub) WriteTo(payload []byte, dest v2net.Destination) (int, error) {
  122. return this.conn.WriteToUDP(payload, &net.UDPAddr{
  123. IP: dest.Address.IP(),
  124. Port: int(dest.Port),
  125. })
  126. }
  127. func (this *UDPHub) start() {
  128. this.cancel.WaitThread()
  129. defer this.cancel.FinishThread()
  130. oobBytes := make([]byte, 256)
  131. for this.Running() {
  132. buffer := this.pool.Allocate()
  133. nBytes, noob, _, addr, err := ReadUDPMsg(this.conn, buffer.Value, oobBytes)
  134. if err != nil {
  135. log.Info("UDP|Hub: Failed to read UDP msg: ", err)
  136. buffer.Release()
  137. continue
  138. }
  139. buffer.Slice(0, nBytes)
  140. session := new(proxy.SessionInfo)
  141. session.Source = v2net.UDPDestination(v2net.IPAddress(addr.IP), v2net.Port(addr.Port))
  142. if this.option.ReceiveOriginalDest && noob > 0 {
  143. session.Destination = RetrieveOriginalDest(oobBytes[:noob])
  144. }
  145. this.queue.Enqueue(UDPPayload{
  146. payload: buffer,
  147. session: session,
  148. })
  149. }
  150. }
  151. func (this *UDPHub) Running() bool {
  152. return !this.cancel.Cancelled()
  153. }
  154. // Connection return the net.Conn underneath this hub.
  155. // Private: Visible for testing only
  156. func (this *UDPHub) Connection() net.Conn {
  157. return this.conn
  158. }
  159. func (this *UDPHub) Addr() net.Addr {
  160. return this.conn.LocalAddr()
  161. }