mutelist blacklist at event handler

This commit is contained in:
0ceanSlim 2024-10-17 17:05:33 -04:00
parent 21c431dd22
commit 3d88938b7e
3 changed files with 52 additions and 42 deletions

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
types "grain/config/types" types "grain/config/types"
"grain/server/utils" "grain/server/utils"
"io"
"log" "log"
"os" "os"
"strings" "strings"
@ -273,13 +274,27 @@ func FetchPubkeysFromLocalMuteList(localRelayURL string, muteListEventIDs []stri
if err != nil { if err != nil {
log.Printf("Failed to marshal close request: %v", err) log.Printf("Failed to marshal close request: %v", err)
} else { } else {
err = conn.WriteMessage(websocket.TextMessage, closeReqJSON) if err = conn.WriteMessage(websocket.TextMessage, closeReqJSON); err != nil {
if err != nil {
log.Printf("Failed to send close request to relay %s: %v", localRelayURL, err) log.Printf("Failed to send close request to relay %s: %v", localRelayURL, err)
} else { } else {
log.Println("Sent CLOSE request to end subscription.") log.Println("Sent CLOSE request to end subscription.")
// Wait for a potential response or timeout
conn.SetReadDeadline(time.Now().Add(500 * time.Millisecond))
_, _, err = conn.ReadMessage()
if err != nil {
if err == io.EOF {
log.Println("Connection closed by the server after CLOSE request (EOF)")
} else if websocket.IsCloseError(err, websocket.CloseNormalClosure, websocket.CloseGoingAway) {
log.Println("WebSocket closed normally after CLOSE request")
} else {
log.Printf("Unexpected error after CLOSE request: %v", err)
} }
} }
}
}
// Ensure we break the loop after handling EOSE
break break
} }
@ -343,32 +358,3 @@ func extractPubkeysFromMuteListEvent(eventData map[string]interface{}) []string
log.Printf("Extracted pubkeys: %v", pubkeys) log.Printf("Extracted pubkeys: %v", pubkeys)
return pubkeys return pubkeys
} }
// AppendFetchedPubkeysToBlacklist fetches pubkeys from the local relay and appends them to the blacklist.
func AppendFetchedPubkeysToBlacklist() error {
// Get the server configuration to determine the local relay URL.
cfg := GetConfig()
if cfg == nil {
return fmt.Errorf("server configuration is not loaded")
}
blacklistCfg := GetBlacklistConfig()
if blacklistCfg == nil {
return fmt.Errorf("blacklist configuration is not loaded")
}
// Construct the local relay WebSocket URL using the configured port.
localRelayURL := fmt.Sprintf("ws://localhost%s", cfg.Server.Port)
// Fetch pubkeys from the mute list events.
pubkeys, err := FetchPubkeysFromLocalMuteList(localRelayURL, blacklistCfg.MuteListEventIDs)
if err != nil {
return fmt.Errorf("failed to fetch pubkeys from mute list: %v", err)
}
// Add the fetched pubkeys to the permanent blacklist.
blacklistCfg.PermanentBlacklistPubkeys = append(blacklistCfg.PermanentBlacklistPubkeys, pubkeys...)
// Save the updated blacklist configuration.
return saveBlacklistConfig(*blacklistCfg)
}

11
main.go
View File

@ -72,17 +72,6 @@ func main() {
mux := setupRoutes() mux := setupRoutes()
server := startServer(cfg, mux, &wg) server := startServer(cfg, mux, &wg)
// Append pubkeys from mute list events to the blacklist after the server starts.
go func() {
// Sleep for a short time to ensure the server is fully up.
time.Sleep(2 * time.Second)
err := config.AppendFetchedPubkeysToBlacklist()
if err != nil {
log.Printf("Failed to update blacklist: %v", err)
}
}()
// Monitor for server restart or shutdown signals. // Monitor for server restart or shutdown signals.
select { select {
case <-restartChan: case <-restartChan:

View File

@ -95,6 +95,41 @@ func handleBlacklistAndWhitelist(ws *websocket.Conn, evt nostr.Event) bool {
return false 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.MuteListEventIDs) > 0 {
localRelayURL := fmt.Sprintf("ws://localhost%s", cfg.Server.Port)
mutelistedPubkeys, err := config.FetchPubkeysFromLocalMuteList(localRelayURL, blacklistCfg.MuteListEventIDs)
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 // Check if the event's kind is whitelisted
if whitelistCfg.KindWhitelist.Enabled && !config.IsKindWhitelisted(evt.Kind) { if whitelistCfg.KindWhitelist.Enabled && !config.IsKindWhitelisted(evt.Kind) {
response.SendOK(ws, evt.ID, false, "not allowed: event kind is not whitelisted") response.SendOK(ws, evt.ID, false, "not allowed: event kind is not whitelisted")