udp.go 7.0 KB

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