visitor.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. "github.com/fatedier/frp/pkg/transport"
  21. netpkg "github.com/fatedier/frp/pkg/util/net"
  22. "github.com/fatedier/frp/pkg/util/xlog"
  23. )
  24. // Helper wraps some functions for visitor to use.
  25. type Helper interface {
  26. // ConnectServer directly connects to the frp server.
  27. ConnectServer() (net.Conn, error)
  28. // TransferConn transfers the connection to another visitor.
  29. TransferConn(string, net.Conn) error
  30. // MsgTransporter returns the message transporter that is used to send and receive messages
  31. // to the frp server through the controller.
  32. MsgTransporter() transport.MessageTransporter
  33. // RunID returns the run id of current controller.
  34. RunID() string
  35. }
  36. // Visitor is used for forward traffics from local port tot remote service.
  37. type Visitor interface {
  38. Run() error
  39. AcceptConn(conn net.Conn) error
  40. Close()
  41. }
  42. func NewVisitor(
  43. ctx context.Context,
  44. cfg v1.VisitorConfigurer,
  45. clientCfg *v1.ClientCommonConfig,
  46. helper Helper,
  47. ) (visitor Visitor) {
  48. xl := xlog.FromContextSafe(ctx).Spawn().AppendPrefix(cfg.GetBaseConfig().Name)
  49. baseVisitor := BaseVisitor{
  50. clientCfg: clientCfg,
  51. helper: helper,
  52. ctx: xlog.NewContext(ctx, xl),
  53. internalLn: netpkg.NewInternalListener(),
  54. }
  55. switch cfg := cfg.(type) {
  56. case *v1.STCPVisitorConfig:
  57. visitor = &STCPVisitor{
  58. BaseVisitor: &baseVisitor,
  59. cfg: cfg,
  60. }
  61. case *v1.XTCPVisitorConfig:
  62. visitor = &XTCPVisitor{
  63. BaseVisitor: &baseVisitor,
  64. cfg: cfg,
  65. startTunnelCh: make(chan struct{}),
  66. }
  67. case *v1.SUDPVisitorConfig:
  68. visitor = &SUDPVisitor{
  69. BaseVisitor: &baseVisitor,
  70. cfg: cfg,
  71. checkCloseCh: make(chan struct{}),
  72. }
  73. }
  74. return
  75. }
  76. type BaseVisitor struct {
  77. clientCfg *v1.ClientCommonConfig
  78. helper Helper
  79. l net.Listener
  80. internalLn *netpkg.InternalListener
  81. mu sync.RWMutex
  82. ctx context.Context
  83. }
  84. func (v *BaseVisitor) AcceptConn(conn net.Conn) error {
  85. return v.internalLn.PutConn(conn)
  86. }
  87. func (v *BaseVisitor) Close() {
  88. if v.l != nil {
  89. v.l.Close()
  90. }
  91. if v.internalLn != nil {
  92. v.internalLn.Close()
  93. }
  94. }