2023-05-28 16:50:43 +08:00
|
|
|
// Copyright 2017 fatedier, fatedier@gmail.com
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package visitor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
|
2023-09-06 10:18:02 +08:00
|
|
|
v1 "github.com/fatedier/frp/pkg/config/v1"
|
2023-05-28 16:50:43 +08:00
|
|
|
"github.com/fatedier/frp/pkg/transport"
|
2023-11-27 15:47:49 +08:00
|
|
|
netpkg "github.com/fatedier/frp/pkg/util/net"
|
2023-05-28 16:50:43 +08:00
|
|
|
"github.com/fatedier/frp/pkg/util/xlog"
|
|
|
|
)
|
|
|
|
|
2023-11-22 14:30:22 +08:00
|
|
|
// Helper wraps some functions for visitor to use.
|
2023-05-30 20:25:22 +08:00
|
|
|
type Helper interface {
|
|
|
|
// ConnectServer directly connects to the frp server.
|
|
|
|
ConnectServer() (net.Conn, error)
|
|
|
|
// TransferConn transfers the connection to another visitor.
|
|
|
|
TransferConn(string, net.Conn) error
|
|
|
|
// MsgTransporter returns the message transporter that is used to send and receive messages
|
|
|
|
// to the frp server through the controller.
|
|
|
|
MsgTransporter() transport.MessageTransporter
|
|
|
|
// RunID returns the run id of current controller.
|
|
|
|
RunID() string
|
|
|
|
}
|
|
|
|
|
2023-05-28 16:50:43 +08:00
|
|
|
// Visitor is used for forward traffics from local port tot remote service.
|
|
|
|
type Visitor interface {
|
|
|
|
Run() error
|
2023-05-30 10:55:00 +08:00
|
|
|
AcceptConn(conn net.Conn) error
|
2023-05-28 16:50:43 +08:00
|
|
|
Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewVisitor(
|
|
|
|
ctx context.Context,
|
2023-09-06 10:18:02 +08:00
|
|
|
cfg v1.VisitorConfigurer,
|
|
|
|
clientCfg *v1.ClientCommonConfig,
|
2023-05-30 20:25:22 +08:00
|
|
|
helper Helper,
|
2023-05-28 16:50:43 +08:00
|
|
|
) (visitor Visitor) {
|
2023-09-06 10:18:02 +08:00
|
|
|
xl := xlog.FromContextSafe(ctx).Spawn().AppendPrefix(cfg.GetBaseConfig().Name)
|
2023-05-28 16:50:43 +08:00
|
|
|
baseVisitor := BaseVisitor{
|
2023-05-30 20:25:22 +08:00
|
|
|
clientCfg: clientCfg,
|
|
|
|
helper: helper,
|
|
|
|
ctx: xlog.NewContext(ctx, xl),
|
2023-11-27 15:47:49 +08:00
|
|
|
internalLn: netpkg.NewInternalListener(),
|
2023-05-28 16:50:43 +08:00
|
|
|
}
|
|
|
|
switch cfg := cfg.(type) {
|
2023-09-06 10:18:02 +08:00
|
|
|
case *v1.STCPVisitorConfig:
|
2023-05-28 16:50:43 +08:00
|
|
|
visitor = &STCPVisitor{
|
|
|
|
BaseVisitor: &baseVisitor,
|
|
|
|
cfg: cfg,
|
|
|
|
}
|
2023-09-06 10:18:02 +08:00
|
|
|
case *v1.XTCPVisitorConfig:
|
2023-05-28 16:50:43 +08:00
|
|
|
visitor = &XTCPVisitor{
|
|
|
|
BaseVisitor: &baseVisitor,
|
|
|
|
cfg: cfg,
|
|
|
|
startTunnelCh: make(chan struct{}),
|
|
|
|
}
|
2023-09-06 10:18:02 +08:00
|
|
|
case *v1.SUDPVisitorConfig:
|
2023-05-28 16:50:43 +08:00
|
|
|
visitor = &SUDPVisitor{
|
|
|
|
BaseVisitor: &baseVisitor,
|
|
|
|
cfg: cfg,
|
|
|
|
checkCloseCh: make(chan struct{}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
type BaseVisitor struct {
|
2023-09-06 10:18:02 +08:00
|
|
|
clientCfg *v1.ClientCommonConfig
|
2023-05-30 20:25:22 +08:00
|
|
|
helper Helper
|
|
|
|
l net.Listener
|
2023-11-27 15:47:49 +08:00
|
|
|
internalLn *netpkg.InternalListener
|
2023-05-28 16:50:43 +08:00
|
|
|
|
|
|
|
mu sync.RWMutex
|
|
|
|
ctx context.Context
|
|
|
|
}
|
2023-05-30 10:55:00 +08:00
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|