1
0

visitor.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 visitor
  15. import (
  16. "context"
  17. "net"
  18. "sync"
  19. v1 "github.com/fatedier/frp/pkg/config/v1"
  20. plugin "github.com/fatedier/frp/pkg/plugin/visitor"
  21. "github.com/fatedier/frp/pkg/transport"
  22. netpkg "github.com/fatedier/frp/pkg/util/net"
  23. "github.com/fatedier/frp/pkg/util/xlog"
  24. "github.com/fatedier/frp/pkg/vnet"
  25. )
  26. // Helper wraps some functions for visitor to use.
  27. type Helper interface {
  28. // ConnectServer directly connects to the frp server.
  29. ConnectServer() (net.Conn, error)
  30. // TransferConn transfers the connection to another visitor.
  31. TransferConn(string, net.Conn) error
  32. // MsgTransporter returns the message transporter that is used to send and receive messages
  33. // to the frp server through the controller.
  34. MsgTransporter() transport.MessageTransporter
  35. // VNetController returns the vnet controller that is used to manage the virtual network.
  36. VNetController() *vnet.Controller
  37. // RunID returns the run id of current controller.
  38. RunID() string
  39. }
  40. // Visitor is used for forward traffics from local port tot remote service.
  41. type Visitor interface {
  42. Run() error
  43. AcceptConn(conn net.Conn) error
  44. Close()
  45. }
  46. func NewVisitor(
  47. ctx context.Context,
  48. cfg v1.VisitorConfigurer,
  49. clientCfg *v1.ClientCommonConfig,
  50. helper Helper,
  51. ) (Visitor, error) {
  52. xl := xlog.FromContextSafe(ctx).Spawn().AppendPrefix(cfg.GetBaseConfig().Name)
  53. ctx = xlog.NewContext(ctx, xl)
  54. var visitor Visitor
  55. baseVisitor := BaseVisitor{
  56. clientCfg: clientCfg,
  57. helper: helper,
  58. ctx: ctx,
  59. internalLn: netpkg.NewInternalListener(),
  60. }
  61. if cfg.GetBaseConfig().Plugin.Type != "" {
  62. p, err := plugin.Create(
  63. cfg.GetBaseConfig().Plugin.Type,
  64. plugin.PluginContext{
  65. Name: cfg.GetBaseConfig().Name,
  66. Ctx: ctx,
  67. VnetController: helper.VNetController(),
  68. HandleConn: func(conn net.Conn) {
  69. _ = baseVisitor.AcceptConn(conn)
  70. },
  71. },
  72. cfg.GetBaseConfig().Plugin.VisitorPluginOptions,
  73. )
  74. if err != nil {
  75. return nil, err
  76. }
  77. baseVisitor.plugin = p
  78. }
  79. switch cfg := cfg.(type) {
  80. case *v1.STCPVisitorConfig:
  81. visitor = &STCPVisitor{
  82. BaseVisitor: &baseVisitor,
  83. cfg: cfg,
  84. }
  85. case *v1.XTCPVisitorConfig:
  86. visitor = &XTCPVisitor{
  87. BaseVisitor: &baseVisitor,
  88. cfg: cfg,
  89. startTunnelCh: make(chan struct{}),
  90. }
  91. case *v1.SUDPVisitorConfig:
  92. visitor = &SUDPVisitor{
  93. BaseVisitor: &baseVisitor,
  94. cfg: cfg,
  95. checkCloseCh: make(chan struct{}),
  96. }
  97. }
  98. return visitor, nil
  99. }
  100. type BaseVisitor struct {
  101. clientCfg *v1.ClientCommonConfig
  102. helper Helper
  103. l net.Listener
  104. internalLn *netpkg.InternalListener
  105. plugin plugin.Plugin
  106. mu sync.RWMutex
  107. ctx context.Context
  108. }
  109. func (v *BaseVisitor) AcceptConn(conn net.Conn) error {
  110. return v.internalLn.PutConn(conn)
  111. }
  112. func (v *BaseVisitor) Close() {
  113. if v.l != nil {
  114. v.l.Close()
  115. }
  116. if v.internalLn != nil {
  117. v.internalLn.Close()
  118. }
  119. if v.plugin != nil {
  120. v.plugin.Close()
  121. }
  122. }