proxy_manager.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Copyright 2023 The frp Authors
  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 proxy
  15. import (
  16. "context"
  17. "fmt"
  18. "net"
  19. "reflect"
  20. "sync"
  21. "github.com/samber/lo"
  22. "github.com/fatedier/frp/client/event"
  23. v1 "github.com/fatedier/frp/pkg/config/v1"
  24. "github.com/fatedier/frp/pkg/msg"
  25. "github.com/fatedier/frp/pkg/transport"
  26. "github.com/fatedier/frp/pkg/util/xlog"
  27. )
  28. type Manager struct {
  29. proxies map[string]*Wrapper
  30. msgTransporter transport.MessageTransporter
  31. inWorkConnCallback func(*v1.ProxyBaseConfig, net.Conn, *msg.StartWorkConn) bool
  32. closed bool
  33. mu sync.RWMutex
  34. clientCfg *v1.ClientCommonConfig
  35. ctx context.Context
  36. }
  37. func NewManager(
  38. ctx context.Context,
  39. clientCfg *v1.ClientCommonConfig,
  40. msgTransporter transport.MessageTransporter,
  41. ) *Manager {
  42. return &Manager{
  43. proxies: make(map[string]*Wrapper),
  44. msgTransporter: msgTransporter,
  45. closed: false,
  46. clientCfg: clientCfg,
  47. ctx: ctx,
  48. }
  49. }
  50. func (pm *Manager) StartProxy(name string, remoteAddr string, serverRespErr string) error {
  51. pm.mu.RLock()
  52. pxy, ok := pm.proxies[name]
  53. pm.mu.RUnlock()
  54. if !ok {
  55. return fmt.Errorf("proxy [%s] not found", name)
  56. }
  57. err := pxy.SetRunningStatus(remoteAddr, serverRespErr)
  58. if err != nil {
  59. return err
  60. }
  61. return nil
  62. }
  63. func (pm *Manager) SetInWorkConnCallback(cb func(*v1.ProxyBaseConfig, net.Conn, *msg.StartWorkConn) bool) {
  64. pm.inWorkConnCallback = cb
  65. }
  66. func (pm *Manager) Close() {
  67. pm.mu.Lock()
  68. defer pm.mu.Unlock()
  69. for _, pxy := range pm.proxies {
  70. pxy.Stop()
  71. }
  72. pm.proxies = make(map[string]*Wrapper)
  73. }
  74. func (pm *Manager) HandleWorkConn(name string, workConn net.Conn, m *msg.StartWorkConn) {
  75. pm.mu.RLock()
  76. pw, ok := pm.proxies[name]
  77. pm.mu.RUnlock()
  78. if ok {
  79. pw.InWorkConn(workConn, m)
  80. } else {
  81. workConn.Close()
  82. }
  83. }
  84. func (pm *Manager) HandleEvent(payload interface{}) error {
  85. var m msg.Message
  86. switch e := payload.(type) {
  87. case *event.StartProxyPayload:
  88. m = e.NewProxyMsg
  89. case *event.CloseProxyPayload:
  90. m = e.CloseProxyMsg
  91. default:
  92. return event.ErrPayloadType
  93. }
  94. return pm.msgTransporter.Send(m)
  95. }
  96. func (pm *Manager) GetAllProxyStatus() []*WorkingStatus {
  97. ps := make([]*WorkingStatus, 0)
  98. pm.mu.RLock()
  99. defer pm.mu.RUnlock()
  100. for _, pxy := range pm.proxies {
  101. ps = append(ps, pxy.GetStatus())
  102. }
  103. return ps
  104. }
  105. func (pm *Manager) GetProxyStatus(name string) (*WorkingStatus, bool) {
  106. pm.mu.RLock()
  107. defer pm.mu.RUnlock()
  108. if pxy, ok := pm.proxies[name]; ok {
  109. return pxy.GetStatus(), true
  110. }
  111. return nil, false
  112. }
  113. func (pm *Manager) UpdateAll(proxyCfgs []v1.ProxyConfigurer) {
  114. xl := xlog.FromContextSafe(pm.ctx)
  115. proxyCfgsMap := lo.KeyBy(proxyCfgs, func(c v1.ProxyConfigurer) string {
  116. return c.GetBaseConfig().Name
  117. })
  118. pm.mu.Lock()
  119. defer pm.mu.Unlock()
  120. delPxyNames := make([]string, 0)
  121. for name, pxy := range pm.proxies {
  122. del := false
  123. cfg, ok := proxyCfgsMap[name]
  124. if !ok || !reflect.DeepEqual(pxy.Cfg, cfg) {
  125. del = true
  126. }
  127. if del {
  128. delPxyNames = append(delPxyNames, name)
  129. delete(pm.proxies, name)
  130. pxy.Stop()
  131. }
  132. }
  133. if len(delPxyNames) > 0 {
  134. xl.Infof("proxy removed: %s", delPxyNames)
  135. }
  136. addPxyNames := make([]string, 0)
  137. for _, cfg := range proxyCfgs {
  138. name := cfg.GetBaseConfig().Name
  139. if _, ok := pm.proxies[name]; !ok {
  140. pxy := NewWrapper(pm.ctx, cfg, pm.clientCfg, pm.HandleEvent, pm.msgTransporter)
  141. if pm.inWorkConnCallback != nil {
  142. pxy.SetInWorkConnCallback(pm.inWorkConnCallback)
  143. }
  144. pm.proxies[name] = pxy
  145. addPxyNames = append(addPxyNames, name)
  146. pxy.Start()
  147. }
  148. }
  149. if len(addPxyNames) > 0 {
  150. xl.Infof("proxy added: %s", addPxyNames)
  151. }
  152. }