| // |
| 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 | "cmp" |
| 19 | "errors" |
| 20 | "fmt" |
| 21 | "maps" |
| 22 | "slices" |
| 23 | "sync" |
| 24 |
| 25 | v1 "github.com/fatedier/frp/pkg/config/v1" |
| 26 | ) |
| 27 |
| 28 | type Aggregator struct { |
| 29 | mu sync.RWMutex |
| 30 |
| 31 | configSource *ConfigSource |
| 32 | storeSource *StoreSource |
| 33 | } |
| 34 |
| 35 | func NewAggregator(configSource *ConfigSource) *Aggregator { |
| 36 | if configSource == nil { |
| 37 | configSource = NewConfigSource() |
| 38 | } |
| 39 | return &Aggregator{ |
| 40 | configSource: configSource, |
| 41 | } |
| 42 | } |
| 43 |
| 44 | func (a *Aggregator) SetStoreSource(storeSource *StoreSource) { |
| 45 | a.mu.Lock() |
| 46 | defer a.mu.Unlock() |
| 47 |
| 48 | a.storeSource = storeSource |
| 49 | } |
| 50 |
| 51 | func (a *Aggregator) ConfigSource() *ConfigSource { |
| 52 | return a.configSource |
| 53 | } |
| 54 |
| 55 | func (a *Aggregator) StoreSource() *StoreSource { |
| 56 | return a.storeSource |
| 57 | } |
| 58 |
| 59 | func (a *Aggregator) getSourcesLocked() []Source { |
| 60 | sources := make([]Source, 0, 2) |
| 61 | if a.configSource != nil { |
| 62 | sources = append(sources, a.configSource) |
| 63 | } |
| 64 | if a.storeSource != nil { |
| 65 | sources = append(sources, a.storeSource) |
| 66 | } |
| 67 | return sources |
| 68 | } |
| 69 |
| 70 | func (a *Aggregator) Load() ([]v1.ProxyConfigurer, []v1.VisitorConfigurer, error) { |
| 71 | a.mu.RLock() |
| 72 | entries := a.getSourcesLocked() |
| 73 | a.mu.RUnlock() |
| 74 |
| 75 | if len(entries) == 0 { |
| 76 | return nil, nil, errors.New("no sources configured") |
| 77 | } |
| 78 |
| 79 | proxyMap := make(map[string]v1.ProxyConfigurer) |
| 80 | visitorMap := make(map[string]v1.VisitorConfigurer) |
| 81 |
| 82 | for _, src := range entries { |
| 83 | proxies, visitors, err := src.Load() |
| 84 | if err != nil { |
| 85 | return nil, nil, fmt.Errorf("load source: %w", err) |
| 86 | } |
| 87 | for _, p := range proxies { |
| 88 | proxyMap[p.GetBaseConfig().Name] = p |
| 89 | } |
| 90 | for _, v := range visitors { |
| 91 | visitorMap[v.GetBaseConfig().Name] = v |
| 92 | } |
| 93 | } |
| 94 | proxies, visitors := a.mapsToSortedSlices(proxyMap, visitorMap) |
| 95 | return proxies, visitors, nil |
| 96 | } |
| 97 |
| 98 | func (a *Aggregator) mapsToSortedSlices( |
| 99 | proxyMap map[string]v1.ProxyConfigurer, |
| 100 | visitorMap map[string]v1.VisitorConfigurer, |
| 101 | ) ([]v1.ProxyConfigurer, []v1.VisitorConfigurer) { |
| 102 | proxies := slices.SortedFunc(maps.Values(proxyMap), func(x, y v1.ProxyConfigurer) int { |
| 103 | return cmp.Compare(x.GetBaseConfig().Name, y.GetBaseConfig().Name) |
| 104 | }) |
| 105 | visitors := slices.SortedFunc(maps.Values(visitorMap), func(x, y v1.VisitorConfigurer) int { |
| 106 | return cmp.Compare(x.GetBaseConfig().Name, y.GetBaseConfig().Name) |
| 107 | }) |
| 108 | return proxies, visitors |
| 109 | } |