1
0

xtcp.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. "fmt"
  17. "github.com/fatedier/golib/errors"
  18. "golang.org/x/time/rate"
  19. "github.com/fatedier/frp/pkg/config"
  20. "github.com/fatedier/frp/pkg/msg"
  21. )
  22. type XTCPProxy struct {
  23. *BaseProxy
  24. cfg *config.XTCPProxyConf
  25. closeCh chan struct{}
  26. }
  27. func (pxy *XTCPProxy) Run() (remoteAddr string, err error) {
  28. xl := pxy.xl
  29. if pxy.rc.NatHoleController == nil {
  30. xl.Error("udp port for xtcp is not specified.")
  31. err = fmt.Errorf("xtcp is not supported in frps")
  32. return
  33. }
  34. sidCh := pxy.rc.NatHoleController.ListenClient(pxy.GetName(), pxy.cfg.Sk)
  35. go func() {
  36. for {
  37. select {
  38. case <-pxy.closeCh:
  39. return
  40. case sid := <-sidCh:
  41. workConn, errRet := pxy.GetWorkConnFromPool(nil, nil)
  42. if errRet != nil {
  43. continue
  44. }
  45. m := &msg.NatHoleSid{
  46. Sid: sid,
  47. }
  48. errRet = msg.WriteMsg(workConn, m)
  49. if errRet != nil {
  50. xl.Warn("write nat hole sid package error, %v", errRet)
  51. }
  52. workConn.Close()
  53. }
  54. }
  55. }()
  56. return
  57. }
  58. func (pxy *XTCPProxy) GetConf() config.ProxyConf {
  59. return pxy.cfg
  60. }
  61. func (pxy *XTCPProxy) GetLimiter() *rate.Limiter {
  62. return pxy.limiter
  63. }
  64. func (pxy *XTCPProxy) Close() {
  65. pxy.BaseProxy.Close()
  66. pxy.rc.NatHoleController.CloseClient(pxy.GetName())
  67. _ = errors.PanicToError(func() {
  68. close(pxy.closeCh)
  69. })
  70. }