udp.go 6.3 KB

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