Skip to content

Commit

Permalink
fix: Add uri to rate limiter request key
Browse files Browse the repository at this point in the history
  • Loading branch information
oSumAtrIX committed Jul 8, 2024
1 parent a6008a2 commit e8c2488
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/main/kotlin/app/revanced/api/configuration/HTTP.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import io.ktor.server.application.*
import io.ktor.server.plugins.*
import io.ktor.server.plugins.cors.routing.*
import io.ktor.server.plugins.ratelimit.*
import io.ktor.server.request.*
import org.koin.ktor.ext.get
import kotlin.time.Duration.Companion.minutes

Expand All @@ -19,13 +20,19 @@ fun Application.configureHTTP() {
}

install(RateLimit) {
register(RateLimitName("weak")) {
fun rateLimit(name: String, block: RateLimitProviderConfig.() -> Unit) = register(RateLimitName(name)) {
requestKey {
it.request.uri + it.request.origin.remoteAddress
}

block()
}

rateLimit("weak") {
rateLimiter(limit = 30, refillPeriod = 2.minutes)
requestKey { it.request.origin.remoteAddress }
}
register(RateLimitName("strong")) {
rateLimit("strong") {
rateLimiter(limit = 5, refillPeriod = 1.minutes)
requestKey { it.request.origin.remoteHost }
}
}
}

0 comments on commit e8c2488

Please sign in to comment.