You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
81 lines
2.3 KiB
81 lines
2.3 KiB
|
14 years ago
|
{-# Language NoMonomorphismRestriction #-}
|
||
|
14 years ago
|
{-# OPTIONS_HADDOCK hide #-}
|
||
|
13 years ago
|
module Data.Conduit.Tls
|
||
|
14 years ago
|
( tlsinit
|
||
|
14 years ago
|
-- , conduitStdout
|
||
|
14 years ago
|
, module TLS
|
||
|
|
, module TLSExtra
|
||
|
|
)
|
||
|
|
where
|
||
|
|
|
||
|
13 years ago
|
import Control.Monad
|
||
|
|
import Control.Monad (liftM, when)
|
||
|
|
import Control.Monad.IO.Class
|
||
|
14 years ago
|
|
||
|
13 years ago
|
import Crypto.Random
|
||
|
14 years ago
|
|
||
|
14 years ago
|
import qualified Data.ByteString as BS
|
||
|
14 years ago
|
import qualified Data.ByteString.Lazy as BL
|
||
|
13 years ago
|
import Data.Conduit
|
||
|
|
import qualified Data.Conduit.Binary as CB
|
||
|
|
import Data.IORef
|
||
|
14 years ago
|
|
||
|
13 years ago
|
import Network.TLS as TLS
|
||
|
|
import Network.TLS.Extra as TLSExtra
|
||
|
14 years ago
|
|
||
|
13 years ago
|
import System.IO (Handle)
|
||
|
14 years ago
|
|
||
|
13 years ago
|
client params gen backend = do
|
||
|
|
contextNew backend params gen
|
||
|
13 years ago
|
|
||
|
|
defaultParams = defaultParamsClient
|
||
|
|
|
||
|
14 years ago
|
tlsinit :: (MonadIO m, MonadIO m1) =>
|
||
|
|
Bool
|
||
|
|
-> TLSParams
|
||
|
13 years ago
|
-> Backend
|
||
|
|
-> m ( Source m1 BS.ByteString
|
||
|
|
, Sink BS.ByteString m1 ()
|
||
|
|
, BS.ByteString -> IO ()
|
||
|
|
, Int -> m1 BS.ByteString
|
||
|
|
, Context
|
||
|
|
)
|
||
|
|
tlsinit debug tlsParams backend = do
|
||
|
13 years ago
|
when debug . liftIO $ putStrLn "TLS with debug mode enabled"
|
||
|
14 years ago
|
gen <- liftIO $ (newGenIO :: IO SystemRandom) -- TODO: Find better random source?
|
||
|
13 years ago
|
con <- client tlsParams gen backend
|
||
|
14 years ago
|
handshake con
|
||
|
|
let src = forever $ do
|
||
|
|
dt <- liftIO $ recvData con
|
||
|
13 years ago
|
when debug (liftIO $ putStr "in: " >> BS.putStrLn dt)
|
||
|
14 years ago
|
yield dt
|
||
|
|
let snk = do
|
||
|
|
d <- await
|
||
|
|
case d of
|
||
|
|
Nothing -> return ()
|
||
|
|
Just x -> do
|
||
|
|
sendData con (BL.fromChunks [x])
|
||
|
13 years ago
|
when debug (liftIO $ putStr "out: " >> BS.putStrLn x)
|
||
|
14 years ago
|
snk
|
||
|
13 years ago
|
read <- liftIO $ mkReadBuffer (recvData con)
|
||
|
14 years ago
|
return ( src
|
||
|
14 years ago
|
, snk
|
||
|
14 years ago
|
, \s -> do
|
||
|
|
when debug (liftIO $ BS.putStrLn s)
|
||
|
14 years ago
|
sendData con $ BL.fromChunks [s]
|
||
|
13 years ago
|
, liftIO . read
|
||
|
14 years ago
|
, con
|
||
|
14 years ago
|
)
|
||
|
13 years ago
|
|
||
|
|
mkReadBuffer :: IO BS.ByteString -> IO (Int -> IO BS.ByteString)
|
||
|
|
mkReadBuffer read = do
|
||
|
|
buffer <- newIORef BS.empty
|
||
|
|
let read' n = do
|
||
|
|
nc <- readIORef buffer
|
||
|
|
bs <- if BS.null nc then read
|
||
|
|
else return nc
|
||
|
|
let (result, rest) = BS.splitAt n bs
|
||
|
|
writeIORef buffer rest
|
||
|
|
return result
|
||
|
|
return read'
|