123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- package visitor
- import (
- "context"
- "net"
- "sync"
- v1 "github.com/fatedier/frp/pkg/config/v1"
- "github.com/fatedier/frp/pkg/transport"
- netpkg "github.com/fatedier/frp/pkg/util/net"
- "github.com/fatedier/frp/pkg/util/xlog"
- )
- type Helper interface {
-
- ConnectServer() (net.Conn, error)
-
- TransferConn(string, net.Conn) error
-
-
- MsgTransporter() transport.MessageTransporter
-
- RunID() string
- }
- type Visitor interface {
- Run() error
- AcceptConn(conn net.Conn) error
- Close()
- }
- func NewVisitor(
- ctx context.Context,
- cfg v1.VisitorConfigurer,
- clientCfg *v1.ClientCommonConfig,
- helper Helper,
- ) (visitor Visitor) {
- xl := xlog.FromContextSafe(ctx).Spawn().AppendPrefix(cfg.GetBaseConfig().Name)
- baseVisitor := BaseVisitor{
- clientCfg: clientCfg,
- helper: helper,
- ctx: xlog.NewContext(ctx, xl),
- internalLn: netpkg.NewInternalListener(),
- }
- switch cfg := cfg.(type) {
- case *v1.STCPVisitorConfig:
- visitor = &STCPVisitor{
- BaseVisitor: &baseVisitor,
- cfg: cfg,
- }
- case *v1.XTCPVisitorConfig:
- visitor = &XTCPVisitor{
- BaseVisitor: &baseVisitor,
- cfg: cfg,
- startTunnelCh: make(chan struct{}),
- }
- case *v1.SUDPVisitorConfig:
- visitor = &SUDPVisitor{
- BaseVisitor: &baseVisitor,
- cfg: cfg,
- checkCloseCh: make(chan struct{}),
- }
- }
- return
- }
- type BaseVisitor struct {
- clientCfg *v1.ClientCommonConfig
- helper Helper
- l net.Listener
- internalLn *netpkg.InternalListener
- mu sync.RWMutex
- ctx context.Context
- }
- func (v *BaseVisitor) AcceptConn(conn net.Conn) error {
- return v.internalLn.PutConn(conn)
- }
- func (v *BaseVisitor) Close() {
- if v.l != nil {
- v.l.Close()
- }
- if v.internalLn != nil {
- v.internalLn.Close()
- }
- }
|