From fe3c26650bb1e267cce756831fdb9cde230cafd5 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Fri, 21 Apr 2017 21:06:04 -0400 Subject: use UUID to generate a unique SessionID --- SessionID.hs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'SessionID.hs') diff --git a/SessionID.hs b/SessionID.hs index 71f2150..3827e1d 100644 --- a/SessionID.hs +++ b/SessionID.hs @@ -13,10 +13,13 @@ import Serialization import System.FilePath import Data.Text import System.IO +import System.Directory import Network.Wai.Handler.Warp (Port) import Network.WebSockets hiding (Message) import qualified Data.Aeson import Data.Maybe +import Data.UUID +import Data.UUID.V4 -- | A SessionID is the base name of the log file to use, -- and may not contain any path information. @@ -48,11 +51,21 @@ sessionLogFile :: FilePath -> SessionID -> FilePath sessionLogFile dir (SessionID f) = dir "debug-me." ++ f ++ ".log" -- | Allocate a new SessionID and return an open Handle to its log file. +-- +-- A UUID is used, to avoid ever generating a SessionID that has been used +-- before. withSessionID :: FilePath -> ((Handle, SessionID) -> IO a) -> IO a withSessionID dir a = do - -- TODO find an unused log file and open it - let sid = SessionID "1" - withFile "debug-me-server.log" WriteMode $ \h -> a (h, sid) + createDirectoryIfMissing False dir + sid <- SessionID . toString <$> nextRandom + let f = sessionLogFile dir sid + -- File should not already exist, but just in case we get + -- spectacularly unlucky (or the RNG is broken..), + -- avoid overwriting a log, and try again. + exists <- doesFileExist f + if exists + then withSessionID dir a + else withFile f WriteMode $ \h -> a (h, sid) type UrlString = String -- cgit v1.2.3