udp.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright 2023 The frp Authors
  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 proxy
  15. import (
  16. "io"
  17. "net"
  18. "strconv"
  19. "time"
  20. "github.com/fatedier/golib/errors"
  21. frpIo "github.com/fatedier/golib/io"
  22. "github.com/fatedier/frp/pkg/config"
  23. "github.com/fatedier/frp/pkg/msg"
  24. "github.com/fatedier/frp/pkg/proto/udp"
  25. "github.com/fatedier/frp/pkg/util/limit"
  26. frpNet "github.com/fatedier/frp/pkg/util/net"
  27. )
  28. // UDP
  29. type UDPProxy struct {
  30. *BaseProxy
  31. cfg *config.UDPProxyConf
  32. localAddr *net.UDPAddr
  33. readCh chan *msg.UDPPacket
  34. // include msg.UDPPacket and msg.Ping
  35. sendCh chan msg.Message
  36. workConn net.Conn
  37. }
  38. func (pxy *UDPProxy) Run() (err error) {
  39. pxy.localAddr, err = net.ResolveUDPAddr("udp", net.JoinHostPort(pxy.cfg.LocalIP, strconv.Itoa(pxy.cfg.LocalPort)))
  40. if err != nil {
  41. return
  42. }
  43. return
  44. }
  45. func (pxy *UDPProxy) Close() {
  46. pxy.mu.Lock()
  47. defer pxy.mu.Unlock()
  48. if !pxy.closed {
  49. pxy.closed = true
  50. if pxy.workConn != nil {
  51. pxy.workConn.Close()
  52. }
  53. if pxy.readCh != nil {
  54. close(pxy.readCh)
  55. }
  56. if pxy.sendCh != nil {
  57. close(pxy.sendCh)
  58. }
  59. }
  60. }
  61. func (pxy *UDPProxy) InWorkConn(conn net.Conn, m *msg.StartWorkConn) {
  62. xl := pxy.xl
  63. xl.Info("incoming a new work connection for udp proxy, %s", conn.RemoteAddr().String())
  64. // close resources releated with old workConn
  65. pxy.Close()
  66. var rwc io.ReadWriteCloser = conn
  67. var err error
  68. if pxy.limiter != nil {
  69. rwc = frpIo.WrapReadWriteCloser(limit.NewReader(conn, pxy.limiter), limit.NewWriter(conn, pxy.limiter), func() error {
  70. return conn.Close()
  71. })
  72. }
  73. if pxy.cfg.UseEncryption {
  74. rwc, err = frpIo.WithEncryption(rwc, []byte(pxy.clientCfg.Token))
  75. if err != nil {
  76. conn.Close()
  77. xl.Error("create encryption stream error: %v", err)
  78. return
  79. }
  80. }
  81. if pxy.cfg.UseCompression {
  82. rwc = frpIo.WithCompression(rwc)
  83. }
  84. conn = frpNet.WrapReadWriteCloserToConn(rwc, conn)
  85. pxy.mu.Lock()
  86. pxy.workConn = conn
  87. pxy.readCh = make(chan *msg.UDPPacket, 1024)
  88. pxy.sendCh = make(chan msg.Message, 1024)
  89. pxy.closed = false
  90. pxy.mu.Unlock()
  91. workConnReaderFn := func(conn net.Conn, readCh chan *msg.UDPPacket) {
  92. for {
  93. var udpMsg msg.UDPPacket
  94. if errRet := msg.ReadMsgInto(conn, &udpMsg); errRet != nil {
  95. xl.Warn("read from workConn for udp error: %v", errRet)
  96. return
  97. }
  98. if errRet := errors.PanicToError(func() {
  99. xl.Trace("get udp package from workConn: %s", udpMsg.Content)
  100. readCh <- &udpMsg
  101. }); errRet != nil {
  102. xl.Info("reader goroutine for udp work connection closed: %v", errRet)
  103. return
  104. }
  105. }
  106. }
  107. workConnSenderFn := func(conn net.Conn, sendCh chan msg.Message) {
  108. defer func() {
  109. xl.Info("writer goroutine for udp work connection closed")
  110. }()
  111. var errRet error
  112. for rawMsg := range sendCh {
  113. switch m := rawMsg.(type) {
  114. case *msg.UDPPacket:
  115. xl.Trace("send udp package to workConn: %s", m.Content)
  116. case *msg.Ping:
  117. xl.Trace("send ping message to udp workConn")
  118. }
  119. if errRet = msg.WriteMsg(conn, rawMsg); errRet != nil {
  120. xl.Error("udp work write error: %v", errRet)
  121. return
  122. }
  123. }
  124. }
  125. heartbeatFn := func(sendCh chan msg.Message) {
  126. var errRet error
  127. for {
  128. time.Sleep(time.Duration(30) * time.Second)
  129. if errRet = errors.PanicToError(func() {
  130. sendCh <- &msg.Ping{}
  131. }); errRet != nil {
  132. xl.Trace("heartbeat goroutine for udp work connection closed")
  133. break
  134. }
  135. }
  136. }
  137. go workConnSenderFn(pxy.workConn, pxy.sendCh)
  138. go workConnReaderFn(pxy.workConn, pxy.readCh)
  139. go heartbeatFn(pxy.sendCh)
  140. udp.Forwarder(pxy.localAddr, pxy.readCh, pxy.sendCh, int(pxy.clientCfg.UDPPacketSize))
  141. }