41 lines
1022 B
Go
41 lines
1022 B
Go
package hold
|
|
|
|
import (
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
// matchPattern checks if a handle matches a pattern
|
|
// Supports wildcards: "*" (all), "*.domain.com" (suffix), "prefix.*" (prefix), "*.mid.*" (contains)
|
|
func matchPattern(pattern, handle string) bool {
|
|
if pattern == "*" {
|
|
// Wildcard matches all
|
|
return true
|
|
}
|
|
|
|
// Convert glob to regex and match
|
|
regex := globToRegex(pattern)
|
|
matched, err := regexp.MatchString(regex, handle)
|
|
if err != nil {
|
|
// Log error but fail closed (don't grant access on regex error)
|
|
return false
|
|
}
|
|
return matched
|
|
}
|
|
|
|
// globToRegex converts a glob pattern to a regex pattern
|
|
// Examples:
|
|
// - "*.example.com" → "^.*\.example\.com$"
|
|
// - "subdomain.*" → "^subdomain\..*$"
|
|
// - "*.bsky.*" → "^.*\.bsky\..*$"
|
|
func globToRegex(pattern string) string {
|
|
// Escape special regex characters (except *)
|
|
escaped := regexp.QuoteMeta(pattern)
|
|
|
|
// Replace escaped \* with .*
|
|
regex := strings.ReplaceAll(escaped, "\\*", ".*")
|
|
|
|
// Anchor to start and end
|
|
return "^" + regex + "$"
|
|
}
|