udp.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Copyright 2017 fatedier, fatedier@gmail.com
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package udp
  15. import (
  16. "encoding/base64"
  17. "net"
  18. "sync"
  19. "time"
  20. "github.com/fatedier/golib/errors"
  21. "github.com/fatedier/golib/pool"
  22. "github.com/fatedier/frp/pkg/msg"
  23. )
  24. func NewUDPPacket(buf []byte, laddr, raddr *net.UDPAddr) *msg.UDPPacket {
  25. return &msg.UDPPacket{
  26. Content: base64.StdEncoding.EncodeToString(buf),
  27. LocalAddr: laddr,
  28. RemoteAddr: raddr,
  29. }
  30. }
  31. func GetContent(m *msg.UDPPacket) (buf []byte, err error) {
  32. buf, err = base64.StdEncoding.DecodeString(m.Content)
  33. return
  34. }
  35. func ForwardUserConn(udpConn *net.UDPConn, readCh <-chan *msg.UDPPacket, sendCh chan<- *msg.UDPPacket, bufSize int) {
  36. // read
  37. go func() {
  38. for udpMsg := range readCh {
  39. buf, err := GetContent(udpMsg)
  40. if err != nil {
  41. continue
  42. }
  43. _, _ = udpConn.WriteToUDP(buf, udpMsg.RemoteAddr)
  44. }
  45. }()
  46. // write
  47. buf := pool.GetBuf(bufSize)
  48. defer pool.PutBuf(buf)
  49. for {
  50. n, remoteAddr, err := udpConn.ReadFromUDP(buf)
  51. if err != nil {
  52. return
  53. }
  54. // buf[:n] will be encoded to string, so the bytes can be reused
  55. udpMsg := NewUDPPacket(buf[:n], nil, remoteAddr)
  56. select {
  57. case sendCh <- udpMsg:
  58. default:
  59. }
  60. }
  61. }
  62. func Forwarder(dstAddr *net.UDPAddr, readCh <-chan *msg.UDPPacket, sendCh chan<- msg.Message, bufSize int) {
  63. var mu sync.RWMutex
  64. udpConnMap := make(map[string]*net.UDPConn)
  65. // read from dstAddr and write to sendCh
  66. writerFn := func(raddr *net.UDPAddr, udpConn *net.UDPConn) {
  67. addr := raddr.String()
  68. defer func() {
  69. mu.Lock()
  70. delete(udpConnMap, addr)
  71. mu.Unlock()
  72. udpConn.Close()
  73. }()
  74. buf := pool.GetBuf(bufSize)
  75. for {
  76. _ = udpConn.SetReadDeadline(time.Now().Add(30 * time.Second))
  77. n, _, err := udpConn.ReadFromUDP(buf)
  78. if err != nil {
  79. return
  80. }
  81. udpMsg := NewUDPPacket(buf[:n], nil, raddr)
  82. if err = errors.PanicToError(func() {
  83. select {
  84. case sendCh <- udpMsg:
  85. default:
  86. }
  87. }); err != nil {
  88. return
  89. }
  90. }
  91. }
  92. // read from readCh
  93. go func() {
  94. for udpMsg := range readCh {
  95. buf, err := GetContent(udpMsg)
  96. if err != nil {
  97. continue
  98. }
  99. mu.Lock()
  100. udpConn, ok := udpConnMap[udpMsg.RemoteAddr.String()]
  101. if !ok {
  102. udpConn, err = net.DialUDP("udp", nil, dstAddr)
  103. if err != nil {
  104. mu.Unlock()
  105. continue
  106. }
  107. udpConnMap[udpMsg.RemoteAddr.String()] = udpConn
  108. }
  109. mu.Unlock()
  110. _, err = udpConn.Write(buf)
  111. if err != nil {
  112. udpConn.Close()
  113. }
  114. if !ok {
  115. go writerFn(udpMsg.RemoteAddr, udpConn)
  116. }
  117. }
  118. }()
  119. }