visitor_manager.go 2.7 KB

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