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.
|
|
|
|
|
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2019-10-12 20:13:12 +08:00
|
|
|
"context"
|
2018-06-25 18:22:35 +08:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/fatedier/frp/models/config"
|
2019-10-12 20:13:12 +08:00
|
|
|
"github.com/fatedier/frp/utils/xlog"
|
2018-06-25 18:22:35 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type VisitorManager struct {
|
|
|
|
ctl *Control
|
|
|
|
|
|
|
|
cfgs map[string]config.VisitorConf
|
|
|
|
visitors map[string]Visitor
|
|
|
|
|
|
|
|
checkInterval time.Duration
|
|
|
|
|
2019-10-12 20:13:12 +08:00
|
|
|
mu sync.Mutex
|
|
|
|
ctx context.Context
|
2020-04-02 10:58:37 +08:00
|
|
|
|
|
|
|
stopCh chan struct{}
|
2018-06-25 18:22:35 +08:00
|
|
|
}
|
|
|
|
|
2019-10-12 20:13:12 +08:00
|
|
|
func NewVisitorManager(ctx context.Context, ctl *Control) *VisitorManager {
|
2018-06-25 18:22:35 +08:00
|
|
|
return &VisitorManager{
|
|
|
|
ctl: ctl,
|
|
|
|
cfgs: make(map[string]config.VisitorConf),
|
|
|
|
visitors: make(map[string]Visitor),
|
|
|
|
checkInterval: 10 * time.Second,
|
2019-10-12 20:13:12 +08:00
|
|
|
ctx: ctx,
|
2020-04-02 10:58:37 +08:00
|
|
|
stopCh: make(chan struct{}),
|
2018-06-25 18:22:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vm *VisitorManager) Run() {
|
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:
|
|
|
|
xl.Info("gracefully shutdown visitor manager")
|
|
|
|
return
|
|
|
|
case <-ticker.C:
|
|
|
|
vm.mu.Lock()
|
|
|
|
for _, cfg := range vm.cfgs {
|
|
|
|
name := cfg.GetBaseInfo().ProxyName
|
|
|
|
if _, exist := vm.visitors[name]; !exist {
|
|
|
|
xl.Info("try to start visitor [%s]", name)
|
|
|
|
vm.startVisitor(cfg)
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hold lock before calling this function.
|
|
|
|
func (vm *VisitorManager) startVisitor(cfg config.VisitorConf) (err error) {
|
2019-10-12 20:13:12 +08:00
|
|
|
xl := xlog.FromContextSafe(vm.ctx)
|
2018-06-25 18:22:35 +08:00
|
|
|
name := cfg.GetBaseInfo().ProxyName
|
2019-10-12 20:13:12 +08:00
|
|
|
visitor := NewVisitor(vm.ctx, vm.ctl, cfg)
|
2018-06-25 18:22:35 +08:00
|
|
|
err = visitor.Run()
|
|
|
|
if err != nil {
|
2019-10-12 20:13:12 +08:00
|
|
|
xl.Warn("start error: %v", err)
|
2018-06-25 18:22:35 +08:00
|
|
|
} else {
|
|
|
|
vm.visitors[name] = visitor
|
2019-10-12 20:13:12 +08:00
|
|
|
xl.Info("start visitor success")
|
2018-06-25 18:22:35 +08:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vm *VisitorManager) Reload(cfgs map[string]config.VisitorConf) {
|
2019-10-12 20:13:12 +08:00
|
|
|
xl := xlog.FromContextSafe(vm.ctx)
|
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
|
|
|
|
cfg, ok := cfgs[name]
|
|
|
|
if !ok {
|
|
|
|
del = true
|
|
|
|
} else {
|
|
|
|
if !oldCfg.Compare(cfg) {
|
|
|
|
del = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2019-10-12 20:13:12 +08:00
|
|
|
xl.Info("visitor removed: %v", delNames)
|
2018-12-07 17:05:36 +08:00
|
|
|
}
|
2018-06-25 18:22:35 +08:00
|
|
|
|
|
|
|
addNames := make([]string, 0)
|
|
|
|
for name, cfg := range cfgs {
|
|
|
|
if _, ok := vm.cfgs[name]; !ok {
|
|
|
|
vm.cfgs[name] = cfg
|
|
|
|
addNames = append(addNames, name)
|
|
|
|
vm.startVisitor(cfg)
|
|
|
|
}
|
|
|
|
}
|
2018-12-07 17:05:36 +08:00
|
|
|
if len(addNames) > 0 {
|
2019-10-12 20:13:12 +08:00
|
|
|
xl.Info("visitor added: %v", addNames)
|
2018-12-07 17:05:36 +08:00
|
|
|
}
|
2018-06-25 18:22:35 +08:00
|
|
|
return
|
|
|
|
}
|
2018-12-07 17:05:36 +08:00
|
|
|
|
|
|
|
func (vm *VisitorManager) Close() {
|
|
|
|
vm.mu.Lock()
|
|
|
|
defer vm.mu.Unlock()
|
|
|
|
for _, v := range vm.visitors {
|
|
|
|
v.Close()
|
|
|
|
}
|
2020-04-02 10:58:37 +08:00
|
|
|
select {
|
|
|
|
case <-vm.stopCh:
|
|
|
|
default:
|
|
|
|
close(vm.stopCh)
|
|
|
|
}
|
2018-12-07 17:05:36 +08:00
|
|
|
}
|