From 745d862fab7c8cdf49a0c97efb6567d92f987542 Mon Sep 17 00:00:00 2001 From: Thibault Duplessis Date: Fri, 2 Sep 2016 14:07:00 +0200 Subject: [PATCH] improve ratelimit logging and IP selection --- app/controllers/Api.scala | 6 +++--- app/controllers/ForumPost.scala | 2 +- app/controllers/ForumTopic.scala | 2 +- app/controllers/Search.scala | 4 ++-- app/controllers/Setup.scala | 8 ++++---- app/controllers/Study.scala | 5 ++--- app/controllers/User.scala | 3 +-- 7 files changed, 14 insertions(+), 16 deletions(-) diff --git a/app/controllers/Api.scala b/app/controllers/Api.scala index 8b6e89f5bd..f6344dd64f 100644 --- a/app/controllers/Api.scala +++ b/app/controllers/Api.scala @@ -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, diff --git a/app/controllers/ForumPost.scala b/app/controllers/ForumPost.scala index 21836aa0f6..cfd4dddd65 100644 --- a/app/controllers/ForumPost.scala +++ b/app/controllers/ForumPost.scala @@ -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)) { diff --git a/app/controllers/ForumTopic.scala b/app/controllers/ForumTopic.scala index c444ac379c..87a93320f4 100644 --- a/app/controllers/ForumTopic.scala +++ b/app/controllers/ForumTopic.scala @@ -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 => diff --git a/app/controllers/Search.scala b/app/controllers/Search.scala index f9397e83fb..25d032b56c 100644 --- a/app/controllers/Search.scala +++ b/app/controllers/Search.scala @@ -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( diff --git a/app/controllers/Setup.scala b/app/controllers/Setup.scala index cfe91e3e2e..66f0dd58d1 100644 --- a/app/controllers/Setup.scala +++ b/app/controllers/Setup.scala @@ -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( diff --git a/app/controllers/Study.scala b/app/controllers/Study.scala index 0627c32e71..f743efbc03 100644 --- a/app/controllers/Study.scala +++ b/app/controllers/Study.scala @@ -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 => diff --git a/app/controllers/User.scala b/app/controllers/User.scala index 45acaca437..3129c8a783 100644 --- a/app/controllers/User.scala +++ b/app/controllers/User.scala @@ -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,