Compare commits

..

No commits in common. "0a52bebe14e2f2949397c44303c854bb0e3a17aa" and "e8b1380c9019078554d401310b0793f0c0f8584e" have entirely different histories.

2 changed files with 20 additions and 49 deletions

View File

@ -1,14 +0,0 @@
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,81 +44,66 @@ 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 handleBlacklistAndWhitelist(ws *websocket.Conn, evt relay.Event) bool { func HandleKind(ctx context.Context, evt relay.Event, ws *websocket.Conn, eventSize int) {
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 false return
} }
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 false return
} }
// 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 false return
} }
// 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 false return
} }
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 false return
} }
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 false return
} }
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: