mirror of
https://github.com/fiatjaf/khatru.git
synced 2026-05-01 09:18:02 +02:00
add ratelimit policy based on NIP-42 authentication
This commit is contained in:
@@ -29,6 +29,18 @@ func EventPubKeyRateLimiter(tokensPerInterval int, interval time.Duration, maxTo
|
||||
}
|
||||
}
|
||||
|
||||
func EventAuthedPubKeyRateLimiter(tokensPerInterval int, interval time.Duration, maxTokens int) func(ctx context.Context, _ *nostr.Event) (reject bool, msg string) {
|
||||
rl := startRateLimitSystem[string](tokensPerInterval, interval, maxTokens)
|
||||
|
||||
return func(ctx context.Context, _ *nostr.Event) (reject bool, msg string) {
|
||||
user := khatru.GetAuthed(ctx)
|
||||
if user == "" {
|
||||
return false, ""
|
||||
}
|
||||
return rl(user), "rate-limited: slow down, please"
|
||||
}
|
||||
}
|
||||
|
||||
func ConnectionRateLimiter(tokensPerInterval int, interval time.Duration, maxTokens int) func(r *http.Request) bool {
|
||||
rl := startRateLimitSystem[string](tokensPerInterval, interval, maxTokens)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user