hub.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package udp
  2. import (
  3. "net"
  4. "sync"
  5. "v2ray.com/core/common/buf"
  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 *buf.Buffer
  15. session *proxy.SessionInfo
  16. }
  17. type UDPPayloadHandler func(*buf.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 (v *UDPPayloadQueue) Enqueue(payload UDPPayload) {
  34. size := len(v.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 v.queue[idx%size] <- payload:
  42. return
  43. default:
  44. idx++
  45. }
  46. }
  47. }
  48. func (v *UDPPayloadQueue) Dequeue(queue <-chan UDPPayload) {
  49. for payload := range queue {
  50. v.callback(payload.payload, payload.session)
  51. }
  52. }
  53. func (v *UDPPayloadQueue) Close() {
  54. for _, queue := range v.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. cancel *signal.CancelSignal
  67. queue *UDPPayloadQueue
  68. option ListenOption
  69. }
  70. func ListenUDP(address v2net.Address, port v2net.Port, option ListenOption) (*UDPHub, error) {
  71. if option.Concurrency < 1 {
  72. option.Concurrency = 1
  73. }
  74. udpConn, err := net.ListenUDP("udp", &net.UDPAddr{
  75. IP: address.IP(),
  76. Port: int(port),
  77. })
  78. if err != nil {
  79. return nil, err
  80. }
  81. if option.ReceiveOriginalDest {
  82. fd, err := internal.GetSysFd(udpConn)
  83. if err != nil {
  84. log.Warning("UDP|Listener: Failed to get fd: ", err)
  85. return nil, err
  86. }
  87. err = SetOriginalDestOptions(fd)
  88. if err != nil {
  89. log.Warning("UDP|Listener: Failed to set socket options: ", err)
  90. return nil, err
  91. }
  92. }
  93. hub := &UDPHub{
  94. conn: udpConn,
  95. queue: NewUDPPayloadQueue(option),
  96. option: option,
  97. cancel: signal.NewCloseSignal(),
  98. }
  99. go hub.start()
  100. return hub, nil
  101. }
  102. func (v *UDPHub) Close() {
  103. v.Lock()
  104. defer v.Unlock()
  105. v.cancel.Cancel()
  106. v.conn.Close()
  107. v.cancel.WaitForDone()
  108. v.queue.Close()
  109. }
  110. func (v *UDPHub) WriteTo(payload []byte, dest v2net.Destination) (int, error) {
  111. return v.conn.WriteToUDP(payload, &net.UDPAddr{
  112. IP: dest.Address.IP(),
  113. Port: int(dest.Port),
  114. })
  115. }
  116. func (v *UDPHub) start() {
  117. v.cancel.WaitThread()
  118. defer v.cancel.FinishThread()
  119. oobBytes := make([]byte, 256)
  120. for v.Running() {
  121. buffer := buf.NewSmallBuffer()
  122. var noob int
  123. var addr *net.UDPAddr
  124. var err error
  125. buffer.AppendFunc(func(b []byte) int {
  126. n, nb, _, a, e := ReadUDPMsg(v.conn, b, oobBytes)
  127. noob = nb
  128. addr = a
  129. err = e
  130. return n
  131. })
  132. if err != nil {
  133. log.Info("UDP|Hub: Failed to read UDP msg: ", err)
  134. buffer.Release()
  135. continue
  136. }
  137. session := new(proxy.SessionInfo)
  138. session.Source = v2net.UDPDestination(v2net.IPAddress(addr.IP), v2net.Port(addr.Port))
  139. if v.option.ReceiveOriginalDest && noob > 0 {
  140. session.Destination = RetrieveOriginalDest(oobBytes[:noob])
  141. }
  142. v.queue.Enqueue(UDPPayload{
  143. payload: buffer,
  144. session: session,
  145. })
  146. }
  147. }
  148. func (v *UDPHub) Running() bool {
  149. return !v.cancel.Cancelled()
  150. }
  151. // Connection return the net.Conn underneath v hub.
  152. // Private: Visible for testing only
  153. func (v *UDPHub) Connection() net.Conn {
  154. return v.conn
  155. }
  156. func (v *UDPHub) Addr() net.Addr {
  157. return v.conn.LocalAddr()
  158. }