visitor.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. "github.com/fatedier/frp/pkg/config"
  20. "github.com/fatedier/frp/pkg/transport"
  21. "github.com/fatedier/frp/pkg/util/xlog"
  22. )
  23. // Visitor is used for forward traffics from local port tot remote service.
  24. type Visitor interface {
  25. Run() error
  26. Close()
  27. }
  28. func NewVisitor(
  29. ctx context.Context,
  30. cfg config.VisitorConf,
  31. clientCfg config.ClientCommonConf,
  32. connectServer func() (net.Conn, error),
  33. msgTransporter transport.MessageTransporter,
  34. ) (visitor Visitor) {
  35. xl := xlog.FromContextSafe(ctx).Spawn().AppendPrefix(cfg.GetBaseInfo().ProxyName)
  36. baseVisitor := BaseVisitor{
  37. clientCfg: clientCfg,
  38. connectServer: connectServer,
  39. msgTransporter: msgTransporter,
  40. ctx: xlog.NewContext(ctx, xl),
  41. }
  42. switch cfg := cfg.(type) {
  43. case *config.STCPVisitorConf:
  44. visitor = &STCPVisitor{
  45. BaseVisitor: &baseVisitor,
  46. cfg: cfg,
  47. }
  48. case *config.XTCPVisitorConf:
  49. visitor = &XTCPVisitor{
  50. BaseVisitor: &baseVisitor,
  51. cfg: cfg,
  52. startTunnelCh: make(chan struct{}),
  53. }
  54. case *config.SUDPVisitorConf:
  55. visitor = &SUDPVisitor{
  56. BaseVisitor: &baseVisitor,
  57. cfg: cfg,
  58. checkCloseCh: make(chan struct{}),
  59. }
  60. }
  61. return
  62. }
  63. type BaseVisitor struct {
  64. clientCfg config.ClientCommonConf
  65. connectServer func() (net.Conn, error)
  66. msgTransporter transport.MessageTransporter
  67. l net.Listener
  68. mu sync.RWMutex
  69. ctx context.Context
  70. }