mirror of
https://github.com/0ceanSlim/grain.git
synced 2024-11-23 17:07:13 +00:00
Compare commits
No commits in common. "0a52bebe14e2f2949397c44303c854bb0e3a17aa" and "e8b1380c9019078554d401310b0793f0c0f8584e" have entirely different histories.
0a52bebe14
...
e8b1380c90
@ -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/
|
@ -44,81 +44,66 @@ func HandleEvent(ws *websocket.Conn, message []interface{}) {
|
||||
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
|
||||
|
||||
if !handleBlacklistAndWhitelist(ws, evt) {
|
||||
return
|
||||
}
|
||||
|
||||
if !handleRateAndSizeLimits(ws, evt, eventSize) {
|
||||
return
|
||||
}
|
||||
|
||||
storeEvent(context.TODO(), evt, ws)
|
||||
HandleKind(context.TODO(), evt, ws, eventSize)
|
||||
|
||||
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 {
|
||||
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 false
|
||||
return
|
||||
}
|
||||
for _, pubkey := range pubkeys {
|
||||
config.GetConfig().PubkeyWhitelist.Pubkeys = append(config.GetConfig().PubkeyWhitelist.Pubkeys, pubkey)
|
||||
}
|
||||
}
|
||||
|
||||
// Check against manual blacklist
|
||||
if blacklisted, msg := utils.CheckBlacklist(evt.PubKey, evt.Content); blacklisted {
|
||||
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) {
|
||||
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) {
|
||||
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)
|
||||
|
||||
if allowed, msg := rateLimiter.AllowEvent(evt.Kind, category); !allowed {
|
||||
response.SendOK(ws, evt.ID, false, msg)
|
||||
return false
|
||||
return
|
||||
}
|
||||
|
||||
if allowed, msg := sizeLimiter.AllowSize(evt.Kind, eventSize); !allowed {
|
||||
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
|
||||
switch {
|
||||
case evt.Kind == 0:
|
||||
|
Loading…
Reference in New Issue
Block a user