improve ratelimit logging and IP selection

This commit is contained in:
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 nb = (getInt("nb") | 10) atLeast 1 atMost 50
val cost = page * nb + 10 val cost = page * nb + 10
val ip = HTTPRequest lastRemoteAddress ctx.req val ip = HTTPRequest lastRemoteAddress ctx.req
UsersRateLimitPerIP(ip, cost = cost, msg = ip) { UsersRateLimitPerIP(ip, cost = cost) {
UsersRateLimitGlobal("-", cost = cost, msg = ip) { UsersRateLimitGlobal("-", cost = cost, msg = ip) {
lila.mon.api.teamUsers.cost(cost) lila.mon.api.teamUsers.cost(cost)
(get("team") ?? Env.team.api.team).flatMap { (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 nb = (getInt("nb") | 10) atLeast 1 atMost 100
val cost = page * nb + 10 val cost = page * nb + 10
val ip = HTTPRequest lastRemoteAddress ctx.req 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) { GamesRateLimitPerUA(~HTTPRequest.userAgent(ctx.req), cost = cost, msg = ip) {
GamesRateLimitGlobal("-", cost = cost, msg = ip) { GamesRateLimitGlobal("-", cost = cost, msg = ip) {
lila.mon.api.userGames.cost(cost) lila.mon.api.userGames.cost(cost)
@ -123,7 +123,7 @@ object Api extends LilaController {
def game(id: String) = ApiRequest { implicit ctx => def game(id: String) = ApiRequest { implicit ctx =>
val ip = HTTPRequest lastRemoteAddress ctx.req val ip = HTTPRequest lastRemoteAddress ctx.req
val key = s"$id:$ip" val key = s"$id:$ip"
GamesRateLimitPerIP(key, cost = 1, msg = key) { GamesRateLimitPerIP(key, cost = 1) {
lila.mon.api.game.cost(1) lila.mon.api.game.cost(1)
gameApi.one( gameApi.one(
id = id take lila.game.Game.gameIdSize, 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 => def create(categSlug: String, slug: String, page: Int) = OpenBody { implicit ctx =>
CreateRateLimit(ctx.req.remoteAddress) { CreateRateLimit(HTTPRequest lastRemoteAddress ctx.req) {
CategGrantWrite(categSlug) { CategGrantWrite(categSlug) {
implicit val req = ctx.body implicit val req = ctx.body
OptionFuResult(topicApi.show(categSlug, slug, page, ctx.troll)) { 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 => def create(categSlug: String) = OpenBody { implicit ctx =>
CreateRateLimit(ctx.req.remoteAddress) { CreateRateLimit(HTTPRequest lastRemoteAddress ctx.req) {
CategGrantWrite(categSlug) { CategGrantWrite(categSlug) {
implicit val req = ctx.body implicit val req = ctx.body
OptionFuResult(CategRepo bySlug categSlug) { categ => OptionFuResult(CategRepo bySlug categSlug) { categ =>

View file

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

View file

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

View file

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

View file

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