| // |
| 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 |
| 15 | package source |
| 16 |
| 17 | import ( |
| 18 | "sync" |
| 19 |
| 20 | v1 "github.com/fatedier/frp/pkg/config/v1" |
| 21 | ) |
| 22 |
| 23 | // baseSource provides shared state and behavior for Source implementations. |
| 24 | // It manages proxy/visitor storage. |
| 25 | // Concrete types (ConfigSource, StoreSource) embed this struct. |
| 26 | type baseSource struct { |
| 27 | mu sync.RWMutex |
| 28 |
| 29 | proxies map[string]v1.ProxyConfigurer |
| 30 | visitors map[string]v1.VisitorConfigurer |
| 31 | } |
| 32 |
| 33 | func newBaseSource() baseSource { |
| 34 | return baseSource{ |
| 35 | proxies: make(map[string]v1.ProxyConfigurer), |
| 36 | visitors: make(map[string]v1.VisitorConfigurer), |
| 37 | } |
| 38 | } |
| 39 |
| 40 | // Load returns all enabled proxy and visitor configurations. |
| 41 | // Configurations with Enabled explicitly set to false are filtered out. |
| 42 | func (s *baseSource) Load() ([]v1.ProxyConfigurer, []v1.VisitorConfigurer, error) { |
| 43 | s.mu.RLock() |
| 44 | defer s.mu.RUnlock() |
| 45 |
| 46 | proxies := make([]v1.ProxyConfigurer, 0, len(s.proxies)) |
| 47 | for _, p := range s.proxies { |
| 48 | // Filter out disabled proxies (nil or true means enabled) |
| 49 | if enabled := p.GetBaseConfig().Enabled; enabled != nil && !*enabled { |
| 50 | continue |
| 51 | } |
| 52 | proxies = append(proxies, p) |
| 53 | } |
| 54 |
| 55 | visitors := make([]v1.VisitorConfigurer, 0, len(s.visitors)) |
| 56 | for _, v := range s.visitors { |
| 57 | // Filter out disabled visitors (nil or true means enabled) |
| 58 | if enabled := v.GetBaseConfig().Enabled; enabled != nil && !*enabled { |
| 59 | continue |
| 60 | } |
| 61 | visitors = append(visitors, v) |
| 62 | } |
| 63 |
| 64 | return cloneConfigurers(proxies, visitors) |
| 65 | } |