proxy.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  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 server
  15. import (
  16. "context"
  17. "fmt"
  18. "io"
  19. "net"
  20. "strings"
  21. "sync"
  22. "time"
  23. "github.com/fatedier/frp/g"
  24. "github.com/fatedier/frp/models/config"
  25. "github.com/fatedier/frp/models/msg"
  26. "github.com/fatedier/frp/models/proto/udp"
  27. frpIo "github.com/fatedier/frp/utils/io"
  28. "github.com/fatedier/frp/utils/log"
  29. frpNet "github.com/fatedier/frp/utils/net"
  30. "github.com/fatedier/frp/utils/util"
  31. "github.com/fatedier/frp/utils/vhost"
  32. "github.com/fatedier/golib/errors"
  33. )
  34. type Proxy interface {
  35. Run() (remoteAddr string, err error)
  36. GetControl() *Control
  37. GetName() string
  38. GetConf() config.ProxyConf
  39. GetWorkConnFromPool() (workConn frpNet.Conn, err error)
  40. GetUsedPortsNum() int
  41. Close()
  42. log.Logger
  43. }
  44. type BaseProxy struct {
  45. name string
  46. ctl *Control
  47. listeners []frpNet.Listener
  48. usedPortsNum int
  49. mu sync.RWMutex
  50. log.Logger
  51. }
  52. func (pxy *BaseProxy) GetName() string {
  53. return pxy.name
  54. }
  55. func (pxy *BaseProxy) GetControl() *Control {
  56. return pxy.ctl
  57. }
  58. func (pxy *BaseProxy) GetUsedPortsNum() int {
  59. return pxy.usedPortsNum
  60. }
  61. func (pxy *BaseProxy) Close() {
  62. pxy.Info("proxy closing")
  63. for _, l := range pxy.listeners {
  64. l.Close()
  65. }
  66. }
  67. func (pxy *BaseProxy) GetWorkConnFromPool() (workConn frpNet.Conn, err error) {
  68. ctl := pxy.GetControl()
  69. // try all connections from the pool
  70. for i := 0; i < ctl.poolCount+1; i++ {
  71. if workConn, err = ctl.GetWorkConn(); err != nil {
  72. pxy.Warn("failed to get work connection: %v", err)
  73. return
  74. }
  75. pxy.Info("get a new work connection: [%s]", workConn.RemoteAddr().String())
  76. workConn.AddLogPrefix(pxy.GetName())
  77. err := msg.WriteMsg(workConn, &msg.StartWorkConn{
  78. ProxyName: pxy.GetName(),
  79. })
  80. if err != nil {
  81. workConn.Warn("failed to send message to work connection from pool: %v, times: %d", err, i)
  82. workConn.Close()
  83. } else {
  84. break
  85. }
  86. }
  87. if err != nil {
  88. pxy.Error("try to get work connection failed in the end")
  89. return
  90. }
  91. return
  92. }
  93. // startListenHandler start a goroutine handler for each listener.
  94. // p: p will just be passed to handler(Proxy, frpNet.Conn).
  95. // handler: each proxy type can set different handler function to deal with connections accepted from listeners.
  96. func (pxy *BaseProxy) startListenHandler(p Proxy, handler func(Proxy, frpNet.Conn)) {
  97. for _, listener := range pxy.listeners {
  98. go func(l frpNet.Listener) {
  99. for {
  100. // block
  101. // if listener is closed, err returned
  102. c, err := l.Accept()
  103. if err != nil {
  104. pxy.Info("listener is closed")
  105. return
  106. }
  107. pxy.Debug("get a user connection [%s]", c.RemoteAddr().String())
  108. go handler(p, c)
  109. }
  110. }(listener)
  111. }
  112. }
  113. func NewProxy(ctl *Control, pxyConf config.ProxyConf) (pxy Proxy, err error) {
  114. basePxy := BaseProxy{
  115. name: pxyConf.GetBaseInfo().ProxyName,
  116. ctl: ctl,
  117. listeners: make([]frpNet.Listener, 0),
  118. Logger: log.NewPrefixLogger(ctl.runId),
  119. }
  120. switch cfg := pxyConf.(type) {
  121. case *config.TcpProxyConf:
  122. basePxy.usedPortsNum = 1
  123. pxy = &TcpProxy{
  124. BaseProxy: basePxy,
  125. cfg: cfg,
  126. }
  127. case *config.HttpProxyConf:
  128. pxy = &HttpProxy{
  129. BaseProxy: basePxy,
  130. cfg: cfg,
  131. }
  132. case *config.HttpsProxyConf:
  133. pxy = &HttpsProxy{
  134. BaseProxy: basePxy,
  135. cfg: cfg,
  136. }
  137. case *config.UdpProxyConf:
  138. basePxy.usedPortsNum = 1
  139. pxy = &UdpProxy{
  140. BaseProxy: basePxy,
  141. cfg: cfg,
  142. }
  143. case *config.StcpProxyConf:
  144. pxy = &StcpProxy{
  145. BaseProxy: basePxy,
  146. cfg: cfg,
  147. }
  148. case *config.XtcpProxyConf:
  149. pxy = &XtcpProxy{
  150. BaseProxy: basePxy,
  151. cfg: cfg,
  152. }
  153. default:
  154. return pxy, fmt.Errorf("proxy type not support")
  155. }
  156. pxy.AddLogPrefix(pxy.GetName())
  157. return
  158. }
  159. type TcpProxy struct {
  160. BaseProxy
  161. cfg *config.TcpProxyConf
  162. realPort int
  163. }
  164. func (pxy *TcpProxy) Run() (remoteAddr string, err error) {
  165. pxy.realPort, err = pxy.ctl.svr.tcpPortManager.Acquire(pxy.name, pxy.cfg.RemotePort)
  166. if err != nil {
  167. return
  168. }
  169. defer func() {
  170. if err != nil {
  171. pxy.ctl.svr.tcpPortManager.Release(pxy.realPort)
  172. }
  173. }()
  174. remoteAddr = fmt.Sprintf(":%d", pxy.realPort)
  175. pxy.cfg.RemotePort = pxy.realPort
  176. listener, errRet := frpNet.ListenTcp(g.GlbServerCfg.ProxyBindAddr, pxy.realPort)
  177. if errRet != nil {
  178. err = errRet
  179. return
  180. }
  181. listener.AddLogPrefix(pxy.name)
  182. pxy.listeners = append(pxy.listeners, listener)
  183. pxy.Info("tcp proxy listen port [%d]", pxy.cfg.RemotePort)
  184. pxy.startListenHandler(pxy, HandleUserTcpConnection)
  185. return
  186. }
  187. func (pxy *TcpProxy) GetConf() config.ProxyConf {
  188. return pxy.cfg
  189. }
  190. func (pxy *TcpProxy) Close() {
  191. pxy.BaseProxy.Close()
  192. pxy.ctl.svr.tcpPortManager.Release(pxy.realPort)
  193. }
  194. type HttpProxy struct {
  195. BaseProxy
  196. cfg *config.HttpProxyConf
  197. closeFuncs []func()
  198. }
  199. func (pxy *HttpProxy) Run() (remoteAddr string, err error) {
  200. routeConfig := vhost.VhostRouteConfig{
  201. RewriteHost: pxy.cfg.HostHeaderRewrite,
  202. Username: pxy.cfg.HttpUser,
  203. Password: pxy.cfg.HttpPwd,
  204. CreateConnFn: pxy.GetRealConn,
  205. }
  206. locations := pxy.cfg.Locations
  207. if len(locations) == 0 {
  208. locations = []string{""}
  209. }
  210. addrs := make([]string, 0)
  211. for _, domain := range pxy.cfg.CustomDomains {
  212. routeConfig.Domain = domain
  213. for _, location := range locations {
  214. routeConfig.Location = location
  215. err = pxy.ctl.svr.httpReverseProxy.Register(routeConfig)
  216. if err != nil {
  217. return
  218. }
  219. tmpDomain := routeConfig.Domain
  220. tmpLocation := routeConfig.Location
  221. addrs = append(addrs, util.CanonicalAddr(tmpDomain, int(g.GlbServerCfg.VhostHttpPort)))
  222. pxy.closeFuncs = append(pxy.closeFuncs, func() {
  223. pxy.ctl.svr.httpReverseProxy.UnRegister(tmpDomain, tmpLocation)
  224. })
  225. pxy.Info("http proxy listen for host [%s] location [%s]", routeConfig.Domain, routeConfig.Location)
  226. }
  227. }
  228. if pxy.cfg.SubDomain != "" {
  229. routeConfig.Domain = pxy.cfg.SubDomain + "." + g.GlbServerCfg.SubDomainHost
  230. for _, location := range locations {
  231. routeConfig.Location = location
  232. err = pxy.ctl.svr.httpReverseProxy.Register(routeConfig)
  233. if err != nil {
  234. return
  235. }
  236. tmpDomain := routeConfig.Domain
  237. tmpLocation := routeConfig.Location
  238. addrs = append(addrs, util.CanonicalAddr(tmpDomain, g.GlbServerCfg.VhostHttpPort))
  239. pxy.closeFuncs = append(pxy.closeFuncs, func() {
  240. pxy.ctl.svr.httpReverseProxy.UnRegister(tmpDomain, tmpLocation)
  241. })
  242. pxy.Info("http proxy listen for host [%s] location [%s]", routeConfig.Domain, routeConfig.Location)
  243. }
  244. }
  245. remoteAddr = strings.Join(addrs, ",")
  246. return
  247. }
  248. func (pxy *HttpProxy) GetConf() config.ProxyConf {
  249. return pxy.cfg
  250. }
  251. func (pxy *HttpProxy) GetRealConn() (workConn frpNet.Conn, err error) {
  252. tmpConn, errRet := pxy.GetWorkConnFromPool()
  253. if errRet != nil {
  254. err = errRet
  255. return
  256. }
  257. var rwc io.ReadWriteCloser = tmpConn
  258. if pxy.cfg.UseEncryption {
  259. rwc, err = frpIo.WithEncryption(rwc, []byte(g.GlbServerCfg.Token))
  260. if err != nil {
  261. pxy.Error("create encryption stream error: %v", err)
  262. return
  263. }
  264. }
  265. if pxy.cfg.UseCompression {
  266. rwc = frpIo.WithCompression(rwc)
  267. }
  268. workConn = frpNet.WrapReadWriteCloserToConn(rwc, tmpConn)
  269. workConn = frpNet.WrapStatsConn(workConn, pxy.updateStatsAfterClosedConn)
  270. StatsOpenConnection(pxy.GetName())
  271. return
  272. }
  273. func (pxy *HttpProxy) updateStatsAfterClosedConn(totalRead, totalWrite int64) {
  274. name := pxy.GetName()
  275. StatsCloseConnection(name)
  276. StatsAddTrafficIn(name, totalWrite)
  277. StatsAddTrafficOut(name, totalRead)
  278. }
  279. func (pxy *HttpProxy) Close() {
  280. pxy.BaseProxy.Close()
  281. for _, closeFn := range pxy.closeFuncs {
  282. closeFn()
  283. }
  284. }
  285. type HttpsProxy struct {
  286. BaseProxy
  287. cfg *config.HttpsProxyConf
  288. }
  289. func (pxy *HttpsProxy) Run() (remoteAddr string, err error) {
  290. routeConfig := &vhost.VhostRouteConfig{}
  291. addrs := make([]string, 0)
  292. for _, domain := range pxy.cfg.CustomDomains {
  293. routeConfig.Domain = domain
  294. l, errRet := pxy.ctl.svr.VhostHttpsMuxer.Listen(routeConfig)
  295. if errRet != nil {
  296. err = errRet
  297. return
  298. }
  299. l.AddLogPrefix(pxy.name)
  300. pxy.Info("https proxy listen for host [%s]", routeConfig.Domain)
  301. pxy.listeners = append(pxy.listeners, l)
  302. addrs = append(addrs, util.CanonicalAddr(routeConfig.Domain, g.GlbServerCfg.VhostHttpsPort))
  303. }
  304. if pxy.cfg.SubDomain != "" {
  305. routeConfig.Domain = pxy.cfg.SubDomain + "." + g.GlbServerCfg.SubDomainHost
  306. l, errRet := pxy.ctl.svr.VhostHttpsMuxer.Listen(routeConfig)
  307. if errRet != nil {
  308. err = errRet
  309. return
  310. }
  311. l.AddLogPrefix(pxy.name)
  312. pxy.Info("https proxy listen for host [%s]", routeConfig.Domain)
  313. pxy.listeners = append(pxy.listeners, l)
  314. addrs = append(addrs, util.CanonicalAddr(routeConfig.Domain, int(g.GlbServerCfg.VhostHttpsPort)))
  315. }
  316. pxy.startListenHandler(pxy, HandleUserTcpConnection)
  317. remoteAddr = strings.Join(addrs, ",")
  318. return
  319. }
  320. func (pxy *HttpsProxy) GetConf() config.ProxyConf {
  321. return pxy.cfg
  322. }
  323. func (pxy *HttpsProxy) Close() {
  324. pxy.BaseProxy.Close()
  325. }
  326. type StcpProxy struct {
  327. BaseProxy
  328. cfg *config.StcpProxyConf
  329. }
  330. func (pxy *StcpProxy) Run() (remoteAddr string, err error) {
  331. listener, errRet := pxy.ctl.svr.visitorManager.Listen(pxy.GetName(), pxy.cfg.Sk)
  332. if errRet != nil {
  333. err = errRet
  334. return
  335. }
  336. listener.AddLogPrefix(pxy.name)
  337. pxy.listeners = append(pxy.listeners, listener)
  338. pxy.Info("stcp proxy custom listen success")
  339. pxy.startListenHandler(pxy, HandleUserTcpConnection)
  340. return
  341. }
  342. func (pxy *StcpProxy) GetConf() config.ProxyConf {
  343. return pxy.cfg
  344. }
  345. func (pxy *StcpProxy) Close() {
  346. pxy.BaseProxy.Close()
  347. pxy.ctl.svr.visitorManager.CloseListener(pxy.GetName())
  348. }
  349. type XtcpProxy struct {
  350. BaseProxy
  351. cfg *config.XtcpProxyConf
  352. closeCh chan struct{}
  353. }
  354. func (pxy *XtcpProxy) Run() (remoteAddr string, err error) {
  355. if pxy.ctl.svr.natHoleController == nil {
  356. pxy.Error("udp port for xtcp is not specified.")
  357. err = fmt.Errorf("xtcp is not supported in frps")
  358. return
  359. }
  360. sidCh := pxy.ctl.svr.natHoleController.ListenClient(pxy.GetName(), pxy.cfg.Sk)
  361. go func() {
  362. for {
  363. select {
  364. case <-pxy.closeCh:
  365. break
  366. case sid := <-sidCh:
  367. workConn, errRet := pxy.GetWorkConnFromPool()
  368. if errRet != nil {
  369. continue
  370. }
  371. m := &msg.NatHoleSid{
  372. Sid: sid,
  373. }
  374. errRet = msg.WriteMsg(workConn, m)
  375. if errRet != nil {
  376. pxy.Warn("write nat hole sid package error, %v", errRet)
  377. }
  378. }
  379. }
  380. }()
  381. return
  382. }
  383. func (pxy *XtcpProxy) GetConf() config.ProxyConf {
  384. return pxy.cfg
  385. }
  386. func (pxy *XtcpProxy) Close() {
  387. pxy.BaseProxy.Close()
  388. pxy.ctl.svr.natHoleController.CloseClient(pxy.GetName())
  389. errors.PanicToError(func() {
  390. close(pxy.closeCh)
  391. })
  392. }
  393. type UdpProxy struct {
  394. BaseProxy
  395. cfg *config.UdpProxyConf
  396. realPort int
  397. // udpConn is the listener of udp packages
  398. udpConn *net.UDPConn
  399. // there are always only one workConn at the same time
  400. // get another one if it closed
  401. workConn net.Conn
  402. // sendCh is used for sending packages to workConn
  403. sendCh chan *msg.UdpPacket
  404. // readCh is used for reading packages from workConn
  405. readCh chan *msg.UdpPacket
  406. // checkCloseCh is used for watching if workConn is closed
  407. checkCloseCh chan int
  408. isClosed bool
  409. }
  410. func (pxy *UdpProxy) Run() (remoteAddr string, err error) {
  411. pxy.realPort, err = pxy.ctl.svr.udpPortManager.Acquire(pxy.name, pxy.cfg.RemotePort)
  412. if err != nil {
  413. return
  414. }
  415. defer func() {
  416. if err != nil {
  417. pxy.ctl.svr.udpPortManager.Release(pxy.realPort)
  418. }
  419. }()
  420. remoteAddr = fmt.Sprintf(":%d", pxy.realPort)
  421. pxy.cfg.RemotePort = pxy.realPort
  422. addr, errRet := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", g.GlbServerCfg.ProxyBindAddr, pxy.realPort))
  423. if errRet != nil {
  424. err = errRet
  425. return
  426. }
  427. udpConn, errRet := net.ListenUDP("udp", addr)
  428. if errRet != nil {
  429. err = errRet
  430. pxy.Warn("listen udp port error: %v", err)
  431. return
  432. }
  433. pxy.Info("udp proxy listen port [%d]", pxy.cfg.RemotePort)
  434. pxy.udpConn = udpConn
  435. pxy.sendCh = make(chan *msg.UdpPacket, 1024)
  436. pxy.readCh = make(chan *msg.UdpPacket, 1024)
  437. pxy.checkCloseCh = make(chan int)
  438. // read message from workConn, if it returns any error, notify proxy to start a new workConn
  439. workConnReaderFn := func(conn net.Conn) {
  440. for {
  441. var (
  442. rawMsg msg.Message
  443. errRet error
  444. )
  445. pxy.Trace("loop waiting message from udp workConn")
  446. // client will send heartbeat in workConn for keeping alive
  447. conn.SetReadDeadline(time.Now().Add(time.Duration(60) * time.Second))
  448. if rawMsg, errRet = msg.ReadMsg(conn); errRet != nil {
  449. pxy.Warn("read from workConn for udp error: %v", errRet)
  450. conn.Close()
  451. // notify proxy to start a new work connection
  452. // ignore error here, it means the proxy is closed
  453. errors.PanicToError(func() {
  454. pxy.checkCloseCh <- 1
  455. })
  456. return
  457. }
  458. conn.SetReadDeadline(time.Time{})
  459. switch m := rawMsg.(type) {
  460. case *msg.Ping:
  461. pxy.Trace("udp work conn get ping message")
  462. continue
  463. case *msg.UdpPacket:
  464. if errRet := errors.PanicToError(func() {
  465. pxy.Trace("get udp message from workConn: %s", m.Content)
  466. pxy.readCh <- m
  467. StatsAddTrafficOut(pxy.GetName(), int64(len(m.Content)))
  468. }); errRet != nil {
  469. conn.Close()
  470. pxy.Info("reader goroutine for udp work connection closed")
  471. return
  472. }
  473. }
  474. }
  475. }
  476. // send message to workConn
  477. workConnSenderFn := func(conn net.Conn, ctx context.Context) {
  478. var errRet error
  479. for {
  480. select {
  481. case udpMsg, ok := <-pxy.sendCh:
  482. if !ok {
  483. pxy.Info("sender goroutine for udp work connection closed")
  484. return
  485. }
  486. if errRet = msg.WriteMsg(conn, udpMsg); errRet != nil {
  487. pxy.Info("sender goroutine for udp work connection closed: %v", errRet)
  488. conn.Close()
  489. return
  490. } else {
  491. pxy.Trace("send message to udp workConn: %s", udpMsg.Content)
  492. StatsAddTrafficIn(pxy.GetName(), int64(len(udpMsg.Content)))
  493. continue
  494. }
  495. case <-ctx.Done():
  496. pxy.Info("sender goroutine for udp work connection closed")
  497. return
  498. }
  499. }
  500. }
  501. go func() {
  502. // Sleep a while for waiting control send the NewProxyResp to client.
  503. time.Sleep(500 * time.Millisecond)
  504. for {
  505. workConn, err := pxy.GetWorkConnFromPool()
  506. if err != nil {
  507. time.Sleep(1 * time.Second)
  508. // check if proxy is closed
  509. select {
  510. case _, ok := <-pxy.checkCloseCh:
  511. if !ok {
  512. return
  513. }
  514. default:
  515. }
  516. continue
  517. }
  518. // close the old workConn and replac it with a new one
  519. if pxy.workConn != nil {
  520. pxy.workConn.Close()
  521. }
  522. pxy.workConn = workConn
  523. ctx, cancel := context.WithCancel(context.Background())
  524. go workConnReaderFn(workConn)
  525. go workConnSenderFn(workConn, ctx)
  526. _, ok := <-pxy.checkCloseCh
  527. cancel()
  528. if !ok {
  529. return
  530. }
  531. }
  532. }()
  533. // Read from user connections and send wrapped udp message to sendCh (forwarded by workConn).
  534. // Client will transfor udp message to local udp service and waiting for response for a while.
  535. // Response will be wrapped to be forwarded by work connection to server.
  536. // Close readCh and sendCh at the end.
  537. go func() {
  538. udp.ForwardUserConn(udpConn, pxy.readCh, pxy.sendCh)
  539. pxy.Close()
  540. }()
  541. return remoteAddr, nil
  542. }
  543. func (pxy *UdpProxy) GetConf() config.ProxyConf {
  544. return pxy.cfg
  545. }
  546. func (pxy *UdpProxy) Close() {
  547. pxy.mu.Lock()
  548. defer pxy.mu.Unlock()
  549. if !pxy.isClosed {
  550. pxy.isClosed = true
  551. pxy.BaseProxy.Close()
  552. if pxy.workConn != nil {
  553. pxy.workConn.Close()
  554. }
  555. pxy.udpConn.Close()
  556. // all channels only closed here
  557. close(pxy.checkCloseCh)
  558. close(pxy.readCh)
  559. close(pxy.sendCh)
  560. }
  561. pxy.ctl.svr.udpPortManager.Release(pxy.realPort)
  562. }
  563. // HandleUserTcpConnection is used for incoming tcp user connections.
  564. // It can be used for tcp, http, https type.
  565. func HandleUserTcpConnection(pxy Proxy, userConn frpNet.Conn) {
  566. defer userConn.Close()
  567. // try all connections from the pool
  568. workConn, err := pxy.GetWorkConnFromPool()
  569. if err != nil {
  570. return
  571. }
  572. defer workConn.Close()
  573. var local io.ReadWriteCloser = workConn
  574. cfg := pxy.GetConf().GetBaseInfo()
  575. if cfg.UseEncryption {
  576. local, err = frpIo.WithEncryption(local, []byte(g.GlbServerCfg.Token))
  577. if err != nil {
  578. pxy.Error("create encryption stream error: %v", err)
  579. return
  580. }
  581. }
  582. if cfg.UseCompression {
  583. local = frpIo.WithCompression(local)
  584. }
  585. pxy.Debug("join connections, workConn(l[%s] r[%s]) userConn(l[%s] r[%s])", workConn.LocalAddr().String(),
  586. workConn.RemoteAddr().String(), userConn.LocalAddr().String(), userConn.RemoteAddr().String())
  587. StatsOpenConnection(pxy.GetName())
  588. inCount, outCount := frpIo.Join(local, userConn)
  589. StatsCloseConnection(pxy.GetName())
  590. StatsAddTrafficIn(pxy.GetName(), inCount)
  591. StatsAddTrafficOut(pxy.GetName(), outCount)
  592. pxy.Debug("join connections closed")
  593. }