From 2ef7d4fe423002bfc4b478b61e5ef1579870220f Mon Sep 17 00:00:00 2001 From: Chris kerr Date: Sun, 4 Aug 2024 14:02:53 -0400 Subject: [PATCH] added NIP05 domains to whitelist --- app/static/examples/config.example.yml | 4 +++ config/types/serverConfig.go | 1 + config/types/whitelistDomainConfig.go | 6 ++++ server/handlers/event.go | 13 +++++++ server/utils/fetchPubkeysFromDomain.go | 48 ++++++++++++++++++++++++++ 5 files changed, 72 insertions(+) create mode 100644 config/types/whitelistDomainConfig.go create mode 100644 server/utils/fetchPubkeysFromDomain.go diff --git a/app/static/examples/config.example.yml b/app/static/examples/config.example.yml index 6b0a239..3c0370d 100644 --- a/app/static/examples/config.example.yml +++ b/app/static/examples/config.example.yml @@ -12,6 +12,10 @@ pubkey_whitelist: kind_whitelist: enabled: false kinds: #[0, 1] + #If pubkey_whitelist not enabled, domain_whitelist will be ignored +domain_whitelist: + enabled: false + domains: #["happytavern.co", "nostrplebs.com"] rate_limit: ws_limit: 100 # Global rate limit for WebSocket messages (50 messages per second) ws_burst: 200 # Global burst limit for WebSocket messages (allows a burst of 100 messages) diff --git a/config/types/serverConfig.go b/config/types/serverConfig.go index 5b5d23b..2602445 100644 --- a/config/types/serverConfig.go +++ b/config/types/serverConfig.go @@ -11,4 +11,5 @@ type ServerConfig struct { RateLimit RateLimitConfig `yaml:"rate_limit"` PubkeyWhitelist PubkeyWhitelistConfig `yaml:"pubkey_whitelist"` KindWhitelist KindWhitelistConfig `yaml:"kind_whitelist"` + DomainWhitelist DomainWhitelistConfig `yaml:"domain_whitelist"` } diff --git a/config/types/whitelistDomainConfig.go b/config/types/whitelistDomainConfig.go new file mode 100644 index 0000000..74e21f4 --- /dev/null +++ b/config/types/whitelistDomainConfig.go @@ -0,0 +1,6 @@ +package config + +type DomainWhitelistConfig struct { + Enabled bool `yaml:"enabled"` + Domains []string `yaml:"domains"` +} diff --git a/server/handlers/event.go b/server/handlers/event.go index d9474fe..5644166 100644 --- a/server/handlers/event.go +++ b/server/handlers/event.go @@ -60,6 +60,19 @@ func HandleKind(ctx context.Context, evt relay.Event, ws *websocket.Conn, eventS rateLimiter := config.GetRateLimiter() sizeLimiter := config.GetSizeLimiter() + if config.GetConfig().DomainWhitelist.Enabled { + domains := config.GetConfig().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 + } + for _, pubkey := range pubkeys { + config.GetConfig().PubkeyWhitelist.Pubkeys = append(config.GetConfig().PubkeyWhitelist.Pubkeys, pubkey) + } + } + // Check if the kind is whitelisted if config.GetConfig().KindWhitelist.Enabled && !isKindWhitelisted(evt.Kind) { response.SendOK(ws, evt.ID, false, "not allowed: event kind is not whitelisted") diff --git a/server/utils/fetchPubkeysFromDomain.go b/server/utils/fetchPubkeysFromDomain.go new file mode 100644 index 0000000..01f0c70 --- /dev/null +++ b/server/utils/fetchPubkeysFromDomain.go @@ -0,0 +1,48 @@ +package utils + +import ( + "encoding/json" + "fmt" + "io" + "net/http" +) + +type NostrJSON struct { + Names map[string]string `json:"names"` +} + +func FetchPubkeysFromDomains(domains []string) ([]string, error) { + var pubkeys []string + for _, domain := range domains { + url := fmt.Sprintf("https://%s/.well-known/nostr.json", domain) + resp, err := http.Get(url) + if err != nil { + fmt.Println("Error fetching nostr.json from domain:", domain, err) + continue + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + fmt.Println("Invalid response from domain:", domain, resp.Status) + continue + } + + body, err := io.ReadAll(resp.Body) + if err != nil { + fmt.Println("Error reading response body from domain:", domain, err) + continue + } + + var nostrData NostrJSON + err = json.Unmarshal(body, &nostrData) + if err != nil { + fmt.Println("Error unmarshaling JSON from domain:", domain, err) + continue + } + + for _, pubkey := range nostrData.Names { + pubkeys = append(pubkeys, pubkey) + } + } + return pubkeys, nil +}