Compare commits

..

No commits in common. "4fef7088bb603cd76d632a5e327b6a4afbcd60e3" and "f1511a9b8be5e8008977e4fe2d58285af0c5b0fc" have entirely different histories.

3 changed files with 70 additions and 74 deletions

View File

@ -59,31 +59,6 @@ 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, "" return false, ""
} }

View File

@ -2,51 +2,27 @@ package config
import ( import (
"fmt" "fmt"
nostr "grain/server/types"
"grain/server/utils" "grain/server/utils"
"log"
"strconv" "strconv"
) )
// CheckWhitelist checks if an event meets the whitelist criteria. // Check if a pubkey or npub is whitelisted
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 { func IsPubKeyWhitelisted(pubKey string) bool {
cfg := GetWhitelistConfig() cfg := GetWhitelistConfig()
if cfg == nil || !cfg.PubkeyWhitelist.Enabled { if !cfg.PubkeyWhitelist.Enabled {
return true // Whitelisting is not enforced if the configuration is missing or disabled return true
} }
// Check statically defined pubkeys
for _, whitelistedKey := range cfg.PubkeyWhitelist.Pubkeys { for _, whitelistedKey := range cfg.PubkeyWhitelist.Pubkeys {
if pubKey == whitelistedKey { if pubKey == whitelistedKey {
return true return true
} }
} }
// Check statically defined npubs after decoding them to pubkeys
for _, npub := range cfg.PubkeyWhitelist.Npubs { for _, npub := range cfg.PubkeyWhitelist.Npubs {
decodedPubKey, err := utils.DecodeNpub(npub) decodedPubKey, err := utils.DecodeNpub(npub)
if err != nil { if err != nil {
log.Printf("Error decoding npub: %v", err) fmt.Println("Error decoding npub:", err)
continue continue
} }
if pubKey == decodedPubKey { if pubKey == decodedPubKey {
@ -54,22 +30,6 @@ 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 return false
} }

View File

@ -106,16 +106,77 @@ func validateEventTimestamp(evt nostr.Event) bool {
} }
func handleBlacklistAndWhitelist(ws *websocket.Conn, evt nostr.Event) bool { func handleBlacklistAndWhitelist(ws *websocket.Conn, evt nostr.Event) bool {
// Use the updated CheckBlacklist function // 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
if blacklisted, msg := config.CheckBlacklist(evt.PubKey, evt.Content); blacklisted { if blacklisted, msg := config.CheckBlacklist(evt.PubKey, evt.Content); blacklisted {
response.SendOK(ws, evt.ID, false, msg) response.SendOK(ws, evt.ID, false, msg)
return false return false
} }
// Check the whitelist using CheckWhitelist function // Check mutelist blacklist
isWhitelisted, msg := config.CheckWhitelist(evt) cfg := config.GetConfig()
if !isWhitelisted { if cfg == nil {
response.SendOK(ws, evt.ID, false, msg) 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")
return false return false
} }