Compare commits

..

2 Commits

Author SHA1 Message Date
4fef7088bb chekwhitelist refactored 2024-11-08 16:57:35 -05:00
c51199737b check white/blacklist refactored 2024-11-08 16:35:54 -05:00
3 changed files with 76 additions and 72 deletions

View File

@ -59,6 +59,31 @@ func CheckBlacklist(pubkey, eventContent string) (bool, string) {
}
}
// Check mutelist blacklist
if len(blacklistConfig.MuteListAuthors) > 0 {
cfg := GetConfig()
if cfg == nil {
log.Println("Server configuration is not loaded")
return true, "Internal server error: server configuration is missing"
}
localRelayURL := fmt.Sprintf("ws://localhost%s", cfg.Server.Port)
mutelistedPubkeys, err := FetchPubkeysFromLocalMuteList(localRelayURL, blacklistConfig.MuteListAuthors)
if err != nil {
log.Printf("Error fetching pubkeys from mutelist: %v", err)
return true, "Error fetching pubkeys from mutelist"
}
for _, mutelistedPubkey := range mutelistedPubkeys {
if pubkey == mutelistedPubkey {
log.Printf("Pubkey %s is in the mutelist", pubkey)
return true, "not allowed: pubkey is in mutelist"
}
}
} else {
log.Println("No mutelist event IDs specified in the blacklist configuration")
}
return false, ""
}

View File

@ -2,27 +2,51 @@ package config
import (
"fmt"
nostr "grain/server/types"
"grain/server/utils"
"log"
"strconv"
)
// Check if a pubkey or npub is whitelisted
func IsPubKeyWhitelisted(pubKey string) bool {
cfg := GetWhitelistConfig()
if !cfg.PubkeyWhitelist.Enabled {
return true
// CheckWhitelist checks if an event meets the whitelist criteria.
func CheckWhitelist(evt nostr.Event) (bool, string) {
whitelistCfg := GetWhitelistConfig()
if whitelistCfg == nil {
return false, "Internal server error: whitelist configuration is missing"
}
// Check if the event's kind is whitelisted
if whitelistCfg.KindWhitelist.Enabled && !IsKindWhitelisted(evt.Kind) {
return false, "not allowed: event kind is not whitelisted"
}
// Check if the event's pubkey is whitelisted
if whitelistCfg.PubkeyWhitelist.Enabled && !IsPubKeyWhitelisted(evt.PubKey) {
return false, "not allowed: pubkey or npub is not whitelisted"
}
return true, ""
}
// IsPubKeyWhitelisted checks if a pubkey or npub is whitelisted, considering pubkeys from domains.
func IsPubKeyWhitelisted(pubKey string) bool {
cfg := GetWhitelistConfig()
if cfg == nil || !cfg.PubkeyWhitelist.Enabled {
return true // Whitelisting is not enforced if the configuration is missing or disabled
}
// Check statically defined pubkeys
for _, whitelistedKey := range cfg.PubkeyWhitelist.Pubkeys {
if pubKey == whitelistedKey {
return true
}
}
// Check statically defined npubs after decoding them to pubkeys
for _, npub := range cfg.PubkeyWhitelist.Npubs {
decodedPubKey, err := utils.DecodeNpub(npub)
if err != nil {
fmt.Println("Error decoding npub:", err)
log.Printf("Error decoding npub: %v", err)
continue
}
if pubKey == decodedPubKey {
@ -30,6 +54,22 @@ func IsPubKeyWhitelisted(pubKey string) bool {
}
}
// Fetch and check pubkeys from domains if domain whitelisting is enabled
if cfg.DomainWhitelist.Enabled {
domains := cfg.DomainWhitelist.Domains
pubkeys, err := utils.FetchPubkeysFromDomains(domains)
if err != nil {
log.Printf("Error fetching pubkeys from domains: %v", err)
return false // Consider returning true or handling based on your application's needs
}
for _, domainPubKey := range pubkeys {
if pubKey == domainPubKey {
return true
}
}
}
return false
}

View File

@ -106,77 +106,16 @@ func validateEventTimestamp(evt nostr.Event) bool {
}
func handleBlacklistAndWhitelist(ws *websocket.Conn, evt nostr.Event) bool {
// Get the current whitelist configuration
whitelistCfg := config.GetWhitelistConfig()
if whitelistCfg == nil {
fmt.Println("Whitelist configuration is not loaded.")
response.SendNotice(ws, "", "Internal server error: whitelist configuration is missing")
return false
}
// If domain whitelisting is enabled, dynamically fetch pubkeys from domains
if whitelistCfg.DomainWhitelist.Enabled {
domains := whitelistCfg.DomainWhitelist.Domains
pubkeys, err := utils.FetchPubkeysFromDomains(domains)
if err != nil {
fmt.Println("Error fetching pubkeys from domains:", err)
response.SendNotice(ws, "", "Error fetching pubkeys from domains")
return false
}
// Update the whitelisted pubkeys dynamically
whitelistCfg.PubkeyWhitelist.Pubkeys = append(whitelistCfg.PubkeyWhitelist.Pubkeys, pubkeys...)
}
// Check if the event's pubkey or content is blacklisted
// Use the updated CheckBlacklist function
if blacklisted, msg := config.CheckBlacklist(evt.PubKey, evt.Content); blacklisted {
response.SendOK(ws, evt.ID, false, msg)
return false
}
// Check mutelist blacklist
cfg := config.GetConfig()
if cfg == nil {
fmt.Println("Server configuration is not loaded")
response.SendNotice(ws, "", "Internal server error: server configuration is missing")
return false
}
blacklistCfg := config.GetBlacklistConfig()
if blacklistCfg == nil {
fmt.Println("Blacklist configuration is not loaded")
response.SendNotice(ws, "", "Internal server error: blacklist configuration is missing")
return false
}
// Only proceed if there are mutelist event IDs specified
if len(blacklistCfg.MuteListAuthors) > 0 {
localRelayURL := fmt.Sprintf("ws://localhost%s", cfg.Server.Port)
mutelistedPubkeys, err := config.FetchPubkeysFromLocalMuteList(localRelayURL, blacklistCfg.MuteListAuthors)
if err != nil {
fmt.Println("Error fetching pubkeys from mutelist:", err)
response.SendNotice(ws, "", "Error fetching pubkeys from mutelist")
return false
}
for _, mutelistedPubkey := range mutelistedPubkeys {
if evt.PubKey == mutelistedPubkey {
response.SendOK(ws, evt.ID, false, "not allowed: pubkey is in mutelist")
return false
}
}
} else {
fmt.Println("No mutelist event IDs specified in the blacklist configuration")
}
// Check if the event's kind is whitelisted
if whitelistCfg.KindWhitelist.Enabled && !config.IsKindWhitelisted(evt.Kind) {
response.SendOK(ws, evt.ID, false, "not allowed: event kind is not whitelisted")
return false
}
// Check if the event's pubkey is whitelisted
if whitelistCfg.PubkeyWhitelist.Enabled && !config.IsPubKeyWhitelisted(evt.PubKey) {
response.SendOK(ws, evt.ID, false, "not allowed: pubkey or npub is not whitelisted")
// Check the whitelist using CheckWhitelist function
isWhitelisted, msg := config.CheckWhitelist(evt)
if !isWhitelisted {
response.SendOK(ws, evt.ID, false, msg)
return false
}