Files
gemini-grc/common/whiteList/whitelist.go
antanst 7d27e5a123 Add whitelist functionality
- Implement whitelist package for filtering URLs
- Support pattern matching for allowed URLs
- Add URL validation against whitelist patterns
- Include test cases for whitelist functionality
2025-05-22 12:46:28 +03:00

75 lines
1.4 KiB
Go

package whiteList
import (
"fmt"
"os"
"regexp"
"strings"
"gemini-grc/config"
"gemini-grc/logging"
"git.antanst.com/antanst/xerrors"
)
var whitelist []regexp.Regexp //nolint:gochecknoglobals
func Initialize() error {
var err error
// Initialize whitelist
if config.CONFIG.WhitelistPath != "" {
if err = loadWhitelist(config.CONFIG.WhitelistPath); err != nil {
return err
}
}
return nil
}
func loadWhitelist(filePath string) error {
if whitelist != nil {
return nil
}
data, err := os.ReadFile(filePath)
if err != nil {
whitelist = []regexp.Regexp{}
return xerrors.NewError(fmt.Errorf("could not load whitelist file: %w", err), 0, "", true)
}
lines := strings.Split(string(data), "\n")
whitelist = []regexp.Regexp{}
for _, line := range lines {
line = strings.TrimSpace(line)
if line == "" || strings.HasPrefix(line, "#") {
continue
}
regex, err := regexp.Compile(line)
if err != nil {
return xerrors.NewError(fmt.Errorf("could not compile whitelist line %s: %w", line, err), 0, "", true)
}
whitelist = append(whitelist, *regex)
}
if len(whitelist) > 0 {
logging.LogInfo("Loaded %d whitelist entries", len(whitelist))
}
return nil
}
func Shutdown() error {
return nil
}
// IsWhitelisted checks if the URL matches any whitelist pattern
func IsWhitelisted(u string) bool {
for _, v := range whitelist {
if v.MatchString(u) {
return true
}
}
return false
}