visitor_manager.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2018 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 client
  15. import (
  16. "context"
  17. "sync"
  18. "time"
  19. "github.com/fatedier/frp/models/config"
  20. "github.com/fatedier/frp/utils/xlog"
  21. )
  22. type VisitorManager struct {
  23. ctl *Control
  24. cfgs map[string]config.VisitorConf
  25. visitors map[string]Visitor
  26. checkInterval time.Duration
  27. mu sync.Mutex
  28. ctx context.Context
  29. }
  30. func NewVisitorManager(ctx context.Context, ctl *Control) *VisitorManager {
  31. return &VisitorManager{
  32. ctl: ctl,
  33. cfgs: make(map[string]config.VisitorConf),
  34. visitors: make(map[string]Visitor),
  35. checkInterval: 10 * time.Second,
  36. ctx: ctx,
  37. }
  38. }
  39. func (vm *VisitorManager) Run() {
  40. xl := xlog.FromContextSafe(vm.ctx)
  41. for {
  42. time.Sleep(vm.checkInterval)
  43. vm.mu.Lock()
  44. for _, cfg := range vm.cfgs {
  45. name := cfg.GetBaseInfo().ProxyName
  46. if _, exist := vm.visitors[name]; !exist {
  47. xl.Info("try to start visitor [%s]", name)
  48. vm.startVisitor(cfg)
  49. }
  50. }
  51. vm.mu.Unlock()
  52. }
  53. }
  54. // Hold lock before calling this function.
  55. func (vm *VisitorManager) startVisitor(cfg config.VisitorConf) (err error) {
  56. xl := xlog.FromContextSafe(vm.ctx)
  57. name := cfg.GetBaseInfo().ProxyName
  58. visitor := NewVisitor(vm.ctx, vm.ctl, cfg)
  59. err = visitor.Run()
  60. if err != nil {
  61. xl.Warn("start error: %v", err)
  62. } else {
  63. vm.visitors[name] = visitor
  64. xl.Info("start visitor success")
  65. }
  66. return
  67. }
  68. func (vm *VisitorManager) Reload(cfgs map[string]config.VisitorConf) {
  69. xl := xlog.FromContextSafe(vm.ctx)
  70. vm.mu.Lock()
  71. defer vm.mu.Unlock()
  72. delNames := make([]string, 0)
  73. for name, oldCfg := range vm.cfgs {
  74. del := false
  75. cfg, ok := cfgs[name]
  76. if !ok {
  77. del = true
  78. } else {
  79. if !oldCfg.Compare(cfg) {
  80. del = true
  81. }
  82. }
  83. if del {
  84. delNames = append(delNames, name)
  85. delete(vm.cfgs, name)
  86. if visitor, ok := vm.visitors[name]; ok {
  87. visitor.Close()
  88. }
  89. delete(vm.visitors, name)
  90. }
  91. }
  92. if len(delNames) > 0 {
  93. xl.Info("visitor removed: %v", delNames)
  94. }
  95. addNames := make([]string, 0)
  96. for name, cfg := range cfgs {
  97. if _, ok := vm.cfgs[name]; !ok {
  98. vm.cfgs[name] = cfg
  99. addNames = append(addNames, name)
  100. vm.startVisitor(cfg)
  101. }
  102. }
  103. if len(addNames) > 0 {
  104. xl.Info("visitor added: %v", addNames)
  105. }
  106. return
  107. }
  108. func (vm *VisitorManager) Close() {
  109. vm.mu.Lock()
  110. defer vm.mu.Unlock()
  111. for _, v := range vm.visitors {
  112. v.Close()
  113. }
  114. }