mirror of
https://github.com/0ceanSlim/grain.git
synced 2024-11-21 16:17:13 +00:00
mutelist blacklist at event handler
This commit is contained in:
parent
21c431dd22
commit
3d88938b7e
@ -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)
|
||||
}
|
11
main.go
11
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:
|
||||
|
@ -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")
|
||||
|
Loading…
Reference in New Issue
Block a user