hub.go 3.7 KB

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