author | Wuzzy <Wuzzy2@mail.ru> |
Tue, 28 May 2019 17:49:04 +0200 | |
changeset 15094 | 7732013ce64c |
parent 14862 | 111c4d750c6d |
permissions | -rw-r--r-- |
10464
d08611b52000
Added two copyrights on gameServer
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10095
diff
changeset
|
1 |
{- |
d08611b52000
Added two copyrights on gameServer
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10095
diff
changeset
|
2 |
* Hedgewars, a free turn based strategy game |
11046 | 3 |
* Copyright (c) 2004-2015 Andrey Korotaev <unC0Rr@gmail.com> |
10464
d08611b52000
Added two copyrights on gameServer
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10095
diff
changeset
|
4 |
* |
d08611b52000
Added two copyrights on gameServer
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10095
diff
changeset
|
5 |
* This program is free software; you can redistribute it and/or modify |
d08611b52000
Added two copyrights on gameServer
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10095
diff
changeset
|
6 |
* it under the terms of the GNU General Public License as published by |
d08611b52000
Added two copyrights on gameServer
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10095
diff
changeset
|
7 |
* the Free Software Foundation; version 2 of the License |
d08611b52000
Added two copyrights on gameServer
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10095
diff
changeset
|
8 |
* |
d08611b52000
Added two copyrights on gameServer
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10095
diff
changeset
|
9 |
* This program is distributed in the hope that it will be useful, |
d08611b52000
Added two copyrights on gameServer
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10095
diff
changeset
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
d08611b52000
Added two copyrights on gameServer
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10095
diff
changeset
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
d08611b52000
Added two copyrights on gameServer
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10095
diff
changeset
|
12 |
* GNU General Public License for more details. |
d08611b52000
Added two copyrights on gameServer
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10095
diff
changeset
|
13 |
* |
d08611b52000
Added two copyrights on gameServer
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10095
diff
changeset
|
14 |
* You should have received a copy of the GNU General Public License |
d08611b52000
Added two copyrights on gameServer
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10095
diff
changeset
|
15 |
* along with this program; if not, write to the Free Software |
d08611b52000
Added two copyrights on gameServer
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10095
diff
changeset
|
16 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
d08611b52000
Added two copyrights on gameServer
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10095
diff
changeset
|
17 |
\-} |
d08611b52000
Added two copyrights on gameServer
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10095
diff
changeset
|
18 |
|
10095 | 19 |
{-# LANGUAGE OverloadedStrings, BangPatterns #-} |
10090 | 20 |
module FloodDetection where |
21 |
||
10093 | 22 |
import Control.Monad.State.Strict |
23 |
import Data.Time |
|
24 |
import Control.Arrow |
|
25 |
---------------- |
|
26 |
import ServerState |
|
10090 | 27 |
import CoreTypes |
10095 | 28 |
import Utils |
10090 | 29 |
|
10093 | 30 |
registerEvent :: Event -> StateT ServerState IO [Action] |
31 |
registerEvent e = do |
|
32 |
eventInfo <- client's $ einfo e |
|
10094 | 33 |
if (null eventInfo) || 0 == (fst $ head eventInfo) then doCheck eventInfo else updateInfo |
10095 | 34 |
|
10093 | 35 |
where |
36 |
einfo LobbyChatMessage = eiLobbyChat |
|
37 |
einfo EngineMessage = eiEM |
|
38 |
einfo RoomJoin = eiJoin |
|
14862 | 39 |
einfo RoomNameUpdate = eiLobbyChat |
10094 | 40 |
|
10093 | 41 |
transformField LobbyChatMessage f = \c -> c{eiLobbyChat = f $ eiLobbyChat c} |
10095 | 42 |
transformField EngineMessage f = \c -> c{eiEM = f $ eiEM c} |
43 |
transformField RoomJoin f = \c -> c{eiJoin = f $ eiJoin c} |
|
14862 | 44 |
transformField RoomNameUpdate f = transformField LobbyChatMessage f |
45 |
||
10094 | 46 |
|
10095 | 47 |
boundaries :: Event -> (Int, (NominalDiffTime, Int), (NominalDiffTime, Int), ([Action], [Action])) |
48 |
boundaries LobbyChatMessage = (3, (10, 2), (30, 3), (chat1, chat2)) |
|
49 |
boundaries EngineMessage = (8, (10, 4), (25, 5), (em1, em2)) |
|
50 |
boundaries RoomJoin = (2, (10, 2), (35, 3), (join1, join2)) |
|
14862 | 51 |
boundaries RoomNameUpdate = (\(a, b, c, _) -> (a, b, c, (roomName1, roomName2))) $ boundaries LobbyChatMessage |
10095 | 52 |
|
53 |
chat1 = [Warning $ loc "Warning! Chat flood protection activated"] |
|
54 |
chat2 = [ByeClient $ loc "Excess flood"] |
|
55 |
em1 = [Warning $ loc "Game messages flood detected - 1"] |
|
11466
4b5c7a5c49fd
Defer kicking to the time when everything is in consistent state
unc0rr
parents:
11046
diff
changeset
|
56 |
em2 = [ByeClient $ loc "Excess flood"] |
10095 | 57 |
join1 = [Warning $ loc "Warning! Joins flood protection activated"] |
58 |
join2 = [ByeClient $ loc "Excess flood"] |
|
14862 | 59 |
roomName1 = [Warning $ loc "Warning! Room name change flood protection activated"] |
60 |
roomName2 = [ByeClient $ loc "Excess flood"] |
|
10094 | 61 |
|
10093 | 62 |
doCheck ei = do |
10094 | 63 |
curTime <- io getCurrentTime |
10095 | 64 |
let (numPerEntry, (sec1, num1), (sec2, num2), (ac1, ac2)) = boundaries e |
65 |
||
66 |
let nei = takeWhile ((>=) sec2 . diffUTCTime curTime . snd) ei |
|
67 |
let l2 = length nei |
|
68 |
let l1 = length $ takeWhile ((>=) sec1 . diffUTCTime curTime . snd) nei |
|
10094 | 69 |
|
10095 | 70 |
let actions = if l2 >= num2 + 1 || l1 >= num1 + 1 then |
71 |
ac2 |
|
72 |
else |
|
73 |
if l1 >= num1 || l2 >= num2 then |
|
74 |
ac1 |
|
75 |
else |
|
76 |
[] |
|
10094 | 77 |
|
11466
4b5c7a5c49fd
Defer kicking to the time when everything is in consistent state
unc0rr
parents:
11046
diff
changeset
|
78 |
return $ [ModifyClient . transformField e . const $ (numPerEntry, curTime) : nei |
4b5c7a5c49fd
Defer kicking to the time when everything is in consistent state
unc0rr
parents:
11046
diff
changeset
|
79 |
, ModifyClient (\c -> c{pendingActions = actions}) -- append? prepend? just replacing for now |
4b5c7a5c49fd
Defer kicking to the time when everything is in consistent state
unc0rr
parents:
11046
diff
changeset
|
80 |
] |
10094 | 81 |
|
10093 | 82 |
updateInfo = return [ |
10094 | 83 |
ModifyClient $ transformField e |
84 |
$ \(h:hs) -> first (flip (-) 1) h : hs |
|
10093 | 85 |
] |