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(cap int) HubOption {
  16. return func(h *Hub) {
  17. h.capacity = cap
  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, options ...HubOption) (*Hub, error) {
  32. hub := &Hub{
  33. capacity: 256,
  34. recvOrigDest: false,
  35. }
  36. for _, opt := range options {
  37. opt(hub)
  38. }
  39. streamSettings := internet.StreamSettingsFromContext(ctx)
  40. if streamSettings != nil && streamSettings.SocketSettings != nil && streamSettings.SocketSettings.ReceiveOriginalDestAddress {
  41. hub.recvOrigDest = true
  42. }
  43. udpConn, err := internet.ListenSystemPacket(ctx, &net.UDPAddr{
  44. IP: address.IP(),
  45. Port: int(port),
  46. })
  47. if err != nil {
  48. return nil, err
  49. }
  50. newError("listening UDP on ", address, ":", port).WriteToLog()
  51. hub.conn = udpConn.(*net.UDPConn)
  52. hub.cache = make(chan *Payload, hub.capacity)
  53. go hub.start()
  54. return hub, nil
  55. }
  56. // Close implements net.Listener.
  57. func (h *Hub) Close() error {
  58. h.conn.Close()
  59. return nil
  60. }
  61. func (h *Hub) WriteTo(payload []byte, dest net.Destination) (int, error) {
  62. return h.conn.WriteToUDP(payload, &net.UDPAddr{
  63. IP: dest.Address.IP(),
  64. Port: int(dest.Port),
  65. })
  66. }
  67. func (h *Hub) start() {
  68. c := h.cache
  69. defer close(c)
  70. oobBytes := make([]byte, 256)
  71. for {
  72. buffer := buf.New()
  73. var noob int
  74. var addr *net.UDPAddr
  75. err := buffer.Reset(func(b []byte) (int, error) {
  76. n, nb, _, a, e := ReadUDPMsg(h.conn, b, oobBytes)
  77. noob = nb
  78. addr = a
  79. return n, e
  80. })
  81. if err != nil {
  82. newError("failed to read UDP msg").Base(err).WriteToLog()
  83. buffer.Release()
  84. break
  85. }
  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. }