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/frp/models/msg"
  21. "github.com/fatedier/frp/utils/errors"
  22. "github.com/fatedier/frp/utils/pool"
  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) {
  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(1500)
  48. defer pool.PutBuf(buf)
  49. for {
  50. n, remoteAddr, err := udpConn.ReadFromUDP(buf)
  51. if err != nil {
  52. udpConn.Close()
  53. return
  54. }
  55. // buf[:n] will be encoded to string, so the bytes can be reused
  56. udpMsg := NewUdpPacket(buf[:n], nil, remoteAddr)
  57. select {
  58. case sendCh <- udpMsg:
  59. default:
  60. }
  61. }
  62. return
  63. }
  64. func Forwarder(dstAddr *net.UDPAddr, readCh <-chan *msg.UdpPacket, sendCh chan<- msg.Message) {
  65. var (
  66. mu sync.RWMutex
  67. )
  68. udpConnMap := make(map[string]*net.UDPConn)
  69. // read from dstAddr and write to sendCh
  70. writerFn := func(raddr *net.UDPAddr, udpConn *net.UDPConn) {
  71. addr := raddr.String()
  72. defer func() {
  73. mu.Lock()
  74. delete(udpConnMap, addr)
  75. mu.Unlock()
  76. }()
  77. buf := pool.GetBuf(1500)
  78. for {
  79. udpConn.SetReadDeadline(time.Now().Add(30 * time.Second))
  80. n, _, err := udpConn.ReadFromUDP(buf)
  81. if err != nil {
  82. return
  83. }
  84. udpMsg := NewUdpPacket(buf[:n], nil, raddr)
  85. if err = errors.PanicToError(func() {
  86. select {
  87. case sendCh <- udpMsg:
  88. default:
  89. }
  90. }); err != nil {
  91. return
  92. }
  93. }
  94. }
  95. // read from readCh
  96. go func() {
  97. for udpMsg := range readCh {
  98. buf, err := GetContent(udpMsg)
  99. if err != nil {
  100. continue
  101. }
  102. mu.Lock()
  103. udpConn, ok := udpConnMap[udpMsg.RemoteAddr.String()]
  104. if !ok {
  105. udpConn, err = net.DialUDP("udp", nil, dstAddr)
  106. if err != nil {
  107. continue
  108. }
  109. udpConnMap[udpMsg.RemoteAddr.String()] = udpConn
  110. }
  111. mu.Unlock()
  112. _, err = udpConn.Write(buf)
  113. if err != nil {
  114. udpConn.Close()
  115. }
  116. if !ok {
  117. go writerFn(udpMsg.RemoteAddr, udpConn)
  118. }
  119. }
  120. }()
  121. }