improve ratelimit logging and IP selection

ios-push-2
Thibault Duplessis 2016-09-02 14:07:00 +02:00
parent 64a4728462
commit 745d862fab
7 changed files with 14 additions and 16 deletions

View File

@ -53,7 +53,7 @@ object Api extends LilaController {
val nb = (getInt("nb") | 10) atLeast 1 atMost 50
val cost = page * nb + 10
val ip = HTTPRequest lastRemoteAddress ctx.req
UsersRateLimitPerIP(ip, cost = cost, msg = ip) {
UsersRateLimitPerIP(ip, cost = cost) {
UsersRateLimitGlobal("-", cost = cost, msg = ip) {
lila.mon.api.teamUsers.cost(cost)
(get("team") ?? Env.team.api.team).flatMap {
@ -88,7 +88,7 @@ object Api extends LilaController {
val nb = (getInt("nb") | 10) atLeast 1 atMost 100
val cost = page * nb + 10
val ip = HTTPRequest lastRemoteAddress ctx.req
GamesRateLimitPerIP(ip, cost = cost, msg = ip) {
GamesRateLimitPerIP(ip, cost = cost) {
GamesRateLimitPerUA(~HTTPRequest.userAgent(ctx.req), cost = cost, msg = ip) {
GamesRateLimitGlobal("-", cost = cost, msg = ip) {
lila.mon.api.userGames.cost(cost)
@ -123,7 +123,7 @@ object Api extends LilaController {
def game(id: String) = ApiRequest { implicit ctx =>
val ip = HTTPRequest lastRemoteAddress ctx.req
val key = s"$id:$ip"
GamesRateLimitPerIP(key, cost = 1, msg = key) {
GamesRateLimitPerIP(key, cost = 1) {
lila.mon.api.game.cost(1)
gameApi.one(
id = id take lila.game.Game.gameIdSize,

View File

@ -31,7 +31,7 @@ object ForumPost extends LilaController with ForumController {
}
def create(categSlug: String, slug: String, page: Int) = OpenBody { implicit ctx =>
CreateRateLimit(ctx.req.remoteAddress) {
CreateRateLimit(HTTPRequest lastRemoteAddress ctx.req) {
CategGrantWrite(categSlug) {
implicit val req = ctx.body
OptionFuResult(topicApi.show(categSlug, slug, page, ctx.troll)) {

View File

@ -23,7 +23,7 @@ object ForumTopic extends LilaController with ForumController {
}
def create(categSlug: String) = OpenBody { implicit ctx =>
CreateRateLimit(ctx.req.remoteAddress) {
CreateRateLimit(HTTPRequest lastRemoteAddress ctx.req) {
CategGrantWrite(categSlug) {
implicit val req = ctx.body
OptionFuResult(CategRepo bySlug categSlug) { categ =>

View File

@ -33,8 +33,8 @@ object Search extends LilaController {
Reasonable(page, 100) {
val ip = HTTPRequest lastRemoteAddress ctx.req
val cost = scala.math.sqrt(page).toInt
RateLimitPerIP(ip, cost = cost, msg = ip) {
RateLimitGlobal("-", cost = cost, msg = ip) {
RateLimitPerIP(ip, cost = cost) {
RateLimitGlobal("-", cost = cost) {
Env.game.cached.nbTotal flatMap { nbGames =>
implicit def req = ctx.body
searchForm.bindFromRequest.fold(

View File

@ -62,7 +62,7 @@ object Setup extends LilaController with TheftPrevention {
def friend(userId: Option[String]) =
OpenBody { implicit ctx =>
implicit val req = ctx.body
PostRateLimit(req.remoteAddress) {
PostRateLimit(HTTPRequest lastRemoteAddress ctx.req) {
env.forms.friend(ctx).bindFromRequest.fold(
f => negotiate(
html = Lobby.renderHome(Results.BadRequest),
@ -127,7 +127,7 @@ object Setup extends LilaController with TheftPrevention {
def hook(uid: String) = OpenBody { implicit ctx =>
implicit val req = ctx.body
PostRateLimit(req.remoteAddress) {
PostRateLimit(HTTPRequest lastRemoteAddress ctx.req) {
NoPlaybanOrCurrent {
env.forms.hook(ctx).bindFromRequest.fold(
err => negotiate(
@ -143,7 +143,7 @@ object Setup extends LilaController with TheftPrevention {
}
def like(uid: String, gameId: String) = Open { implicit ctx =>
PostRateLimit(ctx.req.remoteAddress) {
PostRateLimit(HTTPRequest lastRemoteAddress ctx.req) {
NoPlaybanOrCurrent {
env.forms.hookConfig flatMap { config =>
GameRepo game gameId map {
@ -184,7 +184,7 @@ object Setup extends LilaController with TheftPrevention {
private def process[A](form: Context => Form[A])(op: A => BodyContext[_] => Fu[Pov]) =
OpenBody { implicit ctx =>
PostRateLimit(ctx.req.remoteAddress) {
PostRateLimit(HTTPRequest lastRemoteAddress ctx.req) {
implicit val req = ctx.body
form(ctx).bindFromRequest.fold(
f => negotiate(

View File

@ -191,10 +191,9 @@ object Study extends LilaController {
def cloneApply(id: String) = Auth { implicit ctx =>
me =>
val ip = HTTPRequest lastRemoteAddress ctx.req
implicit val default = ornicar.scalalib.Zero.instance[Fu[Result]](notFound)
CloneLimitPerUser(me.id, cost = 1, msg = me.id) {
CloneLimitPerIP(ip, cost = 1, msg = ip) {
CloneLimitPerUser(me.id, cost = 1) {
CloneLimitPerIP(HTTPRequest lastRemoteAddress ctx.req, cost = 1) {
OptionFuResult(env.api.byId(id)) { prev =>
CanViewResult(prev) {
env.api.clone(me, prev) map { study =>

View File

@ -138,8 +138,7 @@ object User extends LilaController {
filterOption.fold({
Env.simul isHosting u.id map (_.fold(Playing, All).name)
})(fuccess) flatMap { filterName =>
val ip = HTTPRequest lastRemoteAddress ctx.req
UserGamesRateLimitPerIP(ip, cost = page, msg = s"on ${u.username}") {
UserGamesRateLimitPerIP(HTTPRequest lastRemoteAddress ctx.req, cost = page, msg = s"on ${u.username}") {
lila.mon.http.userGames.cost(page)
GameFilterMenu.paginatorOf(
userGameSearch = userGameSearch,