1 {-# LANGUAGE OverloadedStrings #-}
3 module Network.AMQP.Utils.Connection where
5 import qualified Data.ByteString as B
6 import Data.Default.Class
7 import qualified Data.Text as T
9 import Network.AMQP.Utils.Helpers
10 import Network.AMQP.Utils.Options
11 import qualified Network.Connection as N
13 import Network.TLS.Extra
17 -- | opens a connection and a channel
18 connect :: Args -> IO (Connection, Channel)
20 printparam "server" $ server args
21 printparam "port" $ portnumber args
22 printparam "vhost" $ vHost args
23 printparam "connection_name" $ connectionName args
24 printparam "connect timeout" $ [show (connect_timeout args), "s"]
25 globalCertificateStore <- getSystemCertificateStore
28 (defaultParamsClient "" B.empty)
31 { sharedValidationCache = def
32 , sharedCAStore = globalCertificateStore
34 , clientSupported = def {supportedCiphers = ciphersuite_default}
36 def {onCertificateRequest = myCert (cert args) (key args)}
43 [ SASLMechanism "EXTERNAL" B.empty Nothing
44 , plain (T.pack (user args)) (T.pack (pass args))
46 , coVHost = T.pack $ vHost args
49 then Just (TLSCustom myTLS)
51 , coServers = [(server args, portnumber args)]
52 , coHeartbeatDelay = heartBeat args
53 , coName = fmap T.pack $ connectionName args
55 getServerProperties conn >>= return . (formatheaders fieldshow) >>=
56 printparam "server properties"
57 Just chan <- timeout to $ openChannel conn
60 to = connect_timeout args * 1000000
62 -- addChannelExceptionHandler chan
63 -- (\exception -> closeConnection conn >>
64 -- printparam "exiting" (show exception) >>
67 -- -- noop sharedValidationCache, handy when debugging
68 -- noValidation :: ValidationCache
69 -- noValidation = ValidationCache
70 -- (\_ _ _ -> return ValidationCachePass)
71 -- (\_ _ _ -> return ())
74 -- | provides the TLS client certificate
75 myCert :: Maybe FilePath -> Maybe FilePath -> t -> IO (Maybe Credential)
76 myCert (Just cert') (Just key') _ = do
77 result <- credentialLoadX509 cert' key'
79 Left x -> printparam "ERROR" x >> return Nothing
80 Right x -> return $ Just x
81 myCert _ _ _ = return Nothing