tcp.go 2.4 KB

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