2018-06-25 18:22:35 +08:00
|
|
|
// Copyright 2018 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.
|
|
|
|
|
2023-05-28 16:50:43 +08:00
|
|
|
package visitor
|
2018-06-25 18:22:35 +08:00
|
|
|
|
|
|
|
import (
|
2019-10-12 20:13:12 +08:00
|
|
|
"context"
|
2023-05-30 10:55:00 +08:00
|
|
|
"fmt"
|
2023-05-28 16:50:43 +08:00
|
|
|
"net"
|
2023-05-30 20:25:22 +08:00
|
|
|
"reflect"
|
2018-06-25 18:22:35 +08:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2023-09-06 10:18:02 +08:00
|
|
|
"github.com/samber/lo"
|
|
|
|
|
|
|
|
v1 "github.com/fatedier/frp/pkg/config/v1"
|
2023-05-28 16:50:43 +08:00
|
|
|
"github.com/fatedier/frp/pkg/transport"
|
2020-09-23 13:49:14 +08:00
|
|
|
"github.com/fatedier/frp/pkg/util/xlog"
|
2018-06-25 18:22:35 +08:00
|
|
|
)
|
|
|
|
|
2023-05-28 16:50:43 +08:00
|
|
|
type Manager struct {
|
2023-09-06 10:18:02 +08:00
|
|
|
clientCfg *v1.ClientCommonConfig
|
|
|
|
cfgs map[string]v1.VisitorConfigurer
|
2023-05-30 20:25:22 +08:00
|
|
|
visitors map[string]Visitor
|
|
|
|
helper Helper
|
2018-06-25 18:22:35 +08:00
|
|
|
|
2023-11-27 15:47:49 +08:00
|
|
|
checkInterval time.Duration
|
|
|
|
keepVisitorsRunningOnce sync.Once
|
2018-06-25 18:22:35 +08:00
|
|
|
|
2023-05-30 10:55:00 +08:00
|
|
|
mu sync.RWMutex
|
2019-10-12 20:13:12 +08:00
|
|
|
ctx context.Context
|
2020-04-02 10:58:37 +08:00
|
|
|
|
|
|
|
stopCh chan struct{}
|
2018-06-25 18:22:35 +08:00
|
|
|
}
|
|
|
|
|
2023-05-28 16:50:43 +08:00
|
|
|
func NewManager(
|
|
|
|
ctx context.Context,
|
2023-05-30 20:25:22 +08:00
|
|
|
runID string,
|
2023-09-06 10:18:02 +08:00
|
|
|
clientCfg *v1.ClientCommonConfig,
|
2023-05-28 16:50:43 +08:00
|
|
|
connectServer func() (net.Conn, error),
|
|
|
|
msgTransporter transport.MessageTransporter,
|
|
|
|
) *Manager {
|
2023-05-30 20:25:22 +08:00
|
|
|
m := &Manager{
|
|
|
|
clientCfg: clientCfg,
|
2023-09-06 10:18:02 +08:00
|
|
|
cfgs: make(map[string]v1.VisitorConfigurer),
|
2023-05-30 20:25:22 +08:00
|
|
|
visitors: make(map[string]Visitor),
|
|
|
|
checkInterval: 10 * time.Second,
|
|
|
|
ctx: ctx,
|
|
|
|
stopCh: make(chan struct{}),
|
2018-06-25 18:22:35 +08:00
|
|
|
}
|
2023-05-30 20:25:22 +08:00
|
|
|
m.helper = &visitorHelperImpl{
|
|
|
|
connectServerFn: connectServer,
|
|
|
|
msgTransporter: msgTransporter,
|
|
|
|
transferConnFn: m.TransferConn,
|
|
|
|
runID: runID,
|
|
|
|
}
|
|
|
|
return m
|
2018-06-25 18:22:35 +08:00
|
|
|
}
|
|
|
|
|
2023-11-27 15:47:49 +08:00
|
|
|
// keepVisitorsRunning checks all visitors' status periodically, if some visitor is not running, start it.
|
|
|
|
// It will only start after Reload is called and a new visitor is added.
|
|
|
|
func (vm *Manager) keepVisitorsRunning() {
|
2019-10-12 20:13:12 +08:00
|
|
|
xl := xlog.FromContextSafe(vm.ctx)
|
2020-04-02 10:58:37 +08:00
|
|
|
|
|
|
|
ticker := time.NewTicker(vm.checkInterval)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
2018-06-25 18:22:35 +08:00
|
|
|
for {
|
2020-04-02 10:58:37 +08:00
|
|
|
select {
|
|
|
|
case <-vm.stopCh:
|
2024-03-12 13:58:53 +08:00
|
|
|
xl.Tracef("gracefully shutdown visitor manager")
|
2020-04-02 10:58:37 +08:00
|
|
|
return
|
|
|
|
case <-ticker.C:
|
|
|
|
vm.mu.Lock()
|
|
|
|
for _, cfg := range vm.cfgs {
|
2023-09-06 10:18:02 +08:00
|
|
|
name := cfg.GetBaseConfig().Name
|
2020-04-02 10:58:37 +08:00
|
|
|
if _, exist := vm.visitors[name]; !exist {
|
2024-03-12 13:58:53 +08:00
|
|
|
xl.Infof("try to start visitor [%s]", name)
|
2022-08-29 01:02:53 +08:00
|
|
|
_ = vm.startVisitor(cfg)
|
2020-04-02 10:58:37 +08:00
|
|
|
}
|
2018-06-25 18:22:35 +08:00
|
|
|
}
|
2020-04-02 10:58:37 +08:00
|
|
|
vm.mu.Unlock()
|
2018-06-25 18:22:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-30 10:55:00 +08:00
|
|
|
func (vm *Manager) Close() {
|
|
|
|
vm.mu.Lock()
|
|
|
|
defer vm.mu.Unlock()
|
|
|
|
for _, v := range vm.visitors {
|
|
|
|
v.Close()
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-vm.stopCh:
|
|
|
|
default:
|
|
|
|
close(vm.stopCh)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-25 18:22:35 +08:00
|
|
|
// Hold lock before calling this function.
|
2023-09-06 10:18:02 +08:00
|
|
|
func (vm *Manager) startVisitor(cfg v1.VisitorConfigurer) (err error) {
|
2019-10-12 20:13:12 +08:00
|
|
|
xl := xlog.FromContextSafe(vm.ctx)
|
2023-09-06 10:18:02 +08:00
|
|
|
name := cfg.GetBaseConfig().Name
|
2023-05-30 20:25:22 +08:00
|
|
|
visitor := NewVisitor(vm.ctx, cfg, vm.clientCfg, vm.helper)
|
2018-06-25 18:22:35 +08:00
|
|
|
err = visitor.Run()
|
|
|
|
if err != nil {
|
2024-03-12 13:58:53 +08:00
|
|
|
xl.Warnf("start error: %v", err)
|
2018-06-25 18:22:35 +08:00
|
|
|
} else {
|
|
|
|
vm.visitors[name] = visitor
|
2024-03-12 13:58:53 +08:00
|
|
|
xl.Infof("start visitor success")
|
2018-06-25 18:22:35 +08:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-11-27 15:47:49 +08:00
|
|
|
func (vm *Manager) UpdateAll(cfgs []v1.VisitorConfigurer) {
|
|
|
|
if len(cfgs) > 0 {
|
|
|
|
// Only start keepVisitorsRunning goroutine once and only when there is at least one visitor.
|
|
|
|
vm.keepVisitorsRunningOnce.Do(func() {
|
|
|
|
go vm.keepVisitorsRunning()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-10-12 20:13:12 +08:00
|
|
|
xl := xlog.FromContextSafe(vm.ctx)
|
2023-09-06 10:18:02 +08:00
|
|
|
cfgsMap := lo.KeyBy(cfgs, func(c v1.VisitorConfigurer) string {
|
|
|
|
return c.GetBaseConfig().Name
|
|
|
|
})
|
2018-06-25 18:22:35 +08:00
|
|
|
vm.mu.Lock()
|
|
|
|
defer vm.mu.Unlock()
|
|
|
|
|
|
|
|
delNames := make([]string, 0)
|
|
|
|
for name, oldCfg := range vm.cfgs {
|
|
|
|
del := false
|
2023-09-06 10:18:02 +08:00
|
|
|
cfg, ok := cfgsMap[name]
|
2023-05-30 20:25:22 +08:00
|
|
|
if !ok || !reflect.DeepEqual(oldCfg, cfg) {
|
2022-08-29 01:02:53 +08:00
|
|
|
del = true
|
2018-06-25 18:22:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if del {
|
|
|
|
delNames = append(delNames, name)
|
|
|
|
delete(vm.cfgs, name)
|
|
|
|
if visitor, ok := vm.visitors[name]; ok {
|
|
|
|
visitor.Close()
|
|
|
|
}
|
|
|
|
delete(vm.visitors, name)
|
|
|
|
}
|
|
|
|
}
|
2018-12-07 17:05:36 +08:00
|
|
|
if len(delNames) > 0 {
|
2024-03-12 13:58:53 +08:00
|
|
|
xl.Infof("visitor removed: %v", delNames)
|
2018-12-07 17:05:36 +08:00
|
|
|
}
|
2018-06-25 18:22:35 +08:00
|
|
|
|
|
|
|
addNames := make([]string, 0)
|
2023-09-06 10:18:02 +08:00
|
|
|
for _, cfg := range cfgs {
|
|
|
|
name := cfg.GetBaseConfig().Name
|
2018-06-25 18:22:35 +08:00
|
|
|
if _, ok := vm.cfgs[name]; !ok {
|
|
|
|
vm.cfgs[name] = cfg
|
|
|
|
addNames = append(addNames, name)
|
2022-08-29 01:02:53 +08:00
|
|
|
_ = vm.startVisitor(cfg)
|
2018-06-25 18:22:35 +08:00
|
|
|
}
|
|
|
|
}
|
2018-12-07 17:05:36 +08:00
|
|
|
if len(addNames) > 0 {
|
2024-03-12 13:58:53 +08:00
|
|
|
xl.Infof("visitor added: %v", addNames)
|
2018-12-07 17:05:36 +08:00
|
|
|
}
|
2018-06-25 18:22:35 +08:00
|
|
|
}
|
2018-12-07 17:05:36 +08:00
|
|
|
|
2023-05-30 10:55:00 +08:00
|
|
|
// TransferConn transfers a connection to a visitor.
|
|
|
|
func (vm *Manager) TransferConn(name string, conn net.Conn) error {
|
|
|
|
vm.mu.RLock()
|
|
|
|
defer vm.mu.RUnlock()
|
|
|
|
v, ok := vm.visitors[name]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("visitor [%s] not found", name)
|
2020-04-02 10:58:37 +08:00
|
|
|
}
|
2023-05-30 10:55:00 +08:00
|
|
|
return v.AcceptConn(conn)
|
2018-12-07 17:05:36 +08:00
|
|
|
}
|
2023-05-30 20:25:22 +08:00
|
|
|
|
|
|
|
type visitorHelperImpl struct {
|
|
|
|
connectServerFn func() (net.Conn, error)
|
|
|
|
msgTransporter transport.MessageTransporter
|
|
|
|
transferConnFn func(name string, conn net.Conn) error
|
|
|
|
runID string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *visitorHelperImpl) ConnectServer() (net.Conn, error) {
|
|
|
|
return v.connectServerFn()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *visitorHelperImpl) TransferConn(name string, conn net.Conn) error {
|
|
|
|
return v.transferConnFn(name, conn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *visitorHelperImpl) MsgTransporter() transport.MessageTransporter {
|
|
|
|
return v.msgTransporter
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *visitorHelperImpl) RunID() string {
|
|
|
|
return v.runID
|
|
|
|
}
|