hub.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package udp
  2. import (
  3. "context"
  4. "v2ray.com/core/common/buf"
  5. "v2ray.com/core/common/net"
  6. "v2ray.com/core/transport/internet"
  7. )
  8. // Payload represents a single UDP payload.
  9. type Payload struct {
  10. Content *buf.Buffer
  11. Source net.Destination
  12. OriginalDestination net.Destination
  13. }
  14. type HubOption func(h *Hub)
  15. func HubCapacity(capacity int) HubOption {
  16. return func(h *Hub) {
  17. h.capacity = capacity
  18. }
  19. }
  20. func HubReceiveOriginalDestination(r bool) HubOption {
  21. return func(h *Hub) {
  22. h.recvOrigDest = r
  23. }
  24. }
  25. type Hub struct {
  26. conn *net.UDPConn
  27. cache chan *Payload
  28. capacity int
  29. recvOrigDest bool
  30. }
  31. func ListenUDP(ctx context.Context, address net.Address, port net.Port, streamSettings *internet.MemoryStreamConfig, options ...HubOption) (*Hub, error) {
  32. hub := &Hub{
  33. capacity: 256,
  34. recvOrigDest: false,
  35. }
  36. for _, opt := range options {
  37. opt(hub)
  38. }
  39. var sockopt *internet.SocketConfig
  40. if streamSettings != nil {
  41. sockopt = streamSettings.SocketSettings
  42. }
  43. if sockopt != nil && sockopt.ReceiveOriginalDestAddress {
  44. hub.recvOrigDest = true
  45. }
  46. udpConn, err := internet.ListenSystemPacket(ctx, &net.UDPAddr{
  47. IP: address.IP(),
  48. Port: int(port),
  49. }, sockopt)
  50. if err != nil {
  51. return nil, err
  52. }
  53. newError("listening UDP on ", address, ":", port).WriteToLog()
  54. hub.conn = udpConn.(*net.UDPConn)
  55. hub.cache = make(chan *Payload, hub.capacity)
  56. go hub.start()
  57. return hub, nil
  58. }
  59. // Close implements net.Listener.
  60. func (h *Hub) Close() error {
  61. h.conn.Close()
  62. return nil
  63. }
  64. func (h *Hub) WriteTo(payload []byte, dest net.Destination) (int, error) {
  65. return h.conn.WriteToUDP(payload, &net.UDPAddr{
  66. IP: dest.Address.IP(),
  67. Port: int(dest.Port),
  68. })
  69. }
  70. func (h *Hub) start() {
  71. c := h.cache
  72. defer close(c)
  73. oobBytes := make([]byte, 256)
  74. for {
  75. buffer := buf.New()
  76. var noob int
  77. var addr *net.UDPAddr
  78. rawBytes := buffer.Extend(buf.Size)
  79. n, noob, _, addr, err := ReadUDPMsg(h.conn, rawBytes, oobBytes)
  80. if err != nil {
  81. newError("failed to read UDP msg").Base(err).WriteToLog()
  82. buffer.Release()
  83. break
  84. }
  85. buffer.Resize(0, int32(n))
  86. if buffer.IsEmpty() {
  87. buffer.Release()
  88. continue
  89. }
  90. payload := &Payload{
  91. Content: buffer,
  92. Source: net.UDPDestination(net.IPAddress(addr.IP), net.Port(addr.Port)),
  93. }
  94. if h.recvOrigDest && noob > 0 {
  95. payload.OriginalDestination = RetrieveOriginalDest(oobBytes[:noob])
  96. if payload.OriginalDestination.IsValid() {
  97. newError("UDP original destination: ", payload.OriginalDestination).AtDebug().WriteToLog()
  98. } else {
  99. newError("failed to read UDP original destination").WriteToLog()
  100. }
  101. }
  102. select {
  103. case c <- payload:
  104. default:
  105. buffer.Release()
  106. payload.Content = nil
  107. }
  108. }
  109. }
  110. // Addr implements net.Listener.
  111. func (h *Hub) Addr() net.Addr {
  112. return h.conn.LocalAddr()
  113. }
  114. func (h *Hub) Receive() <-chan *Payload {
  115. return h.cache
  116. }