2012-07-26 08:50:09 +00:00
|
|
|
{- Yesod webapp
|
2012-07-26 01:26:13 +00:00
|
|
|
-
|
2015-01-21 16:50:09 +00:00
|
|
|
- Copyright 2012-2014 Joey Hess <id@joeyh.name>
|
2012-07-26 01:26:13 +00:00
|
|
|
-
|
2014-05-10 14:01:27 +00:00
|
|
|
- License: BSD-2-clause
|
2012-07-26 01:26:13 +00:00
|
|
|
-}
|
|
|
|
|
2022-02-25 17:16:36 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings, CPP, RankNTypes, PackageImports #-}
|
2012-07-26 01:26:13 +00:00
|
|
|
|
2019-11-23 15:07:22 +00:00
|
|
|
module Utility.WebApp (
|
|
|
|
browserProc,
|
|
|
|
runWebApp,
|
|
|
|
webAppSessionBackend,
|
|
|
|
checkAuthToken,
|
|
|
|
insertAuthToken,
|
|
|
|
writeHtmlShim,
|
|
|
|
) where
|
2012-07-26 01:26:13 +00:00
|
|
|
|
|
|
|
import Common
|
2013-05-12 23:19:28 +00:00
|
|
|
import Utility.Tmp
|
2013-01-03 22:50:30 +00:00
|
|
|
import Utility.FileMode
|
2016-11-22 18:18:34 +00:00
|
|
|
import Utility.AuthToken
|
2012-07-26 01:26:13 +00:00
|
|
|
|
2012-08-30 17:05:39 +00:00
|
|
|
import qualified Yesod
|
2012-07-26 08:50:09 +00:00
|
|
|
import qualified Network.Wai as Wai
|
2012-07-26 01:26:13 +00:00
|
|
|
import Network.Wai.Handler.Warp
|
2014-03-01 01:32:18 +00:00
|
|
|
import Network.Wai.Handler.WarpTLS
|
2012-07-26 01:26:13 +00:00
|
|
|
import Network.HTTP.Types
|
|
|
|
import Network.Socket
|
2013-10-08 15:14:25 +00:00
|
|
|
import "crypto-api" Crypto.Random
|
2012-07-26 16:41:20 +00:00
|
|
|
import qualified Web.ClientSession as CS
|
2012-07-26 08:50:09 +00:00
|
|
|
import qualified Data.Text as T
|
|
|
|
import qualified Data.Text.Encoding as TE
|
|
|
|
import Blaze.ByteString.Builder.Char.Utf8 (fromText)
|
|
|
|
import Blaze.ByteString.Builder (Builder)
|
|
|
|
import Control.Arrow ((***))
|
2012-07-27 19:33:24 +00:00
|
|
|
import Control.Concurrent
|
2012-07-26 01:26:13 +00:00
|
|
|
|
2013-04-18 16:52:55 +00:00
|
|
|
{- Builds a command to use to start or open a web browser showing an url. -}
|
|
|
|
browserProc :: String -> CreateProcess
|
2012-09-29 18:49:15 +00:00
|
|
|
#ifdef darwin_HOST_OS
|
2013-04-18 16:52:55 +00:00
|
|
|
browserProc url = proc "open" [url]
|
2012-07-26 01:26:13 +00:00
|
|
|
#else
|
2013-12-06 22:18:05 +00:00
|
|
|
#ifdef mingw32_HOST_OS
|
2014-06-10 22:29:15 +00:00
|
|
|
-- Warning: On Windows, no quoting or escaping of the url seems possible,
|
|
|
|
-- so spaces in it will cause problems. One approach is to make the url
|
|
|
|
-- be a relative filename, and adjust the returned CreateProcess to change
|
|
|
|
-- to the directory it's in.
|
2013-12-07 02:07:16 +00:00
|
|
|
browserProc url = proc "cmd" ["/c start " ++ url]
|
2013-12-06 22:18:05 +00:00
|
|
|
#else
|
2013-04-18 16:52:55 +00:00
|
|
|
browserProc url = proc "xdg-open" [url]
|
|
|
|
#endif
|
2012-07-26 01:26:13 +00:00
|
|
|
#endif
|
|
|
|
|
2013-04-08 19:04:35 +00:00
|
|
|
{- Binds to a socket on localhost, or possibly a different specified
|
|
|
|
- hostname or address, and runs a webapp on it.
|
2012-07-26 01:26:13 +00:00
|
|
|
-
|
2013-01-10 03:17:52 +00:00
|
|
|
- An IO action can also be run, to do something with the address,
|
2012-07-26 01:26:13 +00:00
|
|
|
- such as start a web browser to view the webapp.
|
2012-07-27 19:33:24 +00:00
|
|
|
-}
|
2024-01-25 18:08:36 +00:00
|
|
|
runWebApp :: Maybe TLSSettings -> Maybe HostName -> Maybe PortNumber -> Wai.Application -> (SockAddr -> IO ()) -> IO ()
|
|
|
|
runWebApp tlssettings h p app observer = withSocketsDo $ do
|
|
|
|
sock <- getSocket h p
|
2014-03-13 01:21:10 +00:00
|
|
|
void $ forkIO $ go webAppSettings sock app
|
2018-10-13 05:36:06 +00:00
|
|
|
sockaddr <- getSocketName sock
|
2013-05-03 02:38:45 +00:00
|
|
|
observer sockaddr
|
2014-03-12 16:19:48 +00:00
|
|
|
where
|
2014-03-13 01:21:10 +00:00
|
|
|
go = (maybe runSettingsSocket (\ts -> runTLSSocket ts) tlssettings)
|
2013-05-03 02:38:45 +00:00
|
|
|
|
2014-05-29 17:49:45 +00:00
|
|
|
-- disable buggy sloworis attack prevention code
|
2013-03-10 19:43:10 +00:00
|
|
|
webAppSettings :: Settings
|
2014-06-04 18:37:08 +00:00
|
|
|
webAppSettings = setTimeout halfhour defaultSettings
|
|
|
|
where
|
|
|
|
halfhour = 30 * 60
|
2013-03-09 18:57:48 +00:00
|
|
|
|
2013-04-08 19:04:35 +00:00
|
|
|
{- Binds to a local socket, or if specified, to a socket on the specified
|
2024-01-25 18:08:36 +00:00
|
|
|
- hostname or address. Selects any free port, unless a port is specified.
|
2012-09-18 21:19:41 +00:00
|
|
|
-
|
|
|
|
- Prefers to bind to the ipv4 address rather than the ipv6 address
|
|
|
|
- of localhost, if it's available.
|
2013-04-08 19:04:35 +00:00
|
|
|
-}
|
2024-01-25 18:08:36 +00:00
|
|
|
getSocket :: Maybe HostName -> Maybe PortNumber -> IO Socket
|
|
|
|
getSocket h p = do
|
2020-10-08 14:31:03 +00:00
|
|
|
#if defined (mingw32_HOST_OS)
|
2013-05-02 20:48:14 +00:00
|
|
|
-- The HostName is ignored by this code.
|
2020-10-08 14:50:39 +00:00
|
|
|
-- getAddrInfo didn't used to work on windows; current status
|
|
|
|
-- unknown.
|
2013-05-02 20:48:14 +00:00
|
|
|
when (isJust h) $
|
2023-04-10 17:38:14 +00:00
|
|
|
giveup "getSocket with HostName not supported on this OS"
|
2020-10-08 14:50:39 +00:00
|
|
|
let addr = tupleToHostAddress (127,0,0,1)
|
2014-10-09 18:53:13 +00:00
|
|
|
sock <- socket AF_INET Stream defaultProtocol
|
2013-05-02 20:48:14 +00:00
|
|
|
preparesocket sock
|
2024-01-25 18:08:36 +00:00
|
|
|
bind sock (SockAddrInet (fromMaybe defaultPort p) addr)
|
2013-05-02 20:48:14 +00:00
|
|
|
use sock
|
|
|
|
where
|
|
|
|
#else
|
2024-01-25 18:08:36 +00:00
|
|
|
addrs <- getAddrInfo (Just hints) (Just hostname) (fmap show p)
|
2012-09-18 21:19:41 +00:00
|
|
|
case (partition (\a -> addrFamily a == AF_INET) addrs) of
|
|
|
|
(v4addr:_, _) -> go v4addr
|
|
|
|
(_, v6addr:_) -> go v6addr
|
2023-04-10 17:38:14 +00:00
|
|
|
_ -> giveup "unable to bind to a local socket"
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
2014-03-01 02:53:26 +00:00
|
|
|
hostname = fromMaybe localhost h
|
2020-11-24 16:35:09 +00:00
|
|
|
localhost = "localhost"
|
2013-05-02 17:01:42 +00:00
|
|
|
hints = defaultHints { addrSocketType = Stream }
|
2012-12-13 04:24:19 +00:00
|
|
|
{- Repeated attempts because bind sometimes fails for an
|
|
|
|
- unknown reason on OSX. -}
|
|
|
|
go addr = go' 100 addr
|
|
|
|
go' :: Int -> AddrInfo -> IO Socket
|
2023-04-10 17:38:14 +00:00
|
|
|
go' 0 _ = giveup "unable to bind to local socket"
|
2012-12-13 04:24:19 +00:00
|
|
|
go' n addr = do
|
2016-09-05 18:39:44 +00:00
|
|
|
r <- tryIO $ bracketOnError (open addr) close (useaddr addr)
|
2012-12-13 04:24:19 +00:00
|
|
|
either (const $ go' (pred n) addr) return r
|
|
|
|
open addr = socket (addrFamily addr) (addrSocketType addr) (addrProtocol addr)
|
2013-05-02 20:48:14 +00:00
|
|
|
useaddr addr sock = do
|
|
|
|
preparesocket sock
|
2016-09-05 18:39:44 +00:00
|
|
|
bind sock (addrAddress addr)
|
2013-05-02 20:48:14 +00:00
|
|
|
use sock
|
|
|
|
#endif
|
|
|
|
preparesocket sock = setSocketOption sock ReuseAddr 1
|
|
|
|
use sock = do
|
2012-12-13 04:24:19 +00:00
|
|
|
listen sock maxListenQueue
|
|
|
|
return sock
|
2012-07-26 01:26:13 +00:00
|
|
|
|
2012-07-26 16:41:20 +00:00
|
|
|
{- Rather than storing a session key on disk, use a random key
|
|
|
|
- that will only be valid for this run of the webapp. -}
|
2013-06-02 19:57:22 +00:00
|
|
|
webAppSessionBackend :: Yesod.Yesod y => y -> IO (Maybe Yesod.SessionBackend)
|
2012-07-26 16:41:20 +00:00
|
|
|
webAppSessionBackend _ = do
|
|
|
|
g <- newGenIO :: IO SystemRandom
|
|
|
|
case genBytes 96 g of
|
2023-04-10 17:38:14 +00:00
|
|
|
Left e -> giveup $ "failed to generate random key: " ++ show e
|
2012-07-26 16:41:20 +00:00
|
|
|
Right (s, _) -> case CS.initKey s of
|
2023-04-10 17:38:14 +00:00
|
|
|
Left e -> giveup $ "failed to initialize key: " ++ show e
|
2013-03-10 20:02:16 +00:00
|
|
|
Right key -> use key
|
|
|
|
where
|
|
|
|
timeout = 120 * 60 -- 120 minutes
|
|
|
|
use key =
|
2013-06-02 19:57:22 +00:00
|
|
|
Just . Yesod.clientSessionBackend key . fst
|
|
|
|
<$> Yesod.clientSessionDateCacher timeout
|
2012-07-26 16:41:20 +00:00
|
|
|
|
2012-07-26 08:50:09 +00:00
|
|
|
{- A Yesod isAuthorized method, which checks the auth cgi parameter
|
2012-07-29 16:12:14 +00:00
|
|
|
- against a token extracted from the Yesod application.
|
|
|
|
-
|
|
|
|
- Note that the usual Yesod error page is bypassed on error, to avoid
|
|
|
|
- possibly leaking the auth token in urls on that page!
|
2016-11-10 17:48:54 +00:00
|
|
|
-
|
|
|
|
- If the predicate does not match the route, the auth parameter is not
|
|
|
|
- needed.
|
2012-07-29 16:12:14 +00:00
|
|
|
-}
|
2016-11-10 17:48:54 +00:00
|
|
|
checkAuthToken :: Yesod.MonadHandler m => Yesod.RenderRoute site => (Yesod.HandlerSite m -> AuthToken) -> Yesod.Route site -> ([T.Text] -> Bool) -> m Yesod.AuthResult
|
|
|
|
checkAuthToken extractAuthToken r predicate
|
|
|
|
| not (predicate (fst (Yesod.renderRoute r))) = return Yesod.Authorized
|
|
|
|
| otherwise = do
|
|
|
|
webapp <- Yesod.getYesod
|
|
|
|
req <- Yesod.getRequest
|
|
|
|
let params = Yesod.reqGetParams req
|
2016-11-22 18:18:34 +00:00
|
|
|
if (toAuthToken =<< lookup "auth" params) == Just (extractAuthToken webapp)
|
2016-11-10 17:48:54 +00:00
|
|
|
then return Yesod.Authorized
|
|
|
|
else Yesod.sendResponseStatus unauthorized401 ()
|
2012-07-26 08:50:09 +00:00
|
|
|
|
|
|
|
{- A Yesod joinPath method, which adds an auth cgi parameter to every
|
|
|
|
- url matching a predicate, containing a token extracted from the
|
|
|
|
- Yesod application.
|
|
|
|
-
|
|
|
|
- A typical predicate would exclude files under /static.
|
|
|
|
-}
|
2014-03-13 01:21:10 +00:00
|
|
|
insertAuthToken :: forall y. (y -> AuthToken)
|
2012-07-26 08:50:09 +00:00
|
|
|
-> ([T.Text] -> Bool)
|
|
|
|
-> y
|
|
|
|
-> T.Text
|
|
|
|
-> [T.Text]
|
|
|
|
-> [(T.Text, T.Text)]
|
|
|
|
-> Builder
|
2014-03-13 01:21:10 +00:00
|
|
|
insertAuthToken extractAuthToken predicate webapp root pathbits params =
|
2012-07-26 08:50:09 +00:00
|
|
|
fromText root `mappend` encodePath pathbits' encodedparams
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
|
|
|
pathbits' = if null pathbits then [T.empty] else pathbits
|
|
|
|
encodedparams = map (TE.encodeUtf8 *** go) params'
|
|
|
|
go "" = Nothing
|
|
|
|
go x = Just $ TE.encodeUtf8 x
|
2014-03-13 01:21:10 +00:00
|
|
|
authparam = (T.pack "auth", fromAuthToken (extractAuthToken webapp))
|
2012-12-13 04:24:19 +00:00
|
|
|
params'
|
|
|
|
| predicate pathbits = authparam:params
|
|
|
|
| otherwise = params
|
2013-01-03 22:50:30 +00:00
|
|
|
|
|
|
|
{- Creates a html shim file that's used to redirect into the webapp,
|
|
|
|
- to avoid exposing the secret token when launching the web browser. -}
|
|
|
|
writeHtmlShim :: String -> String -> FilePath -> IO ()
|
2020-11-05 22:45:37 +00:00
|
|
|
writeHtmlShim title url file =
|
2020-11-06 18:10:58 +00:00
|
|
|
viaTmp (writeFileProtected . toRawFilePath) file $ genHtmlShim title url
|
2013-01-03 22:50:30 +00:00
|
|
|
|
|
|
|
genHtmlShim :: String -> String -> String
|
|
|
|
genHtmlShim title url = unlines
|
|
|
|
[ "<html>"
|
|
|
|
, "<head>"
|
|
|
|
, "<title>"++ title ++ "</title>"
|
|
|
|
, "<meta http-equiv=\"refresh\" content=\"1; URL="++url++"\">"
|
|
|
|
, "<body>"
|
|
|
|
, "<p>"
|
|
|
|
, "<a href=\"" ++ url ++ "\">" ++ title ++ "</a>"
|
|
|
|
, "</p>"
|
|
|
|
, "</body>"
|
|
|
|
, "</html>"
|
|
|
|
]
|