hub.go 3.9 KB

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