Files

44 lines
948 B
Go

package egressutil
import (
"fmt"
"strings"
)
type ScopeTarget struct {
Scope string
Source string
SourceID string
}
func ParseScope(raw string, sanitizeID func(string) string) (ScopeTarget, error) {
scope := strings.ToLower(strings.TrimSpace(raw))
switch {
case scope == "adguardvpn":
return ScopeTarget{
Scope: "adguardvpn",
Source: "adguardvpn",
}, nil
case scope == "system":
return ScopeTarget{
Scope: "system",
Source: "system",
}, nil
case strings.HasPrefix(scope, "transport:"):
id := strings.TrimSpace(strings.TrimPrefix(scope, "transport:"))
if sanitizeID != nil {
id = sanitizeID(id)
}
if id == "" {
return ScopeTarget{}, fmt.Errorf("invalid transport scope id")
}
return ScopeTarget{
Scope: "transport:" + id,
Source: "transport",
SourceID: id,
}, nil
default:
return ScopeTarget{}, fmt.Errorf("invalid scope, expected adguardvpn|system|transport:<id>")
}
}