1 -- This file is part of Diohsc
2 -- Copyright (C) 2020 Martin Bays <mbays@sdf.org>
4 -- This program is free software: you can redistribute it and/or modify
5 -- it under the terms of version 3 of the GNU General Public License as
6 -- published by the Free Software Foundation, or any later version.
8 -- You should have received a copy of the GNU General Public License
9 -- along with this program. If not, see http://www.gnu.org/licenses/.
11 module ClientSessionManager
12 ( clientSessionManager
18 import Control
.Concurrent
19 import Data
.Map
(fromAscList
, toAscList
)
22 import Data
.Hourglass
(timeAdd
)
23 import Time
.System
(timeCurrent
)
24 import Time
.Types
(Elapsed
(..), Seconds
(..))
26 import qualified Data
.Map
as Map
30 type ClientSessions
= MVar
(Map
.Map
(HostName
, Maybe Fingerprint
) (Elapsed
, (SessionID
, SessionData
)))
32 newClientSessions
:: IO ClientSessions
33 newClientSessions
= newMVar Map
.empty
35 clientSessionManager
:: Int -> ClientSessions
-> Maybe Fingerprint
-> SessionManager
36 clientSessionManager lifetime sess fp
= SessionManager
37 (\_
-> return Nothing
)
38 (\_
-> return Nothing
)
42 insert sid sd
@SessionData
{ sessionClientSNI
= Just sni
} = do
44 let expire
= now `timeAdd` Seconds
(fromIntegral lifetime
)
45 modifyMVar_ sess
$ return .
46 Map
.insert (sni
, fp
) (expire
,(sid
,sd
)) .
47 fromAscList
. filter (\(_
,(t
,(_
,_
))) -> t
>= now
) . toAscList
48 insert _ _
= return ()
50 modifyMVar_ sess
$ return .
51 fromAscList
. filter (\(_
,(_
,(sid
',_
))) -> sid
/= sid
') . toAscList
53 lookupClientSession
:: HostName
-> Maybe Fingerprint
-> ClientSessions
-> IO (Maybe (SessionID
, SessionData
))
54 lookupClientSession sni fp sess
= (snd <$>) . Map
.lookup (sni
,fp
) <$> readMVar sess