xtcp.go 1.7 KB

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