Files
ss/internal/automation/groups.go

43 lines
1.2 KiB
Go
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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)
}