trim imports
[diohsc.git] / ClientSessionManager.hs
blob8e8b678c8e129329b8d95327c4b6e8ff72860875
1 -- This file is part of Diohsc
2 -- Copyright (C) 2020 Martin Bays <mbays@sdf.org>
3 --
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.
7 --
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
13 , ClientSessions
14 , newClientSessions
15 , lookupClientSession
16 ) where
18 import Control.Concurrent
19 import Data.Map (fromAscList, toAscList)
20 import Network.TLS
22 import Data.Hourglass (timeAdd)
23 import Time.System (timeCurrent)
24 import Time.Types (Elapsed (..), Seconds (..))
26 import qualified Data.Map as Map
28 import Fingerprint
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)
39 insert
40 delete
41 where
42 insert sid sd@SessionData{ sessionClientSNI = Just sni } = do
43 now <- timeCurrent
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 ()
49 delete sid =
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