mirror of
https://github.com/0ceanSlim/grain.git
synced 2024-11-21 16:17:13 +00:00
added NIP05 domains to whitelist
This commit is contained in:
parent
2ef15d6e17
commit
2ef7d4fe42
@ -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)
|
||||
|
@ -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"`
|
||||
}
|
||||
|
6
config/types/whitelistDomainConfig.go
Normal file
6
config/types/whitelistDomainConfig.go
Normal file
@ -0,0 +1,6 @@
|
||||
package config
|
||||
|
||||
type DomainWhitelistConfig struct {
|
||||
Enabled bool `yaml:"enabled"`
|
||||
Domains []string `yaml:"domains"`
|
||||
}
|
@ -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")
|
||||
|
48
server/utils/fetchPubkeysFromDomain.go
Normal file
48
server/utils/fetchPubkeysFromDomain.go
Normal 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
|
||||
}
|
Loading…
Reference in New Issue
Block a user