43 lines
1.2 KiB
Go
43 lines
1.2 KiB
Go
package automation
|
||
|
||
import "fmt"
|
||
|
||
// buildProxyGroups implements the "proxy-groups" ssm feature: for each rule,
|
||
// filter the merged proxy pool by rule.Match and append a new proxy-group
|
||
// containing the matched proxies' display names.
|
||
func buildProxyGroups(finalConfig map[string]interface{}, rules []ProxyGroupRule, proxies []ProxyRef) {
|
||
for _, rule := range rules {
|
||
matched := matchProxies(proxies, rule.Match)
|
||
|
||
names := make([]interface{}, 0, len(matched))
|
||
for _, p := range matched {
|
||
names = append(names, p.DisplayName)
|
||
}
|
||
if len(matched) == 0 {
|
||
fmt.Printf("ℹ️ ssm: proxy-group '%s' matched 0 proxies\n", rule.Name)
|
||
}
|
||
|
||
group := map[string]interface{}{
|
||
"name": rule.Name,
|
||
"type": defaultString(rule.Type, "select"),
|
||
"proxies": names,
|
||
}
|
||
for k, v := range rule.Extra {
|
||
group[k] = v
|
||
}
|
||
appendProxyGroup(finalConfig, group)
|
||
}
|
||
}
|
||
|
||
func defaultString(v, fallback string) string {
|
||
if v == "" {
|
||
return fallback
|
||
}
|
||
return v
|
||
}
|
||
|
||
func appendProxyGroup(finalConfig map[string]interface{}, group map[string]interface{}) {
|
||
existing, _ := finalConfig["proxy-groups"].([]interface{})
|
||
finalConfig["proxy-groups"] = append(existing, group)
|
||
}
|