author | unc0rr |
Tue, 20 Oct 2020 20:09:44 +0200 | |
changeset 15750 | 036263d63b05 |
parent 11046 | 47a8c19ecb60 |
permissions | -rw-r--r-- |
10460
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10090
diff
changeset
|
1 |
{- |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10090
diff
changeset
|
2 |
* Hedgewars, a free turn based strategy game |
11046 | 3 |
* Copyright (c) 2004-2015 Andrey Korotaev <unC0Rr@gmail.com> |
10460
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10090
diff
changeset
|
4 |
* |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10090
diff
changeset
|
5 |
* This program is free software; you can redistribute it and/or modify |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10090
diff
changeset
|
6 |
* it under the terms of the GNU General Public License as published by |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10090
diff
changeset
|
7 |
* the Free Software Foundation; version 2 of the License |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10090
diff
changeset
|
8 |
* |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10090
diff
changeset
|
9 |
* This program is distributed in the hope that it will be useful, |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10090
diff
changeset
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10090
diff
changeset
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10090
diff
changeset
|
12 |
* GNU General Public License for more details. |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10090
diff
changeset
|
13 |
* |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10090
diff
changeset
|
14 |
* You should have received a copy of the GNU General Public License |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10090
diff
changeset
|
15 |
* along with this program; if not, write to the Free Software |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10090
diff
changeset
|
16 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10090
diff
changeset
|
17 |
\-} |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10090
diff
changeset
|
18 |
|
9977 | 19 |
{-# LANGUAGE BangPatterns #-} |
20 |
||
21 |
module JoinsMonitor( |
|
22 |
JoinsMonitor |
|
23 |
, newJoinMonitor |
|
24 |
, cleanup |
|
25 |
, joinsSentry |
|
26 |
) where |
|
27 |
||
28 |
import qualified Data.Map as Map |
|
29 |
import Data.Time |
|
30 |
import Data.IORef |
|
31 |
import qualified Data.ByteString as B |
|
32 |
import Data.Maybe |
|
33 |
import Control.Monad |
|
34 |
||
35 |
newtype JoinsMonitor = JoinsMonitor (IORef (Map.Map B.ByteString [UTCTime])) |
|
36 |
||
37 |
||
38 |
newJoinMonitor :: IO JoinsMonitor |
|
39 |
newJoinMonitor = do |
|
40 |
ioref <- newIORef Map.empty |
|
41 |
return (JoinsMonitor ioref) |
|
42 |
||
43 |
||
44 |
cleanup :: JoinsMonitor -> UTCTime -> IO () |
|
10005 | 45 |
cleanup (JoinsMonitor ref) time = modifyIORef ref f |
9977 | 46 |
where |
47 |
f = Map.mapMaybe (\v -> let v' = takeWhile (\t -> diffUTCTime time t < 60*60) v in if null v' then Nothing else Just v') |
|
48 |
||
49 |
||
50 |
joinsSentry :: JoinsMonitor -> B.ByteString -> UTCTime -> IO Bool |
|
51 |
joinsSentry (JoinsMonitor ref) host time = do |
|
52 |
m <- readIORef ref |
|
53 |
let lastJoins = map (diffUTCTime time) $ Map.findWithDefault [] host m |
|
54 |
let last30sec = length $ takeWhile (< 30) lastJoins |
|
55 |
let last2min = length $ takeWhile (< 120) lastJoins |
|
56 |
let last10min = length $ takeWhile (< 600) lastJoins |
|
10090 | 57 |
let pass = last30sec < 2 && last2min < 3 && last10min < 5 |
9977 | 58 |
|
59 |
when pass $ writeIORef ref $ Map.alter (Just . (:) time . fromMaybe []) host m |
|
60 |
||
61 |
return pass |