1 -- SPDX-FileCopyrightText: 2022 Frank Doepper
3 -- SPDX-License-Identifier: GPL-3.0-only
5 {-# LANGUAGE OverloadedStrings #-}
7 module Network.AMQP.Utils.Connection where
9 import qualified Data.ByteString as B
10 import Data.Default.Class
11 import qualified Data.Text as T
13 import Network.AMQP.Utils.Helpers
14 import Network.AMQP.Utils.Options
15 import qualified Network.Connection as N
17 import Network.TLS.Extra
21 -- | opens a connection and a channel
22 connect :: Args -> IO (Connection, Channel)
24 printparam "server" $ server args
25 printparam "port" $ portnumber args
26 printparam "vhost" $ vHost args
27 printparam "connection_name" $ connectionName args
28 printparam "heartbeat" $ liftA2 (\ n m -> show n ++ m) (heartBeat args) (Just " s")
29 printparam "connect timeout" $ [show (connect_timeout args), "s"]
30 globalCertificateStore <- getSystemCertificateStore
33 (defaultParamsClient "" B.empty)
36 { sharedValidationCache = def
37 , sharedCAStore = globalCertificateStore
39 , clientSupported = def {supportedCiphers = ciphersuite_default}
41 def {onCertificateRequest = myCert (cert args) (key args)}
48 [ SASLMechanism "EXTERNAL" B.empty Nothing
49 , plain (T.pack (user args)) (T.pack (pass args))
51 , coVHost = T.pack $ vHost args
54 then Just (TLSCustom myTLS)
56 , coServers = [(server args, portnumber args)]
57 , coHeartbeatDelay = heartBeat args
58 , coName = fmap T.pack $ connectionName args
60 getServerProperties conn >>= return . (formatheaders fieldshow) >>=
61 printparam "server properties"
62 Just chan <- timeout to $ openChannel conn
65 to = connect_timeout args * 1000000
67 -- addChannelExceptionHandler chan
68 -- (\exception -> closeConnection conn >>
69 -- printparam "exiting" (show exception) >>
72 -- -- noop sharedValidationCache, handy when debugging
73 -- noValidation :: ValidationCache
74 -- noValidation = ValidationCache
75 -- (\_ _ _ -> return ValidationCachePass)
76 -- (\_ _ _ -> return ())
79 -- | provides the TLS client certificate
80 myCert :: Maybe FilePath -> Maybe FilePath -> t -> IO (Maybe Credential)
81 myCert (Just cert') (Just key') _ = do
82 result <- credentialLoadX509 cert' key'
84 Left x -> printparam "ERROR" x >> return Nothing
85 Right x -> return $ Just x
86 myCert _ _ _ = return Nothing