| 1 | // Copyright 2026 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 | |
| 15 | package source |
| 16 | |
| 17 | import v1 "github.com/fatedier/frp/pkg/config/v1" |
| 18 | |
| 19 | // ConfigSource implements Source for in-memory configuration. |
| 20 | // All operations are thread-safe. |
| 21 | type ConfigSource struct { |
| 22 | baseSource |
| 23 | } |
| 24 | |
| 25 | func NewConfigSource() *ConfigSource { |
| 26 | return &ConfigSource{ |
| 27 | baseSource: newBaseSource(), |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | // ReplaceAll replaces all proxy and visitor configurations atomically. |
| 32 | func (s *ConfigSource) ReplaceAll(proxies []v1.ProxyConfigurer, visitors []v1.VisitorConfigurer) error { |
| 33 | s.mu.Lock() |
| 34 | defer s.mu.Unlock() |
| 35 | |
| 36 | nextProxies := make(map[string]v1.ProxyConfigurer, len(proxies)) |
| 37 | for _, p := range proxies { |
| 38 | name, err := validateProxyName(p) |
| 39 | if err != nil { |
| 40 | return err |
| 41 | } |
| 42 | nextProxies[name] = p |
| 43 | } |
| 44 | nextVisitors := make(map[string]v1.VisitorConfigurer, len(visitors)) |
| 45 | for _, v := range visitors { |
| 46 | name, err := validateVisitorName(v) |
| 47 | if err != nil { |
| 48 | return err |
| 49 | } |
| 50 | nextVisitors[name] = v |
| 51 | } |
| 52 | s.proxies = nextProxies |
| 53 | s.visitors = nextVisitors |
| 54 | return nil |
| 55 | } |