proxy.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  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. "github.com/fatedier/frp/utils/log"
  28. frpNet "github.com/fatedier/frp/utils/net"
  29. "github.com/fatedier/frp/utils/util"
  30. "github.com/fatedier/frp/utils/vhost"
  31. "github.com/fatedier/golib/errors"
  32. frpIo "github.com/fatedier/golib/io"
  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. Headers: pxy.cfg.Headers,
  203. Username: pxy.cfg.HttpUser,
  204. Password: pxy.cfg.HttpPwd,
  205. CreateConnFn: pxy.GetRealConn,
  206. }
  207. locations := pxy.cfg.Locations
  208. if len(locations) == 0 {
  209. locations = []string{""}
  210. }
  211. addrs := make([]string, 0)
  212. for _, domain := range pxy.cfg.CustomDomains {
  213. routeConfig.Domain = domain
  214. for _, location := range locations {
  215. routeConfig.Location = location
  216. err = pxy.ctl.svr.httpReverseProxy.Register(routeConfig)
  217. if err != nil {
  218. return
  219. }
  220. tmpDomain := routeConfig.Domain
  221. tmpLocation := routeConfig.Location
  222. addrs = append(addrs, util.CanonicalAddr(tmpDomain, int(g.GlbServerCfg.VhostHttpPort)))
  223. pxy.closeFuncs = append(pxy.closeFuncs, func() {
  224. pxy.ctl.svr.httpReverseProxy.UnRegister(tmpDomain, tmpLocation)
  225. })
  226. pxy.Info("http proxy listen for host [%s] location [%s]", routeConfig.Domain, routeConfig.Location)
  227. }
  228. }
  229. if pxy.cfg.SubDomain != "" {
  230. routeConfig.Domain = pxy.cfg.SubDomain + "." + g.GlbServerCfg.SubDomainHost
  231. for _, location := range locations {
  232. routeConfig.Location = location
  233. err = pxy.ctl.svr.httpReverseProxy.Register(routeConfig)
  234. if err != nil {
  235. return
  236. }
  237. tmpDomain := routeConfig.Domain
  238. tmpLocation := routeConfig.Location
  239. addrs = append(addrs, util.CanonicalAddr(tmpDomain, g.GlbServerCfg.VhostHttpPort))
  240. pxy.closeFuncs = append(pxy.closeFuncs, func() {
  241. pxy.ctl.svr.httpReverseProxy.UnRegister(tmpDomain, tmpLocation)
  242. })
  243. pxy.Info("http proxy listen for host [%s] location [%s]", routeConfig.Domain, routeConfig.Location)
  244. }
  245. }
  246. remoteAddr = strings.Join(addrs, ",")
  247. return
  248. }
  249. func (pxy *HttpProxy) GetConf() config.ProxyConf {
  250. return pxy.cfg
  251. }
  252. func (pxy *HttpProxy) GetRealConn() (workConn frpNet.Conn, err error) {
  253. tmpConn, errRet := pxy.GetWorkConnFromPool()
  254. if errRet != nil {
  255. err = errRet
  256. return
  257. }
  258. var rwc io.ReadWriteCloser = tmpConn
  259. if pxy.cfg.UseEncryption {
  260. rwc, err = frpIo.WithEncryption(rwc, []byte(g.GlbServerCfg.Token))
  261. if err != nil {
  262. pxy.Error("create encryption stream error: %v", err)
  263. return
  264. }
  265. }
  266. if pxy.cfg.UseCompression {
  267. rwc = frpIo.WithCompression(rwc)
  268. }
  269. workConn = frpNet.WrapReadWriteCloserToConn(rwc, tmpConn)
  270. workConn = frpNet.WrapStatsConn(workConn, pxy.updateStatsAfterClosedConn)
  271. StatsOpenConnection(pxy.GetName())
  272. return
  273. }
  274. func (pxy *HttpProxy) updateStatsAfterClosedConn(totalRead, totalWrite int64) {
  275. name := pxy.GetName()
  276. StatsCloseConnection(name)
  277. StatsAddTrafficIn(name, totalWrite)
  278. StatsAddTrafficOut(name, totalRead)
  279. }
  280. func (pxy *HttpProxy) Close() {
  281. pxy.BaseProxy.Close()
  282. for _, closeFn := range pxy.closeFuncs {
  283. closeFn()
  284. }
  285. }
  286. type HttpsProxy struct {
  287. BaseProxy
  288. cfg *config.HttpsProxyConf
  289. }
  290. func (pxy *HttpsProxy) Run() (remoteAddr string, err error) {
  291. routeConfig := &vhost.VhostRouteConfig{}
  292. addrs := make([]string, 0)
  293. for _, domain := range pxy.cfg.CustomDomains {
  294. routeConfig.Domain = domain
  295. l, errRet := pxy.ctl.svr.VhostHttpsMuxer.Listen(routeConfig)
  296. if errRet != nil {
  297. err = errRet
  298. return
  299. }
  300. l.AddLogPrefix(pxy.name)
  301. pxy.Info("https proxy listen for host [%s]", routeConfig.Domain)
  302. pxy.listeners = append(pxy.listeners, l)
  303. addrs = append(addrs, util.CanonicalAddr(routeConfig.Domain, g.GlbServerCfg.VhostHttpsPort))
  304. }
  305. if pxy.cfg.SubDomain != "" {
  306. routeConfig.Domain = pxy.cfg.SubDomain + "." + g.GlbServerCfg.SubDomainHost
  307. l, errRet := pxy.ctl.svr.VhostHttpsMuxer.Listen(routeConfig)
  308. if errRet != nil {
  309. err = errRet
  310. return
  311. }
  312. l.AddLogPrefix(pxy.name)
  313. pxy.Info("https proxy listen for host [%s]", routeConfig.Domain)
  314. pxy.listeners = append(pxy.listeners, l)
  315. addrs = append(addrs, util.CanonicalAddr(routeConfig.Domain, int(g.GlbServerCfg.VhostHttpsPort)))
  316. }
  317. pxy.startListenHandler(pxy, HandleUserTcpConnection)
  318. remoteAddr = strings.Join(addrs, ",")
  319. return
  320. }
  321. func (pxy *HttpsProxy) GetConf() config.ProxyConf {
  322. return pxy.cfg
  323. }
  324. func (pxy *HttpsProxy) Close() {
  325. pxy.BaseProxy.Close()
  326. }
  327. type StcpProxy struct {
  328. BaseProxy
  329. cfg *config.StcpProxyConf
  330. }
  331. func (pxy *StcpProxy) Run() (remoteAddr string, err error) {
  332. listener, errRet := pxy.ctl.svr.visitorManager.Listen(pxy.GetName(), pxy.cfg.Sk)
  333. if errRet != nil {
  334. err = errRet
  335. return
  336. }
  337. listener.AddLogPrefix(pxy.name)
  338. pxy.listeners = append(pxy.listeners, listener)
  339. pxy.Info("stcp proxy custom listen success")
  340. pxy.startListenHandler(pxy, HandleUserTcpConnection)
  341. return
  342. }
  343. func (pxy *StcpProxy) GetConf() config.ProxyConf {
  344. return pxy.cfg
  345. }
  346. func (pxy *StcpProxy) Close() {
  347. pxy.BaseProxy.Close()
  348. pxy.ctl.svr.visitorManager.CloseListener(pxy.GetName())
  349. }
  350. type XtcpProxy struct {
  351. BaseProxy
  352. cfg *config.XtcpProxyConf
  353. closeCh chan struct{}
  354. }
  355. func (pxy *XtcpProxy) Run() (remoteAddr string, err error) {
  356. if pxy.ctl.svr.natHoleController == nil {
  357. pxy.Error("udp port for xtcp is not specified.")
  358. err = fmt.Errorf("xtcp is not supported in frps")
  359. return
  360. }
  361. sidCh := pxy.ctl.svr.natHoleController.ListenClient(pxy.GetName(), pxy.cfg.Sk)
  362. go func() {
  363. for {
  364. select {
  365. case <-pxy.closeCh:
  366. break
  367. case sid := <-sidCh:
  368. workConn, errRet := pxy.GetWorkConnFromPool()
  369. if errRet != nil {
  370. continue
  371. }
  372. m := &msg.NatHoleSid{
  373. Sid: sid,
  374. }
  375. errRet = msg.WriteMsg(workConn, m)
  376. if errRet != nil {
  377. pxy.Warn("write nat hole sid package error, %v", errRet)
  378. }
  379. }
  380. }
  381. }()
  382. return
  383. }
  384. func (pxy *XtcpProxy) GetConf() config.ProxyConf {
  385. return pxy.cfg
  386. }
  387. func (pxy *XtcpProxy) Close() {
  388. pxy.BaseProxy.Close()
  389. pxy.ctl.svr.natHoleController.CloseClient(pxy.GetName())
  390. errors.PanicToError(func() {
  391. close(pxy.closeCh)
  392. })
  393. }
  394. type UdpProxy struct {
  395. BaseProxy
  396. cfg *config.UdpProxyConf
  397. realPort int
  398. // udpConn is the listener of udp packages
  399. udpConn *net.UDPConn
  400. // there are always only one workConn at the same time
  401. // get another one if it closed
  402. workConn net.Conn
  403. // sendCh is used for sending packages to workConn
  404. sendCh chan *msg.UdpPacket
  405. // readCh is used for reading packages from workConn
  406. readCh chan *msg.UdpPacket
  407. // checkCloseCh is used for watching if workConn is closed
  408. checkCloseCh chan int
  409. isClosed bool
  410. }
  411. func (pxy *UdpProxy) Run() (remoteAddr string, err error) {
  412. pxy.realPort, err = pxy.ctl.svr.udpPortManager.Acquire(pxy.name, pxy.cfg.RemotePort)
  413. if err != nil {
  414. return
  415. }
  416. defer func() {
  417. if err != nil {
  418. pxy.ctl.svr.udpPortManager.Release(pxy.realPort)
  419. }
  420. }()
  421. remoteAddr = fmt.Sprintf(":%d", pxy.realPort)
  422. pxy.cfg.RemotePort = pxy.realPort
  423. addr, errRet := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", g.GlbServerCfg.ProxyBindAddr, pxy.realPort))
  424. if errRet != nil {
  425. err = errRet
  426. return
  427. }
  428. udpConn, errRet := net.ListenUDP("udp", addr)
  429. if errRet != nil {
  430. err = errRet
  431. pxy.Warn("listen udp port error: %v", err)
  432. return
  433. }
  434. pxy.Info("udp proxy listen port [%d]", pxy.cfg.RemotePort)
  435. pxy.udpConn = udpConn
  436. pxy.sendCh = make(chan *msg.UdpPacket, 1024)
  437. pxy.readCh = make(chan *msg.UdpPacket, 1024)
  438. pxy.checkCloseCh = make(chan int)
  439. // read message from workConn, if it returns any error, notify proxy to start a new workConn
  440. workConnReaderFn := func(conn net.Conn) {
  441. for {
  442. var (
  443. rawMsg msg.Message
  444. errRet error
  445. )
  446. pxy.Trace("loop waiting message from udp workConn")
  447. // client will send heartbeat in workConn for keeping alive
  448. conn.SetReadDeadline(time.Now().Add(time.Duration(60) * time.Second))
  449. if rawMsg, errRet = msg.ReadMsg(conn); errRet != nil {
  450. pxy.Warn("read from workConn for udp error: %v", errRet)
  451. conn.Close()
  452. // notify proxy to start a new work connection
  453. // ignore error here, it means the proxy is closed
  454. errors.PanicToError(func() {
  455. pxy.checkCloseCh <- 1
  456. })
  457. return
  458. }
  459. conn.SetReadDeadline(time.Time{})
  460. switch m := rawMsg.(type) {
  461. case *msg.Ping:
  462. pxy.Trace("udp work conn get ping message")
  463. continue
  464. case *msg.UdpPacket:
  465. if errRet := errors.PanicToError(func() {
  466. pxy.Trace("get udp message from workConn: %s", m.Content)
  467. pxy.readCh <- m
  468. StatsAddTrafficOut(pxy.GetName(), int64(len(m.Content)))
  469. }); errRet != nil {
  470. conn.Close()
  471. pxy.Info("reader goroutine for udp work connection closed")
  472. return
  473. }
  474. }
  475. }
  476. }
  477. // send message to workConn
  478. workConnSenderFn := func(conn net.Conn, ctx context.Context) {
  479. var errRet error
  480. for {
  481. select {
  482. case udpMsg, ok := <-pxy.sendCh:
  483. if !ok {
  484. pxy.Info("sender goroutine for udp work connection closed")
  485. return
  486. }
  487. if errRet = msg.WriteMsg(conn, udpMsg); errRet != nil {
  488. pxy.Info("sender goroutine for udp work connection closed: %v", errRet)
  489. conn.Close()
  490. return
  491. } else {
  492. pxy.Trace("send message to udp workConn: %s", udpMsg.Content)
  493. StatsAddTrafficIn(pxy.GetName(), int64(len(udpMsg.Content)))
  494. continue
  495. }
  496. case <-ctx.Done():
  497. pxy.Info("sender goroutine for udp work connection closed")
  498. return
  499. }
  500. }
  501. }
  502. go func() {
  503. // Sleep a while for waiting control send the NewProxyResp to client.
  504. time.Sleep(500 * time.Millisecond)
  505. for {
  506. workConn, err := pxy.GetWorkConnFromPool()
  507. if err != nil {
  508. time.Sleep(1 * time.Second)
  509. // check if proxy is closed
  510. select {
  511. case _, ok := <-pxy.checkCloseCh:
  512. if !ok {
  513. return
  514. }
  515. default:
  516. }
  517. continue
  518. }
  519. // close the old workConn and replac it with a new one
  520. if pxy.workConn != nil {
  521. pxy.workConn.Close()
  522. }
  523. pxy.workConn = workConn
  524. ctx, cancel := context.WithCancel(context.Background())
  525. go workConnReaderFn(workConn)
  526. go workConnSenderFn(workConn, ctx)
  527. _, ok := <-pxy.checkCloseCh
  528. cancel()
  529. if !ok {
  530. return
  531. }
  532. }
  533. }()
  534. // Read from user connections and send wrapped udp message to sendCh (forwarded by workConn).
  535. // Client will transfor udp message to local udp service and waiting for response for a while.
  536. // Response will be wrapped to be forwarded by work connection to server.
  537. // Close readCh and sendCh at the end.
  538. go func() {
  539. udp.ForwardUserConn(udpConn, pxy.readCh, pxy.sendCh)
  540. pxy.Close()
  541. }()
  542. return remoteAddr, nil
  543. }
  544. func (pxy *UdpProxy) GetConf() config.ProxyConf {
  545. return pxy.cfg
  546. }
  547. func (pxy *UdpProxy) Close() {
  548. pxy.mu.Lock()
  549. defer pxy.mu.Unlock()
  550. if !pxy.isClosed {
  551. pxy.isClosed = true
  552. pxy.BaseProxy.Close()
  553. if pxy.workConn != nil {
  554. pxy.workConn.Close()
  555. }
  556. pxy.udpConn.Close()
  557. // all channels only closed here
  558. close(pxy.checkCloseCh)
  559. close(pxy.readCh)
  560. close(pxy.sendCh)
  561. }
  562. pxy.ctl.svr.udpPortManager.Release(pxy.realPort)
  563. }
  564. // HandleUserTcpConnection is used for incoming tcp user connections.
  565. // It can be used for tcp, http, https type.
  566. func HandleUserTcpConnection(pxy Proxy, userConn frpNet.Conn) {
  567. defer userConn.Close()
  568. // try all connections from the pool
  569. workConn, err := pxy.GetWorkConnFromPool()
  570. if err != nil {
  571. return
  572. }
  573. defer workConn.Close()
  574. var local io.ReadWriteCloser = workConn
  575. cfg := pxy.GetConf().GetBaseInfo()
  576. if cfg.UseEncryption {
  577. local, err = frpIo.WithEncryption(local, []byte(g.GlbServerCfg.Token))
  578. if err != nil {
  579. pxy.Error("create encryption stream error: %v", err)
  580. return
  581. }
  582. }
  583. if cfg.UseCompression {
  584. local = frpIo.WithCompression(local)
  585. }
  586. pxy.Debug("join connections, workConn(l[%s] r[%s]) userConn(l[%s] r[%s])", workConn.LocalAddr().String(),
  587. workConn.RemoteAddr().String(), userConn.LocalAddr().String(), userConn.RemoteAddr().String())
  588. StatsOpenConnection(pxy.GetName())
  589. inCount, outCount := frpIo.Join(local, userConn)
  590. StatsCloseConnection(pxy.GetName())
  591. StatsAddTrafficIn(pxy.GetName(), inCount)
  592. StatsAddTrafficOut(pxy.GetName(), outCount)
  593. pxy.Debug("join connections closed")
  594. }