mirror of
https://github.com/0ceanSlim/grain.git
synced 2024-11-21 16:17:13 +00:00
check white/blacklist refactored
This commit is contained in:
parent
f1511a9b8b
commit
c51199737b
@ -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, ""
|
return false, ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,10 +2,43 @@ package config
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
nostr "grain/server/types"
|
||||||
"grain/server/utils"
|
"grain/server/utils"
|
||||||
"strconv"
|
"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
|
// Check if a pubkey or npub is whitelisted
|
||||||
func IsPubKeyWhitelisted(pubKey string) bool {
|
func IsPubKeyWhitelisted(pubKey string) bool {
|
||||||
cfg := GetWhitelistConfig()
|
cfg := GetWhitelistConfig()
|
||||||
|
@ -106,77 +106,16 @@ func validateEventTimestamp(evt nostr.Event) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func handleBlacklistAndWhitelist(ws *websocket.Conn, evt nostr.Event) bool {
|
func handleBlacklistAndWhitelist(ws *websocket.Conn, evt nostr.Event) bool {
|
||||||
// Get the current whitelist configuration
|
// Use the updated CheckBlacklist function
|
||||||
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 mutelist blacklist
|
// Check the whitelist using CheckWhitelist function
|
||||||
cfg := config.GetConfig()
|
isWhitelisted, msg := config.CheckWhitelist(evt)
|
||||||
if cfg == nil {
|
if !isWhitelisted {
|
||||||
fmt.Println("Server configuration is not loaded")
|
response.SendOK(ws, evt.ID, false, msg)
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user