udp.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // Copyright 2019 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 proxy
  15. import (
  16. "context"
  17. "fmt"
  18. "io"
  19. "net"
  20. "strconv"
  21. "time"
  22. "github.com/fatedier/frp/pkg/config"
  23. "github.com/fatedier/frp/pkg/msg"
  24. "github.com/fatedier/frp/pkg/proto/udp"
  25. frpNet "github.com/fatedier/frp/pkg/util/net"
  26. "github.com/fatedier/frp/server/metrics"
  27. "github.com/fatedier/golib/errors"
  28. frpIo "github.com/fatedier/golib/io"
  29. )
  30. type UDPProxy struct {
  31. *BaseProxy
  32. cfg *config.UDPProxyConf
  33. realPort int
  34. // udpConn is the listener of udp packages
  35. udpConn *net.UDPConn
  36. // there are always only one workConn at the same time
  37. // get another one if it closed
  38. workConn net.Conn
  39. // sendCh is used for sending packages to workConn
  40. sendCh chan *msg.UDPPacket
  41. // readCh is used for reading packages from workConn
  42. readCh chan *msg.UDPPacket
  43. // checkCloseCh is used for watching if workConn is closed
  44. checkCloseCh chan int
  45. isClosed bool
  46. }
  47. func (pxy *UDPProxy) Run() (remoteAddr string, err error) {
  48. xl := pxy.xl
  49. pxy.realPort, err = pxy.rc.UDPPortManager.Acquire(pxy.name, pxy.cfg.RemotePort)
  50. if err != nil {
  51. return "", fmt.Errorf("acquire port %d error: %v", pxy.cfg.RemotePort, err)
  52. }
  53. defer func() {
  54. if err != nil {
  55. pxy.rc.UDPPortManager.Release(pxy.realPort)
  56. }
  57. }()
  58. remoteAddr = fmt.Sprintf(":%d", pxy.realPort)
  59. pxy.cfg.RemotePort = pxy.realPort
  60. addr, errRet := net.ResolveUDPAddr("udp", net.JoinHostPort(pxy.serverCfg.ProxyBindAddr, strconv.Itoa(pxy.realPort)))
  61. if errRet != nil {
  62. err = errRet
  63. return
  64. }
  65. udpConn, errRet := net.ListenUDP("udp", addr)
  66. if errRet != nil {
  67. err = errRet
  68. xl.Warn("listen udp port error: %v", err)
  69. return
  70. }
  71. xl.Info("udp proxy listen port [%d]", pxy.cfg.RemotePort)
  72. pxy.udpConn = udpConn
  73. pxy.sendCh = make(chan *msg.UDPPacket, 1024)
  74. pxy.readCh = make(chan *msg.UDPPacket, 1024)
  75. pxy.checkCloseCh = make(chan int)
  76. // read message from workConn, if it returns any error, notify proxy to start a new workConn
  77. workConnReaderFn := func(conn net.Conn) {
  78. for {
  79. var (
  80. rawMsg msg.Message
  81. errRet error
  82. )
  83. xl.Trace("loop waiting message from udp workConn")
  84. // client will send heartbeat in workConn for keeping alive
  85. conn.SetReadDeadline(time.Now().Add(time.Duration(60) * time.Second))
  86. if rawMsg, errRet = msg.ReadMsg(conn); errRet != nil {
  87. xl.Warn("read from workConn for udp error: %v", errRet)
  88. conn.Close()
  89. // notify proxy to start a new work connection
  90. // ignore error here, it means the proxy is closed
  91. errors.PanicToError(func() {
  92. pxy.checkCloseCh <- 1
  93. })
  94. return
  95. }
  96. conn.SetReadDeadline(time.Time{})
  97. switch m := rawMsg.(type) {
  98. case *msg.Ping:
  99. xl.Trace("udp work conn get ping message")
  100. continue
  101. case *msg.UDPPacket:
  102. if errRet := errors.PanicToError(func() {
  103. xl.Trace("get udp message from workConn: %s", m.Content)
  104. pxy.readCh <- m
  105. metrics.Server.AddTrafficOut(
  106. pxy.GetName(),
  107. pxy.GetConf().GetBaseInfo().ProxyType,
  108. int64(len(m.Content)),
  109. )
  110. }); errRet != nil {
  111. conn.Close()
  112. xl.Info("reader goroutine for udp work connection closed")
  113. return
  114. }
  115. }
  116. }
  117. }
  118. // send message to workConn
  119. workConnSenderFn := func(conn net.Conn, ctx context.Context) {
  120. var errRet error
  121. for {
  122. select {
  123. case udpMsg, ok := <-pxy.sendCh:
  124. if !ok {
  125. xl.Info("sender goroutine for udp work connection closed")
  126. return
  127. }
  128. if errRet = msg.WriteMsg(conn, udpMsg); errRet != nil {
  129. xl.Info("sender goroutine for udp work connection closed: %v", errRet)
  130. conn.Close()
  131. return
  132. }
  133. xl.Trace("send message to udp workConn: %s", udpMsg.Content)
  134. metrics.Server.AddTrafficIn(
  135. pxy.GetName(),
  136. pxy.GetConf().GetBaseInfo().ProxyType,
  137. int64(len(udpMsg.Content)),
  138. )
  139. continue
  140. case <-ctx.Done():
  141. xl.Info("sender goroutine for udp work connection closed")
  142. return
  143. }
  144. }
  145. }
  146. go func() {
  147. // Sleep a while for waiting control send the NewProxyResp to client.
  148. time.Sleep(500 * time.Millisecond)
  149. for {
  150. workConn, err := pxy.GetWorkConnFromPool(nil, nil)
  151. if err != nil {
  152. time.Sleep(1 * time.Second)
  153. // check if proxy is closed
  154. select {
  155. case _, ok := <-pxy.checkCloseCh:
  156. if !ok {
  157. return
  158. }
  159. default:
  160. }
  161. continue
  162. }
  163. // close the old workConn and replace it with a new one
  164. if pxy.workConn != nil {
  165. pxy.workConn.Close()
  166. }
  167. var rwc io.ReadWriteCloser = workConn
  168. if pxy.cfg.UseEncryption {
  169. rwc, err = frpIo.WithEncryption(rwc, []byte(pxy.serverCfg.Token))
  170. if err != nil {
  171. xl.Error("create encryption stream error: %v", err)
  172. workConn.Close()
  173. continue
  174. }
  175. }
  176. if pxy.cfg.UseCompression {
  177. rwc = frpIo.WithCompression(rwc)
  178. }
  179. pxy.workConn = frpNet.WrapReadWriteCloserToConn(rwc, workConn)
  180. ctx, cancel := context.WithCancel(context.Background())
  181. go workConnReaderFn(pxy.workConn)
  182. go workConnSenderFn(pxy.workConn, ctx)
  183. _, ok := <-pxy.checkCloseCh
  184. cancel()
  185. if !ok {
  186. return
  187. }
  188. }
  189. }()
  190. // Read from user connections and send wrapped udp message to sendCh (forwarded by workConn).
  191. // Client will transfor udp message to local udp service and waiting for response for a while.
  192. // Response will be wrapped to be forwarded by work connection to server.
  193. // Close readCh and sendCh at the end.
  194. go func() {
  195. udp.ForwardUserConn(udpConn, pxy.readCh, pxy.sendCh, int(pxy.serverCfg.UDPPacketSize))
  196. pxy.Close()
  197. }()
  198. return remoteAddr, nil
  199. }
  200. func (pxy *UDPProxy) GetConf() config.ProxyConf {
  201. return pxy.cfg
  202. }
  203. func (pxy *UDPProxy) Close() {
  204. pxy.mu.Lock()
  205. defer pxy.mu.Unlock()
  206. if !pxy.isClosed {
  207. pxy.isClosed = true
  208. pxy.BaseProxy.Close()
  209. if pxy.workConn != nil {
  210. pxy.workConn.Close()
  211. }
  212. pxy.udpConn.Close()
  213. // all channels only closed here
  214. close(pxy.checkCloseCh)
  215. close(pxy.readCh)
  216. close(pxy.sendCh)
  217. }
  218. pxy.rc.UDPPortManager.Release(pxy.realPort)
  219. }