added NIP05 domains to whitelist

This commit is contained in:
Chris kerr 2024-08-04 14:02:53 -04:00
parent 2ef15d6e17
commit 2ef7d4fe42
5 changed files with 72 additions and 0 deletions

View File

@ -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)

View File

@ -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"`
}

View File

@ -0,0 +1,6 @@
package config
type DomainWhitelistConfig struct {
Enabled bool `yaml:"enabled"`
Domains []string `yaml:"domains"`
}

View File

@ -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")

View File

@ -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
}