Compare commits

...

3 Commits

2 changed files with 49 additions and 20 deletions

14
maintainers.yaml Normal file
View File

@ -0,0 +1,14 @@
identifier: grain
maintainers:
- npub1zmc6qyqdfnllhnzzxr5wpepfpnzcf8q6m3jdveflmgruqvd3qa9sjv7f60
relays:
- wss://wheat.happytavern.co/
- wss://purplepag.es/
- wss://relay.nostr.band/
- wss://relay.primal.net/
- wss://nos.lol/
- wss://offchain.pub/
- wss://relay.damus.io/
- wss://relay.0xchat.com/
- wss://nostr.happytavern.co/
- wss://relay.snort.social/

View File

@ -44,66 +44,81 @@ func HandleEvent(ws *websocket.Conn, message []interface{}) {
return return
} }
// Signature check moved here
if !utils.CheckSignature(evt) {
response.SendOK(ws, evt.ID, false, "invalid: signature verification failed")
return
}
eventSize := len(eventBytes) // Calculate event size eventSize := len(eventBytes) // Calculate event size
HandleKind(context.TODO(), evt, ws, eventSize)
if !handleBlacklistAndWhitelist(ws, evt) {
return
}
if !handleRateAndSizeLimits(ws, evt, eventSize) {
return
}
storeEvent(context.TODO(), evt, ws)
fmt.Println("Event processed:", evt.ID) fmt.Println("Event processed:", evt.ID)
}) })
} }
func HandleKind(ctx context.Context, evt relay.Event, ws *websocket.Conn, eventSize int) { func handleBlacklistAndWhitelist(ws *websocket.Conn, evt relay.Event) bool {
if !utils.CheckSignature(evt) {
response.SendOK(ws, evt.ID, false, "invalid: signature verification failed")
return
}
collection := db.GetCollection(evt.Kind)
rateLimiter := config.GetRateLimiter()
sizeLimiter := config.GetSizeLimiter()
if config.GetConfig().DomainWhitelist.Enabled { if config.GetConfig().DomainWhitelist.Enabled {
domains := config.GetConfig().DomainWhitelist.Domains domains := config.GetConfig().DomainWhitelist.Domains
pubkeys, err := utils.FetchPubkeysFromDomains(domains) pubkeys, err := utils.FetchPubkeysFromDomains(domains)
if err != nil { if err != nil {
fmt.Println("Error fetching pubkeys from domains:", err) fmt.Println("Error fetching pubkeys from domains:", err)
response.SendNotice(ws, "", "Error fetching pubkeys from domains") response.SendNotice(ws, "", "Error fetching pubkeys from domains")
return return false
} }
for _, pubkey := range pubkeys { for _, pubkey := range pubkeys {
config.GetConfig().PubkeyWhitelist.Pubkeys = append(config.GetConfig().PubkeyWhitelist.Pubkeys, pubkey) config.GetConfig().PubkeyWhitelist.Pubkeys = append(config.GetConfig().PubkeyWhitelist.Pubkeys, pubkey)
} }
} }
// Check against manual blacklist
if blacklisted, msg := utils.CheckBlacklist(evt.PubKey, evt.Content); blacklisted { if blacklisted, msg := utils.CheckBlacklist(evt.PubKey, evt.Content); blacklisted {
response.SendOK(ws, evt.ID, false, msg) response.SendOK(ws, evt.ID, false, msg)
return return false
} }
// Check if the kind is whitelisted
if config.GetConfig().KindWhitelist.Enabled && !utils.IsKindWhitelisted(evt.Kind) { if config.GetConfig().KindWhitelist.Enabled && !utils.IsKindWhitelisted(evt.Kind) {
response.SendOK(ws, evt.ID, false, "not allowed: event kind is not whitelisted") response.SendOK(ws, evt.ID, false, "not allowed: event kind is not whitelisted")
return return false
} }
// Check pubkey/npub whitelist only if the kind is not whitelisted
if config.GetConfig().PubkeyWhitelist.Enabled && !utils.IsPubKeyWhitelisted(evt.PubKey) { if config.GetConfig().PubkeyWhitelist.Enabled && !utils.IsPubKeyWhitelisted(evt.PubKey) {
response.SendOK(ws, evt.ID, false, "not allowed: pubkey or npub is not whitelisted") response.SendOK(ws, evt.ID, false, "not allowed: pubkey or npub is not whitelisted")
return return false
} }
return true
}
func handleRateAndSizeLimits(ws *websocket.Conn, evt relay.Event, eventSize int) bool {
rateLimiter := config.GetRateLimiter()
sizeLimiter := config.GetSizeLimiter()
category := determineCategory(evt.Kind) category := determineCategory(evt.Kind)
if allowed, msg := rateLimiter.AllowEvent(evt.Kind, category); !allowed { if allowed, msg := rateLimiter.AllowEvent(evt.Kind, category); !allowed {
response.SendOK(ws, evt.ID, false, msg) response.SendOK(ws, evt.ID, false, msg)
return return false
} }
if allowed, msg := sizeLimiter.AllowSize(evt.Kind, eventSize); !allowed { if allowed, msg := sizeLimiter.AllowSize(evt.Kind, eventSize); !allowed {
response.SendOK(ws, evt.ID, false, msg) response.SendOK(ws, evt.ID, false, msg)
return return false
} }
return true
}
func storeEvent(ctx context.Context, evt relay.Event, ws *websocket.Conn) {
collection := db.GetCollection(evt.Kind)
var err error var err error
switch { switch {
case evt.Kind == 0: case evt.Kind == 0: