From 3d88938b7e089cf9bd114da3d11b70ee03bc92d1 Mon Sep 17 00:00:00 2001 From: 0ceanSlim Date: Thu, 17 Oct 2024 17:05:33 -0400 Subject: [PATCH] mutelist blacklist at event handler --- config/Blacklist.go | 48 ++++++++++++++-------------------------- main.go | 11 --------- server/handlers/event.go | 35 +++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 42 deletions(-) diff --git a/config/Blacklist.go b/config/Blacklist.go index 99e70f2..faafc9b 100644 --- a/config/Blacklist.go +++ b/config/Blacklist.go @@ -5,6 +5,7 @@ import ( "fmt" types "grain/config/types" "grain/server/utils" + "io" "log" "os" "strings" @@ -273,13 +274,27 @@ func FetchPubkeysFromLocalMuteList(localRelayURL string, muteListEventIDs []stri if err != nil { log.Printf("Failed to marshal close request: %v", err) } else { - err = conn.WriteMessage(websocket.TextMessage, closeReqJSON) - if err != nil { + if err = conn.WriteMessage(websocket.TextMessage, closeReqJSON); err != nil { log.Printf("Failed to send close request to relay %s: %v", localRelayURL, err) } else { 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 } @@ -343,32 +358,3 @@ func extractPubkeysFromMuteListEvent(eventData map[string]interface{}) []string log.Printf("Extracted pubkeys: %v", 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) -} \ No newline at end of file diff --git a/main.go b/main.go index 9f90e87..22613b6 100644 --- a/main.go +++ b/main.go @@ -72,17 +72,6 @@ func main() { mux := setupRoutes() 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. select { case <-restartChan: diff --git a/server/handlers/event.go b/server/handlers/event.go index aca61bb..7485816 100644 --- a/server/handlers/event.go +++ b/server/handlers/event.go @@ -95,6 +95,41 @@ func handleBlacklistAndWhitelist(ws *websocket.Conn, evt nostr.Event) bool { 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 if whitelistCfg.KindWhitelist.Enabled && !config.IsKindWhitelisted(evt.Kind) { response.SendOK(ws, evt.ID, false, "not allowed: event kind is not whitelisted")