streamly-websockets-tls-0.1.0.0: src/Streamly/External/Network/WebSocket/TLS.hs
-- |
-- Module : Streamly.External.Network.WebSocket.TLS
-- Description : WebSockets by URL, and over TLS
-- License : BSD-3-Clause
--
-- Open a WebSocket from its URL, the scheme choosing between plain TCP
-- (@ws@) and TLS (@wss@), and serve WebSockets over TLS.
--
-- > {-# LANGUAGE OverloadedStrings #-}
-- > import Streamly.External.Network.WebSocket (Message (TextMessage), receive,
-- > send)
-- > import Streamly.External.Network.WebSocket.TLS (withUrl)
-- >
-- > main :: IO ()
-- > main = withUrl "wss://example.org/chat" $ \connection -> do
-- > send connection (TextMessage "hello")
-- > receive connection >>= print
module Streamly.External.Network.WebSocket.TLS
( -- * Clients
withUrl
, urlConfig
, withConnectionAt
, Scheme (..)
, Certificates (..)
-- * Servers
, serveSecure
, serveSecureSocket
, Credentials
, loadCredentials
) where
import Control.Exception (bracket, throwIO, try)
import Data.ByteString (ByteString, breakSubstring)
import Data.ByteString.Char8 qualified as BS8 (map)
import Data.Char (toLower)
import Network.Socket (Socket, close)
import Streamly.External.Network.HTTP (HttpError (..))
import Streamly.External.Network.HTTP.URL (Scheme (..), Url (..), parseUrl)
import Streamly.External.Network.TLS (Certificates (..), Credentials,
acceptConnection, loadCredentials,
transport)
import Streamly.External.Network.TLS qualified as NT (Config (certificates, connectTimeout),
ConnectTimedOut (ConnectTimedOut),
config, withConnection)
import Streamly.External.Network.WebSocket (Config (..), Connection,
WebSocketError (..), config,
withConnection, withConnectionOver)
import Streamly.External.Network.WebSocket.Server (ServerOptions (..), Upgrade,
acceptOver, listen,
serveSocketWith)
-- | Open the WebSocket of a URL such as @wss:\/\/example.org\/chat@ and
-- use it, as 'withConnection' does: over TLS for @wss@, checked against
-- the certificates of the system, and in clear for @ws@.
--
-- Throws what 'withConnection' throws; @InvalidRequest@ (an 'HttpError')
-- for what is not a @ws@ or @wss@ URL; and @TLSException@ (of the @tls@
-- package) when the TLS connection fails, among others for a certificate
-- that is not accepted.
withUrl :: ByteString -> (Connection -> IO a) -> IO a
withUrl text use = do
(scheme, settings) <- either (throwIO . InvalidRequest) pure (urlConfig text)
withConnectionAt SystemCertificates scheme settings use
-- | The scheme and the configuration that a @ws@ or @wss@ URL stands for:
-- 'Http' for @ws@, 'Https' for @wss@; the ports are 80 and 443 unless the
-- URL names one. Change the configuration before giving it to
-- 'withConnectionAt' for extra headers or other bounds.
urlConfig :: ByteString -> Either String (Scheme, Config)
urlConfig text = case breakSubstring "://" text of
(name, rest)
| BS8.map toLower name == "ws" -> from ("http" <> rest)
| BS8.map toLower name == "wss" -> from ("https" <> rest)
_ -> Left "not a ws or wss URL"
where
from equivalent = case parseUrl equivalent of
Left _ -> Left "not a ws or wss URL"
Right found -> Right (found.scheme, config found.host found.port found.target)
-- | Open a WebSocket in clear ('Http') or over TLS ('Https') and use it,
-- as 'withConnection' does. Over TLS, the server must present a
-- certificate signed by one of the authorities trusted, and the time
-- allowed to open the connection covers the TLS handshake too.
withConnectionAt :: Certificates -> Scheme -> Config -> (Connection -> IO a) -> IO a
withConnectionAt trusted scheme settings use = case scheme of
Http -> withConnection settings use
Https -> do
let secure = (NT.config settings.host settings.port) {NT.certificates = trusted, NT.connectTimeout = settings.openTimeout}
opened <- try (NT.withConnection secure (\connection -> withConnectionOver (transport connection) settings use))
-- One error for a connection that is not open in time, whatever the
-- scheme.
either (\NT.ConnectTimedOut -> throwIO OpenTimedOut) pure opened
-- | Listen on an address and a port, and serve every client over TLS with
-- a handler, each in a thread of its own, without end. See
-- 'serveSecureSocket'.
serveSecure :: Credentials -> String -> Int -> ServerOptions -> (Upgrade -> Connection -> IO ()) -> IO ()
serveSecure credentials host port options handler =
bracket (listen host port) close (\listener -> serveSecureSocket credentials listener options handler)
-- | Serve over TLS every client that connects to a listening socket, as
-- @serveSocket@ does in clear. The server proves its name with its
-- credentials; the time allowed for the request of a client also bounds
-- its TLS handshake.
serveSecureSocket :: Credentials -> Socket -> ServerOptions -> (Upgrade -> Connection -> IO ()) -> IO ()
serveSecureSocket credentials listener options handler = serveSocketWith listener $ \peer ->
acceptConnection credentials options.openTimeout peer $ \connection ->
acceptOver (transport connection) options handler