1
0

tcp.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. "net"
  18. "reflect"
  19. "strconv"
  20. v1 "github.com/fatedier/frp/pkg/config/v1"
  21. )
  22. func init() {
  23. RegisterProxyFactory(reflect.TypeOf(&v1.TCPProxyConfig{}), NewTCPProxy)
  24. }
  25. type TCPProxy struct {
  26. *BaseProxy
  27. cfg *v1.TCPProxyConfig
  28. realBindPort int
  29. }
  30. func NewTCPProxy(baseProxy *BaseProxy) Proxy {
  31. unwrapped, ok := baseProxy.GetConfigurer().(*v1.TCPProxyConfig)
  32. if !ok {
  33. return nil
  34. }
  35. baseProxy.usedPortsNum = 1
  36. return &TCPProxy{
  37. BaseProxy: baseProxy,
  38. cfg: unwrapped,
  39. }
  40. }
  41. func (pxy *TCPProxy) Run() (remoteAddr string, err error) {
  42. xl := pxy.xl
  43. if pxy.cfg.LoadBalancer.Group != "" {
  44. l, realBindPort, errRet := pxy.rc.TCPGroupCtl.Listen(pxy.name, pxy.cfg.LoadBalancer.Group, pxy.cfg.LoadBalancer.GroupKey,
  45. pxy.serverCfg.ProxyBindAddr, pxy.cfg.RemotePort)
  46. if errRet != nil {
  47. err = errRet
  48. return
  49. }
  50. defer func() {
  51. if err != nil {
  52. l.Close()
  53. }
  54. }()
  55. pxy.realBindPort = realBindPort
  56. pxy.listeners = append(pxy.listeners, l)
  57. xl.Infof("tcp proxy listen port [%d] in group [%s]", pxy.cfg.RemotePort, pxy.cfg.LoadBalancer.Group)
  58. } else {
  59. pxy.realBindPort, err = pxy.rc.TCPPortManager.Acquire(pxy.name, pxy.cfg.RemotePort)
  60. if err != nil {
  61. return
  62. }
  63. defer func() {
  64. if err != nil {
  65. pxy.rc.TCPPortManager.Release(pxy.realBindPort)
  66. }
  67. }()
  68. listener, errRet := net.Listen("tcp", net.JoinHostPort(pxy.serverCfg.ProxyBindAddr, strconv.Itoa(pxy.realBindPort)))
  69. if errRet != nil {
  70. err = errRet
  71. return
  72. }
  73. pxy.listeners = append(pxy.listeners, listener)
  74. xl.Infof("tcp proxy listen port [%d]", pxy.cfg.RemotePort)
  75. }
  76. pxy.cfg.RemotePort = pxy.realBindPort
  77. remoteAddr = fmt.Sprintf(":%d", pxy.realBindPort)
  78. pxy.startCommonTCPListenersHandler()
  79. return
  80. }
  81. func (pxy *TCPProxy) Close() {
  82. pxy.BaseProxy.Close()
  83. if pxy.cfg.LoadBalancer.Group == "" {
  84. pxy.rc.TCPPortManager.Release(pxy.realBindPort)
  85. }
  86. }