From c51199737b845fe6b67ea4912d9fc327fe20ec39 Mon Sep 17 00:00:00 2001 From: 0ceanSlim Date: Fri, 8 Nov 2024 16:35:54 -0500 Subject: [PATCH] check white/blacklist refactored --- config/Blacklist.go | 25 ++++++++++++++ config/Whitelist.go | 33 +++++++++++++++++++ server/handlers/event.go | 71 +++------------------------------------- 3 files changed, 63 insertions(+), 66 deletions(-) diff --git a/config/Blacklist.go b/config/Blacklist.go index b4320a7..f9207d4 100644 --- a/config/Blacklist.go +++ b/config/Blacklist.go @@ -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, "" } diff --git a/config/Whitelist.go b/config/Whitelist.go index 9022421..73be755 100644 --- a/config/Whitelist.go +++ b/config/Whitelist.go @@ -2,10 +2,43 @@ package config import ( "fmt" + nostr "grain/server/types" "grain/server/utils" "strconv" ) +// CheckWhitelist checks if an event meets the whitelist criteria. +func CheckWhitelist(evt nostr.Event) (bool, string) { + // Get the current whitelist configuration + whitelistCfg := GetWhitelistConfig() + if whitelistCfg == nil { + return false, "Internal server error: whitelist configuration is missing" + } + + // If domain whitelisting is enabled, fetch pubkeys from domains + if whitelistCfg.DomainWhitelist.Enabled { + domains := whitelistCfg.DomainWhitelist.Domains + pubkeys, err := utils.FetchPubkeysFromDomains(domains) + if err != nil { + return false, "Error fetching pubkeys from domains" + } + // Update the whitelisted pubkeys dynamically + whitelistCfg.PubkeyWhitelist.Pubkeys = append(whitelistCfg.PubkeyWhitelist.Pubkeys, pubkeys...) + } + + // 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, "" +} + // Check if a pubkey or npub is whitelisted func IsPubKeyWhitelisted(pubKey string) bool { cfg := GetWhitelistConfig() diff --git a/server/handlers/event.go b/server/handlers/event.go index 95b8c85..060e407 100644 --- a/server/handlers/event.go +++ b/server/handlers/event.go @@ -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 }