From f060914ab82cddd40f82b4225aac24292cb4c12b Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Tue, 22 Mar 2022 16:18:16 +0000 Subject: [PATCH 001/131] notifications server rfc/schema (#332) * notifications server rfc/schema * update schema/protocol --- protocol/simplex-uri-request.txt | 4 +- protocol/smp-uri-request.txt | 2 +- rfcs/2022-03-22-nofication-server.md | 91 +++++++++++++++++++ simplexmq.cabal | 1 + .../Agent/Store/SQLite/Migrations.hs | 4 +- .../Migrations/M20220322_notifications.hs | 46 ++++++++++ 6 files changed, 144 insertions(+), 4 deletions(-) create mode 100644 rfcs/2022-03-22-nofication-server.md create mode 100644 src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220322_notifications.hs diff --git a/protocol/simplex-uri-request.txt b/protocol/simplex-uri-request.txt index 968757398..510377cbb 100644 --- a/protocol/simplex-uri-request.txt +++ b/protocol/simplex-uri-request.txt @@ -13,6 +13,6 @@ Change controller: Evgeny Poberezkin References: The syntax for connection requests in the latest version of SimpleX Agent Protocol: -https://github.com/simplex-chat/simplexmq/blob/v5/protocol/agent-protocol.md#connection-request +https://github.com/simplex-chat/simplexmq/blob/master/protocol/agent-protocol.md#connection-request SimpleX Messaging Protocol: -https://github.com/simplex-chat/simplexmq/blob/v5/protocol/simplex-messaging.md +https://github.com/simplex-chat/simplexmq/blob/master/protocol/simplex-messaging.md diff --git a/protocol/smp-uri-request.txt b/protocol/smp-uri-request.txt index ffa3fd263..d924ad9f1 100644 --- a/protocol/smp-uri-request.txt +++ b/protocol/smp-uri-request.txt @@ -13,4 +13,4 @@ Change controller: Evgeny Poberezkin References: The syntax for message queue URIs in the latest version of SimpleX Messaging Protocol: -https://github.com/simplex-chat/simplexmq/blob/v5/protocol/simplex-messaging.md#smp-queue-uri +https://github.com/simplex-chat/simplexmq/blob/master/protocol/simplex-messaging.md#smp-queue-uri diff --git a/rfcs/2022-03-22-nofication-server.md b/rfcs/2022-03-22-nofication-server.md new file mode 100644 index 000000000..eebb94861 --- /dev/null +++ b/rfcs/2022-03-22-nofication-server.md @@ -0,0 +1,91 @@ +# Notification server + +## Background and motivation + +SimpleX Chat clients should receive message notifications when not being online and/or subscribed to SMP servers. + +To avoid revealing identities of clients directly to SMP servers via any kind of push notification tokens, a new party called SimpleX Notification Server is introduced to act as a service for subscribing to SMP server queue notifications on behalf of clients and sending push notifications to them. + +## Proposal + +TCP service using the same TLS transport as SMP server, with the fixed size blocks (256 bytes?) and the following set of commands: + +### Protocol + +#### Create subscription + +Command: + +`%s"CREATE " ntfSmpQueueURI ntfPrivateKey token subPublicKey` + +Response: + +`s%"OK"` + +#### Check subscription status + +Command: + +`%s"CHECK " ntfSmpQueueURI` + +Response: + +```abnf +statusResp = %s"STAT " status +status = %s"ERR AUTH" / "ERR SMP AUTH" / %s"ERR SMP TIMEOUT" / %s"ACTIVE" / %s"PENDING" +``` + +#### Update subscription device token + +Command: + +`%s"TOKEN " ntfSmpQueueURI token` + +Response: + +`s%"OK" / %s"ERR"` + +#### Delete subscription (e.g. when deleting the queue or moving to another notification server) + +Command: + +`%s"DELETE " SP ntfSmpQueueURI` + +Response: + +`s%"OK" / %s"ERR"` + +### Agent schema changes + +See [migration](../src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220322_notifications.hs) + +### Agent code + +```haskell +data NtfOptions = NtfOptions + { ntfServer :: Server, -- same type as for SMP servers, probably will be renamed + ntfToken :: ByteString, + ntfInitialCheckDelay :: Int, -- initial check delay after subscription is created, seconds + ntfPeriodicCheckInterval :: Int -- subscription check interval, seconds + } + +data AgentConfig = AgentConfig { + -- ... + initialNtfOpts :: Maybe NtfOptions + -- ... + } + +data AgentClient = AgentClient { + -- ... + ntfOpts :: TVar (Maybe NtfOptions) + -- ... + } +``` + +A configuration parameter `initialNtfOpts :: Maybe NtfOptions` - if it is set or changes the agent would automatically manage subscriptions as SMP queues are subscribed/created/deleted and as the token or server changes. + +There will be a method to update notifications configuration in case token or server changes. + +All subscriptions will be managed in a separate subscription management loop, that would always take the earliest un-updated subscription that requires some action (ntf_sub_action column) and perform this action - the table of subscription would serve both as the table of existing subscriptions and required actions. + +E.g. if the queue is subscribed and there is no notification subscription, it will be created in the table with "create" action, and the loop would create it and schedule "check" action on it. \ No newline at end of file diff --git a/simplexmq.cabal b/simplexmq.cabal index 5762c6271..4954ca43a 100644 --- a/simplexmq.cabal +++ b/simplexmq.cabal @@ -41,6 +41,7 @@ library Simplex.Messaging.Agent.Store.SQLite.Migrations Simplex.Messaging.Agent.Store.SQLite.Migrations.M20220101_initial Simplex.Messaging.Agent.Store.SQLite.Migrations.M20220301_snd_queue_keys + Simplex.Messaging.Agent.Store.SQLite.Migrations.M20220322_notifications Simplex.Messaging.Client Simplex.Messaging.Crypto Simplex.Messaging.Crypto.Ratchet diff --git a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations.hs b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations.hs index 96891bc52..7e7f9f5b4 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations.hs @@ -26,6 +26,7 @@ import Database.SQLite.Simple.QQ (sql) import qualified Database.SQLite3 as SQLite3 import Simplex.Messaging.Agent.Store.SQLite.Migrations.M20220101_initial import Simplex.Messaging.Agent.Store.SQLite.Migrations.M20220301_snd_queue_keys +import Simplex.Messaging.Agent.Store.SQLite.Migrations.M20220322_notifications data Migration = Migration {name :: String, up :: Text} deriving (Show) @@ -33,7 +34,8 @@ data Migration = Migration {name :: String, up :: Text} schemaMigrations :: [(String, Query)] schemaMigrations = [ ("20220101_initial", m20220101_initial), - ("20220301_snd_queue_keys", m20220301_snd_queue_keys) + ("20220301_snd_queue_keys", m20220301_snd_queue_keys), + ("20220322_notifications", m20220322_notifications) ] -- | The list of migrations in ascending order by date diff --git a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220322_notifications.hs b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220322_notifications.hs new file mode 100644 index 000000000..5ae493a21 --- /dev/null +++ b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220322_notifications.hs @@ -0,0 +1,46 @@ +{-# LANGUAGE QuasiQuotes #-} + +module Simplex.Messaging.Agent.Store.SQLite.Migrations.M20220322_notifications where + +import Database.SQLite.Simple (Query) +import Database.SQLite.Simple.QQ (sql) + +m20220322_notifications :: Query +m20220322_notifications = + [sql| +ALTER TABLE rcv_queues ADD COLUMN ntf_id BLOB; + +ALTER TABLE rcv_queues ADD COLUMN ntf_public_key BLOB; + +ALTER TABLE rcv_queues ADD COLUMN ntf_private_key BLOB; + +CREATE UNIQUE INDEX idx_rcv_queues_ntf ON rcv_queues (host, port, ntf_id); + +CREATE TABLE ntf_servers ( + ntf_host TEXT NOT NULL, + ntf_port TEXT NOT NULL, + ntf_key_hash BLOB NOT NULL, + created_at TEXT NOT NULL, + updated_at TEXT NOT NULL, + PRIMARY KEY (ntf_host, ntf_port) +) WITHOUT ROWID; + +CREATE TABLE ntf_subscriptions ( + ntf_host TEXT NOT NULL, + ntf_port TEXT NOT NULL, + ntf_sub_status TEXT NOT NULL, -- new, created, active, pending, error_auth + ntf_sub_action TEXT, -- if there is an action required on this subscription: create / check / token / delete + ntf_sub_action_ts TEXT, -- the earliest time for the action, e.g. checks can be scheduled every X hours + ntf_token TEXT NOT NULL, -- or BLOB? + smp_host TEXT NOT NULL, + smp_port TEXT NOT NULL, + smp_ntf_id BLOB NOT NULL, + created_at TEXT NOT NULL, + updated_at TEXT NOT NULL, -- this is to check subscription status periodically to know when it was last checked + PRIMARY KEY (ntf_host, ntf_port, smp_host, smp_port, smp_ntf_id), + FOREIGN KEY (ntf_host, ntf_port) REFERENCES ntf_servers + ON DELETE RESTRICT ON UPDATE CASCADE, + FOREIGN KEY (smp_host, smp_port, smp_ntf_id) REFERENCES rcv_queues (host, port, ntf_id) + ON DELETE RESTRICT ON UPDATE CASCADE +) WITHOUT ROWID; +|] From f466fa76e5cde0ecdb029e1c317e761d73ec70b8 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Sun, 27 Mar 2022 08:36:00 +0100 Subject: [PATCH 002/131] notifications: protocol, server (#335) * notifications: protocol * update protocol to include subscription ID and DH public key * update protocol, started server * add notification server subscription DH key * use the same command type in notifications protocol, protocol parsing, server frame * remove empty files --- protocol/simplex-messaging.md | 2 +- simplexmq.cabal | 7 + src/Simplex/Messaging/Agent/Protocol.hs | 1 - .../Migrations/M20220322_notifications.hs | 3 +- src/Simplex/Messaging/Client.hs | 6 +- src/Simplex/Messaging/Crypto.hs | 6 + src/Simplex/Messaging/Encoding.hs | 4 + .../Messaging/Notifications/Protocol.hs | 161 ++++++++++++++++++ src/Simplex/Messaging/Notifications/Server.hs | 111 ++++++++++++ .../Messaging/Notifications/Server/Env.hs | 66 +++++++ .../Notifications/Server/Subscriptions.hs | 25 +++ .../Messaging/Notifications/Transport.hs | 19 +++ src/Simplex/Messaging/Protocol.hs | 33 ++-- src/Simplex/Messaging/Server.hs | 25 +-- src/Simplex/Messaging/Transport.hs | 39 +++-- src/Simplex/Messaging/Transport/Client.hs | 2 +- src/Simplex/Messaging/Transport/Server.hs | 2 +- tests/SMPClient.hs | 2 +- tests/ServerTests.hs | 2 +- 19 files changed, 460 insertions(+), 56 deletions(-) create mode 100644 src/Simplex/Messaging/Notifications/Protocol.hs create mode 100644 src/Simplex/Messaging/Notifications/Server.hs create mode 100644 src/Simplex/Messaging/Notifications/Server/Env.hs create mode 100644 src/Simplex/Messaging/Notifications/Server/Subscriptions.hs create mode 100644 src/Simplex/Messaging/Notifications/Transport.hs diff --git a/protocol/simplex-messaging.md b/protocol/simplex-messaging.md index f24a1dee5..ecd108094 100644 --- a/protocol/simplex-messaging.md +++ b/protocol/simplex-messaging.md @@ -770,7 +770,7 @@ The syntax for error responses: ```abnf error = %s"ERR " errorType errorType = %s"BLOCK" / %s"SESSION" / %s"CMD " cmdError / %s"AUTH" / %s"LARGE_MSG" /%s"INTERNAL" -cmdError = %s"SYNTAX" / %s"PROHIBITED" / %s"NO_AUTH" / %s"HAS_AUTH" / %s"NO_QUEUE" +cmdError = %s"SYNTAX" / %s"PROHIBITED" / %s"NO_AUTH" / %s"HAS_AUTH" / %s"NO_ENTITY" ``` Server implementations must aim to respond within the same time for each command in all cases when `"ERR AUTH"` response is required to prevent timing attacks (e.g., the server should perform signature verification even when the queue does not exist on the server or the signature of different size is sent, using any RSA key with the same size as the signature size). diff --git a/simplexmq.cabal b/simplexmq.cabal index 4954ca43a..fa8cfb729 100644 --- a/simplexmq.cabal +++ b/simplexmq.cabal @@ -47,6 +47,13 @@ library Simplex.Messaging.Crypto.Ratchet Simplex.Messaging.Encoding Simplex.Messaging.Encoding.String + Simplex.Messaging.Notifications.Client + Simplex.Messaging.Notifications.Client.Env + Simplex.Messaging.Notifications.Protocol + Simplex.Messaging.Notifications.Server + Simplex.Messaging.Notifications.Server.Env + Simplex.Messaging.Notifications.Server.Subscriptions + Simplex.Messaging.Notifications.Transport Simplex.Messaging.Parsers Simplex.Messaging.Protocol Simplex.Messaging.Server diff --git a/src/Simplex/Messaging/Agent/Protocol.hs b/src/Simplex/Messaging/Agent/Protocol.hs index b38410d1e..fa1d46a8a 100644 --- a/src/Simplex/Messaging/Agent/Protocol.hs +++ b/src/Simplex/Messaging/Agent/Protocol.hs @@ -539,7 +539,6 @@ data SMPQueueUri = SMPQueueUri } deriving (Eq, Show) --- TODO change SMP queue URI format to include version range and allow unknown parameters instance StrEncoding SMPQueueUri where -- v1 uses short SMP queue URI format strEncode SMPQueueUri {smpServer = srv, senderId = qId, clientVRange = _vr, dhPublicKey = k} = diff --git a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220322_notifications.hs b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220322_notifications.hs index 5ae493a21..8fd996810 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220322_notifications.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220322_notifications.hs @@ -28,6 +28,7 @@ CREATE TABLE ntf_servers ( CREATE TABLE ntf_subscriptions ( ntf_host TEXT NOT NULL, ntf_port TEXT NOT NULL, + ntf_sub_id BLOB NOT NULL, ntf_sub_status TEXT NOT NULL, -- new, created, active, pending, error_auth ntf_sub_action TEXT, -- if there is an action required on this subscription: create / check / token / delete ntf_sub_action_ts TEXT, -- the earliest time for the action, e.g. checks can be scheduled every X hours @@ -37,7 +38,7 @@ CREATE TABLE ntf_subscriptions ( smp_ntf_id BLOB NOT NULL, created_at TEXT NOT NULL, updated_at TEXT NOT NULL, -- this is to check subscription status periodically to know when it was last checked - PRIMARY KEY (ntf_host, ntf_port, smp_host, smp_port, smp_ntf_id), + PRIMARY KEY (ntf_host, ntf_port, ntf_sub_id), FOREIGN KEY (ntf_host, ntf_port) REFERENCES ntf_servers ON DELETE RESTRICT ON UPDATE CASCADE, FOREIGN KEY (smp_host, smp_port, smp_ntf_id) REFERENCES rcv_queues (host, port, ntf_id) diff --git a/src/Simplex/Messaging/Client.hs b/src/Simplex/Messaging/Client.hs index e6e87337f..f91a0100a 100644 --- a/src/Simplex/Messaging/Client.hs +++ b/src/Simplex/Messaging/Client.hs @@ -63,8 +63,8 @@ import Network.Socket (ServiceName) import Numeric.Natural import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Protocol -import Simplex.Messaging.Transport (ATransport (..), THandle (..), TLS, TProxy, Transport (..), TransportError, clientHandshake) -import Simplex.Messaging.Transport.Client (runTransportClient) +import Simplex.Messaging.Transport (ATransport (..), THandle (..), TLS, TProxy, Transport (..), TransportError) +import Simplex.Messaging.Transport.Client (runTransportClient, smpClientHandshake) import Simplex.Messaging.Transport.KeepAlive import Simplex.Messaging.Transport.WebSockets (WS) import Simplex.Messaging.Util (bshow, liftError, raceAny_) @@ -175,7 +175,7 @@ getSMPClient smpServer cfg@SMPClientConfig {qSize, tcpTimeout, tcpKeepAlive, smp client :: forall c. Transport c => TProxy c -> SMPClient -> TMVar (Either SMPClientError (THandle c)) -> c -> IO () client _ c thVar h = - runExceptT (clientHandshake h $ keyHash smpServer) >>= \case + runExceptT (smpClientHandshake h $ keyHash smpServer) >>= \case Left e -> atomically . putTMVar thVar . Left $ SMPTransportError e Right th@THandle {sessionId} -> do atomically $ do diff --git a/src/Simplex/Messaging/Crypto.hs b/src/Simplex/Messaging/Crypto.hs index 669792476..92ff4f75e 100644 --- a/src/Simplex/Messaging/Crypto.hs +++ b/src/Simplex/Messaging/Crypto.hs @@ -284,6 +284,12 @@ instance Eq APrivateSignKey where deriving instance Show APrivateSignKey +instance Encoding APrivateSignKey where + smpEncode = smpEncode . encodePrivKey + {-# INLINE smpEncode #-} + smpDecode = decodePrivKey + {-# INLINE smpDecode #-} + data APublicVerifyKey = forall a. (AlgorithmI a, SignatureAlgorithm a) => diff --git a/src/Simplex/Messaging/Encoding.hs b/src/Simplex/Messaging/Encoding.hs index a0abca35e..6c7c56ddd 100644 --- a/src/Simplex/Messaging/Encoding.hs +++ b/src/Simplex/Messaging/Encoding.hs @@ -141,3 +141,7 @@ instance (Encoding a, Encoding b, Encoding c, Encoding d) => Encoding (a, b, c, instance (Encoding a, Encoding b, Encoding c, Encoding d, Encoding e) => Encoding (a, b, c, d, e) where smpEncode (a, b, c, d, e) = smpEncode a <> smpEncode b <> smpEncode c <> smpEncode d <> smpEncode e smpP = (,,,,) <$> smpP <*> smpP <*> smpP <*> smpP <*> smpP + +instance (Encoding a, Encoding b, Encoding c, Encoding d, Encoding e, Encoding f) => Encoding (a, b, c, d, e, f) where + smpEncode (a, b, c, d, e, f) = smpEncode a <> smpEncode b <> smpEncode c <> smpEncode d <> smpEncode e <> smpEncode f + smpP = (,,,,,) <$> smpP <*> smpP <*> smpP <*> smpP <*> smpP <*> smpP diff --git a/src/Simplex/Messaging/Notifications/Protocol.hs b/src/Simplex/Messaging/Notifications/Protocol.hs new file mode 100644 index 000000000..da66280fd --- /dev/null +++ b/src/Simplex/Messaging/Notifications/Protocol.hs @@ -0,0 +1,161 @@ +{-# LANGUAGE LambdaCase #-} +{-# LANGUAGE NamedFieldPuns #-} +{-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE TypeFamilies #-} + +module Simplex.Messaging.Notifications.Protocol where + +import qualified Data.Attoparsec.ByteString.Char8 as A +import Data.ByteString.Char8 (ByteString) +import qualified Data.ByteString.Char8 as B +import Data.Maybe (isNothing) +import qualified Simplex.Messaging.Crypto as C +import Simplex.Messaging.Encoding +import Simplex.Messaging.Protocol + +data NtfCommandTag + = NCCreate_ + | NCCheck_ + | NCToken_ + | NCDelete_ + deriving (Show) + +instance Encoding NtfCommandTag where + smpEncode = \case + NCCreate_ -> "CREATE" + NCCheck_ -> "CHECK" + NCToken_ -> "TOKEN" + NCDelete_ -> "DELETE" + smpP = messageTagP + +instance ProtocolMsgTag NtfCommandTag where + decodeTag = \case + "CREATE" -> Just NCCreate_ + "CHECK" -> Just NCCheck_ + "TOKEN" -> Just NCToken_ + "DELETE" -> Just NCDelete_ + _ -> Nothing + +data NtfCommand + = NCCreate DeviceToken SMPQueueNtfUri C.APublicVerifyKey C.PublicKeyX25519 + | NCCheck + | NCToken DeviceToken + | NCDelete + +instance Protocol NtfCommand where + type Tag NtfCommand = NtfCommandTag + encodeProtocol = \case + NCCreate token smpQueue verifyKey dhKey -> e (NCCreate_, ' ', token, smpQueue, verifyKey, dhKey) + NCCheck -> e NCCheck_ + NCToken token -> e (NCToken_, ' ', token) + NCDelete -> e NCDelete_ + where + e :: Encoding a => a -> ByteString + e = smpEncode + + protocolP = \case + NCCreate_ -> NCCreate <$> _smpP <*> smpP <*> smpP <*> smpP + NCCheck_ -> pure NCCheck + NCToken_ -> NCToken <$> _smpP + NCDelete_ -> pure NCDelete + + checkCredentials (sig, _, subId, _) cmd = case cmd of + -- CREATE must have signature but NOT subscription ID + NCCreate {} + | isNothing sig -> Left $ CMD NO_AUTH + | not (B.null subId) -> Left $ CMD HAS_AUTH + | otherwise -> Right cmd + -- other client commands must have both signature and subscription ID + _ + | isNothing sig || B.null subId -> Left $ CMD NO_AUTH + | otherwise -> Right cmd + +data NtfResponseTag + = NRSubId_ + | NROk_ + | NRErr_ + | NRStat_ + deriving (Show) + +instance Encoding NtfResponseTag where + smpEncode = \case + NRSubId_ -> "ID" + NROk_ -> "OK" + NRErr_ -> "ERR" + NRStat_ -> "STAT" + smpP = messageTagP + +instance ProtocolMsgTag NtfResponseTag where + decodeTag = \case + "ID" -> Just NRSubId_ + "OK" -> Just NROk_ + "ERR" -> Just NRErr_ + "STAT" -> Just NRStat_ + _ -> Nothing + +data NtfResponse + = NRSubId C.PublicKeyX25519 + | NROk + | NRErr ErrorType + | NRStat NtfStatus + +instance Protocol NtfResponse where + type Tag NtfResponse = NtfResponseTag + encodeProtocol = \case + NRSubId dhKey -> e (NRSubId_, ' ', dhKey) + NROk -> e NROk_ + NRErr err -> e (NRErr_, ' ', err) + NRStat stat -> e (NRStat_, ' ', stat) + where + e :: Encoding a => a -> ByteString + e = smpEncode + + protocolP = \case + NRSubId_ -> NRSubId <$> _smpP + NROk_ -> pure NROk + NRErr_ -> NRErr <$> _smpP + NRStat_ -> NRStat <$> _smpP + + checkCredentials (_, _, subId, _) cmd = case cmd of + -- ERR response does not always have subscription ID + NRErr _ -> Right cmd + -- other server responses must have subscription ID + _ + | B.null subId -> Left $ CMD NO_ENTITY + | otherwise -> Right cmd + +data SMPQueueNtfUri = SMPQueueNtfUri + { smpServer :: SMPServer, + notifierId :: NotifierId, + notifierKey :: NtfPrivateSignKey + } + +instance Encoding SMPQueueNtfUri where + smpEncode SMPQueueNtfUri {smpServer, notifierId, notifierKey} = smpEncode (smpServer, notifierId, notifierKey) + smpP = do + (smpServer, notifierId, notifierKey) <- smpP + pure $ SMPQueueNtfUri smpServer notifierId notifierKey + +newtype DeviceToken = DeviceToken ByteString + +instance Encoding DeviceToken where + smpEncode (DeviceToken t) = smpEncode t + smpP = DeviceToken <$> smpP + +type NtfSubsciptionId = ByteString + +data NtfStatus = NSPending | NSActive | NSEnd | NSSMPAuth + +instance Encoding NtfStatus where + smpEncode = \case + NSPending -> "PENDING" + NSActive -> "ACTIVE" + NSEnd -> "END" + NSSMPAuth -> "SMP_AUTH" + smpP = + A.takeTill (== ' ') >>= \case + "PENDING" -> pure NSPending + "ACTIVE" -> pure NSActive + "END" -> pure NSEnd + "SMP_AUTH" -> pure NSSMPAuth + _ -> fail "bad NtfError" diff --git a/src/Simplex/Messaging/Notifications/Server.hs b/src/Simplex/Messaging/Notifications/Server.hs new file mode 100644 index 000000000..dfdaae801 --- /dev/null +++ b/src/Simplex/Messaging/Notifications/Server.hs @@ -0,0 +1,111 @@ +{-# LANGUAGE DuplicateRecordFields #-} +{-# LANGUAGE FlexibleContexts #-} +{-# LANGUAGE LambdaCase #-} +{-# LANGUAGE NamedFieldPuns #-} +{-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE ScopedTypeVariables #-} + +module Simplex.Messaging.Notifications.Server where + +import Control.Monad.Except +import Control.Monad.IO.Unlift (MonadUnliftIO) +import Control.Monad.Reader +import Crypto.Random (MonadRandom) +import Data.ByteString.Char8 (ByteString) +import Network.Socket (ServiceName) +import qualified Simplex.Messaging.Crypto as C +import Simplex.Messaging.Notifications.Protocol +import Simplex.Messaging.Notifications.Server.Env +import Simplex.Messaging.Notifications.Server.Subscriptions +import Simplex.Messaging.Notifications.Transport +import Simplex.Messaging.Protocol (ErrorType (..), Transmission, encodeTransmission, tGet, tPut) +import Simplex.Messaging.Server +import Simplex.Messaging.Transport (ATransport (..), THandle (..), TProxy, Transport) +import Simplex.Messaging.Transport.Server (runTransportServer) +import Simplex.Messaging.Util +import UnliftIO.Exception +import UnliftIO.STM + +runNtfServer :: (MonadRandom m, MonadUnliftIO m) => NtfServerConfig -> m () +runNtfServer cfg = do + started <- newEmptyTMVarIO + runNtfServerBlocking started cfg + +runNtfServerBlocking :: (MonadRandom m, MonadUnliftIO m) => TMVar Bool -> NtfServerConfig -> m () +runNtfServerBlocking started cfg@NtfServerConfig {transports} = do + env <- newNtfServerEnv cfg + runReaderT ntfServer env + where + ntfServer :: (MonadUnliftIO m', MonadReader NtfEnv m') => m' () + ntfServer = raceAny_ (map runServer transports) + + runServer :: (MonadUnliftIO m', MonadReader NtfEnv m') => (ServiceName, ATransport) -> m' () + runServer (tcpPort, ATransport t) = do + serverParams <- asks tlsServerParams + runTransportServer started tcpPort serverParams (runClient t) + + runClient :: (Transport c, MonadUnliftIO m, MonadReader NtfEnv m) => TProxy c -> c -> m () + runClient _ h = do + kh <- asks serverIdentity + liftIO (runExceptT $ ntfServerHandshake h kh) >>= \case + Right th -> runNtfClientTransport th + Left _ -> pure () + +runNtfClientTransport :: (Transport c, MonadUnliftIO m, MonadReader NtfEnv m) => THandle c -> m () +runNtfClientTransport th@THandle {sessionId} = do + q <- asks $ tbqSize . config + c <- atomically $ newNtfServerClient q sessionId + raceAny_ [send th c, client c, receive th c] + `finally` clientDisconnected c + +clientDisconnected :: MonadUnliftIO m => NtfServerClient -> m () +clientDisconnected NtfServerClient {connected} = atomically $ writeTVar connected False + +receive :: (Transport c, MonadUnliftIO m, MonadReader NtfEnv m) => THandle c -> NtfServerClient -> m () +receive th NtfServerClient {rcvQ, sndQ} = forever $ do + (sig, signed, (corrId, queueId, cmdOrError)) <- tGet th + case cmdOrError of + Left e -> write sndQ (corrId, queueId, NRErr e) + Right cmd -> do + verified <- verifyTransmission sig signed queueId cmd + if verified + then write rcvQ (corrId, queueId, cmd) + else write sndQ (corrId, queueId, NRErr AUTH) + where + write q t = atomically $ writeTBQueue q t + +send :: (Transport c, MonadUnliftIO m) => THandle c -> NtfServerClient -> m () +send h NtfServerClient {sndQ, sessionId} = forever $ do + t <- atomically $ readTBQueue sndQ + liftIO $ tPut h (Nothing, encodeTransmission sessionId t) + +verifyTransmission :: + forall m. (MonadUnliftIO m, MonadReader NtfEnv m) => Maybe C.ASignature -> ByteString -> NtfSubsciptionId -> NtfCommand -> m Bool +verifyTransmission sig_ signed subId cmd = do + case cmd of + NCCreate _ _ k _ -> pure $ verifyCmdSignature sig_ signed k + _ -> do + st <- asks store + verifySubCmd <$> atomically (getNtfSubscription st subId) + where + verifySubCmd = \case + Right sub -> verifyCmdSignature sig_ signed $ subVerifyKey sub + Left _ -> maybe False (dummyVerifyCmd signed) sig_ `seq` False + +client :: forall m. (MonadUnliftIO m, MonadReader NtfEnv m) => NtfServerClient -> m () +client NtfServerClient {rcvQ, sndQ} = + forever $ + atomically (readTBQueue rcvQ) + >>= processCommand + >>= atomically . writeTBQueue sndQ + where + processCommand :: Transmission NtfCommand -> m (Transmission NtfResponse) + processCommand (corrId, subId, cmd) = case cmd of + NCCreate _token _smpQueue _verifyKey _dhKey -> do + pure (corrId, subId, NROk) + NCCheck -> do + pure (corrId, subId, NROk) + NCToken _token -> do + pure (corrId, subId, NROk) + NCDelete -> do + pure (corrId, subId, NROk) diff --git a/src/Simplex/Messaging/Notifications/Server/Env.hs b/src/Simplex/Messaging/Notifications/Server/Env.hs new file mode 100644 index 000000000..3ce4d2b8d --- /dev/null +++ b/src/Simplex/Messaging/Notifications/Server/Env.hs @@ -0,0 +1,66 @@ +{-# LANGUAGE DuplicateRecordFields #-} +{-# LANGUAGE NamedFieldPuns #-} + +module Simplex.Messaging.Notifications.Server.Env where + +import Control.Monad.IO.Unlift +import Crypto.Random +import Data.ByteString.Char8 (ByteString) +import qualified Data.Map.Strict as M +import Data.X509.Validation (Fingerprint (..)) +import Network.Socket +import qualified Network.TLS as T +import Numeric.Natural +import Simplex.Messaging.Agent.RetryInterval +import Simplex.Messaging.Client +import qualified Simplex.Messaging.Crypto as C +import Simplex.Messaging.Notifications.Protocol +import Simplex.Messaging.Notifications.Server.Subscriptions +import Simplex.Messaging.Protocol (Transmission) +import Simplex.Messaging.Transport (ATransport) +import Simplex.Messaging.Transport.Server (loadFingerprint, loadTLSServerParams) +import UnliftIO.STM + +data NtfServerConfig = NtfServerConfig + { transports :: [(ServiceName, ATransport)], + subscriptionIdBytes :: Int, + tbqSize :: Natural, + smpCfg :: SMPClientConfig, + reconnectInterval :: RetryInterval, + -- CA certificate private key is not needed for initialization + caCertificateFile :: FilePath, + privateKeyFile :: FilePath, + certificateFile :: FilePath + } + +data NtfEnv = NtfEnv + { config :: NtfServerConfig, + serverIdentity :: C.KeyHash, + store :: NtfSubscriptions, + idsDrg :: TVar ChaChaDRG, + tlsServerParams :: T.ServerParams, + serverIdentity :: C.KeyHash + } + +newNtfServerEnv :: (MonadUnliftIO m, MonadRandom m) => NtfServerConfig -> m NtfEnv +newNtfServerEnv config@NtfServerConfig {caCertificateFile, certificateFile, privateKeyFile} = do + idsDrg <- newTVarIO =<< drgNew + store <- newTVarIO M.empty + tlsServerParams <- liftIO $ loadTLSServerParams caCertificateFile certificateFile privateKeyFile + Fingerprint fp <- liftIO $ loadFingerprint caCertificateFile + let serverIdentity = C.KeyHash fp + pure NtfEnv {config, store, idsDrg, tlsServerParams, serverIdentity} + +data NtfServerClient = NtfServerClient + { rcvQ :: TBQueue (Transmission NtfCommand), + sndQ :: TBQueue (Transmission NtfResponse), + sessionId :: ByteString, + connected :: TVar Bool + } + +newNtfServerClient :: Natural -> ByteString -> STM NtfServerClient +newNtfServerClient qSize sessionId = do + rcvQ <- newTBQueue qSize + sndQ <- newTBQueue qSize + connected <- newTVar True + return NtfServerClient {rcvQ, sndQ, sessionId, connected} diff --git a/src/Simplex/Messaging/Notifications/Server/Subscriptions.hs b/src/Simplex/Messaging/Notifications/Server/Subscriptions.hs new file mode 100644 index 000000000..5658e336a --- /dev/null +++ b/src/Simplex/Messaging/Notifications/Server/Subscriptions.hs @@ -0,0 +1,25 @@ +module Simplex.Messaging.Notifications.Server.Subscriptions where + +import Control.Concurrent.STM +import Data.Map.Strict (Map) +import qualified Data.Map.Strict as M +import qualified Simplex.Messaging.Crypto as C +import Simplex.Messaging.Notifications.Protocol +import Simplex.Messaging.Protocol (ErrorType (..), NotifierId, NtfPrivateSignKey, SMPServer) + +type NtfSubscriptionsData = Map NtfSubsciptionId NtfSubsciptionRec + +type NtfSubscriptions = TVar NtfSubscriptionsData + +data NtfSubsciptionRec = NtfSubsciptionRec + { smpServer :: SMPServer, + notifierId :: NotifierId, + notifierKey :: NtfPrivateSignKey, + token :: DeviceToken, + status :: TVar NtfStatus, + subVerifyKey :: C.APublicVerifyKey, + subDHSecret :: C.DhSecretX25519 + } + +getNtfSubscription :: NtfSubscriptions -> NtfSubsciptionId -> STM (Either ErrorType NtfSubsciptionRec) +getNtfSubscription st subId = maybe (Left AUTH) Right . M.lookup subId <$> readTVar st diff --git a/src/Simplex/Messaging/Notifications/Transport.hs b/src/Simplex/Messaging/Notifications/Transport.hs new file mode 100644 index 000000000..8d9b77b5c --- /dev/null +++ b/src/Simplex/Messaging/Notifications/Transport.hs @@ -0,0 +1,19 @@ +module Simplex.Messaging.Notifications.Transport where + +import Control.Monad.Except +import qualified Simplex.Messaging.Crypto as C +import Simplex.Messaging.Transport + +ntfBlockSize :: Int +ntfBlockSize = 512 + +-- | Notifcations server transport handshake. +ntfServerHandshake :: Transport c => c -> C.KeyHash -> ExceptT TransportError IO (THandle c) +ntfServerHandshake c _ = pure $ ntfTHandle c + +-- | Notifcations server client transport handshake. +ntfClientHandshake :: Transport c => c -> C.KeyHash -> ExceptT TransportError IO (THandle c) +ntfClientHandshake c _ = pure $ ntfTHandle c + +ntfTHandle :: Transport c => c -> THandle c +ntfTHandle c = THandle {connection = c, sessionId = tlsUnique c, blockSize = ntfBlockSize, thVersion = 0} diff --git a/src/Simplex/Messaging/Protocol.hs b/src/Simplex/Messaging/Protocol.hs index 0d148bf04..dbe5a98a0 100644 --- a/src/Simplex/Messaging/Protocol.hs +++ b/src/Simplex/Messaging/Protocol.hs @@ -37,7 +37,7 @@ module Simplex.Messaging.Protocol e2eEncMessageLength, -- * SMP protocol types - Protocol, + Protocol (..), Command (..), Party (..), Cmd (..), @@ -74,9 +74,11 @@ module Simplex.Messaging.Protocol MsgBody, -- * Parse and serialize + ProtocolMsgTag (..), + messageTagP, encodeTransmission, transmissionP, - encodeProtocol, + _smpP, -- * TCP transport functions tPut, @@ -174,7 +176,7 @@ data RawTransmission = RawTransmission signed :: ByteString, sessId :: ByteString, corrId :: ByteString, - queueId :: ByteString, + entityId :: ByteString, command :: ByteString } @@ -512,8 +514,8 @@ data CommandError NO_AUTH | -- | transmission has credentials that are not allowed for this command HAS_AUTH - | -- | transmission has no required queue ID - NO_QUEUE + | -- | transmission has no required entity ID (e.g. SMP queue) + NO_ENTITY deriving (Eq, Generic, Read, Show) instance ToJSON CommandError where @@ -534,9 +536,9 @@ transmissionP = do trn signature signed = do sessId <- smpP corrId <- smpP - queueId <- smpP + entityId <- smpP command <- A.takeByteString - pure RawTransmission {signature, signed, sessId, corrId, queueId, command} + pure RawTransmission {signature, signed, sessId, corrId, entityId, command} class Protocol msg where type Tag msg @@ -571,7 +573,7 @@ instance PartyI p => Protocol (Command p) where | otherwise -> Right cmd -- SEND must have queue ID, signature is not always required SEND _ - | B.null queueId -> Left $ CMD NO_QUEUE + | B.null queueId -> Left $ CMD NO_ENTITY | otherwise -> Right cmd -- PING must not have queue ID or signature PING @@ -640,7 +642,7 @@ instance Protocol BrokerMsg where | otherwise -> Left $ CMD HAS_AUTH -- other broker responses must have queue ID _ - | B.null queueId -> Left $ CMD NO_QUEUE + | B.null queueId -> Left $ CMD NO_ENTITY | otherwise -> Right cmd _smpP :: Encoding a => Parser a @@ -695,14 +697,15 @@ instance Encoding CommandError where SYNTAX -> "SYNTAX" NO_AUTH -> "NO_AUTH" HAS_AUTH -> "HAS_AUTH" - NO_QUEUE -> "NO_QUEUE" + NO_ENTITY -> "NO_ENTITY" smpP = A.takeTill (== ' ') >>= \case "UNKNOWN" -> pure UNKNOWN "SYNTAX" -> pure SYNTAX "NO_AUTH" -> pure NO_AUTH "HAS_AUTH" -> pure HAS_AUTH - "NO_QUEUE" -> pure NO_QUEUE + "NO_ENTITY" -> pure NO_ENTITY + "NO_QUEUE" -> pure NO_ENTITY _ -> fail "bad command error type" -- | Send signed SMP transmission to TCP transport. @@ -727,9 +730,9 @@ tGet th@THandle {sessionId} = liftIO (tGetParse th) >>= decodeParseValidate where decodeParseValidate :: Either TransportError RawTransmission -> m (SignedTransmission cmd) decodeParseValidate = \case - Right RawTransmission {signature, signed, sessId, corrId, queueId, command} + Right RawTransmission {signature, signed, sessId, corrId, entityId, command} | sessId == sessionId -> - let decodedTransmission = (,corrId,queueId,command) <$> C.decodeSignature signature + let decodedTransmission = (,corrId,entityId,command) <$> C.decodeSignature signature in either (const $ tError corrId) (tParseValidate signed) decodedTransmission | otherwise -> pure (Nothing, "", (CorrId corrId, "", Left SESSION)) Left _ -> tError "" @@ -738,6 +741,6 @@ tGet th@THandle {sessionId} = liftIO (tGetParse th) >>= decodeParseValidate tError corrId = pure (Nothing, "", (CorrId corrId, "", Left BLOCK)) tParseValidate :: ByteString -> SignedRawTransmission -> m (SignedTransmission cmd) - tParseValidate signed t@(sig, corrId, queueId, command) = do + tParseValidate signed t@(sig, corrId, entityId, command) = do let cmd = parseProtocol command >>= checkCredentials t - pure (sig, signed, (CorrId corrId, queueId, cmd)) + pure (sig, signed, (CorrId corrId, entityId, cmd)) diff --git a/src/Simplex/Messaging/Server.hs b/src/Simplex/Messaging/Server.hs index 73b5780ef..e913f8b19 100644 --- a/src/Simplex/Messaging/Server.hs +++ b/src/Simplex/Messaging/Server.hs @@ -23,7 +23,7 @@ -- and optional append only log of SMP queue records. -- -- See https://github.com/simplex-chat/simplexmq/blob/master/protocol/simplex-messaging.md -module Simplex.Messaging.Server (runSMPServer, runSMPServerBlocking) where +module Simplex.Messaging.Server (runSMPServer, runSMPServerBlocking, verifyCmdSignature, dummyVerifyCmd) where import Control.Concurrent.STM (stateTVar) import Control.Monad @@ -121,7 +121,7 @@ runSMPServerBlocking started cfg@ServerConfig {transports} = do runClient :: (Transport c, MonadUnliftIO m, MonadReader Env m) => TProxy c -> c -> m () runClient _ h = do kh <- asks serverIdentity - liftIO (runExceptT $ serverHandshake h kh) >>= \case + liftIO (runExceptT $ smpServerHandshake h kh) >>= \case Right th -> runClientTransport th Left _ -> pure () @@ -176,8 +176,8 @@ verifyTransmission :: forall m. (MonadUnliftIO m, MonadReader Env m) => Maybe C.ASignature -> ByteString -> QueueId -> Cmd -> m Bool verifyTransmission sig_ signed queueId cmd = do case cmd of - Cmd SRecipient (NEW k _) -> pure $ verifySignature k - Cmd SRecipient _ -> verifyCmd SRecipient $ verifySignature . recipientKey + Cmd SRecipient (NEW k _) -> pure $ verifyCmdSignature sig_ signed k + Cmd SRecipient _ -> verifyCmd SRecipient $ verifyCmdSignature sig_ signed . recipientKey Cmd SSender (SEND _) -> verifyCmd SSender $ verifyMaybe . senderKey Cmd SSender PING -> pure True Cmd SNotifier NSUB -> verifyCmd SNotifier $ verifyMaybe . fmap snd . notifier @@ -186,18 +186,21 @@ verifyTransmission sig_ signed queueId cmd = do verifyCmd party f = do st <- asks queueStore q <- atomically $ getQueue st party queueId - pure $ either (const $ maybe False dummyVerify sig_ `seq` False) f q + pure $ either (const $ maybe False (dummyVerifyCmd signed) sig_ `seq` False) f q verifyMaybe :: Maybe C.APublicVerifyKey -> Bool - verifyMaybe = maybe (isNothing sig_) verifySignature - verifySignature :: C.APublicVerifyKey -> Bool - verifySignature key = maybe False (verify key) sig_ + verifyMaybe = maybe (isNothing sig_) $ verifyCmdSignature sig_ signed + +verifyCmdSignature :: Maybe C.ASignature -> ByteString -> C.APublicVerifyKey -> Bool +verifyCmdSignature sig_ signed key = maybe False (verify key) sig_ + where verify :: C.APublicVerifyKey -> C.ASignature -> Bool verify (C.APublicVerifyKey a k) sig@(C.ASignature a' s) = case (testEquality a a', C.signatureSize k == C.signatureSize s) of (Just Refl, True) -> C.verify' k s signed - _ -> dummyVerify sig `seq` False - dummyVerify :: C.ASignature -> Bool - dummyVerify (C.ASignature _ s) = C.verify' (dummyPublicKey s) s signed + _ -> dummyVerifyCmd signed sig `seq` False + +dummyVerifyCmd :: ByteString -> C.ASignature -> Bool +dummyVerifyCmd signed (C.ASignature _ s) = C.verify' (dummyPublicKey s) s signed -- These dummy keys are used with `dummyVerify` function to mitigate timing attacks -- by having the same time of the response whether a queue exists or nor, for all valid key/signature sizes diff --git a/src/Simplex/Messaging/Transport.hs b/src/Simplex/Messaging/Transport.hs index 8379c35bd..8686f6499 100644 --- a/src/Simplex/Messaging/Transport.hs +++ b/src/Simplex/Messaging/Transport.hs @@ -26,7 +26,6 @@ -- See https://github.com/simplex-chat/simplexmq/blob/master/protocol/simplex-messaging.md#appendix-a module Simplex.Messaging.Transport ( -- * SMP transport parameters - smpBlockSize, supportedSMPVersions, simplexMQVersion, @@ -46,8 +45,8 @@ module Simplex.Messaging.Transport -- * SMP transport THandle (..), TransportError (..), - serverHandshake, - clientHandshake, + smpServerHandshake, + smpClientHandshake, tPutBlock, tGetBlock, serializeTransportError, @@ -251,8 +250,9 @@ trimCR s = if B.last s == '\r' then B.init s else s data THandle c = THandle { connection :: c, sessionId :: ByteString, - -- | agreed SMP server protocol version - smpVersion :: Version + blockSize :: Int, + -- | agreed server protocol version + thVersion :: Version } data ServerHandshake = ServerHandshake @@ -332,45 +332,45 @@ serializeTransportError = \case -- | Pad and send block to SMP transport. tPutBlock :: Transport c => THandle c -> ByteString -> IO (Either TransportError ()) -tPutBlock THandle {connection = c} block = +tPutBlock THandle {connection = c, blockSize} block = bimapM (const $ pure TELargeMsg) (cPut c) $ - C.pad block smpBlockSize + C.pad block blockSize -- | Receive block from SMP transport. tGetBlock :: Transport c => THandle c -> IO (Either TransportError ByteString) -tGetBlock THandle {connection = c} = - cGet c smpBlockSize >>= \case +tGetBlock THandle {connection = c, blockSize} = + cGet c blockSize >>= \case "" -> ioe_EOF msg -> pure . first (const TELargeMsg) $ C.unPad msg -- | Server SMP transport handshake. -- -- See https://github.com/simplex-chat/simplexmq/blob/master/protocol/simplex-messaging.md#appendix-a -serverHandshake :: forall c. Transport c => c -> C.KeyHash -> ExceptT TransportError IO (THandle c) -serverHandshake c kh = do - let th@THandle {sessionId} = tHandle c +smpServerHandshake :: forall c. Transport c => c -> C.KeyHash -> ExceptT TransportError IO (THandle c) +smpServerHandshake c kh = do + let th@THandle {sessionId} = smpTHandle c sendHandshake th $ ServerHandshake {sessionId, smpVersionRange = supportedSMPVersions} getHandshake th >>= \case ClientHandshake {smpVersion, keyHash} | keyHash /= kh -> throwE $ TEHandshake IDENTITY | smpVersion `isCompatible` supportedSMPVersions -> do - pure (th :: THandle c) {smpVersion} + pure (th :: THandle c) {thVersion = smpVersion} | otherwise -> throwE $ TEHandshake VERSION -- | Client SMP transport handshake. -- -- See https://github.com/simplex-chat/simplexmq/blob/master/protocol/simplex-messaging.md#appendix-a -clientHandshake :: forall c. Transport c => c -> C.KeyHash -> ExceptT TransportError IO (THandle c) -clientHandshake c keyHash = do - let th@THandle {sessionId} = tHandle c +smpClientHandshake :: forall c. Transport c => c -> C.KeyHash -> ExceptT TransportError IO (THandle c) +smpClientHandshake c keyHash = do + let th@THandle {sessionId} = smpTHandle c ServerHandshake {sessionId = sessId, smpVersionRange} <- getHandshake th if sessionId /= sessId then throwE TEBadSession else case smpVersionRange `compatibleVersion` supportedSMPVersions of Just (Compatible smpVersion) -> do sendHandshake th $ ClientHandshake {smpVersion, keyHash} - pure (th :: THandle c) {smpVersion} + pure (th :: THandle c) {thVersion = smpVersion} Nothing -> throwE $ TEHandshake VERSION sendHandshake :: (Transport c, Encoding smp) => THandle c -> smp -> ExceptT TransportError IO () @@ -379,6 +379,5 @@ sendHandshake th = ExceptT . tPutBlock th . smpEncode getHandshake :: (Transport c, Encoding smp) => THandle c -> ExceptT TransportError IO smp getHandshake th = ExceptT $ (parse smpP (TEHandshake PARSE) =<<) <$> tGetBlock th -tHandle :: Transport c => c -> THandle c -tHandle c = - THandle {connection = c, sessionId = tlsUnique c, smpVersion = 0} +smpTHandle :: Transport c => c -> THandle c +smpTHandle c = THandle {connection = c, sessionId = tlsUnique c, blockSize = smpBlockSize, thVersion = 0} diff --git a/src/Simplex/Messaging/Transport/Client.hs b/src/Simplex/Messaging/Transport/Client.hs index e02480200..3771da99e 100644 --- a/src/Simplex/Messaging/Transport/Client.hs +++ b/src/Simplex/Messaging/Transport/Client.hs @@ -2,7 +2,7 @@ module Simplex.Messaging.Transport.Client ( runTransportClient, - clientHandshake, + smpClientHandshake, ) where diff --git a/src/Simplex/Messaging/Transport/Server.hs b/src/Simplex/Messaging/Transport/Server.hs index d91c596b9..de66f95c8 100644 --- a/src/Simplex/Messaging/Transport/Server.hs +++ b/src/Simplex/Messaging/Transport/Server.hs @@ -6,7 +6,7 @@ module Simplex.Messaging.Transport.Server ( runTransportServer, loadTLSServerParams, loadFingerprint, - serverHandshake, + smpServerHandshake, ) where diff --git a/tests/SMPClient.hs b/tests/SMPClient.hs index 3abf2c4d9..3789647f5 100644 --- a/tests/SMPClient.hs +++ b/tests/SMPClient.hs @@ -47,7 +47,7 @@ testStoreLogFile = "tests/tmp/smp-server-store.log" testSMPClient :: (Transport c, MonadUnliftIO m) => (THandle c -> m a) -> m a testSMPClient client = runTransportClient testHost testPort testKeyHash (Just defaultKeepAliveOpts) $ \h -> - liftIO (runExceptT $ clientHandshake h testKeyHash) >>= \case + liftIO (runExceptT $ smpClientHandshake h testKeyHash) >>= \case Right th -> client th Left e -> error $ show e diff --git a/tests/ServerTests.hs b/tests/ServerTests.hs index 418c86a5e..1cbeba083 100644 --- a/tests/ServerTests.hs +++ b/tests/ServerTests.hs @@ -496,7 +496,7 @@ syntaxTests (ATransport t) = do describe "SEND" $ do it "valid syntax" $ (sampleSig, "cdab", "12345678", (SEND_, ' ', "hello" :: ByteString)) >#> ("", "cdab", "12345678", ERR AUTH) it "no parameters" $ (sampleSig, "abcd", "12345678", SEND_) >#> ("", "abcd", "12345678", ERR $ CMD SYNTAX) - it "no queue ID" $ (sampleSig, "bcda", "", (SEND_, ' ', "hello" :: ByteString)) >#> ("", "bcda", "", ERR $ CMD NO_QUEUE) + it "no queue ID" $ (sampleSig, "bcda", "", (SEND_, ' ', "hello" :: ByteString)) >#> ("", "bcda", "", ERR $ CMD NO_ENTITY) describe "PING" $ do it "valid syntax" $ ("", "abcd", "", PING_) >#> ("", "abcd", "", PONG) describe "broker response not allowed" $ do From d31958855f7072340136a15dc508e082cfcfecfd Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Sat, 2 Apr 2022 16:14:19 +0100 Subject: [PATCH 003/131] ntf server implementation, updated ntf protocol, ntf client based on refactored protocol client, bare-bones SMP agent to manage ntf connections (to connect to ntf server) (#338) * process ntf server commands * when subscription is re-created and it was ENDed, resubscribe to SMP * SMPClientAgent draft * SMPClientAgent: remove double tracking of subscriptions * subscriber frame * PING error now throws error to restart SMPClient for more reliable re-connection (#342) * increase TCP timeout to 5 sec * add pragmas and vacuum db (#343) * vacuum in each connection to enable auto-vacuum (#344) * update protocol, token verification * refactor SMPClient to ProtocoClient, to use with notification server protocol * notification server client, managing notification clients in the agent * stub for push payload Co-authored-by: JRoberts <8711996+jr-simplex@users.noreply.github.com> --- .../diagrams/notifications/register-token.mmd | 30 ++ simplexmq.cabal | 2 + src/Simplex/Messaging/Agent.hs | 16 +- src/Simplex/Messaging/Agent/Client.hs | 264 ++++++++------- src/Simplex/Messaging/Agent/Env/SQLite.hs | 7 +- src/Simplex/Messaging/Agent/Protocol.hs | 7 +- src/Simplex/Messaging/Agent/Store/SQLite.hs | 32 +- src/Simplex/Messaging/Client.hs | 209 ++++++------ src/Simplex/Messaging/Client/Agent.hs | 301 +++++++++++++++++ src/Simplex/Messaging/Notifications/Client.hs | 55 +++ .../Messaging/Notifications/Protocol.hs | 312 ++++++++++++++---- src/Simplex/Messaging/Notifications/Server.hs | 218 ++++++++++-- .../Messaging/Notifications/Server/Env.hs | 59 +++- .../Messaging/Notifications/Server/Push.hs | 11 + .../Notifications/Server/Subscriptions.hs | 114 ++++++- src/Simplex/Messaging/Protocol.hs | 76 +++-- src/Simplex/Messaging/Server.hs | 2 +- src/Simplex/Messaging/TMap.hs | 7 + src/Simplex/Messaging/Transport/KeepAlive.hs | 2 +- tests/AgentTests/ConnectionRequestTests.hs | 4 +- tests/SMPAgentClient.hs | 4 +- 21 files changed, 1337 insertions(+), 395 deletions(-) create mode 100644 protocol/diagrams/notifications/register-token.mmd create mode 100644 src/Simplex/Messaging/Client/Agent.hs create mode 100644 src/Simplex/Messaging/Notifications/Client.hs create mode 100644 src/Simplex/Messaging/Notifications/Server/Push.hs diff --git a/protocol/diagrams/notifications/register-token.mmd b/protocol/diagrams/notifications/register-token.mmd new file mode 100644 index 000000000..bceef296f --- /dev/null +++ b/protocol/diagrams/notifications/register-token.mmd @@ -0,0 +1,30 @@ +sequenceDiagram + participant M as mobile app + participant C as chat core + participant A as agent + participant P as push server + participant APN as APN + + note over M, APN: get device token + M ->> APN: registerForRemoteNotifications() + APN ->> M: device token + + note over M, P: register device token with push server + M ->> C: /_ntf register + C ->> A: registerNtfToken() + A ->> P: TNEW + P ->> A: ID (tokenId) + A ->> C: registered + C ->> M: registered + + note over M, APN: verify device token + P ->> APN: E2E encrypted code
in background
notification + APN ->> M: deliver background notification with e2ee verification token + M ->> C: /_ntf verify + C ->> A: verifyNtfToken() + A ->> P: TVFY code + P ->> A: OK / ERR + A ->> C: verified + C ->> M: verified + + note over M, APN: now token ID can be used diff --git a/simplexmq.cabal b/simplexmq.cabal index c5687addf..3b9e8eb7e 100644 --- a/simplexmq.cabal +++ b/simplexmq.cabal @@ -43,10 +43,12 @@ library Simplex.Messaging.Agent.Store.SQLite.Migrations.M20220301_snd_queue_keys Simplex.Messaging.Agent.Store.SQLite.Migrations.M20220322_notifications Simplex.Messaging.Client + Simplex.Messaging.Client.Agent Simplex.Messaging.Crypto Simplex.Messaging.Crypto.Ratchet Simplex.Messaging.Encoding Simplex.Messaging.Encoding.String + Simplex.Messaging.Notifications.Client Simplex.Messaging.Notifications.Protocol Simplex.Messaging.Notifications.Server Simplex.Messaging.Notifications.Server.Env diff --git a/src/Simplex/Messaging/Agent.hs b/src/Simplex/Messaging/Agent.hs index f1ecad909..821f5fe5a 100644 --- a/src/Simplex/Messaging/Agent.hs +++ b/src/Simplex/Messaging/Agent.hs @@ -76,12 +76,13 @@ import Simplex.Messaging.Agent.Protocol import Simplex.Messaging.Agent.RetryInterval import Simplex.Messaging.Agent.Store import Simplex.Messaging.Agent.Store.SQLite (SQLiteStore) -import Simplex.Messaging.Client (SMPServerTransmission) +import Simplex.Messaging.Client (ServerTransmission) import qualified Simplex.Messaging.Crypto as C import qualified Simplex.Messaging.Crypto.Ratchet as CR import Simplex.Messaging.Encoding +import Simplex.Messaging.Notifications.Protocol (DeviceToken) import Simplex.Messaging.Parsers (parse) -import Simplex.Messaging.Protocol (MsgBody) +import Simplex.Messaging.Protocol (BrokerMsg, MsgBody) import qualified Simplex.Messaging.Protocol as SMP import qualified Simplex.Messaging.TMap as TM import Simplex.Messaging.Util (bshow, liftError, tryError, unlessM) @@ -149,6 +150,10 @@ deleteConnection c = withAgentEnv c . deleteConnection' c setSMPServers :: AgentErrorMonad m => AgentClient -> NonEmpty SMPServer -> m () setSMPServers c = withAgentEnv c . setSMPServers' c +-- | Register device notifications token +registerNtfToken :: AgentErrorMonad m => AgentClient -> DeviceToken -> m () +registerNtfToken c = withAgentEnv c . registerNtfToken' c + withAgentEnv :: AgentClient -> ReaderT Env m a -> m a withAgentEnv c = (`runReaderT` agentEnv c) @@ -490,10 +495,13 @@ deleteConnection' c connId = withStore (`deleteConn` connId) -- | Change servers to be used for creating new queues, in Reader monad -setSMPServers' :: forall m. AgentMonad m => AgentClient -> NonEmpty SMPServer -> m () +setSMPServers' :: AgentMonad m => AgentClient -> NonEmpty SMPServer -> m () setSMPServers' c servers = do atomically $ writeTVar (smpServers c) servers +registerNtfToken' :: AgentMonad m => AgentClient -> DeviceToken -> m () +registerNtfToken' c token = pure () + getSMPServer :: AgentMonad m => AgentClient -> m SMPServer getSMPServer c = do smpServers <- readTVarIO $ smpServers c @@ -511,7 +519,7 @@ subscriber c@AgentClient {msgQ} = forever $ do Left e -> liftIO $ print e Right _ -> return () -processSMPTransmission :: forall m. AgentMonad m => AgentClient -> SMPServerTransmission -> m () +processSMPTransmission :: forall m. AgentMonad m => AgentClient -> ServerTransmission BrokerMsg -> m () processSMPTransmission c@AgentClient {subQ} (srv, rId, cmd) = do withStore (\st -> getRcvConn st srv rId) >>= \case SomeConn SCDuplex (DuplexConnection cData rq _) -> processSMP SCDuplex cData rq diff --git a/src/Simplex/Messaging/Agent/Client.hs b/src/Simplex/Messaging/Agent/Client.hs index 96bcd4529..321fc82ab 100644 --- a/src/Simplex/Messaging/Agent/Client.hs +++ b/src/Simplex/Messaging/Agent/Client.hs @@ -2,14 +2,13 @@ {-# LANGUAGE DataKinds #-} {-# LANGUAGE DuplicateRecordFields #-} {-# LANGUAGE FlexibleContexts #-} -{-# LANGUAGE InstanceSigs #-} +{-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE LambdaCase #-} {-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE OverloadedLists #-} {-# LANGUAGE OverloadedStrings #-} -{-# LANGUAGE RankNTypes #-} {-# LANGUAGE ScopedTypeVariables #-} -{-# OPTIONS_GHC -fno-warn-orphans #-} +{-# LANGUAGE TypeSynonymInstances #-} module Simplex.Messaging.Agent.Client ( AgentClient (..), @@ -57,9 +56,12 @@ import Simplex.Messaging.Agent.Protocol import Simplex.Messaging.Agent.RetryInterval import Simplex.Messaging.Agent.Store import Simplex.Messaging.Client +import Simplex.Messaging.Client.Agent () import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Encoding -import Simplex.Messaging.Protocol (QueueId, QueueIdsKeys (..), SndPublicVerifyKey) +import Simplex.Messaging.Notifications.Client (NtfClient) +import Simplex.Messaging.Notifications.Protocol (NtfResponse) +import Simplex.Messaging.Protocol (BrokerMsg, ProtocolServer (..), QueueId, QueueIdsKeys (..), SndPublicVerifyKey) import qualified Simplex.Messaging.Protocol as SMP import Simplex.Messaging.TMap (TMap) import qualified Simplex.Messaging.TMap as TM @@ -67,18 +69,22 @@ import Simplex.Messaging.Util (bshow, liftEitherError, liftError, tryError, when import Simplex.Messaging.Version import System.Timeout (timeout) import UnliftIO (async, forConcurrently_) -import UnliftIO.Exception (Exception, IOException) import qualified UnliftIO.Exception as E import UnliftIO.STM +type ClientVar msg = TMVar (Either AgentErrorType (ProtocolClient msg)) + type SMPClientVar = TMVar (Either AgentErrorType SMPClient) +type NtfClientVar = TMVar (Either AgentErrorType NtfClient) + data AgentClient = AgentClient { rcvQ :: TBQueue (ATransmission 'Client), subQ :: TBQueue (ATransmission 'Agent), - msgQ :: TBQueue SMPServerTransmission, + msgQ :: TBQueue (ServerTransmission BrokerMsg), smpServers :: TVar (NonEmpty SMPServer), smpClients :: TMap SMPServer SMPClientVar, + ntfClients :: TMap ProtocolServer NtfClientVar, subscrSrvrs :: TMap SMPServer (TMap ConnId RcvQueue), pendingSubscrSrvrs :: TMap SMPServer (TMap ConnId RcvQueue), subscrConns :: TMap ConnId SMPServer, @@ -101,6 +107,7 @@ newAgentClient agentEnv = do msgQ <- newTBQueue qSize smpServers <- newTVar $ initialSMPServers (config agentEnv) smpClients <- TM.empty + ntfClients <- TM.empty subscrSrvrs <- TM.empty pendingSubscrSrvrs <- TM.empty subscrConns <- TM.empty @@ -111,80 +118,30 @@ newAgentClient agentEnv = do asyncClients <- newTVar [] clientId <- stateTVar (clientCounter agentEnv) $ \i -> (i + 1, i + 1) lock <- newTMVar () - return AgentClient {rcvQ, subQ, msgQ, smpServers, smpClients, subscrSrvrs, pendingSubscrSrvrs, subscrConns, connMsgsQueued, smpQueueMsgQueues, smpQueueMsgDeliveries, reconnections, asyncClients, clientId, agentEnv, smpSubscriber = undefined, lock} + return AgentClient {rcvQ, subQ, msgQ, smpServers, smpClients, ntfClients, subscrSrvrs, pendingSubscrSrvrs, subscrConns, connMsgsQueued, smpQueueMsgQueues, smpQueueMsgDeliveries, reconnections, asyncClients, clientId, agentEnv, smpSubscriber = undefined, lock} -- | Agent monad with MonadReader Env and MonadError AgentErrorType type AgentMonad m = (MonadUnliftIO m, MonadReader Env m, MonadError AgentErrorType m) -newtype InternalException e = InternalException {unInternalException :: e} - deriving (Eq, Show) +class ProtocolServerClient msg where + getProtocolServerClient :: AgentMonad m => AgentClient -> ProtocolServer -> m (ProtocolClient msg) -instance Exception e => Exception (InternalException e) +instance ProtocolServerClient BrokerMsg where getProtocolServerClient = getSMPServerClient -instance (MonadUnliftIO m, Exception e) => MonadUnliftIO (ExceptT e m) where - withRunInIO :: ((forall a. ExceptT e m a -> IO a) -> IO b) -> ExceptT e m b - withRunInIO exceptToIO = - withExceptT unInternalException . ExceptT . E.try $ - withRunInIO $ \run -> - exceptToIO $ run . (either (E.throwIO . InternalException) return <=< runExceptT) +instance ProtocolServerClient NtfResponse where getProtocolServerClient = getNtfServerClient getSMPServerClient :: forall m. AgentMonad m => AgentClient -> SMPServer -> m SMPClient getSMPServerClient c@AgentClient {smpClients, msgQ} srv = - atomically getClientVar >>= either newSMPClient waitForSMPClient + atomically (getClientVar srv smpClients) + >>= either + (newProtocolClient c srv smpClients connectClient reconnectClient) + (waitForProtocolClient smpCfg) where - getClientVar :: STM (Either SMPClientVar SMPClientVar) - getClientVar = maybe (Left <$> newClientVar) (pure . Right) =<< TM.lookup srv smpClients - - newClientVar :: STM SMPClientVar - newClientVar = do - smpVar <- newEmptyTMVar - TM.insert srv smpVar smpClients - pure smpVar - - waitForSMPClient :: TMVar (Either AgentErrorType SMPClient) -> m SMPClient - waitForSMPClient smpVar = do - SMPClientConfig {tcpTimeout} <- asks $ smpCfg . config - smpClient_ <- liftIO $ tcpTimeout `timeout` atomically (readTMVar smpVar) - liftEither $ case smpClient_ of - Just (Right smpClient) -> Right smpClient - Just (Left e) -> Left e - Nothing -> Left $ BROKER TIMEOUT - - newSMPClient :: TMVar (Either AgentErrorType SMPClient) -> m SMPClient - newSMPClient smpVar = tryConnectClient pure tryConnectAsync - where - tryConnectClient :: (SMPClient -> m a) -> m () -> m a - tryConnectClient successAction retryAction = - tryError connectClient >>= \r -> case r of - Right smp -> do - logInfo . decodeUtf8 $ "Agent connected to " <> showServer srv - atomically $ putTMVar smpVar r - successAction smp - Left e -> do - if e == BROKER NETWORK || e == BROKER TIMEOUT - then retryAction - else atomically $ do - putTMVar smpVar (Left e) - TM.delete srv smpClients - throwError e - tryConnectAsync :: m () - tryConnectAsync = do - a <- async connectAsync - atomically $ modifyTVar' (asyncClients c) (a :) - connectAsync :: m () - connectAsync = do - ri <- asks $ reconnectInterval . config - withRetryInterval ri $ \loop -> void $ tryConnectClient (const reconnectClient) loop - connectClient :: m SMPClient connectClient = do cfg <- asks $ smpCfg . config u <- askUnliftIO - liftEitherError smpClientError (getSMPClient srv cfg msgQ $ clientDisconnected u) - `E.catch` internalError - where - internalError :: IOException -> m SMPClient - internalError = throwError . INTERNAL . show + liftEitherError protocolClientError (getProtocolClient srv cfg (Just msgQ) $ clientDisconnected u) clientDisconnected :: UnliftIO m -> IO () clientDisconnected u = do @@ -194,13 +151,14 @@ getSMPServerClient c@AgentClient {smpClients, msgQ} srv = removeClientAndSubs :: IO (Maybe (Map ConnId RcvQueue)) removeClientAndSubs = atomically $ do TM.delete srv smpClients - cVar_ <- TM.lookupDelete srv $ subscrSrvrs c - forM cVar_ $ \cVar -> do - cs <- readTVar cVar - modifyTVar' (subscrConns c) (`M.withoutKeys` M.keysSet cs) - addPendingSubs cVar cs - pure cs + TM.lookupDelete srv (subscrSrvrs c) >>= mapM updateSubs where + updateSubs cVar = do + cs <- readTVar cVar + modifyTVar' (subscrConns c) (`M.withoutKeys` M.keysSet cs) + addPendingSubs cVar cs + pure cs + addPendingSubs cVar cs = do let ps = pendingSubscrSrvrs c TM.lookup srv ps >>= \case @@ -225,30 +183,100 @@ getSMPServerClient c@AgentClient {smpClients, msgQ} srv = reconnectClient :: m () reconnectClient = - withAgentLock c . withSMP c srv $ \smp -> do + withAgentLock c . withClient c srv $ \smp -> do cs <- atomically $ mapM readTVar =<< TM.lookup srv (pendingSubscrSrvrs c) forConcurrently_ (maybe [] M.toList cs) $ \sub@(connId, _) -> whenM (atomically $ isNothing <$> TM.lookup connId (subscrConns c)) $ subscribe_ smp sub `catchError` handleError connId where - subscribe_ :: SMPClient -> (ConnId, RcvQueue) -> ExceptT SMPClientError IO () + subscribe_ :: SMPClient -> (ConnId, RcvQueue) -> ExceptT ProtocolClientError IO () subscribe_ smp (connId, rq@RcvQueue {rcvPrivateKey, rcvId}) = do subscribeSMPQueue smp rcvPrivateKey rcvId addSubscription c rq connId liftIO $ notifySub UP connId - handleError :: ConnId -> SMPClientError -> ExceptT SMPClientError IO () + handleError :: ConnId -> ProtocolClientError -> ExceptT ProtocolClientError IO () handleError connId = \case - e@SMPResponseTimeout -> throwError e - e@SMPNetworkError -> throwError e + e@PCEResponseTimeout -> throwError e + e@PCENetworkError -> throwError e e -> do - liftIO $ notifySub (ERR $ smpClientError e) connId + liftIO $ notifySub (ERR $ protocolClientError e) connId atomically $ removePendingSubscription c srv connId notifySub :: ACommand 'Agent -> ConnId -> IO () notifySub cmd connId = atomically $ writeTBQueue (subQ c) ("", connId, cmd) -closeAgentClient :: MonadUnliftIO m => AgentClient -> m () +getNtfServerClient :: forall m. AgentMonad m => AgentClient -> ProtocolServer -> m NtfClient +getNtfServerClient c@AgentClient {ntfClients} srv = + atomically (getClientVar srv ntfClients) + >>= either + (newProtocolClient c srv ntfClients connectClient $ pure ()) + (waitForProtocolClient ntfCfg) + where + connectClient :: m NtfClient + connectClient = do + cfg <- asks $ ntfCfg . config + liftEitherError protocolClientError (getProtocolClient srv cfg Nothing clientDisconnected) + + clientDisconnected :: IO () + clientDisconnected = do + atomically $ TM.delete srv ntfClients + logInfo . decodeUtf8 $ "Agent disconnected from " <> showServer srv + +getClientVar :: forall a. ProtocolServer -> TMap ProtocolServer (TMVar a) -> STM (Either (TMVar a) (TMVar a)) +getClientVar srv clients = maybe (Left <$> newClientVar) (pure . Right) =<< TM.lookup srv clients + where + newClientVar :: STM (TMVar a) + newClientVar = do + var <- newEmptyTMVar + TM.insert srv var clients + pure var + +waitForProtocolClient :: AgentMonad m => (AgentConfig -> ProtocolClientConfig) -> ClientVar msg -> m (ProtocolClient msg) +waitForProtocolClient clientConfig clientVar = do + ProtocolClientConfig {tcpTimeout} <- asks $ clientConfig . config + client_ <- liftIO $ tcpTimeout `timeout` atomically (readTMVar clientVar) + liftEither $ case client_ of + Just (Right smpClient) -> Right smpClient + Just (Left e) -> Left e + Nothing -> Left $ BROKER TIMEOUT + +newProtocolClient :: + forall msg m. + AgentMonad m => + AgentClient -> + ProtocolServer -> + TMap ProtocolServer (ClientVar msg) -> + m (ProtocolClient msg) -> + m () -> + ClientVar msg -> + m (ProtocolClient msg) +newProtocolClient c srv clients connectClient reconnectClient clientVar = tryConnectClient pure tryConnectAsync + where + tryConnectClient :: (ProtocolClient msg -> m a) -> m () -> m a + tryConnectClient successAction retryAction = + tryError connectClient >>= \r -> case r of + Right smp -> do + logInfo . decodeUtf8 $ "Agent connected to " <> showServer srv + atomically $ putTMVar clientVar r + successAction smp + Left e -> do + if e == BROKER NETWORK || e == BROKER TIMEOUT + then retryAction + else atomically $ do + putTMVar clientVar (Left e) + TM.delete srv clients + throwError e + tryConnectAsync :: m () + tryConnectAsync = do + a <- async connectAsync + atomically $ modifyTVar' (asyncClients c) (a :) + connectAsync :: m () + connectAsync = do + ri <- asks $ reconnectInterval . config + withRetryInterval ri $ \loop -> void $ tryConnectClient (const reconnectClient) loop + +closeAgentClient :: MonadIO m => AgentClient -> m () closeAgentClient c = liftIO $ do closeSMPServerClients c cancelActions $ reconnections c @@ -260,7 +288,7 @@ closeSMPServerClients c = readTVarIO (smpClients c) >>= mapM_ (forkIO . closeCli where closeClient smpVar = atomically (readTMVar smpVar) >>= \case - Right smp -> closeSMPClient smp `E.catch` \(_ :: E.SomeException) -> pure () + Right smp -> closeProtocolClient smp `E.catch` \(_ :: E.SomeException) -> pure () _ -> pure () cancelActions :: Foldable f => TVar (f (Async ())) -> IO () @@ -272,40 +300,40 @@ withAgentLock AgentClient {lock} = (void . atomically $ takeTMVar lock) (atomically $ putTMVar lock ()) -withSMP_ :: forall a m. AgentMonad m => AgentClient -> SMPServer -> (SMPClient -> m a) -> m a -withSMP_ c srv action = - (getSMPServerClient c srv >>= action) `catchError` logServerError +withClient_ :: forall a m msg. (AgentMonad m, ProtocolServerClient msg) => AgentClient -> ProtocolServer -> (ProtocolClient msg -> m a) -> m a +withClient_ c srv action = (getProtocolServerClient c srv >>= action) `catchError` logServerError where logServerError :: AgentErrorType -> m a logServerError e = do logServer "<--" c srv "" $ bshow e throwError e -withLogSMP_ :: AgentMonad m => AgentClient -> SMPServer -> QueueId -> ByteString -> (SMPClient -> m a) -> m a -withLogSMP_ c srv qId cmdStr action = do +withLogClient_ :: (AgentMonad m, ProtocolServerClient msg) => AgentClient -> ProtocolServer -> QueueId -> ByteString -> (ProtocolClient msg -> m a) -> m a +withLogClient_ c srv qId cmdStr action = do logServer "-->" c srv qId cmdStr - res <- withSMP_ c srv action + res <- withClient_ c srv action logServer "<--" c srv qId "OK" return res -withSMP :: AgentMonad m => AgentClient -> SMPServer -> (SMPClient -> ExceptT SMPClientError IO a) -> m a -withSMP c srv action = withSMP_ c srv $ liftSMP . action +withClient :: (AgentMonad m, ProtocolServerClient msg) => AgentClient -> ProtocolServer -> (ProtocolClient msg -> ExceptT ProtocolClientError IO a) -> m a +withClient c srv action = withClient_ c srv $ liftClient . action -withLogSMP :: AgentMonad m => AgentClient -> SMPServer -> QueueId -> ByteString -> (SMPClient -> ExceptT SMPClientError IO a) -> m a -withLogSMP c srv qId cmdStr action = withLogSMP_ c srv qId cmdStr $ liftSMP . action +withLogClient :: (AgentMonad m, ProtocolServerClient msg) => AgentClient -> ProtocolServer -> QueueId -> ByteString -> (ProtocolClient msg -> ExceptT ProtocolClientError IO a) -> m a +withLogClient c srv qId cmdStr action = withLogClient_ c srv qId cmdStr $ liftClient . action -liftSMP :: AgentMonad m => ExceptT SMPClientError IO a -> m a -liftSMP = liftError smpClientError +liftClient :: AgentMonad m => ExceptT ProtocolClientError IO a -> m a +liftClient = liftError protocolClientError -smpClientError :: SMPClientError -> AgentErrorType -smpClientError = \case - SMPServerError e -> SMP e - SMPResponseError e -> BROKER $ RESPONSE e - SMPUnexpectedResponse -> BROKER UNEXPECTED - SMPResponseTimeout -> BROKER TIMEOUT - SMPNetworkError -> BROKER NETWORK - SMPTransportError e -> BROKER $ TRANSPORT e - e -> INTERNAL $ show e +protocolClientError :: ProtocolClientError -> AgentErrorType +protocolClientError = \case + PCEProtocolError e -> SMP e + PCEResponseError e -> BROKER $ RESPONSE e + PCEUnexpectedResponse -> BROKER UNEXPECTED + PCEResponseTimeout -> BROKER TIMEOUT + PCENetworkError -> BROKER NETWORK + PCETransportError e -> BROKER $ TRANSPORT e + e@PCESignatureError {} -> INTERNAL $ show e + e@PCEIOError {} -> INTERNAL $ show e newRcvQueue :: AgentMonad m => AgentClient -> SMPServer -> m (RcvQueue, SMPQueueUri) newRcvQueue c srv = @@ -324,7 +352,7 @@ newRcvQueue_ a c srv = do (e2eDhKey, e2ePrivKey) <- liftIO C.generateKeyPair' logServer "-->" c srv "" "NEW" QIK {rcvId, sndId, rcvPublicDhKey} <- - withSMP c srv $ \smp -> createSMPQueue smp rcvPrivateKey recipientKey dhKey + withClient c srv $ \smp -> createSMPQueue smp rcvPrivateKey recipientKey dhKey logServer "<--" c srv "" $ B.unwords ["IDS", logSecret rcvId, logSecret sndId] let rq = RcvQueue @@ -342,15 +370,15 @@ newRcvQueue_ a c srv = do subscribeQueue :: AgentMonad m => AgentClient -> RcvQueue -> ConnId -> m () subscribeQueue c rq@RcvQueue {server, rcvPrivateKey, rcvId} connId = do atomically $ addPendingSubscription c rq connId - withLogSMP c server rcvId "SUB" $ \smp -> do + withLogClient c server rcvId "SUB" $ \smp -> do liftIO (runExceptT $ subscribeSMPQueue smp rcvPrivateKey rcvId) >>= \case Left e -> do - atomically . when (e /= SMPNetworkError && e /= SMPResponseTimeout) $ + atomically . when (e /= PCENetworkError && e /= PCEResponseTimeout) $ removePendingSubscription c server connId throwError e Right _ -> addSubscription c rq connId -addSubscription :: MonadUnliftIO m => AgentClient -> RcvQueue -> ConnId -> m () +addSubscription :: MonadIO m => AgentClient -> RcvQueue -> ConnId -> m () addSubscription c rq@RcvQueue {server} connId = atomically $ do TM.insert connId server $ subscrConns c addSubs_ (subscrSrvrs c) rq connId @@ -365,7 +393,7 @@ addSubs_ ss rq@RcvQueue {server} connId = Just m -> TM.insert connId rq m _ -> TM.singleton connId rq >>= \m -> TM.insert server m ss -removeSubscription :: MonadUnliftIO m => AgentClient -> ConnId -> m () +removeSubscription :: MonadIO m => AgentClient -> ConnId -> m () removeSubscription c@AgentClient {subscrConns} connId = atomically $ do server_ <- TM.lookupDelete connId subscrConns mapM_ (\server -> removeSubs_ (subscrSrvrs c) server connId) server_ @@ -377,12 +405,12 @@ removeSubs_ :: TMap SMPServer (TMap ConnId RcvQueue) -> SMPServer -> ConnId -> S removeSubs_ ss server connId = TM.lookup server ss >>= mapM_ (TM.delete connId) -logServer :: AgentMonad m => ByteString -> AgentClient -> SMPServer -> QueueId -> ByteString -> m () +logServer :: MonadIO m => ByteString -> AgentClient -> SMPServer -> QueueId -> ByteString -> m () logServer dir AgentClient {clientId} srv qId cmdStr = logInfo . decodeUtf8 $ B.unwords ["A", "(" <> bshow clientId <> ")", dir, showServer srv, ":", logSecret qId, cmdStr] showServer :: SMPServer -> ByteString -showServer SMPServer {host, port} = +showServer ProtocolServer {host, port} = B.pack $ host <> if null port then "" else ':' : port logSecret :: ByteString -> ByteString @@ -390,17 +418,17 @@ logSecret bs = encode $ B.take 3 bs sendConfirmation :: forall m. AgentMonad m => AgentClient -> SndQueue -> ByteString -> m () sendConfirmation c sq@SndQueue {server, sndId, sndPublicKey = Just sndPublicKey, e2ePubKey = e2ePubKey@Just {}} agentConfirmation = - withLogSMP_ c server sndId "SEND " $ \smp -> do + withLogClient_ c server sndId "SEND " $ \smp -> do let clientMsg = SMP.ClientMessage (SMP.PHConfirmation sndPublicKey) agentConfirmation msg <- agentCbEncrypt sq e2ePubKey $ smpEncode clientMsg - liftSMP $ sendSMPMessage smp Nothing sndId msg + liftClient $ sendSMPMessage smp Nothing sndId msg sendConfirmation _ _ _ = throwError $ INTERNAL "sendConfirmation called without snd_queue public key(s) in the database" sendInvitation :: forall m. AgentMonad m => AgentClient -> Compatible SMPQueueInfo -> ConnectionRequestUri 'CMInvitation -> ConnInfo -> m () sendInvitation c (Compatible SMPQueueInfo {smpServer, senderId, dhPublicKey}) connReq connInfo = - withLogSMP_ c smpServer senderId "SEND " $ \smp -> do + withLogClient_ c smpServer senderId "SEND " $ \smp -> do msg <- mkInvitation - liftSMP $ sendSMPMessage smp Nothing senderId msg + liftClient $ sendSMPMessage smp Nothing senderId msg where mkInvitation :: m ByteString -- this is only encrypted with per-queue E2E, not with double ratchet @@ -411,30 +439,30 @@ sendInvitation c (Compatible SMPQueueInfo {smpServer, senderId, dhPublicKey}) co secureQueue :: AgentMonad m => AgentClient -> RcvQueue -> SndPublicVerifyKey -> m () secureQueue c RcvQueue {server, rcvId, rcvPrivateKey} senderKey = - withLogSMP c server rcvId "KEY " $ \smp -> + withLogClient c server rcvId "KEY " $ \smp -> secureSMPQueue smp rcvPrivateKey rcvId senderKey sendAck :: AgentMonad m => AgentClient -> RcvQueue -> m () sendAck c RcvQueue {server, rcvId, rcvPrivateKey} = - withLogSMP c server rcvId "ACK" $ \smp -> + withLogClient c server rcvId "ACK" $ \smp -> ackSMPMessage smp rcvPrivateKey rcvId suspendQueue :: AgentMonad m => AgentClient -> RcvQueue -> m () suspendQueue c RcvQueue {server, rcvId, rcvPrivateKey} = - withLogSMP c server rcvId "OFF" $ \smp -> + withLogClient c server rcvId "OFF" $ \smp -> suspendSMPQueue smp rcvPrivateKey rcvId deleteQueue :: AgentMonad m => AgentClient -> RcvQueue -> m () deleteQueue c RcvQueue {server, rcvId, rcvPrivateKey} = - withLogSMP c server rcvId "DEL" $ \smp -> + withLogClient c server rcvId "DEL" $ \smp -> deleteSMPQueue smp rcvPrivateKey rcvId sendAgentMessage :: forall m. AgentMonad m => AgentClient -> SndQueue -> ByteString -> m () sendAgentMessage c sq@SndQueue {server, sndId, sndPrivateKey} agentMsg = - withLogSMP_ c server sndId "SEND " $ \smp -> do + withLogClient_ c server sndId "SEND " $ \smp -> do let clientMsg = SMP.ClientMessage SMP.PHEmpty agentMsg msg <- agentCbEncrypt sq Nothing $ smpEncode clientMsg - liftSMP $ sendSMPMessage smp (Just sndPrivateKey) sndId msg + liftClient $ sendSMPMessage smp (Just sndPrivateKey) sndId msg agentCbEncrypt :: AgentMonad m => SndQueue -> Maybe C.PublicKeyX25519 -> ByteString -> m ByteString agentCbEncrypt SndQueue {e2eDhSecret} e2ePubKey msg = do diff --git a/src/Simplex/Messaging/Agent/Env/SQLite.hs b/src/Simplex/Messaging/Agent/Env/SQLite.hs index a1f03cf6f..66f510ff6 100644 --- a/src/Simplex/Messaging/Agent/Env/SQLite.hs +++ b/src/Simplex/Messaging/Agent/Env/SQLite.hs @@ -23,6 +23,7 @@ import Simplex.Messaging.Agent.RetryInterval import Simplex.Messaging.Agent.Store.SQLite import qualified Simplex.Messaging.Agent.Store.SQLite.Migrations as Migrations import Simplex.Messaging.Client +import Simplex.Messaging.Client.Agent (SMPClientAgentConfig, defaultSMPClientAgentConfig) import qualified Simplex.Messaging.Crypto as C import System.Random (StdGen, newStdGen) import UnliftIO.STM @@ -36,7 +37,8 @@ data AgentConfig = AgentConfig dbFile :: FilePath, dbPoolSize :: Int, yesToMigrations :: Bool, - smpCfg :: SMPClientConfig, + smpCfg :: ProtocolClientConfig, + ntfCfg :: ProtocolClientConfig, reconnectInterval :: RetryInterval, helloTimeout :: NominalDiffTime, caCertificateFile :: FilePath, @@ -55,7 +57,8 @@ defaultAgentConfig = dbFile = "smp-agent.db", dbPoolSize = 4, yesToMigrations = False, - smpCfg = smpDefaultConfig, + smpCfg = defaultClientConfig, + ntfCfg = defaultClientConfig, reconnectInterval = RetryInterval { initialInterval = second, diff --git a/src/Simplex/Messaging/Agent/Protocol.hs b/src/Simplex/Messaging/Agent/Protocol.hs index fa1d46a8a..dbe47d908 100644 --- a/src/Simplex/Messaging/Agent/Protocol.hs +++ b/src/Simplex/Messaging/Agent/Protocol.hs @@ -7,6 +7,7 @@ {-# LANGUAGE LambdaCase #-} {-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE PatternSynonyms #-} {-# LANGUAGE PolyKinds #-} {-# LANGUAGE RankNTypes #-} {-# LANGUAGE ScopedTypeVariables #-} @@ -49,7 +50,8 @@ module Simplex.Messaging.Agent.Protocol AgentMessageType (..), APrivHeader (..), AMessage (..), - SMPServer (..), + SMPServer, + pattern SMPServer, SrvLoc (..), SMPQueueUri (..), SMPQueueInfo (..), @@ -131,9 +133,10 @@ import Simplex.Messaging.Protocol ( ErrorType, MsgBody, MsgId, - SMPServer (..), + SMPServer, SndPublicVerifyKey, SrvLoc (..), + pattern SMPServer, ) import qualified Simplex.Messaging.Protocol as SMP import Simplex.Messaging.Transport (Transport (..), TransportError, serializeTransportError, transportErrorP) diff --git a/src/Simplex/Messaging/Agent/Store/SQLite.hs b/src/Simplex/Messaging/Agent/Store/SQLite.hs index 295e28620..89a3c0e69 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite.hs @@ -9,6 +9,7 @@ {-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE NumericUnderscores #-} {-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE PatternSynonyms #-} {-# LANGUAGE QuasiQuotes #-} {-# LANGUAGE RecordWildCards #-} {-# LANGUAGE ScopedTypeVariables #-} @@ -59,7 +60,7 @@ import Simplex.Messaging.Crypto.Ratchet (RatchetX448, SkippedMsgDiff (..), Skipp import Simplex.Messaging.Encoding import Simplex.Messaging.Encoding.String import Simplex.Messaging.Parsers (blobFieldParser) -import Simplex.Messaging.Protocol (MsgBody) +import Simplex.Messaging.Protocol (MsgBody, ProtocolServer (..)) import qualified Simplex.Messaging.Protocol as SMP import Simplex.Messaging.Util (bshow, liftIOEither) import System.Directory (copyFile, createDirectoryIfMissing, doesFileExist) @@ -127,9 +128,26 @@ connectSQLiteStore dbFilePath poolSize = do connectDB :: FilePath -> IO DB.Connection connectDB path = do dbConn <- DB.open path - DB.execute_ dbConn "PRAGMA foreign_keys = ON; PRAGMA journal_mode = WAL;" + DB.execute_ dbConn "PRAGMA foreign_keys = ON;" + -- DB.execute_ dbConn "PRAGMA trusted_schema = OFF;" + DB.execute_ dbConn "PRAGMA secure_delete = ON;" + DB.execute_ dbConn "PRAGMA auto_vacuum = FULL;" + DB.execute_ dbConn "VACUUM;" + -- _printPragmas dbConn path pure dbConn +_printPragmas :: DB.Connection -> FilePath -> IO () +_printPragmas db path = do + foreign_keys <- DB.query_ db "PRAGMA foreign_keys;" :: IO [[Int]] + print $ path <> " foreign_keys: " <> show foreign_keys + -- when run via sqlite-simple query for trusted_schema seems to return empty list + trusted_schema <- DB.query_ db "PRAGMA trusted_schema;" :: IO [[Int]] + print $ path <> " trusted_schema: " <> show trusted_schema + secure_delete <- DB.query_ db "PRAGMA secure_delete;" :: IO [[Int]] + print $ path <> " secure_delete: " <> show secure_delete + auto_vacuum <- DB.query_ db "PRAGMA auto_vacuum;" :: IO [[Int]] + print $ path <> " auto_vacuum: " <> show auto_vacuum + checkConstraint :: StoreError -> IO (Either StoreError a) -> IO (Either StoreError a) checkConstraint err action = action `E.catch` (pure . Left . handleSQLError err) @@ -190,7 +208,7 @@ instance (MonadUnliftIO m, MonadError StoreError m) => MonadAgentStore SQLiteSto getConn_ db connId getRcvConn :: SQLiteStore -> SMPServer -> SMP.RecipientId -> m SomeConn - getRcvConn st SMPServer {host, port} rcvId = + getRcvConn st ProtocolServer {host, port} rcvId = liftIOEither . withTransaction st $ \db -> DB.queryNamed db @@ -235,7 +253,7 @@ instance (MonadUnliftIO m, MonadError StoreError m) => MonadAgentStore SQLiteSto _ -> pure $ Left SEConnNotFound setRcvQueueStatus :: SQLiteStore -> RcvQueue -> QueueStatus -> m () - setRcvQueueStatus st RcvQueue {rcvId, server = SMPServer {host, port}} status = + setRcvQueueStatus st RcvQueue {rcvId, server = ProtocolServer {host, port}} status = -- ? throw error if queue does not exist? liftIO . withTransaction st $ \db -> DB.executeNamed @@ -248,7 +266,7 @@ instance (MonadUnliftIO m, MonadError StoreError m) => MonadAgentStore SQLiteSto [":status" := status, ":host" := host, ":port" := port, ":rcv_id" := rcvId] setRcvQueueConfirmedE2E :: SQLiteStore -> RcvQueue -> C.DhSecretX25519 -> m () - setRcvQueueConfirmedE2E st RcvQueue {rcvId, server = SMPServer {host, port}} e2eDhSecret = + setRcvQueueConfirmedE2E st RcvQueue {rcvId, server = ProtocolServer {host, port}} e2eDhSecret = liftIO . withTransaction st $ \db -> DB.executeNamed db @@ -266,7 +284,7 @@ instance (MonadUnliftIO m, MonadError StoreError m) => MonadAgentStore SQLiteSto ] setSndQueueStatus :: SQLiteStore -> SndQueue -> QueueStatus -> m () - setSndQueueStatus st SndQueue {sndId, server = SMPServer {host, port}} status = + setSndQueueStatus st SndQueue {sndId, server = ProtocolServer {host, port}} status = -- ? throw error if queue does not exist? liftIO . withTransaction st $ \db -> DB.executeNamed @@ -640,7 +658,7 @@ instance (ToField a, ToField b, ToField c, ToField d, ToField e, ToField f, -- * Server upsert helper upsertServer_ :: DB.Connection -> SMPServer -> IO () -upsertServer_ dbConn SMPServer {host, port, keyHash} = do +upsertServer_ dbConn ProtocolServer {host, port, keyHash} = do DB.executeNamed dbConn [sql| diff --git a/src/Simplex/Messaging/Client.hs b/src/Simplex/Messaging/Client.hs index df28de2c2..db30d3085 100644 --- a/src/Simplex/Messaging/Client.hs +++ b/src/Simplex/Messaging/Client.hs @@ -1,6 +1,7 @@ {-# LANGUAGE DataKinds #-} {-# LANGUAGE DeriveAnyClass #-} {-# LANGUAGE DuplicateRecordFields #-} +{-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE LambdaCase #-} {-# LANGUAGE NamedFieldPuns #-} @@ -23,9 +24,10 @@ -- See https://github.com/simplex-chat/simplexmq/blob/master/protocol/simplex-messaging.md module Simplex.Messaging.Client ( -- * Connect (disconnect) client to (from) SMP server + ProtocolClient, SMPClient, - getSMPClient, - closeSMPClient, + getProtocolClient, + closeProtocolClient, -- * SMP protocol command functions createSMPQueue, @@ -37,13 +39,13 @@ module Simplex.Messaging.Client ackSMPMessage, suspendSMPQueue, deleteSMPQueue, - sendSMPCommand, + sendProtocolCommand, -- * Supporting types and client configuration - SMPClientError (..), - SMPClientConfig (..), - smpDefaultConfig, - SMPServerTransmission, + ProtocolClientError (..), + ProtocolClientConfig (..), + defaultClientConfig, + ServerTransmission, ) where @@ -60,7 +62,7 @@ import Data.Maybe (fromMaybe) import Network.Socket (ServiceName) import Numeric.Natural import qualified Simplex.Messaging.Crypto as C -import Simplex.Messaging.Protocol +import Simplex.Messaging.Protocol as SMP import Simplex.Messaging.TMap (TMap) import qualified Simplex.Messaging.TMap as TM import Simplex.Messaging.Transport (ATransport (..), THandle (..), TLS, TProxy, Transport (..), TransportError) @@ -72,31 +74,30 @@ import System.Timeout (timeout) -- | 'SMPClient' is a handle used to send commands to a specific SMP server. -- --- The only exported selector is blockSize that is negotiated --- with the server during the TCP transport handshake. --- -- Use 'getSMPClient' to connect to an SMP server and create a client handle. -data SMPClient = SMPClient +data ProtocolClient msg = ProtocolClient { action :: Async (), connected :: TVar Bool, sessionId :: ByteString, - smpServer :: SMPServer, + protocolServer :: ProtocolServer, tcpTimeout :: Int, clientCorrId :: TVar Natural, - sentCommands :: TMap CorrId Request, + sentCommands :: TMap CorrId (Request msg), sndQ :: TBQueue SentRawTransmission, - rcvQ :: TBQueue (SignedTransmission BrokerMsg), - msgQ :: TBQueue SMPServerTransmission + rcvQ :: TBQueue (SignedTransmission msg), + msgQ :: Maybe (TBQueue (ServerTransmission msg)) } --- | Type synonym for transmission from some SPM server queue. -type SMPServerTransmission = (SMPServer, RecipientId, BrokerMsg) +type SMPClient = ProtocolClient SMP.BrokerMsg --- | SMP client configuration. -data SMPClientConfig = SMPClientConfig +-- | Type synonym for transmission from some SPM server queue. +type ServerTransmission msg = (ProtocolServer, QueueId, msg) + +-- | protocol client configuration. +data ProtocolClientConfig = ProtocolClientConfig { -- | size of TBQueue to use for server commands and responses qSize :: Natural, - -- | default SMP server port if port is not specified in SMPServer + -- | default server port if port is not specified in ProtocolServer defaultTransport :: (ServiceName, ATransport), -- | timeout of TCP commands (microseconds) tcpTimeout :: Int, @@ -106,34 +107,35 @@ data SMPClientConfig = SMPClientConfig smpPing :: Int } --- | Default SMP client configuration. -smpDefaultConfig :: SMPClientConfig -smpDefaultConfig = - SMPClientConfig +-- | Default protocol client configuration. +defaultClientConfig :: ProtocolClientConfig +defaultClientConfig = + ProtocolClientConfig { qSize = 64, defaultTransport = ("5223", transport @TLS), - tcpTimeout = 4_000_000, + tcpTimeout = 5_000_000, tcpKeepAlive = Just defaultKeepAliveOpts, - smpPing = 600_000_000 -- 10min + smpPing = 300_000_000 -- 5 min } -data Request = Request +data Request msg = Request { queueId :: QueueId, - responseVar :: TMVar Response + responseVar :: TMVar (Response msg) } -type Response = Either SMPClientError BrokerMsg +type Response msg = Either ProtocolClientError msg --- | Connects to 'SMPServer' using passed client configuration +-- | Connects to 'ProtocolServer' using passed client configuration -- and queue for messages and notifications. -- -- A single queue can be used for multiple 'SMPClient' instances, -- as 'SMPServerTransmission' includes server information. -getSMPClient :: SMPServer -> SMPClientConfig -> TBQueue SMPServerTransmission -> IO () -> IO (Either SMPClientError SMPClient) -getSMPClient smpServer cfg@SMPClientConfig {qSize, tcpTimeout, tcpKeepAlive, smpPing} msgQ disconnected = - atomically mkSMPClient >>= runClient useTransport +getProtocolClient :: forall msg. Protocol msg => ProtocolServer -> ProtocolClientConfig -> Maybe (TBQueue (ServerTransmission msg)) -> IO () -> IO (Either ProtocolClientError (ProtocolClient msg)) +getProtocolClient protocolServer cfg@ProtocolClientConfig {qSize, tcpTimeout, tcpKeepAlive, smpPing} msgQ disconnected = + (atomically mkSMPClient >>= runClient useTransport) + `catch` \(e :: IOException) -> pure . Left $ PCEIOError e where - mkSMPClient :: STM SMPClient + mkSMPClient :: STM (ProtocolClient msg) mkSMPClient = do connected <- newTVar False clientCorrId <- newTVar 0 @@ -141,11 +143,11 @@ getSMPClient smpServer cfg@SMPClientConfig {qSize, tcpTimeout, tcpKeepAlive, smp sndQ <- newTBQueue qSize rcvQ <- newTBQueue qSize return - SMPClient + ProtocolClient { action = undefined, sessionId = undefined, connected, - smpServer, + protocolServer, tcpTimeout, clientCorrId, sentCommands, @@ -154,50 +156,51 @@ getSMPClient smpServer cfg@SMPClientConfig {qSize, tcpTimeout, tcpKeepAlive, smp msgQ } - runClient :: (ServiceName, ATransport) -> SMPClient -> IO (Either SMPClientError SMPClient) + runClient :: (ServiceName, ATransport) -> ProtocolClient msg -> IO (Either ProtocolClientError (ProtocolClient msg)) runClient (port', ATransport t) c = do thVar <- newEmptyTMVarIO action <- async $ - runTransportClient (host smpServer) port' (keyHash smpServer) tcpKeepAlive (client t c thVar) - `finally` atomically (putTMVar thVar $ Left SMPNetworkError) + runTransportClient (host protocolServer) port' (keyHash protocolServer) tcpKeepAlive (client t c thVar) + `finally` atomically (putTMVar thVar $ Left PCENetworkError) th_ <- tcpTimeout `timeout` atomically (takeTMVar thVar) pure $ case th_ of Just (Right THandle {sessionId}) -> Right c {action, sessionId} Just (Left e) -> Left e - Nothing -> Left SMPNetworkError + Nothing -> Left PCENetworkError useTransport :: (ServiceName, ATransport) - useTransport = case port smpServer of + useTransport = case port protocolServer of "" -> defaultTransport cfg "80" -> ("80", transport @WS) p -> (p, transport @TLS) - client :: forall c. Transport c => TProxy c -> SMPClient -> TMVar (Either SMPClientError (THandle c)) -> c -> IO () + client :: forall c. Transport c => TProxy c -> ProtocolClient msg -> TMVar (Either ProtocolClientError (THandle c)) -> c -> IO () client _ c thVar h = - runExceptT (smpClientHandshake h $ keyHash smpServer) >>= \case - Left e -> atomically . putTMVar thVar . Left $ SMPTransportError e + runExceptT (smpClientHandshake h $ keyHash protocolServer) >>= \case + Left e -> atomically . putTMVar thVar . Left $ PCETransportError e Right th@THandle {sessionId} -> do atomically $ do writeTVar (connected c) True putTMVar thVar $ Right th - let c' = c {sessionId} :: SMPClient + let c' = c {sessionId} :: ProtocolClient msg + -- TODO remove ping if 0 is passed (or Nothing?) raceAny_ [send c' th, process c', receive c' th, ping c'] `finally` disconnected - send :: Transport c => SMPClient -> THandle c -> IO () - send SMPClient {sndQ} h = forever $ atomically (readTBQueue sndQ) >>= tPut h + send :: Transport c => ProtocolClient msg -> THandle c -> IO () + send ProtocolClient {sndQ} h = forever $ atomically (readTBQueue sndQ) >>= tPut h - receive :: Transport c => SMPClient -> THandle c -> IO () - receive SMPClient {rcvQ} h = forever $ tGet h >>= atomically . writeTBQueue rcvQ + receive :: Transport c => ProtocolClient msg -> THandle c -> IO () + receive ProtocolClient {rcvQ} h = forever $ tGet h >>= atomically . writeTBQueue rcvQ - ping :: SMPClient -> IO () + ping :: ProtocolClient msg -> IO () ping c = forever $ do threadDelay smpPing - runExceptT $ sendSMPCommand c Nothing "" PING + void . either throwIO pure =<< runExceptT (sendProtocolCommand c Nothing "" protocolPing) - process :: SMPClient -> IO () - process SMPClient {rcvQ, sentCommands} = forever $ do + process :: ProtocolClient msg -> IO () + process ProtocolClient {rcvQ, sentCommands} = forever $ do (_, _, (corrId, qId, respOrErr)) <- atomically $ readTBQueue rcvQ if B.null $ bs corrId then sendMsg qId respOrErr @@ -209,45 +212,48 @@ getSMPClient smpServer cfg@SMPClientConfig {qSize, tcpTimeout, tcpKeepAlive, smp putTMVar responseVar $ if queueId == qId then case respOrErr of - Left e -> Left $ SMPResponseError e - Right (ERR e) -> Left $ SMPServerError e - Right r -> Right r - else Left SMPUnexpectedResponse + Left e -> Left $ PCEResponseError e + Right r -> case protocolError r of + Just e -> Left $ PCEProtocolError e + _ -> Right r + else Left PCEUnexpectedResponse - sendMsg :: QueueId -> Either ErrorType BrokerMsg -> IO () + sendMsg :: QueueId -> Either ErrorType msg -> IO () sendMsg qId = \case - Right cmd -> atomically $ writeTBQueue msgQ (smpServer, qId, cmd) + Right cmd -> atomically $ mapM_ (`writeTBQueue` (protocolServer, qId, cmd)) msgQ -- TODO send everything else to errQ and log in agent _ -> return () --- | Disconnects SMP client from the server and terminates client threads. -closeSMPClient :: SMPClient -> IO () -closeSMPClient = uninterruptibleCancel . action +-- | Disconnects client from the server and terminates client threads. +closeProtocolClient :: ProtocolClient msg -> IO () +closeProtocolClient = uninterruptibleCancel . action -- | SMP client error type. -data SMPClientError +data ProtocolClientError = -- | Correctly parsed SMP server ERR response. -- This error is forwarded to the agent client as `ERR SMP err`. - SMPServerError ErrorType + PCEProtocolError ErrorType | -- | Invalid server response that failed to parse. -- Forwarded to the agent client as `ERR BROKER RESPONSE`. - SMPResponseError ErrorType + PCEResponseError ErrorType | -- | Different response from what is expected to a certain SMP command, -- e.g. server should respond `IDS` or `ERR` to `NEW` command, -- other responses would result in this error. -- Forwarded to the agent client as `ERR BROKER UNEXPECTED`. - SMPUnexpectedResponse + PCEUnexpectedResponse | -- | Used for TCP connection and command response timeouts. -- Forwarded to the agent client as `ERR BROKER TIMEOUT`. - SMPResponseTimeout + PCEResponseTimeout | -- | Failure to establish TCP connection. -- Forwarded to the agent client as `ERR BROKER NETWORK`. - SMPNetworkError + PCENetworkError | -- | TCP transport handshake or some other transport error. -- Forwarded to the agent client as `ERR BROKER TRANSPORT e`. - SMPTransportError TransportError + PCETransportError TransportError | -- | Error when cryptographically "signing" the command. - SMPSignatureError C.CryptoError + PCESignatureError C.CryptoError + | -- | IO Error + PCEIOError IOException deriving (Eq, Show, Exception) -- | Create a new SMP queue. @@ -258,92 +264,95 @@ createSMPQueue :: RcvPrivateSignKey -> RcvPublicVerifyKey -> RcvPublicDhKey -> - ExceptT SMPClientError IO QueueIdsKeys + ExceptT ProtocolClientError IO QueueIdsKeys createSMPQueue c rpKey rKey dhKey = sendSMPCommand c (Just rpKey) "" (NEW rKey dhKey) >>= \case IDS qik -> pure qik - _ -> throwE SMPUnexpectedResponse + _ -> throwE PCEUnexpectedResponse -- | Subscribe to the SMP queue. -- -- https://github.com/simplex-chat/simplexmq/blob/master/protocol/simplex-messaging.md#subscribe-to-queue -subscribeSMPQueue :: SMPClient -> RcvPrivateSignKey -> RecipientId -> ExceptT SMPClientError IO () -subscribeSMPQueue c@SMPClient {smpServer, msgQ} rpKey rId = +subscribeSMPQueue :: SMPClient -> RcvPrivateSignKey -> RecipientId -> ExceptT ProtocolClientError IO () +subscribeSMPQueue c@ProtocolClient {protocolServer, msgQ} rpKey rId = sendSMPCommand c (Just rpKey) rId SUB >>= \case OK -> return () cmd@MSG {} -> - lift . atomically $ writeTBQueue msgQ (smpServer, rId, cmd) - _ -> throwE SMPUnexpectedResponse + lift . atomically $ mapM_ (`writeTBQueue` (protocolServer, rId, cmd)) msgQ + _ -> throwE PCEUnexpectedResponse -- | Subscribe to the SMP queue notifications. -- -- https://github.com/simplex-chat/simplexmq/blob/master/protocol/simplex-messaging.md#subscribe-to-queue-notifications -subscribeSMPQueueNotifications :: SMPClient -> NtfPrivateSignKey -> NotifierId -> ExceptT SMPClientError IO () +subscribeSMPQueueNotifications :: SMPClient -> NtfPrivateSignKey -> NotifierId -> ExceptT ProtocolClientError IO () subscribeSMPQueueNotifications = okSMPCommand NSUB -- | Secure the SMP queue by adding a sender public key. -- -- https://github.com/simplex-chat/simplexmq/blob/master/protocol/simplex-messaging.md#secure-queue-command -secureSMPQueue :: SMPClient -> RcvPrivateSignKey -> RecipientId -> SndPublicVerifyKey -> ExceptT SMPClientError IO () +secureSMPQueue :: SMPClient -> RcvPrivateSignKey -> RecipientId -> SndPublicVerifyKey -> ExceptT ProtocolClientError IO () secureSMPQueue c rpKey rId senderKey = okSMPCommand (KEY senderKey) c rpKey rId -- | Enable notifications for the queue for push notifications server. -- -- https://github.com/simplex-chat/simplexmq/blob/master/protocol/simplex-messaging.md#enable-notifications-command -enableSMPQueueNotifications :: SMPClient -> RcvPrivateSignKey -> RecipientId -> NtfPublicVerifyKey -> ExceptT SMPClientError IO NotifierId +enableSMPQueueNotifications :: SMPClient -> RcvPrivateSignKey -> RecipientId -> NtfPublicVerifyKey -> ExceptT ProtocolClientError IO NotifierId enableSMPQueueNotifications c rpKey rId notifierKey = sendSMPCommand c (Just rpKey) rId (NKEY notifierKey) >>= \case NID nId -> pure nId - _ -> throwE SMPUnexpectedResponse + _ -> throwE PCEUnexpectedResponse -- | Send SMP message. -- -- https://github.com/simplex-chat/simplexmq/blob/master/protocol/simplex-messaging.md#send-message -sendSMPMessage :: SMPClient -> Maybe SndPrivateSignKey -> SenderId -> MsgBody -> ExceptT SMPClientError IO () +sendSMPMessage :: SMPClient -> Maybe SndPrivateSignKey -> SenderId -> MsgBody -> ExceptT ProtocolClientError IO () sendSMPMessage c spKey sId msg = sendSMPCommand c spKey sId (SEND msg) >>= \case OK -> pure () - _ -> throwE SMPUnexpectedResponse + _ -> throwE PCEUnexpectedResponse -- | Acknowledge message delivery (server deletes the message). -- -- https://github.com/simplex-chat/simplexmq/blob/master/protocol/simplex-messaging.md#acknowledge-message-delivery -ackSMPMessage :: SMPClient -> RcvPrivateSignKey -> QueueId -> ExceptT SMPClientError IO () -ackSMPMessage c@SMPClient {smpServer, msgQ} rpKey rId = +ackSMPMessage :: SMPClient -> RcvPrivateSignKey -> QueueId -> ExceptT ProtocolClientError IO () +ackSMPMessage c@ProtocolClient {protocolServer, msgQ} rpKey rId = sendSMPCommand c (Just rpKey) rId ACK >>= \case OK -> return () cmd@MSG {} -> - lift . atomically $ writeTBQueue msgQ (smpServer, rId, cmd) - _ -> throwE SMPUnexpectedResponse + lift . atomically $ mapM_ (`writeTBQueue` (protocolServer, rId, cmd)) msgQ + _ -> throwE PCEUnexpectedResponse -- | Irreversibly suspend SMP queue. -- The existing messages from the queue will still be delivered. -- -- https://github.com/simplex-chat/simplexmq/blob/master/protocol/simplex-messaging.md#suspend-queue -suspendSMPQueue :: SMPClient -> RcvPrivateSignKey -> QueueId -> ExceptT SMPClientError IO () +suspendSMPQueue :: SMPClient -> RcvPrivateSignKey -> QueueId -> ExceptT ProtocolClientError IO () suspendSMPQueue = okSMPCommand OFF -- | Irreversibly delete SMP queue and all messages in it. -- -- https://github.com/simplex-chat/simplexmq/blob/master/protocol/simplex-messaging.md#delete-queue -deleteSMPQueue :: SMPClient -> RcvPrivateSignKey -> QueueId -> ExceptT SMPClientError IO () +deleteSMPQueue :: SMPClient -> RcvPrivateSignKey -> QueueId -> ExceptT ProtocolClientError IO () deleteSMPQueue = okSMPCommand DEL -okSMPCommand :: PartyI p => Command p -> SMPClient -> C.APrivateSignKey -> QueueId -> ExceptT SMPClientError IO () +okSMPCommand :: PartyI p => Command p -> SMPClient -> C.APrivateSignKey -> QueueId -> ExceptT ProtocolClientError IO () okSMPCommand cmd c pKey qId = sendSMPCommand c (Just pKey) qId cmd >>= \case OK -> return () - _ -> throwE SMPUnexpectedResponse + _ -> throwE PCEUnexpectedResponse -- | Send SMP command --- TODO sign all requests (SEND of SMP confirmation would be signed with the same key that is passed to the recipient) -sendSMPCommand :: PartyI p => SMPClient -> Maybe C.APrivateSignKey -> QueueId -> Command p -> ExceptT SMPClientError IO BrokerMsg -sendSMPCommand SMPClient {sndQ, sentCommands, clientCorrId, sessionId, tcpTimeout} pKey qId cmd = do +sendSMPCommand :: PartyI p => SMPClient -> Maybe C.APrivateSignKey -> QueueId -> Command p -> ExceptT ProtocolClientError IO BrokerMsg +sendSMPCommand c pKey qId = sendProtocolCommand c pKey qId . Cmd sParty + +-- | Send Protocol command +sendProtocolCommand :: forall msg. ProtocolEncoding (ProtocolCommand msg) => ProtocolClient msg -> Maybe C.APrivateSignKey -> QueueId -> ProtocolCommand msg -> ExceptT ProtocolClientError IO msg +sendProtocolCommand ProtocolClient {sndQ, sentCommands, clientCorrId, sessionId, tcpTimeout} pKey qId cmd = do corrId <- lift_ getNextCorrId t <- signTransmission $ encodeTransmission sessionId (corrId, qId, cmd) ExceptT $ sendRecv corrId t where - lift_ :: STM a -> ExceptT SMPClientError IO a + lift_ :: STM a -> ExceptT ProtocolClientError IO a lift_ action = ExceptT $ Right <$> atomically action getNextCorrId :: STM CorrId @@ -351,20 +360,20 @@ sendSMPCommand SMPClient {sndQ, sentCommands, clientCorrId, sessionId, tcpTimeou i <- stateTVar clientCorrId $ \i -> (i, i + 1) pure . CorrId $ bshow i - signTransmission :: ByteString -> ExceptT SMPClientError IO SentRawTransmission + signTransmission :: ByteString -> ExceptT ProtocolClientError IO SentRawTransmission signTransmission t = case pKey of Nothing -> return (Nothing, t) Just pk -> do - sig <- liftError SMPSignatureError $ C.sign pk t + sig <- liftError PCESignatureError $ C.sign pk t return (Just sig, t) -- two separate "atomically" needed to avoid blocking - sendRecv :: CorrId -> SentRawTransmission -> IO Response + sendRecv :: CorrId -> SentRawTransmission -> IO (Response msg) sendRecv corrId t = atomically (send corrId t) >>= withTimeout . atomically . takeTMVar where - withTimeout a = fromMaybe (Left SMPResponseTimeout) <$> timeout tcpTimeout a + withTimeout a = fromMaybe (Left PCEResponseTimeout) <$> timeout tcpTimeout a - send :: CorrId -> SentRawTransmission -> STM (TMVar Response) + send :: CorrId -> SentRawTransmission -> STM (TMVar (Response msg)) send corrId t = do r <- newEmptyTMVar TM.insert corrId (Request qId r) sentCommands diff --git a/src/Simplex/Messaging/Client/Agent.hs b/src/Simplex/Messaging/Client/Agent.hs new file mode 100644 index 000000000..7a62dc465 --- /dev/null +++ b/src/Simplex/Messaging/Client/Agent.hs @@ -0,0 +1,301 @@ +{-# LANGUAGE DuplicateRecordFields #-} +{-# LANGUAGE InstanceSigs #-} +{-# LANGUAGE LambdaCase #-} +{-# LANGUAGE NamedFieldPuns #-} +{-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE RankNTypes #-} +{-# LANGUAGE ScopedTypeVariables #-} +{-# OPTIONS_GHC -fno-warn-orphans #-} + +module Simplex.Messaging.Client.Agent where + +import Control.Concurrent (forkIO) +import Control.Concurrent.Async (Async, uninterruptibleCancel) +import Control.Logger.Simple +import Control.Monad.Except +import Control.Monad.IO.Unlift +import Control.Monad.Trans.Except +import Data.ByteString.Char8 (ByteString) +import qualified Data.ByteString.Char8 as B +import Data.Map.Strict (Map) +import qualified Data.Map.Strict as M +import Data.Set (Set) +import Data.Text.Encoding +import Numeric.Natural +import Simplex.Messaging.Agent.RetryInterval +import Simplex.Messaging.Client +import qualified Simplex.Messaging.Crypto as C +import Simplex.Messaging.Protocol (BrokerMsg, ProtocolServer (..), QueueId, SMPServer) +import Simplex.Messaging.TMap (TMap) +import qualified Simplex.Messaging.TMap as TM +import Simplex.Messaging.Util (tryE, whenM) +import System.Timeout (timeout) +import UnliftIO (async, forConcurrently_) +import UnliftIO.Exception (Exception) +import qualified UnliftIO.Exception as E +import UnliftIO.STM + +type SMPClientVar = TMVar (Either ProtocolClientError SMPClient) + +data SMPClientAgentEvent + = CAConnected SMPServer + | CADisconnected SMPServer (Set SMPSub) + | CAReconnected SMPServer + | CAResubscribed SMPServer SMPSub + | CASubError SMPServer SMPSub ProtocolClientError + +data SMPSubParty = SPRecipient | SPNotifier + deriving (Eq, Ord) + +type SMPSub = (SMPSubParty, QueueId) + +-- type SMPServerSub = (SMPServer, SMPSub) + +data SMPClientAgentConfig = SMPClientAgentConfig + { smpCfg :: ProtocolClientConfig, + reconnectInterval :: RetryInterval, + msgQSize :: Natural, + agentQSize :: Natural + } + +defaultSMPClientAgentConfig :: SMPClientAgentConfig +defaultSMPClientAgentConfig = + SMPClientAgentConfig + { smpCfg = defaultClientConfig, + reconnectInterval = + RetryInterval + { initialInterval = second, + increaseAfter = 10 * second, + maxInterval = 10 * second + }, + msgQSize = 64, + agentQSize = 64 + } + where + second = 1000000 + +data SMPClientAgent = SMPClientAgent + { agentCfg :: SMPClientAgentConfig, + msgQ :: TBQueue (ServerTransmission BrokerMsg), + agentQ :: TBQueue SMPClientAgentEvent, + smpClients :: TMap SMPServer SMPClientVar, + srvSubs :: TMap SMPServer (TMap SMPSub C.APrivateSignKey), + pendingSrvSubs :: TMap SMPServer (TMap SMPSub C.APrivateSignKey), + reconnections :: TVar [Async ()], + asyncClients :: TVar [Async ()] + } + +newtype InternalException e = InternalException {unInternalException :: e} + deriving (Eq, Show) + +instance Exception e => Exception (InternalException e) + +instance (MonadUnliftIO m, Exception e) => MonadUnliftIO (ExceptT e m) where + withRunInIO :: ((forall a. ExceptT e m a -> IO a) -> IO b) -> ExceptT e m b + withRunInIO exceptToIO = + withExceptT unInternalException . ExceptT . E.try $ + withRunInIO $ \run -> + exceptToIO $ run . (either (E.throwIO . InternalException) return <=< runExceptT) + +newSMPClientAgent :: SMPClientAgentConfig -> STM SMPClientAgent +newSMPClientAgent agentCfg@SMPClientAgentConfig {msgQSize, agentQSize} = do + msgQ <- newTBQueue msgQSize + agentQ <- newTBQueue agentQSize + smpClients <- TM.empty + srvSubs <- TM.empty + pendingSrvSubs <- TM.empty + reconnections <- newTVar [] + asyncClients <- newTVar [] + pure SMPClientAgent {agentCfg, msgQ, agentQ, smpClients, srvSubs, pendingSrvSubs, reconnections, asyncClients} + +getSMPServerClient' :: SMPClientAgent -> SMPServer -> ExceptT ProtocolClientError IO SMPClient +getSMPServerClient' ca@SMPClientAgent {agentCfg, smpClients, msgQ} srv = + atomically getClientVar >>= either newSMPClient waitForSMPClient + where + getClientVar :: STM (Either SMPClientVar SMPClientVar) + getClientVar = maybe (Left <$> newClientVar) (pure . Right) =<< TM.lookup srv smpClients + + newClientVar :: STM SMPClientVar + newClientVar = do + smpVar <- newEmptyTMVar + TM.insert srv smpVar smpClients + pure smpVar + + waitForSMPClient :: SMPClientVar -> ExceptT ProtocolClientError IO SMPClient + waitForSMPClient smpVar = do + let ProtocolClientConfig {tcpTimeout} = smpCfg agentCfg + smpClient_ <- liftIO $ tcpTimeout `timeout` atomically (readTMVar smpVar) + liftEither $ case smpClient_ of + Just (Right smpClient) -> Right smpClient + Just (Left e) -> Left e + Nothing -> Left PCEResponseTimeout + + newSMPClient :: SMPClientVar -> ExceptT ProtocolClientError IO SMPClient + newSMPClient smpVar = tryConnectClient pure tryConnectAsync + where + tryConnectClient :: (SMPClient -> ExceptT ProtocolClientError IO a) -> ExceptT ProtocolClientError IO () -> ExceptT ProtocolClientError IO a + tryConnectClient successAction retryAction = + tryE connectClient >>= \r -> case r of + Right smp -> do + logInfo . decodeUtf8 $ "Agent connected to " <> showServer srv + atomically $ putTMVar smpVar r + successAction smp + Left e -> do + if e == PCENetworkError || e == PCEResponseTimeout + then retryAction + else atomically $ do + putTMVar smpVar (Left e) + TM.delete srv smpClients + throwE e + tryConnectAsync :: ExceptT ProtocolClientError IO () + tryConnectAsync = do + a <- async connectAsync + atomically $ modifyTVar' (asyncClients ca) (a :) + connectAsync :: ExceptT ProtocolClientError IO () + connectAsync = + withRetryInterval (reconnectInterval agentCfg) $ \loop -> + void $ tryConnectClient (const reconnectClient) loop + + connectClient :: ExceptT ProtocolClientError IO SMPClient + connectClient = ExceptT $ getProtocolClient srv (smpCfg agentCfg) (Just msgQ) clientDisconnected + + clientDisconnected :: IO () + clientDisconnected = do + removeClientAndSubs >>= (`forM_` serverDown) + logInfo . decodeUtf8 $ "Agent disconnected from " <> showServer srv + + removeClientAndSubs :: IO (Maybe (Map SMPSub C.APrivateSignKey)) + removeClientAndSubs = atomically $ do + TM.delete srv smpClients + TM.lookupDelete srv (srvSubs ca) >>= mapM updateSubs + where + updateSubs sVar = do + ss <- readTVar sVar + addPendingSubs sVar ss + pure ss + + addPendingSubs sVar ss = do + let ps = pendingSrvSubs ca + TM.lookup srv ps >>= \case + Just v -> TM.union ss v + _ -> TM.insert srv sVar ps + + serverDown :: Map SMPSub C.APrivateSignKey -> IO () + serverDown ss = unless (M.null ss) . void . runExceptT $ do + notify . CADisconnected srv $ M.keysSet ss + reconnectServer + + reconnectServer :: ExceptT ProtocolClientError IO () + reconnectServer = do + a <- async tryReconnectClient + atomically $ modifyTVar' (reconnections ca) (a :) + + tryReconnectClient :: ExceptT ProtocolClientError IO () + tryReconnectClient = do + withRetryInterval (reconnectInterval agentCfg) $ \loop -> + reconnectClient `catchE` const loop + + reconnectClient :: ExceptT ProtocolClientError IO () + reconnectClient = do + withSMP ca srv $ \smp -> do + notify $ CAReconnected srv + cs <- atomically $ mapM readTVar =<< TM.lookup srv (pendingSrvSubs ca) + forConcurrently_ (maybe [] M.assocs cs) $ \sub@(s, _) -> + whenM (atomically $ hasSub (srvSubs ca) srv s) $ + subscribe_ smp sub `catchE` handleError s + where + subscribe_ :: SMPClient -> (SMPSub, C.APrivateSignKey) -> ExceptT ProtocolClientError IO () + subscribe_ smp sub@(s, _) = do + smpSubscribe smp sub + atomically $ addSubscription ca srv sub + notify $ CAResubscribed srv s + + handleError :: SMPSub -> ProtocolClientError -> ExceptT ProtocolClientError IO () + handleError s = \case + e@PCEResponseTimeout -> throwE e + e@PCENetworkError -> throwE e + e -> do + notify $ CASubError srv s e + atomically $ removePendingSubscription ca srv s + + notify :: SMPClientAgentEvent -> ExceptT ProtocolClientError IO () + notify evt = atomically $ writeTBQueue (agentQ ca) evt + +closeSMPClientAgent :: MonadUnliftIO m => SMPClientAgent -> m () +closeSMPClientAgent c = liftIO $ do + closeSMPServerClients c + cancelActions $ reconnections c + cancelActions $ asyncClients c + +closeSMPServerClients :: SMPClientAgent -> IO () +closeSMPServerClients c = readTVarIO (smpClients c) >>= mapM_ (forkIO . closeClient) + where + closeClient smpVar = + atomically (readTMVar smpVar) >>= \case + Right smp -> closeProtocolClient smp `E.catch` \(_ :: E.SomeException) -> pure () + _ -> pure () + +cancelActions :: Foldable f => TVar (f (Async ())) -> IO () +cancelActions as = readTVarIO as >>= mapM_ uninterruptibleCancel + +withSMP :: SMPClientAgent -> SMPServer -> (SMPClient -> ExceptT ProtocolClientError IO a) -> ExceptT ProtocolClientError IO a +withSMP ca srv action = (getSMPServerClient' ca srv >>= action) `catchE` logSMPError + where + logSMPError :: ProtocolClientError -> ExceptT ProtocolClientError IO a + logSMPError e = do + liftIO $ putStrLn $ "SMP error (" <> show srv <> "): " <> show e + throwE e + +subscribeQueue :: SMPClientAgent -> SMPServer -> (SMPSub, C.APrivateSignKey) -> ExceptT ProtocolClientError IO () +subscribeQueue ca srv sub = do + atomically $ addPendingSubscription ca srv sub + withSMP ca srv $ \smp -> subscribe_ smp `catchE` handleError + where + subscribe_ smp = do + smpSubscribe smp sub + atomically $ addSubscription ca srv sub + + handleError e = do + atomically . when (e /= PCENetworkError && e /= PCEResponseTimeout) $ + removePendingSubscription ca srv $ fst sub + throwE e + +showServer :: SMPServer -> ByteString +showServer ProtocolServer {host, port} = + B.pack $ host <> if null port then "" else ':' : port + +smpSubscribe :: SMPClient -> (SMPSub, C.APrivateSignKey) -> ExceptT ProtocolClientError IO () +smpSubscribe smp ((party, queueId), privKey) = subscribe_ smp privKey queueId + where + subscribe_ = case party of + SPRecipient -> subscribeSMPQueue + SPNotifier -> subscribeSMPQueueNotifications + +addSubscription :: SMPClientAgent -> SMPServer -> (SMPSub, C.APrivateSignKey) -> STM () +addSubscription ca srv sub = do + addSub_ (srvSubs ca) srv sub + removePendingSubscription ca srv $ fst sub + +addPendingSubscription :: SMPClientAgent -> SMPServer -> (SMPSub, C.APrivateSignKey) -> STM () +addPendingSubscription = addSub_ . pendingSrvSubs + +addSub_ :: TMap SMPServer (TMap SMPSub C.APrivateSignKey) -> SMPServer -> (SMPSub, C.APrivateSignKey) -> STM () +addSub_ subs srv (s, key) = + TM.lookup srv subs >>= \case + Just m -> TM.insert s key m + _ -> TM.singleton s key >>= \v -> TM.insert srv v subs + +removeSubscription :: SMPClientAgent -> SMPServer -> SMPSub -> STM () +removeSubscription = removeSub_ . srvSubs + +removePendingSubscription :: SMPClientAgent -> SMPServer -> SMPSub -> STM () +removePendingSubscription = removeSub_ . pendingSrvSubs + +removeSub_ :: TMap SMPServer (TMap SMPSub C.APrivateSignKey) -> SMPServer -> SMPSub -> STM () +removeSub_ subs srv s = TM.lookup srv subs >>= mapM_ (TM.delete s) + +getSubKey :: TMap SMPServer (TMap SMPSub C.APrivateSignKey) -> SMPServer -> SMPSub -> STM (Maybe C.APrivateSignKey) +getSubKey subs srv s = fmap join . mapM (TM.lookup s) =<< TM.lookup srv subs + +hasSub :: TMap SMPServer (TMap SMPSub C.APrivateSignKey) -> SMPServer -> SMPSub -> STM Bool +hasSub subs srv s = maybe (pure False) (TM.member s) =<< TM.lookup srv subs diff --git a/src/Simplex/Messaging/Notifications/Client.hs b/src/Simplex/Messaging/Notifications/Client.hs new file mode 100644 index 000000000..48fc89b1e --- /dev/null +++ b/src/Simplex/Messaging/Notifications/Client.hs @@ -0,0 +1,55 @@ +{-# LANGUAGE DataKinds #-} +{-# LANGUAGE LambdaCase #-} +{-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE ScopedTypeVariables #-} + +module Simplex.Messaging.Notifications.Client where + +import Control.Monad.Except +import Control.Monad.Trans.Except +import Data.Word (Word16) +import Simplex.Messaging.Client +import qualified Simplex.Messaging.Crypto as C +import Simplex.Messaging.Notifications.Protocol + +type NtfClient = ProtocolClient NtfResponse + +registerNtfToken :: NtfClient -> C.APrivateSignKey -> NewNtfEntity 'Token -> ExceptT ProtocolClientError IO (NtfTokenId, C.PublicKeyX25519) +registerNtfToken c pKey newTkn = + sendNtfCommand c (Just pKey) "" (TNEW newTkn) >>= \case + NRId tknId dhKey -> pure (tknId, dhKey) + _ -> throwE PCEUnexpectedResponse + +verifyNtfToken :: NtfClient -> C.APrivateSignKey -> NtfTokenId -> NtfRegistrationCode -> ExceptT ProtocolClientError IO () +verifyNtfToken c pKey tknId code = okNtfCommand (TVFY code) c pKey tknId + +deleteNtfToken :: NtfClient -> C.APrivateSignKey -> NtfTokenId -> ExceptT ProtocolClientError IO () +deleteNtfToken = okNtfCommand TDEL + +enableNtfCron :: NtfClient -> C.APrivateSignKey -> NtfTokenId -> Word16 -> ExceptT ProtocolClientError IO () +enableNtfCron c pKey tknId int = okNtfCommand (TCRN int) c pKey tknId + +createNtfSubsciption :: NtfClient -> C.APrivateSignKey -> NewNtfEntity 'Subscription -> ExceptT ProtocolClientError IO (NtfSubscriptionId, C.PublicKeyX25519) +createNtfSubsciption c pKey newSub = + sendNtfCommand c (Just pKey) "" (SNEW newSub) >>= \case + NRId tknId dhKey -> pure (tknId, dhKey) + _ -> throwE PCEUnexpectedResponse + +checkNtfSubscription :: NtfClient -> C.APrivateSignKey -> NtfSubscriptionId -> ExceptT ProtocolClientError IO NtfSubStatus +checkNtfSubscription c pKey subId = + sendNtfCommand c (Just pKey) subId SCHK >>= \case + NRStat stat -> pure stat + _ -> throwE PCEUnexpectedResponse + +deleteNfgSubscription :: NtfClient -> C.APrivateSignKey -> NtfSubscriptionId -> ExceptT ProtocolClientError IO () +deleteNfgSubscription = okNtfCommand SDEL + +-- | Send notification server command +sendNtfCommand :: NtfEntityI e => NtfClient -> Maybe C.APrivateSignKey -> NtfEntityId -> NtfCommand e -> ExceptT ProtocolClientError IO NtfResponse +sendNtfCommand c pKey entId = sendProtocolCommand c pKey entId . NtfCmd sNtfEntity + +okNtfCommand :: NtfEntityI e => NtfCommand e -> NtfClient -> C.APrivateSignKey -> NtfEntityId -> ExceptT ProtocolClientError IO () +okNtfCommand cmd c pKey entId = + sendNtfCommand c (Just pKey) entId cmd >>= \case + NROk -> return () + _ -> throwE PCEUnexpectedResponse diff --git a/src/Simplex/Messaging/Notifications/Protocol.hs b/src/Simplex/Messaging/Notifications/Protocol.hs index da66280fd..1fac10e7e 100644 --- a/src/Simplex/Messaging/Notifications/Protocol.hs +++ b/src/Simplex/Messaging/Notifications/Protocol.hs @@ -1,6 +1,11 @@ +{-# LANGUAGE DataKinds #-} +{-# LANGUAGE GADTs #-} {-# LANGUAGE LambdaCase #-} {-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE ScopedTypeVariables #-} +{-# LANGUAGE StandaloneDeriving #-} +{-# LANGUAGE TypeApplications #-} {-# LANGUAGE TypeFamilies #-} module Simplex.Messaging.Notifications.Protocol where @@ -8,154 +13,333 @@ module Simplex.Messaging.Notifications.Protocol where import qualified Data.Attoparsec.ByteString.Char8 as A import Data.ByteString.Char8 (ByteString) import qualified Data.ByteString.Char8 as B +import Data.Kind import Data.Maybe (isNothing) +import Data.Type.Equality +import Data.Word (Word16) import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Encoding -import Simplex.Messaging.Protocol +import Simplex.Messaging.Protocol hiding (Command (..), CommandTag (..)) +import Simplex.Messaging.Util ((<$?>)) -data NtfCommandTag - = NCCreate_ - | NCCheck_ - | NCToken_ - | NCDelete_ +data NtfEntity = Token | Subscription deriving (Show) -instance Encoding NtfCommandTag where +data SNtfEntity :: NtfEntity -> Type where + SToken :: SNtfEntity 'Token + SSubscription :: SNtfEntity 'Subscription + +instance TestEquality SNtfEntity where + testEquality SToken SToken = Just Refl + testEquality SSubscription SSubscription = Just Refl + testEquality _ _ = Nothing + +deriving instance Show (SNtfEntity e) + +class NtfEntityI (e :: NtfEntity) where sNtfEntity :: SNtfEntity e + +instance NtfEntityI 'Token where sNtfEntity = SToken + +instance NtfEntityI 'Subscription where sNtfEntity = SSubscription + +data NtfCommandTag (e :: NtfEntity) where + TNEW_ :: NtfCommandTag 'Token + TVFY_ :: NtfCommandTag 'Token + TDEL_ :: NtfCommandTag 'Token + TCRN_ :: NtfCommandTag 'Token + SNEW_ :: NtfCommandTag 'Subscription + SCHK_ :: NtfCommandTag 'Subscription + SDEL_ :: NtfCommandTag 'Subscription + PING_ :: NtfCommandTag 'Subscription + +deriving instance Show (NtfCommandTag e) + +data NtfCmdTag = forall e. NtfEntityI e => NCT (SNtfEntity e) (NtfCommandTag e) + +instance NtfEntityI e => Encoding (NtfCommandTag e) where smpEncode = \case - NCCreate_ -> "CREATE" - NCCheck_ -> "CHECK" - NCToken_ -> "TOKEN" - NCDelete_ -> "DELETE" + TNEW_ -> "TNEW" + TVFY_ -> "TVFY" + TDEL_ -> "TDEL" + TCRN_ -> "TCRN" + SNEW_ -> "SNEW" + SCHK_ -> "SCHK" + SDEL_ -> "SDEL" + PING_ -> "PING" smpP = messageTagP -instance ProtocolMsgTag NtfCommandTag where +instance Encoding NtfCmdTag where + smpEncode (NCT _ t) = smpEncode t + smpP = messageTagP + +instance ProtocolMsgTag NtfCmdTag where decodeTag = \case - "CREATE" -> Just NCCreate_ - "CHECK" -> Just NCCheck_ - "TOKEN" -> Just NCToken_ - "DELETE" -> Just NCDelete_ + "TNEW" -> Just $ NCT SToken TNEW_ + "TVFY" -> Just $ NCT SToken TVFY_ + "TDEL" -> Just $ NCT SToken TDEL_ + "TCRN" -> Just $ NCT SToken TCRN_ + "SNEW" -> Just $ NCT SSubscription SNEW_ + "SCHK" -> Just $ NCT SSubscription SCHK_ + "SDEL" -> Just $ NCT SSubscription SDEL_ + "PING" -> Just $ NCT SSubscription PING_ _ -> Nothing -data NtfCommand - = NCCreate DeviceToken SMPQueueNtfUri C.APublicVerifyKey C.PublicKeyX25519 - | NCCheck - | NCToken DeviceToken - | NCDelete +instance NtfEntityI e => ProtocolMsgTag (NtfCommandTag e) where + decodeTag s = decodeTag s >>= (\(NCT _ t) -> checkEntity' t) -instance Protocol NtfCommand where - type Tag NtfCommand = NtfCommandTag +type NtfRegistrationCode = ByteString + +data NewNtfEntity (e :: NtfEntity) where + NewNtfTkn :: DeviceToken -> C.APublicVerifyKey -> C.PublicKeyX25519 -> NewNtfEntity 'Token + NewNtfSub :: NtfTokenId -> SMPQueueNtf -> NewNtfEntity 'Subscription + +data ANewNtfEntity = forall e. NtfEntityI e => ANE (SNtfEntity e) (NewNtfEntity e) + +instance NtfEntityI e => Encoding (NewNtfEntity e) where + smpEncode = \case + NewNtfTkn tkn verifyKey dhPubKey -> smpEncode ('T', tkn, verifyKey, dhPubKey) + NewNtfSub tknId smpQueue -> smpEncode ('S', tknId, smpQueue) + smpP = (\(ANE _ c) -> checkEntity c) <$?> smpP + +instance Encoding ANewNtfEntity where + smpEncode (ANE _ e) = smpEncode e + smpP = + A.anyChar >>= \case + 'T' -> ANE SToken <$> (NewNtfTkn <$> smpP <*> smpP <*> smpP) + 'S' -> ANE SSubscription <$> (NewNtfSub <$> smpP <*> smpP) + _ -> fail "bad ANewNtfEntity" + +instance Protocol NtfResponse where + type ProtocolCommand NtfResponse = NtfCmd + protocolPing = NtfCmd SSubscription PING + protocolError = \case + NRErr e -> Just e + _ -> Nothing + +data NtfCommand (e :: NtfEntity) where + -- | register new device token for notifications + TNEW :: NewNtfEntity 'Token -> NtfCommand 'Token + -- | verify token - uses e2e encrypted random string sent to the device via PN to confirm that the device has the token + TVFY :: NtfRegistrationCode -> NtfCommand 'Token + -- | delete token - all subscriptions will be removed and no more notifications will be sent + TDEL :: NtfCommand 'Token + -- | enable periodic background notification to fetch the new messages - interval is in minutes, minimum is 20, 0 to disable + TCRN :: Word16 -> NtfCommand 'Token + -- | create SMP subscription + SNEW :: NewNtfEntity 'Subscription -> NtfCommand 'Subscription + -- | check SMP subscription status (response is STAT) + SCHK :: NtfCommand 'Subscription + -- | delete SMP subscription + SDEL :: NtfCommand 'Subscription + -- | keep-alive command + PING :: NtfCommand 'Subscription + +data NtfCmd = forall e. NtfEntityI e => NtfCmd (SNtfEntity e) (NtfCommand e) + +instance NtfEntityI e => ProtocolEncoding (NtfCommand e) where + type Tag (NtfCommand e) = NtfCommandTag e encodeProtocol = \case - NCCreate token smpQueue verifyKey dhKey -> e (NCCreate_, ' ', token, smpQueue, verifyKey, dhKey) - NCCheck -> e NCCheck_ - NCToken token -> e (NCToken_, ' ', token) - NCDelete -> e NCDelete_ + TNEW newTkn -> e (TNEW_, ' ', newTkn) + TVFY code -> e (TVFY_, ' ', code) + TDEL -> e TDEL_ + TCRN int -> e (TCRN_, ' ', int) + SNEW newSub -> e (SNEW_, ' ', newSub) + SCHK -> e SCHK_ + SDEL -> e SDEL_ + PING -> e PING_ where e :: Encoding a => a -> ByteString e = smpEncode - protocolP = \case - NCCreate_ -> NCCreate <$> _smpP <*> smpP <*> smpP <*> smpP - NCCheck_ -> pure NCCheck - NCToken_ -> NCToken <$> _smpP - NCDelete_ -> pure NCDelete + protocolP tag = (\(NtfCmd _ c) -> checkEntity c) <$?> protocolP (NCT (sNtfEntity @e) tag) - checkCredentials (sig, _, subId, _) cmd = case cmd of - -- CREATE must have signature but NOT subscription ID - NCCreate {} - | isNothing sig -> Left $ CMD NO_AUTH - | not (B.null subId) -> Left $ CMD HAS_AUTH - | otherwise -> Right cmd - -- other client commands must have both signature and subscription ID + checkCredentials (sig, _, entityId, _) cmd = case cmd of + -- TNEW and SNEW must have signature but NOT token/subscription IDs + TNEW {} -> sigNoEntity + SNEW {} -> sigNoEntity + PING + | isNothing sig && B.null entityId -> Right cmd + | otherwise -> Left $ CMD HAS_AUTH + -- other client commands must have both signature and entity ID _ - | isNothing sig || B.null subId -> Left $ CMD NO_AUTH + | isNothing sig || B.null entityId -> Left $ CMD NO_AUTH | otherwise -> Right cmd + where + sigNoEntity + | isNothing sig = Left $ CMD NO_AUTH + | not (B.null entityId) = Left $ CMD HAS_AUTH + | otherwise = Right cmd + +instance ProtocolEncoding NtfCmd where + type Tag NtfCmd = NtfCmdTag + encodeProtocol (NtfCmd _ c) = encodeProtocol c + + protocolP = \case + NCT SToken tag -> + NtfCmd SToken <$> case tag of + TNEW_ -> TNEW <$> _smpP + TVFY_ -> TVFY <$> _smpP + TDEL_ -> pure TDEL + TCRN_ -> TCRN <$> _smpP + NCT SSubscription tag -> + NtfCmd SSubscription <$> case tag of + SNEW_ -> SNEW <$> _smpP + SCHK_ -> pure SCHK + SDEL_ -> pure SDEL + PING_ -> pure PING + + checkCredentials t (NtfCmd e c) = NtfCmd e <$> checkCredentials t c data NtfResponseTag - = NRSubId_ + = NRId_ | NROk_ | NRErr_ | NRStat_ + | NRPong_ deriving (Show) instance Encoding NtfResponseTag where smpEncode = \case - NRSubId_ -> "ID" + NRId_ -> "ID" NROk_ -> "OK" NRErr_ -> "ERR" NRStat_ -> "STAT" + NRPong_ -> "PONG" smpP = messageTagP instance ProtocolMsgTag NtfResponseTag where decodeTag = \case - "ID" -> Just NRSubId_ + "ID" -> Just NRId_ "OK" -> Just NROk_ "ERR" -> Just NRErr_ "STAT" -> Just NRStat_ + "PONG" -> Just NRPong_ _ -> Nothing data NtfResponse - = NRSubId C.PublicKeyX25519 + = NRId NtfEntityId C.PublicKeyX25519 | NROk | NRErr ErrorType - | NRStat NtfStatus + | NRStat NtfSubStatus + | NRPong -instance Protocol NtfResponse where +instance ProtocolEncoding NtfResponse where type Tag NtfResponse = NtfResponseTag encodeProtocol = \case - NRSubId dhKey -> e (NRSubId_, ' ', dhKey) + NRId entId dhKey -> e (NRId_, ' ', entId, dhKey) NROk -> e NROk_ NRErr err -> e (NRErr_, ' ', err) NRStat stat -> e (NRStat_, ' ', stat) + NRPong -> e NRPong_ where e :: Encoding a => a -> ByteString e = smpEncode protocolP = \case - NRSubId_ -> NRSubId <$> _smpP + NRId_ -> NRId <$> _smpP <*> smpP NROk_ -> pure NROk NRErr_ -> NRErr <$> _smpP NRStat_ -> NRStat <$> _smpP + NRPong_ -> pure NRPong - checkCredentials (_, _, subId, _) cmd = case cmd of - -- ERR response does not always have subscription ID + checkCredentials (_, _, entId, _) cmd = case cmd of + -- ID response must not have queue ID + NRId {} -> noEntity + -- ERR response does not always have entity ID NRErr _ -> Right cmd - -- other server responses must have subscription ID + -- PONG response must not have queue ID + NRPong -> noEntity + -- other server responses must have entity ID _ - | B.null subId -> Left $ CMD NO_ENTITY + | B.null entId -> Left $ CMD NO_ENTITY | otherwise -> Right cmd + where + noEntity + | B.null entId = Right cmd + | otherwise = Left $ CMD HAS_AUTH -data SMPQueueNtfUri = SMPQueueNtfUri - { smpServer :: SMPServer, +data SMPQueueNtf = SMPQueueNtf + { smpServer :: ProtocolServer, notifierId :: NotifierId, notifierKey :: NtfPrivateSignKey } -instance Encoding SMPQueueNtfUri where - smpEncode SMPQueueNtfUri {smpServer, notifierId, notifierKey} = smpEncode (smpServer, notifierId, notifierKey) +instance Encoding SMPQueueNtf where + smpEncode SMPQueueNtf {smpServer, notifierId, notifierKey} = smpEncode (smpServer, notifierId, notifierKey) smpP = do (smpServer, notifierId, notifierKey) <- smpP - pure $ SMPQueueNtfUri smpServer notifierId notifierKey + pure $ SMPQueueNtf smpServer notifierId notifierKey -newtype DeviceToken = DeviceToken ByteString +data PushPlatform = PPApple + +instance Encoding PushPlatform where + smpEncode = \case + PPApple -> "A" + smpP = + A.anyChar >>= \case + 'A' -> pure PPApple + _ -> fail "bad PushPlatform" + +data DeviceToken = DeviceToken PushPlatform ByteString instance Encoding DeviceToken where - smpEncode (DeviceToken t) = smpEncode t - smpP = DeviceToken <$> smpP + smpEncode (DeviceToken p t) = smpEncode (p, t) + smpP = DeviceToken <$> smpP <*> smpP -type NtfSubsciptionId = ByteString +type NtfEntityId = ByteString -data NtfStatus = NSPending | NSActive | NSEnd | NSSMPAuth +type NtfSubscriptionId = NtfEntityId -instance Encoding NtfStatus where +type NtfTokenId = NtfEntityId + +data NtfSubStatus + = -- | state after SNEW + NSNew + | -- | pending connection/subscription to SMP server + NSPending + | -- | connected and subscribed to SMP server + NSActive + | -- | NEND received (we currently do not support it) + NSEnd + | -- | SMP AUTH error + NSSMPAuth + deriving (Eq) + +instance Encoding NtfSubStatus where smpEncode = \case - NSPending -> "PENDING" + NSNew -> "NEW" + NSPending -> "PENDING" -- e.g. after SMP server disconnect/timeout while ntf server is retrying to connect NSActive -> "ACTIVE" NSEnd -> "END" NSSMPAuth -> "SMP_AUTH" smpP = A.takeTill (== ' ') >>= \case + "NEW" -> pure NSNew "PENDING" -> pure NSPending "ACTIVE" -> pure NSActive "END" -> pure NSEnd "SMP_AUTH" -> pure NSSMPAuth _ -> fail "bad NtfError" + +data NtfTknStatus + = -- | state after registration (TNEW) + NTNew + | -- | if initial notification or verification failed (push provider error) + NTInvalid + | -- | if initial notification succeeded + NTConfirmed + | -- | after successful verification (TVFY) + NTActive + | -- | after it is no longer valid (push provider error) + NTExpired + deriving (Eq) + +checkEntity :: forall t e e'. (NtfEntityI e, NtfEntityI e') => t e' -> Either String (t e) +checkEntity c = case testEquality (sNtfEntity @e) (sNtfEntity @e') of + Just Refl -> Right c + Nothing -> Left "bad command party" + +checkEntity' :: forall t p p'. (NtfEntityI p, NtfEntityI p') => t p' -> Maybe (t p) +checkEntity' c = case testEquality (sNtfEntity @p) (sNtfEntity @p') of + Just Refl -> Just c + _ -> Nothing diff --git a/src/Simplex/Messaging/Notifications/Server.hs b/src/Simplex/Messaging/Notifications/Server.hs index dfdaae801..13d4671e6 100644 --- a/src/Simplex/Messaging/Notifications/Server.hs +++ b/src/Simplex/Messaging/Notifications/Server.hs @@ -1,9 +1,12 @@ +{-# LANGUAGE DataKinds #-} {-# LANGUAGE DuplicateRecordFields #-} {-# LANGUAGE FlexibleContexts #-} +{-# LANGUAGE GADTs #-} {-# LANGUAGE LambdaCase #-} {-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE ScopedTypeVariables #-} +{-# LANGUAGE TupleSections #-} module Simplex.Messaging.Notifications.Server where @@ -12,13 +15,16 @@ import Control.Monad.IO.Unlift (MonadUnliftIO) import Control.Monad.Reader import Crypto.Random (MonadRandom) import Data.ByteString.Char8 (ByteString) +import Data.Functor (($>)) import Network.Socket (ServiceName) +import Simplex.Messaging.Client.Agent import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Notifications.Protocol import Simplex.Messaging.Notifications.Server.Env import Simplex.Messaging.Notifications.Server.Subscriptions import Simplex.Messaging.Notifications.Transport -import Simplex.Messaging.Protocol (ErrorType (..), Transmission, encodeTransmission, tGet, tPut) +import Simplex.Messaging.Protocol (ErrorType (..), SignedTransmission, Transmission, encodeTransmission, tGet, tPut) +import qualified Simplex.Messaging.Protocol as SMP import Simplex.Messaging.Server import Simplex.Messaging.Transport (ATransport (..), THandle (..), TProxy, Transport) import Simplex.Messaging.Transport.Server (runTransportServer) @@ -37,7 +43,10 @@ runNtfServerBlocking started cfg@NtfServerConfig {transports} = do runReaderT ntfServer env where ntfServer :: (MonadUnliftIO m', MonadReader NtfEnv m') => m' () - ntfServer = raceAny_ (map runServer transports) + ntfServer = do + s <- asks subscriber + ps <- asks pushServer + raceAny_ (ntfSubscriber s : ntfPush ps : map runServer transports) runServer :: (MonadUnliftIO m', MonadReader NtfEnv m') => (ServiceName, ATransport) -> m' () runServer (tcpPort, ATransport t) = do @@ -51,11 +60,61 @@ runNtfServerBlocking started cfg@NtfServerConfig {transports} = do Right th -> runNtfClientTransport th Left _ -> pure () +ntfSubscriber :: forall m. (MonadUnliftIO m, MonadReader NtfEnv m) => NtfSubscriber -> m () +ntfSubscriber NtfSubscriber {subQ, smpAgent = ca@SMPClientAgent {msgQ, agentQ}} = do + raceAny_ [subscribe, receiveSMP, receiveAgent] + where + subscribe :: m () + subscribe = forever $ do + atomically (readTBQueue subQ) >>= \case + NtfSub NtfSubData {smpQueue} -> do + let SMPQueueNtf {smpServer, notifierId, notifierKey} = smpQueue + liftIO (runExceptT $ subscribeQueue ca smpServer ((SPNotifier, notifierId), notifierKey)) >>= \case + Right _ -> pure () -- update subscription status + Left e -> pure () + + receiveSMP :: m () + receiveSMP = forever $ do + (srv, ntfId, msg) <- atomically $ readTBQueue msgQ + case msg of + SMP.NMSG -> do + -- check when the last NMSG was received from this queue + -- update timestamp + -- check what was the last hidden notification was sent (and whether to this queue) + -- decide whether it should be sent as hidden or visible + -- construct and possibly encrypt notification + -- send it + pure () + _ -> pure () + pure () + + receiveAgent = + forever $ + atomically (readTBQueue agentQ) >>= \case + CAConnected _ -> pure () + CADisconnected srv subs -> do + -- update subscription statuses + pure () + CAReconnected _ -> pure () + CAResubscribed srv sub -> do + -- update subscription status + pure () + CASubError srv sub err -> do + -- update subscription status + pure () + +ntfPush :: (MonadUnliftIO m, MonadReader NtfEnv m) => NtfPushServer -> m () +ntfPush NtfPushServer {pushQ} = forever $ do + atomically (readTBQueue pushQ) >>= \case + (NtfTknData {}, Notification {}) -> pure () + runNtfClientTransport :: (Transport c, MonadUnliftIO m, MonadReader NtfEnv m) => THandle c -> m () runNtfClientTransport th@THandle {sessionId} = do - q <- asks $ tbqSize . config - c <- atomically $ newNtfServerClient q sessionId - raceAny_ [send th c, client c, receive th c] + qSize <- asks $ clientQSize . config + c <- atomically $ newNtfServerClient qSize sessionId + s <- asks subscriber + ps <- asks pushServer + raceAny_ [send th c, client c s ps, receive th c] `finally` clientDisconnected c clientDisconnected :: MonadUnliftIO m => NtfServerClient -> m () @@ -63,14 +122,13 @@ clientDisconnected NtfServerClient {connected} = atomically $ writeTVar connecte receive :: (Transport c, MonadUnliftIO m, MonadReader NtfEnv m) => THandle c -> NtfServerClient -> m () receive th NtfServerClient {rcvQ, sndQ} = forever $ do - (sig, signed, (corrId, queueId, cmdOrError)) <- tGet th + t@(sig, signed, (corrId, subId, cmdOrError)) <- tGet th case cmdOrError of - Left e -> write sndQ (corrId, queueId, NRErr e) - Right cmd -> do - verified <- verifyTransmission sig signed queueId cmd - if verified - then write rcvQ (corrId, queueId, cmd) - else write sndQ (corrId, queueId, NRErr AUTH) + Left e -> write sndQ (corrId, subId, NRErr e) + Right cmd -> + verifyNtfTransmission t cmd >>= \case + VRVerified req -> write rcvQ req + VRFailed -> write sndQ (corrId, subId, NRErr AUTH) where write q t = atomically $ writeTBQueue q t @@ -79,33 +137,121 @@ send h NtfServerClient {sndQ, sessionId} = forever $ do t <- atomically $ readTBQueue sndQ liftIO $ tPut h (Nothing, encodeTransmission sessionId t) -verifyTransmission :: - forall m. (MonadUnliftIO m, MonadReader NtfEnv m) => Maybe C.ASignature -> ByteString -> NtfSubsciptionId -> NtfCommand -> m Bool -verifyTransmission sig_ signed subId cmd = do - case cmd of - NCCreate _ _ k _ -> pure $ verifyCmdSignature sig_ signed k - _ -> do - st <- asks store - verifySubCmd <$> atomically (getNtfSubscription st subId) - where - verifySubCmd = \case - Right sub -> verifyCmdSignature sig_ signed $ subVerifyKey sub - Left _ -> maybe False (dummyVerifyCmd signed) sig_ `seq` False +data VerificationResult = VRVerified NtfRequest | VRFailed -client :: forall m. (MonadUnliftIO m, MonadReader NtfEnv m) => NtfServerClient -> m () -client NtfServerClient {rcvQ, sndQ} = +verifyNtfTransmission :: + forall m. (MonadUnliftIO m, MonadReader NtfEnv m) => SignedTransmission NtfCmd -> NtfCmd -> m VerificationResult +verifyNtfTransmission (sig_, signed, (corrId, entId, _)) cmd = do + case cmd of + NtfCmd SToken (TNEW n@(NewNtfTkn _ k _)) -> + -- TODO check that token is not already in store + pure $ + if verifyCmdSignature sig_ signed k + then VRVerified (NtfReqNew corrId (ANE SToken n)) + else VRFailed + NtfCmd SToken c -> do + st <- asks store + atomically (getNtfToken st entId) >>= \case + Just r@(NtfTkn NtfTknData {tknVerifyKey}) -> + pure $ + if verifyCmdSignature sig_ signed tknVerifyKey + then VRVerified (NtfReqCmd SToken r (corrId, entId, c)) + else VRFailed + _ -> pure VRFailed -- TODO dummy verification + _ -> pure VRFailed + +-- do +-- st <- asks store +-- case cmd of +-- NCSubCreate tokenId smpQueue -> verifyCreateCmd verifyKey newSub <$> atomically (getNtfSubViaSMPQueue st smpQueue) +-- _ -> verifySubCmd <$> atomically (getNtfSub st subId) +-- where +-- verifyCreateCmd k newSub sub_ +-- | verifyCmdSignature sig_ signed k = case sub_ of +-- Just sub -> if k == subVerifyKey sub then VRCommand sub else VRFail +-- _ -> VRCreate newSub +-- | otherwise = VRFail +-- verifySubCmd = \case +-- Just sub -> if verifyCmdSignature sig_ signed $ subVerifyKey sub then VRCommand sub else VRFail +-- _ -> maybe False (dummyVerifyCmd signed) sig_ `seq` VRFail + +client :: forall m. (MonadUnliftIO m, MonadReader NtfEnv m) => NtfServerClient -> NtfSubscriber -> NtfPushServer -> m () +client NtfServerClient {rcvQ, sndQ} NtfSubscriber {subQ} NtfPushServer {pushQ} = forever $ atomically (readTBQueue rcvQ) >>= processCommand >>= atomically . writeTBQueue sndQ where - processCommand :: Transmission NtfCommand -> m (Transmission NtfResponse) - processCommand (corrId, subId, cmd) = case cmd of - NCCreate _token _smpQueue _verifyKey _dhKey -> do - pure (corrId, subId, NROk) - NCCheck -> do - pure (corrId, subId, NROk) - NCToken _token -> do - pure (corrId, subId, NROk) - NCDelete -> do - pure (corrId, subId, NROk) + processCommand :: NtfRequest -> m (Transmission NtfResponse) + processCommand = \case + NtfReqNew corrId (ANE SToken newTkn@(NewNtfTkn _ _ dhPubKey)) -> do + st <- asks store + (srvDhPubKey, srvDrivDhKey) <- liftIO C.generateKeyPair' + let dhSecret = C.dh' dhPubKey srvDrivDhKey + tknId <- getId + atomically $ do + tkn <- mkNtfTknData newTkn dhSecret + addNtfToken st tknId tkn + writeTBQueue pushQ (tkn, Notification) + -- pure (corrId, sId, NRSubId pubDhKey) + pure (corrId, "", NRId tknId srvDhPubKey) + NtfReqCmd SToken tkn (corrId, tknId, cmd) -> + (corrId,tknId,) <$> case cmd of + TNEW newTkn -> pure NROk -- TODO when duplicate token sent + TVFY code -> pure NROk + TDEL -> pure NROk + TCRN int -> pure NROk + NtfReqNew corrId (ANE SSubscription newSub) -> pure (corrId, "", NROk) + NtfReqCmd SSubscription sub (corrId, subId, cmd) -> + (corrId,subId,) <$> case cmd of + SNEW newSub -> pure NROk + SCHK -> pure NROk + SDEL -> pure NROk + PING -> pure NRPong + getId :: m NtfEntityId + getId = do + n <- asks $ subIdBytes . config + gVar <- asks idsDrg + atomically (randomBytes n gVar) + +-- NReqCreate corrId tokenId smpQueue -> pure (corrId, "", NROk) +-- do +-- st <- asks store +-- (pubDhKey, privDhKey) <- liftIO C.generateKeyPair' +-- let dhSecret = C.dh' dhPubKey privDhKey +-- sub <- atomically $ mkNtfSubsciption smpQueue token verifyKey dhSecret +-- addSubRetry 3 st sub >>= \case +-- Nothing -> pure (corrId, "", NRErr INTERNAL) +-- Just sId -> do +-- atomically $ writeTBQueue subQ sub +-- pure (corrId, sId, NRSubId pubDhKey) +-- where +-- addSubRetry :: Int -> NtfSubscriptionsStore -> NtfSubsciption -> m (Maybe NtfSubsciptionId) +-- addSubRetry 0 _ _ = pure Nothing +-- addSubRetry n st sub = do +-- sId <- getId +-- -- create QueueRec record with these ids and keys +-- atomically (addNtfSub st sId sub) >>= \case +-- Nothing -> addSubRetry (n - 1) st sub +-- _ -> pure $ Just sId +-- getId :: m NtfSubsciptionId +-- getId = do +-- n <- asks $ subIdBytes . config +-- gVar <- asks idsDrg +-- atomically (randomBytes n gVar) +-- NReqCommand sub@NtfSubsciption {tokenId, subStatus} (corrId, subId, cmd) -> +-- (corrId,subId,) <$> case cmd of +-- NCSubCreate tokenId smpQueue -> pure NROk +-- do +-- st <- asks store +-- (pubDhKey, privDhKey) <- liftIO C.generateKeyPair' +-- let dhSecret = C.dh' (dhPubKey newSub) privDhKey +-- atomically (updateNtfSub st sub newSub dhSecret) >>= \case +-- Nothing -> pure $ NRErr INTERNAL +-- _ -> atomically $ do +-- whenM ((== NSEnd) <$> readTVar status) $ writeTBQueue subQ sub +-- pure $ NRSubId pubDhKey +-- NCSubCheck -> NRStat <$> readTVarIO subStatus +-- NCSubDelete -> do +-- st <- asks store +-- atomically (deleteNtfSub st subId) $> NROk diff --git a/src/Simplex/Messaging/Notifications/Server/Env.hs b/src/Simplex/Messaging/Notifications/Server/Env.hs index 3ce4d2b8d..fec2aafb6 100644 --- a/src/Simplex/Messaging/Notifications/Server/Env.hs +++ b/src/Simplex/Messaging/Notifications/Server/Env.hs @@ -1,4 +1,7 @@ +{-# LANGUAGE DataKinds #-} {-# LANGUAGE DuplicateRecordFields #-} +{-# LANGUAGE GADTs #-} +{-# LANGUAGE KindSignatures #-} {-# LANGUAGE NamedFieldPuns #-} module Simplex.Messaging.Notifications.Server.Env where @@ -6,26 +9,27 @@ module Simplex.Messaging.Notifications.Server.Env where import Control.Monad.IO.Unlift import Crypto.Random import Data.ByteString.Char8 (ByteString) -import qualified Data.Map.Strict as M import Data.X509.Validation (Fingerprint (..)) import Network.Socket import qualified Network.TLS as T import Numeric.Natural import Simplex.Messaging.Agent.RetryInterval -import Simplex.Messaging.Client +import Simplex.Messaging.Client.Agent import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Notifications.Protocol import Simplex.Messaging.Notifications.Server.Subscriptions -import Simplex.Messaging.Protocol (Transmission) +import Simplex.Messaging.Protocol (CorrId, Transmission) import Simplex.Messaging.Transport (ATransport) import Simplex.Messaging.Transport.Server (loadFingerprint, loadTLSServerParams) import UnliftIO.STM data NtfServerConfig = NtfServerConfig { transports :: [(ServiceName, ATransport)], - subscriptionIdBytes :: Int, - tbqSize :: Natural, - smpCfg :: SMPClientConfig, + subIdBytes :: Int, + clientQSize :: Natural, + subQSize :: Natural, + pushQSize :: Natural, + smpAgentCfg :: SMPClientAgentConfig, reconnectInterval :: RetryInterval, -- CA certificate private key is not needed for initialization caCertificateFile :: FilePath, @@ -33,26 +37,55 @@ data NtfServerConfig = NtfServerConfig certificateFile :: FilePath } +data Notification = Notification + data NtfEnv = NtfEnv { config :: NtfServerConfig, - serverIdentity :: C.KeyHash, - store :: NtfSubscriptions, + subscriber :: NtfSubscriber, + pushServer :: NtfPushServer, + store :: NtfStore, idsDrg :: TVar ChaChaDRG, + serverIdentity :: C.KeyHash, tlsServerParams :: T.ServerParams, serverIdentity :: C.KeyHash } newNtfServerEnv :: (MonadUnliftIO m, MonadRandom m) => NtfServerConfig -> m NtfEnv -newNtfServerEnv config@NtfServerConfig {caCertificateFile, certificateFile, privateKeyFile} = do +newNtfServerEnv config@NtfServerConfig {subQSize, pushQSize, smpAgentCfg, caCertificateFile, certificateFile, privateKeyFile} = do idsDrg <- newTVarIO =<< drgNew - store <- newTVarIO M.empty + store <- atomically newNtfStore + subscriber <- atomically $ newNtfSubscriber subQSize smpAgentCfg + pushServer <- atomically $ newNtfPushServer pushQSize tlsServerParams <- liftIO $ loadTLSServerParams caCertificateFile certificateFile privateKeyFile Fingerprint fp <- liftIO $ loadFingerprint caCertificateFile - let serverIdentity = C.KeyHash fp - pure NtfEnv {config, store, idsDrg, tlsServerParams, serverIdentity} + pure NtfEnv {config, subscriber, pushServer, store, idsDrg, tlsServerParams, serverIdentity = C.KeyHash fp} + +data NtfSubscriber = NtfSubscriber + { subQ :: TBQueue (NtfEntityRec 'Subscription), + smpAgent :: SMPClientAgent + } + +newNtfSubscriber :: Natural -> SMPClientAgentConfig -> STM NtfSubscriber +newNtfSubscriber qSize smpAgentCfg = do + smpAgent <- newSMPClientAgent smpAgentCfg + subQ <- newTBQueue qSize + pure NtfSubscriber {smpAgent, subQ} + +newtype NtfPushServer = NtfPushServer + { pushQ :: TBQueue (NtfTknData, Notification) + } + +newNtfPushServer :: Natural -> STM NtfPushServer +newNtfPushServer qSize = do + pushQ <- newTBQueue qSize + pure NtfPushServer {pushQ} + +data NtfRequest + = NtfReqNew CorrId ANewNtfEntity + | forall e. NtfEntityI e => NtfReqCmd (SNtfEntity e) (NtfEntityRec e) (Transmission (NtfCommand e)) data NtfServerClient = NtfServerClient - { rcvQ :: TBQueue (Transmission NtfCommand), + { rcvQ :: TBQueue NtfRequest, sndQ :: TBQueue (Transmission NtfResponse), sessionId :: ByteString, connected :: TVar Bool diff --git a/src/Simplex/Messaging/Notifications/Server/Push.hs b/src/Simplex/Messaging/Notifications/Server/Push.hs new file mode 100644 index 000000000..87b475f2a --- /dev/null +++ b/src/Simplex/Messaging/Notifications/Server/Push.hs @@ -0,0 +1,11 @@ +module Simplex.Messaging.Notifications.Server.Push where + +import Control.Concurrent.STM +import Data.ByteString.Char8 (ByteString) +import Simplex.Messaging.Protocol (NotifierId, SMPServer) + +data NtfPushPayload = NPVerification ByteString | NPNotification SMPServer NotifierId | NPPing + +class PushProvider p where + newPushProvider :: STM p + requestBody :: p -> NtfPushPayload -> ByteString -- ? diff --git a/src/Simplex/Messaging/Notifications/Server/Subscriptions.hs b/src/Simplex/Messaging/Notifications/Server/Subscriptions.hs index 5658e336a..e5cbd26fc 100644 --- a/src/Simplex/Messaging/Notifications/Server/Subscriptions.hs +++ b/src/Simplex/Messaging/Notifications/Server/Subscriptions.hs @@ -1,25 +1,109 @@ +{-# LANGUAGE DataKinds #-} +{-# LANGUAGE DuplicateRecordFields #-} +{-# LANGUAGE GADTs #-} +{-# LANGUAGE KindSignatures #-} +{-# LANGUAGE LambdaCase #-} +{-# LANGUAGE NamedFieldPuns #-} +{-# LANGUAGE TupleSections #-} + module Simplex.Messaging.Notifications.Server.Subscriptions where import Control.Concurrent.STM +import Control.Monad +import Crypto.PubKey.Curve25519 (dhSecret) import Data.Map.Strict (Map) import qualified Data.Map.Strict as M +import Data.Set (Set) import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Notifications.Protocol -import Simplex.Messaging.Protocol (ErrorType (..), NotifierId, NtfPrivateSignKey, SMPServer) +import Simplex.Messaging.Protocol (ErrorType (..), NotifierId, NtfPrivateSignKey, ProtocolServer) +import Simplex.Messaging.TMap (TMap) +import qualified Simplex.Messaging.TMap as TM +import Simplex.Messaging.Util ((<$$>)) -type NtfSubscriptionsData = Map NtfSubsciptionId NtfSubsciptionRec - -type NtfSubscriptions = TVar NtfSubscriptionsData - -data NtfSubsciptionRec = NtfSubsciptionRec - { smpServer :: SMPServer, - notifierId :: NotifierId, - notifierKey :: NtfPrivateSignKey, - token :: DeviceToken, - status :: TVar NtfStatus, - subVerifyKey :: C.APublicVerifyKey, - subDHSecret :: C.DhSecretX25519 +data NtfStore = NtfStore + { tokens :: TMap NtfTokenId NtfTknData, + tokenIds :: TMap DeviceToken NtfTokenId } -getNtfSubscription :: NtfSubscriptions -> NtfSubsciptionId -> STM (Either ErrorType NtfSubsciptionRec) -getNtfSubscription st subId = maybe (Left AUTH) Right . M.lookup subId <$> readTVar st +newNtfStore :: STM NtfStore +newNtfStore = do + tokens <- TM.empty + tokenIds <- TM.empty + pure NtfStore {tokens, tokenIds} + +data NtfTknData = NtfTknData + { token :: DeviceToken, + tknStatus :: TVar NtfTknStatus, + tknVerifyKey :: C.APublicVerifyKey, + tknDhSecret :: C.DhSecretX25519 + } + +mkNtfTknData :: NewNtfEntity 'Token -> C.DhSecretX25519 -> STM NtfTknData +mkNtfTknData (NewNtfTkn token tknVerifyKey _) tknDhSecret = do + tknStatus <- newTVar NTNew + pure NtfTknData {token, tknStatus, tknVerifyKey, tknDhSecret} + +data NtfSubscriptionsStore = NtfSubscriptionsStore + +-- { subscriptions :: TMap NtfSubsciptionId NtfSubsciption, +-- activeSubscriptions :: TMap (SMPServer, NotifierId) NtfSubsciptionId +-- } +-- do +-- subscriptions <- newTVar M.empty +-- activeSubscriptions <- newTVar M.empty +-- pure NtfSubscriptionsStore {subscriptions, activeSubscriptions} + +data NtfSubData = NtfSubData + { smpQueue :: SMPQueueNtf, + tokenId :: NtfTokenId, + subStatus :: TVar NtfSubStatus + } + +data NtfEntityRec (e :: NtfEntity) where + NtfTkn :: NtfTknData -> NtfEntityRec 'Token + NtfSub :: NtfSubData -> NtfEntityRec 'Subscription + +data ANtfEntityRec = forall e. NtfEntityI e => NER (SNtfEntity e) (NtfEntityRec e) + +getNtfToken :: NtfStore -> NtfTokenId -> STM (Maybe (NtfEntityRec 'Token)) +getNtfToken st tknId = NtfTkn <$$> TM.lookup tknId (tokens st) + +addNtfToken :: NtfStore -> NtfTokenId -> NtfTknData -> STM () +addNtfToken st tknId tkn = pure () + +-- getNtfRec :: NtfStore -> SNtfEntity e -> NtfEntityId -> STM (Maybe (NtfEntityRec e)) +-- getNtfRec st ent entId = case ent of +-- SToken -> NtfTkn <$$> TM.lookup entId (tokens st) +-- SSubscription -> pure Nothing + +-- getNtfVerifyKey :: NtfStore -> SNtfEntity e -> NtfEntityId -> STM (Maybe (NtfEntityRec e, C.APublicVerifyKey)) +-- getNtfVerifyKey st ent entId = +-- getNtfRec st ent entId >>= \case +-- Just r@(NtfTkn NtfTknData {tknVerifyKey}) -> pure $ Just (r, tknVerifyKey) +-- Just r@(NtfSub NtfSubData {tokenId}) -> +-- getNtfRec st SToken tokenId >>= \case +-- Just (NtfTkn NtfTknData {tknVerifyKey}) -> pure $ Just (r, tknVerifyKey) +-- _ -> pure Nothing +-- _ -> pure Nothing + +-- mkNtfSubsciption :: SMPQueueNtf -> NtfTokenId -> STM NtfSubsciption +-- mkNtfSubsciption smpQueue tokenId = do +-- subStatus <- newTVar NSNew +-- pure NtfSubsciption {smpQueue, tokenId, subStatus} + +-- getNtfSub :: NtfSubscriptionsStore -> NtfSubsciptionId -> STM (Maybe NtfSubsciption) +-- getNtfSub st subId = pure Nothing -- maybe (pure $ Left AUTH) (fmap Right . readTVar) . M.lookup subId . subscriptions =<< readTVar st + +-- getNtfSubViaSMPQueue :: NtfSubscriptionsStore -> SMPQueueNtf -> STM (Maybe NtfSubsciption) +-- getNtfSubViaSMPQueue st smpQueue = pure Nothing + +-- -- replace keeping status +-- updateNtfSub :: NtfSubscriptionsStore -> NtfSubsciption -> SMPQueueNtf -> NtfTokenId -> C.DhSecretX25519 -> STM (Maybe ()) +-- updateNtfSub st sub smpQueue tokenId dhSecret = pure Nothing + +-- addNtfSub :: NtfSubscriptionsStore -> NtfSubsciptionId -> NtfSubsciption -> STM (Maybe ()) +-- addNtfSub st subId sub = pure Nothing + +-- deleteNtfSub :: NtfSubscriptionsStore -> NtfSubsciptionId -> STM () +-- deleteNtfSub st subId = pure () diff --git a/src/Simplex/Messaging/Protocol.hs b/src/Simplex/Messaging/Protocol.hs index dbe5a98a0..0e94b5192 100644 --- a/src/Simplex/Messaging/Protocol.hs +++ b/src/Simplex/Messaging/Protocol.hs @@ -1,23 +1,25 @@ {-# LANGUAGE DataKinds #-} {-# LANGUAGE DeriveGeneric #-} +{-# LANGUAGE DuplicateRecordFields #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE LambdaCase #-} {-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE PatternSynonyms #-} {-# LANGUAGE PolyKinds #-} {-# LANGUAGE RankNTypes #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE StandaloneDeriving #-} {-# LANGUAGE TupleSections #-} {-# LANGUAGE TypeApplications #-} -{-# LANGUAGE TypeFamilies #-} +{-# LANGUAGE TypeFamilyDependencies #-} {-# LANGUAGE UndecidableInstances #-} {-# OPTIONS_GHC -fno-warn-unticked-promoted-constructors #-} -- | --- Module : Simplex.Messaging.Protocol +-- Module : Simplex.Messaging.ProtocolEncoding -- Copyright : (c) simplex.chat -- License : AGPL-3 -- @@ -37,7 +39,7 @@ module Simplex.Messaging.Protocol e2eEncMessageLength, -- * SMP protocol types - Protocol (..), + ProtocolEncoding (..), Command (..), Party (..), Cmd (..), @@ -55,7 +57,10 @@ module Simplex.Messaging.Protocol PubHeader (..), ClientMessage (..), PrivHeader (..), - SMPServer (..), + Protocol (..), + ProtocolServer (..), + SMPServer, + pattern SMPServer, SrvLoc (..), CorrId (..), QueueId, @@ -163,7 +168,7 @@ data Cmd = forall p. PartyI p => Cmd (SParty p) (Command p) deriving instance Show Cmd -- | Parsed SMP transmission without signature, size and session ID. -type Transmission c = (CorrId, QueueId, c) +type Transmission c = (CorrId, EntityId, c) -- | signed parsed transmission, with original raw bytes and parsing error. type SignedTransmission c = (Maybe C.ASignature, Signed, Transmission (Either ErrorType c)) @@ -196,7 +201,9 @@ type SenderId = QueueId type NotifierId = QueueId -- | SMP queue ID on the server. -type QueueId = ByteString +type QueueId = EntityId + +type EntityId = ByteString -- | Parameterized type for SMP protocol commands from all clients. data Command (p :: Party) where @@ -266,7 +273,7 @@ class ProtocolMsgTag t where messageTagP :: ProtocolMsgTag t => Parser t messageTagP = - maybe (fail "bad command") pure . decodeTag + maybe (fail "bad message") pure . decodeTag =<< (A.takeTill (== ' ') <* optional A.space) instance PartyI p => Encoding (CommandTag p) where @@ -374,34 +381,39 @@ instance Encoding ClientMessage where smpEncode (ClientMessage h msg) = smpEncode h <> msg smpP = ClientMessage <$> smpP <*> A.takeByteString +type SMPServer = ProtocolServer + +pattern SMPServer :: HostName -> ServiceName -> C.KeyHash -> ProtocolServer +pattern SMPServer host port keyHash = ProtocolServer host port keyHash + -- | SMP server location and transport key digest (hash). -data SMPServer = SMPServer +data ProtocolServer = ProtocolServer { host :: HostName, port :: ServiceName, keyHash :: C.KeyHash } deriving (Eq, Ord, Show) -instance IsString SMPServer where +instance IsString ProtocolServer where fromString = parseString strDecode -instance Encoding SMPServer where - smpEncode SMPServer {host, port, keyHash} = +instance Encoding ProtocolServer where + smpEncode ProtocolServer {host, port, keyHash} = smpEncode (host, port, keyHash) smpP = do (host, port, keyHash) <- smpP - pure SMPServer {host, port, keyHash} + pure ProtocolServer {host, port, keyHash} -instance StrEncoding SMPServer where - strEncode SMPServer {host, port, keyHash} = +instance StrEncoding ProtocolServer where + strEncode ProtocolServer {host, port, keyHash} = "smp://" <> strEncode keyHash <> "@" <> strEncode (SrvLoc host port) strP = do _ <- "smp://" keyHash <- strP <* A.char '@' SrvLoc host port <- strP - pure SMPServer {host, port, keyHash} + pure ProtocolServer {host, port, keyHash} -instance ToJSON SMPServer where +instance ToJSON ProtocolServer where toJSON = strToJSON toEncoding = strToJEncoding @@ -540,13 +552,25 @@ transmissionP = do command <- A.takeByteString pure RawTransmission {signature, signed, sessId, corrId, entityId, command} -class Protocol msg where +class (ProtocolEncoding msg, ProtocolEncoding (ProtocolCommand msg)) => Protocol msg where + type ProtocolCommand msg = cmd | cmd -> msg + protocolPing :: ProtocolCommand msg + protocolError :: msg -> Maybe ErrorType + +instance Protocol BrokerMsg where + type ProtocolCommand BrokerMsg = Cmd + protocolPing = Cmd SSender PING + protocolError = \case + ERR e -> Just e + _ -> Nothing + +class ProtocolMsgTag (Tag msg) => ProtocolEncoding msg where type Tag msg encodeProtocol :: msg -> ByteString protocolP :: Tag msg -> Parser msg checkCredentials :: SignedRawTransmission -> msg -> Either ErrorType msg -instance PartyI p => Protocol (Command p) where +instance PartyI p => ProtocolEncoding (Command p) where type Tag (Command p) = CommandTag p encodeProtocol = \case NEW rKey dhKey -> e (NEW_, ' ', rKey, dhKey) @@ -584,7 +608,7 @@ instance PartyI p => Protocol (Command p) where | isNothing sig || B.null queueId -> Left $ CMD NO_AUTH | otherwise -> Right cmd -instance Protocol Cmd where +instance ProtocolEncoding Cmd where type Tag Cmd = CmdTag encodeProtocol (Cmd _ c) = encodeProtocol c @@ -606,7 +630,7 @@ instance Protocol Cmd where checkCredentials t (Cmd p c) = Cmd p <$> checkCredentials t c -instance Protocol BrokerMsg where +instance ProtocolEncoding BrokerMsg where type Tag BrokerMsg = BrokerMsgTag encodeProtocol = \case IDS (QIK rcvId sndId srvDh) -> e (IDS_, ' ', rcvId, sndId, srvDh) @@ -632,7 +656,7 @@ instance Protocol BrokerMsg where PONG_ -> pure PONG checkCredentials (_, _, queueId, _) cmd = case cmd of - -- IDS response must not have queue ID + -- IDS response should not have queue ID IDS _ -> Right cmd -- ERR response does not always have queue ID ERR _ -> Right cmd @@ -649,7 +673,7 @@ _smpP :: Encoding a => Parser a _smpP = A.space *> smpP -- | Parse SMP protocol commands and broker messages -parseProtocol :: (Protocol msg, ProtocolMsgTag (Tag msg)) => ByteString -> Either ErrorType msg +parseProtocol :: ProtocolEncoding msg => ByteString -> Either ErrorType msg parseProtocol s = let (tag, params) = B.break (== ' ') s in case decodeTag tag of @@ -712,7 +736,7 @@ instance Encoding CommandError where tPut :: Transport c => THandle c -> SentRawTransmission -> IO (Either TransportError ()) tPut th (sig, t) = tPutBlock th $ smpEncode (C.signatureBytes sig) <> t -encodeTransmission :: Protocol c => ByteString -> Transmission c -> ByteString +encodeTransmission :: ProtocolEncoding c => ByteString -> Transmission c -> ByteString encodeTransmission sessionId (CorrId corrId, queueId, command) = smpEncode (sessionId, corrId, queueId) <> encodeProtocol command @@ -721,11 +745,7 @@ tGetParse :: Transport c => THandle c -> IO (Either TransportError RawTransmissi tGetParse th = (parse transmissionP TEBadBlock =<<) <$> tGetBlock th -- | Receive client and server transmissions (determined by `cmd` type). -tGet :: - forall cmd c m. - (Protocol cmd, ProtocolMsgTag (Tag cmd), Transport c, MonadIO m) => - THandle c -> - m (SignedTransmission cmd) +tGet :: forall cmd c m. (ProtocolEncoding cmd, Transport c, MonadIO m) => THandle c -> m (SignedTransmission cmd) tGet th@THandle {sessionId} = liftIO (tGetParse th) >>= decodeParseValidate where decodeParseValidate :: Either TransportError RawTransmission -> m (SignedTransmission cmd) diff --git a/src/Simplex/Messaging/Server.hs b/src/Simplex/Messaging/Server.hs index c76a2aaa3..c3d62bb02 100644 --- a/src/Simplex/Messaging/Server.hs +++ b/src/Simplex/Messaging/Server.hs @@ -23,7 +23,7 @@ -- and optional append only log of SMP queue records. -- -- See https://github.com/simplex-chat/simplexmq/blob/master/protocol/simplex-messaging.md -module Simplex.Messaging.Server (runSMPServer, runSMPServerBlocking, verifyCmdSignature, dummyVerifyCmd) where +module Simplex.Messaging.Server (runSMPServer, runSMPServerBlocking, verifyCmdSignature, dummyVerifyCmd, randomBytes) where import Control.Monad import Control.Monad.Except diff --git a/src/Simplex/Messaging/TMap.hs b/src/Simplex/Messaging/TMap.hs index a6584903e..b5bc01253 100644 --- a/src/Simplex/Messaging/TMap.hs +++ b/src/Simplex/Messaging/TMap.hs @@ -11,6 +11,7 @@ module Simplex.Messaging.TMap adjust, update, alter, + alterF, union, ) where @@ -65,6 +66,12 @@ alter :: Ord k => (Maybe a -> Maybe a) -> k -> TMap k a -> STM () alter f k m = modifyTVar' m $ M.alter f k {-# INLINE alter #-} +alterF :: Ord k => (Maybe a -> STM (Maybe a)) -> k -> TMap k a -> STM () +alterF f k m = do + mv <- M.alterF f k =<< readTVar m + writeTVar m $! mv +{-# INLINE alterF #-} + union :: Ord k => Map k a -> TMap k a -> STM () union m' m = modifyTVar' m $ M.union m' {-# INLINE union #-} diff --git a/src/Simplex/Messaging/Transport/KeepAlive.hs b/src/Simplex/Messaging/Transport/KeepAlive.hs index aa308492d..c949394a0 100644 --- a/src/Simplex/Messaging/Transport/KeepAlive.hs +++ b/src/Simplex/Messaging/Transport/KeepAlive.hs @@ -52,7 +52,7 @@ foreign import capi "netinet/tcp.h value TCP_KEEPINTVL" _TCP_KEEPINTVL :: CInt foreign import capi "netinet/tcp.h value TCP_KEEPCNT" _TCP_KEEPCNT :: CInt -#endif +#endif setSocketKeepAlive :: Socket -> KeepAliveOpts -> IO () setSocketKeepAlive sock KeepAliveOpts {keepCnt, keepIdle, keepIntvl} = do diff --git a/tests/AgentTests/ConnectionRequestTests.hs b/tests/AgentTests/ConnectionRequestTests.hs index cbab22793..1b1598413 100644 --- a/tests/AgentTests/ConnectionRequestTests.hs +++ b/tests/AgentTests/ConnectionRequestTests.hs @@ -11,7 +11,7 @@ import Simplex.Messaging.Agent.Protocol import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Crypto.Ratchet import Simplex.Messaging.Encoding.String -import Simplex.Messaging.Protocol (smpClientVRange) +import Simplex.Messaging.Protocol (ProtocolServer (..), smpClientVRange) import Simplex.Messaging.Version import Test.Hspec @@ -20,7 +20,7 @@ uri = "smp.simplex.im" srv :: SMPServer srv = - SMPServer + ProtocolServer { host = "smp.simplex.im", port = "5223", keyHash = C.KeyHash "\215m\248\251" diff --git a/tests/SMPAgentClient.hs b/tests/SMPAgentClient.hs index 2d7b82375..e232ffdbd 100644 --- a/tests/SMPAgentClient.hs +++ b/tests/SMPAgentClient.hs @@ -24,7 +24,7 @@ import Simplex.Messaging.Agent.Env.SQLite import Simplex.Messaging.Agent.Protocol import Simplex.Messaging.Agent.RetryInterval import Simplex.Messaging.Agent.Server (runSMPAgentBlocking) -import Simplex.Messaging.Client (SMPClientConfig (..), smpDefaultConfig) +import Simplex.Messaging.Client (ProtocolClientConfig (..), defaultClientConfig) import Simplex.Messaging.Transport import Simplex.Messaging.Transport.Client import Simplex.Messaging.Transport.KeepAlive @@ -162,7 +162,7 @@ cfg = tbqSize = 1, dbFile = testDB, smpCfg = - smpDefaultConfig + defaultClientConfig { qSize = 1, defaultTransport = (testPort, transport @TLS), tcpTimeout = 500_000 From fb26916eea4f7148d823b5c48fb7673433681ff1 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Sun, 3 Apr 2022 10:37:32 +0100 Subject: [PATCH 004/131] ntf-server CLI, re-use SMP server CLI as a library (#347) * ntf-server CLI, re-use SMP server CLI as a library * add executable name --- apps/ntf-server/Main.hs | 50 +++ apps/smp-server/Main.hs | 343 ++---------------- package.yaml | 16 +- simplexmq.cabal | 59 +++ src/Simplex/Messaging/Agent/Env/SQLite.hs | 7 +- src/Simplex/Messaging/Client.hs | 2 +- .../Messaging/Notifications/Server/Env.hs | 2 - src/Simplex/Messaging/Server/CLI.hs | 285 +++++++++++++++ src/Simplex/Messaging/Server/Env/STM.hs | 8 +- tests/SMPClient.hs | 9 +- 10 files changed, 458 insertions(+), 323 deletions(-) create mode 100644 apps/ntf-server/Main.hs create mode 100644 src/Simplex/Messaging/Server/CLI.hs diff --git a/apps/ntf-server/Main.hs b/apps/ntf-server/Main.hs new file mode 100644 index 000000000..8426093e5 --- /dev/null +++ b/apps/ntf-server/Main.hs @@ -0,0 +1,50 @@ +{-# LANGUAGE NamedFieldPuns #-} + +module Main where + +import Simplex.Messaging.Client.Agent (defaultSMPClientAgentConfig) +import Simplex.Messaging.Notifications.Server (runNtfServer) +import Simplex.Messaging.Notifications.Server.Env (NtfServerConfig (..)) +import Simplex.Messaging.Server.CLI (ServerCLIConfig (..), protocolServerCLI) +import System.FilePath (combine) + +cfgPath :: FilePath +cfgPath = "/etc/opt/simplex-notifications" + +logPath :: FilePath +logPath = "/var/opt/simplex-notifications" + +main :: IO () +main = protocolServerCLI ntfServerCLIConfig runNtfServer + +ntfServerCLIConfig :: ServerCLIConfig NtfServerConfig +ntfServerCLIConfig = + let caCrtFile = combine cfgPath "ca.crt" + serverKeyFile = combine cfgPath "server.key" + serverCrtFile = combine cfgPath "server.crt" + in ServerCLIConfig + { cfgDir = cfgPath, + logDir = logPath, + iniFile = combine cfgPath "ntf-server.ini", + storeLogFile = combine logPath "ntf-server-store.log", + caKeyFile = combine cfgPath "ca.key", + caCrtFile, + serverKeyFile, + serverCrtFile, + fingerprintFile = combine cfgPath "fingerprint", + defaultServerPort = "443", + executableName = "ntf-server", + serverVersion = "SMP notifications server v0.1.0", + mkServerConfig = \_storeLogFile transports -> + NtfServerConfig + { transports, + subIdBytes = 24, + clientQSize = 16, + subQSize = 64, + pushQSize = 128, + smpAgentCfg = defaultSMPClientAgentConfig, + caCertificateFile = caCrtFile, + privateKeyFile = serverKeyFile, + certificateFile = serverCrtFile + } + } diff --git a/apps/smp-server/Main.hs b/apps/smp-server/Main.hs index 163b312c8..28b6b754b 100644 --- a/apps/smp-server/Main.hs +++ b/apps/smp-server/Main.hs @@ -1,318 +1,53 @@ -{-# LANGUAGE DataKinds #-} {-# LANGUAGE DuplicateRecordFields #-} -{-# LANGUAGE GADTs #-} -{-# LANGUAGE LambdaCase #-} {-# LANGUAGE NamedFieldPuns #-} -{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE TypeApplications #-} module Main where -import Control.Monad.Except -import Data.ByteString.Char8 (ByteString) -import qualified Data.ByteString.Char8 as B -import Data.Either (fromRight) -import Data.Ini (Ini, lookupValue, readIniFile) -import Data.Maybe (fromMaybe) -import qualified Data.Text as T -import Data.X509.Validation (Fingerprint (..)) -import Network.Socket (HostName, ServiceName) -import Options.Applicative -import Simplex.Messaging.Encoding.String import Simplex.Messaging.Server (runSMPServer) -import Simplex.Messaging.Server.Env.STM -import Simplex.Messaging.Server.StoreLog (StoreLog, openReadStoreLog, storeLogFilePath) -import Simplex.Messaging.Transport (ATransport (..), TLS, Transport (..), simplexMQVersion) -import Simplex.Messaging.Transport.Server (loadFingerprint) -import Simplex.Messaging.Transport.WebSockets (WS) -import System.Directory (createDirectoryIfMissing, doesDirectoryExist, doesFileExist, removeDirectoryRecursive) -import System.Exit (exitFailure) +import Simplex.Messaging.Server.CLI (ServerCLIConfig (..), protocolServerCLI) +import Simplex.Messaging.Server.Env.STM (ServerConfig (..)) +import Simplex.Messaging.Transport (simplexMQVersion) import System.FilePath (combine) -import System.IO (BufferMode (..), IOMode (..), hGetLine, hSetBuffering, stderr, stdout, withFile) -import System.Process (readCreateProcess, shell) -import Text.Read (readMaybe) -cfgDir :: FilePath -cfgDir = "/etc/opt/simplex" +cfgPath :: FilePath +cfgPath = "/etc/opt/simplex" -logDir :: FilePath -logDir = "/var/opt/simplex" - -iniFile :: FilePath -iniFile = combine cfgDir "smp-server.ini" - -storeLogFile :: FilePath -storeLogFile = combine logDir "smp-server-store.log" - -caKeyFile :: FilePath -caKeyFile = combine cfgDir "ca.key" - -caCrtFile :: FilePath -caCrtFile = combine cfgDir "ca.crt" - -serverKeyFile :: FilePath -serverKeyFile = combine cfgDir "server.key" - -serverCrtFile :: FilePath -serverCrtFile = combine cfgDir "server.crt" - -fingerprintFile :: FilePath -fingerprintFile = combine cfgDir "fingerprint" +logPath :: FilePath +logPath = "/var/opt/simplex" main :: IO () -main = do - getCliCommand >>= \case - Init opts -> - doesFileExist iniFile >>= \case - True -> exitError $ "Error: server is already initialized (" <> iniFile <> " exists).\nRun `smp-server start`." - _ -> initializeServer opts - Start -> - doesFileExist iniFile >>= \case - True -> readIniFile iniFile >>= either exitError (runServer . mkIniOptions) - _ -> exitError $ "Error: server is not initialized (" <> iniFile <> " does not exist).\nRun `smp-server init`." - Delete -> cleanup >> putStrLn "Deleted configuration and log files" +main = protocolServerCLI smpServerCLIConfig runSMPServer -exitError :: String -> IO () -exitError msg = putStrLn msg >> exitFailure - -data CliCommand - = Init InitOptions - | Start - | Delete - -data InitOptions = InitOptions - { enableStoreLog :: Bool, - signAlgorithm :: SignAlgorithm, - ip :: HostName, - fqdn :: Maybe HostName - } - deriving (Show) - -data SignAlgorithm = ED448 | ED25519 - deriving (Read, Show) - -getCliCommand :: IO CliCommand -getCliCommand = - customExecParser - (prefs showHelpOnEmpty) - ( info - (helper <*> versionOption <*> cliCommandP) - (header version <> fullDesc) - ) - where - versionOption = infoOption version (long "version" <> short 'v' <> help "Show version") - -cliCommandP :: Parser CliCommand -cliCommandP = - hsubparser - ( command "init" (info initP (progDesc $ "Initialize server - creates " <> cfgDir <> " and " <> logDir <> " directories and configuration files")) - <> command "start" (info (pure Start) (progDesc $ "Start server (configuration: " <> iniFile <> ")")) - <> command "delete" (info (pure Delete) (progDesc "Delete configuration and log files")) - ) - where - initP :: Parser CliCommand - initP = - Init - <$> ( InitOptions - <$> switch - ( long "store-log" - <> short 'l' - <> help "Enable store log for SMP queues persistence" - ) - <*> option - (maybeReader readMaybe) - ( long "sign-algorithm" - <> short 'a' - <> help "Signature algorithm used for TLS certificates: ED25519, ED448" - <> value ED448 - <> showDefault - <> metavar "ALG" - ) - <*> strOption - ( long "ip" - <> help - "Server IP address, used as Common Name for TLS online certificate if FQDN is not supplied" - <> value "127.0.0.1" - <> showDefault - <> metavar "IP" - ) - <*> (optional . strOption) - ( long "fqdn" - <> short 'n' - <> help "Server FQDN used as Common Name for TLS online certificate" - <> showDefault - <> metavar "FQDN" - ) - ) - -initializeServer :: InitOptions -> IO () -initializeServer InitOptions {enableStoreLog, signAlgorithm, ip, fqdn} = do - cleanup - createDirectoryIfMissing True cfgDir - createDirectoryIfMissing True logDir - createX509 - fp <- saveFingerprint - createIni - putStrLn $ "Server initialized, you can modify configuration in " <> iniFile <> ".\nRun `smp-server start` to start server." - printServiceInfo fp - warnCAPrivateKeyFile - where - createX509 = do - createOpensslCaConf - createOpensslServerConf - -- CA certificate (identity/offline) - run $ "openssl genpkey -algorithm " <> show signAlgorithm <> " -out " <> caKeyFile - run $ "openssl req -new -x509 -days 999999 -config " <> opensslCaConfFile <> " -extensions v3 -key " <> caKeyFile <> " -out " <> caCrtFile - -- server certificate (online) - run $ "openssl genpkey -algorithm " <> show signAlgorithm <> " -out " <> serverKeyFile - run $ "openssl req -new -config " <> opensslServerConfFile <> " -reqexts v3 -key " <> serverKeyFile <> " -out " <> serverCsrFile - run $ "openssl x509 -req -days 999999 -extfile " <> opensslServerConfFile <> " -extensions v3 -in " <> serverCsrFile <> " -CA " <> caCrtFile <> " -CAkey " <> caKeyFile <> " -CAcreateserial -out " <> serverCrtFile - where - run cmd = void $ readCreateProcess (shell cmd) "" - opensslCaConfFile = combine cfgDir "openssl_ca.conf" - opensslServerConfFile = combine cfgDir "openssl_server.conf" - serverCsrFile = combine cfgDir "server.csr" - createOpensslCaConf = - writeFile - opensslCaConfFile - "[req]\n\ - \distinguished_name = req_distinguished_name\n\ - \prompt = no\n\n\ - \[req_distinguished_name]\n\ - \CN = SMP server CA\n\ - \O = SimpleX\n\n\ - \[v3]\n\ - \subjectKeyIdentifier = hash\n\ - \authorityKeyIdentifier = keyid:always\n\ - \basicConstraints = critical,CA:true\n" - -- TODO revise https://www.rfc-editor.org/rfc/rfc5280#section-4.2.1.3, https://www.rfc-editor.org/rfc/rfc3279#section-2.3.5 - -- IP and FQDN can't both be used as server address interchangeably even if IP is added - -- as Subject Alternative Name, unless the following validation hook is disabled: - -- https://hackage.haskell.org/package/x509-validation-1.6.10/docs/src/Data-X509-Validation.html#validateCertificateName - createOpensslServerConf = - writeFile - opensslServerConfFile - ( "[req]\n\ - \distinguished_name = req_distinguished_name\n\ - \prompt = no\n\n\ - \[req_distinguished_name]\n" - <> ("CN = " <> cn <> "\n\n") - <> "[v3]\n\ - \basicConstraints = CA:FALSE\n\ - \keyUsage = digitalSignature, nonRepudiation, keyAgreement\n\ - \extendedKeyUsage = serverAuth\n" - ) - where - cn = fromMaybe ip fqdn - - saveFingerprint = do - Fingerprint fp <- loadFingerprint caCrtFile - withFile fingerprintFile WriteMode (`B.hPutStrLn` strEncode fp) - pure fp - - createIni = do - writeFile iniFile $ - "[STORE_LOG]\n\ - \# The server uses STM memory to store SMP queues and messages,\n\ - \# that will be lost on restart (e.g., as with redis).\n\ - \# This option enables saving SMP queues to append only log,\n\ - \# and restoring them when the server is started.\n\ - \# Log is compacted on start (deleted queues are removed).\n\ - \# The messages in the queues are not logged.\n" - <> ("enable: " <> (if enableStoreLog then "on" else "off # on") <> "\n\n") - <> "[TRANSPORT]\n\ - \port: 5223\n\ - \websockets: off\n" - - warnCAPrivateKeyFile = - putStrLn $ - "----------\n\ - \You should store CA private key securely and delete it from the server.\n\ - \If server TLS credential is compromised this key can be used to sign a new one, \ - \keeping the same server identity and established connections.\n\ - \CA private key location:\n" - <> caKeyFile - <> "\n----------" - -data IniOptions = IniOptions - { enableStoreLog :: Bool, - port :: ServiceName, - enableWebsockets :: Bool - } - --- TODO ? properly parse ini as a whole -mkIniOptions :: Ini -> IniOptions -mkIniOptions ini = - IniOptions - { enableStoreLog = (== "on") $ strict "STORE_LOG" "enable", - port = T.unpack $ strict "TRANSPORT" "port", - enableWebsockets = (== "on") $ strict "TRANSPORT" "websockets" - } - where - strict :: String -> String -> T.Text - strict section key = - fromRight (error ("no key " <> key <> " in section " <> section)) $ - lookupValue (T.pack section) (T.pack key) ini - -runServer :: IniOptions -> IO () -runServer IniOptions {enableStoreLog, port, enableWebsockets} = do - hSetBuffering stdout LineBuffering - hSetBuffering stderr LineBuffering - fp <- checkSavedFingerprint - printServiceInfo fp - storeLog <- openStoreLog - let cfg = mkServerConfig storeLog - printServerConfig cfg - runSMPServer cfg - where - checkSavedFingerprint = do - savedFingerprint <- loadSavedFingerprint - Fingerprint fp <- loadFingerprint caCrtFile - when (B.pack savedFingerprint /= strEncode fp) $ - exitError "Stored fingerprint is invalid." - pure fp - - mkServerConfig storeLog = - ServerConfig - { transports = (port, transport @TLS) : [("80", transport @WS) | enableWebsockets], - tbqSize = 16, - serverTbqSize = 64, - msgQueueQuota = 128, - queueIdBytes = 24, - msgIdBytes = 24, -- must be at least 24 bytes, it is used as 192-bit nonce for XSalsa20 - caCertificateFile = caCrtFile, - privateKeyFile = serverKeyFile, - certificateFile = serverCrtFile, - storeLog +smpServerCLIConfig :: ServerCLIConfig ServerConfig +smpServerCLIConfig = + let caCrtFile = combine cfgPath "ca.crt" + serverKeyFile = combine cfgPath "server.key" + serverCrtFile = combine cfgPath "server.crt" + in ServerCLIConfig + { cfgDir = cfgPath, + logDir = logPath, + iniFile = combine cfgPath "smp-server.ini", + storeLogFile = combine logPath "smp-server-store.log", + caKeyFile = combine cfgPath "ca.key", + caCrtFile, + serverKeyFile, + serverCrtFile, + fingerprintFile = combine cfgPath "fingerprint", + defaultServerPort = "5223", + executableName = "smp-server", + serverVersion = "SMP server v" <> simplexMQVersion, + mkServerConfig = \storeLogFile transports -> + ServerConfig + { transports, + tbqSize = 16, + serverTbqSize = 64, + msgQueueQuota = 128, + queueIdBytes = 24, + msgIdBytes = 24, -- must be at least 24 bytes, it is used as 192-bit nonce for XSalsa20 + caCertificateFile = caCrtFile, + privateKeyFile = serverKeyFile, + certificateFile = serverCrtFile, + storeLogFile + } } - - openStoreLog :: IO (Maybe (StoreLog 'ReadMode)) - openStoreLog = - if enableStoreLog - then Just <$> openReadStoreLog storeLogFile - else pure Nothing - - printServerConfig ServerConfig {storeLog, transports} = do - putStrLn $ case storeLog of - Just s -> "Store log: " <> storeLogFilePath s - Nothing -> "Store log disabled." - forM_ transports $ \(p, ATransport t) -> - putStrLn $ "Listening on port " <> p <> " (" <> transportName t <> ")..." - -cleanup :: IO () -cleanup = do - deleteDirIfExists cfgDir - deleteDirIfExists logDir - where - deleteDirIfExists path = doesDirectoryExist path >>= (`when` removeDirectoryRecursive path) - -printServiceInfo :: ByteString -> IO () -printServiceInfo fpStr = do - putStrLn version - B.putStrLn $ "Fingerprint: " <> strEncode fpStr - -version :: String -version = "SMP server v" <> simplexMQVersion - -loadSavedFingerprint :: IO String -loadSavedFingerprint = withFile fingerprintFile ReadMode hGetLine diff --git a/package.yaml b/package.yaml index f87ca111b..40d8e8955 100644 --- a/package.yaml +++ b/package.yaml @@ -1,7 +1,7 @@ name: simplexmq version: 1.0.3 synopsis: SimpleXMQ message broker -description: | +description: | This package includes <./docs/Simplex-Messaging-Server.html server>, <./docs/Simplex-Messaging-Client.html client> and <./docs/Simplex-Messaging-Agent.html agent> for SMP protocols: @@ -42,12 +42,15 @@ dependencies: - filepath == 1.4.* - http-types == 0.12.* - generic-random >= 1.3 && < 1.5 + - ini == 0.4.* - iso8601-time == 0.1.* - memory == 0.15.* - mtl == 2.2.* - network == 3.1.2.* - network-transport == 0.5.* + - optparse-applicative >= 0.15 && < 0.17 - QuickCheck == 2.14.* + - process == 1.6.* - random >= 1.1 && < 1.3 - simple-logger == 0.1.* - sqlite-simple == 0.4.* @@ -72,9 +75,14 @@ executables: source-dirs: apps/smp-server main: Main.hs dependencies: - - ini == 0.4.* - - optparse-applicative >= 0.15 && < 0.17 - - process == 1.6.* + - simplexmq + ghc-options: + - -threaded + + ntf-server: + source-dirs: apps/ntf-server + main: Main.hs + dependencies: - simplexmq ghc-options: - -threaded diff --git a/simplexmq.cabal b/simplexmq.cabal index 3b9e8eb7e..66089a3e3 100644 --- a/simplexmq.cabal +++ b/simplexmq.cabal @@ -57,6 +57,7 @@ library Simplex.Messaging.Parsers Simplex.Messaging.Protocol Simplex.Messaging.Server + Simplex.Messaging.Server.CLI Simplex.Messaging.Server.Env.STM Simplex.Messaging.Server.MsgStore Simplex.Messaging.Server.MsgStore.STM @@ -98,11 +99,14 @@ library , filepath ==1.4.* , generic-random >=1.3 && <1.5 , http-types ==0.12.* + , ini ==0.4.* , iso8601-time ==0.1.* , memory ==0.15.* , mtl ==2.2.* , network ==3.1.2.* , network-transport ==0.5.* + , optparse-applicative >=0.15 && <0.17 + , process ==1.6.* , random >=1.1 && <1.3 , simple-logger ==0.1.* , sqlite-simple ==0.4.* @@ -227,6 +231,61 @@ executable smp-server , x509-validation ==1.6.* default-language: Haskell2010 +executable ntf-server + main-is: Main.hs + other-modules: + Paths_simplexmq + hs-source-dirs: + apps/ntf-server + ghc-options: -Wall -Wcompat -Werror=incomplete-patterns -Wredundant-constraints -Wincomplete-record-updates -Wincomplete-uni-patterns -Wunused-type-patterns -threaded + build-depends: + QuickCheck ==2.14.* + , aeson ==2.0.* + , ansi-terminal >=0.10 && <0.12 + , asn1-encoding ==0.9.* + , asn1-types ==0.3.* + , async ==2.2.* + , attoparsec ==0.14.* + , base >=4.7 && <5 + , base64-bytestring >=1.0 && <1.3 + , bytestring ==0.10.* + , composition ==1.0.* + , constraints >=0.12 && <0.14 + , containers ==0.6.* + , cryptonite >=0.27 && <0.30 + , cryptostore ==0.2.* + , data-default ==0.7.* + , direct-sqlite ==2.3.* + , directory ==1.3.* + , filepath ==1.4.* + , generic-random >=1.3 && <1.5 + , http-types ==0.12.* + , ini ==0.4.* + , iso8601-time ==0.1.* + , memory ==0.15.* + , mtl ==2.2.* + , network ==3.1.2.* + , network-transport ==0.5.* + , optparse-applicative >=0.15 && <0.17 + , process ==1.6.* + , random >=1.1 && <1.3 + , simple-logger ==0.1.* + , simplexmq + , sqlite-simple ==0.4.* + , stm ==2.5.* + , template-haskell ==2.16.* + , text ==1.2.* + , time ==1.9.* + , tls >=1.5.7 && <1.6 + , transformers ==0.5.* + , unliftio ==0.2.* + , unliftio-core ==0.2.* + , websockets ==0.12.* + , x509 ==1.7.* + , x509-store ==1.6.* + , x509-validation ==1.6.* + default-language: Haskell2010 + test-suite smp-server-test type: exitcode-stdio-1.0 main-is: Test.hs diff --git a/src/Simplex/Messaging/Agent/Env/SQLite.hs b/src/Simplex/Messaging/Agent/Env/SQLite.hs index 66f510ff6..af400ff81 100644 --- a/src/Simplex/Messaging/Agent/Env/SQLite.hs +++ b/src/Simplex/Messaging/Agent/Env/SQLite.hs @@ -2,6 +2,7 @@ {-# LANGUAGE DuplicateRecordFields #-} {-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE NumericUnderscores #-} +{-# LANGUAGE TypeApplications #-} {-# OPTIONS_GHC -fno-warn-unticked-promoted-constructors #-} module Simplex.Messaging.Agent.Env.SQLite @@ -23,8 +24,8 @@ import Simplex.Messaging.Agent.RetryInterval import Simplex.Messaging.Agent.Store.SQLite import qualified Simplex.Messaging.Agent.Store.SQLite.Migrations as Migrations import Simplex.Messaging.Client -import Simplex.Messaging.Client.Agent (SMPClientAgentConfig, defaultSMPClientAgentConfig) import qualified Simplex.Messaging.Crypto as C +import Simplex.Messaging.Transport (TLS, Transport (..)) import System.Random (StdGen, newStdGen) import UnliftIO.STM @@ -57,8 +58,8 @@ defaultAgentConfig = dbFile = "smp-agent.db", dbPoolSize = 4, yesToMigrations = False, - smpCfg = defaultClientConfig, - ntfCfg = defaultClientConfig, + smpCfg = defaultClientConfig {defaultTransport = ("5223", transport @TLS)}, + ntfCfg = defaultClientConfig {defaultTransport = ("443", transport @TLS)}, reconnectInterval = RetryInterval { initialInterval = second, diff --git a/src/Simplex/Messaging/Client.hs b/src/Simplex/Messaging/Client.hs index db30d3085..75ce01126 100644 --- a/src/Simplex/Messaging/Client.hs +++ b/src/Simplex/Messaging/Client.hs @@ -112,7 +112,7 @@ defaultClientConfig :: ProtocolClientConfig defaultClientConfig = ProtocolClientConfig { qSize = 64, - defaultTransport = ("5223", transport @TLS), + defaultTransport = ("443", transport @TLS), tcpTimeout = 5_000_000, tcpKeepAlive = Just defaultKeepAliveOpts, smpPing = 300_000_000 -- 5 min diff --git a/src/Simplex/Messaging/Notifications/Server/Env.hs b/src/Simplex/Messaging/Notifications/Server/Env.hs index fec2aafb6..8b2a416a1 100644 --- a/src/Simplex/Messaging/Notifications/Server/Env.hs +++ b/src/Simplex/Messaging/Notifications/Server/Env.hs @@ -13,7 +13,6 @@ import Data.X509.Validation (Fingerprint (..)) import Network.Socket import qualified Network.TLS as T import Numeric.Natural -import Simplex.Messaging.Agent.RetryInterval import Simplex.Messaging.Client.Agent import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Notifications.Protocol @@ -30,7 +29,6 @@ data NtfServerConfig = NtfServerConfig subQSize :: Natural, pushQSize :: Natural, smpAgentCfg :: SMPClientAgentConfig, - reconnectInterval :: RetryInterval, -- CA certificate private key is not needed for initialization caCertificateFile :: FilePath, privateKeyFile :: FilePath, diff --git a/src/Simplex/Messaging/Server/CLI.hs b/src/Simplex/Messaging/Server/CLI.hs new file mode 100644 index 000000000..63959d45a --- /dev/null +++ b/src/Simplex/Messaging/Server/CLI.hs @@ -0,0 +1,285 @@ +{-# LANGUAGE DataKinds #-} +{-# LANGUAGE DuplicateRecordFields #-} +{-# LANGUAGE FlexibleContexts #-} +{-# LANGUAGE GADTs #-} +{-# LANGUAGE LambdaCase #-} +{-# LANGUAGE NamedFieldPuns #-} +{-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE TypeApplications #-} + +module Simplex.Messaging.Server.CLI where + +import Control.Monad +import Data.ByteString.Char8 (ByteString) +import qualified Data.ByteString.Char8 as B +import Data.Either (fromRight) +import Data.Ini (Ini, lookupValue, readIniFile) +import Data.Maybe (fromMaybe) +import qualified Data.Text as T +import Data.X509.Validation (Fingerprint (..)) +import Network.Socket (HostName, ServiceName) +import Options.Applicative +import Simplex.Messaging.Encoding.String +import Simplex.Messaging.Transport (ATransport (..), TLS, Transport (..)) +import Simplex.Messaging.Transport.Server (loadFingerprint) +import Simplex.Messaging.Transport.WebSockets (WS) +import System.Directory (createDirectoryIfMissing, doesDirectoryExist, doesFileExist, removeDirectoryRecursive) +import System.Exit (exitFailure) +import System.FilePath (combine) +import System.IO (BufferMode (..), IOMode (..), hGetLine, hSetBuffering, stderr, stdout, withFile) +import System.Process (readCreateProcess, shell) +import Text.Read (readMaybe) + +data ServerCLIConfig cfg = ServerCLIConfig + { cfgDir :: FilePath, + logDir :: FilePath, + iniFile :: FilePath, + storeLogFile :: FilePath, + caKeyFile :: FilePath, + caCrtFile :: FilePath, + serverKeyFile :: FilePath, + serverCrtFile :: FilePath, + fingerprintFile :: FilePath, + defaultServerPort :: ServiceName, + executableName :: String, + serverVersion :: String, + mkServerConfig :: Maybe FilePath -> [(ServiceName, ATransport)] -> cfg + } + +protocolServerCLI :: ServerCLIConfig cfg -> (cfg -> IO ()) -> IO () +protocolServerCLI cliCfg@ServerCLIConfig {iniFile, executableName} server = + getCliCommand cliCfg >>= \case + Init opts -> + doesFileExist iniFile >>= \case + True -> exitError $ "Error: server is already initialized (" <> iniFile <> " exists).\nRun `" <> executableName <> " start`." + _ -> initializeServer cliCfg opts + Start -> + doesFileExist iniFile >>= \case + True -> readIniFile iniFile >>= either exitError (runServer cliCfg server . mkIniOptions) + _ -> exitError $ "Error: server is not initialized (" <> iniFile <> " does not exist).\nRun `" <> executableName <> " init`." + Delete -> cleanup cliCfg >> putStrLn "Deleted configuration and log files" + +exitError :: String -> IO () +exitError msg = putStrLn msg >> exitFailure + +data CliCommand + = Init InitOptions + | Start + | Delete + +data InitOptions = InitOptions + { enableStoreLog :: Bool, + signAlgorithm :: SignAlgorithm, + ip :: HostName, + fqdn :: Maybe HostName + } + deriving (Show) + +data SignAlgorithm = ED448 | ED25519 + deriving (Read, Show) + +getCliCommand :: ServerCLIConfig cfg -> IO CliCommand +getCliCommand cliCfg = + customExecParser + (prefs showHelpOnEmpty) + ( info + (helper <*> versionOption <*> cliCommandP cliCfg) + (header version <> fullDesc) + ) + where + versionOption = infoOption version (long "version" <> short 'v' <> help "Show version") + version = serverVersion cliCfg + +cliCommandP :: ServerCLIConfig cfg -> Parser CliCommand +cliCommandP ServerCLIConfig {cfgDir, logDir, iniFile} = + hsubparser + ( command "init" (info initP (progDesc $ "Initialize server - creates " <> cfgDir <> " and " <> logDir <> " directories and configuration files")) + <> command "start" (info (pure Start) (progDesc $ "Start server (configuration: " <> iniFile <> ")")) + <> command "delete" (info (pure Delete) (progDesc "Delete configuration and log files")) + ) + where + initP :: Parser CliCommand + initP = + Init + <$> ( InitOptions + <$> switch + ( long "store-log" + <> short 'l' + <> help "Enable store log for persistence" + ) + <*> option + (maybeReader readMaybe) + ( long "sign-algorithm" + <> short 'a' + <> help "Signature algorithm used for TLS certificates: ED25519, ED448" + <> value ED448 + <> showDefault + <> metavar "ALG" + ) + <*> strOption + ( long "ip" + <> help + "Server IP address, used as Common Name for TLS online certificate if FQDN is not supplied" + <> value "127.0.0.1" + <> showDefault + <> metavar "IP" + ) + <*> (optional . strOption) + ( long "fqdn" + <> short 'n' + <> help "Server FQDN used as Common Name for TLS online certificate" + <> showDefault + <> metavar "FQDN" + ) + ) + +initializeServer :: ServerCLIConfig cfg -> InitOptions -> IO () +initializeServer cliCfg InitOptions {enableStoreLog, signAlgorithm, ip, fqdn} = do + cleanup cliCfg + createDirectoryIfMissing True cfgDir + createDirectoryIfMissing True logDir + createX509 + fp <- saveFingerprint + createIni + putStrLn $ "Server initialized, you can modify configuration in " <> iniFile <> ".\nRun `" <> executableName <> " start` to start server." + printServiceInfo cliCfg fp + warnCAPrivateKeyFile + where + ServerCLIConfig {cfgDir, logDir, iniFile, executableName, caKeyFile, caCrtFile, serverKeyFile, serverCrtFile, fingerprintFile, defaultServerPort} = cliCfg + createX509 = do + createOpensslCaConf + createOpensslServerConf + -- CA certificate (identity/offline) + run $ "openssl genpkey -algorithm " <> show signAlgorithm <> " -out " <> caKeyFile + run $ "openssl req -new -x509 -days 999999 -config " <> opensslCaConfFile <> " -extensions v3 -key " <> caKeyFile <> " -out " <> caCrtFile + -- server certificate (online) + run $ "openssl genpkey -algorithm " <> show signAlgorithm <> " -out " <> serverKeyFile + run $ "openssl req -new -config " <> opensslServerConfFile <> " -reqexts v3 -key " <> serverKeyFile <> " -out " <> serverCsrFile + run $ "openssl x509 -req -days 999999 -extfile " <> opensslServerConfFile <> " -extensions v3 -in " <> serverCsrFile <> " -CA " <> caCrtFile <> " -CAkey " <> caKeyFile <> " -CAcreateserial -out " <> serverCrtFile + where + run cmd = void $ readCreateProcess (shell cmd) "" + opensslCaConfFile = combine cfgDir "openssl_ca.conf" + opensslServerConfFile = combine cfgDir "openssl_server.conf" + serverCsrFile = combine cfgDir "server.csr" + createOpensslCaConf = + writeFile + opensslCaConfFile + "[req]\n\ + \distinguished_name = req_distinguished_name\n\ + \prompt = no\n\n\ + \[req_distinguished_name]\n\ + \CN = SMP server CA\n\ + \O = SimpleX\n\n\ + \[v3]\n\ + \subjectKeyIdentifier = hash\n\ + \authorityKeyIdentifier = keyid:always\n\ + \basicConstraints = critical,CA:true\n" + -- TODO revise https://www.rfc-editor.org/rfc/rfc5280#section-4.2.1.3, https://www.rfc-editor.org/rfc/rfc3279#section-2.3.5 + -- IP and FQDN can't both be used as server address interchangeably even if IP is added + -- as Subject Alternative Name, unless the following validation hook is disabled: + -- https://hackage.haskell.org/package/x509-validation-1.6.10/docs/src/Data-X509-Validation.html#validateCertificateName + createOpensslServerConf = + writeFile + opensslServerConfFile + ( "[req]\n\ + \distinguished_name = req_distinguished_name\n\ + \prompt = no\n\n\ + \[req_distinguished_name]\n" + <> ("CN = " <> cn <> "\n\n") + <> "[v3]\n\ + \basicConstraints = CA:FALSE\n\ + \keyUsage = digitalSignature, nonRepudiation, keyAgreement\n\ + \extendedKeyUsage = serverAuth\n" + ) + where + cn = fromMaybe ip fqdn + + saveFingerprint = do + Fingerprint fp <- loadFingerprint caCrtFile + withFile fingerprintFile WriteMode (`B.hPutStrLn` strEncode fp) + pure fp + + createIni = do + writeFile iniFile $ + "[STORE_LOG]\n\ + \# The server uses STM memory for persistence,\n\ + \# that will be lost on restart (e.g., as with redis).\n\ + \# This option enables saving memory to append only log,\n\ + \# and restoring it when the server is started.\n\ + \# Log is compacted on start (deleted objects are removed).\n\ + \# The messages are not logged.\n" + <> ("enable: " <> (if enableStoreLog then "on" else "off # on") <> "\n\n") + <> "[TRANSPORT]\n\ + \port: " + <> defaultServerPort + <> "\n\ + \websockets: off\n" + + warnCAPrivateKeyFile = + putStrLn $ + "----------\n\ + \You should store CA private key securely and delete it from the server.\n\ + \If server TLS credential is compromised this key can be used to sign a new one, \ + \keeping the same server identity and established connections.\n\ + \CA private key location:\n" + <> caKeyFile + <> "\n----------" + +data IniOptions = IniOptions + { enableStoreLog :: Bool, + port :: ServiceName, + enableWebsockets :: Bool + } + +-- TODO ? properly parse ini as a whole +mkIniOptions :: Ini -> IniOptions +mkIniOptions ini = + IniOptions + { enableStoreLog = (== "on") $ strict "STORE_LOG" "enable", + port = T.unpack $ strict "TRANSPORT" "port", + enableWebsockets = (== "on") $ strict "TRANSPORT" "websockets" + } + where + strict :: String -> String -> T.Text + strict section key = + fromRight (error ("no key " <> key <> " in section " <> section)) $ + lookupValue (T.pack section) (T.pack key) ini + +runServer :: ServerCLIConfig cfg -> (cfg -> IO ()) -> IniOptions -> IO () +runServer cliCfg server IniOptions {enableStoreLog, port, enableWebsockets} = do + hSetBuffering stdout LineBuffering + hSetBuffering stderr LineBuffering + fp <- checkSavedFingerprint + printServiceInfo cliCfg fp + let transports = (port, transport @TLS) : [("80", transport @WS) | enableWebsockets] + logFile = if enableStoreLog then Just storeLogFile else Nothing + cfg = mkServerConfig logFile transports + printServerConfig logFile transports + server cfg + where + ServerCLIConfig {storeLogFile, caCrtFile, fingerprintFile, mkServerConfig} = cliCfg + checkSavedFingerprint = do + savedFingerprint <- withFile fingerprintFile ReadMode hGetLine + Fingerprint fp <- loadFingerprint caCrtFile + when (B.pack savedFingerprint /= strEncode fp) $ + exitError "Stored fingerprint is invalid." + pure fp + + printServerConfig logFile transports = do + putStrLn $ case logFile of + Just f -> "Store log: " <> f + _ -> "Store log disabled." + forM_ transports $ \(p, ATransport t) -> + putStrLn $ "Listening on port " <> p <> " (" <> transportName t <> ")..." + +cleanup :: ServerCLIConfig cfg -> IO () +cleanup ServerCLIConfig {cfgDir, logDir} = do + deleteDirIfExists cfgDir + deleteDirIfExists logDir + where + deleteDirIfExists path = doesDirectoryExist path >>= (`when` removeDirectoryRecursive path) + +printServiceInfo :: ServerCLIConfig cfg -> ByteString -> IO () +printServiceInfo ServerCLIConfig {serverVersion} fpStr = do + putStrLn serverVersion + B.putStrLn $ "Fingerprint: " <> strEncode fpStr diff --git a/src/Simplex/Messaging/Server/Env/STM.hs b/src/Simplex/Messaging/Server/Env/STM.hs index 3c4599a97..f704367a6 100644 --- a/src/Simplex/Messaging/Server/Env/STM.hs +++ b/src/Simplex/Messaging/Server/Env/STM.hs @@ -35,7 +35,8 @@ data ServerConfig = ServerConfig msgQueueQuota :: Natural, queueIdBytes :: Int, msgIdBytes :: Int, - storeLog :: Maybe (StoreLog 'ReadMode), + storeLogFile :: Maybe FilePath, + -- storeLog :: Maybe (StoreLog 'ReadMode), -- CA certificate private key is not needed for initialization caCertificateFile :: FilePath, privateKeyFile :: FilePath, @@ -99,12 +100,13 @@ newSubscription = do return Sub {subThread = NoSub, delivered} newEnv :: forall m. (MonadUnliftIO m, MonadRandom m) => ServerConfig -> m Env -newEnv config@ServerConfig {caCertificateFile, certificateFile, privateKeyFile} = do +newEnv config@ServerConfig {caCertificateFile, certificateFile, privateKeyFile, storeLogFile} = do server <- atomically $ newServer (serverTbqSize config) queueStore <- atomically newQueueStore msgStore <- atomically newMsgStore idsDrg <- drgNew >>= newTVarIO - s' <- restoreQueues queueStore `mapM` storeLog (config :: ServerConfig) + storeLog <- liftIO $ openReadStoreLog `mapM` storeLogFile + s' <- restoreQueues queueStore `mapM` storeLog tlsServerParams <- liftIO $ loadTLSServerParams caCertificateFile certificateFile privateKeyFile Fingerprint fp <- liftIO $ loadFingerprint caCertificateFile let serverIdentity = KeyHash fp diff --git a/tests/SMPClient.hs b/tests/SMPClient.hs index 3789647f5..e5a57ab72 100644 --- a/tests/SMPClient.hs +++ b/tests/SMPClient.hs @@ -19,7 +19,6 @@ import Simplex.Messaging.Encoding import Simplex.Messaging.Protocol import Simplex.Messaging.Server (runSMPServerBlocking) import Simplex.Messaging.Server.Env.STM -import Simplex.Messaging.Server.StoreLog (openReadStoreLog) import Simplex.Messaging.Transport import Simplex.Messaging.Transport.Client import Simplex.Messaging.Transport.KeepAlive @@ -60,19 +59,17 @@ cfg = msgQueueQuota = 4, queueIdBytes = 24, msgIdBytes = 24, - storeLog = Nothing, + storeLogFile = Nothing, caCertificateFile = "tests/fixtures/ca.crt", privateKeyFile = "tests/fixtures/server.key", certificateFile = "tests/fixtures/server.crt" } withSmpServerStoreLogOn :: (MonadUnliftIO m, MonadRandom m) => ATransport -> ServiceName -> (ThreadId -> m a) -> m a -withSmpServerStoreLogOn t port' client = do - s <- liftIO $ openReadStoreLog testStoreLogFile +withSmpServerStoreLogOn t port' = serverBracket - (\started -> runSMPServerBlocking started cfg {transports = [(port', t)], storeLog = Just s}) + (\started -> runSMPServerBlocking started cfg {transports = [(port', t)], storeLogFile = Just testStoreLogFile}) (pure ()) - client withSmpServerThreadOn :: (MonadUnliftIO m, MonadRandom m) => ATransport -> ServiceName -> (ThreadId -> m a) -> m a withSmpServerThreadOn t port' = From f577fcdacf15cac9f763555e4d97f6b5dd542bcf Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Fri, 8 Apr 2022 08:47:04 +0100 Subject: [PATCH 005/131] agent schema/methods/types/store methods for notifications tokens (#348) * agent schema/methods/types/store methods for notifications tokens * register notification token on the server * agent commands for notification tokens * refactor initial servers from AgentConfig * agent store functions for notification tokens * server STM store methods for tokens * fix protocol client for ntfs (use generic handshake), minimal server and agent tests * server command to verify ntf token --- apps/ntf-server/Main.hs | 1 + apps/smp-agent/Main.hs | 11 +- simplexmq.cabal | 2 + src/Simplex/Messaging/Agent.hs | 117 ++++++++++++++++-- src/Simplex/Messaging/Agent/Client.hs | 44 +++++-- src/Simplex/Messaging/Agent/Env/SQLite.hs | 29 +++-- src/Simplex/Messaging/Agent/Server.hs | 12 +- src/Simplex/Messaging/Agent/Store.hs | 8 ++ src/Simplex/Messaging/Agent/Store/SQLite.hs | 89 +++++++++++-- .../Migrations/M20220322_notifications.hs | 37 ++---- .../M20220404_ntf_subscriptions_draft.hs | 38 ++++++ src/Simplex/Messaging/Client.hs | 10 +- src/Simplex/Messaging/Encoding/String.hs | 8 +- src/Simplex/Messaging/Notifications/Client.hs | 93 +++++++++++--- .../Messaging/Notifications/Protocol.hs | 87 +++++++++++-- src/Simplex/Messaging/Notifications/Server.hs | 35 ++++-- .../Messaging/Notifications/Server/Env.hs | 18 ++- .../Notifications/Server/Subscriptions.hs | 17 +-- src/Simplex/Messaging/Parsers.hs | 10 ++ src/Simplex/Messaging/Protocol.hs | 6 +- tests/AgentTests/FunctionalAPITests.hs | 44 ++++--- tests/NtfClient.hs | 106 ++++++++++++++++ tests/NtfServerTests.hs | 40 ++++++ tests/SMPAgentClient.hs | 15 ++- tests/Test.hs | 2 + 25 files changed, 732 insertions(+), 147 deletions(-) create mode 100644 src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220404_ntf_subscriptions_draft.hs create mode 100644 tests/NtfClient.hs create mode 100644 tests/NtfServerTests.hs diff --git a/apps/ntf-server/Main.hs b/apps/ntf-server/Main.hs index 8426093e5..928b9e514 100644 --- a/apps/ntf-server/Main.hs +++ b/apps/ntf-server/Main.hs @@ -39,6 +39,7 @@ ntfServerCLIConfig = NtfServerConfig { transports, subIdBytes = 24, + regCodeBytes = 32, clientQSize = 16, subQSize = 64, pushQSize = 128, diff --git a/apps/smp-agent/Main.hs b/apps/smp-agent/Main.hs index 32e038f56..72ca7f073 100644 --- a/apps/smp-agent/Main.hs +++ b/apps/smp-agent/Main.hs @@ -11,7 +11,14 @@ import Simplex.Messaging.Agent.Server (runSMPAgent) import Simplex.Messaging.Transport (TLS, Transport (..)) cfg :: AgentConfig -cfg = defaultAgentConfig {initialSMPServers = L.fromList ["smp://bU0K-bRg24xWW__lS0umO1Zdw_SXqpJNtm1_RrPLViE=@localhost:5223"]} +cfg = defaultAgentConfig + +servers :: InitialAgentServers +servers = + InitialAgentServers + { smp = L.fromList ["smp://bU0K-bRg24xWW__lS0umO1Zdw_SXqpJNtm1_RrPLViE=@localhost:5223"], + ntf = [] + } logCfg :: LogConfig logCfg = LogConfig {lc_file = Nothing, lc_stderr = True} @@ -20,4 +27,4 @@ main :: IO () main = do putStrLn $ "SMP agent listening on port " ++ tcpPort (cfg :: AgentConfig) setLogLevel LogInfo -- LogError - withGlobalLogging logCfg $ runSMPAgent (transport @TLS) cfg + withGlobalLogging logCfg $ runSMPAgent (transport @TLS) cfg servers diff --git a/simplexmq.cabal b/simplexmq.cabal index 66089a3e3..6bd072f5b 100644 --- a/simplexmq.cabal +++ b/simplexmq.cabal @@ -298,6 +298,8 @@ test-suite smp-server-test CoreTests.EncodingTests CoreTests.ProtocolErrorTests CoreTests.VersionRangeTests + NtfClient, + NtfServerTests ServerTests SMPAgentClient SMPClient diff --git a/src/Simplex/Messaging/Agent.hs b/src/Simplex/Messaging/Agent.hs index 821f5fe5a..812fb6e72 100644 --- a/src/Simplex/Messaging/Agent.hs +++ b/src/Simplex/Messaging/Agent.hs @@ -48,6 +48,11 @@ module Simplex.Messaging.Agent suspendConnection, deleteConnection, setSMPServers, + setNtfServers, + registerNtfToken, + verifyNtfToken, + enableNtfCron, + deleteNtfToken, logConnection, ) where @@ -69,6 +74,7 @@ import Data.Maybe (isJust) import qualified Data.Text as T import Data.Time.Clock import Data.Time.Clock.System (systemToUTCTime) +import Data.Word (Word16) import Database.SQLite.Simple (SQLError) import Simplex.Messaging.Agent.Client import Simplex.Messaging.Agent.Env.SQLite @@ -80,7 +86,8 @@ import Simplex.Messaging.Client (ServerTransmission) import qualified Simplex.Messaging.Crypto as C import qualified Simplex.Messaging.Crypto.Ratchet as CR import Simplex.Messaging.Encoding -import Simplex.Messaging.Notifications.Protocol (DeviceToken) +import Simplex.Messaging.Notifications.Client +import Simplex.Messaging.Notifications.Protocol (DeviceToken, NtfRegCode, NtfTknStatus (..)) import Simplex.Messaging.Parsers (parse) import Simplex.Messaging.Protocol (BrokerMsg, MsgBody) import qualified Simplex.Messaging.Protocol as SMP @@ -93,11 +100,11 @@ import qualified UnliftIO.Exception as E import UnliftIO.STM -- | Creates an SMP agent client instance -getSMPAgentClient :: (MonadRandom m, MonadUnliftIO m) => AgentConfig -> m AgentClient -getSMPAgentClient cfg = newSMPAgentEnv cfg >>= runReaderT runAgent +getSMPAgentClient :: (MonadRandom m, MonadUnliftIO m) => AgentConfig -> InitialAgentServers -> m AgentClient +getSMPAgentClient cfg initServers = newSMPAgentEnv cfg >>= runReaderT runAgent where runAgent = do - c <- getAgentClient + c <- getAgentClient initServers action <- async $ subscriber c `E.finally` disconnectAgentClient c pure c {smpSubscriber = action} @@ -150,10 +157,24 @@ deleteConnection c = withAgentEnv c . deleteConnection' c setSMPServers :: AgentErrorMonad m => AgentClient -> NonEmpty SMPServer -> m () setSMPServers c = withAgentEnv c . setSMPServers' c +setNtfServers :: AgentErrorMonad m => AgentClient -> [NtfServer] -> m () +setNtfServers c = withAgentEnv c . setNtfServers' c + -- | Register device notifications token registerNtfToken :: AgentErrorMonad m => AgentClient -> DeviceToken -> m () registerNtfToken c = withAgentEnv c . registerNtfToken' c +-- | Verify device notifications token +verifyNtfToken :: AgentErrorMonad m => AgentClient -> DeviceToken -> NtfRegCode -> m () +verifyNtfToken c = withAgentEnv c .: verifyNtfToken' c + +-- | Enable/disable periodic notifications +enableNtfCron :: AgentErrorMonad m => AgentClient -> DeviceToken -> Word16 -> m () +enableNtfCron c = withAgentEnv c .: enableNtfCron' c + +deleteNtfToken :: AgentErrorMonad m => AgentClient -> DeviceToken -> m () +deleteNtfToken c = withAgentEnv c . deleteNtfToken' c + withAgentEnv :: AgentClient -> ReaderT Env m a -> m a withAgentEnv c = (`runReaderT` agentEnv c) @@ -161,8 +182,8 @@ withAgentEnv c = (`runReaderT` agentEnv c) -- withAgentClient c = withAgentLock c . withAgentEnv c -- | Creates an SMP agent client instance that receives commands and sends responses via 'TBQueue's. -getAgentClient :: (MonadUnliftIO m, MonadReader Env m) => m AgentClient -getAgentClient = ask >>= atomically . newAgentClient +getAgentClient :: (MonadUnliftIO m, MonadReader Env m) => InitialAgentServers -> m AgentClient +getAgentClient initServers = ask >>= atomically . newAgentClient initServers logConnection :: MonadUnliftIO m => AgentClient -> Bool -> m () logConnection c connected = @@ -499,8 +520,73 @@ setSMPServers' :: AgentMonad m => AgentClient -> NonEmpty SMPServer -> m () setSMPServers' c servers = do atomically $ writeTVar (smpServers c) servers -registerNtfToken' :: AgentMonad m => AgentClient -> DeviceToken -> m () -registerNtfToken' c token = pure () +registerNtfToken' :: forall m. AgentMonad m => AgentClient -> DeviceToken -> m () +registerNtfToken' c deviceToken = + withStore (`getDeviceNtfToken` deviceToken) >>= \case + Just tkn@NtfToken {ntfTokenId, ntfTknAction} -> case (ntfTokenId, ntfTknAction) of + (Nothing, Just (NTARegister ntfPubKey)) -> registerToken tkn ntfPubKey + -- TODO request verification code again in case there is registration, but no verification code in DB - probably after some timeout? + (Just tknId, Just (NTAVerify code)) -> + t tkn (NTActive, Just NTACheck) $ agentNtfVerifyToken c tknId tkn code + (Just tknId, Just (NTACron interval)) -> + t tkn (NTActive, Just NTACheck) $ agentNtfEnableCron c tknId tkn interval + (Just _tknId, Just NTACheck) -> pure () + (Just tknId, Just NTADelete) -> + t tkn (NTExpired, Nothing) $ agentNtfDeleteToken c tknId tkn + _ -> pure () + _ -> + getNtfServer c >>= \case + Just ntfServer -> + asks (cmdSignAlg . config) >>= \case + C.SignAlg a -> do + (ntfPubKey, ntfPrivKey) <- liftIO $ C.generateSignatureKeyPair a + let tkn = newNtfToken deviceToken ntfServer ntfPrivKey ntfPubKey + withStore $ \st -> createNtfToken st tkn + registerToken tkn ntfPubKey + _ -> throwError $ CMD PROHIBITED + where + t tkn = withToken tkn Nothing + registerToken :: NtfToken -> C.APublicVerifyKey -> m () + registerToken tkn ntfPubKey = do + (pubDhKey, privDhKey) <- liftIO C.generateKeyPair' + (tknId, srvPubDhKey) <- agentNtfRegisterToken c tkn ntfPubKey pubDhKey + let dhSecret = C.dh' srvPubDhKey privDhKey + withStore $ \st -> updateNtfTokenRegistration st tkn tknId dhSecret + +verifyNtfToken' :: AgentMonad m => AgentClient -> DeviceToken -> NtfRegCode -> m () +verifyNtfToken' c deviceToken code = + withStore (`getDeviceNtfToken` deviceToken) >>= \case + Just tkn@NtfToken {ntfTokenId = Just tknId} -> + withToken tkn (Just (NTConfirmed, NTAVerify code)) (NTActive, Just NTACheck) $ + agentNtfVerifyToken c tknId tkn code + _ -> throwError $ CMD PROHIBITED + +enableNtfCron' :: AgentMonad m => AgentClient -> DeviceToken -> Word16 -> m () +enableNtfCron' c deviceToken interval = + withStore (`getDeviceNtfToken` deviceToken) >>= \case + Just tkn@NtfToken {ntfTokenId = Just tknId, ntfTknStatus = NTActive} -> + withToken tkn (Just (NTActive, NTACron interval)) (NTActive, Just NTACheck) $ + agentNtfEnableCron c tknId tkn interval + _ -> throwError $ CMD PROHIBITED + +deleteNtfToken' :: AgentMonad m => AgentClient -> DeviceToken -> m () +deleteNtfToken' c deviceToken = + withStore (`getDeviceNtfToken` deviceToken) >>= \case + Just tkn@NtfToken {ntfTokenId = Just tknId, ntfTknStatus} -> + withToken tkn (Just (ntfTknStatus, NTADelete)) (NTExpired, Nothing) $ + agentNtfDeleteToken c tknId tkn + _ -> throwError $ CMD PROHIBITED + +withToken :: AgentMonad m => NtfToken -> Maybe (NtfTknStatus, NtfTknAction) -> (NtfTknStatus, Maybe NtfTknAction) -> m a -> m a +withToken tkn from_ (toStatus, toAction_) f = do + forM_ from_ $ \(status, action) -> withStore $ \st -> updateNtfToken st tkn status (Just action) + res <- f + withStore $ \st -> updateNtfToken st tkn toStatus toAction_ + pure res + +setNtfServers' :: AgentMonad m => AgentClient -> [NtfServer] -> m () +setNtfServers' c servers = do + atomically $ writeTVar (ntfServers c) servers getSMPServer :: AgentMonad m => AgentClient -> m SMPServer getSMPServer c = do @@ -509,8 +595,19 @@ getSMPServer c = do srv :| [] -> pure srv servers -> do gen <- asks randomServer - i <- atomically . stateTVar gen $ randomR (0, L.length servers - 1) - pure $ servers L.!! i + atomically . stateTVar gen $ + first (servers L.!!) . randomR (0, L.length servers - 1) + +getNtfServer :: AgentMonad m => AgentClient -> m (Maybe NtfServer) +getNtfServer c = do + ntfServers <- readTVarIO $ ntfServers c + case ntfServers of + [] -> pure Nothing + [srv] -> pure $ Just srv + servers -> do + gen <- asks randomServer + atomically . stateTVar gen $ + first (Just . (servers !!)) . randomR (0, length servers - 1) subscriber :: (MonadUnliftIO m, MonadReader Env m) => AgentClient -> m () subscriber c@AgentClient {msgQ} = forever $ do diff --git a/src/Simplex/Messaging/Agent/Client.hs b/src/Simplex/Messaging/Agent/Client.hs index 321fc82ab..fc37334db 100644 --- a/src/Simplex/Messaging/Agent/Client.hs +++ b/src/Simplex/Messaging/Agent/Client.hs @@ -8,7 +8,6 @@ {-# LANGUAGE OverloadedLists #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE ScopedTypeVariables #-} -{-# LANGUAGE TypeSynonymInstances #-} module Simplex.Messaging.Agent.Client ( AgentClient (..), @@ -24,6 +23,10 @@ module Simplex.Messaging.Agent.Client RetryInterval (..), secureQueue, sendAgentMessage, + agentNtfRegisterToken, + agentNtfVerifyToken, + agentNtfDeleteToken, + agentNtfEnableCron, agentCbEncrypt, agentCbDecrypt, cryptoError, @@ -51,6 +54,7 @@ import Data.Map.Strict (Map) import qualified Data.Map.Strict as M import Data.Maybe (isNothing) import Data.Text.Encoding +import Data.Word (Word16) import Simplex.Messaging.Agent.Env.SQLite import Simplex.Messaging.Agent.Protocol import Simplex.Messaging.Agent.RetryInterval @@ -59,8 +63,8 @@ import Simplex.Messaging.Client import Simplex.Messaging.Client.Agent () import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Encoding -import Simplex.Messaging.Notifications.Client (NtfClient) -import Simplex.Messaging.Notifications.Protocol (NtfResponse) +import Simplex.Messaging.Notifications.Client +import Simplex.Messaging.Notifications.Protocol import Simplex.Messaging.Protocol (BrokerMsg, ProtocolServer (..), QueueId, QueueIdsKeys (..), SndPublicVerifyKey) import qualified Simplex.Messaging.Protocol as SMP import Simplex.Messaging.TMap (TMap) @@ -83,8 +87,9 @@ data AgentClient = AgentClient subQ :: TBQueue (ATransmission 'Agent), msgQ :: TBQueue (ServerTransmission BrokerMsg), smpServers :: TVar (NonEmpty SMPServer), + ntfServers :: TVar [NtfServer], smpClients :: TMap SMPServer SMPClientVar, - ntfClients :: TMap ProtocolServer NtfClientVar, + ntfClients :: TMap NtfServer NtfClientVar, subscrSrvrs :: TMap SMPServer (TMap ConnId RcvQueue), pendingSubscrSrvrs :: TMap SMPServer (TMap ConnId RcvQueue), subscrConns :: TMap ConnId SMPServer, @@ -99,13 +104,14 @@ data AgentClient = AgentClient lock :: TMVar () } -newAgentClient :: Env -> STM AgentClient -newAgentClient agentEnv = do +newAgentClient :: InitialAgentServers -> Env -> STM AgentClient +newAgentClient InitialAgentServers {smp, ntf} agentEnv = do let qSize = tbqSize $ config agentEnv rcvQ <- newTBQueue qSize subQ <- newTBQueue qSize msgQ <- newTBQueue qSize - smpServers <- newTVar $ initialSMPServers (config agentEnv) + smpServers <- newTVar smp + ntfServers <- newTVar ntf smpClients <- TM.empty ntfClients <- TM.empty subscrSrvrs <- TM.empty @@ -118,7 +124,7 @@ newAgentClient agentEnv = do asyncClients <- newTVar [] clientId <- stateTVar (clientCounter agentEnv) $ \i -> (i + 1, i + 1) lock <- newTMVar () - return AgentClient {rcvQ, subQ, msgQ, smpServers, smpClients, ntfClients, subscrSrvrs, pendingSubscrSrvrs, subscrConns, connMsgsQueued, smpQueueMsgQueues, smpQueueMsgDeliveries, reconnections, asyncClients, clientId, agentEnv, smpSubscriber = undefined, lock} + return AgentClient {rcvQ, subQ, msgQ, smpServers, ntfServers, smpClients, ntfClients, subscrSrvrs, pendingSubscrSrvrs, subscrConns, connMsgsQueued, smpQueueMsgQueues, smpQueueMsgDeliveries, reconnections, asyncClients, clientId, agentEnv, smpSubscriber = undefined, lock} -- | Agent monad with MonadReader Env and MonadError AgentErrorType type AgentMonad m = (MonadUnliftIO m, MonadReader Env m, MonadError AgentErrorType m) @@ -206,7 +212,7 @@ getSMPServerClient c@AgentClient {smpClients, msgQ} srv = notifySub :: ACommand 'Agent -> ConnId -> IO () notifySub cmd connId = atomically $ writeTBQueue (subQ c) ("", connId, cmd) -getNtfServerClient :: forall m. AgentMonad m => AgentClient -> ProtocolServer -> m NtfClient +getNtfServerClient :: forall m. AgentMonad m => AgentClient -> NtfServer -> m NtfClient getNtfServerClient c@AgentClient {ntfClients} srv = atomically (getClientVar srv ntfClients) >>= either @@ -256,10 +262,10 @@ newProtocolClient c srv clients connectClient reconnectClient clientVar = tryCon tryConnectClient :: (ProtocolClient msg -> m a) -> m () -> m a tryConnectClient successAction retryAction = tryError connectClient >>= \r -> case r of - Right smp -> do + Right client -> do logInfo . decodeUtf8 $ "Agent connected to " <> showServer srv atomically $ putTMVar clientVar r - successAction smp + successAction client Left e -> do if e == BROKER NETWORK || e == BROKER TIMEOUT then retryAction @@ -464,6 +470,22 @@ sendAgentMessage c sq@SndQueue {server, sndId, sndPrivateKey} agentMsg = msg <- agentCbEncrypt sq Nothing $ smpEncode clientMsg liftClient $ sendSMPMessage smp (Just sndPrivateKey) sndId msg +agentNtfRegisterToken :: AgentMonad m => AgentClient -> NtfToken -> C.APublicVerifyKey -> C.PublicKeyX25519 -> m (NtfTokenId, C.PublicKeyX25519) +agentNtfRegisterToken c NtfToken {deviceToken, ntfServer, ntfPrivKey} ntfPubKey pubDhKey = + withClient c ntfServer $ \ntf -> ntfRegisterToken ntf ntfPrivKey (NewNtfTkn deviceToken ntfPubKey pubDhKey) + +agentNtfVerifyToken :: AgentMonad m => AgentClient -> NtfTokenId -> NtfToken -> NtfRegCode -> m () +agentNtfVerifyToken c tknId NtfToken {ntfServer, ntfPrivKey} code = + withLogClient c ntfServer tknId "TVFY" $ \ntf -> ntfVerifyToken ntf ntfPrivKey tknId code + +agentNtfDeleteToken :: AgentMonad m => AgentClient -> NtfTokenId -> NtfToken -> m () +agentNtfDeleteToken c tknId NtfToken {ntfServer, ntfPrivKey} = + withLogClient c ntfServer tknId "TDEL" $ \ntf -> ntfDeleteToken ntf ntfPrivKey tknId + +agentNtfEnableCron :: AgentMonad m => AgentClient -> NtfTokenId -> NtfToken -> Word16 -> m () +agentNtfEnableCron c tknId NtfToken {ntfServer, ntfPrivKey} interval = + withLogClient c ntfServer tknId "TCRN" $ \ntf -> ntfEnableCron ntf ntfPrivKey tknId interval + agentCbEncrypt :: AgentMonad m => SndQueue -> Maybe C.PublicKeyX25519 -> ByteString -> m ByteString agentCbEncrypt SndQueue {e2eDhSecret} e2ePubKey msg = do cmNonce <- liftIO C.randomCbNonce diff --git a/src/Simplex/Messaging/Agent/Env/SQLite.hs b/src/Simplex/Messaging/Agent/Env/SQLite.hs index af400ff81..bd1207426 100644 --- a/src/Simplex/Messaging/Agent/Env/SQLite.hs +++ b/src/Simplex/Messaging/Agent/Env/SQLite.hs @@ -7,7 +7,9 @@ module Simplex.Messaging.Agent.Env.SQLite ( AgentConfig (..), + InitialAgentServers (..), defaultAgentConfig, + defaultReconnectInterval, Env (..), newSMPAgentEnv, ) @@ -25,13 +27,18 @@ import Simplex.Messaging.Agent.Store.SQLite import qualified Simplex.Messaging.Agent.Store.SQLite.Migrations as Migrations import Simplex.Messaging.Client import qualified Simplex.Messaging.Crypto as C +import Simplex.Messaging.Notifications.Client (NtfServer) import Simplex.Messaging.Transport (TLS, Transport (..)) import System.Random (StdGen, newStdGen) import UnliftIO.STM +data InitialAgentServers = InitialAgentServers + { smp :: NonEmpty SMPServer, + ntf :: [NtfServer] + } + data AgentConfig = AgentConfig { tcpPort :: ServiceName, - initialSMPServers :: NonEmpty SMPServer, cmdSignAlg :: C.SignAlg, connIdBytes :: Int, tbqSize :: Natural, @@ -47,11 +54,20 @@ data AgentConfig = AgentConfig certificateFile :: FilePath } +defaultReconnectInterval :: RetryInterval +defaultReconnectInterval = + RetryInterval + { initialInterval = second, + increaseAfter = 10 * second, + maxInterval = 10 * second + } + where + second = 1_000_000 + defaultAgentConfig :: AgentConfig defaultAgentConfig = AgentConfig { tcpPort = "5224", - initialSMPServers = undefined, -- TODO move it elsewhere? cmdSignAlg = C.SignAlg C.SEd448, connIdBytes = 12, tbqSize = 64, @@ -60,12 +76,7 @@ defaultAgentConfig = yesToMigrations = False, smpCfg = defaultClientConfig {defaultTransport = ("5223", transport @TLS)}, ntfCfg = defaultClientConfig {defaultTransport = ("443", transport @TLS)}, - reconnectInterval = - RetryInterval - { initialInterval = second, - increaseAfter = 10 * second, - maxInterval = 10 * second - }, + reconnectInterval = defaultReconnectInterval, helloTimeout = 2 * nominalDay, -- CA certificate private key is not needed for initialization -- ! we do not generate these @@ -73,8 +84,6 @@ defaultAgentConfig = privateKeyFile = "/etc/opt/simplex-agent/agent.key", certificateFile = "/etc/opt/simplex-agent/agent.crt" } - where - second = 1_000_000 data Env = Env { config :: AgentConfig, diff --git a/src/Simplex/Messaging/Agent/Server.hs b/src/Simplex/Messaging/Agent/Server.hs index b3c70c9ac..7193cd772 100644 --- a/src/Simplex/Messaging/Agent/Server.hs +++ b/src/Simplex/Messaging/Agent/Server.hs @@ -31,17 +31,17 @@ import UnliftIO.STM -- | Runs an SMP agent as a TCP service using passed configuration. -- -- See a full agent executable here: https://github.com/simplex-chat/simplexmq/blob/master/apps/smp-agent/Main.hs -runSMPAgent :: (MonadRandom m, MonadUnliftIO m) => ATransport -> AgentConfig -> m () -runSMPAgent t cfg = do +runSMPAgent :: (MonadRandom m, MonadUnliftIO m) => ATransport -> AgentConfig -> InitialAgentServers -> m () +runSMPAgent t cfg initServers = do started <- newEmptyTMVarIO - runSMPAgentBlocking t started cfg + runSMPAgentBlocking t started cfg initServers -- | Runs an SMP agent as a TCP service using passed configuration with signalling. -- -- This function uses passed TMVar to signal when the server is ready to accept TCP requests (True) -- and when it is disconnected from the TCP socket once the server thread is killed (False). -runSMPAgentBlocking :: (MonadRandom m, MonadUnliftIO m) => ATransport -> TMVar Bool -> AgentConfig -> m () -runSMPAgentBlocking (ATransport t) started cfg@AgentConfig {tcpPort, caCertificateFile, certificateFile, privateKeyFile} = do +runSMPAgentBlocking :: (MonadRandom m, MonadUnliftIO m) => ATransport -> TMVar Bool -> AgentConfig -> InitialAgentServers -> m () +runSMPAgentBlocking (ATransport t) started cfg@AgentConfig {tcpPort, caCertificateFile, certificateFile, privateKeyFile} initServers = do runReaderT (smpAgent t) =<< newSMPAgentEnv cfg where smpAgent :: forall c m'. (Transport c, MonadUnliftIO m', MonadReader Env m') => TProxy c -> m' () @@ -50,7 +50,7 @@ runSMPAgentBlocking (ATransport t) started cfg@AgentConfig {tcpPort, caCertifica tlsServerParams <- liftIO $ loadTLSServerParams caCertificateFile certificateFile privateKeyFile runTransportServer started tcpPort tlsServerParams $ \(h :: c) -> do liftIO . putLn h $ "Welcome to SMP agent v" <> B.pack simplexMQVersion - c <- getAgentClient + c <- getAgentClient initServers logConnection c True race_ (connectClient h c) (runAgentClient c) `E.finally` disconnectAgentClient c diff --git a/src/Simplex/Messaging/Agent/Store.hs b/src/Simplex/Messaging/Agent/Store.hs index 4e20594a8..efcd87d3c 100644 --- a/src/Simplex/Messaging/Agent/Store.hs +++ b/src/Simplex/Messaging/Agent/Store.hs @@ -20,6 +20,8 @@ import Data.Type.Equality import Simplex.Messaging.Agent.Protocol import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Crypto.Ratchet (RatchetX448, SkippedMsgDiff, SkippedMsgKeys) +import Simplex.Messaging.Notifications.Client +import Simplex.Messaging.Notifications.Protocol (DeviceToken, NtfTknStatus, NtfTokenId) import Simplex.Messaging.Protocol ( MsgBody, MsgId, @@ -75,6 +77,12 @@ class Monad m => MonadAgentStore s m where getSkippedMsgKeys :: s -> ConnId -> m SkippedMsgKeys updateRatchet :: s -> ConnId -> RatchetX448 -> SkippedMsgDiff -> m () + -- Notification device token persistence + createNtfToken :: s -> NtfToken -> m () + getDeviceNtfToken :: s -> DeviceToken -> m (Maybe NtfToken) -- return current token if it exists and mark any old tokens for deletion + updateNtfTokenRegistration :: s -> NtfToken -> NtfTokenId -> C.DhSecretX25519 -> m () + updateNtfToken :: s -> NtfToken -> NtfTknStatus -> Maybe NtfTknAction -> m () + -- * Queue types -- | A receive queue. SMP queue through which the agent receives messages from a sender. diff --git a/src/Simplex/Messaging/Agent/Store/SQLite.hs b/src/Simplex/Messaging/Agent/Store/SQLite.hs index 3f42fd139..52235f852 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite.hs @@ -23,7 +23,6 @@ module Simplex.Messaging.Agent.Store.SQLite connectSQLiteStore, withConnection, withTransaction, - fromTextField_, firstRow, ) where @@ -41,14 +40,14 @@ import Data.Char (toLower) import Data.Functor (($>)) import Data.List (find, foldl') import qualified Data.Map.Strict as M +import Data.Maybe (listToMaybe) import Data.Text (Text) import qualified Data.Text as T import Data.Text.Encoding (decodeLatin1) -import Database.SQLite.Simple (FromRow, NamedParam (..), Only (..), SQLData (..), SQLError, ToRow, field) +import Data.Time.Clock (getCurrentTime) +import Database.SQLite.Simple (FromRow, NamedParam (..), Only (..), SQLError, ToRow, field) import qualified Database.SQLite.Simple as DB import Database.SQLite.Simple.FromField -import Database.SQLite.Simple.Internal (Field (..)) -import Database.SQLite.Simple.Ok (Ok (Ok)) import Database.SQLite.Simple.QQ (sql) import Database.SQLite.Simple.ToField (ToField (..)) import Simplex.Messaging.Agent.Protocol @@ -59,7 +58,9 @@ import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Crypto.Ratchet (RatchetX448, SkippedMsgDiff (..), SkippedMsgKeys) import Simplex.Messaging.Encoding import Simplex.Messaging.Encoding.String -import Simplex.Messaging.Parsers (blobFieldParser) +import Simplex.Messaging.Notifications.Client (NtfServer, NtfTknAction, NtfToken (..)) +import Simplex.Messaging.Notifications.Protocol (DeviceToken (..), NtfTknStatus (..), NtfTokenId) +import Simplex.Messaging.Parsers (blobFieldParser, fromTextField_) import Simplex.Messaging.Protocol (MsgBody, ProtocolServer (..)) import qualified Simplex.Messaging.Protocol as SMP import Simplex.Messaging.Util (bshow, liftIOEither) @@ -565,6 +566,63 @@ instance (MonadUnliftIO m, MonadError StoreError m) => MonadAgentStore SQLiteSto forM_ (M.assocs mks) $ \(msgN, mk) -> DB.execute db "INSERT INTO skipped_messages (conn_id, header_key, msg_n, msg_key) VALUES (?, ?, ?, ?)" (connId, hk, msgN, mk) + createNtfToken :: SQLiteStore -> NtfToken -> m () + createNtfToken st NtfToken {deviceToken = DeviceToken provider token, ntfServer = srv@ProtocolServer {host, port}, ntfTokenId, ntfPrivKey, ntfDhSecret, ntfTknStatus, ntfTknAction} = + liftIO . withTransaction st $ \db -> do + upsertNtfServer_ db srv + DB.execute + db + [sql| + INSERT INTO ntf_tokens + (provider, device_token, ntf_host, ntf_port, tkn_id, tkn_priv_key, tkn_dh_secret, tkn_status, tkn_action) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) + |] + (provider, token, host, port, ntfTokenId, ntfPrivKey, ntfDhSecret, ntfTknStatus, ntfTknAction) + + getDeviceNtfToken :: SQLiteStore -> DeviceToken -> m (Maybe NtfToken) + getDeviceNtfToken st t@(DeviceToken provider token) = + liftIO . withTransaction st $ \db -> + fmap ntfToken . listToMaybe + <$> DB.query + db + [sql| + SELECT s.ntf_host, s.ntf_port, s.ntf_key_hash, + t.tkn_id, t.tkn_priv_key, t.tkn_dh_secret, t.tkn_status, t.tkn_action + FROM ntf_tokens t + JOIN ntf_servers s USING (ntf_host, ntf_port) + WHERE t.provider = ? AND t.device_token = ? + |] + (provider, token) + where + ntfToken (host, port, keyHash, ntfTokenId, ntfPrivKey, ntfDhSecret, ntfTknStatus, ntfTknAction) = + let ntfServer = ProtocolServer {host, port, keyHash} + in NtfToken {deviceToken = t, ntfServer, ntfTokenId, ntfPrivKey, ntfDhSecret, ntfTknStatus, ntfTknAction} + + updateNtfTokenRegistration :: SQLiteStore -> NtfToken -> NtfTokenId -> C.DhSecretX25519 -> m () + updateNtfTokenRegistration st NtfToken {deviceToken = DeviceToken provider token, ntfServer = ProtocolServer {host, port}} tknId ntfDhSecret = + liftIO . withTransaction st $ \db -> do + updatedAt <- getCurrentTime + DB.execute + db + [sql| + UPDATE ntf_tokens + SET tkn_id = ?, tkn_dh_secret = ?, tkn_status = ?, tkn_action = ?, updated_at = ? + WHERE provider = ? AND device_token = ? AND ntf_host = ? AND ntf_port = ? + |] + (tknId, ntfDhSecret, NTRegistered, Nothing :: Maybe NtfTknAction, updatedAt, provider, token, host, port) + + updateNtfToken :: SQLiteStore -> NtfToken -> NtfTknStatus -> Maybe NtfTknAction -> m () + updateNtfToken st NtfToken {deviceToken = DeviceToken provider token, ntfServer = ProtocolServer {host, port}} tknStatus tknAction = + liftIO . withTransaction st $ \db -> do + updatedAt <- getCurrentTime + DB.execute + db + [sql| + UPDATE ntf_tokens + SET tkn_status = ?, tkn_action = ?, updated_at = ? + WHERE provider = ? AND device_token = ? AND ntf_host = ? AND ntf_port = ? + |] + (tknStatus, tknAction, updatedAt, provider, token, host, port) + -- * Auxiliary helpers instance ToField QueueStatus where toField = toField . serializeQueueStatus @@ -611,14 +669,6 @@ instance ToField (SConnectionMode c) where toField = toField . connMode instance FromField AConnectionMode where fromField = fromTextField_ $ fmap connMode' . connModeT -fromTextField_ :: (E.Typeable a) => (Text -> Maybe a) -> Field -> Ok a -fromTextField_ fromText = \case - f@(Field (SQLText t) _) -> - case fromText t of - Just x -> Ok x - _ -> returnError ConversionFailed f ("invalid text: " <> T.unpack t) - f -> returnError ConversionFailed f "expecting SQLText column type" - listToEither :: e -> [a] -> Either e a listToEither _ (x : _) = Right x listToEither e _ = Left e @@ -669,6 +719,19 @@ upsertServer_ dbConn ProtocolServer {host, port, keyHash} = do |] [":host" := host, ":port" := port, ":key_hash" := keyHash] +upsertNtfServer_ :: DB.Connection -> NtfServer -> IO () +upsertNtfServer_ db ProtocolServer {host, port, keyHash} = do + DB.executeNamed + db + [sql| + INSERT INTO ntf_servers (ntf_host, ntf_port, ntf_key_hash) VALUES (:host,:port,:key_hash) + ON CONFLICT (ntf_host, ntf_port) DO UPDATE SET + ntf_host=excluded.ntf_host, + ntf_port=excluded.ntf_port, + ntf_key_hash=excluded.ntf_key_hash; + |] + [":host" := host, ":port" := port, ":key_hash" := keyHash] + -- * createRcvConn helpers insertRcvQueue_ :: DB.Connection -> ConnId -> RcvQueue -> IO () diff --git a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220322_notifications.hs b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220322_notifications.hs index 8fd996810..3587e05b1 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220322_notifications.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220322_notifications.hs @@ -8,40 +8,29 @@ import Database.SQLite.Simple.QQ (sql) m20220322_notifications :: Query m20220322_notifications = [sql| -ALTER TABLE rcv_queues ADD COLUMN ntf_id BLOB; - -ALTER TABLE rcv_queues ADD COLUMN ntf_public_key BLOB; - -ALTER TABLE rcv_queues ADD COLUMN ntf_private_key BLOB; - -CREATE UNIQUE INDEX idx_rcv_queues_ntf ON rcv_queues (host, port, ntf_id); - CREATE TABLE ntf_servers ( ntf_host TEXT NOT NULL, ntf_port TEXT NOT NULL, ntf_key_hash BLOB NOT NULL, - created_at TEXT NOT NULL, - updated_at TEXT NOT NULL, + created_at TEXT NOT NULL DEFAULT (datetime('now')), + updated_at TEXT NOT NULL DEFAULT (datetime('now')), PRIMARY KEY (ntf_host, ntf_port) ) WITHOUT ROWID; -CREATE TABLE ntf_subscriptions ( +CREATE TABLE ntf_tokens ( + provider TEXT NOT NULL, -- apn + device_token TEXT NOT NULL, ntf_host TEXT NOT NULL, ntf_port TEXT NOT NULL, - ntf_sub_id BLOB NOT NULL, - ntf_sub_status TEXT NOT NULL, -- new, created, active, pending, error_auth - ntf_sub_action TEXT, -- if there is an action required on this subscription: create / check / token / delete - ntf_sub_action_ts TEXT, -- the earliest time for the action, e.g. checks can be scheduled every X hours - ntf_token TEXT NOT NULL, -- or BLOB? - smp_host TEXT NOT NULL, - smp_port TEXT NOT NULL, - smp_ntf_id BLOB NOT NULL, - created_at TEXT NOT NULL, - updated_at TEXT NOT NULL, -- this is to check subscription status periodically to know when it was last checked - PRIMARY KEY (ntf_host, ntf_port, ntf_sub_id), + tkn_id BLOB, -- token ID assigned by notifications server + tkn_priv_key BLOB NOT NULL, -- private key to sign token commands + tkn_dh_secret BLOB, -- DH secret for e2e encryption of notifications + tkn_status TEXT NOT NULL, + tkn_action BLOB, + created_at TEXT NOT NULL DEFAULT (datetime('now')), + updated_at TEXT NOT NULL DEFAULT (datetime('now')), -- this is to check token status periodically to know when it was last checked + PRIMARY KEY (provider, device_token, ntf_host, ntf_port), FOREIGN KEY (ntf_host, ntf_port) REFERENCES ntf_servers - ON DELETE RESTRICT ON UPDATE CASCADE, - FOREIGN KEY (smp_host, smp_port, smp_ntf_id) REFERENCES rcv_queues (host, port, ntf_id) ON DELETE RESTRICT ON UPDATE CASCADE ) WITHOUT ROWID; |] diff --git a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220404_ntf_subscriptions_draft.hs b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220404_ntf_subscriptions_draft.hs new file mode 100644 index 000000000..5f58ca0fd --- /dev/null +++ b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220404_ntf_subscriptions_draft.hs @@ -0,0 +1,38 @@ +{-# LANGUAGE QuasiQuotes #-} + +module Simplex.Messaging.Agent.Store.SQLite.Migrations.M20220404_ntf_subscriptions_draft where + +import Database.SQLite.Simple (Query) +import Database.SQLite.Simple.QQ (sql) + +m20220404_ntf_subscriptions_draft :: Query +m20220404_ntf_subscriptions_draft = + [sql| +ALTER TABLE rcv_queues ADD COLUMN ntf_id BLOB; + +ALTER TABLE rcv_queues ADD COLUMN ntf_public_key BLOB; + +ALTER TABLE rcv_queues ADD COLUMN ntf_private_key BLOB; + +CREATE UNIQUE INDEX idx_rcv_queues_ntf ON rcv_queues (host, port, ntf_id); + +CREATE TABLE ntf_subscriptions ( + ntf_host TEXT NOT NULL, + ntf_port TEXT NOT NULL, + ntf_sub_id BLOB NOT NULL, + ntf_sub_status TEXT NOT NULL, -- new, created, active, pending, error_auth + ntf_sub_action TEXT, -- if there is an action required on this subscription: create / check / token / delete + ntf_sub_action_ts TEXT, -- the earliest time for the action, e.g. checks can be scheduled every X hours + ntf_token TEXT NOT NULL, -- or BLOB? + smp_host TEXT NOT NULL, + smp_port TEXT NOT NULL, + smp_ntf_id BLOB NOT NULL, + created_at TEXT NOT NULL, + updated_at TEXT NOT NULL, -- this is to check subscription status periodically to know when it was last checked + PRIMARY KEY (ntf_host, ntf_port, ntf_sub_id), + FOREIGN KEY (ntf_host, ntf_port) REFERENCES ntf_servers + ON DELETE RESTRICT ON UPDATE CASCADE, + FOREIGN KEY (smp_host, smp_port, smp_ntf_id) REFERENCES rcv_queues (host, port, ntf_id) + ON DELETE RESTRICT ON UPDATE CASCADE +) WITHOUT ROWID; +|] \ No newline at end of file diff --git a/src/Simplex/Messaging/Client.hs b/src/Simplex/Messaging/Client.hs index 75ce01126..4174e8555 100644 --- a/src/Simplex/Messaging/Client.hs +++ b/src/Simplex/Messaging/Client.hs @@ -66,7 +66,7 @@ import Simplex.Messaging.Protocol as SMP import Simplex.Messaging.TMap (TMap) import qualified Simplex.Messaging.TMap as TM import Simplex.Messaging.Transport (ATransport (..), THandle (..), TLS, TProxy, Transport (..), TransportError) -import Simplex.Messaging.Transport.Client (runTransportClient, smpClientHandshake) +import Simplex.Messaging.Transport.Client (runTransportClient) import Simplex.Messaging.Transport.KeepAlive import Simplex.Messaging.Transport.WebSockets (WS) import Simplex.Messaging.Util (bshow, liftError, raceAny_) @@ -132,11 +132,11 @@ type Response msg = Either ProtocolClientError msg -- as 'SMPServerTransmission' includes server information. getProtocolClient :: forall msg. Protocol msg => ProtocolServer -> ProtocolClientConfig -> Maybe (TBQueue (ServerTransmission msg)) -> IO () -> IO (Either ProtocolClientError (ProtocolClient msg)) getProtocolClient protocolServer cfg@ProtocolClientConfig {qSize, tcpTimeout, tcpKeepAlive, smpPing} msgQ disconnected = - (atomically mkSMPClient >>= runClient useTransport) + (atomically mkProtocolClient >>= runClient useTransport) `catch` \(e :: IOException) -> pure . Left $ PCEIOError e where - mkSMPClient :: STM (ProtocolClient msg) - mkSMPClient = do + mkProtocolClient :: STM (ProtocolClient msg) + mkProtocolClient = do connected <- newTVar False clientCorrId <- newTVar 0 sentCommands <- TM.empty @@ -177,7 +177,7 @@ getProtocolClient protocolServer cfg@ProtocolClientConfig {qSize, tcpTimeout, tc client :: forall c. Transport c => TProxy c -> ProtocolClient msg -> TMVar (Either ProtocolClientError (THandle c)) -> c -> IO () client _ c thVar h = - runExceptT (smpClientHandshake h $ keyHash protocolServer) >>= \case + runExceptT (protocolClientHandshake @msg h $ keyHash protocolServer) >>= \case Left e -> atomically . putTMVar thVar . Left $ PCETransportError e Right th@THandle {sessionId} -> do atomically $ do diff --git a/src/Simplex/Messaging/Encoding/String.hs b/src/Simplex/Messaging/Encoding/String.hs index 9e7d32c3f..52b26ef1d 100644 --- a/src/Simplex/Messaging/Encoding/String.hs +++ b/src/Simplex/Messaging/Encoding/String.hs @@ -2,7 +2,8 @@ {-# LANGUAGE OverloadedStrings #-} module Simplex.Messaging.Encoding.String - ( StrEncoding (..), + ( TextEncoding (..), + StrEncoding (..), Str (..), strP_, strToJSON, @@ -26,11 +27,16 @@ import Data.ByteString.Char8 (ByteString) import qualified Data.ByteString.Char8 as B import Data.Char (isAlphaNum) import qualified Data.List.NonEmpty as L +import Data.Text (Text) import Data.Text.Encoding (decodeLatin1, encodeUtf8) import Data.Word (Word16) import Simplex.Messaging.Parsers (parseAll) import Simplex.Messaging.Util ((<$?>)) +class TextEncoding a where + textEncode :: a -> Text + textDecode :: Text -> Maybe a + -- | Serializing human-readable and (where possible) URI-friendly strings for SMP and SMP agent protocols class StrEncoding a where {-# MINIMAL strEncode, (strDecode | strP) #-} diff --git a/src/Simplex/Messaging/Notifications/Client.hs b/src/Simplex/Messaging/Notifications/Client.hs index 48fc89b1e..76667f9fb 100644 --- a/src/Simplex/Messaging/Notifications/Client.hs +++ b/src/Simplex/Messaging/Notifications/Client.hs @@ -1,5 +1,7 @@ {-# LANGUAGE DataKinds #-} +{-# LANGUAGE DuplicateRecordFields #-} {-# LANGUAGE LambdaCase #-} +{-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE ScopedTypeVariables #-} @@ -7,42 +9,50 @@ module Simplex.Messaging.Notifications.Client where import Control.Monad.Except import Control.Monad.Trans.Except +import qualified Data.Attoparsec.ByteString.Char8 as A import Data.Word (Word16) +import Database.SQLite.Simple.FromField (FromField (..)) +import Database.SQLite.Simple.ToField (ToField (..)) import Simplex.Messaging.Client import qualified Simplex.Messaging.Crypto as C +import Simplex.Messaging.Encoding import Simplex.Messaging.Notifications.Protocol +import Simplex.Messaging.Parsers (blobFieldDecoder) +import Simplex.Messaging.Protocol (ProtocolServer) + +type NtfServer = ProtocolServer type NtfClient = ProtocolClient NtfResponse -registerNtfToken :: NtfClient -> C.APrivateSignKey -> NewNtfEntity 'Token -> ExceptT ProtocolClientError IO (NtfTokenId, C.PublicKeyX25519) -registerNtfToken c pKey newTkn = +ntfRegisterToken :: NtfClient -> C.APrivateSignKey -> NewNtfEntity 'Token -> ExceptT ProtocolClientError IO (NtfTokenId, C.PublicKeyX25519) +ntfRegisterToken c pKey newTkn = sendNtfCommand c (Just pKey) "" (TNEW newTkn) >>= \case NRId tknId dhKey -> pure (tknId, dhKey) _ -> throwE PCEUnexpectedResponse -verifyNtfToken :: NtfClient -> C.APrivateSignKey -> NtfTokenId -> NtfRegistrationCode -> ExceptT ProtocolClientError IO () -verifyNtfToken c pKey tknId code = okNtfCommand (TVFY code) c pKey tknId +ntfVerifyToken :: NtfClient -> C.APrivateSignKey -> NtfTokenId -> NtfRegCode -> ExceptT ProtocolClientError IO () +ntfVerifyToken c pKey tknId code = okNtfCommand (TVFY code) c pKey tknId -deleteNtfToken :: NtfClient -> C.APrivateSignKey -> NtfTokenId -> ExceptT ProtocolClientError IO () -deleteNtfToken = okNtfCommand TDEL +ntfDeleteToken :: NtfClient -> C.APrivateSignKey -> NtfTokenId -> ExceptT ProtocolClientError IO () +ntfDeleteToken = okNtfCommand TDEL -enableNtfCron :: NtfClient -> C.APrivateSignKey -> NtfTokenId -> Word16 -> ExceptT ProtocolClientError IO () -enableNtfCron c pKey tknId int = okNtfCommand (TCRN int) c pKey tknId +ntfEnableCron :: NtfClient -> C.APrivateSignKey -> NtfTokenId -> Word16 -> ExceptT ProtocolClientError IO () +ntfEnableCron c pKey tknId int = okNtfCommand (TCRN int) c pKey tknId -createNtfSubsciption :: NtfClient -> C.APrivateSignKey -> NewNtfEntity 'Subscription -> ExceptT ProtocolClientError IO (NtfSubscriptionId, C.PublicKeyX25519) -createNtfSubsciption c pKey newSub = +ntfCreateSubsciption :: NtfClient -> C.APrivateSignKey -> NewNtfEntity 'Subscription -> ExceptT ProtocolClientError IO (NtfSubscriptionId, C.PublicKeyX25519) +ntfCreateSubsciption c pKey newSub = sendNtfCommand c (Just pKey) "" (SNEW newSub) >>= \case NRId tknId dhKey -> pure (tknId, dhKey) _ -> throwE PCEUnexpectedResponse -checkNtfSubscription :: NtfClient -> C.APrivateSignKey -> NtfSubscriptionId -> ExceptT ProtocolClientError IO NtfSubStatus -checkNtfSubscription c pKey subId = +ntfCheckSubscription :: NtfClient -> C.APrivateSignKey -> NtfSubscriptionId -> ExceptT ProtocolClientError IO NtfSubStatus +ntfCheckSubscription c pKey subId = sendNtfCommand c (Just pKey) subId SCHK >>= \case NRStat stat -> pure stat _ -> throwE PCEUnexpectedResponse -deleteNfgSubscription :: NtfClient -> C.APrivateSignKey -> NtfSubscriptionId -> ExceptT ProtocolClientError IO () -deleteNfgSubscription = okNtfCommand SDEL +ntfDeleteSubscription :: NtfClient -> C.APrivateSignKey -> NtfSubscriptionId -> ExceptT ProtocolClientError IO () +ntfDeleteSubscription = okNtfCommand SDEL -- | Send notification server command sendNtfCommand :: NtfEntityI e => NtfClient -> Maybe C.APrivateSignKey -> NtfEntityId -> NtfCommand e -> ExceptT ProtocolClientError IO NtfResponse @@ -53,3 +63,58 @@ okNtfCommand cmd c pKey entId = sendNtfCommand c (Just pKey) entId cmd >>= \case NROk -> return () _ -> throwE PCEUnexpectedResponse + +data NtfTknAction + = NTARegister C.APublicVerifyKey -- public key to send to the server + | NTAVerify NtfRegCode -- code to verify token + | NTACheck + | NTACron Word16 + | NTADelete + deriving (Show) + +instance Encoding NtfTknAction where + smpEncode = \case + NTARegister key -> smpEncode ('R', key) + NTAVerify code -> smpEncode ('V', code) + NTACheck -> "C" + NTACron interval -> smpEncode ('I', interval) + NTADelete -> "D" + smpP = + A.anyChar >>= \case + 'R' -> NTARegister <$> smpP + 'V' -> NTAVerify <$> smpP + 'C' -> pure NTACheck + 'I' -> NTACron <$> smpP + 'D' -> pure NTADelete + _ -> fail "bad NtfTknAction" + +instance FromField NtfTknAction where fromField = blobFieldDecoder smpDecode + +instance ToField NtfTknAction where toField = toField . smpEncode + +data NtfToken = NtfToken + { deviceToken :: DeviceToken, + ntfServer :: NtfServer, + ntfTokenId :: Maybe NtfTokenId, + -- | key used by the ntf client to sign transmissions + ntfPrivKey :: C.APrivateSignKey, + -- | shared DH secret used to encrypt/decrypt notifications e2e + ntfDhSecret :: Maybe C.DhSecretX25519, + -- | token status + ntfTknStatus :: NtfTknStatus, + -- | pending token action and the earliest time + ntfTknAction :: Maybe NtfTknAction + } + deriving (Show) + +newNtfToken :: DeviceToken -> NtfServer -> C.APrivateSignKey -> C.APublicVerifyKey -> NtfToken +newNtfToken deviceToken ntfServer ntfPrivKey ntfPubKey = + NtfToken + { deviceToken, + ntfServer, + ntfTokenId = Nothing, + ntfPrivKey, + ntfDhSecret = Nothing, + ntfTknStatus = NTNew, + ntfTknAction = Just $ NTARegister ntfPubKey + } \ No newline at end of file diff --git a/src/Simplex/Messaging/Notifications/Protocol.hs b/src/Simplex/Messaging/Notifications/Protocol.hs index 1fac10e7e..17c6ece56 100644 --- a/src/Simplex/Messaging/Notifications/Protocol.hs +++ b/src/Simplex/Messaging/Notifications/Protocol.hs @@ -10,6 +10,7 @@ module Simplex.Messaging.Notifications.Protocol where +import Data.Aeson (FromJSON (..), ToJSON (..)) import qualified Data.Attoparsec.ByteString.Char8 as A import Data.ByteString.Char8 (ByteString) import qualified Data.ByteString.Char8 as B @@ -17,8 +18,13 @@ import Data.Kind import Data.Maybe (isNothing) import Data.Type.Equality import Data.Word (Word16) +import Database.SQLite.Simple.FromField (FromField (..)) +import Database.SQLite.Simple.ToField (ToField (..)) import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Encoding +import Simplex.Messaging.Encoding.String +import Simplex.Messaging.Notifications.Transport (ntfClientHandshake) +import Simplex.Messaging.Parsers (fromTextField_) import Simplex.Messaging.Protocol hiding (Command (..), CommandTag (..)) import Simplex.Messaging.Util ((<$?>)) @@ -87,12 +93,31 @@ instance ProtocolMsgTag NtfCmdTag where instance NtfEntityI e => ProtocolMsgTag (NtfCommandTag e) where decodeTag s = decodeTag s >>= (\(NCT _ t) -> checkEntity' t) -type NtfRegistrationCode = ByteString +newtype NtfRegCode = NtfRegCode ByteString + deriving (Eq, Show) + +instance Encoding NtfRegCode where + smpEncode (NtfRegCode code) = smpEncode code + smpP = NtfRegCode <$> smpP + +instance StrEncoding NtfRegCode where + strEncode (NtfRegCode m) = strEncode m + strDecode s = NtfRegCode <$> strDecode s + strP = NtfRegCode <$> strP + +instance FromJSON NtfRegCode where + parseJSON = strParseJSON "NtfRegCode" + +instance ToJSON NtfRegCode where + toJSON = strToJSON + toEncoding = strToJEncoding data NewNtfEntity (e :: NtfEntity) where NewNtfTkn :: DeviceToken -> C.APublicVerifyKey -> C.PublicKeyX25519 -> NewNtfEntity 'Token NewNtfSub :: NtfTokenId -> SMPQueueNtf -> NewNtfEntity 'Subscription +deriving instance Show (NewNtfEntity e) + data ANewNtfEntity = forall e. NtfEntityI e => ANE (SNtfEntity e) (NewNtfEntity e) instance NtfEntityI e => Encoding (NewNtfEntity e) where @@ -111,6 +136,7 @@ instance Encoding ANewNtfEntity where instance Protocol NtfResponse where type ProtocolCommand NtfResponse = NtfCmd + protocolClientHandshake = ntfClientHandshake protocolPing = NtfCmd SSubscription PING protocolError = \case NRErr e -> Just e @@ -120,7 +146,7 @@ data NtfCommand (e :: NtfEntity) where -- | register new device token for notifications TNEW :: NewNtfEntity 'Token -> NtfCommand 'Token -- | verify token - uses e2e encrypted random string sent to the device via PN to confirm that the device has the token - TVFY :: NtfRegistrationCode -> NtfCommand 'Token + TVFY :: NtfRegCode -> NtfCommand 'Token -- | delete token - all subscriptions will be removed and no more notifications will be sent TDEL :: NtfCommand 'Token -- | enable periodic background notification to fetch the new messages - interval is in minutes, minimum is 20, 0 to disable @@ -134,8 +160,12 @@ data NtfCommand (e :: NtfEntity) where -- | keep-alive command PING :: NtfCommand 'Subscription +deriving instance Show (NtfCommand e) + data NtfCmd = forall e. NtfEntityI e => NtfCmd (SNtfEntity e) (NtfCommand e) +deriving instance Show NtfCmd + instance NtfEntityI e => ProtocolEncoding (NtfCommand e) where type Tag (NtfCommand e) = NtfCommandTag e encodeProtocol = \case @@ -263,6 +293,7 @@ data SMPQueueNtf = SMPQueueNtf notifierId :: NotifierId, notifierKey :: NtfPrivateSignKey } + deriving (Show) instance Encoding SMPQueueNtf where smpEncode SMPQueueNtf {smpServer, notifierId, notifierKey} = smpEncode (smpServer, notifierId, notifierKey) @@ -270,17 +301,30 @@ instance Encoding SMPQueueNtf where (smpServer, notifierId, notifierKey) <- smpP pure $ SMPQueueNtf smpServer notifierId notifierKey -data PushPlatform = PPApple +data PushProvider = PPApple + deriving (Eq, Ord, Show) -instance Encoding PushPlatform where +instance Encoding PushProvider where smpEncode = \case PPApple -> "A" smpP = A.anyChar >>= \case 'A' -> pure PPApple - _ -> fail "bad PushPlatform" + _ -> fail "bad PushProvider" -data DeviceToken = DeviceToken PushPlatform ByteString +instance TextEncoding PushProvider where + textEncode = \case + PPApple -> "apple" + textDecode = \case + "apple" -> Just PPApple + _ -> Nothing + +instance FromField PushProvider where fromField = fromTextField_ textDecode + +instance ToField PushProvider where toField = toField . textEncode + +data DeviceToken = DeviceToken PushProvider ByteString + deriving (Eq, Ord, Show) instance Encoding DeviceToken where smpEncode (DeviceToken p t) = smpEncode (p, t) @@ -322,17 +366,40 @@ instance Encoding NtfSubStatus where _ -> fail "bad NtfError" data NtfTknStatus - = -- | state after registration (TNEW) + = -- | Token created in DB NTNew - | -- | if initial notification or verification failed (push provider error) + | -- | state after registration (TNEW) + NTRegistered + | -- | if initial notification failed (push provider error) or verification failed NTInvalid - | -- | if initial notification succeeded + | -- | Token confirmed via notification (accepted by push provider or verification code received by client) NTConfirmed | -- | after successful verification (TVFY) NTActive | -- | after it is no longer valid (push provider error) NTExpired - deriving (Eq) + deriving (Eq, Show) + +instance TextEncoding NtfTknStatus where + textEncode = \case + NTNew -> "new" + NTRegistered -> "registered" + NTInvalid -> "invalid" + NTConfirmed -> "confirmed" + NTActive -> "active" + NTExpired -> "expired" + textDecode = \case + "new" -> Just NTNew + "registered" -> Just NTRegistered + "invalid" -> Just NTInvalid + "confirmed" -> Just NTConfirmed + "active" -> Just NTActive + "expired" -> Just NTExpired + _ -> Nothing + +instance FromField NtfTknStatus where fromField = fromTextField_ textDecode + +instance ToField NtfTknStatus where toField = toField . textEncode checkEntity :: forall t e e'. (NtfEntityI e, NtfEntityI e') => t e' -> Either String (t e) checkEntity c = case testEquality (sNtfEntity @e) (sNtfEntity @e') of diff --git a/src/Simplex/Messaging/Notifications/Server.hs b/src/Simplex/Messaging/Notifications/Server.hs index 13d4671e6..916c64111 100644 --- a/src/Simplex/Messaging/Notifications/Server.hs +++ b/src/Simplex/Messaging/Notifications/Server.hs @@ -14,8 +14,8 @@ import Control.Monad.Except import Control.Monad.IO.Unlift (MonadUnliftIO) import Control.Monad.Reader import Crypto.Random (MonadRandom) +import qualified Data.Aeson as J import Data.ByteString.Char8 (ByteString) -import Data.Functor (($>)) import Network.Socket (ServiceName) import Simplex.Messaging.Client.Agent import qualified Simplex.Messaging.Crypto as C @@ -106,7 +106,12 @@ ntfSubscriber NtfSubscriber {subQ, smpAgent = ca@SMPClientAgent {msgQ, agentQ}} ntfPush :: (MonadUnliftIO m, MonadReader NtfEnv m) => NtfPushServer -> m () ntfPush NtfPushServer {pushQ} = forever $ do atomically (readTBQueue pushQ) >>= \case - (NtfTknData {}, Notification {}) -> pure () + (NtfTknData {tknStatus}, notification) -> do + liftIO $ print $ J.encode notification + -- TODO status update should happen after the token status successfully sent + case notification of + PNVerification _ -> atomically $ writeTVar tknStatus NTConfirmed + _ -> pure () runNtfClientTransport :: (Transport c, MonadUnliftIO m, MonadReader NtfEnv m) => THandle c -> m () runNtfClientTransport th@THandle {sessionId} = do @@ -122,7 +127,7 @@ clientDisconnected NtfServerClient {connected} = atomically $ writeTVar connecte receive :: (Transport c, MonadUnliftIO m, MonadReader NtfEnv m) => THandle c -> NtfServerClient -> m () receive th NtfServerClient {rcvQ, sndQ} = forever $ do - t@(sig, signed, (corrId, subId, cmdOrError)) <- tGet th + t@(_, _, (corrId, subId, cmdOrError)) <- tGet th case cmdOrError of Left e -> write sndQ (corrId, subId, NRErr e) Right cmd -> @@ -144,7 +149,7 @@ verifyNtfTransmission :: verifyNtfTransmission (sig_, signed, (corrId, entId, _)) cmd = do case cmd of NtfCmd SToken (TNEW n@(NewNtfTkn _ k _)) -> - -- TODO check that token is not already in store + -- TODO check that token is not already in store, if it is - verify that the saved key is the same pure $ if verifyCmdSignature sig_ signed k then VRVerified (NtfReqNew corrId (ANE SToken n)) @@ -189,16 +194,21 @@ client NtfServerClient {rcvQ, sndQ} NtfSubscriber {subQ} NtfPushServer {pushQ} = (srvDhPubKey, srvDrivDhKey) <- liftIO C.generateKeyPair' let dhSecret = C.dh' dhPubKey srvDrivDhKey tknId <- getId + regCode <- getRegCode atomically $ do - tkn <- mkNtfTknData newTkn dhSecret + tkn <- mkNtfTknData newTkn dhSecret regCode addNtfToken st tknId tkn - writeTBQueue pushQ (tkn, Notification) - -- pure (corrId, sId, NRSubId pubDhKey) + writeTBQueue pushQ (tkn, PNVerification regCode) pure (corrId, "", NRId tknId srvDhPubKey) - NtfReqCmd SToken tkn (corrId, tknId, cmd) -> + NtfReqCmd SToken (NtfTkn NtfTknData {tknStatus, tknRegCode}) (corrId, tknId, cmd) -> do + status <- readTVarIO tknStatus (corrId,tknId,) <$> case cmd of TNEW newTkn -> pure NROk -- TODO when duplicate token sent - TVFY code -> pure NROk + TVFY code -- this allows repeated verification for cases when client connection dropped before server response + | (status == NTRegistered || status == NTConfirmed || status == NTActive) && tknRegCode == code -> do + atomically $ writeTVar tknStatus NTActive + pure NROk + | otherwise -> pure $ NRErr AUTH TDEL -> pure NROk TCRN int -> pure NROk NtfReqNew corrId (ANE SSubscription newSub) -> pure (corrId, "", NROk) @@ -209,8 +219,11 @@ client NtfServerClient {rcvQ, sndQ} NtfSubscriber {subQ} NtfPushServer {pushQ} = SDEL -> pure NROk PING -> pure NRPong getId :: m NtfEntityId - getId = do - n <- asks $ subIdBytes . config + getId = getRandomBytes =<< asks (subIdBytes . config) + getRegCode :: m NtfRegCode + getRegCode = NtfRegCode <$> (getRandomBytes =<< asks (regCodeBytes . config)) + getRandomBytes :: Int -> m ByteString + getRandomBytes n = do gVar <- asks idsDrg atomically (randomBytes n gVar) diff --git a/src/Simplex/Messaging/Notifications/Server/Env.hs b/src/Simplex/Messaging/Notifications/Server/Env.hs index 8b2a416a1..e76c63375 100644 --- a/src/Simplex/Messaging/Notifications/Server/Env.hs +++ b/src/Simplex/Messaging/Notifications/Server/Env.hs @@ -1,4 +1,5 @@ {-# LANGUAGE DataKinds #-} +{-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE DuplicateRecordFields #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE KindSignatures #-} @@ -8,8 +9,11 @@ module Simplex.Messaging.Notifications.Server.Env where import Control.Monad.IO.Unlift import Crypto.Random +import Data.Aeson (FromJSON, ToJSON) +import qualified Data.Aeson as J import Data.ByteString.Char8 (ByteString) import Data.X509.Validation (Fingerprint (..)) +import GHC.Generics import Network.Socket import qualified Network.TLS as T import Numeric.Natural @@ -17,6 +21,7 @@ import Simplex.Messaging.Client.Agent import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Notifications.Protocol import Simplex.Messaging.Notifications.Server.Subscriptions +import Simplex.Messaging.Parsers (dropPrefix, taggedObjectJSON) import Simplex.Messaging.Protocol (CorrId, Transmission) import Simplex.Messaging.Transport (ATransport) import Simplex.Messaging.Transport.Server (loadFingerprint, loadTLSServerParams) @@ -25,6 +30,7 @@ import UnliftIO.STM data NtfServerConfig = NtfServerConfig { transports :: [(ServiceName, ATransport)], subIdBytes :: Int, + regCodeBytes :: Int, clientQSize :: Natural, subQSize :: Natural, pushQSize :: Natural, @@ -35,7 +41,15 @@ data NtfServerConfig = NtfServerConfig certificateFile :: FilePath } -data Notification = Notification +data PushNotification = PNVerification {code :: NtfRegCode} | PNPeriodic + deriving (Show, Generic) + +instance FromJSON PushNotification where + parseJSON = J.genericParseJSON . taggedObjectJSON $ dropPrefix "PN" + +instance ToJSON PushNotification where + toJSON = J.genericToJSON . taggedObjectJSON $ dropPrefix "PN" + toEncoding = J.genericToEncoding . taggedObjectJSON $ dropPrefix "PN" data NtfEnv = NtfEnv { config :: NtfServerConfig, @@ -70,7 +84,7 @@ newNtfSubscriber qSize smpAgentCfg = do pure NtfSubscriber {smpAgent, subQ} newtype NtfPushServer = NtfPushServer - { pushQ :: TBQueue (NtfTknData, Notification) + { pushQ :: TBQueue (NtfTknData, PushNotification) } newNtfPushServer :: Natural -> STM NtfPushServer diff --git a/src/Simplex/Messaging/Notifications/Server/Subscriptions.hs b/src/Simplex/Messaging/Notifications/Server/Subscriptions.hs index e5cbd26fc..fa5790d85 100644 --- a/src/Simplex/Messaging/Notifications/Server/Subscriptions.hs +++ b/src/Simplex/Messaging/Notifications/Server/Subscriptions.hs @@ -36,15 +36,16 @@ data NtfTknData = NtfTknData { token :: DeviceToken, tknStatus :: TVar NtfTknStatus, tknVerifyKey :: C.APublicVerifyKey, - tknDhSecret :: C.DhSecretX25519 + tknDhSecret :: C.DhSecretX25519, + tknRegCode :: NtfRegCode } -mkNtfTknData :: NewNtfEntity 'Token -> C.DhSecretX25519 -> STM NtfTknData -mkNtfTknData (NewNtfTkn token tknVerifyKey _) tknDhSecret = do - tknStatus <- newTVar NTNew - pure NtfTknData {token, tknStatus, tknVerifyKey, tknDhSecret} +mkNtfTknData :: NewNtfEntity 'Token -> C.DhSecretX25519 -> NtfRegCode -> STM NtfTknData +mkNtfTknData (NewNtfTkn token tknVerifyKey _) tknDhSecret tknRegCode = do + tknStatus <- newTVar NTRegistered + pure NtfTknData {token, tknStatus, tknVerifyKey, tknDhSecret, tknRegCode} -data NtfSubscriptionsStore = NtfSubscriptionsStore +-- data NtfSubscriptionsStore = NtfSubscriptionsStore -- { subscriptions :: TMap NtfSubsciptionId NtfSubsciption, -- activeSubscriptions :: TMap (SMPServer, NotifierId) NtfSubsciptionId @@ -70,7 +71,9 @@ getNtfToken :: NtfStore -> NtfTokenId -> STM (Maybe (NtfEntityRec 'Token)) getNtfToken st tknId = NtfTkn <$$> TM.lookup tknId (tokens st) addNtfToken :: NtfStore -> NtfTokenId -> NtfTknData -> STM () -addNtfToken st tknId tkn = pure () +addNtfToken st tknId tkn@NtfTknData {token} = do + TM.insert tknId tkn (tokens st) + TM.insert token tknId (tokenIds st) -- getNtfRec :: NtfStore -> SNtfEntity e -> NtfEntityId -> STM (Maybe (NtfEntityRec e)) -- getNtfRec st ent entId = case ent of diff --git a/src/Simplex/Messaging/Parsers.hs b/src/Simplex/Messaging/Parsers.hs index ab4c1a4b5..ed0606fb2 100644 --- a/src/Simplex/Messaging/Parsers.hs +++ b/src/Simplex/Messaging/Parsers.hs @@ -13,6 +13,8 @@ import Data.ByteString.Base64 import Data.ByteString.Char8 (ByteString) import qualified Data.ByteString.Char8 as B import Data.Char (isAlphaNum, toLower) +import Data.Text (Text) +import qualified Data.Text as T import Data.Time.Clock (UTCTime) import Data.Time.ISO8601 (parseISO8601) import Data.Typeable (Typeable) @@ -81,6 +83,14 @@ blobFieldDecoder dec = \case Left e -> returnError ConversionFailed f ("couldn't parse field: " ++ e) f -> returnError ConversionFailed f "expecting SQLBlob column type" +fromTextField_ :: (Typeable a) => (Text -> Maybe a) -> Field -> Ok a +fromTextField_ fromText = \case + f@(Field (SQLText t) _) -> + case fromText t of + Just x -> Ok x + _ -> returnError ConversionFailed f ("invalid text: " <> T.unpack t) + f -> returnError ConversionFailed f "expecting SQLText column type" + fstToLower :: String -> String fstToLower "" = "" fstToLower (h : t) = toLower h : t diff --git a/src/Simplex/Messaging/Protocol.hs b/src/Simplex/Messaging/Protocol.hs index 0e94b5192..869855ee0 100644 --- a/src/Simplex/Messaging/Protocol.hs +++ b/src/Simplex/Messaging/Protocol.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE AllowAmbiguousTypes #-} {-# LANGUAGE DataKinds #-} {-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE DuplicateRecordFields #-} @@ -115,7 +116,7 @@ import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Encoding import Simplex.Messaging.Encoding.String import Simplex.Messaging.Parsers -import Simplex.Messaging.Transport (THandle (..), Transport, TransportError (..), tGetBlock, tPutBlock) +import Simplex.Messaging.Transport (THandle (..), Transport, TransportError (..), smpClientHandshake, tGetBlock, tPutBlock) import Simplex.Messaging.Util (bshow, (<$?>)) import Simplex.Messaging.Version import Test.QuickCheck (Arbitrary (..)) @@ -184,6 +185,7 @@ data RawTransmission = RawTransmission entityId :: ByteString, command :: ByteString } + deriving (Show) -- | unparsed sent SMP transmission with signature, without session ID. type SignedRawTransmission = (Maybe C.ASignature, ByteString, ByteString, ByteString) @@ -554,11 +556,13 @@ transmissionP = do class (ProtocolEncoding msg, ProtocolEncoding (ProtocolCommand msg)) => Protocol msg where type ProtocolCommand msg = cmd | cmd -> msg + protocolClientHandshake :: forall c. Transport c => c -> C.KeyHash -> ExceptT TransportError IO (THandle c) protocolPing :: ProtocolCommand msg protocolError :: msg -> Maybe ErrorType instance Protocol BrokerMsg where type ProtocolCommand BrokerMsg = Cmd + protocolClientHandshake = smpClientHandshake protocolPing = Cmd SSender PING protocolError = \case ERR e -> Just e diff --git a/tests/AgentTests/FunctionalAPITests.hs b/tests/AgentTests/FunctionalAPITests.hs index 1ebe91f24..a37bcff19 100644 --- a/tests/AgentTests/FunctionalAPITests.hs +++ b/tests/AgentTests/FunctionalAPITests.hs @@ -9,11 +9,13 @@ module AgentTests.FunctionalAPITests (functionalAPITests) where import Control.Monad.Except (ExceptT, runExceptT) import Control.Monad.IO.Unlift +import NtfClient (withNtfServer) import SMPAgentClient import SMPClient (testPort, withSmpServer, withSmpServerStoreLogOn) import Simplex.Messaging.Agent import Simplex.Messaging.Agent.Env.SQLite (AgentConfig (..)) import Simplex.Messaging.Agent.Protocol +import Simplex.Messaging.Notifications.Protocol (DeviceToken (DeviceToken), PushProvider (PPApple)) import Simplex.Messaging.Protocol (ErrorType (..), MsgBody) import Simplex.Messaging.Transport (ATransport (..)) import System.Timeout @@ -48,11 +50,14 @@ functionalAPITests t = do testAsyncServerOffline t it "should notify after HELLO timeout" $ withSmpServer t testAsyncHelloTimeout + describe "Notification server" $ do + it "should register device token" $ + withNtfServer t testNotificationToken testAgentClient :: IO () testAgentClient = do - alice <- getSMPAgentClient cfg - bob <- getSMPAgentClient cfg {dbFile = testDB2} + alice <- getSMPAgentClient cfg initAgentServers + bob <- getSMPAgentClient cfg {dbFile = testDB2} initAgentServers Right () <- runExceptT $ do (bobId, qInfo) <- createConnection alice SCMInvitation aliceId <- joinConnection bob qInfo "bob's connInfo" @@ -95,13 +100,13 @@ testAgentClient = do testAsyncInitiatingOffline :: IO () testAsyncInitiatingOffline = do - alice <- getSMPAgentClient cfg - bob <- getSMPAgentClient cfg {dbFile = testDB2} + alice <- getSMPAgentClient cfg initAgentServers + bob <- getSMPAgentClient cfg {dbFile = testDB2} initAgentServers Right () <- runExceptT $ do (bobId, cReq) <- createConnection alice SCMInvitation disconnectAgentClient alice aliceId <- joinConnection bob cReq "bob's connInfo" - alice' <- liftIO $ getSMPAgentClient cfg + alice' <- liftIO $ getSMPAgentClient cfg initAgentServers subscribeConnection alice' bobId ("", _, CONF confId "bob's connInfo") <- get alice' allowConnection alice' bobId confId "alice's connInfo" @@ -113,15 +118,15 @@ testAsyncInitiatingOffline = do testAsyncJoiningOfflineBeforeActivation :: IO () testAsyncJoiningOfflineBeforeActivation = do - alice <- getSMPAgentClient cfg - bob <- getSMPAgentClient cfg {dbFile = testDB2} + alice <- getSMPAgentClient cfg initAgentServers + bob <- getSMPAgentClient cfg {dbFile = testDB2} initAgentServers Right () <- runExceptT $ do (bobId, qInfo) <- createConnection alice SCMInvitation aliceId <- joinConnection bob qInfo "bob's connInfo" disconnectAgentClient bob ("", _, CONF confId "bob's connInfo") <- get alice allowConnection alice bobId confId "alice's connInfo" - bob' <- liftIO $ getSMPAgentClient cfg {dbFile = testDB2} + bob' <- liftIO $ getSMPAgentClient cfg {dbFile = testDB2} initAgentServers subscribeConnection bob' aliceId get alice ##> ("", bobId, CON) get bob' ##> ("", aliceId, INFO "alice's connInfo") @@ -131,18 +136,18 @@ testAsyncJoiningOfflineBeforeActivation = do testAsyncBothOffline :: IO () testAsyncBothOffline = do - alice <- getSMPAgentClient cfg - bob <- getSMPAgentClient cfg {dbFile = testDB2} + alice <- getSMPAgentClient cfg initAgentServers + bob <- getSMPAgentClient cfg {dbFile = testDB2} initAgentServers Right () <- runExceptT $ do (bobId, cReq) <- createConnection alice SCMInvitation disconnectAgentClient alice aliceId <- joinConnection bob cReq "bob's connInfo" disconnectAgentClient bob - alice' <- liftIO $ getSMPAgentClient cfg + alice' <- liftIO $ getSMPAgentClient cfg initAgentServers subscribeConnection alice' bobId ("", _, CONF confId "bob's connInfo") <- get alice' allowConnection alice' bobId confId "alice's connInfo" - bob' <- liftIO $ getSMPAgentClient cfg {dbFile = testDB2} + bob' <- liftIO $ getSMPAgentClient cfg {dbFile = testDB2} initAgentServers subscribeConnection bob' aliceId get alice' ##> ("", bobId, CON) get bob' ##> ("", aliceId, INFO "alice's connInfo") @@ -152,8 +157,8 @@ testAsyncBothOffline = do testAsyncServerOffline :: ATransport -> IO () testAsyncServerOffline t = do - alice <- getSMPAgentClient cfg - bob <- getSMPAgentClient cfg {dbFile = testDB2} + alice <- getSMPAgentClient cfg initAgentServers + bob <- getSMPAgentClient cfg {dbFile = testDB2} initAgentServers -- create connection and shutdown the server Right (bobId, cReq) <- withSmpServerStoreLogOn t testPort $ \_ -> runExceptT $ createConnection alice SCMInvitation @@ -176,8 +181,8 @@ testAsyncServerOffline t = do testAsyncHelloTimeout :: IO () testAsyncHelloTimeout = do - alice <- getSMPAgentClient cfg - bob <- getSMPAgentClient cfg {dbFile = testDB2, helloTimeout = 1} + alice <- getSMPAgentClient cfg initAgentServers + bob <- getSMPAgentClient cfg {dbFile = testDB2, helloTimeout = 1} initAgentServers Right () <- runExceptT $ do (_, cReq) <- createConnection alice SCMInvitation disconnectAgentClient alice @@ -185,6 +190,13 @@ testAsyncHelloTimeout = do get bob ##> ("", aliceId, ERR $ CONN NOT_ACCEPTED) pure () +testNotificationToken :: IO () +testNotificationToken = do + alice <- getSMPAgentClient cfg initAgentServers + Right () <- runExceptT $ do + registerNtfToken alice $ DeviceToken PPApple "abcd" + pure () + exchangeGreetings :: AgentClient -> ConnId -> AgentClient -> ConnId -> ExceptT AgentErrorType IO () exchangeGreetings alice bobId bob aliceId = do 5 <- sendMessage alice bobId "hello" diff --git a/tests/NtfClient.hs b/tests/NtfClient.hs new file mode 100644 index 000000000..bb7f6dec6 --- /dev/null +++ b/tests/NtfClient.hs @@ -0,0 +1,106 @@ +{-# LANGUAGE DataKinds #-} +{-# LANGUAGE DuplicateRecordFields #-} +{-# LANGUAGE FlexibleContexts #-} +{-# LANGUAGE LambdaCase #-} +{-# LANGUAGE NumericUnderscores #-} +{-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE ScopedTypeVariables #-} +{-# LANGUAGE TypeApplications #-} + +module NtfClient where + +import Control.Monad.Except (runExceptT) +import Control.Monad.IO.Unlift +import Crypto.Random +import Data.ByteString.Char8 (ByteString) +import Network.Socket +import Simplex.Messaging.Client.Agent (defaultSMPClientAgentConfig) +import qualified Simplex.Messaging.Crypto as C +import Simplex.Messaging.Encoding +import Simplex.Messaging.Notifications.Server (runNtfServerBlocking) +import Simplex.Messaging.Notifications.Server.Env +import Simplex.Messaging.Notifications.Transport +import Simplex.Messaging.Protocol +import Simplex.Messaging.Transport +import Simplex.Messaging.Transport.Client +import Simplex.Messaging.Transport.KeepAlive +import UnliftIO.Concurrent +import qualified UnliftIO.Exception as E +import UnliftIO.STM (TMVar, atomically, newEmptyTMVarIO, takeTMVar) +import UnliftIO.Timeout (timeout) + +testHost :: HostName +testHost = "localhost" + +testPort :: ServiceName +testPort = "6001" + +testKeyHash :: C.KeyHash +testKeyHash = "LcJUMfVhwD8yxjAiSaDzzGF3-kLG4Uh0Fl_ZIjrRwjI=" + +testNtfClient :: (Transport c, MonadUnliftIO m) => (THandle c -> m a) -> m a +testNtfClient client = + runTransportClient testHost testPort testKeyHash (Just defaultKeepAliveOpts) $ \h -> + liftIO (runExceptT $ ntfClientHandshake h testKeyHash) >>= \case + Right th -> client th + Left e -> error $ show e + +cfg :: NtfServerConfig +cfg = + NtfServerConfig + { transports = undefined, + subIdBytes = 24, + regCodeBytes = 32, + clientQSize = 1, + subQSize = 1, + pushQSize = 1, + smpAgentCfg = defaultSMPClientAgentConfig, + -- CA certificate private key is not needed for initialization + caCertificateFile = "tests/fixtures/ca.crt", + privateKeyFile = "tests/fixtures/server.key", + certificateFile = "tests/fixtures/server.crt" + } + +withNtfServerThreadOn :: (MonadUnliftIO m, MonadRandom m) => ATransport -> ServiceName -> (ThreadId -> m a) -> m a +withNtfServerThreadOn t port' = + serverBracket + (\started -> runNtfServerBlocking started cfg {transports = [(port', t)]}) + (pure ()) + +serverBracket :: MonadUnliftIO m => (TMVar Bool -> m ()) -> m () -> (ThreadId -> m a) -> m a +serverBracket process afterProcess f = do + started <- newEmptyTMVarIO + E.bracket + (forkIOWithUnmask ($ process started)) + (\t -> killThread t >> afterProcess >> waitFor started "stop") + (\t -> waitFor started "start" >> f t) + where + waitFor started s = + 5_000_000 `timeout` atomically (takeTMVar started) >>= \case + Nothing -> error $ "server did not " <> s + _ -> pure () + +withNtfServerOn :: (MonadUnliftIO m, MonadRandom m) => ATransport -> ServiceName -> m a -> m a +withNtfServerOn t port' = withNtfServerThreadOn t port' . const + +withNtfServer :: (MonadUnliftIO m, MonadRandom m) => ATransport -> m a -> m a +withNtfServer t = withNtfServerOn t testPort + +runNtfTest :: forall c m a. (Transport c, MonadUnliftIO m, MonadRandom m) => (THandle c -> m a) -> m a +runNtfTest test = withNtfServer (transport @c) $ testNtfClient test + +ntfServerTest :: + forall c smp. + (Transport c, Encoding smp) => + TProxy c -> + (Maybe C.ASignature, ByteString, ByteString, smp) -> + IO (Maybe C.ASignature, ByteString, ByteString, BrokerMsg) +ntfServerTest _ t = runNtfTest $ \h -> tPut' h t >> tGet' h + where + tPut' h (sig, corrId, queueId, smp) = do + let t' = smpEncode (sessionId (h :: THandle c), corrId, queueId, smp) + Right () <- tPut h (sig, t') + pure () + tGet' h = do + (Nothing, _, (CorrId corrId, qId, Right cmd)) <- tGet h + pure (Nothing, corrId, qId, cmd) \ No newline at end of file diff --git a/tests/NtfServerTests.hs b/tests/NtfServerTests.hs new file mode 100644 index 000000000..93195e8ac --- /dev/null +++ b/tests/NtfServerTests.hs @@ -0,0 +1,40 @@ +{-# LANGUAGE DataKinds #-} +{-# LANGUAGE GADTs #-} +{-# LANGUAGE LambdaCase #-} +{-# LANGUAGE NamedFieldPuns #-} +{-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE PatternSynonyms #-} +{-# LANGUAGE ScopedTypeVariables #-} +{-# LANGUAGE TypeApplications #-} + +module NtfServerTests where + +import Data.ByteString.Char8 (ByteString) +import NtfClient +import ServerTests (sampleDhPubKey, samplePubKey, sampleSig) +import qualified Simplex.Messaging.Crypto as C +import Simplex.Messaging.Encoding +import Simplex.Messaging.Notifications.Protocol +import Simplex.Messaging.Protocol +import Simplex.Messaging.Transport +import Test.Hspec + +ntfServerTests :: ATransport -> Spec +ntfServerTests t = do + describe "notifications server protocol syntax" $ ntfSyntaxTests t + +ntfSyntaxTests :: ATransport -> Spec +ntfSyntaxTests (ATransport t) = do + it "unknown command" $ ("", "abcd", "1234", ('H', 'E', 'L', 'L', 'O')) >#> ("", "abcd", "1234", ERR $ CMD UNKNOWN) + describe "NEW" $ do + it "no parameters" $ (sampleSig, "bcda", "", TNEW_) >#> ("", "bcda", "", ERR $ CMD SYNTAX) + it "many parameters" $ (sampleSig, "cdab", "", (TNEW_, (' ', '\x01', 'A'), ('T', 'A', "abcd" :: ByteString), samplePubKey, sampleDhPubKey)) >#> ("", "cdab", "", ERR $ CMD SYNTAX) + it "no signature" $ ("", "dabc", "", (TNEW_, ' ', ('T', 'A', "abcd" :: ByteString), samplePubKey, sampleDhPubKey)) >#> ("", "dabc", "", ERR $ CMD NO_AUTH) + it "token ID" $ (sampleSig, "abcd", "12345678", (TNEW_, ' ', ('T', 'A', "abcd" :: ByteString), samplePubKey, sampleDhPubKey)) >#> ("", "abcd", "12345678", ERR $ CMD HAS_AUTH) + where + (>#>) :: + Encoding smp => + (Maybe C.ASignature, ByteString, ByteString, smp) -> + (Maybe C.ASignature, ByteString, ByteString, BrokerMsg) -> + Expectation + command >#> response = ntfServerTest t command `shouldReturn` response \ No newline at end of file diff --git a/tests/SMPAgentClient.hs b/tests/SMPAgentClient.hs index e232ffdbd..304ba9674 100644 --- a/tests/SMPAgentClient.hs +++ b/tests/SMPAgentClient.hs @@ -154,11 +154,17 @@ smpAgentTest1_1_1 test' = _test [h] = test' h _test _ = error "expected 1 handle" +initAgentServers :: InitialAgentServers +initAgentServers = + InitialAgentServers + { smp = L.fromList ["smp://LcJUMfVhwD8yxjAiSaDzzGF3-kLG4Uh0Fl_ZIjrRwjI=@localhost:5001"], + ntf = ["smp://LcJUMfVhwD8yxjAiSaDzzGF3-kLG4Uh0Fl_ZIjrRwjI=@localhost:6001"] + } + cfg :: AgentConfig cfg = defaultAgentConfig { tcpPort = agentTestPort, - initialSMPServers = L.fromList ["smp://LcJUMfVhwD8yxjAiSaDzzGF3-kLG4Uh0Fl_ZIjrRwjI=@localhost:5001"], tbqSize = 1, dbFile = testDB, smpCfg = @@ -167,7 +173,7 @@ cfg = defaultTransport = (testPort, transport @TLS), tcpTimeout = 500_000 }, - reconnectInterval = (reconnectInterval defaultAgentConfig) {initialInterval = 50_000}, + reconnectInterval = defaultReconnectInterval {initialInterval = 50_000}, caCertificateFile = "tests/fixtures/ca.crt", privateKeyFile = "tests/fixtures/server.key", certificateFile = "tests/fixtures/server.crt" @@ -175,9 +181,10 @@ cfg = withSmpAgentThreadOn_ :: (MonadUnliftIO m, MonadRandom m) => ATransport -> (ServiceName, ServiceName, String) -> m () -> (ThreadId -> m a) -> m a withSmpAgentThreadOn_ t (port', smpPort', db') afterProcess = - let cfg' = cfg {tcpPort = port', dbFile = db', initialSMPServers = L.fromList [SMPServer "localhost" smpPort' testKeyHash]} + let cfg' = cfg {tcpPort = port', dbFile = db'} + initServers' = initAgentServers {smp = L.fromList [SMPServer "localhost" smpPort' testKeyHash]} in serverBracket - (\started -> runSMPAgentBlocking t started cfg') + (\started -> runSMPAgentBlocking t started cfg' initServers') afterProcess withSmpAgentThreadOn :: (MonadUnliftIO m, MonadRandom m) => ATransport -> (ServiceName, ServiceName, String) -> (ThreadId -> m a) -> m a diff --git a/tests/Test.hs b/tests/Test.hs index 03eab361d..bdd67c0dc 100644 --- a/tests/Test.hs +++ b/tests/Test.hs @@ -4,6 +4,7 @@ import AgentTests (agentTests) import CoreTests.EncodingTests import CoreTests.ProtocolErrorTests import CoreTests.VersionRangeTests +import NtfServerTests (ntfServerTests) import ServerTests import Simplex.Messaging.Transport (TLS, Transport (..)) import Simplex.Messaging.Transport.WebSockets (WS) @@ -20,5 +21,6 @@ main = do describe "Version range" versionRangeTests describe "SMP server via TLS" $ serverTests (transport @TLS) describe "SMP server via WebSockets" $ serverTests (transport @WS) + describe "Ntf server via TLS" $ ntfServerTests (transport @TLS) describe "SMP client agent" $ agentTests (transport @TLS) removeDirectoryRecursive "tests/tmp" From 45ddecc4b8d7592eb79650288401e163536b1c2f Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Thu, 14 Apr 2022 11:33:17 +0100 Subject: [PATCH 006/131] APNS/HTTP2 push client for notifications server (#350) * APN push client (WIP) * APNS push client works * TODO comments * comment * send notification and process the response * config * e2e encryption for notification parameter * read apns key filename and ID from env * connect APNS client to server, fix notification JSON encoding to use dash case * do not connect to APNS on start to pass CI tests * fix CI test * remove redundant import --- apps/ntf-server/Main.hs | 2 + package.yaml | 3 + simplexmq.cabal | 250 +++++++------ src/Simplex/Messaging/Agent/Store.hs | 4 +- src/Simplex/Messaging/Client.hs | 2 +- src/Simplex/Messaging/Crypto.hs | 17 +- src/Simplex/Messaging/Notifications/Server.hs | 61 +-- .../Messaging/Notifications/Server/Env.hs | 50 +-- .../Messaging/Notifications/Server/Push.hs | 11 - .../Notifications/Server/Push/.gitignore | 1 + .../Notifications/Server/Push/APNS.hs | 348 ++++++++++++++++++ .../Notifications/Server/Push/testpush.sh | 20 + .../Notifications/Server/Subscriptions.hs | 8 - src/Simplex/Messaging/Server.hs | 11 +- src/Simplex/Messaging/Transport/Client.hs | 23 +- .../Messaging/Transport/Client/HTTP2.hs | 169 +++++++++ src/Simplex/Messaging/Transport/KeepAlive.hs | 1 + tests/NtfClient.hs | 4 +- tests/NtfServerTests.hs | 6 +- tests/SMPAgentClient.hs | 2 +- tests/SMPClient.hs | 2 +- 21 files changed, 791 insertions(+), 204 deletions(-) delete mode 100644 src/Simplex/Messaging/Notifications/Server/Push.hs create mode 100644 src/Simplex/Messaging/Notifications/Server/Push/.gitignore create mode 100644 src/Simplex/Messaging/Notifications/Server/Push/APNS.hs create mode 100755 src/Simplex/Messaging/Notifications/Server/Push/testpush.sh create mode 100644 src/Simplex/Messaging/Transport/Client/HTTP2.hs diff --git a/apps/ntf-server/Main.hs b/apps/ntf-server/Main.hs index 928b9e514..478077e25 100644 --- a/apps/ntf-server/Main.hs +++ b/apps/ntf-server/Main.hs @@ -5,6 +5,7 @@ module Main where import Simplex.Messaging.Client.Agent (defaultSMPClientAgentConfig) import Simplex.Messaging.Notifications.Server (runNtfServer) import Simplex.Messaging.Notifications.Server.Env (NtfServerConfig (..)) +import Simplex.Messaging.Notifications.Server.Push.APNS (defaultAPNSPushClientConfig) import Simplex.Messaging.Server.CLI (ServerCLIConfig (..), protocolServerCLI) import System.FilePath (combine) @@ -44,6 +45,7 @@ ntfServerCLIConfig = subQSize = 64, pushQSize = 128, smpAgentCfg = defaultSMPClientAgentConfig, + apnsConfig = defaultAPNSPushClientConfig, caCertificateFile = caCrtFile, privateKeyFile = serverKeyFile, certificateFile = serverCrtFile diff --git a/package.yaml b/package.yaml index 7b804a55f..61e000158 100644 --- a/package.yaml +++ b/package.yaml @@ -31,6 +31,7 @@ dependencies: - base >= 4.7 && < 5 - base64-bytestring >= 1.0 && < 1.3 - bytestring == 0.10.* + - case-insensitive == 1.2.* - composition == 1.0.* - constraints >= 0.12 && < 0.14 - containers == 0.6.* @@ -41,6 +42,7 @@ dependencies: - directory == 1.3.* - filepath == 1.4.* - http-types == 0.12.* + - http2 == 3.0.* - generic-random >= 1.3 && < 1.5 - ini == 0.4.* - iso8601-time == 0.1.* @@ -58,6 +60,7 @@ dependencies: - template-haskell == 2.16.* - text == 1.2.* - time == 1.9.* + - time-manager == 0.0.* - tls >= 1.5.7 && < 1.6 - transformers == 0.5.* - unliftio == 0.2.* diff --git a/simplexmq.cabal b/simplexmq.cabal index 61c2001ad..6a64f8214 100644 --- a/simplexmq.cabal +++ b/simplexmq.cabal @@ -47,6 +47,7 @@ library Simplex.Messaging.Agent.Store.SQLite.Migrations.M20220101_initial Simplex.Messaging.Agent.Store.SQLite.Migrations.M20220301_snd_queue_keys Simplex.Messaging.Agent.Store.SQLite.Migrations.M20220322_notifications + Simplex.Messaging.Agent.Store.SQLite.Migrations.M20220404_ntf_subscriptions_draft Simplex.Messaging.Client Simplex.Messaging.Client.Agent Simplex.Messaging.Crypto @@ -57,6 +58,7 @@ library Simplex.Messaging.Notifications.Protocol Simplex.Messaging.Notifications.Server Simplex.Messaging.Notifications.Server.Env + Simplex.Messaging.Notifications.Server.Push.APNS Simplex.Messaging.Notifications.Server.Subscriptions Simplex.Messaging.Notifications.Transport Simplex.Messaging.Parsers @@ -72,6 +74,7 @@ library Simplex.Messaging.TMap Simplex.Messaging.Transport Simplex.Messaging.Transport.Client + Simplex.Messaging.Transport.Client.HTTP2 Simplex.Messaging.Transport.KeepAlive Simplex.Messaging.Transport.Server Simplex.Messaging.Transport.WebSockets @@ -93,6 +96,7 @@ library , base >=4.7 && <5 , base64-bytestring >=1.0 && <1.3 , bytestring ==0.10.* + , case-insensitive ==1.2.* , composition ==1.0.* , constraints >=0.12 && <0.14 , containers ==0.6.* @@ -104,6 +108,7 @@ library , filepath ==1.4.* , generic-random >=1.3 && <1.5 , http-types ==0.12.* + , http2 ==3.0.* , ini ==0.4.* , iso8601-time ==0.1.* , memory ==0.15.* @@ -119,117 +124,7 @@ library , template-haskell ==2.16.* , text ==1.2.* , time ==1.9.* - , tls >=1.5.7 && <1.6 - , transformers ==0.5.* - , unliftio ==0.2.* - , unliftio-core ==0.2.* - , websockets ==0.12.* - , x509 ==1.7.* - , x509-store ==1.6.* - , x509-validation ==1.6.* - if flag(swift) - ghc-options: -DswiftJSON - default-language: Haskell2010 - -executable smp-agent - main-is: Main.hs - other-modules: - Paths_simplexmq - hs-source-dirs: - apps/smp-agent - ghc-options: -Wall -Wcompat -Werror=incomplete-patterns -Wredundant-constraints -Wincomplete-record-updates -Wincomplete-uni-patterns -Wunused-type-patterns -threaded - build-depends: - QuickCheck ==2.14.* - , aeson ==2.0.* - , ansi-terminal >=0.10 && <0.12 - , asn1-encoding ==0.9.* - , asn1-types ==0.3.* - , async ==2.2.* - , attoparsec ==0.14.* - , base >=4.7 && <5 - , base64-bytestring >=1.0 && <1.3 - , bytestring ==0.10.* - , composition ==1.0.* - , constraints >=0.12 && <0.14 - , containers ==0.6.* - , cryptonite >=0.27 && <0.30 - , cryptostore ==0.2.* - , data-default ==0.7.* - , direct-sqlite ==2.3.* - , directory ==1.3.* - , filepath ==1.4.* - , generic-random >=1.3 && <1.5 - , http-types ==0.12.* - , iso8601-time ==0.1.* - , memory ==0.15.* - , mtl ==2.2.* - , network ==3.1.2.* - , network-transport ==0.5.* - , random >=1.1 && <1.3 - , simple-logger ==0.1.* - , simplexmq - , sqlite-simple ==0.4.* - , stm ==2.5.* - , template-haskell ==2.16.* - , text ==1.2.* - , time ==1.9.* - , tls >=1.5.7 && <1.6 - , transformers ==0.5.* - , unliftio ==0.2.* - , unliftio-core ==0.2.* - , websockets ==0.12.* - , x509 ==1.7.* - , x509-store ==1.6.* - , x509-validation ==1.6.* - if flag(swift) - ghc-options: -DswiftJSON - default-language: Haskell2010 - -executable smp-server - main-is: Main.hs - other-modules: - Paths_simplexmq - hs-source-dirs: - apps/smp-server - ghc-options: -Wall -Wcompat -Werror=incomplete-patterns -Wredundant-constraints -Wincomplete-record-updates -Wincomplete-uni-patterns -Wunused-type-patterns -threaded - build-depends: - QuickCheck ==2.14.* - , aeson ==2.0.* - , ansi-terminal >=0.10 && <0.12 - , asn1-encoding ==0.9.* - , asn1-types ==0.3.* - , async ==2.2.* - , attoparsec ==0.14.* - , base >=4.7 && <5 - , base64-bytestring >=1.0 && <1.3 - , bytestring ==0.10.* - , composition ==1.0.* - , constraints >=0.12 && <0.14 - , containers ==0.6.* - , cryptonite >=0.27 && <0.30 - , cryptostore ==0.2.* - , data-default ==0.7.* - , direct-sqlite ==2.3.* - , directory ==1.3.* - , filepath ==1.4.* - , generic-random >=1.3 && <1.5 - , http-types ==0.12.* - , ini ==0.4.* - , iso8601-time ==0.1.* - , memory ==0.15.* - , mtl ==2.2.* - , network ==3.1.2.* - , network-transport ==0.5.* - , optparse-applicative >=0.15 && <0.17 - , process ==1.6.* - , random >=1.1 && <1.3 - , simple-logger ==0.1.* - , simplexmq - , sqlite-simple ==0.4.* - , stm ==2.5.* - , template-haskell ==2.16.* - , text ==1.2.* - , time ==1.9.* + , time-manager ==0.0.* , tls >=1.5.7 && <1.6 , transformers ==0.5.* , unliftio ==0.2.* @@ -260,6 +155,7 @@ executable ntf-server , base >=4.7 && <5 , base64-bytestring >=1.0 && <1.3 , bytestring ==0.10.* + , case-insensitive ==1.2.* , composition ==1.0.* , constraints >=0.12 && <0.14 , containers ==0.6.* @@ -271,6 +167,7 @@ executable ntf-server , filepath ==1.4.* , generic-random >=1.3 && <1.5 , http-types ==0.12.* + , http2 ==3.0.* , ini ==0.4.* , iso8601-time ==0.1.* , memory ==0.15.* @@ -287,6 +184,7 @@ executable ntf-server , template-haskell ==2.16.* , text ==1.2.* , time ==1.9.* + , time-manager ==0.0.* , tls >=1.5.7 && <1.6 , transformers ==0.5.* , unliftio ==0.2.* @@ -295,6 +193,128 @@ executable ntf-server , x509 ==1.7.* , x509-store ==1.6.* , x509-validation ==1.6.* + if flag(swift) + ghc-options: -DswiftJSON + default-language: Haskell2010 + +executable smp-agent + main-is: Main.hs + other-modules: + Paths_simplexmq + hs-source-dirs: + apps/smp-agent + ghc-options: -Wall -Wcompat -Werror=incomplete-patterns -Wredundant-constraints -Wincomplete-record-updates -Wincomplete-uni-patterns -Wunused-type-patterns -threaded + build-depends: + QuickCheck ==2.14.* + , aeson ==2.0.* + , ansi-terminal >=0.10 && <0.12 + , asn1-encoding ==0.9.* + , asn1-types ==0.3.* + , async ==2.2.* + , attoparsec ==0.14.* + , base >=4.7 && <5 + , base64-bytestring >=1.0 && <1.3 + , bytestring ==0.10.* + , case-insensitive ==1.2.* + , composition ==1.0.* + , constraints >=0.12 && <0.14 + , containers ==0.6.* + , cryptonite >=0.27 && <0.30 + , cryptostore ==0.2.* + , data-default ==0.7.* + , direct-sqlite ==2.3.* + , directory ==1.3.* + , filepath ==1.4.* + , generic-random >=1.3 && <1.5 + , http-types ==0.12.* + , http2 ==3.0.* + , ini ==0.4.* + , iso8601-time ==0.1.* + , memory ==0.15.* + , mtl ==2.2.* + , network ==3.1.2.* + , network-transport ==0.5.* + , optparse-applicative >=0.15 && <0.17 + , process ==1.6.* + , random >=1.1 && <1.3 + , simple-logger ==0.1.* + , simplexmq + , sqlite-simple ==0.4.* + , stm ==2.5.* + , template-haskell ==2.16.* + , text ==1.2.* + , time ==1.9.* + , time-manager ==0.0.* + , tls >=1.5.7 && <1.6 + , transformers ==0.5.* + , unliftio ==0.2.* + , unliftio-core ==0.2.* + , websockets ==0.12.* + , x509 ==1.7.* + , x509-store ==1.6.* + , x509-validation ==1.6.* + if flag(swift) + ghc-options: -DswiftJSON + default-language: Haskell2010 + +executable smp-server + main-is: Main.hs + other-modules: + Paths_simplexmq + hs-source-dirs: + apps/smp-server + ghc-options: -Wall -Wcompat -Werror=incomplete-patterns -Wredundant-constraints -Wincomplete-record-updates -Wincomplete-uni-patterns -Wunused-type-patterns -threaded + build-depends: + QuickCheck ==2.14.* + , aeson ==2.0.* + , ansi-terminal >=0.10 && <0.12 + , asn1-encoding ==0.9.* + , asn1-types ==0.3.* + , async ==2.2.* + , attoparsec ==0.14.* + , base >=4.7 && <5 + , base64-bytestring >=1.0 && <1.3 + , bytestring ==0.10.* + , case-insensitive ==1.2.* + , composition ==1.0.* + , constraints >=0.12 && <0.14 + , containers ==0.6.* + , cryptonite >=0.27 && <0.30 + , cryptostore ==0.2.* + , data-default ==0.7.* + , direct-sqlite ==2.3.* + , directory ==1.3.* + , filepath ==1.4.* + , generic-random >=1.3 && <1.5 + , http-types ==0.12.* + , http2 ==3.0.* + , ini ==0.4.* + , iso8601-time ==0.1.* + , memory ==0.15.* + , mtl ==2.2.* + , network ==3.1.2.* + , network-transport ==0.5.* + , optparse-applicative >=0.15 && <0.17 + , process ==1.6.* + , random >=1.1 && <1.3 + , simple-logger ==0.1.* + , simplexmq + , sqlite-simple ==0.4.* + , stm ==2.5.* + , template-haskell ==2.16.* + , text ==1.2.* + , time ==1.9.* + , time-manager ==0.0.* + , tls >=1.5.7 && <1.6 + , transformers ==0.5.* + , unliftio ==0.2.* + , unliftio-core ==0.2.* + , websockets ==0.12.* + , x509 ==1.7.* + , x509-store ==1.6.* + , x509-validation ==1.6.* + if flag(swift) + ghc-options: -DswiftJSON default-language: Haskell2010 test-suite smp-server-test @@ -309,7 +329,7 @@ test-suite smp-server-test CoreTests.EncodingTests CoreTests.ProtocolErrorTests CoreTests.VersionRangeTests - NtfClient, + NtfClient NtfServerTests ServerTests SMPAgentClient @@ -330,6 +350,7 @@ test-suite smp-server-test , base >=4.7 && <5 , base64-bytestring >=1.0 && <1.3 , bytestring ==0.10.* + , case-insensitive ==1.2.* , composition ==1.0.* , constraints >=0.12 && <0.14 , containers ==0.6.* @@ -343,11 +364,15 @@ test-suite smp-server-test , hspec ==2.7.* , hspec-core ==2.7.* , http-types ==0.12.* + , http2 ==3.0.* + , ini ==0.4.* , iso8601-time ==0.1.* , memory ==0.15.* , mtl ==2.2.* , network ==3.1.2.* , network-transport ==0.5.* + , optparse-applicative >=0.15 && <0.17 + , process ==1.6.* , random >=1.1 && <1.3 , simple-logger ==0.1.* , simplexmq @@ -356,6 +381,7 @@ test-suite smp-server-test , template-haskell ==2.16.* , text ==1.2.* , time ==1.9.* + , time-manager ==0.0.* , timeit ==2.0.* , tls >=1.5.7 && <1.6 , transformers ==0.5.* diff --git a/src/Simplex/Messaging/Agent/Store.hs b/src/Simplex/Messaging/Agent/Store.hs index efcd87d3c..5a2c246ac 100644 --- a/src/Simplex/Messaging/Agent/Store.hs +++ b/src/Simplex/Messaging/Agent/Store.hs @@ -79,7 +79,9 @@ class Monad m => MonadAgentStore s m where -- Notification device token persistence createNtfToken :: s -> NtfToken -> m () - getDeviceNtfToken :: s -> DeviceToken -> m (Maybe NtfToken) -- return current token if it exists and mark any old tokens for deletion + + -- TODO this should also return old tokens so that they are deleted from the server + getDeviceNtfToken :: s -> DeviceToken -> m (Maybe NtfToken) -- return current token if it exists updateNtfTokenRegistration :: s -> NtfToken -> NtfTokenId -> C.DhSecretX25519 -> m () updateNtfToken :: s -> NtfToken -> NtfTknStatus -> Maybe NtfTknAction -> m () diff --git a/src/Simplex/Messaging/Client.hs b/src/Simplex/Messaging/Client.hs index 8ff256e6d..dbe47d5f6 100644 --- a/src/Simplex/Messaging/Client.hs +++ b/src/Simplex/Messaging/Client.hs @@ -161,7 +161,7 @@ getProtocolClient protocolServer cfg@ProtocolClientConfig {qSize, tcpTimeout, tc thVar <- newEmptyTMVarIO action <- async $ - runTransportClient (host protocolServer) port' (keyHash protocolServer) tcpKeepAlive (client t c thVar) + runTransportClient (host protocolServer) port' (Just $ keyHash protocolServer) tcpKeepAlive (client t c thVar) `finally` atomically (putTMVar thVar $ Left PCENetworkError) th_ <- tcpTimeout `timeout` atomically (takeTMVar thVar) pure $ case th_ of diff --git a/src/Simplex/Messaging/Crypto.hs b/src/Simplex/Messaging/Crypto.hs index 92ff4f75e..9f275e805 100644 --- a/src/Simplex/Messaging/Crypto.hs +++ b/src/Simplex/Messaging/Crypto.hs @@ -103,6 +103,10 @@ module Simplex.Messaging.Crypto cbDecrypt, cbNonce, randomCbNonce, + pseudoRandomCbNonce, + + -- * pseudo-random bytes + pseudoRandomBytes, -- * SHA256 hash sha256Hash, @@ -116,6 +120,7 @@ module Simplex.Messaging.Crypto ) where +import Control.Concurrent.STM import Control.Exception (Exception) import Control.Monad.Except import Control.Monad.Trans.Except @@ -129,7 +134,7 @@ import qualified Crypto.PubKey.Curve25519 as X25519 import qualified Crypto.PubKey.Curve448 as X448 import qualified Crypto.PubKey.Ed25519 as Ed25519 import qualified Crypto.PubKey.Ed448 as Ed448 -import Crypto.Random (getRandomBytes) +import Crypto.Random (ChaChaDRG, getRandomBytes, randomBytesGenerate) import Data.ASN1.BinaryEncoding import Data.ASN1.Encoding import Data.ASN1.Types @@ -876,6 +881,16 @@ cbNonce s randomCbNonce :: IO CbNonce randomCbNonce = CbNonce <$> getRandomBytes 24 +pseudoRandomCbNonce :: TVar ChaChaDRG -> STM CbNonce +pseudoRandomCbNonce gVar = CbNonce <$> pseudoRandomBytes 24 gVar + +pseudoRandomBytes :: Int -> TVar ChaChaDRG -> STM ByteString +pseudoRandomBytes n gVar = do + g <- readTVar gVar + let (bytes, g') = randomBytesGenerate n g + writeTVar gVar g' + return bytes + instance Encoding CbNonce where smpEncode = unCbNonce smpP = CbNonce <$> A.take 24 diff --git a/src/Simplex/Messaging/Notifications/Server.hs b/src/Simplex/Messaging/Notifications/Server.hs index 64312e717..942b2db42 100644 --- a/src/Simplex/Messaging/Notifications/Server.hs +++ b/src/Simplex/Messaging/Notifications/Server.hs @@ -14,13 +14,13 @@ import Control.Monad.Except import Control.Monad.IO.Unlift (MonadUnliftIO) import Control.Monad.Reader import Crypto.Random (MonadRandom) -import qualified Data.Aeson as J import Data.ByteString.Char8 (ByteString) import Network.Socket (ServiceName) import Simplex.Messaging.Client.Agent import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Notifications.Protocol import Simplex.Messaging.Notifications.Server.Env +import Simplex.Messaging.Notifications.Server.Push.APNS import Simplex.Messaging.Notifications.Server.Subscriptions import Simplex.Messaging.Notifications.Transport import Simplex.Messaging.Protocol (ErrorType (..), SignedTransmission, Transmission, encodeTransmission, tGet, tPut) @@ -60,7 +60,7 @@ runNtfServerBlocking started cfg@NtfServerConfig {transports} = do Right th -> runNtfClientTransport th Left _ -> pure () -ntfSubscriber :: forall m. (MonadUnliftIO m, MonadReader NtfEnv m) => NtfSubscriber -> m () +ntfSubscriber :: forall m. MonadUnliftIO m => NtfSubscriber -> m () ntfSubscriber NtfSubscriber {subQ, smpAgent = ca@SMPClientAgent {msgQ, agentQ}} = do raceAny_ [subscribe, receiveSMP, receiveAgent] where @@ -71,11 +71,11 @@ ntfSubscriber NtfSubscriber {subQ, smpAgent = ca@SMPClientAgent {msgQ, agentQ}} let SMPQueueNtf {smpServer, notifierId, notifierKey} = smpQueue liftIO (runExceptT $ subscribeQueue ca smpServer ((SPNotifier, notifierId), notifierKey)) >>= \case Right _ -> pure () -- update subscription status - Left e -> pure () + Left _e -> pure () receiveSMP :: m () receiveSMP = forever $ do - (srv, sessId, ntfId, msg) <- atomically $ readTBQueue msgQ + (_srv, _sessId, _ntfId, msg) <- atomically $ readTBQueue msgQ case msg of SMP.NMSG -> do -- check when the last NMSG was received from this queue @@ -92,26 +92,37 @@ ntfSubscriber NtfSubscriber {subQ, smpAgent = ca@SMPClientAgent {msgQ, agentQ}} forever $ atomically (readTBQueue agentQ) >>= \case CAConnected _ -> pure () - CADisconnected srv subs -> do + CADisconnected _srv _subs -> do -- update subscription statuses pure () CAReconnected _ -> pure () - CAResubscribed srv sub -> do + CAResubscribed _srv _sub -> do -- update subscription status pure () - CASubError srv sub err -> do + CASubError _srv _sub _err -> do -- update subscription status pure () -ntfPush :: (MonadUnliftIO m, MonadReader NtfEnv m) => NtfPushServer -> m () -ntfPush NtfPushServer {pushQ} = forever $ do - atomically (readTBQueue pushQ) >>= \case - (NtfTknData {tknStatus}, notification) -> do - liftIO $ print $ J.encode notification - -- TODO status update should happen after the token status successfully sent - case notification of - PNVerification _ -> atomically $ writeTVar tknStatus NTConfirmed - _ -> pure () +ntfPush :: MonadUnliftIO m => NtfPushServer -> m () +ntfPush s@NtfPushServer {pushQ} = liftIO . forever . runExceptT $ do + (tkn@NtfTknData {token = DeviceToken pp _, tknStatus}, ntf) <- atomically (readTBQueue pushQ) + liftIO $ putStrLn $ "sending push notification to " <> show pp + status <- readTVarIO tknStatus + case (status, ntf) of + (_, PNVerification _) -> do + -- TODO check token status + deliverNotification pp tkn ntf + atomically $ writeTVar tknStatus NTConfirmed + (NTActive, PNCheckMessages) -> do + deliverNotification pp tkn ntf + _ -> do + liftIO $ putStrLn "bad notification token status" + where + deliverNotification :: PushProvider -> PushProviderClient + deliverNotification pp tkn ntf = do + deliver <- liftIO $ getPushClient s pp + -- TODO retry later based on the error + deliver tkn ntf `catchError` \e -> liftIO (putStrLn $ "Push provider error (" <> show pp <> "): " <> show e) >> throwError e runNtfClientTransport :: (Transport c, MonadUnliftIO m, MonadReader NtfEnv m) => THandle c -> m () runNtfClientTransport th@THandle {sessionId} = do @@ -128,6 +139,7 @@ clientDisconnected NtfServerClient {connected} = atomically $ writeTVar connecte receive :: (Transport c, MonadUnliftIO m, MonadReader NtfEnv m) => THandle c -> NtfServerClient -> m () receive th NtfServerClient {rcvQ, sndQ} = forever $ do t@(_, _, (corrId, subId, cmdOrError)) <- tGet th + liftIO $ putStrLn "receive" case cmdOrError of Left e -> write sndQ (corrId, subId, NRErr e) Right cmd -> @@ -181,7 +193,7 @@ verifyNtfTransmission (sig_, signed, (corrId, entId, _)) cmd = do -- _ -> maybe False (dummyVerifyCmd signed) sig_ `seq` VRFail client :: forall m. (MonadUnliftIO m, MonadReader NtfEnv m) => NtfServerClient -> NtfSubscriber -> NtfPushServer -> m () -client NtfServerClient {rcvQ, sndQ} NtfSubscriber {subQ} NtfPushServer {pushQ} = +client NtfServerClient {rcvQ, sndQ} NtfSubscriber {subQ = _} NtfPushServer {pushQ} = forever $ atomically (readTBQueue rcvQ) >>= processCommand @@ -190,6 +202,7 @@ client NtfServerClient {rcvQ, sndQ} NtfSubscriber {subQ} NtfPushServer {pushQ} = processCommand :: NtfRequest -> m (Transmission NtfResponse) processCommand = \case NtfReqNew corrId (ANE SToken newTkn@(NewNtfTkn _ _ dhPubKey)) -> do + liftIO $ putStrLn "TNEW" st <- asks store (srvDhPubKey, srvDrivDhKey) <- liftIO C.generateKeyPair' let dhSecret = C.dh' dhPubKey srvDrivDhKey @@ -203,18 +216,20 @@ client NtfServerClient {rcvQ, sndQ} NtfSubscriber {subQ} NtfPushServer {pushQ} = NtfReqCmd SToken (NtfTkn NtfTknData {tknStatus, tknRegCode}) (corrId, tknId, cmd) -> do status <- readTVarIO tknStatus (corrId,tknId,) <$> case cmd of - TNEW newTkn -> pure NROk -- TODO when duplicate token sent + TNEW _newTkn -> do + liftIO $ putStrLn "TNEW'" + pure NROk -- TODO when duplicate token sent TVFY code -- this allows repeated verification for cases when client connection dropped before server response | (status == NTRegistered || status == NTConfirmed || status == NTActive) && tknRegCode == code -> do atomically $ writeTVar tknStatus NTActive pure NROk | otherwise -> pure $ NRErr AUTH TDEL -> pure NROk - TCRN int -> pure NROk - NtfReqNew corrId (ANE SSubscription newSub) -> pure (corrId, "", NROk) - NtfReqCmd SSubscription sub (corrId, subId, cmd) -> + TCRN _int -> pure NROk + NtfReqNew corrId (ANE SSubscription _newSub) -> pure (corrId, "", NROk) + NtfReqCmd SSubscription _sub (corrId, subId, cmd) -> (corrId,subId,) <$> case cmd of - SNEW newSub -> pure NROk + SNEW _newSub -> pure NROk SCHK -> pure NROk SDEL -> pure NROk PING -> pure NRPong @@ -225,7 +240,7 @@ client NtfServerClient {rcvQ, sndQ} NtfSubscriber {subQ} NtfPushServer {pushQ} = getRandomBytes :: Int -> m ByteString getRandomBytes n = do gVar <- asks idsDrg - atomically (randomBytes n gVar) + atomically (C.pseudoRandomBytes n gVar) -- NReqCreate corrId tokenId smpQueue -> pure (corrId, "", NROk) -- do diff --git a/src/Simplex/Messaging/Notifications/Server/Env.hs b/src/Simplex/Messaging/Notifications/Server/Env.hs index e76c63375..67b83f1af 100644 --- a/src/Simplex/Messaging/Notifications/Server/Env.hs +++ b/src/Simplex/Messaging/Notifications/Server/Env.hs @@ -1,28 +1,27 @@ {-# LANGUAGE DataKinds #-} -{-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE DuplicateRecordFields #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE KindSignatures #-} +{-# LANGUAGE LambdaCase #-} {-# LANGUAGE NamedFieldPuns #-} module Simplex.Messaging.Notifications.Server.Env where import Control.Monad.IO.Unlift import Crypto.Random -import Data.Aeson (FromJSON, ToJSON) -import qualified Data.Aeson as J import Data.ByteString.Char8 (ByteString) import Data.X509.Validation (Fingerprint (..)) -import GHC.Generics import Network.Socket import qualified Network.TLS as T import Numeric.Natural import Simplex.Messaging.Client.Agent import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Notifications.Protocol +import Simplex.Messaging.Notifications.Server.Push.APNS import Simplex.Messaging.Notifications.Server.Subscriptions -import Simplex.Messaging.Parsers (dropPrefix, taggedObjectJSON) import Simplex.Messaging.Protocol (CorrId, Transmission) +import Simplex.Messaging.TMap (TMap) +import qualified Simplex.Messaging.TMap as TM import Simplex.Messaging.Transport (ATransport) import Simplex.Messaging.Transport.Server (loadFingerprint, loadTLSServerParams) import UnliftIO.STM @@ -35,22 +34,13 @@ data NtfServerConfig = NtfServerConfig subQSize :: Natural, pushQSize :: Natural, smpAgentCfg :: SMPClientAgentConfig, + apnsConfig :: APNSPushClientConfig, -- CA certificate private key is not needed for initialization caCertificateFile :: FilePath, privateKeyFile :: FilePath, certificateFile :: FilePath } -data PushNotification = PNVerification {code :: NtfRegCode} | PNPeriodic - deriving (Show, Generic) - -instance FromJSON PushNotification where - parseJSON = J.genericParseJSON . taggedObjectJSON $ dropPrefix "PN" - -instance ToJSON PushNotification where - toJSON = J.genericToJSON . taggedObjectJSON $ dropPrefix "PN" - toEncoding = J.genericToEncoding . taggedObjectJSON $ dropPrefix "PN" - data NtfEnv = NtfEnv { config :: NtfServerConfig, subscriber :: NtfSubscriber, @@ -63,11 +53,13 @@ data NtfEnv = NtfEnv } newNtfServerEnv :: (MonadUnliftIO m, MonadRandom m) => NtfServerConfig -> m NtfEnv -newNtfServerEnv config@NtfServerConfig {subQSize, pushQSize, smpAgentCfg, caCertificateFile, certificateFile, privateKeyFile} = do +newNtfServerEnv config@NtfServerConfig {subQSize, pushQSize, smpAgentCfg, apnsConfig, caCertificateFile, certificateFile, privateKeyFile} = do idsDrg <- newTVarIO =<< drgNew store <- atomically newNtfStore subscriber <- atomically $ newNtfSubscriber subQSize smpAgentCfg - pushServer <- atomically $ newNtfPushServer pushQSize + pushServer <- atomically $ newNtfPushServer pushQSize apnsConfig + -- TODO not creating APNS client on start to pass CI test, has to be replaced with mock APNS server + -- void . liftIO $ newPushClient pushServer PPApple tlsServerParams <- liftIO $ loadTLSServerParams caCertificateFile certificateFile privateKeyFile Fingerprint fp <- liftIO $ loadFingerprint caCertificateFile pure NtfEnv {config, subscriber, pushServer, store, idsDrg, tlsServerParams, serverIdentity = C.KeyHash fp} @@ -83,14 +75,28 @@ newNtfSubscriber qSize smpAgentCfg = do subQ <- newTBQueue qSize pure NtfSubscriber {smpAgent, subQ} -newtype NtfPushServer = NtfPushServer - { pushQ :: TBQueue (NtfTknData, PushNotification) +data NtfPushServer = NtfPushServer + { pushQ :: TBQueue (NtfTknData, PushNotification), + pushClients :: TMap PushProvider PushProviderClient, + apnsConfig :: APNSPushClientConfig } -newNtfPushServer :: Natural -> STM NtfPushServer -newNtfPushServer qSize = do +newNtfPushServer :: Natural -> APNSPushClientConfig -> STM NtfPushServer +newNtfPushServer qSize apnsConfig = do pushQ <- newTBQueue qSize - pure NtfPushServer {pushQ} + pushClients <- TM.empty + pure NtfPushServer {pushQ, pushClients, apnsConfig} + +newPushClient :: NtfPushServer -> PushProvider -> IO PushProviderClient +newPushClient NtfPushServer {apnsConfig, pushClients} = \case + PPApple -> do + c <- apnsPushProviderClient <$> createAPNSPushClient apnsConfig + atomically $ TM.insert PPApple c pushClients + pure c + +getPushClient :: NtfPushServer -> PushProvider -> IO PushProviderClient +getPushClient s@NtfPushServer {pushClients} pp = + atomically (TM.lookup pp pushClients) >>= maybe (newPushClient s pp) pure data NtfRequest = NtfReqNew CorrId ANewNtfEntity diff --git a/src/Simplex/Messaging/Notifications/Server/Push.hs b/src/Simplex/Messaging/Notifications/Server/Push.hs deleted file mode 100644 index 87b475f2a..000000000 --- a/src/Simplex/Messaging/Notifications/Server/Push.hs +++ /dev/null @@ -1,11 +0,0 @@ -module Simplex.Messaging.Notifications.Server.Push where - -import Control.Concurrent.STM -import Data.ByteString.Char8 (ByteString) -import Simplex.Messaging.Protocol (NotifierId, SMPServer) - -data NtfPushPayload = NPVerification ByteString | NPNotification SMPServer NotifierId | NPPing - -class PushProvider p where - newPushProvider :: STM p - requestBody :: p -> NtfPushPayload -> ByteString -- ? diff --git a/src/Simplex/Messaging/Notifications/Server/Push/.gitignore b/src/Simplex/Messaging/Notifications/Server/Push/.gitignore new file mode 100644 index 000000000..61d5e2e09 --- /dev/null +++ b/src/Simplex/Messaging/Notifications/Server/Push/.gitignore @@ -0,0 +1 @@ +local.env diff --git a/src/Simplex/Messaging/Notifications/Server/Push/APNS.hs b/src/Simplex/Messaging/Notifications/Server/Push/APNS.hs new file mode 100644 index 000000000..8a67f9fcf --- /dev/null +++ b/src/Simplex/Messaging/Notifications/Server/Push/APNS.hs @@ -0,0 +1,348 @@ +{-# LANGUAGE DeriveGeneric #-} +{-# LANGUAGE DuplicateRecordFields #-} +{-# LANGUAGE GeneralizedNewtypeDeriving #-} +{-# LANGUAGE LambdaCase #-} +{-# LANGUAGE NamedFieldPuns #-} +{-# LANGUAGE OverloadedStrings #-} + +module Simplex.Messaging.Notifications.Server.Push.APNS where + +import Control.Monad.Except +import Crypto.Hash.Algorithms (SHA256 (..)) +import qualified Crypto.PubKey.ECC.ECDSA as EC +import qualified Crypto.PubKey.ECC.Types as ECT +import Crypto.Random (ChaChaDRG, drgNew) +import qualified Crypto.Store.PKCS8 as PK +import Data.ASN1.BinaryEncoding (DER (..)) +import Data.ASN1.Encoding +import Data.ASN1.Types +import Data.Aeson (FromJSON, ToJSON, (.=)) +import qualified Data.Aeson as J +import qualified Data.Aeson.Encoding as JE +import Data.Bifunctor (first) +import qualified Data.ByteString.Base64.URL as U +import Data.ByteString.Builder (lazyByteString) +import Data.ByteString.Char8 (ByteString) +import qualified Data.ByteString.Lazy.Char8 as LB +import qualified Data.CaseInsensitive as CI +import Data.Int (Int64) +import Data.Map.Strict (Map) +import Data.Maybe (fromMaybe) +import Data.Text (Text) +import qualified Data.Text as T +import Data.Text.Encoding (decodeUtf8With) +import Data.Time.Clock.System +import qualified Data.X509 as X +import GHC.Generics +import Network.HTTP.Types (HeaderName, Status, hAuthorization, methodPost) +import qualified Network.HTTP.Types as N +import Network.HTTP2.Client (Request) +import qualified Network.HTTP2.Client as H +import Network.Socket (HostName, ServiceName) +import qualified Simplex.Messaging.Crypto as C +import Simplex.Messaging.Encoding.String +import Simplex.Messaging.Notifications.Protocol +import Simplex.Messaging.Notifications.Server.Subscriptions (NtfTknData (..)) +import Simplex.Messaging.Protocol (NotifierId, SMPServer) +import Simplex.Messaging.Transport.Client.HTTP2 +import System.Environment (getEnv) +import UnliftIO.STM + +data JWTHeader = JWTHeader + { alg :: Text, -- key algorithm, ES256 for APNS + kid :: Text -- key ID + } + deriving (Show, Generic) + +instance ToJSON JWTHeader where toEncoding = J.genericToEncoding J.defaultOptions + +data JWTClaims = JWTClaims + { iss :: Text, -- issuer, team ID for APNS + iat :: Int64 -- issue time, seconds from epoch + } + deriving (Show, Generic) + +instance ToJSON JWTClaims where toEncoding = J.genericToEncoding J.defaultOptions + +data JWTToken = JWTToken JWTHeader JWTClaims + deriving (Show) + +mkJWTToken :: JWTHeader -> Text -> IO JWTToken +mkJWTToken hdr iss = do + iat <- systemSeconds <$> getSystemTime + pure $ JWTToken hdr JWTClaims {iss, iat} + +type SignedJWTToken = ByteString + +signedJWTToken :: EC.PrivateKey -> JWTToken -> IO SignedJWTToken +signedJWTToken pk (JWTToken hdr claims) = do + let hc = jwtEncode hdr <> "." <> jwtEncode claims + sig <- EC.sign pk SHA256 hc + pure $ hc <> "." <> serialize sig + where + jwtEncode :: ToJSON a => a -> ByteString + jwtEncode = U.encodeUnpadded . LB.toStrict . J.encode + serialize sig = U.encodeUnpadded $ encodeASN1' DER [Start Sequence, IntVal (EC.sign_r sig), IntVal (EC.sign_s sig), End Sequence] + +readECPrivateKey :: FilePath -> IO EC.PrivateKey +readECPrivateKey f = do + -- TODO this is specific to APNS key + [PK.Unprotected (X.PrivKeyEC X.PrivKeyEC_Named {privkeyEC_name, privkeyEC_priv})] <- PK.readKeyFile f + pure EC.PrivateKey {private_curve = ECT.getCurveByName privkeyEC_name, private_d = privkeyEC_priv} + +data PushNotification + = PNVerification NtfRegCode + | PNMessage SMPServer NotifierId + | PNAlert Text + | PNCheckMessages + +data APNSNotification = APNSNotification {aps :: APNSNotificationBody, notificationData :: Maybe J.Value} + deriving (Show, Generic) + +instance ToJSON APNSNotification where + toJSON = J.genericToJSON J.defaultOptions {J.omitNothingFields = True} + toEncoding = J.genericToEncoding J.defaultOptions {J.omitNothingFields = True} + +data APNSNotificationBody + = APNSAlert {alert :: APNSAlertBody, badge :: Maybe Int, sound :: Maybe Text, category :: Maybe Text} + | APNSBackground {contentAvailable :: Int} + | APNSMutableContent {mutableContent :: Int, alert :: APNSAlertBody, category :: Maybe Text} + deriving (Show, Generic) + +apnsJSONOptions :: J.Options +apnsJSONOptions = J.defaultOptions {J.omitNothingFields = True, J.sumEncoding = J.UntaggedValue, J.fieldLabelModifier = J.camelTo2 '-'} + +instance ToJSON APNSNotificationBody where + toJSON = J.genericToJSON apnsJSONOptions + toEncoding = J.genericToEncoding apnsJSONOptions + +type APNSNotificationData = Map Text Text + +data APNSAlertBody = APNSAlertObject {title :: Text, subtitle :: Text, body :: Text} | APNSAlertText Text + deriving (Show) + +instance ToJSON APNSAlertBody where + toEncoding = \case + APNSAlertObject {title, subtitle, body} -> J.pairs $ "title" .= title <> "subtitle" .= subtitle <> "body" .= body + APNSAlertText t -> JE.text t + toJSON = \case + APNSAlertObject {title, subtitle, body} -> J.object ["title" .= title, "subtitle" .= subtitle, "body" .= body] + APNSAlertText t -> J.String t + +-- APNS notification types +-- +-- Visible alerts: +-- { +-- "aps" : { +-- "alert" : { +-- "title" : "Game Request", +-- "subtitle" : "Five Card Draw", +-- "body" : "Bob wants to play poker" +-- }, +-- "badge" : 9, +-- "sound" : "bingbong.aiff", +-- "category" : "GAME_INVITATION" +-- }, +-- "gameID" : "12345678" +-- } +-- +-- Simple text alert: +-- {"aps":{"alert":"you have a new message"}} +-- +-- Background notification to fetch content +-- {"aps":{"content-available":1}} +-- +-- Mutable content notification that must be shown but can be processed before before being shown (up to 30 sec) +-- { +-- "aps" : { +-- "category" : "SECRET", +-- "mutable-content" : 1, +-- "alert" : { +-- "title" : "Secret Message!", +-- "body" : "(Encrypted)" +-- }, +-- }, +-- "ENCRYPTED_DATA" : "Salted__·öîQÊ$UDì_¶Ù∞èΩ^¬%gq∞NÿÒQùw" +-- } + +data APNSPushClientConfig = APNSPushClientConfig + { tokenTTL :: Int64, + authKeyFileEnv :: String, + authKeyAlg :: Text, + authKeyIdEnv :: String, + paddedNtfLength :: Int, + appName :: ByteString, + appTeamId :: Text, + apnHost :: HostName, + apnPort :: ServiceName, + https2cfg :: HTTP2SClientConfig + } + deriving (Show) + +defaultAPNSPushClientConfig :: APNSPushClientConfig +defaultAPNSPushClientConfig = + APNSPushClientConfig + { tokenTTL = 1200, -- 20 minutes + authKeyFileEnv = "APNS_KEY_FILE", -- the environment variables APNS_KEY_FILE and APNS_KEY_ID must be set, or the server would fail to start + authKeyAlg = "ES256", + authKeyIdEnv = "APNS_KEY_ID", + paddedNtfLength = 256, + appName = "chat.simplex.app", + appTeamId = "5NN7GUYB6T", + apnHost = "api.sandbox.push.apple.com", + apnPort = "443", + https2cfg = defaultHTTP2SClientConfig + } + +data APNSPushClient = APNSPushClient + { https2Client :: TVar (Maybe HTTPS2Client), + privateKey :: EC.PrivateKey, + jwtHeader :: JWTHeader, + jwtToken :: TVar (JWTToken, SignedJWTToken), + nonceDrg :: TVar ChaChaDRG, + apnsCfg :: APNSPushClientConfig + } + +createAPNSPushClient :: APNSPushClientConfig -> IO APNSPushClient +createAPNSPushClient apnsCfg@APNSPushClientConfig {authKeyFileEnv, authKeyAlg, authKeyIdEnv, appTeamId} = do + https2Client <- newTVarIO Nothing + void $ connectHTTPS2 apnsCfg https2Client + privateKey <- readECPrivateKey =<< getEnv authKeyFileEnv + authKeyId <- T.pack <$> getEnv authKeyIdEnv + putStrLn $ authKeyIdEnv <> "=" <> T.unpack authKeyId + let jwtHeader = JWTHeader {alg = authKeyAlg, kid = authKeyId} + jwtToken <- newTVarIO =<< mkApnsJWTToken appTeamId jwtHeader privateKey + nonceDrg <- drgNew >>= newTVarIO + pure APNSPushClient {https2Client, privateKey, jwtHeader, jwtToken, nonceDrg, apnsCfg} + +getApnsJWTToken :: APNSPushClient -> IO SignedJWTToken +getApnsJWTToken APNSPushClient {apnsCfg = APNSPushClientConfig {appTeamId, tokenTTL}, privateKey, jwtHeader, jwtToken} = do + (jwt, signedJWT) <- readTVarIO jwtToken + age <- jwtTokenAge jwt + if age < tokenTTL + then pure signedJWT + else do + t@(_, signedJWT') <- mkApnsJWTToken appTeamId jwtHeader privateKey + atomically $ writeTVar jwtToken t + pure signedJWT' + where + jwtTokenAge (JWTToken _ JWTClaims {iat}) = (iat -) . systemSeconds <$> getSystemTime + +mkApnsJWTToken :: Text -> JWTHeader -> EC.PrivateKey -> IO (JWTToken, SignedJWTToken) +mkApnsJWTToken appTeamId jwtHeader privateKey = do + jwt <- mkJWTToken jwtHeader appTeamId + signedJWT <- signedJWTToken privateKey jwt + pure (jwt, signedJWT) + +connectHTTPS2 :: APNSPushClientConfig -> TVar (Maybe HTTPS2Client) -> IO (Either HTTPS2ClientError HTTPS2Client) +connectHTTPS2 APNSPushClientConfig {apnHost, apnPort, https2cfg} https2Client = do + r <- getHTTPS2Client apnHost apnPort https2cfg disconnected + case r of + Right client -> atomically . writeTVar https2Client $ Just client + Left e -> putStrLn $ "Error connecting to APNS: " <> show e + pure r + where + disconnected = atomically $ writeTVar https2Client Nothing + +getApnsHTTP2Client :: APNSPushClient -> IO (Either HTTPS2ClientError HTTPS2Client) +getApnsHTTP2Client APNSPushClient {https2Client, apnsCfg} = + readTVarIO https2Client >>= maybe (connectHTTPS2 apnsCfg https2Client) (pure . Right) + +disconnectApnsHTTP2Client :: APNSPushClient -> IO () +disconnectApnsHTTP2Client APNSPushClient {https2Client} = + readTVarIO https2Client >>= mapM_ closeHTTPS2Client >> atomically (writeTVar https2Client Nothing) + +apnsNotification :: NtfTknData -> C.CbNonce -> Int -> PushNotification -> Either C.CryptoError APNSNotification +apnsNotification NtfTknData {tknDhSecret} nonce paddedLen = \case + PNVerification (NtfRegCode code) -> + encrypt code $ \code' -> + apn APNSBackground {contentAvailable = 1} . Just $ J.object ["verification" .= code'] + PNMessage srv nId -> + encrypt (strEncode srv <> "/" <> strEncode nId) $ \ntfQueue -> + apn apnMutableContent . Just $ J.object ["checkMessage" .= ntfQueue] + PNAlert text -> Right $ apn (apnAlert $ APNSAlertText text) Nothing + PNCheckMessages -> Right $ apn APNSBackground {contentAvailable = 1} . Just $ J.object ["checkMessages" .= True] + where + encrypt :: ByteString -> (Text -> APNSNotification) -> Either C.CryptoError APNSNotification + encrypt ntfData f = f . safeDecodeUtf8 . U.encode <$> C.cbEncrypt tknDhSecret nonce ntfData paddedLen + apn aps notificationData = APNSNotification {aps, notificationData} + apnMutableContent = APNSMutableContent {mutableContent = 1, alert = APNSAlertText "Encrypted message or some other app event", category = Nothing} + apnAlert alert = APNSAlert {alert, badge = Nothing, sound = Nothing, category = Nothing} + safeDecodeUtf8 = decodeUtf8With onError where onError _ _ = Just '?' + +apnsRequest :: APNSPushClient -> ByteString -> APNSNotification -> IO Request +apnsRequest c tkn ntf@APNSNotification {aps} = do + signedJWT <- getApnsJWTToken c + pure $ H.requestBuilder methodPost path (headers signedJWT) (lazyByteString $ J.encode ntf) + where + path = "/3/device/" <> tkn + headers signedJWT = + [ (hApnsTopic, appName $ apnsCfg (c :: APNSPushClient)), + (hApnsPushType, pushType aps), + (hAuthorization, "bearer " <> signedJWT) + ] + <> [(hApnsPriority, "5") | isBackground aps] + isBackground = \case + APNSBackground {} -> True + _ -> False + pushType = \case + APNSBackground {} -> "background" + _ -> "alert" + +data PushProviderError + = PPConnection HTTPS2ClientError + | PPCryptoError C.CryptoError + | PPResponseError (Maybe Status) Text + | PPTokenInvalid + | PPRetryLater + | PPPermanentError + deriving (Show) + +type PushProviderClient = NtfTknData -> PushNotification -> ExceptT PushProviderError IO () + +newtype APNSErrorReponse = APNSErrorReponse {reason :: Text} + deriving (Generic, FromJSON) + +apnsPushProviderClient :: APNSPushClient -> PushProviderClient +apnsPushProviderClient c@APNSPushClient {nonceDrg, apnsCfg} tkn@NtfTknData {token = DeviceToken PPApple tknStr} pn = do + http2 <- liftHTTPS2 $ getApnsHTTP2Client c + nonce <- atomically $ C.pseudoRandomCbNonce nonceDrg + apnsNtf <- liftEither $ first PPCryptoError $ apnsNotification tkn nonce (paddedNtfLength apnsCfg) pn + liftIO $ putStrLn $ "APNS notification: " <> show apnsNtf + req <- liftIO $ apnsRequest c tknStr apnsNtf + liftIO $ putStrLn $ "APNS request: " <> show req + HTTP2Response {response, respBody} <- liftHTTPS2 $ sendRequest http2 req + let status = H.responseStatus response + reason = fromMaybe "" $ J.decodeStrict' =<< respBody + liftIO $ putStrLn $ "APNS response: " <> show status <> " " <> T.unpack reason + result status reason + where + result :: Maybe Status -> Text -> ExceptT PushProviderError IO () + result status reason + | status == Just N.ok200 = pure () + | status == Just N.badRequest400 = + case reason of + "BadDeviceToken" -> throwError PPTokenInvalid + "DeviceTokenNotForTopic" -> throwError PPTokenInvalid + "TopicDisallowed" -> throwError PPPermanentError + _ -> err status reason + | status == Just N.forbidden403 = case reason of + "ExpiredProviderToken" -> throwError PPPermanentError -- there should be no point retrying it as the token was refreshed + "InvalidProviderToken" -> throwError PPPermanentError + _ -> err status reason + | status == Just N.gone410 = throwError PPTokenInvalid + | status == Just N.serviceUnavailable503 = liftIO (disconnectApnsHTTP2Client c) >> throwError PPRetryLater + -- Just tooManyRequests429 -> TODO TooManyRequests - too many requests for the same token + | otherwise = err status reason + err :: Maybe Status -> Text -> ExceptT PushProviderError IO () + err s r = throwError $ PPResponseError s r + liftHTTPS2 a = ExceptT $ first PPConnection <$> a + +hApnsTopic :: HeaderName +hApnsTopic = CI.mk "apns-topic" + +hApnsPushType :: HeaderName +hApnsPushType = CI.mk "apns-push-type" + +hApnsPriority :: HeaderName +hApnsPriority = CI.mk "apns-priority" diff --git a/src/Simplex/Messaging/Notifications/Server/Push/testpush.sh b/src/Simplex/Messaging/Notifications/Server/Push/testpush.sh new file mode 100755 index 000000000..204fce599 --- /dev/null +++ b/src/Simplex/Messaging/Notifications/Server/Push/testpush.sh @@ -0,0 +1,20 @@ +#!/bin/sh + +export TEAM_ID=5NN7GUYB6T +# export APNS_KEY_FILE="" +# export APNS_KEY_ID="" +export TOPIC=chat.simplex.app +# export DEVICE_TOKEN="" +export APNS_HOST_NAME=api.sandbox.push.apple.com + +export JWT_ISSUE_TIME=$(date +%s) +export JWT_HEADER=$(printf '{"alg":"ES256","kid":"%s"}' "${APNS_KEY_ID}" | openssl base64 -e -A | tr -- '+/' '-_' | tr -d =) +export JWT_CLAIMS=$(printf '{"iss":"%s","iat":%d}' "${TEAM_ID}" "${JWT_ISSUE_TIME}" | openssl base64 -e -A | tr -- '+/' '-_' | tr -d =) +export JWT_HEADER_CLAIMS="${JWT_HEADER}.${JWT_CLAIMS}" + +export JWT_SIGNED_HEADER_CLAIMS=$(printf "${JWT_HEADER_CLAIMS}" | openssl dgst -binary -sha256 -sign "${APNS_KEY_FILE}" | openssl base64 -e -A | tr -- '+/' '-_' | tr -d =) +export AUTHENTICATION_TOKEN="${JWT_HEADER}.${JWT_CLAIMS}.${JWT_SIGNED_HEADER_CLAIMS}" + +# curl -v --header "apns-topic: $TOPIC" --header "apns-push-type: alert" --header "authorization: bearer $AUTHENTICATION_TOKEN" --data '{"aps":{"alert":"you have a new message"}}' --http2 https://${APNS_HOST_NAME}/3/device/${DEVICE_TOKEN} + +curl -v --header "apns-topic: $TOPIC" --header "apns-push-type: background" --header "apns-priority: 5" --header "authorization: bearer $AUTHENTICATION_TOKEN" --data '{"aps":{"content-available":1}}' --http2 https://${APNS_HOST_NAME}/3/device/${DEVICE_TOKEN} diff --git a/src/Simplex/Messaging/Notifications/Server/Subscriptions.hs b/src/Simplex/Messaging/Notifications/Server/Subscriptions.hs index fa5790d85..366ba26c2 100644 --- a/src/Simplex/Messaging/Notifications/Server/Subscriptions.hs +++ b/src/Simplex/Messaging/Notifications/Server/Subscriptions.hs @@ -2,21 +2,13 @@ {-# LANGUAGE DuplicateRecordFields #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE KindSignatures #-} -{-# LANGUAGE LambdaCase #-} {-# LANGUAGE NamedFieldPuns #-} -{-# LANGUAGE TupleSections #-} module Simplex.Messaging.Notifications.Server.Subscriptions where import Control.Concurrent.STM -import Control.Monad -import Crypto.PubKey.Curve25519 (dhSecret) -import Data.Map.Strict (Map) -import qualified Data.Map.Strict as M -import Data.Set (Set) import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Notifications.Protocol -import Simplex.Messaging.Protocol (ErrorType (..), NotifierId, NtfPrivateSignKey, ProtocolServer) import Simplex.Messaging.TMap (TMap) import qualified Simplex.Messaging.TMap as TM import Simplex.Messaging.Util ((<$$>)) diff --git a/src/Simplex/Messaging/Server.hs b/src/Simplex/Messaging/Server.hs index c3d62bb02..eaa342c1d 100644 --- a/src/Simplex/Messaging/Server.hs +++ b/src/Simplex/Messaging/Server.hs @@ -23,7 +23,7 @@ -- and optional append only log of SMP queue records. -- -- See https://github.com/simplex-chat/simplexmq/blob/master/protocol/simplex-messaging.md -module Simplex.Messaging.Server (runSMPServer, runSMPServerBlocking, verifyCmdSignature, dummyVerifyCmd, randomBytes) where +module Simplex.Messaging.Server (runSMPServer, runSMPServerBlocking, verifyCmdSignature, dummyVerifyCmd) where import Control.Monad import Control.Monad.Except @@ -439,11 +439,4 @@ withLog action = do randomId :: (MonadUnliftIO m, MonadReader Env m) => Int -> m ByteString randomId n = do gVar <- asks idsDrg - atomically (randomBytes n gVar) - -randomBytes :: Int -> TVar ChaChaDRG -> STM ByteString -randomBytes n gVar = do - g <- readTVar gVar - let (bytes, g') = randomBytesGenerate n g - writeTVar gVar g' - return bytes + atomically (C.pseudoRandomBytes n gVar) diff --git a/src/Simplex/Messaging/Transport/Client.hs b/src/Simplex/Messaging/Transport/Client.hs index 3771da99e..cf24c75ef 100644 --- a/src/Simplex/Messaging/Transport/Client.hs +++ b/src/Simplex/Messaging/Transport/Client.hs @@ -1,7 +1,9 @@ +{-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE ScopedTypeVariables #-} module Simplex.Messaging.Transport.Client ( runTransportClient, + runTLSTransportClient, smpClientHandshake, ) where @@ -26,9 +28,12 @@ import UnliftIO.Exception (IOException) import qualified UnliftIO.Exception as E -- | Connect to passed TCP host:port and pass handle to the client. -runTransportClient :: Transport c => MonadUnliftIO m => HostName -> ServiceName -> C.KeyHash -> Maybe KeepAliveOpts -> (c -> m a) -> m a -runTransportClient host port keyHash keepAliveOpts client = do - let clientParams = mkTLSClientParams host port keyHash +runTransportClient :: (Transport c, MonadUnliftIO m) => HostName -> ServiceName -> Maybe C.KeyHash -> Maybe KeepAliveOpts -> (c -> m a) -> m a +runTransportClient = runTLSTransportClient supportedParameters Nothing + +runTLSTransportClient :: (Transport c, MonadUnliftIO m) => T.Supported -> Maybe XS.CertificateStore -> HostName -> ServiceName -> Maybe C.KeyHash -> Maybe KeepAliveOpts -> (c -> m a) -> m a +runTLSTransportClient tlsParams caStore_ host port keyHash keepAliveOpts client = do + let clientParams = mkTLSClientParams tlsParams caStore_ host port keyHash c <- liftIO $ startTCPClient host port clientParams keepAliveOpts client c `E.finally` liftIO (closeConnection c) @@ -56,13 +61,15 @@ startTCPClient host port clientParams keepAliveOpts = withSocketsDo $ resolve >> ctx <- connectTLS clientParams sock getClientConnection ctx -mkTLSClientParams :: HostName -> ServiceName -> C.KeyHash -> T.ClientParams -mkTLSClientParams host port keyHash = do +-- readCertificateStore :: FilePath -> IO (Maybe CertificateStore) + +mkTLSClientParams :: T.Supported -> Maybe XS.CertificateStore -> HostName -> ServiceName -> Maybe C.KeyHash -> T.ClientParams +mkTLSClientParams supported caStore_ host port keyHash_ = do let p = B.pack port (T.defaultParamsClient host p) - { T.clientShared = def, - T.clientHooks = def {T.onServerCertificate = \_ _ _ -> validateCertificateChain keyHash host p}, - T.clientSupported = supportedParameters + { T.clientShared = maybe def (\caStore -> def {T.sharedCAStore = caStore}) caStore_, + T.clientHooks = maybe def (\keyHash -> def {T.onServerCertificate = \_ _ _ -> validateCertificateChain keyHash host p}) keyHash_, + T.clientSupported = supported } validateCertificateChain :: C.KeyHash -> HostName -> ByteString -> X.CertificateChain -> IO [XV.FailedReason] diff --git a/src/Simplex/Messaging/Transport/Client/HTTP2.hs b/src/Simplex/Messaging/Transport/Client/HTTP2.hs new file mode 100644 index 000000000..8e3a35741 --- /dev/null +++ b/src/Simplex/Messaging/Transport/Client/HTTP2.hs @@ -0,0 +1,169 @@ +{-# LANGUAGE DuplicateRecordFields #-} +{-# LANGUAGE LambdaCase #-} +{-# LANGUAGE NamedFieldPuns #-} +{-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE ScopedTypeVariables #-} + +module Simplex.Messaging.Transport.Client.HTTP2 where + +import Control.Concurrent.Async +import Control.Exception (IOException, catch, finally) +import qualified Control.Exception as E +import Control.Monad.Except +import Data.ByteString.Char8 (ByteString) +import qualified Data.ByteString.Char8 as B +import Data.Default (def) +import Data.Maybe (isNothing) +import qualified Data.X509.CertificateStore as XS +import Foreign (mallocBytes) +import Network.HPACK (BufferSize, HeaderTable) +import Network.HTTP2.Client (ClientConfig (..), Config (..), Request, Response) +import qualified Network.HTTP2.Client as H +import Network.Socket (HostName, ServiceName) +import qualified Network.TLS as T +import qualified Network.TLS.Extra as TE +import Numeric.Natural (Natural) +import Simplex.Messaging.Transport (TLS, Transport (cGet, cPut)) +import Simplex.Messaging.Transport.Client (runTLSTransportClient) +import Simplex.Messaging.Transport.KeepAlive (KeepAliveOpts) +import qualified System.TimeManager as TI +import UnliftIO.STM +import UnliftIO.Timeout + +data HTTPS2Client = HTTPS2Client + { action :: Async (), + connected :: TVar Bool, + host :: HostName, + port :: ServiceName, + config :: HTTP2SClientConfig, + reqQ :: TBQueue (Request, TMVar HTTP2Response) + } + +data HTTP2Response = HTTP2Response + { response :: Response, + respBody :: Maybe ByteString, + respTrailers :: Maybe HeaderTable + } + +data HTTP2SClientConfig = HTTP2SClientConfig + { qSize :: Natural, + maxBody :: Int, + connTimeout :: Int, + tcpKeepAlive :: Maybe KeepAliveOpts, + caStoreFile :: FilePath, + suportedTLSParams :: T.Supported + } + deriving (Show) + +defaultHTTP2SClientConfig :: HTTP2SClientConfig +defaultHTTP2SClientConfig = + HTTP2SClientConfig + { qSize = 64, + maxBody = 500000, + connTimeout = 5000000, + tcpKeepAlive = Nothing, + caStoreFile = "/etc/ssl/cert.pem", + suportedTLSParams = + def + { T.supportedVersions = [T.TLS13, T.TLS12], + T.supportedCiphers = TE.ciphersuite_strong_det, + T.supportedSecureRenegotiation = False + } + } + +data HTTPS2ClientError = HCResponseTimeout | HCNetworkError | HCIOError IOException + deriving (Show) + +getHTTPS2Client :: HostName -> ServiceName -> HTTP2SClientConfig -> IO () -> IO (Either HTTPS2ClientError HTTPS2Client) +getHTTPS2Client host port config@HTTP2SClientConfig {tcpKeepAlive, connTimeout, caStoreFile, suportedTLSParams} disconnected = + (atomically mkHTTPS2Client >>= runClient) + `catch` \(e :: IOException) -> pure . Left $ HCIOError e + where + mkHTTPS2Client :: STM HTTPS2Client + mkHTTPS2Client = do + connected <- newTVar False + reqQ <- newTBQueue $ qSize config + pure HTTPS2Client {action = undefined, connected, host, port, config, reqQ} + + runClient :: HTTPS2Client -> IO (Either HTTPS2ClientError HTTPS2Client) + runClient c = do + cVar <- newEmptyTMVarIO + caStore <- XS.readCertificateStore caStoreFile + when (isNothing caStore) . putStrLn $ "Error loading CertificateStore from " <> caStoreFile + action <- + async $ + runHTTPS2Client suportedTLSParams caStore host port tcpKeepAlive (client c cVar) + `finally` atomically (putTMVar cVar $ Left HCNetworkError) + conn_ <- connTimeout `timeout` atomically (takeTMVar cVar) + pure $ case conn_ of + Just (Right ()) -> Right c {action} + Just (Left e) -> Left e + Nothing -> Left HCNetworkError + + client :: HTTPS2Client -> TMVar (Either HTTPS2ClientError ()) -> (Request -> (Response -> IO ()) -> IO ()) -> IO () + client c cVar sendReq = do + atomically $ do + writeTVar (connected c) True + putTMVar cVar $ Right () + process c sendReq `finally` disconnected + + process :: HTTPS2Client -> (Request -> (Response -> IO ()) -> IO ()) -> IO () + process HTTPS2Client {reqQ} sendReq = forever $ do + (req, respVar) <- atomically $ readTBQueue reqQ + sendReq req $ \r -> do + let writeResp respBody respTrailers = atomically $ putTMVar respVar HTTP2Response {response = r, respBody, respTrailers} + case H.responseBodySize r of + Just sz -> + if sz <= maxBody config + then do + respBody <- getResponseBody r "" sz + respTrailers <- join <$> mapM (const $ H.getResponseTrailers r) respBody + writeResp respBody respTrailers + else writeResp Nothing Nothing + _ -> writeResp Nothing Nothing + + getResponseBody :: Response -> ByteString -> Int -> IO (Maybe ByteString) + getResponseBody r s sz = + H.getResponseBodyChunk r >>= \chunk -> do + if chunk == "" + then pure (if B.length s == sz then Just s else Nothing) + else do + let s' = s <> chunk + if B.length s' > sz then pure Nothing else getResponseBody r s' sz + +-- | Disconnects client from the server and terminates client threads. +closeHTTPS2Client :: HTTPS2Client -> IO () +-- TODO disconnect +closeHTTPS2Client = uninterruptibleCancel . action + +sendRequest :: HTTPS2Client -> Request -> IO (Either HTTPS2ClientError HTTP2Response) +sendRequest HTTPS2Client {reqQ, config} req = do + resp <- newEmptyTMVarIO + atomically $ writeTBQueue reqQ (req, resp) + maybe (Left HCResponseTimeout) Right <$> (connTimeout config `timeout` atomically (takeTMVar resp)) + +runHTTPS2Client :: T.Supported -> Maybe XS.CertificateStore -> HostName -> ServiceName -> Maybe KeepAliveOpts -> ((Request -> (Response -> IO ()) -> IO ()) -> IO ()) -> IO () +runHTTPS2Client tlsParams caStore host port keepAliveOpts client = + runTLSTransportClient tlsParams caStore host port Nothing keepAliveOpts https2Client + where + cfg = ClientConfig "https" (B.pack host) 20 + https2Client :: TLS -> IO () + https2Client c = + E.bracket + (allocTlsConfig c 16384) + H.freeSimpleConfig + (\conf -> H.run cfg conf client) + + allocTlsConfig :: TLS -> BufferSize -> IO Config + allocTlsConfig c sz = do + buf <- mallocBytes sz + tm <- TI.initialize $ 30 * 1000000 + pure + Config + { confWriteBuffer = buf, + confBufferSize = sz, + confSendAll = cPut c, + confReadN = cGet c, + confPositionReadMaker = H.defaultPositionReadMaker, + confTimeoutManager = tm + } diff --git a/src/Simplex/Messaging/Transport/KeepAlive.hs b/src/Simplex/Messaging/Transport/KeepAlive.hs index c949394a0..08d17d47c 100644 --- a/src/Simplex/Messaging/Transport/KeepAlive.hs +++ b/src/Simplex/Messaging/Transport/KeepAlive.hs @@ -12,6 +12,7 @@ data KeepAliveOpts = KeepAliveOpts keepIntvl :: Int, keepCnt :: Int } + deriving (Show) defaultKeepAliveOpts :: KeepAliveOpts defaultKeepAliveOpts = diff --git a/tests/NtfClient.hs b/tests/NtfClient.hs index bb7f6dec6..c9ef301ab 100644 --- a/tests/NtfClient.hs +++ b/tests/NtfClient.hs @@ -19,6 +19,7 @@ import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Encoding import Simplex.Messaging.Notifications.Server (runNtfServerBlocking) import Simplex.Messaging.Notifications.Server.Env +import Simplex.Messaging.Notifications.Server.Push.APNS (defaultAPNSPushClientConfig) import Simplex.Messaging.Notifications.Transport import Simplex.Messaging.Protocol import Simplex.Messaging.Transport @@ -40,7 +41,7 @@ testKeyHash = "LcJUMfVhwD8yxjAiSaDzzGF3-kLG4Uh0Fl_ZIjrRwjI=" testNtfClient :: (Transport c, MonadUnliftIO m) => (THandle c -> m a) -> m a testNtfClient client = - runTransportClient testHost testPort testKeyHash (Just defaultKeepAliveOpts) $ \h -> + runTransportClient testHost testPort (Just testKeyHash) (Just defaultKeepAliveOpts) $ \h -> liftIO (runExceptT $ ntfClientHandshake h testKeyHash) >>= \case Right th -> client th Left e -> error $ show e @@ -55,6 +56,7 @@ cfg = subQSize = 1, pushQSize = 1, smpAgentCfg = defaultSMPClientAgentConfig, + apnsConfig = defaultAPNSPushClientConfig, -- CA certificate private key is not needed for initialization caCertificateFile = "tests/fixtures/ca.crt", privateKeyFile = "tests/fixtures/server.key", diff --git a/tests/NtfServerTests.hs b/tests/NtfServerTests.hs index 93195e8ac..680676dea 100644 --- a/tests/NtfServerTests.hs +++ b/tests/NtfServerTests.hs @@ -1,11 +1,7 @@ {-# LANGUAGE DataKinds #-} {-# LANGUAGE GADTs #-} -{-# LANGUAGE LambdaCase #-} -{-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE OverloadedStrings #-} -{-# LANGUAGE PatternSynonyms #-} {-# LANGUAGE ScopedTypeVariables #-} -{-# LANGUAGE TypeApplications #-} module NtfServerTests where @@ -37,4 +33,4 @@ ntfSyntaxTests (ATransport t) = do (Maybe C.ASignature, ByteString, ByteString, smp) -> (Maybe C.ASignature, ByteString, ByteString, BrokerMsg) -> Expectation - command >#> response = ntfServerTest t command `shouldReturn` response \ No newline at end of file + command >#> response = ntfServerTest t command `shouldReturn` response diff --git a/tests/SMPAgentClient.hs b/tests/SMPAgentClient.hs index 304ba9674..be3de3e2c 100644 --- a/tests/SMPAgentClient.hs +++ b/tests/SMPAgentClient.hs @@ -198,7 +198,7 @@ withSmpAgent t = withSmpAgentOn t (agentTestPort, testPort, testDB) testSMPAgentClientOn :: (Transport c, MonadUnliftIO m) => ServiceName -> (c -> m a) -> m a testSMPAgentClientOn port' client = do - runTransportClient agentTestHost port' testKeyHash (Just defaultKeepAliveOpts) $ \h -> do + runTransportClient agentTestHost port' (Just testKeyHash) (Just defaultKeepAliveOpts) $ \h -> do line <- liftIO $ getLn h if line == "Welcome to SMP agent v" <> B.pack simplexMQVersion then client h diff --git a/tests/SMPClient.hs b/tests/SMPClient.hs index e5a57ab72..90f090b6f 100644 --- a/tests/SMPClient.hs +++ b/tests/SMPClient.hs @@ -45,7 +45,7 @@ testStoreLogFile = "tests/tmp/smp-server-store.log" testSMPClient :: (Transport c, MonadUnliftIO m) => (THandle c -> m a) -> m a testSMPClient client = - runTransportClient testHost testPort testKeyHash (Just defaultKeepAliveOpts) $ \h -> + runTransportClient testHost testPort (Just testKeyHash) (Just defaultKeepAliveOpts) $ \h -> liftIO (runExceptT $ smpClientHandshake h testKeyHash) >>= \case Right th -> client th Left e -> error $ show e From 9d8a9c4fe4dd5aa4095306c6c2b0e9bea50a7803 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Sun, 17 Apr 2022 10:34:51 +0100 Subject: [PATCH 007/131] interval notifications (TCRN command) (#352) * notifications: periodic notifications * agent: allow repeat token registrations, delete old tokens from notification server (e.g., when database is moved to another device) * decrypt token verification code in the agent * check token status, send TCRN on registration if it was enabled * fix http2/apns response handling for error responses (also, APNS seems not to send content-length header?) --- apps/ntf-server/Main.hs | 8 +- src/Simplex/Messaging/Agent.hs | 82 +++++++----- src/Simplex/Messaging/Agent/Client.hs | 5 + src/Simplex/Messaging/Agent/Store.hs | 3 +- src/Simplex/Messaging/Agent/Store/SQLite.hs | 54 +++++--- .../Migrations/M20220322_notifications.hs | 5 +- src/Simplex/Messaging/Crypto.hs | 9 ++ src/Simplex/Messaging/Notifications/Client.hs | 28 ++-- .../Messaging/Notifications/Protocol.hs | 75 ++++++----- src/Simplex/Messaging/Notifications/Server.hs | 123 +++++++++++------- .../Messaging/Notifications/Server/Env.hs | 18 ++- .../Notifications/Server/Push/APNS.hs | 41 +++--- .../Notifications/Server/Subscriptions.hs | 15 ++- .../Messaging/Transport/Client/HTTP2.hs | 32 ++--- tests/AgentTests/FunctionalAPITests.hs | 4 +- 15 files changed, 312 insertions(+), 190 deletions(-) diff --git a/apps/ntf-server/Main.hs b/apps/ntf-server/Main.hs index 478077e25..41625d6f2 100644 --- a/apps/ntf-server/Main.hs +++ b/apps/ntf-server/Main.hs @@ -2,6 +2,7 @@ module Main where +import Control.Logger.Simple import Simplex.Messaging.Client.Agent (defaultSMPClientAgentConfig) import Simplex.Messaging.Notifications.Server (runNtfServer) import Simplex.Messaging.Notifications.Server.Env (NtfServerConfig (..)) @@ -15,8 +16,13 @@ cfgPath = "/etc/opt/simplex-notifications" logPath :: FilePath logPath = "/var/opt/simplex-notifications" +logCfg :: LogConfig +logCfg = LogConfig {lc_file = Nothing, lc_stderr = True} + main :: IO () -main = protocolServerCLI ntfServerCLIConfig runNtfServer +main = do + setLogLevel LogDebug -- TODO change to LogError in production + withGlobalLogging logCfg $ protocolServerCLI ntfServerCLIConfig runNtfServer ntfServerCLIConfig :: ServerCLIConfig NtfServerConfig ntfServerCLIConfig = diff --git a/src/Simplex/Messaging/Agent.hs b/src/Simplex/Messaging/Agent.hs index 705b3d248..4ed89185a 100644 --- a/src/Simplex/Messaging/Agent.hs +++ b/src/Simplex/Messaging/Agent.hs @@ -63,7 +63,7 @@ import Control.Monad.Except import Control.Monad.IO.Unlift (MonadUnliftIO) import Control.Monad.Reader import Crypto.Random (MonadRandom) -import Data.Bifunctor (first, second) +import Data.Bifunctor (bimap, first, second) import Data.ByteString.Char8 (ByteString) import Data.Composition ((.:), (.:.)) import Data.Functor (($>)) @@ -87,7 +87,7 @@ import qualified Simplex.Messaging.Crypto as C import qualified Simplex.Messaging.Crypto.Ratchet as CR import Simplex.Messaging.Encoding import Simplex.Messaging.Notifications.Client -import Simplex.Messaging.Notifications.Protocol (DeviceToken, NtfRegCode, NtfTknStatus (..)) +import Simplex.Messaging.Notifications.Protocol (DeviceToken, NtfRegCode (NtfRegCode), NtfTknStatus (..)) import Simplex.Messaging.Parsers (parse) import Simplex.Messaging.Protocol (BrokerMsg, MsgBody) import qualified Simplex.Messaging.Protocol as SMP @@ -165,8 +165,8 @@ registerNtfToken :: AgentErrorMonad m => AgentClient -> DeviceToken -> m () registerNtfToken c = withAgentEnv c . registerNtfToken' c -- | Verify device notifications token -verifyNtfToken :: AgentErrorMonad m => AgentClient -> DeviceToken -> NtfRegCode -> m () -verifyNtfToken c = withAgentEnv c .: verifyNtfToken' c +verifyNtfToken :: AgentErrorMonad m => AgentClient -> DeviceToken -> ByteString -> C.CbNonce -> m () +verifyNtfToken c = withAgentEnv c .:. verifyNtfToken' c -- | Enable/disable periodic notifications enableNtfCron :: AgentErrorMonad m => AgentClient -> DeviceToken -> Word16 -> m () @@ -523,60 +523,78 @@ setSMPServers' c servers = do registerNtfToken' :: forall m. AgentMonad m => AgentClient -> DeviceToken -> m () registerNtfToken' c deviceToken = withStore (`getDeviceNtfToken` deviceToken) >>= \case - Just tkn@NtfToken {ntfTokenId, ntfTknAction} -> case (ntfTokenId, ntfTknAction) of - (Nothing, Just (NTARegister ntfPubKey)) -> registerToken tkn ntfPubKey - -- TODO request verification code again in case there is registration, but no verification code in DB - probably after some timeout? - (Just tknId, Just (NTAVerify code)) -> - t tkn (NTActive, Just NTACheck) $ agentNtfVerifyToken c tknId tkn code - (Just tknId, Just (NTACron interval)) -> - t tkn (NTActive, Just NTACheck) $ agentNtfEnableCron c tknId tkn interval - (Just _tknId, Just NTACheck) -> pure () - (Just tknId, Just NTADelete) -> - t tkn (NTExpired, Nothing) $ agentNtfDeleteToken c tknId tkn - _ -> pure () + (Just tkn@NtfToken {ntfTokenId, ntfTknStatus, ntfTknAction}, prevTokens) -> do + mapM_ (deleteToken_ c) prevTokens + case (ntfTokenId, ntfTknAction) of + (Nothing, Just NTARegister) -> registerToken tkn + -- TODO minimal time before repeat registration + (Just _, Nothing) -> when (ntfTknStatus == NTRegistered) $ registerToken tkn + (Just tknId, Just (NTAVerify code)) -> + t tkn (NTActive, Just NTACheck) $ agentNtfVerifyToken c tknId tkn code + (Just tknId, Just (NTACron interval)) -> + t tkn (cronSuccess interval) $ agentNtfEnableCron c tknId tkn interval + (Just _tknId, Just NTACheck) -> pure () -- TODO + -- agentNtfCheckToken c tknId tkn >>= \case + (Just tknId, Just NTADelete) -> do + agentNtfDeleteToken c tknId tkn + withStore $ \st -> removeNtfToken st tkn + _ -> pure () _ -> getNtfServer c >>= \case Just ntfServer -> asks (cmdSignAlg . config) >>= \case C.SignAlg a -> do - (ntfPubKey, ntfPrivKey) <- liftIO $ C.generateSignatureKeyPair a - let tkn = newNtfToken deviceToken ntfServer ntfPrivKey ntfPubKey + tknKeys <- liftIO $ C.generateSignatureKeyPair a + dhKeys <- liftIO C.generateKeyPair' + let tkn = newNtfToken deviceToken ntfServer tknKeys dhKeys withStore $ \st -> createNtfToken st tkn - registerToken tkn ntfPubKey + registerToken tkn _ -> throwError $ CMD PROHIBITED where t tkn = withToken tkn Nothing - registerToken :: NtfToken -> C.APublicVerifyKey -> m () - registerToken tkn ntfPubKey = do - (pubDhKey, privDhKey) <- liftIO C.generateKeyPair' + registerToken :: NtfToken -> m () + registerToken tkn@NtfToken {ntfPubKey, ntfDhKeys = (pubDhKey, privDhKey)} = do (tknId, srvPubDhKey) <- agentNtfRegisterToken c tkn ntfPubKey pubDhKey let dhSecret = C.dh' srvPubDhKey privDhKey withStore $ \st -> updateNtfTokenRegistration st tkn tknId dhSecret -verifyNtfToken' :: AgentMonad m => AgentClient -> DeviceToken -> NtfRegCode -> m () -verifyNtfToken' c deviceToken code = +-- TODO decrypt verification code +verifyNtfToken' :: AgentMonad m => AgentClient -> DeviceToken -> ByteString -> C.CbNonce -> m () +verifyNtfToken' c deviceToken code nonce = withStore (`getDeviceNtfToken` deviceToken) >>= \case - Just tkn@NtfToken {ntfTokenId = Just tknId} -> - withToken tkn (Just (NTConfirmed, NTAVerify code)) (NTActive, Just NTACheck) $ - agentNtfVerifyToken c tknId tkn code + (Just tkn@NtfToken {ntfTokenId = Just tknId, ntfDhSecret = Just dhSecret}, _) -> do + code' <- liftEither . bimap cryptoError NtfRegCode $ C.cbDecrypt dhSecret nonce code + withToken tkn (Just (NTConfirmed, NTAVerify code')) (NTActive, Just NTACheck) $ + agentNtfVerifyToken c tknId tkn code' _ -> throwError $ CMD PROHIBITED enableNtfCron' :: AgentMonad m => AgentClient -> DeviceToken -> Word16 -> m () -enableNtfCron' c deviceToken interval = +enableNtfCron' c deviceToken interval = do + when (interval < 20) . throwError $ CMD PROHIBITED withStore (`getDeviceNtfToken` deviceToken) >>= \case - Just tkn@NtfToken {ntfTokenId = Just tknId, ntfTknStatus = NTActive} -> - withToken tkn (Just (NTActive, NTACron interval)) (NTActive, Just NTACheck) $ + (Just tkn@NtfToken {ntfTokenId = Just tknId, ntfTknStatus = NTActive}, _) -> + withToken tkn (Just (NTActive, NTACron interval)) (cronSuccess interval) $ agentNtfEnableCron c tknId tkn interval _ -> throwError $ CMD PROHIBITED +cronSuccess :: Word16 -> (NtfTknStatus, Maybe NtfTknAction) +cronSuccess interval + | interval == 0 = (NTActive, Just NTACheck) + | otherwise = (NTActive, Just $ NTACron interval) + deleteNtfToken' :: AgentMonad m => AgentClient -> DeviceToken -> m () deleteNtfToken' c deviceToken = withStore (`getDeviceNtfToken` deviceToken) >>= \case - Just tkn@NtfToken {ntfTokenId = Just tknId, ntfTknStatus} -> - withToken tkn (Just (ntfTknStatus, NTADelete)) (NTExpired, Nothing) $ - agentNtfDeleteToken c tknId tkn + (Just tkn, _) -> deleteToken_ c tkn _ -> throwError $ CMD PROHIBITED +deleteToken_ :: AgentMonad m => AgentClient -> NtfToken -> m () +deleteToken_ c tkn@NtfToken {ntfTokenId, ntfTknStatus} = do + forM_ ntfTokenId $ \tknId -> do + withStore $ \st -> updateNtfToken st tkn ntfTknStatus (Just NTADelete) + agentNtfDeleteToken c tknId tkn + withStore $ \st -> removeNtfToken st tkn + withToken :: AgentMonad m => NtfToken -> Maybe (NtfTknStatus, NtfTknAction) -> (NtfTknStatus, Maybe NtfTknAction) -> m a -> m a withToken tkn from_ (toStatus, toAction_) f = do forM_ from_ $ \(status, action) -> withStore $ \st -> updateNtfToken st tkn status (Just action) diff --git a/src/Simplex/Messaging/Agent/Client.hs b/src/Simplex/Messaging/Agent/Client.hs index 8d51a3c9b..8108f36fd 100644 --- a/src/Simplex/Messaging/Agent/Client.hs +++ b/src/Simplex/Messaging/Agent/Client.hs @@ -25,6 +25,7 @@ module Simplex.Messaging.Agent.Client sendAgentMessage, agentNtfRegisterToken, agentNtfVerifyToken, + agentNtfCheckToken, agentNtfDeleteToken, agentNtfEnableCron, agentCbEncrypt, @@ -478,6 +479,10 @@ agentNtfVerifyToken :: AgentMonad m => AgentClient -> NtfTokenId -> NtfToken -> agentNtfVerifyToken c tknId NtfToken {ntfServer, ntfPrivKey} code = withLogClient c ntfServer tknId "TVFY" $ \ntf -> ntfVerifyToken ntf ntfPrivKey tknId code +agentNtfCheckToken :: AgentMonad m => AgentClient -> NtfTokenId -> NtfToken -> m NtfTknStatus +agentNtfCheckToken c tknId NtfToken {ntfServer, ntfPrivKey} = + withLogClient c ntfServer tknId "TCHK" $ \ntf -> ntfCheckToken ntf ntfPrivKey tknId + agentNtfDeleteToken :: AgentMonad m => AgentClient -> NtfTokenId -> NtfToken -> m () agentNtfDeleteToken c tknId NtfToken {ntfServer, ntfPrivKey} = withLogClient c ntfServer tknId "TDEL" $ \ntf -> ntfDeleteToken ntf ntfPrivKey tknId diff --git a/src/Simplex/Messaging/Agent/Store.hs b/src/Simplex/Messaging/Agent/Store.hs index 5a2c246ac..6931f53be 100644 --- a/src/Simplex/Messaging/Agent/Store.hs +++ b/src/Simplex/Messaging/Agent/Store.hs @@ -81,9 +81,10 @@ class Monad m => MonadAgentStore s m where createNtfToken :: s -> NtfToken -> m () -- TODO this should also return old tokens so that they are deleted from the server - getDeviceNtfToken :: s -> DeviceToken -> m (Maybe NtfToken) -- return current token if it exists + getDeviceNtfToken :: s -> DeviceToken -> m (Maybe NtfToken, [NtfToken]) -- return current token if it exists updateNtfTokenRegistration :: s -> NtfToken -> NtfTokenId -> C.DhSecretX25519 -> m () updateNtfToken :: s -> NtfToken -> NtfTknStatus -> Maybe NtfTknAction -> m () + removeNtfToken :: s -> NtfToken -> m () -- * Queue types diff --git a/src/Simplex/Messaging/Agent/Store/SQLite.hs b/src/Simplex/Messaging/Agent/Store/SQLite.hs index 52235f852..0400ef07e 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite.hs @@ -33,12 +33,12 @@ import Control.Exception (bracket) import Control.Monad.Except import Control.Monad.IO.Unlift (MonadUnliftIO) import Crypto.Random (ChaChaDRG, randomBytesGenerate) -import Data.Bifunctor (second) +import Data.Bifunctor (first, second) import Data.ByteString (ByteString) import qualified Data.ByteString.Base64.URL as U import Data.Char (toLower) import Data.Functor (($>)) -import Data.List (find, foldl') +import Data.List (find, foldl', partition) import qualified Data.Map.Strict as M import Data.Maybe (listToMaybe) import Data.Text (Text) @@ -567,35 +567,36 @@ instance (MonadUnliftIO m, MonadError StoreError m) => MonadAgentStore SQLiteSto DB.execute db "INSERT INTO skipped_messages (conn_id, header_key, msg_n, msg_key) VALUES (?, ?, ?, ?)" (connId, hk, msgN, mk) createNtfToken :: SQLiteStore -> NtfToken -> m () - createNtfToken st NtfToken {deviceToken = DeviceToken provider token, ntfServer = srv@ProtocolServer {host, port}, ntfTokenId, ntfPrivKey, ntfDhSecret, ntfTknStatus, ntfTknAction} = + createNtfToken st NtfToken {deviceToken = DeviceToken provider token, ntfServer = srv@ProtocolServer {host, port}, ntfTokenId, ntfPubKey, ntfPrivKey, ntfDhKeys = (ntfDhPubKey, ntfDhPrivKey), ntfDhSecret, ntfTknStatus, ntfTknAction} = liftIO . withTransaction st $ \db -> do upsertNtfServer_ db srv DB.execute db [sql| INSERT INTO ntf_tokens - (provider, device_token, ntf_host, ntf_port, tkn_id, tkn_priv_key, tkn_dh_secret, tkn_status, tkn_action) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) + (provider, device_token, ntf_host, ntf_port, tkn_id, tkn_pub_key, tkn_priv_key, tkn_pub_dh_key, tkn_priv_dh_key, tkn_dh_secret, tkn_status, tkn_action) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) |] - (provider, token, host, port, ntfTokenId, ntfPrivKey, ntfDhSecret, ntfTknStatus, ntfTknAction) + (provider, token, host, port, ntfTokenId, ntfPubKey, ntfPrivKey, ntfDhPubKey, ntfDhPrivKey, ntfDhSecret, ntfTknStatus, ntfTknAction) - getDeviceNtfToken :: SQLiteStore -> DeviceToken -> m (Maybe NtfToken) - getDeviceNtfToken st t@(DeviceToken provider token) = - liftIO . withTransaction st $ \db -> - fmap ntfToken . listToMaybe - <$> DB.query - db - [sql| - SELECT s.ntf_host, s.ntf_port, s.ntf_key_hash, - t.tkn_id, t.tkn_priv_key, t.tkn_dh_secret, t.tkn_status, t.tkn_action - FROM ntf_tokens t - JOIN ntf_servers s USING (ntf_host, ntf_port) - WHERE t.provider = ? AND t.device_token = ? - |] - (provider, token) + getDeviceNtfToken :: SQLiteStore -> DeviceToken -> m (Maybe NtfToken, [NtfToken]) + getDeviceNtfToken st t = + liftIO . withTransaction st $ \db -> do + tokens <- + map ntfToken + <$> DB.query_ + db + [sql| + SELECT s.ntf_host, s.ntf_port, s.ntf_key_hash, + t.tkn_id, t.tkn_pub_key, t.tkn_priv_key, t.tkn_pub_dh_key, t.tkn_priv_dh_key, t.tkn_dh_secret, t.tkn_status, t.tkn_action + FROM ntf_tokens t + JOIN ntf_servers s USING (ntf_host, ntf_port) + |] + pure . first listToMaybe $ partition ((t ==) . deviceToken) tokens where - ntfToken (host, port, keyHash, ntfTokenId, ntfPrivKey, ntfDhSecret, ntfTknStatus, ntfTknAction) = + ntfToken (host, port, keyHash, ntfTokenId, ntfPubKey, ntfPrivKey, ntfDhPubKey, ntfDhPrivKey, ntfDhSecret, ntfTknStatus, ntfTknAction) = let ntfServer = ProtocolServer {host, port, keyHash} - in NtfToken {deviceToken = t, ntfServer, ntfTokenId, ntfPrivKey, ntfDhSecret, ntfTknStatus, ntfTknAction} + ntfDhKeys = (ntfDhPubKey, ntfDhPrivKey) + in NtfToken {deviceToken = t, ntfServer, ntfTokenId, ntfPubKey, ntfPrivKey, ntfDhKeys, ntfDhSecret, ntfTknStatus, ntfTknAction} updateNtfTokenRegistration :: SQLiteStore -> NtfToken -> NtfTokenId -> C.DhSecretX25519 -> m () updateNtfTokenRegistration st NtfToken {deviceToken = DeviceToken provider token, ntfServer = ProtocolServer {host, port}} tknId ntfDhSecret = @@ -623,6 +624,17 @@ instance (MonadUnliftIO m, MonadError StoreError m) => MonadAgentStore SQLiteSto |] (tknStatus, tknAction, updatedAt, provider, token, host, port) + removeNtfToken :: SQLiteStore -> NtfToken -> m () + removeNtfToken st NtfToken {deviceToken = DeviceToken provider token, ntfServer = ProtocolServer {host, port}} = + liftIO . withTransaction st $ \db -> + DB.execute + db + [sql| + DELETE FROM ntf_tokens + WHERE provider = ? AND device_token = ? AND ntf_host = ? AND ntf_port = ? + |] + (provider, token, host, port) + -- * Auxiliary helpers instance ToField QueueStatus where toField = toField . serializeQueueStatus diff --git a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220322_notifications.hs b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220322_notifications.hs index 3587e05b1..4b93c0db9 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220322_notifications.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220322_notifications.hs @@ -23,7 +23,10 @@ CREATE TABLE ntf_tokens ( ntf_host TEXT NOT NULL, ntf_port TEXT NOT NULL, tkn_id BLOB, -- token ID assigned by notifications server - tkn_priv_key BLOB NOT NULL, -- private key to sign token commands + tkn_pub_key BLOB NOT NULL, -- client's public key to verify token commands (used by server, for repeat registraions) + tkn_priv_key BLOB NOT NULL, -- client's private key to sign token commands + tkn_pub_dh_key BLOB NOT NULL, -- client's public DH key (for repeat registraions) + tkn_priv_dh_key BLOB NOT NULL, -- client's private DH key (for repeat registraions) tkn_dh_secret BLOB, -- DH secret for e2e encryption of notifications tkn_status TEXT NOT NULL, tkn_action BLOB, diff --git a/src/Simplex/Messaging/Crypto.hs b/src/Simplex/Messaging/Crypto.hs index 9f275e805..7caa6dedb 100644 --- a/src/Simplex/Messaging/Crypto.hs +++ b/src/Simplex/Messaging/Crypto.hs @@ -52,6 +52,7 @@ module Simplex.Messaging.Crypto CryptoPublicKey (..), CryptoPrivateKey (..), KeyPair, + ASignatureKeyPair, DhSecret (..), DhSecretX25519, ADhSecret (..), @@ -870,6 +871,14 @@ cbDecrypt secret (CbNonce nonce) packet newtype CbNonce = CbNonce {unCbNonce :: ByteString} deriving (Show) +instance StrEncoding CbNonce where + strEncode (CbNonce s) = strEncode s + strP = cbNonce <$> strP + +instance ToJSON CbNonce where + toJSON = strToJSON + toEncoding = strToJEncoding + cbNonce :: ByteString -> CbNonce cbNonce s | len == 24 = CbNonce s diff --git a/src/Simplex/Messaging/Notifications/Client.hs b/src/Simplex/Messaging/Notifications/Client.hs index 572831c8b..f7dc4fc52 100644 --- a/src/Simplex/Messaging/Notifications/Client.hs +++ b/src/Simplex/Messaging/Notifications/Client.hs @@ -33,6 +33,12 @@ ntfRegisterToken c pKey newTkn = ntfVerifyToken :: NtfClient -> C.APrivateSignKey -> NtfTokenId -> NtfRegCode -> ExceptT ProtocolClientError IO () ntfVerifyToken c pKey tknId code = okNtfCommand (TVFY code) c pKey tknId +ntfCheckToken :: NtfClient -> C.APrivateSignKey -> NtfTokenId -> ExceptT ProtocolClientError IO NtfTknStatus +ntfCheckToken c pKey tknId = + sendNtfCommand c (Just pKey) tknId TCHK >>= \case + NRTkn stat -> pure stat + _ -> throwE PCEUnexpectedResponse + ntfDeleteToken :: NtfClient -> C.APrivateSignKey -> NtfTokenId -> ExceptT ProtocolClientError IO () ntfDeleteToken = okNtfCommand TDEL @@ -48,7 +54,7 @@ ntfCreateSubsciption c pKey newSub = ntfCheckSubscription :: NtfClient -> C.APrivateSignKey -> NtfSubscriptionId -> ExceptT ProtocolClientError IO NtfSubStatus ntfCheckSubscription c pKey subId = sendNtfCommand c (Just pKey) subId SCHK >>= \case - NRStat stat -> pure stat + NRSub stat -> pure stat _ -> throwE PCEUnexpectedResponse ntfDeleteSubscription :: NtfClient -> C.APrivateSignKey -> NtfSubscriptionId -> ExceptT ProtocolClientError IO () @@ -65,7 +71,7 @@ okNtfCommand cmd c pKey entId = _ -> throwE PCEUnexpectedResponse data NtfTknAction - = NTARegister C.APublicVerifyKey -- public key to send to the server + = NTARegister | NTAVerify NtfRegCode -- code to verify token | NTACheck | NTACron Word16 @@ -74,14 +80,14 @@ data NtfTknAction instance Encoding NtfTknAction where smpEncode = \case - NTARegister key -> smpEncode ('R', key) + NTARegister -> "R" NTAVerify code -> smpEncode ('V', code) NTACheck -> "C" NTACron interval -> smpEncode ('I', interval) NTADelete -> "D" smpP = A.anyChar >>= \case - 'R' -> NTARegister <$> smpP + 'R' -> pure NTARegister 'V' -> NTAVerify <$> smpP 'C' -> pure NTACheck 'I' -> NTACron <$> smpP @@ -96,8 +102,12 @@ data NtfToken = NtfToken { deviceToken :: DeviceToken, ntfServer :: NtfServer, ntfTokenId :: Maybe NtfTokenId, + -- | key used by the ntf server to verify transmissions + ntfPubKey :: C.APublicVerifyKey, -- | key used by the ntf client to sign transmissions ntfPrivKey :: C.APrivateSignKey, + -- | client's DH keys (to repeat registration if necessary) + ntfDhKeys :: C.KeyPair 'C.X25519, -- | shared DH secret used to encrypt/decrypt notifications e2e ntfDhSecret :: Maybe C.DhSecretX25519, -- | token status @@ -107,14 +117,16 @@ data NtfToken = NtfToken } deriving (Show) -newNtfToken :: DeviceToken -> NtfServer -> C.APrivateSignKey -> C.APublicVerifyKey -> NtfToken -newNtfToken deviceToken ntfServer ntfPrivKey ntfPubKey = +newNtfToken :: DeviceToken -> NtfServer -> C.ASignatureKeyPair -> C.KeyPair 'C.X25519 -> NtfToken +newNtfToken deviceToken ntfServer (ntfPubKey, ntfPrivKey) ntfDhKeys = NtfToken { deviceToken, ntfServer, ntfTokenId = Nothing, + ntfPubKey, ntfPrivKey, + ntfDhKeys, ntfDhSecret = Nothing, ntfTknStatus = NTNew, - ntfTknAction = Just $ NTARegister ntfPubKey - } \ No newline at end of file + ntfTknAction = Just NTARegister + } diff --git a/src/Simplex/Messaging/Notifications/Protocol.hs b/src/Simplex/Messaging/Notifications/Protocol.hs index 17c6ece56..d5985d213 100644 --- a/src/Simplex/Messaging/Notifications/Protocol.hs +++ b/src/Simplex/Messaging/Notifications/Protocol.hs @@ -16,6 +16,7 @@ import Data.ByteString.Char8 (ByteString) import qualified Data.ByteString.Char8 as B import Data.Kind import Data.Maybe (isNothing) +import Data.Text.Encoding (decodeLatin1, encodeUtf8) import Data.Type.Equality import Data.Word (Word16) import Database.SQLite.Simple.FromField (FromField (..)) @@ -51,6 +52,7 @@ instance NtfEntityI 'Subscription where sNtfEntity = SSubscription data NtfCommandTag (e :: NtfEntity) where TNEW_ :: NtfCommandTag 'Token TVFY_ :: NtfCommandTag 'Token + TCHK_ :: NtfCommandTag 'Token TDEL_ :: NtfCommandTag 'Token TCRN_ :: NtfCommandTag 'Token SNEW_ :: NtfCommandTag 'Subscription @@ -66,6 +68,7 @@ instance NtfEntityI e => Encoding (NtfCommandTag e) where smpEncode = \case TNEW_ -> "TNEW" TVFY_ -> "TVFY" + TCHK_ -> "TCHK" TDEL_ -> "TDEL" TCRN_ -> "TCRN" SNEW_ -> "SNEW" @@ -82,6 +85,7 @@ instance ProtocolMsgTag NtfCmdTag where decodeTag = \case "TNEW" -> Just $ NCT SToken TNEW_ "TVFY" -> Just $ NCT SToken TVFY_ + "TCHK" -> Just $ NCT SToken TCHK_ "TDEL" -> Just $ NCT SToken TDEL_ "TCRN" -> Just $ NCT SToken TCRN_ "SNEW" -> Just $ NCT SSubscription SNEW_ @@ -147,6 +151,8 @@ data NtfCommand (e :: NtfEntity) where TNEW :: NewNtfEntity 'Token -> NtfCommand 'Token -- | verify token - uses e2e encrypted random string sent to the device via PN to confirm that the device has the token TVFY :: NtfRegCode -> NtfCommand 'Token + -- | check token status + TCHK :: NtfCommand 'Token -- | delete token - all subscriptions will be removed and no more notifications will be sent TDEL :: NtfCommand 'Token -- | enable periodic background notification to fetch the new messages - interval is in minutes, minimum is 20, 0 to disable @@ -171,6 +177,7 @@ instance NtfEntityI e => ProtocolEncoding (NtfCommand e) where encodeProtocol = \case TNEW newTkn -> e (TNEW_, ' ', newTkn) TVFY code -> e (TVFY_, ' ', code) + TCHK -> e TCHK_ TDEL -> e TDEL_ TCRN int -> e (TCRN_, ' ', int) SNEW newSub -> e (SNEW_, ' ', newSub) @@ -209,6 +216,7 @@ instance ProtocolEncoding NtfCmd where NtfCmd SToken <$> case tag of TNEW_ -> TNEW <$> _smpP TVFY_ -> TVFY <$> _smpP + TCHK_ -> pure TCHK TDEL_ -> pure TDEL TCRN_ -> TCRN <$> _smpP NCT SSubscription tag -> @@ -224,7 +232,8 @@ data NtfResponseTag = NRId_ | NROk_ | NRErr_ - | NRStat_ + | NRTkn_ + | NRSub_ | NRPong_ deriving (Show) @@ -233,7 +242,8 @@ instance Encoding NtfResponseTag where NRId_ -> "ID" NROk_ -> "OK" NRErr_ -> "ERR" - NRStat_ -> "STAT" + NRTkn_ -> "TKN" + NRSub_ -> "SUB" NRPong_ -> "PONG" smpP = messageTagP @@ -242,7 +252,8 @@ instance ProtocolMsgTag NtfResponseTag where "ID" -> Just NRId_ "OK" -> Just NROk_ "ERR" -> Just NRErr_ - "STAT" -> Just NRStat_ + "TKN" -> Just NRTkn_ + "SUB" -> Just NRSub_ "PONG" -> Just NRPong_ _ -> Nothing @@ -250,7 +261,8 @@ data NtfResponse = NRId NtfEntityId C.PublicKeyX25519 | NROk | NRErr ErrorType - | NRStat NtfSubStatus + | NRTkn NtfTknStatus + | NRSub NtfSubStatus | NRPong instance ProtocolEncoding NtfResponse where @@ -259,7 +271,8 @@ instance ProtocolEncoding NtfResponse where NRId entId dhKey -> e (NRId_, ' ', entId, dhKey) NROk -> e NROk_ NRErr err -> e (NRErr_, ' ', err) - NRStat stat -> e (NRStat_, ' ', stat) + NRTkn stat -> e (NRTkn_, ' ', stat) + NRSub stat -> e (NRSub_, ' ', stat) NRPong -> e NRPong_ where e :: Encoding a => a -> ByteString @@ -269,7 +282,8 @@ instance ProtocolEncoding NtfResponse where NRId_ -> NRId <$> _smpP <*> smpP NROk_ -> pure NROk NRErr_ -> NRErr <$> _smpP - NRStat_ -> NRStat <$> _smpP + NRTkn_ -> NRTkn <$> _smpP + NRSub_ -> NRSub <$> _smpP NRPong_ -> pure NRPong checkCredentials (_, _, entId, _) cmd = case cmd of @@ -301,22 +315,22 @@ instance Encoding SMPQueueNtf where (smpServer, notifierId, notifierKey) <- smpP pure $ SMPQueueNtf smpServer notifierId notifierKey -data PushProvider = PPApple +data PushProvider = PPApns deriving (Eq, Ord, Show) instance Encoding PushProvider where smpEncode = \case - PPApple -> "A" + PPApns -> "A" smpP = A.anyChar >>= \case - 'A' -> pure PPApple + 'A' -> pure PPApns _ -> fail "bad PushProvider" instance TextEncoding PushProvider where textEncode = \case - PPApple -> "apple" + PPApns -> "apple" textDecode = \case - "apple" -> Just PPApple + "apple" -> Just PPApns _ -> Nothing instance FromField PushProvider where fromField = fromTextField_ textDecode @@ -363,7 +377,7 @@ instance Encoding NtfSubStatus where "ACTIVE" -> pure NSActive "END" -> pure NSEnd "SMP_AUTH" -> pure NSSMPAuth - _ -> fail "bad NtfError" + _ -> fail "bad NtfSubStatus" data NtfTknStatus = -- | Token created in DB @@ -380,26 +394,27 @@ data NtfTknStatus NTExpired deriving (Eq, Show) -instance TextEncoding NtfTknStatus where - textEncode = \case - NTNew -> "new" - NTRegistered -> "registered" - NTInvalid -> "invalid" - NTConfirmed -> "confirmed" - NTActive -> "active" - NTExpired -> "expired" - textDecode = \case - "new" -> Just NTNew - "registered" -> Just NTRegistered - "invalid" -> Just NTInvalid - "confirmed" -> Just NTConfirmed - "active" -> Just NTActive - "expired" -> Just NTExpired - _ -> Nothing +instance Encoding NtfTknStatus where + smpEncode = \case + NTNew -> "NEW" + NTRegistered -> "REGISTERED" + NTInvalid -> "INVALID" + NTConfirmed -> "CONFIRMED" + NTActive -> "ACTIVE" + NTExpired -> "EXPIRED" + smpP = + A.takeTill (== ' ') >>= \case + "NEW" -> pure NTNew + "REGISTERED" -> pure NTRegistered + "INVALID" -> pure NTInvalid + "CONFIRMED" -> pure NTConfirmed + "ACTIVE" -> pure NTActive + "EXPIRED" -> pure NTExpired + _ -> fail "bad NtfTknStatus" -instance FromField NtfTknStatus where fromField = fromTextField_ textDecode +instance FromField NtfTknStatus where fromField = fromTextField_ $ either (const Nothing) Just . smpDecode . encodeUtf8 -instance ToField NtfTknStatus where toField = toField . textEncode +instance ToField NtfTknStatus where toField = toField . decodeLatin1 . smpEncode checkEntity :: forall t e e'. (NtfEntityI e, NtfEntityI e') => t e' -> Either String (t e) checkEntity c = case testEquality (sNtfEntity @e) (sNtfEntity @e') of diff --git a/src/Simplex/Messaging/Notifications/Server.hs b/src/Simplex/Messaging/Notifications/Server.hs index 942b2db42..5d81b3715 100644 --- a/src/Simplex/Messaging/Notifications/Server.hs +++ b/src/Simplex/Messaging/Notifications/Server.hs @@ -10,11 +10,13 @@ module Simplex.Messaging.Notifications.Server where +import Control.Logger.Simple import Control.Monad.Except import Control.Monad.IO.Unlift (MonadUnliftIO) import Control.Monad.Reader import Crypto.Random (MonadRandom) import Data.ByteString.Char8 (ByteString) +import qualified Data.Text as T import Network.Socket (ServiceName) import Simplex.Messaging.Client.Agent import qualified Simplex.Messaging.Crypto as C @@ -26,9 +28,12 @@ import Simplex.Messaging.Notifications.Transport import Simplex.Messaging.Protocol (ErrorType (..), SignedTransmission, Transmission, encodeTransmission, tGet, tPut) import qualified Simplex.Messaging.Protocol as SMP import Simplex.Messaging.Server +import qualified Simplex.Messaging.TMap as TM import Simplex.Messaging.Transport (ATransport (..), THandle (..), TProxy, Transport) import Simplex.Messaging.Transport.Server (runTransportServer) import Simplex.Messaging.Util +import UnliftIO (async, uninterruptibleCancel) +import UnliftIO.Concurrent (threadDelay) import UnliftIO.Exception import UnliftIO.STM @@ -106,7 +111,7 @@ ntfSubscriber NtfSubscriber {subQ, smpAgent = ca@SMPClientAgent {msgQ, agentQ}} ntfPush :: MonadUnliftIO m => NtfPushServer -> m () ntfPush s@NtfPushServer {pushQ} = liftIO . forever . runExceptT $ do (tkn@NtfTknData {token = DeviceToken pp _, tknStatus}, ntf) <- atomically (readTBQueue pushQ) - liftIO $ putStrLn $ "sending push notification to " <> show pp + logDebug $ "sending push notification to " <> T.pack (show pp) status <- readTVarIO tknStatus case (status, ntf) of (_, PNVerification _) -> do @@ -116,13 +121,13 @@ ntfPush s@NtfPushServer {pushQ} = liftIO . forever . runExceptT $ do (NTActive, PNCheckMessages) -> do deliverNotification pp tkn ntf _ -> do - liftIO $ putStrLn "bad notification token status" + logError "bad notification token status" where deliverNotification :: PushProvider -> PushProviderClient deliverNotification pp tkn ntf = do deliver <- liftIO $ getPushClient s pp -- TODO retry later based on the error - deliver tkn ntf `catchError` \e -> liftIO (putStrLn $ "Push provider error (" <> show pp <> "): " <> show e) >> throwError e + deliver tkn ntf `catchError` \e -> logError (T.pack $ "Push provider error (" <> show pp <> "): " <> show e) >> throwError e runNtfClientTransport :: (Transport c, MonadUnliftIO m, MonadReader NtfEnv m) => THandle c -> m () runNtfClientTransport th@THandle {sessionId} = do @@ -138,14 +143,14 @@ clientDisconnected NtfServerClient {connected} = atomically $ writeTVar connecte receive :: (Transport c, MonadUnliftIO m, MonadReader NtfEnv m) => THandle c -> NtfServerClient -> m () receive th NtfServerClient {rcvQ, sndQ} = forever $ do - t@(_, _, (corrId, subId, cmdOrError)) <- tGet th - liftIO $ putStrLn "receive" + t@(_, _, (corrId, entId, cmdOrError)) <- tGet th + logDebug "received transmission" case cmdOrError of - Left e -> write sndQ (corrId, subId, NRErr e) + Left e -> write sndQ (corrId, entId, NRErr e) Right cmd -> verifyNtfTransmission t cmd >>= \case VRVerified req -> write rcvQ req - VRFailed -> write sndQ (corrId, subId, NRErr AUTH) + VRFailed -> write sndQ (corrId, entId, NRErr AUTH) where write q t = atomically $ writeTBQueue q t @@ -159,41 +164,31 @@ data VerificationResult = VRVerified NtfRequest | VRFailed verifyNtfTransmission :: forall m. (MonadUnliftIO m, MonadReader NtfEnv m) => SignedTransmission NtfCmd -> NtfCmd -> m VerificationResult verifyNtfTransmission (sig_, signed, (corrId, entId, _)) cmd = do + st <- asks store case cmd of - NtfCmd SToken (TNEW n@(NewNtfTkn _ k _)) -> - -- TODO check that token is not already in store, if it is - verify that the saved key is the same + NtfCmd SToken c@(TNEW n@(NewNtfTkn _ k _)) -> do + r_ <- atomically $ getNtfToken st entId pure $ if verifyCmdSignature sig_ signed k - then VRVerified (NtfReqNew corrId (ANE SToken n)) + then case r_ of + Just r@(NtfTkn NtfTknData {tknVerifyKey}) + | k == tknVerifyKey -> tknCmd r c + | otherwise -> VRFailed + _ -> VRVerified (NtfReqNew corrId (ANE SToken n)) else VRFailed NtfCmd SToken c -> do - st <- asks store - atomically (getNtfToken st entId) >>= \case - Just r@(NtfTkn NtfTknData {tknVerifyKey}) -> - pure $ - if verifyCmdSignature sig_ signed tknVerifyKey - then VRVerified (NtfReqCmd SToken r (corrId, entId, c)) - else VRFailed - _ -> pure VRFailed -- TODO dummy verification + r_ <- atomically $ getNtfToken st entId + pure $ case r_ of + Just r@(NtfTkn NtfTknData {tknVerifyKey}) + | verifyCmdSignature sig_ signed tknVerifyKey -> tknCmd r c + | otherwise -> VRFailed + _ -> maybe False (dummyVerifyCmd signed) sig_ `seq` VRFailed _ -> pure VRFailed - --- do --- st <- asks store --- case cmd of --- NCSubCreate tokenId smpQueue -> verifyCreateCmd verifyKey newSub <$> atomically (getNtfSubViaSMPQueue st smpQueue) --- _ -> verifySubCmd <$> atomically (getNtfSub st subId) --- where --- verifyCreateCmd k newSub sub_ --- | verifyCmdSignature sig_ signed k = case sub_ of --- Just sub -> if k == subVerifyKey sub then VRCommand sub else VRFail --- _ -> VRCreate newSub --- | otherwise = VRFail --- verifySubCmd = \case --- Just sub -> if verifyCmdSignature sig_ signed $ subVerifyKey sub then VRCommand sub else VRFail --- _ -> maybe False (dummyVerifyCmd signed) sig_ `seq` VRFail + where + tknCmd r c = VRVerified (NtfReqCmd SToken r (corrId, entId, c)) client :: forall m. (MonadUnliftIO m, MonadReader NtfEnv m) => NtfServerClient -> NtfSubscriber -> NtfPushServer -> m () -client NtfServerClient {rcvQ, sndQ} NtfSubscriber {subQ = _} NtfPushServer {pushQ} = +client NtfServerClient {rcvQ, sndQ} NtfSubscriber {subQ = _} NtfPushServer {pushQ, intervalNotifiers} = forever $ atomically (readTBQueue rcvQ) >>= processCommand @@ -202,30 +197,68 @@ client NtfServerClient {rcvQ, sndQ} NtfSubscriber {subQ = _} NtfPushServer {push processCommand :: NtfRequest -> m (Transmission NtfResponse) processCommand = \case NtfReqNew corrId (ANE SToken newTkn@(NewNtfTkn _ _ dhPubKey)) -> do - liftIO $ putStrLn "TNEW" + logDebug "TNEW - new token" st <- asks store - (srvDhPubKey, srvDrivDhKey) <- liftIO C.generateKeyPair' - let dhSecret = C.dh' dhPubKey srvDrivDhKey + ks@(srvDhPubKey, srvDhPrivKey) <- liftIO C.generateKeyPair' + let dhSecret = C.dh' dhPubKey srvDhPrivKey tknId <- getId regCode <- getRegCode atomically $ do - tkn <- mkNtfTknData newTkn dhSecret regCode + tkn <- mkNtfTknData newTkn ks dhSecret regCode addNtfToken st tknId tkn writeTBQueue pushQ (tkn, PNVerification regCode) pure (corrId, "", NRId tknId srvDhPubKey) - NtfReqCmd SToken (NtfTkn NtfTknData {tknStatus, tknRegCode}) (corrId, tknId, cmd) -> do + NtfReqCmd SToken (NtfTkn tkn@NtfTknData {tknStatus, tknRegCode, tknDhSecret, tknDhKeys = (srvDhPubKey, srvDhPrivKey)}) (corrId, tknId, cmd) -> do status <- readTVarIO tknStatus (corrId,tknId,) <$> case cmd of - TNEW _newTkn -> do - liftIO $ putStrLn "TNEW'" - pure NROk -- TODO when duplicate token sent + TNEW (NewNtfTkn _ _ dhPubKey) -> do + logDebug "TNEW - registered token" + let dhSecret = C.dh' dhPubKey srvDhPrivKey + -- it is required that DH secret is the same, to avoid failed verifications if notification is delaying + if tknDhSecret == dhSecret + then do + atomically $ writeTBQueue pushQ (tkn, PNVerification tknRegCode) + pure $ NRId tknId srvDhPubKey + else pure $ NRErr AUTH TVFY code -- this allows repeated verification for cases when client connection dropped before server response | (status == NTRegistered || status == NTConfirmed || status == NTActive) && tknRegCode == code -> do + logDebug "TVFY - token verified" atomically $ writeTVar tknStatus NTActive pure NROk - | otherwise -> pure $ NRErr AUTH - TDEL -> pure NROk - TCRN _int -> pure NROk + | otherwise -> do + logDebug "TVFY - incorrect code or token status" + pure $ NRErr AUTH + TCHK -> pure $ NRTkn status + TDEL -> do + logDebug "TDEL" + st <- asks store + atomically $ deleteNtfToken st tknId + pure NROk + TCRN 0 -> + logDebug "TCRN 0" + >> atomically (TM.lookupDelete tknId intervalNotifiers) + >>= mapM_ (uninterruptibleCancel . action) + >> pure NROk + TCRN int + | int < 20 -> pure $ NRErr QUOTA + | otherwise -> do + logDebug "TCRN" + atomically (TM.lookup tknId intervalNotifiers) >>= \case + Nothing -> runIntervalNotifier int + Just IntervalNotifier {interval, action} -> + unless (interval == int) $ do + uninterruptibleCancel action + runIntervalNotifier int + pure NROk + where + runIntervalNotifier interval = do + action <- async . intervalNotifier $ fromIntegral interval * 1000000 * 60 + let notifier = IntervalNotifier {action, token = tkn, interval} + atomically $ TM.insert tknId notifier intervalNotifiers + where + intervalNotifier delay = forever $ do + threadDelay delay + atomically $ writeTBQueue pushQ (tkn, PNCheckMessages) NtfReqNew corrId (ANE SSubscription _newSub) -> pure (corrId, "", NROk) NtfReqCmd SSubscription _sub (corrId, subId, cmd) -> (corrId,subId,) <$> case cmd of diff --git a/src/Simplex/Messaging/Notifications/Server/Env.hs b/src/Simplex/Messaging/Notifications/Server/Env.hs index 67b83f1af..9c94c5ace 100644 --- a/src/Simplex/Messaging/Notifications/Server/Env.hs +++ b/src/Simplex/Messaging/Notifications/Server/Env.hs @@ -7,9 +7,11 @@ module Simplex.Messaging.Notifications.Server.Env where +import Control.Concurrent.Async (Async) import Control.Monad.IO.Unlift import Crypto.Random import Data.ByteString.Char8 (ByteString) +import Data.Word (Word16) import Data.X509.Validation (Fingerprint (..)) import Network.Socket import qualified Network.TLS as T @@ -59,7 +61,7 @@ newNtfServerEnv config@NtfServerConfig {subQSize, pushQSize, smpAgentCfg, apnsCo subscriber <- atomically $ newNtfSubscriber subQSize smpAgentCfg pushServer <- atomically $ newNtfPushServer pushQSize apnsConfig -- TODO not creating APNS client on start to pass CI test, has to be replaced with mock APNS server - -- void . liftIO $ newPushClient pushServer PPApple + -- void . liftIO $ newPushClient pushServer PPApns tlsServerParams <- liftIO $ loadTLSServerParams caCertificateFile certificateFile privateKeyFile Fingerprint fp <- liftIO $ loadFingerprint caCertificateFile pure NtfEnv {config, subscriber, pushServer, store, idsDrg, tlsServerParams, serverIdentity = C.KeyHash fp} @@ -78,20 +80,28 @@ newNtfSubscriber qSize smpAgentCfg = do data NtfPushServer = NtfPushServer { pushQ :: TBQueue (NtfTknData, PushNotification), pushClients :: TMap PushProvider PushProviderClient, + intervalNotifiers :: TMap NtfTokenId IntervalNotifier, apnsConfig :: APNSPushClientConfig } +data IntervalNotifier = IntervalNotifier + { action :: Async (), + token :: NtfTknData, + interval :: Word16 + } + newNtfPushServer :: Natural -> APNSPushClientConfig -> STM NtfPushServer newNtfPushServer qSize apnsConfig = do pushQ <- newTBQueue qSize pushClients <- TM.empty - pure NtfPushServer {pushQ, pushClients, apnsConfig} + intervalNotifiers <- TM.empty + pure NtfPushServer {pushQ, pushClients, intervalNotifiers, apnsConfig} newPushClient :: NtfPushServer -> PushProvider -> IO PushProviderClient newPushClient NtfPushServer {apnsConfig, pushClients} = \case - PPApple -> do + PPApns -> do c <- apnsPushProviderClient <$> createAPNSPushClient apnsConfig - atomically $ TM.insert PPApple c pushClients + atomically $ TM.insert PPApns c pushClients pure c getPushClient :: NtfPushServer -> PushProvider -> IO PushProviderClient diff --git a/src/Simplex/Messaging/Notifications/Server/Push/APNS.hs b/src/Simplex/Messaging/Notifications/Server/Push/APNS.hs index 8a67f9fcf..0143fd20b 100644 --- a/src/Simplex/Messaging/Notifications/Server/Push/APNS.hs +++ b/src/Simplex/Messaging/Notifications/Server/Push/APNS.hs @@ -1,12 +1,16 @@ +{-# LANGUAGE DeriveAnyClass #-} {-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE DuplicateRecordFields #-} -{-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# LANGUAGE LambdaCase #-} {-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE OverloadedStrings #-} +{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-} + +{-# HLINT ignore "Use newtype instead of data" #-} module Simplex.Messaging.Notifications.Server.Push.APNS where +import Control.Logger.Simple import Control.Monad.Except import Crypto.Hash.Algorithms (SHA256 (..)) import qualified Crypto.PubKey.ECC.ECDSA as EC @@ -20,6 +24,7 @@ import Data.Aeson (FromJSON, ToJSON, (.=)) import qualified Data.Aeson as J import qualified Data.Aeson.Encoding as JE import Data.Bifunctor (first) +import qualified Data.ByteString.Base64 as B64 import qualified Data.ByteString.Base64.URL as U import Data.ByteString.Builder (lazyByteString) import Data.ByteString.Char8 (ByteString) @@ -27,7 +32,6 @@ import qualified Data.ByteString.Lazy.Char8 as LB import qualified Data.CaseInsensitive as CI import Data.Int (Int64) import Data.Map.Strict (Map) -import Data.Maybe (fromMaybe) import Data.Text (Text) import qualified Data.Text as T import Data.Text.Encoding (decodeUtf8With) @@ -226,7 +230,7 @@ getApnsJWTToken APNSPushClient {apnsCfg = APNSPushClientConfig {appTeamId, token atomically $ writeTVar jwtToken t pure signedJWT' where - jwtTokenAge (JWTToken _ JWTClaims {iat}) = (iat -) . systemSeconds <$> getSystemTime + jwtTokenAge (JWTToken _ JWTClaims {iat}) = subtract iat . systemSeconds <$> getSystemTime mkApnsJWTToken :: Text -> JWTHeader -> EC.PrivateKey -> IO (JWTToken, SignedJWTToken) mkApnsJWTToken appTeamId jwtHeader privateKey = do @@ -256,15 +260,15 @@ apnsNotification :: NtfTknData -> C.CbNonce -> Int -> PushNotification -> Either apnsNotification NtfTknData {tknDhSecret} nonce paddedLen = \case PNVerification (NtfRegCode code) -> encrypt code $ \code' -> - apn APNSBackground {contentAvailable = 1} . Just $ J.object ["verification" .= code'] + apn APNSBackground {contentAvailable = 1} . Just $ J.object ["verification" .= code', "nonce" .= nonce] PNMessage srv nId -> encrypt (strEncode srv <> "/" <> strEncode nId) $ \ntfQueue -> - apn apnMutableContent . Just $ J.object ["checkMessage" .= ntfQueue] + apn apnMutableContent . Just $ J.object ["checkMessage" .= ntfQueue, "nonce" .= nonce] PNAlert text -> Right $ apn (apnAlert $ APNSAlertText text) Nothing PNCheckMessages -> Right $ apn APNSBackground {contentAvailable = 1} . Just $ J.object ["checkMessages" .= True] where encrypt :: ByteString -> (Text -> APNSNotification) -> Either C.CryptoError APNSNotification - encrypt ntfData f = f . safeDecodeUtf8 . U.encode <$> C.cbEncrypt tknDhSecret nonce ntfData paddedLen + encrypt ntfData f = f . safeDecodeUtf8 . B64.encode <$> C.cbEncrypt tknDhSecret nonce ntfData paddedLen apn aps notificationData = APNSNotification {aps, notificationData} apnMutableContent = APNSMutableContent {mutableContent = 1, alert = APNSAlertText "Encrypted message or some other app event", category = Nothing} apnAlert alert = APNSAlert {alert, badge = Nothing, sound = Nothing, category = Nothing} @@ -300,40 +304,39 @@ data PushProviderError type PushProviderClient = NtfTknData -> PushNotification -> ExceptT PushProviderError IO () -newtype APNSErrorReponse = APNSErrorReponse {reason :: Text} +-- this is not a newtype on purpose to have a correct JSON encoding as a record +data APNSErrorReponse = APNSErrorReponse {reason :: Text} deriving (Generic, FromJSON) apnsPushProviderClient :: APNSPushClient -> PushProviderClient -apnsPushProviderClient c@APNSPushClient {nonceDrg, apnsCfg} tkn@NtfTknData {token = DeviceToken PPApple tknStr} pn = do +apnsPushProviderClient c@APNSPushClient {nonceDrg, apnsCfg} tkn@NtfTknData {token = DeviceToken PPApns tknStr} pn = do http2 <- liftHTTPS2 $ getApnsHTTP2Client c nonce <- atomically $ C.pseudoRandomCbNonce nonceDrg apnsNtf <- liftEither $ first PPCryptoError $ apnsNotification tkn nonce (paddedNtfLength apnsCfg) pn - liftIO $ putStrLn $ "APNS notification: " <> show apnsNtf req <- liftIO $ apnsRequest c tknStr apnsNtf - liftIO $ putStrLn $ "APNS request: " <> show req HTTP2Response {response, respBody} <- liftHTTPS2 $ sendRequest http2 req let status = H.responseStatus response - reason = fromMaybe "" $ J.decodeStrict' =<< respBody - liftIO $ putStrLn $ "APNS response: " <> show status <> " " <> T.unpack reason - result status reason + reason' = maybe "?" reason $ J.decodeStrict' respBody + logDebug $ "APNS response: " <> T.pack (show status) <> " " <> reason' + result status reason' where result :: Maybe Status -> Text -> ExceptT PushProviderError IO () - result status reason + result status reason' | status == Just N.ok200 = pure () | status == Just N.badRequest400 = - case reason of + case reason' of "BadDeviceToken" -> throwError PPTokenInvalid "DeviceTokenNotForTopic" -> throwError PPTokenInvalid "TopicDisallowed" -> throwError PPPermanentError - _ -> err status reason - | status == Just N.forbidden403 = case reason of + _ -> err status reason' + | status == Just N.forbidden403 = case reason' of "ExpiredProviderToken" -> throwError PPPermanentError -- there should be no point retrying it as the token was refreshed "InvalidProviderToken" -> throwError PPPermanentError - _ -> err status reason + _ -> err status reason' | status == Just N.gone410 = throwError PPTokenInvalid | status == Just N.serviceUnavailable503 = liftIO (disconnectApnsHTTP2Client c) >> throwError PPRetryLater -- Just tooManyRequests429 -> TODO TooManyRequests - too many requests for the same token - | otherwise = err status reason + | otherwise = err status reason' err :: Maybe Status -> Text -> ExceptT PushProviderError IO () err s r = throwError $ PPResponseError s r liftHTTPS2 a = ExceptT $ first PPConnection <$> a diff --git a/src/Simplex/Messaging/Notifications/Server/Subscriptions.hs b/src/Simplex/Messaging/Notifications/Server/Subscriptions.hs index 366ba26c2..9123a690f 100644 --- a/src/Simplex/Messaging/Notifications/Server/Subscriptions.hs +++ b/src/Simplex/Messaging/Notifications/Server/Subscriptions.hs @@ -28,14 +28,15 @@ data NtfTknData = NtfTknData { token :: DeviceToken, tknStatus :: TVar NtfTknStatus, tknVerifyKey :: C.APublicVerifyKey, + tknDhKeys :: C.KeyPair 'C.X25519, tknDhSecret :: C.DhSecretX25519, tknRegCode :: NtfRegCode } -mkNtfTknData :: NewNtfEntity 'Token -> C.DhSecretX25519 -> NtfRegCode -> STM NtfTknData -mkNtfTknData (NewNtfTkn token tknVerifyKey _) tknDhSecret tknRegCode = do +mkNtfTknData :: NewNtfEntity 'Token -> C.KeyPair 'C.X25519 -> C.DhSecretX25519 -> NtfRegCode -> STM NtfTknData +mkNtfTknData (NewNtfTkn token tknVerifyKey _) tknDhKeys tknDhSecret tknRegCode = do tknStatus <- newTVar NTRegistered - pure NtfTknData {token, tknStatus, tknVerifyKey, tknDhSecret, tknRegCode} + pure NtfTknData {token, tknStatus, tknVerifyKey, tknDhKeys, tknDhSecret, tknRegCode} -- data NtfSubscriptionsStore = NtfSubscriptionsStore @@ -64,8 +65,12 @@ getNtfToken st tknId = NtfTkn <$$> TM.lookup tknId (tokens st) addNtfToken :: NtfStore -> NtfTokenId -> NtfTknData -> STM () addNtfToken st tknId tkn@NtfTknData {token} = do - TM.insert tknId tkn (tokens st) - TM.insert token tknId (tokenIds st) + TM.insert tknId tkn $ tokens st + TM.insert token tknId $ tokenIds st + +deleteNtfToken :: NtfStore -> NtfTokenId -> STM () +deleteNtfToken st tknId = do + TM.lookupDelete tknId (tokens st) >>= mapM_ (\NtfTknData {token} -> TM.delete token $ tokenIds st) -- getNtfRec :: NtfStore -> SNtfEntity e -> NtfEntityId -> STM (Maybe (NtfEntityRec e)) -- getNtfRec st ent entId = case ent of diff --git a/src/Simplex/Messaging/Transport/Client/HTTP2.hs b/src/Simplex/Messaging/Transport/Client/HTTP2.hs index 8e3a35741..2d019abd3 100644 --- a/src/Simplex/Messaging/Transport/Client/HTTP2.hs +++ b/src/Simplex/Messaging/Transport/Client/HTTP2.hs @@ -9,11 +9,13 @@ module Simplex.Messaging.Transport.Client.HTTP2 where import Control.Concurrent.Async import Control.Exception (IOException, catch, finally) import qualified Control.Exception as E +import Control.Logger.Simple (logDebug) import Control.Monad.Except import Data.ByteString.Char8 (ByteString) import qualified Data.ByteString.Char8 as B import Data.Default (def) import Data.Maybe (isNothing) +import qualified Data.Text as T import qualified Data.X509.CertificateStore as XS import Foreign (mallocBytes) import Network.HPACK (BufferSize, HeaderTable) @@ -41,13 +43,12 @@ data HTTPS2Client = HTTPS2Client data HTTP2Response = HTTP2Response { response :: Response, - respBody :: Maybe ByteString, + respBody :: ByteString, respTrailers :: Maybe HeaderTable } data HTTP2SClientConfig = HTTP2SClientConfig { qSize :: Natural, - maxBody :: Int, connTimeout :: Int, tcpKeepAlive :: Maybe KeepAliveOpts, caStoreFile :: FilePath, @@ -59,8 +60,7 @@ defaultHTTP2SClientConfig :: HTTP2SClientConfig defaultHTTP2SClientConfig = HTTP2SClientConfig { qSize = 64, - maxBody = 500000, - connTimeout = 5000000, + connTimeout = 10000000, tcpKeepAlive = Nothing, caStoreFile = "/etc/ssl/cert.pem", suportedTLSParams = @@ -112,24 +112,14 @@ getHTTPS2Client host port config@HTTP2SClientConfig {tcpKeepAlive, connTimeout, (req, respVar) <- atomically $ readTBQueue reqQ sendReq req $ \r -> do let writeResp respBody respTrailers = atomically $ putTMVar respVar HTTP2Response {response = r, respBody, respTrailers} - case H.responseBodySize r of - Just sz -> - if sz <= maxBody config - then do - respBody <- getResponseBody r "" sz - respTrailers <- join <$> mapM (const $ H.getResponseTrailers r) respBody - writeResp respBody respTrailers - else writeResp Nothing Nothing - _ -> writeResp Nothing Nothing + respBody <- getResponseBody r "" + respTrailers <- H.getResponseTrailers r + writeResp respBody respTrailers - getResponseBody :: Response -> ByteString -> Int -> IO (Maybe ByteString) - getResponseBody r s sz = - H.getResponseBodyChunk r >>= \chunk -> do - if chunk == "" - then pure (if B.length s == sz then Just s else Nothing) - else do - let s' = s <> chunk - if B.length s' > sz then pure Nothing else getResponseBody r s' sz + getResponseBody :: Response -> ByteString -> IO ByteString + getResponseBody r s = + H.getResponseBodyChunk r >>= \chunk -> + if B.null chunk then pure s else getResponseBody r $ s <> chunk -- | Disconnects client from the server and terminates client threads. closeHTTPS2Client :: HTTPS2Client -> IO () diff --git a/tests/AgentTests/FunctionalAPITests.hs b/tests/AgentTests/FunctionalAPITests.hs index a37bcff19..a08e72623 100644 --- a/tests/AgentTests/FunctionalAPITests.hs +++ b/tests/AgentTests/FunctionalAPITests.hs @@ -15,7 +15,7 @@ import SMPClient (testPort, withSmpServer, withSmpServerStoreLogOn) import Simplex.Messaging.Agent import Simplex.Messaging.Agent.Env.SQLite (AgentConfig (..)) import Simplex.Messaging.Agent.Protocol -import Simplex.Messaging.Notifications.Protocol (DeviceToken (DeviceToken), PushProvider (PPApple)) +import Simplex.Messaging.Notifications.Protocol (DeviceToken (..), PushProvider (..)) import Simplex.Messaging.Protocol (ErrorType (..), MsgBody) import Simplex.Messaging.Transport (ATransport (..)) import System.Timeout @@ -194,7 +194,7 @@ testNotificationToken :: IO () testNotificationToken = do alice <- getSMPAgentClient cfg initAgentServers Right () <- runExceptT $ do - registerNtfToken alice $ DeviceToken PPApple "abcd" + registerNtfToken alice $ DeviceToken PPApns "abcd" pure () exchangeGreetings :: AgentClient -> ConnId -> AgentClient -> ConnId -> ExceptT AgentErrorType IO () From 17888f89a904db008ff3a1e8839acea1213c55de Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Mon, 18 Apr 2022 08:57:55 +0100 Subject: [PATCH 008/131] test notification token with agent and notifications server (#353) * test notification token with agent and notifications server * notification server test with APNS mock * set environment variables in the test * use base64url encoding in encrypted notification data --- simplexmq.cabal | 5 +- .../Messaging/Notifications/Server/Env.hs | 3 +- .../Notifications/Server/Push/APNS.hs | 44 +++-- .../Messaging/Transport/Client/HTTP2.hs | 159 ------------------ src/Simplex/Messaging/Transport/HTTP2.hs | 36 ++++ .../Messaging/Transport/HTTP2/Client.hs | 128 ++++++++++++++ .../Messaging/Transport/HTTP2/Server.hs | 70 ++++++++ src/Simplex/Messaging/Transport/Server.hs | 8 +- tests/AgentTests/FunctionalAPITests.hs | 44 ++--- tests/AgentTests/NotificationTests.hs | 49 ++++++ tests/NtfClient.hs | 107 ++++++++++-- tests/NtfServerTests.hs | 2 +- tests/SMPAgentClient.hs | 12 +- tests/Test.hs | 8 +- tests/fixtures/AuthKey_H82WD9K9AQ.p8 | 6 + 15 files changed, 451 insertions(+), 230 deletions(-) delete mode 100644 src/Simplex/Messaging/Transport/Client/HTTP2.hs create mode 100644 src/Simplex/Messaging/Transport/HTTP2.hs create mode 100644 src/Simplex/Messaging/Transport/HTTP2/Client.hs create mode 100644 src/Simplex/Messaging/Transport/HTTP2/Server.hs create mode 100644 tests/AgentTests/NotificationTests.hs create mode 100644 tests/fixtures/AuthKey_H82WD9K9AQ.p8 diff --git a/simplexmq.cabal b/simplexmq.cabal index 6a64f8214..1472db2f4 100644 --- a/simplexmq.cabal +++ b/simplexmq.cabal @@ -74,7 +74,9 @@ library Simplex.Messaging.TMap Simplex.Messaging.Transport Simplex.Messaging.Transport.Client - Simplex.Messaging.Transport.Client.HTTP2 + Simplex.Messaging.Transport.HTTP2 + Simplex.Messaging.Transport.HTTP2.Client + Simplex.Messaging.Transport.HTTP2.Server Simplex.Messaging.Transport.KeepAlive Simplex.Messaging.Transport.Server Simplex.Messaging.Transport.WebSockets @@ -325,6 +327,7 @@ test-suite smp-server-test AgentTests.ConnectionRequestTests AgentTests.DoubleRatchetTests AgentTests.FunctionalAPITests + AgentTests.NotificationTests AgentTests.SQLiteTests CoreTests.EncodingTests CoreTests.ProtocolErrorTests diff --git a/src/Simplex/Messaging/Notifications/Server/Env.hs b/src/Simplex/Messaging/Notifications/Server/Env.hs index 9c94c5ace..ce00ab354 100644 --- a/src/Simplex/Messaging/Notifications/Server/Env.hs +++ b/src/Simplex/Messaging/Notifications/Server/Env.hs @@ -8,6 +8,7 @@ module Simplex.Messaging.Notifications.Server.Env where import Control.Concurrent.Async (Async) +import Control.Monad (void) import Control.Monad.IO.Unlift import Crypto.Random import Data.ByteString.Char8 (ByteString) @@ -61,7 +62,7 @@ newNtfServerEnv config@NtfServerConfig {subQSize, pushQSize, smpAgentCfg, apnsCo subscriber <- atomically $ newNtfSubscriber subQSize smpAgentCfg pushServer <- atomically $ newNtfPushServer pushQSize apnsConfig -- TODO not creating APNS client on start to pass CI test, has to be replaced with mock APNS server - -- void . liftIO $ newPushClient pushServer PPApns + void . liftIO $ newPushClient pushServer PPApns tlsServerParams <- liftIO $ loadTLSServerParams caCertificateFile certificateFile privateKeyFile Fingerprint fp <- liftIO $ loadFingerprint caCertificateFile pure NtfEnv {config, subscriber, pushServer, store, idsDrg, tlsServerParams, serverIdentity = C.KeyHash fp} diff --git a/src/Simplex/Messaging/Notifications/Server/Push/APNS.hs b/src/Simplex/Messaging/Notifications/Server/Push/APNS.hs index 0143fd20b..2f826e469 100644 --- a/src/Simplex/Messaging/Notifications/Server/Push/APNS.hs +++ b/src/Simplex/Messaging/Notifications/Server/Push/APNS.hs @@ -24,7 +24,6 @@ import Data.Aeson (FromJSON, ToJSON, (.=)) import qualified Data.Aeson as J import qualified Data.Aeson.Encoding as JE import Data.Bifunctor (first) -import qualified Data.ByteString.Base64 as B64 import qualified Data.ByteString.Base64.URL as U import Data.ByteString.Builder (lazyByteString) import Data.ByteString.Char8 (ByteString) @@ -37,8 +36,8 @@ import qualified Data.Text as T import Data.Text.Encoding (decodeUtf8With) import Data.Time.Clock.System import qualified Data.X509 as X -import GHC.Generics -import Network.HTTP.Types (HeaderName, Status, hAuthorization, methodPost) +import GHC.Generics (Generic) +import Network.HTTP.Types (HeaderName, Status) import qualified Network.HTTP.Types as N import Network.HTTP2.Client (Request) import qualified Network.HTTP2.Client as H @@ -48,7 +47,7 @@ import Simplex.Messaging.Encoding.String import Simplex.Messaging.Notifications.Protocol import Simplex.Messaging.Notifications.Server.Subscriptions (NtfTknData (..)) import Simplex.Messaging.Protocol (NotifierId, SMPServer) -import Simplex.Messaging.Transport.Client.HTTP2 +import Simplex.Messaging.Transport.HTTP2.Client import System.Environment (getEnv) import UnliftIO.STM @@ -177,9 +176,9 @@ data APNSPushClientConfig = APNSPushClientConfig paddedNtfLength :: Int, appName :: ByteString, appTeamId :: Text, - apnHost :: HostName, - apnPort :: ServiceName, - https2cfg :: HTTP2SClientConfig + apnsHost :: HostName, + apnsPort :: ServiceName, + http2cfg :: HTTP2ClientConfig } deriving (Show) @@ -193,13 +192,13 @@ defaultAPNSPushClientConfig = paddedNtfLength = 256, appName = "chat.simplex.app", appTeamId = "5NN7GUYB6T", - apnHost = "api.sandbox.push.apple.com", - apnPort = "443", - https2cfg = defaultHTTP2SClientConfig + apnsHost = "api.sandbox.push.apple.com", + apnsPort = "443", + http2cfg = defaultHTTP2ClientConfig } data APNSPushClient = APNSPushClient - { https2Client :: TVar (Maybe HTTPS2Client), + { https2Client :: TVar (Maybe HTTP2Client), privateKey :: EC.PrivateKey, jwtHeader :: JWTHeader, jwtToken :: TVar (JWTToken, SignedJWTToken), @@ -213,7 +212,6 @@ createAPNSPushClient apnsCfg@APNSPushClientConfig {authKeyFileEnv, authKeyAlg, a void $ connectHTTPS2 apnsCfg https2Client privateKey <- readECPrivateKey =<< getEnv authKeyFileEnv authKeyId <- T.pack <$> getEnv authKeyIdEnv - putStrLn $ authKeyIdEnv <> "=" <> T.unpack authKeyId let jwtHeader = JWTHeader {alg = authKeyAlg, kid = authKeyId} jwtToken <- newTVarIO =<< mkApnsJWTToken appTeamId jwtHeader privateKey nonceDrg <- drgNew >>= newTVarIO @@ -238,9 +236,9 @@ mkApnsJWTToken appTeamId jwtHeader privateKey = do signedJWT <- signedJWTToken privateKey jwt pure (jwt, signedJWT) -connectHTTPS2 :: APNSPushClientConfig -> TVar (Maybe HTTPS2Client) -> IO (Either HTTPS2ClientError HTTPS2Client) -connectHTTPS2 APNSPushClientConfig {apnHost, apnPort, https2cfg} https2Client = do - r <- getHTTPS2Client apnHost apnPort https2cfg disconnected +connectHTTPS2 :: APNSPushClientConfig -> TVar (Maybe HTTP2Client) -> IO (Either HTTP2ClientError HTTP2Client) +connectHTTPS2 APNSPushClientConfig {apnsHost, apnsPort, http2cfg} https2Client = do + r <- getHTTP2Client apnsHost apnsPort http2cfg disconnected case r of Right client -> atomically . writeTVar https2Client $ Just client Left e -> putStrLn $ "Error connecting to APNS: " <> show e @@ -248,13 +246,13 @@ connectHTTPS2 APNSPushClientConfig {apnHost, apnPort, https2cfg} https2Client = where disconnected = atomically $ writeTVar https2Client Nothing -getApnsHTTP2Client :: APNSPushClient -> IO (Either HTTPS2ClientError HTTPS2Client) +getApnsHTTP2Client :: APNSPushClient -> IO (Either HTTP2ClientError HTTP2Client) getApnsHTTP2Client APNSPushClient {https2Client, apnsCfg} = readTVarIO https2Client >>= maybe (connectHTTPS2 apnsCfg https2Client) (pure . Right) disconnectApnsHTTP2Client :: APNSPushClient -> IO () disconnectApnsHTTP2Client APNSPushClient {https2Client} = - readTVarIO https2Client >>= mapM_ closeHTTPS2Client >> atomically (writeTVar https2Client Nothing) + readTVarIO https2Client >>= mapM_ closeHTTP2Client >> atomically (writeTVar https2Client Nothing) apnsNotification :: NtfTknData -> C.CbNonce -> Int -> PushNotification -> Either C.CryptoError APNSNotification apnsNotification NtfTknData {tknDhSecret} nonce paddedLen = \case @@ -268,7 +266,7 @@ apnsNotification NtfTknData {tknDhSecret} nonce paddedLen = \case PNCheckMessages -> Right $ apn APNSBackground {contentAvailable = 1} . Just $ J.object ["checkMessages" .= True] where encrypt :: ByteString -> (Text -> APNSNotification) -> Either C.CryptoError APNSNotification - encrypt ntfData f = f . safeDecodeUtf8 . B64.encode <$> C.cbEncrypt tknDhSecret nonce ntfData paddedLen + encrypt ntfData f = f . safeDecodeUtf8 . U.encode <$> C.cbEncrypt tknDhSecret nonce ntfData paddedLen apn aps notificationData = APNSNotification {aps, notificationData} apnMutableContent = APNSMutableContent {mutableContent = 1, alert = APNSAlertText "Encrypted message or some other app event", category = Nothing} apnAlert alert = APNSAlert {alert, badge = Nothing, sound = Nothing, category = Nothing} @@ -277,13 +275,13 @@ apnsNotification NtfTknData {tknDhSecret} nonce paddedLen = \case apnsRequest :: APNSPushClient -> ByteString -> APNSNotification -> IO Request apnsRequest c tkn ntf@APNSNotification {aps} = do signedJWT <- getApnsJWTToken c - pure $ H.requestBuilder methodPost path (headers signedJWT) (lazyByteString $ J.encode ntf) + pure $ H.requestBuilder N.methodPost path (headers signedJWT) (lazyByteString $ J.encode ntf) where path = "/3/device/" <> tkn headers signedJWT = [ (hApnsTopic, appName $ apnsCfg (c :: APNSPushClient)), (hApnsPushType, pushType aps), - (hAuthorization, "bearer " <> signedJWT) + (N.hAuthorization, "bearer " <> signedJWT) ] <> [(hApnsPriority, "5") | isBackground aps] isBackground = \case @@ -294,7 +292,7 @@ apnsRequest c tkn ntf@APNSNotification {aps} = do _ -> "alert" data PushProviderError - = PPConnection HTTPS2ClientError + = PPConnection HTTP2ClientError | PPCryptoError C.CryptoError | PPResponseError (Maybe Status) Text | PPTokenInvalid @@ -305,7 +303,7 @@ data PushProviderError type PushProviderClient = NtfTknData -> PushNotification -> ExceptT PushProviderError IO () -- this is not a newtype on purpose to have a correct JSON encoding as a record -data APNSErrorReponse = APNSErrorReponse {reason :: Text} +data APNSErrorResponse = APNSErrorResponse {reason :: Text} deriving (Generic, FromJSON) apnsPushProviderClient :: APNSPushClient -> PushProviderClient @@ -316,7 +314,7 @@ apnsPushProviderClient c@APNSPushClient {nonceDrg, apnsCfg} tkn@NtfTknData {toke req <- liftIO $ apnsRequest c tknStr apnsNtf HTTP2Response {response, respBody} <- liftHTTPS2 $ sendRequest http2 req let status = H.responseStatus response - reason' = maybe "?" reason $ J.decodeStrict' respBody + reason' = maybe "" reason $ J.decodeStrict' respBody logDebug $ "APNS response: " <> T.pack (show status) <> " " <> reason' result status reason' where diff --git a/src/Simplex/Messaging/Transport/Client/HTTP2.hs b/src/Simplex/Messaging/Transport/Client/HTTP2.hs deleted file mode 100644 index 2d019abd3..000000000 --- a/src/Simplex/Messaging/Transport/Client/HTTP2.hs +++ /dev/null @@ -1,159 +0,0 @@ -{-# LANGUAGE DuplicateRecordFields #-} -{-# LANGUAGE LambdaCase #-} -{-# LANGUAGE NamedFieldPuns #-} -{-# LANGUAGE OverloadedStrings #-} -{-# LANGUAGE ScopedTypeVariables #-} - -module Simplex.Messaging.Transport.Client.HTTP2 where - -import Control.Concurrent.Async -import Control.Exception (IOException, catch, finally) -import qualified Control.Exception as E -import Control.Logger.Simple (logDebug) -import Control.Monad.Except -import Data.ByteString.Char8 (ByteString) -import qualified Data.ByteString.Char8 as B -import Data.Default (def) -import Data.Maybe (isNothing) -import qualified Data.Text as T -import qualified Data.X509.CertificateStore as XS -import Foreign (mallocBytes) -import Network.HPACK (BufferSize, HeaderTable) -import Network.HTTP2.Client (ClientConfig (..), Config (..), Request, Response) -import qualified Network.HTTP2.Client as H -import Network.Socket (HostName, ServiceName) -import qualified Network.TLS as T -import qualified Network.TLS.Extra as TE -import Numeric.Natural (Natural) -import Simplex.Messaging.Transport (TLS, Transport (cGet, cPut)) -import Simplex.Messaging.Transport.Client (runTLSTransportClient) -import Simplex.Messaging.Transport.KeepAlive (KeepAliveOpts) -import qualified System.TimeManager as TI -import UnliftIO.STM -import UnliftIO.Timeout - -data HTTPS2Client = HTTPS2Client - { action :: Async (), - connected :: TVar Bool, - host :: HostName, - port :: ServiceName, - config :: HTTP2SClientConfig, - reqQ :: TBQueue (Request, TMVar HTTP2Response) - } - -data HTTP2Response = HTTP2Response - { response :: Response, - respBody :: ByteString, - respTrailers :: Maybe HeaderTable - } - -data HTTP2SClientConfig = HTTP2SClientConfig - { qSize :: Natural, - connTimeout :: Int, - tcpKeepAlive :: Maybe KeepAliveOpts, - caStoreFile :: FilePath, - suportedTLSParams :: T.Supported - } - deriving (Show) - -defaultHTTP2SClientConfig :: HTTP2SClientConfig -defaultHTTP2SClientConfig = - HTTP2SClientConfig - { qSize = 64, - connTimeout = 10000000, - tcpKeepAlive = Nothing, - caStoreFile = "/etc/ssl/cert.pem", - suportedTLSParams = - def - { T.supportedVersions = [T.TLS13, T.TLS12], - T.supportedCiphers = TE.ciphersuite_strong_det, - T.supportedSecureRenegotiation = False - } - } - -data HTTPS2ClientError = HCResponseTimeout | HCNetworkError | HCIOError IOException - deriving (Show) - -getHTTPS2Client :: HostName -> ServiceName -> HTTP2SClientConfig -> IO () -> IO (Either HTTPS2ClientError HTTPS2Client) -getHTTPS2Client host port config@HTTP2SClientConfig {tcpKeepAlive, connTimeout, caStoreFile, suportedTLSParams} disconnected = - (atomically mkHTTPS2Client >>= runClient) - `catch` \(e :: IOException) -> pure . Left $ HCIOError e - where - mkHTTPS2Client :: STM HTTPS2Client - mkHTTPS2Client = do - connected <- newTVar False - reqQ <- newTBQueue $ qSize config - pure HTTPS2Client {action = undefined, connected, host, port, config, reqQ} - - runClient :: HTTPS2Client -> IO (Either HTTPS2ClientError HTTPS2Client) - runClient c = do - cVar <- newEmptyTMVarIO - caStore <- XS.readCertificateStore caStoreFile - when (isNothing caStore) . putStrLn $ "Error loading CertificateStore from " <> caStoreFile - action <- - async $ - runHTTPS2Client suportedTLSParams caStore host port tcpKeepAlive (client c cVar) - `finally` atomically (putTMVar cVar $ Left HCNetworkError) - conn_ <- connTimeout `timeout` atomically (takeTMVar cVar) - pure $ case conn_ of - Just (Right ()) -> Right c {action} - Just (Left e) -> Left e - Nothing -> Left HCNetworkError - - client :: HTTPS2Client -> TMVar (Either HTTPS2ClientError ()) -> (Request -> (Response -> IO ()) -> IO ()) -> IO () - client c cVar sendReq = do - atomically $ do - writeTVar (connected c) True - putTMVar cVar $ Right () - process c sendReq `finally` disconnected - - process :: HTTPS2Client -> (Request -> (Response -> IO ()) -> IO ()) -> IO () - process HTTPS2Client {reqQ} sendReq = forever $ do - (req, respVar) <- atomically $ readTBQueue reqQ - sendReq req $ \r -> do - let writeResp respBody respTrailers = atomically $ putTMVar respVar HTTP2Response {response = r, respBody, respTrailers} - respBody <- getResponseBody r "" - respTrailers <- H.getResponseTrailers r - writeResp respBody respTrailers - - getResponseBody :: Response -> ByteString -> IO ByteString - getResponseBody r s = - H.getResponseBodyChunk r >>= \chunk -> - if B.null chunk then pure s else getResponseBody r $ s <> chunk - --- | Disconnects client from the server and terminates client threads. -closeHTTPS2Client :: HTTPS2Client -> IO () --- TODO disconnect -closeHTTPS2Client = uninterruptibleCancel . action - -sendRequest :: HTTPS2Client -> Request -> IO (Either HTTPS2ClientError HTTP2Response) -sendRequest HTTPS2Client {reqQ, config} req = do - resp <- newEmptyTMVarIO - atomically $ writeTBQueue reqQ (req, resp) - maybe (Left HCResponseTimeout) Right <$> (connTimeout config `timeout` atomically (takeTMVar resp)) - -runHTTPS2Client :: T.Supported -> Maybe XS.CertificateStore -> HostName -> ServiceName -> Maybe KeepAliveOpts -> ((Request -> (Response -> IO ()) -> IO ()) -> IO ()) -> IO () -runHTTPS2Client tlsParams caStore host port keepAliveOpts client = - runTLSTransportClient tlsParams caStore host port Nothing keepAliveOpts https2Client - where - cfg = ClientConfig "https" (B.pack host) 20 - https2Client :: TLS -> IO () - https2Client c = - E.bracket - (allocTlsConfig c 16384) - H.freeSimpleConfig - (\conf -> H.run cfg conf client) - - allocTlsConfig :: TLS -> BufferSize -> IO Config - allocTlsConfig c sz = do - buf <- mallocBytes sz - tm <- TI.initialize $ 30 * 1000000 - pure - Config - { confWriteBuffer = buf, - confBufferSize = sz, - confSendAll = cPut c, - confReadN = cGet c, - confPositionReadMaker = H.defaultPositionReadMaker, - confTimeoutManager = tm - } diff --git a/src/Simplex/Messaging/Transport/HTTP2.hs b/src/Simplex/Messaging/Transport/HTTP2.hs new file mode 100644 index 000000000..750a5c87f --- /dev/null +++ b/src/Simplex/Messaging/Transport/HTTP2.hs @@ -0,0 +1,36 @@ +module Simplex.Messaging.Transport.HTTP2 where + +import qualified Control.Exception as E +import Data.Default (def) +import Foreign (mallocBytes) +import Network.HPACK (BufferSize) +import Network.HTTP2.Client (Config (..), defaultPositionReadMaker, freeSimpleConfig) +import qualified Network.TLS as T +import qualified Network.TLS.Extra as TE +import Simplex.Messaging.Transport (TLS, Transport (cGet, cPut)) +import qualified System.TimeManager as TI + +withTlsConfig :: TLS -> BufferSize -> (Config -> IO ()) -> IO () +withTlsConfig c sz = E.bracket (allocTlsConfig c sz) freeSimpleConfig + +allocTlsConfig :: TLS -> BufferSize -> IO Config +allocTlsConfig c sz = do + buf <- mallocBytes sz + tm <- TI.initialize $ 30 * 1000000 + pure + Config + { confWriteBuffer = buf, + confBufferSize = sz, + confSendAll = cPut c, + confReadN = cGet c, + confPositionReadMaker = defaultPositionReadMaker, + confTimeoutManager = tm + } + +http2TLSParams :: T.Supported +http2TLSParams = + def + { T.supportedVersions = [T.TLS13, T.TLS12], + T.supportedCiphers = TE.ciphersuite_strong_det, + T.supportedSecureRenegotiation = False + } diff --git a/src/Simplex/Messaging/Transport/HTTP2/Client.hs b/src/Simplex/Messaging/Transport/HTTP2/Client.hs new file mode 100644 index 000000000..a79368ad1 --- /dev/null +++ b/src/Simplex/Messaging/Transport/HTTP2/Client.hs @@ -0,0 +1,128 @@ +{-# LANGUAGE DuplicateRecordFields #-} +{-# LANGUAGE NamedFieldPuns #-} +{-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE ScopedTypeVariables #-} + +module Simplex.Messaging.Transport.HTTP2.Client where + +import Control.Concurrent.Async +import Control.Exception (IOException) +import qualified Control.Exception as E +import Control.Monad.Except +import Data.ByteString.Char8 (ByteString) +import qualified Data.ByteString.Char8 as B +import Data.Maybe (isNothing) +import qualified Data.X509.CertificateStore as XS +import Network.HPACK (HeaderTable) +import Network.HTTP2.Client (ClientConfig (..), Request, Response) +import qualified Network.HTTP2.Client as H +import Network.Socket (HostName, ServiceName) +import qualified Network.TLS as T +import Numeric.Natural (Natural) +import Simplex.Messaging.Transport.Client (runTLSTransportClient) +import Simplex.Messaging.Transport.HTTP2 (http2TLSParams, withTlsConfig) +import Simplex.Messaging.Transport.KeepAlive (KeepAliveOpts) +import UnliftIO.STM +import UnliftIO.Timeout + +data HTTP2Client = HTTP2Client + { action :: Async (), + connected :: TVar Bool, + host :: HostName, + port :: ServiceName, + config :: HTTP2ClientConfig, + reqQ :: TBQueue (Request, TMVar HTTP2Response) + } + +data HTTP2Response = HTTP2Response + { response :: Response, + respBody :: ByteString, + respTrailers :: Maybe HeaderTable + } + +data HTTP2ClientConfig = HTTP2ClientConfig + { qSize :: Natural, + connTimeout :: Int, + tcpKeepAlive :: Maybe KeepAliveOpts, + caStoreFile :: FilePath, + suportedTLSParams :: T.Supported + } + deriving (Show) + +defaultHTTP2ClientConfig :: HTTP2ClientConfig +defaultHTTP2ClientConfig = + HTTP2ClientConfig + { qSize = 64, + connTimeout = 10000000, + tcpKeepAlive = Nothing, + caStoreFile = "/etc/ssl/cert.pem", + suportedTLSParams = http2TLSParams + } + +data HTTP2ClientError = HCResponseTimeout | HCNetworkError | HCNetworkError1 | HCIOError IOException + deriving (Show) + +getHTTP2Client :: HostName -> ServiceName -> HTTP2ClientConfig -> IO () -> IO (Either HTTP2ClientError HTTP2Client) +getHTTP2Client host port config@HTTP2ClientConfig {tcpKeepAlive, connTimeout, caStoreFile, suportedTLSParams} disconnected = + (atomically mkHTTPS2Client >>= runClient) + `E.catch` \(e :: IOException) -> pure . Left $ HCIOError e + where + mkHTTPS2Client :: STM HTTP2Client + mkHTTPS2Client = do + connected <- newTVar False + reqQ <- newTBQueue $ qSize config + pure HTTP2Client {action = undefined, connected, host, port, config, reqQ} + + runClient :: HTTP2Client -> IO (Either HTTP2ClientError HTTP2Client) + runClient c = do + cVar <- newEmptyTMVarIO + caStore <- XS.readCertificateStore caStoreFile + when (isNothing caStore) . putStrLn $ "Error loading CertificateStore from " <> caStoreFile + action <- + async $ + runHTTP2Client suportedTLSParams caStore host port tcpKeepAlive (client c cVar) + `E.finally` atomically (putTMVar cVar $ Left HCNetworkError) + conn_ <- connTimeout `timeout` atomically (takeTMVar cVar) + pure $ case conn_ of + Just (Right ()) -> Right c {action} + Just (Left e) -> Left e + Nothing -> Left HCNetworkError1 + + client :: HTTP2Client -> TMVar (Either HTTP2ClientError ()) -> (Request -> (Response -> IO ()) -> IO ()) -> IO () + client c cVar sendReq = do + atomically $ do + writeTVar (connected c) True + putTMVar cVar $ Right () + process c sendReq `E.finally` disconnected + + process :: HTTP2Client -> (Request -> (Response -> IO ()) -> IO ()) -> IO () + process HTTP2Client {reqQ} sendReq = forever $ do + (req, respVar) <- atomically $ readTBQueue reqQ + sendReq req $ \r -> do + let writeResp respBody respTrailers = atomically $ putTMVar respVar HTTP2Response {response = r, respBody, respTrailers} + respBody <- getResponseBody r "" + respTrailers <- H.getResponseTrailers r + writeResp respBody respTrailers + + getResponseBody :: Response -> ByteString -> IO ByteString + getResponseBody r s = + H.getResponseBodyChunk r >>= \chunk -> + if B.null chunk then pure s else getResponseBody r $ s <> chunk + +-- | Disconnects client from the server and terminates client threads. +closeHTTP2Client :: HTTP2Client -> IO () +-- TODO disconnect +closeHTTP2Client = uninterruptibleCancel . action + +sendRequest :: HTTP2Client -> Request -> IO (Either HTTP2ClientError HTTP2Response) +sendRequest HTTP2Client {reqQ, config} req = do + resp <- newEmptyTMVarIO + atomically $ writeTBQueue reqQ (req, resp) + maybe (Left HCResponseTimeout) Right <$> (connTimeout config `timeout` atomically (takeTMVar resp)) + +runHTTP2Client :: T.Supported -> Maybe XS.CertificateStore -> HostName -> ServiceName -> Maybe KeepAliveOpts -> ((Request -> (Response -> IO ()) -> IO ()) -> IO ()) -> IO () +runHTTP2Client tlsParams caStore host port keepAliveOpts client = + runTLSTransportClient tlsParams caStore host port Nothing keepAliveOpts $ \c -> + withTlsConfig c 16384 (`run` client) + where + run = H.run $ ClientConfig "https" (B.pack host) 20 diff --git a/src/Simplex/Messaging/Transport/HTTP2/Server.hs b/src/Simplex/Messaging/Transport/HTTP2/Server.hs new file mode 100644 index 000000000..f546c4a0e --- /dev/null +++ b/src/Simplex/Messaging/Transport/HTTP2/Server.hs @@ -0,0 +1,70 @@ +{-# LANGUAGE NamedFieldPuns #-} +{-# LANGUAGE OverloadedStrings #-} + +module Simplex.Messaging.Transport.HTTP2.Server where + +import Control.Concurrent.Async (Async, async, uninterruptibleCancel) +import Control.Concurrent.STM +import Control.Monad +import Data.ByteString (ByteString) +import qualified Data.ByteString.Char8 as B +import Network.HPACK (HeaderTable) +import Network.HTTP2.Server (Aux, PushPromise, Request, Response) +import qualified Network.HTTP2.Server as H +import Network.Socket +import qualified Network.TLS as T +import Numeric.Natural (Natural) +import Simplex.Messaging.Transport.HTTP2 (withTlsConfig) +import Simplex.Messaging.Transport.Server (loadSupportedTLSServerParams, runTransportServer) + +type HTTP2ServerFunc = (Request -> (Response -> IO ()) -> IO ()) + +data HTTP2ServerConfig = HTTP2ServerConfig + { qSize :: Natural, + http2Port :: ServiceName, + serverSupported :: T.Supported, + caCertificateFile :: FilePath, + privateKeyFile :: FilePath, + certificateFile :: FilePath + } + deriving (Show) + +data HTTP2Request = HTTP2Request + { request :: Request, + reqBody :: ByteString, + reqTrailers :: Maybe HeaderTable, + sendResponse :: Response -> IO () + } + +data HTTP2Server = HTTP2Server + { action :: Async (), + reqQ :: TBQueue HTTP2Request + } + +getHTTP2Server :: HTTP2ServerConfig -> IO HTTP2Server +getHTTP2Server HTTP2ServerConfig {qSize, http2Port, serverSupported, caCertificateFile, certificateFile, privateKeyFile} = do + tlsServerParams <- loadSupportedTLSServerParams serverSupported caCertificateFile certificateFile privateKeyFile + started <- newEmptyTMVarIO + reqQ <- newTBQueueIO qSize + action <- async $ + runHTTP2Server started http2Port tlsServerParams $ \r sendResponse -> do + reqBody <- getRequestBody r "" + reqTrailers <- H.getRequestTrailers r + atomically $ writeTBQueue reqQ HTTP2Request {request = r, reqBody, reqTrailers, sendResponse} + void . atomically $ takeTMVar started + pure HTTP2Server {action, reqQ} + where + getRequestBody :: Request -> ByteString -> IO ByteString + getRequestBody r s = + H.getRequestBodyChunk r >>= \chunk -> + if B.null chunk then pure s else getRequestBody r $ s <> chunk + +closeHTTP2Server :: HTTP2Server -> IO () +closeHTTP2Server = uninterruptibleCancel . action + +runHTTP2Server :: TMVar Bool -> ServiceName -> T.ServerParams -> HTTP2ServerFunc -> IO () +runHTTP2Server started port serverParams http2Server = + runTransportServer started port serverParams $ \c -> withTlsConfig c 16384 (`H.run` server) + where + server :: Request -> Aux -> (Response -> [PushPromise] -> IO ()) -> IO () + server req _aux sendResp = http2Server req (`sendResp` []) diff --git a/src/Simplex/Messaging/Transport/Server.hs b/src/Simplex/Messaging/Transport/Server.hs index 74ffae279..b4cb69bbc 100644 --- a/src/Simplex/Messaging/Transport/Server.hs +++ b/src/Simplex/Messaging/Transport/Server.hs @@ -4,6 +4,7 @@ module Simplex.Messaging.Transport.Server ( runTransportServer, + loadSupportedTLSServerParams, loadTLSServerParams, loadFingerprint, smpServerHandshake, @@ -71,7 +72,10 @@ startTCPServer started port = withSocketsDo $ resolve >>= open >>= setStarted setStarted sock = atomically (tryPutTMVar started True) >> pure sock loadTLSServerParams :: FilePath -> FilePath -> FilePath -> IO T.ServerParams -loadTLSServerParams caCertificateFile certificateFile privateKeyFile = +loadTLSServerParams = loadSupportedTLSServerParams supportedParameters + +loadSupportedTLSServerParams :: T.Supported -> FilePath -> FilePath -> FilePath -> IO T.ServerParams +loadSupportedTLSServerParams serverSupported caCertificateFile certificateFile privateKeyFile = fromCredential <$> loadServerCredential where loadServerCredential :: IO T.Credential @@ -85,7 +89,7 @@ loadTLSServerParams caCertificateFile certificateFile privateKeyFile = { T.serverWantClientCert = False, T.serverShared = def {T.sharedCredentials = T.Credentials [credential]}, T.serverHooks = def, - T.serverSupported = supportedParameters + T.serverSupported = serverSupported } loadFingerprint :: FilePath -> IO Fingerprint diff --git a/tests/AgentTests/FunctionalAPITests.hs b/tests/AgentTests/FunctionalAPITests.hs index a08e72623..b20154a3f 100644 --- a/tests/AgentTests/FunctionalAPITests.hs +++ b/tests/AgentTests/FunctionalAPITests.hs @@ -9,13 +9,11 @@ module AgentTests.FunctionalAPITests (functionalAPITests) where import Control.Monad.Except (ExceptT, runExceptT) import Control.Monad.IO.Unlift -import NtfClient (withNtfServer) import SMPAgentClient import SMPClient (testPort, withSmpServer, withSmpServerStoreLogOn) import Simplex.Messaging.Agent import Simplex.Messaging.Agent.Env.SQLite (AgentConfig (..)) import Simplex.Messaging.Agent.Protocol -import Simplex.Messaging.Notifications.Protocol (DeviceToken (..), PushProvider (..)) import Simplex.Messaging.Protocol (ErrorType (..), MsgBody) import Simplex.Messaging.Transport (ATransport (..)) import System.Timeout @@ -50,14 +48,11 @@ functionalAPITests t = do testAsyncServerOffline t it "should notify after HELLO timeout" $ withSmpServer t testAsyncHelloTimeout - describe "Notification server" $ do - it "should register device token" $ - withNtfServer t testNotificationToken testAgentClient :: IO () testAgentClient = do - alice <- getSMPAgentClient cfg initAgentServers - bob <- getSMPAgentClient cfg {dbFile = testDB2} initAgentServers + alice <- getSMPAgentClient agentCfg initAgentServers + bob <- getSMPAgentClient agentCfg {dbFile = testDB2} initAgentServers Right () <- runExceptT $ do (bobId, qInfo) <- createConnection alice SCMInvitation aliceId <- joinConnection bob qInfo "bob's connInfo" @@ -100,13 +95,13 @@ testAgentClient = do testAsyncInitiatingOffline :: IO () testAsyncInitiatingOffline = do - alice <- getSMPAgentClient cfg initAgentServers - bob <- getSMPAgentClient cfg {dbFile = testDB2} initAgentServers + alice <- getSMPAgentClient agentCfg initAgentServers + bob <- getSMPAgentClient agentCfg {dbFile = testDB2} initAgentServers Right () <- runExceptT $ do (bobId, cReq) <- createConnection alice SCMInvitation disconnectAgentClient alice aliceId <- joinConnection bob cReq "bob's connInfo" - alice' <- liftIO $ getSMPAgentClient cfg initAgentServers + alice' <- liftIO $ getSMPAgentClient agentCfg initAgentServers subscribeConnection alice' bobId ("", _, CONF confId "bob's connInfo") <- get alice' allowConnection alice' bobId confId "alice's connInfo" @@ -118,15 +113,15 @@ testAsyncInitiatingOffline = do testAsyncJoiningOfflineBeforeActivation :: IO () testAsyncJoiningOfflineBeforeActivation = do - alice <- getSMPAgentClient cfg initAgentServers - bob <- getSMPAgentClient cfg {dbFile = testDB2} initAgentServers + alice <- getSMPAgentClient agentCfg initAgentServers + bob <- getSMPAgentClient agentCfg {dbFile = testDB2} initAgentServers Right () <- runExceptT $ do (bobId, qInfo) <- createConnection alice SCMInvitation aliceId <- joinConnection bob qInfo "bob's connInfo" disconnectAgentClient bob ("", _, CONF confId "bob's connInfo") <- get alice allowConnection alice bobId confId "alice's connInfo" - bob' <- liftIO $ getSMPAgentClient cfg {dbFile = testDB2} initAgentServers + bob' <- liftIO $ getSMPAgentClient agentCfg {dbFile = testDB2} initAgentServers subscribeConnection bob' aliceId get alice ##> ("", bobId, CON) get bob' ##> ("", aliceId, INFO "alice's connInfo") @@ -136,18 +131,18 @@ testAsyncJoiningOfflineBeforeActivation = do testAsyncBothOffline :: IO () testAsyncBothOffline = do - alice <- getSMPAgentClient cfg initAgentServers - bob <- getSMPAgentClient cfg {dbFile = testDB2} initAgentServers + alice <- getSMPAgentClient agentCfg initAgentServers + bob <- getSMPAgentClient agentCfg {dbFile = testDB2} initAgentServers Right () <- runExceptT $ do (bobId, cReq) <- createConnection alice SCMInvitation disconnectAgentClient alice aliceId <- joinConnection bob cReq "bob's connInfo" disconnectAgentClient bob - alice' <- liftIO $ getSMPAgentClient cfg initAgentServers + alice' <- liftIO $ getSMPAgentClient agentCfg initAgentServers subscribeConnection alice' bobId ("", _, CONF confId "bob's connInfo") <- get alice' allowConnection alice' bobId confId "alice's connInfo" - bob' <- liftIO $ getSMPAgentClient cfg {dbFile = testDB2} initAgentServers + bob' <- liftIO $ getSMPAgentClient agentCfg {dbFile = testDB2} initAgentServers subscribeConnection bob' aliceId get alice' ##> ("", bobId, CON) get bob' ##> ("", aliceId, INFO "alice's connInfo") @@ -157,8 +152,8 @@ testAsyncBothOffline = do testAsyncServerOffline :: ATransport -> IO () testAsyncServerOffline t = do - alice <- getSMPAgentClient cfg initAgentServers - bob <- getSMPAgentClient cfg {dbFile = testDB2} initAgentServers + alice <- getSMPAgentClient agentCfg initAgentServers + bob <- getSMPAgentClient agentCfg {dbFile = testDB2} initAgentServers -- create connection and shutdown the server Right (bobId, cReq) <- withSmpServerStoreLogOn t testPort $ \_ -> runExceptT $ createConnection alice SCMInvitation @@ -181,8 +176,8 @@ testAsyncServerOffline t = do testAsyncHelloTimeout :: IO () testAsyncHelloTimeout = do - alice <- getSMPAgentClient cfg initAgentServers - bob <- getSMPAgentClient cfg {dbFile = testDB2, helloTimeout = 1} initAgentServers + alice <- getSMPAgentClient agentCfg initAgentServers + bob <- getSMPAgentClient agentCfg {dbFile = testDB2, helloTimeout = 1} initAgentServers Right () <- runExceptT $ do (_, cReq) <- createConnection alice SCMInvitation disconnectAgentClient alice @@ -190,13 +185,6 @@ testAsyncHelloTimeout = do get bob ##> ("", aliceId, ERR $ CONN NOT_ACCEPTED) pure () -testNotificationToken :: IO () -testNotificationToken = do - alice <- getSMPAgentClient cfg initAgentServers - Right () <- runExceptT $ do - registerNtfToken alice $ DeviceToken PPApns "abcd" - pure () - exchangeGreetings :: AgentClient -> ConnId -> AgentClient -> ConnId -> ExceptT AgentErrorType IO () exchangeGreetings alice bobId bob aliceId = do 5 <- sendMessage alice bobId "hello" diff --git a/tests/AgentTests/NotificationTests.hs b/tests/AgentTests/NotificationTests.hs new file mode 100644 index 000000000..9b6a9e0f6 --- /dev/null +++ b/tests/AgentTests/NotificationTests.hs @@ -0,0 +1,49 @@ +{-# LANGUAGE NamedFieldPuns #-} +{-# LANGUAGE OverloadedStrings #-} + +module AgentTests.NotificationTests where + +import Control.Monad.Except +import qualified Data.Aeson as J +import qualified Data.Aeson.Types as JT +import Data.Bifunctor (bimap) +import qualified Data.ByteString.Base64.URL as U +import Data.ByteString.Char8 (ByteString) +import Data.Text.Encoding (encodeUtf8) +import NtfClient +import SMPAgentClient (agentCfg, initAgentServers) +import Simplex.Messaging.Agent +import Simplex.Messaging.Agent.Protocol +import qualified Simplex.Messaging.Crypto as C +import Simplex.Messaging.Notifications.Protocol +import Simplex.Messaging.Notifications.Server.Push.APNS +import Simplex.Messaging.Transport (ATransport) +import Test.Hspec +import UnliftIO.STM + +notificationTests :: ATransport -> Spec +notificationTests t = do + describe "Managing notification tokens" $ + it "should register and verify notification token" $ + withAPNSMockServer $ \apns -> withNtfServer t $ testNotificationToken apns + +testNotificationToken :: APNSMockServer -> IO () +testNotificationToken APNSMockServer {apnsQ} = do + a <- getSMPAgentClient agentCfg initAgentServers + Right () <- runExceptT $ do + let tkn = DeviceToken PPApns "abcd" + registerNtfToken a tkn + APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData}, sendApnsResponse} <- + atomically $ readTBQueue apnsQ + verification <- ntfData .-> "verification" + nonce <- C.cbNonce <$> ntfData .-> "nonce" + liftIO $ sendApnsResponse APNSRespOk + verifyNtfToken a tkn verification nonce + enableNtfCron a tkn 30 + pure () + pure () + where + (.->) :: J.Value -> J.Key -> ExceptT AgentErrorType IO ByteString + v .-> key = do + J.Object o <- pure v + liftEither . bimap INTERNAL (U.decodeLenient . encodeUtf8) $ JT.parseEither (J..: key) o diff --git a/tests/NtfClient.hs b/tests/NtfClient.hs index c9ef301ab..861e38016 100644 --- a/tests/NtfClient.hs +++ b/tests/NtfClient.hs @@ -1,53 +1,74 @@ {-# LANGUAGE DataKinds #-} +{-# LANGUAGE DeriveAnyClass #-} +{-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE DuplicateRecordFields #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE LambdaCase #-} +{-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE NumericUnderscores #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE ScopedTypeVariables #-} +{-# LANGUAGE StandaloneDeriving #-} {-# LANGUAGE TypeApplications #-} +{-# OPTIONS_GHC -fno-warn-orphans #-} module NtfClient where +import Control.Monad import Control.Monad.Except (runExceptT) import Control.Monad.IO.Unlift import Crypto.Random +import Data.Aeson (FromJSON (..), ToJSON (..)) +import qualified Data.Aeson as J +import Data.ByteString.Builder (lazyByteString) import Data.ByteString.Char8 (ByteString) +import Data.Text (Text) +import GHC.Generics (Generic) +import Network.HTTP.Types (Status) +import qualified Network.HTTP.Types as N +import qualified Network.HTTP2.Server as H import Network.Socket import Simplex.Messaging.Client.Agent (defaultSMPClientAgentConfig) import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Encoding import Simplex.Messaging.Notifications.Server (runNtfServerBlocking) import Simplex.Messaging.Notifications.Server.Env -import Simplex.Messaging.Notifications.Server.Push.APNS (defaultAPNSPushClientConfig) +import Simplex.Messaging.Notifications.Server.Push.APNS import Simplex.Messaging.Notifications.Transport import Simplex.Messaging.Protocol import Simplex.Messaging.Transport import Simplex.Messaging.Transport.Client +import Simplex.Messaging.Transport.HTTP2 (http2TLSParams) +import Simplex.Messaging.Transport.HTTP2.Client +import Simplex.Messaging.Transport.HTTP2.Server import Simplex.Messaging.Transport.KeepAlive +import UnliftIO.Async import UnliftIO.Concurrent import qualified UnliftIO.Exception as E -import UnliftIO.STM (TMVar, atomically, newEmptyTMVarIO, takeTMVar) +import UnliftIO.STM import UnliftIO.Timeout (timeout) testHost :: HostName testHost = "localhost" -testPort :: ServiceName -testPort = "6001" +ntfTestPort :: ServiceName +ntfTestPort = "6001" + +apnsTestPort :: ServiceName +apnsTestPort = "6010" testKeyHash :: C.KeyHash testKeyHash = "LcJUMfVhwD8yxjAiSaDzzGF3-kLG4Uh0Fl_ZIjrRwjI=" testNtfClient :: (Transport c, MonadUnliftIO m) => (THandle c -> m a) -> m a testNtfClient client = - runTransportClient testHost testPort (Just testKeyHash) (Just defaultKeepAliveOpts) $ \h -> + runTransportClient testHost ntfTestPort (Just testKeyHash) (Just defaultKeepAliveOpts) $ \h -> liftIO (runExceptT $ ntfClientHandshake h testKeyHash) >>= \case Right th -> client th Left e -> error $ show e -cfg :: NtfServerConfig -cfg = +ntfServerCfg :: NtfServerConfig +ntfServerCfg = NtfServerConfig { transports = undefined, subIdBytes = 24, @@ -56,7 +77,12 @@ cfg = subQSize = 1, pushQSize = 1, smpAgentCfg = defaultSMPClientAgentConfig, - apnsConfig = defaultAPNSPushClientConfig, + apnsConfig = + defaultAPNSPushClientConfig + { apnsHost = "localhost", + apnsPort = apnsTestPort, + http2cfg = defaultHTTP2ClientConfig {caStoreFile = "tests/fixtures/ca.crt"} + }, -- CA certificate private key is not needed for initialization caCertificateFile = "tests/fixtures/ca.crt", privateKeyFile = "tests/fixtures/server.key", @@ -66,7 +92,7 @@ cfg = withNtfServerThreadOn :: (MonadUnliftIO m, MonadRandom m) => ATransport -> ServiceName -> (ThreadId -> m a) -> m a withNtfServerThreadOn t port' = serverBracket - (\started -> runNtfServerBlocking started cfg {transports = [(port', t)]}) + (\started -> runNtfServerBlocking started ntfServerCfg {transports = [(port', t)]}) (pure ()) serverBracket :: MonadUnliftIO m => (TMVar Bool -> m ()) -> m () -> (ThreadId -> m a) -> m a @@ -86,7 +112,7 @@ withNtfServerOn :: (MonadUnliftIO m, MonadRandom m) => ATransport -> ServiceName withNtfServerOn t port' = withNtfServerThreadOn t port' . const withNtfServer :: (MonadUnliftIO m, MonadRandom m) => ATransport -> m a -> m a -withNtfServer t = withNtfServerOn t testPort +withNtfServer t = withNtfServerOn t ntfTestPort runNtfTest :: forall c m a. (Transport c, MonadUnliftIO m, MonadRandom m) => (THandle c -> m a) -> m a runNtfTest test = withNtfServer (transport @c) $ testNtfClient test @@ -105,4 +131,63 @@ ntfServerTest _ t = runNtfTest $ \h -> tPut' h t >> tGet' h pure () tGet' h = do (Nothing, _, (CorrId corrId, qId, Right cmd)) <- tGet h - pure (Nothing, corrId, qId, cmd) \ No newline at end of file + pure (Nothing, corrId, qId, cmd) + +data APNSMockRequest = APNSMockRequest + { notification :: APNSNotification, + sendApnsResponse :: APNSMockResponse -> IO () + } + +data APNSMockResponse = APNSRespOk | APNSRespError Status Text + +data APNSMockServer = APNSMockServer + { action :: Async (), + apnsQ :: TBQueue APNSMockRequest, + http2Server :: HTTP2Server + } + +apnsMockServerConfig :: HTTP2ServerConfig +apnsMockServerConfig = + HTTP2ServerConfig + { qSize = 1, + http2Port = apnsTestPort, + serverSupported = http2TLSParams, + caCertificateFile = "tests/fixtures/ca.crt", + privateKeyFile = "tests/fixtures/server.key", + certificateFile = "tests/fixtures/server.crt" + } + +withAPNSMockServer :: (APNSMockServer -> IO ()) -> IO () +withAPNSMockServer = E.bracket (getAPNSMockServer apnsMockServerConfig) closeAPNSMockServer + +deriving instance Generic APNSAlertBody + +deriving instance FromJSON APNSAlertBody + +instance FromJSON APNSNotificationBody where parseJSON = J.genericParseJSON apnsJSONOptions + +deriving instance FromJSON APNSNotification + +deriving instance ToJSON APNSErrorResponse + +getAPNSMockServer :: HTTP2ServerConfig -> IO APNSMockServer +getAPNSMockServer config@HTTP2ServerConfig {qSize} = do + http2Server <- getHTTP2Server config + apnsQ <- newTBQueueIO qSize + action <- async $ runAPNSMockServer apnsQ http2Server + pure APNSMockServer {action, apnsQ, http2Server} + where + runAPNSMockServer apnsQ HTTP2Server {reqQ} = forever $ do + HTTP2Request {reqBody, sendResponse} <- atomically $ readTBQueue reqQ + let sendApnsResponse = \case + APNSRespOk -> sendResponse $ H.responseNoBody N.ok200 [] + APNSRespError status reason -> + sendResponse . H.responseBuilder status [] . lazyByteString $ J.encode APNSErrorResponse {reason} + case J.decodeStrict' reqBody of + Just notification -> atomically $ writeTBQueue apnsQ APNSMockRequest {notification, sendApnsResponse} + _ -> sendApnsResponse $ APNSRespError N.badRequest400 "bad_request_body" + +closeAPNSMockServer :: APNSMockServer -> IO () +closeAPNSMockServer APNSMockServer {action, http2Server} = do + closeHTTP2Server http2Server + uninterruptibleCancel action diff --git a/tests/NtfServerTests.hs b/tests/NtfServerTests.hs index 680676dea..c68c2438e 100644 --- a/tests/NtfServerTests.hs +++ b/tests/NtfServerTests.hs @@ -33,4 +33,4 @@ ntfSyntaxTests (ATransport t) = do (Maybe C.ASignature, ByteString, ByteString, smp) -> (Maybe C.ASignature, ByteString, ByteString, BrokerMsg) -> Expectation - command >#> response = ntfServerTest t command `shouldReturn` response + command >#> response = withAPNSMockServer $ \_ -> ntfServerTest t command `shouldReturn` response diff --git a/tests/SMPAgentClient.hs b/tests/SMPAgentClient.hs index be3de3e2c..d23ab522e 100644 --- a/tests/SMPAgentClient.hs +++ b/tests/SMPAgentClient.hs @@ -11,6 +11,7 @@ import Crypto.Random import qualified Data.ByteString.Char8 as B import qualified Data.List.NonEmpty as L import Network.Socket (HostName, ServiceName) +import NtfClient (ntfTestPort) import SMPClient ( serverBracket, testKeyHash, @@ -161,8 +162,8 @@ initAgentServers = ntf = ["smp://LcJUMfVhwD8yxjAiSaDzzGF3-kLG4Uh0Fl_ZIjrRwjI=@localhost:6001"] } -cfg :: AgentConfig -cfg = +agentCfg :: AgentConfig +agentCfg = defaultAgentConfig { tcpPort = agentTestPort, tbqSize = 1, @@ -173,6 +174,11 @@ cfg = defaultTransport = (testPort, transport @TLS), tcpTimeout = 500_000 }, + ntfCfg = + defaultClientConfig + { qSize = 1, + defaultTransport = (ntfTestPort, transport @TLS) + }, reconnectInterval = defaultReconnectInterval {initialInterval = 50_000}, caCertificateFile = "tests/fixtures/ca.crt", privateKeyFile = "tests/fixtures/server.key", @@ -181,7 +187,7 @@ cfg = withSmpAgentThreadOn_ :: (MonadUnliftIO m, MonadRandom m) => ATransport -> (ServiceName, ServiceName, String) -> m () -> (ThreadId -> m a) -> m a withSmpAgentThreadOn_ t (port', smpPort', db') afterProcess = - let cfg' = cfg {tcpPort = port', dbFile = db'} + let cfg' = agentCfg {tcpPort = port', dbFile = db'} initServers' = initAgentServers {smp = L.fromList [SMPServer "localhost" smpPort' testKeyHash]} in serverBracket (\started -> runSMPAgentBlocking t started cfg' initServers') diff --git a/tests/Test.hs b/tests/Test.hs index bdd67c0dc..acbd15645 100644 --- a/tests/Test.hs +++ b/tests/Test.hs @@ -1,6 +1,7 @@ {-# LANGUAGE TypeApplications #-} import AgentTests (agentTests) +import AgentTests.NotificationTests (notificationTests) import CoreTests.EncodingTests import CoreTests.ProtocolErrorTests import CoreTests.VersionRangeTests @@ -9,11 +10,14 @@ import ServerTests import Simplex.Messaging.Transport (TLS, Transport (..)) import Simplex.Messaging.Transport.WebSockets (WS) import System.Directory (createDirectoryIfMissing, removeDirectoryRecursive) +import System.Environment (setEnv) import Test.Hspec main :: IO () main = do createDirectoryIfMissing False "tests/tmp" + setEnv "APNS_KEY_ID" "H82WD9K9AQ" + setEnv "APNS_KEY_FILE" "./tests/fixtures/AuthKey_H82WD9K9AQ.p8" hspec $ do describe "Core tests" $ do describe "Encoding tests" encodingTests @@ -21,6 +25,8 @@ main = do describe "Version range" versionRangeTests describe "SMP server via TLS" $ serverTests (transport @TLS) describe "SMP server via WebSockets" $ serverTests (transport @WS) - describe "Ntf server via TLS" $ ntfServerTests (transport @TLS) + describe "Notifications server" $ do + ntfServerTests (transport @TLS) + notificationTests (transport @TLS) describe "SMP client agent" $ agentTests (transport @TLS) removeDirectoryRecursive "tests/tmp" diff --git a/tests/fixtures/AuthKey_H82WD9K9AQ.p8 b/tests/fixtures/AuthKey_H82WD9K9AQ.p8 new file mode 100644 index 000000000..d6531ce30 --- /dev/null +++ b/tests/fixtures/AuthKey_H82WD9K9AQ.p8 @@ -0,0 +1,6 @@ +-----BEGIN PRIVATE KEY----- +MIGTAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBHkwdwIBAQQgWuPap5jF6eioxuHM +XWZWUK78LdcxkTnMXWg2GqyXuBugCgYIKoZIzj0DAQehRANCAAQn64CvAIbEEzvM +KwYjlOxVD5SxlgP1ZcYvVM/+VHLFu0aCkG7ueICTi3qWyqoB5hjjuAqwtc3EK0q0 +yupyM7Yx +-----END PRIVATE KEY----- \ No newline at end of file From b5c5a8ad2a18a922ccdc0a03db651b16cd5d11a0 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Wed, 20 Apr 2022 11:54:37 +0100 Subject: [PATCH 009/131] rename Subscriptions.hs/Store.hs --- simplexmq.cabal | 2 +- src/Simplex/Messaging/Notifications/Server.hs | 2 +- src/Simplex/Messaging/Notifications/Server/Env.hs | 2 +- src/Simplex/Messaging/Notifications/Server/Push/APNS.hs | 2 +- .../Notifications/Server/{Subscriptions.hs => Store.hs} | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) rename src/Simplex/Messaging/Notifications/Server/{Subscriptions.hs => Store.hs} (98%) diff --git a/simplexmq.cabal b/simplexmq.cabal index 1472db2f4..9a174f6d6 100644 --- a/simplexmq.cabal +++ b/simplexmq.cabal @@ -59,7 +59,7 @@ library Simplex.Messaging.Notifications.Server Simplex.Messaging.Notifications.Server.Env Simplex.Messaging.Notifications.Server.Push.APNS - Simplex.Messaging.Notifications.Server.Subscriptions + Simplex.Messaging.Notifications.Server.Store Simplex.Messaging.Notifications.Transport Simplex.Messaging.Parsers Simplex.Messaging.Protocol diff --git a/src/Simplex/Messaging/Notifications/Server.hs b/src/Simplex/Messaging/Notifications/Server.hs index 5d81b3715..babe98a70 100644 --- a/src/Simplex/Messaging/Notifications/Server.hs +++ b/src/Simplex/Messaging/Notifications/Server.hs @@ -23,7 +23,7 @@ import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Notifications.Protocol import Simplex.Messaging.Notifications.Server.Env import Simplex.Messaging.Notifications.Server.Push.APNS -import Simplex.Messaging.Notifications.Server.Subscriptions +import Simplex.Messaging.Notifications.Server.Store import Simplex.Messaging.Notifications.Transport import Simplex.Messaging.Protocol (ErrorType (..), SignedTransmission, Transmission, encodeTransmission, tGet, tPut) import qualified Simplex.Messaging.Protocol as SMP diff --git a/src/Simplex/Messaging/Notifications/Server/Env.hs b/src/Simplex/Messaging/Notifications/Server/Env.hs index ce00ab354..7443a87f2 100644 --- a/src/Simplex/Messaging/Notifications/Server/Env.hs +++ b/src/Simplex/Messaging/Notifications/Server/Env.hs @@ -21,7 +21,7 @@ import Simplex.Messaging.Client.Agent import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Notifications.Protocol import Simplex.Messaging.Notifications.Server.Push.APNS -import Simplex.Messaging.Notifications.Server.Subscriptions +import Simplex.Messaging.Notifications.Server.Store import Simplex.Messaging.Protocol (CorrId, Transmission) import Simplex.Messaging.TMap (TMap) import qualified Simplex.Messaging.TMap as TM diff --git a/src/Simplex/Messaging/Notifications/Server/Push/APNS.hs b/src/Simplex/Messaging/Notifications/Server/Push/APNS.hs index 2f826e469..7c62f630a 100644 --- a/src/Simplex/Messaging/Notifications/Server/Push/APNS.hs +++ b/src/Simplex/Messaging/Notifications/Server/Push/APNS.hs @@ -45,7 +45,7 @@ import Network.Socket (HostName, ServiceName) import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Encoding.String import Simplex.Messaging.Notifications.Protocol -import Simplex.Messaging.Notifications.Server.Subscriptions (NtfTknData (..)) +import Simplex.Messaging.Notifications.Server.Store (NtfTknData (..)) import Simplex.Messaging.Protocol (NotifierId, SMPServer) import Simplex.Messaging.Transport.HTTP2.Client import System.Environment (getEnv) diff --git a/src/Simplex/Messaging/Notifications/Server/Subscriptions.hs b/src/Simplex/Messaging/Notifications/Server/Store.hs similarity index 98% rename from src/Simplex/Messaging/Notifications/Server/Subscriptions.hs rename to src/Simplex/Messaging/Notifications/Server/Store.hs index 9123a690f..bc7040b67 100644 --- a/src/Simplex/Messaging/Notifications/Server/Subscriptions.hs +++ b/src/Simplex/Messaging/Notifications/Server/Store.hs @@ -4,7 +4,7 @@ {-# LANGUAGE KindSignatures #-} {-# LANGUAGE NamedFieldPuns #-} -module Simplex.Messaging.Notifications.Server.Subscriptions where +module Simplex.Messaging.Notifications.Server.Store where import Control.Concurrent.STM import qualified Simplex.Messaging.Crypto as C From e6fbaf5e503429f00041863fb943b331b29f2c24 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Thu, 21 Apr 2022 17:04:26 +0100 Subject: [PATCH 010/131] primary tokens (#356) * primary tokens * support repeat token registration (TODO fix tests) * fix notifications tests * fix/test repeat/new registrations of the same token * re-register token when subsequent ntf command fails with AUTH error (e.g. when server is re-started) * cancel periodic notifications when token is deleted on the server * debug failing test on CI * fix notification test in CI * debug CI test * add delay in notificaitons test after server restart --- src/Simplex/Messaging/Agent.hs | 42 ++++-- src/Simplex/Messaging/Agent/Client.hs | 43 +++--- src/Simplex/Messaging/Agent/Protocol.hs | 4 + src/Simplex/Messaging/Client/Agent.hs | 4 +- .../Messaging/Notifications/Protocol.hs | 3 +- src/Simplex/Messaging/Notifications/Server.hs | 43 +++--- .../Messaging/Notifications/Server/Store.hs | 76 ++++++++-- src/Simplex/Messaging/Server.hs | 5 +- .../Messaging/Server/QueueStore/STM.hs | 9 +- src/Simplex/Messaging/TMap.hs | 5 + src/Simplex/Messaging/Util.hs | 3 + tests/AgentTests/NotificationTests.hs | 138 ++++++++++++++++-- 12 files changed, 291 insertions(+), 84 deletions(-) diff --git a/src/Simplex/Messaging/Agent.hs b/src/Simplex/Messaging/Agent.hs index 39e351f99..a2aeb9d68 100644 --- a/src/Simplex/Messaging/Agent.hs +++ b/src/Simplex/Messaging/Agent.hs @@ -52,6 +52,7 @@ module Simplex.Messaging.Agent registerNtfToken, verifyNtfToken, enableNtfCron, + checkNtfToken, deleteNtfToken, logConnection, ) @@ -89,10 +90,10 @@ import Simplex.Messaging.Encoding import Simplex.Messaging.Notifications.Client import Simplex.Messaging.Notifications.Protocol (DeviceToken, NtfRegCode (NtfRegCode), NtfTknStatus (..)) import Simplex.Messaging.Parsers (parse) -import Simplex.Messaging.Protocol (BrokerMsg, MsgBody) +import Simplex.Messaging.Protocol (BrokerMsg, ErrorType (AUTH), MsgBody) import qualified Simplex.Messaging.Protocol as SMP import qualified Simplex.Messaging.TMap as TM -import Simplex.Messaging.Util (bshow, liftError, tryError, unlessM) +import Simplex.Messaging.Util (bshow, liftError, tryError, unlessM, ($>>=)) import Simplex.Messaging.Version import System.Random (randomR) import UnliftIO.Async (async, race_) @@ -172,6 +173,9 @@ verifyNtfToken c = withAgentEnv c .:. verifyNtfToken' c enableNtfCron :: AgentErrorMonad m => AgentClient -> DeviceToken -> Word16 -> m () enableNtfCron c = withAgentEnv c .: enableNtfCron' c +checkNtfToken :: AgentErrorMonad m => AgentClient -> DeviceToken -> m NtfTknStatus +checkNtfToken c = withAgentEnv c . checkNtfToken' c + deleteNtfToken :: AgentErrorMonad m => AgentClient -> DeviceToken -> m () deleteNtfToken c = withAgentEnv c . deleteNtfToken' c @@ -552,7 +556,7 @@ registerNtfToken' c deviceToken = registerToken tkn _ -> throwError $ CMD PROHIBITED where - t tkn = withToken tkn Nothing + t tkn = withToken c tkn Nothing registerToken :: NtfToken -> m () registerToken tkn@NtfToken {ntfPubKey, ntfDhKeys = (pubDhKey, privDhKey)} = do (tknId, srvPubDhKey) <- agentNtfRegisterToken c tkn ntfPubKey pubDhKey @@ -565,7 +569,7 @@ verifyNtfToken' c deviceToken code nonce = withStore (`getDeviceNtfToken` deviceToken) >>= \case (Just tkn@NtfToken {ntfTokenId = Just tknId, ntfDhSecret = Just dhSecret}, _) -> do code' <- liftEither . bimap cryptoError NtfRegCode $ C.cbDecrypt dhSecret nonce code - withToken tkn (Just (NTConfirmed, NTAVerify code')) (NTActive, Just NTACheck) $ + withToken c tkn (Just (NTConfirmed, NTAVerify code')) (NTActive, Just NTACheck) $ agentNtfVerifyToken c tknId tkn code' _ -> throwError $ CMD PROHIBITED @@ -574,7 +578,7 @@ enableNtfCron' c deviceToken interval = do when (interval < 20) . throwError $ CMD PROHIBITED withStore (`getDeviceNtfToken` deviceToken) >>= \case (Just tkn@NtfToken {ntfTokenId = Just tknId, ntfTknStatus = NTActive}, _) -> - withToken tkn (Just (NTActive, NTACron interval)) (cronSuccess interval) $ + withToken c tkn (Just (NTActive, NTACron interval)) (cronSuccess interval) $ agentNtfEnableCron c tknId tkn interval _ -> throwError $ CMD PROHIBITED @@ -583,6 +587,12 @@ cronSuccess interval | interval == 0 = (NTActive, Just NTACheck) | otherwise = (NTActive, Just $ NTACron interval) +checkNtfToken' :: AgentMonad m => AgentClient -> DeviceToken -> m NtfTknStatus +checkNtfToken' c deviceToken = + withStore (`getDeviceNtfToken` deviceToken) >>= \case + (Just tkn@NtfToken {ntfTokenId = Just tknId}, _) -> agentNtfCheckToken c tknId tkn + _ -> throwError $ CMD PROHIBITED + deleteNtfToken' :: AgentMonad m => AgentClient -> DeviceToken -> m () deleteNtfToken' c deviceToken = withStore (`getDeviceNtfToken` deviceToken) >>= \case @@ -593,15 +603,23 @@ deleteToken_ :: AgentMonad m => AgentClient -> NtfToken -> m () deleteToken_ c tkn@NtfToken {ntfTokenId, ntfTknStatus} = do forM_ ntfTokenId $ \tknId -> do withStore $ \st -> updateNtfToken st tkn ntfTknStatus (Just NTADelete) - agentNtfDeleteToken c tknId tkn + agentNtfDeleteToken c tknId tkn `catchError` \case + NTF AUTH -> pure () + e -> throwError e withStore $ \st -> removeNtfToken st tkn -withToken :: AgentMonad m => NtfToken -> Maybe (NtfTknStatus, NtfTknAction) -> (NtfTknStatus, Maybe NtfTknAction) -> m a -> m a -withToken tkn from_ (toStatus, toAction_) f = do +withToken :: AgentMonad m => AgentClient -> NtfToken -> Maybe (NtfTknStatus, NtfTknAction) -> (NtfTknStatus, Maybe NtfTknAction) -> m a -> m a +withToken c tkn@NtfToken {deviceToken} from_ (toStatus, toAction_) f = do forM_ from_ $ \(status, action) -> withStore $ \st -> updateNtfToken st tkn status (Just action) - res <- f - withStore $ \st -> updateNtfToken st tkn toStatus toAction_ - pure res + tryError f >>= \case + Right res -> do + withStore $ \st -> updateNtfToken st tkn toStatus toAction_ + pure res + Left e@(NTF AUTH) -> do + withStore $ \st -> removeNtfToken st tkn + registerNtfToken' c deviceToken + throwError e + Left e -> throwError e setNtfServers' :: AgentMonad m => AgentClient -> [NtfServer] -> m () setNtfServers' c servers = do @@ -678,7 +696,7 @@ processSMPTransmission c@AgentClient {smpClients, subQ} (srv, sessId, rId, cmd) _ -> prohibited >> ack _ -> prohibited >> ack SMP.END -> - atomically (TM.lookup srv smpClients >>= fmap join . mapM tryReadTMVar >>= processEND) + atomically (TM.lookup srv smpClients $>>= tryReadTMVar >>= processEND) >>= logServer "<--" c srv rId where processEND = \case diff --git a/src/Simplex/Messaging/Agent/Client.hs b/src/Simplex/Messaging/Agent/Client.hs index 8108f36fd..e063ae9bc 100644 --- a/src/Simplex/Messaging/Agent/Client.hs +++ b/src/Simplex/Messaging/Agent/Client.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE AllowAmbiguousTypes #-} {-# LANGUAGE ConstraintKinds #-} {-# LANGUAGE DataKinds #-} {-# LANGUAGE DuplicateRecordFields #-} @@ -8,6 +9,7 @@ {-# LANGUAGE OverloadedLists #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE ScopedTypeVariables #-} +{-# LANGUAGE TypeApplications #-} module Simplex.Messaging.Agent.Client ( AgentClient (..), @@ -66,7 +68,7 @@ import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Encoding import Simplex.Messaging.Notifications.Client import Simplex.Messaging.Notifications.Protocol -import Simplex.Messaging.Protocol (BrokerMsg, ProtocolServer (..), QueueId, QueueIdsKeys (..), SndPublicVerifyKey) +import Simplex.Messaging.Protocol (BrokerMsg, ErrorType, ProtocolServer (..), QueueId, QueueIdsKeys (..), SndPublicVerifyKey) import qualified Simplex.Messaging.Protocol as SMP import Simplex.Messaging.TMap (TMap) import qualified Simplex.Messaging.TMap as TM @@ -132,10 +134,15 @@ type AgentMonad m = (MonadUnliftIO m, MonadReader Env m, MonadError AgentErrorTy class ProtocolServerClient msg where getProtocolServerClient :: AgentMonad m => AgentClient -> ProtocolServer -> m (ProtocolClient msg) + protocolError :: ErrorType -> AgentErrorType -instance ProtocolServerClient BrokerMsg where getProtocolServerClient = getSMPServerClient +instance ProtocolServerClient BrokerMsg where + getProtocolServerClient = getSMPServerClient + protocolError = SMP -instance ProtocolServerClient NtfResponse where getProtocolServerClient = getNtfServerClient +instance ProtocolServerClient NtfResponse where + getProtocolServerClient = getNtfServerClient + protocolError = NTF getSMPServerClient :: forall m. AgentMonad m => AgentClient -> SMPServer -> m SMPClient getSMPServerClient c@AgentClient {smpClients, msgQ} srv = @@ -148,7 +155,7 @@ getSMPServerClient c@AgentClient {smpClients, msgQ} srv = connectClient = do cfg <- asks $ smpCfg . config u <- askUnliftIO - liftEitherError protocolClientError (getProtocolClient srv cfg (Just msgQ) $ clientDisconnected u) + liftEitherError (protocolClientError SMP) (getProtocolClient srv cfg (Just msgQ) $ clientDisconnected u) clientDisconnected :: UnliftIO m -> IO () clientDisconnected u = do @@ -207,7 +214,7 @@ getSMPServerClient c@AgentClient {smpClients, msgQ} srv = e@PCEResponseTimeout -> throwError e e@PCENetworkError -> throwError e e -> do - liftIO $ notifySub (ERR $ protocolClientError e) connId + liftIO $ notifySub (ERR $ protocolClientError SMP e) connId atomically $ removePendingSubscription c srv connId notifySub :: ACommand 'Agent -> ConnId -> IO () @@ -223,7 +230,7 @@ getNtfServerClient c@AgentClient {ntfClients} srv = connectClient :: m NtfClient connectClient = do cfg <- asks $ ntfCfg . config - liftEitherError protocolClientError (getProtocolClient srv cfg Nothing clientDisconnected) + liftEitherError (protocolClientError NTF) (getProtocolClient srv cfg Nothing clientDisconnected) clientDisconnected :: IO () clientDisconnected = do @@ -322,18 +329,18 @@ withLogClient_ c srv qId cmdStr action = do logServer "<--" c srv qId "OK" return res -withClient :: (AgentMonad m, ProtocolServerClient msg) => AgentClient -> ProtocolServer -> (ProtocolClient msg -> ExceptT ProtocolClientError IO a) -> m a -withClient c srv action = withClient_ c srv $ liftClient . action +withClient :: forall m msg a. (AgentMonad m, ProtocolServerClient msg) => AgentClient -> ProtocolServer -> (ProtocolClient msg -> ExceptT ProtocolClientError IO a) -> m a +withClient c srv action = withClient_ c srv $ liftClient (protocolError @msg) . action -withLogClient :: (AgentMonad m, ProtocolServerClient msg) => AgentClient -> ProtocolServer -> QueueId -> ByteString -> (ProtocolClient msg -> ExceptT ProtocolClientError IO a) -> m a -withLogClient c srv qId cmdStr action = withLogClient_ c srv qId cmdStr $ liftClient . action +withLogClient :: forall m msg a. (AgentMonad m, ProtocolServerClient msg) => AgentClient -> ProtocolServer -> QueueId -> ByteString -> (ProtocolClient msg -> ExceptT ProtocolClientError IO a) -> m a +withLogClient c srv qId cmdStr action = withLogClient_ c srv qId cmdStr $ liftClient (protocolError @msg) . action -liftClient :: AgentMonad m => ExceptT ProtocolClientError IO a -> m a -liftClient = liftError protocolClientError +liftClient :: AgentMonad m => (ErrorType -> AgentErrorType) -> ExceptT ProtocolClientError IO a -> m a +liftClient = liftError . protocolClientError -protocolClientError :: ProtocolClientError -> AgentErrorType -protocolClientError = \case - PCEProtocolError e -> SMP e +protocolClientError :: (ErrorType -> AgentErrorType) -> ProtocolClientError -> AgentErrorType +protocolClientError protocolError_ = \case + PCEProtocolError e -> protocolError_ e PCEResponseError e -> BROKER $ RESPONSE e PCEUnexpectedResponse -> BROKER UNEXPECTED PCEResponseTimeout -> BROKER TIMEOUT @@ -428,14 +435,14 @@ sendConfirmation c sq@SndQueue {server, sndId, sndPublicKey = Just sndPublicKey, withLogClient_ c server sndId "SEND " $ \smp -> do let clientMsg = SMP.ClientMessage (SMP.PHConfirmation sndPublicKey) agentConfirmation msg <- agentCbEncrypt sq e2ePubKey $ smpEncode clientMsg - liftClient $ sendSMPMessage smp Nothing sndId msg + liftClient SMP $ sendSMPMessage smp Nothing sndId msg sendConfirmation _ _ _ = throwError $ INTERNAL "sendConfirmation called without snd_queue public key(s) in the database" sendInvitation :: forall m. AgentMonad m => AgentClient -> Compatible SMPQueueInfo -> ConnectionRequestUri 'CMInvitation -> ConnInfo -> m () sendInvitation c (Compatible SMPQueueInfo {smpServer, senderId, dhPublicKey}) connReq connInfo = withLogClient_ c smpServer senderId "SEND " $ \smp -> do msg <- mkInvitation - liftClient $ sendSMPMessage smp Nothing senderId msg + liftClient SMP $ sendSMPMessage smp Nothing senderId msg where mkInvitation :: m ByteString -- this is only encrypted with per-queue E2E, not with double ratchet @@ -469,7 +476,7 @@ sendAgentMessage c sq@SndQueue {server, sndId, sndPrivateKey} agentMsg = withLogClient_ c server sndId "SEND " $ \smp -> do let clientMsg = SMP.ClientMessage SMP.PHEmpty agentMsg msg <- agentCbEncrypt sq Nothing $ smpEncode clientMsg - liftClient $ sendSMPMessage smp (Just sndPrivateKey) sndId msg + liftClient SMP $ sendSMPMessage smp (Just sndPrivateKey) sndId msg agentNtfRegisterToken :: AgentMonad m => AgentClient -> NtfToken -> C.APublicVerifyKey -> C.PublicKeyX25519 -> m (NtfTokenId, C.PublicKeyX25519) agentNtfRegisterToken c NtfToken {deviceToken, ntfServer, ntfPrivKey} ntfPubKey pubDhKey = diff --git a/src/Simplex/Messaging/Agent/Protocol.hs b/src/Simplex/Messaging/Agent/Protocol.hs index dbe47d908..8be63a060 100644 --- a/src/Simplex/Messaging/Agent/Protocol.hs +++ b/src/Simplex/Messaging/Agent/Protocol.hs @@ -686,6 +686,8 @@ data AgentErrorType CONN {connErr :: ConnectionErrorType} | -- | SMP protocol errors forwarded to agent clients SMP {smpErr :: ErrorType} + | -- | NTF protocol errors forwarded to agent clients + NTF {ntfErr :: ErrorType} | -- | SMP server errors BROKER {brokerErr :: BrokerErrorType} | -- | errors of other agents @@ -774,6 +776,7 @@ instance StrEncoding AgentErrorType where "CMD " *> (CMD <$> parseRead1) <|> "CONN " *> (CONN <$> parseRead1) <|> "SMP " *> (SMP <$> strP) + <|> "NTF " *> (NTF <$> strP) <|> "BROKER RESPONSE " *> (BROKER . RESPONSE <$> strP) <|> "BROKER TRANSPORT " *> (BROKER . TRANSPORT <$> transportErrorP) <|> "BROKER " *> (BROKER <$> parseRead1) @@ -783,6 +786,7 @@ instance StrEncoding AgentErrorType where CMD e -> "CMD " <> bshow e CONN e -> "CONN " <> bshow e SMP e -> "SMP " <> strEncode e + NTF e -> "NTF " <> strEncode e BROKER (RESPONSE e) -> "BROKER RESPONSE " <> strEncode e BROKER (TRANSPORT e) -> "BROKER TRANSPORT " <> serializeTransportError e BROKER e -> "BROKER " <> bshow e diff --git a/src/Simplex/Messaging/Client/Agent.hs b/src/Simplex/Messaging/Client/Agent.hs index 7a62dc465..1f9d215cd 100644 --- a/src/Simplex/Messaging/Client/Agent.hs +++ b/src/Simplex/Messaging/Client/Agent.hs @@ -28,7 +28,7 @@ import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Protocol (BrokerMsg, ProtocolServer (..), QueueId, SMPServer) import Simplex.Messaging.TMap (TMap) import qualified Simplex.Messaging.TMap as TM -import Simplex.Messaging.Util (tryE, whenM) +import Simplex.Messaging.Util (tryE, whenM, ($>>=)) import System.Timeout (timeout) import UnliftIO (async, forConcurrently_) import UnliftIO.Exception (Exception) @@ -295,7 +295,7 @@ removeSub_ :: TMap SMPServer (TMap SMPSub C.APrivateSignKey) -> SMPServer -> SMP removeSub_ subs srv s = TM.lookup srv subs >>= mapM_ (TM.delete s) getSubKey :: TMap SMPServer (TMap SMPSub C.APrivateSignKey) -> SMPServer -> SMPSub -> STM (Maybe C.APrivateSignKey) -getSubKey subs srv s = fmap join . mapM (TM.lookup s) =<< TM.lookup srv subs +getSubKey subs srv s = TM.lookup srv subs $>>= TM.lookup s hasSub :: TMap SMPServer (TMap SMPSub C.APrivateSignKey) -> SMPServer -> SMPSub -> STM Bool hasSub subs srv s = maybe (pure False) (TM.member s) =<< TM.lookup srv subs diff --git a/src/Simplex/Messaging/Notifications/Protocol.hs b/src/Simplex/Messaging/Notifications/Protocol.hs index d5985d213..d3ad6d043 100644 --- a/src/Simplex/Messaging/Notifications/Protocol.hs +++ b/src/Simplex/Messaging/Notifications/Protocol.hs @@ -264,6 +264,7 @@ data NtfResponse | NRTkn NtfTknStatus | NRSub NtfSubStatus | NRPong + deriving (Show) instance ProtocolEncoding NtfResponse where type Tag NtfResponse = NtfResponseTag @@ -361,7 +362,7 @@ data NtfSubStatus NSEnd | -- | SMP AUTH error NSSMPAuth - deriving (Eq) + deriving (Eq, Show) instance Encoding NtfSubStatus where smpEncode = \case diff --git a/src/Simplex/Messaging/Notifications/Server.hs b/src/Simplex/Messaging/Notifications/Server.hs index babe98a70..f7100cf14 100644 --- a/src/Simplex/Messaging/Notifications/Server.hs +++ b/src/Simplex/Messaging/Notifications/Server.hs @@ -117,7 +117,7 @@ ntfPush s@NtfPushServer {pushQ} = liftIO . forever . runExceptT $ do (_, PNVerification _) -> do -- TODO check token status deliverNotification pp tkn ntf - atomically $ writeTVar tknStatus NTConfirmed + atomically $ modifyTVar tknStatus $ \status' -> if status' == NTActive then NTActive else NTConfirmed (NTActive, PNCheckMessages) -> do deliverNotification pp tkn ntf _ -> do @@ -166,26 +166,26 @@ verifyNtfTransmission :: verifyNtfTransmission (sig_, signed, (corrId, entId, _)) cmd = do st <- asks store case cmd of - NtfCmd SToken c@(TNEW n@(NewNtfTkn _ k _)) -> do - r_ <- atomically $ getNtfToken st entId + NtfCmd SToken c@(TNEW tkn@(NewNtfTkn _ k _)) -> do + r_ <- atomically $ getNtfTokenRegistration st tkn pure $ if verifyCmdSignature sig_ signed k then case r_ of - Just r@(NtfTkn NtfTknData {tknVerifyKey}) - | k == tknVerifyKey -> tknCmd r c + Just t@NtfTknData {tknVerifyKey} + | k == tknVerifyKey -> verifiedTknCmd t c | otherwise -> VRFailed - _ -> VRVerified (NtfReqNew corrId (ANE SToken n)) + _ -> VRVerified (NtfReqNew corrId (ANE SToken tkn)) else VRFailed NtfCmd SToken c -> do - r_ <- atomically $ getNtfToken st entId - pure $ case r_ of - Just r@(NtfTkn NtfTknData {tknVerifyKey}) - | verifyCmdSignature sig_ signed tknVerifyKey -> tknCmd r c + t_ <- atomically $ getNtfToken st entId + pure $ case t_ of + Just t@NtfTknData {tknVerifyKey} + | verifyCmdSignature sig_ signed tknVerifyKey -> verifiedTknCmd t c | otherwise -> VRFailed _ -> maybe False (dummyVerifyCmd signed) sig_ `seq` VRFailed _ -> pure VRFailed where - tknCmd r c = VRVerified (NtfReqCmd SToken r (corrId, entId, c)) + verifiedTknCmd t c = VRVerified (NtfReqCmd SToken (NtfTkn t) (corrId, entId, c)) client :: forall m. (MonadUnliftIO m, MonadReader NtfEnv m) => NtfServerClient -> NtfSubscriber -> NtfPushServer -> m () client NtfServerClient {rcvQ, sndQ} NtfSubscriber {subQ = _} NtfPushServer {pushQ, intervalNotifiers} = @@ -204,11 +204,11 @@ client NtfServerClient {rcvQ, sndQ} NtfSubscriber {subQ = _} NtfPushServer {push tknId <- getId regCode <- getRegCode atomically $ do - tkn <- mkNtfTknData newTkn ks dhSecret regCode + tkn <- mkNtfTknData tknId newTkn ks dhSecret regCode addNtfToken st tknId tkn writeTBQueue pushQ (tkn, PNVerification regCode) pure (corrId, "", NRId tknId srvDhPubKey) - NtfReqCmd SToken (NtfTkn tkn@NtfTknData {tknStatus, tknRegCode, tknDhSecret, tknDhKeys = (srvDhPubKey, srvDhPrivKey)}) (corrId, tknId, cmd) -> do + NtfReqCmd SToken (NtfTkn tkn@NtfTknData {ntfTknId, tknStatus, tknRegCode, tknDhSecret, tknDhKeys = (srvDhPubKey, srvDhPrivKey)}) (corrId, tknId, cmd) -> do status <- readTVarIO tknStatus (corrId,tknId,) <$> case cmd of TNEW (NewNtfTkn _ _ dhPubKey) -> do @@ -218,12 +218,15 @@ client NtfServerClient {rcvQ, sndQ} NtfSubscriber {subQ = _} NtfPushServer {push if tknDhSecret == dhSecret then do atomically $ writeTBQueue pushQ (tkn, PNVerification tknRegCode) - pure $ NRId tknId srvDhPubKey + pure $ NRId ntfTknId srvDhPubKey else pure $ NRErr AUTH TVFY code -- this allows repeated verification for cases when client connection dropped before server response | (status == NTRegistered || status == NTConfirmed || status == NTActive) && tknRegCode == code -> do logDebug "TVFY - token verified" + st <- asks store atomically $ writeTVar tknStatus NTActive + tIds <- atomically $ removeInactiveTokenRegistrations st tkn + forM_ tIds cancelInvervalNotifications pure NROk | otherwise -> do logDebug "TVFY - incorrect code or token status" @@ -233,12 +236,12 @@ client NtfServerClient {rcvQ, sndQ} NtfSubscriber {subQ = _} NtfPushServer {push logDebug "TDEL" st <- asks store atomically $ deleteNtfToken st tknId + cancelInvervalNotifications tknId pure NROk - TCRN 0 -> + TCRN 0 -> do logDebug "TCRN 0" - >> atomically (TM.lookupDelete tknId intervalNotifiers) - >>= mapM_ (uninterruptibleCancel . action) - >> pure NROk + cancelInvervalNotifications tknId + pure NROk TCRN int | int < 20 -> pure $ NRErr QUOTA | otherwise -> do @@ -274,6 +277,10 @@ client NtfServerClient {rcvQ, sndQ} NtfSubscriber {subQ = _} NtfPushServer {push getRandomBytes n = do gVar <- asks idsDrg atomically (C.pseudoRandomBytes n gVar) + cancelInvervalNotifications :: NtfTokenId -> m () + cancelInvervalNotifications tknId = + atomically (TM.lookupDelete tknId intervalNotifiers) + >>= mapM_ (uninterruptibleCancel . action) -- NReqCreate corrId tokenId smpQueue -> pure (corrId, "", NROk) -- do diff --git a/src/Simplex/Messaging/Notifications/Server/Store.hs b/src/Simplex/Messaging/Notifications/Server/Store.hs index bc7040b67..e39e7d3e1 100644 --- a/src/Simplex/Messaging/Notifications/Server/Store.hs +++ b/src/Simplex/Messaging/Notifications/Server/Store.hs @@ -2,30 +2,38 @@ {-# LANGUAGE DuplicateRecordFields #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE KindSignatures #-} +{-# LANGUAGE LambdaCase #-} {-# LANGUAGE NamedFieldPuns #-} +{-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE ScopedTypeVariables #-} +{-# LANGUAGE TupleSections #-} module Simplex.Messaging.Notifications.Server.Store where import Control.Concurrent.STM +import Control.Monad +import Data.ByteString.Char8 (ByteString) +import qualified Data.Map.Strict as M import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Notifications.Protocol import Simplex.Messaging.TMap (TMap) import qualified Simplex.Messaging.TMap as TM -import Simplex.Messaging.Util ((<$$>)) +import Simplex.Messaging.Util (whenM, ($>>=)) data NtfStore = NtfStore { tokens :: TMap NtfTokenId NtfTknData, - tokenIds :: TMap DeviceToken NtfTokenId + tokenRegistrations :: TMap DeviceToken (TMap ByteString NtfTokenId) } newNtfStore :: STM NtfStore newNtfStore = do tokens <- TM.empty - tokenIds <- TM.empty - pure NtfStore {tokens, tokenIds} + tokenRegistrations <- TM.empty + pure NtfStore {tokens, tokenRegistrations} data NtfTknData = NtfTknData - { token :: DeviceToken, + { ntfTknId :: NtfTokenId, + token :: DeviceToken, tknStatus :: TVar NtfTknStatus, tknVerifyKey :: C.APublicVerifyKey, tknDhKeys :: C.KeyPair 'C.X25519, @@ -33,10 +41,10 @@ data NtfTknData = NtfTknData tknRegCode :: NtfRegCode } -mkNtfTknData :: NewNtfEntity 'Token -> C.KeyPair 'C.X25519 -> C.DhSecretX25519 -> NtfRegCode -> STM NtfTknData -mkNtfTknData (NewNtfTkn token tknVerifyKey _) tknDhKeys tknDhSecret tknRegCode = do +mkNtfTknData :: NtfTokenId -> NewNtfEntity 'Token -> C.KeyPair 'C.X25519 -> C.DhSecretX25519 -> NtfRegCode -> STM NtfTknData +mkNtfTknData ntfTknId (NewNtfTkn token tknVerifyKey _) tknDhKeys tknDhSecret tknRegCode = do tknStatus <- newTVar NTRegistered - pure NtfTknData {token, tknStatus, tknVerifyKey, tknDhKeys, tknDhSecret, tknRegCode} + pure NtfTknData {ntfTknId, token, tknStatus, tknVerifyKey, tknDhKeys, tknDhSecret, tknRegCode} -- data NtfSubscriptionsStore = NtfSubscriptionsStore @@ -58,19 +66,57 @@ data NtfEntityRec (e :: NtfEntity) where NtfTkn :: NtfTknData -> NtfEntityRec 'Token NtfSub :: NtfSubData -> NtfEntityRec 'Subscription -data ANtfEntityRec = forall e. NtfEntityI e => NER (SNtfEntity e) (NtfEntityRec e) - -getNtfToken :: NtfStore -> NtfTokenId -> STM (Maybe (NtfEntityRec 'Token)) -getNtfToken st tknId = NtfTkn <$$> TM.lookup tknId (tokens st) +getNtfToken :: NtfStore -> NtfTokenId -> STM (Maybe NtfTknData) +getNtfToken st tknId = TM.lookup tknId (tokens st) addNtfToken :: NtfStore -> NtfTokenId -> NtfTknData -> STM () -addNtfToken st tknId tkn@NtfTknData {token} = do +addNtfToken st tknId tkn@NtfTknData {token, tknVerifyKey} = do TM.insert tknId tkn $ tokens st - TM.insert token tknId $ tokenIds st + TM.lookup token regs >>= \case + Just tIds -> TM.insert regKey tknId tIds + _ -> do + tIds <- TM.singleton regKey tknId + TM.insert token tIds regs + where + regs = tokenRegistrations st + regKey = C.toPubKey C.pubKeyBytes tknVerifyKey + +getNtfTokenRegistration :: NtfStore -> NewNtfEntity 'Token -> STM (Maybe NtfTknData) +getNtfTokenRegistration st (NewNtfTkn token tknVerifyKey _) = + TM.lookup token (tokenRegistrations st) + $>>= TM.lookup regKey + $>>= (`TM.lookup` tokens st) + where + regKey = C.toPubKey C.pubKeyBytes tknVerifyKey + +removeInactiveTokenRegistrations :: NtfStore -> NtfTknData -> STM [NtfTokenId] +removeInactiveTokenRegistrations st NtfTknData {ntfTknId = tId, token} = + TM.lookup token (tokenRegistrations st) + >>= maybe (pure []) removeRegs + where + removeRegs :: TMap ByteString NtfTokenId -> STM [NtfTokenId] + removeRegs tknRegs = do + tIds <- filter ((/= tId) . snd) . M.assocs <$> readTVar tknRegs + forM_ tIds $ \(regKey, tId') -> do + TM.delete regKey tknRegs + TM.delete tId' $ tokens st + pure $ map snd tIds deleteNtfToken :: NtfStore -> NtfTokenId -> STM () deleteNtfToken st tknId = do - TM.lookupDelete tknId (tokens st) >>= mapM_ (\NtfTknData {token} -> TM.delete token $ tokenIds st) + TM.lookupDelete tknId (tokens st) + >>= mapM_ + ( \NtfTknData {token, tknVerifyKey} -> + TM.lookup token regs + >>= mapM_ + ( \tIds -> do + TM.delete (regKey tknVerifyKey) tIds + whenM (TM.null tIds) $ TM.delete token regs + ) + ) + where + regs = tokenRegistrations st + regKey = C.toPubKey C.pubKeyBytes -- getNtfRec :: NtfStore -> SNtfEntity e -> NtfEntityId -> STM (Maybe (NtfEntityRec e)) -- getNtfRec st ent entId = case ent of diff --git a/src/Simplex/Messaging/Server.hs b/src/Simplex/Messaging/Server.hs index 6809899f8..c2ce106ed 100644 --- a/src/Simplex/Messaging/Server.hs +++ b/src/Simplex/Messaging/Server.hs @@ -98,7 +98,7 @@ smpServer started = do m () serverThread s subQ subs clientSubs unsub = forever $ do atomically updateSubscribers - >>= fmap join . mapM endPreviousSubscriptions + $>>= endPreviousSubscriptions >>= mapM_ unsub where updateSubscribers :: STM (Maybe (QueueId, Client)) @@ -110,8 +110,7 @@ smpServer started = do else do yes <- readTVar $ connected c' pure $ if yes then Just (qId, c') else Nothing - TM.lookupInsert qId clnt (subs s) - >>= fmap join . mapM clientToBeNotified + TM.lookupInsert qId clnt (subs s) $>>= clientToBeNotified endPreviousSubscriptions :: (QueueId, Client) -> m (Maybe s) endPreviousSubscriptions (qId, c) = do void . forkIO . atomically $ diff --git a/src/Simplex/Messaging/Server/QueueStore/STM.hs b/src/Simplex/Messaging/Server/QueueStore/STM.hs index 401e7ee30..d04c8f14b 100644 --- a/src/Simplex/Messaging/Server/QueueStore/STM.hs +++ b/src/Simplex/Messaging/Server/QueueStore/STM.hs @@ -18,7 +18,7 @@ import Simplex.Messaging.Protocol import Simplex.Messaging.Server.QueueStore import Simplex.Messaging.TMap (TMap) import qualified Simplex.Messaging.TMap as TM -import Simplex.Messaging.Util (ifM) +import Simplex.Messaging.Util (ifM, ($>>=)) import UnliftIO.STM data QueueStore = QueueStore @@ -51,9 +51,8 @@ instance MonadQueueStore QueueStore STM where where getVar = case party of SRecipient -> TM.lookup qId queues - SSender -> TM.lookup qId senders >>= get - SNotifier -> TM.lookup qId notifiers >>= get - get = fmap join . mapM (`TM.lookup` queues) + SSender -> TM.lookup qId senders $>>= (`TM.lookup` queues) + SNotifier -> TM.lookup qId notifiers $>>= (`TM.lookup` queues) secureQueue :: QueueStore -> RecipientId -> SndPublicVerifyKey -> STM (Either ErrorType QueueRec) secureQueue QueueStore {queues} rId sKey = @@ -91,4 +90,4 @@ toResult :: Maybe a -> Either ErrorType a toResult = maybe (Left AUTH) Right withQueue :: RecipientId -> TMap RecipientId (TVar QueueRec) -> (TVar QueueRec -> STM (Maybe a)) -> STM (Either ErrorType a) -withQueue rId queues f = toResult <$> (TM.lookup rId queues >>= fmap join . mapM f) +withQueue rId queues f = toResult <$> TM.lookup rId queues $>>= f diff --git a/src/Simplex/Messaging/TMap.hs b/src/Simplex/Messaging/TMap.hs index b5bc01253..761a41c93 100644 --- a/src/Simplex/Messaging/TMap.hs +++ b/src/Simplex/Messaging/TMap.hs @@ -2,6 +2,7 @@ module Simplex.Messaging.TMap ( TMap, empty, singleton, + Simplex.Messaging.TMap.null, Simplex.Messaging.TMap.lookup, member, insert, @@ -30,6 +31,10 @@ singleton :: k -> a -> STM (TMap k a) singleton k v = newTVar $ M.singleton k v {-# INLINE singleton #-} +null :: TMap k a -> STM Bool +null m = M.null <$> readTVar m +{-# INLINE null #-} + lookup :: Ord k => k -> TMap k a -> STM (Maybe a) lookup k m = M.lookup k <$> readTVar m {-# INLINE lookup #-} diff --git a/src/Simplex/Messaging/Util.hs b/src/Simplex/Messaging/Util.hs index ea53bc60e..0d741bb43 100644 --- a/src/Simplex/Messaging/Util.hs +++ b/src/Simplex/Messaging/Util.hs @@ -65,3 +65,6 @@ whenM b a = ifM b a $ pure () unlessM :: Monad m => m Bool -> m () -> m () unlessM b = ifM b $ pure () {-# INLINE unlessM #-} + +($>>=) :: (Monad m, Monad f, Traversable f) => m (f a) -> (a -> m (f b)) -> m (f b) +f $>>= g = f >>= fmap join . mapM g diff --git a/tests/AgentTests/NotificationTests.hs b/tests/AgentTests/NotificationTests.hs index 9b6a9e0f6..752c97760 100644 --- a/tests/AgentTests/NotificationTests.hs +++ b/tests/AgentTests/NotificationTests.hs @@ -3,6 +3,9 @@ module AgentTests.NotificationTests where +-- import Control.Logger.Simple (LogConfig (..), LogLevel (..), setLogLevel, withGlobalLogging) + +import Control.Concurrent (threadDelay) import Control.Monad.Except import qualified Data.Aeson as J import qualified Data.Aeson.Types as JT @@ -11,21 +14,36 @@ import qualified Data.ByteString.Base64.URL as U import Data.ByteString.Char8 (ByteString) import Data.Text.Encoding (encodeUtf8) import NtfClient -import SMPAgentClient (agentCfg, initAgentServers) +import SMPAgentClient (agentCfg, initAgentServers, testDB, testDB2) import Simplex.Messaging.Agent +import Simplex.Messaging.Agent.Env.SQLite (AgentConfig (..)) import Simplex.Messaging.Agent.Protocol import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Notifications.Protocol import Simplex.Messaging.Notifications.Server.Push.APNS +import Simplex.Messaging.Protocol (ErrorType (AUTH)) import Simplex.Messaging.Transport (ATransport) +import Simplex.Messaging.Util (tryE) +import System.Directory (removeFile) import Test.Hspec import UnliftIO.STM notificationTests :: ATransport -> Spec -notificationTests t = do - describe "Managing notification tokens" $ - it "should register and verify notification token" $ - withAPNSMockServer $ \apns -> withNtfServer t $ testNotificationToken apns +notificationTests t = + after_ (removeFile testDB) $ + describe "Managing notification tokens" $ do + it "should register and verify notification token" $ + withAPNSMockServer $ \apns -> + withNtfServer t $ testNotificationToken apns + it "should allow repeated registration with the same credentials" $ \_ -> + withAPNSMockServer $ \apns -> + withNtfServer t $ testNtfTokenRepeatRegistration apns + it "should allow the second registration with different credentials and delete the first after verification" $ \_ -> + withAPNSMockServer $ \apns -> + withNtfServer t $ testNtfTokenSecondRegistration apns + it "should re-register token when notification server is restarted" $ \_ -> + withAPNSMockServer $ \apns -> + testNtfTokenServerRestart t apns testNotificationToken :: APNSMockServer -> IO () testNotificationToken APNSMockServer {apnsQ} = do @@ -40,10 +58,110 @@ testNotificationToken APNSMockServer {apnsQ} = do liftIO $ sendApnsResponse APNSRespOk verifyNtfToken a tkn verification nonce enableNtfCron a tkn 30 + NTActive <- checkNtfToken a tkn + deleteNtfToken a tkn + -- agent deleted this token + Left (CMD PROHIBITED) <- tryE $ checkNtfToken a tkn pure () pure () - where - (.->) :: J.Value -> J.Key -> ExceptT AgentErrorType IO ByteString - v .-> key = do - J.Object o <- pure v - liftEither . bimap INTERNAL (U.decodeLenient . encodeUtf8) $ JT.parseEither (J..: key) o + +(.->) :: J.Value -> J.Key -> ExceptT AgentErrorType IO ByteString +v .-> key = do + J.Object o <- pure v + liftEither . bimap INTERNAL (U.decodeLenient . encodeUtf8) $ JT.parseEither (J..: key) o + +-- logCfg :: LogConfig +-- logCfg = LogConfig {lc_file = Nothing, lc_stderr = True} + +testNtfTokenRepeatRegistration :: APNSMockServer -> IO () +testNtfTokenRepeatRegistration APNSMockServer {apnsQ} = do + -- setLogLevel LogError -- LogDebug + -- withGlobalLogging logCfg $ do + a <- getSMPAgentClient agentCfg initAgentServers + Right () <- runExceptT $ do + let tkn = DeviceToken PPApns "abcd" + registerNtfToken a tkn + APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData}, sendApnsResponse} <- + atomically $ readTBQueue apnsQ + verification <- ntfData .-> "verification" + nonce <- C.cbNonce <$> ntfData .-> "nonce" + liftIO $ sendApnsResponse APNSRespOk + registerNtfToken a tkn + APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData'}, sendApnsResponse = sendApnsResponse'} <- + atomically $ readTBQueue apnsQ + _ <- ntfData' .-> "verification" + _ <- C.cbNonce <$> ntfData' .-> "nonce" + liftIO $ sendApnsResponse' APNSRespOk + -- can still use the first verification code, it is the same after decryption + verifyNtfToken a tkn verification nonce + enableNtfCron a tkn 30 + NTActive <- checkNtfToken a tkn + pure () + pure () + +testNtfTokenSecondRegistration :: APNSMockServer -> IO () +testNtfTokenSecondRegistration APNSMockServer {apnsQ} = do + -- setLogLevel LogError -- LogDebug + -- withGlobalLogging logCfg $ do + a <- getSMPAgentClient agentCfg initAgentServers + a' <- getSMPAgentClient agentCfg {dbFile = testDB2} initAgentServers + Right () <- runExceptT $ do + let tkn = DeviceToken PPApns "abcd" + registerNtfToken a tkn + APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData}, sendApnsResponse} <- + atomically $ readTBQueue apnsQ + verification <- ntfData .-> "verification" + nonce <- C.cbNonce <$> ntfData .-> "nonce" + liftIO $ sendApnsResponse APNSRespOk + verifyNtfToken a tkn verification nonce + + registerNtfToken a' tkn + APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData'}, sendApnsResponse = sendApnsResponse'} <- + atomically $ readTBQueue apnsQ + verification' <- ntfData' .-> "verification" + nonce' <- C.cbNonce <$> ntfData' .-> "nonce" + liftIO $ sendApnsResponse' APNSRespOk + + -- at this point the first token is still active + NTActive <- checkNtfToken a tkn + -- and the second is not yet verified + NTConfirmed <- checkNtfToken a' tkn + -- now the second token registration is verified + verifyNtfToken a' tkn verification' nonce' + -- the first registration is removed + Left (NTF AUTH) <- tryE $ checkNtfToken a tkn + -- and the second is active + NTActive <- checkNtfToken a' tkn + enableNtfCron a' tkn 30 + pure () + pure () + +testNtfTokenServerRestart :: ATransport -> APNSMockServer -> IO () +testNtfTokenServerRestart t APNSMockServer {apnsQ} = do + a <- getSMPAgentClient agentCfg initAgentServers + let tkn = DeviceToken PPApns "abcd" + Right ntfData <- withNtfServer t . runExceptT $ do + registerNtfToken a tkn + APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData}, sendApnsResponse} <- + atomically $ readTBQueue apnsQ + liftIO $ sendApnsResponse APNSRespOk + pure ntfData + -- the new agent is created as otherwise when running the tests in CI the old agent was keeping the connection to the server + threadDelay 1000000 + disconnectAgentClient a + a' <- getSMPAgentClient agentCfg initAgentServers + -- server stopped before token is verified, so now the attempt to verify it will return AUTH error but re-register token, + -- so that repeat verification happens without restarting the clients, when notification arrives + Right () <- withNtfServer t . runExceptT $ do + verification <- ntfData .-> "verification" + nonce <- C.cbNonce <$> ntfData .-> "nonce" + Left (NTF AUTH) <- tryE $ verifyNtfToken a' tkn verification nonce + APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData'}, sendApnsResponse = sendApnsResponse'} <- + atomically $ readTBQueue apnsQ + verification' <- ntfData' .-> "verification" + nonce' <- C.cbNonce <$> ntfData' .-> "nonce" + liftIO $ sendApnsResponse' APNSRespOk + verifyNtfToken a' tkn verification' nonce' + NTActive <- checkNtfToken a' tkn + enableNtfCron a' tkn 30 + pure () From f718a84335ae2a84bd314da2e2cc3e91e6c3b683 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Thu, 21 Apr 2022 17:07:39 +0100 Subject: [PATCH 011/131] change log level to error --- apps/ntf-server/Main.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/ntf-server/Main.hs b/apps/ntf-server/Main.hs index 41625d6f2..7a05d0a21 100644 --- a/apps/ntf-server/Main.hs +++ b/apps/ntf-server/Main.hs @@ -21,7 +21,7 @@ logCfg = LogConfig {lc_file = Nothing, lc_stderr = True} main :: IO () main = do - setLogLevel LogDebug -- TODO change to LogError in production + setLogLevel LogError withGlobalLogging logCfg $ protocolServerCLI ntfServerCLIConfig runNtfServer ntfServerCLIConfig :: ServerCLIConfig NtfServerConfig From a2204c9b8440ac3a477ce33b4649fe6e321a4915 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Thu, 21 Apr 2022 17:15:41 +0100 Subject: [PATCH 012/131] remove comments, add EOL --- src/Simplex/Messaging/Agent/Store.hs | 3 +-- .../SQLite/Migrations/M20220404_ntf_subscriptions_draft.hs | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Simplex/Messaging/Agent/Store.hs b/src/Simplex/Messaging/Agent/Store.hs index 6931f53be..ec564ceb8 100644 --- a/src/Simplex/Messaging/Agent/Store.hs +++ b/src/Simplex/Messaging/Agent/Store.hs @@ -80,8 +80,7 @@ class Monad m => MonadAgentStore s m where -- Notification device token persistence createNtfToken :: s -> NtfToken -> m () - -- TODO this should also return old tokens so that they are deleted from the server - getDeviceNtfToken :: s -> DeviceToken -> m (Maybe NtfToken, [NtfToken]) -- return current token if it exists + getDeviceNtfToken :: s -> DeviceToken -> m (Maybe NtfToken, [NtfToken]) updateNtfTokenRegistration :: s -> NtfToken -> NtfTokenId -> C.DhSecretX25519 -> m () updateNtfToken :: s -> NtfToken -> NtfTknStatus -> Maybe NtfTknAction -> m () removeNtfToken :: s -> NtfToken -> m () diff --git a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220404_ntf_subscriptions_draft.hs b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220404_ntf_subscriptions_draft.hs index 5f58ca0fd..7ddedddd3 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220404_ntf_subscriptions_draft.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220404_ntf_subscriptions_draft.hs @@ -35,4 +35,4 @@ CREATE TABLE ntf_subscriptions ( FOREIGN KEY (smp_host, smp_port, smp_ntf_id) REFERENCES rcv_queues (host, port, ntf_id) ON DELETE RESTRICT ON UPDATE CASCADE ) WITHOUT ROWID; -|] \ No newline at end of file +|] From 7774fc327173ac86f5b94bc16742296f1d1fb85f Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Thu, 21 Apr 2022 18:14:41 +0100 Subject: [PATCH 013/131] update swift flag --- simplexmq.cabal | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/simplexmq.cabal b/simplexmq.cabal index 4a16a969c..e5f49232f 100644 --- a/simplexmq.cabal +++ b/simplexmq.cabal @@ -136,7 +136,7 @@ library , x509-store ==1.6.* , x509-validation ==1.6.* if flag(swift) - ghc-options: -DswiftJSON + cpp-options: -DswiftJSON default-language: Haskell2010 executable ntf-server From 7652ece0459cef59a74d4febd81da26f3fcac97e Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Fri, 22 Apr 2022 17:48:53 +0100 Subject: [PATCH 014/131] registerNtfToken returns token status --- src/Simplex/Messaging/Agent.hs | 27 ++++++++++--------- .../Notifications/Server/Push/testpush.sh | 6 ++--- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/Simplex/Messaging/Agent.hs b/src/Simplex/Messaging/Agent.hs index a2aeb9d68..9ee45f362 100644 --- a/src/Simplex/Messaging/Agent.hs +++ b/src/Simplex/Messaging/Agent.hs @@ -162,7 +162,7 @@ setNtfServers :: AgentErrorMonad m => AgentClient -> [NtfServer] -> m () setNtfServers c = withAgentEnv c . setNtfServers' c -- | Register device notifications token -registerNtfToken :: AgentErrorMonad m => AgentClient -> DeviceToken -> m () +registerNtfToken :: AgentErrorMonad m => AgentClient -> DeviceToken -> m NtfTknStatus registerNtfToken c = withAgentEnv c . registerNtfToken' c -- | Verify device notifications token @@ -525,25 +525,25 @@ setSMPServers' :: AgentMonad m => AgentClient -> NonEmpty SMPServer -> m () setSMPServers' c servers = do atomically $ writeTVar (smpServers c) servers -registerNtfToken' :: forall m. AgentMonad m => AgentClient -> DeviceToken -> m () +registerNtfToken' :: forall m. AgentMonad m => AgentClient -> DeviceToken -> m NtfTknStatus registerNtfToken' c deviceToken = withStore (`getDeviceNtfToken` deviceToken) >>= \case (Just tkn@NtfToken {ntfTokenId, ntfTknStatus, ntfTknAction}, prevTokens) -> do mapM_ (deleteToken_ c) prevTokens case (ntfTokenId, ntfTknAction) of - (Nothing, Just NTARegister) -> registerToken tkn + (Nothing, Just NTARegister) -> registerToken tkn $> NTRegistered -- TODO minimal time before repeat registration - (Just _, Nothing) -> when (ntfTknStatus == NTRegistered) $ registerToken tkn + (Just _, Nothing) -> when (ntfTknStatus == NTRegistered) (registerToken tkn) $> NTRegistered (Just tknId, Just (NTAVerify code)) -> t tkn (NTActive, Just NTACheck) $ agentNtfVerifyToken c tknId tkn code (Just tknId, Just (NTACron interval)) -> t tkn (cronSuccess interval) $ agentNtfEnableCron c tknId tkn interval - (Just _tknId, Just NTACheck) -> pure () -- TODO + (Just _tknId, Just NTACheck) -> pure ntfTknStatus -- TODO -- agentNtfCheckToken c tknId tkn >>= \case (Just tknId, Just NTADelete) -> do agentNtfDeleteToken c tknId tkn - withStore $ \st -> removeNtfToken st tkn - _ -> pure () + withStore $ \st -> removeNtfToken st tkn $> NTExpired + _ -> pure ntfTknStatus _ -> getNtfServer c >>= \case Just ntfServer -> @@ -554,6 +554,7 @@ registerNtfToken' c deviceToken = let tkn = newNtfToken deviceToken ntfServer tknKeys dhKeys withStore $ \st -> createNtfToken st tkn registerToken tkn + pure NTRegistered _ -> throwError $ CMD PROHIBITED where t tkn = withToken c tkn Nothing @@ -569,7 +570,7 @@ verifyNtfToken' c deviceToken code nonce = withStore (`getDeviceNtfToken` deviceToken) >>= \case (Just tkn@NtfToken {ntfTokenId = Just tknId, ntfDhSecret = Just dhSecret}, _) -> do code' <- liftEither . bimap cryptoError NtfRegCode $ C.cbDecrypt dhSecret nonce code - withToken c tkn (Just (NTConfirmed, NTAVerify code')) (NTActive, Just NTACheck) $ + void . withToken c tkn (Just (NTConfirmed, NTAVerify code')) (NTActive, Just NTACheck) $ agentNtfVerifyToken c tknId tkn code' _ -> throwError $ CMD PROHIBITED @@ -578,7 +579,7 @@ enableNtfCron' c deviceToken interval = do when (interval < 20) . throwError $ CMD PROHIBITED withStore (`getDeviceNtfToken` deviceToken) >>= \case (Just tkn@NtfToken {ntfTokenId = Just tknId, ntfTknStatus = NTActive}, _) -> - withToken c tkn (Just (NTActive, NTACron interval)) (cronSuccess interval) $ + void . withToken c tkn (Just (NTActive, NTACron interval)) (cronSuccess interval) $ agentNtfEnableCron c tknId tkn interval _ -> throwError $ CMD PROHIBITED @@ -608,16 +609,16 @@ deleteToken_ c tkn@NtfToken {ntfTokenId, ntfTknStatus} = do e -> throwError e withStore $ \st -> removeNtfToken st tkn -withToken :: AgentMonad m => AgentClient -> NtfToken -> Maybe (NtfTknStatus, NtfTknAction) -> (NtfTknStatus, Maybe NtfTknAction) -> m a -> m a +withToken :: AgentMonad m => AgentClient -> NtfToken -> Maybe (NtfTknStatus, NtfTknAction) -> (NtfTknStatus, Maybe NtfTknAction) -> m a -> m NtfTknStatus withToken c tkn@NtfToken {deviceToken} from_ (toStatus, toAction_) f = do forM_ from_ $ \(status, action) -> withStore $ \st -> updateNtfToken st tkn status (Just action) tryError f >>= \case - Right res -> do + Right _ -> do withStore $ \st -> updateNtfToken st tkn toStatus toAction_ - pure res + pure toStatus Left e@(NTF AUTH) -> do withStore $ \st -> removeNtfToken st tkn - registerNtfToken' c deviceToken + void $ registerNtfToken' c deviceToken throwError e Left e -> throwError e diff --git a/src/Simplex/Messaging/Notifications/Server/Push/testpush.sh b/src/Simplex/Messaging/Notifications/Server/Push/testpush.sh index 204fce599..2af08356e 100755 --- a/src/Simplex/Messaging/Notifications/Server/Push/testpush.sh +++ b/src/Simplex/Messaging/Notifications/Server/Push/testpush.sh @@ -4,7 +4,7 @@ export TEAM_ID=5NN7GUYB6T # export APNS_KEY_FILE="" # export APNS_KEY_ID="" export TOPIC=chat.simplex.app -# export DEVICE_TOKEN="" +# export DEVICE_TOKEN= export APNS_HOST_NAME=api.sandbox.push.apple.com export JWT_ISSUE_TIME=$(date +%s) @@ -15,6 +15,6 @@ export JWT_HEADER_CLAIMS="${JWT_HEADER}.${JWT_CLAIMS}" export JWT_SIGNED_HEADER_CLAIMS=$(printf "${JWT_HEADER_CLAIMS}" | openssl dgst -binary -sha256 -sign "${APNS_KEY_FILE}" | openssl base64 -e -A | tr -- '+/' '-_' | tr -d =) export AUTHENTICATION_TOKEN="${JWT_HEADER}.${JWT_CLAIMS}.${JWT_SIGNED_HEADER_CLAIMS}" -# curl -v --header "apns-topic: $TOPIC" --header "apns-push-type: alert" --header "authorization: bearer $AUTHENTICATION_TOKEN" --data '{"aps":{"alert":"you have a new message"}}' --http2 https://${APNS_HOST_NAME}/3/device/${DEVICE_TOKEN} +curl -v --header "apns-topic: $TOPIC" --header "apns-push-type: alert" --header "authorization: bearer $AUTHENTICATION_TOKEN" --data '{"aps":{"alert":"you have a new message"},"data":{"test":"123"}}' --http2 https://${APNS_HOST_NAME}/3/device/${DEVICE_TOKEN} -curl -v --header "apns-topic: $TOPIC" --header "apns-push-type: background" --header "apns-priority: 5" --header "authorization: bearer $AUTHENTICATION_TOKEN" --data '{"aps":{"content-available":1}}' --http2 https://${APNS_HOST_NAME}/3/device/${DEVICE_TOKEN} +# curl -v --header "apns-topic: $TOPIC" --header "apns-push-type: background" --header "apns-priority: 5" --header "authorization: bearer $AUTHENTICATION_TOKEN" --data '{"aps":{"content-available":1}}' --http2 https://${APNS_HOST_NAME}/3/device/${DEVICE_TOKEN} From 633cd675b5cd9476dc9624512d552f3851fab34c Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Fri, 22 Apr 2022 20:02:02 +0100 Subject: [PATCH 015/131] ToJSON instance for NtfTknStatus (#362) --- src/Simplex/Messaging/Notifications/Protocol.hs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Simplex/Messaging/Notifications/Protocol.hs b/src/Simplex/Messaging/Notifications/Protocol.hs index d3ad6d043..3640004e0 100644 --- a/src/Simplex/Messaging/Notifications/Protocol.hs +++ b/src/Simplex/Messaging/Notifications/Protocol.hs @@ -11,6 +11,8 @@ module Simplex.Messaging.Notifications.Protocol where import Data.Aeson (FromJSON (..), ToJSON (..)) +import qualified Data.Aeson as J +import qualified Data.Aeson.Encoding as JE import qualified Data.Attoparsec.ByteString.Char8 as A import Data.ByteString.Char8 (ByteString) import qualified Data.ByteString.Char8 as B @@ -417,6 +419,10 @@ instance FromField NtfTknStatus where fromField = fromTextField_ $ either (const instance ToField NtfTknStatus where toField = toField . decodeLatin1 . smpEncode +instance ToJSON NtfTknStatus where + toEncoding = JE.text . decodeLatin1 . smpEncode + toJSON = J.String . decodeLatin1 . smpEncode + checkEntity :: forall t e e'. (NtfEntityI e, NtfEntityI e') => t e' -> Either String (t e) checkEntity c = case testEquality (sNtfEntity @e) (sNtfEntity @e') of Just Refl -> Right c From c6dde772b459a2d8f392ad5455d6f8fd6f16e867 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Mon, 25 Apr 2022 08:26:52 +0100 Subject: [PATCH 016/131] batch all connections in DOWN/UP agent messages (#363) --- src/Simplex/Messaging/Agent/Client.hs | 31 ++++++++++++++----------- src/Simplex/Messaging/Agent/Protocol.hs | 19 ++++++++++----- src/Simplex/Messaging/Protocol.hs | 2 ++ tests/AgentTests.hs | 25 +++++++++++--------- tests/AgentTests/FunctionalAPITests.hs | 11 +++++---- tests/AgentTests/NotificationTests.hs | 12 +++++----- tests/SMPAgentClient.hs | 5 +++- 7 files changed, 64 insertions(+), 41 deletions(-) diff --git a/src/Simplex/Messaging/Agent/Client.hs b/src/Simplex/Messaging/Agent/Client.hs index e063ae9bc..44c843074 100644 --- a/src/Simplex/Messaging/Agent/Client.hs +++ b/src/Simplex/Messaging/Agent/Client.hs @@ -55,7 +55,7 @@ import qualified Data.ByteString.Char8 as B import Data.List.NonEmpty (NonEmpty) import Data.Map.Strict (Map) import qualified Data.Map.Strict as M -import Data.Maybe (isNothing) +import Data.Maybe (catMaybes, isNothing) import Data.Text.Encoding import Data.Word (Word16) import Simplex.Messaging.Agent.Env.SQLite @@ -72,10 +72,10 @@ import Simplex.Messaging.Protocol (BrokerMsg, ErrorType, ProtocolServer (..), Qu import qualified Simplex.Messaging.Protocol as SMP import Simplex.Messaging.TMap (TMap) import qualified Simplex.Messaging.TMap as TM -import Simplex.Messaging.Util (bshow, liftEitherError, liftError, tryError, whenM) +import Simplex.Messaging.Util (bshow, ifM, liftEitherError, liftError, tryError) import Simplex.Messaging.Version import System.Timeout (timeout) -import UnliftIO (async, forConcurrently_) +import UnliftIO (async, forConcurrently) import qualified UnliftIO.Exception as E import UnliftIO.STM @@ -181,7 +181,8 @@ getSMPServerClient c@AgentClient {smpClients, msgQ} srv = serverDown :: UnliftIO m -> Map ConnId RcvQueue -> IO () serverDown u cs = unless (M.null cs) $ do - mapM_ (notifySub DOWN) $ M.keysSet cs + let conns = M.keys cs + unless (null conns) . notifySub "" $ DOWN srv conns unliftIO u reconnectServer reconnectServer :: m () @@ -199,26 +200,30 @@ getSMPServerClient c@AgentClient {smpClients, msgQ} srv = reconnectClient = withAgentLock c . withClient c srv $ \smp -> do cs <- atomically $ mapM readTVar =<< TM.lookup srv (pendingSubscrSrvrs c) - forConcurrently_ (maybe [] M.toList cs) $ \sub@(connId, _) -> - whenM (atomically $ isNothing <$> TM.lookup connId (subscrConns c)) $ - subscribe_ smp sub `catchError` handleError connId + conns <- forConcurrently (maybe [] M.toList cs) $ \sub@(connId, _) -> + ifM + (atomically $ isNothing <$> TM.lookup connId (subscrConns c)) + (subscribe_ smp sub `catchError` handleError connId) + (pure $ Just connId) + liftIO . unless (null conns) . notifySub "" . UP srv $ catMaybes conns where - subscribe_ :: SMPClient -> (ConnId, RcvQueue) -> ExceptT ProtocolClientError IO () + subscribe_ :: SMPClient -> (ConnId, RcvQueue) -> ExceptT ProtocolClientError IO (Maybe ConnId) subscribe_ smp (connId, rq@RcvQueue {rcvPrivateKey, rcvId}) = do subscribeSMPQueue smp rcvPrivateKey rcvId addSubscription c rq connId - liftIO $ notifySub UP connId + pure $ Just connId - handleError :: ConnId -> ProtocolClientError -> ExceptT ProtocolClientError IO () + handleError :: ConnId -> ProtocolClientError -> ExceptT ProtocolClientError IO (Maybe ConnId) handleError connId = \case e@PCEResponseTimeout -> throwError e e@PCENetworkError -> throwError e e -> do - liftIO $ notifySub (ERR $ protocolClientError SMP e) connId + liftIO . notifySub connId . ERR $ protocolClientError SMP e atomically $ removePendingSubscription c srv connId + pure Nothing - notifySub :: ACommand 'Agent -> ConnId -> IO () - notifySub cmd connId = atomically $ writeTBQueue (subQ c) ("", connId, cmd) + notifySub :: ConnId -> ACommand 'Agent -> IO () + notifySub connId cmd = atomically $ writeTBQueue (subQ c) ("", connId, cmd) getNtfServerClient :: forall m. AgentMonad m => AgentClient -> NtfServer -> m NtfClient getNtfServerClient c@AgentClient {ntfClients} srv = diff --git a/src/Simplex/Messaging/Agent/Protocol.hs b/src/Simplex/Messaging/Agent/Protocol.hs index 8be63a060..31ce96c3b 100644 --- a/src/Simplex/Messaging/Agent/Protocol.hs +++ b/src/Simplex/Messaging/Agent/Protocol.hs @@ -210,8 +210,8 @@ data ACommand (p :: AParty) where CON :: ACommand Agent -- notification that connection is established SUB :: ACommand Client END :: ACommand Agent - DOWN :: ACommand Agent - UP :: ACommand Agent + DOWN :: SMPServer -> [ConnId] -> ACommand Agent + UP :: SMPServer -> [ConnId] -> ACommand Agent SEND :: MsgBody -> ACommand Client MID :: AgentMsgId -> ACommand Agent SENT :: AgentMsgId -> ACommand Agent @@ -817,8 +817,8 @@ commandP = <|> "INFO " *> infoCmd <|> "SUB" $> ACmd SClient SUB <|> "END" $> ACmd SAgent END - <|> "DOWN" $> ACmd SAgent DOWN - <|> "UP" $> ACmd SAgent UP + <|> "DOWN " *> downsResp + <|> "UP " *> upsResp <|> "SEND " *> sendCmd <|> "MID " *> msgIdResp <|> "SENT " *> sentResp @@ -840,12 +840,15 @@ commandP = acptCmd = ACmd SClient .: ACPT <$> A.takeTill (== ' ') <* A.space <*> A.takeByteString rjctCmd = ACmd SClient . RJCT <$> A.takeByteString infoCmd = ACmd SAgent . INFO <$> A.takeByteString + downsResp = ACmd SAgent .: DOWN <$> strP <* A.space <*> connections + upsResp = ACmd SAgent .: UP <$> strP <* A.space <*> connections sendCmd = ACmd SClient . SEND <$> A.takeByteString msgIdResp = ACmd SAgent . MID <$> A.decimal sentResp = ACmd SAgent . SENT <$> A.decimal msgErrResp = ACmd SAgent .: MERR <$> A.decimal <* A.space <*> strP message = ACmd SAgent .: MSG <$> msgMetaP <* A.space <*> A.takeByteString ackCmd = ACmd SClient . ACK <$> A.decimal + connections = strP `A.sepBy'` (A.char ',') msgMetaP = do integrity <- strP recipient <- " R=" *> partyMeta A.decimal @@ -872,8 +875,8 @@ serializeCommand = \case INFO cInfo -> "INFO " <> serializeBinary cInfo SUB -> "SUB" END -> "END" - DOWN -> "DOWN" - UP -> "UP" + DOWN srv conns -> B.unwords ["DOWN", strEncode srv, connections conns] + UP srv conns -> B.unwords ["UP", strEncode srv, connections conns] SEND msgBody -> "SEND " <> serializeBinary msgBody MID mId -> "MID " <> bshow mId SENT mId -> "SENT " <> bshow mId @@ -888,6 +891,8 @@ serializeCommand = \case where showTs :: UTCTime -> ByteString showTs = B.pack . formatISO8601Millis + connections :: [ConnId] -> ByteString + connections = B.intercalate "," . map strEncode serializeMsgMeta :: MsgMeta -> ByteString serializeMsgMeta MsgMeta {integrity, recipient = (rmId, rTs), broker = (bmId, bTs), sndMsgId} = B.unwords @@ -939,6 +944,8 @@ tGet party h = liftIO (tGetRaw h) >>= tParseLoadBody ACPT {} -> Right cmd -- ERROR response does not always have connId ERR _ -> Right cmd + DOWN {} -> Right cmd + UP {} -> Right cmd -- other responses must have connId _ | B.null connId -> Left $ CMD NO_CONN diff --git a/src/Simplex/Messaging/Protocol.hs b/src/Simplex/Messaging/Protocol.hs index 4c58c8adc..dde5992b3 100644 --- a/src/Simplex/Messaging/Protocol.hs +++ b/src/Simplex/Messaging/Protocol.hs @@ -388,6 +388,8 @@ type SMPServer = ProtocolServer pattern SMPServer :: HostName -> ServiceName -> C.KeyHash -> ProtocolServer pattern SMPServer host port keyHash = ProtocolServer host port keyHash +{-# COMPLETE SMPServer #-} + -- | SMP server location and transport key digest (hash). data ProtocolServer = ProtocolServer { host :: HostName, diff --git a/tests/AgentTests.hs b/tests/AgentTests.hs index 6885fa8e6..f71531fa2 100644 --- a/tests/AgentTests.hs +++ b/tests/AgentTests.hs @@ -19,7 +19,7 @@ import Data.ByteString.Char8 (ByteString) import qualified Data.ByteString.Char8 as B import Network.HTTP.Types (urlEncode) import SMPAgentClient -import SMPClient (testPort, testPort2, testStoreLogFile, withSmpServer, withSmpServerStoreLogOn) +import SMPClient (testKeyHash, testPort, testPort2, testStoreLogFile, withSmpServer, withSmpServerStoreLogOn) import Simplex.Messaging.Agent.Protocol import qualified Simplex.Messaging.Agent.Protocol as A import Simplex.Messaging.Encoding.String @@ -273,7 +273,7 @@ testSubscrNotification t (server, _) client = do client #: ("1", "conn1", "NEW INV") =#> \case ("1", "conn1", INV {}) -> True; _ -> False client #:# "nothing should be delivered to client before the server is killed" killThread server - client <# ("", "conn1", DOWN) + client <# ("", "", DOWN testSMPServer ["conn1"]) withSmpServer (ATransport t) $ client <# ("", "conn1", ERR (SMP AUTH)) -- this new server does not have the queue @@ -287,14 +287,15 @@ testMsgDeliveryServerRestart t alice bob = do alice #: ("11", "bob", "ACK 5") #> ("11", "bob", OK) alice #:# "nothing else delivered before the server is killed" - alice <# ("", "bob", DOWN) + let server = (SMPServer "localhost" testPort2 testKeyHash) + alice <# ("", "", DOWN server ["bob"]) bob #: ("2", "alice", "SEND 11\nhello again") #> ("2", "alice", MID 6) bob #:# "nothing else delivered before the server is restarted" alice #:# "nothing else delivered before the server is restarted" withServer $ do bob <# ("", "alice", SENT 6) - alice <# ("", "bob", UP) + alice <# ("", "", UP server ["bob"]) alice <#= \case ("", "bob", Msg "hello again") -> True; _ -> False alice #: ("12", "bob", "ACK 6") #> ("12", "bob", OK) @@ -309,8 +310,8 @@ testServerConnectionAfterError t _ = do withServer $ do connect (bob, "bob") (alice, "alice") - bob <# ("", "alice", DOWN) - alice <# ("", "bob", DOWN) + bob <# ("", "", DOWN server ["alice"]) + alice <# ("", "", DOWN server ["bob"]) alice #: ("1", "bob", "SEND 5\nhello") #> ("1", "bob", MID 5) alice #:# "nothing else delivered before the server is restarted" bob #:# "nothing else delivered before the server is restarted" @@ -320,11 +321,11 @@ testServerConnectionAfterError t _ = do bob #: ("1", "alice", "SUB") #> ("1", "alice", ERR (BROKER NETWORK)) alice #: ("1", "bob", "SUB") #> ("1", "bob", ERR (BROKER NETWORK)) withServer $ do - alice <#= \case ("", "bob", cmd) -> cmd == UP || cmd == SENT 5; _ -> False - alice <#= \case ("", "bob", cmd) -> cmd == UP || cmd == SENT 5; _ -> False - bob <# ("", "alice", UP) + alice <# ("", "bob", SENT 5) + bob <# ("", "", UP server ["alice"]) bob <#= \case ("", "alice", Msg "hello") -> True; _ -> False bob #: ("2", "alice", "ACK 5") #> ("2", "alice", OK) + alice <# ("", "", UP server ["bob"]) alice #: ("1", "bob", "SEND 11\nhello again") #> ("1", "bob", MID 6) alice <# ("", "bob", SENT 6) bob <#= \case ("", "alice", Msg "hello again") -> True; _ -> False @@ -333,6 +334,7 @@ testServerConnectionAfterError t _ = do removeFile testDB removeFile testDB2 where + server = SMPServer "localhost" testPort2 testKeyHash withServer test' = withSmpServerStoreLogOn (ATransport t) testPort2 (const test') `shouldReturn` () withAgent1 = withAgent agentTestPort testDB withAgent2 = withAgent agentTestPort2 testDB2 @@ -341,6 +343,7 @@ testServerConnectionAfterError t _ = do testMsgDeliveryAgentRestart :: Transport c => TProxy c -> c -> IO () testMsgDeliveryAgentRestart t bob = do + let server = SMPServer "localhost" testPort2 testKeyHash withAgent $ \alice -> do withServer $ do connect (bob, "bob") (alice, "alice") @@ -350,7 +353,7 @@ testMsgDeliveryAgentRestart t bob = do bob #: ("11", "alice", "ACK 5") #> ("11", "alice", OK) bob #:# "nothing else delivered before the server is down" - bob <# ("", "alice", DOWN) + bob <# ("", "", DOWN server ["alice"]) alice #: ("2", "bob", "SEND 11\nhello again") #> ("2", "bob", MID 6) alice #:# "nothing else delivered before the server is restarted" bob #:# "nothing else delivered before the server is restarted" @@ -363,7 +366,7 @@ testMsgDeliveryAgentRestart t bob = do (corrId == "3" && cmd == OK) || (corrId == "" && cmd == SENT 6) _ -> False - bob <# ("", "alice", UP) + bob <# ("", "", UP server ["alice"]) bob <#= \case ("", "alice", Msg "hello again") -> True; _ -> False bob #: ("12", "alice", "ACK 6") #> ("12", "alice", OK) diff --git a/tests/AgentTests/FunctionalAPITests.hs b/tests/AgentTests/FunctionalAPITests.hs index b20154a3f..030fad7b3 100644 --- a/tests/AgentTests/FunctionalAPITests.hs +++ b/tests/AgentTests/FunctionalAPITests.hs @@ -159,12 +159,15 @@ testAsyncServerOffline t = do runExceptT $ createConnection alice SCMInvitation -- connection fails Left (BROKER NETWORK) <- runExceptT $ joinConnection bob cReq "bob's connInfo" - ("", bobId1, DOWN) <- get alice - bobId1 `shouldBe` bobId + ("", "", DOWN srv conns) <- get alice + srv `shouldBe` testSMPServer + conns `shouldBe` [bobId] -- connection succeeds after server start Right () <- withSmpServerStoreLogOn t testPort $ \_ -> runExceptT $ do - ("", bobId2, UP) <- get alice - liftIO $ bobId2 `shouldBe` bobId + ("", "", UP srv1 conns1) <- get alice + liftIO $ do + srv1 `shouldBe` testSMPServer + conns1 `shouldBe` [bobId] aliceId <- joinConnection bob cReq "bob's connInfo" ("", _, CONF confId "bob's connInfo") <- get alice allowConnection alice bobId confId "alice's connInfo" diff --git a/tests/AgentTests/NotificationTests.hs b/tests/AgentTests/NotificationTests.hs index 752c97760..599e421f6 100644 --- a/tests/AgentTests/NotificationTests.hs +++ b/tests/AgentTests/NotificationTests.hs @@ -50,7 +50,7 @@ testNotificationToken APNSMockServer {apnsQ} = do a <- getSMPAgentClient agentCfg initAgentServers Right () <- runExceptT $ do let tkn = DeviceToken PPApns "abcd" - registerNtfToken a tkn + NTRegistered <- registerNtfToken a tkn APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData}, sendApnsResponse} <- atomically $ readTBQueue apnsQ verification <- ntfData .-> "verification" @@ -80,13 +80,13 @@ testNtfTokenRepeatRegistration APNSMockServer {apnsQ} = do a <- getSMPAgentClient agentCfg initAgentServers Right () <- runExceptT $ do let tkn = DeviceToken PPApns "abcd" - registerNtfToken a tkn + NTRegistered <- registerNtfToken a tkn APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData}, sendApnsResponse} <- atomically $ readTBQueue apnsQ verification <- ntfData .-> "verification" nonce <- C.cbNonce <$> ntfData .-> "nonce" liftIO $ sendApnsResponse APNSRespOk - registerNtfToken a tkn + NTRegistered <- registerNtfToken a tkn APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData'}, sendApnsResponse = sendApnsResponse'} <- atomically $ readTBQueue apnsQ _ <- ntfData' .-> "verification" @@ -107,7 +107,7 @@ testNtfTokenSecondRegistration APNSMockServer {apnsQ} = do a' <- getSMPAgentClient agentCfg {dbFile = testDB2} initAgentServers Right () <- runExceptT $ do let tkn = DeviceToken PPApns "abcd" - registerNtfToken a tkn + NTRegistered <- registerNtfToken a tkn APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData}, sendApnsResponse} <- atomically $ readTBQueue apnsQ verification <- ntfData .-> "verification" @@ -115,7 +115,7 @@ testNtfTokenSecondRegistration APNSMockServer {apnsQ} = do liftIO $ sendApnsResponse APNSRespOk verifyNtfToken a tkn verification nonce - registerNtfToken a' tkn + NTRegistered <- registerNtfToken a' tkn APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData'}, sendApnsResponse = sendApnsResponse'} <- atomically $ readTBQueue apnsQ verification' <- ntfData' .-> "verification" @@ -141,7 +141,7 @@ testNtfTokenServerRestart t APNSMockServer {apnsQ} = do a <- getSMPAgentClient agentCfg initAgentServers let tkn = DeviceToken PPApns "abcd" Right ntfData <- withNtfServer t . runExceptT $ do - registerNtfToken a tkn + NTRegistered <- registerNtfToken a tkn APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData}, sendApnsResponse} <- atomically $ readTBQueue apnsQ liftIO $ sendApnsResponse APNSRespOk diff --git a/tests/SMPAgentClient.hs b/tests/SMPAgentClient.hs index d23ab522e..0865e7208 100644 --- a/tests/SMPAgentClient.hs +++ b/tests/SMPAgentClient.hs @@ -155,10 +155,13 @@ smpAgentTest1_1_1 test' = _test [h] = test' h _test _ = error "expected 1 handle" +testSMPServer :: SMPServer +testSMPServer = "smp://LcJUMfVhwD8yxjAiSaDzzGF3-kLG4Uh0Fl_ZIjrRwjI=@localhost:5001" + initAgentServers :: InitialAgentServers initAgentServers = InitialAgentServers - { smp = L.fromList ["smp://LcJUMfVhwD8yxjAiSaDzzGF3-kLG4Uh0Fl_ZIjrRwjI=@localhost:5001"], + { smp = L.fromList [testSMPServer], ntf = ["smp://LcJUMfVhwD8yxjAiSaDzzGF3-kLG4Uh0Fl_ZIjrRwjI=@localhost:6001"] } From f8ec838912579159ca0ebd956a559fc20bf1d7ea Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Mon, 25 Apr 2022 16:04:06 +0100 Subject: [PATCH 017/131] prevent servers from reconnecting when the agent is closed (#364) * prevent servers reconnecting when the agent is closed * fail creating NTF server connection when the agent is closed --- src/Simplex/Messaging/Agent/Client.hs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/Simplex/Messaging/Agent/Client.hs b/src/Simplex/Messaging/Agent/Client.hs index 44c843074..6e5112cef 100644 --- a/src/Simplex/Messaging/Agent/Client.hs +++ b/src/Simplex/Messaging/Agent/Client.hs @@ -72,7 +72,7 @@ import Simplex.Messaging.Protocol (BrokerMsg, ErrorType, ProtocolServer (..), Qu import qualified Simplex.Messaging.Protocol as SMP import Simplex.Messaging.TMap (TMap) import qualified Simplex.Messaging.TMap as TM -import Simplex.Messaging.Util (bshow, ifM, liftEitherError, liftError, tryError) +import Simplex.Messaging.Util (bshow, ifM, liftEitherError, liftError, tryError, unlessM, whenM) import Simplex.Messaging.Version import System.Timeout (timeout) import UnliftIO (async, forConcurrently) @@ -86,7 +86,8 @@ type SMPClientVar = TMVar (Either AgentErrorType SMPClient) type NtfClientVar = TMVar (Either AgentErrorType NtfClient) data AgentClient = AgentClient - { rcvQ :: TBQueue (ATransmission 'Client), + { active :: TVar Bool, + rcvQ :: TBQueue (ATransmission 'Client), subQ :: TBQueue (ATransmission 'Agent), msgQ :: TBQueue (ServerTransmission BrokerMsg), smpServers :: TVar (NonEmpty SMPServer), @@ -110,6 +111,7 @@ data AgentClient = AgentClient newAgentClient :: InitialAgentServers -> Env -> STM AgentClient newAgentClient InitialAgentServers {smp, ntf} agentEnv = do let qSize = tbqSize $ config agentEnv + active <- newTVar True rcvQ <- newTBQueue qSize subQ <- newTBQueue qSize msgQ <- newTBQueue qSize @@ -127,7 +129,7 @@ newAgentClient InitialAgentServers {smp, ntf} agentEnv = do asyncClients <- newTVar [] clientId <- stateTVar (clientCounter agentEnv) $ \i -> (i + 1, i + 1) lock <- newTMVar () - return AgentClient {rcvQ, subQ, msgQ, smpServers, ntfServers, smpClients, ntfClients, subscrSrvrs, pendingSubscrSrvrs, subscrConns, connMsgsQueued, smpQueueMsgQueues, smpQueueMsgDeliveries, reconnections, asyncClients, clientId, agentEnv, smpSubscriber = undefined, lock} + return AgentClient {active, rcvQ, subQ, msgQ, smpServers, ntfServers, smpClients, ntfClients, subscrSrvrs, pendingSubscrSrvrs, subscrConns, connMsgsQueued, smpQueueMsgQueues, smpQueueMsgDeliveries, reconnections, asyncClients, clientId, agentEnv, smpSubscriber = undefined, lock} -- | Agent monad with MonadReader Env and MonadError AgentErrorType type AgentMonad m = (MonadUnliftIO m, MonadReader Env m, MonadError AgentErrorType m) @@ -145,7 +147,8 @@ instance ProtocolServerClient NtfResponse where protocolError = NTF getSMPServerClient :: forall m. AgentMonad m => AgentClient -> SMPServer -> m SMPClient -getSMPServerClient c@AgentClient {smpClients, msgQ} srv = +getSMPServerClient c@AgentClient {active, smpClients, msgQ} srv = do + unlessM (readTVarIO active) . throwError $ INTERNAL "agent is stopped" atomically (getClientVar srv smpClients) >>= either (newProtocolClient c srv smpClients connectClient reconnectClient) @@ -183,7 +186,7 @@ getSMPServerClient c@AgentClient {smpClients, msgQ} srv = serverDown u cs = unless (M.null cs) $ do let conns = M.keys cs unless (null conns) . notifySub "" $ DOWN srv conns - unliftIO u reconnectServer + whenM (readTVarIO active) $ unliftIO u reconnectServer reconnectServer :: m () reconnectServer = do @@ -226,7 +229,8 @@ getSMPServerClient c@AgentClient {smpClients, msgQ} srv = notifySub connId cmd = atomically $ writeTBQueue (subQ c) ("", connId, cmd) getNtfServerClient :: forall m. AgentMonad m => AgentClient -> NtfServer -> m NtfClient -getNtfServerClient c@AgentClient {ntfClients} srv = +getNtfServerClient c@AgentClient {active, ntfClients} srv = do + unlessM (readTVarIO active) . throwError $ INTERNAL "agent is stopped" atomically (getClientVar srv ntfClients) >>= either (newProtocolClient c srv ntfClients connectClient $ pure ()) @@ -297,6 +301,7 @@ newProtocolClient c srv clients connectClient reconnectClient clientVar = tryCon closeAgentClient :: MonadIO m => AgentClient -> m () closeAgentClient c = liftIO $ do + atomically $ writeTVar (active c) False closeSMPServerClients c cancelActions $ reconnections c cancelActions $ asyncClients c From 7249cb0f0e63ae4e38f16fea55e937e84a5971d4 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Fri, 29 Apr 2022 13:12:30 +0100 Subject: [PATCH 018/131] close socket on connection exception (#365) --- src/Simplex/Messaging/Agent/Client.hs | 4 ++-- src/Simplex/Messaging/Client/Agent.hs | 4 ++-- src/Simplex/Messaging/Transport.hs | 9 +++++---- src/Simplex/Messaging/Transport/Server.hs | 5 +++-- src/Simplex/Messaging/Util.hs | 10 ++++++++++ 5 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/Simplex/Messaging/Agent/Client.hs b/src/Simplex/Messaging/Agent/Client.hs index 6e5112cef..21128b2ce 100644 --- a/src/Simplex/Messaging/Agent/Client.hs +++ b/src/Simplex/Messaging/Agent/Client.hs @@ -72,7 +72,7 @@ import Simplex.Messaging.Protocol (BrokerMsg, ErrorType, ProtocolServer (..), Qu import qualified Simplex.Messaging.Protocol as SMP import Simplex.Messaging.TMap (TMap) import qualified Simplex.Messaging.TMap as TM -import Simplex.Messaging.Util (bshow, ifM, liftEitherError, liftError, tryError, unlessM, whenM) +import Simplex.Messaging.Util (bshow, catchAll_, ifM, liftEitherError, liftError, tryError, unlessM, whenM) import Simplex.Messaging.Version import System.Timeout (timeout) import UnliftIO (async, forConcurrently) @@ -312,7 +312,7 @@ closeSMPServerClients c = readTVarIO (smpClients c) >>= mapM_ (forkIO . closeCli where closeClient smpVar = atomically (readTMVar smpVar) >>= \case - Right smp -> closeProtocolClient smp `E.catch` \(_ :: E.SomeException) -> pure () + Right smp -> closeProtocolClient smp `catchAll_` pure () _ -> pure () cancelActions :: Foldable f => TVar (f (Async ())) -> IO () diff --git a/src/Simplex/Messaging/Client/Agent.hs b/src/Simplex/Messaging/Client/Agent.hs index 1f9d215cd..dd3b0e90b 100644 --- a/src/Simplex/Messaging/Client/Agent.hs +++ b/src/Simplex/Messaging/Client/Agent.hs @@ -28,7 +28,7 @@ import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Protocol (BrokerMsg, ProtocolServer (..), QueueId, SMPServer) import Simplex.Messaging.TMap (TMap) import qualified Simplex.Messaging.TMap as TM -import Simplex.Messaging.Util (tryE, whenM, ($>>=)) +import Simplex.Messaging.Util (catchAll_, tryE, whenM, ($>>=)) import System.Timeout (timeout) import UnliftIO (async, forConcurrently_) import UnliftIO.Exception (Exception) @@ -232,7 +232,7 @@ closeSMPServerClients c = readTVarIO (smpClients c) >>= mapM_ (forkIO . closeCli where closeClient smpVar = atomically (readTMVar smpVar) >>= \case - Right smp -> closeProtocolClient smp `E.catch` \(_ :: E.SomeException) -> pure () + Right smp -> closeProtocolClient smp `catchAll_` pure () _ -> pure () cancelActions :: Foldable f => TVar (f (Async ())) -> IO () diff --git a/src/Simplex/Messaging/Transport.hs b/src/Simplex/Messaging/Transport.hs index 3d0071733..f3f553b79 100644 --- a/src/Simplex/Messaging/Transport.hs +++ b/src/Simplex/Messaging/Transport.hs @@ -80,7 +80,7 @@ import qualified Network.TLS.Extra as TE import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Encoding import Simplex.Messaging.Parsers (dropPrefix, parse, parseRead1, sumTypeJSON) -import Simplex.Messaging.Util (bshow) +import Simplex.Messaging.Util (bshow, catchAll, catchAll_) import Simplex.Messaging.Version import Test.QuickCheck (Arbitrary (..)) import UnliftIO.Exception (Exception) @@ -154,7 +154,7 @@ connectTLS :: T.TLSParams p => p -> Socket -> IO T.Context connectTLS params sock = E.bracketOnError (T.contextNew sock params) closeTLS $ \ctx -> do T.handshake ctx - `E.catch` \(e :: E.SomeException) -> putStrLn ("exception: " <> show e) >> E.throwIO e + `catchAll` \e -> putStrLn ("exception: " <> show e) >> E.throwIO e pure ctx getTLS :: TransportPeer -> T.Context -> IO TLS @@ -175,8 +175,9 @@ withTlsUnique peer cxt f = closeTLS :: T.Context -> IO () closeTLS ctx = - (T.bye ctx >> T.contextClose ctx) -- sometimes socket was closed before 'TLS.bye' - `E.catch` (\(_ :: E.SomeException) -> pure ()) -- so we catch the 'Broken pipe' error here + T.bye ctx -- sometimes socket was closed before 'TLS.bye' so we catch the 'Broken pipe' error here + `catchAll_` T.contextClose ctx + `catchAll_` pure () supportedParameters :: T.Supported supportedParameters = diff --git a/src/Simplex/Messaging/Transport/Server.hs b/src/Simplex/Messaging/Transport/Server.hs index d4009b4f6..55393d278 100644 --- a/src/Simplex/Messaging/Transport/Server.hs +++ b/src/Simplex/Messaging/Transport/Server.hs @@ -24,6 +24,7 @@ import qualified Data.X509.Validation as XV import Network.Socket import qualified Network.TLS as T import Simplex.Messaging.Transport +import Simplex.Messaging.Util (catchAll_) import System.Exit (exitFailure) import UnliftIO.Concurrent import qualified UnliftIO.Exception as E @@ -42,7 +43,7 @@ runTransportServer started port serverParams server = do (closeServer started clients) $ \sock -> forever $ do (connSock, _) <- accept sock - tid <- forkIO $ connectClient u connSock `E.catch` \(_ :: E.SomeException) -> pure () + tid <- forkIO $ connectClient u connSock `catchAll_` close connSock `catchAll_` pure () atomically . modifyTVar' clients $ S.insert tid where connectClient :: UnliftIO m -> Socket -> IO () @@ -60,7 +61,7 @@ runTCPServer started port server = do (closeServer started clients) $ \sock -> forever $ do (connSock, _) <- accept sock - tid <- forkIO $ server connSock `E.catch` \(_ :: E.SomeException) -> pure () + tid <- forkIO $ server connSock `catchAll_` pure () atomically . modifyTVar' clients $ S.insert tid closeServer :: TMVar Bool -> TVar (Set ThreadId) -> Socket -> IO () diff --git a/src/Simplex/Messaging/Util.hs b/src/Simplex/Messaging/Util.hs index 0d741bb43..24291773b 100644 --- a/src/Simplex/Messaging/Util.hs +++ b/src/Simplex/Messaging/Util.hs @@ -1,7 +1,9 @@ {-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE ScopedTypeVariables #-} module Simplex.Messaging.Util where +import qualified Control.Exception as E import Control.Monad.Except import Control.Monad.IO.Unlift import Control.Monad.Trans.Except @@ -68,3 +70,11 @@ unlessM b = ifM b $ pure () ($>>=) :: (Monad m, Monad f, Traversable f) => m (f a) -> (a -> m (f b)) -> m (f b) f $>>= g = f >>= fmap join . mapM g + +catchAll :: IO a -> (E.SomeException -> IO a) -> IO a +catchAll = E.catch +{-# INLINE catchAll #-} + +catchAll_ :: IO a -> IO a -> IO a +catchAll_ a = catchAll a . const +{-# INLINE catchAll_ #-} From 2733a09a40336211fa0278d192f5ca5d112437b9 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Sat, 30 Apr 2022 09:36:52 +0100 Subject: [PATCH 019/131] limit concurrency when resubscribing, method to resubscribe if not subscribed (#368) --- src/Simplex/Messaging/Agent.hs | 12 +++++++++++- src/Simplex/Messaging/Agent/Client.hs | 17 +++++++++++------ src/Simplex/Messaging/Agent/Env/SQLite.hs | 2 ++ src/Simplex/Messaging/Server.hs | 2 +- 4 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/Simplex/Messaging/Agent.hs b/src/Simplex/Messaging/Agent.hs index 9ee45f362..ee67611a7 100644 --- a/src/Simplex/Messaging/Agent.hs +++ b/src/Simplex/Messaging/Agent.hs @@ -43,6 +43,7 @@ module Simplex.Messaging.Agent acceptContact, rejectContact, subscribeConnection, + resubscribeConnection, sendMessage, ackMessage, suspendConnection, @@ -139,6 +140,9 @@ rejectContact c = withAgentEnv c .: rejectContact' c subscribeConnection :: AgentErrorMonad m => AgentClient -> ConnId -> m () subscribeConnection c = withAgentEnv c . subscribeConnection' c +resubscribeConnection :: AgentErrorMonad m => AgentClient -> ConnId -> m () +resubscribeConnection c = withAgentEnv c . resubscribeConnection' c + -- | Send message to the connection (SEND command) sendMessage :: AgentErrorMonad m => AgentClient -> ConnId -> MsgBody -> m AgentMsgId sendMessage c = withAgentEnv c .: sendMessage' c @@ -355,6 +359,12 @@ subscribeConnection' c connId = SomeConn _ (RcvConnection _ rq) -> subscribeQueue c rq connId SomeConn _ (ContactConnection _ rq) -> subscribeQueue c rq connId +resubscribeConnection' :: forall m. AgentMonad m => AgentClient -> ConnId -> m () +resubscribeConnection' c connId = + unlessM + (atomically $ hasActiveSubscription c connId) + (subscribeConnection' c connId) + -- | Send message to the connection (SEND command) in Reader monad sendMessage' :: forall m. AgentMonad m => AgentClient -> ConnId -> MsgBody -> m AgentMsgId sendMessage' c connId msg = @@ -398,7 +408,7 @@ resumeMsgDelivery c connId sq@SndQueue {server, sndId} = do withStore (`getPendingMsgs` connId) >>= queuePendingMsgs c connId sq where - queueDelivering qKey = atomically $ isJust <$> TM.lookup qKey (smpQueueMsgDeliveries c) + queueDelivering qKey = atomically $ TM.member qKey (smpQueueMsgDeliveries c) connQueued = atomically $ isJust <$> TM.lookupInsert connId True (connMsgsQueued c) queuePendingMsgs :: AgentMonad m => AgentClient -> ConnId -> SndQueue -> [InternalId] -> m () diff --git a/src/Simplex/Messaging/Agent/Client.hs b/src/Simplex/Messaging/Agent/Client.hs index 21128b2ce..2d842e3c9 100644 --- a/src/Simplex/Messaging/Agent/Client.hs +++ b/src/Simplex/Messaging/Agent/Client.hs @@ -38,6 +38,7 @@ module Simplex.Messaging.Agent.Client deleteQueue, logServer, removeSubscription, + hasActiveSubscription, ) where @@ -55,7 +56,7 @@ import qualified Data.ByteString.Char8 as B import Data.List.NonEmpty (NonEmpty) import Data.Map.Strict (Map) import qualified Data.Map.Strict as M -import Data.Maybe (catMaybes, isNothing) +import Data.Maybe (catMaybes) import Data.Text.Encoding import Data.Word (Word16) import Simplex.Messaging.Agent.Env.SQLite @@ -75,7 +76,7 @@ import qualified Simplex.Messaging.TMap as TM import Simplex.Messaging.Util (bshow, catchAll_, ifM, liftEitherError, liftError, tryError, unlessM, whenM) import Simplex.Messaging.Version import System.Timeout (timeout) -import UnliftIO (async, forConcurrently) +import UnliftIO (async, pooledForConcurrentlyN) import qualified UnliftIO.Exception as E import UnliftIO.STM @@ -200,14 +201,15 @@ getSMPServerClient c@AgentClient {active, smpClients, msgQ} srv = do reconnectClient `catchError` const loop reconnectClient :: m () - reconnectClient = + reconnectClient = do + n <- asks $ resubscriptionConcurrency . config withAgentLock c . withClient c srv $ \smp -> do cs <- atomically $ mapM readTVar =<< TM.lookup srv (pendingSubscrSrvrs c) - conns <- forConcurrently (maybe [] M.toList cs) $ \sub@(connId, _) -> + conns <- pooledForConcurrentlyN n (maybe [] M.toList cs) $ \sub@(connId, _) -> ifM - (atomically $ isNothing <$> TM.lookup connId (subscrConns c)) - (subscribe_ smp sub `catchError` handleError connId) + (atomically $ hasActiveSubscription c connId) (pure $ Just connId) + (subscribe_ smp sub `catchError` handleError connId) liftIO . unless (null conns) . notifySub "" . UP srv $ catMaybes conns where subscribe_ :: SMPClient -> (ConnId, RcvQueue) -> ExceptT ProtocolClientError IO (Maybe ConnId) @@ -408,6 +410,9 @@ addSubscription c rq@RcvQueue {server} connId = atomically $ do addSubs_ (subscrSrvrs c) rq connId removePendingSubscription c server connId +hasActiveSubscription :: AgentClient -> ConnId -> STM Bool +hasActiveSubscription c connId = TM.member connId (subscrConns c) + addPendingSubscription :: AgentClient -> RcvQueue -> ConnId -> STM () addPendingSubscription = addSubs_ . pendingSubscrSrvrs diff --git a/src/Simplex/Messaging/Agent/Env/SQLite.hs b/src/Simplex/Messaging/Agent/Env/SQLite.hs index bd1207426..8dba33087 100644 --- a/src/Simplex/Messaging/Agent/Env/SQLite.hs +++ b/src/Simplex/Messaging/Agent/Env/SQLite.hs @@ -49,6 +49,7 @@ data AgentConfig = AgentConfig ntfCfg :: ProtocolClientConfig, reconnectInterval :: RetryInterval, helloTimeout :: NominalDiffTime, + resubscriptionConcurrency :: Int, caCertificateFile :: FilePath, privateKeyFile :: FilePath, certificateFile :: FilePath @@ -78,6 +79,7 @@ defaultAgentConfig = ntfCfg = defaultClientConfig {defaultTransport = ("443", transport @TLS)}, reconnectInterval = defaultReconnectInterval, helloTimeout = 2 * nominalDay, + resubscriptionConcurrency = 16, -- CA certificate private key is not needed for initialization -- ! we do not generate these caCertificateFile = "/etc/opt/simplex-agent/ca.crt", diff --git a/src/Simplex/Messaging/Server.hs b/src/Simplex/Messaging/Server.hs index c2ce106ed..5a246bd92 100644 --- a/src/Simplex/Messaging/Server.hs +++ b/src/Simplex/Messaging/Server.hs @@ -343,7 +343,7 @@ client clnt@Client {subscriptions, ntfSubscriptions, rcvQ, sndQ} Server {subscri subscribeNotifications :: m (Transmission BrokerMsg) subscribeNotifications = atomically $ do - whenM (isNothing <$> TM.lookup queueId ntfSubscriptions) $ do + unlessM (TM.member queueId ntfSubscriptions) $ do writeTBQueue ntfSubscribedQ (queueId, clnt) TM.insert queueId () ntfSubscriptions pure ok From 1064e9c315bb4b9c09ba62141f86e21690876d76 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Sat, 30 Apr 2022 10:56:01 +0100 Subject: [PATCH 020/131] disconnect inactive clients (#366) * disconnect inactive clients * update test description * use one thread per client to disconnect inactive clients * remove comments * more comments --- apps/ntf-server/Main.hs | 3 +- apps/smp-server/Main.hs | 6 +-- simplexmq.cabal | 1 + src/Simplex/Messaging/Notifications/Server.hs | 39 ++++++++------ .../Messaging/Notifications/Server/Env.hs | 20 +++++-- src/Simplex/Messaging/Server.hs | 53 +++++++++++++------ src/Simplex/Messaging/Server/Env/STM.hs | 36 +++++++++---- src/Simplex/Messaging/Server/Expiration.hs | 17 ++++++ tests/AgentTests/FunctionalAPITests.hs | 53 +++++++++++++++++-- tests/NtfClient.hs | 1 + tests/SMPClient.hs | 4 +- tests/ServerTests.hs | 12 +++-- 12 files changed, 186 insertions(+), 59 deletions(-) create mode 100644 src/Simplex/Messaging/Server/Expiration.hs diff --git a/apps/ntf-server/Main.hs b/apps/ntf-server/Main.hs index 7a05d0a21..5bef2eb55 100644 --- a/apps/ntf-server/Main.hs +++ b/apps/ntf-server/Main.hs @@ -5,7 +5,7 @@ module Main where import Control.Logger.Simple import Simplex.Messaging.Client.Agent (defaultSMPClientAgentConfig) import Simplex.Messaging.Notifications.Server (runNtfServer) -import Simplex.Messaging.Notifications.Server.Env (NtfServerConfig (..)) +import Simplex.Messaging.Notifications.Server.Env (NtfServerConfig (..), defaultInactiveClientExpiration) import Simplex.Messaging.Notifications.Server.Push.APNS (defaultAPNSPushClientConfig) import Simplex.Messaging.Server.CLI (ServerCLIConfig (..), protocolServerCLI) import System.FilePath (combine) @@ -52,6 +52,7 @@ ntfServerCLIConfig = pushQSize = 128, smpAgentCfg = defaultSMPClientAgentConfig, apnsConfig = defaultAPNSPushClientConfig, + inactiveClientExpiration = Just defaultInactiveClientExpiration, caCertificateFile = caCrtFile, privateKeyFile = serverKeyFile, certificateFile = serverCrtFile diff --git a/apps/smp-server/Main.hs b/apps/smp-server/Main.hs index 3b314ad69..7bc13b3fb 100644 --- a/apps/smp-server/Main.hs +++ b/apps/smp-server/Main.hs @@ -7,7 +7,7 @@ module Main where import Simplex.Messaging.Server (runSMPServer) import Simplex.Messaging.Server.CLI (ServerCLIConfig (..), protocolServerCLI) -import Simplex.Messaging.Server.Env.STM (ServerConfig (..)) +import Simplex.Messaging.Server.Env.STM (ServerConfig (..), defaultInactiveClientExpiration, defaultMessageExpiration) import Simplex.Messaging.Transport (simplexMQVersion) import System.FilePath (combine) @@ -51,7 +51,7 @@ smpServerCLIConfig = certificateFile = serverCrtFile, storeLogFile, allowNewQueues = True, - messageTTL = Just $ 7 * 86400, -- 7 days - expireMessagesInterval = Just 21600_000000 -- microseconds, 6 hours + messageExpiration = Just defaultMessageExpiration, + inactiveClientExpiration = Just defaultInactiveClientExpiration } } diff --git a/simplexmq.cabal b/simplexmq.cabal index e5f49232f..55a3eade3 100644 --- a/simplexmq.cabal +++ b/simplexmq.cabal @@ -66,6 +66,7 @@ library Simplex.Messaging.Server Simplex.Messaging.Server.CLI Simplex.Messaging.Server.Env.STM + Simplex.Messaging.Server.Expiration Simplex.Messaging.Server.MsgStore Simplex.Messaging.Server.MsgStore.STM Simplex.Messaging.Server.QueueStore diff --git a/src/Simplex/Messaging/Notifications/Server.hs b/src/Simplex/Messaging/Notifications/Server.hs index f7100cf14..2bb412b39 100644 --- a/src/Simplex/Messaging/Notifications/Server.hs +++ b/src/Simplex/Messaging/Notifications/Server.hs @@ -17,6 +17,7 @@ import Control.Monad.Reader import Crypto.Random (MonadRandom) import Data.ByteString.Char8 (ByteString) import qualified Data.Text as T +import Data.Time.Clock.System (getSystemTime) import Network.Socket (ServiceName) import Simplex.Messaging.Client.Agent import qualified Simplex.Messaging.Crypto as C @@ -29,7 +30,7 @@ import Simplex.Messaging.Protocol (ErrorType (..), SignedTransmission, Transmiss import qualified Simplex.Messaging.Protocol as SMP import Simplex.Messaging.Server import qualified Simplex.Messaging.TMap as TM -import Simplex.Messaging.Transport (ATransport (..), THandle (..), TProxy, Transport) +import Simplex.Messaging.Transport (ATransport (..), THandle (..), TProxy, Transport (..)) import Simplex.Messaging.Transport.Server (runTransportServer) import Simplex.Messaging.Util import UnliftIO (async, uninterruptibleCancel) @@ -43,22 +44,20 @@ runNtfServer cfg = do runNtfServerBlocking started cfg runNtfServerBlocking :: (MonadRandom m, MonadUnliftIO m) => TMVar Bool -> NtfServerConfig -> m () -runNtfServerBlocking started cfg@NtfServerConfig {transports} = do - env <- newNtfServerEnv cfg - runReaderT ntfServer env - where - ntfServer :: (MonadUnliftIO m', MonadReader NtfEnv m') => m' () - ntfServer = do - s <- asks subscriber - ps <- asks pushServer - raceAny_ (ntfSubscriber s : ntfPush ps : map runServer transports) +runNtfServerBlocking started cfg = runReaderT (ntfServer cfg started) =<< newNtfServerEnv cfg - runServer :: (MonadUnliftIO m', MonadReader NtfEnv m') => (ServiceName, ATransport) -> m' () +ntfServer :: forall m. (MonadUnliftIO m, MonadReader NtfEnv m) => NtfServerConfig -> TMVar Bool -> m () +ntfServer NtfServerConfig {transports} started = do + s <- asks subscriber + ps <- asks pushServer + raceAny_ (ntfSubscriber s : ntfPush ps : map runServer transports) + where + runServer :: (ServiceName, ATransport) -> m () runServer (tcpPort, ATransport t) = do serverParams <- asks tlsServerParams runTransportServer started tcpPort serverParams (runClient t) - runClient :: (Transport c, MonadUnliftIO m, MonadReader NtfEnv m) => TProxy c -> c -> m () + runClient :: Transport c => TProxy c -> c -> m () runClient _ h = do kh <- asks serverIdentity liftIO (runExceptT $ ntfServerHandshake h kh) >>= \case @@ -132,18 +131,23 @@ ntfPush s@NtfPushServer {pushQ} = liftIO . forever . runExceptT $ do runNtfClientTransport :: (Transport c, MonadUnliftIO m, MonadReader NtfEnv m) => THandle c -> m () runNtfClientTransport th@THandle {sessionId} = do qSize <- asks $ clientQSize . config - c <- atomically $ newNtfServerClient qSize sessionId + ts <- liftIO getSystemTime + c <- atomically $ newNtfServerClient qSize sessionId ts s <- asks subscriber ps <- asks pushServer - raceAny_ [send th c, client c s ps, receive th c] + expCfg <- asks $ inactiveClientExpiration . config + raceAny_ ([send th c, client c s ps, receive th c] <> disconnectThread_ c expCfg) `finally` clientDisconnected c + where + disconnectThread_ c expCfg = maybe [] ((: []) . disconnectTransport th c activeAt) expCfg clientDisconnected :: MonadUnliftIO m => NtfServerClient -> m () clientDisconnected NtfServerClient {connected} = atomically $ writeTVar connected False receive :: (Transport c, MonadUnliftIO m, MonadReader NtfEnv m) => THandle c -> NtfServerClient -> m () -receive th NtfServerClient {rcvQ, sndQ} = forever $ do +receive th NtfServerClient {rcvQ, sndQ, activeAt} = forever $ do t@(_, _, (corrId, entId, cmdOrError)) <- tGet th + atomically . writeTVar activeAt =<< liftIO getSystemTime logDebug "received transmission" case cmdOrError of Left e -> write sndQ (corrId, entId, NRErr e) @@ -155,9 +159,10 @@ receive th NtfServerClient {rcvQ, sndQ} = forever $ do write q t = atomically $ writeTBQueue q t send :: (Transport c, MonadUnliftIO m) => THandle c -> NtfServerClient -> m () -send h NtfServerClient {sndQ, sessionId} = forever $ do +send h NtfServerClient {sndQ, sessionId, activeAt} = forever $ do t <- atomically $ readTBQueue sndQ - liftIO $ tPut h (Nothing, encodeTransmission sessionId t) + void . liftIO $ tPut h (Nothing, encodeTransmission sessionId t) + atomically . writeTVar activeAt =<< liftIO getSystemTime data VerificationResult = VRVerified NtfRequest | VRFailed diff --git a/src/Simplex/Messaging/Notifications/Server/Env.hs b/src/Simplex/Messaging/Notifications/Server/Env.hs index 7443a87f2..aaf04beb4 100644 --- a/src/Simplex/Messaging/Notifications/Server/Env.hs +++ b/src/Simplex/Messaging/Notifications/Server/Env.hs @@ -12,6 +12,7 @@ import Control.Monad (void) import Control.Monad.IO.Unlift import Crypto.Random import Data.ByteString.Char8 (ByteString) +import Data.Time.Clock.System (SystemTime) import Data.Word (Word16) import Data.X509.Validation (Fingerprint (..)) import Network.Socket @@ -23,6 +24,7 @@ import Simplex.Messaging.Notifications.Protocol import Simplex.Messaging.Notifications.Server.Push.APNS import Simplex.Messaging.Notifications.Server.Store import Simplex.Messaging.Protocol (CorrId, Transmission) +import Simplex.Messaging.Server.Expiration import Simplex.Messaging.TMap (TMap) import qualified Simplex.Messaging.TMap as TM import Simplex.Messaging.Transport (ATransport) @@ -38,12 +40,20 @@ data NtfServerConfig = NtfServerConfig pushQSize :: Natural, smpAgentCfg :: SMPClientAgentConfig, apnsConfig :: APNSPushClientConfig, + inactiveClientExpiration :: Maybe ExpirationConfig, -- CA certificate private key is not needed for initialization caCertificateFile :: FilePath, privateKeyFile :: FilePath, certificateFile :: FilePath } +defaultInactiveClientExpiration :: ExpirationConfig +defaultInactiveClientExpiration = + ExpirationConfig + { ttl = 7200, -- 2 hours + checkInterval = 3600 -- seconds, 1 hour + } + data NtfEnv = NtfEnv { config :: NtfServerConfig, subscriber :: NtfSubscriber, @@ -117,12 +127,14 @@ data NtfServerClient = NtfServerClient { rcvQ :: TBQueue NtfRequest, sndQ :: TBQueue (Transmission NtfResponse), sessionId :: ByteString, - connected :: TVar Bool + connected :: TVar Bool, + activeAt :: TVar SystemTime } -newNtfServerClient :: Natural -> ByteString -> STM NtfServerClient -newNtfServerClient qSize sessionId = do +newNtfServerClient :: Natural -> ByteString -> SystemTime -> STM NtfServerClient +newNtfServerClient qSize sessionId ts = do rcvQ <- newTBQueue qSize sndQ <- newTBQueue qSize connected <- newTVar True - return NtfServerClient {rcvQ, sndQ, sessionId, connected} + activeAt <- newTVar ts + return NtfServerClient {rcvQ, sndQ, sessionId, connected, activeAt} diff --git a/src/Simplex/Messaging/Server.hs b/src/Simplex/Messaging/Server.hs index 5a246bd92..f2cbe2d68 100644 --- a/src/Simplex/Messaging/Server.hs +++ b/src/Simplex/Messaging/Server.hs @@ -23,7 +23,14 @@ -- and optional append only log of SMP queue records. -- -- See https://github.com/simplex-chat/simplexmq/blob/master/protocol/simplex-messaging.md -module Simplex.Messaging.Server (runSMPServer, runSMPServerBlocking, verifyCmdSignature, dummyVerifyCmd) where +module Simplex.Messaging.Server + ( runSMPServer, + runSMPServerBlocking, + disconnectTransport, + verifyCmdSignature, + dummyVerifyCmd, + ) +where import Control.Monad import Control.Monad.Except @@ -33,7 +40,6 @@ import Crypto.Random import Data.ByteString.Char8 (ByteString) import qualified Data.ByteString.Char8 as B import Data.Functor (($>)) -import Data.Int (Int64) import qualified Data.Map.Strict as M import Data.Maybe (isNothing) import Data.Time.Clock.System (SystemTime (..), getSystemTime) @@ -42,6 +48,7 @@ import Network.Socket (ServiceName) import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Protocol import Simplex.Messaging.Server.Env.STM +import Simplex.Messaging.Server.Expiration import Simplex.Messaging.Server.MsgStore import Simplex.Messaging.Server.MsgStore.STM (MsgQueue) import Simplex.Messaging.Server.QueueStore @@ -118,18 +125,16 @@ smpServer started = do atomically $ TM.lookupDelete qId (clientSubs c) expireMessagesThread_ :: ServerConfig -> [m ()] - expireMessagesThread_ ServerConfig {messageTTL, expireMessagesInterval} = - case (messageTTL, expireMessagesInterval) of - (Just ttl, Just int) -> [expireMessages ttl int] - _ -> [] + expireMessagesThread_ = maybe [] ((: []) . expireMessages) . messageExpiration - expireMessages :: Int64 -> Int -> m () - expireMessages ttl interval = do + expireMessages :: ExpirationConfig -> m () + expireMessages expCfg = do ms <- asks msgStore quota <- asks $ msgQueueQuota . config + let interval = checkInterval expCfg * 1000000 forever $ do threadDelay interval - old <- subtract ttl . systemSeconds <$> liftIO getSystemTime + old <- liftIO $ expireBeforeEpoch expCfg rIds <- M.keysSet <$> readTVarIO ms forM_ rIds $ \rId -> atomically (getMsgQueue ms rId quota) @@ -145,10 +150,14 @@ smpServer started = do runClientTransport :: (Transport c, MonadUnliftIO m, MonadReader Env m) => THandle c -> m () runClientTransport th@THandle {sessionId} = do q <- asks $ tbqSize . config - c <- atomically $ newClient q sessionId + ts <- liftIO getSystemTime + c <- atomically $ newClient q sessionId ts s <- asks server - raceAny_ [send th c, client c s, receive th c] + expCfg <- asks $ inactiveClientExpiration . config + raceAny_ ([send th c, client c s, receive th c] <> disconnectThread_ c expCfg) `finally` clientDisconnected c + where + disconnectThread_ c expCfg = maybe [] ((: []) . disconnectTransport th c activeAt) expCfg clientDisconnected :: (MonadUnliftIO m, MonadReader Env m) => Client -> m () clientDisconnected c@Client {subscriptions, connected} = do @@ -172,8 +181,9 @@ cancelSub = \case _ -> return () receive :: (Transport c, MonadUnliftIO m, MonadReader Env m) => THandle c -> Client -> m () -receive th Client {rcvQ, sndQ} = forever $ do +receive th Client {rcvQ, sndQ, activeAt} = forever $ do (sig, signed, (corrId, queueId, cmdOrError)) <- tGet th + atomically . writeTVar activeAt =<< liftIO getSystemTime case cmdOrError of Left e -> write sndQ (corrId, queueId, ERR e) Right cmd -> do @@ -185,9 +195,20 @@ receive th Client {rcvQ, sndQ} = forever $ do write q t = atomically $ writeTBQueue q t send :: (Transport c, MonadUnliftIO m) => THandle c -> Client -> m () -send h Client {sndQ, sessionId} = forever $ do +send h Client {sndQ, sessionId, activeAt} = forever $ do t <- atomically $ readTBQueue sndQ - liftIO $ tPut h (Nothing, encodeTransmission sessionId t) + -- TODO the line below can return Left, but we ignore it and do not disconnect the client + void . liftIO $ tPut h (Nothing, encodeTransmission sessionId t) + atomically . writeTVar activeAt =<< liftIO getSystemTime + +disconnectTransport :: (Transport c, MonadUnliftIO m) => THandle c -> client -> (client -> TVar SystemTime) -> ExpirationConfig -> m () +disconnectTransport THandle {connection} c activeAt expCfg = do + let interval = checkInterval expCfg * 1000000 + forever . liftIO $ do + threadDelay interval + old <- expireBeforeEpoch expCfg + ts <- readTVarIO $ activeAt c + when (systemSeconds ts < old) $ closeConnection connection verifyTransmission :: forall m. (MonadUnliftIO m, MonadReader Env m) => Maybe C.ASignature -> ByteString -> QueueId -> Cmd -> m Bool @@ -373,8 +394,8 @@ client clnt@Client {subscriptions, ntfSubscriptions, rcvQ, sndQ} Server {subscri Left _ -> pure $ err LARGE_MSG Right msg -> do ms <- asks msgStore - ServerConfig {messageTTL, msgQueueQuota} <- asks config - old <- forM messageTTL $ \ttl -> subtract ttl . systemSeconds <$> liftIO getSystemTime + ServerConfig {messageExpiration, msgQueueQuota} <- asks config + old <- liftIO $ mapM expireBeforeEpoch messageExpiration atomically $ do q <- getMsgQueue ms (recipientId qr) msgQueueQuota mapM_ (deleteExpiredMsgs q) old diff --git a/src/Simplex/Messaging/Server/Env/STM.hs b/src/Simplex/Messaging/Server/Env/STM.hs index 043a4e38c..6ca63df7b 100644 --- a/src/Simplex/Messaging/Server/Env/STM.hs +++ b/src/Simplex/Messaging/Server/Env/STM.hs @@ -9,15 +9,16 @@ import Control.Concurrent (ThreadId) import Control.Monad.IO.Unlift import Crypto.Random import Data.ByteString.Char8 (ByteString) -import Data.Int (Int64) import Data.Map.Strict (Map) import qualified Data.Map.Strict as M +import Data.Time.Clock.System (SystemTime) import Data.X509.Validation (Fingerprint (..)) import Network.Socket (ServiceName) import qualified Network.TLS as T import Numeric.Natural import Simplex.Messaging.Crypto (KeyHash (..)) import Simplex.Messaging.Protocol +import Simplex.Messaging.Server.Expiration import Simplex.Messaging.Server.MsgStore.STM import Simplex.Messaging.Server.QueueStore (QueueRec (..)) import Simplex.Messaging.Server.QueueStore.STM @@ -39,16 +40,31 @@ data ServerConfig = ServerConfig storeLogFile :: Maybe FilePath, -- | set to False to prohibit creating new queues allowNewQueues :: Bool, - -- | time after which the messages can be removed from the queues, seconds - messageTTL :: Maybe Int64, - -- | interval to periodically remove expired messages (when no messages are sent to the queue), microseconds - expireMessagesInterval :: Maybe Int, + -- | time after which the messages can be removed from the queues and check interval, seconds + messageExpiration :: Maybe ExpirationConfig, + -- | time after which the socket with inactive client can be disconnected (without any messages or commands, incl. PING), + -- and check interval, seconds + inactiveClientExpiration :: Maybe ExpirationConfig, -- CA certificate private key is not needed for initialization caCertificateFile :: FilePath, privateKeyFile :: FilePath, certificateFile :: FilePath } +defaultMessageExpiration :: ExpirationConfig +defaultMessageExpiration = + ExpirationConfig + { ttl = 30 * 86400, -- seconds, 30 days + checkInterval = 43200 -- seconds, 12 hours + } + +defaultInactiveClientExpiration :: ExpirationConfig +defaultInactiveClientExpiration = + ExpirationConfig + { ttl = 7200, -- 2 hours + checkInterval = 3600 -- seconds, 1 hour + } + data Env = Env { config :: ServerConfig, server :: Server, @@ -73,7 +89,8 @@ data Client = Client rcvQ :: TBQueue (Transmission Cmd), sndQ :: TBQueue (Transmission BrokerMsg), sessionId :: ByteString, - connected :: TVar Bool + connected :: TVar Bool, + activeAt :: TVar SystemTime } data SubscriptionThread = NoSub | SubPending | SubThread ThreadId @@ -91,14 +108,15 @@ newServer qSize = do notifiers <- TM.empty return Server {subscribedQ, subscribers, ntfSubscribedQ, notifiers} -newClient :: Natural -> ByteString -> STM Client -newClient qSize sessionId = do +newClient :: Natural -> ByteString -> SystemTime -> STM Client +newClient qSize sessionId ts = do subscriptions <- TM.empty ntfSubscriptions <- TM.empty rcvQ <- newTBQueue qSize sndQ <- newTBQueue qSize connected <- newTVar True - return Client {subscriptions, ntfSubscriptions, rcvQ, sndQ, sessionId, connected} + activeAt <- newTVar ts + return Client {subscriptions, ntfSubscriptions, rcvQ, sndQ, sessionId, connected, activeAt} newSubscription :: STM Sub newSubscription = do diff --git a/src/Simplex/Messaging/Server/Expiration.hs b/src/Simplex/Messaging/Server/Expiration.hs new file mode 100644 index 000000000..8f369d516 --- /dev/null +++ b/src/Simplex/Messaging/Server/Expiration.hs @@ -0,0 +1,17 @@ +{-# LANGUAGE NamedFieldPuns #-} + +module Simplex.Messaging.Server.Expiration where + +import Control.Monad.IO.Class +import Data.Int (Int64) +import Data.Time.Clock.System (SystemTime (..), getSystemTime) + +data ExpirationConfig = ExpirationConfig + { -- time after which the entity can be expired, seconds + ttl :: Int64, + -- interval to check expiration, seconds + checkInterval :: Int + } + +expireBeforeEpoch :: ExpirationConfig -> IO Int64 +expireBeforeEpoch ExpirationConfig {ttl} = subtract ttl . systemSeconds <$> liftIO getSystemTime diff --git a/tests/AgentTests/FunctionalAPITests.hs b/tests/AgentTests/FunctionalAPITests.hs index 030fad7b3..a0195de23 100644 --- a/tests/AgentTests/FunctionalAPITests.hs +++ b/tests/AgentTests/FunctionalAPITests.hs @@ -1,24 +1,28 @@ {-# LANGUAGE DataKinds #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE LambdaCase #-} +{-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE PatternSynonyms #-} {-# OPTIONS_GHC -fno-warn-incomplete-uni-patterns #-} module AgentTests.FunctionalAPITests (functionalAPITests) where +import Control.Concurrent (threadDelay) import Control.Monad.Except (ExceptT, runExceptT) import Control.Monad.IO.Unlift +import Data.Time.Clock.System (SystemTime (..), getSystemTime) import SMPAgentClient -import SMPClient (testPort, withSmpServer, withSmpServerStoreLogOn) +import SMPClient (cfg, testPort, withSmpServer, withSmpServerConfigOn, withSmpServerStoreLogOn) import Simplex.Messaging.Agent import Simplex.Messaging.Agent.Env.SQLite (AgentConfig (..)) import Simplex.Messaging.Agent.Protocol import Simplex.Messaging.Protocol (ErrorType (..), MsgBody) +import Simplex.Messaging.Server.Env.STM (ServerConfig (..)) +import Simplex.Messaging.Server.Expiration import Simplex.Messaging.Transport (ATransport (..)) -import System.Timeout import Test.Hspec -import UnliftIO.STM +import UnliftIO (##>) :: MonadIO m => m (ATransmission 'Agent) -> ATransmission 'Agent -> m () a ##> t = a >>= \t' -> liftIO (t' `shouldBe` t) @@ -48,6 +52,11 @@ functionalAPITests t = do testAsyncServerOffline t it "should notify after HELLO timeout" $ withSmpServer t testAsyncHelloTimeout + describe "Inactive client disconnection" $ do + it "should disconnect clients if it was inactive longer than TTL" $ + testInactiveClientDisconnected t + it "should NOT disconnect active clients" $ + testActiveClientNotDisconnected t testAgentClient :: IO () testAgentClient = do @@ -188,6 +197,44 @@ testAsyncHelloTimeout = do get bob ##> ("", aliceId, ERR $ CONN NOT_ACCEPTED) pure () +testInactiveClientDisconnected :: ATransport -> IO () +testInactiveClientDisconnected t = do + let cfg' = cfg {inactiveClientExpiration = Just ExpirationConfig {ttl = 1, checkInterval = 1}} + withSmpServerConfigOn t cfg' testPort $ \_ -> do + alice <- getSMPAgentClient agentCfg initAgentServers + Right () <- runExceptT $ do + (connId, _cReq) <- createConnection alice SCMInvitation + get alice ##> ("", "", DOWN testSMPServer [connId]) + pure () + +testActiveClientNotDisconnected :: ATransport -> IO () +testActiveClientNotDisconnected t = do + let cfg' = cfg {inactiveClientExpiration = Just ExpirationConfig {ttl = 1, checkInterval = 1}} + withSmpServerConfigOn t cfg' testPort $ \_ -> do + alice <- getSMPAgentClient agentCfg initAgentServers + ts <- getSystemTime + Right () <- runExceptT $ do + (connId, _cReq) <- createConnection alice SCMInvitation + keepSubscribing alice connId ts + pure () + where + keepSubscribing :: AgentClient -> ConnId -> SystemTime -> ExceptT AgentErrorType IO () + keepSubscribing alice connId ts = do + ts' <- liftIO $ getSystemTime + if milliseconds ts' - milliseconds ts < 2200 + then do + -- keep sending SUB for 2.2 seconds + liftIO $ threadDelay 200000 + subscribeConnection alice connId + keepSubscribing alice connId ts + else do + -- check that nothing is sent from agent + Nothing <- 800000 `timeout` get alice + liftIO $ threadDelay 1200000 + -- and after 2 sec of inactivity DOWN is sent + get alice ##> ("", "", DOWN testSMPServer [connId]) + milliseconds ts = systemSeconds ts * 1000 + fromIntegral (systemNanoseconds ts `div` 1000000) + exchangeGreetings :: AgentClient -> ConnId -> AgentClient -> ConnId -> ExceptT AgentErrorType IO () exchangeGreetings alice bobId bob aliceId = do 5 <- sendMessage alice bobId "hello" diff --git a/tests/NtfClient.hs b/tests/NtfClient.hs index 861e38016..cc0fa4af8 100644 --- a/tests/NtfClient.hs +++ b/tests/NtfClient.hs @@ -83,6 +83,7 @@ ntfServerCfg = apnsPort = apnsTestPort, http2cfg = defaultHTTP2ClientConfig {caStoreFile = "tests/fixtures/ca.crt"} }, + inactiveClientExpiration = Just defaultInactiveClientExpiration, -- CA certificate private key is not needed for initialization caCertificateFile = "tests/fixtures/ca.crt", privateKeyFile = "tests/fixtures/server.key", diff --git a/tests/SMPClient.hs b/tests/SMPClient.hs index 2d76f3e93..0dc15a1af 100644 --- a/tests/SMPClient.hs +++ b/tests/SMPClient.hs @@ -61,8 +61,8 @@ cfg = msgIdBytes = 24, storeLogFile = Nothing, allowNewQueues = True, - messageTTL = Just $ 7 * 86400, -- seconds, 7 days - expireMessagesInterval = Just 21600_000000, -- microseconds, 6 hours + messageExpiration = Just defaultMessageExpiration, + inactiveClientExpiration = Just defaultInactiveClientExpiration, caCertificateFile = "tests/fixtures/ca.crt", privateKeyFile = "tests/fixtures/server.key", certificateFile = "tests/fixtures/server.crt" diff --git a/tests/ServerTests.hs b/tests/ServerTests.hs index 4c548d84e..49614d8e7 100644 --- a/tests/ServerTests.hs +++ b/tests/ServerTests.hs @@ -22,6 +22,7 @@ import Simplex.Messaging.Encoding import Simplex.Messaging.Encoding.String import Simplex.Messaging.Protocol import Simplex.Messaging.Server.Env.STM (ServerConfig (..)) +import Simplex.Messaging.Server.Expiration import Simplex.Messaging.Transport import System.Directory (removeFile) import System.TimeIt (timeItT) @@ -486,7 +487,8 @@ testMsgExpireOnSend :: forall c. Transport c => TProxy c -> Spec testMsgExpireOnSend t = it "should expire messages that are not received before messageTTL on SEND" $ do (sPub, sKey) <- C.generateSignatureKeyPair C.SEd25519 - withSmpServerConfigOn (ATransport t) cfg {messageTTL = Just 1} testPort $ \_ -> + let cfg' = cfg {messageExpiration = Just ExpirationConfig {ttl = 1, checkInterval = 10000}} + withSmpServerConfigOn (ATransport t) cfg' testPort $ \_ -> testSMPClient @c $ \sh -> do (sId, rId, rKey, dhShared) <- testSMPClient @c $ \rh -> createAndSecureQueue rh sPub let dec nonce = C.cbDecrypt dhShared (C.cbNonce nonce) @@ -504,7 +506,8 @@ testMsgExpireOnInterval :: forall c. Transport c => TProxy c -> Spec testMsgExpireOnInterval t = it "should expire messages that are not received before messageTTL after expiry interval" $ do (sPub, sKey) <- C.generateSignatureKeyPair C.SEd25519 - withSmpServerConfigOn (ATransport t) cfg {messageTTL = Just 1, expireMessagesInterval = Just 1000000} testPort $ \_ -> + let cfg' = cfg {messageExpiration = Just ExpirationConfig {ttl = 1, checkInterval = 1}} + withSmpServerConfigOn (ATransport t) cfg' testPort $ \_ -> testSMPClient @c $ \sh -> do (sId, rId, rKey, _) <- testSMPClient @c $ \rh -> createAndSecureQueue rh sPub Resp "1" _ OK <- signSendRecv sh sKey ("1", sId, SEND "hello (should expire)") @@ -517,9 +520,10 @@ testMsgExpireOnInterval t = testMsgNOTExpireOnInterval :: forall c. Transport c => TProxy c -> Spec testMsgNOTExpireOnInterval t = - it "should NOT expire messages that are not received before messageTTL if expiry interval is not set" $ do + it "should NOT expire messages that are not received before messageTTL if expiry interval is large" $ do (sPub, sKey) <- C.generateSignatureKeyPair C.SEd25519 - withSmpServerConfigOn (ATransport t) cfg {messageTTL = Just 1, expireMessagesInterval = Nothing} testPort $ \_ -> + let cfg' = cfg {messageExpiration = Just ExpirationConfig {ttl = 1, checkInterval = 10000}} + withSmpServerConfigOn (ATransport t) cfg' testPort $ \_ -> testSMPClient @c $ \sh -> do (sId, rId, rKey, dhShared) <- testSMPClient @c $ \rh -> createAndSecureQueue rh sPub let dec nonce = C.cbDecrypt dhShared (C.cbNonce nonce) From ace94d7c6916c36249e9f8793043ba08dcb97e77 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Sat, 30 Apr 2022 13:47:21 +0100 Subject: [PATCH 021/131] log server stats (#367) * log server stats * separate stats updates from the existing transactions --- apps/smp-server/Main.hs | 12 +++++- src/Simplex/Messaging/Server.hs | 50 +++++++++++++++++++++++-- src/Simplex/Messaging/Server/Env/STM.hs | 38 +++++++++++++++++-- tests/SMPClient.hs | 2 + 4 files changed, 94 insertions(+), 8 deletions(-) diff --git a/apps/smp-server/Main.hs b/apps/smp-server/Main.hs index 7bc13b3fb..9cb56e08a 100644 --- a/apps/smp-server/Main.hs +++ b/apps/smp-server/Main.hs @@ -5,6 +5,7 @@ module Main where +import Control.Logger.Simple import Simplex.Messaging.Server (runSMPServer) import Simplex.Messaging.Server.CLI (ServerCLIConfig (..), protocolServerCLI) import Simplex.Messaging.Server.Env.STM (ServerConfig (..), defaultInactiveClientExpiration, defaultMessageExpiration) @@ -17,8 +18,13 @@ cfgPath = "/etc/opt/simplex" logPath :: FilePath logPath = "/var/opt/simplex" +logCfg :: LogConfig +logCfg = LogConfig {lc_file = Nothing, lc_stderr = True} + main :: IO () -main = protocolServerCLI smpServerCLIConfig runSMPServer +main = do + setLogLevel LogInfo + withGlobalLogging logCfg $ protocolServerCLI smpServerCLIConfig runSMPServer smpServerCLIConfig :: ServerCLIConfig ServerConfig smpServerCLIConfig = @@ -52,6 +58,8 @@ smpServerCLIConfig = storeLogFile, allowNewQueues = True, messageExpiration = Just defaultMessageExpiration, - inactiveClientExpiration = Just defaultInactiveClientExpiration + inactiveClientExpiration = Just defaultInactiveClientExpiration, + logStatsInterval = Just 86400, -- seconds + logStatsStartTime = 0 -- seconds from 00:00 UTC } } diff --git a/src/Simplex/Messaging/Server.hs b/src/Simplex/Messaging/Server.hs index f2cbe2d68..901f0ee94 100644 --- a/src/Simplex/Messaging/Server.hs +++ b/src/Simplex/Messaging/Server.hs @@ -5,6 +5,7 @@ {-# LANGUAGE KindSignatures #-} {-# LANGUAGE LambdaCase #-} {-# LANGUAGE NamedFieldPuns #-} +{-# LANGUAGE NumericUnderscores #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE RankNTypes #-} {-# LANGUAGE ScopedTypeVariables #-} @@ -32,6 +33,7 @@ module Simplex.Messaging.Server ) where +import Control.Logger.Simple import Control.Monad import Control.Monad.Except import Control.Monad.IO.Unlift @@ -40,8 +42,12 @@ import Crypto.Random import Data.ByteString.Char8 (ByteString) import qualified Data.ByteString.Char8 as B import Data.Functor (($>)) +import Data.List (intercalate) import qualified Data.Map.Strict as M import Data.Maybe (isNothing) +import qualified Data.Set as S +import qualified Data.Text as T +import Data.Time.Clock (UTCTime (..), diffTimeToPicoseconds, getCurrentTime) import Data.Time.Clock.System (SystemTime (..), getSystemTime) import Data.Type.Equality import Network.Socket (ServiceName) @@ -86,7 +92,7 @@ smpServer started = do raceAny_ ( serverThread s subscribedQ subscribers subscriptions cancelSub : serverThread s ntfSubscribedQ notifiers ntfSubscriptions (\_ -> pure ()) : - map runServer transports <> expireMessagesThread_ cfg + map runServer transports <> expireMessagesThread_ cfg <> serverStatsThread_ cfg ) `finally` withLog closeStoreLog where @@ -140,6 +146,29 @@ smpServer started = do atomically (getMsgQueue ms rId quota) >>= atomically . (`deleteExpiredMsgs` old) + serverStatsThread_ :: ServerConfig -> [m ()] + serverStatsThread_ ServerConfig {logStatsInterval, logStatsStartTime} = + maybe [] ((: []) . logServerStats logStatsStartTime) logStatsInterval + + logServerStats :: Int -> Int -> m () + logServerStats startAt logInterval = do + initialDelay <- (startAt -) . fromIntegral . (`div` 1000000_000000) . diffTimeToPicoseconds . utctDayTime <$> liftIO getCurrentTime + logInfo $ "fromTime,qCreated,qSecured,qDeleted,msgSent,msgRecv,msgQueues" + threadDelay $ 1000000 * (initialDelay + if initialDelay < 0 then 86400 else 0) + ServerStats {fromTime, qCreated, qSecured, qDeleted, msgSent, msgRecv, msgQueues} <- asks serverStats + let interval = 1000000 * logInterval + forever $ do + ts <- liftIO getCurrentTime + fromTime' <- atomically $ swapTVar fromTime ts + qCreated' <- atomically $ swapTVar qCreated 0 + qSecured' <- atomically $ swapTVar qSecured 0 + qDeleted' <- atomically $ swapTVar qDeleted 0 + msgSent' <- atomically $ swapTVar msgSent 0 + msgRecv' <- atomically $ swapTVar msgRecv 0 + msgQueues' <- atomically $ S.size <$> swapTVar msgQueues S.empty + logInfo . T.pack $ intercalate "," [show fromTime', show qCreated', show qSecured', show qDeleted', show msgSent', show msgRecv', show msgQueues'] + threadDelay interval + runClient :: Transport c => TProxy c -> c -> m () runClient _ h = do kh <- asks serverIdentity @@ -311,6 +340,8 @@ client clnt@Client {subscriptions, ntfSubscriptions, rcvQ, sndQ} Server {subscri Left e -> pure $ ERR e Right _ -> do withLog (`logCreateById` rId) + stats <- asks serverStats + atomically $ modifyTVar (qCreated stats) (+ 1) subscribeQueue rId $> IDS (qik ids) logCreateById :: StoreLog 'WriteMode -> RecipientId -> IO () @@ -327,6 +358,8 @@ client clnt@Client {subscriptions, ntfSubscriptions, rcvQ, sndQ} Server {subscri secureQueue_ :: QueueStore -> SndPublicVerifyKey -> m (Transmission BrokerMsg) secureQueue_ st sKey = do withLog $ \s -> logSecureQueue s queueId sKey + stats <- asks serverStats + atomically $ modifyTVar (qSecured stats) (+ 1) atomically $ (corrId,queueId,) . either ERR (const OK) <$> secureQueue st queueId sKey addQueueNotifier_ :: QueueStore -> NtfPublicVerifyKey -> m (Transmission BrokerMsg) @@ -373,7 +406,11 @@ client clnt@Client {subscriptions, ntfSubscriptions, rcvQ, sndQ} Server {subscri acknowledgeMsg = atomically (withSub queueId $ \s -> const s <$$> tryTakeTMVar (delivered s)) >>= \case - Just (Just s) -> deliverMessage tryDelPeekMsg queueId s + Just (Just s) -> do + stats <- asks serverStats + atomically $ modifyTVar (msgRecv stats) (+ 1) + atomically $ modifyTVar (msgQueues stats) (S.insert queueId) + deliverMessage tryDelPeekMsg queueId s _ -> return $ err NO_MSG withSub :: RecipientId -> (Sub -> STM a) -> STM (Maybe a) @@ -396,13 +433,18 @@ client clnt@Client {subscriptions, ntfSubscriptions, rcvQ, sndQ} Server {subscri ms <- asks msgStore ServerConfig {messageExpiration, msgQueueQuota} <- asks config old <- liftIO $ mapM expireBeforeEpoch messageExpiration - atomically $ do + resp@(_, _, sent) <- atomically $ do q <- getMsgQueue ms (recipientId qr) msgQueueQuota mapM_ (deleteExpiredMsgs q) old ifM (isFull q) (pure $ err QUOTA) $ do trySendNotification writeMsg q msg pure ok + when (sent == OK) $ do + stats <- asks serverStats + atomically $ modifyTVar (msgSent stats) (+ 1) + atomically $ modifyTVar (msgQueues stats) (S.insert $ recipientId qr) + pure resp where mkMessage :: m (Either C.CryptoError Message) mkMessage = do @@ -460,6 +502,8 @@ client clnt@Client {subscriptions, ntfSubscriptions, rcvQ, sndQ} Server {subscri delQueueAndMsgs st = do withLog (`logDeleteQueue` queueId) ms <- asks msgStore + stats <- asks serverStats + atomically $ modifyTVar (qDeleted stats) (+ 1) atomically $ deleteQueue st queueId >>= \case Left e -> pure $ err e diff --git a/src/Simplex/Messaging/Server/Env/STM.hs b/src/Simplex/Messaging/Server/Env/STM.hs index 6ca63df7b..163979d61 100644 --- a/src/Simplex/Messaging/Server/Env/STM.hs +++ b/src/Simplex/Messaging/Server/Env/STM.hs @@ -2,6 +2,7 @@ {-# LANGUAGE DuplicateRecordFields #-} {-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE ScopedTypeVariables #-} +{-# LANGUAGE TupleSections #-} module Simplex.Messaging.Server.Env.STM where @@ -11,6 +12,9 @@ import Crypto.Random import Data.ByteString.Char8 (ByteString) import Data.Map.Strict (Map) import qualified Data.Map.Strict as M +import Data.Set (Set) +import qualified Data.Set as S +import Data.Time.Clock (UTCTime, getCurrentTime) import Data.Time.Clock.System (SystemTime) import Data.X509.Validation (Fingerprint (..)) import Network.Socket (ServiceName) @@ -45,7 +49,12 @@ data ServerConfig = ServerConfig -- | time after which the socket with inactive client can be disconnected (without any messages or commands, incl. PING), -- and check interval, seconds inactiveClientExpiration :: Maybe ExpirationConfig, - -- CA certificate private key is not needed for initialization + -- | log SMP server usage statistics, only aggregates are logged, seconds + logStatsInterval :: Maybe Int, + -- | time of the day when the stats are logged first, to log at consistent times, + -- irrespective of when the server is started (seconds from 00:00 UTC) + logStatsStartTime :: Int, + -- | CA certificate private key is not needed for initialization caCertificateFile :: FilePath, privateKeyFile :: FilePath, certificateFile :: FilePath @@ -73,7 +82,8 @@ data Env = Env msgStore :: STMMsgStore, idsDrg :: TVar ChaChaDRG, storeLog :: Maybe (StoreLog 'WriteMode), - tlsServerParams :: T.ServerParams + tlsServerParams :: T.ServerParams, + serverStats :: ServerStats } data Server = Server @@ -93,6 +103,16 @@ data Client = Client activeAt :: TVar SystemTime } +data ServerStats = ServerStats + { qCreated :: TVar Int, + qSecured :: TVar Int, + qDeleted :: TVar Int, + msgSent :: TVar Int, + msgRecv :: TVar Int, + msgQueues :: TVar (Set RecipientId), + fromTime :: TVar UTCTime + } + data SubscriptionThread = NoSub | SubPending | SubThread ThreadId data Sub = Sub @@ -118,6 +138,17 @@ newClient qSize sessionId ts = do activeAt <- newTVar ts return Client {subscriptions, ntfSubscriptions, rcvQ, sndQ, sessionId, connected, activeAt} +newServerStats :: UTCTime -> STM ServerStats +newServerStats ts = do + qCreated <- newTVar 0 + qSecured <- newTVar 0 + qDeleted <- newTVar 0 + msgSent <- newTVar 0 + msgRecv <- newTVar 0 + msgQueues <- newTVar S.empty + fromTime <- newTVar ts + pure ServerStats {qCreated, qSecured, qDeleted, msgSent, msgRecv, msgQueues, fromTime} + newSubscription :: STM Sub newSubscription = do delivered <- newEmptyTMVar @@ -134,7 +165,8 @@ newEnv config@ServerConfig {caCertificateFile, certificateFile, privateKeyFile, tlsServerParams <- liftIO $ loadTLSServerParams caCertificateFile certificateFile privateKeyFile Fingerprint fp <- liftIO $ loadFingerprint caCertificateFile let serverIdentity = KeyHash fp - return Env {config, server, serverIdentity, queueStore, msgStore, idsDrg, storeLog = s', tlsServerParams} + serverStats <- atomically . newServerStats =<< liftIO getCurrentTime + return Env {config, server, serverIdentity, queueStore, msgStore, idsDrg, storeLog = s', tlsServerParams, serverStats} where restoreQueues :: QueueStore -> StoreLog 'ReadMode -> m (StoreLog 'WriteMode) restoreQueues QueueStore {queues, senders, notifiers} s = do diff --git a/tests/SMPClient.hs b/tests/SMPClient.hs index 0dc15a1af..5e5f7e611 100644 --- a/tests/SMPClient.hs +++ b/tests/SMPClient.hs @@ -63,6 +63,8 @@ cfg = allowNewQueues = True, messageExpiration = Just defaultMessageExpiration, inactiveClientExpiration = Just defaultInactiveClientExpiration, + logStatsInterval = Nothing, + logStatsStartTime = 0, caCertificateFile = "tests/fixtures/ca.crt", privateKeyFile = "tests/fixtures/server.key", certificateFile = "tests/fixtures/server.crt" From 6a1cea93b5c5d41351c9019ea16f412509e11ce8 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Sat, 30 Apr 2022 14:09:25 +0100 Subject: [PATCH 022/131] fix: disconnect socket --- src/Simplex/Messaging/Transport.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Simplex/Messaging/Transport.hs b/src/Simplex/Messaging/Transport.hs index f3f553b79..87f4ef5a9 100644 --- a/src/Simplex/Messaging/Transport.hs +++ b/src/Simplex/Messaging/Transport.hs @@ -176,7 +176,7 @@ withTlsUnique peer cxt f = closeTLS :: T.Context -> IO () closeTLS ctx = T.bye ctx -- sometimes socket was closed before 'TLS.bye' so we catch the 'Broken pipe' error here - `catchAll_` T.contextClose ctx + `E.finally` T.contextClose ctx `catchAll_` pure () supportedParameters :: T.Supported From 964daf5442e1069634762450bc28cfd69a2968a1 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Tue, 3 May 2022 14:32:37 +0100 Subject: [PATCH 023/131] v2.0.0 (#360) * v2.0.0: notification server release * update changelog * update changelog * update changelog --- .github/workflows/build.yml | 6 ++++-- CHANGELOG.md | 19 +++++++++++++++++++ apps/ntf-server/Main.hs | 2 +- package.yaml | 2 +- simplexmq.cabal | 2 +- src/Simplex/Messaging/Transport.hs | 2 +- 6 files changed, 27 insertions(+), 6 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 69603aff9..66d66d8a5 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -19,9 +19,9 @@ jobs: - name: Setup Stack uses: haskell/actions/setup@v1 with: - ghc-version: '8.10.7' + ghc-version: "8.10.7" enable-stack: true - stack-version: 'latest' + stack-version: "latest" - name: Cache dependencies uses: actions/cache@v2 @@ -36,6 +36,7 @@ jobs: stack build --test --force-dirty install_root=$(stack path --local-install-root) mv ${install_root}/bin/smp-server smp-server-ubuntu-20_04-x86-64 + mv ${install_root}/bin/ntf-server ntf-server-ubuntu-20_04-x86-64 - name: Build changelog if: startsWith(github.ref, 'refs/tags/v') @@ -73,6 +74,7 @@ jobs: files: | LICENSE smp-server-ubuntu-20_04-x86-64 + ntf-server-ubuntu-20_04-x86-64 fail_on_unmatched_files: true env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/CHANGELOG.md b/CHANGELOG.md index 42dce14ae..1746ca9c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,22 @@ +# 2.0.0 + +Push notifications server (beta): + +- supports APNS +- manage device tokens verification via notification delivery +- sending periodic background notification to check messages (not more frequent than every 20 min) + +SMP server: + +- disconnect inactive clients after some period +- remove undelivered messages after 30 days +- log aggregate usage daily stats: only the number of queues created/secured/deleted/used and messages sent/delivered is logged, as one line per day, so we can plan server capacity and diagnose any problems. + +SMP agent: + +- manage device tokens and notification server connection +- DOWN/UP events to the agent user about server disconnections/reconnections are now sent once per server + # 1.1.0 SMP server: diff --git a/apps/ntf-server/Main.hs b/apps/ntf-server/Main.hs index 5bef2eb55..b23e75b9d 100644 --- a/apps/ntf-server/Main.hs +++ b/apps/ntf-server/Main.hs @@ -21,7 +21,7 @@ logCfg = LogConfig {lc_file = Nothing, lc_stderr = True} main :: IO () main = do - setLogLevel LogError + setLogLevel LogDebug -- change to LogError in production withGlobalLogging logCfg $ protocolServerCLI ntfServerCLIConfig runNtfServer ntfServerCLIConfig :: ServerCLIConfig NtfServerConfig diff --git a/package.yaml b/package.yaml index 8f894c472..c37b4b19e 100644 --- a/package.yaml +++ b/package.yaml @@ -1,5 +1,5 @@ name: simplexmq -version: 1.1.0 +version: 2.0.0 synopsis: SimpleXMQ message broker description: | This package includes <./docs/Simplex-Messaging-Server.html server>, diff --git a/simplexmq.cabal b/simplexmq.cabal index 55a3eade3..815db3fbb 100644 --- a/simplexmq.cabal +++ b/simplexmq.cabal @@ -5,7 +5,7 @@ cabal-version: 1.12 -- see: https://github.com/sol/hpack name: simplexmq -version: 1.1.0 +version: 2.0.0 synopsis: SimpleXMQ message broker description: This package includes <./docs/Simplex-Messaging-Server.html server>, <./docs/Simplex-Messaging-Client.html client> and diff --git a/src/Simplex/Messaging/Transport.hs b/src/Simplex/Messaging/Transport.hs index 87f4ef5a9..46eb9cf37 100644 --- a/src/Simplex/Messaging/Transport.hs +++ b/src/Simplex/Messaging/Transport.hs @@ -96,7 +96,7 @@ supportedSMPVersions :: VersionRange supportedSMPVersions = mkVersionRange 1 1 simplexMQVersion :: String -simplexMQVersion = "1.1.0" +simplexMQVersion = "2.0.0" -- * Transport connection class From 4e4eea34f9652571f01788ae8719c01d91c014fa Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Wed, 11 May 2022 12:43:54 +0100 Subject: [PATCH 024/131] server: configuration to expire inactive clients in ini file (#369) * server: configuration to expire inactive clients in ini file * corrections Co-authored-by: JRoberts <8711996+jr-simplex@users.noreply.github.com> Co-authored-by: JRoberts <8711996+jr-simplex@users.noreply.github.com> --- apps/ntf-server/Main.hs | 16 +++++++- apps/smp-server/Main.hs | 39 ++++++++++++++++-- src/Simplex/Messaging/Server.hs | 11 +++-- src/Simplex/Messaging/Server/CLI.hs | 54 ++++++++++--------------- src/Simplex/Messaging/Server/Env/STM.hs | 4 +- 5 files changed, 80 insertions(+), 44 deletions(-) diff --git a/apps/ntf-server/Main.hs b/apps/ntf-server/Main.hs index b23e75b9d..17c42e37d 100644 --- a/apps/ntf-server/Main.hs +++ b/apps/ntf-server/Main.hs @@ -42,7 +42,21 @@ ntfServerCLIConfig = defaultServerPort = "443", executableName = "ntf-server", serverVersion = "SMP notifications server v0.1.0", - mkServerConfig = \_storeLogFile transports -> + mkIniFile = \enableStoreLog defaultServerPort -> + "[STORE_LOG]\n\ + \# The server uses STM memory for persistence,\n\ + \# that will be lost on restart (e.g., as with redis).\n\ + \# This option enables saving memory to append only log,\n\ + \# and restoring it when the server is started.\n\ + \# Log is compacted on start (deleted objects are removed).\n\ + \# The messages are not logged.\n" + <> ("enable: " <> (if enableStoreLog then "on" else "off # on") <> "\n\n") + <> "[TRANSPORT]\n\ + \port: " + <> defaultServerPort + <> "\n\ + \websockets: off\n", + mkServerConfig = \_storeLogFile transports _ -> NtfServerConfig { transports, subIdBytes = 24, diff --git a/apps/smp-server/Main.hs b/apps/smp-server/Main.hs index 9cb56e08a..b00b266b5 100644 --- a/apps/smp-server/Main.hs +++ b/apps/smp-server/Main.hs @@ -1,14 +1,16 @@ {-# LANGUAGE DuplicateRecordFields #-} {-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE NumericUnderscores #-} +{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE TypeApplications #-} module Main where import Control.Logger.Simple import Simplex.Messaging.Server (runSMPServer) -import Simplex.Messaging.Server.CLI (ServerCLIConfig (..), protocolServerCLI) +import Simplex.Messaging.Server.CLI (ServerCLIConfig (..), protocolServerCLI, readStrictIni, strictIni) import Simplex.Messaging.Server.Env.STM (ServerConfig (..), defaultInactiveClientExpiration, defaultMessageExpiration) +import Simplex.Messaging.Server.Expiration import Simplex.Messaging.Transport (simplexMQVersion) import System.FilePath (combine) @@ -24,7 +26,11 @@ logCfg = LogConfig {lc_file = Nothing, lc_stderr = True} main :: IO () main = do setLogLevel LogInfo - withGlobalLogging logCfg $ protocolServerCLI smpServerCLIConfig runSMPServer + withGlobalLogging logCfg . protocolServerCLI smpServerCLIConfig $ \cfg@ServerConfig {inactiveClientExpiration} -> do + putStrLn $ case inactiveClientExpiration of + Just ExpirationConfig {ttl, checkInterval} -> "expiring clients inactive for " <> show ttl <> " seconds every " <> show checkInterval <> " seconds" + _ -> "not expiring inactive clients" + runSMPServer cfg smpServerCLIConfig :: ServerCLIConfig ServerConfig smpServerCLIConfig = @@ -44,7 +50,24 @@ smpServerCLIConfig = defaultServerPort = "5223", executableName = "smp-server", serverVersion = "SMP server v" <> simplexMQVersion, - mkServerConfig = \storeLogFile transports -> + mkIniFile = \enableStoreLog defaultServerPort -> + "[STORE_LOG]\n\ + \# The server uses STM memory for persistence,\n\ + \# that will be lost on restart (e.g., as with redis).\n\ + \# This option enables saving memory to append only log,\n\ + \# and restoring it when the server is started.\n\ + \# Log is compacted on start (deleted objects are removed).\n\ + \# The messages are not logged.\n" + <> ("enable: " <> (if enableStoreLog then "on" else "off # on") <> "\n\n") + <> "[TRANSPORT]\n" + <> ("port: " <> defaultServerPort <> "\n") + <> "websockets: off\n\n" + <> "[INACTIVE_CLIENTS]\n\ + \# TTL and interval to check inactive clients\n\ + \disconnect: on\n" + <> ("ttl: " <> show (ttl defaultInactiveClientExpiration) <> "\n") + <> ("check_interval: " <> show (checkInterval defaultInactiveClientExpiration) <> "\n"), + mkServerConfig = \storeLogFile transports ini -> ServerConfig { transports, tbqSize = 16, @@ -58,7 +81,15 @@ smpServerCLIConfig = storeLogFile, allowNewQueues = True, messageExpiration = Just defaultMessageExpiration, - inactiveClientExpiration = Just defaultInactiveClientExpiration, + inactiveClientExpiration = + if strictIni "INACTIVE_CLIENTS" "disconnect" ini == "on" + then + Just + ExpirationConfig + { ttl = readStrictIni "INACTIVE_CLIENTS" "ttl" ini, + checkInterval = readStrictIni "INACTIVE_CLIENTS" "check_interval" ini + } + else Nothing, logStatsInterval = Just 86400, -- seconds logStatsStartTime = 0 -- seconds from 00:00 UTC } diff --git a/src/Simplex/Messaging/Server.hs b/src/Simplex/Messaging/Server.hs index 901f0ee94..aac1db5ee 100644 --- a/src/Simplex/Messaging/Server.hs +++ b/src/Simplex/Messaging/Server.hs @@ -131,7 +131,8 @@ smpServer started = do atomically $ TM.lookupDelete qId (clientSubs c) expireMessagesThread_ :: ServerConfig -> [m ()] - expireMessagesThread_ = maybe [] ((: []) . expireMessages) . messageExpiration + expireMessagesThread_ ServerConfig {messageExpiration = Just msgExp} = [expireMessages msgExp] + expireMessagesThread_ _ = [] expireMessages :: ExpirationConfig -> m () expireMessages expCfg = do @@ -147,8 +148,9 @@ smpServer started = do >>= atomically . (`deleteExpiredMsgs` old) serverStatsThread_ :: ServerConfig -> [m ()] - serverStatsThread_ ServerConfig {logStatsInterval, logStatsStartTime} = - maybe [] ((: []) . logServerStats logStatsStartTime) logStatsInterval + serverStatsThread_ ServerConfig {logStatsInterval = Just interval, logStatsStartTime} = + [logServerStats logStatsStartTime interval] + serverStatsThread_ _ = [] logServerStats :: Int -> Int -> m () logServerStats startAt logInterval = do @@ -186,7 +188,8 @@ runClientTransport th@THandle {sessionId} = do raceAny_ ([send th c, client c s, receive th c] <> disconnectThread_ c expCfg) `finally` clientDisconnected c where - disconnectThread_ c expCfg = maybe [] ((: []) . disconnectTransport th c activeAt) expCfg + disconnectThread_ c (Just expCfg) = [disconnectTransport th c activeAt expCfg] + disconnectThread_ _ _ = [] clientDisconnected :: (MonadUnliftIO m, MonadReader Env m) => Client -> m () clientDisconnected c@Client {subscriptions, connected} = do diff --git a/src/Simplex/Messaging/Server/CLI.hs b/src/Simplex/Messaging/Server/CLI.hs index 63959d45a..7f988ddf7 100644 --- a/src/Simplex/Messaging/Server/CLI.hs +++ b/src/Simplex/Messaging/Server/CLI.hs @@ -43,7 +43,8 @@ data ServerCLIConfig cfg = ServerCLIConfig defaultServerPort :: ServiceName, executableName :: String, serverVersion :: String, - mkServerConfig :: Maybe FilePath -> [(ServiceName, ATransport)] -> cfg + mkIniFile :: Bool -> ServiceName -> String, + mkServerConfig :: Maybe FilePath -> [(ServiceName, ATransport)] -> Ini -> cfg } protocolServerCLI :: ServerCLIConfig cfg -> (cfg -> IO ()) -> IO () @@ -55,7 +56,7 @@ protocolServerCLI cliCfg@ServerCLIConfig {iniFile, executableName} server = _ -> initializeServer cliCfg opts Start -> doesFileExist iniFile >>= \case - True -> readIniFile iniFile >>= either exitError (runServer cliCfg server . mkIniOptions) + True -> readIniFile iniFile >>= either exitError (runServer cliCfg server) _ -> exitError $ "Error: server is not initialized (" <> iniFile <> " does not exist).\nRun `" <> executableName <> " init`." Delete -> cleanup cliCfg >> putStrLn "Deleted configuration and log files" @@ -140,12 +141,12 @@ initializeServer cliCfg InitOptions {enableStoreLog, signAlgorithm, ip, fqdn} = createDirectoryIfMissing True logDir createX509 fp <- saveFingerprint - createIni + writeFile iniFile $ mkIniFile enableStoreLog defaultServerPort putStrLn $ "Server initialized, you can modify configuration in " <> iniFile <> ".\nRun `" <> executableName <> " start` to start server." printServiceInfo cliCfg fp warnCAPrivateKeyFile where - ServerCLIConfig {cfgDir, logDir, iniFile, executableName, caKeyFile, caCrtFile, serverKeyFile, serverCrtFile, fingerprintFile, defaultServerPort} = cliCfg + ServerCLIConfig {cfgDir, logDir, iniFile, executableName, caKeyFile, caCrtFile, serverKeyFile, serverCrtFile, fingerprintFile, defaultServerPort, mkIniFile} = cliCfg createX509 = do createOpensslCaConf createOpensslServerConf @@ -199,22 +200,6 @@ initializeServer cliCfg InitOptions {enableStoreLog, signAlgorithm, ip, fqdn} = withFile fingerprintFile WriteMode (`B.hPutStrLn` strEncode fp) pure fp - createIni = do - writeFile iniFile $ - "[STORE_LOG]\n\ - \# The server uses STM memory for persistence,\n\ - \# that will be lost on restart (e.g., as with redis).\n\ - \# This option enables saving memory to append only log,\n\ - \# and restoring it when the server is started.\n\ - \# Log is compacted on start (deleted objects are removed).\n\ - \# The messages are not logged.\n" - <> ("enable: " <> (if enableStoreLog then "on" else "off # on") <> "\n\n") - <> "[TRANSPORT]\n\ - \port: " - <> defaultServerPort - <> "\n\ - \websockets: off\n" - warnCAPrivateKeyFile = putStrLn $ "----------\n\ @@ -231,29 +216,32 @@ data IniOptions = IniOptions enableWebsockets :: Bool } --- TODO ? properly parse ini as a whole mkIniOptions :: Ini -> IniOptions mkIniOptions ini = IniOptions - { enableStoreLog = (== "on") $ strict "STORE_LOG" "enable", - port = T.unpack $ strict "TRANSPORT" "port", - enableWebsockets = (== "on") $ strict "TRANSPORT" "websockets" + { enableStoreLog = (== "on") $ strictIni "STORE_LOG" "enable" ini, + port = T.unpack $ strictIni "TRANSPORT" "port" ini, + enableWebsockets = (== "on") $ strictIni "TRANSPORT" "websockets" ini } - where - strict :: String -> String -> T.Text - strict section key = - fromRight (error ("no key " <> key <> " in section " <> section)) $ - lookupValue (T.pack section) (T.pack key) ini -runServer :: ServerCLIConfig cfg -> (cfg -> IO ()) -> IniOptions -> IO () -runServer cliCfg server IniOptions {enableStoreLog, port, enableWebsockets} = do +strictIni :: String -> String -> Ini -> T.Text +strictIni section key ini = + fromRight (error ("no key " <> key <> " in section " <> section)) $ + lookupValue (T.pack section) (T.pack key) ini + +readStrictIni :: Read a => String -> String -> Ini -> a +readStrictIni section key = read . T.unpack . strictIni section key + +runServer :: ServerCLIConfig cfg -> (cfg -> IO ()) -> Ini -> IO () +runServer cliCfg server ini = do hSetBuffering stdout LineBuffering hSetBuffering stderr LineBuffering fp <- checkSavedFingerprint printServiceInfo cliCfg fp - let transports = (port, transport @TLS) : [("80", transport @WS) | enableWebsockets] + let IniOptions {enableStoreLog, port, enableWebsockets} = mkIniOptions ini + transports = (port, transport @TLS) : [("80", transport @WS) | enableWebsockets] logFile = if enableStoreLog then Just storeLogFile else Nothing - cfg = mkServerConfig logFile transports + cfg = mkServerConfig logFile transports ini printServerConfig logFile transports server cfg where diff --git a/src/Simplex/Messaging/Server/Env/STM.hs b/src/Simplex/Messaging/Server/Env/STM.hs index 163979d61..d70fe203f 100644 --- a/src/Simplex/Messaging/Server/Env/STM.hs +++ b/src/Simplex/Messaging/Server/Env/STM.hs @@ -70,8 +70,8 @@ defaultMessageExpiration = defaultInactiveClientExpiration :: ExpirationConfig defaultInactiveClientExpiration = ExpirationConfig - { ttl = 7200, -- 2 hours - checkInterval = 3600 -- seconds, 1 hour + { ttl = 86400, -- seconds, 24 hours + checkInterval = 43200 -- seconds, 12 hours } data Env = Env From 8dda99d77f294aa9716c7ffd69f8f28c4d269625 Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Wed, 11 May 2022 15:57:29 +0400 Subject: [PATCH 025/131] v2.1.0 (#370) --- CHANGELOG.md | 6 ++++++ package.yaml | 2 +- simplexmq.cabal | 2 +- src/Simplex/Messaging/Transport.hs | 2 +- 4 files changed, 9 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1746ca9c5..7d3147875 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +# 2.1.0 + +SMP server: + +- configuration to expire inactive clients in ini file, increased TTL and check interval for client expiration + # 2.0.0 Push notifications server (beta): diff --git a/package.yaml b/package.yaml index c37b4b19e..b3fbc9d50 100644 --- a/package.yaml +++ b/package.yaml @@ -1,5 +1,5 @@ name: simplexmq -version: 2.0.0 +version: 2.1.0 synopsis: SimpleXMQ message broker description: | This package includes <./docs/Simplex-Messaging-Server.html server>, diff --git a/simplexmq.cabal b/simplexmq.cabal index 815db3fbb..c9d33c5a1 100644 --- a/simplexmq.cabal +++ b/simplexmq.cabal @@ -5,7 +5,7 @@ cabal-version: 1.12 -- see: https://github.com/sol/hpack name: simplexmq -version: 2.0.0 +version: 2.1.0 synopsis: SimpleXMQ message broker description: This package includes <./docs/Simplex-Messaging-Server.html server>, <./docs/Simplex-Messaging-Client.html client> and diff --git a/src/Simplex/Messaging/Transport.hs b/src/Simplex/Messaging/Transport.hs index 46eb9cf37..72fd353e5 100644 --- a/src/Simplex/Messaging/Transport.hs +++ b/src/Simplex/Messaging/Transport.hs @@ -96,7 +96,7 @@ supportedSMPVersions :: VersionRange supportedSMPVersions = mkVersionRange 1 1 simplexMQVersion :: String -simplexMQVersion = "2.0.0" +simplexMQVersion = "2.1.0" -- * Transport connection class From 49070fffe0667aeb922fa11808912b64003c29bc Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Wed, 18 May 2022 18:30:06 +0100 Subject: [PATCH 026/131] make server compatible with old ini files (#372) * make server compatible with old ini files * fix * clean up --- apps/smp-server/Main.hs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/apps/smp-server/Main.hs b/apps/smp-server/Main.hs index b00b266b5..86d599ad8 100644 --- a/apps/smp-server/Main.hs +++ b/apps/smp-server/Main.hs @@ -7,8 +7,9 @@ module Main where import Control.Logger.Simple +import Data.Ini (lookupValue) import Simplex.Messaging.Server (runSMPServer) -import Simplex.Messaging.Server.CLI (ServerCLIConfig (..), protocolServerCLI, readStrictIni, strictIni) +import Simplex.Messaging.Server.CLI (ServerCLIConfig (..), protocolServerCLI, readStrictIni) import Simplex.Messaging.Server.Env.STM (ServerConfig (..), defaultInactiveClientExpiration, defaultMessageExpiration) import Simplex.Messaging.Server.Expiration import Simplex.Messaging.Transport (simplexMQVersion) @@ -82,7 +83,7 @@ smpServerCLIConfig = allowNewQueues = True, messageExpiration = Just defaultMessageExpiration, inactiveClientExpiration = - if strictIni "INACTIVE_CLIENTS" "disconnect" ini == "on" + if lookupValue "INACTIVE_CLIENTS" "disconnect" ini == Right "on" then Just ExpirationConfig From c74f4d729b91bad6268f8a7958464a9cf4fd98d3 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Tue, 24 May 2022 19:28:40 +0100 Subject: [PATCH 027/131] create/verify agent schema during tests (#374) * create/verify agent schema during tests * add --indent to .schema, enable all tests * remove -threaded from test --- simplexmq.cabal | 1 + .../Store/SQLite/Migrations/agent_schema.sql | 161 ++++++++++++++++++ tests/AgentTests.hs | 2 + tests/AgentTests/SchemaDump.hs | 29 ++++ 4 files changed, 193 insertions(+) create mode 100644 src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql create mode 100644 tests/AgentTests/SchemaDump.hs diff --git a/simplexmq.cabal b/simplexmq.cabal index c9d33c5a1..48a1f354e 100644 --- a/simplexmq.cabal +++ b/simplexmq.cabal @@ -329,6 +329,7 @@ test-suite smp-server-test AgentTests.DoubleRatchetTests AgentTests.FunctionalAPITests AgentTests.NotificationTests + AgentTests.SchemaDump AgentTests.SQLiteTests CoreTests.EncodingTests CoreTests.ProtocolErrorTests diff --git a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql new file mode 100644 index 000000000..4fd602e87 --- /dev/null +++ b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql @@ -0,0 +1,161 @@ +CREATE TABLE migrations( + name TEXT NOT NULL, + ts TEXT NOT NULL, + PRIMARY KEY(name) +); +CREATE TABLE servers( + host TEXT NOT NULL, + port TEXT NOT NULL, + key_hash BLOB NOT NULL, + PRIMARY KEY(host, port) +) WITHOUT ROWID; +CREATE TABLE connections( + conn_id BLOB NOT NULL PRIMARY KEY, + conn_mode TEXT NOT NULL, + last_internal_msg_id INTEGER NOT NULL DEFAULT 0, + last_internal_rcv_msg_id INTEGER NOT NULL DEFAULT 0, + last_internal_snd_msg_id INTEGER NOT NULL DEFAULT 0, + last_external_snd_msg_id INTEGER NOT NULL DEFAULT 0, + last_rcv_msg_hash BLOB NOT NULL DEFAULT x'', + last_snd_msg_hash BLOB NOT NULL DEFAULT x'', + smp_agent_version INTEGER NOT NULL DEFAULT 1 +) WITHOUT ROWID; +CREATE TABLE rcv_queues( + host TEXT NOT NULL, + port TEXT NOT NULL, + rcv_id BLOB NOT NULL, + conn_id BLOB NOT NULL REFERENCES connections ON DELETE CASCADE, + rcv_private_key BLOB NOT NULL, + rcv_dh_secret BLOB NOT NULL, + e2e_priv_key BLOB NOT NULL, + e2e_dh_secret BLOB, + snd_id BLOB NOT NULL, + snd_key BLOB, + status TEXT NOT NULL, + smp_server_version INTEGER NOT NULL DEFAULT 1, + smp_client_version INTEGER, + PRIMARY KEY(host, port, rcv_id), + FOREIGN KEY(host, port) REFERENCES servers + ON DELETE RESTRICT ON UPDATE CASCADE, + UNIQUE(host, port, snd_id) +) WITHOUT ROWID; +CREATE TABLE snd_queues( + host TEXT NOT NULL, + port TEXT NOT NULL, + snd_id BLOB NOT NULL, + conn_id BLOB NOT NULL REFERENCES connections ON DELETE CASCADE, + snd_private_key BLOB NOT NULL, + e2e_dh_secret BLOB NOT NULL, + status TEXT NOT NULL, + smp_server_version INTEGER NOT NULL DEFAULT 1, + smp_client_version INTEGER NOT NULL DEFAULT 1, + snd_public_key BLOB, + e2e_pub_key BLOB, + PRIMARY KEY(host, port, snd_id), + FOREIGN KEY(host, port) REFERENCES servers + ON DELETE RESTRICT ON UPDATE CASCADE +) WITHOUT ROWID; +CREATE TABLE messages( + conn_id BLOB NOT NULL REFERENCES connections(conn_id) + ON DELETE CASCADE, + internal_id INTEGER NOT NULL, + internal_ts TEXT NOT NULL, + internal_rcv_id INTEGER, + internal_snd_id INTEGER, + msg_type BLOB NOT NULL, --(H)ELLO,(R)EPLY,(D)ELETE. Should SMP confirmation be saved too? + msg_body BLOB NOT NULL DEFAULT x'', + PRIMARY KEY(conn_id, internal_id), + FOREIGN KEY(conn_id, internal_rcv_id) REFERENCES rcv_messages + ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED, + FOREIGN KEY(conn_id, internal_snd_id) REFERENCES snd_messages + ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED +) WITHOUT ROWID; +CREATE TABLE rcv_messages( + conn_id BLOB NOT NULL, + internal_rcv_id INTEGER NOT NULL, + internal_id INTEGER NOT NULL, + external_snd_id INTEGER NOT NULL, + broker_id BLOB NOT NULL, + broker_ts TEXT NOT NULL, + internal_hash BLOB NOT NULL, + external_prev_snd_hash BLOB NOT NULL, + integrity BLOB NOT NULL, + PRIMARY KEY(conn_id, internal_rcv_id), + FOREIGN KEY(conn_id, internal_id) REFERENCES messages + ON DELETE CASCADE +) WITHOUT ROWID; +CREATE TABLE snd_messages( + conn_id BLOB NOT NULL, + internal_snd_id INTEGER NOT NULL, + internal_id INTEGER NOT NULL, + internal_hash BLOB NOT NULL, + previous_msg_hash BLOB NOT NULL DEFAULT x'', + PRIMARY KEY(conn_id, internal_snd_id), + FOREIGN KEY(conn_id, internal_id) REFERENCES messages + ON DELETE CASCADE +) WITHOUT ROWID; +CREATE TABLE conn_confirmations( + confirmation_id BLOB NOT NULL PRIMARY KEY, + conn_id BLOB NOT NULL REFERENCES connections ON DELETE CASCADE, + e2e_snd_pub_key BLOB NOT NULL, -- TODO per-queue key. Split? + sender_key BLOB NOT NULL, -- TODO per-queue key. Split? + ratchet_state BLOB NOT NULL, + sender_conn_info BLOB NOT NULL, + accepted INTEGER NOT NULL, + own_conn_info BLOB, + created_at TEXT NOT NULL DEFAULT(datetime('now')) +) WITHOUT ROWID; +CREATE TABLE conn_invitations( + invitation_id BLOB NOT NULL PRIMARY KEY, + contact_conn_id BLOB NOT NULL REFERENCES connections ON DELETE CASCADE, + cr_invitation BLOB NOT NULL, + recipient_conn_info BLOB NOT NULL, + accepted INTEGER NOT NULL DEFAULT 0, + own_conn_info BLOB, + created_at TEXT NOT NULL DEFAULT(datetime('now')) +) WITHOUT ROWID; +CREATE TABLE ratchets( + conn_id BLOB NOT NULL PRIMARY KEY REFERENCES connections + ON DELETE CASCADE, + -- x3dh keys are not saved on the sending side(the side accepting the connection) + x3dh_priv_key_1 BLOB, + x3dh_priv_key_2 BLOB, + -- ratchet is initially empty on the receiving side(the side offering the connection) + ratchet_state BLOB, + e2e_version INTEGER NOT NULL DEFAULT 1 +) WITHOUT ROWID; +CREATE TABLE skipped_messages( + skipped_message_id INTEGER PRIMARY KEY, + conn_id BLOB NOT NULL REFERENCES ratchets + ON DELETE CASCADE, + header_key BLOB NOT NULL, + msg_n INTEGER NOT NULL, + msg_key BLOB NOT NULL +); +CREATE TABLE ntf_servers( + ntf_host TEXT NOT NULL, + ntf_port TEXT NOT NULL, + ntf_key_hash BLOB NOT NULL, + created_at TEXT NOT NULL DEFAULT(datetime('now')), + updated_at TEXT NOT NULL DEFAULT(datetime('now')), + PRIMARY KEY(ntf_host, ntf_port) +) WITHOUT ROWID; +CREATE TABLE ntf_tokens( + provider TEXT NOT NULL, -- apn + device_token TEXT NOT NULL, + ntf_host TEXT NOT NULL, + ntf_port TEXT NOT NULL, + tkn_id BLOB, -- token ID assigned by notifications server + tkn_pub_key BLOB NOT NULL, -- client's public key to verify token commands(used by server, for repeat registraions) +tkn_priv_key BLOB NOT NULL, -- client's private key to sign token commands +tkn_pub_dh_key BLOB NOT NULL, -- client's public DH key(for repeat registraions) +tkn_priv_dh_key BLOB NOT NULL, -- client's private DH key(for repeat registraions) +tkn_dh_secret BLOB, -- DH secret for e2e encryption of notifications + tkn_status TEXT NOT NULL, + tkn_action BLOB, + created_at TEXT NOT NULL DEFAULT(datetime('now')), + updated_at TEXT NOT NULL DEFAULT(datetime('now')), -- this is to check token status periodically to know when it was last checked + PRIMARY KEY(provider, device_token, ntf_host, ntf_port), + FOREIGN KEY(ntf_host, ntf_port) REFERENCES ntf_servers + ON DELETE RESTRICT ON UPDATE CASCADE +) WITHOUT ROWID; diff --git a/tests/AgentTests.hs b/tests/AgentTests.hs index f71531fa2..36cfc30fb 100644 --- a/tests/AgentTests.hs +++ b/tests/AgentTests.hs @@ -13,6 +13,7 @@ import AgentTests.ConnectionRequestTests import AgentTests.DoubleRatchetTests (doubleRatchetTests) import AgentTests.FunctionalAPITests (functionalAPITests) import AgentTests.SQLiteTests (storeTests) +import AgentTests.SchemaDump (schemaDumpTest) import Control.Concurrent import Control.Monad (forM_) import Data.ByteString.Char8 (ByteString) @@ -36,6 +37,7 @@ agentTests (ATransport t) = do describe "Double ratchet tests" doubleRatchetTests describe "Functional API" $ functionalAPITests (ATransport t) describe "SQLite store" storeTests + describe "SQLite schema dump" schemaDumpTest describe "SMP agent protocol syntax" $ syntaxTests t describe "Establishing duplex connection" $ do it "should connect via one server and one agent" $ diff --git a/tests/AgentTests/SchemaDump.hs b/tests/AgentTests/SchemaDump.hs new file mode 100644 index 000000000..214730012 --- /dev/null +++ b/tests/AgentTests/SchemaDump.hs @@ -0,0 +1,29 @@ +{-# LANGUAGE OverloadedStrings #-} + +module AgentTests.SchemaDump where + +import Control.Monad (void) +import Simplex.Messaging.Agent.Store.SQLite +import qualified Simplex.Messaging.Agent.Store.SQLite.Migrations as Migrations +import System.Process (readCreateProcess, shell) +import Test.Hspec + +testDB :: FilePath +testDB = "tests/tmp/test_agent_schema.db" + +schema :: FilePath +schema = "src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql" + +schemaDumpTest :: Spec +schemaDumpTest = + it "verify and overwrite schema dump" testVerifySchemaDump + +testVerifySchemaDump :: IO () +testVerifySchemaDump = do + void $ createSQLiteStore testDB 1 Migrations.app False + void $ readCreateProcess (shell $ "touch " <> schema) "" + savedSchema <- readFile schema + savedSchema `seq` pure () + void $ readCreateProcess (shell $ "sqlite3 " <> testDB <> " '.schema --indent' > " <> schema) "" + currentSchema <- readFile schema + savedSchema `shouldBe` currentSchema From fd410280b5aa479b4dee0e491f65029153fd5054 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Tue, 24 May 2022 19:38:14 +0100 Subject: [PATCH 028/131] gracefully close sockets on client disconnection (#373) * gracefully close sockets on client disconnection * remove -threaded --- apps/ntf-server/Main.hs | 4 ++-- apps/smp-server/Main.hs | 6 +++--- src/Simplex/Messaging/Transport/Server.hs | 16 ++++++++-------- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/apps/ntf-server/Main.hs b/apps/ntf-server/Main.hs index 17c42e37d..5111955cd 100644 --- a/apps/ntf-server/Main.hs +++ b/apps/ntf-server/Main.hs @@ -5,7 +5,7 @@ module Main where import Control.Logger.Simple import Simplex.Messaging.Client.Agent (defaultSMPClientAgentConfig) import Simplex.Messaging.Notifications.Server (runNtfServer) -import Simplex.Messaging.Notifications.Server.Env (NtfServerConfig (..), defaultInactiveClientExpiration) +import Simplex.Messaging.Notifications.Server.Env (NtfServerConfig (..)) import Simplex.Messaging.Notifications.Server.Push.APNS (defaultAPNSPushClientConfig) import Simplex.Messaging.Server.CLI (ServerCLIConfig (..), protocolServerCLI) import System.FilePath (combine) @@ -66,7 +66,7 @@ ntfServerCLIConfig = pushQSize = 128, smpAgentCfg = defaultSMPClientAgentConfig, apnsConfig = defaultAPNSPushClientConfig, - inactiveClientExpiration = Just defaultInactiveClientExpiration, + inactiveClientExpiration = Nothing, caCertificateFile = caCrtFile, privateKeyFile = serverKeyFile, certificateFile = serverCrtFile diff --git a/apps/smp-server/Main.hs b/apps/smp-server/Main.hs index 86d599ad8..5f9253665 100644 --- a/apps/smp-server/Main.hs +++ b/apps/smp-server/Main.hs @@ -65,9 +65,9 @@ smpServerCLIConfig = <> "websockets: off\n\n" <> "[INACTIVE_CLIENTS]\n\ \# TTL and interval to check inactive clients\n\ - \disconnect: on\n" - <> ("ttl: " <> show (ttl defaultInactiveClientExpiration) <> "\n") - <> ("check_interval: " <> show (checkInterval defaultInactiveClientExpiration) <> "\n"), + \disconnect: off\n" + <> ("# ttl: " <> show (ttl defaultInactiveClientExpiration) <> "\n") + <> ("# check_interval: " <> show (checkInterval defaultInactiveClientExpiration) <> "\n"), mkServerConfig = \storeLogFile transports ini -> ServerConfig { transports, diff --git a/src/Simplex/Messaging/Transport/Server.hs b/src/Simplex/Messaging/Transport/Server.hs index 55393d278..c2e12aff0 100644 --- a/src/Simplex/Messaging/Transport/Server.hs +++ b/src/Simplex/Messaging/Transport/Server.hs @@ -41,27 +41,27 @@ runTransportServer started port serverParams server = do E.bracket (startTCPServer started port) (closeServer started clients) - $ \sock -> forever $ do - (connSock, _) <- accept sock - tid <- forkIO $ connectClient u connSock `catchAll_` close connSock `catchAll_` pure () + $ \sock -> forever . E.bracketOnError (accept sock) (close . fst) $ \(conn, _peer) -> do + -- catchAll_ is needed here in case the connection was closed earlier + tid <- forkFinally (connectClient u conn) (const . liftIO $ gracefulClose conn 5000 `catchAll_` pure ()) atomically . modifyTVar' clients $ S.insert tid where connectClient :: UnliftIO m -> Socket -> IO () - connectClient u connSock = + connectClient u conn = E.bracket - (connectTLS serverParams connSock >>= getServerConnection) + (connectTLS serverParams conn >>= getServerConnection) closeConnection (unliftIO u . server) +-- | Run TCP server without TLS - only used in SimpleX Chat runTCPServer :: TMVar Bool -> ServiceName -> (Socket -> IO ()) -> IO () runTCPServer started port server = do clients <- newTVarIO S.empty E.bracket (startTCPServer started port) (closeServer started clients) - $ \sock -> forever $ do - (connSock, _) <- accept sock - tid <- forkIO $ server connSock `catchAll_` pure () + $ \sock -> forever . E.bracketOnError (accept sock) (close . fst) $ \(conn, _peer) -> do + tid <- forkFinally (server conn) (const $ gracefulClose conn 5000) atomically . modifyTVar' clients $ S.insert tid closeServer :: TMVar Bool -> TVar (Set ThreadId) -> Socket -> IO () From ce3ca081999675f05c1c8a3b1a9b3810cbc1f485 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Tue, 24 May 2022 19:45:22 +0100 Subject: [PATCH 029/131] server: CLI warning when deleting server configuration (#371) * server: CLI warning when deleting server configuration * correction Co-authored-by: JRoberts <8711996+jr-simplex@users.noreply.github.com> Co-authored-by: JRoberts <8711996+jr-simplex@users.noreply.github.com> --- src/Simplex/Messaging/Server/CLI.hs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/Simplex/Messaging/Server/CLI.hs b/src/Simplex/Messaging/Server/CLI.hs index 7f988ddf7..bfa095270 100644 --- a/src/Simplex/Messaging/Server/CLI.hs +++ b/src/Simplex/Messaging/Server/CLI.hs @@ -26,7 +26,7 @@ import Simplex.Messaging.Transport.WebSockets (WS) import System.Directory (createDirectoryIfMissing, doesDirectoryExist, doesFileExist, removeDirectoryRecursive) import System.Exit (exitFailure) import System.FilePath (combine) -import System.IO (BufferMode (..), IOMode (..), hGetLine, hSetBuffering, stderr, stdout, withFile) +import System.IO (BufferMode (..), IOMode (..), hFlush, hGetLine, hSetBuffering, stderr, stdout, withFile) import System.Process (readCreateProcess, shell) import Text.Read (readMaybe) @@ -58,11 +58,22 @@ protocolServerCLI cliCfg@ServerCLIConfig {iniFile, executableName} server = doesFileExist iniFile >>= \case True -> readIniFile iniFile >>= either exitError (runServer cliCfg server) _ -> exitError $ "Error: server is not initialized (" <> iniFile <> " does not exist).\nRun `" <> executableName <> " init`." - Delete -> cleanup cliCfg >> putStrLn "Deleted configuration and log files" + Delete -> do + confirmOrExit "WARNING: deleting the server will make all queues inaccessible, because the server identity (certificate fingerprint) will change.\nTHIS CANNOT BE UNDONE!" + cleanup cliCfg + putStrLn "Deleted configuration and log files" exitError :: String -> IO () exitError msg = putStrLn msg >> exitFailure +confirmOrExit :: String -> IO () +confirmOrExit s = do + putStrLn s + putStr "Continue (Y/n): " + hFlush stdout + ok <- getLine + when (ok /= "Y") exitFailure + data CliCommand = Init InitOptions | Start From b67c3e593579f8b7fa6df00d3defc5b93f235a5f Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Wed, 25 May 2022 06:38:17 +0100 Subject: [PATCH 030/131] version 2.1.1 (#375) --- package.yaml | 2 +- simplexmq.cabal | 2 +- src/Simplex/Messaging/Transport.hs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package.yaml b/package.yaml index b3fbc9d50..177d4ad86 100644 --- a/package.yaml +++ b/package.yaml @@ -1,5 +1,5 @@ name: simplexmq -version: 2.1.0 +version: 2.1.1 synopsis: SimpleXMQ message broker description: | This package includes <./docs/Simplex-Messaging-Server.html server>, diff --git a/simplexmq.cabal b/simplexmq.cabal index 48a1f354e..14abecc27 100644 --- a/simplexmq.cabal +++ b/simplexmq.cabal @@ -5,7 +5,7 @@ cabal-version: 1.12 -- see: https://github.com/sol/hpack name: simplexmq -version: 2.1.0 +version: 2.1.1 synopsis: SimpleXMQ message broker description: This package includes <./docs/Simplex-Messaging-Server.html server>, <./docs/Simplex-Messaging-Client.html client> and diff --git a/src/Simplex/Messaging/Transport.hs b/src/Simplex/Messaging/Transport.hs index 72fd353e5..9f58e5441 100644 --- a/src/Simplex/Messaging/Transport.hs +++ b/src/Simplex/Messaging/Transport.hs @@ -96,7 +96,7 @@ supportedSMPVersions :: VersionRange supportedSMPVersions = mkVersionRange 1 1 simplexMQVersion :: String -simplexMQVersion = "2.1.0" +simplexMQVersion = "2.1.1" -- * Transport connection class From 95a2f7560d3080c1b3e5c3557042ecb54b1af1b9 Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Wed, 25 May 2022 10:08:55 +0400 Subject: [PATCH 031/131] add 2.1.1 to changelog --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d3147875..c7af5a10a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +# 2.1.1 + +SMP server: + +- gracefully close sockets on client disconnection +- CLI warning when deleting server configuration + # 2.1.0 SMP server: From 9f6316fa6dbdf8b23aa026deb750b64ba9b332a5 Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Mon, 30 May 2022 22:58:47 +0400 Subject: [PATCH 032/131] fix getDeviceNtfToken (#376) --- src/Simplex/Messaging/Agent/Store/SQLite.hs | 8 ++++---- .../Store/SQLite/Migrations/M20220322_notifications.hs | 2 +- .../Agent/Store/SQLite/Migrations/agent_schema.sql | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Simplex/Messaging/Agent/Store/SQLite.hs b/src/Simplex/Messaging/Agent/Store/SQLite.hs index 0400ef07e..5c4e679b7 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite.hs @@ -45,7 +45,7 @@ import Data.Text (Text) import qualified Data.Text as T import Data.Text.Encoding (decodeLatin1) import Data.Time.Clock (getCurrentTime) -import Database.SQLite.Simple (FromRow, NamedParam (..), Only (..), SQLError, ToRow, field) +import Database.SQLite.Simple (FromRow, NamedParam (..), Only (..), SQLError, ToRow, field, (:.) (..)) import qualified Database.SQLite.Simple as DB import Database.SQLite.Simple.FromField import Database.SQLite.Simple.QQ (sql) @@ -587,16 +587,16 @@ instance (MonadUnliftIO m, MonadError StoreError m) => MonadAgentStore SQLiteSto db [sql| SELECT s.ntf_host, s.ntf_port, s.ntf_key_hash, - t.tkn_id, t.tkn_pub_key, t.tkn_priv_key, t.tkn_pub_dh_key, t.tkn_priv_dh_key, t.tkn_dh_secret, t.tkn_status, t.tkn_action + t.provider, t.device_token, t.tkn_id, t.tkn_pub_key, t.tkn_priv_key, t.tkn_pub_dh_key, t.tkn_priv_dh_key, t.tkn_dh_secret, t.tkn_status, t.tkn_action FROM ntf_tokens t JOIN ntf_servers s USING (ntf_host, ntf_port) |] pure . first listToMaybe $ partition ((t ==) . deviceToken) tokens where - ntfToken (host, port, keyHash, ntfTokenId, ntfPubKey, ntfPrivKey, ntfDhPubKey, ntfDhPrivKey, ntfDhSecret, ntfTknStatus, ntfTknAction) = + ntfToken ((host, port, keyHash) :. (provider, dt, ntfTokenId, ntfPubKey, ntfPrivKey, ntfDhPubKey, ntfDhPrivKey, ntfDhSecret, ntfTknStatus, ntfTknAction)) = let ntfServer = ProtocolServer {host, port, keyHash} ntfDhKeys = (ntfDhPubKey, ntfDhPrivKey) - in NtfToken {deviceToken = t, ntfServer, ntfTokenId, ntfPubKey, ntfPrivKey, ntfDhKeys, ntfDhSecret, ntfTknStatus, ntfTknAction} + in NtfToken {deviceToken = DeviceToken provider dt, ntfServer, ntfTokenId, ntfPubKey, ntfPrivKey, ntfDhKeys, ntfDhSecret, ntfTknStatus, ntfTknAction} updateNtfTokenRegistration :: SQLiteStore -> NtfToken -> NtfTokenId -> C.DhSecretX25519 -> m () updateNtfTokenRegistration st NtfToken {deviceToken = DeviceToken provider token, ntfServer = ProtocolServer {host, port}} tknId ntfDhSecret = diff --git a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220322_notifications.hs b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220322_notifications.hs index 4b93c0db9..4ee6695d4 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220322_notifications.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220322_notifications.hs @@ -19,7 +19,7 @@ CREATE TABLE ntf_servers ( CREATE TABLE ntf_tokens ( provider TEXT NOT NULL, -- apn - device_token TEXT NOT NULL, + device_token TEXT NOT NULL, -- ! this field is mislabeled and is actually saved as binary ntf_host TEXT NOT NULL, ntf_port TEXT NOT NULL, tkn_id BLOB, -- token ID assigned by notifications server diff --git a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql index 4fd602e87..d5fbc7718 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql +++ b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql @@ -142,7 +142,7 @@ CREATE TABLE ntf_servers( ) WITHOUT ROWID; CREATE TABLE ntf_tokens( provider TEXT NOT NULL, -- apn - device_token TEXT NOT NULL, + device_token TEXT NOT NULL, -- ! this field is mislabeled and is actually saved as binary ntf_host TEXT NOT NULL, ntf_port TEXT NOT NULL, tkn_id BLOB, -- token ID assigned by notifications server From ef7c66762ea43612e9001aa9a1544220e4798334 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Mon, 30 May 2022 21:16:25 +0100 Subject: [PATCH 033/131] update script to send test notifications --- .../Messaging/Notifications/Server/Push/testpush.sh | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Simplex/Messaging/Notifications/Server/Push/testpush.sh b/src/Simplex/Messaging/Notifications/Server/Push/testpush.sh index 2af08356e..b41135d83 100755 --- a/src/Simplex/Messaging/Notifications/Server/Push/testpush.sh +++ b/src/Simplex/Messaging/Notifications/Server/Push/testpush.sh @@ -15,6 +15,11 @@ export JWT_HEADER_CLAIMS="${JWT_HEADER}.${JWT_CLAIMS}" export JWT_SIGNED_HEADER_CLAIMS=$(printf "${JWT_HEADER_CLAIMS}" | openssl dgst -binary -sha256 -sign "${APNS_KEY_FILE}" | openssl base64 -e -A | tr -- '+/' '-_' | tr -d =) export AUTHENTICATION_TOKEN="${JWT_HEADER}.${JWT_CLAIMS}.${JWT_SIGNED_HEADER_CLAIMS}" -curl -v --header "apns-topic: $TOPIC" --header "apns-push-type: alert" --header "authorization: bearer $AUTHENTICATION_TOKEN" --data '{"aps":{"alert":"you have a new message"},"data":{"test":"123"}}' --http2 https://${APNS_HOST_NAME}/3/device/${DEVICE_TOKEN} +# simple alert +# curl -v --header "apns-topic: $TOPIC" --header "apns-push-type: alert" --header "authorization: bearer $AUTHENTICATION_TOKEN" --data '{"aps":{"alert":"you have a new message"},"data":{"test":"123"}}' --http2 https://${APNS_HOST_NAME}/3/device/${DEVICE_TOKEN} +# background notification # curl -v --header "apns-topic: $TOPIC" --header "apns-push-type: background" --header "apns-priority: 5" --header "authorization: bearer $AUTHENTICATION_TOKEN" --data '{"aps":{"content-available":1}}' --http2 https://${APNS_HOST_NAME}/3/device/${DEVICE_TOKEN} + +# mutable-content notification +curl -v --header "apns-topic: $TOPIC" --header "apns-push-type: alert" --header "authorization: bearer $AUTHENTICATION_TOKEN" --data '{"aps":{"category": "NTF_CAT_CHECK_MESSAGE__SECRET", "mutable-content": 1, "alert":"received encrypted message"}, "data": {"test":"123"}}' --http2 https://${APNS_HOST_NAME}/3/device/${DEVICE_TOKEN} \ No newline at end of file From c2ec691a46c806c4034767047ce9ceda23fed813 Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Tue, 31 May 2022 15:40:43 +0400 Subject: [PATCH 034/131] ntf subscription diagram (#377) --- .../diagrams/notifications/subscription.mmd | 40 +++++++++++++++++++ src/Simplex/Messaging/Notifications/Client.hs | 2 +- .../Messaging/Notifications/Protocol.hs | 2 +- 3 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 protocol/diagrams/notifications/subscription.mmd diff --git a/protocol/diagrams/notifications/subscription.mmd b/protocol/diagrams/notifications/subscription.mmd new file mode 100644 index 000000000..0db7cd7cc --- /dev/null +++ b/protocol/diagrams/notifications/subscription.mmd @@ -0,0 +1,40 @@ +sequenceDiagram + participant M as mobile app + participant C as chat core + participant A as agent + participant S as SMP server + participant N as NTF server + participant APN as APN + + note over M, APN: register subscription + + alt register existing + M -->> A: on /_ntf register, for subscribed queues + else create new connection + A -->> S: NEW / JOIN + note over A, S: ...
Connection handshake
... + S -->> A: CON + end + A ->> S: NKEY nKey + S ->> A: NID nId + A ->> N: SNEW tknId dhKey (smpServer, nId, nKey) + N ->> A: ID subId dhKey + N ->> S: NSUB nId + S ->> N: OK [/ NMSG] + + note over M, APN: notify about message + + S ->> N: NMSG + N ->> APN: APNSMutableContent
ntfQueue, nonce + APN ->> M: UNMutableNotificationContent + note over M, S: ...
Client awaken, message is received
... + S ->> M: message + note over M: mutate notification + + note over M, APN: change APN token + + APN ->> M: new device token + M -->> C: /_ntf_sub update tkn + C -->> A: updateNtfToken() + A -->> N: TUPD tknId newDeviceToken + note over M, N: ...
Verify token
... diff --git a/src/Simplex/Messaging/Notifications/Client.hs b/src/Simplex/Messaging/Notifications/Client.hs index f7dc4fc52..3a5e8c827 100644 --- a/src/Simplex/Messaging/Notifications/Client.hs +++ b/src/Simplex/Messaging/Notifications/Client.hs @@ -48,7 +48,7 @@ ntfEnableCron c pKey tknId int = okNtfCommand (TCRN int) c pKey tknId ntfCreateSubsciption :: NtfClient -> C.APrivateSignKey -> NewNtfEntity 'Subscription -> ExceptT ProtocolClientError IO (NtfSubscriptionId, C.PublicKeyX25519) ntfCreateSubsciption c pKey newSub = sendNtfCommand c (Just pKey) "" (SNEW newSub) >>= \case - NRId tknId dhKey -> pure (tknId, dhKey) + NRId subId dhKey -> pure (subId, dhKey) _ -> throwE PCEUnexpectedResponse ntfCheckSubscription :: NtfClient -> C.APrivateSignKey -> NtfSubscriptionId -> ExceptT ProtocolClientError IO NtfSubStatus diff --git a/src/Simplex/Messaging/Notifications/Protocol.hs b/src/Simplex/Messaging/Notifications/Protocol.hs index 3640004e0..04fb3e21f 100644 --- a/src/Simplex/Messaging/Notifications/Protocol.hs +++ b/src/Simplex/Messaging/Notifications/Protocol.hs @@ -120,7 +120,7 @@ instance ToJSON NtfRegCode where data NewNtfEntity (e :: NtfEntity) where NewNtfTkn :: DeviceToken -> C.APublicVerifyKey -> C.PublicKeyX25519 -> NewNtfEntity 'Token - NewNtfSub :: NtfTokenId -> SMPQueueNtf -> NewNtfEntity 'Subscription + NewNtfSub :: NtfTokenId -> SMPQueueNtf -> NewNtfEntity 'Subscription -- NtfTokenId -> C.APublicVerifyKey -> SMPQueueNtf deriving instance Show (NewNtfEntity e) From b60408837a35569eddb87661ebdd4a73a275e4e4 Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Wed, 1 Jun 2022 18:42:35 +0400 Subject: [PATCH 035/131] notifications: SNEW draft (#378) --- .../Messaging/Notifications/Protocol.hs | 2 +- src/Simplex/Messaging/Notifications/Server.hs | 67 ++++++++++++++++--- .../Messaging/Notifications/Server/Env.hs | 19 ++++-- .../Messaging/Notifications/Server/Store.hs | 39 ++++++++++- 4 files changed, 111 insertions(+), 16 deletions(-) diff --git a/src/Simplex/Messaging/Notifications/Protocol.hs b/src/Simplex/Messaging/Notifications/Protocol.hs index 04fb3e21f..3640004e0 100644 --- a/src/Simplex/Messaging/Notifications/Protocol.hs +++ b/src/Simplex/Messaging/Notifications/Protocol.hs @@ -120,7 +120,7 @@ instance ToJSON NtfRegCode where data NewNtfEntity (e :: NtfEntity) where NewNtfTkn :: DeviceToken -> C.APublicVerifyKey -> C.PublicKeyX25519 -> NewNtfEntity 'Token - NewNtfSub :: NtfTokenId -> SMPQueueNtf -> NewNtfEntity 'Subscription -- NtfTokenId -> C.APublicVerifyKey -> SMPQueueNtf + NewNtfSub :: NtfTokenId -> SMPQueueNtf -> NewNtfEntity 'Subscription deriving instance Show (NewNtfEntity e) diff --git a/src/Simplex/Messaging/Notifications/Server.hs b/src/Simplex/Messaging/Notifications/Server.hs index 2bb412b39..a8e225fad 100644 --- a/src/Simplex/Messaging/Notifications/Server.hs +++ b/src/Simplex/Messaging/Notifications/Server.hs @@ -26,7 +26,7 @@ import Simplex.Messaging.Notifications.Server.Env import Simplex.Messaging.Notifications.Server.Push.APNS import Simplex.Messaging.Notifications.Server.Store import Simplex.Messaging.Notifications.Transport -import Simplex.Messaging.Protocol (ErrorType (..), SignedTransmission, Transmission, encodeTransmission, tGet, tPut) +import Simplex.Messaging.Protocol (ErrorType (..), SignedTransmission, Transmission, encodeTransmission, tGet, tPut, SMPServer) import qualified Simplex.Messaging.Protocol as SMP import Simplex.Messaging.Server import qualified Simplex.Messaging.TMap as TM @@ -48,10 +48,12 @@ runNtfServerBlocking started cfg = runReaderT (ntfServer cfg started) =<< newNtf ntfServer :: forall m. (MonadUnliftIO m, MonadReader NtfEnv m) => NtfServerConfig -> TMVar Bool -> m () ntfServer NtfServerConfig {transports} started = do - s <- asks subscriber + -- s <- asks subscriber ps <- asks pushServer - raceAny_ (ntfSubscriber s : ntfPush ps : map runServer transports) + raceAny_ (ntfPush ps : map runServer transports) where + -- raceAny_ (ntfSubscriber s : ntfPush ps : map runServer transports) + runServer :: (ServiceName, ATransport) -> m () runServer (tcpPort, ATransport t) = do serverParams <- asks tlsServerParams @@ -65,18 +67,29 @@ ntfServer NtfServerConfig {transports} started = do Left _ -> pure () ntfSubscriber :: forall m. MonadUnliftIO m => NtfSubscriber -> m () -ntfSubscriber NtfSubscriber {subQ, smpAgent = ca@SMPClientAgent {msgQ, agentQ}} = do +ntfSubscriber NtfSubscriber {newSubQ, smpAgent = ca@SMPClientAgent {msgQ, agentQ}} = do raceAny_ [subscribe, receiveSMP, receiveAgent] where subscribe :: m () subscribe = forever $ do - atomically (readTBQueue subQ) >>= \case + atomically (readTBQueue newSubQ) >>= \case + -- TODO create workers, workers do subscription NtfSub NtfSubData {smpQueue} -> do let SMPQueueNtf {smpServer, notifierId, notifierKey} = smpQueue liftIO (runExceptT $ subscribeQueue ca smpServer ((SPNotifier, notifierId), notifierKey)) >>= \case Right _ -> pure () -- update subscription status Left _e -> pure () + getSMPSubscriber :: (MonadUnliftIO m, MonadReader NtfEnv m) => SMPServer -> m SMPSubscriber + getSMPSubscriber smpServer = do + -- TODO check the map + -- if not there, create env + start process (runNtfSubscriber) + -- return env + atomically $ newSMPSubscriber 0 -- todo get from config + + runSMPSubscriber :: NtfSubscriber -> m () + runSMPSubscriber = forever $ pure () + receiveSMP :: m () receiveSMP = forever $ do (_srv, _sessId, _ntfId, msg) <- atomically $ readTBQueue msgQ @@ -188,12 +201,34 @@ verifyNtfTransmission (sig_, signed, (corrId, entId, _)) cmd = do | verifyCmdSignature sig_ signed tknVerifyKey -> verifiedTknCmd t c | otherwise -> VRFailed _ -> maybe False (dummyVerifyCmd signed) sig_ `seq` VRFailed - _ -> pure VRFailed + NtfCmd SSubscription c@(SNEW sub@(NewNtfSub tknId _)) -> do + -- require that token is active, e.g. return Nothing from store if not + (tkn_, sub_) <- atomically $ findNtfSubscription st sub + case tkn_ of + Just NtfTknData {tknVerifyKey} -> + pure $ + if verifyCmdSignature sig_ signed tknVerifyKey + then case sub_ of + Just s@NtfSubData {tokenId} + | tknId == tokenId -> verifiedSubCmd s c + | otherwise -> VRFailed + _ -> VRVerified (NtfReqNew corrId (ANE SSubscription sub)) + else VRFailed + _ -> pure $ maybe False (dummyVerifyCmd signed) sig_ `seq` VRFailed + NtfCmd SSubscription c -> do + -- require that token is active, e.g. return Nothing from store if not + r_ <- atomically $ getNtfSubscription st entId + pure $ case r_ of + Just (s, NtfTknData {tknVerifyKey}) + | verifyCmdSignature sig_ signed tknVerifyKey -> verifiedSubCmd s c + | otherwise -> VRFailed + _ -> maybe False (dummyVerifyCmd signed) sig_ `seq` VRFailed where verifiedTknCmd t c = VRVerified (NtfReqCmd SToken (NtfTkn t) (corrId, entId, c)) + verifiedSubCmd s c = VRVerified (NtfReqCmd SSubscription (NtfSub s) (corrId, entId, c)) client :: forall m. (MonadUnliftIO m, MonadReader NtfEnv m) => NtfServerClient -> NtfSubscriber -> NtfPushServer -> m () -client NtfServerClient {rcvQ, sndQ} NtfSubscriber {subQ = _} NtfPushServer {pushQ, intervalNotifiers} = +client NtfServerClient {rcvQ, sndQ} NtfSubscriber {newSubQ = _} NtfPushServer {pushQ, intervalNotifiers} = forever $ atomically (readTBQueue rcvQ) >>= processCommand @@ -267,10 +302,24 @@ client NtfServerClient {rcvQ, sndQ} NtfSubscriber {subQ = _} NtfPushServer {push intervalNotifier delay = forever $ do threadDelay delay atomically $ writeTBQueue pushQ (tkn, PNCheckMessages) - NtfReqNew corrId (ANE SSubscription _newSub) -> pure (corrId, "", NROk) + NtfReqNew corrId (ANE SSubscription newSub) -> do + st <- asks store + subId <- getId + atomically $ do + sub <- mkNtfSubData newSub + addNtfSubscription st subId sub + -- TODO launch NSUB + -- writeTBQueue subQ ("", connId, cmd) + -- TODO response ID + pure (corrId, "", NROk) NtfReqCmd SSubscription _sub (corrId, subId, cmd) -> (corrId,subId,) <$> case cmd of - SNEW _newSub -> pure NROk + SNEW _newSub -> do + logDebug "SNEW - registered subscription" + -- st <- asks store + -- TODO check NKEY? + -- TODO response ID + pure NROk SCHK -> pure NROk SDEL -> pure NROk PING -> pure NRPong diff --git a/src/Simplex/Messaging/Notifications/Server/Env.hs b/src/Simplex/Messaging/Notifications/Server/Env.hs index aaf04beb4..26a2a7de9 100644 --- a/src/Simplex/Messaging/Notifications/Server/Env.hs +++ b/src/Simplex/Messaging/Notifications/Server/Env.hs @@ -23,7 +23,7 @@ import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Notifications.Protocol import Simplex.Messaging.Notifications.Server.Push.APNS import Simplex.Messaging.Notifications.Server.Store -import Simplex.Messaging.Protocol (CorrId, Transmission) +import Simplex.Messaging.Protocol (CorrId, Transmission, SMPServer) import Simplex.Messaging.Server.Expiration import Simplex.Messaging.TMap (TMap) import qualified Simplex.Messaging.TMap as TM @@ -78,15 +78,26 @@ newNtfServerEnv config@NtfServerConfig {subQSize, pushQSize, smpAgentCfg, apnsCo pure NtfEnv {config, subscriber, pushServer, store, idsDrg, tlsServerParams, serverIdentity = C.KeyHash fp} data NtfSubscriber = NtfSubscriber - { subQ :: TBQueue (NtfEntityRec 'Subscription), + { smpSubscribers :: TMap SMPServer SMPSubscriber, + newSubQ :: TBQueue (NtfEntityRec 'Subscription), smpAgent :: SMPClientAgent } newNtfSubscriber :: Natural -> SMPClientAgentConfig -> STM NtfSubscriber newNtfSubscriber qSize smpAgentCfg = do + smpSubscribers <- TM.empty + newSubQ <- newTBQueue qSize smpAgent <- newSMPClientAgent smpAgentCfg - subQ <- newTBQueue qSize - pure NtfSubscriber {smpAgent, subQ} + pure NtfSubscriber {smpSubscribers, newSubQ, smpAgent} + +newtype SMPSubscriber = SMPSubscriber + { newSubQ :: TBQueue (NtfEntityRec 'Subscription) + } + +newSMPSubscriber :: Natural -> STM SMPSubscriber +newSMPSubscriber qSize = do + newSubQ <- newTBQueue qSize + pure SMPSubscriber {newSubQ} data NtfPushServer = NtfPushServer { pushQ :: TBQueue (NtfTknData, PushNotification), diff --git a/src/Simplex/Messaging/Notifications/Server/Store.hs b/src/Simplex/Messaging/Notifications/Server/Store.hs index e39e7d3e1..d0b2e9d7d 100644 --- a/src/Simplex/Messaging/Notifications/Server/Store.hs +++ b/src/Simplex/Messaging/Notifications/Server/Store.hs @@ -14,22 +14,30 @@ import Control.Concurrent.STM import Control.Monad import Data.ByteString.Char8 (ByteString) import qualified Data.Map.Strict as M +import Data.Set (Set) import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Notifications.Protocol +import Simplex.Messaging.Protocol (NotifierId, ProtocolServer) import Simplex.Messaging.TMap (TMap) import qualified Simplex.Messaging.TMap as TM import Simplex.Messaging.Util (whenM, ($>>=)) data NtfStore = NtfStore { tokens :: TMap NtfTokenId NtfTknData, - tokenRegistrations :: TMap DeviceToken (TMap ByteString NtfTokenId) + tokenRegistrations :: TMap DeviceToken (TMap ByteString NtfTokenId), + subscriptions :: TMap NtfSubscriptionId NtfSubData, + tokenSubscriptions :: TMap NtfTokenId (TVar (Set NtfSubscriptionId)), + subscriptionLookup :: TMap (NtfTokenId, ProtocolServer, NotifierId) NtfSubscriptionId } newNtfStore :: STM NtfStore newNtfStore = do tokens <- TM.empty tokenRegistrations <- TM.empty - pure NtfStore {tokens, tokenRegistrations} + subscriptions <- TM.empty + tokenSubscriptions <- TM.empty + subscriptionLookup <- TM.empty + pure NtfStore {tokens, tokenRegistrations, subscriptions, tokenSubscriptions, subscriptionLookup} data NtfTknData = NtfTknData { ntfTknId :: NtfTokenId, @@ -118,6 +126,33 @@ deleteNtfToken st tknId = do regs = tokenRegistrations st regKey = C.toPubKey C.pubKeyBytes +getNtfSubscription :: NtfStore -> NtfSubscriptionId -> STM (Maybe (NtfSubData, NtfTknData)) +getNtfSubscription st subId = pure Nothing -- TM.lookup subId (subscriptions st) + +findNtfSubscription :: NtfStore -> NewNtfEntity 'Subscription -> STM (Maybe NtfTknData, Maybe NtfSubData) +findNtfSubscription st (NewNtfSub tknId smpQueue) = pure (Nothing, Nothing) + +-- findNtfSubscription :: NtfStore -> NewNtfEntity 'Subscription -> STM (Maybe NtfSubData) +-- findNtfSubscription st (NewNtfSub tknId smpQueue) = +-- TM.lookup (tknId, smpQueue) (subscriptionLookup st) +-- $>>= (`TM.lookup` subscriptions st) + +mkNtfSubData :: NewNtfEntity 'Subscription -> STM NtfSubData +mkNtfSubData (NewNtfSub tokenId smpQueue) = do + subStatus <- newTVar NSNew + pure NtfSubData {smpQueue, tokenId, subStatus} + +addNtfSubscription :: NtfStore -> NtfSubscriptionId -> NtfSubData -> STM () +addNtfSubscription st subId sub@NtfSubData {smpQueue = SMPQueueNtf {smpServer, notifierId}, tokenId} = do + TM.insert subId sub $ subscriptions st + TM.lookup (tokenId, smpServer, notifierId) sRegs + >>= ( \case + Nothing -> pure () + Just _ -> pure () + ) + where + sRegs = subscriptionLookup st + -- getNtfRec :: NtfStore -> SNtfEntity e -> NtfEntityId -> STM (Maybe (NtfEntityRec e)) -- getNtfRec st ent entId = case ent of -- SToken -> NtfTkn <$$> TM.lookup entId (tokens st) From b5ccce0f6af66c400d43b02da5b414820f367d48 Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Wed, 1 Jun 2022 21:03:32 +0400 Subject: [PATCH 036/131] ntf: store get methods (#380) --- src/Simplex/Messaging/Notifications/Server.hs | 28 ++++++++----------- .../Messaging/Notifications/Server/Store.hs | 26 +++++++++++------ 2 files changed, 30 insertions(+), 24 deletions(-) diff --git a/src/Simplex/Messaging/Notifications/Server.hs b/src/Simplex/Messaging/Notifications/Server.hs index a8e225fad..b4cbf60c3 100644 --- a/src/Simplex/Messaging/Notifications/Server.hs +++ b/src/Simplex/Messaging/Notifications/Server.hs @@ -26,7 +26,7 @@ import Simplex.Messaging.Notifications.Server.Env import Simplex.Messaging.Notifications.Server.Push.APNS import Simplex.Messaging.Notifications.Server.Store import Simplex.Messaging.Notifications.Transport -import Simplex.Messaging.Protocol (ErrorType (..), SignedTransmission, Transmission, encodeTransmission, tGet, tPut, SMPServer) +import Simplex.Messaging.Protocol (ErrorType (..), SMPServer, SignedTransmission, Transmission, encodeTransmission, tGet, tPut) import qualified Simplex.Messaging.Protocol as SMP import Simplex.Messaging.Server import qualified Simplex.Messaging.TMap as TM @@ -86,7 +86,6 @@ ntfSubscriber NtfSubscriber {newSubQ, smpAgent = ca@SMPClientAgent {msgQ, agentQ -- if not there, create env + start process (runNtfSubscriber) -- return env atomically $ newSMPSubscriber 0 -- todo get from config - runSMPSubscriber :: NtfSubscriber -> m () runSMPSubscriber = forever $ pure () @@ -202,21 +201,18 @@ verifyNtfTransmission (sig_, signed, (corrId, entId, _)) cmd = do | otherwise -> VRFailed _ -> maybe False (dummyVerifyCmd signed) sig_ `seq` VRFailed NtfCmd SSubscription c@(SNEW sub@(NewNtfSub tknId _)) -> do - -- require that token is active, e.g. return Nothing from store if not - (tkn_, sub_) <- atomically $ findNtfSubscription st sub - case tkn_ of - Just NtfTknData {tknVerifyKey} -> - pure $ - if verifyCmdSignature sig_ signed tknVerifyKey - then case sub_ of - Just s@NtfSubData {tokenId} - | tknId == tokenId -> verifiedSubCmd s c - | otherwise -> VRFailed - _ -> VRVerified (NtfReqNew corrId (ANE SSubscription sub)) - else VRFailed - _ -> pure $ maybe False (dummyVerifyCmd signed) sig_ `seq` VRFailed + r_ <- atomically $ findNtfSubscription st sub + pure $ case r_ of + Just (NtfTknData {tknVerifyKey}, sub_) -> + if verifyCmdSignature sig_ signed tknVerifyKey + then case sub_ of + Just s@NtfSubData {tokenId} + | tknId == tokenId -> verifiedSubCmd s c + | otherwise -> VRFailed + _ -> VRVerified (NtfReqNew corrId (ANE SSubscription sub)) + else VRFailed + _ -> maybe False (dummyVerifyCmd signed) sig_ `seq` VRFailed NtfCmd SSubscription c -> do - -- require that token is active, e.g. return Nothing from store if not r_ <- atomically $ getNtfSubscription st entId pure $ case r_ of Just (s, NtfTknData {tknVerifyKey}) diff --git a/src/Simplex/Messaging/Notifications/Server/Store.hs b/src/Simplex/Messaging/Notifications/Server/Store.hs index d0b2e9d7d..9defbbd0c 100644 --- a/src/Simplex/Messaging/Notifications/Server/Store.hs +++ b/src/Simplex/Messaging/Notifications/Server/Store.hs @@ -20,7 +20,7 @@ import Simplex.Messaging.Notifications.Protocol import Simplex.Messaging.Protocol (NotifierId, ProtocolServer) import Simplex.Messaging.TMap (TMap) import qualified Simplex.Messaging.TMap as TM -import Simplex.Messaging.Util (whenM, ($>>=)) +import Simplex.Messaging.Util (whenM, ($>>=), (<$$>)) data NtfStore = NtfStore { tokens :: TMap NtfTokenId NtfTknData, @@ -127,15 +127,25 @@ deleteNtfToken st tknId = do regKey = C.toPubKey C.pubKeyBytes getNtfSubscription :: NtfStore -> NtfSubscriptionId -> STM (Maybe (NtfSubData, NtfTknData)) -getNtfSubscription st subId = pure Nothing -- TM.lookup subId (subscriptions st) +getNtfSubscription st subId = + TM.lookup subId (subscriptions st) + $>>= \sub@NtfSubData {tokenId} -> + (sub,) <$$> getActiveNtfToken st tokenId -findNtfSubscription :: NtfStore -> NewNtfEntity 'Subscription -> STM (Maybe NtfTknData, Maybe NtfSubData) -findNtfSubscription st (NewNtfSub tknId smpQueue) = pure (Nothing, Nothing) +findNtfSubscription :: NtfStore -> NewNtfEntity 'Subscription -> STM (Maybe (NtfTknData, Maybe NtfSubData)) +findNtfSubscription st (NewNtfSub tknId SMPQueueNtf {smpServer, notifierId}) = do + getActiveNtfToken st tknId >>= mapM (\tkn -> (tkn,) <$> getSub) + where + getSub :: STM (Maybe NtfSubData) + getSub = + TM.lookup (tknId, smpServer, notifierId) (subscriptionLookup st) + $>>= (`TM.lookup` subscriptions st) --- findNtfSubscription :: NtfStore -> NewNtfEntity 'Subscription -> STM (Maybe NtfSubData) --- findNtfSubscription st (NewNtfSub tknId smpQueue) = --- TM.lookup (tknId, smpQueue) (subscriptionLookup st) --- $>>= (`TM.lookup` subscriptions st) +getActiveNtfToken :: NtfStore -> NtfTokenId -> STM (Maybe NtfTknData) +getActiveNtfToken st tknId = + getNtfToken st tknId $>>= \tkn@NtfTknData {tknStatus} -> do + tStatus <- readTVar tknStatus + pure $ if tStatus == NTActive then Just tkn else Nothing mkNtfSubData :: NewNtfEntity 'Subscription -> STM NtfSubData mkNtfSubData (NewNtfSub tokenId smpQueue) = do From 6a27b827590a614bce1a620a6b3fbfea931c7361 Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Thu, 2 Jun 2022 14:52:14 +0400 Subject: [PATCH 037/131] ntf: SMP subscribers logic (#381) --- src/Simplex/Messaging/Notifications/Server.hs | 75 ++++++++++++------- .../Messaging/Notifications/Server/Store.hs | 18 ++--- 2 files changed, 54 insertions(+), 39 deletions(-) diff --git a/src/Simplex/Messaging/Notifications/Server.hs b/src/Simplex/Messaging/Notifications/Server.hs index b4cbf60c3..03b291f17 100644 --- a/src/Simplex/Messaging/Notifications/Server.hs +++ b/src/Simplex/Messaging/Notifications/Server.hs @@ -16,6 +16,7 @@ import Control.Monad.IO.Unlift (MonadUnliftIO) import Control.Monad.Reader import Crypto.Random (MonadRandom) import Data.ByteString.Char8 (ByteString) +import Data.Functor (($>)) import qualified Data.Text as T import Data.Time.Clock.System (getSystemTime) import Network.Socket (ServiceName) @@ -34,7 +35,7 @@ import Simplex.Messaging.Transport (ATransport (..), THandle (..), TProxy, Trans import Simplex.Messaging.Transport.Server (runTransportServer) import Simplex.Messaging.Util import UnliftIO (async, uninterruptibleCancel) -import UnliftIO.Concurrent (threadDelay) +import UnliftIO.Concurrent (forkFinally, threadDelay) import UnliftIO.Exception import UnliftIO.STM @@ -48,12 +49,10 @@ runNtfServerBlocking started cfg = runReaderT (ntfServer cfg started) =<< newNtf ntfServer :: forall m. (MonadUnliftIO m, MonadReader NtfEnv m) => NtfServerConfig -> TMVar Bool -> m () ntfServer NtfServerConfig {transports} started = do - -- s <- asks subscriber + s <- asks subscriber ps <- asks pushServer - raceAny_ (ntfPush ps : map runServer transports) + raceAny_ (ntfSubscriber s : ntfPush ps : map runServer transports) where - -- raceAny_ (ntfSubscriber s : ntfPush ps : map runServer transports) - runServer :: (ServiceName, ATransport) -> m () runServer (tcpPort, ATransport t) = do serverParams <- asks tlsServerParams @@ -66,28 +65,43 @@ ntfServer NtfServerConfig {transports} started = do Right th -> runNtfClientTransport th Left _ -> pure () -ntfSubscriber :: forall m. MonadUnliftIO m => NtfSubscriber -> m () -ntfSubscriber NtfSubscriber {newSubQ, smpAgent = ca@SMPClientAgent {msgQ, agentQ}} = do +ntfSubscriber :: forall m. (MonadUnliftIO m, MonadReader NtfEnv m) => NtfSubscriber -> m () +ntfSubscriber NtfSubscriber {smpSubscribers, newSubQ, smpAgent = ca@SMPClientAgent {msgQ, agentQ}} = do raceAny_ [subscribe, receiveSMP, receiveAgent] where subscribe :: m () - subscribe = forever $ do - atomically (readTBQueue newSubQ) >>= \case - -- TODO create workers, workers do subscription - NtfSub NtfSubData {smpQueue} -> do - let SMPQueueNtf {smpServer, notifierId, notifierKey} = smpQueue - liftIO (runExceptT $ subscribeQueue ca smpServer ((SPNotifier, notifierId), notifierKey)) >>= \case - Right _ -> pure () -- update subscription status - Left _e -> pure () + subscribe = + forever $ + atomically (readTBQueue newSubQ) >>= \case + sub@(NtfSub NtfSubData {smpQueue = SMPQueueNtf {smpServer}}) -> do + SMPSubscriber {newSubQ = subscriberSubQ} <- getSMPSubscriber smpServer + atomically $ writeTBQueue subscriberSubQ sub - getSMPSubscriber :: (MonadUnliftIO m, MonadReader NtfEnv m) => SMPServer -> m SMPSubscriber - getSMPSubscriber smpServer = do - -- TODO check the map - -- if not there, create env + start process (runNtfSubscriber) - -- return env - atomically $ newSMPSubscriber 0 -- todo get from config - runSMPSubscriber :: NtfSubscriber -> m () - runSMPSubscriber = forever $ pure () + getSMPSubscriber :: SMPServer -> m SMPSubscriber + getSMPSubscriber smpServer = + atomically (TM.lookup smpServer smpSubscribers) >>= maybe createSMPSubscriber pure + where + createSMPSubscriber = do + qSize <- asks $ subQSize . config + newSubscriber <- atomically $ newSMPSubscriber qSize + atomically $ TM.insert smpServer newSubscriber smpSubscribers + _ <- runSMPSubscriber newSubscriber `forkFinally` \_ -> atomically (TM.delete smpServer smpSubscribers >> failSubscriptions newSubscriber) + pure newSubscriber + -- TODO mark subscriptions as failed + failSubscriptions _ = pure () + + runSMPSubscriber :: SMPSubscriber -> m () + runSMPSubscriber SMPSubscriber {newSubQ = subscriberSubQ} = + forever $ + atomically (peekTBQueue subscriberSubQ) >>= \case + NtfSub NtfSubData {smpQueue} -> do + let SMPQueueNtf {smpServer, notifierId, notifierKey} = smpQueue + liftIO (runExceptT $ subscribeQueue ca smpServer ((SPNotifier, notifierId), notifierKey)) >>= \case + Right _ -> do + -- update subscription status + _ <- atomically $ readTBQueue subscriberSubQ + pure () + Left _e -> pure () receiveSMP :: m () receiveSMP = forever $ do @@ -194,6 +208,7 @@ verifyNtfTransmission (sig_, signed, (corrId, entId, _)) cmd = do _ -> VRVerified (NtfReqNew corrId (ANE SToken tkn)) else VRFailed NtfCmd SToken c -> do + -- TODO move active token check here to differentiate error t_ <- atomically $ getNtfToken st entId pure $ case t_ of Just t@NtfTknData {tknVerifyKey} @@ -201,6 +216,7 @@ verifyNtfTransmission (sig_, signed, (corrId, entId, _)) cmd = do | otherwise -> VRFailed _ -> maybe False (dummyVerifyCmd signed) sig_ `seq` VRFailed NtfCmd SSubscription c@(SNEW sub@(NewNtfSub tknId _)) -> do + -- TODO move active token check here to differentiate error r_ <- atomically $ findNtfSubscription st sub pure $ case r_ of Just (NtfTknData {tknVerifyKey}, sub_) -> @@ -224,7 +240,7 @@ verifyNtfTransmission (sig_, signed, (corrId, entId, _)) cmd = do verifiedSubCmd s c = VRVerified (NtfReqCmd SSubscription (NtfSub s) (corrId, entId, c)) client :: forall m. (MonadUnliftIO m, MonadReader NtfEnv m) => NtfServerClient -> NtfSubscriber -> NtfPushServer -> m () -client NtfServerClient {rcvQ, sndQ} NtfSubscriber {newSubQ = _} NtfPushServer {pushQ, intervalNotifiers} = +client NtfServerClient {rcvQ, sndQ} NtfSubscriber {newSubQ} NtfPushServer {pushQ, intervalNotifiers} = forever $ atomically (readTBQueue rcvQ) >>= processCommand @@ -303,11 +319,12 @@ client NtfServerClient {rcvQ, sndQ} NtfSubscriber {newSubQ = _} NtfPushServer {p subId <- getId atomically $ do sub <- mkNtfSubData newSub - addNtfSubscription st subId sub - -- TODO launch NSUB - -- writeTBQueue subQ ("", connId, cmd) - -- TODO response ID - pure (corrId, "", NROk) + (corrId,"",) + <$> ( addNtfSubscription st subId sub >>= \case + -- TODO response ID + Just _ -> writeTBQueue newSubQ (NtfSub sub) $> NROk + _ -> pure $ NRErr AUTH + ) NtfReqCmd SSubscription _sub (corrId, subId, cmd) -> (corrId,subId,) <$> case cmd of SNEW _newSub -> do diff --git a/src/Simplex/Messaging/Notifications/Server/Store.hs b/src/Simplex/Messaging/Notifications/Server/Store.hs index 9defbbd0c..c15428f63 100644 --- a/src/Simplex/Messaging/Notifications/Server/Store.hs +++ b/src/Simplex/Messaging/Notifications/Server/Store.hs @@ -14,7 +14,7 @@ import Control.Concurrent.STM import Control.Monad import Data.ByteString.Char8 (ByteString) import qualified Data.Map.Strict as M -import Data.Set (Set) +import Data.Set (Set, insert) import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Notifications.Protocol import Simplex.Messaging.Protocol (NotifierId, ProtocolServer) @@ -152,16 +152,14 @@ mkNtfSubData (NewNtfSub tokenId smpQueue) = do subStatus <- newTVar NSNew pure NtfSubData {smpQueue, tokenId, subStatus} -addNtfSubscription :: NtfStore -> NtfSubscriptionId -> NtfSubData -> STM () -addNtfSubscription st subId sub@NtfSubData {smpQueue = SMPQueueNtf {smpServer, notifierId}, tokenId} = do - TM.insert subId sub $ subscriptions st - TM.lookup (tokenId, smpServer, notifierId) sRegs - >>= ( \case - Nothing -> pure () - Just _ -> pure () - ) +addNtfSubscription :: NtfStore -> NtfSubscriptionId -> NtfSubData -> STM (Maybe ()) +addNtfSubscription st subId sub@NtfSubData {smpQueue = SMPQueueNtf {smpServer, notifierId}, tokenId} = + TM.lookup tokenId (tokenSubscriptions st) >>= mapM insertSub where - sRegs = subscriptionLookup st + insertSub ts = do + modifyTVar' ts $ insert subId + TM.insert subId sub $ subscriptions st + TM.insert (tokenId, smpServer, notifierId) subId (subscriptionLookup st) -- getNtfRec :: NtfStore -> SNtfEntity e -> NtfEntityId -> STM (Maybe (NtfEntityRec e)) -- getNtfRec st ent entId = case ent of From facb6f4561bc60e6e141f72877e0967e00a53cdb Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Thu, 2 Jun 2022 15:36:16 +0400 Subject: [PATCH 038/131] ntf: separate notifierKey from SMPQueueNtf (#382) --- src/Simplex/Messaging/Notifications/Protocol.hs | 17 ++++++++--------- src/Simplex/Messaging/Notifications/Server.hs | 6 +++--- .../Messaging/Notifications/Server/Store.hs | 17 +++++++++-------- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/Simplex/Messaging/Notifications/Protocol.hs b/src/Simplex/Messaging/Notifications/Protocol.hs index 3640004e0..8830ff457 100644 --- a/src/Simplex/Messaging/Notifications/Protocol.hs +++ b/src/Simplex/Messaging/Notifications/Protocol.hs @@ -120,7 +120,7 @@ instance ToJSON NtfRegCode where data NewNtfEntity (e :: NtfEntity) where NewNtfTkn :: DeviceToken -> C.APublicVerifyKey -> C.PublicKeyX25519 -> NewNtfEntity 'Token - NewNtfSub :: NtfTokenId -> SMPQueueNtf -> NewNtfEntity 'Subscription + NewNtfSub :: NtfTokenId -> SMPQueueNtf -> NtfPrivateSignKey -> NewNtfEntity 'Subscription deriving instance Show (NewNtfEntity e) @@ -129,7 +129,7 @@ data ANewNtfEntity = forall e. NtfEntityI e => ANE (SNtfEntity e) (NewNtfEntity instance NtfEntityI e => Encoding (NewNtfEntity e) where smpEncode = \case NewNtfTkn tkn verifyKey dhPubKey -> smpEncode ('T', tkn, verifyKey, dhPubKey) - NewNtfSub tknId smpQueue -> smpEncode ('S', tknId, smpQueue) + NewNtfSub tknId smpQueue notifierKey -> smpEncode ('S', tknId, smpQueue, notifierKey) smpP = (\(ANE _ c) -> checkEntity c) <$?> smpP instance Encoding ANewNtfEntity where @@ -137,7 +137,7 @@ instance Encoding ANewNtfEntity where smpP = A.anyChar >>= \case 'T' -> ANE SToken <$> (NewNtfTkn <$> smpP <*> smpP <*> smpP) - 'S' -> ANE SSubscription <$> (NewNtfSub <$> smpP <*> smpP) + 'S' -> ANE SSubscription <$> (NewNtfSub <$> smpP <*> smpP <*> smpP) _ -> fail "bad ANewNtfEntity" instance Protocol NtfResponse where @@ -307,16 +307,15 @@ instance ProtocolEncoding NtfResponse where data SMPQueueNtf = SMPQueueNtf { smpServer :: ProtocolServer, - notifierId :: NotifierId, - notifierKey :: NtfPrivateSignKey + notifierId :: NotifierId } - deriving (Show) + deriving (Eq, Ord, Show) instance Encoding SMPQueueNtf where - smpEncode SMPQueueNtf {smpServer, notifierId, notifierKey} = smpEncode (smpServer, notifierId, notifierKey) + smpEncode SMPQueueNtf {smpServer, notifierId} = smpEncode (smpServer, notifierId) smpP = do - (smpServer, notifierId, notifierKey) <- smpP - pure $ SMPQueueNtf smpServer notifierId notifierKey + (smpServer, notifierId) <- smpP + pure $ SMPQueueNtf smpServer notifierId data PushProvider = PPApns deriving (Eq, Ord, Show) diff --git a/src/Simplex/Messaging/Notifications/Server.hs b/src/Simplex/Messaging/Notifications/Server.hs index 03b291f17..18448c19e 100644 --- a/src/Simplex/Messaging/Notifications/Server.hs +++ b/src/Simplex/Messaging/Notifications/Server.hs @@ -94,8 +94,8 @@ ntfSubscriber NtfSubscriber {smpSubscribers, newSubQ, smpAgent = ca@SMPClientAge runSMPSubscriber SMPSubscriber {newSubQ = subscriberSubQ} = forever $ atomically (peekTBQueue subscriberSubQ) >>= \case - NtfSub NtfSubData {smpQueue} -> do - let SMPQueueNtf {smpServer, notifierId, notifierKey} = smpQueue + NtfSub NtfSubData {smpQueue, notifierKey} -> do + let SMPQueueNtf {smpServer, notifierId} = smpQueue liftIO (runExceptT $ subscribeQueue ca smpServer ((SPNotifier, notifierId), notifierKey)) >>= \case Right _ -> do -- update subscription status @@ -215,7 +215,7 @@ verifyNtfTransmission (sig_, signed, (corrId, entId, _)) cmd = do | verifyCmdSignature sig_ signed tknVerifyKey -> verifiedTknCmd t c | otherwise -> VRFailed _ -> maybe False (dummyVerifyCmd signed) sig_ `seq` VRFailed - NtfCmd SSubscription c@(SNEW sub@(NewNtfSub tknId _)) -> do + NtfCmd SSubscription c@(SNEW sub@(NewNtfSub tknId _ _)) -> do -- TODO move active token check here to differentiate error r_ <- atomically $ findNtfSubscription st sub pure $ case r_ of diff --git a/src/Simplex/Messaging/Notifications/Server/Store.hs b/src/Simplex/Messaging/Notifications/Server/Store.hs index c15428f63..37a0b4f37 100644 --- a/src/Simplex/Messaging/Notifications/Server/Store.hs +++ b/src/Simplex/Messaging/Notifications/Server/Store.hs @@ -17,7 +17,7 @@ import qualified Data.Map.Strict as M import Data.Set (Set, insert) import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Notifications.Protocol -import Simplex.Messaging.Protocol (NotifierId, ProtocolServer) +import Simplex.Messaging.Protocol (NtfPrivateSignKey) import Simplex.Messaging.TMap (TMap) import qualified Simplex.Messaging.TMap as TM import Simplex.Messaging.Util (whenM, ($>>=), (<$$>)) @@ -27,7 +27,7 @@ data NtfStore = NtfStore tokenRegistrations :: TMap DeviceToken (TMap ByteString NtfTokenId), subscriptions :: TMap NtfSubscriptionId NtfSubData, tokenSubscriptions :: TMap NtfTokenId (TVar (Set NtfSubscriptionId)), - subscriptionLookup :: TMap (NtfTokenId, ProtocolServer, NotifierId) NtfSubscriptionId + subscriptionLookup :: TMap (NtfTokenId, SMPQueueNtf) NtfSubscriptionId } newNtfStore :: STM NtfStore @@ -66,6 +66,7 @@ mkNtfTknData ntfTknId (NewNtfTkn token tknVerifyKey _) tknDhKeys tknDhSecret tkn data NtfSubData = NtfSubData { smpQueue :: SMPQueueNtf, + notifierKey :: NtfPrivateSignKey, tokenId :: NtfTokenId, subStatus :: TVar NtfSubStatus } @@ -133,12 +134,12 @@ getNtfSubscription st subId = (sub,) <$$> getActiveNtfToken st tokenId findNtfSubscription :: NtfStore -> NewNtfEntity 'Subscription -> STM (Maybe (NtfTknData, Maybe NtfSubData)) -findNtfSubscription st (NewNtfSub tknId SMPQueueNtf {smpServer, notifierId}) = do +findNtfSubscription st (NewNtfSub tknId smpQueue _) = do getActiveNtfToken st tknId >>= mapM (\tkn -> (tkn,) <$> getSub) where getSub :: STM (Maybe NtfSubData) getSub = - TM.lookup (tknId, smpServer, notifierId) (subscriptionLookup st) + TM.lookup (tknId, smpQueue) (subscriptionLookup st) $>>= (`TM.lookup` subscriptions st) getActiveNtfToken :: NtfStore -> NtfTokenId -> STM (Maybe NtfTknData) @@ -148,18 +149,18 @@ getActiveNtfToken st tknId = pure $ if tStatus == NTActive then Just tkn else Nothing mkNtfSubData :: NewNtfEntity 'Subscription -> STM NtfSubData -mkNtfSubData (NewNtfSub tokenId smpQueue) = do +mkNtfSubData (NewNtfSub tokenId smpQueue notifierKey) = do subStatus <- newTVar NSNew - pure NtfSubData {smpQueue, tokenId, subStatus} + pure NtfSubData {smpQueue, tokenId, subStatus, notifierKey} addNtfSubscription :: NtfStore -> NtfSubscriptionId -> NtfSubData -> STM (Maybe ()) -addNtfSubscription st subId sub@NtfSubData {smpQueue = SMPQueueNtf {smpServer, notifierId}, tokenId} = +addNtfSubscription st subId sub@NtfSubData {smpQueue, tokenId} = TM.lookup tokenId (tokenSubscriptions st) >>= mapM insertSub where insertSub ts = do modifyTVar' ts $ insert subId TM.insert subId sub $ subscriptions st - TM.insert (tokenId, smpServer, notifierId) subId (subscriptionLookup st) + TM.insert (tokenId, smpQueue) subId (subscriptionLookup st) -- getNtfRec :: NtfStore -> SNtfEntity e -> NtfEntityId -> STM (Maybe (NtfEntityRec e)) -- getNtfRec st ent entId = case ent of From 46ba0470778199ae57bde3a05a613c7503e39248 Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Thu, 2 Jun 2022 19:36:56 +0400 Subject: [PATCH 039/131] ntf: IDSUB response, check notifierKey and return subId on repeat SNEW (#383) --- src/Simplex/Messaging/Notifications/Client.hs | 8 +++--- .../Messaging/Notifications/Protocol.hs | 24 ++++++++++++------ src/Simplex/Messaging/Notifications/Server.hs | 25 ++++++++++--------- 3 files changed, 33 insertions(+), 24 deletions(-) diff --git a/src/Simplex/Messaging/Notifications/Client.hs b/src/Simplex/Messaging/Notifications/Client.hs index 3a5e8c827..13971e13e 100644 --- a/src/Simplex/Messaging/Notifications/Client.hs +++ b/src/Simplex/Messaging/Notifications/Client.hs @@ -27,7 +27,7 @@ type NtfClient = ProtocolClient NtfResponse ntfRegisterToken :: NtfClient -> C.APrivateSignKey -> NewNtfEntity 'Token -> ExceptT ProtocolClientError IO (NtfTokenId, C.PublicKeyX25519) ntfRegisterToken c pKey newTkn = sendNtfCommand c (Just pKey) "" (TNEW newTkn) >>= \case - NRId tknId dhKey -> pure (tknId, dhKey) + NRTknId tknId dhKey -> pure (tknId, dhKey) _ -> throwE PCEUnexpectedResponse ntfVerifyToken :: NtfClient -> C.APrivateSignKey -> NtfTokenId -> NtfRegCode -> ExceptT ProtocolClientError IO () @@ -45,10 +45,10 @@ ntfDeleteToken = okNtfCommand TDEL ntfEnableCron :: NtfClient -> C.APrivateSignKey -> NtfTokenId -> Word16 -> ExceptT ProtocolClientError IO () ntfEnableCron c pKey tknId int = okNtfCommand (TCRN int) c pKey tknId -ntfCreateSubsciption :: NtfClient -> C.APrivateSignKey -> NewNtfEntity 'Subscription -> ExceptT ProtocolClientError IO (NtfSubscriptionId, C.PublicKeyX25519) -ntfCreateSubsciption c pKey newSub = +ntfCreateSubscription :: NtfClient -> C.APrivateSignKey -> NewNtfEntity 'Subscription -> ExceptT ProtocolClientError IO NtfSubscriptionId +ntfCreateSubscription c pKey newSub = sendNtfCommand c (Just pKey) "" (SNEW newSub) >>= \case - NRId subId dhKey -> pure (subId, dhKey) + NRSubId subId -> pure subId _ -> throwE PCEUnexpectedResponse ntfCheckSubscription :: NtfClient -> C.APrivateSignKey -> NtfSubscriptionId -> ExceptT ProtocolClientError IO NtfSubStatus diff --git a/src/Simplex/Messaging/Notifications/Protocol.hs b/src/Simplex/Messaging/Notifications/Protocol.hs index 8830ff457..4ff643136 100644 --- a/src/Simplex/Messaging/Notifications/Protocol.hs +++ b/src/Simplex/Messaging/Notifications/Protocol.hs @@ -231,7 +231,8 @@ instance ProtocolEncoding NtfCmd where checkCredentials t (NtfCmd e c) = NtfCmd e <$> checkCredentials t c data NtfResponseTag - = NRId_ + = NRTknId_ + | NRSubId_ | NROk_ | NRErr_ | NRTkn_ @@ -241,7 +242,8 @@ data NtfResponseTag instance Encoding NtfResponseTag where smpEncode = \case - NRId_ -> "ID" + NRTknId_ -> "IDTKN" + NRSubId_ -> "IDSUB" NROk_ -> "OK" NRErr_ -> "ERR" NRTkn_ -> "TKN" @@ -251,7 +253,8 @@ instance Encoding NtfResponseTag where instance ProtocolMsgTag NtfResponseTag where decodeTag = \case - "ID" -> Just NRId_ + "IDTKN" -> Just NRTknId_ + "IDSUB" -> Just NRSubId_ "OK" -> Just NROk_ "ERR" -> Just NRErr_ "TKN" -> Just NRTkn_ @@ -260,7 +263,8 @@ instance ProtocolMsgTag NtfResponseTag where _ -> Nothing data NtfResponse - = NRId NtfEntityId C.PublicKeyX25519 + = NRTknId NtfEntityId C.PublicKeyX25519 + | NRSubId NtfEntityId | NROk | NRErr ErrorType | NRTkn NtfTknStatus @@ -271,7 +275,8 @@ data NtfResponse instance ProtocolEncoding NtfResponse where type Tag NtfResponse = NtfResponseTag encodeProtocol = \case - NRId entId dhKey -> e (NRId_, ' ', entId, dhKey) + NRTknId entId dhKey -> e (NRTknId_, ' ', entId, dhKey) + NRSubId entId -> e (NRSubId_, ' ', entId) NROk -> e NROk_ NRErr err -> e (NRErr_, ' ', err) NRTkn stat -> e (NRTkn_, ' ', stat) @@ -282,7 +287,8 @@ instance ProtocolEncoding NtfResponse where e = smpEncode protocolP = \case - NRId_ -> NRId <$> _smpP <*> smpP + NRTknId_ -> NRTknId <$> _smpP <*> smpP + NRSubId_ -> NRSubId <$> _smpP NROk_ -> pure NROk NRErr_ -> NRErr <$> _smpP NRTkn_ -> NRTkn <$> _smpP @@ -290,8 +296,10 @@ instance ProtocolEncoding NtfResponse where NRPong_ -> pure NRPong checkCredentials (_, _, entId, _) cmd = case cmd of - -- ID response must not have queue ID - NRId {} -> noEntity + -- IDTKN response must not have queue ID + NRTknId {} -> noEntity + -- IDSUB response must not have queue ID + NRSubId {} -> noEntity -- ERR response does not always have entity ID NRErr _ -> Right cmd -- PONG response must not have queue ID diff --git a/src/Simplex/Messaging/Notifications/Server.hs b/src/Simplex/Messaging/Notifications/Server.hs index 18448c19e..715ea9a99 100644 --- a/src/Simplex/Messaging/Notifications/Server.hs +++ b/src/Simplex/Messaging/Notifications/Server.hs @@ -208,7 +208,6 @@ verifyNtfTransmission (sig_, signed, (corrId, entId, _)) cmd = do _ -> VRVerified (NtfReqNew corrId (ANE SToken tkn)) else VRFailed NtfCmd SToken c -> do - -- TODO move active token check here to differentiate error t_ <- atomically $ getNtfToken st entId pure $ case t_ of Just t@NtfTknData {tknVerifyKey} @@ -229,6 +228,7 @@ verifyNtfTransmission (sig_, signed, (corrId, entId, _)) cmd = do else VRFailed _ -> maybe False (dummyVerifyCmd signed) sig_ `seq` VRFailed NtfCmd SSubscription c -> do + -- TODO move active token check here to differentiate error r_ <- atomically $ getNtfSubscription st entId pure $ case r_ of Just (s, NtfTknData {tknVerifyKey}) @@ -259,7 +259,7 @@ client NtfServerClient {rcvQ, sndQ} NtfSubscriber {newSubQ} NtfPushServer {pushQ tkn <- mkNtfTknData tknId newTkn ks dhSecret regCode addNtfToken st tknId tkn writeTBQueue pushQ (tkn, PNVerification regCode) - pure (corrId, "", NRId tknId srvDhPubKey) + pure (corrId, "", NRTknId tknId srvDhPubKey) NtfReqCmd SToken (NtfTkn tkn@NtfTknData {ntfTknId, tknStatus, tknRegCode, tknDhSecret, tknDhKeys = (srvDhPubKey, srvDhPrivKey)}) (corrId, tknId, cmd) -> do status <- readTVarIO tknStatus (corrId,tknId,) <$> case cmd of @@ -270,7 +270,7 @@ client NtfServerClient {rcvQ, sndQ} NtfSubscriber {newSubQ} NtfPushServer {pushQ if tknDhSecret == dhSecret then do atomically $ writeTBQueue pushQ (tkn, PNVerification tknRegCode) - pure $ NRId ntfTknId srvDhPubKey + pure $ NRTknId ntfTknId srvDhPubKey else pure $ NRErr AUTH TVFY code -- this allows repeated verification for cases when client connection dropped before server response | (status == NTRegistered || status == NTConfirmed || status == NTActive) && tknRegCode == code -> do @@ -315,24 +315,25 @@ client NtfServerClient {rcvQ, sndQ} NtfSubscriber {newSubQ} NtfPushServer {pushQ threadDelay delay atomically $ writeTBQueue pushQ (tkn, PNCheckMessages) NtfReqNew corrId (ANE SSubscription newSub) -> do + logDebug "SNEW - new subscription" st <- asks store subId <- getId atomically $ do sub <- mkNtfSubData newSub (corrId,"",) <$> ( addNtfSubscription st subId sub >>= \case - -- TODO response ID - Just _ -> writeTBQueue newSubQ (NtfSub sub) $> NROk + Just _ -> writeTBQueue newSubQ (NtfSub sub) $> NRSubId subId _ -> pure $ NRErr AUTH ) - NtfReqCmd SSubscription _sub (corrId, subId, cmd) -> + NtfReqCmd SSubscription (NtfSub NtfSubData {notifierKey = registeredNKey}) (corrId, subId, cmd) -> (corrId,subId,) <$> case cmd of - SNEW _newSub -> do - logDebug "SNEW - registered subscription" - -- st <- asks store - -- TODO check NKEY? - -- TODO response ID - pure NROk + SNEW (NewNtfSub _ _ notifierKey) -> do + logDebug "SNEW - existing subscription" + -- TODO retry if subscription failed, if pending or AUTH do nothing + pure $ + if notifierKey == registeredNKey + then NRSubId subId + else NRErr AUTH SCHK -> pure NROk SDEL -> pure NROk PING -> pure NRPong From 628930df1fa1c3fff6fd1413e7b437148c4a83b5 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Sat, 4 Jun 2022 13:08:05 +0100 Subject: [PATCH 040/131] support stopping and resuming agent (#385) * export agentDbPath * support fully closing and resuming agent * whitespace * clean up --- src/Simplex/Messaging/Agent.hs | 6 ++- src/Simplex/Messaging/Agent/Client.hs | 41 +++++++++++++------ .../Notifications/Server/Push/testpush.sh | 3 +- 3 files changed, 35 insertions(+), 15 deletions(-) diff --git a/src/Simplex/Messaging/Agent.hs b/src/Simplex/Messaging/Agent.hs index ee67611a7..b8384fa27 100644 --- a/src/Simplex/Messaging/Agent.hs +++ b/src/Simplex/Messaging/Agent.hs @@ -35,7 +35,8 @@ module Simplex.Messaging.Agent AgentMonad, AgentErrorMonad, getSMPAgentClient, - disconnectAgentClient, -- used in tests + disconnectAgentClient, + resumeAgentClient, withAgentLock, createConnection, joinConnection, @@ -113,6 +114,9 @@ getSMPAgentClient cfg initServers = newSMPAgentEnv cfg >>= runReaderT runAgent disconnectAgentClient :: MonadUnliftIO m => AgentClient -> m () disconnectAgentClient c = closeAgentClient c >> logConnection c False +resumeAgentClient :: MonadIO m => AgentClient -> m () +resumeAgentClient c = atomically $ writeTVar (active c) True + -- | type AgentErrorMonad m = (MonadUnliftIO m, MonadError AgentErrorType m) diff --git a/src/Simplex/Messaging/Agent/Client.hs b/src/Simplex/Messaging/Agent/Client.hs index 2d842e3c9..e6e3c0c47 100644 --- a/src/Simplex/Messaging/Agent/Client.hs +++ b/src/Simplex/Messaging/Agent/Client.hs @@ -39,6 +39,7 @@ module Simplex.Messaging.Agent.Client logServer, removeSubscription, hasActiveSubscription, + agentDbPath, ) where @@ -63,6 +64,7 @@ import Simplex.Messaging.Agent.Env.SQLite import Simplex.Messaging.Agent.Protocol import Simplex.Messaging.Agent.RetryInterval import Simplex.Messaging.Agent.Store +import Simplex.Messaging.Agent.Store.SQLite (SQLiteStore (..)) import Simplex.Messaging.Client import Simplex.Messaging.Client.Agent () import qualified Simplex.Messaging.Crypto as C @@ -132,6 +134,9 @@ newAgentClient InitialAgentServers {smp, ntf} agentEnv = do lock <- newTMVar () return AgentClient {active, rcvQ, subQ, msgQ, smpServers, ntfServers, smpClients, ntfClients, subscrSrvrs, pendingSubscrSrvrs, subscrConns, connMsgsQueued, smpQueueMsgQueues, smpQueueMsgDeliveries, reconnections, asyncClients, clientId, agentEnv, smpSubscriber = undefined, lock} +agentDbPath :: AgentClient -> FilePath +agentDbPath AgentClient {agentEnv = Env {store = SQLiteStore {dbFilePath}}} = dbFilePath + -- | Agent monad with MonadReader Env and MonadError AgentErrorType type AgentMonad m = (MonadUnliftIO m, MonadReader Env m, MonadError AgentErrorType m) @@ -184,10 +189,11 @@ getSMPServerClient c@AgentClient {active, smpClients, msgQ} srv = do _ -> TM.insert srv cVar ps serverDown :: UnliftIO m -> Map ConnId RcvQueue -> IO () - serverDown u cs = unless (M.null cs) $ do - let conns = M.keys cs - unless (null conns) . notifySub "" $ DOWN srv conns - whenM (readTVarIO active) $ unliftIO u reconnectServer + serverDown u cs = unless (M.null cs) $ + whenM (readTVarIO active) $ do + let conns = M.keys cs + unless (null conns) . notifySub "" $ DOWN srv conns + unliftIO u reconnectServer reconnectServer :: m () reconnectServer = do @@ -304,21 +310,30 @@ newProtocolClient c srv clients connectClient reconnectClient clientVar = tryCon closeAgentClient :: MonadIO m => AgentClient -> m () closeAgentClient c = liftIO $ do atomically $ writeTVar (active c) False - closeSMPServerClients c + closeProtocolServerClients (clientTimeout smpCfg) $ smpClients c + closeProtocolServerClients (clientTimeout ntfCfg) $ ntfClients c cancelActions $ reconnections c cancelActions $ asyncClients c cancelActions $ smpQueueMsgDeliveries c - -closeSMPServerClients :: AgentClient -> IO () -closeSMPServerClients c = readTVarIO (smpClients c) >>= mapM_ (forkIO . closeClient) + clear subscrSrvrs + clear pendingSubscrSrvrs + clear subscrConns + clear connMsgsQueued + clear smpQueueMsgQueues where - closeClient smpVar = - atomically (readTMVar smpVar) >>= \case - Right smp -> closeProtocolClient smp `catchAll_` pure () + clientTimeout sel = tcpTimeout . sel . config $ agentEnv c + clear sel = atomically $ writeTVar (sel c) M.empty + +closeProtocolServerClients :: Int -> TMap ProtocolServer (ClientVar msg) -> IO () +closeProtocolServerClients tcpTimeout cs = readTVarIO cs >>= mapM_ (forkIO . closeClient) >> atomically (writeTVar cs M.empty) + where + closeClient cVar = + tcpTimeout `timeout` atomically (readTMVar cVar) >>= \case + Just (Right client) -> closeProtocolClient client `catchAll_` pure () _ -> pure () -cancelActions :: Foldable f => TVar (f (Async ())) -> IO () -cancelActions as = readTVarIO as >>= mapM_ uninterruptibleCancel +cancelActions :: (Foldable f, Monoid (f (Async ()))) => TVar (f (Async ())) -> IO () +cancelActions as = readTVarIO as >>= mapM_ uninterruptibleCancel >> atomically (writeTVar as mempty) withAgentLock :: MonadUnliftIO m => AgentClient -> m a -> m a withAgentLock AgentClient {lock} = diff --git a/src/Simplex/Messaging/Notifications/Server/Push/testpush.sh b/src/Simplex/Messaging/Notifications/Server/Push/testpush.sh index b41135d83..bac5d8684 100755 --- a/src/Simplex/Messaging/Notifications/Server/Push/testpush.sh +++ b/src/Simplex/Messaging/Notifications/Server/Push/testpush.sh @@ -22,4 +22,5 @@ export AUTHENTICATION_TOKEN="${JWT_HEADER}.${JWT_CLAIMS}.${JWT_SIGNED_HEADER_CLA # curl -v --header "apns-topic: $TOPIC" --header "apns-push-type: background" --header "apns-priority: 5" --header "authorization: bearer $AUTHENTICATION_TOKEN" --data '{"aps":{"content-available":1}}' --http2 https://${APNS_HOST_NAME}/3/device/${DEVICE_TOKEN} # mutable-content notification -curl -v --header "apns-topic: $TOPIC" --header "apns-push-type: alert" --header "authorization: bearer $AUTHENTICATION_TOKEN" --data '{"aps":{"category": "NTF_CAT_CHECK_MESSAGE__SECRET", "mutable-content": 1, "alert":"received encrypted message"}, "data": {"test":"123"}}' --http2 https://${APNS_HOST_NAME}/3/device/${DEVICE_TOKEN} \ No newline at end of file +# NTF_CAT_CHECK_MESSAGE category will not show alert if the app is in foreground +curl -v --header "apns-topic: $TOPIC" --header "apns-push-type: alert" --header "authorization: bearer $AUTHENTICATION_TOKEN" --data '{"aps":{"category": "NTF_CAT_CHECK_MESSAGE__SECRET", "mutable-content": 1, "alert":"received encrypted message"}, "data": {"test":"123"}}' --http2 https://${APNS_HOST_NAME}/3/device/${DEVICE_TOKEN} From 0dc34cd287924631ac9cff615e8d112a694f7d0f Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Mon, 6 Jun 2022 14:52:22 +0400 Subject: [PATCH 041/131] ntf: NMSG, remove NtfTokenId from subscriptionLookup key (#384) --- src/Simplex/Messaging/Notifications/Server.hs | 65 +++++++++++-------- .../Messaging/Notifications/Server/Store.hs | 27 ++++---- 2 files changed, 51 insertions(+), 41 deletions(-) diff --git a/src/Simplex/Messaging/Notifications/Server.hs b/src/Simplex/Messaging/Notifications/Server.hs index 715ea9a99..125a8e5d2 100644 --- a/src/Simplex/Messaging/Notifications/Server.hs +++ b/src/Simplex/Messaging/Notifications/Server.hs @@ -105,7 +105,7 @@ ntfSubscriber NtfSubscriber {smpSubscribers, newSubQ, smpAgent = ca@SMPClientAge receiveSMP :: m () receiveSMP = forever $ do - (_srv, _sessId, _ntfId, msg) <- atomically $ readTBQueue msgQ + (srv, _sessId, ntfId, msg) <- atomically $ readTBQueue msgQ case msg of SMP.NMSG -> do -- check when the last NMSG was received from this queue @@ -114,7 +114,11 @@ ntfSubscriber NtfSubscriber {smpSubscribers, newSubQ, smpAgent = ca@SMPClientAge -- decide whether it should be sent as hidden or visible -- construct and possibly encrypt notification -- send it - pure () + NtfPushServer {pushQ} <- asks pushServer + st <- asks store + atomically $ + findNtfSubscriptionToken st (SMPQueueNtf srv ntfId) + >>= mapM_ (\tkn -> writeTBQueue pushQ (tkn, PNMessage srv ntfId)) _ -> pure () pure () @@ -209,35 +213,42 @@ verifyNtfTransmission (sig_, signed, (corrId, entId, _)) cmd = do else VRFailed NtfCmd SToken c -> do t_ <- atomically $ getNtfToken st entId - pure $ case t_ of - Just t@NtfTknData {tknVerifyKey} - | verifyCmdSignature sig_ signed tknVerifyKey -> verifiedTknCmd t c - | otherwise -> VRFailed - _ -> maybe False (dummyVerifyCmd signed) sig_ `seq` VRFailed - NtfCmd SSubscription c@(SNEW sub@(NewNtfSub tknId _ _)) -> do - -- TODO move active token check here to differentiate error - r_ <- atomically $ findNtfSubscription st sub - pure $ case r_ of - Just (NtfTknData {tknVerifyKey}, sub_) -> - if verifyCmdSignature sig_ signed tknVerifyKey - then case sub_ of - Just s@NtfSubData {tokenId} - | tknId == tokenId -> verifiedSubCmd s c - | otherwise -> VRFailed - _ -> VRVerified (NtfReqNew corrId (ANE SSubscription sub)) - else VRFailed - _ -> maybe False (dummyVerifyCmd signed) sig_ `seq` VRFailed + verifyToken t_ (`verifiedTknCmd` c) + NtfCmd SSubscription c@(SNEW sub@(NewNtfSub tknId smpQueue _)) -> do + s_ <- atomically $ findNtfSubscription st smpQueue + case s_ of + Nothing -> do + -- TODO move active token check here to differentiate error + t_ <- atomically $ getActiveNtfToken st tknId + verifyToken' t_ $ VRVerified (NtfReqNew corrId (ANE SSubscription sub)) + Just s@NtfSubData {tokenId = subTknId} -> + if subTknId == tknId + then do + -- TODO move active token check here to differentiate error + t_ <- atomically $ getActiveNtfToken st subTknId + verifyToken' t_ $ verifiedSubCmd s c + else pure $ maybe False (dummyVerifyCmd signed) sig_ `seq` VRFailed NtfCmd SSubscription c -> do - -- TODO move active token check here to differentiate error - r_ <- atomically $ getNtfSubscription st entId - pure $ case r_ of - Just (s, NtfTknData {tknVerifyKey}) - | verifyCmdSignature sig_ signed tknVerifyKey -> verifiedSubCmd s c - | otherwise -> VRFailed - _ -> maybe False (dummyVerifyCmd signed) sig_ `seq` VRFailed + s_ <- atomically $ getNtfSubscription st entId + case s_ of + Just s@NtfSubData {tokenId = subTknId} -> do + -- TODO move active token check here to differentiate error + t_ <- atomically $ getActiveNtfToken st subTknId + verifyToken' t_ $ verifiedSubCmd s c + _ -> pure $ maybe False (dummyVerifyCmd signed) sig_ `seq` VRFailed where verifiedTknCmd t c = VRVerified (NtfReqCmd SToken (NtfTkn t) (corrId, entId, c)) verifiedSubCmd s c = VRVerified (NtfReqCmd SSubscription (NtfSub s) (corrId, entId, c)) + verifyToken :: Maybe NtfTknData -> (NtfTknData -> VerificationResult) -> m VerificationResult + verifyToken t_ positiveVerificationResult = + pure $ case t_ of + Just t@NtfTknData {tknVerifyKey} -> + if verifyCmdSignature sig_ signed tknVerifyKey + then positiveVerificationResult t + else VRFailed + _ -> maybe False (dummyVerifyCmd signed) sig_ `seq` VRFailed + verifyToken' :: Maybe NtfTknData -> VerificationResult -> m VerificationResult + verifyToken' t_ = verifyToken t_ . const client :: forall m. (MonadUnliftIO m, MonadReader NtfEnv m) => NtfServerClient -> NtfSubscriber -> NtfPushServer -> m () client NtfServerClient {rcvQ, sndQ} NtfSubscriber {newSubQ} NtfPushServer {pushQ, intervalNotifiers} = diff --git a/src/Simplex/Messaging/Notifications/Server/Store.hs b/src/Simplex/Messaging/Notifications/Server/Store.hs index 37a0b4f37..3497cad93 100644 --- a/src/Simplex/Messaging/Notifications/Server/Store.hs +++ b/src/Simplex/Messaging/Notifications/Server/Store.hs @@ -20,14 +20,14 @@ import Simplex.Messaging.Notifications.Protocol import Simplex.Messaging.Protocol (NtfPrivateSignKey) import Simplex.Messaging.TMap (TMap) import qualified Simplex.Messaging.TMap as TM -import Simplex.Messaging.Util (whenM, ($>>=), (<$$>)) +import Simplex.Messaging.Util (whenM, ($>>=)) data NtfStore = NtfStore { tokens :: TMap NtfTokenId NtfTknData, tokenRegistrations :: TMap DeviceToken (TMap ByteString NtfTokenId), subscriptions :: TMap NtfSubscriptionId NtfSubData, tokenSubscriptions :: TMap NtfTokenId (TVar (Set NtfSubscriptionId)), - subscriptionLookup :: TMap (NtfTokenId, SMPQueueNtf) NtfSubscriptionId + subscriptionLookup :: TMap SMPQueueNtf NtfSubscriptionId } newNtfStore :: STM NtfStore @@ -127,20 +127,19 @@ deleteNtfToken st tknId = do regs = tokenRegistrations st regKey = C.toPubKey C.pubKeyBytes -getNtfSubscription :: NtfStore -> NtfSubscriptionId -> STM (Maybe (NtfSubData, NtfTknData)) +getNtfSubscription :: NtfStore -> NtfSubscriptionId -> STM (Maybe NtfSubData) getNtfSubscription st subId = TM.lookup subId (subscriptions st) - $>>= \sub@NtfSubData {tokenId} -> - (sub,) <$$> getActiveNtfToken st tokenId -findNtfSubscription :: NtfStore -> NewNtfEntity 'Subscription -> STM (Maybe (NtfTknData, Maybe NtfSubData)) -findNtfSubscription st (NewNtfSub tknId smpQueue _) = do - getActiveNtfToken st tknId >>= mapM (\tkn -> (tkn,) <$> getSub) - where - getSub :: STM (Maybe NtfSubData) - getSub = - TM.lookup (tknId, smpQueue) (subscriptionLookup st) - $>>= (`TM.lookup` subscriptions st) +findNtfSubscription :: NtfStore -> SMPQueueNtf -> STM (Maybe NtfSubData) +findNtfSubscription st smpQueue = do + TM.lookup smpQueue (subscriptionLookup st) + $>>= \subId -> TM.lookup subId (subscriptions st) + +findNtfSubscriptionToken :: NtfStore -> SMPQueueNtf -> STM (Maybe NtfTknData) +findNtfSubscriptionToken st smpQueue = do + findNtfSubscription st smpQueue + $>>= \NtfSubData {tokenId} -> getActiveNtfToken st tokenId getActiveNtfToken :: NtfStore -> NtfTokenId -> STM (Maybe NtfTknData) getActiveNtfToken st tknId = @@ -160,7 +159,7 @@ addNtfSubscription st subId sub@NtfSubData {smpQueue, tokenId} = insertSub ts = do modifyTVar' ts $ insert subId TM.insert subId sub $ subscriptions st - TM.insert (tokenId, smpQueue) subId (subscriptionLookup st) + TM.insert smpQueue subId (subscriptionLookup st) -- getNtfRec :: NtfStore -> SNtfEntity e -> NtfEntityId -> STM (Maybe (NtfEntityRec e)) -- getNtfRec st ent entId = case ent of From 4b3d04bd27822c6c307274c311870660fdf55dbe Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Mon, 6 Jun 2022 12:59:45 +0100 Subject: [PATCH 042/131] support message flags visible to SMP server to control notifications (and for any future extensions) (#386) * support stopping and resuming agent (#385) * export agentDbPath * support fully closing and resuming agent * whitespace * clean up * support message flags visible to SMP server to control notifications (and for any future extensions) * simplify message flags encoding * GET command --- rfcs/2022-06-05-smp-notifications.md | 34 +++++ simplexmq.cabal | 1 + src/Simplex/Messaging/Agent.hs | 59 +++++---- src/Simplex/Messaging/Agent/Client.hs | 53 +++++--- src/Simplex/Messaging/Agent/Protocol.hs | 23 ++-- src/Simplex/Messaging/Agent/Store.hs | 14 ++- src/Simplex/Messaging/Agent/Store/SQLite.hs | 31 +++-- .../Agent/Store/SQLite/Migrations.hs | 4 +- .../SQLite/Migrations/M20220605_msg_flags.hs | 12 ++ .../Store/SQLite/Migrations/agent_schema.sql | 1 + src/Simplex/Messaging/Client.hs | 19 +-- src/Simplex/Messaging/Encoding.hs | 52 ++++++++ src/Simplex/Messaging/Encoding/String.hs | 25 ++++ .../Messaging/Notifications/Protocol.hs | 12 +- src/Simplex/Messaging/Notifications/Server.hs | 4 +- .../Notifications/Server/Push/testpush.sh | 3 +- src/Simplex/Messaging/Protocol.hs | 82 ++++++++---- src/Simplex/Messaging/Server.hs | 68 +++++++--- src/Simplex/Messaging/Server/Env/STM.hs | 12 +- src/Simplex/Messaging/Server/MsgStore.hs | 4 +- src/Simplex/Messaging/Server/MsgStore/STM.hs | 5 +- src/Simplex/Messaging/Transport.hs | 2 +- src/Simplex/Messaging/Util.hs | 4 + tests/AgentTests.hs | 60 ++++----- tests/AgentTests/FunctionalAPITests.hs | 17 +-- tests/AgentTests/SQLiteTests.hs | 3 + tests/ServerTests.hs | 119 +++++++++--------- 27 files changed, 491 insertions(+), 232 deletions(-) create mode 100644 rfcs/2022-06-05-smp-notifications.md create mode 100644 src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220605_msg_flags.hs diff --git a/rfcs/2022-06-05-smp-notifications.md b/rfcs/2022-06-05-smp-notifications.md new file mode 100644 index 000000000..4e58189b7 --- /dev/null +++ b/rfcs/2022-06-05-smp-notifications.md @@ -0,0 +1,34 @@ +# SMP protocol changes to support push notifications on iOS + +## Problem + +There are already commands/responses to allow subscriptions to message notifications - NKEY/NID, NSUB/NMSG. These commands will be used by SMP agent (NKEY/NID) and by notification server (NSUB/NMSG) to have message notifications delivered to notification server, so it can forward them to APNS server using device token. + +There are two remaining problems that these commands do not solve. + +1. Receiving the message when notification arrives. + +iOS requires creating a bundled notification service extension (NSE) that runs in isolated container and, if we were to use the existing commands, would have SMP subscription to the same SMP servers as the main app, triggering resubscriptions every time the message reception switches between the app and NSE. That would cause a substantial increase in traffic and battery consumption to the users. + +2. Showing notifications for service messages. + +Users do not expect to see notifications for every single SMP messages - e.g., we currently do not show notifications when messages are edited and deleted, and users do not expect them. NSE requires that for every received push notification there should be some notification shown to the users. So only we would have to show a notification for message deletes and updates, we would have to show it for all service messages - e.g. user accepted file invitation, or file transmission has started, contact profile updates and so on. + +We considered differentiating whether notifications are sent per queue, from the recipient side, so we do not send notifications for file queues. But it seems insufficient, particularly if we add such features as message receipts, status updates, etc. + +## Proposal + +1. To retrieve messages when push notifications arrive, we will add 2 SMP commands: + +- GET: retrieve one message from the connection. Resonse could be either MSG (the same as when MSG is delivered, but with the correlation id) or GMSG (to simplify processing) – TBC during implementation. If message is not available, the response could be ERR NO_MSG +- ACK or GACK: acknowledge that the message was processed by the client and can be removed - TBC which one is better. The response is OK or ERR NO_MSG if there was nothing to acknowledge (same as with ACK now) + +This would allow receiving a single message from the queue without subscription, this way avoiding that the main app is unsubscribed from the queue. + +2. The only way to avoid showing unnecessary notifications (status updates, service messages, etc.) is to avoid sending them. That requires instructing SMP server whether notification should be sent both per queue, from the recipient side, and per message - from the sender side. So the notification would only be sent if the queue has them enabled (via NKEY command) and the sender includes an additional flag in SEND command. The same flag should be included into MSG, so when the message is retrieved with GET command, the client knows, on the agent or chat level (or both), whether this message should have notification shown to the user, and if not - retrieve the next one(s) as well. + +This is a substantial change to SMP protocol, that would require client and server upgrade for notifications to be supported. + +We should consider whether to increase the SMP protocol version number to 2, so that the new clients can connect to the old clients but without notifications, or we could keep the old commands in the protocol and instead of adding flags to the existing commands, create new commands. + +We can also consider making commands extensible so that the new flags can be added (and ignored by parsers if not supported) to at least some existing commands. diff --git a/simplexmq.cabal b/simplexmq.cabal index 14abecc27..c6e50f2ed 100644 --- a/simplexmq.cabal +++ b/simplexmq.cabal @@ -48,6 +48,7 @@ library Simplex.Messaging.Agent.Store.SQLite.Migrations.M20220301_snd_queue_keys Simplex.Messaging.Agent.Store.SQLite.Migrations.M20220322_notifications Simplex.Messaging.Agent.Store.SQLite.Migrations.M20220404_ntf_subscriptions_draft + Simplex.Messaging.Agent.Store.SQLite.Migrations.M20220605_msg_flags Simplex.Messaging.Client Simplex.Messaging.Client.Agent Simplex.Messaging.Crypto diff --git a/src/Simplex/Messaging/Agent.hs b/src/Simplex/Messaging/Agent.hs index ee67611a7..a19b1c677 100644 --- a/src/Simplex/Messaging/Agent.hs +++ b/src/Simplex/Messaging/Agent.hs @@ -35,7 +35,8 @@ module Simplex.Messaging.Agent AgentMonad, AgentErrorMonad, getSMPAgentClient, - disconnectAgentClient, -- used in tests + disconnectAgentClient, + resumeAgentClient, withAgentLock, createConnection, joinConnection, @@ -91,7 +92,7 @@ import Simplex.Messaging.Encoding import Simplex.Messaging.Notifications.Client import Simplex.Messaging.Notifications.Protocol (DeviceToken, NtfRegCode (NtfRegCode), NtfTknStatus (..)) import Simplex.Messaging.Parsers (parse) -import Simplex.Messaging.Protocol (BrokerMsg, ErrorType (AUTH), MsgBody) +import Simplex.Messaging.Protocol (BrokerMsg, ErrorType (AUTH), MsgBody, MsgFlags) import qualified Simplex.Messaging.Protocol as SMP import qualified Simplex.Messaging.TMap as TM import Simplex.Messaging.Util (bshow, liftError, tryError, unlessM, ($>>=)) @@ -113,6 +114,9 @@ getSMPAgentClient cfg initServers = newSMPAgentEnv cfg >>= runReaderT runAgent disconnectAgentClient :: MonadUnliftIO m => AgentClient -> m () disconnectAgentClient c = closeAgentClient c >> logConnection c False +resumeAgentClient :: MonadIO m => AgentClient -> m () +resumeAgentClient c = atomically $ writeTVar (active c) True + -- | type AgentErrorMonad m = (MonadUnliftIO m, MonadError AgentErrorType m) @@ -144,8 +148,8 @@ resubscribeConnection :: AgentErrorMonad m => AgentClient -> ConnId -> m () resubscribeConnection c = withAgentEnv c . resubscribeConnection' c -- | Send message to the connection (SEND command) -sendMessage :: AgentErrorMonad m => AgentClient -> ConnId -> MsgBody -> m AgentMsgId -sendMessage c = withAgentEnv c .: sendMessage' c +sendMessage :: AgentErrorMonad m => AgentClient -> ConnId -> MsgFlags -> MsgBody -> m AgentMsgId +sendMessage c = withAgentEnv c .:. sendMessage' c ackMessage :: AgentErrorMonad m => AgentClient -> ConnId -> AgentMsgId -> m () ackMessage c = withAgentEnv c .: ackMessage' c @@ -242,7 +246,7 @@ processCommand c (connId, cmd) = case cmd of ACPT invId ownCInfo -> (,OK) <$> acceptContact' c connId invId ownCInfo RJCT invId -> rejectContact' c connId invId $> (connId, OK) SUB -> subscribeConnection' c connId $> (connId, OK) - SEND msgBody -> (connId,) . MID <$> sendMessage' c connId msgBody + SEND msgFlags msgBody -> (connId,) . MID <$> sendMessage' c connId msgFlags msgBody ACK msgId -> ackMessage' c connId msgId $> (connId, OK) OFF -> suspendConnection' c connId $> (connId, OK) DEL -> deleteConnection' c connId $> (connId, OK) @@ -283,7 +287,7 @@ joinConn c connId (CRInvitationUri (ConnReqUriData _ agentVRange (qUri :| _)) e2 pure connId' tryError (confirmQueue c connId' sq cInfo $ Just e2eSndParams) >>= \case Right _ -> do - void $ enqueueMessage c connId' sq HELLO + void $ enqueueMessage c connId' sq SMP.noMsgFlags HELLO pure connId' Left e -> do -- TODO recovery for failure on network timeout, see rfcs/2022-04-20-smp-conf-timeout-recovery.md @@ -309,7 +313,7 @@ createReplyQueue c connId sq = do let qInfo = toVersionT qUri SMP.smpClientVersion addSubscription c rq connId withStore $ \st -> upgradeSndConnToDuplex st connId rq - void . enqueueMessage c connId sq $ REPLY [qInfo] + void . enqueueMessage c connId sq SMP.noMsgFlags $ REPLY [qInfo] -- | Approve confirmation (LET command) in Reader monad allowConnection' :: AgentMonad m => AgentClient -> ConnId -> ConfirmationId -> ConnInfo -> m () @@ -366,18 +370,18 @@ resubscribeConnection' c connId = (subscribeConnection' c connId) -- | Send message to the connection (SEND command) in Reader monad -sendMessage' :: forall m. AgentMonad m => AgentClient -> ConnId -> MsgBody -> m AgentMsgId -sendMessage' c connId msg = +sendMessage' :: forall m. AgentMonad m => AgentClient -> ConnId -> MsgFlags -> MsgBody -> m AgentMsgId +sendMessage' c connId msgFlags msg = withStore (`getConn` connId) >>= \case SomeConn _ (DuplexConnection _ _ sq) -> enqueueMsg sq SomeConn _ (SndConnection _ sq) -> enqueueMsg sq _ -> throwError $ CONN SIMPLEX where enqueueMsg :: SndQueue -> m AgentMsgId - enqueueMsg sq = enqueueMessage c connId sq $ A_MSG msg + enqueueMsg sq = enqueueMessage c connId sq msgFlags $ A_MSG msg -enqueueMessage :: forall m. AgentMonad m => AgentClient -> ConnId -> SndQueue -> AMessage -> m AgentMsgId -enqueueMessage c connId sq aMessage = do +enqueueMessage :: forall m. AgentMonad m => AgentClient -> ConnId -> SndQueue -> MsgFlags -> AMessage -> m AgentMsgId +enqueueMessage c connId sq msgFlags aMessage = do resumeMsgDelivery c connId sq msgId <- storeSentMsg queuePendingMsgs c connId sq [msgId] @@ -394,7 +398,7 @@ enqueueMessage c connId sq aMessage = do encAgentMessage <- agentRatchetEncrypt connId agentMsgStr e2eEncUserMsgLength let msgBody = smpEncode $ AgentMsgEnvelope {agentVersion = smpAgentVersion, encAgentMessage} msgType = agentMessageType agentMsg - msgData = SndMsgData {internalId, internalSndId, internalTs, msgType, msgBody, internalHash, prevMsgHash} + msgData = SndMsgData {internalId, internalSndId, internalTs, msgType, msgFlags, msgBody, internalHash, prevMsgHash} withStore $ \st -> createSndMsg st connId msgData pure internalId @@ -436,9 +440,12 @@ runSmpQueueMsgDelivery c@AgentClient {subQ} connId sq = do withStore (\st -> E.try $ getPendingMsgData st connId msgId) >>= \case Left (e :: E.SomeException) -> notify $ MERR mId (INTERNAL $ show e) - Right (rq_, (msgType, msgBody, internalTs)) -> - withRetryInterval ri $ \loop -> - tryError (send msgType c sq msgBody) >>= \case + Right (rq_, PendingMsgData {msgType, msgBody, msgFlags, internalTs}) -> + withRetryInterval ri $ \loop -> do + resp <- tryError $ case msgType of + AM_CONN_INFO -> sendConfirmation c sq msgBody + _ -> sendAgentMessage c sq msgFlags msgBody + case resp of Left e -> do let err = if msgType == AM_CONN_INFO then ERR e else MERR mId e case e of @@ -468,7 +475,8 @@ runSmpQueueMsgDelivery c@AgentClient {subQ} connId sq = do AM_CONN_INFO -> do withStore $ \st -> setSndQueueStatus st sq Confirmed when (isJust rq_) $ withStore (`removeConfirmations` connId) - void $ enqueueMessage c connId sq HELLO + -- TODO possibly notification flag should be ON for one of the parties, to result in contact connected notification + void $ enqueueMessage c connId sq SMP.noMsgFlags HELLO AM_HELLO_ -> do withStore $ \st -> setSndQueueStatus st sq Active case rq_ of @@ -482,9 +490,6 @@ runSmpQueueMsgDelivery c@AgentClient {subQ} connId sq = do _ -> pure () delMsg msgId where - send = \case - AM_CONN_INFO -> sendConfirmation - _ -> sendAgentMessage delMsg :: InternalId -> m () delMsg msgId = withStore $ \st -> deleteMsg st connId msgId notify :: ACommand 'Agent -> m () @@ -675,7 +680,7 @@ processSMPTransmission c@AgentClient {smpClients, subQ} (srv, sessId, rId, cmd) processSMP :: SConnType c -> ConnData -> RcvQueue -> m () processSMP cType ConnData {connId} rq@RcvQueue {rcvDhSecret, e2ePrivKey, e2eDhSecret, status} = case cmd of - SMP.MSG srvMsgId srvTs msgBody' -> handleNotifyAck $ do + SMP.MSG srvMsgId srvTs msgFlags msgBody' -> handleNotifyAck $ do -- TODO deduplicate with previously received msgBody <- agentCbDecrypt rcvDhSecret (C.cbNonce srvMsgId) msgBody' clientMsg@SMP.ClientMsgEnvelope {cmHeader = SMP.PubHeader phVer e2ePubKey_} <- @@ -697,12 +702,12 @@ processSMPTransmission c@AgentClient {smpClients, subQ} (srv, sessId, rId, cmd) parseMessage agentMsgBody >>= \case agentMsg@(AgentMessage APrivHeader {sndMsgId, prevMsgHash} aMessage) -> do let msgType = agentMessageType agentMsg - (msgId, msgMeta) <- agentClientMsg prevMsgHash sndMsgId (srvMsgId, systemToUTCTime srvTs) agentMsgBody msgType + (msgId, msgMeta) <- agentClientMsg prevMsgHash sndMsgId (srvMsgId, systemToUTCTime srvTs) msgFlags agentMsgBody msgType case aMessage of HELLO -> helloMsg >> ack >> withStore (\st -> deleteMsg st connId msgId) REPLY cReq -> replyMsg cReq >> ack >> withStore (\st -> deleteMsg st connId msgId) -- note that there is no ACK sent here, it is sent with agent's user ACK command - A_MSG body -> notify $ MSG msgMeta body + A_MSG body -> notify $ MSG msgMeta msgFlags body _ -> prohibited >> ack _ -> prohibited >> ack _ -> prohibited >> ack @@ -801,8 +806,8 @@ processSMPTransmission c@AgentClient {smpClients, subQ} (srv, sessId, rId, cmd) enqueueConfirmation c connId sq ownConnInfo Nothing _ -> prohibited - agentClientMsg :: PrevRcvMsgHash -> ExternalSndId -> (BrokerId, BrokerTs) -> MsgBody -> AgentMessageType -> m (InternalId, MsgMeta) - agentClientMsg externalPrevSndHash sndMsgId broker msgBody msgType = do + agentClientMsg :: PrevRcvMsgHash -> ExternalSndId -> (BrokerId, BrokerTs) -> MsgFlags -> MsgBody -> AgentMessageType -> m (InternalId, MsgMeta) + agentClientMsg externalPrevSndHash sndMsgId broker msgFlags msgBody msgType = do logServer "<--" c srv rId "MSG " let internalHash = C.sha256Hash msgBody internalTs <- liftIO getCurrentTime @@ -810,7 +815,7 @@ processSMPTransmission c@AgentClient {smpClients, subQ} (srv, sessId, rId, cmd) let integrity = checkMsgIntegrity prevExtSndId sndMsgId prevRcvMsgHash externalPrevSndHash recipient = (unId internalId, internalTs) msgMeta = MsgMeta {integrity, recipient, broker, sndMsgId} - rcvMsg = RcvMsgData {msgMeta, msgType, msgBody, internalRcvId, internalHash, externalPrevSndHash} + rcvMsg = RcvMsgData {msgMeta, msgType, msgFlags, msgBody, internalRcvId, internalHash, externalPrevSndHash} withStore $ \st -> createRcvMsg st connId rcvMsg pure (internalId, msgMeta) @@ -862,7 +867,7 @@ enqueueConfirmation c connId sq connInfo e2eEncryption = do encConnInfo <- agentRatchetEncrypt connId agentMsgStr e2eEncConnInfoLength let msgBody = smpEncode $ AgentConfirmation {agentVersion = smpAgentVersion, e2eEncryption, encConnInfo} msgType = agentMessageType agentMsg - msgData = SndMsgData {internalId, internalSndId, internalTs, msgType, msgBody, internalHash, prevMsgHash} + msgData = SndMsgData {internalId, internalSndId, internalTs, msgType, msgBody, msgFlags = SMP.noMsgFlags, internalHash, prevMsgHash} withStore $ \st -> createSndMsg st connId msgData pure internalId diff --git a/src/Simplex/Messaging/Agent/Client.hs b/src/Simplex/Messaging/Agent/Client.hs index 2d842e3c9..6f186ece2 100644 --- a/src/Simplex/Messaging/Agent/Client.hs +++ b/src/Simplex/Messaging/Agent/Client.hs @@ -39,6 +39,7 @@ module Simplex.Messaging.Agent.Client logServer, removeSubscription, hasActiveSubscription, + agentDbPath, ) where @@ -63,13 +64,14 @@ import Simplex.Messaging.Agent.Env.SQLite import Simplex.Messaging.Agent.Protocol import Simplex.Messaging.Agent.RetryInterval import Simplex.Messaging.Agent.Store +import Simplex.Messaging.Agent.Store.SQLite (SQLiteStore (..)) import Simplex.Messaging.Client import Simplex.Messaging.Client.Agent () import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Encoding import Simplex.Messaging.Notifications.Client import Simplex.Messaging.Notifications.Protocol -import Simplex.Messaging.Protocol (BrokerMsg, ErrorType, ProtocolServer (..), QueueId, QueueIdsKeys (..), SndPublicVerifyKey) +import Simplex.Messaging.Protocol (BrokerMsg, ErrorType, MsgFlags (..), ProtocolServer (..), QueueId, QueueIdsKeys (..), SndPublicVerifyKey) import qualified Simplex.Messaging.Protocol as SMP import Simplex.Messaging.TMap (TMap) import qualified Simplex.Messaging.TMap as TM @@ -132,6 +134,9 @@ newAgentClient InitialAgentServers {smp, ntf} agentEnv = do lock <- newTMVar () return AgentClient {active, rcvQ, subQ, msgQ, smpServers, ntfServers, smpClients, ntfClients, subscrSrvrs, pendingSubscrSrvrs, subscrConns, connMsgsQueued, smpQueueMsgQueues, smpQueueMsgDeliveries, reconnections, asyncClients, clientId, agentEnv, smpSubscriber = undefined, lock} +agentDbPath :: AgentClient -> FilePath +agentDbPath AgentClient {agentEnv = Env {store = SQLiteStore {dbFilePath}}} = dbFilePath + -- | Agent monad with MonadReader Env and MonadError AgentErrorType type AgentMonad m = (MonadUnliftIO m, MonadReader Env m, MonadError AgentErrorType m) @@ -184,10 +189,11 @@ getSMPServerClient c@AgentClient {active, smpClients, msgQ} srv = do _ -> TM.insert srv cVar ps serverDown :: UnliftIO m -> Map ConnId RcvQueue -> IO () - serverDown u cs = unless (M.null cs) $ do - let conns = M.keys cs - unless (null conns) . notifySub "" $ DOWN srv conns - whenM (readTVarIO active) $ unliftIO u reconnectServer + serverDown u cs = unless (M.null cs) $ + whenM (readTVarIO active) $ do + let conns = M.keys cs + unless (null conns) . notifySub "" $ DOWN srv conns + unliftIO u reconnectServer reconnectServer :: m () reconnectServer = do @@ -304,21 +310,30 @@ newProtocolClient c srv clients connectClient reconnectClient clientVar = tryCon closeAgentClient :: MonadIO m => AgentClient -> m () closeAgentClient c = liftIO $ do atomically $ writeTVar (active c) False - closeSMPServerClients c + closeProtocolServerClients (clientTimeout smpCfg) $ smpClients c + closeProtocolServerClients (clientTimeout ntfCfg) $ ntfClients c cancelActions $ reconnections c cancelActions $ asyncClients c cancelActions $ smpQueueMsgDeliveries c - -closeSMPServerClients :: AgentClient -> IO () -closeSMPServerClients c = readTVarIO (smpClients c) >>= mapM_ (forkIO . closeClient) + clear subscrSrvrs + clear pendingSubscrSrvrs + clear subscrConns + clear connMsgsQueued + clear smpQueueMsgQueues where - closeClient smpVar = - atomically (readTMVar smpVar) >>= \case - Right smp -> closeProtocolClient smp `catchAll_` pure () + clientTimeout sel = tcpTimeout . sel . config $ agentEnv c + clear sel = atomically $ writeTVar (sel c) M.empty + +closeProtocolServerClients :: Int -> TMap ProtocolServer (ClientVar msg) -> IO () +closeProtocolServerClients tcpTimeout cs = readTVarIO cs >>= mapM_ (forkIO . closeClient) >> atomically (writeTVar cs M.empty) + where + closeClient cVar = + tcpTimeout `timeout` atomically (readTMVar cVar) >>= \case + Just (Right client) -> closeProtocolClient client `catchAll_` pure () _ -> pure () -cancelActions :: Foldable f => TVar (f (Async ())) -> IO () -cancelActions as = readTVarIO as >>= mapM_ uninterruptibleCancel +cancelActions :: (Foldable f, Monoid (f (Async ()))) => TVar (f (Async ())) -> IO () +cancelActions as = readTVarIO as >>= mapM_ uninterruptibleCancel >> atomically (writeTVar as mempty) withAgentLock :: MonadUnliftIO m => AgentClient -> m a -> m a withAgentLock AgentClient {lock} = @@ -450,14 +465,14 @@ sendConfirmation c sq@SndQueue {server, sndId, sndPublicKey = Just sndPublicKey, withLogClient_ c server sndId "SEND " $ \smp -> do let clientMsg = SMP.ClientMessage (SMP.PHConfirmation sndPublicKey) agentConfirmation msg <- agentCbEncrypt sq e2ePubKey $ smpEncode clientMsg - liftClient SMP $ sendSMPMessage smp Nothing sndId msg + liftClient SMP $ sendSMPMessage smp Nothing sndId SMP.noMsgFlags msg sendConfirmation _ _ _ = throwError $ INTERNAL "sendConfirmation called without snd_queue public key(s) in the database" sendInvitation :: forall m. AgentMonad m => AgentClient -> Compatible SMPQueueInfo -> ConnectionRequestUri 'CMInvitation -> ConnInfo -> m () sendInvitation c (Compatible SMPQueueInfo {smpServer, senderId, dhPublicKey}) connReq connInfo = withLogClient_ c smpServer senderId "SEND " $ \smp -> do msg <- mkInvitation - liftClient SMP $ sendSMPMessage smp Nothing senderId msg + liftClient SMP $ sendSMPMessage smp Nothing senderId MsgFlags {notification = True} msg where mkInvitation :: m ByteString -- this is only encrypted with per-queue E2E, not with double ratchet @@ -486,12 +501,12 @@ deleteQueue c RcvQueue {server, rcvId, rcvPrivateKey} = withLogClient c server rcvId "DEL" $ \smp -> deleteSMPQueue smp rcvPrivateKey rcvId -sendAgentMessage :: forall m. AgentMonad m => AgentClient -> SndQueue -> ByteString -> m () -sendAgentMessage c sq@SndQueue {server, sndId, sndPrivateKey} agentMsg = +sendAgentMessage :: forall m. AgentMonad m => AgentClient -> SndQueue -> MsgFlags -> ByteString -> m () +sendAgentMessage c sq@SndQueue {server, sndId, sndPrivateKey} msgFlags agentMsg = withLogClient_ c server sndId "SEND " $ \smp -> do let clientMsg = SMP.ClientMessage SMP.PHEmpty agentMsg msg <- agentCbEncrypt sq Nothing $ smpEncode clientMsg - liftClient SMP $ sendSMPMessage smp (Just sndPrivateKey) sndId msg + liftClient SMP $ sendSMPMessage smp (Just sndPrivateKey) sndId msgFlags msg agentNtfRegisterToken :: AgentMonad m => AgentClient -> NtfToken -> C.APublicVerifyKey -> C.PublicKeyX25519 -> m (NtfTokenId, C.PublicKeyX25519) agentNtfRegisterToken c NtfToken {deviceToken, ntfServer, ntfPrivKey} ntfPubKey pubDhKey = diff --git a/src/Simplex/Messaging/Agent/Protocol.hs b/src/Simplex/Messaging/Agent/Protocol.hs index 31ce96c3b..bfb4b785a 100644 --- a/src/Simplex/Messaging/Agent/Protocol.hs +++ b/src/Simplex/Messaging/Agent/Protocol.hs @@ -110,7 +110,7 @@ import qualified Data.Attoparsec.ByteString.Char8 as A import Data.ByteString.Base64 import Data.ByteString.Char8 (ByteString) import qualified Data.ByteString.Char8 as B -import Data.Composition ((.:)) +import Data.Composition ((.:), (.:.)) import Data.Functor (($>)) import Data.Int (Int64) import Data.Kind (Type) @@ -132,6 +132,7 @@ import Simplex.Messaging.Parsers import Simplex.Messaging.Protocol ( ErrorType, MsgBody, + MsgFlags, MsgId, SMPServer, SndPublicVerifyKey, @@ -212,11 +213,11 @@ data ACommand (p :: AParty) where END :: ACommand Agent DOWN :: SMPServer -> [ConnId] -> ACommand Agent UP :: SMPServer -> [ConnId] -> ACommand Agent - SEND :: MsgBody -> ACommand Client + SEND :: MsgFlags -> MsgBody -> ACommand Client MID :: AgentMsgId -> ACommand Agent SENT :: AgentMsgId -> ACommand Agent MERR :: AgentMsgId -> AgentErrorType -> ACommand Agent - MSG :: MsgMeta -> MsgBody -> ACommand Agent + MSG :: MsgMeta -> MsgFlags -> MsgBody -> ACommand Agent ACK :: AgentMsgId -> ACommand Client OFF :: ACommand Client DEL :: ACommand Client @@ -840,13 +841,13 @@ commandP = acptCmd = ACmd SClient .: ACPT <$> A.takeTill (== ' ') <* A.space <*> A.takeByteString rjctCmd = ACmd SClient . RJCT <$> A.takeByteString infoCmd = ACmd SAgent . INFO <$> A.takeByteString - downsResp = ACmd SAgent .: DOWN <$> strP <* A.space <*> connections - upsResp = ACmd SAgent .: UP <$> strP <* A.space <*> connections - sendCmd = ACmd SClient . SEND <$> A.takeByteString + downsResp = ACmd SAgent .: DOWN <$> strP_ <*> connections + upsResp = ACmd SAgent .: UP <$> strP_ <*> connections + sendCmd = ACmd SClient .: SEND <$> smpP <* A.space <*> A.takeByteString msgIdResp = ACmd SAgent . MID <$> A.decimal sentResp = ACmd SAgent . SENT <$> A.decimal msgErrResp = ACmd SAgent .: MERR <$> A.decimal <* A.space <*> strP - message = ACmd SAgent .: MSG <$> msgMetaP <* A.space <*> A.takeByteString + message = ACmd SAgent .:. MSG <$> msgMetaP <* A.space <*> smpP <* A.space <*> A.takeByteString ackCmd = ACmd SClient . ACK <$> A.decimal connections = strP `A.sepBy'` (A.char ',') msgMetaP = do @@ -877,11 +878,11 @@ serializeCommand = \case END -> "END" DOWN srv conns -> B.unwords ["DOWN", strEncode srv, connections conns] UP srv conns -> B.unwords ["UP", strEncode srv, connections conns] - SEND msgBody -> "SEND " <> serializeBinary msgBody + SEND msgFlags msgBody -> "SEND " <> smpEncode msgFlags <> " " <> serializeBinary msgBody MID mId -> "MID " <> bshow mId SENT mId -> "SENT " <> bshow mId MERR mId e -> B.unwords ["MERR", bshow mId, strEncode e] - MSG msgMeta msgBody -> B.unwords ["MSG", serializeMsgMeta msgMeta, serializeBinary msgBody] + MSG msgMeta msgFlags msgBody -> B.unwords ["MSG", serializeMsgMeta msgMeta, smpEncode msgFlags, serializeBinary msgBody] ACK mId -> "ACK " <> bshow mId OFF -> "OFF" DEL -> "DEL" @@ -953,8 +954,8 @@ tGet party h = liftIO (tGetRaw h) >>= tParseLoadBody cmdWithMsgBody :: ACommand p -> m (Either AgentErrorType (ACommand p)) cmdWithMsgBody = \case - SEND body -> SEND <$$> getBody body - MSG msgMeta body -> MSG msgMeta <$$> getBody body + SEND msgFlags body -> SEND msgFlags <$$> getBody body + MSG msgMeta msgFlags body -> MSG msgMeta msgFlags <$$> getBody body JOIN qUri cInfo -> JOIN qUri <$$> getBody cInfo CONF confId cInfo -> CONF confId <$$> getBody cInfo LET confId cInfo -> LET confId <$$> getBody cInfo diff --git a/src/Simplex/Messaging/Agent/Store.hs b/src/Simplex/Messaging/Agent/Store.hs index ec564ceb8..34c1579ae 100644 --- a/src/Simplex/Messaging/Agent/Store.hs +++ b/src/Simplex/Messaging/Agent/Store.hs @@ -24,6 +24,7 @@ import Simplex.Messaging.Notifications.Client import Simplex.Messaging.Notifications.Protocol (DeviceToken, NtfTknStatus, NtfTokenId) import Simplex.Messaging.Protocol ( MsgBody, + MsgFlags, MsgId, RcvDhSecret, RcvPrivateSignKey, @@ -64,7 +65,7 @@ class Monad m => MonadAgentStore s m where createRcvMsg :: s -> ConnId -> RcvMsgData -> m () updateSndIds :: s -> ConnId -> m (InternalId, InternalSndId, PrevSndMsgHash) createSndMsg :: s -> ConnId -> SndMsgData -> m () - getPendingMsgData :: s -> ConnId -> InternalId -> m (Maybe RcvQueue, (AgentMessageType, MsgBody, InternalTs)) + getPendingMsgData :: s -> ConnId -> InternalId -> m (Maybe RcvQueue, PendingMsgData) getPendingMsgs :: s -> ConnId -> m [InternalId] checkRcvMsg :: s -> ConnId -> InternalId -> m () deleteMsg :: s -> ConnId -> InternalId -> m () @@ -235,6 +236,7 @@ type PrevSndMsgHash = MsgHash data RcvMsgData = RcvMsgData { msgMeta :: MsgMeta, msgType :: AgentMessageType, + msgFlags :: MsgFlags, msgBody :: MsgBody, internalRcvId :: InternalRcvId, internalHash :: MsgHash, @@ -246,14 +248,18 @@ data SndMsgData = SndMsgData internalSndId :: InternalSndId, internalTs :: InternalTs, msgType :: AgentMessageType, + msgFlags :: MsgFlags, msgBody :: MsgBody, internalHash :: MsgHash, prevMsgHash :: MsgHash } -data PendingMsg = PendingMsg - { connId :: ConnId, - msgId :: InternalId +data PendingMsgData = PendingMsgData + { msgId :: InternalId, + msgType :: AgentMessageType, + msgFlags :: MsgFlags, + msgBody :: MsgBody, + internalTs :: InternalTs } deriving (Show) diff --git a/src/Simplex/Messaging/Agent/Store/SQLite.hs b/src/Simplex/Messaging/Agent/Store/SQLite.hs index 5c4e679b7..ec34ab9a3 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite.hs @@ -43,7 +43,7 @@ import qualified Data.Map.Strict as M import Data.Maybe (listToMaybe) import Data.Text (Text) import qualified Data.Text as T -import Data.Text.Encoding (decodeLatin1) +import Data.Text.Encoding (decodeLatin1, encodeUtf8) import Data.Time.Clock (getCurrentTime) import Database.SQLite.Simple (FromRow, NamedParam (..), Only (..), SQLError, ToRow, field, (:.) (..)) import qualified Database.SQLite.Simple as DB @@ -61,9 +61,9 @@ import Simplex.Messaging.Encoding.String import Simplex.Messaging.Notifications.Client (NtfServer, NtfTknAction, NtfToken (..)) import Simplex.Messaging.Notifications.Protocol (DeviceToken (..), NtfTknStatus (..), NtfTokenId) import Simplex.Messaging.Parsers (blobFieldParser, fromTextField_) -import Simplex.Messaging.Protocol (MsgBody, ProtocolServer (..)) +import Simplex.Messaging.Protocol (MsgBody, MsgFlags, ProtocolServer (..)) import qualified Simplex.Messaging.Protocol as SMP -import Simplex.Messaging.Util (bshow, liftIOEither) +import Simplex.Messaging.Util (bshow, eitherToMaybe, liftIOEither) import System.Directory (copyFile, createDirectoryIfMissing, doesFileExist) import System.Exit (exitFailure) import System.FilePath (takeDirectory) @@ -459,22 +459,25 @@ instance (MonadUnliftIO m, MonadError StoreError m) => MonadAgentStore SQLiteSto insertSndMsgDetails_ db connId sndMsgData updateHashSnd_ db connId sndMsgData - getPendingMsgData :: SQLiteStore -> ConnId -> InternalId -> m (Maybe RcvQueue, (AgentMessageType, MsgBody, InternalTs)) + getPendingMsgData :: SQLiteStore -> ConnId -> InternalId -> m (Maybe RcvQueue, PendingMsgData) getPendingMsgData st connId msgId = liftIOEither . withTransaction st $ \db -> runExceptT $ do rq_ <- liftIO $ getRcvQueueByConnId_ db connId msgData <- - ExceptT . firstRow id SEMsgNotFound $ + ExceptT . firstRow pendingMsgData SEMsgNotFound $ DB.query db [sql| - SELECT m.msg_type, m.msg_body, m.internal_ts + SELECT m.msg_type, m.msg_flags, m.msg_body, m.internal_ts FROM messages m JOIN snd_messages s ON s.conn_id = m.conn_id AND s.internal_id = m.internal_id WHERE m.conn_id = ? AND m.internal_id = ? |] (connId, msgId) pure (rq_, msgData) + where + pendingMsgData :: (AgentMessageType, MsgFlags, MsgBody, InternalTs) -> PendingMsgData + pendingMsgData (msgType, msgFlags, msgBody, internalTs) = PendingMsgData {msgId, msgType, msgFlags, msgBody, internalTs} getPendingMsgs :: SQLiteStore -> ConnId -> m [InternalId] getPendingMsgs st connId = @@ -681,6 +684,10 @@ instance ToField (SConnectionMode c) where toField = toField . connMode instance FromField AConnectionMode where fromField = fromTextField_ $ fmap connMode' . connModeT +instance ToField MsgFlags where toField = toField . decodeLatin1 . smpEncode + +instance FromField MsgFlags where fromField = fromTextField_ $ eitherToMaybe . smpDecode . encodeUtf8 + listToEither :: e -> [a] -> Either e a listToEither _ (x : _) = Right x listToEither e _ = Left e @@ -862,21 +869,22 @@ updateLastIdsRcv_ dbConn connId newInternalId newInternalRcvId = -- * createRcvMsg helpers insertRcvMsgBase_ :: DB.Connection -> ConnId -> RcvMsgData -> IO () -insertRcvMsgBase_ dbConn connId RcvMsgData {msgMeta, msgType, msgBody, internalRcvId} = do +insertRcvMsgBase_ dbConn connId RcvMsgData {msgMeta, msgType, msgFlags, msgBody, internalRcvId} = do let MsgMeta {recipient = (internalId, internalTs)} = msgMeta DB.executeNamed dbConn [sql| INSERT INTO messages - ( conn_id, internal_id, internal_ts, internal_rcv_id, internal_snd_id, msg_type, msg_body) + ( conn_id, internal_id, internal_ts, internal_rcv_id, internal_snd_id, msg_type, msg_flags, msg_body) VALUES - (:conn_id,:internal_id,:internal_ts,:internal_rcv_id, NULL,:msg_type, :msg_body); + (:conn_id,:internal_id,:internal_ts,:internal_rcv_id, NULL,:msg_type,:msg_flags,:msg_body); |] [ ":conn_id" := connId, ":internal_id" := internalId, ":internal_ts" := internalTs, ":internal_rcv_id" := internalRcvId, ":msg_type" := msgType, + ":msg_flags" := msgFlags, ":msg_body" := msgBody ] @@ -962,15 +970,16 @@ insertSndMsgBase_ dbConn connId SndMsgData {..} = do dbConn [sql| INSERT INTO messages - ( conn_id, internal_id, internal_ts, internal_rcv_id, internal_snd_id, msg_type, msg_body) + ( conn_id, internal_id, internal_ts, internal_rcv_id, internal_snd_id, msg_type, msg_flags, msg_body) VALUES - (:conn_id,:internal_id,:internal_ts, NULL,:internal_snd_id,:msg_type, :msg_body); + (:conn_id,:internal_id,:internal_ts, NULL,:internal_snd_id,:msg_type,:msg_flags,:msg_body); |] [ ":conn_id" := connId, ":internal_id" := internalId, ":internal_ts" := internalTs, ":internal_snd_id" := internalSndId, ":msg_type" := msgType, + ":msg_flags" := msgFlags, ":msg_body" := msgBody ] diff --git a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations.hs b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations.hs index 7e7f9f5b4..449c7c340 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations.hs @@ -27,6 +27,7 @@ import qualified Database.SQLite3 as SQLite3 import Simplex.Messaging.Agent.Store.SQLite.Migrations.M20220101_initial import Simplex.Messaging.Agent.Store.SQLite.Migrations.M20220301_snd_queue_keys import Simplex.Messaging.Agent.Store.SQLite.Migrations.M20220322_notifications +import Simplex.Messaging.Agent.Store.SQLite.Migrations.M20220605_msg_flags data Migration = Migration {name :: String, up :: Text} deriving (Show) @@ -35,7 +36,8 @@ schemaMigrations :: [(String, Query)] schemaMigrations = [ ("20220101_initial", m20220101_initial), ("20220301_snd_queue_keys", m20220301_snd_queue_keys), - ("20220322_notifications", m20220322_notifications) + ("20220322_notifications", m20220322_notifications), + ("20220605_msg_flags", m20220605_msg_flags) ] -- | The list of migrations in ascending order by date diff --git a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220605_msg_flags.hs b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220605_msg_flags.hs new file mode 100644 index 000000000..2b6311a63 --- /dev/null +++ b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220605_msg_flags.hs @@ -0,0 +1,12 @@ +{-# LANGUAGE QuasiQuotes #-} + +module Simplex.Messaging.Agent.Store.SQLite.Migrations.M20220605_msg_flags where + +import Database.SQLite.Simple (Query) +import Database.SQLite.Simple.QQ (sql) + +m20220605_msg_flags :: Query +m20220605_msg_flags = + [sql| +ALTER TABLE messages ADD COLUMN msg_flags TEXT NULL; +|] diff --git a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql index d5fbc7718..1e694c87f 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql +++ b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql @@ -64,6 +64,7 @@ CREATE TABLE messages( internal_snd_id INTEGER, msg_type BLOB NOT NULL, --(H)ELLO,(R)EPLY,(D)ELETE. Should SMP confirmation be saved too? msg_body BLOB NOT NULL DEFAULT x'', + msg_flags TEXT NULL, PRIMARY KEY(conn_id, internal_id), FOREIGN KEY(conn_id, internal_rcv_id) REFERENCES rcv_messages ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED, diff --git a/src/Simplex/Messaging/Client.hs b/src/Simplex/Messaging/Client.hs index dbe47d5f6..e87a56e8c 100644 --- a/src/Simplex/Messaging/Client.hs +++ b/src/Simplex/Messaging/Client.hs @@ -70,6 +70,7 @@ import Simplex.Messaging.Transport.Client (runTransportClient) import Simplex.Messaging.Transport.KeepAlive import Simplex.Messaging.Transport.WebSockets (WS) import Simplex.Messaging.Util (bshow, liftError, raceAny_) +import Simplex.Messaging.Version import System.Timeout (timeout) -- | 'SMPClient' is a handle used to send commands to a specific SMP server. @@ -79,6 +80,7 @@ data ProtocolClient msg = ProtocolClient { action :: Async (), connected :: TVar Bool, sessionId :: SessionId, + thVersion :: Version, protocolServer :: ProtocolServer, tcpTimeout :: Int, clientCorrId :: TVar Natural, @@ -146,6 +148,7 @@ getProtocolClient protocolServer cfg@ProtocolClientConfig {qSize, tcpTimeout, tc ProtocolClient { action = undefined, sessionId = undefined, + thVersion = undefined, connected, protocolServer, tcpTimeout, @@ -165,7 +168,7 @@ getProtocolClient protocolServer cfg@ProtocolClientConfig {qSize, tcpTimeout, tc `finally` atomically (putTMVar thVar $ Left PCENetworkError) th_ <- tcpTimeout `timeout` atomically (takeTMVar thVar) pure $ case th_ of - Just (Right THandle {sessionId}) -> Right c {action, sessionId} + Just (Right THandle {sessionId, thVersion}) -> Right c {action, sessionId, thVersion} Just (Left e) -> Left e Nothing -> Left PCENetworkError @@ -179,11 +182,11 @@ getProtocolClient protocolServer cfg@ProtocolClientConfig {qSize, tcpTimeout, tc client _ c thVar h = runExceptT (protocolClientHandshake @msg h $ keyHash protocolServer) >>= \case Left e -> atomically . putTMVar thVar . Left $ PCETransportError e - Right th@THandle {sessionId} -> do + Right th@THandle {sessionId, thVersion} -> do atomically $ do writeTVar (connected c) True putTMVar thVar $ Right th - let c' = c {sessionId} :: ProtocolClient msg + let c' = c {sessionId, thVersion} :: ProtocolClient msg -- TODO remove ping if 0 is passed (or Nothing?) raceAny_ [send c' th, process c', receive c' th, ping c'] `finally` disconnected @@ -305,9 +308,9 @@ enableSMPQueueNotifications c rpKey rId notifierKey = -- | Send SMP message. -- -- https://github.com/simplex-chat/simplexmq/blob/master/protocol/simplex-messaging.md#send-message -sendSMPMessage :: SMPClient -> Maybe SndPrivateSignKey -> SenderId -> MsgBody -> ExceptT ProtocolClientError IO () -sendSMPMessage c spKey sId msg = - sendSMPCommand c spKey sId (SEND msg) >>= \case +sendSMPMessage :: SMPClient -> Maybe SndPrivateSignKey -> SenderId -> MsgFlags -> MsgBody -> ExceptT ProtocolClientError IO () +sendSMPMessage c spKey sId flags msg = + sendSMPCommand c spKey sId (SEND flags msg) >>= \case OK -> pure () _ -> throwE PCEUnexpectedResponse @@ -347,9 +350,9 @@ sendSMPCommand c pKey qId cmd = sendProtocolCommand c pKey qId (Cmd sParty cmd) -- | Send Protocol command sendProtocolCommand :: forall msg. ProtocolEncoding (ProtocolCommand msg) => ProtocolClient msg -> Maybe C.APrivateSignKey -> QueueId -> ProtocolCommand msg -> ExceptT ProtocolClientError IO msg -sendProtocolCommand ProtocolClient {sndQ, sentCommands, clientCorrId, sessionId, tcpTimeout} pKey qId cmd = do +sendProtocolCommand ProtocolClient {sndQ, sentCommands, clientCorrId, sessionId, thVersion, tcpTimeout} pKey qId cmd = do corrId <- lift_ getNextCorrId - t <- signTransmission $ encodeTransmission sessionId (corrId, qId, cmd) + t <- signTransmission $ encodeTransmission thVersion sessionId (corrId, qId, cmd) ExceptT $ sendRecv corrId t where lift_ :: STM a -> ExceptT ProtocolClientError IO a diff --git a/src/Simplex/Messaging/Encoding.hs b/src/Simplex/Messaging/Encoding.hs index 6c7c56ddd..695065419 100644 --- a/src/Simplex/Messaging/Encoding.hs +++ b/src/Simplex/Messaging/Encoding.hs @@ -47,66 +47,100 @@ class Encoding a where instance Encoding Char where smpEncode = B.singleton + {-# INLINE smpEncode #-} smpP = A.anyChar + {-# INLINE smpP #-} + +instance Encoding Bool where + smpEncode = \case + True -> "T" + False -> "F" + {-# INLINE smpEncode #-} + smpP = + smpP >>= \case + 'T' -> pure True + 'F' -> pure False + _ -> fail "invalid Bool" + {-# INLINE smpP #-} instance Encoding Word16 where smpEncode = encodeWord16 + {-# INLINE smpEncode #-} smpP = decodeWord16 <$> A.take 2 + {-# INLINE smpP #-} instance Encoding Word32 where smpEncode = encodeWord32 + {-# INLINE smpEncode #-} smpP = decodeWord32 <$> A.take 4 + {-# INLINE smpP #-} instance Encoding Int64 where smpEncode i = w32 (i `shiftR` 32) <> w32 i + {-# INLINE smpEncode #-} smpP = do l <- w32P r <- w32P pure $ (l `shiftL` 32) .|. r + {-# INLINE smpP #-} w32 :: Int64 -> ByteString w32 = smpEncode @Word32 . fromIntegral +{-# INLINE w32 #-} w32P :: Parser Int64 w32P = fromIntegral <$> smpP @Word32 +{-# INLINE w32P #-} -- ByteStrings are assumed no longer than 255 bytes instance Encoding ByteString where smpEncode s = B.cons (lenEncode $ B.length s) s + {-# INLINE smpEncode #-} smpP = A.take =<< lenP + {-# INLINE smpP #-} lenEncode :: Int -> Char lenEncode = w2c . fromIntegral +{-# INLINE lenEncode #-} lenP :: Parser Int lenP = fromIntegral . c2w <$> A.anyChar +{-# INLINE lenP #-} instance Encoding a => Encoding (Maybe a) where smpEncode s = maybe "0" (("1" <>) . smpEncode) s + {-# INLINE smpEncode #-} smpP = smpP >>= \case '0' -> pure Nothing '1' -> Just <$> smpP _ -> fail "invalid Maybe tag" + {-# INLINE smpP #-} newtype Tail = Tail {unTail :: ByteString} instance Encoding Tail where smpEncode = unTail + {-# INLINE smpEncode #-} smpP = Tail <$> A.takeByteString + {-# INLINE smpP #-} -- newtype for encoding/decoding ByteStrings over 255 bytes with 2-bytes length prefix newtype Large = Large {unLarge :: ByteString} instance Encoding Large where smpEncode (Large s) = smpEncode @Word16 (fromIntegral $ B.length s) <> s + {-# INLINE smpEncode #-} smpP = do len <- fromIntegral <$> smpP @Word16 Large <$> A.take len + {-# INLINE smpP #-} instance Encoding SystemTime where smpEncode = smpEncode . systemSeconds + {-# INLINE smpEncode #-} smpP = MkSystemTime <$> smpP <*> pure 0 + {-# INLINE smpP #-} -- lists encode/parse as a sequence of items prefixed with list length (as 1 byte) smpEncodeList :: Encoding a => [a] -> ByteString @@ -117,7 +151,9 @@ smpListP = (`A.count` smpP) =<< lenP instance Encoding String where smpEncode = smpEncode . B.pack + {-# INLINE smpEncode #-} smpP = B.unpack <$> smpP + {-# INLINE smpP #-} instance Encoding a => Encoding (L.NonEmpty a) where smpEncode = smpEncodeList . L.toList @@ -128,20 +164,36 @@ instance Encoding a => Encoding (L.NonEmpty a) where instance (Encoding a, Encoding b) => Encoding (a, b) where smpEncode (a, b) = smpEncode a <> smpEncode b + {-# INLINE smpEncode #-} smpP = (,) <$> smpP <*> smpP + {-# INLINE smpP #-} instance (Encoding a, Encoding b, Encoding c) => Encoding (a, b, c) where smpEncode (a, b, c) = smpEncode a <> smpEncode b <> smpEncode c + {-# INLINE smpEncode #-} smpP = (,,) <$> smpP <*> smpP <*> smpP + {-# INLINE smpP #-} instance (Encoding a, Encoding b, Encoding c, Encoding d) => Encoding (a, b, c, d) where smpEncode (a, b, c, d) = smpEncode a <> smpEncode b <> smpEncode c <> smpEncode d + {-# INLINE smpEncode #-} smpP = (,,,) <$> smpP <*> smpP <*> smpP <*> smpP + {-# INLINE smpP #-} instance (Encoding a, Encoding b, Encoding c, Encoding d, Encoding e) => Encoding (a, b, c, d, e) where smpEncode (a, b, c, d, e) = smpEncode a <> smpEncode b <> smpEncode c <> smpEncode d <> smpEncode e + {-# INLINE smpEncode #-} smpP = (,,,,) <$> smpP <*> smpP <*> smpP <*> smpP <*> smpP + {-# INLINE smpP #-} instance (Encoding a, Encoding b, Encoding c, Encoding d, Encoding e, Encoding f) => Encoding (a, b, c, d, e, f) where smpEncode (a, b, c, d, e, f) = smpEncode a <> smpEncode b <> smpEncode c <> smpEncode d <> smpEncode e <> smpEncode f + {-# INLINE smpEncode #-} smpP = (,,,,,) <$> smpP <*> smpP <*> smpP <*> smpP <*> smpP <*> smpP + {-# INLINE smpP #-} + +instance (Encoding a, Encoding b, Encoding c, Encoding d, Encoding e, Encoding f, Encoding g) => Encoding (a, b, c, d, e, f, g) where + smpEncode (a, b, c, d, e, f, g) = smpEncode a <> smpEncode b <> smpEncode c <> smpEncode d <> smpEncode e <> smpEncode f <> smpEncode g + {-# INLINE smpEncode #-} + smpP = (,,,,,,) <$> smpP <*> smpP <*> smpP <*> smpP <*> smpP <*> smpP <*> smpP + {-# INLINE smpP #-} diff --git a/src/Simplex/Messaging/Encoding/String.hs b/src/Simplex/Messaging/Encoding/String.hs index 52b26ef1d..d61d1a558 100644 --- a/src/Simplex/Messaging/Encoding/String.hs +++ b/src/Simplex/Messaging/Encoding/String.hs @@ -30,6 +30,7 @@ import qualified Data.List.NonEmpty as L import Data.Text (Text) import Data.Text.Encoding (decodeLatin1, encodeUtf8) import Data.Word (Word16) +import Simplex.Messaging.Encoding import Simplex.Messaging.Parsers (parseAll) import Simplex.Messaging.Util ((<$?>)) @@ -76,11 +77,27 @@ instance FromJSON Str where instance StrEncoding a => StrEncoding (Maybe a) where strEncode = maybe "" strEncode + {-# INLINE strEncode #-} strP = optional strP + {-# INLINE strP #-} instance StrEncoding Word16 where strEncode = B.pack . show + {-# INLINE strEncode #-} strP = A.decimal + {-# INLINE strP #-} + +instance StrEncoding Char where + strEncode = smpEncode + {-# INLINE strEncode #-} + strP = strP + {-# INLINE strP #-} + +instance StrEncoding Bool where + strEncode = smpEncode + {-# INLINE strEncode #-} + strP = smpP + {-# INLINE strP #-} -- lists encode/parse as comma-separated strings strEncodeList :: StrEncoding a => [a] -> ByteString @@ -99,19 +116,27 @@ listItem = parseAll strP <$?> A.takeTill (== ',') instance (StrEncoding a, StrEncoding b) => StrEncoding (a, b) where strEncode (a, b) = B.unwords [strEncode a, strEncode b] + {-# INLINE strEncode #-} strP = (,) <$> strP_ <*> strP + {-# INLINE strP #-} instance (StrEncoding a, StrEncoding b, StrEncoding c) => StrEncoding (a, b, c) where strEncode (a, b, c) = B.unwords [strEncode a, strEncode b, strEncode c] + {-# INLINE strEncode #-} strP = (,,) <$> strP_ <*> strP_ <*> strP + {-# INLINE strP #-} instance (StrEncoding a, StrEncoding b, StrEncoding c, StrEncoding d) => StrEncoding (a, b, c, d) where strEncode (a, b, c, d) = B.unwords [strEncode a, strEncode b, strEncode c, strEncode d] + {-# INLINE strEncode #-} strP = (,,,) <$> strP_ <*> strP_ <*> strP_ <*> strP + {-# INLINE strP #-} instance (StrEncoding a, StrEncoding b, StrEncoding c, StrEncoding d, StrEncoding e) => StrEncoding (a, b, c, d, e) where strEncode (a, b, c, d, e) = B.unwords [strEncode a, strEncode b, strEncode c, strEncode d, strEncode e] + {-# INLINE strEncode #-} strP = (,,,,) <$> strP_ <*> strP_ <*> strP_ <*> strP_ <*> strP + {-# INLINE strP #-} strP_ :: StrEncoding a => Parser a strP_ = strP <* A.space diff --git a/src/Simplex/Messaging/Notifications/Protocol.hs b/src/Simplex/Messaging/Notifications/Protocol.hs index 4ff643136..f997c3c44 100644 --- a/src/Simplex/Messaging/Notifications/Protocol.hs +++ b/src/Simplex/Messaging/Notifications/Protocol.hs @@ -176,7 +176,7 @@ deriving instance Show NtfCmd instance NtfEntityI e => ProtocolEncoding (NtfCommand e) where type Tag (NtfCommand e) = NtfCommandTag e - encodeProtocol = \case + encodeProtocol _v = \case TNEW newTkn -> e (TNEW_, ' ', newTkn) TVFY code -> e (TVFY_, ' ', code) TCHK -> e TCHK_ @@ -190,7 +190,7 @@ instance NtfEntityI e => ProtocolEncoding (NtfCommand e) where e :: Encoding a => a -> ByteString e = smpEncode - protocolP tag = (\(NtfCmd _ c) -> checkEntity c) <$?> protocolP (NCT (sNtfEntity @e) tag) + protocolP _v tag = (\(NtfCmd _ c) -> checkEntity c) <$?> protocolP _v (NCT (sNtfEntity @e) tag) checkCredentials (sig, _, entityId, _) cmd = case cmd of -- TNEW and SNEW must have signature but NOT token/subscription IDs @@ -211,9 +211,9 @@ instance NtfEntityI e => ProtocolEncoding (NtfCommand e) where instance ProtocolEncoding NtfCmd where type Tag NtfCmd = NtfCmdTag - encodeProtocol (NtfCmd _ c) = encodeProtocol c + encodeProtocol _v (NtfCmd _ c) = encodeProtocol _v c - protocolP = \case + protocolP _v = \case NCT SToken tag -> NtfCmd SToken <$> case tag of TNEW_ -> TNEW <$> _smpP @@ -274,7 +274,7 @@ data NtfResponse instance ProtocolEncoding NtfResponse where type Tag NtfResponse = NtfResponseTag - encodeProtocol = \case + encodeProtocol _v = \case NRTknId entId dhKey -> e (NRTknId_, ' ', entId, dhKey) NRSubId entId -> e (NRSubId_, ' ', entId) NROk -> e NROk_ @@ -286,7 +286,7 @@ instance ProtocolEncoding NtfResponse where e :: Encoding a => a -> ByteString e = smpEncode - protocolP = \case + protocolP _v = \case NRTknId_ -> NRTknId <$> _smpP <*> smpP NRSubId_ -> NRSubId <$> _smpP NROk_ -> pure NROk diff --git a/src/Simplex/Messaging/Notifications/Server.hs b/src/Simplex/Messaging/Notifications/Server.hs index 125a8e5d2..e1011b290 100644 --- a/src/Simplex/Messaging/Notifications/Server.hs +++ b/src/Simplex/Messaging/Notifications/Server.hs @@ -189,9 +189,9 @@ receive th NtfServerClient {rcvQ, sndQ, activeAt} = forever $ do write q t = atomically $ writeTBQueue q t send :: (Transport c, MonadUnliftIO m) => THandle c -> NtfServerClient -> m () -send h NtfServerClient {sndQ, sessionId, activeAt} = forever $ do +send h@THandle {thVersion = v} NtfServerClient {sndQ, sessionId, activeAt} = forever $ do t <- atomically $ readTBQueue sndQ - void . liftIO $ tPut h (Nothing, encodeTransmission sessionId t) + void . liftIO $ tPut h (Nothing, encodeTransmission v sessionId t) atomically . writeTVar activeAt =<< liftIO getSystemTime data VerificationResult = VRVerified NtfRequest | VRFailed diff --git a/src/Simplex/Messaging/Notifications/Server/Push/testpush.sh b/src/Simplex/Messaging/Notifications/Server/Push/testpush.sh index b41135d83..bac5d8684 100755 --- a/src/Simplex/Messaging/Notifications/Server/Push/testpush.sh +++ b/src/Simplex/Messaging/Notifications/Server/Push/testpush.sh @@ -22,4 +22,5 @@ export AUTHENTICATION_TOKEN="${JWT_HEADER}.${JWT_CLAIMS}.${JWT_SIGNED_HEADER_CLA # curl -v --header "apns-topic: $TOPIC" --header "apns-push-type: background" --header "apns-priority: 5" --header "authorization: bearer $AUTHENTICATION_TOKEN" --data '{"aps":{"content-available":1}}' --http2 https://${APNS_HOST_NAME}/3/device/${DEVICE_TOKEN} # mutable-content notification -curl -v --header "apns-topic: $TOPIC" --header "apns-push-type: alert" --header "authorization: bearer $AUTHENTICATION_TOKEN" --data '{"aps":{"category": "NTF_CAT_CHECK_MESSAGE__SECRET", "mutable-content": 1, "alert":"received encrypted message"}, "data": {"test":"123"}}' --http2 https://${APNS_HOST_NAME}/3/device/${DEVICE_TOKEN} \ No newline at end of file +# NTF_CAT_CHECK_MESSAGE category will not show alert if the app is in foreground +curl -v --header "apns-topic: $TOPIC" --header "apns-push-type: alert" --header "authorization: bearer $AUTHENTICATION_TOKEN" --data '{"aps":{"category": "NTF_CAT_CHECK_MESSAGE__SECRET", "mutable-content": 1, "alert":"received encrypted message"}, "data": {"test":"123"}}' --http2 https://${APNS_HOST_NAME}/3/device/${DEVICE_TOKEN} diff --git a/src/Simplex/Messaging/Protocol.hs b/src/Simplex/Messaging/Protocol.hs index dde5992b3..2503bb9f2 100644 --- a/src/Simplex/Messaging/Protocol.hs +++ b/src/Simplex/Messaging/Protocol.hs @@ -78,6 +78,8 @@ module Simplex.Messaging.Protocol NtfPublicVerifyKey, MsgId, MsgBody, + MsgFlags (..), + noMsgFlags, -- * Parse and serialize ProtocolMsgTag (..), @@ -214,11 +216,14 @@ data Command (p :: Party) where SUB :: Command Recipient KEY :: SndPublicVerifyKey -> Command Recipient NKEY :: NtfPublicVerifyKey -> Command Recipient + GET :: Command Recipient ACK :: Command Recipient OFF :: Command Recipient DEL :: Command Recipient -- SMP sender commands - SEND :: MsgBody -> Command Sender + -- SEND v1 has to be supported for encoding/decoding + -- SEND :: MsgBody -> Command Sender + SEND :: MsgFlags -> MsgBody -> Command Sender PING :: Command Sender -- SMP notification subscriber commands NSUB :: Command Notifier @@ -230,7 +235,9 @@ deriving instance Eq (Command p) data BrokerMsg where -- SMP broker messages (responses, client messages, notifications) IDS :: QueueIdsKeys -> BrokerMsg - MSG :: MsgId -> SystemTime -> MsgBody -> BrokerMsg + -- MSG v1 has to be supported for encoding/decoding + -- MSG :: MsgId -> SystemTime -> MsgBody -> BrokerMsg + MSG :: MsgId -> SystemTime -> MsgFlags -> MsgBody -> BrokerMsg NID :: NotifierId -> BrokerMsg NMSG :: BrokerMsg END :: BrokerMsg @@ -239,6 +246,18 @@ data BrokerMsg where PONG :: BrokerMsg deriving (Eq, Show) +newtype MsgFlags = MsgFlags {notification :: Bool} + deriving (Eq, Show) + +instance Encoding MsgFlags where + smpEncode MsgFlags {notification} = smpEncode notification + smpP = do + notification <- smpP <* A.takeTill (== ' ') + pure MsgFlags {notification} + +noMsgFlags :: MsgFlags +noMsgFlags = MsgFlags {notification = False} + -- * SMP command tags data CommandTag (p :: Party) where @@ -246,6 +265,7 @@ data CommandTag (p :: Party) where SUB_ :: CommandTag Recipient KEY_ :: CommandTag Recipient NKEY_ :: CommandTag Recipient + GET_ :: CommandTag Recipient ACK_ :: CommandTag Recipient OFF_ :: CommandTag Recipient DEL_ :: CommandTag Recipient @@ -284,6 +304,7 @@ instance PartyI p => Encoding (CommandTag p) where SUB_ -> "SUB" KEY_ -> "KEY" NKEY_ -> "NKEY" + GET_ -> "GET" ACK_ -> "ACK" OFF_ -> "OFF" DEL_ -> "DEL" @@ -298,6 +319,7 @@ instance ProtocolMsgTag CmdTag where "SUB" -> Just $ CT SRecipient SUB_ "KEY" -> Just $ CT SRecipient KEY_ "NKEY" -> Just $ CT SRecipient NKEY_ + "GET" -> Just $ CT SRecipient GET_ "ACK" -> Just $ CT SRecipient ACK_ "OFF" -> Just $ CT SRecipient OFF_ "DEL" -> Just $ CT SRecipient DEL_ @@ -526,6 +548,8 @@ data CommandError UNKNOWN | -- | error parsing command SYNTAX + | -- | command is not allowed (SUB/GET cannot be used with the same queue in the same TCP connection) + PROHIBITED | -- | transmission has no required credentials (signature or queue ID) NO_AUTH | -- | transmission has credentials that are not allowed for this command @@ -572,28 +596,31 @@ instance Protocol BrokerMsg where class ProtocolMsgTag (Tag msg) => ProtocolEncoding msg where type Tag msg - encodeProtocol :: msg -> ByteString - protocolP :: Tag msg -> Parser msg + encodeProtocol :: Version -> msg -> ByteString + protocolP :: Version -> Tag msg -> Parser msg checkCredentials :: SignedRawTransmission -> msg -> Either ErrorType msg instance PartyI p => ProtocolEncoding (Command p) where type Tag (Command p) = CommandTag p - encodeProtocol = \case + encodeProtocol v = \case NEW rKey dhKey -> e (NEW_, ' ', rKey, dhKey) SUB -> e SUB_ KEY k -> e (KEY_, ' ', k) NKEY k -> e (NKEY_, ' ', k) + GET -> e GET_ ACK -> e ACK_ OFF -> e OFF_ DEL -> e DEL_ - SEND msg -> e (SEND_, ' ', Tail msg) + SEND flags msg + | v == 1 -> e (SEND_, ' ', Tail msg) + | otherwise -> e (SEND_, ' ', flags, ' ', Tail msg) PING -> e PING_ NSUB -> e NSUB_ where e :: Encoding a => a -> ByteString e = smpEncode - protocolP tag = (\(Cmd _ c) -> checkParty c) <$?> protocolP (CT (sParty @p) tag) + protocolP v tag = (\(Cmd _ c) -> checkParty c) <$?> protocolP v (CT (sParty @p) tag) checkCredentials (sig, _, queueId, _) cmd = case cmd of -- NEW must have signature but NOT queue ID @@ -602,7 +629,7 @@ instance PartyI p => ProtocolEncoding (Command p) where | not (B.null queueId) -> Left $ CMD HAS_AUTH | otherwise -> Right cmd -- SEND must have queue ID, signature is not always required - SEND _ + SEND {} | B.null queueId -> Left $ CMD NO_ENTITY | otherwise -> Right cmd -- PING must not have queue ID or signature @@ -616,21 +643,24 @@ instance PartyI p => ProtocolEncoding (Command p) where instance ProtocolEncoding Cmd where type Tag Cmd = CmdTag - encodeProtocol (Cmd _ c) = encodeProtocol c + encodeProtocol v (Cmd _ c) = encodeProtocol v c - protocolP = \case + protocolP v = \case CT SRecipient tag -> Cmd SRecipient <$> case tag of NEW_ -> NEW <$> _smpP <*> smpP SUB_ -> pure SUB KEY_ -> KEY <$> _smpP NKEY_ -> NKEY <$> _smpP + GET_ -> pure GET ACK_ -> pure ACK OFF_ -> pure OFF DEL_ -> pure DEL CT SSender tag -> Cmd SSender <$> case tag of - SEND_ -> SEND . unTail <$> _smpP + SEND_ + | v == 1 -> SEND <$> pure noMsgFlags <*> (unTail <$> _smpP) + | otherwise -> SEND <$> _smpP <*> (unTail <$> _smpP) PING_ -> pure PING CT SNotifier NSUB_ -> pure $ Cmd SNotifier NSUB @@ -638,9 +668,11 @@ instance ProtocolEncoding Cmd where instance ProtocolEncoding BrokerMsg where type Tag BrokerMsg = BrokerMsgTag - encodeProtocol = \case + encodeProtocol v = \case IDS (QIK rcvId sndId srvDh) -> e (IDS_, ' ', rcvId, sndId, srvDh) - MSG msgId ts msgBody -> e (MSG_, ' ', msgId, ts, Tail msgBody) + MSG msgId ts flags msgBody + | v == 1 -> e (MSG_, ' ', msgId, ts, Tail msgBody) + | otherwise -> e (MSG_, ' ', msgId, ts, flags, ' ', Tail msgBody) NID nId -> e (NID_, ' ', nId) NMSG -> e NMSG_ END -> e END_ @@ -651,8 +683,10 @@ instance ProtocolEncoding BrokerMsg where e :: Encoding a => a -> ByteString e = smpEncode - protocolP = \case - MSG_ -> MSG <$> _smpP <*> smpP <*> (unTail <$> smpP) + protocolP v = \case + MSG_ + | v == 1 -> MSG <$> _smpP <*> smpP <*> pure noMsgFlags <*> (unTail <$> smpP) + | otherwise -> MSG <$> _smpP <*> smpP <*> smpP <*> (unTail <$> _smpP) IDS_ -> IDS <$> (QIK <$> _smpP <*> smpP <*> smpP) NID_ -> NID <$> _smpP NMSG_ -> pure NMSG @@ -679,11 +713,11 @@ _smpP :: Encoding a => Parser a _smpP = A.space *> smpP -- | Parse SMP protocol commands and broker messages -parseProtocol :: ProtocolEncoding msg => ByteString -> Either ErrorType msg -parseProtocol s = +parseProtocol :: ProtocolEncoding msg => Version -> ByteString -> Either ErrorType msg +parseProtocol v s = let (tag, params) = B.break (== ' ') s in case decodeTag tag of - Just cmd -> parse (protocolP cmd) (CMD SYNTAX) params + Just cmd -> parse (protocolP v cmd) (CMD SYNTAX) params Nothing -> Left $ CMD UNKNOWN checkParty :: forall t p p'. (PartyI p, PartyI p') => t p' -> Either String (t p) @@ -725,6 +759,7 @@ instance Encoding CommandError where smpEncode e = case e of UNKNOWN -> "UNKNOWN" SYNTAX -> "SYNTAX" + PROHIBITED -> "PROHIBITED" NO_AUTH -> "NO_AUTH" HAS_AUTH -> "HAS_AUTH" NO_ENTITY -> "NO_ENTITY" @@ -732,6 +767,7 @@ instance Encoding CommandError where A.takeTill (== ' ') >>= \case "UNKNOWN" -> pure UNKNOWN "SYNTAX" -> pure SYNTAX + "PROHIBITED" -> pure PROHIBITED "NO_AUTH" -> pure NO_AUTH "HAS_AUTH" -> pure HAS_AUTH "NO_ENTITY" -> pure NO_ENTITY @@ -742,9 +778,9 @@ instance Encoding CommandError where tPut :: Transport c => THandle c -> SentRawTransmission -> IO (Either TransportError ()) tPut th (sig, t) = tPutBlock th $ smpEncode (C.signatureBytes sig) <> t -encodeTransmission :: ProtocolEncoding c => ByteString -> Transmission c -> ByteString -encodeTransmission sessionId (CorrId corrId, queueId, command) = - smpEncode (sessionId, corrId, queueId) <> encodeProtocol command +encodeTransmission :: ProtocolEncoding c => Version -> ByteString -> Transmission c -> ByteString +encodeTransmission v sessionId (CorrId corrId, queueId, command) = + smpEncode (sessionId, corrId, queueId) <> encodeProtocol v command -- | Receive and parse transmission from the TCP transport (ignoring any trailing padding). tGetParse :: Transport c => THandle c -> IO (Either TransportError RawTransmission) @@ -752,7 +788,7 @@ tGetParse th = (parse transmissionP TEBadBlock =<<) <$> tGetBlock th -- | Receive client and server transmissions (determined by `cmd` type). tGet :: forall cmd c m. (ProtocolEncoding cmd, Transport c, MonadIO m) => THandle c -> m (SignedTransmission cmd) -tGet th@THandle {sessionId} = liftIO (tGetParse th) >>= decodeParseValidate +tGet th@THandle {sessionId, thVersion = v} = liftIO (tGetParse th) >>= decodeParseValidate where decodeParseValidate :: Either TransportError RawTransmission -> m (SignedTransmission cmd) decodeParseValidate = \case @@ -768,5 +804,5 @@ tGet th@THandle {sessionId} = liftIO (tGetParse th) >>= decodeParseValidate tParseValidate :: ByteString -> SignedRawTransmission -> m (SignedTransmission cmd) tParseValidate signed t@(sig, corrId, entityId, command) = do - let cmd = parseProtocol command >>= checkCredentials t + let cmd = parseProtocol v command >>= checkCredentials t pure (sig, signed, (CorrId corrId, entityId, cmd)) diff --git a/src/Simplex/Messaging/Server.hs b/src/Simplex/Messaging/Server.hs index aac1db5ee..bda7261ad 100644 --- a/src/Simplex/Messaging/Server.hs +++ b/src/Simplex/Messaging/Server.hs @@ -227,10 +227,10 @@ receive th Client {rcvQ, sndQ, activeAt} = forever $ do write q t = atomically $ writeTBQueue q t send :: (Transport c, MonadUnliftIO m) => THandle c -> Client -> m () -send h Client {sndQ, sessionId, activeAt} = forever $ do +send h@THandle {thVersion = v} Client {sndQ, sessionId, activeAt} = forever $ do t <- atomically $ readTBQueue sndQ -- TODO the line below can return Left, but we ignore it and do not disconnect the client - void . liftIO $ tPut h (Nothing, encodeTransmission sessionId t) + void . liftIO $ tPut h (Nothing, encodeTransmission v sessionId t) atomically . writeTVar activeAt =<< liftIO getSystemTime disconnectTransport :: (Transport c, MonadUnliftIO m) => THandle c -> client -> (client -> TVar SystemTime) -> ExpirationConfig -> m () @@ -248,7 +248,7 @@ verifyTransmission sig_ signed queueId cmd = do case cmd of Cmd SRecipient (NEW k _) -> pure $ verifyCmdSignature sig_ signed k Cmd SRecipient _ -> verifyCmd SRecipient $ verifyCmdSignature sig_ signed . recipientKey - Cmd SSender (SEND _) -> verifyCmd SSender $ verifyMaybe . senderKey + Cmd SSender SEND {} -> verifyCmd SSender $ verifyMaybe . senderKey Cmd SSender PING -> pure True Cmd SNotifier NSUB -> verifyCmd SNotifier $ verifyMaybe . fmap snd . notifier where @@ -298,7 +298,7 @@ client clnt@Client {subscriptions, ntfSubscriptions, rcvQ, sndQ} Server {subscri case cmd of Cmd SSender command -> case command of - SEND msgBody -> sendMessage st msgBody + SEND flags msgBody -> sendMessage st flags msgBody PING -> pure (corrId, "", PONG) Cmd SNotifier NSUB -> subscribeNotifications Cmd SRecipient command -> @@ -309,6 +309,7 @@ client clnt@Client {subscriptions, ntfSubscriptions, rcvQ, sndQ} Server {subscri (createQueue st rKey dhKey) (pure (corrId, queueId, ERR AUTH)) SUB -> subscribeQueue queueId + GET -> getMessage ACK -> acknowledgeMsg KEY sKey -> secureQueue_ st sKey NKEY nKey -> addQueueNotifier_ st nKey @@ -386,17 +387,42 @@ client clnt@Client {subscriptions, ntfSubscriptions, rcvQ, sndQ} Server {subscri subscribeQueue :: RecipientId -> m (Transmission BrokerMsg) subscribeQueue rId = - atomically (getSubscription rId) >>= deliverMessage tryPeekMsg rId + atomically (getSubscription rId) >>= \case + Just s -> deliverMessage tryPeekMsg rId s + -- cannot use SUB in the same connection where GET was used + _ -> pure (corrId, rId, ERR $ CMD PROHIBITED) - getSubscription :: RecipientId -> STM Sub + getSubscription :: RecipientId -> STM (Maybe Sub) getSubscription rId = do TM.lookup rId subscriptions >>= \case - Just s -> tryTakeTMVar (delivered s) $> s + Just Sub {subThread = ProhibitSub} -> pure Nothing + Just s -> tryTakeTMVar (delivered s) $> Just s Nothing -> do writeTBQueue subscribedQ (rId, clnt) s <- newSubscription TM.insert rId s subscriptions - return s + pure $ Just s + + getMessage :: m (Transmission BrokerMsg) + getMessage = + atomically getProhibitedSub >>= \case + Just s -> do + q <- getStoreMsgQueue queueId + atomically $ + tryPeekMsg q >>= \case + Just msg -> tryPutTMVar (delivered s) () $> (corrId, queueId, msgCmd msg) + _ -> pure (corrId, queueId, ERR NO_MSG) + _ -> pure (corrId, queueId, ERR $ CMD PROHIBITED) -- cannot use GET in the same connection where there is an active subscription + where + getProhibitedSub :: STM (Maybe Sub) + getProhibitedSub = + TM.lookup queueId subscriptions >>= \case + Just s@Sub {subThread = ProhibitSub} -> tryTakeTMVar (delivered s) $> Just s + Just _ -> pure Nothing + Nothing -> do + s <- prohibitedSubscription + TM.insert queueId s subscriptions + pure $ Just s subscribeNotifications :: m (Transmission BrokerMsg) subscribeNotifications = atomically $ do @@ -413,14 +439,18 @@ client clnt@Client {subscriptions, ntfSubscriptions, rcvQ, sndQ} Server {subscri stats <- asks serverStats atomically $ modifyTVar (msgRecv stats) (+ 1) atomically $ modifyTVar (msgQueues stats) (S.insert queueId) - deliverMessage tryDelPeekMsg queueId s + case s of + Sub {subThread = ProhibitSub} -> + (getStoreMsgQueue queueId >>= atomically . tryDelMsg) $> ok + _ -> + deliverMessage tryDelPeekMsg queueId s _ -> return $ err NO_MSG withSub :: RecipientId -> (Sub -> STM a) -> STM (Maybe a) withSub rId f = mapM f =<< TM.lookup rId subscriptions - sendMessage :: QueueStore -> MsgBody -> m (Transmission BrokerMsg) - sendMessage st msgBody + sendMessage :: QueueStore -> MsgFlags -> MsgBody -> m (Transmission BrokerMsg) + sendMessage st flags msgBody | B.length msgBody > maxMessageLength = pure $ err LARGE_MSG | otherwise = do qr <- atomically $ getQueue st SSender queueId @@ -454,7 +484,7 @@ client clnt@Client {subscriptions, ntfSubscriptions, rcvQ, sndQ} Server {subscri msgId <- randomId =<< asks (msgIdBytes . config) ts <- liftIO getSystemTime let c = C.cbEncrypt (rcvDhSecret qr) (C.cbNonce msgId) msgBody (maxMessageLength + 2) - pure $ Message msgId ts <$> c + pure $ Message msgId ts flags <$> c trySendNotification :: STM () trySendNotification = @@ -469,9 +499,7 @@ client clnt@Client {subscriptions, ntfSubscriptions, rcvQ, sndQ} Server {subscri deliverMessage :: (MsgQueue -> STM (Maybe Message)) -> RecipientId -> Sub -> m (Transmission BrokerMsg) deliverMessage tryPeek rId = \case Sub {subThread = NoSub} -> do - ms <- asks msgStore - quota <- asks $ msgQueueQuota . config - q <- atomically $ getMsgQueue ms rId quota + q <- getStoreMsgQueue rId atomically (tryPeek q) >>= \case Nothing -> forkSub q $> ok Just msg -> atomically setDelivered $> (corrId, rId, msgCmd msg) @@ -498,8 +526,14 @@ client clnt@Client {subscriptions, ntfSubscriptions, rcvQ, sndQ} Server {subscri setDelivered :: STM (Maybe Bool) setDelivered = withSub rId $ \s -> tryPutTMVar (delivered s) () - msgCmd :: Message -> BrokerMsg - msgCmd Message {msgId, ts, msgBody} = MSG msgId ts msgBody + getStoreMsgQueue :: RecipientId -> m MsgQueue + getStoreMsgQueue rId = do + ms <- asks msgStore + quota <- asks $ msgQueueQuota . config + atomically $ getMsgQueue ms rId quota + + msgCmd :: Message -> BrokerMsg + msgCmd Message {msgId, ts, msgFlags, msgBody} = MSG msgId ts msgFlags msgBody delQueueAndMsgs :: QueueStore -> m (Transmission BrokerMsg) delQueueAndMsgs st = do diff --git a/src/Simplex/Messaging/Server/Env/STM.hs b/src/Simplex/Messaging/Server/Env/STM.hs index d70fe203f..3bec11ee4 100644 --- a/src/Simplex/Messaging/Server/Env/STM.hs +++ b/src/Simplex/Messaging/Server/Env/STM.hs @@ -113,7 +113,7 @@ data ServerStats = ServerStats fromTime :: TVar UTCTime } -data SubscriptionThread = NoSub | SubPending | SubThread ThreadId +data SubscriptionThread = NoSub | SubPending | SubThread ThreadId | ProhibitSub data Sub = Sub { subThread :: SubscriptionThread, @@ -150,9 +150,15 @@ newServerStats ts = do pure ServerStats {qCreated, qSecured, qDeleted, msgSent, msgRecv, msgQueues, fromTime} newSubscription :: STM Sub -newSubscription = do +newSubscription = newSubscription_ NoSub + +prohibitedSubscription :: STM Sub +prohibitedSubscription = newSubscription_ ProhibitSub + +newSubscription_ :: SubscriptionThread -> STM Sub +newSubscription_ subThread = do delivered <- newEmptyTMVar - return Sub {subThread = NoSub, delivered} + return Sub {subThread, delivered} newEnv :: forall m. (MonadUnliftIO m, MonadRandom m) => ServerConfig -> m Env newEnv config@ServerConfig {caCertificateFile, certificateFile, privateKeyFile, storeLogFile} = do diff --git a/src/Simplex/Messaging/Server/MsgStore.hs b/src/Simplex/Messaging/Server/MsgStore.hs index a7180311f..c4f36acb5 100644 --- a/src/Simplex/Messaging/Server/MsgStore.hs +++ b/src/Simplex/Messaging/Server/MsgStore.hs @@ -5,11 +5,12 @@ module Simplex.Messaging.Server.MsgStore where import Data.Int (Int64) import Data.Time.Clock.System (SystemTime) import Numeric.Natural -import Simplex.Messaging.Protocol (MsgBody, MsgId, RecipientId) +import Simplex.Messaging.Protocol (MsgBody, MsgFlags, MsgId, RecipientId) data Message = Message { msgId :: MsgId, ts :: SystemTime, + msgFlags :: MsgFlags, msgBody :: MsgBody } @@ -22,5 +23,6 @@ class MonadMsgQueue q m where writeMsg :: q -> Message -> m () -- non blocking tryPeekMsg :: q -> m (Maybe Message) -- non blocking peekMsg :: q -> m Message -- blocking + tryDelMsg :: q -> m () -- non blocking tryDelPeekMsg :: q -> m (Maybe Message) -- atomic delete (== read) last and peek next message, if available deleteExpiredMsgs :: q -> Int64 -> m () diff --git a/src/Simplex/Messaging/Server/MsgStore/STM.hs b/src/Simplex/Messaging/Server/MsgStore/STM.hs index be395f918..06048b8e1 100644 --- a/src/Simplex/Messaging/Server/MsgStore/STM.hs +++ b/src/Simplex/Messaging/Server/MsgStore/STM.hs @@ -7,7 +7,7 @@ module Simplex.Messaging.Server.MsgStore.STM where -import Control.Monad (when) +import Control.Monad (void, when) import Data.Int (Int64) import Data.Time.Clock.System (SystemTime (systemSeconds)) import Numeric.Natural @@ -49,6 +49,9 @@ instance MonadMsgQueue MsgQueue STM where peekMsg :: MsgQueue -> STM Message peekMsg = peekTBQueue . msgQueue + tryDelMsg :: MsgQueue -> STM () + tryDelMsg = void . tryReadTBQueue . msgQueue + -- atomic delete (== read) last and peek next message if available tryDelPeekMsg :: MsgQueue -> STM (Maybe Message) tryDelPeekMsg (MsgQueue q) = tryReadTBQueue q >> tryPeekTBQueue q diff --git a/src/Simplex/Messaging/Transport.hs b/src/Simplex/Messaging/Transport.hs index 9f58e5441..ac7f34c9a 100644 --- a/src/Simplex/Messaging/Transport.hs +++ b/src/Simplex/Messaging/Transport.hs @@ -93,7 +93,7 @@ smpBlockSize :: Int smpBlockSize = 16384 supportedSMPVersions :: VersionRange -supportedSMPVersions = mkVersionRange 1 1 +supportedSMPVersions = mkVersionRange 1 2 simplexMQVersion :: String simplexMQVersion = "2.1.1" diff --git a/src/Simplex/Messaging/Util.hs b/src/Simplex/Messaging/Util.hs index 24291773b..8940f239b 100644 --- a/src/Simplex/Messaging/Util.hs +++ b/src/Simplex/Messaging/Util.hs @@ -78,3 +78,7 @@ catchAll = E.catch catchAll_ :: IO a -> IO a -> IO a catchAll_ a = catchAll a . const {-# INLINE catchAll_ #-} + +eitherToMaybe :: Either a b -> Maybe b +eitherToMaybe = either (const Nothing) Just +{-# INLINE eitherToMaybe #-} diff --git a/tests/AgentTests.hs b/tests/AgentTests.hs index 36cfc30fb..318b9e9de 100644 --- a/tests/AgentTests.hs +++ b/tests/AgentTests.hs @@ -117,7 +117,7 @@ h #:# err = tryGet `shouldReturn` () _ -> return () pattern Msg :: MsgBody -> ACommand 'Agent -pattern Msg msgBody <- MSG MsgMeta {integrity = MsgOk} msgBody +pattern Msg msgBody <- MSG MsgMeta {integrity = MsgOk} _ msgBody testDuplexConnection :: Transport c => TProxy c -> c -> c -> IO () testDuplexConnection _ alice bob = do @@ -130,24 +130,24 @@ testDuplexConnection _ alice bob = do bob <# ("", "alice", CON) alice <# ("", "bob", CON) -- message IDs 1 to 3 get assigned to control messages, so first MSG is assigned ID 4 - alice #: ("3", "bob", "SEND :hello") #> ("3", "bob", MID 5) + alice #: ("3", "bob", "SEND F :hello") #> ("3", "bob", MID 5) alice <# ("", "bob", SENT 5) bob <#= \case ("", "alice", Msg "hello") -> True; _ -> False bob #: ("12", "alice", "ACK 5") #> ("12", "alice", OK) - alice #: ("4", "bob", "SEND :how are you?") #> ("4", "bob", MID 6) + alice #: ("4", "bob", "SEND F :how are you?") #> ("4", "bob", MID 6) alice <# ("", "bob", SENT 6) bob <#= \case ("", "alice", Msg "how are you?") -> True; _ -> False bob #: ("13", "alice", "ACK 6") #> ("13", "alice", OK) - bob #: ("14", "alice", "SEND 9\nhello too") #> ("14", "alice", MID 7) + bob #: ("14", "alice", "SEND F 9\nhello too") #> ("14", "alice", MID 7) bob <# ("", "alice", SENT 7) alice <#= \case ("", "bob", Msg "hello too") -> True; _ -> False alice #: ("3a", "bob", "ACK 7") #> ("3a", "bob", OK) - bob #: ("15", "alice", "SEND 9\nmessage 1") #> ("15", "alice", MID 8) + bob #: ("15", "alice", "SEND F 9\nmessage 1") #> ("15", "alice", MID 8) bob <# ("", "alice", SENT 8) alice <#= \case ("", "bob", Msg "message 1") -> True; _ -> False alice #: ("4a", "bob", "ACK 8") #> ("4a", "bob", OK) alice #: ("5", "bob", "OFF") #> ("5", "bob", OK) - bob #: ("17", "alice", "SEND 9\nmessage 3") #> ("17", "alice", MID 9) + bob #: ("17", "alice", "SEND F 9\nmessage 3") #> ("17", "alice", MID 9) bob <# ("", "alice", MERR 9 (SMP AUTH)) alice #: ("6", "bob", "DEL") #> ("6", "bob", OK) alice #:# "nothing else should be delivered to alice" @@ -163,24 +163,24 @@ testDuplexConnRandomIds _ alice bob = do bob <# ("", aliceConn, INFO "alice's connInfo") bob <# ("", aliceConn, CON) alice <# ("", bobConn, CON) - alice #: ("2", bobConn, "SEND :hello") #> ("2", bobConn, MID 5) + alice #: ("2", bobConn, "SEND F :hello") #> ("2", bobConn, MID 5) alice <# ("", bobConn, SENT 5) bob <#= \case ("", c, Msg "hello") -> c == aliceConn; _ -> False bob #: ("12", aliceConn, "ACK 5") #> ("12", aliceConn, OK) - alice #: ("3", bobConn, "SEND :how are you?") #> ("3", bobConn, MID 6) + alice #: ("3", bobConn, "SEND F :how are you?") #> ("3", bobConn, MID 6) alice <# ("", bobConn, SENT 6) bob <#= \case ("", c, Msg "how are you?") -> c == aliceConn; _ -> False bob #: ("13", aliceConn, "ACK 6") #> ("13", aliceConn, OK) - bob #: ("14", aliceConn, "SEND 9\nhello too") #> ("14", aliceConn, MID 7) + bob #: ("14", aliceConn, "SEND F 9\nhello too") #> ("14", aliceConn, MID 7) bob <# ("", aliceConn, SENT 7) alice <#= \case ("", c, Msg "hello too") -> c == bobConn; _ -> False alice #: ("3a", bobConn, "ACK 7") #> ("3a", bobConn, OK) - bob #: ("15", aliceConn, "SEND 9\nmessage 1") #> ("15", aliceConn, MID 8) + bob #: ("15", aliceConn, "SEND F 9\nmessage 1") #> ("15", aliceConn, MID 8) bob <# ("", aliceConn, SENT 8) alice <#= \case ("", c, Msg "message 1") -> c == bobConn; _ -> False alice #: ("4a", bobConn, "ACK 8") #> ("4a", bobConn, OK) alice #: ("5", bobConn, "OFF") #> ("5", bobConn, OK) - bob #: ("17", aliceConn, "SEND 9\nmessage 3") #> ("17", aliceConn, MID 9) + bob #: ("17", aliceConn, "SEND F 9\nmessage 3") #> ("17", aliceConn, MID 9) bob <# ("", aliceConn, MERR 9 (SMP AUTH)) alice #: ("6", bobConn, "DEL") #> ("6", bobConn, OK) alice #:# "nothing else should be delivered to alice" @@ -198,7 +198,7 @@ testContactConnection _ alice bob tom = do alice <# ("", "bob", INFO "bob's connInfo 2") alice <# ("", "bob", CON) bob <# ("", "alice", CON) - alice #: ("3", "bob", "SEND :hi") #> ("3", "bob", MID 5) + alice #: ("3", "bob", "SEND F :hi") #> ("3", "bob", MID 5) alice <# ("", "bob", SENT 5) bob <#= \case ("", "alice", Msg "hi") -> True; _ -> False bob #: ("13", "alice", "ACK 5") #> ("13", "alice", OK) @@ -211,7 +211,7 @@ testContactConnection _ alice bob tom = do alice <# ("", "tom", INFO "tom's connInfo 2") alice <# ("", "tom", CON) tom <# ("", "alice", CON) - alice #: ("5", "tom", "SEND :hi there") #> ("5", "tom", MID 5) + alice #: ("5", "tom", "SEND F :hi there") #> ("5", "tom", MID 5) alice <# ("", "tom", SENT 5) tom <#= \case ("", "alice", Msg "hi there") -> True; _ -> False tom #: ("23", "alice", "ACK 5") #> ("23", "alice", OK) @@ -234,7 +234,7 @@ testContactConnRandomIds _ alice bob = do alice <# ("", bobConn, CON) bob <# ("", aliceConn, CON) - alice #: ("3", bobConn, "SEND :hi") #> ("3", bobConn, MID 5) + alice #: ("3", bobConn, "SEND F :hi") #> ("3", bobConn, MID 5) alice <# ("", bobConn, SENT 5) bob <#= \case ("", c, Msg "hi") -> c == aliceConn; _ -> False bob #: ("13", aliceConn, "ACK 5") #> ("13", aliceConn, OK) @@ -254,17 +254,17 @@ testRejectContactRequest _ alice bob = do testSubscription :: Transport c => TProxy c -> c -> c -> c -> IO () testSubscription _ alice1 alice2 bob = do (alice1, "alice") `connect` (bob, "bob") - bob #: ("12", "alice", "SEND 5\nhello") #> ("12", "alice", MID 5) + bob #: ("12", "alice", "SEND F 5\nhello") #> ("12", "alice", MID 5) bob <# ("", "alice", SENT 5) alice1 <#= \case ("", "bob", Msg "hello") -> True; _ -> False alice1 #: ("1", "bob", "ACK 5") #> ("1", "bob", OK) - bob #: ("13", "alice", "SEND 11\nhello again") #> ("13", "alice", MID 6) + bob #: ("13", "alice", "SEND F 11\nhello again") #> ("13", "alice", MID 6) bob <# ("", "alice", SENT 6) alice1 <#= \case ("", "bob", Msg "hello again") -> True; _ -> False alice1 #: ("2", "bob", "ACK 6") #> ("2", "bob", OK) alice2 #: ("21", "bob", "SUB") #> ("21", "bob", OK) alice1 <# ("", "bob", END) - bob #: ("14", "alice", "SEND 2\nhi") #> ("14", "alice", MID 7) + bob #: ("14", "alice", "SEND F 2\nhi") #> ("14", "alice", MID 7) bob <# ("", "alice", SENT 7) alice2 <#= \case ("", "bob", Msg "hi") -> True; _ -> False alice2 #: ("22", "bob", "ACK 7") #> ("22", "bob", OK) @@ -283,7 +283,7 @@ testMsgDeliveryServerRestart :: Transport c => TProxy c -> c -> c -> IO () testMsgDeliveryServerRestart t alice bob = do withServer $ do connect (alice, "alice") (bob, "bob") - bob #: ("1", "alice", "SEND 2\nhi") #> ("1", "alice", MID 5) + bob #: ("1", "alice", "SEND F 2\nhi") #> ("1", "alice", MID 5) bob <# ("", "alice", SENT 5) alice <#= \case ("", "bob", Msg "hi") -> True; _ -> False alice #: ("11", "bob", "ACK 5") #> ("11", "bob", OK) @@ -291,7 +291,7 @@ testMsgDeliveryServerRestart t alice bob = do let server = (SMPServer "localhost" testPort2 testKeyHash) alice <# ("", "", DOWN server ["bob"]) - bob #: ("2", "alice", "SEND 11\nhello again") #> ("2", "alice", MID 6) + bob #: ("2", "alice", "SEND F 11\nhello again") #> ("2", "alice", MID 6) bob #:# "nothing else delivered before the server is restarted" alice #:# "nothing else delivered before the server is restarted" @@ -314,7 +314,7 @@ testServerConnectionAfterError t _ = do bob <# ("", "", DOWN server ["alice"]) alice <# ("", "", DOWN server ["bob"]) - alice #: ("1", "bob", "SEND 5\nhello") #> ("1", "bob", MID 5) + alice #: ("1", "bob", "SEND F 5\nhello") #> ("1", "bob", MID 5) alice #:# "nothing else delivered before the server is restarted" bob #:# "nothing else delivered before the server is restarted" @@ -328,7 +328,7 @@ testServerConnectionAfterError t _ = do bob <#= \case ("", "alice", Msg "hello") -> True; _ -> False bob #: ("2", "alice", "ACK 5") #> ("2", "alice", OK) alice <# ("", "", UP server ["bob"]) - alice #: ("1", "bob", "SEND 11\nhello again") #> ("1", "bob", MID 6) + alice #: ("1", "bob", "SEND F 11\nhello again") #> ("1", "bob", MID 6) alice <# ("", "bob", SENT 6) bob <#= \case ("", "alice", Msg "hello again") -> True; _ -> False @@ -349,14 +349,14 @@ testMsgDeliveryAgentRestart t bob = do withAgent $ \alice -> do withServer $ do connect (bob, "bob") (alice, "alice") - alice #: ("1", "bob", "SEND 5\nhello") #> ("1", "bob", MID 5) + alice #: ("1", "bob", "SEND F 5\nhello") #> ("1", "bob", MID 5) alice <# ("", "bob", SENT 5) bob <#= \case ("", "alice", Msg "hello") -> True; _ -> False bob #: ("11", "alice", "ACK 5") #> ("11", "alice", OK) bob #:# "nothing else delivered before the server is down" bob <# ("", "", DOWN server ["alice"]) - alice #: ("2", "bob", "SEND 11\nhello again") #> ("2", "bob", MID 6) + alice #: ("2", "bob", "SEND F 11\nhello again") #> ("2", "bob", MID 6) alice #:# "nothing else delivered before the server is restarted" bob #:# "nothing else delivered before the server is restarted" @@ -394,11 +394,11 @@ testConcurrentMsgDelivery _ alice bob = do -- the first connection should not be blocked by the second one sendMessage (alice, "alice") (bob, "bob") "hello" - -- alice #: ("2", "bob", "SEND :hello") #> ("2", "bob", MID 1) + -- alice #: ("2", "bob", "SEND F :hello") #> ("2", "bob", MID 1) -- alice <# ("", "bob", SENT 1) -- bob <#= \case ("", "alice", Msg "hello") -> True; _ -> False -- bob #: ("12", "alice", "ACK 1") #> ("12", "alice", OK) - bob #: ("14", "alice", "SEND 9\nhello too") #> ("14", "alice", MID 6) + bob #: ("14", "alice", "SEND F 9\nhello too") #> ("14", "alice", MID 6) bob <# ("", "alice", SENT 6) -- if delivery is blocked it won't go further alice <#= \case ("", "bob", Msg "hello too") -> True; _ -> False @@ -411,11 +411,11 @@ testMsgDeliveryQuotaExceeded _ alice bob = do forM_ [1 .. 4 :: Int] $ \i -> do let corrId = bshow i msg = "message " <> bshow i - (_, "bob", Right (MID mId)) <- alice #: (corrId, "bob", "SEND :" <> msg) + (_, "bob", Right (MID mId)) <- alice #: (corrId, "bob", "SEND F :" <> msg) alice <#= \case ("", "bob", SENT m) -> m == mId; _ -> False - (_, "bob", Right (MID _)) <- alice #: ("5", "bob", "SEND :over quota") + (_, "bob", Right (MID _)) <- alice #: ("5", "bob", "SEND F :over quota") - alice #: ("1", "bob2", "SEND :hello") #> ("1", "bob2", MID 5) + alice #: ("1", "bob2", "SEND F :hello") #> ("1", "bob2", MID 5) -- if delivery is blocked it won't go further alice <# ("", "bob2", SENT 5) @@ -432,10 +432,10 @@ connect (h1, name1) (h2, name2) = do sendMessage :: Transport c => (c, ConnId) -> (c, ConnId) -> ByteString -> IO () sendMessage (h1, name1) (h2, name2) msg = do - ("m1", name2', Right (MID mId)) <- h1 #: ("m1", name2, "SEND :" <> msg) + ("m1", name2', Right (MID mId)) <- h1 #: ("m1", name2, "SEND F :" <> msg) name2' `shouldBe` name2 h1 <#= \case ("", n, SENT m) -> n == name2 && m == mId; _ -> False - ("", name1', Right (MSG MsgMeta {recipient = (msgId', _)} msg')) <- (h2 <#:) + ("", name1', Right (MSG MsgMeta {recipient = (msgId', _)} _ msg')) <- (h2 <#:) name1' `shouldBe` name1 msg' `shouldBe` msg h2 #: ("m2", name1, "ACK " <> bshow msgId') =#> \case ("m2", n, OK) -> n == name1; _ -> False diff --git a/tests/AgentTests/FunctionalAPITests.hs b/tests/AgentTests/FunctionalAPITests.hs index a0195de23..43925c830 100644 --- a/tests/AgentTests/FunctionalAPITests.hs +++ b/tests/AgentTests/FunctionalAPITests.hs @@ -18,6 +18,7 @@ import Simplex.Messaging.Agent import Simplex.Messaging.Agent.Env.SQLite (AgentConfig (..)) import Simplex.Messaging.Agent.Protocol import Simplex.Messaging.Protocol (ErrorType (..), MsgBody) +import qualified Simplex.Messaging.Protocol as SMP import Simplex.Messaging.Server.Env.STM (ServerConfig (..)) import Simplex.Messaging.Server.Expiration import Simplex.Messaging.Transport (ATransport (..)) @@ -34,7 +35,7 @@ get :: MonadIO m => AgentClient -> m (ATransmission 'Agent) get c = atomically (readTBQueue $ subQ c) pattern Msg :: MsgBody -> ACommand 'Agent -pattern Msg msgBody <- MSG MsgMeta {integrity = MsgOk} msgBody +pattern Msg msgBody <- MSG MsgMeta {integrity = MsgOk} _ msgBody functionalAPITests :: ATransport -> Spec functionalAPITests t = do @@ -71,24 +72,24 @@ testAgentClient = do get bob ##> ("", aliceId, INFO "alice's connInfo") get bob ##> ("", aliceId, CON) -- message IDs 1 to 4 get assigned to control messages, so first MSG is assigned ID 5 - 5 <- sendMessage alice bobId "hello" + 5 <- sendMessage alice bobId SMP.noMsgFlags "hello" get alice ##> ("", bobId, SENT 5) - 6 <- sendMessage alice bobId "how are you?" + 6 <- sendMessage alice bobId SMP.noMsgFlags "how are you?" get alice ##> ("", bobId, SENT 6) get bob =##> \case ("", c, Msg "hello") -> c == aliceId; _ -> False ackMessage bob aliceId 5 get bob =##> \case ("", c, Msg "how are you?") -> c == aliceId; _ -> False ackMessage bob aliceId 6 - 7 <- sendMessage bob aliceId "hello too" + 7 <- sendMessage bob aliceId SMP.noMsgFlags "hello too" get bob ##> ("", aliceId, SENT 7) - 8 <- sendMessage bob aliceId "message 1" + 8 <- sendMessage bob aliceId SMP.noMsgFlags "message 1" get bob ##> ("", aliceId, SENT 8) get alice =##> \case ("", c, Msg "hello too") -> c == bobId; _ -> False ackMessage alice bobId 7 get alice =##> \case ("", c, Msg "message 1") -> c == bobId; _ -> False ackMessage alice bobId 8 suspendConnection alice bobId - 9 <- sendMessage bob aliceId "message 2" + 9 <- sendMessage bob aliceId SMP.noMsgFlags "message 2" get bob ##> ("", aliceId, MERR 9 (SMP AUTH)) deleteConnection alice bobId liftIO $ noMessages alice "nothing else should be delivered to alice" @@ -237,11 +238,11 @@ testActiveClientNotDisconnected t = do exchangeGreetings :: AgentClient -> ConnId -> AgentClient -> ConnId -> ExceptT AgentErrorType IO () exchangeGreetings alice bobId bob aliceId = do - 5 <- sendMessage alice bobId "hello" + 5 <- sendMessage alice bobId SMP.noMsgFlags "hello" get alice ##> ("", bobId, SENT 5) get bob =##> \case ("", c, Msg "hello") -> c == aliceId; _ -> False ackMessage bob aliceId 5 - 6 <- sendMessage bob aliceId "hello too" + 6 <- sendMessage bob aliceId SMP.noMsgFlags "hello too" get bob ##> ("", aliceId, SENT 6) get alice =##> \case ("", c, Msg "hello too") -> c == bobId; _ -> False ackMessage alice bobId 6 diff --git a/tests/AgentTests/SQLiteTests.hs b/tests/AgentTests/SQLiteTests.hs index 28f39379b..4dd9f89bf 100644 --- a/tests/AgentTests/SQLiteTests.hs +++ b/tests/AgentTests/SQLiteTests.hs @@ -27,6 +27,7 @@ import Simplex.Messaging.Agent.Store import Simplex.Messaging.Agent.Store.SQLite import qualified Simplex.Messaging.Agent.Store.SQLite.Migrations as Migrations import qualified Simplex.Messaging.Crypto as C +import qualified Simplex.Messaging.Protocol as SMP import System.Random import Test.Hspec import UnliftIO.Directory (removeFile) @@ -398,6 +399,7 @@ mkRcvMsgData internalId internalRcvId externalSndId brokerId internalHash = broker = (brokerId, ts) }, msgType = AM_A_MSG_, + msgFlags = SMP.noMsgFlags, msgBody = hw, internalHash, externalPrevSndHash = "hash_from_sender" @@ -427,6 +429,7 @@ mkSndMsgData internalId internalSndId internalHash = internalSndId, internalTs = ts, msgType = AM_A_MSG_, + msgFlags = SMP.noMsgFlags, msgBody = hw, internalHash, prevMsgHash = internalHash diff --git a/tests/ServerTests.hs b/tests/ServerTests.hs index 49614d8e7..725688ba8 100644 --- a/tests/ServerTests.hs +++ b/tests/ServerTests.hs @@ -56,14 +56,14 @@ pattern Ids :: RecipientId -> SenderId -> RcvPublicDhKey -> BrokerMsg pattern Ids rId sId srvDh <- IDS (QIK rId sId srvDh) sendRecv :: forall c p. (Transport c, PartyI p) => THandle c -> (Maybe C.ASignature, ByteString, ByteString, Command p) -> IO (SignedTransmission BrokerMsg) -sendRecv h@THandle {sessionId} (sgn, corrId, qId, cmd) = do - let t = encodeTransmission sessionId (CorrId corrId, qId, cmd) +sendRecv h@THandle {thVersion, sessionId} (sgn, corrId, qId, cmd) = do + let t = encodeTransmission thVersion sessionId (CorrId corrId, qId, cmd) Right () <- tPut h (sgn, t) tGet h signSendRecv :: forall c p. (Transport c, PartyI p) => THandle c -> C.APrivateSignKey -> (ByteString, ByteString, Command p) -> IO (SignedTransmission BrokerMsg) -signSendRecv h@THandle {sessionId} pk (corrId, qId, cmd) = do - let t = encodeTransmission sessionId (CorrId corrId, qId, cmd) +signSendRecv h@THandle {thVersion, sessionId} pk (corrId, qId, cmd) = do + let t = encodeTransmission thVersion sessionId (CorrId corrId, qId, cmd) Right sig <- runExceptT $ C.sign pk t Right () <- tPut h (Just sig, t) tGet h @@ -71,6 +71,9 @@ signSendRecv h@THandle {sessionId} pk (corrId, qId, cmd) = do (#==) :: (HasCallStack, Eq a, Show a) => (a, a) -> String -> Assertion (actual, expected) #== message = assertEqual message expected actual +_SEND :: MsgBody -> Command 'Sender +_SEND = SEND noMsgFlags + testCreateSecure :: ATransport -> Spec testCreateSecure (ATransport t) = it "should create (NEW) and secure (KEY) queue" $ @@ -81,11 +84,11 @@ testCreateSecure (ATransport t) = let dec nonce = C.cbDecrypt (C.dh' srvDh dhPriv) (C.cbNonce nonce) (rId1, "") #== "creates queue" - Resp "bcda" sId1 ok1 <- sendRecv h ("", "bcda", sId, SEND "hello") + Resp "bcda" sId1 ok1 <- sendRecv h ("", "bcda", sId, _SEND "hello") (ok1, OK) #== "accepts unsigned SEND" (sId1, sId) #== "same queue ID in response 1" - Resp "" _ (MSG mId1 _ msg1) <- tGet h + Resp "" _ (MSG mId1 _ _ msg1) <- tGet h (dec mId1 msg1, Right "hello") #== "delivers message" Resp "cdab" _ ok4 <- signSendRecv h rKey ("cdab", rId, ACK) @@ -95,7 +98,7 @@ testCreateSecure (ATransport t) = (err6, ERR NO_MSG) #== "replies ERR when message acknowledged without messages" (sPub, sKey) <- C.generateSignatureKeyPair C.SEd448 - Resp "abcd" sId2 err1 <- signSendRecv h sKey ("abcd", sId, SEND "hello") + Resp "abcd" sId2 err1 <- signSendRecv h sKey ("abcd", sId, _SEND "hello") (err1, ERR AUTH) #== "rejects signed SEND" (sId2, sId) #== "same queue ID in response 2" @@ -112,16 +115,16 @@ testCreateSecure (ATransport t) = Resp "abcd" _ err4 <- signSendRecv h rKey ("abcd", rId, KEY sPub) (err4, ERR AUTH) #== "rejects KEY if already secured" - Resp "bcda" _ ok3 <- signSendRecv h sKey ("bcda", sId, SEND "hello again") + Resp "bcda" _ ok3 <- signSendRecv h sKey ("bcda", sId, _SEND "hello again") (ok3, OK) #== "accepts signed SEND" - Resp "" _ (MSG mId2 _ msg2) <- tGet h + Resp "" _ (MSG mId2 _ _ msg2) <- tGet h (dec mId2 msg2, Right "hello again") #== "delivers message 2" Resp "cdab" _ ok5 <- signSendRecv h rKey ("cdab", rId, ACK) (ok5, OK) #== "replies OK when message acknowledged 2" - Resp "dabc" _ err5 <- sendRecv h ("", "dabc", sId, SEND "hello") + Resp "dabc" _ err5 <- sendRecv h ("", "dabc", sId, _SEND "hello") (err5, ERR AUTH) #== "rejects unsigned SEND" testCreateDelete :: ATransport -> Spec @@ -138,13 +141,13 @@ testCreateDelete (ATransport t) = Resp "bcda" _ ok1 <- signSendRecv rh rKey ("bcda", rId, KEY sPub) (ok1, OK) #== "secures queue" - Resp "cdab" _ ok2 <- signSendRecv sh sKey ("cdab", sId, SEND "hello") + Resp "cdab" _ ok2 <- signSendRecv sh sKey ("cdab", sId, _SEND "hello") (ok2, OK) #== "accepts signed SEND" - Resp "dabc" _ ok7 <- signSendRecv sh sKey ("dabc", sId, SEND "hello 2") + Resp "dabc" _ ok7 <- signSendRecv sh sKey ("dabc", sId, _SEND "hello 2") (ok7, OK) #== "accepts signed SEND 2 - this message is not delivered because the first is not ACKed" - Resp "" _ (MSG mId1 _ msg1) <- tGet rh + Resp "" _ (MSG mId1 _ _ msg1) <- tGet rh (dec mId1 msg1, Right "hello") #== "delivers message" Resp "abcd" _ err1 <- sendRecv rh (sampleSig, "abcd", rId, OFF) @@ -157,16 +160,16 @@ testCreateDelete (ATransport t) = (ok3, OK) #== "suspends queue" (rId2, rId) #== "same queue ID in response 2" - Resp "dabc" _ err3 <- signSendRecv sh sKey ("dabc", sId, SEND "hello") + Resp "dabc" _ err3 <- signSendRecv sh sKey ("dabc", sId, _SEND "hello") (err3, ERR AUTH) #== "rejects signed SEND" - Resp "abcd" _ err4 <- sendRecv sh ("", "abcd", sId, SEND "hello") + Resp "abcd" _ err4 <- sendRecv sh ("", "abcd", sId, _SEND "hello") (err4, ERR AUTH) #== "reject unsigned SEND too" Resp "bcda" _ ok4 <- signSendRecv rh rKey ("bcda", rId, OFF) (ok4, OK) #== "accepts OFF when suspended" - Resp "cdab" _ (MSG mId2 _ msg2) <- signSendRecv rh rKey ("cdab", rId, SUB) + Resp "cdab" _ (MSG mId2 _ _ msg2) <- signSendRecv rh rKey ("cdab", rId, SUB) (dec mId2 msg2, Right "hello") #== "accepts SUB when suspended and delivers the message again (because was not ACKed)" Resp "dabc" _ err5 <- sendRecv rh (sampleSig, "dabc", rId, DEL) @@ -179,10 +182,10 @@ testCreateDelete (ATransport t) = (ok6, OK) #== "deletes queue" (rId3, rId) #== "same queue ID in response 3" - Resp "cdab" _ err7 <- signSendRecv sh sKey ("cdab", sId, SEND "hello") + Resp "cdab" _ err7 <- signSendRecv sh sKey ("cdab", sId, _SEND "hello") (err7, ERR AUTH) #== "rejects signed SEND when deleted" - Resp "dabc" _ err8 <- sendRecv sh ("", "dabc", sId, SEND "hello") + Resp "dabc" _ err8 <- sendRecv sh ("", "dabc", sId, _SEND "hello") (err8, ERR AUTH) #== "rejects unsigned SEND too when deleted" Resp "abcd" _ err11 <- signSendRecv rh rKey ("abcd", rId, ACK) @@ -232,10 +235,10 @@ testDuplex (ATransport t) = -- aSnd ID is passed to Bob out-of-band (bsPub, bsKey) <- C.generateSignatureKeyPair C.SEd448 - Resp "bcda" _ OK <- sendRecv bob ("", "bcda", aSnd, SEND $ "key " <> strEncode bsPub) + Resp "bcda" _ OK <- sendRecv bob ("", "bcda", aSnd, _SEND $ "key " <> strEncode bsPub) -- "key ..." is ad-hoc, not a part of SMP protocol - Resp "" _ (MSG mId1 _ msg1) <- tGet alice + Resp "" _ (MSG mId1 _ _ msg1) <- tGet alice Resp "cdab" _ OK <- signSendRecv alice arKey ("cdab", aRcv, ACK) Right ["key", bobKey] <- pure $ B.words <$> aDec mId1 msg1 (bobKey, strEncode bsPub) #== "key received from Bob" @@ -245,33 +248,33 @@ testDuplex (ATransport t) = (bDhPub, bDhPriv :: C.PrivateKeyX25519) <- C.generateKeyPair' Resp "abcd" _ (Ids bRcv bSnd bSrvDh) <- signSendRecv bob brKey ("abcd", "", NEW brPub bDhPub) let bDec nonce = C.cbDecrypt (C.dh' bSrvDh bDhPriv) (C.cbNonce nonce) - Resp "bcda" _ OK <- signSendRecv bob bsKey ("bcda", aSnd, SEND $ "reply_id " <> encode bSnd) + Resp "bcda" _ OK <- signSendRecv bob bsKey ("bcda", aSnd, _SEND $ "reply_id " <> encode bSnd) -- "reply_id ..." is ad-hoc, not a part of SMP protocol - Resp "" _ (MSG mId2 _ msg2) <- tGet alice + Resp "" _ (MSG mId2 _ _ msg2) <- tGet alice Resp "cdab" _ OK <- signSendRecv alice arKey ("cdab", aRcv, ACK) Right ["reply_id", bId] <- pure $ B.words <$> aDec mId2 msg2 (bId, encode bSnd) #== "reply queue ID received from Bob" (asPub, asKey) <- C.generateSignatureKeyPair C.SEd448 - Resp "dabc" _ OK <- sendRecv alice ("", "dabc", bSnd, SEND $ "key " <> strEncode asPub) + Resp "dabc" _ OK <- sendRecv alice ("", "dabc", bSnd, _SEND $ "key " <> strEncode asPub) -- "key ..." is ad-hoc, not a part of SMP protocol - Resp "" _ (MSG mId3 _ msg3) <- tGet bob + Resp "" _ (MSG mId3 _ _ msg3) <- tGet bob Resp "abcd" _ OK <- signSendRecv bob brKey ("abcd", bRcv, ACK) Right ["key", aliceKey] <- pure $ B.words <$> bDec mId3 msg3 (aliceKey, strEncode asPub) #== "key received from Alice" Resp "bcda" _ OK <- signSendRecv bob brKey ("bcda", bRcv, KEY asPub) - Resp "cdab" _ OK <- signSendRecv bob bsKey ("cdab", aSnd, SEND "hi alice") + Resp "cdab" _ OK <- signSendRecv bob bsKey ("cdab", aSnd, _SEND "hi alice") - Resp "" _ (MSG mId4 _ msg4) <- tGet alice + Resp "" _ (MSG mId4 _ _ msg4) <- tGet alice Resp "dabc" _ OK <- signSendRecv alice arKey ("dabc", aRcv, ACK) (aDec mId4 msg4, Right "hi alice") #== "message received from Bob" - Resp "abcd" _ OK <- signSendRecv alice asKey ("abcd", bSnd, SEND "how are you bob") + Resp "abcd" _ OK <- signSendRecv alice asKey ("abcd", bSnd, _SEND "how are you bob") - Resp "" _ (MSG mId5 _ msg5) <- tGet bob + Resp "" _ (MSG mId5 _ _ msg5) <- tGet bob Resp "bcda" _ OK <- signSendRecv bob brKey ("bcda", bRcv, ACK) (bDec mId5 msg5, Right "how are you bob") #== "message received from alice" @@ -283,26 +286,26 @@ testSwitchSub (ATransport t) = (dhPub, dhPriv :: C.PrivateKeyX25519) <- C.generateKeyPair' Resp "abcd" _ (Ids rId sId srvDh) <- signSendRecv rh1 rKey ("abcd", "", NEW rPub dhPub) let dec nonce = C.cbDecrypt (C.dh' srvDh dhPriv) (C.cbNonce nonce) - Resp "bcda" _ ok1 <- sendRecv sh ("", "bcda", sId, SEND "test1") + Resp "bcda" _ ok1 <- sendRecv sh ("", "bcda", sId, _SEND "test1") (ok1, OK) #== "sent test message 1" - Resp "cdab" _ ok2 <- sendRecv sh ("", "cdab", sId, SEND "test2, no ACK") + Resp "cdab" _ ok2 <- sendRecv sh ("", "cdab", sId, _SEND "test2, no ACK") (ok2, OK) #== "sent test message 2" - Resp "" _ (MSG mId1 _ msg1) <- tGet rh1 + Resp "" _ (MSG mId1 _ _ msg1) <- tGet rh1 (dec mId1 msg1, Right "test1") #== "test message 1 delivered to the 1st TCP connection" - Resp "abcd" _ (MSG mId2 _ msg2) <- signSendRecv rh1 rKey ("abcd", rId, ACK) + Resp "abcd" _ (MSG mId2 _ _ msg2) <- signSendRecv rh1 rKey ("abcd", rId, ACK) (dec mId2 msg2, Right "test2, no ACK") #== "test message 2 delivered, no ACK" - Resp "bcda" _ (MSG mId2' _ msg2') <- signSendRecv rh2 rKey ("bcda", rId, SUB) + Resp "bcda" _ (MSG mId2' _ _ msg2') <- signSendRecv rh2 rKey ("bcda", rId, SUB) (dec mId2' msg2', Right "test2, no ACK") #== "same simplex queue via another TCP connection, tes2 delivered again (no ACK in 1st queue)" Resp "cdab" _ OK <- signSendRecv rh2 rKey ("cdab", rId, ACK) Resp "" _ end <- tGet rh1 (end, END) #== "unsubscribed the 1st TCP connection" - Resp "dabc" _ OK <- sendRecv sh ("", "dabc", sId, SEND "test3") + Resp "dabc" _ OK <- sendRecv sh ("", "dabc", sId, _SEND "test3") - Resp "" _ (MSG mId3 _ msg3) <- tGet rh2 + Resp "" _ (MSG mId3 _ _ msg3) <- tGet rh2 (dec mId3 msg3, Right "test3") #== "delivered to the 2nd TCP connection" Resp "abcd" _ err <- signSendRecv rh1 rKey ("abcd", rId, ACK) @@ -338,15 +341,15 @@ testWithStoreLog at@(ATransport t) = writeTVar senderId1 sId1 writeTVar notifierId nId Resp "dabc" _ OK <- signSendRecv h1 nKey ("dabc", nId, NSUB) - Resp "bcda" _ OK <- signSendRecv h sKey1 ("bcda", sId1, SEND "hello") - Resp "" _ (MSG mId1 _ msg1) <- tGet h + Resp "bcda" _ OK <- signSendRecv h sKey1 ("bcda", sId1, _SEND "hello") + Resp "" _ (MSG mId1 _ _ msg1) <- tGet h (C.cbDecrypt dhShared (C.cbNonce mId1) msg1, Right "hello") #== "delivered from queue 1" Resp "" _ NMSG <- tGet h1 (sId2, rId2, rKey2, dhShared2) <- createAndSecureQueue h sPub2 atomically $ writeTVar senderId2 sId2 - Resp "cdab" _ OK <- signSendRecv h sKey2 ("cdab", sId2, SEND "hello too") - Resp "" _ (MSG mId2 _ msg2) <- tGet h + Resp "cdab" _ OK <- signSendRecv h sKey2 ("cdab", sId2, _SEND "hello too") + Resp "" _ (MSG mId2 _ _ msg2) <- tGet h (C.cbDecrypt dhShared2 (C.cbNonce mId2) msg2, Right "hello too") #== "delivered from queue 2" Resp "dabc" _ OK <- signSendRecv h rKey2 ("dabc", rId2, DEL) @@ -357,7 +360,7 @@ testWithStoreLog at@(ATransport t) = withSmpServerThreadOn at testPort . runTest t $ \h -> do sId1 <- readTVarIO senderId1 -- fails if store log is disabled - Resp "bcda" _ (ERR AUTH) <- signSendRecv h sKey1 ("bcda", sId1, SEND "hello") + Resp "bcda" _ (ERR AUTH) <- signSendRecv h sKey1 ("bcda", sId1, _SEND "hello") pure () withSmpServerStoreLogOn at testPort . runTest t $ \h -> runClient t $ \h1 -> do @@ -368,13 +371,13 @@ testWithStoreLog at@(ATransport t) = sId1 <- readTVarIO senderId1 nId <- readTVarIO notifierId Resp "dabc" _ OK <- signSendRecv h1 nKey ("dabc", nId, NSUB) - Resp "bcda" _ OK <- signSendRecv h sKey1 ("bcda", sId1, SEND "hello") - Resp "cdab" _ (MSG mId3 _ msg3) <- signSendRecv h rKey1 ("cdab", rId1, SUB) + Resp "bcda" _ OK <- signSendRecv h sKey1 ("bcda", sId1, _SEND "hello") + Resp "cdab" _ (MSG mId3 _ _ msg3) <- signSendRecv h rKey1 ("cdab", rId1, SUB) (C.cbDecrypt dh1 (C.cbNonce mId3) msg3, Right "hello") #== "delivered from restored queue" Resp "" _ NMSG <- tGet h1 -- this queue is removed - not restored sId2 <- readTVarIO senderId2 - Resp "cdab" _ (ERR AUTH) <- signSendRecv h sKey2 ("cdab", sId2, SEND "hello too") + Resp "cdab" _ (ERR AUTH) <- signSendRecv h sKey2 ("cdab", sId2, _SEND "hello too") pure () logSize `shouldReturn` 1 @@ -432,11 +435,11 @@ testTiming (ATransport t) = (sPub, sKey) <- generateKeys goodKeySize Resp "dabc" _ OK <- signSendRecv rh rKey ("dabc", rId, KEY sPub) - Resp "bcda" _ OK <- signSendRecv sh sKey ("bcda", sId, SEND "hello") - Resp "" _ (MSG mId _ msg) <- tGet rh + Resp "bcda" _ OK <- signSendRecv sh sKey ("bcda", sId, _SEND "hello") + Resp "" _ (MSG mId _ _ msg) <- tGet rh (dec mId msg, Right "hello") #== "delivered from queue" - runTimingTest sh badKey sId $ SEND "hello" + runTimingTest sh badKey sId $ _SEND "hello" where generateKeys = \case 32 -> C.generateSignatureKeyPair C.SEd25519 @@ -468,15 +471,15 @@ testMessageNotifications (ATransport t) = let dec nonce = C.cbDecrypt dhShared (C.cbNonce nonce) Resp "1" _ (NID nId) <- signSendRecv rh rKey ("1", rId, NKEY nPub) Resp "2" _ OK <- signSendRecv nh1 nKey ("2", nId, NSUB) - Resp "3" _ OK <- signSendRecv sh sKey ("3", sId, SEND "hello") - Resp "" _ (MSG mId1 _ msg1) <- tGet rh + Resp "3" _ OK <- signSendRecv sh sKey ("3", sId, _SEND "hello") + Resp "" _ (MSG mId1 _ _ msg1) <- tGet rh (dec mId1 msg1, Right "hello") #== "delivered from queue" Resp "3a" _ OK <- signSendRecv rh rKey ("3a", rId, ACK) Resp "" _ NMSG <- tGet nh1 Resp "4" _ OK <- signSendRecv nh2 nKey ("4", nId, NSUB) Resp "" _ END <- tGet nh1 - Resp "5" _ OK <- signSendRecv sh sKey ("5", sId, SEND "hello again") - Resp "" _ (MSG mId2 _ msg2) <- tGet rh + Resp "5" _ OK <- signSendRecv sh sKey ("5", sId, _SEND "hello again") + Resp "" _ (MSG mId2 _ _ msg2) <- tGet rh (dec mId2 msg2, Right "hello again") #== "delivered from queue again" Resp "" _ NMSG <- tGet nh2 1000 `timeout` tGet @BrokerMsg nh1 >>= \case @@ -492,11 +495,11 @@ testMsgExpireOnSend t = testSMPClient @c $ \sh -> do (sId, rId, rKey, dhShared) <- testSMPClient @c $ \rh -> createAndSecureQueue rh sPub let dec nonce = C.cbDecrypt dhShared (C.cbNonce nonce) - Resp "1" _ OK <- signSendRecv sh sKey ("1", sId, SEND "hello (should expire)") + Resp "1" _ OK <- signSendRecv sh sKey ("1", sId, _SEND "hello (should expire)") threadDelay 2500000 - Resp "2" _ OK <- signSendRecv sh sKey ("2", sId, SEND "hello (should NOT expire)") + Resp "2" _ OK <- signSendRecv sh sKey ("2", sId, _SEND "hello (should NOT expire)") testSMPClient @c $ \rh -> do - Resp "3" _ (MSG mId _ msg) <- signSendRecv rh rKey ("3", rId, SUB) + Resp "3" _ (MSG mId _ _ msg) <- signSendRecv rh rKey ("3", rId, SUB) (dec mId msg, Right "hello (should NOT expire)") #== "delivered" 1000 `timeout` tGet @BrokerMsg rh >>= \case Nothing -> return () @@ -510,7 +513,7 @@ testMsgExpireOnInterval t = withSmpServerConfigOn (ATransport t) cfg' testPort $ \_ -> testSMPClient @c $ \sh -> do (sId, rId, rKey, _) <- testSMPClient @c $ \rh -> createAndSecureQueue rh sPub - Resp "1" _ OK <- signSendRecv sh sKey ("1", sId, SEND "hello (should expire)") + Resp "1" _ OK <- signSendRecv sh sKey ("1", sId, _SEND "hello (should expire)") threadDelay 2500000 testSMPClient @c $ \rh -> do Resp "2" _ OK <- signSendRecv rh rKey ("2", rId, SUB) @@ -527,10 +530,10 @@ testMsgNOTExpireOnInterval t = testSMPClient @c $ \sh -> do (sId, rId, rKey, dhShared) <- testSMPClient @c $ \rh -> createAndSecureQueue rh sPub let dec nonce = C.cbDecrypt dhShared (C.cbNonce nonce) - Resp "1" _ OK <- signSendRecv sh sKey ("1", sId, SEND "hello (should NOT expire)") + Resp "1" _ OK <- signSendRecv sh sKey ("1", sId, _SEND "hello (should NOT expire)") threadDelay 2500000 testSMPClient @c $ \rh -> do - Resp "2" _ (MSG mId _ msg) <- signSendRecv rh rKey ("2", rId, SUB) + Resp "2" _ (MSG mId _ _ msg) <- signSendRecv rh rKey ("2", rId, SUB) (dec mId msg, Right "hello (should NOT expire)") #== "delivered" 1000 `timeout` tGet @BrokerMsg rh >>= \case Nothing -> return () @@ -564,9 +567,9 @@ syntaxTests (ATransport t) = do noParamsSyntaxTest "OFF" OFF_ noParamsSyntaxTest "DEL" DEL_ describe "SEND" $ do - it "valid syntax" $ (sampleSig, "cdab", "12345678", (SEND_, ' ', "hello" :: ByteString)) >#> ("", "cdab", "12345678", ERR AUTH) + it "valid syntax" $ (sampleSig, "cdab", "12345678", (SEND_, ' ', noMsgFlags, ' ', "hello" :: ByteString)) >#> ("", "cdab", "12345678", ERR AUTH) it "no parameters" $ (sampleSig, "abcd", "12345678", SEND_) >#> ("", "abcd", "12345678", ERR $ CMD SYNTAX) - it "no queue ID" $ (sampleSig, "bcda", "", (SEND_, ' ', "hello" :: ByteString)) >#> ("", "bcda", "", ERR $ CMD NO_ENTITY) + it "no queue ID" $ (sampleSig, "bcda", "", (SEND_, ' ', noMsgFlags, ' ', "hello" :: ByteString)) >#> ("", "bcda", "", ERR $ CMD NO_ENTITY) describe "PING" $ do it "valid syntax" $ ("", "abcd", "", PING_) >#> ("", "abcd", "", PONG) describe "broker response not allowed" $ do From 60294521f4e7a8faa576872eba140de1a3ffd21c Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Tue, 7 Jun 2022 10:18:40 +0100 Subject: [PATCH 043/131] add msgId to ACK to avoid the risks of losing messages with concurrent delivery (in app/NSE) (#387) * add msgId to ACK to avoid the risks of losing messages with concurrent delivery (in app/NSE) * update ACK to only remove message and update stats if msgId matches * add tests, fix * rename sameMsgId/msgDeleted --- src/Simplex/Messaging/Agent.hs | 15 +- src/Simplex/Messaging/Agent/Client.hs | 8 +- src/Simplex/Messaging/Agent/Store.hs | 2 +- src/Simplex/Messaging/Agent/Store/SQLite.hs | 11 +- src/Simplex/Messaging/Client.hs | 6 +- src/Simplex/Messaging/Protocol.hs | 12 +- src/Simplex/Messaging/Server.hs | 170 +++++++++++-------- src/Simplex/Messaging/Server/Env/STM.hs | 14 +- src/Simplex/Messaging/Server/MsgStore.hs | 4 +- src/Simplex/Messaging/Server/MsgStore/STM.hs | 25 ++- tests/ServerTests.hs | 102 +++++++++-- 11 files changed, 238 insertions(+), 131 deletions(-) diff --git a/src/Simplex/Messaging/Agent.hs b/src/Simplex/Messaging/Agent.hs index a19b1c677..a0239b3e7 100644 --- a/src/Simplex/Messaging/Agent.hs +++ b/src/Simplex/Messaging/Agent.hs @@ -508,8 +508,8 @@ ackMessage' c connId msgId = do ack :: RcvQueue -> m () ack rq = do let mId = InternalId msgId - withStore $ \st -> checkRcvMsg st connId mId - sendAck c rq + srvMsgId <- withStore $ \st -> checkRcvMsg st connId mId + sendAck c rq srvMsgId withStore $ \st -> deleteMsg st connId mId -- | Suspend SMP agent connection (OFF command) in Reader monad @@ -711,6 +711,11 @@ processSMPTransmission c@AgentClient {smpClients, subQ} (srv, sessId, rId, cmd) _ -> prohibited >> ack _ -> prohibited >> ack _ -> prohibited >> ack + where + ack :: m () + ack = sendAck c rq srvMsgId + handleNotifyAck :: m () -> m () + handleNotifyAck m = m `catchError` \e -> notify (ERR e) >> ack SMP.END -> atomically (TM.lookup srv smpClients $>>= tryReadTMVar >>= processEND) >>= logServer "<--" c srv rId @@ -731,15 +736,9 @@ processSMPTransmission c@AgentClient {smpClients, subQ} (srv, sessId, rId, cmd) notify :: ACommand 'Agent -> m () notify msg = atomically $ writeTBQueue subQ ("", connId, msg) - handleNotifyAck :: m () -> m () - handleNotifyAck m = m `catchError` \e -> notify (ERR e) >> ack - prohibited :: m () prohibited = notify . ERR $ AGENT A_PROHIBITED - ack :: m () - ack = sendAck c rq - decryptClientMessage :: C.DhSecretX25519 -> SMP.ClientMsgEnvelope -> m (SMP.PrivHeader, AgentMsgEnvelope) decryptClientMessage e2eDh SMP.ClientMsgEnvelope {cmNonce, cmEncBody} = do clientMsg <- agentCbDecrypt e2eDh cmNonce cmEncBody diff --git a/src/Simplex/Messaging/Agent/Client.hs b/src/Simplex/Messaging/Agent/Client.hs index 6f186ece2..999f0d786 100644 --- a/src/Simplex/Messaging/Agent/Client.hs +++ b/src/Simplex/Messaging/Agent/Client.hs @@ -71,7 +71,7 @@ import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Encoding import Simplex.Messaging.Notifications.Client import Simplex.Messaging.Notifications.Protocol -import Simplex.Messaging.Protocol (BrokerMsg, ErrorType, MsgFlags (..), ProtocolServer (..), QueueId, QueueIdsKeys (..), SndPublicVerifyKey) +import Simplex.Messaging.Protocol (BrokerMsg, ErrorType, MsgFlags (..), MsgId, ProtocolServer (..), QueueId, QueueIdsKeys (..), SndPublicVerifyKey) import qualified Simplex.Messaging.Protocol as SMP import Simplex.Messaging.TMap (TMap) import qualified Simplex.Messaging.TMap as TM @@ -486,10 +486,10 @@ secureQueue c RcvQueue {server, rcvId, rcvPrivateKey} senderKey = withLogClient c server rcvId "KEY " $ \smp -> secureSMPQueue smp rcvPrivateKey rcvId senderKey -sendAck :: AgentMonad m => AgentClient -> RcvQueue -> m () -sendAck c RcvQueue {server, rcvId, rcvPrivateKey} = +sendAck :: AgentMonad m => AgentClient -> RcvQueue -> MsgId -> m () +sendAck c RcvQueue {server, rcvId, rcvPrivateKey} msgId = withLogClient c server rcvId "ACK" $ \smp -> - ackSMPMessage smp rcvPrivateKey rcvId + ackSMPMessage smp rcvPrivateKey rcvId msgId suspendQueue :: AgentMonad m => AgentClient -> RcvQueue -> m () suspendQueue c RcvQueue {server, rcvId, rcvPrivateKey} = diff --git a/src/Simplex/Messaging/Agent/Store.hs b/src/Simplex/Messaging/Agent/Store.hs index 34c1579ae..d2629333e 100644 --- a/src/Simplex/Messaging/Agent/Store.hs +++ b/src/Simplex/Messaging/Agent/Store.hs @@ -67,7 +67,7 @@ class Monad m => MonadAgentStore s m where createSndMsg :: s -> ConnId -> SndMsgData -> m () getPendingMsgData :: s -> ConnId -> InternalId -> m (Maybe RcvQueue, PendingMsgData) getPendingMsgs :: s -> ConnId -> m [InternalId] - checkRcvMsg :: s -> ConnId -> InternalId -> m () + checkRcvMsg :: s -> ConnId -> InternalId -> m MsgId deleteMsg :: s -> ConnId -> InternalId -> m () -- Double ratchet persistence diff --git a/src/Simplex/Messaging/Agent/Store/SQLite.hs b/src/Simplex/Messaging/Agent/Store/SQLite.hs index ec34ab9a3..0eea2bc11 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite.hs @@ -485,21 +485,18 @@ instance (MonadUnliftIO m, MonadError StoreError m) => MonadAgentStore SQLiteSto map fromOnly <$> DB.query db "SELECT internal_id FROM snd_messages WHERE conn_id = ?" (Only connId) - checkRcvMsg :: SQLiteStore -> ConnId -> InternalId -> m () + checkRcvMsg :: SQLiteStore -> ConnId -> InternalId -> m SMP.MsgId checkRcvMsg st connId msgId = liftIOEither . withTransaction st $ \db -> - hasMsg - <$> DB.query + firstRow fromOnly SEMsgNotFound $ + DB.query db [sql| - SELECT conn_id, internal_id + SELECT broker_id FROM rcv_messages WHERE conn_id = ? AND internal_id = ? |] (connId, msgId) - where - hasMsg :: [(ConnId, InternalId)] -> Either StoreError () - hasMsg r = if null r then Left SEMsgNotFound else Right () deleteMsg :: SQLiteStore -> ConnId -> InternalId -> m () deleteMsg st connId msgId = diff --git a/src/Simplex/Messaging/Client.hs b/src/Simplex/Messaging/Client.hs index e87a56e8c..10fef28de 100644 --- a/src/Simplex/Messaging/Client.hs +++ b/src/Simplex/Messaging/Client.hs @@ -317,9 +317,9 @@ sendSMPMessage c spKey sId flags msg = -- | Acknowledge message delivery (server deletes the message). -- -- https://github.com/simplex-chat/simplexmq/blob/master/protocol/simplex-messaging.md#acknowledge-message-delivery -ackSMPMessage :: SMPClient -> RcvPrivateSignKey -> QueueId -> ExceptT ProtocolClientError IO () -ackSMPMessage c@ProtocolClient {protocolServer, sessionId, msgQ} rpKey rId = - sendSMPCommand c (Just rpKey) rId ACK >>= \case +ackSMPMessage :: SMPClient -> RcvPrivateSignKey -> QueueId -> MsgId -> ExceptT ProtocolClientError IO () +ackSMPMessage c@ProtocolClient {protocolServer, sessionId, msgQ} rpKey rId msgId = + sendSMPCommand c (Just rpKey) rId (ACK msgId) >>= \case OK -> return () cmd@MSG {} -> lift . atomically $ mapM_ (`writeTBQueue` (protocolServer, sessionId, rId, cmd)) msgQ diff --git a/src/Simplex/Messaging/Protocol.hs b/src/Simplex/Messaging/Protocol.hs index 2503bb9f2..bb4d9593a 100644 --- a/src/Simplex/Messaging/Protocol.hs +++ b/src/Simplex/Messaging/Protocol.hs @@ -217,7 +217,9 @@ data Command (p :: Party) where KEY :: SndPublicVerifyKey -> Command Recipient NKEY :: NtfPublicVerifyKey -> Command Recipient GET :: Command Recipient - ACK :: Command Recipient + -- ACK v1 has to be supported for encoding/decoding + -- ACK :: Command Recipient + ACK :: MsgId -> Command Recipient OFF :: Command Recipient DEL :: Command Recipient -- SMP sender commands @@ -608,7 +610,9 @@ instance PartyI p => ProtocolEncoding (Command p) where KEY k -> e (KEY_, ' ', k) NKEY k -> e (NKEY_, ' ', k) GET -> e GET_ - ACK -> e ACK_ + ACK msgId + | v == 1 -> e ACK_ + | otherwise -> e (ACK_, ' ', msgId) OFF -> e OFF_ DEL -> e DEL_ SEND flags msg @@ -653,7 +657,9 @@ instance ProtocolEncoding Cmd where KEY_ -> KEY <$> _smpP NKEY_ -> NKEY <$> _smpP GET_ -> pure GET - ACK_ -> pure ACK + ACK_ + | v == 1 -> pure $ ACK "" + | otherwise -> ACK <$> _smpP OFF_ -> pure OFF DEL_ -> pure DEL CT SSender tag -> diff --git a/src/Simplex/Messaging/Server.hs b/src/Simplex/Messaging/Server.hs index bda7261ad..5b0ee4f5a 100644 --- a/src/Simplex/Messaging/Server.hs +++ b/src/Simplex/Messaging/Server.hs @@ -207,10 +207,11 @@ clientDisconnected c@Client {subscriptions, connected} = do sameClientSession :: Client -> Client -> Bool sameClientSession Client {sessionId} Client {sessionId = s'} = sessionId == s' -cancelSub :: MonadUnliftIO m => Sub -> m () -cancelSub = \case - Sub {subThread = SubThread t} -> killThread t - _ -> return () +cancelSub :: MonadUnliftIO m => TVar Sub -> m () +cancelSub sub = + readTVarIO sub >>= \case + Sub {subThread = SubThread t} -> killThread t + _ -> return () receive :: (Transport c, MonadUnliftIO m, MonadReader Env m) => THandle c -> Client -> m () receive th Client {rcvQ, sndQ, activeAt} = forever $ do @@ -310,7 +311,7 @@ client clnt@Client {subscriptions, ntfSubscriptions, rcvQ, sndQ} Server {subscri (pure (corrId, queueId, ERR AUTH)) SUB -> subscribeQueue queueId GET -> getMessage - ACK -> acknowledgeMsg + ACK msgId -> acknowledgeMsg msgId KEY sKey -> secureQueue_ st sKey NKEY nKey -> addQueueNotifier_ st nKey OFF -> suspendQueue_ st @@ -387,42 +388,55 @@ client clnt@Client {subscriptions, ntfSubscriptions, rcvQ, sndQ} Server {subscri subscribeQueue :: RecipientId -> m (Transmission BrokerMsg) subscribeQueue rId = - atomically (getSubscription rId) >>= \case - Just s -> deliverMessage tryPeekMsg rId s - -- cannot use SUB in the same connection where GET was used - _ -> pure (corrId, rId, ERR $ CMD PROHIBITED) - - getSubscription :: RecipientId -> STM (Maybe Sub) - getSubscription rId = do - TM.lookup rId subscriptions >>= \case - Just Sub {subThread = ProhibitSub} -> pure Nothing - Just s -> tryTakeTMVar (delivered s) $> Just s - Nothing -> do + atomically (TM.lookup rId subscriptions) >>= \case + Nothing -> + atomically newSub >>= deliver + Just sub -> + readTVarIO sub >>= \case + Sub {subThread = ProhibitSub} -> + -- cannot use SUB in the same connection where GET was used + pure (corrId, rId, ERR $ CMD PROHIBITED) + s -> + atomically (tryTakeTMVar $ delivered s) >> deliver sub + where + newSub :: STM (TVar Sub) + newSub = do writeTBQueue subscribedQ (rId, clnt) - s <- newSubscription - TM.insert rId s subscriptions - pure $ Just s + sub <- newTVar =<< newSubscription NoSub + TM.insert rId sub subscriptions + pure sub + deliver :: TVar Sub -> m (Transmission BrokerMsg) + deliver sub = do + q <- getStoreMsgQueue rId + msg_ <- atomically $ tryPeekMsg q + deliverMessage rId sub q msg_ getMessage :: m (Transmission BrokerMsg) getMessage = - atomically getProhibitedSub >>= \case - Just s -> do + atomically (TM.lookup queueId subscriptions) >>= \case + Nothing -> + atomically newSub >>= getMessage_ + Just sub -> + readTVarIO sub >>= \case + s@Sub {subThread = ProhibitSub} -> + atomically (tryTakeTMVar $ delivered s) + >> getMessage_ s + -- cannot use GET in the same connection where there is an active subscription + _ -> pure (corrId, queueId, ERR $ CMD PROHIBITED) + where + newSub :: STM Sub + newSub = do + s <- newSubscription ProhibitSub + sub <- newTVar s + TM.insert queueId sub subscriptions + pure s + getMessage_ :: Sub -> m (Transmission BrokerMsg) + getMessage_ s = do q <- getStoreMsgQueue queueId atomically $ tryPeekMsg q >>= \case - Just msg -> tryPutTMVar (delivered s) () $> (corrId, queueId, msgCmd msg) + Just msg -> setDelivered s msg $> (corrId, queueId, msgCmd msg) _ -> pure (corrId, queueId, ERR NO_MSG) - _ -> pure (corrId, queueId, ERR $ CMD PROHIBITED) -- cannot use GET in the same connection where there is an active subscription - where - getProhibitedSub :: STM (Maybe Sub) - getProhibitedSub = - TM.lookup queueId subscriptions >>= \case - Just s@Sub {subThread = ProhibitSub} -> tryTakeTMVar (delivered s) $> Just s - Just _ -> pure Nothing - Nothing -> do - s <- prohibitedSubscription - TM.insert queueId s subscriptions - pure $ Just s subscribeNotifications :: m (Transmission BrokerMsg) subscribeNotifications = atomically $ do @@ -431,23 +445,37 @@ client clnt@Client {subscriptions, ntfSubscriptions, rcvQ, sndQ} Server {subscri TM.insert queueId () ntfSubscriptions pure ok - acknowledgeMsg :: m (Transmission BrokerMsg) - acknowledgeMsg = - atomically (withSub queueId $ \s -> const s <$$> tryTakeTMVar (delivered s)) - >>= \case - Just (Just s) -> do - stats <- asks serverStats - atomically $ modifyTVar (msgRecv stats) (+ 1) - atomically $ modifyTVar (msgQueues stats) (S.insert queueId) - case s of - Sub {subThread = ProhibitSub} -> - (getStoreMsgQueue queueId >>= atomically . tryDelMsg) $> ok - _ -> - deliverMessage tryDelPeekMsg queueId s - _ -> return $ err NO_MSG - - withSub :: RecipientId -> (Sub -> STM a) -> STM (Maybe a) - withSub rId f = mapM f =<< TM.lookup rId subscriptions + acknowledgeMsg :: MsgId -> m (Transmission BrokerMsg) + acknowledgeMsg msgId = do + atomically (TM.lookup queueId subscriptions) >>= \case + Nothing -> pure $ err NO_MSG + Just sub -> + atomically (getDelivered sub) >>= \case + Just s -> do + q <- getStoreMsgQueue queueId + case s of + Sub {subThread = ProhibitSub} -> do + msgDeleted <- atomically $ tryDelMsg q msgId + when msgDeleted updateStats + pure ok + _ -> do + (msgDeleted, msg_) <- atomically $ tryDelPeekMsg q msgId + when msgDeleted updateStats + deliverMessage queueId sub q msg_ + _ -> pure $ err NO_MSG + where + getDelivered :: TVar Sub -> STM (Maybe Sub) + getDelivered sub = do + s@Sub {delivered} <- readTVar sub + tryTakeTMVar delivered $>>= \msgId' -> + if B.null msgId || msgId == msgId' + then pure $ Just s + else putTMVar delivered msgId' $> Nothing + updateStats :: m () + updateStats = do + stats <- asks serverStats + atomically $ modifyTVar (msgRecv stats) (+ 1) + atomically $ modifyTVar (msgQueues stats) (S.insert queueId) sendMessage :: QueueStore -> MsgFlags -> MsgBody -> m (Transmission BrokerMsg) sendMessage st flags msgBody @@ -496,35 +524,33 @@ client clnt@Client {subscriptions, ntfSubscriptions, rcvQ, sndQ} Server {subscri unlessM (isFullTBQueue sndQ) $ writeTBQueue q (CorrId "", nId, NMSG) - deliverMessage :: (MsgQueue -> STM (Maybe Message)) -> RecipientId -> Sub -> m (Transmission BrokerMsg) - deliverMessage tryPeek rId = \case - Sub {subThread = NoSub} -> do - q <- getStoreMsgQueue rId - atomically (tryPeek q) >>= \case - Nothing -> forkSub q $> ok - Just msg -> atomically setDelivered $> (corrId, rId, msgCmd msg) - _ -> pure ok + deliverMessage :: RecipientId -> TVar Sub -> MsgQueue -> Maybe Message -> m (Transmission BrokerMsg) + deliverMessage rId sub q msg_ = + readTVarIO sub >>= \case + s@Sub {subThread = NoSub} -> + case msg_ of + Just msg -> atomically (setDelivered s msg) $> (corrId, rId, msgCmd msg) + _ -> forkSub $> ok + _ -> pure ok where - forkSub :: MsgQueue -> m () - forkSub q = do - atomically . setSub $ \s -> s {subThread = SubPending} - t <- forkIO $ subscriber q - atomically . setSub $ \case + forkSub :: m () + forkSub = do + atomically . modifyTVar sub $ \s -> s {subThread = SubPending} + t <- forkIO subscriber + atomically . modifyTVar sub $ \case s@Sub {subThread = SubPending} -> s {subThread = SubThread t} s -> s - subscriber :: MsgQueue -> m () - subscriber q = atomically $ do + subscriber :: m () + subscriber = atomically $ do msg <- peekMsg q writeTBQueue sndQ (CorrId "", rId, msgCmd msg) - setSub (\s -> s {subThread = NoSub}) - void setDelivered + s <- readTVar sub + void $ setDelivered s msg + writeTVar sub s {subThread = NoSub} - setSub :: (Sub -> Sub) -> STM () - setSub f = TM.adjust f rId subscriptions - - setDelivered :: STM (Maybe Bool) - setDelivered = withSub rId $ \s -> tryPutTMVar (delivered s) () + setDelivered :: Sub -> Message -> STM Bool + setDelivered s Message {msgId} = tryPutTMVar (delivered s) msgId getStoreMsgQueue :: RecipientId -> m MsgQueue getStoreMsgQueue rId = do diff --git a/src/Simplex/Messaging/Server/Env/STM.hs b/src/Simplex/Messaging/Server/Env/STM.hs index 3bec11ee4..35f0dc051 100644 --- a/src/Simplex/Messaging/Server/Env/STM.hs +++ b/src/Simplex/Messaging/Server/Env/STM.hs @@ -94,7 +94,7 @@ data Server = Server } data Client = Client - { subscriptions :: TMap RecipientId Sub, + { subscriptions :: TMap RecipientId (TVar Sub), ntfSubscriptions :: TMap NotifierId (), rcvQ :: TBQueue (Transmission Cmd), sndQ :: TBQueue (Transmission BrokerMsg), @@ -117,7 +117,7 @@ data SubscriptionThread = NoSub | SubPending | SubThread ThreadId | ProhibitSub data Sub = Sub { subThread :: SubscriptionThread, - delivered :: TMVar () + delivered :: TMVar MsgId } newServer :: Natural -> STM Server @@ -149,14 +149,8 @@ newServerStats ts = do fromTime <- newTVar ts pure ServerStats {qCreated, qSecured, qDeleted, msgSent, msgRecv, msgQueues, fromTime} -newSubscription :: STM Sub -newSubscription = newSubscription_ NoSub - -prohibitedSubscription :: STM Sub -prohibitedSubscription = newSubscription_ ProhibitSub - -newSubscription_ :: SubscriptionThread -> STM Sub -newSubscription_ subThread = do +newSubscription :: SubscriptionThread -> STM Sub +newSubscription subThread = do delivered <- newEmptyTMVar return Sub {subThread, delivered} diff --git a/src/Simplex/Messaging/Server/MsgStore.hs b/src/Simplex/Messaging/Server/MsgStore.hs index c4f36acb5..56b86cc49 100644 --- a/src/Simplex/Messaging/Server/MsgStore.hs +++ b/src/Simplex/Messaging/Server/MsgStore.hs @@ -23,6 +23,6 @@ class MonadMsgQueue q m where writeMsg :: q -> Message -> m () -- non blocking tryPeekMsg :: q -> m (Maybe Message) -- non blocking peekMsg :: q -> m Message -- blocking - tryDelMsg :: q -> m () -- non blocking - tryDelPeekMsg :: q -> m (Maybe Message) -- atomic delete (== read) last and peek next message, if available + tryDelMsg :: q -> MsgId -> m Bool -- non blocking + tryDelPeekMsg :: q -> MsgId -> m (Bool, Maybe Message) -- atomic delete (== read) last and peek next message, if available deleteExpiredMsgs :: q -> Int64 -> m () diff --git a/src/Simplex/Messaging/Server/MsgStore/STM.hs b/src/Simplex/Messaging/Server/MsgStore/STM.hs index 06048b8e1..ae0ae60fd 100644 --- a/src/Simplex/Messaging/Server/MsgStore/STM.hs +++ b/src/Simplex/Messaging/Server/MsgStore/STM.hs @@ -2,16 +2,19 @@ {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE InstanceSigs #-} +{-# LANGUAGE LambdaCase #-} {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE NamedFieldPuns #-} +{-# LANGUAGE TupleSections #-} module Simplex.Messaging.Server.MsgStore.STM where -import Control.Monad (void, when) +import Control.Monad (when) +import Data.Functor (($>)) import Data.Int (Int64) import Data.Time.Clock.System (SystemTime (systemSeconds)) import Numeric.Natural -import Simplex.Messaging.Protocol (RecipientId) +import Simplex.Messaging.Protocol (MsgId, RecipientId) import Simplex.Messaging.Server.MsgStore import Simplex.Messaging.TMap (TMap) import qualified Simplex.Messaging.TMap as TM @@ -49,12 +52,22 @@ instance MonadMsgQueue MsgQueue STM where peekMsg :: MsgQueue -> STM Message peekMsg = peekTBQueue . msgQueue - tryDelMsg :: MsgQueue -> STM () - tryDelMsg = void . tryReadTBQueue . msgQueue + tryDelMsg :: MsgQueue -> MsgId -> STM Bool + tryDelMsg (MsgQueue q) msgId' = + tryPeekTBQueue q >>= \case + Just Message {msgId} + | msgId == msgId' -> tryReadTBQueue q $> True + | otherwise -> pure False + _ -> pure False -- atomic delete (== read) last and peek next message if available - tryDelPeekMsg :: MsgQueue -> STM (Maybe Message) - tryDelPeekMsg (MsgQueue q) = tryReadTBQueue q >> tryPeekTBQueue q + tryDelPeekMsg :: MsgQueue -> MsgId -> STM (Bool, Maybe Message) + tryDelPeekMsg (MsgQueue q) msgId' = + tryPeekTBQueue q >>= \case + msg_@(Just Message {msgId}) + | msgId == msgId' -> (True,) <$> (tryReadTBQueue q >> tryPeekTBQueue q) + | otherwise -> pure (False, msg_) + _ -> pure (False, Nothing) deleteExpiredMsgs :: MsgQueue -> Int64 -> STM () deleteExpiredMsgs (MsgQueue q) old = loop diff --git a/tests/ServerTests.hs b/tests/ServerTests.hs index 725688ba8..92b737768 100644 --- a/tests/ServerTests.hs +++ b/tests/ServerTests.hs @@ -41,6 +41,8 @@ serverTests t@(ATransport t') = do describe "SMP messages" $ do describe "duplex communication over 2 SMP connections" $ testDuplex t describe "switch subscription to another TCP connection" $ testSwitchSub t + describe "GET command" $ testGetCommand t' + describe "GET & SUB commands" $ testGetSubCommands t' describe "Store log" $ testWithStoreLog t describe "Timing of AUTH error" $ testTiming t describe "Message notifications" $ testMessageNotifications t @@ -91,10 +93,10 @@ testCreateSecure (ATransport t) = Resp "" _ (MSG mId1 _ _ msg1) <- tGet h (dec mId1 msg1, Right "hello") #== "delivers message" - Resp "cdab" _ ok4 <- signSendRecv h rKey ("cdab", rId, ACK) + Resp "cdab" _ ok4 <- signSendRecv h rKey ("cdab", rId, ACK mId1) (ok4, OK) #== "replies OK when message acknowledged if no more messages" - Resp "dabc" _ err6 <- signSendRecv h rKey ("dabc", rId, ACK) + Resp "dabc" _ err6 <- signSendRecv h rKey ("dabc", rId, ACK mId1) (err6, ERR NO_MSG) #== "replies ERR when message acknowledged without messages" (sPub, sKey) <- C.generateSignatureKeyPair C.SEd448 @@ -121,7 +123,7 @@ testCreateSecure (ATransport t) = Resp "" _ (MSG mId2 _ _ msg2) <- tGet h (dec mId2 msg2, Right "hello again") #== "delivers message 2" - Resp "cdab" _ ok5 <- signSendRecv h rKey ("cdab", rId, ACK) + Resp "cdab" _ ok5 <- signSendRecv h rKey ("cdab", rId, ACK mId2) (ok5, OK) #== "replies OK when message acknowledged 2" Resp "dabc" _ err5 <- sendRecv h ("", "dabc", sId, _SEND "hello") @@ -188,7 +190,7 @@ testCreateDelete (ATransport t) = Resp "dabc" _ err8 <- sendRecv sh ("", "dabc", sId, _SEND "hello") (err8, ERR AUTH) #== "rejects unsigned SEND too when deleted" - Resp "abcd" _ err11 <- signSendRecv rh rKey ("abcd", rId, ACK) + Resp "abcd" _ err11 <- signSendRecv rh rKey ("abcd", rId, ACK "") (err11, ERR AUTH) #== "rejects ACK when conn deleted - the second message is deleted" Resp "bcda" _ err9 <- signSendRecv rh rKey ("bcda", rId, OFF) @@ -239,7 +241,7 @@ testDuplex (ATransport t) = -- "key ..." is ad-hoc, not a part of SMP protocol Resp "" _ (MSG mId1 _ _ msg1) <- tGet alice - Resp "cdab" _ OK <- signSendRecv alice arKey ("cdab", aRcv, ACK) + Resp "cdab" _ OK <- signSendRecv alice arKey ("cdab", aRcv, ACK mId1) Right ["key", bobKey] <- pure $ B.words <$> aDec mId1 msg1 (bobKey, strEncode bsPub) #== "key received from Bob" Resp "dabc" _ OK <- signSendRecv alice arKey ("dabc", aRcv, KEY bsPub) @@ -252,7 +254,7 @@ testDuplex (ATransport t) = -- "reply_id ..." is ad-hoc, not a part of SMP protocol Resp "" _ (MSG mId2 _ _ msg2) <- tGet alice - Resp "cdab" _ OK <- signSendRecv alice arKey ("cdab", aRcv, ACK) + Resp "cdab" _ OK <- signSendRecv alice arKey ("cdab", aRcv, ACK mId2) Right ["reply_id", bId] <- pure $ B.words <$> aDec mId2 msg2 (bId, encode bSnd) #== "reply queue ID received from Bob" @@ -261,7 +263,7 @@ testDuplex (ATransport t) = -- "key ..." is ad-hoc, not a part of SMP protocol Resp "" _ (MSG mId3 _ _ msg3) <- tGet bob - Resp "abcd" _ OK <- signSendRecv bob brKey ("abcd", bRcv, ACK) + Resp "abcd" _ OK <- signSendRecv bob brKey ("abcd", bRcv, ACK mId3) Right ["key", aliceKey] <- pure $ B.words <$> bDec mId3 msg3 (aliceKey, strEncode asPub) #== "key received from Alice" Resp "bcda" _ OK <- signSendRecv bob brKey ("bcda", bRcv, KEY asPub) @@ -269,13 +271,13 @@ testDuplex (ATransport t) = Resp "cdab" _ OK <- signSendRecv bob bsKey ("cdab", aSnd, _SEND "hi alice") Resp "" _ (MSG mId4 _ _ msg4) <- tGet alice - Resp "dabc" _ OK <- signSendRecv alice arKey ("dabc", aRcv, ACK) + Resp "dabc" _ OK <- signSendRecv alice arKey ("dabc", aRcv, ACK mId4) (aDec mId4 msg4, Right "hi alice") #== "message received from Bob" Resp "abcd" _ OK <- signSendRecv alice asKey ("abcd", bSnd, _SEND "how are you bob") Resp "" _ (MSG mId5 _ _ msg5) <- tGet bob - Resp "bcda" _ OK <- signSendRecv bob brKey ("bcda", bRcv, ACK) + Resp "bcda" _ OK <- signSendRecv bob brKey ("bcda", bRcv, ACK mId5) (bDec mId5 msg5, Right "how are you bob") #== "message received from alice" testSwitchSub :: ATransport -> Spec @@ -293,12 +295,12 @@ testSwitchSub (ATransport t) = Resp "" _ (MSG mId1 _ _ msg1) <- tGet rh1 (dec mId1 msg1, Right "test1") #== "test message 1 delivered to the 1st TCP connection" - Resp "abcd" _ (MSG mId2 _ _ msg2) <- signSendRecv rh1 rKey ("abcd", rId, ACK) + Resp "abcd" _ (MSG mId2 _ _ msg2) <- signSendRecv rh1 rKey ("abcd", rId, ACK mId1) (dec mId2 msg2, Right "test2, no ACK") #== "test message 2 delivered, no ACK" Resp "bcda" _ (MSG mId2' _ _ msg2') <- signSendRecv rh2 rKey ("bcda", rId, SUB) (dec mId2' msg2', Right "test2, no ACK") #== "same simplex queue via another TCP connection, tes2 delivered again (no ACK in 1st queue)" - Resp "cdab" _ OK <- signSendRecv rh2 rKey ("cdab", rId, ACK) + Resp "cdab" _ OK <- signSendRecv rh2 rKey ("cdab", rId, ACK mId2') Resp "" _ end <- tGet rh1 (end, END) #== "unsubscribed the 1st TCP connection" @@ -308,16 +310,82 @@ testSwitchSub (ATransport t) = Resp "" _ (MSG mId3 _ _ msg3) <- tGet rh2 (dec mId3 msg3, Right "test3") #== "delivered to the 2nd TCP connection" - Resp "abcd" _ err <- signSendRecv rh1 rKey ("abcd", rId, ACK) + Resp "abcd" _ err <- signSendRecv rh1 rKey ("abcd", rId, ACK mId3) (err, ERR NO_MSG) #== "rejects ACK from the 1st TCP connection" - Resp "bcda" _ ok3 <- signSendRecv rh2 rKey ("bcda", rId, ACK) + Resp "bcda" _ ok3 <- signSendRecv rh2 rKey ("bcda", rId, ACK mId3) (ok3, OK) #== "accepts ACK from the 2nd TCP connection" 1000 `timeout` tGet @BrokerMsg rh1 >>= \case Nothing -> return () Just _ -> error "nothing else is delivered to the 1st TCP connection" +testGetCommand :: forall c. Transport c => TProxy c -> Spec +testGetCommand t = + it "should retrieve messages from the queue using GET command" $ do + (sPub, sKey) <- C.generateSignatureKeyPair C.SEd25519 + smpTest t $ \sh -> do + queue <- newEmptyTMVarIO + testSMPClient @c $ \rh -> + atomically . putTMVar queue =<< createAndSecureQueue rh sPub + testSMPClient @c $ \rh -> do + (sId, rId, rKey, dhShared) <- atomically $ takeTMVar queue + let dec nonce = C.cbDecrypt dhShared (C.cbNonce nonce) + Resp "1" _ OK <- signSendRecv sh sKey ("1", sId, _SEND "hello") + Resp "2" _ (MSG mId1 _ _ msg1) <- signSendRecv rh rKey ("2", rId, GET) + (dec mId1 msg1, Right "hello") #== "retrieved from queue" + Resp "3" _ OK <- signSendRecv rh rKey ("3", rId, ACK mId1) + pure () + +testGetSubCommands :: forall c. Transport c => TProxy c -> Spec +testGetSubCommands t = + it "should retrieve messages with GET and receive with SUB, only one ACK would work" $ do + (sPub, sKey) <- C.generateSignatureKeyPair C.SEd25519 + smpTest3 t $ \rh1 rh2 sh -> do + (sId, rId, rKey, dhShared) <- createAndSecureQueue rh1 sPub + let dec nonce = C.cbDecrypt dhShared (C.cbNonce nonce) + Resp "1" _ OK <- signSendRecv sh sKey ("1", sId, _SEND "hello 1") + Resp "1a" _ OK <- signSendRecv sh sKey ("1a", sId, _SEND "hello 2") + Resp "1b" _ OK <- signSendRecv sh sKey ("1b", sId, _SEND "hello 3") + Resp "1c" _ OK <- signSendRecv sh sKey ("1c", sId, _SEND "hello 4") + -- both get the same if not ACK'd + Resp "" _ (MSG mId1 _ _ msg1) <- tGet rh1 + Resp "2" _ (MSG mId1' _ _ msg1') <- signSendRecv rh2 rKey ("2", rId, GET) + (dec mId1 msg1, Right "hello 1") #== "received from queue via SUB" + (dec mId1' msg1', Right "hello 1") #== "retrieved from queue with GET" + mId1 `shouldBe` mId1' + msg1 `shouldBe` msg1' + -- subscriber cannot GET, getter cannot SUB + Resp "3" _ (ERR (CMD PROHIBITED)) <- signSendRecv rh1 rKey ("3", rId, GET) + Resp "3a" _ (ERR (CMD PROHIBITED)) <- signSendRecv rh2 rKey ("3a", rId, SUB) + -- ACK for SUB delivers the next message + Resp "4" _ (MSG mId2 _ _ msg2) <- signSendRecv rh1 rKey ("4", rId, ACK mId1) + (dec mId2 msg2, Right "hello 2") #== "received from queue via SUB" + -- bad msgId returns error + Resp "5" _ (ERR NO_MSG) <- signSendRecv rh2 rKey ("5", rId, ACK "1234") + -- already ACK'd by subscriber, but still returns OK when msgId matches + Resp "5a" _ OK <- signSendRecv rh2 rKey ("5a", rId, ACK mId1) + -- msg2 is not lost - even if subscriber does not ACK it, it is delivered to getter + Resp "6" _ (MSG mId2' _ _ msg2') <- signSendRecv rh2 rKey ("6", rId, GET) + (dec mId2' msg2', Right "hello 2") #== "retrieved from queue with GET" + mId2 `shouldBe` mId2' + msg2 `shouldBe` msg2' + -- getter ACK returns OK, even though there is the next message + Resp "7" _ OK <- signSendRecv rh2 rKey ("7", rId, ACK mId2') + Resp "8" _ (MSG mId3 _ _ msg3) <- signSendRecv rh2 rKey ("8", rId, GET) + (dec mId3 msg3, Right "hello 3") #== "retrieved from queue with GET" + -- subscriber ACK does not lose message + Resp "9" _ (MSG mId3' _ _ msg3') <- signSendRecv rh1 rKey ("9", rId, ACK mId2') + (dec mId3' msg3', Right "hello 3") #== "retrieved from queue with GET" + mId3 `shouldBe` mId3' + msg3 `shouldBe` msg3' + Resp "10" _ (MSG mId4 _ _ msg4) <- signSendRecv rh1 rKey ("10", rId, ACK mId3) + (dec mId4 msg4, Right "hello 4") #== "retrieved from queue with GET" + Resp "11" _ OK <- signSendRecv rh1 rKey ("11", rId, ACK mId4) + -- no more messages for getter too + Resp "12" _ (ERR NO_MSG) <- signSendRecv rh2 rKey ("12", rId, GET) + pure () + testWithStoreLog :: ATransport -> Spec testWithStoreLog at@(ATransport t) = it "should store simplex queues to log and restore them after server restart" $ do @@ -474,7 +542,7 @@ testMessageNotifications (ATransport t) = Resp "3" _ OK <- signSendRecv sh sKey ("3", sId, _SEND "hello") Resp "" _ (MSG mId1 _ _ msg1) <- tGet rh (dec mId1 msg1, Right "hello") #== "delivered from queue" - Resp "3a" _ OK <- signSendRecv rh rKey ("3a", rId, ACK) + Resp "3a" _ OK <- signSendRecv rh rKey ("3a", rId, ACK mId1) Resp "" _ NMSG <- tGet nh1 Resp "4" _ OK <- signSendRecv nh2 nKey ("4", nId, NSUB) Resp "" _ END <- tGet nh1 @@ -563,13 +631,17 @@ syntaxTests (ATransport t) = do it "no signature" $ ("", "abcd", "12345678", (KEY_, ' ', samplePubKey)) >#> ("", "abcd", "12345678", ERR $ CMD NO_AUTH) it "no queue ID" $ (sampleSig, "bcda", "", (KEY_, ' ', samplePubKey)) >#> ("", "bcda", "", ERR $ CMD NO_AUTH) noParamsSyntaxTest "SUB" SUB_ - noParamsSyntaxTest "ACK" ACK_ noParamsSyntaxTest "OFF" OFF_ noParamsSyntaxTest "DEL" DEL_ describe "SEND" $ do it "valid syntax" $ (sampleSig, "cdab", "12345678", (SEND_, ' ', noMsgFlags, ' ', "hello" :: ByteString)) >#> ("", "cdab", "12345678", ERR AUTH) it "no parameters" $ (sampleSig, "abcd", "12345678", SEND_) >#> ("", "abcd", "12345678", ERR $ CMD SYNTAX) it "no queue ID" $ (sampleSig, "bcda", "", (SEND_, ' ', noMsgFlags, ' ', "hello" :: ByteString)) >#> ("", "bcda", "", ERR $ CMD NO_ENTITY) + describe "ACK" $ do + it "valid syntax" $ (sampleSig, "cdab", "12345678", (ACK_, ' ', "1234" :: ByteString)) >#> ("", "cdab", "12345678", ERR AUTH) + it "no parameters" $ (sampleSig, "abcd", "12345678", ACK_) >#> ("", "abcd", "12345678", ERR $ CMD SYNTAX) + it "no queue ID" $ (sampleSig, "bcda", "", (ACK_, ' ', "1234" :: ByteString)) >#> ("", "bcda", "", ERR $ CMD NO_AUTH) + it "no signature" $ ("", "cdab", "12345678", (ACK_, ' ', "1234" :: ByteString)) >#> ("", "cdab", "12345678", ERR $ CMD NO_AUTH) describe "PING" $ do it "valid syntax" $ ("", "abcd", "", PING_) >#> ("", "abcd", "", PONG) describe "broker response not allowed" $ do From 3f69636f1a5f48292b6155195eb6840789364f86 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Tue, 7 Jun 2022 11:52:32 +0100 Subject: [PATCH 044/131] fix sockets/threads/memory leak (#388) * fix sockets/threads/memory leak * refactor --- src/Simplex/Messaging/Transport/Server.hs | 40 ++++++++++------------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/src/Simplex/Messaging/Transport/Server.hs b/src/Simplex/Messaging/Transport/Server.hs index c2e12aff0..3c1604a2f 100644 --- a/src/Simplex/Messaging/Transport/Server.hs +++ b/src/Simplex/Messaging/Transport/Server.hs @@ -12,20 +12,22 @@ module Simplex.Messaging.Transport.Server ) where +import Control.Concurrent.STM (stateTVar) import Control.Monad.Except import Control.Monad.IO.Unlift import qualified Crypto.Store.X509 as SX import Data.Default (def) -import Data.Set (Set) -import qualified Data.Set as S import qualified Data.X509 as X import Data.X509.Validation (Fingerprint (..)) import qualified Data.X509.Validation as XV import Network.Socket import qualified Network.TLS as T +import Simplex.Messaging.TMap (TMap) +import qualified Simplex.Messaging.TMap as TM import Simplex.Messaging.Transport import Simplex.Messaging.Util (catchAll_) import System.Exit (exitFailure) +import System.Mem.Weak (Weak, deRefWeak) import UnliftIO.Concurrent import qualified UnliftIO.Exception as E import UnliftIO.STM @@ -36,37 +38,29 @@ import UnliftIO.STM runTransportServer :: forall c m. (Transport c, MonadUnliftIO m) => TMVar Bool -> ServiceName -> T.ServerParams -> (c -> m ()) -> m () runTransportServer started port serverParams server = do u <- askUnliftIO - liftIO $ do - clients <- newTVarIO S.empty + liftIO . runTCPServer started port $ \conn -> E.bracket - (startTCPServer started port) - (closeServer started clients) - $ \sock -> forever . E.bracketOnError (accept sock) (close . fst) $ \(conn, _peer) -> do - -- catchAll_ is needed here in case the connection was closed earlier - tid <- forkFinally (connectClient u conn) (const . liftIO $ gracefulClose conn 5000 `catchAll_` pure ()) - atomically . modifyTVar' clients $ S.insert tid - where - connectClient :: UnliftIO m -> Socket -> IO () - connectClient u conn = - E.bracket - (connectTLS serverParams conn >>= getServerConnection) - closeConnection - (unliftIO u . server) + (connectTLS serverParams conn >>= getServerConnection) + closeConnection + (unliftIO u . server) --- | Run TCP server without TLS - only used in SimpleX Chat +-- | Run TCP server without TLS runTCPServer :: TMVar Bool -> ServiceName -> (Socket -> IO ()) -> IO () runTCPServer started port server = do - clients <- newTVarIO S.empty + clients <- atomically TM.empty + clientId <- newTVarIO 0 E.bracket (startTCPServer started port) (closeServer started clients) $ \sock -> forever . E.bracketOnError (accept sock) (close . fst) $ \(conn, _peer) -> do - tid <- forkFinally (server conn) (const $ gracefulClose conn 5000) - atomically . modifyTVar' clients $ S.insert tid + -- catchAll_ is needed here in case the connection was closed earlier + cId <- atomically $ stateTVar clientId $ \cId -> (cId + 1, cId + 1) + tId <- mkWeakThreadId =<< forkFinally (server conn) (const $ gracefulClose conn 5000 `catchAll_` atomically (TM.delete cId clients)) + atomically $ TM.insert cId tId clients -closeServer :: TMVar Bool -> TVar (Set ThreadId) -> Socket -> IO () +closeServer :: TMVar Bool -> TMap Int (Weak ThreadId) -> Socket -> IO () closeServer started clients sock = do - readTVarIO clients >>= mapM_ killThread + readTVarIO clients >>= mapM_ (deRefWeak >=> mapM_ killThread) close sock void . atomically $ tryPutTMVar started False From a3d1f5540d55513fd00a8d7f4a43c0ac10a1145e Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Tue, 7 Jun 2022 11:55:28 +0100 Subject: [PATCH 045/131] v2.2.0 --- CHANGELOG.md | 10 ++++++++++ package.yaml | 2 +- simplexmq.cabal | 2 +- src/Simplex/Messaging/Transport.hs | 2 +- 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c7af5a10a..048edcb25 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,13 @@ +# 2.2.0 + +SMP server: + +- Fix sockets/threads/memory leak + +SMP agent: + +- Support stopping and resuming agent with `disconnectAgentClient` / `resumeAgentClient` + # 2.1.1 SMP server: diff --git a/package.yaml b/package.yaml index 177d4ad86..4f05628ea 100644 --- a/package.yaml +++ b/package.yaml @@ -1,5 +1,5 @@ name: simplexmq -version: 2.1.1 +version: 2.2.0 synopsis: SimpleXMQ message broker description: | This package includes <./docs/Simplex-Messaging-Server.html server>, diff --git a/simplexmq.cabal b/simplexmq.cabal index 14abecc27..5c992e587 100644 --- a/simplexmq.cabal +++ b/simplexmq.cabal @@ -5,7 +5,7 @@ cabal-version: 1.12 -- see: https://github.com/sol/hpack name: simplexmq -version: 2.1.1 +version: 2.2.0 synopsis: SimpleXMQ message broker description: This package includes <./docs/Simplex-Messaging-Server.html server>, <./docs/Simplex-Messaging-Client.html client> and diff --git a/src/Simplex/Messaging/Transport.hs b/src/Simplex/Messaging/Transport.hs index 9f58e5441..5971c9314 100644 --- a/src/Simplex/Messaging/Transport.hs +++ b/src/Simplex/Messaging/Transport.hs @@ -96,7 +96,7 @@ supportedSMPVersions :: VersionRange supportedSMPVersions = mkVersionRange 1 1 simplexMQVersion :: String -simplexMQVersion = "2.1.1" +simplexMQVersion = "2.2.0" -- * Transport connection class From bfb556c8606c473609f7f165af0f051d25fd9321 Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Tue, 7 Jun 2022 19:14:51 +0400 Subject: [PATCH 046/131] ntf: test notification subscription (#389) --- .../Messaging/Notifications/Protocol.hs | 2 + src/Simplex/Messaging/Notifications/Server.hs | 11 +-- .../Messaging/Notifications/Server/Env.hs | 2 +- .../Messaging/Notifications/Server/Store.hs | 14 ++- tests/AgentTests.hs | 2 + tests/NtfClient.hs | 25 ++++- tests/NtfServerTests.hs | 97 ++++++++++++++++++- tests/Test.hs | 5 +- 8 files changed, 135 insertions(+), 23 deletions(-) diff --git a/src/Simplex/Messaging/Notifications/Protocol.hs b/src/Simplex/Messaging/Notifications/Protocol.hs index f997c3c44..2c33ab86a 100644 --- a/src/Simplex/Messaging/Notifications/Protocol.hs +++ b/src/Simplex/Messaging/Notifications/Protocol.hs @@ -126,6 +126,8 @@ deriving instance Show (NewNtfEntity e) data ANewNtfEntity = forall e. NtfEntityI e => ANE (SNtfEntity e) (NewNtfEntity e) +deriving instance Show ANewNtfEntity + instance NtfEntityI e => Encoding (NewNtfEntity e) where smpEncode = \case NewNtfTkn tkn verifyKey dhPubKey -> smpEncode ('T', tkn, verifyKey, dhPubKey) diff --git a/src/Simplex/Messaging/Notifications/Server.hs b/src/Simplex/Messaging/Notifications/Server.hs index e1011b290..393ef56de 100644 --- a/src/Simplex/Messaging/Notifications/Server.hs +++ b/src/Simplex/Messaging/Notifications/Server.hs @@ -108,12 +108,6 @@ ntfSubscriber NtfSubscriber {smpSubscribers, newSubQ, smpAgent = ca@SMPClientAge (srv, _sessId, ntfId, msg) <- atomically $ readTBQueue msgQ case msg of SMP.NMSG -> do - -- check when the last NMSG was received from this queue - -- update timestamp - -- check what was the last hidden notification was sent (and whether to this queue) - -- decide whether it should be sent as hidden or visible - -- construct and possibly encrypt notification - -- send it NtfPushServer {pushQ} <- asks pushServer st <- asks store atomically $ @@ -149,6 +143,8 @@ ntfPush s@NtfPushServer {pushQ} = liftIO . forever . runExceptT $ do atomically $ modifyTVar tknStatus $ \status' -> if status' == NTActive then NTActive else NTConfirmed (NTActive, PNCheckMessages) -> do deliverNotification pp tkn ntf + (NTActive, PNMessage _ _) -> do + deliverNotification pp tkn ntf _ -> do logError "bad notification token status" where @@ -194,6 +190,9 @@ send h@THandle {thVersion = v} NtfServerClient {sndQ, sessionId, activeAt} = for void . liftIO $ tPut h (Nothing, encodeTransmission v sessionId t) atomically . writeTVar activeAt =<< liftIO getSystemTime +-- instance Show a => Show (TVar a) where +-- show x = unsafePerformIO $ show <$> readTVarIO x + data VerificationResult = VRVerified NtfRequest | VRFailed verifyNtfTransmission :: diff --git a/src/Simplex/Messaging/Notifications/Server/Env.hs b/src/Simplex/Messaging/Notifications/Server/Env.hs index 26a2a7de9..2605f51d1 100644 --- a/src/Simplex/Messaging/Notifications/Server/Env.hs +++ b/src/Simplex/Messaging/Notifications/Server/Env.hs @@ -23,7 +23,7 @@ import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Notifications.Protocol import Simplex.Messaging.Notifications.Server.Push.APNS import Simplex.Messaging.Notifications.Server.Store -import Simplex.Messaging.Protocol (CorrId, Transmission, SMPServer) +import Simplex.Messaging.Protocol (CorrId, SMPServer, Transmission) import Simplex.Messaging.Server.Expiration import Simplex.Messaging.TMap (TMap) import qualified Simplex.Messaging.TMap as TM diff --git a/src/Simplex/Messaging/Notifications/Server/Store.hs b/src/Simplex/Messaging/Notifications/Server/Store.hs index 3497cad93..c7d9ac89f 100644 --- a/src/Simplex/Messaging/Notifications/Server/Store.hs +++ b/src/Simplex/Messaging/Notifications/Server/Store.hs @@ -6,7 +6,6 @@ {-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE ScopedTypeVariables #-} -{-# LANGUAGE TupleSections #-} module Simplex.Messaging.Notifications.Server.Store where @@ -14,7 +13,8 @@ import Control.Concurrent.STM import Control.Monad import Data.ByteString.Char8 (ByteString) import qualified Data.Map.Strict as M -import Data.Set (Set, insert) +import Data.Set (Set) +import qualified Data.Set as S import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Notifications.Protocol import Simplex.Messaging.Protocol (NtfPrivateSignKey) @@ -154,12 +154,18 @@ mkNtfSubData (NewNtfSub tokenId smpQueue notifierKey) = do addNtfSubscription :: NtfStore -> NtfSubscriptionId -> NtfSubData -> STM (Maybe ()) addNtfSubscription st subId sub@NtfSubData {smpQueue, tokenId} = - TM.lookup tokenId (tokenSubscriptions st) >>= mapM insertSub + TM.lookup tokenId (tokenSubscriptions st) >>= maybe newTokenSub pure >>= insertSub where + newTokenSub = do + ts <- newTVar S.empty + TM.insert tokenId ts $ tokenSubscriptions st + pure ts insertSub ts = do - modifyTVar' ts $ insert subId + modifyTVar' ts $ S.insert subId TM.insert subId sub $ subscriptions st TM.insert smpQueue subId (subscriptionLookup st) + -- return Nothing if subscription existed before + pure $ Just () -- getNtfRec :: NtfStore -> SNtfEntity e -> NtfEntityId -> STM (Maybe (NtfEntityRec e)) -- getNtfRec st ent entId = case ent of diff --git a/tests/AgentTests.hs b/tests/AgentTests.hs index 318b9e9de..7984418f3 100644 --- a/tests/AgentTests.hs +++ b/tests/AgentTests.hs @@ -12,6 +12,7 @@ module AgentTests (agentTests) where import AgentTests.ConnectionRequestTests import AgentTests.DoubleRatchetTests (doubleRatchetTests) import AgentTests.FunctionalAPITests (functionalAPITests) +import AgentTests.NotificationTests (notificationTests) import AgentTests.SQLiteTests (storeTests) import AgentTests.SchemaDump (schemaDumpTest) import Control.Concurrent @@ -36,6 +37,7 @@ agentTests (ATransport t) = do describe "Connection request" connectionRequestTests describe "Double ratchet tests" doubleRatchetTests describe "Functional API" $ functionalAPITests (ATransport t) + describe "Notification tests" $ notificationTests (ATransport t) describe "SQLite store" storeTests describe "SQLite schema dump" schemaDumpTest describe "SMP agent protocol syntax" $ syntaxTests t diff --git a/tests/NtfClient.hs b/tests/NtfClient.hs index cc0fa4af8..c3d406cc4 100644 --- a/tests/NtfClient.hs +++ b/tests/NtfClient.hs @@ -18,8 +18,9 @@ import Control.Monad import Control.Monad.Except (runExceptT) import Control.Monad.IO.Unlift import Crypto.Random -import Data.Aeson (FromJSON (..), ToJSON (..)) +import Data.Aeson (FromJSON (..), ToJSON (..), (.:)) import qualified Data.Aeson as J +import qualified Data.Aeson.Types as JT import Data.ByteString.Builder (lazyByteString) import Data.ByteString.Char8 (ByteString) import Data.Text (Text) @@ -42,6 +43,7 @@ import Simplex.Messaging.Transport.HTTP2 (http2TLSParams) import Simplex.Messaging.Transport.HTTP2.Client import Simplex.Messaging.Transport.HTTP2.Server import Simplex.Messaging.Transport.KeepAlive +import Test.Hspec import UnliftIO.Async import UnliftIO.Concurrent import qualified UnliftIO.Exception as E @@ -134,6 +136,9 @@ ntfServerTest _ t = runNtfTest $ \h -> tPut' h t >> tGet' h (Nothing, _, (CorrId corrId, qId, Right cmd)) <- tGet h pure (Nothing, corrId, qId, cmd) +ntfTest :: Transport c => TProxy c -> (THandle c -> IO ()) -> Expectation +ntfTest _ test' = runNtfTest test' `shouldReturn` () + data APNSMockRequest = APNSMockRequest { notification :: APNSNotification, sendApnsResponse :: APNSMockResponse -> IO () @@ -163,9 +168,16 @@ withAPNSMockServer = E.bracket (getAPNSMockServer apnsMockServerConfig) closeAPN deriving instance Generic APNSAlertBody -deriving instance FromJSON APNSAlertBody +instance FromJSON APNSAlertBody where + parseJSON (J.Object v) = do + title <- v .: "title" + subtitle <- v .: "subtitle" + body <- v .: "body" + pure APNSAlertObject {title, subtitle, body} + parseJSON (J.String v) = pure $ APNSAlertText v + parseJSON invalid = JT.prependFailure "parsing Coord failed, " (JT.typeMismatch "Object" invalid) -instance FromJSON APNSNotificationBody where parseJSON = J.genericParseJSON apnsJSONOptions +instance FromJSON APNSNotificationBody where parseJSON = J.genericParseJSON apnsJSONOptions {J.rejectUnknownFields = True} deriving instance FromJSON APNSNotification @@ -185,8 +197,11 @@ getAPNSMockServer config@HTTP2ServerConfig {qSize} = do APNSRespError status reason -> sendResponse . H.responseBuilder status [] . lazyByteString $ J.encode APNSErrorResponse {reason} case J.decodeStrict' reqBody of - Just notification -> atomically $ writeTBQueue apnsQ APNSMockRequest {notification, sendApnsResponse} - _ -> sendApnsResponse $ APNSRespError N.badRequest400 "bad_request_body" + Just notification -> + atomically $ writeTBQueue apnsQ APNSMockRequest {notification, sendApnsResponse} + _ -> do + putStrLn $ "runAPNSMockServer J.decodeStrict' error, reqBody: " <> show reqBody + sendApnsResponse $ APNSRespError N.badRequest400 "bad_request_body" closeAPNSMockServer :: APNSMockServer -> IO () closeAPNSMockServer APNSMockServer {action, http2Server} = do diff --git a/tests/NtfServerTests.hs b/tests/NtfServerTests.hs index c68c2438e..91ad1bf9c 100644 --- a/tests/NtfServerTests.hs +++ b/tests/NtfServerTests.hs @@ -1,23 +1,46 @@ {-# LANGUAGE DataKinds #-} {-# LANGUAGE GADTs #-} +{-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE PatternSynonyms #-} {-# LANGUAGE ScopedTypeVariables #-} +{-# OPTIONS_GHC -fno-warn-incomplete-uni-patterns #-} module NtfServerTests where +import Control.Concurrent (threadDelay) +import Control.Monad.Except (runExceptT) +import qualified Data.Aeson as J +import qualified Data.Aeson.Types as JT +import qualified Data.ByteString.Base64.URL as U import Data.ByteString.Char8 (ByteString) +import Data.Text.Encoding (encodeUtf8) import NtfClient -import ServerTests (sampleDhPubKey, samplePubKey, sampleSig) +import SMPClient as SMP +import ServerTests + ( createAndSecureQueue, + sampleDhPubKey, + samplePubKey, + sampleSig, + signSendRecv, + (#==), + _SEND, + pattern Resp, + ) import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Encoding +import Simplex.Messaging.Encoding.String import Simplex.Messaging.Notifications.Protocol -import Simplex.Messaging.Protocol +import Simplex.Messaging.Notifications.Server.Push.APNS +import Simplex.Messaging.Protocol hiding (notification) import Simplex.Messaging.Transport import Test.Hspec +import UnliftIO.STM ntfServerTests :: ATransport -> Spec ntfServerTests t = do - describe "notifications server protocol syntax" $ ntfSyntaxTests t + describe "Notifications server protocol syntax" $ ntfSyntaxTests t + describe "Managing notification subscriptions" $ testNotificationSubscription t ntfSyntaxTests :: ATransport -> Spec ntfSyntaxTests (ATransport t) = do @@ -34,3 +57,71 @@ ntfSyntaxTests (ATransport t) = do (Maybe C.ASignature, ByteString, ByteString, BrokerMsg) -> Expectation command >#> response = withAPNSMockServer $ \_ -> ntfServerTest t command `shouldReturn` response + +pattern RespNtf :: CorrId -> QueueId -> NtfResponse -> SignedTransmission NtfResponse +pattern RespNtf corrId queueId command <- (_, _, (corrId, queueId, Right command)) + +sendRecvNtf :: forall c e. (Transport c, NtfEntityI e) => THandle c -> (Maybe C.ASignature, ByteString, ByteString, NtfCommand e) -> IO (SignedTransmission NtfResponse) +sendRecvNtf h@THandle {thVersion, sessionId} (sgn, corrId, qId, cmd) = do + let t = encodeTransmission thVersion sessionId (CorrId corrId, qId, cmd) + Right () <- tPut h (sgn, t) + tGet h + +signSendRecvNtf :: forall c e. (Transport c, NtfEntityI e) => THandle c -> C.APrivateSignKey -> (ByteString, ByteString, NtfCommand e) -> IO (SignedTransmission NtfResponse) +signSendRecvNtf h@THandle {thVersion, sessionId} pk (corrId, qId, cmd) = do + let t = encodeTransmission thVersion sessionId (CorrId corrId, qId, cmd) + Right sig <- runExceptT $ C.sign pk t + Right () <- tPut h (Just sig, t) + tGet h + +(.->) :: J.Value -> J.Key -> Either String ByteString +v .-> key = + let J.Object o = v + in U.decodeLenient . encodeUtf8 <$> JT.parseEither (J..: key) o + +testNotificationSubscription :: ATransport -> Spec +testNotificationSubscription (ATransport t) = + it "should create new notification subscription and notify when message is received" $ do + (sPub, sKey) <- C.generateSignatureKeyPair C.SEd25519 + (nPub, nKey) <- C.generateSignatureKeyPair C.SEd25519 + (tknPub, tknKey) <- C.generateSignatureKeyPair C.SEd25519 + (dhPub, dhPriv :: C.PrivateKeyX25519) <- C.generateKeyPair' + let tkn = DeviceToken PPApns "abcd" + withAPNSMockServer $ \APNSMockServer {apnsQ} -> + smpTest2 t $ \rh sh -> + ntfTest t $ \nh -> do + -- create queue + (sId, rId, rKey, _dhShared) <- createAndSecureQueue rh sPub + -- register and verify token + RespNtf "1" "" (NRTknId tId ntfDh) <- signSendRecvNtf nh tknKey ("1", "", TNEW $ NewNtfTkn tkn tknPub dhPub) + APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData}, sendApnsResponse = send} <- + atomically $ readTBQueue apnsQ + send APNSRespOk + let dhSecret = C.dh' ntfDh dhPriv + Right verification = ntfData .-> "verification" + Right nonce = C.cbNonce <$> ntfData .-> "nonce" + Right code = NtfRegCode <$> C.cbDecrypt dhSecret nonce verification + RespNtf "2" _ NROk <- signSendRecvNtf nh tknKey ("2", tId, TVFY code) + RespNtf "2a" _ (NRTkn NTActive) <- signSendRecvNtf nh tknKey ("2a", tId, TCHK) + -- enable queue notifications + Resp "3" _ (NID nId) <- signSendRecv rh rKey ("3", rId, NKEY nPub) + let srv = SMPServer SMP.testHost SMP.testPort SMP.testKeyHash + q = SMPQueueNtf srv nId + RespNtf "4" _ (NRSubId _subId) <- signSendRecvNtf nh tknKey ("4", "", SNEW $ NewNtfSub tId q nKey) + -- send message + threadDelay 50000 + Resp "5" _ OK <- signSendRecv sh sKey ("5", sId, _SEND "hello") + -- receive notification + APNSMockRequest {notification, sendApnsResponse = send'} <- atomically $ readTBQueue apnsQ + let APNSNotification {aps = APNSMutableContent {}, notificationData = Just ntfData'} = notification + Right checkMessage = ntfData' .-> "checkMessage" + Right nonce' = C.cbNonce <$> ntfData' .-> "nonce" + Right smpQueueURI = C.cbDecrypt dhSecret nonce' checkMessage + smpQueueURI `shouldBe` strEncode srv <> "/" <> strEncode nId + send' APNSRespOk + -- receive message + let dec _nonce = C.cbDecrypt _dhShared (C.cbNonce _nonce) + Resp "" _ (MSG mId1 _ _ msg1) <- tGet rh + (dec mId1 msg1, Right "hello") #== "delivered from queue" + Resp "6" _ OK <- signSendRecv rh rKey ("6", rId, ACK mId1) + pure () diff --git a/tests/Test.hs b/tests/Test.hs index acbd15645..d67277dd3 100644 --- a/tests/Test.hs +++ b/tests/Test.hs @@ -1,7 +1,6 @@ {-# LANGUAGE TypeApplications #-} import AgentTests (agentTests) -import AgentTests.NotificationTests (notificationTests) import CoreTests.EncodingTests import CoreTests.ProtocolErrorTests import CoreTests.VersionRangeTests @@ -25,8 +24,6 @@ main = do describe "Version range" versionRangeTests describe "SMP server via TLS" $ serverTests (transport @TLS) describe "SMP server via WebSockets" $ serverTests (transport @WS) - describe "Notifications server" $ do - ntfServerTests (transport @TLS) - notificationTests (transport @TLS) + describe "Notifications server" $ ntfServerTests (transport @TLS) describe "SMP client agent" $ agentTests (transport @TLS) removeDirectoryRecursive "tests/tmp" From 80309a0089811a2b1503c234f7960f0f8c6e2622 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Wed, 8 Jun 2022 08:59:12 +0100 Subject: [PATCH 047/131] fix possible leak (#391) * fix possible leak * remove subscriptions map from the client --- src/Simplex/Messaging/Server.hs | 6 ++++-- src/Simplex/Messaging/Server/Env/STM.hs | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Simplex/Messaging/Server.hs b/src/Simplex/Messaging/Server.hs index aac1db5ee..2b2dbaf7f 100644 --- a/src/Simplex/Messaging/Server.hs +++ b/src/Simplex/Messaging/Server.hs @@ -65,6 +65,7 @@ import qualified Simplex.Messaging.TMap as TM import Simplex.Messaging.Transport import Simplex.Messaging.Transport.Server import Simplex.Messaging.Util +import System.Mem.Weak (deRefWeak) import UnliftIO.Concurrent import UnliftIO.Exception import UnliftIO.IO @@ -196,6 +197,7 @@ clientDisconnected c@Client {subscriptions, connected} = do atomically $ writeTVar connected False subs <- readTVarIO subscriptions mapM_ cancelSub subs + atomically $ writeTVar subscriptions M.empty cs <- asks $ subscribers . server atomically . mapM_ (\rId -> TM.update deleteCurrentClient rId cs) $ M.keys subs where @@ -209,7 +211,7 @@ sameClientSession Client {sessionId} Client {sessionId = s'} = sessionId == s' cancelSub :: MonadUnliftIO m => Sub -> m () cancelSub = \case - Sub {subThread = SubThread t} -> killThread t + Sub {subThread = SubThread t} -> liftIO $ deRefWeak t >>= mapM_ killThread _ -> return () receive :: (Transport c, MonadUnliftIO m, MonadReader Env m) => THandle c -> Client -> m () @@ -480,7 +482,7 @@ client clnt@Client {subscriptions, ntfSubscriptions, rcvQ, sndQ} Server {subscri forkSub :: MsgQueue -> m () forkSub q = do atomically . setSub $ \s -> s {subThread = SubPending} - t <- forkIO $ subscriber q + t <- mkWeakThreadId =<< forkIO (subscriber q) atomically . setSub $ \case s@Sub {subThread = SubPending} -> s {subThread = SubThread t} s -> s diff --git a/src/Simplex/Messaging/Server/Env/STM.hs b/src/Simplex/Messaging/Server/Env/STM.hs index d70fe203f..3f83ebfff 100644 --- a/src/Simplex/Messaging/Server/Env/STM.hs +++ b/src/Simplex/Messaging/Server/Env/STM.hs @@ -32,6 +32,7 @@ import qualified Simplex.Messaging.TMap as TM import Simplex.Messaging.Transport (ATransport) import Simplex.Messaging.Transport.Server (loadFingerprint, loadTLSServerParams) import System.IO (IOMode (..)) +import System.Mem.Weak (Weak) import UnliftIO.STM data ServerConfig = ServerConfig @@ -113,7 +114,7 @@ data ServerStats = ServerStats fromTime :: TVar UTCTime } -data SubscriptionThread = NoSub | SubPending | SubThread ThreadId +data SubscriptionThread = NoSub | SubPending | SubThread (Weak ThreadId) data Sub = Sub { subThread :: SubscriptionThread, From 7736ef857673a2a294ac5c765cbc2762376c0a59 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Wed, 8 Jun 2022 09:08:34 +0100 Subject: [PATCH 048/131] v2.2.1 --- package.yaml | 2 +- simplexmq.cabal | 2 +- src/Simplex/Messaging/Transport.hs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package.yaml b/package.yaml index 4f05628ea..897122846 100644 --- a/package.yaml +++ b/package.yaml @@ -1,5 +1,5 @@ name: simplexmq -version: 2.2.0 +version: 2.2.1 synopsis: SimpleXMQ message broker description: | This package includes <./docs/Simplex-Messaging-Server.html server>, diff --git a/simplexmq.cabal b/simplexmq.cabal index 5c992e587..dcc9597d8 100644 --- a/simplexmq.cabal +++ b/simplexmq.cabal @@ -5,7 +5,7 @@ cabal-version: 1.12 -- see: https://github.com/sol/hpack name: simplexmq -version: 2.2.0 +version: 2.2.1 synopsis: SimpleXMQ message broker description: This package includes <./docs/Simplex-Messaging-Server.html server>, <./docs/Simplex-Messaging-Client.html client> and diff --git a/src/Simplex/Messaging/Transport.hs b/src/Simplex/Messaging/Transport.hs index 5971c9314..6d697b07f 100644 --- a/src/Simplex/Messaging/Transport.hs +++ b/src/Simplex/Messaging/Transport.hs @@ -96,7 +96,7 @@ supportedSMPVersions :: VersionRange supportedSMPVersions = mkVersionRange 1 1 simplexMQVersion :: String -simplexMQVersion = "2.2.0" +simplexMQVersion = "2.2.1" -- * Transport connection class From c1348aa54fba292d34339d6b111572cb1c74b546 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Thu, 9 Jun 2022 13:47:07 +0100 Subject: [PATCH 049/131] SMP connection handshake v2 (#390) * SMP connection handshake v2 * hadshake v2 [mostly] works * all tests pass, some race conditions remain * fix build * fix race conditions, send CON after all HELLOs in duplexHandshake mode * add comments * comment * add comments --- apps/smp-server/Main.hs | 5 +- .../duplex-messaging/duplex-creating-v2.mmd | 71 ++++ .../duplex-messaging/duplex-creating.mmd | 4 +- simplexmq.cabal | 2 +- src/Simplex/Messaging/Agent.hs | 376 +++++++++++------- src/Simplex/Messaging/Agent/Client.hs | 14 +- src/Simplex/Messaging/Agent/Env/SQLite.hs | 13 +- src/Simplex/Messaging/Agent/Protocol.hs | 37 +- src/Simplex/Messaging/Agent/Store.hs | 10 +- src/Simplex/Messaging/Agent/Store/SQLite.hs | 46 ++- .../Agent/Store/SQLite/Migrations.hs | 4 +- .../SQLite/Migrations/M20220605_msg_flags.hs | 12 - .../Store/SQLite/Migrations/M20220608_v2.hs | 16 + .../Store/SQLite/Migrations/agent_schema.sql | 4 + src/Simplex/Messaging/Client.hs | 11 +- src/Simplex/Messaging/Crypto.hs | 4 +- src/Simplex/Messaging/Crypto/Ratchet.hs | 3 +- src/Simplex/Messaging/Notifications/Server.hs | 2 +- .../Messaging/Notifications/Transport.hs | 12 +- src/Simplex/Messaging/Protocol.hs | 2 +- src/Simplex/Messaging/Server.hs | 3 +- src/Simplex/Messaging/Server/Env/STM.hs | 5 +- src/Simplex/Messaging/Transport.hs | 20 +- tests/AgentTests.hs | 136 +++---- tests/AgentTests/ConnectionRequestTests.hs | 4 +- tests/AgentTests/FunctionalAPITests.hs | 166 ++++++-- tests/AgentTests/SQLiteTests.hs | 2 +- tests/NtfClient.hs | 2 +- tests/SMPClient.hs | 5 +- 29 files changed, 660 insertions(+), 331 deletions(-) create mode 100644 protocol/diagrams/duplex-messaging/duplex-creating-v2.mmd delete mode 100644 src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220605_msg_flags.hs create mode 100644 src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220608_v2.hs diff --git a/apps/smp-server/Main.hs b/apps/smp-server/Main.hs index 5f9253665..cbf956dd5 100644 --- a/apps/smp-server/Main.hs +++ b/apps/smp-server/Main.hs @@ -12,7 +12,7 @@ import Simplex.Messaging.Server (runSMPServer) import Simplex.Messaging.Server.CLI (ServerCLIConfig (..), protocolServerCLI, readStrictIni) import Simplex.Messaging.Server.Env.STM (ServerConfig (..), defaultInactiveClientExpiration, defaultMessageExpiration) import Simplex.Messaging.Server.Expiration -import Simplex.Messaging.Transport (simplexMQVersion) +import Simplex.Messaging.Transport (simplexMQVersion, supportedSMPServerVRange) import System.FilePath (combine) cfgPath :: FilePath @@ -92,6 +92,7 @@ smpServerCLIConfig = } else Nothing, logStatsInterval = Just 86400, -- seconds - logStatsStartTime = 0 -- seconds from 00:00 UTC + logStatsStartTime = 0, -- seconds from 00:00 UTC + smpServerVRange = supportedSMPServerVRange } } diff --git a/protocol/diagrams/duplex-messaging/duplex-creating-v2.mmd b/protocol/diagrams/duplex-messaging/duplex-creating-v2.mmd new file mode 100644 index 000000000..09ee913ff --- /dev/null +++ b/protocol/diagrams/duplex-messaging/duplex-creating-v2.mmd @@ -0,0 +1,71 @@ +sequenceDiagram + participant A as Alice + participant AA as Alice's
agent + participant AS as Alice's
server + participant BS as Bob's
server + participant BA as Bob's
agent + participant B as Bob + + note over AA, BA: status (receive/send): NONE/NONE + + note over A, AA: 1. request connection
from agent + A ->> AA: NEW: create
duplex connection + + note over AA, AS: 2. create Alice's SMP queue + AA ->> AS: NEW: create SMP queue + AS ->> AA: IDS: SMP queue IDs + note over AA: status: NEW/NONE + + AA ->> A: INV: invitation
to connect + + note over A, B: 3. out-of-band invitation + A ->> B: OOB: invitation to connect + + note over BA, B: 4. accept connection + B ->> BA: JOIN:
via invitation info + note over BA: status: NONE/NEW + + note over BA, BS: 5. create Bob's SMP queue + BA ->> BS: NEW: create SMP queue + BS ->> BA: IDS: SMP queue IDs + note over BA: status: NEW/NEW + + note over BA, AA: 6. establish Alice's SMP queue + BA ->> AS: SEND: Bob's info and sender server key (SMP confirmation with reply queues) + note over BA: status: NEW/CONFIRMED + + AS ->> AA: MSG: Bob's info and
sender server key + note over AA: status: CONFIRMED/NONE + AA ->> AS: ACK: confirm message + AA ->> A: CONF: connection request ID
and Bob's info + A ->> AA: LET: accept connection request,
send Alice's info + AA ->> AS: KEY: secure queue + note over AA: status: SECURED/NONE + + AA ->> BS: SEND: Alice's info and sender's server key (SMP confirmation without reply queues) + note over AA: status: SECURED/CONFIRMED + + BS ->> BA: MSG: Alice's info and
sender's server key + note over BA: status: CONFIRMED/CONFIRMED + BA ->> B: INFO: Alice's info + BA ->> BS: ACK: confirm message + BA ->> BS: KEY: secure queue + note over BA: status: SECURED/CONFIRMED + + BA ->> AS: SEND: HELLO: only needs to be sent once in v2 + + note over BA: status: SECURED/ACTIVE + note over BA, B: 7a. notify Bob
about connection success + BA ->> B: CON: connected + + AS ->> AA: MSG: HELLO: Alice's agent
knows Bob can send + note over AA: status: SECURED/ACTIVE + AA ->> AS: ACK: confirm message + note over A, AA: 7a. notify Alice
about connection success + AA ->> A: CON: connected + + AA ->> BS: SEND: HELLO: only needs to be sent once in v2 + note over AA: status: ACTIVE/ACTIVE + BS ->> BA: MSG: HELLO: Bob's agent
knows Alice can send + note over BA: status: ACTIVE/ACTIVE + BA ->> BS: ACK: confirm message diff --git a/protocol/diagrams/duplex-messaging/duplex-creating.mmd b/protocol/diagrams/duplex-messaging/duplex-creating.mmd index d8df2d199..738bad325 100644 --- a/protocol/diagrams/duplex-messaging/duplex-creating.mmd +++ b/protocol/diagrams/duplex-messaging/duplex-creating.mmd @@ -33,8 +33,8 @@ sequenceDiagram AS ->> AA: MSG: Bob's info and
sender server key note over AA: status: CONFIRMED/NONE AA ->> AS: ACK: confirm message - AA ->> A: REQ: connection request ID
and Bob's info - A ->> AA: ACPT: accept connection request,
send Alice's info + AA ->> A: CONF: connection request ID
and Bob's info + A ->> AA: LET: accept connection request,
send Alice's info AA ->> AS: KEY: secure queue note over AA: status: SECURED/NONE diff --git a/simplexmq.cabal b/simplexmq.cabal index 7a4cb46de..f5abad638 100644 --- a/simplexmq.cabal +++ b/simplexmq.cabal @@ -48,7 +48,7 @@ library Simplex.Messaging.Agent.Store.SQLite.Migrations.M20220301_snd_queue_keys Simplex.Messaging.Agent.Store.SQLite.Migrations.M20220322_notifications Simplex.Messaging.Agent.Store.SQLite.Migrations.M20220404_ntf_subscriptions_draft - Simplex.Messaging.Agent.Store.SQLite.Migrations.M20220605_msg_flags + Simplex.Messaging.Agent.Store.SQLite.Migrations.M20220608_v2 Simplex.Messaging.Client Simplex.Messaging.Client.Agent Simplex.Messaging.Crypto diff --git a/src/Simplex/Messaging/Agent.hs b/src/Simplex/Messaging/Agent.hs index a0239b3e7..d8eac56a6 100644 --- a/src/Simplex/Messaging/Agent.hs +++ b/src/Simplex/Messaging/Agent.hs @@ -84,7 +84,7 @@ import Simplex.Messaging.Agent.Env.SQLite import Simplex.Messaging.Agent.Protocol import Simplex.Messaging.Agent.RetryInterval import Simplex.Messaging.Agent.Store -import Simplex.Messaging.Agent.Store.SQLite (SQLiteStore) +import Simplex.Messaging.Agent.Store.SQLite (AgentStoreMonad, SQLiteStore) import Simplex.Messaging.Client (ProtocolClient (..), ServerTransmission) import qualified Simplex.Messaging.Crypto as C import qualified Simplex.Messaging.Crypto.Ratchet as CR @@ -214,10 +214,7 @@ client c@AgentClient {rcvQ, subQ} = forever $ do Left e -> (corrId, connId, ERR e) Right (connId', resp) -> (corrId, connId', resp) -withStore :: - AgentMonad m => - (forall m'. (MonadUnliftIO m', MonadError StoreError m') => SQLiteStore -> m' a) -> - m a +withStore :: AgentMonad m => (forall m'. AgentStoreMonad m' => SQLiteStore -> m' a) -> m a withStore action = do st <- asks store runExceptT (action st `E.catch` handleInternal) >>= \case @@ -235,6 +232,10 @@ withStore action = do SEBadConnType CRcv -> CONN SIMPLEX SEBadConnType CSnd -> CONN SIMPLEX SEInvitationNotFound -> CMD PROHIBITED + -- this error is never reported as store error, + -- it is used to wrap agent operations when "transaction-like" store access is needed + -- NOTE: network IO should NOT be used inside AgentStoreMonad + SEAgentError e -> e e -> INTERNAL $ show e -- | execute any SMP agent command @@ -256,10 +257,12 @@ newConn c connId cMode = do srv <- getSMPServer c (rq, qUri) <- newRcvQueue c srv g <- asks idsDrg - let cData = ConnData {connId} + agentVersion <- asks $ smpAgentVersion . config + let cData = ConnData {connId, connAgentVersion = agentVersion, duplexHandshake = Nothing} -- connection mode is determined by the accepting agent connId' <- withStore $ \st -> createRcvConn st g cData rq cMode addSubscription c rq connId' - let crData = ConnReqUriData simplexChat smpAgentVRange [qUri] + aVRange <- asks $ smpAgentVRange . config + let crData = ConnReqUriData simplexChat aVRange [qUri] case cMode of SCMContact -> pure (connId', CRContactUri crData) SCMInvitation -> do @@ -268,35 +271,39 @@ newConn c connId cMode = do pure (connId', CRInvitationUri crData $ toVersionRangeT e2eRcvParams CR.e2eEncryptVRange) joinConn :: AgentMonad m => AgentClient -> ConnId -> ConnectionRequestUri c -> ConnInfo -> m ConnId -joinConn c connId (CRInvitationUri (ConnReqUriData _ agentVRange (qUri :| _)) e2eRcvParamsUri) cInfo = +joinConn c connId (CRInvitationUri (ConnReqUriData _ agentVRange (qUri :| _)) e2eRcvParamsUri) cInfo = do + aVRange <- asks $ smpAgentVRange . config case ( qUri `compatibleVersion` SMP.smpClientVRange, e2eRcvParamsUri `compatibleVersion` CR.e2eEncryptVRange, - agentVRange `compatibleVersion` smpAgentVRange + agentVRange `compatibleVersion` aVRange ) of - (Just qInfo, Just (Compatible e2eRcvParams@(CR.E2ERatchetParams _ _ rcDHRr)), Just _) -> do + (Just qInfo, Just (Compatible e2eRcvParams@(CR.E2ERatchetParams _ _ rcDHRr)), Just aVersion@(Compatible connAgentVersion)) -> do -- TODO in agent v2 - use found compatible version rather than current (pk1, pk2, e2eSndParams) <- liftIO . CR.generateE2EParams $ version e2eRcvParams (_, rcDHRs) <- liftIO C.generateKeyPair' let rc = CR.initSndRatchet rcDHRr rcDHRs $ CR.x3dhSnd pk1 pk2 e2eRcvParams sq <- newSndQueue qInfo g <- asks idsDrg - let cData = ConnData {connId} + let duplexHS = connAgentVersion /= 1 + cData = ConnData {connId, connAgentVersion, duplexHandshake = Just duplexHS} connId' <- withStore $ \st -> do connId' <- createSndConn st g cData sq createRatchet st connId' rc pure connId' - tryError (confirmQueue c connId' sq cInfo $ Just e2eSndParams) >>= \case + let cData' = (cData :: ConnData) {connId = connId'} + tryError (confirmQueue aVersion c connId' sq cInfo $ Just e2eSndParams) >>= \case Right _ -> do - void $ enqueueMessage c connId' sq SMP.noMsgFlags HELLO + unless duplexHS . void $ enqueueMessage c cData' sq SMP.noMsgFlags HELLO pure connId' Left e -> do -- TODO recovery for failure on network timeout, see rfcs/2022-04-20-smp-conf-timeout-recovery.md withStore (`deleteConn` connId') throwError e _ -> throwError $ AGENT A_VERSION -joinConn c connId (CRContactUri (ConnReqUriData _ agentVRange (qUri :| _))) cInfo = +joinConn c connId (CRContactUri (ConnReqUriData _ agentVRange (qUri :| _))) cInfo = do + aVRange <- asks $ smpAgentVRange . config case ( qUri `compatibleVersion` SMP.smpClientVRange, - agentVRange `compatibleVersion` smpAgentVRange + agentVRange `compatibleVersion` aVRange ) of (Just qInfo, Just _) -> do -- TODO in agent v2 - use found compatible version rather than current @@ -305,24 +312,25 @@ joinConn c connId (CRContactUri (ConnReqUriData _ agentVRange (qUri :| _))) cInf pure connId' _ -> throwError $ AGENT A_VERSION -createReplyQueue :: AgentMonad m => AgentClient -> ConnId -> SndQueue -> m () -createReplyQueue c connId sq = do +createReplyQueue :: AgentMonad m => AgentClient -> ConnId -> m SMPQueueInfo +createReplyQueue c connId = do srv <- getSMPServer c (rq, qUri) <- newRcvQueue c srv -- TODO reply queue version should be the same as send queue, ignoring it in v1 let qInfo = toVersionT qUri SMP.smpClientVersion addSubscription c rq connId withStore $ \st -> upgradeSndConnToDuplex st connId rq - void . enqueueMessage c connId sq SMP.noMsgFlags $ REPLY [qInfo] + pure qInfo -- | Approve confirmation (LET command) in Reader monad allowConnection' :: AgentMonad m => AgentClient -> ConnId -> ConfirmationId -> ConnInfo -> m () allowConnection' c connId confId ownConnInfo = do withStore (`getConn` connId) >>= \case - SomeConn _ (RcvConnection _ rq) -> do + SomeConn _ (RcvConnection cData rq) -> do AcceptedConfirmation {senderConf, ratchetState} <- withStore $ \st -> acceptConfirmation st confId ownConnInfo withStore $ \st -> createRatchet st connId ratchetState processConfirmation c rq senderConf + mapM_ (connectReplyQueues c cData ownConnInfo) (L.nonEmpty $ smpReplyQueues senderConf) _ -> throwError $ CMD PROHIBITED -- | Accept contact (ACPT command) in Reader monad @@ -351,11 +359,11 @@ processConfirmation c rq@RcvQueue {e2ePrivKey} SMPConfirmation {senderKey, e2ePu subscribeConnection' :: forall m. AgentMonad m => AgentClient -> ConnId -> m () subscribeConnection' c connId = withStore (`getConn` connId) >>= \case - SomeConn _ (DuplexConnection _ rq sq) -> do - resumeMsgDelivery c connId sq + SomeConn _ (DuplexConnection cData rq sq) -> do + resumeMsgDelivery c cData sq subscribeQueue c rq connId - SomeConn _ (SndConnection _ sq) -> do - resumeMsgDelivery c connId sq + SomeConn _ (SndConnection cData sq) -> do + resumeMsgDelivery c cData sq case status (sq :: SndQueue) of Confirmed -> pure () Active -> throwError $ CONN SIMPLEX @@ -373,40 +381,40 @@ resubscribeConnection' c connId = sendMessage' :: forall m. AgentMonad m => AgentClient -> ConnId -> MsgFlags -> MsgBody -> m AgentMsgId sendMessage' c connId msgFlags msg = withStore (`getConn` connId) >>= \case - SomeConn _ (DuplexConnection _ _ sq) -> enqueueMsg sq - SomeConn _ (SndConnection _ sq) -> enqueueMsg sq + SomeConn _ (DuplexConnection cData _ sq) -> enqueueMsg cData sq + SomeConn _ (SndConnection cData sq) -> enqueueMsg cData sq _ -> throwError $ CONN SIMPLEX where - enqueueMsg :: SndQueue -> m AgentMsgId - enqueueMsg sq = enqueueMessage c connId sq msgFlags $ A_MSG msg + enqueueMsg :: ConnData -> SndQueue -> m AgentMsgId + enqueueMsg cData sq = enqueueMessage c cData sq msgFlags $ A_MSG msg -enqueueMessage :: forall m. AgentMonad m => AgentClient -> ConnId -> SndQueue -> MsgFlags -> AMessage -> m AgentMsgId -enqueueMessage c connId sq msgFlags aMessage = do - resumeMsgDelivery c connId sq +enqueueMessage :: forall m. AgentMonad m => AgentClient -> ConnData -> SndQueue -> MsgFlags -> AMessage -> m AgentMsgId +enqueueMessage c cData@ConnData {connId, connAgentVersion} sq msgFlags aMessage = do + resumeMsgDelivery c cData sq msgId <- storeSentMsg queuePendingMsgs c connId sq [msgId] pure $ unId msgId where storeSentMsg :: m InternalId - storeSentMsg = do + storeSentMsg = withStore $ \st -> do internalTs <- liftIO getCurrentTime - (internalId, internalSndId, prevMsgHash) <- withStore (`updateSndIds` connId) + (internalId, internalSndId, prevMsgHash) <- updateSndIds st connId let privHeader = APrivHeader (unSndId internalSndId) prevMsgHash agentMsg = AgentMessage privHeader aMessage agentMsgStr = smpEncode agentMsg internalHash = C.sha256Hash agentMsgStr - encAgentMessage <- agentRatchetEncrypt connId agentMsgStr e2eEncUserMsgLength - let msgBody = smpEncode $ AgentMsgEnvelope {agentVersion = smpAgentVersion, encAgentMessage} + encAgentMessage <- agentRatchetEncrypt st connId agentMsgStr e2eEncUserMsgLength + let msgBody = smpEncode $ AgentMsgEnvelope {agentVersion = connAgentVersion, encAgentMessage} msgType = agentMessageType agentMsg msgData = SndMsgData {internalId, internalSndId, internalTs, msgType, msgFlags, msgBody, internalHash, prevMsgHash} - withStore $ \st -> createSndMsg st connId msgData + createSndMsg st connId msgData pure internalId -resumeMsgDelivery :: forall m. AgentMonad m => AgentClient -> ConnId -> SndQueue -> m () -resumeMsgDelivery c connId sq@SndQueue {server, sndId} = do +resumeMsgDelivery :: forall m. AgentMonad m => AgentClient -> ConnData -> SndQueue -> m () +resumeMsgDelivery c cData@ConnData {connId} sq@SndQueue {server, sndId} = do let qKey = (connId, server, sndId) unlessM (queueDelivering qKey) $ - async (runSmpQueueMsgDelivery c connId sq) + async (runSmpQueueMsgDelivery c cData sq) >>= \a -> atomically (TM.insert qKey a $ smpQueueMsgDeliveries c) unlessM connQueued $ withStore (`getPendingMsgs` connId) @@ -430,8 +438,8 @@ getPendingMsgQ c connId SndQueue {server, sndId} = do TM.insert qKey mq $ smpQueueMsgQueues c pure mq -runSmpQueueMsgDelivery :: forall m. AgentMonad m => AgentClient -> ConnId -> SndQueue -> m () -runSmpQueueMsgDelivery c@AgentClient {subQ} connId sq = do +runSmpQueueMsgDelivery :: forall m. AgentMonad m => AgentClient -> ConnData -> SndQueue -> m () +runSmpQueueMsgDelivery c@AgentClient {subQ} cData@ConnData {connId, duplexHandshake} sq = do mq <- atomically $ getPendingMsgQ c connId sq ri <- asks $ reconnectInterval . config forever $ do @@ -451,19 +459,27 @@ runSmpQueueMsgDelivery c@AgentClient {subQ} connId sq = do case e of SMP SMP.QUOTA -> case msgType of AM_CONN_INFO -> connError msgId NOT_AVAILABLE + AM_CONN_INFO_REPLY -> connError msgId NOT_AVAILABLE _ -> loop SMP SMP.AUTH -> case msgType of AM_CONN_INFO -> connError msgId NOT_AVAILABLE - AM_HELLO_ -> do - helloTimeout <- asks $ helloTimeout . config - currentTime <- liftIO getCurrentTime - if diffUTCTime currentTime internalTs > helloTimeout - then case rq_ of + AM_CONN_INFO_REPLY -> connError msgId NOT_AVAILABLE + AM_HELLO_ + -- in duplexHandshake mode (v2) HELLO is only sent once, without retrying, + -- because the queue must be secured by the time the confirmation or the first HELLO is received + | duplexHandshake == Just True -> connErr + | otherwise -> do + helloTimeout <- asks $ helloTimeout . config + currentTime <- liftIO getCurrentTime + if diffUTCTime currentTime internalTs > helloTimeout + then connErr + else loop + where + connErr = case rq_ of -- party initiating connection Just _ -> connError msgId NOT_AVAILABLE -- party joining connection _ -> connError msgId NOT_ACCEPTED - else loop AM_REPLY_ -> notifyDel msgId $ ERR e AM_A_MSG_ -> notifyDel msgId $ MERR mId e SMP (SMP.CMD _) -> notifyDel msgId err @@ -476,16 +492,28 @@ runSmpQueueMsgDelivery c@AgentClient {subQ} connId sq = do withStore $ \st -> setSndQueueStatus st sq Confirmed when (isJust rq_) $ withStore (`removeConfirmations` connId) -- TODO possibly notification flag should be ON for one of the parties, to result in contact connected notification - void $ enqueueMessage c connId sq SMP.noMsgFlags HELLO + unless (duplexHandshake == Just True) . void $ enqueueMessage c cData sq SMP.noMsgFlags HELLO AM_HELLO_ -> do withStore $ \st -> setSndQueueStatus st sq Active case rq_ of - -- party initiating connection - Just rq -> do - subscribeQueue c rq connId - notify CON - -- party joining connection - _ -> createReplyQueue c connId sq + -- party initiating connection (in v1) + Just RcvQueue {status} -> + -- TODO it is unclear why subscribeQueue was needed here, + -- message delivery can only be enabled for queues that were created in the current session or subscribed + -- subscribeQueue c rq connId + -- + -- If initiating party were to send CON to the user without waiting for reply HELLO (to reduce handshake time), + -- it would lead to the non-deterministic internal ID of the first sent message, at to some other race conditions, + -- because it can be sent before HELLO is received + -- With `status == Aclive` condition, CON is sent here only by the accepting party, that previously received HELLO + when (status == Active) $ notify CON + -- Party joining connection sends REPLY after HELLO in v1, + -- it is an error to send REPLY in duplexHandshake mode (v2), + -- and this branch should never be reached as receive is created before the confirmation, + -- so the condition is not necessary here, strictly speaking. + _ -> unless (duplexHandshake == Just True) $ do + qInfo <- createReplyQueue c connId + void . enqueueMessage c cData sq SMP.noMsgFlags $ REPLY [qInfo] AM_A_MSG_ -> notify $ SENT mId _ -> pure () delMsg msgId @@ -509,7 +537,9 @@ ackMessage' c connId msgId = do ack rq = do let mId = InternalId msgId srvMsgId <- withStore $ \st -> checkRcvMsg st connId mId - sendAck c rq srvMsgId + sendAck c rq srvMsgId `catchError` \case + SMP SMP.NO_MSG -> pure () + e -> throwError e withStore $ \st -> deleteMsg st connId mId -- | Suspend SMP agent connection (OFF command) in Reader monad @@ -672,13 +702,13 @@ subscriber c@AgentClient {msgQ} = forever $ do processSMPTransmission :: forall m. AgentMonad m => AgentClient -> ServerTransmission BrokerMsg -> m () processSMPTransmission c@AgentClient {smpClients, subQ} (srv, sessId, rId, cmd) = do withStore (\st -> getRcvConn st srv rId) >>= \case - SomeConn SCDuplex (DuplexConnection cData rq _) -> processSMP SCDuplex cData rq - SomeConn SCRcv (RcvConnection cData rq) -> processSMP SCRcv cData rq - SomeConn SCContact (ContactConnection cData rq) -> processSMP SCContact cData rq + SomeConn _ conn@(DuplexConnection cData rq _) -> processSMP conn cData rq + SomeConn _ conn@(RcvConnection cData rq) -> processSMP conn cData rq + SomeConn _ conn@(ContactConnection cData rq) -> processSMP conn cData rq _ -> atomically $ writeTBQueue subQ ("", "", ERR $ CONN NOT_FOUND) where - processSMP :: SConnType c -> ConnData -> RcvQueue -> m () - processSMP cType ConnData {connId} rq@RcvQueue {rcvDhSecret, e2ePrivKey, e2eDhSecret, status} = + processSMP :: Connection c -> ConnData -> RcvQueue -> m () + processSMP conn cData@ConnData {connId, duplexHandshake} rq@RcvQueue {rcvDhSecret, e2ePrivKey, e2eDhSecret, status} = case cmd of SMP.MSG srvMsgId srvTs msgFlags msgBody' -> handleNotifyAck $ do -- TODO deduplicate with previously received @@ -690,32 +720,54 @@ processSMPTransmission c@AgentClient {smpClients, subQ} (srv, sessId, rId, cmd) (Nothing, Just e2ePubKey) -> do let e2eDh = C.dh' e2ePubKey e2ePrivKey decryptClientMessage e2eDh clientMsg >>= \case - (SMP.PHConfirmation senderKey, AgentConfirmation {e2eEncryption, encConnInfo}) -> - smpConfirmation senderKey e2ePubKey e2eEncryption encConnInfo >> ack + (SMP.PHConfirmation senderKey, AgentConfirmation {e2eEncryption, encConnInfo, agentVersion}) -> + smpConfirmation senderKey e2ePubKey e2eEncryption encConnInfo agentVersion >> ack (SMP.PHEmpty, AgentInvitation {connReq, connInfo}) -> smpInvitation connReq connInfo >> ack _ -> prohibited >> ack (Just e2eDh, Nothing) -> do decryptClientMessage e2eDh clientMsg >>= \case - (SMP.PHEmpty, AgentMsgEnvelope _ encAgentMsg) -> do - agentMsgBody <- agentRatchetDecrypt connId encAgentMsg - parseMessage agentMsgBody >>= \case - agentMsg@(AgentMessage APrivHeader {sndMsgId, prevMsgHash} aMessage) -> do - let msgType = agentMessageType agentMsg - (msgId, msgMeta) <- agentClientMsg prevMsgHash sndMsgId (srvMsgId, systemToUTCTime srvTs) msgFlags agentMsgBody msgType - case aMessage of - HELLO -> helloMsg >> ack >> withStore (\st -> deleteMsg st connId msgId) - REPLY cReq -> replyMsg cReq >> ack >> withStore (\st -> deleteMsg st connId msgId) - -- note that there is no ACK sent here, it is sent with agent's user ACK command - A_MSG body -> notify $ MSG msgMeta msgFlags body + (SMP.PHEmpty, AgentMsgEnvelope _ encAgentMsg) -> + agentClientMsg >>= \case + Just (msgId, msgMeta, aMessage) -> case aMessage of + HELLO -> helloMsg >> ack >> withStore (\st -> deleteMsg st connId msgId) + REPLY cReq -> replyMsg cReq >> ack >> withStore (\st -> deleteMsg st connId msgId) + -- note that there is no ACK sent for A_MSG, it is sent with agent's user ACK command + A_MSG body -> do + logServer "<--" c srv rId "MSG " + notify $ MSG msgMeta msgFlags body _ -> prohibited >> ack + where + agentClientMsg :: m (Maybe (InternalId, MsgMeta, AMessage)) + agentClientMsg = withStore $ \st -> do + agentMsgBody <- agentRatchetDecrypt st connId encAgentMsg + liftEither (parse smpP (SEAgentError $ AGENT A_MESSAGE) agentMsgBody) >>= \case + agentMsg@(AgentMessage APrivHeader {sndMsgId, prevMsgHash} aMessage) -> do + let msgType = agentMessageType agentMsg + internalHash = C.sha256Hash agentMsgBody + internalTs <- liftIO getCurrentTime + (internalId, internalRcvId, prevExtSndId, prevRcvMsgHash) <- updateRcvIds st connId + let integrity = checkMsgIntegrity prevExtSndId sndMsgId prevRcvMsgHash prevMsgHash + recipient = (unId internalId, internalTs) + broker = (srvMsgId, systemToUTCTime srvTs) + msgMeta = MsgMeta {integrity, recipient, broker, sndMsgId} + rcvMsg = RcvMsgData {msgMeta, msgType, msgFlags, msgBody, internalRcvId, internalHash, externalPrevSndHash = prevMsgHash} + createRcvMsg st connId rcvMsg + pure $ Just (internalId, msgMeta, aMessage) + _ -> pure Nothing _ -> prohibited >> ack _ -> prohibited >> ack where ack :: m () - ack = sendAck c rq srvMsgId + ack = + sendAck c rq srvMsgId `catchError` \case + SMP SMP.NO_MSG -> pure () + e -> throwError e handleNotifyAck :: m () -> m () - handleNotifyAck m = m `catchError` \e -> notify (ERR e) >> ack + handleNotifyAck m = + m `catchError` \case + AGENT A_DUPLICATE -> ack + e -> notify (ERR e) >> ack SMP.END -> atomically (TM.lookup srv smpClients $>>= tryReadTMVar >>= processEND) >>= logServer "<--" c srv rId @@ -744,38 +796,53 @@ processSMPTransmission c@AgentClient {smpClients, subQ} (srv, sessId, rId, cmd) clientMsg <- agentCbDecrypt e2eDh cmNonce cmEncBody SMP.ClientMessage privHeader clientBody <- parseMessage clientMsg agentEnvelope <- parseMessage clientBody - if agentVersion agentEnvelope `isCompatible` smpAgentVRange - then pure (privHeader, agentEnvelope) - else throwError $ AGENT A_VERSION + -- Version check is removed here, because when connecting via v1 contact address the agent still sends v2 message, + -- to allow duplexHandshake mode, in case the receiving agent was updated to v2 after the address was created. + -- aVRange <- asks $ smpAgentVRange . config + -- if agentVersion agentEnvelope `isCompatible` aVRange + -- then pure (privHeader, agentEnvelope) + -- else throwError $ AGENT A_VERSION + pure (privHeader, agentEnvelope) parseMessage :: Encoding a => ByteString -> m a parseMessage = liftEither . parse smpP (AGENT A_MESSAGE) - smpConfirmation :: C.APublicVerifyKey -> C.PublicKeyX25519 -> Maybe (CR.E2ERatchetParams 'C.X448) -> ByteString -> m () - smpConfirmation senderKey e2ePubKey e2eEncryption encConnInfo = do + smpConfirmation :: C.APublicVerifyKey -> C.PublicKeyX25519 -> Maybe (CR.E2ERatchetParams 'C.X448) -> ByteString -> Version -> m () + smpConfirmation senderKey e2ePubKey e2eEncryption encConnInfo agentVersion = do logServer "<--" c srv rId "MSG " + aVRange <- asks $ smpAgentVRange . config + unless (agentVersion `isCompatible` aVRange) . throwError $ AGENT A_VERSION case status of - New -> case (cType, e2eEncryption) of - (SCRcv, Just e2eSndParams) -> do + New -> case (conn, e2eEncryption) of + -- party initiating connection + (RcvConnection {}, Just e2eSndParams) -> do (pk1, rcDHRs) <- withStore $ \st -> getRatchetX3dhKeys st connId let rc = CR.initRcvRatchet rcDHRs $ CR.x3dhRcv pk1 rcDHRs e2eSndParams (agentMsgBody_, rc', skipped) <- liftError cryptoError $ CR.rcDecrypt rc M.empty encConnInfo case (agentMsgBody_, skipped) of (Right agentMsgBody, CR.SMDNoChange) -> parseMessage agentMsgBody >>= \case - AgentConnInfo connInfo -> do - g <- asks idsDrg - let senderConf = SMPConfirmation {senderKey, e2ePubKey, connInfo} - newConfirmation = NewConfirmation {connId, senderConf, ratchetState = rc'} - confId <- withStore $ \st -> createConfirmation st g newConfirmation - notify $ CONF confId connInfo + AgentConnInfo connInfo -> + processConf connInfo SMPConfirmation {senderKey, e2ePubKey, connInfo, smpReplyQueues = []} False + AgentConnInfoReply smpQueues connInfo -> do + processConf connInfo SMPConfirmation {senderKey, e2ePubKey, connInfo, smpReplyQueues = L.toList smpQueues} True _ -> prohibited + where + processConf connInfo senderConf duplexHS = do + let newConfirmation = NewConfirmation {connId, senderConf, ratchetState = rc'} + g <- asks idsDrg + confId <- withStore $ \st -> do + setHandshakeVersion st connId agentVersion duplexHS + createConfirmation st g newConfirmation + notify $ CONF confId connInfo _ -> prohibited - (SCDuplex, Nothing) -> do - agentRatchetDecrypt connId encConnInfo >>= parseMessage >>= \case + -- party accepting connection + (DuplexConnection _ _ sq, Nothing) -> do + withStore (\st -> agentRatchetDecrypt st connId encConnInfo) >>= parseMessage >>= \case AgentConnInfo connInfo -> do notify $ INFO connInfo - processConfirmation c rq $ SMPConfirmation {senderKey, e2ePubKey, connInfo} + processConfirmation c rq $ SMPConfirmation {senderKey, e2ePubKey, connInfo, smpReplyQueues = []} + when (duplexHandshake == Just True) $ enqueueDuplexHello sq _ -> prohibited _ -> prohibited _ -> prohibited @@ -787,42 +854,36 @@ processSMPTransmission c@AgentClient {smpClients, subQ} (srv, sessId, rId, cmd) Active -> prohibited _ -> do withStore $ \st -> setRcvQueueStatus st rq Active - case cType of - SCDuplex -> notifyConnected c connId + case conn of + DuplexConnection _ _ sq@SndQueue {status = sndStatus} + -- `sndStatus == Active` when HELLO was previously sent, and this is the reply HELLO + -- this branch is executed by the accepting party in duplexHandshake mode (v2) + -- and by the initiating party in v1 + -- Also see comment where HELLO is sent. + | sndStatus == Active -> atomically $ writeTBQueue subQ ("", connId, CON) + | duplexHandshake == Just True -> enqueueDuplexHello sq + | otherwise -> pure () _ -> pure () - replyMsg :: L.NonEmpty SMPQueueInfo -> m () - replyMsg (qInfo :| _) = do - logServer "<--" c srv rId "MSG " - case cType of - SCRcv -> do - AcceptedConfirmation {ownConnInfo} <- withStore (`getAcceptedConfirmation` connId) - case qInfo `proveCompatible` SMP.smpClientVRange of - Nothing -> notify . ERR $ AGENT A_VERSION - Just qInfo' -> do - sq <- newSndQueue qInfo' - withStore $ \st -> upgradeRcvConnToDuplex st connId sq - enqueueConfirmation c connId sq ownConnInfo Nothing - _ -> prohibited + enqueueDuplexHello :: SndQueue -> m () + enqueueDuplexHello sq = void $ enqueueMessage c cData sq SMP.MsgFlags {notification = True} HELLO - agentClientMsg :: PrevRcvMsgHash -> ExternalSndId -> (BrokerId, BrokerTs) -> MsgFlags -> MsgBody -> AgentMessageType -> m (InternalId, MsgMeta) - agentClientMsg externalPrevSndHash sndMsgId broker msgFlags msgBody msgType = do - logServer "<--" c srv rId "MSG " - let internalHash = C.sha256Hash msgBody - internalTs <- liftIO getCurrentTime - (internalId, internalRcvId, prevExtSndId, prevRcvMsgHash) <- withStore (`updateRcvIds` connId) - let integrity = checkMsgIntegrity prevExtSndId sndMsgId prevRcvMsgHash externalPrevSndHash - recipient = (unId internalId, internalTs) - msgMeta = MsgMeta {integrity, recipient, broker, sndMsgId} - rcvMsg = RcvMsgData {msgMeta, msgType, msgFlags, msgBody, internalRcvId, internalHash, externalPrevSndHash} - withStore $ \st -> createRcvMsg st connId rcvMsg - pure (internalId, msgMeta) + replyMsg :: L.NonEmpty SMPQueueInfo -> m () + replyMsg smpQueues = do + logServer "<--" c srv rId "MSG " + case duplexHandshake of + Just True -> prohibited + _ -> case conn of + RcvConnection {} -> do + AcceptedConfirmation {ownConnInfo} <- withStore (`getAcceptedConfirmation` connId) + connectReplyQueues c cData ownConnInfo smpQueues `catchError` (notify . ERR) + _ -> prohibited smpInvitation :: ConnectionRequestUri 'CMInvitation -> ConnInfo -> m () smpInvitation connReq cInfo = do logServer "<--" c srv rId "MSG " - case cType of - SCContact -> do + case conn of + ContactConnection {} -> do g <- asks idsDrg let newInv = NewInvitation {contactConnId = connId, connReq, recipientConnInfo = cInfo} invId <- withStore $ \st -> createInvitation st g newInv @@ -838,57 +899,70 @@ processSMPTransmission c@AgentClient {smpClients, subQ} (srv, sessId, rId, cmd) | internalPrevMsgHash /= receivedPrevMsgHash = MsgError MsgBadHash | otherwise = MsgError MsgDuplicate -- this case is not possible -confirmQueue :: forall m. AgentMonad m => AgentClient -> ConnId -> SndQueue -> ConnInfo -> Maybe (CR.E2ERatchetParams 'C.X448) -> m () -confirmQueue c connId sq connInfo e2eEncryption = do - _ <- withStore (`updateSndIds` connId) - msg <- mkConfirmation +connectReplyQueues :: AgentMonad m => AgentClient -> ConnData -> ConnInfo -> L.NonEmpty SMPQueueInfo -> m () +connectReplyQueues c cData@ConnData {connId} ownConnInfo (qInfo :| _) = do + -- TODO make this proof on receiving confirmation too + case qInfo `proveCompatible` SMP.smpClientVRange of + Nothing -> throwError $ AGENT A_VERSION + Just qInfo' -> do + sq <- newSndQueue qInfo' + withStore $ \st -> upgradeRcvConnToDuplex st connId sq + enqueueConfirmation c cData sq ownConnInfo Nothing + +confirmQueue :: forall m. AgentMonad m => Compatible Version -> AgentClient -> ConnId -> SndQueue -> ConnInfo -> Maybe (CR.E2ERatchetParams 'C.X448) -> m () +confirmQueue (Compatible agentVersion) c connId sq connInfo e2eEncryption = do + aMessage <- mkAgentMessage agentVersion + msg <- mkConfirmation aMessage sendConfirmation c sq msg withStore $ \st -> setSndQueueStatus st sq Confirmed where - mkConfirmation :: m MsgBody - mkConfirmation = do - encConnInfo <- agentRatchetEncrypt connId (smpEncode $ AgentConnInfo connInfo) e2eEncConnInfoLength - pure . smpEncode $ AgentConfirmation {agentVersion = smpAgentVersion, e2eEncryption, encConnInfo} + mkConfirmation :: AgentMessage -> m MsgBody + mkConfirmation aMessage = withStore $ \st -> do + void $ updateSndIds st connId + encConnInfo <- agentRatchetEncrypt st connId (smpEncode aMessage) e2eEncConnInfoLength + pure . smpEncode $ AgentConfirmation {agentVersion, e2eEncryption, encConnInfo} + mkAgentMessage :: Version -> m AgentMessage + mkAgentMessage 1 = pure $ AgentConnInfo connInfo + mkAgentMessage _ = do + qInfo <- createReplyQueue c connId + pure $ AgentConnInfoReply (qInfo :| []) connInfo -enqueueConfirmation :: forall m. AgentMonad m => AgentClient -> ConnId -> SndQueue -> ConnInfo -> Maybe (CR.E2ERatchetParams 'C.X448) -> m () -enqueueConfirmation c connId sq connInfo e2eEncryption = do - resumeMsgDelivery c connId sq +enqueueConfirmation :: forall m. AgentMonad m => AgentClient -> ConnData -> SndQueue -> ConnInfo -> Maybe (CR.E2ERatchetParams 'C.X448) -> m () +enqueueConfirmation c cData@ConnData {connId, connAgentVersion} sq connInfo e2eEncryption = do + resumeMsgDelivery c cData sq msgId <- storeConfirmation queuePendingMsgs c connId sq [msgId] where storeConfirmation :: m InternalId - storeConfirmation = do + storeConfirmation = withStore $ \st -> do internalTs <- liftIO getCurrentTime - (internalId, internalSndId, prevMsgHash) <- withStore (`updateSndIds` connId) + (internalId, internalSndId, prevMsgHash) <- updateSndIds st connId let agentMsg = AgentConnInfo connInfo agentMsgStr = smpEncode agentMsg internalHash = C.sha256Hash agentMsgStr - encConnInfo <- agentRatchetEncrypt connId agentMsgStr e2eEncConnInfoLength - let msgBody = smpEncode $ AgentConfirmation {agentVersion = smpAgentVersion, e2eEncryption, encConnInfo} + encConnInfo <- agentRatchetEncrypt st connId agentMsgStr e2eEncConnInfoLength + let msgBody = smpEncode $ AgentConfirmation {agentVersion = connAgentVersion, e2eEncryption, encConnInfo} msgType = agentMessageType agentMsg - msgData = SndMsgData {internalId, internalSndId, internalTs, msgType, msgBody, msgFlags = SMP.noMsgFlags, internalHash, prevMsgHash} - withStore $ \st -> createSndMsg st connId msgData + msgData = SndMsgData {internalId, internalSndId, internalTs, msgType, msgBody, msgFlags = SMP.MsgFlags {notification = True}, internalHash, prevMsgHash} + createSndMsg st connId msgData pure internalId -- encoded AgentMessage -> encoded EncAgentMessage -agentRatchetEncrypt :: AgentMonad m => ConnId -> ByteString -> Int -> m ByteString -agentRatchetEncrypt connId msg paddedLen = do - rc <- withStore $ \st -> getRatchet st connId - (encMsg, rc') <- liftError cryptoError $ CR.rcEncrypt rc paddedLen msg - withStore $ \st -> updateRatchet st connId rc' CR.SMDNoChange +agentRatchetEncrypt :: AgentStoreMonad m => SQLiteStore -> ConnId -> ByteString -> Int -> m ByteString +agentRatchetEncrypt st connId msg paddedLen = do + rc <- getRatchet st connId + (encMsg, rc') <- liftError (SEAgentError . cryptoError) $ CR.rcEncrypt rc paddedLen msg + updateRatchet st connId rc' CR.SMDNoChange pure encMsg -- encoded EncAgentMessage -> encoded AgentMessage -agentRatchetDecrypt :: AgentMonad m => ConnId -> ByteString -> m ByteString -agentRatchetDecrypt connId encAgentMsg = do - (rc, skipped) <- withStore $ \st -> - (,) <$> getRatchet st connId <*> getSkippedMsgKeys st connId - (agentMsgBody_, rc', skippedDiff) <- liftError cryptoError $ CR.rcDecrypt rc skipped encAgentMsg - withStore $ \st -> updateRatchet st connId rc' skippedDiff - liftEither $ first cryptoError agentMsgBody_ - -notifyConnected :: AgentMonad m => AgentClient -> ConnId -> m () -notifyConnected c connId = atomically $ writeTBQueue (subQ c) ("", connId, CON) +agentRatchetDecrypt :: AgentStoreMonad m => SQLiteStore -> ConnId -> ByteString -> m ByteString +agentRatchetDecrypt st connId encAgentMsg = do + rc <- getRatchet st connId + skipped <- getSkippedMsgKeys st connId + (agentMsgBody_, rc', skippedDiff) <- liftError (SEAgentError . cryptoError) $ CR.rcDecrypt rc skipped encAgentMsg + updateRatchet st connId rc' skippedDiff + liftEither $ first (SEAgentError . cryptoError) agentMsgBody_ newSndQueue :: (MonadUnliftIO m, MonadReader Env m) => Compatible SMPQueueInfo -> m SndQueue newSndQueue qInfo = diff --git a/src/Simplex/Messaging/Agent/Client.hs b/src/Simplex/Messaging/Agent/Client.hs index 999f0d786..c01882ceb 100644 --- a/src/Simplex/Messaging/Agent/Client.hs +++ b/src/Simplex/Messaging/Agent/Client.hs @@ -142,15 +142,15 @@ type AgentMonad m = (MonadUnliftIO m, MonadReader Env m, MonadError AgentErrorTy class ProtocolServerClient msg where getProtocolServerClient :: AgentMonad m => AgentClient -> ProtocolServer -> m (ProtocolClient msg) - protocolError :: ErrorType -> AgentErrorType + clientProtocolError :: ErrorType -> AgentErrorType instance ProtocolServerClient BrokerMsg where getProtocolServerClient = getSMPServerClient - protocolError = SMP + clientProtocolError = SMP instance ProtocolServerClient NtfResponse where getProtocolServerClient = getNtfServerClient - protocolError = NTF + clientProtocolError = NTF getSMPServerClient :: forall m. AgentMonad m => AgentClient -> SMPServer -> m SMPClient getSMPServerClient c@AgentClient {active, smpClients, msgQ} srv = do @@ -357,10 +357,10 @@ withLogClient_ c srv qId cmdStr action = do return res withClient :: forall m msg a. (AgentMonad m, ProtocolServerClient msg) => AgentClient -> ProtocolServer -> (ProtocolClient msg -> ExceptT ProtocolClientError IO a) -> m a -withClient c srv action = withClient_ c srv $ liftClient (protocolError @msg) . action +withClient c srv action = withClient_ c srv $ liftClient (clientProtocolError @msg) . action withLogClient :: forall m msg a. (AgentMonad m, ProtocolServerClient msg) => AgentClient -> ProtocolServer -> QueueId -> ByteString -> (ProtocolClient msg -> ExceptT ProtocolClientError IO a) -> m a -withLogClient c srv qId cmdStr action = withLogClient_ c srv qId cmdStr $ liftClient (protocolError @msg) . action +withLogClient c srv qId cmdStr action = withLogClient_ c srv qId cmdStr $ liftClient (clientProtocolError @msg) . action liftClient :: AgentMonad m => (ErrorType -> AgentErrorType) -> ExceptT ProtocolClientError IO a -> m a liftClient = liftError . protocolClientError @@ -477,7 +477,8 @@ sendInvitation c (Compatible SMPQueueInfo {smpServer, senderId, dhPublicKey}) co mkInvitation :: m ByteString -- this is only encrypted with per-queue E2E, not with double ratchet mkInvitation = do - let agentEnvelope = AgentInvitation {agentVersion = smpAgentVersion, connReq, connInfo} + agentVersion <- asks $ smpAgentVersion . config + let agentEnvelope = AgentInvitation {agentVersion, connReq, connInfo} agentCbEncryptOnce dhPublicKey . smpEncode $ SMP.ClientMessage SMP.PHEmpty $ smpEncode agentEnvelope @@ -565,4 +566,5 @@ cryptoError = \case C.CryptoHeaderError _ -> AGENT A_ENCRYPTION C.AESDecryptError -> AGENT A_ENCRYPTION C.CBDecryptError -> AGENT A_ENCRYPTION + -- C.CERatchetDuplicateMessage -> AGENT A_DUPLICATE e -> INTERNAL $ show e diff --git a/src/Simplex/Messaging/Agent/Env/SQLite.hs b/src/Simplex/Messaging/Agent/Env/SQLite.hs index 8dba33087..8693414fb 100644 --- a/src/Simplex/Messaging/Agent/Env/SQLite.hs +++ b/src/Simplex/Messaging/Agent/Env/SQLite.hs @@ -21,7 +21,7 @@ import Data.List.NonEmpty (NonEmpty) import Data.Time.Clock (NominalDiffTime, nominalDay) import Network.Socket import Numeric.Natural -import Simplex.Messaging.Agent.Protocol (SMPServer) +import Simplex.Messaging.Agent.Protocol (SMPServer, currentSMPAgentVersion, supportedSMPAgentVRange) import Simplex.Messaging.Agent.RetryInterval import Simplex.Messaging.Agent.Store.SQLite import qualified Simplex.Messaging.Agent.Store.SQLite.Migrations as Migrations @@ -29,6 +29,7 @@ import Simplex.Messaging.Client import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Notifications.Client (NtfServer) import Simplex.Messaging.Transport (TLS, Transport (..)) +import Simplex.Messaging.Version import System.Random (StdGen, newStdGen) import UnliftIO.STM @@ -52,7 +53,9 @@ data AgentConfig = AgentConfig resubscriptionConcurrency :: Int, caCertificateFile :: FilePath, privateKeyFile :: FilePath, - certificateFile :: FilePath + certificateFile :: FilePath, + smpAgentVersion :: Version, + smpAgentVRange :: VersionRange } defaultReconnectInterval :: RetryInterval @@ -73,7 +76,7 @@ defaultAgentConfig = connIdBytes = 12, tbqSize = 64, dbFile = "smp-agent.db", - dbPoolSize = 4, + dbPoolSize = 1, yesToMigrations = False, smpCfg = defaultClientConfig {defaultTransport = ("5223", transport @TLS)}, ntfCfg = defaultClientConfig {defaultTransport = ("443", transport @TLS)}, @@ -84,7 +87,9 @@ defaultAgentConfig = -- ! we do not generate these caCertificateFile = "/etc/opt/simplex-agent/ca.crt", privateKeyFile = "/etc/opt/simplex-agent/agent.key", - certificateFile = "/etc/opt/simplex-agent/agent.crt" + certificateFile = "/etc/opt/simplex-agent/agent.crt", + smpAgentVersion = currentSMPAgentVersion, + smpAgentVRange = supportedSMPAgentVRange } data Env = Env diff --git a/src/Simplex/Messaging/Agent/Protocol.hs b/src/Simplex/Messaging/Agent/Protocol.hs index bfb4b785a..7e5f50030 100644 --- a/src/Simplex/Messaging/Agent/Protocol.hs +++ b/src/Simplex/Messaging/Agent/Protocol.hs @@ -32,8 +32,8 @@ -- See https://github.com/simplex-chat/simplexmq/blob/master/protocol/agent-protocol.md module Simplex.Messaging.Agent.Protocol ( -- * Protocol parameters - smpAgentVersion, - smpAgentVRange, + currentSMPAgentVersion, + supportedSMPAgentVRange, e2eEncConnInfoLength, e2eEncUserMsgLength, @@ -147,11 +147,11 @@ import Test.QuickCheck (Arbitrary (..)) import Text.Read import UnliftIO.Exception (Exception) -smpAgentVersion :: Version -smpAgentVersion = 1 +currentSMPAgentVersion :: Version +currentSMPAgentVersion = 2 -smpAgentVRange :: VersionRange -smpAgentVRange = mkVersionRange 1 smpAgentVersion +supportedSMPAgentVRange :: VersionRange +supportedSMPAgentVRange = mkVersionRange 1 currentSMPAgentVersion -- it is shorter to allow all handshake headers, -- including E2E (double-ratchet) parameters and @@ -288,7 +288,9 @@ data SMPConfirmation = SMPConfirmation -- | sender's DH public key for simple per-queue e2e encryption e2ePubKey :: C.PublicKeyX25519, -- | sender's information to be associated with the connection, e.g. sender's profile information - connInfo :: ConnInfo + connInfo :: ConnInfo, + -- | optional reply queues included in confirmation (added in agent protocol v2) + smpReplyQueues :: [SMPQueueInfo] } deriving (Show) @@ -334,31 +336,40 @@ instance Encoding AgentMsgEnvelope where -- SMP agent message formats (after double ratchet decryption, -- or in case of AgentInvitation - in plain text body) -data AgentMessage = AgentConnInfo ConnInfo | AgentMessage APrivHeader AMessage +data AgentMessage + = AgentConnInfo ConnInfo + | -- AgentConnInfoReply is only used in duplexHandshake mode (v2), allowing to include reply queue(s) in the initial confirmation. + -- It makes REPLY message unnecessary. + AgentConnInfoReply (L.NonEmpty SMPQueueInfo) ConnInfo + | AgentMessage APrivHeader AMessage deriving (Show) instance Encoding AgentMessage where smpEncode = \case AgentConnInfo cInfo -> smpEncode ('I', Tail cInfo) + AgentConnInfoReply smpQueues cInfo -> smpEncode ('D', smpQueues, Tail cInfo) -- 'D' stands for "duplex" AgentMessage hdr aMsg -> smpEncode ('M', hdr, aMsg) smpP = smpP >>= \case 'I' -> AgentConnInfo . unTail <$> smpP + 'D' -> AgentConnInfoReply <$> smpP <*> (unTail <$> smpP) 'M' -> AgentMessage <$> smpP <*> smpP _ -> fail "bad AgentMessage" -data AgentMessageType = AM_CONN_INFO | AM_HELLO_ | AM_REPLY_ | AM_A_MSG_ +data AgentMessageType = AM_CONN_INFO | AM_CONN_INFO_REPLY | AM_HELLO_ | AM_REPLY_ | AM_A_MSG_ deriving (Eq, Show) instance Encoding AgentMessageType where smpEncode = \case AM_CONN_INFO -> "C" + AM_CONN_INFO_REPLY -> "D" AM_HELLO_ -> "H" AM_REPLY_ -> "R" AM_A_MSG_ -> "M" smpP = A.anyChar >>= \case 'C' -> pure AM_CONN_INFO + 'D' -> pure AM_CONN_INFO_REPLY 'H' -> pure AM_HELLO_ 'R' -> pure AM_REPLY_ 'M' -> pure AM_A_MSG_ @@ -367,8 +378,14 @@ instance Encoding AgentMessageType where agentMessageType :: AgentMessage -> AgentMessageType agentMessageType = \case AgentConnInfo _ -> AM_CONN_INFO + AgentConnInfoReply {} -> AM_CONN_INFO_REPLY AgentMessage _ aMsg -> case aMsg of + -- HELLO is used both in v1 and in v2, but differently. + -- - in v1 (and, possibly, in v2 for simplex connections) can be sent multiple times, + -- until the queue is secured - the OK response from the server instead of initial AUTH errors confirms it. + -- - in v2 duplexHandshake it is sent only once, when it is known that the queue was secured. HELLO -> AM_HELLO_ + -- REPLY is only used in v1 REPLY _ -> AM_REPLY_ A_MSG _ -> AM_A_MSG_ @@ -766,6 +783,8 @@ data SMPAgentError A_VERSION | -- | cannot decrypt message A_ENCRYPTION + | -- | duplicate message - this error is detected by ratchet decryption - this message will be ignored and not shown + A_DUPLICATE deriving (Eq, Generic, Read, Show, Exception) instance ToJSON SMPAgentError where diff --git a/src/Simplex/Messaging/Agent/Store.hs b/src/Simplex/Messaging/Agent/Store.hs index d2629333e..31224488a 100644 --- a/src/Simplex/Messaging/Agent/Store.hs +++ b/src/Simplex/Messaging/Agent/Store.hs @@ -31,6 +31,7 @@ import Simplex.Messaging.Protocol SndPrivateSignKey, ) import qualified Simplex.Messaging.Protocol as SMP +import Simplex.Messaging.Version -- * Store management @@ -53,6 +54,7 @@ class Monad m => MonadAgentStore s m where acceptConfirmation :: s -> ConfirmationId -> ConnInfo -> m AcceptedConfirmation getAcceptedConfirmation :: s -> ConnId -> m AcceptedConfirmation removeConfirmations :: s -> ConnId -> m () + setHandshakeVersion :: s -> ConnId -> Version -> Bool -> m () -- Invitations - sent via Contact connections createInvitation :: s -> TVar ChaChaDRG -> NewInvitation -> m InvitationId @@ -184,7 +186,11 @@ instance Eq SomeConn where deriving instance Show SomeConn -newtype ConnData = ConnData {connId :: ConnId} +data ConnData = ConnData + { connId :: ConnId, + connAgentVersion :: Version, + duplexHandshake :: Maybe Bool -- added in agent protocol v2 + } deriving (Eq, Show) -- * Confirmation types @@ -327,4 +333,6 @@ data StoreError SEX3dhKeysNotFound | -- | Used in `getMsg` that is not implemented/used. TODO remove. SENotImplemented + | -- | Used to wrap agent errors inside store operations to avoid race conditions + SEAgentError AgentErrorType deriving (Eq, Show, Exception) diff --git a/src/Simplex/Messaging/Agent/Store/SQLite.hs b/src/Simplex/Messaging/Agent/Store/SQLite.hs index 0eea2bc11..c22ec5408 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE ConstraintKinds #-} {-# LANGUAGE DataKinds #-} {-# LANGUAGE DuplicateRecordFields #-} {-# LANGUAGE FlexibleContexts #-} @@ -19,6 +20,7 @@ module Simplex.Messaging.Agent.Store.SQLite ( SQLiteStore (..), + AgentStoreMonad, createSQLiteStore, connectSQLiteStore, withConnection, @@ -40,7 +42,7 @@ import Data.Char (toLower) import Data.Functor (($>)) import Data.List (find, foldl', partition) import qualified Data.Map.Strict as M -import Data.Maybe (listToMaybe) +import Data.Maybe (fromMaybe, listToMaybe) import Data.Text (Text) import qualified Data.Text as T import Data.Text.Encoding (decodeLatin1, encodeUtf8) @@ -64,6 +66,7 @@ import Simplex.Messaging.Parsers (blobFieldParser, fromTextField_) import Simplex.Messaging.Protocol (MsgBody, MsgFlags, ProtocolServer (..)) import qualified Simplex.Messaging.Protocol as SMP import Simplex.Messaging.Util (bshow, eitherToMaybe, liftIOEither) +import Simplex.Messaging.Version import System.Directory (copyFile, createDirectoryIfMissing, doesFileExist) import System.Exit (exitFailure) import System.FilePath (takeDirectory) @@ -187,19 +190,21 @@ createConn_ st gVar cData create = ConnData {connId = ""} -> createWithRandomId gVar $ create db ConnData {connId} -> create db connId $> Right connId +type AgentStoreMonad m = (MonadUnliftIO m, MonadError StoreError m, MonadAgentStore SQLiteStore m) + instance (MonadUnliftIO m, MonadError StoreError m) => MonadAgentStore SQLiteStore m where createRcvConn :: SQLiteStore -> TVar ChaChaDRG -> ConnData -> RcvQueue -> SConnectionMode c -> m ConnId createRcvConn st gVar cData q@RcvQueue {server} cMode = createConn_ st gVar cData $ \db connId -> do upsertServer_ db server - DB.execute db "INSERT INTO connections (conn_id, conn_mode) VALUES (?, ?)" (connId, cMode) + DB.execute db "INSERT INTO connections (conn_id, conn_mode, smp_agent_version, duplex_handshake) VALUES (?, ?, ?, ?)" (connId, cMode, connAgentVersion cData, duplexHandshake cData) insertRcvQueue_ db connId q createSndConn :: SQLiteStore -> TVar ChaChaDRG -> ConnData -> SndQueue -> m ConnId createSndConn st gVar cData q@SndQueue {server} = createConn_ st gVar cData $ \db connId -> do upsertServer_ db server - DB.execute db "INSERT INTO connections (conn_id, conn_mode) VALUES (?, ?)" (connId, SCMInvitation) + DB.execute db "INSERT INTO connections (conn_id, conn_mode, smp_agent_version, duplex_handshake) VALUES (?, ?, ?, ?)" (connId, SCMInvitation, connAgentVersion cData, duplexHandshake cData) insertSndQueue_ db connId q getConn :: SQLiteStore -> ConnId -> m SomeConn @@ -297,16 +302,16 @@ instance (MonadUnliftIO m, MonadError StoreError m) => MonadAgentStore SQLiteSto [":status" := status, ":host" := host, ":port" := port, ":snd_id" := sndId] createConfirmation :: SQLiteStore -> TVar ChaChaDRG -> NewConfirmation -> m ConfirmationId - createConfirmation st gVar NewConfirmation {connId, senderConf = SMPConfirmation {senderKey, e2ePubKey, connInfo}, ratchetState} = + createConfirmation st gVar NewConfirmation {connId, senderConf = SMPConfirmation {senderKey, e2ePubKey, connInfo, smpReplyQueues}, ratchetState} = liftIOEither . withTransaction st $ \db -> createWithRandomId gVar $ \confirmationId -> DB.execute db [sql| INSERT INTO conn_confirmations - (confirmation_id, conn_id, sender_key, e2e_snd_pub_key, ratchet_state, sender_conn_info, accepted) VALUES (?, ?, ?, ?, ?, ?, 0); + (confirmation_id, conn_id, sender_key, e2e_snd_pub_key, ratchet_state, sender_conn_info, smp_reply_queues, accepted) VALUES (?, ?, ?, ?, ?, ?, ?, 0); |] - (confirmationId, connId, senderKey, e2ePubKey, ratchetState, connInfo) + (confirmationId, connId, senderKey, e2ePubKey, ratchetState, connInfo, smpReplyQueues) acceptConfirmation :: SQLiteStore -> ConfirmationId -> ConnInfo -> m AcceptedConfirmation acceptConfirmation st confirmationId ownConnInfo = @@ -326,17 +331,17 @@ instance (MonadUnliftIO m, MonadError StoreError m) => MonadAgentStore SQLiteSto DB.query db [sql| - SELECT conn_id, sender_key, e2e_snd_pub_key, ratchet_state, sender_conn_info + SELECT conn_id, sender_key, e2e_snd_pub_key, ratchet_state, sender_conn_info, smp_reply_queues FROM conn_confirmations WHERE confirmation_id = ?; |] (Only confirmationId) where - confirmation (connId, senderKey, e2ePubKey, ratchetState, connInfo) = + confirmation (connId, senderKey, e2ePubKey, ratchetState, connInfo, smpReplyQueues_) = AcceptedConfirmation { confirmationId, connId, - senderConf = SMPConfirmation {senderKey, e2ePubKey, connInfo}, + senderConf = SMPConfirmation {senderKey, e2ePubKey, connInfo, smpReplyQueues = fromMaybe [] smpReplyQueues_}, ratchetState, ownConnInfo } @@ -348,17 +353,17 @@ instance (MonadUnliftIO m, MonadError StoreError m) => MonadAgentStore SQLiteSto DB.query db [sql| - SELECT confirmation_id, sender_key, e2e_snd_pub_key, ratchet_state, sender_conn_info, own_conn_info + SELECT confirmation_id, sender_key, e2e_snd_pub_key, ratchet_state, sender_conn_info, smp_reply_queues, own_conn_info FROM conn_confirmations WHERE conn_id = ? AND accepted = 1; |] (Only connId) where - confirmation (confirmationId, senderKey, e2ePubKey, ratchetState, connInfo, ownConnInfo) = + confirmation (confirmationId, senderKey, e2ePubKey, ratchetState, connInfo, smpReplyQueues_, ownConnInfo) = AcceptedConfirmation { confirmationId, connId, - senderConf = SMPConfirmation {senderKey, e2ePubKey, connInfo}, + senderConf = SMPConfirmation {senderKey, e2ePubKey, connInfo, smpReplyQueues = fromMaybe [] smpReplyQueues_}, ratchetState, ownConnInfo } @@ -374,6 +379,11 @@ instance (MonadUnliftIO m, MonadError StoreError m) => MonadAgentStore SQLiteSto |] [":conn_id" := connId] + setHandshakeVersion :: SQLiteStore -> ConnId -> Version -> Bool -> m () + setHandshakeVersion st connId aVersion duplexHS = + liftIO . withTransaction st $ \db -> + DB.execute db "UPDATE connections SET smp_agent_version = ?, duplex_handshake = ? WHERE conn_id = ?" (aVersion, duplexHS, connId) + createInvitation :: SQLiteStore -> TVar ChaChaDRG -> NewInvitation -> m InvitationId createInvitation st gVar NewInvitation {contactConnId, connReq, recipientConnInfo} = liftIOEither . withTransaction st $ \db -> @@ -685,6 +695,10 @@ instance ToField MsgFlags where toField = toField . decodeLatin1 . smpEncode instance FromField MsgFlags where fromField = fromTextField_ $ eitherToMaybe . smpDecode . encodeUtf8 +instance ToField [SMPQueueInfo] where toField = toField . smpEncodeList + +instance FromField [SMPQueueInfo] where fromField = blobFieldParser smpListP + listToEither :: e -> [a] -> Either e a listToEither _ (x : _) = Right x listToEither e _ = Left e @@ -692,6 +706,10 @@ listToEither e _ = Left e firstRow :: (a -> b) -> e -> IO [a] -> IO (Either e b) firstRow f e a = second f . listToEither e <$> a +-- TODO move from simplex-chat +-- firstRow' :: (a -> Either e b) -> e -> IO [a] -> IO (Either e b) +-- firstRow' f e a = (f <=< listToEither e) <$> a + {- ORMOLU_DISABLE -} -- SQLite.Simple only has these up to 10 fields, which is insufficient for some of our queries instance (FromField a, FromField b, FromField c, FromField d, FromField e, @@ -791,9 +809,9 @@ getConn_ dbConn connId = getConnData_ :: DB.Connection -> ConnId -> IO (Maybe (ConnData, ConnectionMode)) getConnData_ dbConn connId' = connData - <$> DB.query dbConn "SELECT conn_id, conn_mode FROM connections WHERE conn_id = ?;" (Only connId') + <$> DB.query dbConn "SELECT conn_id, conn_mode, smp_agent_version, duplex_handshake FROM connections WHERE conn_id = ?;" (Only connId') where - connData [(connId, cMode)] = Just (ConnData {connId}, cMode) + connData [(connId, cMode, connAgentVersion, duplexHandshake)] = Just (ConnData {connId, connAgentVersion, duplexHandshake}, cMode) connData _ = Nothing getRcvQueueByConnId_ :: DB.Connection -> ConnId -> IO (Maybe RcvQueue) diff --git a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations.hs b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations.hs index 449c7c340..83a660edc 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations.hs @@ -27,7 +27,7 @@ import qualified Database.SQLite3 as SQLite3 import Simplex.Messaging.Agent.Store.SQLite.Migrations.M20220101_initial import Simplex.Messaging.Agent.Store.SQLite.Migrations.M20220301_snd_queue_keys import Simplex.Messaging.Agent.Store.SQLite.Migrations.M20220322_notifications -import Simplex.Messaging.Agent.Store.SQLite.Migrations.M20220605_msg_flags +import Simplex.Messaging.Agent.Store.SQLite.Migrations.M20220608_v2 data Migration = Migration {name :: String, up :: Text} deriving (Show) @@ -37,7 +37,7 @@ schemaMigrations = [ ("20220101_initial", m20220101_initial), ("20220301_snd_queue_keys", m20220301_snd_queue_keys), ("20220322_notifications", m20220322_notifications), - ("20220605_msg_flags", m20220605_msg_flags) + ("20220607_v2", m20220608_v2) ] -- | The list of migrations in ascending order by date diff --git a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220605_msg_flags.hs b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220605_msg_flags.hs deleted file mode 100644 index 2b6311a63..000000000 --- a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220605_msg_flags.hs +++ /dev/null @@ -1,12 +0,0 @@ -{-# LANGUAGE QuasiQuotes #-} - -module Simplex.Messaging.Agent.Store.SQLite.Migrations.M20220605_msg_flags where - -import Database.SQLite.Simple (Query) -import Database.SQLite.Simple.QQ (sql) - -m20220605_msg_flags :: Query -m20220605_msg_flags = - [sql| -ALTER TABLE messages ADD COLUMN msg_flags TEXT NULL; -|] diff --git a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220608_v2.hs b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220608_v2.hs new file mode 100644 index 000000000..334f10555 --- /dev/null +++ b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220608_v2.hs @@ -0,0 +1,16 @@ +{-# LANGUAGE QuasiQuotes #-} + +module Simplex.Messaging.Agent.Store.SQLite.Migrations.M20220608_v2 where + +import Database.SQLite.Simple (Query) +import Database.SQLite.Simple.QQ (sql) + +m20220608_v2 :: Query +m20220608_v2 = + [sql| +ALTER TABLE messages ADD COLUMN msg_flags TEXT NULL; + +ALTER TABLE conn_confirmations ADD COLUMN smp_reply_queues BLOB NULL; + +ALTER TABLE connections ADD COLUMN duplex_handshake INTEGER NULL DEFAULT 0; +|] diff --git a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql index 1e694c87f..a016c1dae 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql +++ b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql @@ -19,6 +19,8 @@ CREATE TABLE connections( last_rcv_msg_hash BLOB NOT NULL DEFAULT x'', last_snd_msg_hash BLOB NOT NULL DEFAULT x'', smp_agent_version INTEGER NOT NULL DEFAULT 1 + , + duplex_handshake INTEGER NULL DEFAULT 0 ) WITHOUT ROWID; CREATE TABLE rcv_queues( host TEXT NOT NULL, @@ -105,6 +107,8 @@ CREATE TABLE conn_confirmations( accepted INTEGER NOT NULL, own_conn_info BLOB, created_at TEXT NOT NULL DEFAULT(datetime('now')) + , + smp_reply_queues BLOB NULL ) WITHOUT ROWID; CREATE TABLE conn_invitations( invitation_id BLOB NOT NULL PRIMARY KEY, diff --git a/src/Simplex/Messaging/Client.hs b/src/Simplex/Messaging/Client.hs index 10fef28de..6c37008af 100644 --- a/src/Simplex/Messaging/Client.hs +++ b/src/Simplex/Messaging/Client.hs @@ -106,7 +106,9 @@ data ProtocolClientConfig = ProtocolClientConfig -- | TCP keep-alive options, Nothing to skip enabling keep-alive tcpKeepAlive :: Maybe KeepAliveOpts, -- | period for SMP ping commands (microseconds) - smpPing :: Int + smpPing :: Int, + -- | SMP client-server protocol version range + smpServerVRange :: VersionRange } -- | Default protocol client configuration. @@ -117,7 +119,8 @@ defaultClientConfig = defaultTransport = ("443", transport @TLS), tcpTimeout = 5_000_000, tcpKeepAlive = Just defaultKeepAliveOpts, - smpPing = 600_000_000 -- 10min + smpPing = 600_000_000, -- 10min + smpServerVRange = supportedSMPServerVRange } data Request msg = Request @@ -133,7 +136,7 @@ type Response msg = Either ProtocolClientError msg -- A single queue can be used for multiple 'SMPClient' instances, -- as 'SMPServerTransmission' includes server information. getProtocolClient :: forall msg. Protocol msg => ProtocolServer -> ProtocolClientConfig -> Maybe (TBQueue (ServerTransmission msg)) -> IO () -> IO (Either ProtocolClientError (ProtocolClient msg)) -getProtocolClient protocolServer cfg@ProtocolClientConfig {qSize, tcpTimeout, tcpKeepAlive, smpPing} msgQ disconnected = +getProtocolClient protocolServer cfg@ProtocolClientConfig {qSize, tcpTimeout, tcpKeepAlive, smpPing, smpServerVRange} msgQ disconnected = (atomically mkProtocolClient >>= runClient useTransport) `catch` \(e :: IOException) -> pure . Left $ PCEIOError e where @@ -180,7 +183,7 @@ getProtocolClient protocolServer cfg@ProtocolClientConfig {qSize, tcpTimeout, tc client :: forall c. Transport c => TProxy c -> ProtocolClient msg -> TMVar (Either ProtocolClientError (THandle c)) -> c -> IO () client _ c thVar h = - runExceptT (protocolClientHandshake @msg h $ keyHash protocolServer) >>= \case + runExceptT (protocolClientHandshake @msg h (keyHash protocolServer) smpServerVRange) >>= \case Left e -> atomically . putTMVar thVar . Left $ PCETransportError e Right th@THandle {sessionId, thVersion} -> do atomically $ do diff --git a/src/Simplex/Messaging/Crypto.hs b/src/Simplex/Messaging/Crypto.hs index 7caa6dedb..6ce1af779 100644 --- a/src/Simplex/Messaging/Crypto.hs +++ b/src/Simplex/Messaging/Crypto.hs @@ -678,7 +678,9 @@ data CryptoError CERatchetHeader | -- | too many skipped messages CERatchetTooManySkipped - | -- | duplicate message number (or, possibly, skipped message that failed to decrypt?) + | -- | earlier message number (or, possibly, skipped message that failed to decrypt?) + CERatchetEarlierMessage + | -- | duplicate message number CERatchetDuplicateMessage deriving (Eq, Show, Exception) diff --git a/src/Simplex/Messaging/Crypto/Ratchet.hs b/src/Simplex/Messaging/Crypto/Ratchet.hs index 9e6ca20e7..84c1fee09 100644 --- a/src/Simplex/Messaging/Crypto/Ratchet.hs +++ b/src/Simplex/Messaging/Crypto/Ratchet.hs @@ -416,7 +416,8 @@ rcDecrypt rc@Ratchet {rcRcv, rcAD = Str rcAD} rcMKSkipped msg' = do skipMessageKeys :: Word32 -> Ratchet a -> Either CryptoError (Ratchet a, SkippedMsgKeys) skipMessageKeys _ r@Ratchet {rcRcv = Nothing} = Right (r, M.empty) skipMessageKeys untilN r@Ratchet {rcRcv = Just rr@RcvRatchet {rcCKr, rcHKr}, rcNr} - | rcNr > untilN = Left CERatchetDuplicateMessage + | rcNr > untilN + 1 = Left CERatchetEarlierMessage + | rcNr == untilN + 1 = Left CERatchetDuplicateMessage | rcNr + maxSkip < untilN = Left CERatchetTooManySkipped | rcNr == untilN = Right (r, M.empty) | otherwise = diff --git a/src/Simplex/Messaging/Notifications/Server.hs b/src/Simplex/Messaging/Notifications/Server.hs index 393ef56de..a9b51aae0 100644 --- a/src/Simplex/Messaging/Notifications/Server.hs +++ b/src/Simplex/Messaging/Notifications/Server.hs @@ -61,7 +61,7 @@ ntfServer NtfServerConfig {transports} started = do runClient :: Transport c => TProxy c -> c -> m () runClient _ h = do kh <- asks serverIdentity - liftIO (runExceptT $ ntfServerHandshake h kh) >>= \case + liftIO (runExceptT $ ntfServerHandshake h kh supportedNTFServerVRange) >>= \case Right th -> runNtfClientTransport th Left _ -> pure () diff --git a/src/Simplex/Messaging/Notifications/Transport.hs b/src/Simplex/Messaging/Notifications/Transport.hs index 8d9b77b5c..f988be495 100644 --- a/src/Simplex/Messaging/Notifications/Transport.hs +++ b/src/Simplex/Messaging/Notifications/Transport.hs @@ -3,17 +3,21 @@ module Simplex.Messaging.Notifications.Transport where import Control.Monad.Except import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Transport +import Simplex.Messaging.Version ntfBlockSize :: Int ntfBlockSize = 512 +supportedNTFServerVRange :: VersionRange +supportedNTFServerVRange = mkVersionRange 1 1 + -- | Notifcations server transport handshake. -ntfServerHandshake :: Transport c => c -> C.KeyHash -> ExceptT TransportError IO (THandle c) -ntfServerHandshake c _ = pure $ ntfTHandle c +ntfServerHandshake :: Transport c => c -> C.KeyHash -> VersionRange -> ExceptT TransportError IO (THandle c) +ntfServerHandshake c _ _ = pure $ ntfTHandle c -- | Notifcations server client transport handshake. -ntfClientHandshake :: Transport c => c -> C.KeyHash -> ExceptT TransportError IO (THandle c) -ntfClientHandshake c _ = pure $ ntfTHandle c +ntfClientHandshake :: Transport c => c -> C.KeyHash -> VersionRange -> ExceptT TransportError IO (THandle c) +ntfClientHandshake c _ _ = pure $ ntfTHandle c ntfTHandle :: Transport c => c -> THandle c ntfTHandle c = THandle {connection = c, sessionId = tlsUnique c, blockSize = ntfBlockSize, thVersion = 0} diff --git a/src/Simplex/Messaging/Protocol.hs b/src/Simplex/Messaging/Protocol.hs index bb4d9593a..591348cc3 100644 --- a/src/Simplex/Messaging/Protocol.hs +++ b/src/Simplex/Messaging/Protocol.hs @@ -584,7 +584,7 @@ transmissionP = do class (ProtocolEncoding msg, ProtocolEncoding (ProtocolCommand msg)) => Protocol msg where type ProtocolCommand msg = cmd | cmd -> msg - protocolClientHandshake :: forall c. Transport c => c -> C.KeyHash -> ExceptT TransportError IO (THandle c) + protocolClientHandshake :: forall c. Transport c => c -> C.KeyHash -> VersionRange -> ExceptT TransportError IO (THandle c) protocolPing :: ProtocolCommand msg protocolError :: msg -> Maybe ErrorType diff --git a/src/Simplex/Messaging/Server.hs b/src/Simplex/Messaging/Server.hs index b43cad9cb..49694e2d6 100644 --- a/src/Simplex/Messaging/Server.hs +++ b/src/Simplex/Messaging/Server.hs @@ -175,7 +175,8 @@ smpServer started = do runClient :: Transport c => TProxy c -> c -> m () runClient _ h = do kh <- asks serverIdentity - liftIO (runExceptT $ smpServerHandshake h kh) >>= \case + smpVRange <- asks $ smpServerVRange . config + liftIO (runExceptT $ smpServerHandshake h kh smpVRange) >>= \case Right th -> runClientTransport th Left _ -> pure () diff --git a/src/Simplex/Messaging/Server/Env/STM.hs b/src/Simplex/Messaging/Server/Env/STM.hs index 1440486a3..d1f0f8bbc 100644 --- a/src/Simplex/Messaging/Server/Env/STM.hs +++ b/src/Simplex/Messaging/Server/Env/STM.hs @@ -31,6 +31,7 @@ import Simplex.Messaging.TMap (TMap) import qualified Simplex.Messaging.TMap as TM import Simplex.Messaging.Transport (ATransport) import Simplex.Messaging.Transport.Server (loadFingerprint, loadTLSServerParams) +import Simplex.Messaging.Version import System.IO (IOMode (..)) import System.Mem.Weak (Weak) import UnliftIO.STM @@ -58,7 +59,9 @@ data ServerConfig = ServerConfig -- | CA certificate private key is not needed for initialization caCertificateFile :: FilePath, privateKeyFile :: FilePath, - certificateFile :: FilePath + certificateFile :: FilePath, + -- | SMP client-server protocol version range + smpServerVRange :: VersionRange } defaultMessageExpiration :: ExpirationConfig diff --git a/src/Simplex/Messaging/Transport.hs b/src/Simplex/Messaging/Transport.hs index 680cc36b4..944518905 100644 --- a/src/Simplex/Messaging/Transport.hs +++ b/src/Simplex/Messaging/Transport.hs @@ -26,7 +26,7 @@ -- See https://github.com/simplex-chat/simplexmq/blob/master/protocol/simplex-messaging.md#appendix-a module Simplex.Messaging.Transport ( -- * SMP transport parameters - supportedSMPVersions, + supportedSMPServerVRange, simplexMQVersion, -- * Transport connection class @@ -92,8 +92,8 @@ import UnliftIO.STM smpBlockSize :: Int smpBlockSize = 16384 -supportedSMPVersions :: VersionRange -supportedSMPVersions = mkVersionRange 1 2 +supportedSMPServerVRange :: VersionRange +supportedSMPServerVRange = mkVersionRange 1 2 simplexMQVersion :: String simplexMQVersion = "2.2.1" @@ -351,28 +351,28 @@ tGetBlock THandle {connection = c, blockSize} = -- | Server SMP transport handshake. -- -- See https://github.com/simplex-chat/simplexmq/blob/master/protocol/simplex-messaging.md#appendix-a -smpServerHandshake :: forall c. Transport c => c -> C.KeyHash -> ExceptT TransportError IO (THandle c) -smpServerHandshake c kh = do +smpServerHandshake :: forall c. Transport c => c -> C.KeyHash -> VersionRange -> ExceptT TransportError IO (THandle c) +smpServerHandshake c kh smpVRange = do let th@THandle {sessionId} = smpTHandle c - sendHandshake th $ ServerHandshake {sessionId, smpVersionRange = supportedSMPVersions} + sendHandshake th $ ServerHandshake {sessionId, smpVersionRange = smpVRange} getHandshake th >>= \case ClientHandshake {smpVersion, keyHash} | keyHash /= kh -> throwE $ TEHandshake IDENTITY - | smpVersion `isCompatible` supportedSMPVersions -> do + | smpVersion `isCompatible` smpVRange -> do pure (th :: THandle c) {thVersion = smpVersion} | otherwise -> throwE $ TEHandshake VERSION -- | Client SMP transport handshake. -- -- See https://github.com/simplex-chat/simplexmq/blob/master/protocol/simplex-messaging.md#appendix-a -smpClientHandshake :: forall c. Transport c => c -> C.KeyHash -> ExceptT TransportError IO (THandle c) -smpClientHandshake c keyHash = do +smpClientHandshake :: forall c. Transport c => c -> C.KeyHash -> VersionRange -> ExceptT TransportError IO (THandle c) +smpClientHandshake c keyHash smpVRange = do let th@THandle {sessionId} = smpTHandle c ServerHandshake {sessionId = sessId, smpVersionRange} <- getHandshake th if sessionId /= sessId then throwE TEBadSession - else case smpVersionRange `compatibleVersion` supportedSMPVersions of + else case smpVersionRange `compatibleVersion` smpVRange of Just (Compatible smpVersion) -> do sendHandshake th $ ClientHandshake {smpVersion, keyHash} pure (th :: THandle c) {thVersion = smpVersion} diff --git a/tests/AgentTests.hs b/tests/AgentTests.hs index 7984418f3..c921e91c2 100644 --- a/tests/AgentTests.hs +++ b/tests/AgentTests.hs @@ -132,25 +132,25 @@ testDuplexConnection _ alice bob = do bob <# ("", "alice", CON) alice <# ("", "bob", CON) -- message IDs 1 to 3 get assigned to control messages, so first MSG is assigned ID 4 - alice #: ("3", "bob", "SEND F :hello") #> ("3", "bob", MID 5) - alice <# ("", "bob", SENT 5) + alice #: ("3", "bob", "SEND F :hello") #> ("3", "bob", MID 4) + alice <# ("", "bob", SENT 4) bob <#= \case ("", "alice", Msg "hello") -> True; _ -> False - bob #: ("12", "alice", "ACK 5") #> ("12", "alice", OK) - alice #: ("4", "bob", "SEND F :how are you?") #> ("4", "bob", MID 6) - alice <# ("", "bob", SENT 6) + bob #: ("12", "alice", "ACK 4") #> ("12", "alice", OK) + alice #: ("4", "bob", "SEND F :how are you?") #> ("4", "bob", MID 5) + alice <# ("", "bob", SENT 5) bob <#= \case ("", "alice", Msg "how are you?") -> True; _ -> False - bob #: ("13", "alice", "ACK 6") #> ("13", "alice", OK) - bob #: ("14", "alice", "SEND F 9\nhello too") #> ("14", "alice", MID 7) - bob <# ("", "alice", SENT 7) + bob #: ("13", "alice", "ACK 5") #> ("13", "alice", OK) + bob #: ("14", "alice", "SEND F 9\nhello too") #> ("14", "alice", MID 6) + bob <# ("", "alice", SENT 6) alice <#= \case ("", "bob", Msg "hello too") -> True; _ -> False - alice #: ("3a", "bob", "ACK 7") #> ("3a", "bob", OK) - bob #: ("15", "alice", "SEND F 9\nmessage 1") #> ("15", "alice", MID 8) - bob <# ("", "alice", SENT 8) + alice #: ("3a", "bob", "ACK 6") #> ("3a", "bob", OK) + bob #: ("15", "alice", "SEND F 9\nmessage 1") #> ("15", "alice", MID 7) + bob <# ("", "alice", SENT 7) alice <#= \case ("", "bob", Msg "message 1") -> True; _ -> False - alice #: ("4a", "bob", "ACK 8") #> ("4a", "bob", OK) + alice #: ("4a", "bob", "ACK 7") #> ("4a", "bob", OK) alice #: ("5", "bob", "OFF") #> ("5", "bob", OK) - bob #: ("17", "alice", "SEND F 9\nmessage 3") #> ("17", "alice", MID 9) - bob <# ("", "alice", MERR 9 (SMP AUTH)) + bob #: ("17", "alice", "SEND F 9\nmessage 3") #> ("17", "alice", MID 8) + bob <# ("", "alice", MERR 8 (SMP AUTH)) alice #: ("6", "bob", "DEL") #> ("6", "bob", OK) alice #:# "nothing else should be delivered to alice" @@ -165,25 +165,25 @@ testDuplexConnRandomIds _ alice bob = do bob <# ("", aliceConn, INFO "alice's connInfo") bob <# ("", aliceConn, CON) alice <# ("", bobConn, CON) - alice #: ("2", bobConn, "SEND F :hello") #> ("2", bobConn, MID 5) - alice <# ("", bobConn, SENT 5) + alice #: ("2", bobConn, "SEND F :hello") #> ("2", bobConn, MID 4) + alice <# ("", bobConn, SENT 4) bob <#= \case ("", c, Msg "hello") -> c == aliceConn; _ -> False - bob #: ("12", aliceConn, "ACK 5") #> ("12", aliceConn, OK) - alice #: ("3", bobConn, "SEND F :how are you?") #> ("3", bobConn, MID 6) - alice <# ("", bobConn, SENT 6) + bob #: ("12", aliceConn, "ACK 4") #> ("12", aliceConn, OK) + alice #: ("3", bobConn, "SEND F :how are you?") #> ("3", bobConn, MID 5) + alice <# ("", bobConn, SENT 5) bob <#= \case ("", c, Msg "how are you?") -> c == aliceConn; _ -> False - bob #: ("13", aliceConn, "ACK 6") #> ("13", aliceConn, OK) - bob #: ("14", aliceConn, "SEND F 9\nhello too") #> ("14", aliceConn, MID 7) - bob <# ("", aliceConn, SENT 7) + bob #: ("13", aliceConn, "ACK 5") #> ("13", aliceConn, OK) + bob #: ("14", aliceConn, "SEND F 9\nhello too") #> ("14", aliceConn, MID 6) + bob <# ("", aliceConn, SENT 6) alice <#= \case ("", c, Msg "hello too") -> c == bobConn; _ -> False - alice #: ("3a", bobConn, "ACK 7") #> ("3a", bobConn, OK) - bob #: ("15", aliceConn, "SEND F 9\nmessage 1") #> ("15", aliceConn, MID 8) - bob <# ("", aliceConn, SENT 8) + alice #: ("3a", bobConn, "ACK 6") #> ("3a", bobConn, OK) + bob #: ("15", aliceConn, "SEND F 9\nmessage 1") #> ("15", aliceConn, MID 7) + bob <# ("", aliceConn, SENT 7) alice <#= \case ("", c, Msg "message 1") -> c == bobConn; _ -> False - alice #: ("4a", bobConn, "ACK 8") #> ("4a", bobConn, OK) + alice #: ("4a", bobConn, "ACK 7") #> ("4a", bobConn, OK) alice #: ("5", bobConn, "OFF") #> ("5", bobConn, OK) - bob #: ("17", aliceConn, "SEND F 9\nmessage 3") #> ("17", aliceConn, MID 9) - bob <# ("", aliceConn, MERR 9 (SMP AUTH)) + bob #: ("17", aliceConn, "SEND F 9\nmessage 3") #> ("17", aliceConn, MID 8) + bob <# ("", aliceConn, MERR 8 (SMP AUTH)) alice #: ("6", bobConn, "DEL") #> ("6", bobConn, OK) alice #:# "nothing else should be delivered to alice" @@ -200,10 +200,10 @@ testContactConnection _ alice bob tom = do alice <# ("", "bob", INFO "bob's connInfo 2") alice <# ("", "bob", CON) bob <# ("", "alice", CON) - alice #: ("3", "bob", "SEND F :hi") #> ("3", "bob", MID 5) - alice <# ("", "bob", SENT 5) + alice #: ("3", "bob", "SEND F :hi") #> ("3", "bob", MID 4) + alice <# ("", "bob", SENT 4) bob <#= \case ("", "alice", Msg "hi") -> True; _ -> False - bob #: ("13", "alice", "ACK 5") #> ("13", "alice", OK) + bob #: ("13", "alice", "ACK 4") #> ("13", "alice", OK) tom #: ("21", "alice", "JOIN " <> cReq' <> " 14\ntom's connInfo") #> ("21", "alice", OK) ("", "alice_contact", Right (REQ aInvId' "tom's connInfo")) <- (alice <#:) @@ -213,10 +213,10 @@ testContactConnection _ alice bob tom = do alice <# ("", "tom", INFO "tom's connInfo 2") alice <# ("", "tom", CON) tom <# ("", "alice", CON) - alice #: ("5", "tom", "SEND F :hi there") #> ("5", "tom", MID 5) - alice <# ("", "tom", SENT 5) + alice #: ("5", "tom", "SEND F :hi there") #> ("5", "tom", MID 4) + alice <# ("", "tom", SENT 4) tom <#= \case ("", "alice", Msg "hi there") -> True; _ -> False - tom #: ("23", "alice", "ACK 5") #> ("23", "alice", OK) + tom #: ("23", "alice", "ACK 4") #> ("23", "alice", OK) testContactConnRandomIds :: Transport c => TProxy c -> c -> c -> IO () testContactConnRandomIds _ alice bob = do @@ -236,10 +236,10 @@ testContactConnRandomIds _ alice bob = do alice <# ("", bobConn, CON) bob <# ("", aliceConn, CON) - alice #: ("3", bobConn, "SEND F :hi") #> ("3", bobConn, MID 5) - alice <# ("", bobConn, SENT 5) + alice #: ("3", bobConn, "SEND F :hi") #> ("3", bobConn, MID 4) + alice <# ("", bobConn, SENT 4) bob <#= \case ("", c, Msg "hi") -> c == aliceConn; _ -> False - bob #: ("13", aliceConn, "ACK 5") #> ("13", aliceConn, OK) + bob #: ("13", aliceConn, "ACK 4") #> ("13", aliceConn, OK) testRejectContactRequest :: Transport c => TProxy c -> c -> c -> IO () testRejectContactRequest _ alice bob = do @@ -256,20 +256,20 @@ testRejectContactRequest _ alice bob = do testSubscription :: Transport c => TProxy c -> c -> c -> c -> IO () testSubscription _ alice1 alice2 bob = do (alice1, "alice") `connect` (bob, "bob") - bob #: ("12", "alice", "SEND F 5\nhello") #> ("12", "alice", MID 5) - bob <# ("", "alice", SENT 5) + bob #: ("12", "alice", "SEND F 5\nhello") #> ("12", "alice", MID 4) + bob <# ("", "alice", SENT 4) alice1 <#= \case ("", "bob", Msg "hello") -> True; _ -> False - alice1 #: ("1", "bob", "ACK 5") #> ("1", "bob", OK) - bob #: ("13", "alice", "SEND F 11\nhello again") #> ("13", "alice", MID 6) - bob <# ("", "alice", SENT 6) + alice1 #: ("1", "bob", "ACK 4") #> ("1", "bob", OK) + bob #: ("13", "alice", "SEND F 11\nhello again") #> ("13", "alice", MID 5) + bob <# ("", "alice", SENT 5) alice1 <#= \case ("", "bob", Msg "hello again") -> True; _ -> False - alice1 #: ("2", "bob", "ACK 6") #> ("2", "bob", OK) + alice1 #: ("2", "bob", "ACK 5") #> ("2", "bob", OK) alice2 #: ("21", "bob", "SUB") #> ("21", "bob", OK) alice1 <# ("", "bob", END) - bob #: ("14", "alice", "SEND F 2\nhi") #> ("14", "alice", MID 7) - bob <# ("", "alice", SENT 7) + bob #: ("14", "alice", "SEND F 2\nhi") #> ("14", "alice", MID 6) + bob <# ("", "alice", SENT 6) alice2 <#= \case ("", "bob", Msg "hi") -> True; _ -> False - alice2 #: ("22", "bob", "ACK 7") #> ("22", "bob", OK) + alice2 #: ("22", "bob", "ACK 6") #> ("22", "bob", OK) alice1 #:# "nothing else should be delivered to alice1" testSubscrNotification :: Transport c => TProxy c -> (ThreadId, ThreadId) -> c -> IO () @@ -285,23 +285,23 @@ testMsgDeliveryServerRestart :: Transport c => TProxy c -> c -> c -> IO () testMsgDeliveryServerRestart t alice bob = do withServer $ do connect (alice, "alice") (bob, "bob") - bob #: ("1", "alice", "SEND F 2\nhi") #> ("1", "alice", MID 5) - bob <# ("", "alice", SENT 5) + bob #: ("1", "alice", "SEND F 2\nhi") #> ("1", "alice", MID 4) + bob <# ("", "alice", SENT 4) alice <#= \case ("", "bob", Msg "hi") -> True; _ -> False - alice #: ("11", "bob", "ACK 5") #> ("11", "bob", OK) + alice #: ("11", "bob", "ACK 4") #> ("11", "bob", OK) alice #:# "nothing else delivered before the server is killed" let server = (SMPServer "localhost" testPort2 testKeyHash) alice <# ("", "", DOWN server ["bob"]) - bob #: ("2", "alice", "SEND F 11\nhello again") #> ("2", "alice", MID 6) + bob #: ("2", "alice", "SEND F 11\nhello again") #> ("2", "alice", MID 5) bob #:# "nothing else delivered before the server is restarted" alice #:# "nothing else delivered before the server is restarted" withServer $ do - bob <# ("", "alice", SENT 6) + bob <# ("", "alice", SENT 5) alice <# ("", "", UP server ["bob"]) alice <#= \case ("", "bob", Msg "hello again") -> True; _ -> False - alice #: ("12", "bob", "ACK 6") #> ("12", "bob", OK) + alice #: ("12", "bob", "ACK 5") #> ("12", "bob", OK) removeFile testStoreLogFile where @@ -316,7 +316,7 @@ testServerConnectionAfterError t _ = do bob <# ("", "", DOWN server ["alice"]) alice <# ("", "", DOWN server ["bob"]) - alice #: ("1", "bob", "SEND F 5\nhello") #> ("1", "bob", MID 5) + alice #: ("1", "bob", "SEND F 5\nhello") #> ("1", "bob", MID 4) alice #:# "nothing else delivered before the server is restarted" bob #:# "nothing else delivered before the server is restarted" @@ -325,13 +325,13 @@ testServerConnectionAfterError t _ = do bob #: ("1", "alice", "SUB") #> ("1", "alice", ERR (BROKER NETWORK)) alice #: ("1", "bob", "SUB") #> ("1", "bob", ERR (BROKER NETWORK)) withServer $ do - alice <# ("", "bob", SENT 5) + alice <# ("", "bob", SENT 4) bob <# ("", "", UP server ["alice"]) bob <#= \case ("", "alice", Msg "hello") -> True; _ -> False - bob #: ("2", "alice", "ACK 5") #> ("2", "alice", OK) + bob #: ("2", "alice", "ACK 4") #> ("2", "alice", OK) alice <# ("", "", UP server ["bob"]) - alice #: ("1", "bob", "SEND F 11\nhello again") #> ("1", "bob", MID 6) - alice <# ("", "bob", SENT 6) + alice #: ("1", "bob", "SEND F 11\nhello again") #> ("1", "bob", MID 5) + alice <# ("", "bob", SENT 5) bob <#= \case ("", "alice", Msg "hello again") -> True; _ -> False removeFile testStoreLogFile @@ -351,14 +351,14 @@ testMsgDeliveryAgentRestart t bob = do withAgent $ \alice -> do withServer $ do connect (bob, "bob") (alice, "alice") - alice #: ("1", "bob", "SEND F 5\nhello") #> ("1", "bob", MID 5) - alice <# ("", "bob", SENT 5) + alice #: ("1", "bob", "SEND F 5\nhello") #> ("1", "bob", MID 4) + alice <# ("", "bob", SENT 4) bob <#= \case ("", "alice", Msg "hello") -> True; _ -> False - bob #: ("11", "alice", "ACK 5") #> ("11", "alice", OK) + bob #: ("11", "alice", "ACK 4") #> ("11", "alice", OK) bob #:# "nothing else delivered before the server is down" bob <# ("", "", DOWN server ["alice"]) - alice #: ("2", "bob", "SEND F 11\nhello again") #> ("2", "bob", MID 6) + alice #: ("2", "bob", "SEND F 11\nhello again") #> ("2", "bob", MID 5) alice #:# "nothing else delivered before the server is restarted" bob #:# "nothing else delivered before the server is restarted" @@ -368,11 +368,11 @@ testMsgDeliveryAgentRestart t bob = do alice <#= \case (corrId, "bob", cmd) -> (corrId == "3" && cmd == OK) - || (corrId == "" && cmd == SENT 6) + || (corrId == "" && cmd == SENT 5) _ -> False bob <# ("", "", UP server ["alice"]) bob <#= \case ("", "alice", Msg "hello again") -> True; _ -> False - bob #: ("12", "alice", "ACK 6") #> ("12", "alice", OK) + bob #: ("12", "alice", "ACK 5") #> ("12", "alice", OK) removeFile testStoreLogFile removeFile testDB @@ -400,11 +400,11 @@ testConcurrentMsgDelivery _ alice bob = do -- alice <# ("", "bob", SENT 1) -- bob <#= \case ("", "alice", Msg "hello") -> True; _ -> False -- bob #: ("12", "alice", "ACK 1") #> ("12", "alice", OK) - bob #: ("14", "alice", "SEND F 9\nhello too") #> ("14", "alice", MID 6) - bob <# ("", "alice", SENT 6) + bob #: ("14", "alice", "SEND F 9\nhello too") #> ("14", "alice", MID 5) + bob <# ("", "alice", SENT 5) -- if delivery is blocked it won't go further alice <#= \case ("", "bob", Msg "hello too") -> True; _ -> False - alice #: ("3", "bob", "ACK 6") #> ("3", "bob", OK) + alice #: ("3", "bob", "ACK 5") #> ("3", "bob", OK) testMsgDeliveryQuotaExceeded :: Transport c => TProxy c -> c -> c -> IO () testMsgDeliveryQuotaExceeded _ alice bob = do @@ -417,9 +417,9 @@ testMsgDeliveryQuotaExceeded _ alice bob = do alice <#= \case ("", "bob", SENT m) -> m == mId; _ -> False (_, "bob", Right (MID _)) <- alice #: ("5", "bob", "SEND F :over quota") - alice #: ("1", "bob2", "SEND F :hello") #> ("1", "bob2", MID 5) + alice #: ("1", "bob2", "SEND F :hello") #> ("1", "bob2", MID 4) -- if delivery is blocked it won't go further - alice <# ("", "bob2", SENT 5) + alice <# ("", "bob2", SENT 4) connect :: forall c. Transport c => (c, ByteString) -> (c, ByteString) -> IO () connect (h1, name1) (h2, name2) = do diff --git a/tests/AgentTests/ConnectionRequestTests.hs b/tests/AgentTests/ConnectionRequestTests.hs index 1b1598413..af4f749bc 100644 --- a/tests/AgentTests/ConnectionRequestTests.hs +++ b/tests/AgentTests/ConnectionRequestTests.hs @@ -48,7 +48,7 @@ connReqData :: ConnReqUriData connReqData = ConnReqUriData { crScheme = simplexChat, - crAgentVRange = smpAgentVRange, + crAgentVRange = mkVersionRange 1 1, crSmpQueues = [queue] } @@ -70,7 +70,7 @@ connectionRequest12 :: AConnectionRequestUri connectionRequest12 = ACR SCMInvitation $ CRInvitationUri - connReqData {crAgentVRange = mkVersionRange 1 2, crSmpQueues = [queue, queue]} + connReqData {crAgentVRange = supportedSMPAgentVRange, crSmpQueues = [queue, queue]} testE2ERatchetParams13 connectionRequestTests :: Spec diff --git a/tests/AgentTests/FunctionalAPITests.hs b/tests/AgentTests/FunctionalAPITests.hs index 43925c830..35ac8b229 100644 --- a/tests/AgentTests/FunctionalAPITests.hs +++ b/tests/AgentTests/FunctionalAPITests.hs @@ -22,6 +22,7 @@ import qualified Simplex.Messaging.Protocol as SMP import Simplex.Messaging.Server.Env.STM (ServerConfig (..)) import Simplex.Messaging.Server.Expiration import Simplex.Messaging.Transport (ATransport (..)) +import Simplex.Messaging.Version import Test.Hspec import UnliftIO @@ -37,11 +38,31 @@ get c = atomically (readTBQueue $ subQ c) pattern Msg :: MsgBody -> ACommand 'Agent pattern Msg msgBody <- MSG MsgMeta {integrity = MsgOk} _ msgBody +agentCfgV1 :: AgentConfig +agentCfgV1 = agentCfg {smpAgentVersion = 1, smpAgentVRange = mkVersionRange 1 1} + functionalAPITests :: ATransport -> Spec functionalAPITests t = do describe "Establishing duplex connection" $ it "should connect via one server using SMP agent clients" $ withSmpServer t testAgentClient + describe "Duplex connection between agent versions 1 and 2" $ do + it "should connect agent v1 to v1" $ + withSmpServer t testAgentClientV1toV1 + it "should connect agent v1 to v2" $ + withSmpServer t testAgentClientV1toV2 + it "should connect agent v2 to v1" $ + withSmpServer t testAgentClientV2toV1 + describe "Establish duplex connection via contact address" $ + it "should connect via one server using SMP agent clients" $ + withSmpServer t testAgentClientContact + describe "Duplex connection via contact address between agent versions 1 and 2" $ do + it "should connect agent v1 to v1" $ + withSmpServer t testAgentClientContactV1toV1 + it "should connect agent v1 to v2" $ + withSmpServer t testAgentClientContactV1toV2 + it "should connect agent v2 to v1" $ + withSmpServer t testAgentClientContactV2toV1 describe "Establishing connection asynchronously" $ do it "should connect with initiating client going offline" $ withSmpServer t testAsyncInitiatingOffline @@ -63,6 +84,52 @@ testAgentClient :: IO () testAgentClient = do alice <- getSMPAgentClient agentCfg initAgentServers bob <- getSMPAgentClient agentCfg {dbFile = testDB2} initAgentServers + runAgentClientTest alice bob 3 + +testAgentClientV1toV1 :: IO () +testAgentClientV1toV1 = do + alice <- getSMPAgentClient agentCfgV1 initAgentServers + bob <- getSMPAgentClient agentCfgV1 {dbFile = testDB2} initAgentServers + runAgentClientTest alice bob 4 + +testAgentClientV1toV2 :: IO () +testAgentClientV1toV2 = do + alice <- getSMPAgentClient agentCfgV1 initAgentServers + bob <- getSMPAgentClient agentCfg {dbFile = testDB2} initAgentServers + runAgentClientTest alice bob 4 + +testAgentClientV2toV1 :: IO () +testAgentClientV2toV1 = do + alice <- getSMPAgentClient agentCfg initAgentServers + bob <- getSMPAgentClient agentCfgV1 {dbFile = testDB2} initAgentServers + runAgentClientTest alice bob 4 + +testAgentClientContact :: IO () +testAgentClientContact = do + alice <- getSMPAgentClient agentCfg initAgentServers + bob <- getSMPAgentClient agentCfg {dbFile = testDB2} initAgentServers + runAgentClientContactTest alice bob 3 + +testAgentClientContactV1toV1 :: IO () +testAgentClientContactV1toV1 = do + alice <- getSMPAgentClient agentCfgV1 initAgentServers + bob <- getSMPAgentClient agentCfgV1 {dbFile = testDB2} initAgentServers + runAgentClientContactTest alice bob 4 + +testAgentClientContactV1toV2 :: IO () +testAgentClientContactV1toV2 = do + alice <- getSMPAgentClient agentCfg initAgentServers + bob <- getSMPAgentClient agentCfgV1 {dbFile = testDB2} initAgentServers + runAgentClientContactTest alice bob 4 + +testAgentClientContactV2toV1 :: IO () +testAgentClientContactV2toV1 = do + alice <- getSMPAgentClient agentCfgV1 initAgentServers + bob <- getSMPAgentClient agentCfg {dbFile = testDB2} initAgentServers + runAgentClientContactTest alice bob 4 + +runAgentClientTest :: AgentClient -> AgentClient -> AgentMsgId -> IO () +runAgentClientTest alice bob baseId = do Right () <- runExceptT $ do (bobId, qInfo) <- createConnection alice SCMInvitation aliceId <- joinConnection bob qInfo "bob's connInfo" @@ -71,37 +138,77 @@ testAgentClient = do get alice ##> ("", bobId, CON) get bob ##> ("", aliceId, INFO "alice's connInfo") get bob ##> ("", aliceId, CON) - -- message IDs 1 to 4 get assigned to control messages, so first MSG is assigned ID 5 - 5 <- sendMessage alice bobId SMP.noMsgFlags "hello" - get alice ##> ("", bobId, SENT 5) - 6 <- sendMessage alice bobId SMP.noMsgFlags "how are you?" - get alice ##> ("", bobId, SENT 6) + -- message IDs 1 to 3 (or 1 to 4 in v1) get assigned to control messages, so first MSG is assigned ID 4 + 1 <- msgId <$> sendMessage alice bobId SMP.noMsgFlags "hello" + get alice ##> ("", bobId, SENT $ baseId + 1) + 2 <- msgId <$> sendMessage alice bobId SMP.noMsgFlags "how are you?" + get alice ##> ("", bobId, SENT $ baseId + 2) get bob =##> \case ("", c, Msg "hello") -> c == aliceId; _ -> False - ackMessage bob aliceId 5 + ackMessage bob aliceId $ baseId + 1 get bob =##> \case ("", c, Msg "how are you?") -> c == aliceId; _ -> False - ackMessage bob aliceId 6 - 7 <- sendMessage bob aliceId SMP.noMsgFlags "hello too" - get bob ##> ("", aliceId, SENT 7) - 8 <- sendMessage bob aliceId SMP.noMsgFlags "message 1" - get bob ##> ("", aliceId, SENT 8) + ackMessage bob aliceId $ baseId + 2 + 3 <- msgId <$> sendMessage bob aliceId SMP.noMsgFlags "hello too" + get bob ##> ("", aliceId, SENT $ baseId + 3) + 4 <- msgId <$> sendMessage bob aliceId SMP.noMsgFlags "message 1" + get bob ##> ("", aliceId, SENT $ baseId + 4) get alice =##> \case ("", c, Msg "hello too") -> c == bobId; _ -> False - ackMessage alice bobId 7 + ackMessage alice bobId $ baseId + 3 get alice =##> \case ("", c, Msg "message 1") -> c == bobId; _ -> False - ackMessage alice bobId 8 + ackMessage alice bobId $ baseId + 4 suspendConnection alice bobId - 9 <- sendMessage bob aliceId SMP.noMsgFlags "message 2" - get bob ##> ("", aliceId, MERR 9 (SMP AUTH)) + 5 <- msgId <$> sendMessage bob aliceId SMP.noMsgFlags "message 2" + get bob ##> ("", aliceId, MERR (baseId + 5) (SMP AUTH)) deleteConnection alice bobId liftIO $ noMessages alice "nothing else should be delivered to alice" pure () where - noMessages :: AgentClient -> String -> Expectation - noMessages c err = tryGet `shouldReturn` () - where - tryGet = - 10000 `timeout` get c >>= \case - Just _ -> error err - _ -> return () + msgId = subtract baseId + +runAgentClientContactTest :: AgentClient -> AgentClient -> AgentMsgId -> IO () +runAgentClientContactTest alice bob baseId = do + Right () <- runExceptT $ do + (_, qInfo) <- createConnection alice SCMContact + aliceId <- joinConnection bob qInfo "bob's connInfo" + ("", _, REQ invId "bob's connInfo") <- get alice + bobId <- acceptContact alice invId "alice's connInfo" + ("", _, CONF confId "alice's connInfo") <- get bob + allowConnection bob aliceId confId "bob's connInfo" + get alice ##> ("", bobId, INFO "bob's connInfo") + get alice ##> ("", bobId, CON) + get bob ##> ("", aliceId, CON) + -- message IDs 1 to 3 (or 1 to 4 in v1) get assigned to control messages, so first MSG is assigned ID 4 + 1 <- msgId <$> sendMessage alice bobId SMP.noMsgFlags "hello" + get alice ##> ("", bobId, SENT $ baseId + 1) + 2 <- msgId <$> sendMessage alice bobId SMP.noMsgFlags "how are you?" + get alice ##> ("", bobId, SENT $ baseId + 2) + get bob =##> \case ("", c, Msg "hello") -> c == aliceId; _ -> False + ackMessage bob aliceId $ baseId + 1 + get bob =##> \case ("", c, Msg "how are you?") -> c == aliceId; _ -> False + ackMessage bob aliceId $ baseId + 2 + 3 <- msgId <$> sendMessage bob aliceId SMP.noMsgFlags "hello too" + get bob ##> ("", aliceId, SENT $ baseId + 3) + 4 <- msgId <$> sendMessage bob aliceId SMP.noMsgFlags "message 1" + get bob ##> ("", aliceId, SENT $ baseId + 4) + get alice =##> \case ("", c, Msg "hello too") -> c == bobId; _ -> False + ackMessage alice bobId $ baseId + 3 + get alice =##> \case ("", c, Msg "message 1") -> c == bobId; _ -> False + ackMessage alice bobId $ baseId + 4 + suspendConnection alice bobId + 5 <- msgId <$> sendMessage bob aliceId SMP.noMsgFlags "message 2" + get bob ##> ("", aliceId, MERR (baseId + 5) (SMP AUTH)) + deleteConnection alice bobId + liftIO $ noMessages alice "nothing else should be delivered to alice" + pure () + where + msgId = subtract baseId + +noMessages :: AgentClient -> String -> Expectation +noMessages c err = tryGet `shouldReturn` () + where + tryGet = + 10000 `timeout` get c >>= \case + Just _ -> error err + _ -> return () testAsyncInitiatingOffline :: IO () testAsyncInitiatingOffline = do @@ -189,7 +296,8 @@ testAsyncServerOffline t = do testAsyncHelloTimeout :: IO () testAsyncHelloTimeout = do - alice <- getSMPAgentClient agentCfg initAgentServers + -- this test would only work if any of the agent is v1, there is no HELLO timeout in v2 + alice <- getSMPAgentClient agentCfgV1 initAgentServers bob <- getSMPAgentClient agentCfg {dbFile = testDB2, helloTimeout = 1} initAgentServers Right () <- runExceptT $ do (_, cReq) <- createConnection alice SCMInvitation @@ -238,11 +346,11 @@ testActiveClientNotDisconnected t = do exchangeGreetings :: AgentClient -> ConnId -> AgentClient -> ConnId -> ExceptT AgentErrorType IO () exchangeGreetings alice bobId bob aliceId = do - 5 <- sendMessage alice bobId SMP.noMsgFlags "hello" - get alice ##> ("", bobId, SENT 5) + 4 <- sendMessage alice bobId SMP.noMsgFlags "hello" + get alice ##> ("", bobId, SENT 4) get bob =##> \case ("", c, Msg "hello") -> c == aliceId; _ -> False - ackMessage bob aliceId 5 - 6 <- sendMessage bob aliceId SMP.noMsgFlags "hello too" - get bob ##> ("", aliceId, SENT 6) + ackMessage bob aliceId 4 + 5 <- sendMessage bob aliceId SMP.noMsgFlags "hello too" + get bob ##> ("", aliceId, SENT 5) get alice =##> \case ("", c, Msg "hello too") -> c == bobId; _ -> False - ackMessage alice bobId 6 + ackMessage alice bobId 5 diff --git a/tests/AgentTests/SQLiteTests.hs b/tests/AgentTests/SQLiteTests.hs index 4dd9f89bf..af083747c 100644 --- a/tests/AgentTests/SQLiteTests.hs +++ b/tests/AgentTests/SQLiteTests.hs @@ -145,7 +145,7 @@ testForeignKeysEnabled = `shouldThrow` (\e -> DB.sqlError e == DB.ErrorConstraint) cData1 :: ConnData -cData1 = ConnData {connId = "conn1"} +cData1 = ConnData {connId = "conn1", connAgentVersion = 1, duplexHandshake = Nothing} testPrivateSignKey :: C.APrivateSignKey testPrivateSignKey = C.APrivateSignKey C.SEd25519 "MC4CAQAwBQYDK2VwBCIEIDfEfevydXXfKajz3sRkcQ7RPvfWUPoq6pu1TYHV1DEe" diff --git a/tests/NtfClient.hs b/tests/NtfClient.hs index c3d406cc4..4662e921f 100644 --- a/tests/NtfClient.hs +++ b/tests/NtfClient.hs @@ -65,7 +65,7 @@ testKeyHash = "LcJUMfVhwD8yxjAiSaDzzGF3-kLG4Uh0Fl_ZIjrRwjI=" testNtfClient :: (Transport c, MonadUnliftIO m) => (THandle c -> m a) -> m a testNtfClient client = runTransportClient testHost ntfTestPort (Just testKeyHash) (Just defaultKeepAliveOpts) $ \h -> - liftIO (runExceptT $ ntfClientHandshake h testKeyHash) >>= \case + liftIO (runExceptT $ ntfClientHandshake h testKeyHash supportedNTFServerVRange) >>= \case Right th -> client th Left e -> error $ show e diff --git a/tests/SMPClient.hs b/tests/SMPClient.hs index 5e5f7e611..5b7e83306 100644 --- a/tests/SMPClient.hs +++ b/tests/SMPClient.hs @@ -46,7 +46,7 @@ testStoreLogFile = "tests/tmp/smp-server-store.log" testSMPClient :: (Transport c, MonadUnliftIO m) => (THandle c -> m a) -> m a testSMPClient client = runTransportClient testHost testPort (Just testKeyHash) (Just defaultKeepAliveOpts) $ \h -> - liftIO (runExceptT $ smpClientHandshake h testKeyHash) >>= \case + liftIO (runExceptT $ smpClientHandshake h testKeyHash supportedSMPServerVRange) >>= \case Right th -> client th Left e -> error $ show e @@ -67,7 +67,8 @@ cfg = logStatsStartTime = 0, caCertificateFile = "tests/fixtures/ca.crt", privateKeyFile = "tests/fixtures/server.key", - certificateFile = "tests/fixtures/server.crt" + certificateFile = "tests/fixtures/server.crt", + smpServerVRange = supportedSMPServerVRange } withSmpServerStoreLogOn :: (MonadUnliftIO m, MonadRandom m) => ATransport -> ServiceName -> (ThreadId -> m a) -> m a From 9c12b4edc0437415d29b898f70d5419e6549df30 Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Fri, 10 Jun 2022 16:35:06 +0400 Subject: [PATCH 050/131] ntf: agent spike, NtfSupervisor (#392) Co-authored-by: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> --- simplexmq.cabal | 1 + src/Simplex/Messaging/Agent.hs | 125 +++++++++------ src/Simplex/Messaging/Agent/Client.hs | 28 ++-- src/Simplex/Messaging/Agent/Env/SQLite.hs | 77 ++++++++- .../Messaging/Agent/NtfSubSupervisor.hs | 149 ++++++++++++++++++ src/Simplex/Messaging/Agent/Store.hs | 20 +++ src/Simplex/Messaging/Agent/Store/SQLite.hs | 26 ++- .../M20220404_ntf_subscriptions_draft.hs | 19 ++- src/Simplex/Messaging/Notifications/Client.hs | 66 +++++++- .../Messaging/Notifications/Protocol.hs | 6 +- 10 files changed, 440 insertions(+), 77 deletions(-) create mode 100644 src/Simplex/Messaging/Agent/NtfSubSupervisor.hs diff --git a/simplexmq.cabal b/simplexmq.cabal index f5abad638..69b6e3bd1 100644 --- a/simplexmq.cabal +++ b/simplexmq.cabal @@ -37,6 +37,7 @@ library Simplex.Messaging.Agent Simplex.Messaging.Agent.Client Simplex.Messaging.Agent.Env.SQLite + Simplex.Messaging.Agent.NtfSubSupervisor Simplex.Messaging.Agent.Protocol Simplex.Messaging.Agent.QueryString Simplex.Messaging.Agent.RetryInterval diff --git a/src/Simplex/Messaging/Agent.hs b/src/Simplex/Messaging/Agent.hs index d8eac56a6..6758654e0 100644 --- a/src/Simplex/Messaging/Agent.hs +++ b/src/Simplex/Messaging/Agent.hs @@ -78,9 +78,9 @@ import qualified Data.Text as T import Data.Time.Clock import Data.Time.Clock.System (systemToUTCTime) import Data.Word (Word16) -import Database.SQLite.Simple (SQLError) import Simplex.Messaging.Agent.Client import Simplex.Messaging.Agent.Env.SQLite +import Simplex.Messaging.Agent.NtfSubSupervisor import Simplex.Messaging.Agent.Protocol import Simplex.Messaging.Agent.RetryInterval import Simplex.Messaging.Agent.Store @@ -99,6 +99,7 @@ import Simplex.Messaging.Util (bshow, liftError, tryError, unlessM, ($>>=)) import Simplex.Messaging.Version import System.Random (randomR) import UnliftIO.Async (async, race_) +import UnliftIO.Concurrent (forkFinally) import qualified UnliftIO.Exception as E import UnliftIO.STM @@ -108,11 +109,14 @@ getSMPAgentClient cfg initServers = newSMPAgentEnv cfg >>= runReaderT runAgent where runAgent = do c <- getAgentClient initServers - action <- async $ subscriber c `E.finally` disconnectAgentClient c - pure c {smpSubscriber = action} + void $ race_ (subscriber c) (runNtfSupervisor c) `forkFinally` const (disconnectAgentClient c) + pure c disconnectAgentClient :: MonadUnliftIO m => AgentClient -> m () -disconnectAgentClient c = closeAgentClient c >> logConnection c False +disconnectAgentClient c@AgentClient {agentEnv = Env {ntfSupervisor = ns}} = do + closeAgentClient c + liftIO $ closeNtfSupervisor ns + logConnection c False resumeAgentClient :: MonadIO m => AgentClient -> m () resumeAgentClient c = atomically $ writeTVar (active c) True @@ -214,30 +218,6 @@ client c@AgentClient {rcvQ, subQ} = forever $ do Left e -> (corrId, connId, ERR e) Right (connId', resp) -> (corrId, connId', resp) -withStore :: AgentMonad m => (forall m'. AgentStoreMonad m' => SQLiteStore -> m' a) -> m a -withStore action = do - st <- asks store - runExceptT (action st `E.catch` handleInternal) >>= \case - Right c -> return c - Left e -> throwError $ storeError e - where - -- TODO when parsing exception happens in store, the agent hangs; - -- changing SQLError to SomeException does not help - handleInternal :: (MonadError StoreError m') => SQLError -> m' a - handleInternal e = throwError . SEInternal $ bshow e - storeError :: StoreError -> AgentErrorType - storeError = \case - SEConnNotFound -> CONN NOT_FOUND - SEConnDuplicate -> CONN DUPLICATE - SEBadConnType CRcv -> CONN SIMPLEX - SEBadConnType CSnd -> CONN SIMPLEX - SEInvitationNotFound -> CMD PROHIBITED - -- this error is never reported as store error, - -- it is used to wrap agent operations when "transaction-like" store access is needed - -- NOTE: network IO should NOT be used inside AgentStoreMonad - SEAgentError e -> e - e -> INTERNAL $ show e - -- | execute any SMP agent command processCommand :: forall m. AgentMonad m => AgentClient -> (ConnId, ACommand 'Client) -> m (ConnId, ACommand 'Agent) processCommand c (connId, cmd) = case cmd of @@ -255,12 +235,14 @@ processCommand c (connId, cmd) = case cmd of newConn :: AgentMonad m => AgentClient -> ConnId -> SConnectionMode c -> m (ConnId, ConnectionRequestUri c) newConn c connId cMode = do srv <- getSMPServer c - (rq, qUri) <- newRcvQueue c srv + (rq, qUri) <- newRcvQueueNtf c srv g <- asks idsDrg agentVersion <- asks $ smpAgentVersion . config let cData = ConnData {connId, connAgentVersion = agentVersion, duplexHandshake = Nothing} -- connection mode is determined by the accepting agent connId' <- withStore $ \st -> createRcvConn st g cData rq cMode addSubscription c rq connId' + ns <- asks ntfSupervisor + atomically $ sendNtfSubCommand ns (rq, NSCCreate) aVRange <- asks $ smpAgentVRange . config let crData = ConnReqUriData simplexChat aVRange [qUri] case cMode of @@ -315,7 +297,7 @@ joinConn c connId (CRContactUri (ConnReqUriData _ agentVRange (qUri :| _))) cInf createReplyQueue :: AgentMonad m => AgentClient -> ConnId -> m SMPQueueInfo createReplyQueue c connId = do srv <- getSMPServer c - (rq, qUri) <- newRcvQueue c srv + (rq, qUri) <- newRcvQueueNtf c srv -- TODO reply queue version should be the same as send queue, ignoring it in v1 let qInfo = toVersionT qUri SMP.smpClientVersion addSubscription c rq connId @@ -361,15 +343,21 @@ subscribeConnection' c connId = withStore (`getConn` connId) >>= \case SomeConn _ (DuplexConnection cData rq sq) -> do resumeMsgDelivery c cData sq - subscribeQueue c rq connId + subscribe rq SomeConn _ (SndConnection cData sq) -> do resumeMsgDelivery c cData sq case status (sq :: SndQueue) of Confirmed -> pure () Active -> throwError $ CONN SIMPLEX _ -> throwError $ INTERNAL "unexpected queue status" - SomeConn _ (RcvConnection _ rq) -> subscribeQueue c rq connId - SomeConn _ (ContactConnection _ rq) -> subscribeQueue c rq connId + SomeConn _ (RcvConnection _ rq) -> subscribe rq + SomeConn _ (ContactConnection _ rq) -> subscribe rq + where + subscribe :: RcvQueue -> m () + subscribe rq = do + subscribeQueue c rq connId + ns <- asks ntfSupervisor + atomically $ sendNtfSubCommand ns (rq, NSCCreate) resubscribeConnection' :: forall m. AgentMonad m => AgentClient -> ConnId -> m () resubscribeConnection' c connId = @@ -562,7 +550,10 @@ deleteConnection' c connId = delete :: RcvQueue -> m () delete rq = do deleteQueue c rq - atomically $ removeSubscription c connId + ns <- asks ntfSupervisor + atomically $ do + removeSubscription c connId + sendNtfSubCommand ns (rq, NSCDelete) withStore (`deleteConn` connId) -- | Change servers to be used for creating new queues, in Reader monad @@ -575,6 +566,7 @@ registerNtfToken' c deviceToken = withStore (`getDeviceNtfToken` deviceToken) >>= \case (Just tkn@NtfToken {ntfTokenId, ntfTknStatus, ntfTknAction}, prevTokens) -> do mapM_ (deleteToken_ c) prevTokens + ns <- asks ntfSupervisor case (ntfTokenId, ntfTknAction) of (Nothing, Just NTARegister) -> registerToken tkn $> NTRegistered -- TODO minimal time before repeat registration @@ -583,11 +575,17 @@ registerNtfToken' c deviceToken = t tkn (NTActive, Just NTACheck) $ agentNtfVerifyToken c tknId tkn code (Just tknId, Just (NTACron interval)) -> t tkn (cronSuccess interval) $ agentNtfEnableCron c tknId tkn interval - (Just _tknId, Just NTACheck) -> pure ntfTknStatus -- TODO - -- agentNtfCheckToken c tknId tkn >>= \case + (Just _tknId, Just NTACheck) -> do + if ntfTknStatus == NTActive + then initializeNtfSubQ c tkn + else atomically $ nsUpdateToken ns tkn + pure ntfTknStatus -- TODO + -- agentNtfCheckToken c tknId tkn >>= \case (Just tknId, Just NTADelete) -> do agentNtfDeleteToken c tknId tkn - withStore $ \st -> removeNtfToken st tkn $> NTExpired + withStore $ \st -> removeNtfToken st tkn + atomically $ nsRemoveNtfToken ns + pure NTExpired _ -> pure ntfTknStatus _ -> getNtfServer c >>= \case @@ -608,6 +606,8 @@ registerNtfToken' c deviceToken = (tknId, srvPubDhKey) <- agentNtfRegisterToken c tkn ntfPubKey pubDhKey let dhSecret = C.dh' srvPubDhKey privDhKey withStore $ \st -> updateNtfTokenRegistration st tkn tknId dhSecret + ns <- asks ntfSupervisor + atomically $ nsUpdateToken ns tkn -- TODO decrypt verification code verifyNtfToken' :: AgentMonad m => AgentClient -> DeviceToken -> ByteString -> C.CbNonce -> m () @@ -615,7 +615,7 @@ verifyNtfToken' c deviceToken code nonce = withStore (`getDeviceNtfToken` deviceToken) >>= \case (Just tkn@NtfToken {ntfTokenId = Just tknId, ntfDhSecret = Just dhSecret}, _) -> do code' <- liftEither . bimap cryptoError NtfRegCode $ C.cbDecrypt dhSecret nonce code - void . withToken c tkn (Just (NTConfirmed, NTAVerify code')) (NTActive, Just NTACheck) $ + void . withToken c tkn (Just (NTConfirmed, NTAVerify code')) (NTActive, Just NTACheck) $ do agentNtfVerifyToken c tknId tkn code' _ -> throwError $ CMD PROHIBITED @@ -647,26 +647,53 @@ deleteNtfToken' c deviceToken = deleteToken_ :: AgentMonad m => AgentClient -> NtfToken -> m () deleteToken_ c tkn@NtfToken {ntfTokenId, ntfTknStatus} = do + ns <- asks ntfSupervisor forM_ ntfTokenId $ \tknId -> do - withStore $ \st -> updateNtfToken st tkn ntfTknStatus (Just NTADelete) + let ntfTknAction = Just NTADelete + withStore $ \st -> updateNtfToken st tkn ntfTknStatus ntfTknAction + atomically $ nsUpdateToken ns tkn {ntfTknStatus, ntfTknAction} agentNtfDeleteToken c tknId tkn `catchError` \case NTF AUTH -> pure () e -> throwError e withStore $ \st -> removeNtfToken st tkn + atomically $ nsRemoveNtfToken ns withToken :: AgentMonad m => AgentClient -> NtfToken -> Maybe (NtfTknStatus, NtfTknAction) -> (NtfTknStatus, Maybe NtfTknAction) -> m a -> m NtfTknStatus withToken c tkn@NtfToken {deviceToken} from_ (toStatus, toAction_) f = do - forM_ from_ $ \(status, action) -> withStore $ \st -> updateNtfToken st tkn status (Just action) + ns <- asks ntfSupervisor + forM_ from_ $ \(status, action) -> do + withStore $ \st -> updateNtfToken st tkn status (Just action) + atomically $ nsUpdateToken ns tkn {ntfTknStatus = status, ntfTknAction = Just action} tryError f >>= \case Right _ -> do withStore $ \st -> updateNtfToken st tkn toStatus toAction_ + let updatedToken = tkn {ntfTknStatus = toStatus, ntfTknAction = toAction_} + if toStatus == NTActive + then initializeNtfSubQ c updatedToken + else atomically $ nsUpdateToken ns updatedToken pure toStatus Left e@(NTF AUTH) -> do withStore $ \st -> removeNtfToken st tkn + atomically $ nsRemoveNtfToken ns void $ registerNtfToken' c deviceToken throwError e Left e -> throwError e +initializeNtfSubQ :: AgentMonad m => AgentClient -> NtfToken -> m () +initializeNtfSubQ c tkn = do + ns <- asks ntfSupervisor + connIds <- atomically $ do + nsUpdateToken ns tkn + getSubscriptions c + forM_ connIds $ \connId -> do + rq <- withStore $ \st -> getRcvQueue st connId + atomically $ sendNtfSubCommand ns (rq, NSCCreate) + +-- TODO +-- There should probably be another function to cancel all subscriptions that would flush the queue first, +-- so that superwisor stops processing pending commands? +-- It is an optimization, but I am thinking how it would behave if a user were to flip on/off quickly several times. + setNtfServers' :: AgentMonad m => AgentClient -> [NtfServer] -> m () setNtfServers' c servers = do atomically $ writeTVar (ntfServers c) servers @@ -681,17 +708,6 @@ getSMPServer c = do atomically . stateTVar gen $ first (servers L.!!) . randomR (0, L.length servers - 1) -getNtfServer :: AgentMonad m => AgentClient -> m (Maybe NtfServer) -getNtfServer c = do - ntfServers <- readTVarIO $ ntfServers c - case ntfServers of - [] -> pure Nothing - [srv] -> pure $ Just srv - servers -> do - gen <- asks randomServer - atomically . stateTVar gen $ - first (Just . (servers !!)) . randomR (0, length servers - 1) - subscriber :: (MonadUnliftIO m, MonadReader Env m) => AgentClient -> m () subscriber c@AgentClient {msgQ} = forever $ do t <- atomically $ readTBQueue msgQ @@ -964,6 +980,13 @@ agentRatchetDecrypt st connId encAgentMsg = do updateRatchet st connId rc' skippedDiff liftEither $ first (SEAgentError . cryptoError) agentMsgBody_ +newRcvQueueNtf :: AgentMonad m => AgentClient -> SMPServer -> m (RcvQueue, SMPQueueUri) +newRcvQueueNtf c srv = do + (rq, qUri) <- newRcvQueue c srv + ns <- asks ntfSupervisor + atomically $ sendNtfSubCommand ns (rq, NSCCreate) + pure (rq, qUri) + newSndQueue :: (MonadUnliftIO m, MonadReader Env m) => Compatible SMPQueueInfo -> m SndQueue newSndQueue qInfo = asks (cmdSignAlg . config) >>= \case diff --git a/src/Simplex/Messaging/Agent/Client.hs b/src/Simplex/Messaging/Agent/Client.hs index c01882ceb..d7eba7f58 100644 --- a/src/Simplex/Messaging/Agent/Client.hs +++ b/src/Simplex/Messaging/Agent/Client.hs @@ -14,12 +14,12 @@ module Simplex.Messaging.Agent.Client ( AgentClient (..), newAgentClient, - AgentMonad, withAgentLock, closeAgentClient, newRcvQueue, subscribeQueue, addSubscription, + getSubscriptions, sendConfirmation, sendInvitation, RetryInterval (..), @@ -30,6 +30,7 @@ module Simplex.Messaging.Agent.Client agentNtfCheckToken, agentNtfDeleteToken, agentNtfEnableCron, + agentNtfCreateSubscription, agentCbEncrypt, agentCbDecrypt, cryptoError, @@ -58,6 +59,7 @@ import Data.List.NonEmpty (NonEmpty) import Data.Map.Strict (Map) import qualified Data.Map.Strict as M import Data.Maybe (catMaybes) +import Data.Set (Set) import Data.Text.Encoding import Data.Word (Word16) import Simplex.Messaging.Agent.Env.SQLite @@ -71,7 +73,7 @@ import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Encoding import Simplex.Messaging.Notifications.Client import Simplex.Messaging.Notifications.Protocol -import Simplex.Messaging.Protocol (BrokerMsg, ErrorType, MsgFlags (..), MsgId, ProtocolServer (..), QueueId, QueueIdsKeys (..), SndPublicVerifyKey) +import Simplex.Messaging.Protocol (BrokerMsg, ErrorType, MsgFlags (..), MsgId, NtfPrivateSignKey, ProtocolServer (..), QueueId, QueueIdsKeys (..), SndPublicVerifyKey) import qualified Simplex.Messaging.Protocol as SMP import Simplex.Messaging.TMap (TMap) import qualified Simplex.Messaging.TMap as TM @@ -94,8 +96,8 @@ data AgentClient = AgentClient subQ :: TBQueue (ATransmission 'Agent), msgQ :: TBQueue (ServerTransmission BrokerMsg), smpServers :: TVar (NonEmpty SMPServer), - ntfServers :: TVar [NtfServer], smpClients :: TMap SMPServer SMPClientVar, + ntfServers :: TVar [NtfServer], ntfClients :: TMap NtfServer NtfClientVar, subscrSrvrs :: TMap SMPServer (TMap ConnId RcvQueue), pendingSubscrSrvrs :: TMap SMPServer (TMap ConnId RcvQueue), @@ -107,7 +109,6 @@ data AgentClient = AgentClient asyncClients :: TVar [Async ()], clientId :: Int, agentEnv :: Env, - smpSubscriber :: Async (), lock :: TMVar () } @@ -119,8 +120,8 @@ newAgentClient InitialAgentServers {smp, ntf} agentEnv = do subQ <- newTBQueue qSize msgQ <- newTBQueue qSize smpServers <- newTVar smp - ntfServers <- newTVar ntf smpClients <- TM.empty + ntfServers <- newTVar ntf ntfClients <- TM.empty subscrSrvrs <- TM.empty pendingSubscrSrvrs <- TM.empty @@ -132,14 +133,11 @@ newAgentClient InitialAgentServers {smp, ntf} agentEnv = do asyncClients <- newTVar [] clientId <- stateTVar (clientCounter agentEnv) $ \i -> (i + 1, i + 1) lock <- newTMVar () - return AgentClient {active, rcvQ, subQ, msgQ, smpServers, ntfServers, smpClients, ntfClients, subscrSrvrs, pendingSubscrSrvrs, subscrConns, connMsgsQueued, smpQueueMsgQueues, smpQueueMsgDeliveries, reconnections, asyncClients, clientId, agentEnv, smpSubscriber = undefined, lock} + return AgentClient {active, rcvQ, subQ, msgQ, smpServers, smpClients, ntfServers, ntfClients, subscrSrvrs, pendingSubscrSrvrs, subscrConns, connMsgsQueued, smpQueueMsgQueues, smpQueueMsgDeliveries, reconnections, asyncClients, clientId, agentEnv, lock} agentDbPath :: AgentClient -> FilePath agentDbPath AgentClient {agentEnv = Env {store = SQLiteStore {dbFilePath}}} = dbFilePath --- | Agent monad with MonadReader Env and MonadError AgentErrorType -type AgentMonad m = (MonadUnliftIO m, MonadReader Env m, MonadError AgentErrorType m) - class ProtocolServerClient msg where getProtocolServerClient :: AgentMonad m => AgentClient -> ProtocolServer -> m (ProtocolClient msg) clientProtocolError :: ErrorType -> AgentErrorType @@ -417,7 +415,8 @@ subscribeQueue c rq@RcvQueue {server, rcvPrivateKey, rcvId} connId = do atomically . when (e /= PCENetworkError && e /= PCEResponseTimeout) $ removePendingSubscription c server connId throwError e - Right _ -> addSubscription c rq connId + Right _ -> do + addSubscription c rq connId addSubscription :: MonadIO m => AgentClient -> RcvQueue -> ConnId -> m () addSubscription c rq@RcvQueue {server} connId = atomically $ do @@ -449,6 +448,11 @@ removeSubs_ :: TMap SMPServer (TMap ConnId RcvQueue) -> SMPServer -> ConnId -> S removeSubs_ ss server connId = TM.lookup server ss >>= mapM_ (TM.delete connId) +getSubscriptions :: AgentClient -> STM (Set ConnId) +getSubscriptions AgentClient {subscrConns} = do + m <- readTVar subscrConns + pure $ M.keysSet m + logServer :: MonadIO m => ByteString -> AgentClient -> SMPServer -> QueueId -> ByteString -> m () logServer dir AgentClient {clientId} srv qId cmdStr = logInfo . decodeUtf8 $ B.unwords ["A", "(" <> bshow clientId <> ")", dir, showServer srv, ":", logSecret qId, cmdStr] @@ -529,6 +533,10 @@ agentNtfEnableCron :: AgentMonad m => AgentClient -> NtfTokenId -> NtfToken -> W agentNtfEnableCron c tknId NtfToken {ntfServer, ntfPrivKey} interval = withLogClient c ntfServer tknId "TCRN" $ \ntf -> ntfEnableCron ntf ntfPrivKey tknId interval +agentNtfCreateSubscription :: AgentMonad m => AgentClient -> NtfTokenId -> NtfToken -> SMPQueueNtf -> NtfPrivateSignKey -> m NtfSubscriptionId +agentNtfCreateSubscription c tknId NtfToken {ntfServer, ntfPrivKey} smpQueue nKey = + withLogClient c ntfServer tknId "SNEW" $ \ntf -> ntfCreateSubscription ntf ntfPrivKey (NewNtfSub tknId smpQueue nKey) + agentCbEncrypt :: AgentMonad m => SndQueue -> Maybe C.PublicKeyX25519 -> ByteString -> m ByteString agentCbEncrypt SndQueue {e2eDhSecret} e2ePubKey msg = do cmNonce <- liftIO C.randomCbNonce diff --git a/src/Simplex/Messaging/Agent/Env/SQLite.hs b/src/Simplex/Messaging/Agent/Env/SQLite.hs index 8693414fb..41fbe1f44 100644 --- a/src/Simplex/Messaging/Agent/Env/SQLite.hs +++ b/src/Simplex/Messaging/Agent/Env/SQLite.hs @@ -1,38 +1,60 @@ +{-# LANGUAGE ConstraintKinds #-} {-# LANGUAGE DataKinds #-} {-# LANGUAGE DuplicateRecordFields #-} +{-# LANGUAGE FlexibleContexts #-} +{-# LANGUAGE GADTs #-} +{-# LANGUAGE LambdaCase #-} {-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE NumericUnderscores #-} +{-# LANGUAGE RankNTypes #-} {-# LANGUAGE TypeApplications #-} {-# OPTIONS_GHC -fno-warn-unticked-promoted-constructors #-} module Simplex.Messaging.Agent.Env.SQLite - ( AgentConfig (..), + ( AgentMonad, + AgentConfig (..), InitialAgentServers (..), defaultAgentConfig, defaultReconnectInterval, Env (..), newSMPAgentEnv, + NtfSupervisor (..), + NtfSupervisorCommand (..), + withStore, ) where +import Control.Monad.Except import Control.Monad.IO.Unlift +import Control.Monad.Reader import Crypto.Random import Data.List.NonEmpty (NonEmpty) import Data.Time.Clock (NominalDiffTime, nominalDay) +import Database.SQLite.Simple (SQLError) import Network.Socket import Numeric.Natural -import Simplex.Messaging.Agent.Protocol (SMPServer, currentSMPAgentVersion, supportedSMPAgentVRange) +import Simplex.Messaging.Agent.Protocol import Simplex.Messaging.Agent.RetryInterval +import Simplex.Messaging.Agent.Store (ConnType (..), RcvQueue, StoreError (..)) import Simplex.Messaging.Agent.Store.SQLite import qualified Simplex.Messaging.Agent.Store.SQLite.Migrations as Migrations import Simplex.Messaging.Client +import Simplex.Messaging.Client.Agent () import qualified Simplex.Messaging.Crypto as C -import Simplex.Messaging.Notifications.Client (NtfServer) +import Simplex.Messaging.Notifications.Client (NtfServer, NtfToken) +import Simplex.Messaging.TMap (TMap) +import qualified Simplex.Messaging.TMap as TM import Simplex.Messaging.Transport (TLS, Transport (..)) +import Simplex.Messaging.Util (bshow) import Simplex.Messaging.Version import System.Random (StdGen, newStdGen) +import UnliftIO (Async) +import qualified UnliftIO.Exception as E import UnliftIO.STM +-- | Agent monad with MonadReader Env and MonadError AgentErrorType +type AgentMonad m = (MonadUnliftIO m, MonadReader Env m, MonadError AgentErrorType m) + data InitialAgentServers = InitialAgentServers { smp :: NonEmpty SMPServer, ntf :: [NtfServer] @@ -51,6 +73,7 @@ data AgentConfig = AgentConfig reconnectInterval :: RetryInterval, helloTimeout :: NominalDiffTime, resubscriptionConcurrency :: Int, + ntfWorkerThrottle :: Int, caCertificateFile :: FilePath, privateKeyFile :: FilePath, certificateFile :: FilePath, @@ -83,6 +106,7 @@ defaultAgentConfig = reconnectInterval = defaultReconnectInterval, helloTimeout = 2 * nominalDay, resubscriptionConcurrency = 16, + ntfWorkerThrottle = 1000000, -- microseconds -- CA certificate private key is not needed for initialization -- ! we do not generate these caCertificateFile = "/etc/opt/simplex-agent/ca.crt", @@ -97,7 +121,8 @@ data Env = Env store :: SQLiteStore, idsDrg :: TVar ChaChaDRG, clientCounter :: TVar Int, - randomServer :: TVar StdGen + randomServer :: TVar StdGen, + ntfSupervisor :: NtfSupervisor } newSMPAgentEnv :: (MonadUnliftIO m, MonadRandom m) => AgentConfig -> m Env @@ -106,4 +131,46 @@ newSMPAgentEnv config@AgentConfig {dbFile, dbPoolSize, yesToMigrations} = do store <- liftIO $ createSQLiteStore dbFile dbPoolSize Migrations.app yesToMigrations clientCounter <- newTVarIO 0 randomServer <- newTVarIO =<< liftIO newStdGen - return Env {config, store, idsDrg, clientCounter, randomServer} + ntfSupervisor <- atomically . newNtfSubSupervisor $ tbqSize config + return Env {config, store, idsDrg, clientCounter, randomServer, ntfSupervisor} + +data NtfSupervisor = NtfSupervisor + { ntfTkn :: TVar (Maybe NtfToken), + ntfSubQ :: TBQueue (RcvQueue, NtfSupervisorCommand), + ntfWorkers :: TMap NtfServer (TMVar (), Async ()), + ntfSMPWorkers :: TMap SMPServer (TMVar (), Async ()) + } + +data NtfSupervisorCommand = NSCCreate | NSCDelete + +newNtfSubSupervisor :: Natural -> STM NtfSupervisor +newNtfSubSupervisor qSize = do + ntfTkn <- newTVar Nothing + ntfSubQ <- newTBQueue qSize + ntfWorkers <- TM.empty + ntfSMPWorkers <- TM.empty + pure NtfSupervisor {ntfTkn, ntfSubQ, ntfWorkers, ntfSMPWorkers} + +withStore :: AgentMonad m => (forall m'. AgentStoreMonad m' => SQLiteStore -> m' a) -> m a +withStore action = do + st <- asks store + runExceptT (action st `E.catch` handleInternal) >>= \case + Right c -> return c + Left e -> throwError $ storeError e + where + -- TODO when parsing exception happens in store, the agent hangs; + -- changing SQLError to SomeException does not help + handleInternal :: (MonadError StoreError m') => SQLError -> m' a + handleInternal e = throwError . SEInternal $ bshow e + storeError :: StoreError -> AgentErrorType + storeError = \case + SEConnNotFound -> CONN NOT_FOUND + SEConnDuplicate -> CONN DUPLICATE + SEBadConnType CRcv -> CONN SIMPLEX + SEBadConnType CSnd -> CONN SIMPLEX + SEInvitationNotFound -> CMD PROHIBITED + -- this error is never reported as store error, + -- it is used to wrap agent operations when "transaction-like" store access is needed + -- NOTE: network IO should NOT be used inside AgentStoreMonad + SEAgentError e -> e + e -> INTERNAL $ show e diff --git a/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs b/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs new file mode 100644 index 000000000..6842da6d6 --- /dev/null +++ b/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs @@ -0,0 +1,149 @@ +{-# LANGUAGE DuplicateRecordFields #-} +{-# LANGUAGE FlexibleContexts #-} +{-# LANGUAGE LambdaCase #-} +{-# LANGUAGE NamedFieldPuns #-} +{-# LANGUAGE ScopedTypeVariables #-} + +module Simplex.Messaging.Agent.NtfSubSupervisor + ( runNtfSupervisor, + nsUpdateToken, + nsRemoveNtfToken, + sendNtfSubCommand, + closeNtfSupervisor, + getNtfServer, + ) +where + +import Control.Concurrent (threadDelay) +import Control.Concurrent.Async (Async, uninterruptibleCancel) +import Control.Concurrent.STM (stateTVar) +import Control.Monad +import Control.Monad.Except (runExceptT) +import Control.Monad.IO.Unlift (MonadUnliftIO) +import Control.Monad.Reader +import Data.Bifunctor (first) +import qualified Data.Map.Strict as M +import Data.Time (getCurrentTime) +import Simplex.Messaging.Agent.Client +import Simplex.Messaging.Agent.Env.SQLite +import Simplex.Messaging.Agent.Store +import Simplex.Messaging.Client.Agent () +import Simplex.Messaging.Notifications.Client +import Simplex.Messaging.Notifications.Protocol (NtfTknStatus (..)) +import Simplex.Messaging.Protocol (ProtocolServer) +import Simplex.Messaging.TMap (TMap) +import qualified Simplex.Messaging.TMap as TM +import System.Random (randomR) +import UnliftIO (async) +import qualified UnliftIO.Exception as E +import UnliftIO.STM + +runNtfSupervisor :: (MonadUnliftIO m, MonadReader Env m) => AgentClient -> m () +runNtfSupervisor c = forever $ do + ns <- asks ntfSupervisor + cmd <- atomically . readTBQueue $ ntfSubQ ns + runExceptT (processNtfSub c cmd) >>= \case + Left e -> liftIO $ print e + Right _ -> return () + +processNtfSub :: forall m. AgentMonad m => AgentClient -> (RcvQueue, NtfSupervisorCommand) -> m () +processNtfSub c (rcvQueue@RcvQueue {server = smpServer, rcvId}, cmd) = do + ntfServer_ <- getNtfServer c + ns <- asks ntfSupervisor + ntfToken_ <- readTVarIO $ ntfTkn ns + case cmd of + NSCCreate -> do + sub_ <- withStore $ \st -> getNtfSubscription st rcvQueue + case (sub_, ntfServer_, ntfToken_) of + (Nothing, Just ntfServer, Just tkn) -> do + currentTime <- liftIO getCurrentTime + let newSub = newNtfSubscription ntfServer tkn smpServer rcvId currentTime + withStore $ \st -> createNtfSubscription st newSub + -- TODO optimize? + -- TODO - read action in getNtfSubscription and decide which worker to create + -- TODO - SMP worker can create Ntf worker on NKEY completion + addNtfSMPWorker smpServer + addNtfWorker ntfServer + (Just _, Just ntfServer, Just _) -> do + addNtfSMPWorker smpServer + addNtfWorker ntfServer + _ -> pure () + NSCDelete -> do + withStore $ \st -> markNtfSubscriptionForDeletion st rcvQueue + case (ntfServer_, ntfToken_) of + (Just ntfServer, Just _) -> addNtfWorker ntfServer + _ -> pure () + where + addNtfWorker = addWorker ntfWorkers runNtfWorker + addNtfSMPWorker = addWorker ntfSMPWorkers runNtfSMPWorker + addWorker :: + (NtfSupervisor -> TMap ProtocolServer (TMVar (), Async ())) -> + (AgentClient -> ProtocolServer -> TMVar () -> m ()) -> + ProtocolServer -> + m () + addWorker wsSel runWorker srv = do + ws <- asks $ wsSel . ntfSupervisor + atomically (TM.lookup srv ws) >>= \case + Nothing -> do + doWork <- newTMVarIO () + worker <- async $ runWorker c srv doWork `E.finally` atomically (TM.delete srv ws) + atomically $ TM.insert srv (doWork, worker) ws + Just (doWork, _) -> void . atomically $ tryPutTMVar doWork () + +runNtfWorker :: AgentMonad m => AgentClient -> NtfServer -> TMVar () -> m () +runNtfWorker _c srv doWork = forever $ do + void . atomically $ readTMVar doWork + withStore $ \st -> + getNextNtfSubAction st srv >>= \case + Nothing -> void . atomically $ tryTakeTMVar doWork + Just (_sub, ntfSubAction) -> + forM_ ntfSubAction $ \case + NSANew _nKey -> pure () + NSACheck -> pure () + NSADelete -> pure () + delay <- asks $ ntfWorkerThrottle . config + liftIO $ threadDelay delay + +runNtfSMPWorker :: AgentMonad m => AgentClient -> NtfServer -> TMVar () -> m () +runNtfSMPWorker _c srv doWork = forever $ do + void . atomically $ readTMVar doWork + withStore $ \st -> + getNextNtfSubSMPAction st srv >>= \case + Nothing -> void . atomically $ tryTakeTMVar doWork + Just (_sub, ntfSubAction) -> + forM_ ntfSubAction $ \case + NSAKey -> pure () + delay <- asks $ ntfWorkerThrottle . config + liftIO $ threadDelay delay + +nsUpdateToken :: NtfSupervisor -> NtfToken -> STM () +nsUpdateToken ns tkn = writeTVar (ntfTkn ns) $ Just tkn + +nsRemoveNtfToken :: NtfSupervisor -> STM () +nsRemoveNtfToken ns = writeTVar (ntfTkn ns) Nothing + +sendNtfSubCommand :: NtfSupervisor -> (RcvQueue, NtfSupervisorCommand) -> STM () +sendNtfSubCommand ns cmd = + readTVar (ntfTkn ns) + >>= mapM_ (\NtfToken {ntfTknStatus} -> when (ntfTknStatus == NTActive) $ writeTBQueue (ntfSubQ ns) cmd) + +closeNtfSupervisor :: NtfSupervisor -> IO () +closeNtfSupervisor ns = do + cancelNtfWorkers_ $ ntfWorkers ns + cancelNtfWorkers_ $ ntfSMPWorkers ns + +cancelNtfWorkers_ :: TMap ProtocolServer (TMVar (), Async ()) -> IO () +cancelNtfWorkers_ wsVar = do + ws <- atomically $ stateTVar wsVar $ \ws -> (ws, M.empty) + forM_ ws $ uninterruptibleCancel . snd + +getNtfServer :: AgentMonad m => AgentClient -> m (Maybe NtfServer) +getNtfServer c = do + ntfServers <- readTVarIO $ ntfServers c + case ntfServers of + [] -> pure Nothing + [srv] -> pure $ Just srv + servers -> do + gen <- asks randomServer + atomically . stateTVar gen $ + first (Just . (servers !!)) . randomR (0, length servers - 1) diff --git a/src/Simplex/Messaging/Agent/Store.hs b/src/Simplex/Messaging/Agent/Store.hs index 31224488a..7b642fb90 100644 --- a/src/Simplex/Messaging/Agent/Store.hs +++ b/src/Simplex/Messaging/Agent/Store.hs @@ -48,6 +48,7 @@ class Monad m => MonadAgentStore s m where setRcvQueueStatus :: s -> RcvQueue -> QueueStatus -> m () setRcvQueueConfirmedE2E :: s -> RcvQueue -> C.DhSecretX25519 -> m () setSndQueueStatus :: s -> SndQueue -> QueueStatus -> m () + getRcvQueue :: s -> ConnId -> m RcvQueue -- Confirmations createConfirmation :: s -> TVar ChaChaDRG -> NewConfirmation -> m ConfirmationId @@ -88,6 +89,15 @@ class Monad m => MonadAgentStore s m where updateNtfToken :: s -> NtfToken -> NtfTknStatus -> Maybe NtfTknAction -> m () removeNtfToken :: s -> NtfToken -> m () + -- Notification subscription persistence + getNtfSubscription :: s -> RcvQueue -> m (Maybe NtfSubscription) + createNtfSubscription :: s -> NtfSubscription -> m () + markNtfSubscriptionForDeletion :: s -> RcvQueue -> m () + updateNtfSubscription :: s -> RcvQueue -> NtfSubscription -> m () + deleteNtfSubscription :: s -> RcvQueue -> m () + getNextNtfSubAction :: s -> NtfServer -> m (Maybe (NtfSubscription, Maybe NtfSubAction)) + getNextNtfSubSMPAction :: s -> SMPServer -> m (Maybe (NtfSubscription, Maybe NtfSubSMPAction)) + -- * Queue types -- | A receive queue. SMP queue through which the agent receives messages from a sender. @@ -336,3 +346,13 @@ data StoreError | -- | Used to wrap agent errors inside store operations to avoid race conditions SEAgentError AgentErrorType deriving (Eq, Show, Exception) + +-- * Ntf Subscriptions + +-- data NewNtfSubscription = NewNtfSubscription +-- { ntfServer :: NtfServer, +-- ntfSubId :: NtfSubscriptionId, +-- ratchetState :: RatchetX448 +-- } + +data NtfSubscriptionAction = NSubSMPAction SMPServer | NSubNtfAction NtfServer diff --git a/src/Simplex/Messaging/Agent/Store/SQLite.hs b/src/Simplex/Messaging/Agent/Store/SQLite.hs index c22ec5408..968f86f1f 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite.hs @@ -60,7 +60,7 @@ import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Crypto.Ratchet (RatchetX448, SkippedMsgDiff (..), SkippedMsgKeys) import Simplex.Messaging.Encoding import Simplex.Messaging.Encoding.String -import Simplex.Messaging.Notifications.Client (NtfServer, NtfTknAction, NtfToken (..)) +import Simplex.Messaging.Notifications.Client (NtfServer, NtfSubAction, NtfSubSMPAction, NtfSubscription, NtfTknAction, NtfToken (..)) import Simplex.Messaging.Notifications.Protocol (DeviceToken (..), NtfTknStatus (..), NtfTokenId) import Simplex.Messaging.Parsers (blobFieldParser, fromTextField_) import Simplex.Messaging.Protocol (MsgBody, MsgFlags, ProtocolServer (..)) @@ -301,6 +301,9 @@ instance (MonadUnliftIO m, MonadError StoreError m) => MonadAgentStore SQLiteSto |] [":status" := status, ":host" := host, ":port" := port, ":snd_id" := sndId] + getRcvQueue :: SQLiteStore -> ConnId -> m RcvQueue + getRcvQueue _st _connId = throwError SENotImplemented + createConfirmation :: SQLiteStore -> TVar ChaChaDRG -> NewConfirmation -> m ConfirmationId createConfirmation st gVar NewConfirmation {connId, senderConf = SMPConfirmation {senderKey, e2ePubKey, connInfo, smpReplyQueues}, ratchetState} = liftIOEither . withTransaction st $ \db -> @@ -645,6 +648,27 @@ instance (MonadUnliftIO m, MonadError StoreError m) => MonadAgentStore SQLiteSto |] (provider, token, host, port) + getNtfSubscription :: SQLiteStore -> RcvQueue -> m (Maybe NtfSubscription) + getNtfSubscription _st _rcvQueue = throwError SENotImplemented + + createNtfSubscription :: SQLiteStore -> NtfSubscription -> m () + createNtfSubscription _st _ntfSub = throwError SENotImplemented + + markNtfSubscriptionForDeletion :: s -> RcvQueue -> m () + markNtfSubscriptionForDeletion _st _rcvQueue = throwError SENotImplemented + + updateNtfSubscription :: s -> RcvQueue -> NtfSubscription -> m () + updateNtfSubscription _st _rcvQueue _ntfSub = throwError SENotImplemented + + deleteNtfSubscription :: s -> RcvQueue -> m () + deleteNtfSubscription _st _rcvQueue = throwError SENotImplemented + + getNextNtfSubAction :: s -> NtfServer -> m (Maybe (NtfSubscription, Maybe NtfSubAction)) + getNextNtfSubAction _st _srv = throwError SENotImplemented + + getNextNtfSubSMPAction :: s -> SMPServer -> m (Maybe (NtfSubscription, Maybe NtfSubSMPAction)) + getNextNtfSubSMPAction _st _srv = throwError SENotImplemented + -- * Auxiliary helpers instance ToField QueueStatus where toField = toField . serializeQueueStatus diff --git a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220404_ntf_subscriptions_draft.hs b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220404_ntf_subscriptions_draft.hs index 7ddedddd3..775d5a175 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220404_ntf_subscriptions_draft.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220404_ntf_subscriptions_draft.hs @@ -19,20 +19,23 @@ CREATE UNIQUE INDEX idx_rcv_queues_ntf ON rcv_queues (host, port, ntf_id); CREATE TABLE ntf_subscriptions ( ntf_host TEXT NOT NULL, ntf_port TEXT NOT NULL, - ntf_sub_id BLOB NOT NULL, + ntf_sub_id BLOB, ntf_sub_status TEXT NOT NULL, -- new, created, active, pending, error_auth - ntf_sub_action TEXT, -- if there is an action required on this subscription: create / check / token / delete + ntf_sub_action TEXT, -- if there is an action required on this subscription: create / check / delete + ntf_sub_smp_action TEXT, -- action with SMP server: nkey; only one of this and ntf_sub_action can (should) be not null in same record ntf_sub_action_ts TEXT, -- the earliest time for the action, e.g. checks can be scheduled every X hours ntf_token TEXT NOT NULL, -- or BLOB? - smp_host TEXT NOT NULL, - smp_port TEXT NOT NULL, - smp_ntf_id BLOB NOT NULL, + smp_host TEXT NULL, + smp_port TEXT NULL, + smp_rcv_id BLOB NULL, + smp_ntf_id BLOB, + marked_for_deletion INTEGER NOT NULL DEFAULT 0, -- to be checked on updates by workers to not overwrite delete command created_at TEXT NOT NULL, updated_at TEXT NOT NULL, -- this is to check subscription status periodically to know when it was last checked - PRIMARY KEY (ntf_host, ntf_port, ntf_sub_id), + PRIMARY KEY (smp_host, smp_port, smp_rcv_id), FOREIGN KEY (ntf_host, ntf_port) REFERENCES ntf_servers ON DELETE RESTRICT ON UPDATE CASCADE, - FOREIGN KEY (smp_host, smp_port, smp_ntf_id) REFERENCES rcv_queues (host, port, ntf_id) - ON DELETE RESTRICT ON UPDATE CASCADE + FOREIGN KEY (smp_host, smp_port, smp_rcv_id) REFERENCES rcv_queues (host, port, rcv_id) + ON DELETE SET NULL ON UPDATE CASCADE ) WITHOUT ROWID; |] diff --git a/src/Simplex/Messaging/Notifications/Client.hs b/src/Simplex/Messaging/Notifications/Client.hs index 13971e13e..ae4eaf1ea 100644 --- a/src/Simplex/Messaging/Notifications/Client.hs +++ b/src/Simplex/Messaging/Notifications/Client.hs @@ -10,6 +10,7 @@ module Simplex.Messaging.Notifications.Client where import Control.Monad.Except import Control.Monad.Trans.Except import qualified Data.Attoparsec.ByteString.Char8 as A +import Data.Time (UTCTime) import Data.Word (Word16) import Database.SQLite.Simple.FromField (FromField (..)) import Database.SQLite.Simple.ToField (ToField (..)) @@ -18,7 +19,7 @@ import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Encoding import Simplex.Messaging.Notifications.Protocol import Simplex.Messaging.Parsers (blobFieldDecoder) -import Simplex.Messaging.Protocol (ProtocolServer) +import Simplex.Messaging.Protocol (NotifierId, NtfPrivateSignKey, ProtocolServer, RecipientId, SMPServer) type NtfServer = ProtocolServer @@ -130,3 +131,66 @@ newNtfToken deviceToken ntfServer (ntfPubKey, ntfPrivKey) ntfDhKeys = ntfTknStatus = NTNew, ntfTknAction = Just NTARegister } + +data NtfSubAction + = NSANew NtfPrivateSignKey + | NSACheck + | NSADelete + deriving (Show) + +instance Encoding NtfSubAction where + smpEncode = \case + NSANew nKey -> smpEncode ('N', nKey) + NSACheck -> "C" + NSADelete -> "D" + smpP = + A.anyChar >>= \case + 'N' -> NSANew <$> smpP + 'C' -> pure NSACheck + 'D' -> pure NSADelete + _ -> fail "bad NtfSubAction" + +instance FromField NtfSubAction where fromField = blobFieldDecoder smpDecode + +instance ToField NtfSubAction where toField = toField . smpEncode + +data NtfSubSMPAction + = NSAKey + deriving (Show) + +instance Encoding NtfSubSMPAction where + smpEncode = \case + NSAKey -> "K" + smpP = + A.anyChar >>= \case + 'K' -> pure NSAKey + _ -> fail "bad NtfSubSMPAction" + +instance FromField NtfSubSMPAction where fromField = blobFieldDecoder smpDecode + +instance ToField NtfSubSMPAction where toField = toField . smpEncode + +data NtfSubscription = NtfSubscription + { ntfServer :: NtfServer, + ntfSubId :: Maybe NtfSubscriptionId, + ntfSubStatus :: NtfSubStatus, + ntfSubActionTs :: UTCTime, + ntfToken :: NtfToken, -- ? + smpServer :: SMPServer, -- use SMPQueueNtf? + rcvQueueId :: RecipientId, + ntfQueueId :: Maybe NotifierId + } + deriving (Show) + +newNtfSubscription :: NtfServer -> NtfToken -> SMPServer -> RecipientId -> UTCTime -> NtfSubscription +newNtfSubscription ntfServer ntfToken smpServer rcvQueueId ntfSubActionTs = + NtfSubscription + { ntfServer, + ntfSubId = Nothing, + ntfSubStatus = NSKey, + ntfSubActionTs, + ntfToken, + smpServer, + rcvQueueId, + ntfQueueId = Nothing + } diff --git a/src/Simplex/Messaging/Notifications/Protocol.hs b/src/Simplex/Messaging/Notifications/Protocol.hs index 2c33ab86a..83b9d6685 100644 --- a/src/Simplex/Messaging/Notifications/Protocol.hs +++ b/src/Simplex/Messaging/Notifications/Protocol.hs @@ -363,7 +363,9 @@ type NtfSubscriptionId = NtfEntityId type NtfTokenId = NtfEntityId data NtfSubStatus - = -- | state after SNEW + = -- | state after NKEY + NSKey + | -- | state after SNEW NSNew | -- | pending connection/subscription to SMP server NSPending @@ -377,6 +379,7 @@ data NtfSubStatus instance Encoding NtfSubStatus where smpEncode = \case + NSKey -> "KEY" NSNew -> "NEW" NSPending -> "PENDING" -- e.g. after SMP server disconnect/timeout while ntf server is retrying to connect NSActive -> "ACTIVE" @@ -384,6 +387,7 @@ instance Encoding NtfSubStatus where NSSMPAuth -> "SMP_AUTH" smpP = A.takeTill (== ' ') >>= \case + "KEY" -> pure NSKey "NEW" -> pure NSNew "PENDING" -> pure NSPending "ACTIVE" -> pure NSActive From 0a71822dd0a83bb4d9cb8814dd876990a4429c4a Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Sun, 12 Jun 2022 15:59:14 +0100 Subject: [PATCH 051/131] v2.3.0: save and restore messages when the server is restarted (#395) * save and restore messages when server is restarted * read file line by line * add import * optmize restoring messages by reading the whole file * update version to 2.3.0 * update scripts * add script * update readme --- CHANGELOG.md | 6 ++ README.md | 13 +-- apps/smp-server/Main.hs | 16 +++- package.yaml | 2 +- scripts/smp-server-linode.sh | 1 + scripts/update-smp-server.sh | 12 +++ simplexmq.cabal | 2 +- src/Simplex/Messaging/Encoding/String.hs | 10 +++ src/Simplex/Messaging/Server.hs | 36 ++++++++- src/Simplex/Messaging/Server/Env/STM.hs | 1 + src/Simplex/Messaging/Server/MsgStore.hs | 15 ++++ src/Simplex/Messaging/Server/MsgStore/STM.hs | 4 + src/Simplex/Messaging/Transport.hs | 2 +- tests/SMPClient.hs | 7 ++ tests/ServerTests.hs | 83 ++++++++++++++++++-- 15 files changed, 191 insertions(+), 19 deletions(-) create mode 100755 scripts/update-smp-server.sh diff --git a/CHANGELOG.md b/CHANGELOG.md index 048edcb25..f5208269d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +# 2.3.0 + +SMP server: + +- Save and restore undelivered messages, to avoid losing them. To save messages the server has to be stopped with SIGINT signal, if it is stopped with SIGTERM undelivered messages would not be saved. + # 2.2.0 SMP server: diff --git a/README.md b/README.md index 62fb440ab..bb94bf407 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,8 @@ SMP server uses in-memory persistence with an optional append-only log of create To enable store log, initialize server using `smp-server -l` command, or modify `smp-server.ini` created during initialization (uncomment `enable: on` option in the store log section). Use `smp-server --help` for other usage tips. +Starting from version 2.3.0, when store log is enabled, the server would also enable saving undelivered messages on exit and restoring them on start. This can be disabled via a separate setting `restore_messages` in `smp-server.ini` file. Saving messages would only work if the server is stopped with SIGINT signal (keyboard interrupt), if it is stopped with SIGTERM signal the messages would not be saved. + > **Please note:** On initialization SMP server creates a chain of two certificates: a self-signed CA certificate ("offline") and a server certificate used for TLS handshake ("online"). **You should store CA certificate private key securely and delete it from the server. If server TLS credential is compromised this key can be used to sign a new one, keeping the same server identity and established connections.** CA private key location by default is `/etc/opt/simplex/ca.key`. SMP server implements [SMP protocol](https://github.com/simplex-chat/simplexmq/blob/master/protocol/simplex-messaging.md). @@ -61,6 +63,7 @@ Now `openssl version` should be saying "OpenSSL". You can now run `smp-server in ### SMP client library [SMP client](https://github.com/simplex-chat/simplexmq/blob/master/src/Simplex/Messaging/Client.hs) is a Haskell library to connect to SMP servers that allows to: + - execute commands with a functional API. - receive messages and other notifications via STM queue. - automatically send keep-alive commands. @@ -118,11 +121,11 @@ Deployment on Linode is performed via StackScripts, which serve as recipes for L - Create a Linode account or login with an already existing one. - Open [SMP server StackScript](https://cloud.linode.com/stackscripts/748014) and click "Deploy New Linode". - You can optionally configure the following parameters: - - SMP Server store log flag for queue persistence on server restart, recommended. - - [Linode API token](https://www.linode.com/docs/guides/getting-started-with-the-linode-api#get-an-access-token) to attach server address etc. as tags to Linode and to add A record to your 2nd level domain (e.g. `example.com` [domain should be created](https://cloud.linode.com/domains/create) in your account prior to deployment). The API token access scopes: - - read/write for "linodes" - - read/write for "domains" - - Domain name to use instead of Linode IP address, e.g. `smp1.example.com`. + - SMP Server store log flag for queue persistence on server restart, recommended. + - [Linode API token](https://www.linode.com/docs/guides/getting-started-with-the-linode-api#get-an-access-token) to attach server address etc. as tags to Linode and to add A record to your 2nd level domain (e.g. `example.com` [domain should be created](https://cloud.linode.com/domains/create) in your account prior to deployment). The API token access scopes: + - read/write for "linodes" + - read/write for "domains" + - Domain name to use instead of Linode IP address, e.g. `smp1.example.com`. - Choose the region and plan, Shared CPU Nanode with 1Gb is sufficient. - Provide ssh key to be able to connect to your Linode via ssh. If you haven't provided a Linode API token this step is required to login to your Linode and get the server's fingerprint either from the welcome message or from the file `/etc/opt/simplex/fingerprint` after server starts. See [Linode's guide on ssh](https://www.linode.com/docs/guides/use-public-key-authentication-with-ssh/) . - Deploy your Linode. After it starts wait for SMP server to start and for tags to appear (if a Linode API token was provided). It may take up to 5 minutes depending on the connection speed on the Linode. Connecting Linode IP address to provided domain name may take some additional time. diff --git a/apps/smp-server/Main.hs b/apps/smp-server/Main.hs index 5f9253665..d6d99c90e 100644 --- a/apps/smp-server/Main.hs +++ b/apps/smp-server/Main.hs @@ -7,6 +7,7 @@ module Main where import Control.Logger.Simple +import Data.Functor (($>)) import Data.Ini (lookupValue) import Simplex.Messaging.Server (runSMPServer) import Simplex.Messaging.Server.CLI (ServerCLIConfig (..), protocolServerCLI, readStrictIni) @@ -57,9 +58,11 @@ smpServerCLIConfig = \# that will be lost on restart (e.g., as with redis).\n\ \# This option enables saving memory to append only log,\n\ \# and restoring it when the server is started.\n\ - \# Log is compacted on start (deleted objects are removed).\n\ - \# The messages are not logged.\n" - <> ("enable: " <> (if enableStoreLog then "on" else "off # on") <> "\n\n") + \# Log is compacted on start (deleted objects are removed).\n" + <> ("enable: " <> (if enableStoreLog then "on" else "off # on") <> "\n") + <> "# The messages are optionally saved and restored when the server restarts,\n\ + \# they are deleted after restarting.\n" + <> ("restore_messages: " <> (if enableStoreLog then "on" else "off # on") <> "\n\n") <> "[TRANSPORT]\n" <> ("port: " <> defaultServerPort <> "\n") <> "websockets: off\n\n" @@ -80,6 +83,13 @@ smpServerCLIConfig = privateKeyFile = serverKeyFile, certificateFile = serverCrtFile, storeLogFile, + storeMsgsFile = + let messagesPath = combine logPath "smp-server-messages.log" + in case lookupValue "STORE_LOG" "restore_messages" ini of + Right "on" -> Just messagesPath + Right _ -> Nothing + -- if the setting is not set, it is enabled when store log is enabled + _ -> storeLogFile $> messagesPath, allowNewQueues = True, messageExpiration = Just defaultMessageExpiration, inactiveClientExpiration = diff --git a/package.yaml b/package.yaml index 897122846..6f11d6eec 100644 --- a/package.yaml +++ b/package.yaml @@ -1,5 +1,5 @@ name: simplexmq -version: 2.2.1 +version: 2.3.0 synopsis: SimpleXMQ message broker description: | This package includes <./docs/Simplex-Messaging-Server.html server>, diff --git a/scripts/smp-server-linode.sh b/scripts/smp-server-linode.sh index a34a9a625..aedf975de 100644 --- a/scripts/smp-server-linode.sh +++ b/scripts/smp-server-linode.sh @@ -157,6 +157,7 @@ Description=SMP server [Service] Type=simple ExecStart=/bin/sh -c "exec $binary start >> /var/opt/simplex/smp-server.log 2>&1" +KillSignal=SIGINT Restart=always RestartSec=10 LimitNOFILE=1000000 diff --git a/scripts/update-smp-server.sh b/scripts/update-smp-server.sh new file mode 100755 index 000000000..2182eb527 --- /dev/null +++ b/scripts/update-smp-server.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +# systemd has to be configured to use SIGINT to save and restore undelivered messages after restart. +# Add this to [Service] section: +# KillSignal=SIGINT +curl -L -o /opt/simplex/bin/smp-server-new https://github.com/simplex-chat/simplexmq/releases/latest/download/smp-server-ubuntu-20_04-x86-64 +systemctl stop smp-server +cp /var/opt/simplex/smp-server-store.log /var/opt/simplex/smp-server-store.log.bak +mv /opt/simplex/bin/smp-server /opt/simplex/bin/smp-server-old +mv /opt/simplex/bin/smp-server-new /opt/simplex/bin/smp-server +chmod +x /opt/simplex/bin/smp-server +systemctl start smp-server diff --git a/simplexmq.cabal b/simplexmq.cabal index dcc9597d8..77318feb3 100644 --- a/simplexmq.cabal +++ b/simplexmq.cabal @@ -5,7 +5,7 @@ cabal-version: 1.12 -- see: https://github.com/sol/hpack name: simplexmq -version: 2.2.1 +version: 2.3.0 synopsis: SimpleXMQ message broker description: This package includes <./docs/Simplex-Messaging-Server.html server>, <./docs/Simplex-Messaging-Client.html client> and diff --git a/src/Simplex/Messaging/Encoding/String.hs b/src/Simplex/Messaging/Encoding/String.hs index 52b26ef1d..ec4b934bd 100644 --- a/src/Simplex/Messaging/Encoding/String.hs +++ b/src/Simplex/Messaging/Encoding/String.hs @@ -26,9 +26,11 @@ import qualified Data.ByteString.Base64.URL as U import Data.ByteString.Char8 (ByteString) import qualified Data.ByteString.Char8 as B import Data.Char (isAlphaNum) +import Data.Int (Int64) import qualified Data.List.NonEmpty as L import Data.Text (Text) import Data.Text.Encoding (decodeLatin1, encodeUtf8) +import Data.Time.Clock.System (SystemTime (..)) import Data.Word (Word16) import Simplex.Messaging.Parsers (parseAll) import Simplex.Messaging.Util ((<$?>)) @@ -82,6 +84,14 @@ instance StrEncoding Word16 where strEncode = B.pack . show strP = A.decimal +instance StrEncoding Int64 where + strEncode = B.pack . show + strP = A.decimal + +instance StrEncoding SystemTime where + strEncode = strEncode . systemSeconds + strP = MkSystemTime <$> strP <*> pure 0 + -- lists encode/parse as comma-separated strings strEncodeList :: StrEncoding a => [a] -> ByteString strEncodeList = B.intercalate "," . map strEncode diff --git a/src/Simplex/Messaging/Server.hs b/src/Simplex/Messaging/Server.hs index 2b2dbaf7f..9f0730799 100644 --- a/src/Simplex/Messaging/Server.hs +++ b/src/Simplex/Messaging/Server.hs @@ -52,6 +52,7 @@ import Data.Time.Clock.System (SystemTime (..), getSystemTime) import Data.Type.Equality import Network.Socket (ServiceName) import qualified Simplex.Messaging.Crypto as C +import Simplex.Messaging.Encoding.String import Simplex.Messaging.Protocol import Simplex.Messaging.Server.Env.STM import Simplex.Messaging.Server.Expiration @@ -67,6 +68,7 @@ import Simplex.Messaging.Transport.Server import Simplex.Messaging.Util import System.Mem.Weak (deRefWeak) import UnliftIO.Concurrent +import UnliftIO.Directory (doesFileExist, renameFile) import UnliftIO.Exception import UnliftIO.IO import UnliftIO.STM @@ -90,12 +92,13 @@ smpServer :: forall m. (MonadUnliftIO m, MonadReader Env m) => TMVar Bool -> m ( smpServer started = do s <- asks server cfg@ServerConfig {transports} <- asks config + restoreServerMessages raceAny_ ( serverThread s subscribedQ subscribers subscriptions cancelSub : serverThread s ntfSubscribedQ notifiers ntfSubscriptions (\_ -> pure ()) : map runServer transports <> expireMessagesThread_ cfg <> serverStatsThread_ cfg ) - `finally` withLog closeStoreLog + `finally` (withLog closeStoreLog >> saveServerMessages) where runServer :: (ServiceName, ATransport) -> m () runServer (tcpPort, ATransport t) = do @@ -532,3 +535,34 @@ randomId :: (MonadUnliftIO m, MonadReader Env m) => Int -> m ByteString randomId n = do gVar <- asks idsDrg atomically (C.pseudoRandomBytes n gVar) + +saveServerMessages :: forall m. (MonadUnliftIO m, MonadReader Env m) => m () +saveServerMessages = asks (storeMsgsFile . config) >>= mapM_ saveMessages + where + saveMessages f = do + liftIO $ putStrLn $ "saving messages to file " <> f + ms <- asks msgStore + liftIO . withFile f WriteMode $ \h -> + readTVarIO ms >>= mapM_ (saveQueueMsgs ms h) . M.keys + where + saveQueueMsgs ms h rId = + atomically (flushMsgQueue ms rId) + >>= mapM_ (B.hPutStrLn h . strEncode . MsgLogRecord rId) + +restoreServerMessages :: forall m. (MonadUnliftIO m, MonadReader Env m) => m () +restoreServerMessages = asks (storeMsgsFile . config) >>= mapM_ restoreMessages + where + restoreMessages f = whenM (doesFileExist f) $ do + liftIO $ putStrLn $ "restoring messages from file " <> f + ms <- asks msgStore + quota <- asks $ msgQueueQuota . config + liftIO $ mapM_ (restoreMsg ms quota) . B.lines =<< B.readFile f + renameFile f $ f <> ".bak" + where + restoreMsg ms quota s = case strDecode s of + Left e -> B.putStrLn $ "message parsing error (" <> B.pack e <> "): " <> B.take 100 s + Right (MsgLogRecord rId msg) -> do + full <- atomically $ do + q <- getMsgQueue ms rId quota + ifM (isFull q) (pure True) (writeMsg q msg $> False) + when full . B.putStrLn $ "message queue " <> strEncode rId <> " is full, message not restored: " <> strEncode (msgId msg) diff --git a/src/Simplex/Messaging/Server/Env/STM.hs b/src/Simplex/Messaging/Server/Env/STM.hs index 3f83ebfff..badf6a983 100644 --- a/src/Simplex/Messaging/Server/Env/STM.hs +++ b/src/Simplex/Messaging/Server/Env/STM.hs @@ -43,6 +43,7 @@ data ServerConfig = ServerConfig queueIdBytes :: Int, msgIdBytes :: Int, storeLogFile :: Maybe FilePath, + storeMsgsFile :: Maybe FilePath, -- | set to False to prohibit creating new queues allowNewQueues :: Bool, -- | time after which the messages can be removed from the queues and check interval, seconds diff --git a/src/Simplex/Messaging/Server/MsgStore.hs b/src/Simplex/Messaging/Server/MsgStore.hs index a7180311f..92eaf935a 100644 --- a/src/Simplex/Messaging/Server/MsgStore.hs +++ b/src/Simplex/Messaging/Server/MsgStore.hs @@ -1,10 +1,12 @@ {-# LANGUAGE FunctionalDependencies #-} +{-# LANGUAGE NamedFieldPuns #-} module Simplex.Messaging.Server.MsgStore where import Data.Int (Int64) import Data.Time.Clock.System (SystemTime) import Numeric.Natural +import Simplex.Messaging.Encoding.String import Simplex.Messaging.Protocol (MsgBody, MsgId, RecipientId) data Message = Message @@ -13,9 +15,22 @@ data Message = Message msgBody :: MsgBody } +instance StrEncoding Message where + strEncode Message {msgId, ts, msgBody} = strEncode (msgId, ts, msgBody) + strP = do + (msgId, ts, msgBody) <- strP + pure Message {msgId, ts, msgBody} + +data MsgLogRecord = MsgLogRecord RecipientId Message + +instance StrEncoding MsgLogRecord where + strEncode (MsgLogRecord rId msg) = strEncode (rId, msg) + strP = MsgLogRecord <$> strP_ <*> strP + class MonadMsgStore s q m | s -> q where getMsgQueue :: s -> RecipientId -> Natural -> m q delMsgQueue :: s -> RecipientId -> m () + flushMsgQueue :: s -> RecipientId -> m [Message] class MonadMsgQueue q m where isFull :: q -> m Bool diff --git a/src/Simplex/Messaging/Server/MsgStore/STM.hs b/src/Simplex/Messaging/Server/MsgStore/STM.hs index be395f918..ad55b758d 100644 --- a/src/Simplex/Messaging/Server/MsgStore/STM.hs +++ b/src/Simplex/Messaging/Server/MsgStore/STM.hs @@ -7,6 +7,7 @@ module Simplex.Messaging.Server.MsgStore.STM where +import Control.Concurrent.STM.TBQueue (flushTBQueue) import Control.Monad (when) import Data.Int (Int64) import Data.Time.Clock.System (SystemTime (systemSeconds)) @@ -36,6 +37,9 @@ instance MonadMsgStore STMMsgStore MsgQueue STM where delMsgQueue :: STMMsgStore -> RecipientId -> STM () delMsgQueue st rId = TM.delete rId st + flushMsgQueue :: STMMsgStore -> RecipientId -> STM [Message] + flushMsgQueue st rId = TM.lookup rId st >>= maybe (pure []) (flushTBQueue . msgQueue) + instance MonadMsgQueue MsgQueue STM where isFull :: MsgQueue -> STM Bool isFull = isFullTBQueue . msgQueue diff --git a/src/Simplex/Messaging/Transport.hs b/src/Simplex/Messaging/Transport.hs index 6d697b07f..69dfa30d3 100644 --- a/src/Simplex/Messaging/Transport.hs +++ b/src/Simplex/Messaging/Transport.hs @@ -96,7 +96,7 @@ supportedSMPVersions :: VersionRange supportedSMPVersions = mkVersionRange 1 1 simplexMQVersion :: String -simplexMQVersion = "2.2.1" +simplexMQVersion = "2.3.0" -- * Transport connection class diff --git a/tests/SMPClient.hs b/tests/SMPClient.hs index 5e5f7e611..01914aa44 100644 --- a/tests/SMPClient.hs +++ b/tests/SMPClient.hs @@ -43,6 +43,9 @@ testKeyHash = "LcJUMfVhwD8yxjAiSaDzzGF3-kLG4Uh0Fl_ZIjrRwjI=" testStoreLogFile :: FilePath testStoreLogFile = "tests/tmp/smp-server-store.log" +testStoreMsgsFile :: FilePath +testStoreMsgsFile = "tests/tmp/smp-server-messages.log" + testSMPClient :: (Transport c, MonadUnliftIO m) => (THandle c -> m a) -> m a testSMPClient client = runTransportClient testHost testPort (Just testKeyHash) (Just defaultKeepAliveOpts) $ \h -> @@ -60,6 +63,7 @@ cfg = queueIdBytes = 24, msgIdBytes = 24, storeLogFile = Nothing, + storeMsgsFile = Nothing, allowNewQueues = True, messageExpiration = Just defaultMessageExpiration, inactiveClientExpiration = Just defaultInactiveClientExpiration, @@ -70,6 +74,9 @@ cfg = certificateFile = "tests/fixtures/server.crt" } +withSmpServerStoreMsgLogOn :: (MonadUnliftIO m, MonadRandom m) => ATransport -> ServiceName -> (ThreadId -> m a) -> m a +withSmpServerStoreMsgLogOn t = withSmpServerConfigOn t cfg {storeLogFile = Just testStoreLogFile, storeMsgsFile = Just testStoreMsgsFile} + withSmpServerStoreLogOn :: (MonadUnliftIO m, MonadRandom m) => ATransport -> ServiceName -> (ThreadId -> m a) -> m a withSmpServerStoreLogOn t = withSmpServerConfigOn t cfg {storeLogFile = Just testStoreLogFile} diff --git a/tests/ServerTests.hs b/tests/ServerTests.hs index 49614d8e7..4589e1e01 100644 --- a/tests/ServerTests.hs +++ b/tests/ServerTests.hs @@ -42,6 +42,7 @@ serverTests t@(ATransport t') = do describe "duplex communication over 2 SMP connections" $ testDuplex t describe "switch subscription to another TCP connection" $ testSwitchSub t describe "Store log" $ testWithStoreLog t + describe "Restore messages" $ testRestoreMessages t describe "Timing of AUTH error" $ testTiming t describe "Message notifications" $ testMessageNotifications t describe "Message expiration" $ do @@ -352,7 +353,7 @@ testWithStoreLog at@(ATransport t) = Resp "dabc" _ OK <- signSendRecv h rKey2 ("dabc", rId2, DEL) pure () - logSize `shouldReturn` 6 + logSize testStoreLogFile `shouldReturn` 6 withSmpServerThreadOn at testPort . runTest t $ \h -> do sId1 <- readTVarIO senderId1 @@ -377,7 +378,7 @@ testWithStoreLog at@(ATransport t) = Resp "cdab" _ (ERR AUTH) <- signSendRecv h sKey2 ("cdab", sId2, SEND "hello too") pure () - logSize `shouldReturn` 1 + logSize testStoreLogFile `shouldReturn` 1 removeFile testStoreLogFile where runTest :: Transport c => TProxy c -> (THandle c -> IO ()) -> ThreadId -> Expectation @@ -388,11 +389,79 @@ testWithStoreLog at@(ATransport t) = runClient :: Transport c => TProxy c -> (THandle c -> IO ()) -> Expectation runClient _ test' = testSMPClient test' `shouldReturn` () - logSize :: IO Int - logSize = - try (length . B.lines <$> B.readFile testStoreLogFile) >>= \case - Right l -> pure l - Left (_ :: SomeException) -> logSize +logSize :: FilePath -> IO Int +logSize f = + try (length . B.lines <$> B.readFile f) >>= \case + Right l -> pure l + Left (_ :: SomeException) -> logSize f + +testRestoreMessages :: ATransport -> Spec +testRestoreMessages at@(ATransport t) = + it "should store messages on exit and restore on start" $ do + (sPub, sKey) <- C.generateSignatureKeyPair C.SEd25519 + recipientId <- newTVarIO "" + recipientKey <- newTVarIO Nothing + dhShared <- newTVarIO Nothing + senderId <- newTVarIO "" + + withSmpServerStoreMsgLogOn at testPort . runTest t $ \h -> do + runClient t $ \h1 -> do + (sId, rId, rKey, dh) <- createAndSecureQueue h1 sPub + atomically $ do + writeTVar recipientId rId + writeTVar recipientKey $ Just rKey + writeTVar dhShared $ Just dh + writeTVar senderId sId + Resp "1" _ OK <- signSendRecv h sKey ("1", sId, SEND "hello") + Resp "" _ (MSG mId1 _ msg1) <- tGet h1 + Resp "1a" _ OK <- signSendRecv h1 rKey ("1a", rId, ACK) + (C.cbDecrypt dh (C.cbNonce mId1) msg1, Right "hello") #== "message delivered" + -- messages below are delivered after server restart + sId <- readTVarIO senderId + Resp "2" _ OK <- signSendRecv h sKey ("2", sId, SEND "hello 2") + Resp "3" _ OK <- signSendRecv h sKey ("3", sId, SEND "hello 3") + Resp "4" _ OK <- signSendRecv h sKey ("4", sId, SEND "hello 4") + pure () + + logSize testStoreLogFile `shouldReturn` 2 + logSize testStoreMsgsFile `shouldReturn` 3 + + withSmpServerStoreMsgLogOn at testPort . runTest t $ \h -> do + rId <- readTVarIO recipientId + Just rKey <- readTVarIO recipientKey + Just dh <- readTVarIO dhShared + Resp "2" _ (MSG mId2 _ msg2) <- signSendRecv h rKey ("2", rId, SUB) + (C.cbDecrypt dh (C.cbNonce mId2) msg2, Right "hello 2") #== "restored message delivered" + Resp "3" _ (MSG mId3 _ msg3) <- signSendRecv h rKey ("3", rId, ACK) + (C.cbDecrypt dh (C.cbNonce mId3) msg3, Right "hello 3") #== "restored message delivered" + Resp "4" _ (MSG mId4 _ msg4) <- signSendRecv h rKey ("4", rId, ACK) + (C.cbDecrypt dh (C.cbNonce mId4) msg4, Right "hello 4") #== "restored message delivered" + + logSize testStoreLogFile `shouldReturn` 1 + -- the last message is not removed because it was not ACK'd + logSize testStoreMsgsFile `shouldReturn` 1 + + withSmpServerStoreMsgLogOn at testPort . runTest t $ \h -> do + rId <- readTVarIO recipientId + Just rKey <- readTVarIO recipientKey + Just dh <- readTVarIO dhShared + Resp "4" _ (MSG mId4 _ msg4) <- signSendRecv h rKey ("4", rId, SUB) + Resp "5" _ OK <- signSendRecv h rKey ("5", rId, ACK) + (C.cbDecrypt dh (C.cbNonce mId4) msg4, Right "hello 4") #== "restored message delivered" + + logSize testStoreLogFile `shouldReturn` 1 + logSize testStoreMsgsFile `shouldReturn` 0 + + removeFile testStoreLogFile + removeFile testStoreMsgsFile + where + runTest :: Transport c => TProxy c -> (THandle c -> IO ()) -> ThreadId -> Expectation + runTest _ test' server = do + testSMPClient test' `shouldReturn` () + killThread server + + runClient :: Transport c => TProxy c -> (THandle c -> IO ()) -> Expectation + runClient _ test' = testSMPClient test' `shouldReturn` () createAndSecureQueue :: Transport c => THandle c -> SndPublicVerifyKey -> IO (SenderId, RecipientId, RcvPrivateSignKey, RcvDhSecret) createAndSecureQueue h sPub = do From 62485b936760f058e3fa5b319d3af713c854daf1 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Mon, 13 Jun 2022 08:27:08 +0100 Subject: [PATCH 052/131] re-deliver duplicate message if it was not acknowledged by the user, ignore otherwise (#397) * re-deliver duplicate message if it was not acknowledged by the user, ignore otherwise * delete message after ACK --- src/Simplex/Messaging/Agent.hs | 29 ++++++--- src/Simplex/Messaging/Agent/Client.hs | 2 +- src/Simplex/Messaging/Agent/Store.hs | 12 +++- src/Simplex/Messaging/Agent/Store/SQLite.hs | 29 ++++++--- .../Store/SQLite/Migrations/M20220608_v2.hs | 2 + .../Store/SQLite/Migrations/agent_schema.sql | 1 + tests/AgentTests/FunctionalAPITests.hs | 61 ++++++++++++++++++- 7 files changed, 116 insertions(+), 20 deletions(-) diff --git a/src/Simplex/Messaging/Agent.hs b/src/Simplex/Messaging/Agent.hs index 6758654e0..de1afe2ae 100644 --- a/src/Simplex/Messaging/Agent.hs +++ b/src/Simplex/Messaging/Agent.hs @@ -524,7 +524,7 @@ ackMessage' c connId msgId = do ack :: RcvQueue -> m () ack rq = do let mId = InternalId msgId - srvMsgId <- withStore $ \st -> checkRcvMsg st connId mId + srvMsgId <- withStore $ \st -> setMsgUserAck st connId mId sendAck c rq srvMsgId `catchError` \case SMP SMP.NO_MSG -> pure () e -> throwError e @@ -744,15 +744,29 @@ processSMPTransmission c@AgentClient {smpClients, subQ} (srv, sessId, rId, cmd) (Just e2eDh, Nothing) -> do decryptClientMessage e2eDh clientMsg >>= \case (SMP.PHEmpty, AgentMsgEnvelope _ encAgentMsg) -> - agentClientMsg >>= \case - Just (msgId, msgMeta, aMessage) -> case aMessage of + tryError agentClientMsg >>= \case + Right (Just (msgId, msgMeta, aMessage)) -> case aMessage of HELLO -> helloMsg >> ack >> withStore (\st -> deleteMsg st connId msgId) REPLY cReq -> replyMsg cReq >> ack >> withStore (\st -> deleteMsg st connId msgId) -- note that there is no ACK sent for A_MSG, it is sent with agent's user ACK command A_MSG body -> do logServer "<--" c srv rId "MSG " notify $ MSG msgMeta msgFlags body - _ -> prohibited >> ack + Right _ -> prohibited >> ack + Left e@(AGENT A_DUPLICATE) -> do + withStore (\st -> getLastMsg st connId srvMsgId) >>= \case + Just RcvMsg {internalId, msgMeta, msgBody = agentMsgBody, userAck} + | userAck -> do + ack + withStore $ \st -> deleteMsg st connId internalId + | otherwise -> do + liftEither (parse smpP (AGENT A_MESSAGE) agentMsgBody) >>= \case + AgentMessage _ (A_MSG body) -> do + logServer "<--" c srv rId "MSG " + notify $ MSG msgMeta msgFlags body + _ -> pure () + _ -> throwError e + Left e -> throwError e where agentClientMsg :: m (Maybe (InternalId, MsgMeta, AMessage)) agentClientMsg = withStore $ \st -> do @@ -767,7 +781,7 @@ processSMPTransmission c@AgentClient {smpClients, subQ} (srv, sessId, rId, cmd) recipient = (unId internalId, internalTs) broker = (srvMsgId, systemToUTCTime srvTs) msgMeta = MsgMeta {integrity, recipient, broker, sndMsgId} - rcvMsg = RcvMsgData {msgMeta, msgType, msgFlags, msgBody, internalRcvId, internalHash, externalPrevSndHash = prevMsgHash} + rcvMsg = RcvMsgData {msgMeta, msgType, msgFlags, msgBody = agentMsgBody, internalRcvId, internalHash, externalPrevSndHash = prevMsgHash} createRcvMsg st connId rcvMsg pure $ Just (internalId, msgMeta, aMessage) _ -> pure Nothing @@ -780,10 +794,7 @@ processSMPTransmission c@AgentClient {smpClients, subQ} (srv, sessId, rId, cmd) SMP SMP.NO_MSG -> pure () e -> throwError e handleNotifyAck :: m () -> m () - handleNotifyAck m = - m `catchError` \case - AGENT A_DUPLICATE -> ack - e -> notify (ERR e) >> ack + handleNotifyAck m = m `catchError` \e -> notify (ERR e) >> ack SMP.END -> atomically (TM.lookup srv smpClients $>>= tryReadTMVar >>= processEND) >>= logServer "<--" c srv rId diff --git a/src/Simplex/Messaging/Agent/Client.hs b/src/Simplex/Messaging/Agent/Client.hs index d7eba7f58..ef86528fe 100644 --- a/src/Simplex/Messaging/Agent/Client.hs +++ b/src/Simplex/Messaging/Agent/Client.hs @@ -574,5 +574,5 @@ cryptoError = \case C.CryptoHeaderError _ -> AGENT A_ENCRYPTION C.AESDecryptError -> AGENT A_ENCRYPTION C.CBDecryptError -> AGENT A_ENCRYPTION - -- C.CERatchetDuplicateMessage -> AGENT A_DUPLICATE + C.CERatchetDuplicateMessage -> AGENT A_DUPLICATE e -> INTERNAL $ show e diff --git a/src/Simplex/Messaging/Agent/Store.hs b/src/Simplex/Messaging/Agent/Store.hs index 7b642fb90..5fd7dfa4f 100644 --- a/src/Simplex/Messaging/Agent/Store.hs +++ b/src/Simplex/Messaging/Agent/Store.hs @@ -70,7 +70,8 @@ class Monad m => MonadAgentStore s m where createSndMsg :: s -> ConnId -> SndMsgData -> m () getPendingMsgData :: s -> ConnId -> InternalId -> m (Maybe RcvQueue, PendingMsgData) getPendingMsgs :: s -> ConnId -> m [InternalId] - checkRcvMsg :: s -> ConnId -> InternalId -> m MsgId + setMsgUserAck :: s -> ConnId -> InternalId -> m MsgId + getLastMsg :: s -> ConnId -> SMP.MsgId -> m (Maybe RcvMsg) deleteMsg :: s -> ConnId -> InternalId -> m () -- Double ratchet persistence @@ -247,7 +248,7 @@ type PrevRcvMsgHash = MsgHash -- | Corresponds to `last_snd_msg_hash` in `connections` table type PrevSndMsgHash = MsgHash --- * Message data containers - used on Msg creation to reduce number of parameters +-- * Message data containers data RcvMsgData = RcvMsgData { msgMeta :: MsgMeta, @@ -259,6 +260,13 @@ data RcvMsgData = RcvMsgData externalPrevSndHash :: MsgHash } +data RcvMsg = RcvMsg + { internalId :: InternalId, + msgMeta :: MsgMeta, + msgBody :: MsgBody, + userAck :: Bool + } + data SndMsgData = SndMsgData { internalId :: InternalId, internalSndId :: InternalSndId, diff --git a/src/Simplex/Messaging/Agent/Store/SQLite.hs b/src/Simplex/Messaging/Agent/Store/SQLite.hs index 968f86f1f..6fcd49ffc 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite.hs @@ -498,18 +498,33 @@ instance (MonadUnliftIO m, MonadError StoreError m) => MonadAgentStore SQLiteSto map fromOnly <$> DB.query db "SELECT internal_id FROM snd_messages WHERE conn_id = ?" (Only connId) - checkRcvMsg :: SQLiteStore -> ConnId -> InternalId -> m SMP.MsgId - checkRcvMsg st connId msgId = - liftIOEither . withTransaction st $ \db -> + setMsgUserAck :: SQLiteStore -> ConnId -> InternalId -> m SMP.MsgId + setMsgUserAck st connId agentMsgId = + liftIOEither . withTransaction st $ \db -> do + DB.execute db "UPDATE rcv_messages SET user_ack = ? WHERE conn_id = ? AND internal_id = ?" (True, connId, agentMsgId) firstRow fromOnly SEMsgNotFound $ - DB.query + DB.query db "SELECT broker_id FROM rcv_messages WHERE conn_id = ? AND internal_id = ?" (connId, agentMsgId) + + getLastMsg :: SQLiteStore -> ConnId -> SMP.MsgId -> m (Maybe RcvMsg) + getLastMsg st connId msgId = + liftIO . withTransaction st $ \db -> + fmap rcvMsg . listToMaybe + <$> DB.query db [sql| - SELECT broker_id - FROM rcv_messages - WHERE conn_id = ? AND internal_id = ? + SELECT + r.internal_id, m.internal_ts, r.broker_id, r.broker_ts, r.external_snd_id, r.integrity, + m.msg_body, r.user_ack + FROM rcv_messages r + JOIN messages m ON r.internal_id = m.internal_id + JOIN connections c ON r.conn_id = c.conn_id AND c.last_internal_msg_id = r.internal_id + WHERE r.conn_id = ? AND r.broker_id = ? |] (connId, msgId) + where + rcvMsg (agentMsgId, internalTs, brokerId, brokerTs, sndMsgId, integrity, msgBody, userAck) = + let msgMeta = MsgMeta {recipient = (agentMsgId, internalTs), broker = (brokerId, brokerTs), sndMsgId, integrity} + in RcvMsg {internalId = InternalId agentMsgId, msgMeta, msgBody, userAck} deleteMsg :: SQLiteStore -> ConnId -> InternalId -> m () deleteMsg st connId msgId = diff --git a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220608_v2.hs b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220608_v2.hs index 334f10555..eb01275a5 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220608_v2.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220608_v2.hs @@ -13,4 +13,6 @@ ALTER TABLE messages ADD COLUMN msg_flags TEXT NULL; ALTER TABLE conn_confirmations ADD COLUMN smp_reply_queues BLOB NULL; ALTER TABLE connections ADD COLUMN duplex_handshake INTEGER NULL DEFAULT 0; + +ALTER TABLE rcv_messages ADD COLUMN user_ack INTEGER NULL DEFAULT 0; |] diff --git a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql index a016c1dae..bd24a4985 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql +++ b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql @@ -83,6 +83,7 @@ CREATE TABLE rcv_messages( internal_hash BLOB NOT NULL, external_prev_snd_hash BLOB NOT NULL, integrity BLOB NOT NULL, + user_ack INTEGER NULL DEFAULT 0, PRIMARY KEY(conn_id, internal_rcv_id), FOREIGN KEY(conn_id, internal_id) REFERENCES messages ON DELETE CASCADE diff --git a/tests/AgentTests/FunctionalAPITests.hs b/tests/AgentTests/FunctionalAPITests.hs index 35ac8b229..656ca6f99 100644 --- a/tests/AgentTests/FunctionalAPITests.hs +++ b/tests/AgentTests/FunctionalAPITests.hs @@ -13,7 +13,7 @@ import Control.Monad.Except (ExceptT, runExceptT) import Control.Monad.IO.Unlift import Data.Time.Clock.System (SystemTime (..), getSystemTime) import SMPAgentClient -import SMPClient (cfg, testPort, withSmpServer, withSmpServerConfigOn, withSmpServerStoreLogOn) +import SMPClient (cfg, testPort, withSmpServer, withSmpServerConfigOn, withSmpServerStoreLogOn, withSmpServerStoreMsgLogOn) import Simplex.Messaging.Agent import Simplex.Messaging.Agent.Env.SQLite (AgentConfig (..)) import Simplex.Messaging.Agent.Protocol @@ -74,6 +74,9 @@ functionalAPITests t = do testAsyncServerOffline t it "should notify after HELLO timeout" $ withSmpServer t testAsyncHelloTimeout + describe "Duplicate message delivery" $ + it "should deliver messages to the user once, even if repeat delivery is made by the server (no ACK)" $ + testDuplicateMessage t describe "Inactive client disconnection" $ do it "should disconnect clients if it was inactive longer than TTL" $ testInactiveClientDisconnected t @@ -306,6 +309,62 @@ testAsyncHelloTimeout = do get bob ##> ("", aliceId, ERR $ CONN NOT_ACCEPTED) pure () +testDuplicateMessage :: ATransport -> IO () +testDuplicateMessage t = do + alice <- getSMPAgentClient agentCfg initAgentServers + bob <- getSMPAgentClient agentCfg {dbFile = testDB2} initAgentServers + (aliceId, bobId, bob1) <- withSmpServerStoreMsgLogOn t testPort $ \_ -> do + Right (aliceId, bobId) <- runExceptT $ do + (bobId, qInfo) <- createConnection alice SCMInvitation + aliceId <- joinConnection bob qInfo "bob's connInfo" + ("", _, CONF confId "bob's connInfo") <- get alice + allowConnection alice bobId confId "alice's connInfo" + get alice ##> ("", bobId, CON) + get bob ##> ("", aliceId, INFO "alice's connInfo") + get bob ##> ("", aliceId, CON) + 4 <- sendMessage alice bobId SMP.noMsgFlags "hello" + get alice ##> ("", bobId, SENT 4) + get bob =##> \case ("", c, Msg "hello") -> c == aliceId; _ -> False + pure (aliceId, bobId) + disconnectAgentClient bob + + -- if the agent user did not send ACK, the message will be delivered again + bob1 <- getSMPAgentClient agentCfg {dbFile = testDB2} initAgentServers + Right () <- runExceptT $ do + subscribeConnection bob1 aliceId + get bob1 =##> \case ("", c, Msg "hello") -> c == aliceId; _ -> False + ackMessage bob1 aliceId 4 + 5 <- sendMessage alice bobId SMP.noMsgFlags "hello 2" + get alice ##> ("", bobId, SENT 5) + get bob1 =##> \case ("", c, Msg "hello 2") -> c == aliceId; _ -> False + + pure (aliceId, bobId, bob1) + + get alice =##> \case ("", "", DOWN _ [c]) -> c == bobId; _ -> False + get bob1 =##> \case ("", "", DOWN _ [c]) -> c == aliceId; _ -> False + -- commenting two lines below and uncommenting further two lines would also pass, + -- it is the scenario tested above, when the message was not acknowledged by the user + threadDelay 200000 + Left (BROKER TIMEOUT) <- runExceptT $ ackMessage bob1 aliceId 5 + + disconnectAgentClient alice + disconnectAgentClient bob1 + + alice2 <- getSMPAgentClient agentCfg initAgentServers + bob2 <- getSMPAgentClient agentCfg {dbFile = testDB2} initAgentServers + + withSmpServerStoreMsgLogOn t testPort $ \_ -> do + Right () <- runExceptT $ do + subscribeConnection bob2 aliceId + subscribeConnection alice2 bobId + -- get bob2 =##> \case ("", c, Msg "hello 2") -> c == aliceId; _ -> False + -- ackMessage bob2 aliceId 5 + -- message 2 is not delivered again, even though it was delivered to the agent + 6 <- sendMessage alice2 bobId SMP.noMsgFlags "hello 3" + get alice2 ##> ("", bobId, SENT 6) + get bob2 =##> \case ("", c, Msg "hello 3") -> c == aliceId; _ -> False + pure () + testInactiveClientDisconnected :: ATransport -> IO () testInactiveClientDisconnected t = do let cfg' = cfg {inactiveClientExpiration = Just ExpirationConfig {ttl = 1, checkInterval = 1}} From 68c2682e70895d378397d93cdf314f1c52d12d6d Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Tue, 14 Jun 2022 10:27:45 +0100 Subject: [PATCH 053/131] ios: synchronizing processing between app & nse (#398) * ios: synchronizing processing between app & nse * update rfc * add sequence diagram * manage agent phase * track agent operation and phase changes * update * remove APInactive * Update src/Simplex/Messaging/Agent/Protocol.hs Co-authored-by: JRoberts <8711996+jr-simplex@users.noreply.github.com> --- rfcs/2022-06-13-db-sync.md | 75 +++++++++ rfcs/2022-06-13-db-sync.mmd | 61 +++++++ rfcs/{ => done}/2021-05-17-open-connection.md | 0 .../2022-03-22-nofication-server.md | 0 .../2022-06-05-smp-notifications.md | 0 rfcs/{ => rejected}/protocol_overview.md | 0 src/Simplex/Messaging/Agent.hs | 155 ++++++++++-------- src/Simplex/Messaging/Agent/Client.hs | 63 ++++++- src/Simplex/Messaging/Agent/Env/SQLite.hs | 47 ++---- .../Messaging/Agent/NtfSubSupervisor.hs | 14 +- src/Simplex/Messaging/Agent/Protocol.hs | 25 +++ 11 files changed, 333 insertions(+), 107 deletions(-) create mode 100644 rfcs/2022-06-13-db-sync.md create mode 100644 rfcs/2022-06-13-db-sync.mmd rename rfcs/{ => done}/2021-05-17-open-connection.md (100%) rename rfcs/{ => done}/2022-03-22-nofication-server.md (100%) rename rfcs/{ => done}/2022-06-05-smp-notifications.md (100%) rename rfcs/{ => rejected}/protocol_overview.md (100%) diff --git a/rfcs/2022-06-13-db-sync.md b/rfcs/2022-06-13-db-sync.md new file mode 100644 index 000000000..dc375e20f --- /dev/null +++ b/rfcs/2022-06-13-db-sync.md @@ -0,0 +1,75 @@ +# DB access and processing messages for iOS notification service extension + +## Problem + +The only way to receive/process notificaitons is via a separate NSE process that requires a concurrent DB and network access. + +SQLite concurenncy does not work, so we need to sync database access. + +The problem is complex, as we do not directly control db access from the app, it can be triggered by the message arriving and it may fail to complete in case the app is suspended in the background. + +So we need to prevent db access from starting when we know the app is about to be suspended. + +The last problem is how to receive and process messages in NSE - should it use recently added GET command or should it subscribe to the connections that receive messages and process messages normally. + +To summarize, 2 problems need to be solved: + +1. sync db access between 2 (or more, if we add share extension) processes + +2. prevent access from starting when the process is due to suspend, only complete operations. + +3. Receiveing and processing agent messages in NSE + +## Proposed solution + +For problem 1, we can use Posix semaphores from our core code, in the same bracket that manages database connection - it would wait for semaphore to be free and unlock it once the db operation is complete. + +For problem 2, we need to communicate from the app when it goes to the background to prevent database access starting and completing before the suspension. This would set some `aboutToSuspend` STM flag (or the opposite) that would prevent operations from progressing (using STM retry, that would block until the flag has the value allowing operation to progress). + +Several possibilities can be considered: + +- use this flag in the bracket that provides DB connections. While simple, it may lock some operations in the middle and may also lead to the situation when network operation succeeds but database access was blocked, and the database is not updated. +- use this flag to stop network operations that would require database updates - like sending messages, subscriptions and ACK - all these operations would require database access once they succeed. +- use two flags, for both cases above, but set them at different times since going to background - block new network operations as soon as the app goes to the background and block database access once the app is about to be suspended. + +The last option seems more robust. To do it, there will be an api allowing the app to communicat its phase: + +- app going to the background would trigger blocking new network operations and start a new background task - `background` phase. +- background task receiving completion warning, or, maybe, some time after it is started - probably 20 seconds - or whatever happens earlier - would trigger call blocking db access - `suspended` phase. +- app becoming active would trigger unblocking both flags - `active` phase. + +`/_app phase ` where `phase` can be one of the above values. + +NSE would also use the same phases: + +- sending `active` when it is started (the process starts as active, but it is possible that the new notification arrives to the same process, after the previous one sent background/suspension) +- sending `background + suspended` (or `suspended` should set both flags) once it is finished processing the notification, provided no new notification arrived and started processing - this should be tracked in NSE. + +For problem 3, NSE can do one of the following: + +- use SUB and process messages normally - the downside is that the app will have to resubscribe and it has to be tracked. +- use GET and process messages by pushing them through the processing function - the downside it that a rewiring of message processing is needed. +- use GET but deliver messages through the same queue as when they arrive normally (in which case getMessage agent function should not return the message, but will return a flag showing whether the message was received, or, possibly, or, possibly will return a message but the message would also be sent to the queue?). +- process messages in agent manually and in chat via the queue. + +One of the downside of GET is that it requires calling GET again after ACK. We could have two variants of ACK (or additional ACK parameter) - one that never delivers a new message, and another one that does. In this case, if get needs to process the next message (when the current one has no notification flag), it can call ACK that delivers the next message. But, it is probably a premature optimization, and having general support of batched commands would add more value. + +Additional problem is concurrency in NSE - if the new notification from the same queue arrives before the current one finishes processing in the same process one of the following can happen: + +- the 2nd notification naively call GET and receives the same message. +- the 2nd notification waits until the first finished processing, in which case it can run out of time. + +The problem is that the app won't know it's the same queue, as nId is encrypted, so the agent should handle this scenario when the new call to getMessage is made before the previous one finished processing, and differentiate between calls made for additional messages (possibly, getMessage should include previous message ID, if it is available) and the first call. + +EDIT: GETs have to be sent from UI to chat and from chat to agent as function calls, but the agent will have to queue get calls to make sure they return different messages. GET call would return message flags (incl. notification flag), so that the UI can send the next GET if needed without waiting. + +Considered alternative: include notification content in the message and have NSE only perform decryption, without any network IO. In this case notification content would be in SEND and in NMSG, e2e encrypted. + +While promising, as it solves network coordination issues and makes GET unnecessary, it creates mutliple other problems, so it was rejected: + +- message content is exposed to centralized ntf and apns servers, creating additional attack vector. +- it adds complexity in security critical parts of the stack - double ratchet encryption, as it requires either storing message keys and using different IVs for notifications, or initializing completely separate ratchet for notifications content. +- it reduces the size of the message. +- it makes user experience worse, as: + - it would not accelerate handshake for new contacts and for file delivery - this approach only works for content messages. + - it would open the app without the new messages - the users would have to wait until the messages are received. It is also bad for "security optics" - the users might think that the message content was exposed to notifications. diff --git a/rfcs/2022-06-13-db-sync.mmd b/rfcs/2022-06-13-db-sync.mmd new file mode 100644 index 000000000..022f57d65 --- /dev/null +++ b/rfcs/2022-06-13-db-sync.mmd @@ -0,0 +1,61 @@ +sequenceDiagram + participant M as iOS message
notification + participant S as iOS system + participant N as iOS NSE + participant U as iOS UI + participant C as Core chat + participant A as Core agent + + M ->> N: notification + S ->> N: get app pref + note over N: ignore,
app is active + + note over M, A: app going to background + S ->> U: phase: background
(possibly, "will" method) + U ->> S: set app pref "pausing" + U ->> C: /_app phase paused, result CRCmdOk + C ->> A: pauseAgent
(no new network IO) + M ->> N: notification + S ->> N: get app pref + note over N: wait/poll for
"paused"/"suspending"/"suspended"
event/pref + A ->> C: event "IO paused"
(after in-flight op completed)
PHASE PAUSED + C ->> U: event "IO paused" (CRAppPaused) + U ->> S: set shared pref "paused" + + note over M, A: process notification + M ->> N: notification + S ->> N: get app pref
continue if
"paused"/"suspending"/"suspended" + N ->> S: set NSE pref "active" + N ->> C: /_get message + C ->> A: getMessage + A ->> C: msg flags + C ->> N: msg flags + note over N: get messages
until notification flag set + A ->> C: MSG/CONF/INFO + C ->> N: some event + N ->> S: set NSE pref "completed" + N ->> S: show notification + + note over M, A: app about to be suspended
(or 15-20 sec after background) + S ->> U: background task notice + U ->> S: set app pref "suspending" + U ->> C: /_app phase suspended, response ok + C ->> A: suspendAgent
(no new DB) + A ->> C: event "DB paused"
(after in-flight op completed)
PHASE SUSPENDED + C ->> U: event "DB paused" (CRAppSuspended) + U ->> S: set app pref "suspended" + + note over M, A: app about to be activated + S ->> U: phase: active
(or inactive?)

(possibly, "will" method) + S ->> U: get NSE pref + U ->> S: set app pref "activating" + alt nse active? + U ->> C: /_app phase inactive + note over U: poll/wait till NSE pref is "completed" + end + + U ->> C: /_app phase active (response result) + C ->> A: activateAgent
(allow IO/DB) + A ->> C: result () + C ->> U: CRCmdOk + U ->> S: set app pref "active" diff --git a/rfcs/2021-05-17-open-connection.md b/rfcs/done/2021-05-17-open-connection.md similarity index 100% rename from rfcs/2021-05-17-open-connection.md rename to rfcs/done/2021-05-17-open-connection.md diff --git a/rfcs/2022-03-22-nofication-server.md b/rfcs/done/2022-03-22-nofication-server.md similarity index 100% rename from rfcs/2022-03-22-nofication-server.md rename to rfcs/done/2022-03-22-nofication-server.md diff --git a/rfcs/2022-06-05-smp-notifications.md b/rfcs/done/2022-06-05-smp-notifications.md similarity index 100% rename from rfcs/2022-06-05-smp-notifications.md rename to rfcs/done/2022-06-05-smp-notifications.md diff --git a/rfcs/protocol_overview.md b/rfcs/rejected/protocol_overview.md similarity index 100% rename from rfcs/protocol_overview.md rename to rfcs/rejected/protocol_overview.md diff --git a/src/Simplex/Messaging/Agent.hs b/src/Simplex/Messaging/Agent.hs index de1afe2ae..609d7bbff 100644 --- a/src/Simplex/Messaging/Agent.hs +++ b/src/Simplex/Messaging/Agent.hs @@ -56,6 +56,7 @@ module Simplex.Messaging.Agent enableNtfCron, checkNtfToken, deleteNtfToken, + setAgentPhase, logConnection, ) where @@ -191,6 +192,9 @@ checkNtfToken c = withAgentEnv c . checkNtfToken' c deleteNtfToken :: AgentErrorMonad m => AgentClient -> DeviceToken -> m () deleteNtfToken c = withAgentEnv c . deleteNtfToken' c +setAgentPhase :: AgentErrorMonad m => AgentClient -> AgentPhase -> m () +setAgentPhase c = withAgentEnv c . setAgentPhase' c + withAgentEnv :: AgentClient -> ReaderT Env m a -> m a withAgentEnv c = (`runReaderT` agentEnv c) @@ -239,7 +243,7 @@ newConn c connId cMode = do g <- asks idsDrg agentVersion <- asks $ smpAgentVersion . config let cData = ConnData {connId, connAgentVersion = agentVersion, duplexHandshake = Nothing} -- connection mode is determined by the accepting agent - connId' <- withStore $ \st -> createRcvConn st g cData rq cMode + connId' <- withStore c $ \st -> createRcvConn st g cData rq cMode addSubscription c rq connId' ns <- asks ntfSupervisor atomically $ sendNtfSubCommand ns (rq, NSCCreate) @@ -249,7 +253,7 @@ newConn c connId cMode = do SCMContact -> pure (connId', CRContactUri crData) SCMInvitation -> do (pk1, pk2, e2eRcvParams) <- liftIO $ CR.generateE2EParams CR.e2eEncryptVersion - withStore $ \st -> createRatchetX3dhKeys st connId' pk1 pk2 + withStore c $ \st -> createRatchetX3dhKeys st connId' pk1 pk2 pure (connId', CRInvitationUri crData $ toVersionRangeT e2eRcvParams CR.e2eEncryptVRange) joinConn :: AgentMonad m => AgentClient -> ConnId -> ConnectionRequestUri c -> ConnInfo -> m ConnId @@ -268,7 +272,7 @@ joinConn c connId (CRInvitationUri (ConnReqUriData _ agentVRange (qUri :| _)) e2 g <- asks idsDrg let duplexHS = connAgentVersion /= 1 cData = ConnData {connId, connAgentVersion, duplexHandshake = Just duplexHS} - connId' <- withStore $ \st -> do + connId' <- withStore c $ \st -> do connId' <- createSndConn st g cData sq createRatchet st connId' rc pure connId' @@ -279,7 +283,7 @@ joinConn c connId (CRInvitationUri (ConnReqUriData _ agentVRange (qUri :| _)) e2 pure connId' Left e -> do -- TODO recovery for failure on network timeout, see rfcs/2022-04-20-smp-conf-timeout-recovery.md - withStore (`deleteConn` connId') + withStore c (`deleteConn` connId') throwError e _ -> throwError $ AGENT A_VERSION joinConn c connId (CRContactUri (ConnReqUriData _ agentVRange (qUri :| _))) cInfo = do @@ -301,16 +305,16 @@ createReplyQueue c connId = do -- TODO reply queue version should be the same as send queue, ignoring it in v1 let qInfo = toVersionT qUri SMP.smpClientVersion addSubscription c rq connId - withStore $ \st -> upgradeSndConnToDuplex st connId rq + withStore c $ \st -> upgradeSndConnToDuplex st connId rq pure qInfo -- | Approve confirmation (LET command) in Reader monad allowConnection' :: AgentMonad m => AgentClient -> ConnId -> ConfirmationId -> ConnInfo -> m () allowConnection' c connId confId ownConnInfo = do - withStore (`getConn` connId) >>= \case + withStore c (`getConn` connId) >>= \case SomeConn _ (RcvConnection cData rq) -> do - AcceptedConfirmation {senderConf, ratchetState} <- withStore $ \st -> acceptConfirmation st confId ownConnInfo - withStore $ \st -> createRatchet st connId ratchetState + AcceptedConfirmation {senderConf, ratchetState} <- withStore c $ \st -> acceptConfirmation st confId ownConnInfo + withStore c $ \st -> createRatchet st connId ratchetState processConfirmation c rq senderConf mapM_ (connectReplyQueues c cData ownConnInfo) (L.nonEmpty $ smpReplyQueues senderConf) _ -> throwError $ CMD PROHIBITED @@ -318,29 +322,29 @@ allowConnection' c connId confId ownConnInfo = do -- | Accept contact (ACPT command) in Reader monad acceptContact' :: AgentMonad m => AgentClient -> ConnId -> InvitationId -> ConnInfo -> m ConnId acceptContact' c connId invId ownConnInfo = do - Invitation {contactConnId, connReq} <- withStore (`getInvitation` invId) - withStore (`getConn` contactConnId) >>= \case + Invitation {contactConnId, connReq} <- withStore c (`getInvitation` invId) + withStore c (`getConn` contactConnId) >>= \case SomeConn _ ContactConnection {} -> do - withStore $ \st -> acceptInvitation st invId ownConnInfo + withStore c $ \st -> acceptInvitation st invId ownConnInfo joinConn c connId connReq ownConnInfo _ -> throwError $ CMD PROHIBITED -- | Reject contact (RJCT command) in Reader monad rejectContact' :: AgentMonad m => AgentClient -> ConnId -> InvitationId -> m () -rejectContact' _ contactConnId invId = - withStore $ \st -> deleteInvitation st contactConnId invId +rejectContact' c contactConnId invId = + withStore c $ \st -> deleteInvitation st contactConnId invId processConfirmation :: AgentMonad m => AgentClient -> RcvQueue -> SMPConfirmation -> m () processConfirmation c rq@RcvQueue {e2ePrivKey} SMPConfirmation {senderKey, e2ePubKey} = do let dhSecret = C.dh' e2ePubKey e2ePrivKey - withStore $ \st -> setRcvQueueConfirmedE2E st rq dhSecret + withStore c $ \st -> setRcvQueueConfirmedE2E st rq dhSecret secureQueue c rq senderKey - withStore $ \st -> setRcvQueueStatus st rq Secured + withStore c $ \st -> setRcvQueueStatus st rq Secured -- | Subscribe to receive connection messages (SUB command) in Reader monad subscribeConnection' :: forall m. AgentMonad m => AgentClient -> ConnId -> m () subscribeConnection' c connId = - withStore (`getConn` connId) >>= \case + withStore c (`getConn` connId) >>= \case SomeConn _ (DuplexConnection cData rq sq) -> do resumeMsgDelivery c cData sq subscribe rq @@ -368,7 +372,7 @@ resubscribeConnection' c connId = -- | Send message to the connection (SEND command) in Reader monad sendMessage' :: forall m. AgentMonad m => AgentClient -> ConnId -> MsgFlags -> MsgBody -> m AgentMsgId sendMessage' c connId msgFlags msg = - withStore (`getConn` connId) >>= \case + withStore c (`getConn` connId) >>= \case SomeConn _ (DuplexConnection cData _ sq) -> enqueueMsg cData sq SomeConn _ (SndConnection cData sq) -> enqueueMsg cData sq _ -> throwError $ CONN SIMPLEX @@ -384,7 +388,7 @@ enqueueMessage c cData@ConnData {connId, connAgentVersion} sq msgFlags aMessage pure $ unId msgId where storeSentMsg :: m InternalId - storeSentMsg = withStore $ \st -> do + storeSentMsg = withStore c $ \st -> do internalTs <- liftIO getCurrentTime (internalId, internalSndId, prevMsgHash) <- updateSndIds st connId let privHeader = APrivHeader (unSndId internalSndId) prevMsgHash @@ -405,7 +409,7 @@ resumeMsgDelivery c cData@ConnData {connId} sq@SndQueue {server, sndId} = do async (runSmpQueueMsgDelivery c cData sq) >>= \a -> atomically (TM.insert qKey a $ smpQueueMsgDeliveries c) unlessM connQueued $ - withStore (`getPendingMsgs` connId) + withStore c (`getPendingMsgs` connId) >>= queuePendingMsgs c connId sq where queueDelivering qKey = atomically $ TM.member qKey (smpQueueMsgDeliveries c) @@ -431,9 +435,11 @@ runSmpQueueMsgDelivery c@AgentClient {subQ} cData@ConnData {connId, duplexHandsh mq <- atomically $ getPendingMsgQ c connId sq ri <- asks $ reconnectInterval . config forever $ do + atomically $ endAgentOperation c AONetwork msgId <- atomically $ readTQueue mq + atomically $ beginAgentOperation c AONetwork let mId = unId msgId - withStore (\st -> E.try $ getPendingMsgData st connId msgId) >>= \case + withStore c (\st -> E.try $ getPendingMsgData st connId msgId) >>= \case Left (e :: E.SomeException) -> notify $ MERR mId (INTERNAL $ show e) Right (rq_, PendingMsgData {msgType, msgBody, msgFlags, internalTs}) -> @@ -448,7 +454,7 @@ runSmpQueueMsgDelivery c@AgentClient {subQ} cData@ConnData {connId, duplexHandsh SMP SMP.QUOTA -> case msgType of AM_CONN_INFO -> connError msgId NOT_AVAILABLE AM_CONN_INFO_REPLY -> connError msgId NOT_AVAILABLE - _ -> loop + _ -> retrySending loop SMP SMP.AUTH -> case msgType of AM_CONN_INFO -> connError msgId NOT_AVAILABLE AM_CONN_INFO_REPLY -> connError msgId NOT_AVAILABLE @@ -461,7 +467,7 @@ runSmpQueueMsgDelivery c@AgentClient {subQ} cData@ConnData {connId, duplexHandsh currentTime <- liftIO getCurrentTime if diffUTCTime currentTime internalTs > helloTimeout then connErr - else loop + else retrySending loop where connErr = case rq_ of -- party initiating connection @@ -472,17 +478,17 @@ runSmpQueueMsgDelivery c@AgentClient {subQ} cData@ConnData {connId, duplexHandsh AM_A_MSG_ -> notifyDel msgId $ MERR mId e SMP (SMP.CMD _) -> notifyDel msgId err SMP SMP.LARGE_MSG -> notifyDel msgId err - SMP {} -> notify err >> loop - _ -> loop + SMP {} -> notify err >> retrySending loop + _ -> retrySending loop Right () -> do case msgType of AM_CONN_INFO -> do - withStore $ \st -> setSndQueueStatus st sq Confirmed - when (isJust rq_) $ withStore (`removeConfirmations` connId) + withStore c $ \st -> setSndQueueStatus st sq Confirmed + when (isJust rq_) $ withStore c (`removeConfirmations` connId) -- TODO possibly notification flag should be ON for one of the parties, to result in contact connected notification unless (duplexHandshake == Just True) . void $ enqueueMessage c cData sq SMP.noMsgFlags HELLO AM_HELLO_ -> do - withStore $ \st -> setSndQueueStatus st sq Active + withStore c $ \st -> setSndQueueStatus st sq Active case rq_ of -- party initiating connection (in v1) Just RcvQueue {status} -> @@ -507,16 +513,21 @@ runSmpQueueMsgDelivery c@AgentClient {subQ} cData@ConnData {connId, duplexHandsh delMsg msgId where delMsg :: InternalId -> m () - delMsg msgId = withStore $ \st -> deleteMsg st connId msgId + delMsg msgId = withStore c $ \st -> deleteMsg st connId msgId notify :: ACommand 'Agent -> m () notify cmd = atomically $ writeTBQueue subQ ("", connId, cmd) notifyDel :: InternalId -> ACommand 'Agent -> m () notifyDel msgId cmd = notify cmd >> delMsg msgId connError msgId = notifyDel msgId . ERR . CONN + retrySending loop = do + atomically $ do + endAgentOperation c AONetwork + beginAgentOperation c AONetwork + loop ackMessage' :: forall m. AgentMonad m => AgentClient -> ConnId -> AgentMsgId -> m () ackMessage' c connId msgId = do - withStore (`getConn` connId) >>= \case + withStore c (`getConn` connId) >>= \case SomeConn _ (DuplexConnection _ rq _) -> ack rq SomeConn _ (RcvConnection _ rq) -> ack rq _ -> throwError $ CONN SIMPLEX @@ -524,16 +535,16 @@ ackMessage' c connId msgId = do ack :: RcvQueue -> m () ack rq = do let mId = InternalId msgId - srvMsgId <- withStore $ \st -> setMsgUserAck st connId mId + srvMsgId <- withStore c $ \st -> setMsgUserAck st connId mId sendAck c rq srvMsgId `catchError` \case SMP SMP.NO_MSG -> pure () e -> throwError e - withStore $ \st -> deleteMsg st connId mId + withStore c $ \st -> deleteMsg st connId mId -- | Suspend SMP agent connection (OFF command) in Reader monad suspendConnection' :: AgentMonad m => AgentClient -> ConnId -> m () suspendConnection' c connId = - withStore (`getConn` connId) >>= \case + withStore c (`getConn` connId) >>= \case SomeConn _ (DuplexConnection _ rq _) -> suspendQueue c rq SomeConn _ (RcvConnection _ rq) -> suspendQueue c rq _ -> throwError $ CONN SIMPLEX @@ -541,11 +552,11 @@ suspendConnection' c connId = -- | Delete SMP agent connection (DEL command) in Reader monad deleteConnection' :: forall m. AgentMonad m => AgentClient -> ConnId -> m () deleteConnection' c connId = - withStore (`getConn` connId) >>= \case + withStore c (`getConn` connId) >>= \case SomeConn _ (DuplexConnection _ rq _) -> delete rq SomeConn _ (RcvConnection _ rq) -> delete rq SomeConn _ (ContactConnection _ rq) -> delete rq - SomeConn _ (SndConnection _ _) -> withStore (`deleteConn` connId) + SomeConn _ (SndConnection _ _) -> withStore c (`deleteConn` connId) where delete :: RcvQueue -> m () delete rq = do @@ -554,7 +565,7 @@ deleteConnection' c connId = atomically $ do removeSubscription c connId sendNtfSubCommand ns (rq, NSCDelete) - withStore (`deleteConn` connId) + withStore c (`deleteConn` connId) -- | Change servers to be used for creating new queues, in Reader monad setSMPServers' :: AgentMonad m => AgentClient -> NonEmpty SMPServer -> m () @@ -563,7 +574,7 @@ setSMPServers' c servers = do registerNtfToken' :: forall m. AgentMonad m => AgentClient -> DeviceToken -> m NtfTknStatus registerNtfToken' c deviceToken = - withStore (`getDeviceNtfToken` deviceToken) >>= \case + withStore c (`getDeviceNtfToken` deviceToken) >>= \case (Just tkn@NtfToken {ntfTokenId, ntfTknStatus, ntfTknAction}, prevTokens) -> do mapM_ (deleteToken_ c) prevTokens ns <- asks ntfSupervisor @@ -583,7 +594,7 @@ registerNtfToken' c deviceToken = -- agentNtfCheckToken c tknId tkn >>= \case (Just tknId, Just NTADelete) -> do agentNtfDeleteToken c tknId tkn - withStore $ \st -> removeNtfToken st tkn + withStore c $ \st -> removeNtfToken st tkn atomically $ nsRemoveNtfToken ns pure NTExpired _ -> pure ntfTknStatus @@ -595,7 +606,7 @@ registerNtfToken' c deviceToken = tknKeys <- liftIO $ C.generateSignatureKeyPair a dhKeys <- liftIO C.generateKeyPair' let tkn = newNtfToken deviceToken ntfServer tknKeys dhKeys - withStore $ \st -> createNtfToken st tkn + withStore c $ \st -> createNtfToken st tkn registerToken tkn pure NTRegistered _ -> throwError $ CMD PROHIBITED @@ -605,14 +616,14 @@ registerNtfToken' c deviceToken = registerToken tkn@NtfToken {ntfPubKey, ntfDhKeys = (pubDhKey, privDhKey)} = do (tknId, srvPubDhKey) <- agentNtfRegisterToken c tkn ntfPubKey pubDhKey let dhSecret = C.dh' srvPubDhKey privDhKey - withStore $ \st -> updateNtfTokenRegistration st tkn tknId dhSecret + withStore c $ \st -> updateNtfTokenRegistration st tkn tknId dhSecret ns <- asks ntfSupervisor atomically $ nsUpdateToken ns tkn -- TODO decrypt verification code verifyNtfToken' :: AgentMonad m => AgentClient -> DeviceToken -> ByteString -> C.CbNonce -> m () verifyNtfToken' c deviceToken code nonce = - withStore (`getDeviceNtfToken` deviceToken) >>= \case + withStore c (`getDeviceNtfToken` deviceToken) >>= \case (Just tkn@NtfToken {ntfTokenId = Just tknId, ntfDhSecret = Just dhSecret}, _) -> do code' <- liftEither . bimap cryptoError NtfRegCode $ C.cbDecrypt dhSecret nonce code void . withToken c tkn (Just (NTConfirmed, NTAVerify code')) (NTActive, Just NTACheck) $ do @@ -622,7 +633,7 @@ verifyNtfToken' c deviceToken code nonce = enableNtfCron' :: AgentMonad m => AgentClient -> DeviceToken -> Word16 -> m () enableNtfCron' c deviceToken interval = do when (interval < 20) . throwError $ CMD PROHIBITED - withStore (`getDeviceNtfToken` deviceToken) >>= \case + withStore c (`getDeviceNtfToken` deviceToken) >>= \case (Just tkn@NtfToken {ntfTokenId = Just tknId, ntfTknStatus = NTActive}, _) -> void . withToken c tkn (Just (NTActive, NTACron interval)) (cronSuccess interval) $ agentNtfEnableCron c tknId tkn interval @@ -635,13 +646,13 @@ cronSuccess interval checkNtfToken' :: AgentMonad m => AgentClient -> DeviceToken -> m NtfTknStatus checkNtfToken' c deviceToken = - withStore (`getDeviceNtfToken` deviceToken) >>= \case + withStore c (`getDeviceNtfToken` deviceToken) >>= \case (Just tkn@NtfToken {ntfTokenId = Just tknId}, _) -> agentNtfCheckToken c tknId tkn _ -> throwError $ CMD PROHIBITED deleteNtfToken' :: AgentMonad m => AgentClient -> DeviceToken -> m () deleteNtfToken' c deviceToken = - withStore (`getDeviceNtfToken` deviceToken) >>= \case + withStore c (`getDeviceNtfToken` deviceToken) >>= \case (Just tkn, _) -> deleteToken_ c tkn _ -> throwError $ CMD PROHIBITED @@ -650,30 +661,30 @@ deleteToken_ c tkn@NtfToken {ntfTokenId, ntfTknStatus} = do ns <- asks ntfSupervisor forM_ ntfTokenId $ \tknId -> do let ntfTknAction = Just NTADelete - withStore $ \st -> updateNtfToken st tkn ntfTknStatus ntfTknAction + withStore c $ \st -> updateNtfToken st tkn ntfTknStatus ntfTknAction atomically $ nsUpdateToken ns tkn {ntfTknStatus, ntfTknAction} agentNtfDeleteToken c tknId tkn `catchError` \case NTF AUTH -> pure () e -> throwError e - withStore $ \st -> removeNtfToken st tkn + withStore c $ \st -> removeNtfToken st tkn atomically $ nsRemoveNtfToken ns withToken :: AgentMonad m => AgentClient -> NtfToken -> Maybe (NtfTknStatus, NtfTknAction) -> (NtfTknStatus, Maybe NtfTknAction) -> m a -> m NtfTknStatus withToken c tkn@NtfToken {deviceToken} from_ (toStatus, toAction_) f = do ns <- asks ntfSupervisor forM_ from_ $ \(status, action) -> do - withStore $ \st -> updateNtfToken st tkn status (Just action) + withStore c $ \st -> updateNtfToken st tkn status (Just action) atomically $ nsUpdateToken ns tkn {ntfTknStatus = status, ntfTknAction = Just action} tryError f >>= \case Right _ -> do - withStore $ \st -> updateNtfToken st tkn toStatus toAction_ + withStore c $ \st -> updateNtfToken st tkn toStatus toAction_ let updatedToken = tkn {ntfTknStatus = toStatus, ntfTknAction = toAction_} if toStatus == NTActive then initializeNtfSubQ c updatedToken else atomically $ nsUpdateToken ns updatedToken pure toStatus Left e@(NTF AUTH) -> do - withStore $ \st -> removeNtfToken st tkn + withStore c $ \st -> removeNtfToken st tkn atomically $ nsRemoveNtfToken ns void $ registerNtfToken' c deviceToken throwError e @@ -686,7 +697,7 @@ initializeNtfSubQ c tkn = do nsUpdateToken ns tkn getSubscriptions c forM_ connIds $ \connId -> do - rq <- withStore $ \st -> getRcvQueue st connId + rq <- withStore c $ \st -> getRcvQueue st connId atomically $ sendNtfSubCommand ns (rq, NSCCreate) -- TODO @@ -695,8 +706,14 @@ initializeNtfSubQ c tkn = do -- It is an optimization, but I am thinking how it would behave if a user were to flip on/off quickly several times. setNtfServers' :: AgentMonad m => AgentClient -> [NtfServer] -> m () -setNtfServers' c servers = do - atomically $ writeTVar (ntfServers c) servers +setNtfServers' c = atomically . writeTVar (ntfServers c) + +setAgentPhase' :: AgentMonad m => AgentClient -> AgentPhase -> m () +setAgentPhase' c p = do + aPhase <- asks agentPhase + atomically $ do + writeTVar aPhase (p, False) + notifyAgentPhaseChanged c getSMPServer :: AgentMonad m => AgentClient -> m SMPServer getSMPServer c = do @@ -710,14 +727,16 @@ getSMPServer c = do subscriber :: (MonadUnliftIO m, MonadReader Env m) => AgentClient -> m () subscriber c@AgentClient {msgQ} = forever $ do + atomically $ endAgentOperation c AONetwork t <- atomically $ readTBQueue msgQ + atomically $ beginAgentOperation c AONetwork withAgentLock c (runExceptT $ processSMPTransmission c t) >>= \case Left e -> liftIO $ print e Right _ -> return () processSMPTransmission :: forall m. AgentMonad m => AgentClient -> ServerTransmission BrokerMsg -> m () -processSMPTransmission c@AgentClient {smpClients, subQ} (srv, sessId, rId, cmd) = do - withStore (\st -> getRcvConn st srv rId) >>= \case +processSMPTransmission c@AgentClient {smpClients, subQ} (srv, sessId, rId, cmd) = + withStore c (\st -> getRcvConn st srv rId) >>= \case SomeConn _ conn@(DuplexConnection cData rq _) -> processSMP conn cData rq SomeConn _ conn@(RcvConnection cData rq) -> processSMP conn cData rq SomeConn _ conn@(ContactConnection cData rq) -> processSMP conn cData rq @@ -746,19 +765,19 @@ processSMPTransmission c@AgentClient {smpClients, subQ} (srv, sessId, rId, cmd) (SMP.PHEmpty, AgentMsgEnvelope _ encAgentMsg) -> tryError agentClientMsg >>= \case Right (Just (msgId, msgMeta, aMessage)) -> case aMessage of - HELLO -> helloMsg >> ack >> withStore (\st -> deleteMsg st connId msgId) - REPLY cReq -> replyMsg cReq >> ack >> withStore (\st -> deleteMsg st connId msgId) + HELLO -> helloMsg >> ack >> withStore c (\st -> deleteMsg st connId msgId) + REPLY cReq -> replyMsg cReq >> ack >> withStore c (\st -> deleteMsg st connId msgId) -- note that there is no ACK sent for A_MSG, it is sent with agent's user ACK command A_MSG body -> do logServer "<--" c srv rId "MSG " notify $ MSG msgMeta msgFlags body Right _ -> prohibited >> ack Left e@(AGENT A_DUPLICATE) -> do - withStore (\st -> getLastMsg st connId srvMsgId) >>= \case + withStore c (\st -> getLastMsg st connId srvMsgId) >>= \case Just RcvMsg {internalId, msgMeta, msgBody = agentMsgBody, userAck} | userAck -> do ack - withStore $ \st -> deleteMsg st connId internalId + withStore c $ \st -> deleteMsg st connId internalId | otherwise -> do liftEither (parse smpP (AGENT A_MESSAGE) agentMsgBody) >>= \case AgentMessage _ (A_MSG body) -> do @@ -769,7 +788,7 @@ processSMPTransmission c@AgentClient {smpClients, subQ} (srv, sessId, rId, cmd) Left e -> throwError e where agentClientMsg :: m (Maybe (InternalId, MsgMeta, AMessage)) - agentClientMsg = withStore $ \st -> do + agentClientMsg = withStore c $ \st -> do agentMsgBody <- agentRatchetDecrypt st connId encAgentMsg liftEither (parse smpP (SEAgentError $ AGENT A_MESSAGE) agentMsgBody) >>= \case agentMsg@(AgentMessage APrivHeader {sndMsgId, prevMsgHash} aMessage) -> do @@ -843,7 +862,7 @@ processSMPTransmission c@AgentClient {smpClients, subQ} (srv, sessId, rId, cmd) New -> case (conn, e2eEncryption) of -- party initiating connection (RcvConnection {}, Just e2eSndParams) -> do - (pk1, rcDHRs) <- withStore $ \st -> getRatchetX3dhKeys st connId + (pk1, rcDHRs) <- withStore c $ \st -> getRatchetX3dhKeys st connId let rc = CR.initRcvRatchet rcDHRs $ CR.x3dhRcv pk1 rcDHRs e2eSndParams (agentMsgBody_, rc', skipped) <- liftError cryptoError $ CR.rcDecrypt rc M.empty encConnInfo case (agentMsgBody_, skipped) of @@ -858,14 +877,14 @@ processSMPTransmission c@AgentClient {smpClients, subQ} (srv, sessId, rId, cmd) processConf connInfo senderConf duplexHS = do let newConfirmation = NewConfirmation {connId, senderConf, ratchetState = rc'} g <- asks idsDrg - confId <- withStore $ \st -> do + confId <- withStore c $ \st -> do setHandshakeVersion st connId agentVersion duplexHS createConfirmation st g newConfirmation notify $ CONF confId connInfo _ -> prohibited -- party accepting connection (DuplexConnection _ _ sq, Nothing) -> do - withStore (\st -> agentRatchetDecrypt st connId encConnInfo) >>= parseMessage >>= \case + withStore c (\st -> agentRatchetDecrypt st connId encConnInfo) >>= parseMessage >>= \case AgentConnInfo connInfo -> do notify $ INFO connInfo processConfirmation c rq $ SMPConfirmation {senderKey, e2ePubKey, connInfo, smpReplyQueues = []} @@ -880,7 +899,7 @@ processSMPTransmission c@AgentClient {smpClients, subQ} (srv, sessId, rId, cmd) case status of Active -> prohibited _ -> do - withStore $ \st -> setRcvQueueStatus st rq Active + withStore c $ \st -> setRcvQueueStatus st rq Active case conn of DuplexConnection _ _ sq@SndQueue {status = sndStatus} -- `sndStatus == Active` when HELLO was previously sent, and this is the reply HELLO @@ -902,7 +921,7 @@ processSMPTransmission c@AgentClient {smpClients, subQ} (srv, sessId, rId, cmd) Just True -> prohibited _ -> case conn of RcvConnection {} -> do - AcceptedConfirmation {ownConnInfo} <- withStore (`getAcceptedConfirmation` connId) + AcceptedConfirmation {ownConnInfo} <- withStore c (`getAcceptedConfirmation` connId) connectReplyQueues c cData ownConnInfo smpQueues `catchError` (notify . ERR) _ -> prohibited @@ -913,7 +932,7 @@ processSMPTransmission c@AgentClient {smpClients, subQ} (srv, sessId, rId, cmd) ContactConnection {} -> do g <- asks idsDrg let newInv = NewInvitation {contactConnId = connId, connReq, recipientConnInfo = cInfo} - invId <- withStore $ \st -> createInvitation st g newInv + invId <- withStore c $ \st -> createInvitation st g newInv notify $ REQ invId cInfo _ -> prohibited @@ -933,7 +952,7 @@ connectReplyQueues c cData@ConnData {connId} ownConnInfo (qInfo :| _) = do Nothing -> throwError $ AGENT A_VERSION Just qInfo' -> do sq <- newSndQueue qInfo' - withStore $ \st -> upgradeRcvConnToDuplex st connId sq + withStore c $ \st -> upgradeRcvConnToDuplex st connId sq enqueueConfirmation c cData sq ownConnInfo Nothing confirmQueue :: forall m. AgentMonad m => Compatible Version -> AgentClient -> ConnId -> SndQueue -> ConnInfo -> Maybe (CR.E2ERatchetParams 'C.X448) -> m () @@ -941,10 +960,10 @@ confirmQueue (Compatible agentVersion) c connId sq connInfo e2eEncryption = do aMessage <- mkAgentMessage agentVersion msg <- mkConfirmation aMessage sendConfirmation c sq msg - withStore $ \st -> setSndQueueStatus st sq Confirmed + withStore c $ \st -> setSndQueueStatus st sq Confirmed where mkConfirmation :: AgentMessage -> m MsgBody - mkConfirmation aMessage = withStore $ \st -> do + mkConfirmation aMessage = withStore c $ \st -> do void $ updateSndIds st connId encConnInfo <- agentRatchetEncrypt st connId (smpEncode aMessage) e2eEncConnInfoLength pure . smpEncode $ AgentConfirmation {agentVersion, e2eEncryption, encConnInfo} @@ -961,7 +980,7 @@ enqueueConfirmation c cData@ConnData {connId, connAgentVersion} sq connInfo e2eE queuePendingMsgs c connId sq [msgId] where storeConfirmation :: m InternalId - storeConfirmation = withStore $ \st -> do + storeConfirmation = withStore c $ \st -> do internalTs <- liftIO getCurrentTime (internalId, internalSndId, prevMsgHash) <- updateSndIds st connId let agentMsg = AgentConnInfo connInfo diff --git a/src/Simplex/Messaging/Agent/Client.hs b/src/Simplex/Messaging/Agent/Client.hs index ef86528fe..c351bc04a 100644 --- a/src/Simplex/Messaging/Agent/Client.hs +++ b/src/Simplex/Messaging/Agent/Client.hs @@ -4,10 +4,12 @@ {-# LANGUAGE DuplicateRecordFields #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE FlexibleInstances #-} +{-# LANGUAGE GADTs #-} {-# LANGUAGE LambdaCase #-} {-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE OverloadedLists #-} {-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE RankNTypes #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE TypeApplications #-} @@ -41,12 +43,16 @@ module Simplex.Messaging.Agent.Client removeSubscription, hasActiveSubscription, agentDbPath, + beginAgentOperation, + endAgentOperation, + notifyAgentPhaseChanged, + withStore, ) where import Control.Concurrent (forkIO) import Control.Concurrent.Async (Async, uninterruptibleCancel) -import Control.Concurrent.STM (stateTVar) +import Control.Concurrent.STM (retry, stateTVar) import Control.Logger.Simple import Control.Monad.Except import Control.Monad.IO.Unlift @@ -62,11 +68,12 @@ import Data.Maybe (catMaybes) import Data.Set (Set) import Data.Text.Encoding import Data.Word (Word16) +import Database.SQLite.Simple (SQLError) import Simplex.Messaging.Agent.Env.SQLite import Simplex.Messaging.Agent.Protocol import Simplex.Messaging.Agent.RetryInterval import Simplex.Messaging.Agent.Store -import Simplex.Messaging.Agent.Store.SQLite (SQLiteStore (..)) +import Simplex.Messaging.Agent.Store.SQLite (AgentStoreMonad, SQLiteStore (..)) import Simplex.Messaging.Client import Simplex.Messaging.Client.Agent () import qualified Simplex.Messaging.Crypto as C @@ -209,6 +216,9 @@ getSMPServerClient c@AgentClient {active, smpClients, msgQ} srv = do n <- asks $ resubscriptionConcurrency . config withAgentLock c . withClient c srv $ \smp -> do cs <- atomically $ mapM readTVar =<< TM.lookup srv (pendingSubscrSrvrs c) + -- TODO if any of the subscriptions fails here (e.g. because of timeout), it terminates the whole process for all subscriptions + -- instead it should only report successful subscriptions and schedule the next call to reconnectClient to subscribe for the remaining subscriptions + -- this way, for each DOWN event there can be several UP events conns <- pooledForConcurrentlyN n (maybe [] M.toList cs) $ \sub@(connId, _) -> ifM (atomically $ hasActiveSubscription c connId) @@ -320,6 +330,7 @@ closeAgentClient c = liftIO $ do clear smpQueueMsgQueues where clientTimeout sel = tcpTimeout . sel . config $ agentEnv c + clear :: (AgentClient -> TMap k a) -> IO () clear sel = atomically $ writeTVar (sel c) M.empty closeProtocolServerClients :: Int -> TMap ProtocolServer (ClientVar msg) -> IO () @@ -576,3 +587,51 @@ cryptoError = \case C.CBDecryptError -> AGENT A_ENCRYPTION C.CERatchetDuplicateMessage -> AGENT A_DUPLICATE e -> INTERNAL $ show e + +endAgentOperation :: AgentClient -> AgentOperation -> STM () +endAgentOperation c@AgentClient {agentEnv = Env {agentOperations}} op = do + TM.alter (Just . maybe 0 (\n -> min 0 $ n - 1)) op agentOperations + notifyAgentPhaseChanged c + +beginAgentOperation :: AgentClient -> AgentOperation -> STM () +beginAgentOperation AgentClient {agentEnv = Env {agentPhase, agentOperations}} op = do + (p, _) <- readTVar agentPhase + when (op `elem` disallowedOperations p) retry + TM.alter (Just . maybe 1 (+ 1)) op agentOperations + +notifyAgentPhaseChanged :: AgentClient -> STM () +notifyAgentPhaseChanged AgentClient {subQ, agentEnv = Env {agentPhase, agentOperations}} = do + (p, notified) <- readTVar agentPhase + unless notified $ do + ops <- readTVar agentOperations + let opsPaused = all (maybe True (== 0) . (`M.lookup` ops)) $ disallowedOperations p + when opsPaused $ do + writeTBQueue subQ ("", "", PHASE p) + writeTVar agentPhase (p, True) + +withStore :: AgentMonad m => AgentClient -> (forall m'. AgentStoreMonad m' => SQLiteStore -> m' a) -> m a +withStore c action = do + st <- asks store + atomically $ beginAgentOperation c AODatabase + r <- runExceptT (action st `E.catch` handleInternal) + atomically $ endAgentOperation c AODatabase + case r of + Right res -> pure res + Left e -> throwError $ storeError e + where + -- TODO when parsing exception happens in store, the agent hangs; + -- changing SQLError to SomeException does not help + handleInternal :: (MonadError StoreError m') => SQLError -> m' a + handleInternal e = throwError . SEInternal $ bshow e + storeError :: StoreError -> AgentErrorType + storeError = \case + SEConnNotFound -> CONN NOT_FOUND + SEConnDuplicate -> CONN DUPLICATE + SEBadConnType CRcv -> CONN SIMPLEX + SEBadConnType CSnd -> CONN SIMPLEX + SEInvitationNotFound -> CMD PROHIBITED + -- this error is never reported as store error, + -- it is used to wrap agent operations when "transaction-like" store access is needed + -- NOTE: network IO should NOT be used inside AgentStoreMonad + SEAgentError e -> e + e -> INTERNAL $ show e diff --git a/src/Simplex/Messaging/Agent/Env/SQLite.hs b/src/Simplex/Messaging/Agent/Env/SQLite.hs index 41fbe1f44..b5fa72ffc 100644 --- a/src/Simplex/Messaging/Agent/Env/SQLite.hs +++ b/src/Simplex/Messaging/Agent/Env/SQLite.hs @@ -17,10 +17,11 @@ module Simplex.Messaging.Agent.Env.SQLite defaultAgentConfig, defaultReconnectInterval, Env (..), + AgentOperation (..), + disallowedOperations, newSMPAgentEnv, NtfSupervisor (..), NtfSupervisorCommand (..), - withStore, ) where @@ -30,12 +31,11 @@ import Control.Monad.Reader import Crypto.Random import Data.List.NonEmpty (NonEmpty) import Data.Time.Clock (NominalDiffTime, nominalDay) -import Database.SQLite.Simple (SQLError) import Network.Socket import Numeric.Natural import Simplex.Messaging.Agent.Protocol import Simplex.Messaging.Agent.RetryInterval -import Simplex.Messaging.Agent.Store (ConnType (..), RcvQueue, StoreError (..)) +import Simplex.Messaging.Agent.Store (RcvQueue) import Simplex.Messaging.Agent.Store.SQLite import qualified Simplex.Messaging.Agent.Store.SQLite.Migrations as Migrations import Simplex.Messaging.Client @@ -45,11 +45,9 @@ import Simplex.Messaging.Notifications.Client (NtfServer, NtfToken) import Simplex.Messaging.TMap (TMap) import qualified Simplex.Messaging.TMap as TM import Simplex.Messaging.Transport (TLS, Transport (..)) -import Simplex.Messaging.Util (bshow) import Simplex.Messaging.Version import System.Random (StdGen, newStdGen) import UnliftIO (Async) -import qualified UnliftIO.Exception as E import UnliftIO.STM -- | Agent monad with MonadReader Env and MonadError AgentErrorType @@ -122,17 +120,30 @@ data Env = Env idsDrg :: TVar ChaChaDRG, clientCounter :: TVar Int, randomServer :: TVar StdGen, + agentPhase :: TVar (AgentPhase, Bool), + agentOperations :: TMap AgentOperation Int, ntfSupervisor :: NtfSupervisor } +data AgentOperation = AONetwork | AODatabase + deriving (Eq, Ord) + +disallowedOperations :: AgentPhase -> [AgentOperation] +disallowedOperations = \case + APActive -> [] + APPaused -> [AONetwork] + APSuspended -> [AONetwork, AODatabase] + newSMPAgentEnv :: (MonadUnliftIO m, MonadRandom m) => AgentConfig -> m Env newSMPAgentEnv config@AgentConfig {dbFile, dbPoolSize, yesToMigrations} = do idsDrg <- newTVarIO =<< drgNew store <- liftIO $ createSQLiteStore dbFile dbPoolSize Migrations.app yesToMigrations clientCounter <- newTVarIO 0 randomServer <- newTVarIO =<< liftIO newStdGen + agentPhase <- newTVarIO (APActive, True) + agentOperations <- atomically TM.empty ntfSupervisor <- atomically . newNtfSubSupervisor $ tbqSize config - return Env {config, store, idsDrg, clientCounter, randomServer, ntfSupervisor} + return Env {config, store, idsDrg, clientCounter, randomServer, agentPhase, agentOperations, ntfSupervisor} data NtfSupervisor = NtfSupervisor { ntfTkn :: TVar (Maybe NtfToken), @@ -150,27 +161,3 @@ newNtfSubSupervisor qSize = do ntfWorkers <- TM.empty ntfSMPWorkers <- TM.empty pure NtfSupervisor {ntfTkn, ntfSubQ, ntfWorkers, ntfSMPWorkers} - -withStore :: AgentMonad m => (forall m'. AgentStoreMonad m' => SQLiteStore -> m' a) -> m a -withStore action = do - st <- asks store - runExceptT (action st `E.catch` handleInternal) >>= \case - Right c -> return c - Left e -> throwError $ storeError e - where - -- TODO when parsing exception happens in store, the agent hangs; - -- changing SQLError to SomeException does not help - handleInternal :: (MonadError StoreError m') => SQLError -> m' a - handleInternal e = throwError . SEInternal $ bshow e - storeError :: StoreError -> AgentErrorType - storeError = \case - SEConnNotFound -> CONN NOT_FOUND - SEConnDuplicate -> CONN DUPLICATE - SEBadConnType CRcv -> CONN SIMPLEX - SEBadConnType CSnd -> CONN SIMPLEX - SEInvitationNotFound -> CMD PROHIBITED - -- this error is never reported as store error, - -- it is used to wrap agent operations when "transaction-like" store access is needed - -- NOTE: network IO should NOT be used inside AgentStoreMonad - SEAgentError e -> e - e -> INTERNAL $ show e diff --git a/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs b/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs index 6842da6d6..91b0759ac 100644 --- a/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs +++ b/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs @@ -53,12 +53,12 @@ processNtfSub c (rcvQueue@RcvQueue {server = smpServer, rcvId}, cmd) = do ntfToken_ <- readTVarIO $ ntfTkn ns case cmd of NSCCreate -> do - sub_ <- withStore $ \st -> getNtfSubscription st rcvQueue + sub_ <- withStore c $ \st -> getNtfSubscription st rcvQueue case (sub_, ntfServer_, ntfToken_) of (Nothing, Just ntfServer, Just tkn) -> do currentTime <- liftIO getCurrentTime let newSub = newNtfSubscription ntfServer tkn smpServer rcvId currentTime - withStore $ \st -> createNtfSubscription st newSub + withStore c $ \st -> createNtfSubscription st newSub -- TODO optimize? -- TODO - read action in getNtfSubscription and decide which worker to create -- TODO - SMP worker can create Ntf worker on NKEY completion @@ -69,7 +69,7 @@ processNtfSub c (rcvQueue@RcvQueue {server = smpServer, rcvId}, cmd) = do addNtfWorker ntfServer _ -> pure () NSCDelete -> do - withStore $ \st -> markNtfSubscriptionForDeletion st rcvQueue + withStore c $ \st -> markNtfSubscriptionForDeletion st rcvQueue case (ntfServer_, ntfToken_) of (Just ntfServer, Just _) -> addNtfWorker ntfServer _ -> pure () @@ -91,9 +91,9 @@ processNtfSub c (rcvQueue@RcvQueue {server = smpServer, rcvId}, cmd) = do Just (doWork, _) -> void . atomically $ tryPutTMVar doWork () runNtfWorker :: AgentMonad m => AgentClient -> NtfServer -> TMVar () -> m () -runNtfWorker _c srv doWork = forever $ do +runNtfWorker c srv doWork = forever $ do void . atomically $ readTMVar doWork - withStore $ \st -> + withStore c $ \st -> getNextNtfSubAction st srv >>= \case Nothing -> void . atomically $ tryTakeTMVar doWork Just (_sub, ntfSubAction) -> @@ -105,9 +105,9 @@ runNtfWorker _c srv doWork = forever $ do liftIO $ threadDelay delay runNtfSMPWorker :: AgentMonad m => AgentClient -> NtfServer -> TMVar () -> m () -runNtfSMPWorker _c srv doWork = forever $ do +runNtfSMPWorker c srv doWork = forever $ do void . atomically $ readTMVar doWork - withStore $ \st -> + withStore c $ \st -> getNextNtfSubSMPAction st srv >>= \case Nothing -> void . atomically $ tryTakeTMVar doWork Just (_sub, ntfSubAction) -> diff --git a/src/Simplex/Messaging/Agent/Protocol.hs b/src/Simplex/Messaging/Agent/Protocol.hs index 7e5f50030..4425c83b0 100644 --- a/src/Simplex/Messaging/Agent/Protocol.hs +++ b/src/Simplex/Messaging/Agent/Protocol.hs @@ -82,6 +82,7 @@ module Simplex.Messaging.Agent.Protocol QueueStatus (..), ACorrId, AgentMsgId, + AgentPhase (..), -- * Encode/decode serializeCommand, @@ -223,11 +224,34 @@ data ACommand (p :: AParty) where DEL :: ACommand Client OK :: ACommand Agent ERR :: AgentErrorType -> ACommand Agent + PHASE :: AgentPhase -> ACommand Agent deriving instance Eq (ACommand p) deriving instance Show (ACommand p) +-- | Agent phase allows to have two agent processes concurrently working with the same database +data AgentPhase + = -- | agent is operating normally + APActive + | -- | agent is paused - no new send/receive operations will be started - they will STM-retry + APPaused + | -- | agent is suspended - no new send/receive/database operations will be started - they will STM-retry + APSuspended + deriving (Eq, Show) + +instance StrEncoding AgentPhase where + strEncode = \case + APActive -> "ACTIVE" + APPaused -> "PAUSED" + APSuspended -> "SUSPENDED" + strP = + A.takeTill (== ' ') >>= \case + "ACTIVE" -> pure APActive + "PAUSED" -> pure APPaused + "SUSPENDED" -> pure APSuspended + _ -> fail "bad AgentPhase" + data ConnectionMode = CMInvitation | CMContact deriving (Eq, Show) @@ -908,6 +932,7 @@ serializeCommand = \case CON -> "CON" ERR e -> "ERR " <> strEncode e OK -> "OK" + PHASE p -> "PHASE " <> strEncode p where showTs :: UTCTime -> ByteString showTs = B.pack . formatISO8601Millis From f7bd8a398275bd3bf35b61b6ee9c91303c73ce25 Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Tue, 14 Jun 2022 14:34:18 +0400 Subject: [PATCH 054/131] ntf: agent ntf workers implementation, store methods (#394) --- simplexmq.cabal | 1 - src/Simplex/Messaging/Agent.hs | 20 +- src/Simplex/Messaging/Agent/Client.hs | 13 +- src/Simplex/Messaging/Agent/Env/SQLite.hs | 5 +- .../Messaging/Agent/NtfSubSupervisor.hs | 117 ++++++++--- src/Simplex/Messaging/Agent/Store.hs | 39 ++-- src/Simplex/Messaging/Agent/Store/SQLite.hs | 183 ++++++++++++++++-- .../M20220404_ntf_subscriptions_draft.hs | 41 ---- .../Store/SQLite/Migrations/M20220608_v2.hs | 32 +++ .../Store/SQLite/Migrations/agent_schema.sql | 27 +++ src/Simplex/Messaging/Notifications/Client.hs | 85 ++++++-- .../Messaging/Notifications/Protocol.hs | 6 +- src/Simplex/Messaging/Protocol.hs | 4 +- tests/AgentTests/SQLiteTests.hs | 10 +- 14 files changed, 424 insertions(+), 159 deletions(-) delete mode 100644 src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220404_ntf_subscriptions_draft.hs diff --git a/simplexmq.cabal b/simplexmq.cabal index e0cde2c6d..2c466bfff 100644 --- a/simplexmq.cabal +++ b/simplexmq.cabal @@ -48,7 +48,6 @@ library Simplex.Messaging.Agent.Store.SQLite.Migrations.M20220101_initial Simplex.Messaging.Agent.Store.SQLite.Migrations.M20220301_snd_queue_keys Simplex.Messaging.Agent.Store.SQLite.Migrations.M20220322_notifications - Simplex.Messaging.Agent.Store.SQLite.Migrations.M20220404_ntf_subscriptions_draft Simplex.Messaging.Agent.Store.SQLite.Migrations.M20220608_v2 Simplex.Messaging.Client Simplex.Messaging.Client.Agent diff --git a/src/Simplex/Messaging/Agent.hs b/src/Simplex/Messaging/Agent.hs index 609d7bbff..42f68bf3a 100644 --- a/src/Simplex/Messaging/Agent.hs +++ b/src/Simplex/Messaging/Agent.hs @@ -239,14 +239,14 @@ processCommand c (connId, cmd) = case cmd of newConn :: AgentMonad m => AgentClient -> ConnId -> SConnectionMode c -> m (ConnId, ConnectionRequestUri c) newConn c connId cMode = do srv <- getSMPServer c - (rq, qUri) <- newRcvQueueNtf c srv + (rq, qUri) <- newRcvQueueNtf c srv connId g <- asks idsDrg agentVersion <- asks $ smpAgentVersion . config let cData = ConnData {connId, connAgentVersion = agentVersion, duplexHandshake = Nothing} -- connection mode is determined by the accepting agent connId' <- withStore c $ \st -> createRcvConn st g cData rq cMode addSubscription c rq connId' ns <- asks ntfSupervisor - atomically $ sendNtfSubCommand ns (rq, NSCCreate) + atomically $ sendNtfSubCommand ns (connId, NSCCreate) aVRange <- asks $ smpAgentVRange . config let crData = ConnReqUriData simplexChat aVRange [qUri] case cMode of @@ -301,7 +301,7 @@ joinConn c connId (CRContactUri (ConnReqUriData _ agentVRange (qUri :| _))) cInf createReplyQueue :: AgentMonad m => AgentClient -> ConnId -> m SMPQueueInfo createReplyQueue c connId = do srv <- getSMPServer c - (rq, qUri) <- newRcvQueueNtf c srv + (rq, qUri) <- newRcvQueueNtf c srv connId -- TODO reply queue version should be the same as send queue, ignoring it in v1 let qInfo = toVersionT qUri SMP.smpClientVersion addSubscription c rq connId @@ -361,7 +361,7 @@ subscribeConnection' c connId = subscribe rq = do subscribeQueue c rq connId ns <- asks ntfSupervisor - atomically $ sendNtfSubCommand ns (rq, NSCCreate) + atomically $ sendNtfSubCommand ns (connId, NSCCreate) resubscribeConnection' :: forall m. AgentMonad m => AgentClient -> ConnId -> m () resubscribeConnection' c connId = @@ -564,7 +564,7 @@ deleteConnection' c connId = ns <- asks ntfSupervisor atomically $ do removeSubscription c connId - sendNtfSubCommand ns (rq, NSCDelete) + sendNtfSubCommand ns (connId, NSCDelete) withStore c (`deleteConn` connId) -- | Change servers to be used for creating new queues, in Reader monad @@ -696,9 +696,7 @@ initializeNtfSubQ c tkn = do connIds <- atomically $ do nsUpdateToken ns tkn getSubscriptions c - forM_ connIds $ \connId -> do - rq <- withStore c $ \st -> getRcvQueue st connId - atomically $ sendNtfSubCommand ns (rq, NSCCreate) + forM_ connIds $ \connId -> atomically $ sendNtfSubCommand ns (connId, NSCCreate) -- TODO -- There should probably be another function to cancel all subscriptions that would flush the queue first, @@ -1010,11 +1008,11 @@ agentRatchetDecrypt st connId encAgentMsg = do updateRatchet st connId rc' skippedDiff liftEither $ first (SEAgentError . cryptoError) agentMsgBody_ -newRcvQueueNtf :: AgentMonad m => AgentClient -> SMPServer -> m (RcvQueue, SMPQueueUri) -newRcvQueueNtf c srv = do +newRcvQueueNtf :: AgentMonad m => AgentClient -> SMPServer -> ConnId -> m (RcvQueue, SMPQueueUri) +newRcvQueueNtf c srv connId = do (rq, qUri) <- newRcvQueue c srv ns <- asks ntfSupervisor - atomically $ sendNtfSubCommand ns (rq, NSCCreate) + atomically $ sendNtfSubCommand ns (connId, NSCCreate) pure (rq, qUri) newSndQueue :: (MonadUnliftIO m, MonadReader Env m) => Compatible SMPQueueInfo -> m SndQueue diff --git a/src/Simplex/Messaging/Agent/Client.hs b/src/Simplex/Messaging/Agent/Client.hs index c351bc04a..8793c388b 100644 --- a/src/Simplex/Messaging/Agent/Client.hs +++ b/src/Simplex/Messaging/Agent/Client.hs @@ -26,6 +26,7 @@ module Simplex.Messaging.Agent.Client sendInvitation, RetryInterval (..), secureQueue, + enableQueueNotifications, sendAgentMessage, agentNtfRegisterToken, agentNtfVerifyToken, @@ -80,7 +81,7 @@ import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Encoding import Simplex.Messaging.Notifications.Client import Simplex.Messaging.Notifications.Protocol -import Simplex.Messaging.Protocol (BrokerMsg, ErrorType, MsgFlags (..), MsgId, NtfPrivateSignKey, ProtocolServer (..), QueueId, QueueIdsKeys (..), SndPublicVerifyKey) +import Simplex.Messaging.Protocol (BrokerMsg, ErrorType, MsgFlags (..), MsgId, NotifierId, NtfPrivateSignKey, NtfPublicVerifyKey, ProtocolServer (..), QueueId, QueueIdsKeys (..), SndPublicVerifyKey) import qualified Simplex.Messaging.Protocol as SMP import Simplex.Messaging.TMap (TMap) import qualified Simplex.Messaging.TMap as TM @@ -413,7 +414,10 @@ newRcvQueue_ a c srv = do e2ePrivKey, e2eDhSecret = Nothing, sndId = Just sndId, - status = New + status = New, + ntfPublicKey = Nothing, + ntfPrivateKey = Nothing, + notifierId = Nothing } pure (rq, SMPQueueUri srv sndId SMP.smpClientVRange e2eDhKey) @@ -502,6 +506,11 @@ secureQueue c RcvQueue {server, rcvId, rcvPrivateKey} senderKey = withLogClient c server rcvId "KEY " $ \smp -> secureSMPQueue smp rcvPrivateKey rcvId senderKey +enableQueueNotifications :: AgentMonad m => AgentClient -> RcvQueue -> NtfPublicVerifyKey -> m NotifierId +enableQueueNotifications c RcvQueue {server, rcvId, rcvPrivateKey} notifierKey = + withLogClient c server rcvId "NKEY " $ \smp -> + enableSMPQueueNotifications smp rcvPrivateKey rcvId notifierKey + sendAck :: AgentMonad m => AgentClient -> RcvQueue -> MsgId -> m () sendAck c RcvQueue {server, rcvId, rcvPrivateKey} msgId = withLogClient c server rcvId "ACK" $ \smp -> diff --git a/src/Simplex/Messaging/Agent/Env/SQLite.hs b/src/Simplex/Messaging/Agent/Env/SQLite.hs index b5fa72ffc..68815a4dc 100644 --- a/src/Simplex/Messaging/Agent/Env/SQLite.hs +++ b/src/Simplex/Messaging/Agent/Env/SQLite.hs @@ -35,7 +35,6 @@ import Network.Socket import Numeric.Natural import Simplex.Messaging.Agent.Protocol import Simplex.Messaging.Agent.RetryInterval -import Simplex.Messaging.Agent.Store (RcvQueue) import Simplex.Messaging.Agent.Store.SQLite import qualified Simplex.Messaging.Agent.Store.SQLite.Migrations as Migrations import Simplex.Messaging.Client @@ -147,12 +146,12 @@ newSMPAgentEnv config@AgentConfig {dbFile, dbPoolSize, yesToMigrations} = do data NtfSupervisor = NtfSupervisor { ntfTkn :: TVar (Maybe NtfToken), - ntfSubQ :: TBQueue (RcvQueue, NtfSupervisorCommand), + ntfSubQ :: TBQueue (ConnId, NtfSupervisorCommand), ntfWorkers :: TMap NtfServer (TMVar (), Async ()), ntfSMPWorkers :: TMap SMPServer (TMVar (), Async ()) } -data NtfSupervisorCommand = NSCCreate | NSCDelete +data NtfSupervisorCommand = NSCCreate | NSCDelete | NSCNtfWorker NtfServer newNtfSubSupervisor :: Natural -> STM NtfSupervisor newNtfSubSupervisor qSize = do diff --git a/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs b/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs index 91b0759ac..f4154a0ed 100644 --- a/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs +++ b/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs @@ -26,11 +26,13 @@ import qualified Data.Map.Strict as M import Data.Time (getCurrentTime) import Simplex.Messaging.Agent.Client import Simplex.Messaging.Agent.Env.SQLite +import Simplex.Messaging.Agent.Protocol (ConnId) import Simplex.Messaging.Agent.Store import Simplex.Messaging.Client.Agent () +import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Notifications.Client -import Simplex.Messaging.Notifications.Protocol (NtfTknStatus (..)) -import Simplex.Messaging.Protocol (ProtocolServer) +import Simplex.Messaging.Notifications.Protocol (NtfTknStatus (..), SMPQueueNtf (..)) +import Simplex.Messaging.Protocol import Simplex.Messaging.TMap (TMap) import qualified Simplex.Messaging.TMap as TM import System.Random (randomR) @@ -46,33 +48,46 @@ runNtfSupervisor c = forever $ do Left e -> liftIO $ print e Right _ -> return () -processNtfSub :: forall m. AgentMonad m => AgentClient -> (RcvQueue, NtfSupervisorCommand) -> m () -processNtfSub c (rcvQueue@RcvQueue {server = smpServer, rcvId}, cmd) = do +processNtfSub :: forall m. AgentMonad m => AgentClient -> (ConnId, NtfSupervisorCommand) -> m () +processNtfSub c (connId, cmd) = do ntfServer_ <- getNtfServer c - ns <- asks ntfSupervisor - ntfToken_ <- readTVarIO $ ntfTkn ns case cmd of NSCCreate -> do - sub_ <- withStore c $ \st -> getNtfSubscription st rcvQueue - case (sub_, ntfServer_, ntfToken_) of - (Nothing, Just ntfServer, Just tkn) -> do + sub_ <- withStore c $ \st -> getNtfSubscription st connId + RcvQueue {notifierId, server = smpServer} <- withStore c $ \st -> getRcvQueue st connId + case (sub_, ntfServer_) of + (Nothing, Just ntfServer) -> do currentTime <- liftIO getCurrentTime - let newSub = newNtfSubscription ntfServer tkn smpServer rcvId currentTime - withStore c $ \st -> createNtfSubscription st newSub + case notifierId of + (Just nId) -> do + let newSub = newNtfSubscription connId smpServer (Just nId) ntfServer NASKey currentTime + withStore c $ \st -> createNtfSubscription st newSub (NtfSubAction NSACreate) + _ -> do + let newSub = newNtfSubscription connId smpServer Nothing ntfServer NASNew currentTime + withStore c $ \st -> createNtfSubscription st newSub (NtfSubSMPAction NSAKey) -- TODO optimize? -- TODO - read action in getNtfSubscription and decide which worker to create -- TODO - SMP worker can create Ntf worker on NKEY completion addNtfSMPWorker smpServer addNtfWorker ntfServer - (Just _, Just ntfServer, Just _) -> do + (Just _, Just ntfServer) -> do + -- TODO subscription may have to be updated depending on current state: + -- TODO - e.g., if it was previously marked for deletion action has to be updated + -- TODO - should action depend on subscription status or always be NSAKey (NSACreate if notifierId exists) + -- TODO in case worker is currently deleting it? When deleting worker should check for updated_by_supervisor + -- TODO and if it is set perform update instead of delete. If worker was not deleting it yet it should + -- TODO idempotently replay commands. addNtfSMPWorker smpServer addNtfWorker ntfServer _ -> pure () NSCDelete -> do - withStore c $ \st -> markNtfSubscriptionForDeletion st rcvQueue - case (ntfServer_, ntfToken_) of - (Just ntfServer, Just _) -> addNtfWorker ntfServer + -- TODO delete notifier ID and Key from SMP server (SDEL, then NDEL) + withStore c $ \st -> markNtfSubscriptionForDeletion st connId + case ntfServer_ of + (Just ntfServer) -> addNtfWorker ntfServer _ -> pure () + NSCNtfWorker ntfServer -> + addNtfWorker ntfServer where addNtfWorker = addWorker ntfWorkers runNtfWorker addNtfSMPWorker = addWorker ntfSMPWorkers runNtfSMPWorker @@ -93,28 +108,68 @@ processNtfSub c (rcvQueue@RcvQueue {server = smpServer, rcvId}, cmd) = do runNtfWorker :: AgentMonad m => AgentClient -> NtfServer -> TMVar () -> m () runNtfWorker c srv doWork = forever $ do void . atomically $ readTMVar doWork - withStore c $ \st -> - getNextNtfSubAction st srv >>= \case - Nothing -> void . atomically $ tryTakeTMVar doWork - Just (_sub, ntfSubAction) -> - forM_ ntfSubAction $ \case - NSANew _nKey -> pure () - NSACheck -> pure () - NSADelete -> pure () + getNtfToken_ >>= \case + Just tkn@NtfToken {ntfTokenId = Just tknId, ntfTknStatus} -> + withStore c (`getNextNtfSubAction` srv) >>= \case + Just (ntfSub@NtfSubscription {connId, smpServer}, ntfSubAction, RcvQueue {ntfPrivateKey, notifierId}) -> do + case ntfSubAction of + NSACreate -> case (ntfPrivateKey, notifierId) of + (Just ntfPrivKey, Just nId) + | ntfTknStatus == NTActive -> do + ntfSubId <- agentNtfCreateSubscription c tknId tkn (SMPQueueNtf smpServer nId) ntfPrivKey + ts <- liftIO getCurrentTime + withStore c $ \st -> + updateNtfSubscription st connId ntfSub {ntfSubId = Just ntfSubId, ntfSubStatus = NASCreated, ntfSubActionTs = ts} (NtfSubAction NSACheck) + | otherwise -> pure () -- error + _ -> pure () -- error + NSACheck -> pure () + NSADelete -> pure () + Nothing -> noWorkToDo + _ -> noWorkToDo delay <- asks $ ntfWorkerThrottle . config liftIO $ threadDelay delay + where + noWorkToDo = void . atomically $ tryTakeTMVar doWork -runNtfSMPWorker :: AgentMonad m => AgentClient -> NtfServer -> TMVar () -> m () +runNtfSMPWorker :: forall m. AgentMonad m => AgentClient -> SMPServer -> TMVar () -> m () runNtfSMPWorker c srv doWork = forever $ do void . atomically $ readTMVar doWork - withStore c $ \st -> - getNextNtfSubSMPAction st srv >>= \case - Nothing -> void . atomically $ tryTakeTMVar doWork - Just (_sub, ntfSubAction) -> - forM_ ntfSubAction $ \case - NSAKey -> pure () + getNtfToken_ >>= \case + Just NtfToken {ntfTknStatus} -> do + withStore c (`getNextNtfSubSMPAction` srv) >>= \case + Just (ntfSub@NtfSubscription {connId, ntfServer}, ntfSubAction, rq@RcvQueue {ntfPublicKey}) -> do + case ntfSubAction of + NSAKey + | ntfTknStatus == NTActive -> + case ntfPublicKey of + Just ntfPubKey -> + enableNotificationsWithNKey ntfPubKey + _ -> do + C.SignAlg a <- asks (cmdSignAlg . config) + (ntfPubKey, ntfPrivKey) <- liftIO $ C.generateSignatureKeyPair a + withStore c $ \st -> setRcvQueueNotifierKey st connId ntfPubKey ntfPrivKey + enableNotificationsWithNKey ntfPubKey + | otherwise -> pure () -- error + where + enableNotificationsWithNKey ntfPubKey = do + nId <- enableQueueNotifications c rq ntfPubKey + ts <- liftIO getCurrentTime + withStore c $ \st -> do + setRcvQueueNotifierId st connId nId + updateNtfSubscription st connId ntfSub {ntfQueueId = Just nId, ntfSubStatus = NASKey, ntfSubActionTs = ts} (NtfSubAction NSACreate) + ns <- asks ntfSupervisor + atomically $ sendNtfSubCommand ns (connId, NSCNtfWorker ntfServer) + Nothing -> noWorkToDo + _ -> noWorkToDo delay <- asks $ ntfWorkerThrottle . config liftIO $ threadDelay delay + where + noWorkToDo = void . atomically $ tryTakeTMVar doWork + +getNtfToken_ :: AgentMonad m => m (Maybe NtfToken) +getNtfToken_ = do + tkn <- asks $ ntfTkn . ntfSupervisor + readTVarIO tkn nsUpdateToken :: NtfSupervisor -> NtfToken -> STM () nsUpdateToken ns tkn = writeTVar (ntfTkn ns) $ Just tkn @@ -122,7 +177,7 @@ nsUpdateToken ns tkn = writeTVar (ntfTkn ns) $ Just tkn nsRemoveNtfToken :: NtfSupervisor -> STM () nsRemoveNtfToken ns = writeTVar (ntfTkn ns) Nothing -sendNtfSubCommand :: NtfSupervisor -> (RcvQueue, NtfSupervisorCommand) -> STM () +sendNtfSubCommand :: NtfSupervisor -> (ConnId, NtfSupervisorCommand) -> STM () sendNtfSubCommand ns cmd = readTVar (ntfTkn ns) >>= mapM_ (\NtfToken {ntfTknStatus} -> when (ntfTknStatus == NTActive) $ writeTBQueue (ntfSubQ ns) cmd) diff --git a/src/Simplex/Messaging/Agent/Store.hs b/src/Simplex/Messaging/Agent/Store.hs index 5fd7dfa4f..63e581f16 100644 --- a/src/Simplex/Messaging/Agent/Store.hs +++ b/src/Simplex/Messaging/Agent/Store.hs @@ -26,6 +26,9 @@ import Simplex.Messaging.Protocol ( MsgBody, MsgFlags, MsgId, + NotifierId, + NtfPrivateSignKey, + NtfPublicVerifyKey, RcvDhSecret, RcvPrivateSignKey, SndPrivateSignKey, @@ -50,6 +53,10 @@ class Monad m => MonadAgentStore s m where setSndQueueStatus :: s -> SndQueue -> QueueStatus -> m () getRcvQueue :: s -> ConnId -> m RcvQueue + -- RcvQueue notifier key and ID + setRcvQueueNotifierKey :: s -> ConnId -> NtfPublicVerifyKey -> NtfPrivateSignKey -> m () + setRcvQueueNotifierId :: s -> ConnId -> NotifierId -> m () + -- Confirmations createConfirmation :: s -> TVar ChaChaDRG -> NewConfirmation -> m ConfirmationId acceptConfirmation :: s -> ConfirmationId -> ConnInfo -> m AcceptedConfirmation @@ -84,20 +91,19 @@ class Monad m => MonadAgentStore s m where -- Notification device token persistence createNtfToken :: s -> NtfToken -> m () - getDeviceNtfToken :: s -> DeviceToken -> m (Maybe NtfToken, [NtfToken]) updateNtfTokenRegistration :: s -> NtfToken -> NtfTokenId -> C.DhSecretX25519 -> m () updateNtfToken :: s -> NtfToken -> NtfTknStatus -> Maybe NtfTknAction -> m () removeNtfToken :: s -> NtfToken -> m () -- Notification subscription persistence - getNtfSubscription :: s -> RcvQueue -> m (Maybe NtfSubscription) - createNtfSubscription :: s -> NtfSubscription -> m () - markNtfSubscriptionForDeletion :: s -> RcvQueue -> m () - updateNtfSubscription :: s -> RcvQueue -> NtfSubscription -> m () - deleteNtfSubscription :: s -> RcvQueue -> m () - getNextNtfSubAction :: s -> NtfServer -> m (Maybe (NtfSubscription, Maybe NtfSubAction)) - getNextNtfSubSMPAction :: s -> SMPServer -> m (Maybe (NtfSubscription, Maybe NtfSubSMPAction)) + getNtfSubscription :: s -> ConnId -> m (Maybe NtfSubscription) + createNtfSubscription :: s -> NtfSubscription -> NtfSubOrSMPAction -> m () + markNtfSubscriptionForDeletion :: s -> ConnId -> m () + updateNtfSubscription :: s -> ConnId -> NtfSubscription -> NtfSubOrSMPAction -> m () + deleteNtfSubscription :: s -> ConnId -> m () + getNextNtfSubAction :: s -> NtfServer -> m (Maybe (NtfSubscription, NtfSubAction, RcvQueue)) + getNextNtfSubSMPAction :: s -> SMPServer -> m (Maybe (NtfSubscription, NtfSubSMPAction, RcvQueue)) -- * Queue types @@ -117,7 +123,12 @@ data RcvQueue = RcvQueue -- | sender queue ID sndId :: Maybe SMP.SenderId, -- | queue status - status :: QueueStatus + status :: QueueStatus, + -- | key pair to be used by the notification server to sign transmissions + ntfPublicKey :: Maybe NtfPublicVerifyKey, + ntfPrivateKey :: Maybe NtfPrivateSignKey, + -- | queue ID to be used by the notification server for NSUB command + notifierId :: Maybe NotifierId } deriving (Eq, Show) @@ -354,13 +365,3 @@ data StoreError | -- | Used to wrap agent errors inside store operations to avoid race conditions SEAgentError AgentErrorType deriving (Eq, Show, Exception) - --- * Ntf Subscriptions - --- data NewNtfSubscription = NewNtfSubscription --- { ntfServer :: NtfServer, --- ntfSubId :: NtfSubscriptionId, --- ratchetState :: RatchetX448 --- } - -data NtfSubscriptionAction = NSubSMPAction SMPServer | NSubNtfAction NtfServer diff --git a/src/Simplex/Messaging/Agent/Store/SQLite.hs b/src/Simplex/Messaging/Agent/Store/SQLite.hs index 6fcd49ffc..d14e1a350 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite.hs @@ -60,10 +60,10 @@ import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Crypto.Ratchet (RatchetX448, SkippedMsgDiff (..), SkippedMsgKeys) import Simplex.Messaging.Encoding import Simplex.Messaging.Encoding.String -import Simplex.Messaging.Notifications.Client (NtfServer, NtfSubAction, NtfSubSMPAction, NtfSubscription, NtfTknAction, NtfToken (..)) +import Simplex.Messaging.Notifications.Client (NtfServer, NtfSubAction, NtfSubOrSMPAction (..), NtfSubSMPAction, NtfSubscription (..), NtfTknAction, NtfToken (..)) import Simplex.Messaging.Notifications.Protocol (DeviceToken (..), NtfTknStatus (..), NtfTokenId) import Simplex.Messaging.Parsers (blobFieldParser, fromTextField_) -import Simplex.Messaging.Protocol (MsgBody, MsgFlags, ProtocolServer (..)) +import Simplex.Messaging.Protocol (MsgBody, MsgFlags, NotifierId, NtfPrivateSignKey, NtfPublicVerifyKey, ProtocolServer (..)) import qualified Simplex.Messaging.Protocol as SMP import Simplex.Messaging.Util (bshow, eitherToMaybe, liftIOEither) import Simplex.Messaging.Version @@ -302,7 +302,34 @@ instance (MonadUnliftIO m, MonadError StoreError m) => MonadAgentStore SQLiteSto [":status" := status, ":host" := host, ":port" := port, ":snd_id" := sndId] getRcvQueue :: SQLiteStore -> ConnId -> m RcvQueue - getRcvQueue _st _connId = throwError SENotImplemented + getRcvQueue st connId = + liftIOEither . withTransaction st $ \db -> do + rq_ <- getRcvQueueByConnId_ db connId + pure $ maybe (Left SEConnNotFound) Right rq_ + + setRcvQueueNotifierKey :: SQLiteStore -> ConnId -> NtfPublicVerifyKey -> NtfPrivateSignKey -> m () + setRcvQueueNotifierKey st connId ntfPublicKey ntfPrivateKey = + liftIO . withTransaction st $ \db -> + DB.execute + db + [sql| + UPDATE rcv_queues + SET ntf_public_key = ?, ntf_private_key = ? + WHERE conn_id = ? + |] + (ntfPublicKey, ntfPrivateKey, connId) + + setRcvQueueNotifierId :: SQLiteStore -> ConnId -> NotifierId -> m () + setRcvQueueNotifierId st connId nId = + liftIO . withTransaction st $ \db -> + DB.execute + db + [sql| + UPDATE rcv_queues + SET ntf_id = ? + WHERE conn_id = ? + |] + (nId, connId) createConfirmation :: SQLiteStore -> TVar ChaChaDRG -> NewConfirmation -> m ConfirmationId createConfirmation st gVar NewConfirmation {connId, senderConf = SMPConfirmation {senderKey, e2ePubKey, connInfo, smpReplyQueues}, ratchetState} = @@ -663,26 +690,133 @@ instance (MonadUnliftIO m, MonadError StoreError m) => MonadAgentStore SQLiteSto |] (provider, token, host, port) - getNtfSubscription :: SQLiteStore -> RcvQueue -> m (Maybe NtfSubscription) - getNtfSubscription _st _rcvQueue = throwError SENotImplemented + getNtfSubscription :: SQLiteStore -> ConnId -> m (Maybe NtfSubscription) + getNtfSubscription st connId = + liftIO . withTransaction st $ \db -> + maybeFirstRow ntfSubscription $ + DB.query + db + [sql| + SELECT s.host, s.port, s.key_hash, ns.ntf_host, ns.ntf_port, ns.ntf_key_hash, + nsb.smp_ntf_id, nsb.ntf_sub_id, nsb.ntf_sub_status, nsb.ntf_sub_action_ts + FROM ntf_subscriptions nsb + JOIN servers s ON s.host = nsb.smp_host, s.port = nsb.smp_port + JOIN ntf_servers ns USING (ntf_host, ntf_port) + WHERE nsb.conn_id = ? + |] + (Only connId) + where + ntfSubscription (smpHost, smpPort, smpKeyHash, ntfHost, ntfPort, ntfKeyHash, ntfQueueId, ntfSubId, ntfSubStatus, ntfSubActionTs) = + let smpServer = SMPServer smpHost smpPort smpKeyHash + ntfServer = ProtocolServer ntfHost ntfPort ntfKeyHash + in NtfSubscription {connId, smpServer, ntfQueueId, ntfServer, ntfSubId, ntfSubStatus, ntfSubActionTs} - createNtfSubscription :: SQLiteStore -> NtfSubscription -> m () - createNtfSubscription _st _ntfSub = throwError SENotImplemented + createNtfSubscription :: SQLiteStore -> NtfSubscription -> NtfSubOrSMPAction -> m () + createNtfSubscription st NtfSubscription {smpServer = (SMPServer host port _), ntfQueueId, ntfServer = (SMPServer ntfHost ntfPort _), ntfSubId, ntfSubStatus, ntfSubActionTs} ntfAction = + liftIO . withTransaction st $ \db -> + DB.execute + db + [sql| + INSERT INTO ntf_subscriptions + (smp_host, smp_port, smp_ntf_id, ntf_host, ntf_port, ntf_sub_id, + ntf_sub_status, ntf_sub_action, ntf_sub_smp_action, ntf_sub_action_ts) + VALUES (?,?,?,?,?,?,?,?,?,?) + |] + ( (host, port, ntfQueueId, ntfHost, ntfPort, ntfSubId) + :. (ntfSubStatus, ntfSubAction, ntfSubSMPAction, ntfSubActionTs) + ) + where + (ntfSubAction, ntfSubSMPAction) = ntfSubAndSMPAction ntfAction - markNtfSubscriptionForDeletion :: s -> RcvQueue -> m () + markNtfSubscriptionForDeletion :: SQLiteStore -> ConnId -> m () markNtfSubscriptionForDeletion _st _rcvQueue = throwError SENotImplemented - updateNtfSubscription :: s -> RcvQueue -> NtfSubscription -> m () - updateNtfSubscription _st _rcvQueue _ntfSub = throwError SENotImplemented + updateNtfSubscription :: SQLiteStore -> ConnId -> NtfSubscription -> NtfSubOrSMPAction -> m () + updateNtfSubscription st connId NtfSubscription {ntfQueueId, ntfSubId, ntfSubStatus, ntfSubActionTs} ntfAction = + liftIO . withTransaction st $ \db -> do + r <- maybeFirstRow fromOnly $ DB.query db "SELECT updated_by_supervisor FROM ntf_subscriptions WHERE conn_id = ?" (Only connId) + forM_ r $ \updatedBySupervisor -> do + updatedAt <- getCurrentTime + if updatedBySupervisor + then + DB.execute + db + [sql| + UPDATE ntf_subscriptions + SET smp_ntf_id = ?, ntf_sub_id = ?, ntf_sub_status = ?, updated_by_supervisor = ?, updated_at = ? + WHERE conn_id = ? + |] + (ntfQueueId, ntfSubId, ntfSubStatus, False, updatedAt, connId) + else + DB.execute + db + [sql| + UPDATE ntf_subscriptions + SET smp_ntf_id = ?, ntf_sub_id = ?, ntf_sub_status = ?, ntf_sub_action = ?, ntf_sub_smp_action = ?, ntf_sub_action_ts = ?, updated_by_supervisor = ?, updated_at = ? + WHERE conn_id = ? + |] + (ntfQueueId, ntfSubId, ntfSubStatus, ntfSubAction, ntfSubSMPAction, ntfSubActionTs, False, updatedAt, connId) + where + (ntfSubAction, ntfSubSMPAction) = ntfSubAndSMPAction ntfAction - deleteNtfSubscription :: s -> RcvQueue -> m () - deleteNtfSubscription _st _rcvQueue = throwError SENotImplemented + deleteNtfSubscription :: SQLiteStore -> ConnId -> m () + deleteNtfSubscription _st _connId = throwError SENotImplemented - getNextNtfSubAction :: s -> NtfServer -> m (Maybe (NtfSubscription, Maybe NtfSubAction)) - getNextNtfSubAction _st _srv = throwError SENotImplemented + getNextNtfSubAction :: SQLiteStore -> NtfServer -> m (Maybe (NtfSubscription, NtfSubAction, RcvQueue)) + getNextNtfSubAction st ntfServer@(ProtocolServer ntfHost ntfPort _) = + liftIO . withTransaction st $ \db -> do + r <- + maybeFirstRow ntfSubscription $ + DB.query + db + [sql| + SELECT ns.connId, s.host, s.port, s.key_hash, + ns.smp_ntf_id, ns.ntf_sub_id, ns.ntf_sub_status, ns.ntf_sub_action_ts, ns.ntf_sub_action + FROM ntf_subscriptions ns + JOIN servers s ON s.host = ns.smp_host, s.port = ns.smp_port + WHERE ns.ntf_host = ? AND ns.ntf_port = ? AND ns.ntf_sub_action IS NOT NULL + ORDER BY ns.ntf_sub_action_ts ASC + LIMIT 1 + |] + (ntfHost, ntfPort) + case r of + Just (ntfSub@NtfSubscription {connId}, ntfSubAction) -> do + DB.execute db "UPDATE ntf_subscriptions SET updated_by_supervisor = ? WHERE conn_id = ?" (False, connId) + rq_ <- getRcvQueueByConnId_ db connId + pure $ (\rq -> Just (ntfSub, ntfSubAction, rq)) =<< rq_ + Nothing -> pure Nothing + where + ntfSubscription (connId, smpHost, smpPort, smpKeyHash, ntfQueueId, ntfSubId, ntfSubStatus, ntfSubActionTs, ntfSubAction) = + let smpServer = SMPServer smpHost smpPort smpKeyHash + in (NtfSubscription {connId, smpServer, ntfQueueId, ntfServer, ntfSubId, ntfSubStatus, ntfSubActionTs}, ntfSubAction) - getNextNtfSubSMPAction :: s -> SMPServer -> m (Maybe (NtfSubscription, Maybe NtfSubSMPAction)) - getNextNtfSubSMPAction _st _srv = throwError SENotImplemented + getNextNtfSubSMPAction :: SQLiteStore -> SMPServer -> m (Maybe (NtfSubscription, NtfSubSMPAction, RcvQueue)) + getNextNtfSubSMPAction st smpServer@(SMPServer smpHost smpPort _) = + liftIO . withTransaction st $ \db -> do + r <- + maybeFirstRow ntfSubscription $ + DB.query + db + [sql| + SELECT ns.connId, s.ntf_host, s.ntf_port, s.ntf_key_hash, + ns.smp_ntf_id, ns.ntf_sub_id, ns.ntf_sub_status, ns.ntf_sub_action_ts, ns.ntf_sub_smp_action + FROM ntf_subscriptions ns + JOIN ntf_servers s USING (ntf_host, ntf_port) + WHERE ns.smp_host = ? AND ns.smp_port = ? AND ns.ntf_sub_smp_action IS NOT NULL + ORDER BY ns.ntf_sub_action_ts ASC + LIMIT 1 + |] + (smpHost, smpPort) + case r of + Just (ntfSub@NtfSubscription {connId}, ntfSubAction) -> do + DB.execute db "UPDATE ntf_subscriptions SET updated_by_supervisor = ? WHERE conn_id = ?" (False, connId) + rq_ <- getRcvQueueByConnId_ db connId + pure $ (\rq -> Just (ntfSub, ntfSubAction, rq)) =<< rq_ + Nothing -> pure Nothing + where + ntfSubscription (connId, ntfHost, ntfPort, ntfKeyHash, ntfQueueId, ntfSubId, ntfSubStatus, ntfSubActionTs, ntfSubAction) = + let ntfServer = ProtocolServer ntfHost ntfPort ntfKeyHash + in (NtfSubscription {connId, smpServer, ntfQueueId, ntfServer, ntfSubId, ntfSubStatus, ntfSubActionTs}, ntfSubAction) -- * Auxiliary helpers @@ -745,6 +879,9 @@ listToEither e _ = Left e firstRow :: (a -> b) -> e -> IO [a] -> IO (Either e b) firstRow f e a = second f . listToEither e <$> a +maybeFirstRow :: Functor f => (a -> b) -> f [a] -> f (Maybe b) +maybeFirstRow f q = fmap f . listToMaybe <$> q + -- TODO move from simplex-chat -- firstRow' :: (a -> Either e b) -> e -> IO [a] -> IO (Either e b) -- firstRow' f e a = (f <=< listToEither e) <$> a @@ -855,22 +992,22 @@ getConnData_ dbConn connId' = getRcvQueueByConnId_ :: DB.Connection -> ConnId -> IO (Maybe RcvQueue) getRcvQueueByConnId_ dbConn connId = - rcvQueue + listToMaybe . map rcvQueue <$> DB.query dbConn [sql| SELECT s.key_hash, q.host, q.port, q.rcv_id, q.rcv_private_key, q.rcv_dh_secret, - q.e2e_priv_key, q.e2e_dh_secret, q.snd_id, q.status + q.e2e_priv_key, q.e2e_dh_secret, q.snd_id, q.status, + q.ntf_public_key, q.ntf_private_key, q.ntf_id FROM rcv_queues q INNER JOIN servers s ON q.host = s.host AND q.port = s.port WHERE q.conn_id = ?; |] (Only connId) where - rcvQueue [(keyHash, host, port, rcvId, rcvPrivateKey, rcvDhSecret, e2ePrivKey, e2eDhSecret, sndId, status)] = + rcvQueue ((keyHash, host, port, rcvId, rcvPrivateKey, rcvDhSecret, e2ePrivKey, e2eDhSecret, sndId, status) :. (ntfPublicKey, ntfPrivateKey, notifierId)) = let server = SMPServer host port keyHash - in Just RcvQueue {server, rcvId, rcvPrivateKey, rcvDhSecret, e2ePrivKey, e2eDhSecret, sndId, status} - rcvQueue _ = Nothing + in RcvQueue {server, rcvId, rcvPrivateKey, rcvDhSecret, e2ePrivKey, e2eDhSecret, sndId, status, ntfPublicKey, ntfPrivateKey, notifierId} getSndQueueByConnId_ :: DB.Connection -> ConnId -> IO (Maybe SndQueue) getSndQueueByConnId_ dbConn connId = @@ -1086,3 +1223,7 @@ createWithRandomId gVar create = tryCreate 3 randomId :: TVar ChaChaDRG -> Int -> IO ByteString randomId gVar n = U.encode <$> (atomically . stateTVar gVar $ randomBytesGenerate n) + +ntfSubAndSMPAction :: NtfSubOrSMPAction -> (Maybe NtfSubAction, Maybe NtfSubSMPAction) +ntfSubAndSMPAction (NtfSubAction nsa) = (Just nsa, Nothing) +ntfSubAndSMPAction (NtfSubSMPAction nsa) = (Nothing, Just nsa) diff --git a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220404_ntf_subscriptions_draft.hs b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220404_ntf_subscriptions_draft.hs deleted file mode 100644 index 775d5a175..000000000 --- a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220404_ntf_subscriptions_draft.hs +++ /dev/null @@ -1,41 +0,0 @@ -{-# LANGUAGE QuasiQuotes #-} - -module Simplex.Messaging.Agent.Store.SQLite.Migrations.M20220404_ntf_subscriptions_draft where - -import Database.SQLite.Simple (Query) -import Database.SQLite.Simple.QQ (sql) - -m20220404_ntf_subscriptions_draft :: Query -m20220404_ntf_subscriptions_draft = - [sql| -ALTER TABLE rcv_queues ADD COLUMN ntf_id BLOB; - -ALTER TABLE rcv_queues ADD COLUMN ntf_public_key BLOB; - -ALTER TABLE rcv_queues ADD COLUMN ntf_private_key BLOB; - -CREATE UNIQUE INDEX idx_rcv_queues_ntf ON rcv_queues (host, port, ntf_id); - -CREATE TABLE ntf_subscriptions ( - ntf_host TEXT NOT NULL, - ntf_port TEXT NOT NULL, - ntf_sub_id BLOB, - ntf_sub_status TEXT NOT NULL, -- new, created, active, pending, error_auth - ntf_sub_action TEXT, -- if there is an action required on this subscription: create / check / delete - ntf_sub_smp_action TEXT, -- action with SMP server: nkey; only one of this and ntf_sub_action can (should) be not null in same record - ntf_sub_action_ts TEXT, -- the earliest time for the action, e.g. checks can be scheduled every X hours - ntf_token TEXT NOT NULL, -- or BLOB? - smp_host TEXT NULL, - smp_port TEXT NULL, - smp_rcv_id BLOB NULL, - smp_ntf_id BLOB, - marked_for_deletion INTEGER NOT NULL DEFAULT 0, -- to be checked on updates by workers to not overwrite delete command - created_at TEXT NOT NULL, - updated_at TEXT NOT NULL, -- this is to check subscription status periodically to know when it was last checked - PRIMARY KEY (smp_host, smp_port, smp_rcv_id), - FOREIGN KEY (ntf_host, ntf_port) REFERENCES ntf_servers - ON DELETE RESTRICT ON UPDATE CASCADE, - FOREIGN KEY (smp_host, smp_port, smp_rcv_id) REFERENCES rcv_queues (host, port, rcv_id) - ON DELETE SET NULL ON UPDATE CASCADE -) WITHOUT ROWID; -|] diff --git a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220608_v2.hs b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220608_v2.hs index eb01275a5..5bd85faa1 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220608_v2.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220608_v2.hs @@ -15,4 +15,36 @@ ALTER TABLE conn_confirmations ADD COLUMN smp_reply_queues BLOB NULL; ALTER TABLE connections ADD COLUMN duplex_handshake INTEGER NULL DEFAULT 0; ALTER TABLE rcv_messages ADD COLUMN user_ack INTEGER NULL DEFAULT 0; + +ALTER TABLE rcv_queues ADD COLUMN ntf_public_key BLOB; + +ALTER TABLE rcv_queues ADD COLUMN ntf_private_key BLOB; + +ALTER TABLE rcv_queues ADD COLUMN ntf_id BLOB; + +CREATE UNIQUE INDEX idx_rcv_queues_ntf ON rcv_queues (host, port, ntf_id); + +CREATE TABLE ntf_subscriptions ( + conn_id BLOB NOT NULL, + smp_host TEXT NULL, + smp_port TEXT NULL, + smp_ntf_id BLOB, + ntf_host TEXT NOT NULL, + ntf_port TEXT NOT NULL, + ntf_sub_id BLOB, + ntf_sub_status TEXT NOT NULL, -- see NtfAgentSubStatus + ntf_sub_action TEXT, -- if there is an action required on this subscription: NtfSubAction + ntf_sub_smp_action TEXT, -- action with SMP server: NtfSubSMPAction; only one of this and ntf_sub_action can (should) be not null in same record + ntf_sub_action_ts TEXT, -- the earliest time for the action, e.g. checks can be scheduled every X hours + updated_by_supervisor INTEGER NOT NULL DEFAULT 0, -- to be checked on updates by workers to not overwrite supervisor command (state still should be updated) + created_at TEXT NOT NULL DEFAULT (datetime('now')), + updated_at TEXT NOT NULL DEFAULT (datetime('now')), + PRIMARY KEY (conn_id), + FOREIGN KEY (conn_id) REFERENCES connections + ON DELETE SET NULL ON UPDATE CASCADE, + FOREIGN KEY (smp_host, smp_port) REFERENCES servers (host, port) + ON DELETE SET NULL ON UPDATE CASCADE, + FOREIGN KEY (ntf_host, ntf_port) REFERENCES ntf_servers + ON DELETE RESTRICT ON UPDATE CASCADE +) WITHOUT ROWID; |] diff --git a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql index bd24a4985..47543fa58 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql +++ b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql @@ -36,6 +36,9 @@ CREATE TABLE rcv_queues( status TEXT NOT NULL, smp_server_version INTEGER NOT NULL DEFAULT 1, smp_client_version INTEGER, + ntf_public_key BLOB, + ntf_private_key BLOB, + ntf_id BLOB, PRIMARY KEY(host, port, rcv_id), FOREIGN KEY(host, port) REFERENCES servers ON DELETE RESTRICT ON UPDATE CASCADE, @@ -165,3 +168,27 @@ tkn_dh_secret BLOB, -- DH secret for e2e encryption of notifications FOREIGN KEY(ntf_host, ntf_port) REFERENCES ntf_servers ON DELETE RESTRICT ON UPDATE CASCADE ) WITHOUT ROWID; +CREATE UNIQUE INDEX idx_rcv_queues_ntf ON rcv_queues(host, port, ntf_id); +CREATE TABLE ntf_subscriptions( + conn_id BLOB NOT NULL, + smp_host TEXT NULL, + smp_port TEXT NULL, + smp_ntf_id BLOB, + ntf_host TEXT NOT NULL, + ntf_port TEXT NOT NULL, + ntf_sub_id BLOB, + ntf_sub_status TEXT NOT NULL, -- see NtfAgentSubStatus + ntf_sub_action TEXT, -- if there is an action required on this subscription: NtfSubAction + ntf_sub_smp_action TEXT, -- action with SMP server: NtfSubSMPAction; only one of this and ntf_sub_action can(should) be not null in same record + ntf_sub_action_ts TEXT, -- the earliest time for the action, e.g. checks can be scheduled every X hours + updated_by_supervisor INTEGER NOT NULL DEFAULT 0, -- to be checked on updates by workers to not overwrite supervisor command(state still should be updated) + created_at TEXT NOT NULL DEFAULT(datetime('now')), + updated_at TEXT NOT NULL DEFAULT(datetime('now')), + PRIMARY KEY(conn_id), + FOREIGN KEY(conn_id) REFERENCES connections + ON DELETE SET NULL ON UPDATE CASCADE, + FOREIGN KEY(smp_host, smp_port) REFERENCES servers(host, port) + ON DELETE SET NULL ON UPDATE CASCADE, + FOREIGN KEY(ntf_host, ntf_port) REFERENCES ntf_servers + ON DELETE RESTRICT ON UPDATE CASCADE +) WITHOUT ROWID; diff --git a/src/Simplex/Messaging/Notifications/Client.hs b/src/Simplex/Messaging/Notifications/Client.hs index ae4eaf1ea..d6740f345 100644 --- a/src/Simplex/Messaging/Notifications/Client.hs +++ b/src/Simplex/Messaging/Notifications/Client.hs @@ -10,6 +10,7 @@ module Simplex.Messaging.Notifications.Client where import Control.Monad.Except import Control.Monad.Trans.Except import qualified Data.Attoparsec.ByteString.Char8 as A +import Data.Text.Encoding (decodeLatin1, encodeUtf8) import Data.Time (UTCTime) import Data.Word (Word16) import Database.SQLite.Simple.FromField (FromField (..)) @@ -18,8 +19,9 @@ import Simplex.Messaging.Client import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Encoding import Simplex.Messaging.Notifications.Protocol -import Simplex.Messaging.Parsers (blobFieldDecoder) -import Simplex.Messaging.Protocol (NotifierId, NtfPrivateSignKey, ProtocolServer, RecipientId, SMPServer) +import Simplex.Messaging.Parsers (blobFieldDecoder, fromTextField_) +import Simplex.Messaging.Protocol (NotifierId, ProtocolServer, SMPServer) +import Simplex.Messaging.Agent.Protocol (ConnId) type NtfServer = ProtocolServer @@ -132,20 +134,22 @@ newNtfToken deviceToken ntfServer (ntfPubKey, ntfPrivKey) ntfDhKeys = ntfTknAction = Just NTARegister } +data NtfSubOrSMPAction = NtfSubAction NtfSubAction | NtfSubSMPAction NtfSubSMPAction + data NtfSubAction - = NSANew NtfPrivateSignKey + = NSACreate | NSACheck | NSADelete deriving (Show) instance Encoding NtfSubAction where smpEncode = \case - NSANew nKey -> smpEncode ('N', nKey) + NSACreate -> "N" NSACheck -> "C" NSADelete -> "D" smpP = A.anyChar >>= \case - 'N' -> NSANew <$> smpP + 'N' -> pure NSACreate 'C' -> pure NSACheck 'D' -> pure NSADelete _ -> fail "bad NtfSubAction" @@ -170,27 +174,66 @@ instance FromField NtfSubSMPAction where fromField = blobFieldDecoder smpDecode instance ToField NtfSubSMPAction where toField = toField . smpEncode +data NtfAgentSubStatus + = -- | subscription started + NASNew + | -- | state after NKEY - notifier ID is assigned to queue on SMP server + NASKey + | -- | state after SNEW - subscription created on notification server + NASCreated + | -- | connected and subscribed to SMP server + NASActive + | -- | communicated by notification server that NEND received (we currently do not support it) + NASEnded + | -- | communicated by notification server that SMP AUTH error occured + NASSMPAuth + | -- | state after SDEL (subscription is deleted on notification server) + NASDeleted + deriving (Eq, Show) + +instance Encoding NtfAgentSubStatus where + smpEncode = \case + NASNew -> "NEW" + NASKey -> "NKEY" + NASCreated -> "CREATED" + NASActive -> "ACTIVE" + NASEnded -> "ENDED" + NASSMPAuth -> "SMP_AUTH" + NASDeleted -> "DELETED" + smpP = + A.takeTill (== ' ') >>= \case + "NEW" -> pure NASNew + "NKEY" -> pure NASKey + "CREATED" -> pure NASCreated + "ACTIVE" -> pure NASActive + "ENDED" -> pure NASEnded + "SMP_AUTH" -> pure NASSMPAuth + "DELETED" -> pure NASDeleted + _ -> fail "bad NtfAgentSubStatus" + +instance FromField NtfAgentSubStatus where fromField = fromTextField_ $ either (const Nothing) Just . smpDecode . encodeUtf8 + +instance ToField NtfAgentSubStatus where toField = toField . decodeLatin1 . smpEncode + data NtfSubscription = NtfSubscription - { ntfServer :: NtfServer, + { connId :: ConnId, + smpServer :: SMPServer, + ntfQueueId :: Maybe NotifierId, + ntfServer :: NtfServer, ntfSubId :: Maybe NtfSubscriptionId, - ntfSubStatus :: NtfSubStatus, - ntfSubActionTs :: UTCTime, - ntfToken :: NtfToken, -- ? - smpServer :: SMPServer, -- use SMPQueueNtf? - rcvQueueId :: RecipientId, - ntfQueueId :: Maybe NotifierId + ntfSubStatus :: NtfAgentSubStatus, + ntfSubActionTs :: UTCTime } deriving (Show) -newNtfSubscription :: NtfServer -> NtfToken -> SMPServer -> RecipientId -> UTCTime -> NtfSubscription -newNtfSubscription ntfServer ntfToken smpServer rcvQueueId ntfSubActionTs = +newNtfSubscription :: ConnId -> SMPServer -> Maybe NotifierId -> NtfServer -> NtfAgentSubStatus -> UTCTime -> NtfSubscription +newNtfSubscription connId smpServer ntfQueueId ntfServer ntfSubStatus ntfSubActionTs = NtfSubscription - { ntfServer, - ntfSubId = Nothing, - ntfSubStatus = NSKey, - ntfSubActionTs, - ntfToken, + { connId, smpServer, - rcvQueueId, - ntfQueueId = Nothing + ntfQueueId, + ntfServer, + ntfSubId = Nothing, + ntfSubStatus, + ntfSubActionTs } diff --git a/src/Simplex/Messaging/Notifications/Protocol.hs b/src/Simplex/Messaging/Notifications/Protocol.hs index 83b9d6685..2c33ab86a 100644 --- a/src/Simplex/Messaging/Notifications/Protocol.hs +++ b/src/Simplex/Messaging/Notifications/Protocol.hs @@ -363,9 +363,7 @@ type NtfSubscriptionId = NtfEntityId type NtfTokenId = NtfEntityId data NtfSubStatus - = -- | state after NKEY - NSKey - | -- | state after SNEW + = -- | state after SNEW NSNew | -- | pending connection/subscription to SMP server NSPending @@ -379,7 +377,6 @@ data NtfSubStatus instance Encoding NtfSubStatus where smpEncode = \case - NSKey -> "KEY" NSNew -> "NEW" NSPending -> "PENDING" -- e.g. after SMP server disconnect/timeout while ntf server is retrying to connect NSActive -> "ACTIVE" @@ -387,7 +384,6 @@ instance Encoding NtfSubStatus where NSSMPAuth -> "SMP_AUTH" smpP = A.takeTill (== ' ') >>= \case - "KEY" -> pure NSKey "NEW" -> pure NSNew "PENDING" -> pure NSPending "ACTIVE" -> pure NSActive diff --git a/src/Simplex/Messaging/Protocol.hs b/src/Simplex/Messaging/Protocol.hs index d18800f0e..0c8ff448d 100644 --- a/src/Simplex/Messaging/Protocol.hs +++ b/src/Simplex/Messaging/Protocol.hs @@ -506,10 +506,10 @@ type SndPrivateSignKey = C.APrivateSignKey -- | Sender's public key used by SMP server to verify authorization of SMP commands. type SndPublicVerifyKey = C.APublicVerifyKey --- | Private key used by push notifications server to authorize (sign) LSTN command. +-- | Private key used by push notifications server to authorize (sign) NSUB command. type NtfPrivateSignKey = C.APrivateSignKey --- | Public key used by SMP server to verify authorization of LSTN command sent by push notifications server. +-- | Public key used by SMP server to verify authorization of NSUB command sent by push notifications server. type NtfPublicVerifyKey = C.APublicVerifyKey -- | SMP message server ID. diff --git a/tests/AgentTests/SQLiteTests.hs b/tests/AgentTests/SQLiteTests.hs index af083747c..3ad177918 100644 --- a/tests/AgentTests/SQLiteTests.hs +++ b/tests/AgentTests/SQLiteTests.hs @@ -166,7 +166,10 @@ rcvQueue1 = e2ePrivKey = testPrivDhKey, e2eDhSecret = Nothing, sndId = Just "2345", - status = New + status = New, + ntfPublicKey = Nothing, + ntfPrivateKey = Nothing, + notifierId = Nothing } sndQueue1 :: SndQueue @@ -332,7 +335,10 @@ testUpgradeSndConnToDuplex = e2ePrivKey = testPrivDhKey, e2eDhSecret = Nothing, sndId = Just "4567", - status = New + status = New, + ntfPublicKey = Nothing, + ntfPrivateKey = Nothing, + notifierId = Nothing } upgradeSndConnToDuplex store "conn1" anotherRcvQueue `throwsError` SEBadConnType CRcv From 331918bac75e0dc06493287f52ac89a57dbe10d9 Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Wed, 15 Jun 2022 14:22:55 +0400 Subject: [PATCH 055/131] ntf: agent tests (#399) --- src/Simplex/Messaging/Agent.hs | 15 +- src/Simplex/Messaging/Agent/Client.hs | 2 +- .../Messaging/Agent/NtfSubSupervisor.hs | 18 +-- src/Simplex/Messaging/Agent/Store/SQLite.hs | 16 +-- .../Store/SQLite/Migrations/M20220608_v2.hs | 6 +- .../Store/SQLite/Migrations/agent_schema.sql | 6 +- src/Simplex/Messaging/Server.hs | 2 +- tests/AgentTests/FunctionalAPITests.hs | 9 +- tests/AgentTests/NotificationTests.hs | 130 +++++++++++++++++- tests/NtfServerTests.hs | 8 +- tests/SMPAgentClient.hs | 1 + tests/ServerTests.hs | 11 +- 12 files changed, 179 insertions(+), 45 deletions(-) diff --git a/src/Simplex/Messaging/Agent.hs b/src/Simplex/Messaging/Agent.hs index 42f68bf3a..f958792ac 100644 --- a/src/Simplex/Messaging/Agent.hs +++ b/src/Simplex/Messaging/Agent.hs @@ -239,14 +239,14 @@ processCommand c (connId, cmd) = case cmd of newConn :: AgentMonad m => AgentClient -> ConnId -> SConnectionMode c -> m (ConnId, ConnectionRequestUri c) newConn c connId cMode = do srv <- getSMPServer c - (rq, qUri) <- newRcvQueueNtf c srv connId + (rq, qUri) <- newRcvQueue c srv g <- asks idsDrg agentVersion <- asks $ smpAgentVersion . config let cData = ConnData {connId, connAgentVersion = agentVersion, duplexHandshake = Nothing} -- connection mode is determined by the accepting agent connId' <- withStore c $ \st -> createRcvConn st g cData rq cMode addSubscription c rq connId' ns <- asks ntfSupervisor - atomically $ sendNtfSubCommand ns (connId, NSCCreate) + atomically $ sendNtfSubCommand ns (connId', NSCCreate) aVRange <- asks $ smpAgentVRange . config let crData = ConnReqUriData simplexChat aVRange [qUri] case cMode of @@ -301,11 +301,13 @@ joinConn c connId (CRContactUri (ConnReqUriData _ agentVRange (qUri :| _))) cInf createReplyQueue :: AgentMonad m => AgentClient -> ConnId -> m SMPQueueInfo createReplyQueue c connId = do srv <- getSMPServer c - (rq, qUri) <- newRcvQueueNtf c srv connId + (rq, qUri) <- newRcvQueue c srv -- TODO reply queue version should be the same as send queue, ignoring it in v1 let qInfo = toVersionT qUri SMP.smpClientVersion addSubscription c rq connId withStore c $ \st -> upgradeSndConnToDuplex st connId rq + ns <- asks ntfSupervisor + atomically $ sendNtfSubCommand ns (connId, NSCCreate) pure qInfo -- | Approve confirmation (LET command) in Reader monad @@ -1008,13 +1010,6 @@ agentRatchetDecrypt st connId encAgentMsg = do updateRatchet st connId rc' skippedDiff liftEither $ first (SEAgentError . cryptoError) agentMsgBody_ -newRcvQueueNtf :: AgentMonad m => AgentClient -> SMPServer -> ConnId -> m (RcvQueue, SMPQueueUri) -newRcvQueueNtf c srv connId = do - (rq, qUri) <- newRcvQueue c srv - ns <- asks ntfSupervisor - atomically $ sendNtfSubCommand ns (connId, NSCCreate) - pure (rq, qUri) - newSndQueue :: (MonadUnliftIO m, MonadReader Env m) => Compatible SMPQueueInfo -> m SndQueue newSndQueue qInfo = asks (cmdSignAlg . config) >>= \case diff --git a/src/Simplex/Messaging/Agent/Client.hs b/src/Simplex/Messaging/Agent/Client.hs index 8793c388b..a68cec7b9 100644 --- a/src/Simplex/Messaging/Agent/Client.hs +++ b/src/Simplex/Messaging/Agent/Client.hs @@ -484,7 +484,7 @@ sendConfirmation c sq@SndQueue {server, sndId, sndPublicKey = Just sndPublicKey, withLogClient_ c server sndId "SEND " $ \smp -> do let clientMsg = SMP.ClientMessage (SMP.PHConfirmation sndPublicKey) agentConfirmation msg <- agentCbEncrypt sq e2ePubKey $ smpEncode clientMsg - liftClient SMP $ sendSMPMessage smp Nothing sndId SMP.noMsgFlags msg + liftClient SMP $ sendSMPMessage smp Nothing sndId (SMP.MsgFlags {notification = True}) msg sendConfirmation _ _ _ = throwError $ INTERNAL "sendConfirmation called without snd_queue public key(s) in the database" sendInvitation :: forall m. AgentMonad m => AgentClient -> Compatible SMPQueueInfo -> ConnectionRequestUri 'CMInvitation -> ConnInfo -> m () diff --git a/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs b/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs index f4154a0ed..f428705bc 100644 --- a/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs +++ b/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs @@ -53,6 +53,7 @@ processNtfSub c (connId, cmd) = do ntfServer_ <- getNtfServer c case cmd of NSCCreate -> do + -- TODO merge getNtfSubscription and getRcvQueue into one method to read both in same transaction? sub_ <- withStore c $ \st -> getNtfSubscription st connId RcvQueue {notifierId, server = smpServer} <- withStore c $ \st -> getRcvQueue st connId case (sub_, ntfServer_) of @@ -79,7 +80,7 @@ processNtfSub c (connId, cmd) = do -- TODO idempotently replay commands. addNtfSMPWorker smpServer addNtfWorker ntfServer - _ -> pure () + _ -> pure () -- error - notification server not configured NSCDelete -> do -- TODO delete notifier ID and Key from SMP server (SDEL, then NDEL) withStore c $ \st -> markNtfSubscriptionForDeletion st connId @@ -103,7 +104,8 @@ processNtfSub c (connId, cmd) = do doWork <- newTMVarIO () worker <- async $ runWorker c srv doWork `E.finally` atomically (TM.delete srv ws) atomically $ TM.insert srv (doWork, worker) ws - Just (doWork, _) -> void . atomically $ tryPutTMVar doWork () + Just (doWork, _) -> + void . atomically $ tryPutTMVar doWork () runNtfWorker :: AgentMonad m => AgentClient -> NtfServer -> TMVar () -> m () runNtfWorker c srv doWork = forever $ do @@ -120,10 +122,10 @@ runNtfWorker c srv doWork = forever $ do ts <- liftIO getCurrentTime withStore c $ \st -> updateNtfSubscription st connId ntfSub {ntfSubId = Just ntfSubId, ntfSubStatus = NASCreated, ntfSubActionTs = ts} (NtfSubAction NSACheck) - | otherwise -> pure () -- error - _ -> pure () -- error - NSACheck -> pure () - NSADelete -> pure () + | otherwise -> pure () -- error -- TODO move action further to future + _ -> pure () -- error -- TODO move action further to future + NSACheck -> noWorkToDo + NSADelete -> noWorkToDo Nothing -> noWorkToDo _ -> noWorkToDo delay <- asks $ ntfWorkerThrottle . config @@ -135,7 +137,7 @@ runNtfSMPWorker :: forall m. AgentMonad m => AgentClient -> SMPServer -> TMVar ( runNtfSMPWorker c srv doWork = forever $ do void . atomically $ readTMVar doWork getNtfToken_ >>= \case - Just NtfToken {ntfTknStatus} -> do + Just NtfToken {ntfTknStatus} -> withStore c (`getNextNtfSubSMPAction` srv) >>= \case Just (ntfSub@NtfSubscription {connId, ntfServer}, ntfSubAction, rq@RcvQueue {ntfPublicKey}) -> do case ntfSubAction of @@ -149,7 +151,7 @@ runNtfSMPWorker c srv doWork = forever $ do (ntfPubKey, ntfPrivKey) <- liftIO $ C.generateSignatureKeyPair a withStore c $ \st -> setRcvQueueNotifierKey st connId ntfPubKey ntfPrivKey enableNotificationsWithNKey ntfPubKey - | otherwise -> pure () -- error + | otherwise -> pure () -- error -- TODO move action further to future where enableNotificationsWithNKey ntfPubKey = do nId <- enableQueueNotifications c rq ntfPubKey diff --git a/src/Simplex/Messaging/Agent/Store/SQLite.hs b/src/Simplex/Messaging/Agent/Store/SQLite.hs index d14e1a350..e9784b42d 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite.hs @@ -700,7 +700,7 @@ instance (MonadUnliftIO m, MonadError StoreError m) => MonadAgentStore SQLiteSto SELECT s.host, s.port, s.key_hash, ns.ntf_host, ns.ntf_port, ns.ntf_key_hash, nsb.smp_ntf_id, nsb.ntf_sub_id, nsb.ntf_sub_status, nsb.ntf_sub_action_ts FROM ntf_subscriptions nsb - JOIN servers s ON s.host = nsb.smp_host, s.port = nsb.smp_port + JOIN servers s ON s.host = nsb.smp_host AND s.port = nsb.smp_port JOIN ntf_servers ns USING (ntf_host, ntf_port) WHERE nsb.conn_id = ? |] @@ -712,17 +712,17 @@ instance (MonadUnliftIO m, MonadError StoreError m) => MonadAgentStore SQLiteSto in NtfSubscription {connId, smpServer, ntfQueueId, ntfServer, ntfSubId, ntfSubStatus, ntfSubActionTs} createNtfSubscription :: SQLiteStore -> NtfSubscription -> NtfSubOrSMPAction -> m () - createNtfSubscription st NtfSubscription {smpServer = (SMPServer host port _), ntfQueueId, ntfServer = (SMPServer ntfHost ntfPort _), ntfSubId, ntfSubStatus, ntfSubActionTs} ntfAction = + createNtfSubscription st NtfSubscription {connId, smpServer = (SMPServer host port _), ntfQueueId, ntfServer = (SMPServer ntfHost ntfPort _), ntfSubId, ntfSubStatus, ntfSubActionTs} ntfAction = liftIO . withTransaction st $ \db -> DB.execute db [sql| INSERT INTO ntf_subscriptions - (smp_host, smp_port, smp_ntf_id, ntf_host, ntf_port, ntf_sub_id, + (conn_id, smp_host, smp_port, smp_ntf_id, ntf_host, ntf_port, ntf_sub_id, ntf_sub_status, ntf_sub_action, ntf_sub_smp_action, ntf_sub_action_ts) - VALUES (?,?,?,?,?,?,?,?,?,?) + VALUES (?,?,?,?,?,?,?,?,?,?,?) |] - ( (host, port, ntfQueueId, ntfHost, ntfPort, ntfSubId) + ( (connId, host, port, ntfQueueId, ntfHost, ntfPort, ntfSubId) :. (ntfSubStatus, ntfSubAction, ntfSubSMPAction, ntfSubActionTs) ) where @@ -770,10 +770,10 @@ instance (MonadUnliftIO m, MonadError StoreError m) => MonadAgentStore SQLiteSto DB.query db [sql| - SELECT ns.connId, s.host, s.port, s.key_hash, + SELECT ns.conn_id, s.host, s.port, s.key_hash, ns.smp_ntf_id, ns.ntf_sub_id, ns.ntf_sub_status, ns.ntf_sub_action_ts, ns.ntf_sub_action FROM ntf_subscriptions ns - JOIN servers s ON s.host = ns.smp_host, s.port = ns.smp_port + JOIN servers s ON s.host = ns.smp_host AND s.port = ns.smp_port WHERE ns.ntf_host = ? AND ns.ntf_port = ? AND ns.ntf_sub_action IS NOT NULL ORDER BY ns.ntf_sub_action_ts ASC LIMIT 1 @@ -798,7 +798,7 @@ instance (MonadUnliftIO m, MonadError StoreError m) => MonadAgentStore SQLiteSto DB.query db [sql| - SELECT ns.connId, s.ntf_host, s.ntf_port, s.ntf_key_hash, + SELECT ns.conn_id, s.ntf_host, s.ntf_port, s.ntf_key_hash, ns.smp_ntf_id, ns.ntf_sub_id, ns.ntf_sub_status, ns.ntf_sub_action_ts, ns.ntf_sub_smp_action FROM ntf_subscriptions ns JOIN ntf_servers s USING (ntf_host, ntf_port) diff --git a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220608_v2.hs b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220608_v2.hs index 5bd85faa1..2760322d5 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220608_v2.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220608_v2.hs @@ -25,7 +25,7 @@ ALTER TABLE rcv_queues ADD COLUMN ntf_id BLOB; CREATE UNIQUE INDEX idx_rcv_queues_ntf ON rcv_queues (host, port, ntf_id); CREATE TABLE ntf_subscriptions ( - conn_id BLOB NOT NULL, + conn_id BLOB NOT NULL, -- ? make nullable for when connection is deleted but we still need to delete subscription smp_host TEXT NULL, smp_port TEXT NULL, smp_ntf_id BLOB, @@ -40,8 +40,8 @@ CREATE TABLE ntf_subscriptions ( created_at TEXT NOT NULL DEFAULT (datetime('now')), updated_at TEXT NOT NULL DEFAULT (datetime('now')), PRIMARY KEY (conn_id), - FOREIGN KEY (conn_id) REFERENCES connections - ON DELETE SET NULL ON UPDATE CASCADE, + -- FOREIGN KEY (conn_id) REFERENCES connections + -- ON DELETE SET NULL ON UPDATE CASCADE, FOREIGN KEY (smp_host, smp_port) REFERENCES servers (host, port) ON DELETE SET NULL ON UPDATE CASCADE, FOREIGN KEY (ntf_host, ntf_port) REFERENCES ntf_servers diff --git a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql index 47543fa58..bdd3efeab 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql +++ b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql @@ -170,7 +170,7 @@ tkn_dh_secret BLOB, -- DH secret for e2e encryption of notifications ) WITHOUT ROWID; CREATE UNIQUE INDEX idx_rcv_queues_ntf ON rcv_queues(host, port, ntf_id); CREATE TABLE ntf_subscriptions( - conn_id BLOB NOT NULL, + conn_id BLOB NOT NULL, -- ? make nullable for when connection is deleted but we still need to delete subscription smp_host TEXT NULL, smp_port TEXT NULL, smp_ntf_id BLOB, @@ -185,8 +185,8 @@ CREATE TABLE ntf_subscriptions( created_at TEXT NOT NULL DEFAULT(datetime('now')), updated_at TEXT NOT NULL DEFAULT(datetime('now')), PRIMARY KEY(conn_id), - FOREIGN KEY(conn_id) REFERENCES connections - ON DELETE SET NULL ON UPDATE CASCADE, + -- FOREIGN KEY(conn_id) REFERENCES connections + -- ON DELETE SET NULL ON UPDATE CASCADE, FOREIGN KEY(smp_host, smp_port) REFERENCES servers(host, port) ON DELETE SET NULL ON UPDATE CASCADE, FOREIGN KEY(ntf_host, ntf_port) REFERENCES ntf_servers diff --git a/src/Simplex/Messaging/Server.hs b/src/Simplex/Messaging/Server.hs index 95fa0fc92..daeb49601 100644 --- a/src/Simplex/Messaging/Server.hs +++ b/src/Simplex/Messaging/Server.hs @@ -504,7 +504,7 @@ client clnt@Client {subscriptions, ntfSubscriptions, rcvQ, sndQ} Server {subscri q <- getMsgQueue ms (recipientId qr) msgQueueQuota mapM_ (deleteExpiredMsgs q) old ifM (isFull q) (pure $ err QUOTA) $ do - trySendNotification + when (notification flags) trySendNotification writeMsg q msg pure ok when (sent == OK) $ do diff --git a/tests/AgentTests/FunctionalAPITests.hs b/tests/AgentTests/FunctionalAPITests.hs index 656ca6f99..b630e95cc 100644 --- a/tests/AgentTests/FunctionalAPITests.hs +++ b/tests/AgentTests/FunctionalAPITests.hs @@ -6,7 +6,14 @@ {-# LANGUAGE PatternSynonyms #-} {-# OPTIONS_GHC -fno-warn-incomplete-uni-patterns #-} -module AgentTests.FunctionalAPITests (functionalAPITests) where +module AgentTests.FunctionalAPITests + ( functionalAPITests, + get, + (##>), + (=##>), + pattern Msg, + ) +where import Control.Concurrent (threadDelay) import Control.Monad.Except (ExceptT, runExceptT) diff --git a/tests/AgentTests/NotificationTests.hs b/tests/AgentTests/NotificationTests.hs index 599e421f6..1bf9b919e 100644 --- a/tests/AgentTests/NotificationTests.hs +++ b/tests/AgentTests/NotificationTests.hs @@ -1,10 +1,15 @@ +{-# LANGUAGE GADTs #-} +{-# LANGUAGE LambdaCase #-} {-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE PatternSynonyms #-} +{-# OPTIONS_GHC -fno-warn-incomplete-uni-patterns #-} module AgentTests.NotificationTests where -- import Control.Logger.Simple (LogConfig (..), LogLevel (..), setLogLevel, withGlobalLogging) +import AgentTests.FunctionalAPITests (get, (##>), (=##>), pattern Msg) import Control.Concurrent (threadDelay) import Control.Monad.Except import qualified Data.Aeson as J @@ -15,6 +20,7 @@ import Data.ByteString.Char8 (ByteString) import Data.Text.Encoding (encodeUtf8) import NtfClient import SMPAgentClient (agentCfg, initAgentServers, testDB, testDB2) +import SMPClient (withSmpServer) import Simplex.Messaging.Agent import Simplex.Messaging.Agent.Env.SQLite (AgentConfig (..)) import Simplex.Messaging.Agent.Protocol @@ -22,15 +28,16 @@ import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Notifications.Protocol import Simplex.Messaging.Notifications.Server.Push.APNS import Simplex.Messaging.Protocol (ErrorType (AUTH)) +import qualified Simplex.Messaging.Protocol as SMP import Simplex.Messaging.Transport (ATransport) import Simplex.Messaging.Util (tryE) import System.Directory (removeFile) import Test.Hspec -import UnliftIO.STM +import UnliftIO notificationTests :: ATransport -> Spec notificationTests t = - after_ (removeFile testDB) $ + after_ (removeFile testDB) $ do describe "Managing notification tokens" $ do it "should register and verify notification token" $ withAPNSMockServer $ \apns -> @@ -44,6 +51,15 @@ notificationTests t = it "should re-register token when notification server is restarted" $ \_ -> withAPNSMockServer $ \apns -> testNtfTokenServerRestart t apns + describe "Managing notification subscriptions" $ do + it "should create notification subscription for existing connection" $ \_ -> + withSmpServer t $ + withAPNSMockServer $ \apns -> + withNtfServer t $ testNotificationSubscriptionExistingConnection apns + it "should create notification subscription for new connection" $ \_ -> + withSmpServer t $ + withAPNSMockServer $ \apns -> + withNtfServer t $ testNotificationSubscriptionNewConnection apns testNotificationToken :: APNSMockServer -> IO () testNotificationToken APNSMockServer {apnsQ} = do @@ -165,3 +181,113 @@ testNtfTokenServerRestart t APNSMockServer {apnsQ} = do NTActive <- checkNtfToken a' tkn enableNtfCron a' tkn 30 pure () + +testNotificationSubscriptionExistingConnection :: APNSMockServer -> IO () +testNotificationSubscriptionExistingConnection APNSMockServer {apnsQ} = do + alice <- getSMPAgentClient agentCfg initAgentServers + bob <- getSMPAgentClient agentCfg {dbFile = testDB2} initAgentServers + Right () <- runExceptT $ do + -- establish connection + (bobId, qInfo) <- createConnection alice SCMInvitation + aliceId <- joinConnection bob qInfo "bob's connInfo" + ("", _, CONF confId "bob's connInfo") <- get alice + allowConnection alice bobId confId "alice's connInfo" + get bob ##> ("", aliceId, INFO "alice's connInfo") + get alice ##> ("", bobId, CON) + get bob ##> ("", aliceId, CON) + -- register notification token + let tkn = DeviceToken PPApns "abcd" + NTRegistered <- registerNtfToken alice tkn + APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData}, sendApnsResponse} <- + atomically $ readTBQueue apnsQ + verification <- ntfData .-> "verification" + nonce <- C.cbNonce <$> ntfData .-> "nonce" + liftIO $ sendApnsResponse APNSRespOk + verifyNtfToken alice tkn verification nonce + NTActive <- checkNtfToken alice tkn + -- send message + liftIO $ threadDelay 50000 + 1 <- msgId <$> sendMessage bob aliceId (SMP.MsgFlags True) "hello" + get bob ##> ("", aliceId, SENT $ baseId + 1) + messageNotification apnsQ + get alice =##> \case ("", c, Msg "hello") -> c == bobId; _ -> False + ackMessage alice bobId $ baseId + 1 + -- no unexpected notifications should follow + 500000 `timeout` atomically (readTBQueue apnsQ) >>= \case + Nothing -> pure () + _ -> error "unexpected notification" + pure () + where + baseId = 3 + msgId = subtract baseId + +testNotificationSubscriptionNewConnection :: APNSMockServer -> IO () +testNotificationSubscriptionNewConnection APNSMockServer {apnsQ} = do + alice <- getSMPAgentClient agentCfg initAgentServers + bob <- getSMPAgentClient agentCfg {dbFile = testDB2} initAgentServers + Right () <- runExceptT $ do + -- alice registers notification token + let aliceTkn = DeviceToken PPApns "abcd" + NTRegistered <- registerNtfToken alice aliceTkn + APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData}, sendApnsResponse} <- + atomically $ readTBQueue apnsQ + verification <- ntfData .-> "verification" + nonce <- C.cbNonce <$> ntfData .-> "nonce" + liftIO $ sendApnsResponse APNSRespOk + verifyNtfToken alice aliceTkn verification nonce + NTActive <- checkNtfToken alice aliceTkn + -- bob registers notification token + let bobTkn = DeviceToken PPApns "bcde" + NTRegistered <- registerNtfToken bob bobTkn + APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData'}, sendApnsResponse = sendApnsResponse'} <- + atomically $ readTBQueue apnsQ + verification' <- ntfData' .-> "verification" + nonce' <- C.cbNonce <$> ntfData' .-> "nonce" + liftIO $ sendApnsResponse' APNSRespOk + verifyNtfToken bob bobTkn verification' nonce' + NTActive <- checkNtfToken bob bobTkn + -- establish connection + liftIO $ threadDelay 50000 + (bobId, qInfo) <- createConnection alice SCMInvitation + liftIO $ threadDelay 500000 + aliceId <- joinConnection bob qInfo "bob's connInfo" + messageNotification apnsQ + ("", _, CONF confId "bob's connInfo") <- get alice + liftIO $ threadDelay 500000 + allowConnection alice bobId confId "alice's connInfo" + messageNotification apnsQ + get bob ##> ("", aliceId, INFO "alice's connInfo") + messageNotification apnsQ + get alice ##> ("", bobId, CON) + messageNotification apnsQ + get bob ##> ("", aliceId, CON) + -- bob sends message + 1 <- msgId <$> sendMessage bob aliceId (SMP.MsgFlags True) "hello" + get bob ##> ("", aliceId, SENT $ baseId + 1) + messageNotification apnsQ + get alice =##> \case ("", c, Msg "hello") -> c == bobId; _ -> False + ackMessage alice bobId $ baseId + 1 + -- alice sends message + 2 <- msgId <$> sendMessage alice bobId (SMP.MsgFlags True) "hey there" + get alice ##> ("", bobId, SENT $ baseId + 2) + messageNotification apnsQ + get bob =##> \case ("", c, Msg "hey there") -> c == aliceId; _ -> False + ackMessage bob aliceId $ baseId + 2 + -- no unexpected notifications should follow + 500000 `timeout` atomically (readTBQueue apnsQ) >>= \case + Nothing -> pure () + _ -> error "unexpected notification" + pure () + where + baseId = 3 + msgId = subtract baseId + +messageNotification :: TBQueue APNSMockRequest -> ExceptT AgentErrorType IO () +messageNotification apnsQ = do + 500000 `timeout` atomically (readTBQueue apnsQ) >>= \case + Nothing -> error "no notification" + Just APNSMockRequest {notification = APNSNotification {aps = APNSMutableContent {}, notificationData = Just ntfData}, sendApnsResponse} -> do + _ <- ntfData .-> "checkMessage" + _ <- C.cbNonce <$> ntfData .-> "nonce" + liftIO $ sendApnsResponse APNSRespOk + _ -> error "bad notification" diff --git a/tests/NtfServerTests.hs b/tests/NtfServerTests.hs index 91ad1bf9c..99955cc5f 100644 --- a/tests/NtfServerTests.hs +++ b/tests/NtfServerTests.hs @@ -24,7 +24,7 @@ import ServerTests sampleSig, signSendRecv, (#==), - _SEND, + _SEND', pattern Resp, ) import qualified Simplex.Messaging.Crypto as C @@ -40,7 +40,7 @@ import UnliftIO.STM ntfServerTests :: ATransport -> Spec ntfServerTests t = do describe "Notifications server protocol syntax" $ ntfSyntaxTests t - describe "Managing notification subscriptions" $ testNotificationSubscription t + describe "Notification subscriptions" $ testNotificationSubscription t ntfSyntaxTests :: ATransport -> Spec ntfSyntaxTests (ATransport t) = do @@ -81,7 +81,7 @@ v .-> key = testNotificationSubscription :: ATransport -> Spec testNotificationSubscription (ATransport t) = - it "should create new notification subscription and notify when message is received" $ do + it "should create notification subscription and notify when message is received" $ do (sPub, sKey) <- C.generateSignatureKeyPair C.SEd25519 (nPub, nKey) <- C.generateSignatureKeyPair C.SEd25519 (tknPub, tknKey) <- C.generateSignatureKeyPair C.SEd25519 @@ -110,7 +110,7 @@ testNotificationSubscription (ATransport t) = RespNtf "4" _ (NRSubId _subId) <- signSendRecvNtf nh tknKey ("4", "", SNEW $ NewNtfSub tId q nKey) -- send message threadDelay 50000 - Resp "5" _ OK <- signSendRecv sh sKey ("5", sId, _SEND "hello") + Resp "5" _ OK <- signSendRecv sh sKey ("5", sId, _SEND' "hello") -- receive notification APNSMockRequest {notification, sendApnsResponse = send'} <- atomically $ readTBQueue apnsQ let APNSNotification {aps = APNSMutableContent {}, notificationData = Just ntfData'} = notification diff --git a/tests/SMPAgentClient.hs b/tests/SMPAgentClient.hs index 0865e7208..fd3d5b980 100644 --- a/tests/SMPAgentClient.hs +++ b/tests/SMPAgentClient.hs @@ -183,6 +183,7 @@ agentCfg = defaultTransport = (ntfTestPort, transport @TLS) }, reconnectInterval = defaultReconnectInterval {initialInterval = 50_000}, + ntfWorkerThrottle = 1000, caCertificateFile = "tests/fixtures/ca.crt", privateKeyFile = "tests/fixtures/server.key", certificateFile = "tests/fixtures/server.crt" diff --git a/tests/ServerTests.hs b/tests/ServerTests.hs index 6a3b38ab1..706ac938a 100644 --- a/tests/ServerTests.hs +++ b/tests/ServerTests.hs @@ -77,6 +77,9 @@ signSendRecv h@THandle {thVersion, sessionId} pk (corrId, qId, cmd) = do _SEND :: MsgBody -> Command 'Sender _SEND = SEND noMsgFlags +_SEND' :: MsgBody -> Command 'Sender +_SEND' = SEND MsgFlags {notification = True} + testCreateSecure :: ATransport -> Spec testCreateSecure (ATransport t) = it "should create (NEW) and secure (KEY) queue" $ @@ -410,7 +413,7 @@ testWithStoreLog at@(ATransport t) = writeTVar senderId1 sId1 writeTVar notifierId nId Resp "dabc" _ OK <- signSendRecv h1 nKey ("dabc", nId, NSUB) - Resp "bcda" _ OK <- signSendRecv h sKey1 ("bcda", sId1, _SEND "hello") + Resp "bcda" _ OK <- signSendRecv h sKey1 ("bcda", sId1, _SEND' "hello") Resp "" _ (MSG mId1 _ _ msg1) <- tGet h (C.cbDecrypt dhShared (C.cbNonce mId1) msg1, Right "hello") #== "delivered from queue 1" Resp "" _ NMSG <- tGet h1 @@ -440,7 +443,7 @@ testWithStoreLog at@(ATransport t) = sId1 <- readTVarIO senderId1 nId <- readTVarIO notifierId Resp "dabc" _ OK <- signSendRecv h1 nKey ("dabc", nId, NSUB) - Resp "bcda" _ OK <- signSendRecv h sKey1 ("bcda", sId1, _SEND "hello") + Resp "bcda" _ OK <- signSendRecv h sKey1 ("bcda", sId1, _SEND' "hello") Resp "cdab" _ (MSG mId3 _ _ msg3) <- signSendRecv h rKey1 ("cdab", rId1, SUB) (C.cbDecrypt dh1 (C.cbNonce mId3) msg3, Right "hello") #== "delivered from restored queue" Resp "" _ NMSG <- tGet h1 @@ -608,14 +611,14 @@ testMessageNotifications (ATransport t) = let dec nonce = C.cbDecrypt dhShared (C.cbNonce nonce) Resp "1" _ (NID nId) <- signSendRecv rh rKey ("1", rId, NKEY nPub) Resp "2" _ OK <- signSendRecv nh1 nKey ("2", nId, NSUB) - Resp "3" _ OK <- signSendRecv sh sKey ("3", sId, _SEND "hello") + Resp "3" _ OK <- signSendRecv sh sKey ("3", sId, _SEND' "hello") Resp "" _ (MSG mId1 _ _ msg1) <- tGet rh (dec mId1 msg1, Right "hello") #== "delivered from queue" Resp "3a" _ OK <- signSendRecv rh rKey ("3a", rId, ACK mId1) Resp "" _ NMSG <- tGet nh1 Resp "4" _ OK <- signSendRecv nh2 nKey ("4", nId, NSUB) Resp "" _ END <- tGet nh1 - Resp "5" _ OK <- signSendRecv sh sKey ("5", sId, _SEND "hello again") + Resp "5" _ OK <- signSendRecv sh sKey ("5", sId, _SEND' "hello again") Resp "" _ (MSG mId2 _ _ msg2) <- tGet rh (dec mId2 msg2, Right "hello again") #== "delivered from queue again" Resp "" _ NMSG <- tGet nh2 From 5f94240ed420c42c725810b72b7f512ab320a98f Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Wed, 15 Jun 2022 14:11:03 +0100 Subject: [PATCH 056/131] remove DB connection pool to prepare for using semaphores (#400) --- src/Simplex/Messaging/Agent/Env/SQLite.hs | 6 ++---- src/Simplex/Messaging/Agent/Store/SQLite.hs | 24 ++++++++++----------- tests/AgentTests/SQLiteTests.hs | 6 +++--- tests/AgentTests/SchemaDump.hs | 2 +- 4 files changed, 17 insertions(+), 21 deletions(-) diff --git a/src/Simplex/Messaging/Agent/Env/SQLite.hs b/src/Simplex/Messaging/Agent/Env/SQLite.hs index 68815a4dc..e41abe6dc 100644 --- a/src/Simplex/Messaging/Agent/Env/SQLite.hs +++ b/src/Simplex/Messaging/Agent/Env/SQLite.hs @@ -63,7 +63,6 @@ data AgentConfig = AgentConfig connIdBytes :: Int, tbqSize :: Natural, dbFile :: FilePath, - dbPoolSize :: Int, yesToMigrations :: Bool, smpCfg :: ProtocolClientConfig, ntfCfg :: ProtocolClientConfig, @@ -96,7 +95,6 @@ defaultAgentConfig = connIdBytes = 12, tbqSize = 64, dbFile = "smp-agent.db", - dbPoolSize = 1, yesToMigrations = False, smpCfg = defaultClientConfig {defaultTransport = ("5223", transport @TLS)}, ntfCfg = defaultClientConfig {defaultTransport = ("443", transport @TLS)}, @@ -134,9 +132,9 @@ disallowedOperations = \case APSuspended -> [AONetwork, AODatabase] newSMPAgentEnv :: (MonadUnliftIO m, MonadRandom m) => AgentConfig -> m Env -newSMPAgentEnv config@AgentConfig {dbFile, dbPoolSize, yesToMigrations} = do +newSMPAgentEnv config@AgentConfig {dbFile, yesToMigrations} = do idsDrg <- newTVarIO =<< drgNew - store <- liftIO $ createSQLiteStore dbFile dbPoolSize Migrations.app yesToMigrations + store <- liftIO $ createSQLiteStore dbFile Migrations.app yesToMigrations clientCounter <- newTVarIO 0 randomServer <- newTVarIO =<< liftIO newStdGen agentPhase <- newTVarIO (APActive, True) diff --git a/src/Simplex/Messaging/Agent/Store/SQLite.hs b/src/Simplex/Messaging/Agent/Store/SQLite.hs index e9784b42d..823b69f1e 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite.hs @@ -77,15 +77,15 @@ import qualified UnliftIO.Exception as E data SQLiteStore = SQLiteStore { dbFilePath :: FilePath, - dbConnPool :: TBQueue DB.Connection, + dbConnection :: TMVar DB.Connection, dbNew :: Bool } -createSQLiteStore :: FilePath -> Int -> [Migration] -> Bool -> IO SQLiteStore -createSQLiteStore dbFilePath poolSize migrations yesToMigrations = do +createSQLiteStore :: FilePath -> [Migration] -> Bool -> IO SQLiteStore +createSQLiteStore dbFilePath migrations yesToMigrations = do let dbDir = takeDirectory dbFilePath createDirectoryIfMissing False dbDir - st <- connectSQLiteStore dbFilePath poolSize + st <- connectSQLiteStore dbFilePath checkThreadsafe st migrateSchema st migrations yesToMigrations pure st @@ -121,13 +121,11 @@ confirmOrExit s = do ok <- getLine when (map toLower ok /= "y") exitFailure -connectSQLiteStore :: FilePath -> Int -> IO SQLiteStore -connectSQLiteStore dbFilePath poolSize = do +connectSQLiteStore :: FilePath -> IO SQLiteStore +connectSQLiteStore dbFilePath = do dbNew <- not <$> doesFileExist dbFilePath - dbConnPool <- newTBQueueIO $ toEnum poolSize - replicateM_ poolSize $ - connectDB dbFilePath >>= atomically . writeTBQueue dbConnPool - pure SQLiteStore {dbFilePath, dbConnPool, dbNew} + dbConnection <- newTMVarIO =<< connectDB dbFilePath + pure SQLiteStore {dbFilePath, dbConnection, dbNew} connectDB :: FilePath -> IO DB.Connection connectDB path = do @@ -160,10 +158,10 @@ handleSQLError err e | otherwise = SEInternal $ bshow e withConnection :: SQLiteStore -> (DB.Connection -> IO a) -> IO a -withConnection SQLiteStore {dbConnPool} = +withConnection SQLiteStore {dbConnection} = bracket - (atomically $ readTBQueue dbConnPool) - (atomically . writeTBQueue dbConnPool) + (atomically $ takeTMVar dbConnection) + (atomically . putTMVar dbConnection) withTransaction :: forall a. SQLiteStore -> (DB.Connection -> IO a) -> IO a withTransaction st action = withConnection st $ loop 100 100_000 diff --git a/tests/AgentTests/SQLiteTests.hs b/tests/AgentTests/SQLiteTests.hs index 3ad177918..47a818503 100644 --- a/tests/AgentTests/SQLiteTests.hs +++ b/tests/AgentTests/SQLiteTests.hs @@ -44,7 +44,7 @@ withStore2 = before connect2 . after (removeStore . fst) connect2 :: IO (SQLiteStore, SQLiteStore) connect2 = do s1 <- createStore - s2 <- connectSQLiteStore (dbFilePath s1) 4 + s2 <- connectSQLiteStore (dbFilePath s1) pure (s1, s2) createStore :: IO SQLiteStore @@ -52,7 +52,7 @@ createStore = do -- Randomize DB file name to avoid SQLite IO errors supposedly caused by asynchronous -- IO operations on multiple similarly named files; error seems to be environment specific r <- randomIO :: IO Word32 - createSQLiteStore (testDB <> show r) 4 Migrations.app True + createSQLiteStore (testDB <> show r) Migrations.app True removeStore :: SQLiteStore -> IO () removeStore store = do @@ -60,7 +60,7 @@ removeStore store = do removeFile $ dbFilePath store where close :: SQLiteStore -> IO () - close st = mapM_ DB.close =<< atomically (flushTBQueue $ dbConnPool st) + close st = mapM_ DB.close =<< atomically (tryTakeTMVar $ dbConnection st) returnsResult :: (Eq a, Eq e, Show a, Show e) => ExceptT e IO a -> a -> Expectation action `returnsResult` r = runExceptT action `shouldReturn` Right r diff --git a/tests/AgentTests/SchemaDump.hs b/tests/AgentTests/SchemaDump.hs index 214730012..03baa28b2 100644 --- a/tests/AgentTests/SchemaDump.hs +++ b/tests/AgentTests/SchemaDump.hs @@ -20,7 +20,7 @@ schemaDumpTest = testVerifySchemaDump :: IO () testVerifySchemaDump = do - void $ createSQLiteStore testDB 1 Migrations.app False + void $ createSQLiteStore testDB Migrations.app False void $ readCreateProcess (shell $ "touch " <> schema) "" savedSchema <- readFile schema savedSchema `seq` pure () From 5ccebaeb21fb067c13b4eba5efef182fe872d0e6 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Thu, 16 Jun 2022 08:49:07 +0100 Subject: [PATCH 057/131] fix transport (treat "" as eof, remove threadId), upgrade TLS to 1.6.0 (#401) --- package.yaml | 4 ++-- simplexmq.cabal | 20 ++++++++++---------- src/Simplex/Messaging/Transport.hs | 9 +++++---- src/Simplex/Messaging/Transport/Server.hs | 3 ++- stack.yaml | 6 ++---- 5 files changed, 21 insertions(+), 21 deletions(-) diff --git a/package.yaml b/package.yaml index 6f11d6eec..fd983e476 100644 --- a/package.yaml +++ b/package.yaml @@ -48,7 +48,7 @@ dependencies: - iso8601-time == 0.1.* - memory == 0.15.* - mtl == 2.2.* - - network == 3.1.2.* + - network >= 3.1.2.7 && < 3.2 - network-transport == 0.5.* - optparse-applicative >= 0.15 && < 0.17 - QuickCheck == 2.14.* @@ -61,7 +61,7 @@ dependencies: - text == 1.2.* - time == 1.9.* - time-manager == 0.0.* - - tls >= 1.5.7 && < 1.6 + - tls >= 1.6.0 && < 1.7 - transformers == 0.5.* - unliftio == 0.2.* - unliftio-core == 0.2.* diff --git a/simplexmq.cabal b/simplexmq.cabal index 77318feb3..8ef8fe65e 100644 --- a/simplexmq.cabal +++ b/simplexmq.cabal @@ -116,7 +116,7 @@ library , iso8601-time ==0.1.* , memory ==0.15.* , mtl ==2.2.* - , network ==3.1.2.* + , network >=3.1.2.7 && <3.2 , network-transport ==0.5.* , optparse-applicative >=0.15 && <0.17 , process ==1.6.* @@ -128,7 +128,7 @@ library , text ==1.2.* , time ==1.9.* , time-manager ==0.0.* - , tls >=1.5.7 && <1.6 + , tls >=1.6.0 && <1.7 , transformers ==0.5.* , unliftio ==0.2.* , unliftio-core ==0.2.* @@ -175,7 +175,7 @@ executable ntf-server , iso8601-time ==0.1.* , memory ==0.15.* , mtl ==2.2.* - , network ==3.1.2.* + , network >=3.1.2.7 && <3.2 , network-transport ==0.5.* , optparse-applicative >=0.15 && <0.17 , process ==1.6.* @@ -188,7 +188,7 @@ executable ntf-server , text ==1.2.* , time ==1.9.* , time-manager ==0.0.* - , tls >=1.5.7 && <1.6 + , tls >=1.6.0 && <1.7 , transformers ==0.5.* , unliftio ==0.2.* , unliftio-core ==0.2.* @@ -235,7 +235,7 @@ executable smp-agent , iso8601-time ==0.1.* , memory ==0.15.* , mtl ==2.2.* - , network ==3.1.2.* + , network >=3.1.2.7 && <3.2 , network-transport ==0.5.* , optparse-applicative >=0.15 && <0.17 , process ==1.6.* @@ -248,7 +248,7 @@ executable smp-agent , text ==1.2.* , time ==1.9.* , time-manager ==0.0.* - , tls >=1.5.7 && <1.6 + , tls >=1.6.0 && <1.7 , transformers ==0.5.* , unliftio ==0.2.* , unliftio-core ==0.2.* @@ -295,7 +295,7 @@ executable smp-server , iso8601-time ==0.1.* , memory ==0.15.* , mtl ==2.2.* - , network ==3.1.2.* + , network >=3.1.2.7 && <3.2 , network-transport ==0.5.* , optparse-applicative >=0.15 && <0.17 , process ==1.6.* @@ -308,7 +308,7 @@ executable smp-server , text ==1.2.* , time ==1.9.* , time-manager ==0.0.* - , tls >=1.5.7 && <1.6 + , tls >=1.6.0 && <1.7 , transformers ==0.5.* , unliftio ==0.2.* , unliftio-core ==0.2.* @@ -374,7 +374,7 @@ test-suite smp-server-test , iso8601-time ==0.1.* , memory ==0.15.* , mtl ==2.2.* - , network ==3.1.2.* + , network >=3.1.2.7 && <3.2 , network-transport ==0.5.* , optparse-applicative >=0.15 && <0.17 , process ==1.6.* @@ -388,7 +388,7 @@ test-suite smp-server-test , time ==1.9.* , time-manager ==0.0.* , timeit ==2.0.* - , tls >=1.5.7 && <1.6 + , tls >=1.6.0 && <1.7 , transformers ==0.5.* , unliftio ==0.2.* , unliftio-core ==0.2.* diff --git a/src/Simplex/Messaging/Transport.hs b/src/Simplex/Messaging/Transport.hs index 69dfa30d3..95c9373c9 100644 --- a/src/Simplex/Messaging/Transport.hs +++ b/src/Simplex/Messaging/Transport.hs @@ -214,10 +214,11 @@ instance Transport TLS where readChunks :: ByteString -> IO ByteString readChunks b | B.length b >= n = pure b - | otherwise = readChunks . (b <>) =<< T.recvData tlsContext `E.catch` handleEOF - handleEOF = \case - T.Error_EOF -> E.throwIO TEBadBlock - e -> E.throwIO e + | otherwise = + T.recvData tlsContext >>= \case + -- https://hackage.haskell.org/package/tls-1.6.0/docs/Network-TLS.html#v:recvData + "" -> ioe_EOF + s -> readChunks $ b <> s cPut :: TLS -> ByteString -> IO () cPut tls = T.sendData (tlsContext tls) . BL.fromStrict diff --git a/src/Simplex/Messaging/Transport/Server.hs b/src/Simplex/Messaging/Transport/Server.hs index 3c1604a2f..2920b1d9b 100644 --- a/src/Simplex/Messaging/Transport/Server.hs +++ b/src/Simplex/Messaging/Transport/Server.hs @@ -55,7 +55,8 @@ runTCPServer started port server = do $ \sock -> forever . E.bracketOnError (accept sock) (close . fst) $ \(conn, _peer) -> do -- catchAll_ is needed here in case the connection was closed earlier cId <- atomically $ stateTVar clientId $ \cId -> (cId + 1, cId + 1) - tId <- mkWeakThreadId =<< forkFinally (server conn) (const $ gracefulClose conn 5000 `catchAll_` atomically (TM.delete cId clients)) + let closeConn _ = atomically (TM.delete cId clients) >> gracefulClose conn 5000 `catchAll_` pure () + tId <- mkWeakThreadId =<< server conn `forkFinally` closeConn atomically $ TM.insert cId tId clients closeServer :: TMVar Bool -> TMap Int (Weak ThreadId) -> Socket -> IO () diff --git a/stack.yaml b/stack.yaml index ce1c13455..f6a60ed74 100644 --- a/stack.yaml +++ b/stack.yaml @@ -17,7 +17,7 @@ # # resolver: ./custom-snapshot.yaml # resolver: https://example.com/snapshots/2018-01-01.yaml -resolver: lts-18.21 +resolver: lts-18.28 # User packages to be built. # Various formats can be used as shown in the example below. @@ -38,8 +38,7 @@ extra-deps: - cryptostore-0.2.1.0@sha256:9896e2984f36a1c8790f057fd5ce3da4cbcaf8aa73eb2d9277916886978c5b19,3881 - network-3.1.2.7@sha256:e3d78b13db9512aeb106e44a334ab42b7aa48d26c097299084084cb8be5c5568,4888 - simple-logger-0.1.0@sha256:be8ede4bd251a9cac776533bae7fb643369ebd826eb948a9a18df1a8dd252ff8,1079 - - tls-1.5.7@sha256:1cc30253a9696b65a9cafc0317fbf09f7dcea15e3a145ed6c9c0e28c632fa23a,6991 -# below dependancies are to update Aeson to 2.0.3 + - tls-1.6.0@sha256:7ae39373fd2de27fb80e90f76d22aeeb9a074a0ddd120cbd02c9c52f516a9e55,6987 # below dependancies are to update Aeson to 2.0.3 - OneTuple-0.3.1@sha256:a848c096c9d29e82ffdd30a9998aa2931cbccb3a1bc137539d80f6174d31603e,2262 - attoparsec-0.14.4@sha256:79584bdada8b730cb5138fca8c35c76fbef75fc1d1e01e6b1d815a5ee9843191,5810 - hashable-1.4.0.2@sha256:0cddd0229d1aac305ea0404409c0bbfab81f075817bd74b8b2929eff58333e55,5005 @@ -53,7 +52,6 @@ extra-deps: # commit: f6cc753611f80af300401cfae63846e9d7c40d9e # subdirs: # - core - # Override default flag values for local packages and extra-deps # flags: {} From 1d56ef3a60553d9b9812b26be833fdda4122d676 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Thu, 16 Jun 2022 12:35:17 +0100 Subject: [PATCH 058/131] agent method to GET message (#403) * agent method to GET message * correction Co-authored-by: JRoberts <8711996+jr-simplex@users.noreply.github.com> Co-authored-by: JRoberts <8711996+jr-simplex@users.noreply.github.com> --- src/Simplex/Messaging/Agent.hs | 32 +++++++++++++++++++++++---- src/Simplex/Messaging/Agent/Client.hs | 23 ++++++++++++++++++- src/Simplex/Messaging/Client.hs | 13 +++++++++++ src/Simplex/Messaging/Server.hs | 2 +- tests/ServerTests.hs | 3 ++- 5 files changed, 66 insertions(+), 7 deletions(-) diff --git a/src/Simplex/Messaging/Agent.hs b/src/Simplex/Messaging/Agent.hs index f958792ac..069b2fee2 100644 --- a/src/Simplex/Messaging/Agent.hs +++ b/src/Simplex/Messaging/Agent.hs @@ -44,6 +44,8 @@ module Simplex.Messaging.Agent acceptContact, rejectContact, subscribeConnection, + getConnectionMessage, + getNotificationMessage, resubscribeConnection, sendMessage, ackMessage, @@ -96,7 +98,7 @@ import Simplex.Messaging.Parsers (parse) import Simplex.Messaging.Protocol (BrokerMsg, ErrorType (AUTH), MsgBody, MsgFlags) import qualified Simplex.Messaging.Protocol as SMP import qualified Simplex.Messaging.TMap as TM -import Simplex.Messaging.Util (bshow, liftError, tryError, unlessM, ($>>=)) +import Simplex.Messaging.Util (bshow, liftError, tryError, unlessM, whenM, ($>>=)) import Simplex.Messaging.Version import System.Random (randomR) import UnliftIO.Async (async, race_) @@ -149,6 +151,14 @@ rejectContact c = withAgentEnv c .: rejectContact' c subscribeConnection :: AgentErrorMonad m => AgentClient -> ConnId -> m () subscribeConnection c = withAgentEnv c . subscribeConnection' c +-- | Get connection message (GET command) +getConnectionMessage :: AgentErrorMonad m => AgentClient -> ConnId -> m (Maybe (SMP.MsgId, MsgFlags)) +getConnectionMessage c = withAgentEnv c . getConnectionMessage' c + +-- | Get connection message for received notification +getNotificationMessage :: AgentErrorMonad m => AgentClient -> ByteString -> m (Maybe (SMP.MsgId, MsgFlags)) +getNotificationMessage c = withAgentEnv c . getNotificationMessage' c + resubscribeConnection :: AgentErrorMonad m => AgentClient -> ConnId -> m () resubscribeConnection c = withAgentEnv c . resubscribeConnection' c @@ -365,12 +375,24 @@ subscribeConnection' c connId = ns <- asks ntfSupervisor atomically $ sendNtfSubCommand ns (connId, NSCCreate) -resubscribeConnection' :: forall m. AgentMonad m => AgentClient -> ConnId -> m () +resubscribeConnection' :: AgentMonad m => AgentClient -> ConnId -> m () resubscribeConnection' c connId = unlessM (atomically $ hasActiveSubscription c connId) (subscribeConnection' c connId) +getConnectionMessage' :: AgentMonad m => AgentClient -> ConnId -> m (Maybe (SMP.MsgId, MsgFlags)) +getConnectionMessage' c connId = do + whenM (atomically $ hasActiveSubscription c connId) . throwError $ CMD PROHIBITED + withStore c (`getConn` connId) >>= \case + SomeConn _ (DuplexConnection _ rq _) -> getQueueMessage c rq connId + SomeConn _ (RcvConnection _ rq) -> getQueueMessage c rq connId + SomeConn _ (ContactConnection {}) -> throwError $ CMD PROHIBITED + SomeConn _ (SndConnection {}) -> throwError $ CONN SIMPLEX + +getNotificationMessage' :: AgentErrorMonad m => AgentClient -> ByteString -> m (Maybe (SMP.MsgId, MsgFlags)) +getNotificationMessage' c _encMessageInfo = throwError $ CMD PROHIBITED + -- | Send message to the connection (SEND command) in Reader monad sendMessage' :: forall m. AgentMonad m => AgentClient -> ConnId -> MsgFlags -> MsgBody -> m AgentMsgId sendMessage' c connId msgFlags msg = @@ -532,7 +554,8 @@ ackMessage' c connId msgId = do withStore c (`getConn` connId) >>= \case SomeConn _ (DuplexConnection _ rq _) -> ack rq SomeConn _ (RcvConnection _ rq) -> ack rq - _ -> throwError $ CONN SIMPLEX + SomeConn _ (SndConnection _ _) -> throwError $ CONN SIMPLEX + SomeConn _ (ContactConnection _ _) -> throwError $ CMD PROHIBITED where ack :: RcvQueue -> m () ack rq = do @@ -549,7 +572,8 @@ suspendConnection' c connId = withStore c (`getConn` connId) >>= \case SomeConn _ (DuplexConnection _ rq _) -> suspendQueue c rq SomeConn _ (RcvConnection _ rq) -> suspendQueue c rq - _ -> throwError $ CONN SIMPLEX + SomeConn _ (ContactConnection _ rq) -> suspendQueue c rq + SomeConn _ (SndConnection _ _) -> throwError $ CONN SIMPLEX -- | Delete SMP agent connection (DEL command) in Reader monad deleteConnection' :: forall m. AgentMonad m => AgentClient -> ConnId -> m () diff --git a/src/Simplex/Messaging/Agent/Client.hs b/src/Simplex/Messaging/Agent/Client.hs index a68cec7b9..c626ed43b 100644 --- a/src/Simplex/Messaging/Agent/Client.hs +++ b/src/Simplex/Messaging/Agent/Client.hs @@ -20,6 +20,7 @@ module Simplex.Messaging.Agent.Client closeAgentClient, newRcvQueue, subscribeQueue, + getQueueMessage, addSubscription, getSubscriptions, sendConfirmation, @@ -113,6 +114,7 @@ data AgentClient = AgentClient connMsgsQueued :: TMap ConnId Bool, smpQueueMsgQueues :: TMap (ConnId, SMPServer, SMP.SenderId) (TQueue InternalId), smpQueueMsgDeliveries :: TMap (ConnId, SMPServer, SMP.SenderId) (Async ()), + getMsgLocks :: TMap (ConnId, SMPServer, SMP.RecipientId) (TMVar ()), reconnections :: TVar [Async ()], asyncClients :: TVar [Async ()], clientId :: Int, @@ -137,11 +139,12 @@ newAgentClient InitialAgentServers {smp, ntf} agentEnv = do connMsgsQueued <- TM.empty smpQueueMsgQueues <- TM.empty smpQueueMsgDeliveries <- TM.empty + getMsgLocks <- TM.empty reconnections <- newTVar [] asyncClients <- newTVar [] clientId <- stateTVar (clientCounter agentEnv) $ \i -> (i + 1, i + 1) lock <- newTMVar () - return AgentClient {active, rcvQ, subQ, msgQ, smpServers, smpClients, ntfServers, ntfClients, subscrSrvrs, pendingSubscrSrvrs, subscrConns, connMsgsQueued, smpQueueMsgQueues, smpQueueMsgDeliveries, reconnections, asyncClients, clientId, agentEnv, lock} + return AgentClient {active, rcvQ, subQ, msgQ, smpServers, smpClients, ntfServers, ntfClients, subscrSrvrs, pendingSubscrSrvrs, subscrConns, connMsgsQueued, smpQueueMsgQueues, smpQueueMsgDeliveries, getMsgLocks, reconnections, asyncClients, clientId, agentEnv, lock} agentDbPath :: AgentClient -> FilePath agentDbPath AgentClient {agentEnv = Env {store = SQLiteStore {dbFilePath}}} = dbFilePath @@ -423,6 +426,7 @@ newRcvQueue_ a c srv = do subscribeQueue :: AgentMonad m => AgentClient -> RcvQueue -> ConnId -> m () subscribeQueue c rq@RcvQueue {server, rcvPrivateKey, rcvId} connId = do + whenM (atomically . TM.member (connId, server, rcvId) $ getMsgLocks c) . throwError $ CMD PROHIBITED atomically $ addPendingSubscription c rq connId withLogClient c server rcvId "SUB" $ \smp -> do liftIO (runExceptT $ subscribeSMPQueue smp rcvPrivateKey rcvId) >>= \case @@ -501,6 +505,23 @@ sendInvitation c (Compatible SMPQueueInfo {smpServer, senderId, dhPublicKey}) co agentCbEncryptOnce dhPublicKey . smpEncode $ SMP.ClientMessage SMP.PHEmpty $ smpEncode agentEnvelope +getQueueMessage :: AgentMonad m => AgentClient -> RcvQueue -> ConnId -> m (Maybe (MsgId, MsgFlags)) +getQueueMessage c@AgentClient {getMsgLocks} RcvQueue {server, rcvId, rcvPrivateKey} connId = + E.bracket (atomically createTakeLock) (atomically . (`putTMVar` ())) $ \_ -> + withLogClient c server rcvId "GET" $ \smp -> + getSMPMessage smp rcvPrivateKey rcvId + where + k = (connId, server, rcvId) + createTakeLock = do + l <- TM.lookup k getMsgLocks >>= maybe newLock pure + takeTMVar l + pure l + where + newLock = do + l <- newTMVar () + TM.insert k l getMsgLocks + pure l + secureQueue :: AgentMonad m => AgentClient -> RcvQueue -> SndPublicVerifyKey -> m () secureQueue c RcvQueue {server, rcvId, rcvPrivateKey} senderKey = withLogClient c server rcvId "KEY " $ \smp -> diff --git a/src/Simplex/Messaging/Client.hs b/src/Simplex/Messaging/Client.hs index 6c37008af..92d749091 100644 --- a/src/Simplex/Messaging/Client.hs +++ b/src/Simplex/Messaging/Client.hs @@ -32,6 +32,7 @@ module Simplex.Messaging.Client -- * SMP protocol command functions createSMPQueue, subscribeSMPQueue, + getSMPMessage, subscribeSMPQueueNotifications, secureSMPQueue, enableSMPQueueNotifications, @@ -287,6 +288,18 @@ subscribeSMPQueue c@ProtocolClient {protocolServer, sessionId, msgQ} rpKey rId = lift . atomically $ mapM_ (`writeTBQueue` (protocolServer, sessionId, rId, cmd)) msgQ _ -> throwE PCEUnexpectedResponse +-- | Get message from SMP queue. The server returns ERR PROHIBITED if a client uses SUB and GET via the same transport connection for the same queue +-- +-- https://github.covm/simplex-chat/simplexmq/blob/master/protocol/simplex-messaging.md#receive-a-message-from-the-queue +getSMPMessage :: SMPClient -> RcvPrivateSignKey -> RecipientId -> ExceptT ProtocolClientError IO (Maybe (MsgId, MsgFlags)) +getSMPMessage c@ProtocolClient {protocolServer, sessionId, msgQ} rpKey rId = + sendSMPCommand c (Just rpKey) rId GET >>= \case + OK -> pure Nothing + cmd@(MSG msgId _ msgFlags _) -> do + lift . atomically $ mapM_ (`writeTBQueue` (protocolServer, sessionId, rId, cmd)) msgQ + pure $ Just (msgId, msgFlags) + _ -> throwE PCEUnexpectedResponse + -- | Subscribe to the SMP queue notifications. -- -- https://github.com/simplex-chat/simplexmq/blob/master/protocol/simplex-messaging.md#subscribe-to-queue-notifications diff --git a/src/Simplex/Messaging/Server.hs b/src/Simplex/Messaging/Server.hs index daeb49601..3f0ba8340 100644 --- a/src/Simplex/Messaging/Server.hs +++ b/src/Simplex/Messaging/Server.hs @@ -442,7 +442,7 @@ client clnt@Client {subscriptions, ntfSubscriptions, rcvQ, sndQ} Server {subscri atomically $ tryPeekMsg q >>= \case Just msg -> setDelivered s msg $> (corrId, queueId, msgCmd msg) - _ -> pure (corrId, queueId, ERR NO_MSG) + _ -> pure (corrId, queueId, OK) subscribeNotifications :: m (Transmission BrokerMsg) subscribeNotifications = atomically $ do diff --git a/tests/ServerTests.hs b/tests/ServerTests.hs index 706ac938a..6e561772b 100644 --- a/tests/ServerTests.hs +++ b/tests/ServerTests.hs @@ -339,6 +339,7 @@ testGetCommand t = Resp "2" _ (MSG mId1 _ _ msg1) <- signSendRecv rh rKey ("2", rId, GET) (dec mId1 msg1, Right "hello") #== "retrieved from queue" Resp "3" _ OK <- signSendRecv rh rKey ("3", rId, ACK mId1) + Resp "4" _ OK <- signSendRecv rh rKey ("4", rId, GET) pure () testGetSubCommands :: forall c. Transport c => TProxy c -> Spec @@ -387,7 +388,7 @@ testGetSubCommands t = (dec mId4 msg4, Right "hello 4") #== "retrieved from queue with GET" Resp "11" _ OK <- signSendRecv rh1 rKey ("11", rId, ACK mId4) -- no more messages for getter too - Resp "12" _ (ERR NO_MSG) <- signSendRecv rh2 rKey ("12", rId, GET) + Resp "12" _ OK <- signSendRecv rh2 rKey ("12", rId, GET) pure () testWithStoreLog :: ATransport -> Spec From 3336f987c34c760337e1af6a7ba1fed047a8b90e Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Thu, 16 Jun 2022 16:11:46 +0400 Subject: [PATCH 059/131] ntf: SCHK, cancel actions on errors, wait until action ts before executing (#402) * SCHK * NSACheck * updateSubFutureTs * dont execute too early * fix diffTimeToMillis * rename function * errors * nested status * increase ntfSubCheckInterval * adjust import * Update src/Simplex/Messaging/Agent/NtfSubSupervisor.hs Co-authored-by: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> * Update src/Simplex/Messaging/Agent/NtfSubSupervisor.hs Co-authored-by: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> * refactor rescheduleAction Co-authored-by: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> --- src/Simplex/Messaging/Agent.hs | 2 +- src/Simplex/Messaging/Agent/Client.hs | 5 + src/Simplex/Messaging/Agent/Env/SQLite.hs | 2 + .../Messaging/Agent/NtfSubSupervisor.hs | 138 ++++++++++++------ src/Simplex/Messaging/Agent/Store.hs | 1 + src/Simplex/Messaging/Agent/Store/SQLite.hs | 18 ++- src/Simplex/Messaging/Notifications/Client.hs | 26 +--- .../Messaging/Notifications/Protocol.hs | 2 +- src/Simplex/Messaging/Notifications/Server.hs | 5 +- 9 files changed, 130 insertions(+), 69 deletions(-) diff --git a/src/Simplex/Messaging/Agent.hs b/src/Simplex/Messaging/Agent.hs index 069b2fee2..0377eabd7 100644 --- a/src/Simplex/Messaging/Agent.hs +++ b/src/Simplex/Messaging/Agent.hs @@ -391,7 +391,7 @@ getConnectionMessage' c connId = do SomeConn _ (SndConnection {}) -> throwError $ CONN SIMPLEX getNotificationMessage' :: AgentErrorMonad m => AgentClient -> ByteString -> m (Maybe (SMP.MsgId, MsgFlags)) -getNotificationMessage' c _encMessageInfo = throwError $ CMD PROHIBITED +getNotificationMessage' _c _encMessageInfo = throwError $ CMD PROHIBITED -- | Send message to the connection (SEND command) in Reader monad sendMessage' :: forall m. AgentMonad m => AgentClient -> ConnId -> MsgFlags -> MsgBody -> m AgentMsgId diff --git a/src/Simplex/Messaging/Agent/Client.hs b/src/Simplex/Messaging/Agent/Client.hs index c626ed43b..ceae7fc56 100644 --- a/src/Simplex/Messaging/Agent/Client.hs +++ b/src/Simplex/Messaging/Agent/Client.hs @@ -35,6 +35,7 @@ module Simplex.Messaging.Agent.Client agentNtfDeleteToken, agentNtfEnableCron, agentNtfCreateSubscription, + agentNtfCheckSubscription, agentCbEncrypt, agentCbDecrypt, cryptoError, @@ -578,6 +579,10 @@ agentNtfCreateSubscription :: AgentMonad m => AgentClient -> NtfTokenId -> NtfTo agentNtfCreateSubscription c tknId NtfToken {ntfServer, ntfPrivKey} smpQueue nKey = withLogClient c ntfServer tknId "SNEW" $ \ntf -> ntfCreateSubscription ntf ntfPrivKey (NewNtfSub tknId smpQueue nKey) +agentNtfCheckSubscription :: AgentMonad m => AgentClient -> NtfSubscriptionId -> NtfToken -> m NtfSubStatus +agentNtfCheckSubscription c subId NtfToken {ntfServer, ntfPrivKey} = + withLogClient c ntfServer subId "SCHK" $ \ntf -> ntfCheckSubscription ntf ntfPrivKey subId + agentCbEncrypt :: AgentMonad m => SndQueue -> Maybe C.PublicKeyX25519 -> ByteString -> m ByteString agentCbEncrypt SndQueue {e2eDhSecret} e2ePubKey msg = do cmNonce <- liftIO C.randomCbNonce diff --git a/src/Simplex/Messaging/Agent/Env/SQLite.hs b/src/Simplex/Messaging/Agent/Env/SQLite.hs index e41abe6dc..ffd5af8b7 100644 --- a/src/Simplex/Messaging/Agent/Env/SQLite.hs +++ b/src/Simplex/Messaging/Agent/Env/SQLite.hs @@ -70,6 +70,7 @@ data AgentConfig = AgentConfig helloTimeout :: NominalDiffTime, resubscriptionConcurrency :: Int, ntfWorkerThrottle :: Int, + ntfSubCheckInterval :: NominalDiffTime, caCertificateFile :: FilePath, privateKeyFile :: FilePath, certificateFile :: FilePath, @@ -102,6 +103,7 @@ defaultAgentConfig = helloTimeout = 2 * nominalDay, resubscriptionConcurrency = 16, ntfWorkerThrottle = 1000000, -- microseconds + ntfSubCheckInterval = nominalDay, -- CA certificate private key is not needed for initialization -- ! we do not generate these caCertificateFile = "/etc/opt/simplex-agent/ca.crt", diff --git a/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs b/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs index f428705bc..84b51ec5b 100644 --- a/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs +++ b/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs @@ -2,6 +2,7 @@ {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE LambdaCase #-} {-# LANGUAGE NamedFieldPuns #-} +{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE ScopedTypeVariables #-} module Simplex.Messaging.Agent.NtfSubSupervisor @@ -14,7 +15,6 @@ module Simplex.Messaging.Agent.NtfSubSupervisor ) where -import Control.Concurrent (threadDelay) import Control.Concurrent.Async (Async, uninterruptibleCancel) import Control.Concurrent.STM (stateTVar) import Control.Monad @@ -22,21 +22,25 @@ import Control.Monad.Except (runExceptT) import Control.Monad.IO.Unlift (MonadUnliftIO) import Control.Monad.Reader import Data.Bifunctor (first) +import Data.Fixed (Fixed (MkFixed), Pico) import qualified Data.Map.Strict as M -import Data.Time (getCurrentTime) +import Data.Time (UTCTime, addUTCTime, diffUTCTime, getCurrentTime, nominalDiffTimeToSeconds) import Simplex.Messaging.Agent.Client import Simplex.Messaging.Agent.Env.SQLite import Simplex.Messaging.Agent.Protocol (ConnId) +import qualified Simplex.Messaging.Agent.Protocol as AP import Simplex.Messaging.Agent.Store import Simplex.Messaging.Client.Agent () import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Notifications.Client -import Simplex.Messaging.Notifications.Protocol (NtfTknStatus (..), SMPQueueNtf (..)) +import Simplex.Messaging.Notifications.Protocol (NtfSubStatus (..), NtfTknStatus (..), SMPQueueNtf (..)) import Simplex.Messaging.Protocol import Simplex.Messaging.TMap (TMap) import qualified Simplex.Messaging.TMap as TM +import Simplex.Messaging.Util (unlessM) import System.Random (randomR) import UnliftIO (async) +import UnliftIO.Concurrent (forkIO, threadDelay) import qualified UnliftIO.Exception as E import UnliftIO.STM @@ -110,23 +114,42 @@ processNtfSub c (connId, cmd) = do runNtfWorker :: AgentMonad m => AgentClient -> NtfServer -> TMVar () -> m () runNtfWorker c srv doWork = forever $ do void . atomically $ readTMVar doWork - getNtfToken_ >>= \case - Just tkn@NtfToken {ntfTokenId = Just tknId, ntfTknStatus} -> - withStore c (`getNextNtfSubAction` srv) >>= \case - Just (ntfSub@NtfSubscription {connId, smpServer}, ntfSubAction, RcvQueue {ntfPrivateKey, notifierId}) -> do - case ntfSubAction of - NSACreate -> case (ntfPrivateKey, notifierId) of - (Just ntfPrivKey, Just nId) - | ntfTknStatus == NTActive -> do - ntfSubId <- agentNtfCreateSubscription c tknId tkn (SMPQueueNtf smpServer nId) ntfPrivKey - ts <- liftIO getCurrentTime - withStore c $ \st -> - updateNtfSubscription st connId ntfSub {ntfSubId = Just ntfSubId, ntfSubStatus = NASCreated, ntfSubActionTs = ts} (NtfSubAction NSACheck) - | otherwise -> pure () -- error -- TODO move action further to future - _ -> pure () -- error -- TODO move action further to future - NSACheck -> noWorkToDo - NSADelete -> noWorkToDo + getNtfToken >>= \case + Just tkn@NtfToken {ntfTokenId = Just tknId, ntfTknStatus} -> do + nextSub_ <- withStore c (`getNextNtfSubAction` srv) + ts <- liftIO getCurrentTime + case nextSub_ of Nothing -> noWorkToDo + Just (ntfSub@NtfSubscription {connId, smpServer, ntfSubId}, ntfSubAction, RcvQueue {ntfPrivateKey, notifierId}) -> + unlessM (rescheduleAction doWork ts ntfSub) $ + case ntfSubAction of + NSACreate -> case (ntfPrivateKey, notifierId) of + (Just ntfPrivKey, Just nId) + | ntfTknStatus == NTActive -> do + nSubId <- agentNtfCreateSubscription c tknId tkn (SMPQueueNtf smpServer nId) ntfPrivKey + let actionTs = addUTCTime 30 ts + withStore c $ \st -> + updateNtfSubscription st connId ntfSub {ntfSubId = Just nSubId, ntfSubStatus = NASCreated NSNew, ntfSubActionTs = actionTs} (NtfSubAction NSACheck) + | otherwise -> ntfInternalError c connId "NSACreate - token not active" + _ -> ntfInternalError c connId "NSACreate - no notifier key or ID" + NSACheck -> case ntfSubId of + Just nSubId -> + agentNtfCheckSubscription c nSubId tkn >>= \case + NSNew -> updateSubNextCheck NSNew + NSPending -> updateSubNextCheck NSPending + NSActive -> updateSubNextCheck NSActive + NSEnd -> updateSubNextCheck NSEnd + NSSMPAuth -> updateSub (NASCreated NSSMPAuth) (NtfSubAction NSADelete) ts + Nothing -> ntfInternalError c connId "NSACheck - no subscription ID" + NSADelete -> pure () + where + updateSubNextCheck toStatus = do + checkInterval <- asks $ ntfSubCheckInterval . config + let nextCheckTs = addUTCTime checkInterval ts + updateSub (NASCreated toStatus) (NtfSubAction NSACheck) nextCheckTs + updateSub toStatus toAction actionTs = + withStore c $ \st -> + updateNtfSubscription st connId ntfSub {ntfSubStatus = toStatus, ntfSubActionTs = actionTs} toAction _ -> noWorkToDo delay <- asks $ ntfWorkerThrottle . config liftIO $ threadDelay delay @@ -136,40 +159,63 @@ runNtfWorker c srv doWork = forever $ do runNtfSMPWorker :: forall m. AgentMonad m => AgentClient -> SMPServer -> TMVar () -> m () runNtfSMPWorker c srv doWork = forever $ do void . atomically $ readTMVar doWork - getNtfToken_ >>= \case - Just NtfToken {ntfTknStatus} -> - withStore c (`getNextNtfSubSMPAction` srv) >>= \case - Just (ntfSub@NtfSubscription {connId, ntfServer}, ntfSubAction, rq@RcvQueue {ntfPublicKey}) -> do - case ntfSubAction of - NSAKey - | ntfTknStatus == NTActive -> - case ntfPublicKey of - Just ntfPubKey -> - enableNotificationsWithNKey ntfPubKey - _ -> do - C.SignAlg a <- asks (cmdSignAlg . config) - (ntfPubKey, ntfPrivKey) <- liftIO $ C.generateSignatureKeyPair a - withStore c $ \st -> setRcvQueueNotifierKey st connId ntfPubKey ntfPrivKey - enableNotificationsWithNKey ntfPubKey - | otherwise -> pure () -- error -- TODO move action further to future - where - enableNotificationsWithNKey ntfPubKey = do - nId <- enableQueueNotifications c rq ntfPubKey - ts <- liftIO getCurrentTime - withStore c $ \st -> do - setRcvQueueNotifierId st connId nId - updateNtfSubscription st connId ntfSub {ntfQueueId = Just nId, ntfSubStatus = NASKey, ntfSubActionTs = ts} (NtfSubAction NSACreate) - ns <- asks ntfSupervisor - atomically $ sendNtfSubCommand ns (connId, NSCNtfWorker ntfServer) + getNtfToken >>= \case + Just NtfToken {ntfTknStatus} -> do + nextSub_ <- withStore c (`getNextNtfSubSMPAction` srv) + ts <- liftIO getCurrentTime + case nextSub_ of Nothing -> noWorkToDo + Just (ntfSub@NtfSubscription {connId, ntfServer}, ntfSubAction, rq@RcvQueue {ntfPublicKey}) -> + unlessM (rescheduleAction doWork ts ntfSub) $ + case ntfSubAction of + NSAKey + | ntfTknStatus == NTActive -> + case ntfPublicKey of + Just ntfPubKey -> + enableNotificationsWithNKey ntfPubKey + _ -> do + C.SignAlg a <- asks (cmdSignAlg . config) + (ntfPubKey, ntfPrivKey) <- liftIO $ C.generateSignatureKeyPair a + withStore c $ \st -> setRcvQueueNotifierKey st connId ntfPubKey ntfPrivKey + enableNotificationsWithNKey ntfPubKey + | otherwise -> ntfInternalError c connId "NSAKey - token not active" + where + enableNotificationsWithNKey ntfPubKey = do + nId <- enableQueueNotifications c rq ntfPubKey + withStore c $ \st -> do + setRcvQueueNotifierId st connId nId + updateNtfSubscription st connId ntfSub {ntfQueueId = Just nId, ntfSubStatus = NASKey, ntfSubActionTs = ts} (NtfSubAction NSACreate) + ns <- asks ntfSupervisor + atomically $ sendNtfSubCommand ns (connId, NSCNtfWorker ntfServer) _ -> noWorkToDo delay <- asks $ ntfWorkerThrottle . config liftIO $ threadDelay delay where noWorkToDo = void . atomically $ tryTakeTMVar doWork -getNtfToken_ :: AgentMonad m => m (Maybe NtfToken) -getNtfToken_ = do +rescheduleAction :: AgentMonad m => TMVar () -> UTCTime -> NtfSubscription -> m Bool +rescheduleAction doWork ts NtfSubscription {ntfSubActionTs} + | ntfSubActionTs <= ts = pure False + | otherwise = do + void . atomically $ tryTakeTMVar doWork + void . forkIO $ do + threadDelay $ diffInMicros ntfSubActionTs ts + void . atomically $ tryPutTMVar doWork () + pure True + +fromPico :: Pico -> Integer +fromPico (MkFixed i) = i + +diffInMicros :: UTCTime -> UTCTime -> Int +diffInMicros a b = (`div` 1000000) . fromInteger . fromPico . nominalDiffTimeToSeconds $ diffUTCTime a b + +ntfInternalError :: AgentMonad m => AgentClient -> ConnId -> String -> m () +ntfInternalError c@AgentClient {subQ} connId internalErrStr = do + withStore c $ \st -> setNullNtfSubscriptionAction st connId + atomically $ writeTBQueue subQ ("", connId, AP.ERR $ AP.INTERNAL internalErrStr) + +getNtfToken :: AgentMonad m => m (Maybe NtfToken) +getNtfToken = do tkn <- asks $ ntfTkn . ntfSupervisor readTVarIO tkn diff --git a/src/Simplex/Messaging/Agent/Store.hs b/src/Simplex/Messaging/Agent/Store.hs index 63e581f16..6d688dc8c 100644 --- a/src/Simplex/Messaging/Agent/Store.hs +++ b/src/Simplex/Messaging/Agent/Store.hs @@ -101,6 +101,7 @@ class Monad m => MonadAgentStore s m where createNtfSubscription :: s -> NtfSubscription -> NtfSubOrSMPAction -> m () markNtfSubscriptionForDeletion :: s -> ConnId -> m () updateNtfSubscription :: s -> ConnId -> NtfSubscription -> NtfSubOrSMPAction -> m () + setNullNtfSubscriptionAction :: s -> ConnId -> m () deleteNtfSubscription :: s -> ConnId -> m () getNextNtfSubAction :: s -> NtfServer -> m (Maybe (NtfSubscription, NtfSubAction, RcvQueue)) getNextNtfSubSMPAction :: s -> SMPServer -> m (Maybe (NtfSubscription, NtfSubSMPAction, RcvQueue)) diff --git a/src/Simplex/Messaging/Agent/Store/SQLite.hs b/src/Simplex/Messaging/Agent/Store/SQLite.hs index 823b69f1e..99e1f955e 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite.hs @@ -46,7 +46,7 @@ import Data.Maybe (fromMaybe, listToMaybe) import Data.Text (Text) import qualified Data.Text as T import Data.Text.Encoding (decodeLatin1, encodeUtf8) -import Data.Time.Clock (getCurrentTime) +import Data.Time.Clock (UTCTime, getCurrentTime) import Database.SQLite.Simple (FromRow, NamedParam (..), Only (..), SQLError, ToRow, field, (:.) (..)) import qualified Database.SQLite.Simple as DB import Database.SQLite.Simple.FromField @@ -757,6 +757,22 @@ instance (MonadUnliftIO m, MonadError StoreError m) => MonadAgentStore SQLiteSto where (ntfSubAction, ntfSubSMPAction) = ntfSubAndSMPAction ntfAction + setNullNtfSubscriptionAction :: SQLiteStore -> ConnId -> m () + setNullNtfSubscriptionAction st connId = + liftIO . withTransaction st $ \db -> do + r <- maybeFirstRow fromOnly $ DB.query db "SELECT updated_by_supervisor FROM ntf_subscriptions WHERE conn_id = ?" (Only connId) + forM_ r $ \updatedBySupervisor -> + unless updatedBySupervisor $ do + updatedAt <- getCurrentTime + DB.execute + db + [sql| + UPDATE ntf_subscriptions + SET ntf_sub_action = ?, ntf_sub_smp_action = ?, ntf_sub_action_ts = ?, updated_by_supervisor = ?, updated_at = ? + WHERE conn_id = ? + |] + (Nothing :: Maybe NtfSubAction, Nothing :: Maybe NtfSubSMPAction, Nothing :: Maybe UTCTime, False, updatedAt, connId) + deleteNtfSubscription :: SQLiteStore -> ConnId -> m () deleteNtfSubscription _st _connId = throwError SENotImplemented diff --git a/src/Simplex/Messaging/Notifications/Client.hs b/src/Simplex/Messaging/Notifications/Client.hs index d6740f345..1be641a0b 100644 --- a/src/Simplex/Messaging/Notifications/Client.hs +++ b/src/Simplex/Messaging/Notifications/Client.hs @@ -15,13 +15,13 @@ import Data.Time (UTCTime) import Data.Word (Word16) import Database.SQLite.Simple.FromField (FromField (..)) import Database.SQLite.Simple.ToField (ToField (..)) +import Simplex.Messaging.Agent.Protocol (ConnId) import Simplex.Messaging.Client import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Encoding import Simplex.Messaging.Notifications.Protocol import Simplex.Messaging.Parsers (blobFieldDecoder, fromTextField_) import Simplex.Messaging.Protocol (NotifierId, ProtocolServer, SMPServer) -import Simplex.Messaging.Agent.Protocol (ConnId) type NtfServer = ProtocolServer @@ -180,13 +180,7 @@ data NtfAgentSubStatus | -- | state after NKEY - notifier ID is assigned to queue on SMP server NASKey | -- | state after SNEW - subscription created on notification server - NASCreated - | -- | connected and subscribed to SMP server - NASActive - | -- | communicated by notification server that NEND received (we currently do not support it) - NASEnded - | -- | communicated by notification server that SMP AUTH error occured - NASSMPAuth + NASCreated NtfSubStatus | -- | state after SDEL (subscription is deleted on notification server) NASDeleted deriving (Eq, Show) @@ -194,20 +188,16 @@ data NtfAgentSubStatus instance Encoding NtfAgentSubStatus where smpEncode = \case NASNew -> "NEW" - NASKey -> "NKEY" - NASCreated -> "CREATED" - NASActive -> "ACTIVE" - NASEnded -> "ENDED" - NASSMPAuth -> "SMP_AUTH" + NASKey -> "KEY" + NASCreated status -> "CREATED " <> smpEncode status NASDeleted -> "DELETED" smpP = A.takeTill (== ' ') >>= \case "NEW" -> pure NASNew - "NKEY" -> pure NASKey - "CREATED" -> pure NASCreated - "ACTIVE" -> pure NASActive - "ENDED" -> pure NASEnded - "SMP_AUTH" -> pure NASSMPAuth + "KEY" -> pure NASKey + "CREATED" -> do + _ <- A.space + NASCreated <$> smpP "DELETED" -> pure NASDeleted _ -> fail "bad NtfAgentSubStatus" diff --git a/src/Simplex/Messaging/Notifications/Protocol.hs b/src/Simplex/Messaging/Notifications/Protocol.hs index 2c33ab86a..e4bb044ae 100644 --- a/src/Simplex/Messaging/Notifications/Protocol.hs +++ b/src/Simplex/Messaging/Notifications/Protocol.hs @@ -163,7 +163,7 @@ data NtfCommand (e :: NtfEntity) where TCRN :: Word16 -> NtfCommand 'Token -- | create SMP subscription SNEW :: NewNtfEntity 'Subscription -> NtfCommand 'Subscription - -- | check SMP subscription status (response is STAT) + -- | check SMP subscription status (response is SUB) SCHK :: NtfCommand 'Subscription -- | delete SMP subscription SDEL :: NtfCommand 'Subscription diff --git a/src/Simplex/Messaging/Notifications/Server.hs b/src/Simplex/Messaging/Notifications/Server.hs index a9b51aae0..507b42f0d 100644 --- a/src/Simplex/Messaging/Notifications/Server.hs +++ b/src/Simplex/Messaging/Notifications/Server.hs @@ -335,7 +335,8 @@ client NtfServerClient {rcvQ, sndQ} NtfSubscriber {newSubQ} NtfPushServer {pushQ Just _ -> writeTBQueue newSubQ (NtfSub sub) $> NRSubId subId _ -> pure $ NRErr AUTH ) - NtfReqCmd SSubscription (NtfSub NtfSubData {notifierKey = registeredNKey}) (corrId, subId, cmd) -> + NtfReqCmd SSubscription (NtfSub NtfSubData {notifierKey = registeredNKey, subStatus}) (corrId, subId, cmd) -> do + status <- readTVarIO subStatus (corrId,subId,) <$> case cmd of SNEW (NewNtfSub _ _ notifierKey) -> do logDebug "SNEW - existing subscription" @@ -344,7 +345,7 @@ client NtfServerClient {rcvQ, sndQ} NtfSubscriber {newSubQ} NtfPushServer {pushQ if notifierKey == registeredNKey then NRSubId subId else NRErr AUTH - SCHK -> pure NROk + SCHK -> pure $ NRSub status SDEL -> pure NROk PING -> pure NRPong getId :: m NtfEntityId From 50ddcd3a135984caab6de98e414e7fd394c88bfc Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Thu, 16 Jun 2022 15:35:45 +0100 Subject: [PATCH 060/131] log weekly and monthly active queues (#404) * log weekly and monthly active queues * update --- package.yaml | 1 + simplexmq.cabal | 5 ++++ src/Simplex/Messaging/Server.hs | 32 ++++++++++++++++++++----- src/Simplex/Messaging/Server/Env/STM.hs | 10 +++++--- 4 files changed, 39 insertions(+), 9 deletions(-) diff --git a/package.yaml b/package.yaml index fd983e476..7cd522404 100644 --- a/package.yaml +++ b/package.yaml @@ -60,6 +60,7 @@ dependencies: - template-haskell == 2.16.* - text == 1.2.* - time == 1.9.* + - time-compat == 1.9.* - time-manager == 0.0.* - tls >= 1.6.0 && < 1.7 - transformers == 0.5.* diff --git a/simplexmq.cabal b/simplexmq.cabal index 8ef8fe65e..3db372d22 100644 --- a/simplexmq.cabal +++ b/simplexmq.cabal @@ -127,6 +127,7 @@ library , template-haskell ==2.16.* , text ==1.2.* , time ==1.9.* + , time-compat ==1.9.* , time-manager ==0.0.* , tls >=1.6.0 && <1.7 , transformers ==0.5.* @@ -187,6 +188,7 @@ executable ntf-server , template-haskell ==2.16.* , text ==1.2.* , time ==1.9.* + , time-compat ==1.9.* , time-manager ==0.0.* , tls >=1.6.0 && <1.7 , transformers ==0.5.* @@ -247,6 +249,7 @@ executable smp-agent , template-haskell ==2.16.* , text ==1.2.* , time ==1.9.* + , time-compat ==1.9.* , time-manager ==0.0.* , tls >=1.6.0 && <1.7 , transformers ==0.5.* @@ -307,6 +310,7 @@ executable smp-server , template-haskell ==2.16.* , text ==1.2.* , time ==1.9.* + , time-compat ==1.9.* , time-manager ==0.0.* , tls >=1.6.0 && <1.7 , transformers ==0.5.* @@ -386,6 +390,7 @@ test-suite smp-server-test , template-haskell ==2.16.* , text ==1.2.* , time ==1.9.* + , time-compat ==1.9.* , time-manager ==0.0.* , timeit ==2.0.* , tls >=1.6.0 && <1.7 diff --git a/src/Simplex/Messaging/Server.hs b/src/Simplex/Messaging/Server.hs index 9f0730799..46a65e2e0 100644 --- a/src/Simplex/Messaging/Server.hs +++ b/src/Simplex/Messaging/Server.hs @@ -7,6 +7,7 @@ {-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE NumericUnderscores #-} {-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE PatternSynonyms #-} {-# LANGUAGE RankNTypes #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE TupleSections #-} @@ -45,8 +46,11 @@ import Data.Functor (($>)) import Data.List (intercalate) import qualified Data.Map.Strict as M import Data.Maybe (isNothing) +import Data.Set (Set) import qualified Data.Set as S import qualified Data.Text as T +import Data.Time.Calendar.Month.Compat (pattern MonthDay) +import Data.Time.Calendar.OrdinalDate (mondayStartWeek) import Data.Time.Clock (UTCTime (..), diffTimeToPicoseconds, getCurrentTime) import Data.Time.Clock.System (SystemTime (..), getSystemTime) import Data.Type.Equality @@ -159,9 +163,9 @@ smpServer started = do logServerStats :: Int -> Int -> m () logServerStats startAt logInterval = do initialDelay <- (startAt -) . fromIntegral . (`div` 1000000_000000) . diffTimeToPicoseconds . utctDayTime <$> liftIO getCurrentTime - logInfo $ "fromTime,qCreated,qSecured,qDeleted,msgSent,msgRecv,msgQueues" + logInfo $ "fromTime,qCreated,qSecured,qDeleted,msgSent,msgRecv,dayMsgQueues,weekMsgQueues,monthMsgQueues" threadDelay $ 1000000 * (initialDelay + if initialDelay < 0 then 86400 else 0) - ServerStats {fromTime, qCreated, qSecured, qDeleted, msgSent, msgRecv, msgQueues} <- asks serverStats + ServerStats {fromTime, qCreated, qSecured, qDeleted, msgSent, msgRecv, dayMsgQueues, weekMsgQueues, monthMsgQueues} <- asks serverStats let interval = 1000000 * logInterval forever $ do ts <- liftIO getCurrentTime @@ -171,9 +175,17 @@ smpServer started = do qDeleted' <- atomically $ swapTVar qDeleted 0 msgSent' <- atomically $ swapTVar msgSent 0 msgRecv' <- atomically $ swapTVar msgRecv 0 - msgQueues' <- atomically $ S.size <$> swapTVar msgQueues S.empty - logInfo . T.pack $ intercalate "," [show fromTime', show qCreated', show qSecured', show qDeleted', show msgSent', show msgRecv', show msgQueues'] + let day = utctDay ts + (_, wDay) = mondayStartWeek day + MonthDay _ mDay = day + (dayMsgQueues', weekMsgQueues', monthMsgQueues') <- + atomically $ (,,) <$> periodCount 1 dayMsgQueues <*> periodCount wDay weekMsgQueues <*> periodCount mDay monthMsgQueues + logInfo . T.pack $ intercalate "," [show fromTime', show qCreated', show qSecured', show qDeleted', show msgSent', show msgRecv', show dayMsgQueues', weekMsgQueues', monthMsgQueues'] threadDelay interval + where + periodCount :: Int -> TVar (Set RecipientId) -> STM String + periodCount 1 pVar = show . S.size <$> swapTVar pVar S.empty + periodCount _ _ = pure "" runClient :: Transport c => TProxy c -> c -> m () runClient _ h = do @@ -417,10 +429,18 @@ client clnt@Client {subscriptions, ntfSubscriptions, rcvQ, sndQ} Server {subscri Just (Just s) -> do stats <- asks serverStats atomically $ modifyTVar (msgRecv stats) (+ 1) - atomically $ modifyTVar (msgQueues stats) (S.insert queueId) + atomically $ updateActiveQueues stats queueId deliverMessage tryDelPeekMsg queueId s _ -> return $ err NO_MSG + updateActiveQueues :: ServerStats -> RecipientId -> STM () + updateActiveQueues stats qId = do + updatePeriod dayMsgQueues + updatePeriod weekMsgQueues + updatePeriod monthMsgQueues + where + updatePeriod pSel = modifyTVar (pSel stats) (S.insert qId) + withSub :: RecipientId -> (Sub -> STM a) -> STM (Maybe a) withSub rId f = mapM f =<< TM.lookup rId subscriptions @@ -451,7 +471,7 @@ client clnt@Client {subscriptions, ntfSubscriptions, rcvQ, sndQ} Server {subscri when (sent == OK) $ do stats <- asks serverStats atomically $ modifyTVar (msgSent stats) (+ 1) - atomically $ modifyTVar (msgQueues stats) (S.insert $ recipientId qr) + atomically $ updateActiveQueues stats $ recipientId qr pure resp where mkMessage :: m (Either C.CryptoError Message) diff --git a/src/Simplex/Messaging/Server/Env/STM.hs b/src/Simplex/Messaging/Server/Env/STM.hs index badf6a983..f995eceb1 100644 --- a/src/Simplex/Messaging/Server/Env/STM.hs +++ b/src/Simplex/Messaging/Server/Env/STM.hs @@ -111,7 +111,9 @@ data ServerStats = ServerStats qDeleted :: TVar Int, msgSent :: TVar Int, msgRecv :: TVar Int, - msgQueues :: TVar (Set RecipientId), + dayMsgQueues :: TVar (Set RecipientId), + weekMsgQueues :: TVar (Set RecipientId), + monthMsgQueues :: TVar (Set RecipientId), fromTime :: TVar UTCTime } @@ -147,9 +149,11 @@ newServerStats ts = do qDeleted <- newTVar 0 msgSent <- newTVar 0 msgRecv <- newTVar 0 - msgQueues <- newTVar S.empty + dayMsgQueues <- newTVar S.empty + weekMsgQueues <- newTVar S.empty + monthMsgQueues <- newTVar S.empty fromTime <- newTVar ts - pure ServerStats {qCreated, qSecured, qDeleted, msgSent, msgRecv, msgQueues, fromTime} + pure ServerStats {qCreated, qSecured, qDeleted, msgSent, msgRecv, dayMsgQueues, weekMsgQueues, monthMsgQueues, fromTime} newSubscription :: STM Sub newSubscription = do From 8c2227ccce119b7449ef2372e5bab091704a2378 Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Thu, 16 Jun 2022 19:22:21 +0400 Subject: [PATCH 061/131] ntf: fix test (#405) --- tests/AgentTests/NotificationTests.hs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/AgentTests/NotificationTests.hs b/tests/AgentTests/NotificationTests.hs index 1bf9b919e..11eb5b934 100644 --- a/tests/AgentTests/NotificationTests.hs +++ b/tests/AgentTests/NotificationTests.hs @@ -141,7 +141,10 @@ testNtfTokenSecondRegistration APNSMockServer {apnsQ} = do -- at this point the first token is still active NTActive <- checkNtfToken a tkn -- and the second is not yet verified - NTConfirmed <- checkNtfToken a' tkn + liftIO $ threadDelay 50000 + r <- checkNtfToken a' tkn + liftIO . print $ "checkNtfToken a' tkn: " <> show r + unless (r == NTConfirmed) $ error "bad status" -- now the second token registration is verified verifyNtfToken a' tkn verification' nonce' -- the first registration is removed From 9f981042b925a2346f953a33eace0a73dc2c656e Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Thu, 16 Jun 2022 17:15:16 +0100 Subject: [PATCH 062/131] v2.3.1 --- package.yaml | 2 +- simplexmq.cabal | 2 +- src/Simplex/Messaging/Transport.hs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package.yaml b/package.yaml index 7cd522404..18a3d2a8f 100644 --- a/package.yaml +++ b/package.yaml @@ -1,5 +1,5 @@ name: simplexmq -version: 2.3.0 +version: 2.3.1 synopsis: SimpleXMQ message broker description: | This package includes <./docs/Simplex-Messaging-Server.html server>, diff --git a/simplexmq.cabal b/simplexmq.cabal index 3db372d22..c3519aa03 100644 --- a/simplexmq.cabal +++ b/simplexmq.cabal @@ -5,7 +5,7 @@ cabal-version: 1.12 -- see: https://github.com/sol/hpack name: simplexmq -version: 2.3.0 +version: 2.3.1 synopsis: SimpleXMQ message broker description: This package includes <./docs/Simplex-Messaging-Server.html server>, <./docs/Simplex-Messaging-Client.html client> and diff --git a/src/Simplex/Messaging/Transport.hs b/src/Simplex/Messaging/Transport.hs index 95c9373c9..5666ad591 100644 --- a/src/Simplex/Messaging/Transport.hs +++ b/src/Simplex/Messaging/Transport.hs @@ -96,7 +96,7 @@ supportedSMPVersions :: VersionRange supportedSMPVersions = mkVersionRange 1 1 simplexMQVersion :: String -simplexMQVersion = "2.3.0" +simplexMQVersion = "2.3.1" -- * Transport connection class From b58523d4a29235c29c56a461edb686ed93bc1e89 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Thu, 16 Jun 2022 17:15:16 +0100 Subject: [PATCH 063/131] v2.3.1 --- package.yaml | 2 +- simplexmq.cabal | 2 +- src/Simplex/Messaging/Transport.hs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package.yaml b/package.yaml index 7cd522404..18a3d2a8f 100644 --- a/package.yaml +++ b/package.yaml @@ -1,5 +1,5 @@ name: simplexmq -version: 2.3.0 +version: 2.3.1 synopsis: SimpleXMQ message broker description: | This package includes <./docs/Simplex-Messaging-Server.html server>, diff --git a/simplexmq.cabal b/simplexmq.cabal index 50b50bebf..dcf76cd0d 100644 --- a/simplexmq.cabal +++ b/simplexmq.cabal @@ -5,7 +5,7 @@ cabal-version: 1.12 -- see: https://github.com/sol/hpack name: simplexmq -version: 2.3.0 +version: 2.3.1 synopsis: SimpleXMQ message broker description: This package includes <./docs/Simplex-Messaging-Server.html server>, <./docs/Simplex-Messaging-Client.html client> and diff --git a/src/Simplex/Messaging/Transport.hs b/src/Simplex/Messaging/Transport.hs index 3c09e67bf..7f73b8f64 100644 --- a/src/Simplex/Messaging/Transport.hs +++ b/src/Simplex/Messaging/Transport.hs @@ -96,7 +96,7 @@ supportedSMPServerVRange :: VersionRange supportedSMPServerVRange = mkVersionRange 1 2 simplexMQVersion :: String -simplexMQVersion = "2.3.0" +simplexMQVersion = "2.3.1" -- * Transport connection class From 31c78e516dbc4fa2ca7cb61dd1838a3ed2028daa Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Fri, 17 Jun 2022 20:25:13 +0400 Subject: [PATCH 064/131] remove FQHN validation during TLS handshake (#409) --- src/Simplex/Messaging/Transport/Client.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Simplex/Messaging/Transport/Client.hs b/src/Simplex/Messaging/Transport/Client.hs index cf24c75ef..edb29094d 100644 --- a/src/Simplex/Messaging/Transport/Client.hs +++ b/src/Simplex/Messaging/Transport/Client.hs @@ -84,7 +84,7 @@ validateCertificateChain (C.KeyHash kh) host port cc@(X.CertificateChain sc@[_, x509validate = XV.validate X.HashSHA256 hooks checks certStore cache serviceID cc where hooks = XV.defaultHooks - checks = XV.defaultChecks + checks = XV.defaultChecks {XV.checkFQHN = False} certStore = XS.makeCertificateStore sc cache = XV.exceptionValidationCache [] -- we manually check fingerprint only of the identity certificate (ca.crt) serviceID = (host, port) From ec70429106afd61947588ad498e94d7a9cef1b9e Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Sat, 18 Jun 2022 18:21:35 +0400 Subject: [PATCH 065/131] ntf: getNotificationMessage' (#406) * ntf: getNotificationMessage' * refactor * ntfTs * fix parser * length * fix test * fix encryption, test * remove test focus * getActiveNtfToken * rename * Update src/Simplex/Messaging/Protocol.hs Co-authored-by: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> * getNtfRcvQueue * refactor * don't fail * Update src/Simplex/Messaging/Notifications/Protocol.hs Co-authored-by: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> * Update src/Simplex/Messaging/Notifications/Server.hs Co-authored-by: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> * Update src/Simplex/Messaging/Server.hs Co-authored-by: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> * import Co-authored-by: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> --- src/Simplex/Messaging/Agent.hs | 28 +++++++++--- src/Simplex/Messaging/Agent/Store.hs | 6 ++- src/Simplex/Messaging/Agent/Store/SQLite.hs | 37 +++++++++++++++- src/Simplex/Messaging/Crypto.hs | 2 +- .../Messaging/Notifications/Protocol.hs | 6 ++- src/Simplex/Messaging/Notifications/Server.hs | 14 +++--- .../Notifications/Server/Push/APNS.hs | 26 ++++++++--- src/Simplex/Messaging/Protocol.hs | 23 ++++++++-- src/Simplex/Messaging/Server.hs | 29 +++++++++---- tests/AgentTests/NotificationTests.hs | 43 +++++++++++++------ tests/NtfServerTests.hs | 23 +++++++--- tests/ServerTests.hs | 8 ++-- 12 files changed, 185 insertions(+), 60 deletions(-) diff --git a/src/Simplex/Messaging/Agent.hs b/src/Simplex/Messaging/Agent.hs index 0377eabd7..d863c23ad 100644 --- a/src/Simplex/Messaging/Agent.hs +++ b/src/Simplex/Messaging/Agent.hs @@ -92,10 +92,12 @@ import Simplex.Messaging.Client (ProtocolClient (..), ServerTransmission) import qualified Simplex.Messaging.Crypto as C import qualified Simplex.Messaging.Crypto.Ratchet as CR import Simplex.Messaging.Encoding +import Simplex.Messaging.Encoding.String (StrEncoding (..)) import Simplex.Messaging.Notifications.Client import Simplex.Messaging.Notifications.Protocol (DeviceToken, NtfRegCode (NtfRegCode), NtfTknStatus (..)) +import Simplex.Messaging.Notifications.Server.Push.APNS (PNMessageData (..)) import Simplex.Messaging.Parsers (parse) -import Simplex.Messaging.Protocol (BrokerMsg, ErrorType (AUTH), MsgBody, MsgFlags) +import Simplex.Messaging.Protocol (BrokerMsg, ErrorType (AUTH), MsgBody, MsgFlags, NMsgMeta (..)) import qualified Simplex.Messaging.Protocol as SMP import qualified Simplex.Messaging.TMap as TM import Simplex.Messaging.Util (bshow, liftError, tryError, unlessM, whenM, ($>>=)) @@ -156,8 +158,8 @@ getConnectionMessage :: AgentErrorMonad m => AgentClient -> ConnId -> m (Maybe ( getConnectionMessage c = withAgentEnv c . getConnectionMessage' c -- | Get connection message for received notification -getNotificationMessage :: AgentErrorMonad m => AgentClient -> ByteString -> m (Maybe (SMP.MsgId, MsgFlags)) -getNotificationMessage c = withAgentEnv c . getNotificationMessage' c +getNotificationMessage :: AgentErrorMonad m => AgentClient -> ByteString -> C.CbNonce -> m (Maybe (SMP.MsgId, MsgFlags)) +getNotificationMessage c = withAgentEnv c .: getNotificationMessage' c resubscribeConnection :: AgentErrorMonad m => AgentClient -> ConnId -> m () resubscribeConnection c = withAgentEnv c . resubscribeConnection' c @@ -387,11 +389,23 @@ getConnectionMessage' c connId = do withStore c (`getConn` connId) >>= \case SomeConn _ (DuplexConnection _ rq _) -> getQueueMessage c rq connId SomeConn _ (RcvConnection _ rq) -> getQueueMessage c rq connId - SomeConn _ (ContactConnection {}) -> throwError $ CMD PROHIBITED - SomeConn _ (SndConnection {}) -> throwError $ CONN SIMPLEX + SomeConn _ ContactConnection {} -> throwError $ CMD PROHIBITED + SomeConn _ SndConnection {} -> throwError $ CONN SIMPLEX -getNotificationMessage' :: AgentErrorMonad m => AgentClient -> ByteString -> m (Maybe (SMP.MsgId, MsgFlags)) -getNotificationMessage' _c _encMessageInfo = throwError $ CMD PROHIBITED +getNotificationMessage' :: forall m. AgentMonad m => AgentClient -> ByteString -> C.CbNonce -> m (Maybe (SMP.MsgId, MsgFlags)) +getNotificationMessage' c encMessageInfo nonce = do + withStore c getActiveNtfToken >>= \case + Just NtfToken {ntfDhSecret = Just dhSecret} -> do + ntfData <- agentCbDecrypt dhSecret nonce encMessageInfo + PNMessageData {smpQueue, ntfTs, nmsgNonce, encNMsgMeta} <- liftEither (parse strP (INTERNAL "error parsing PNMessageData") ntfData) + (connId, rcvDhSecret) <- withStore c $ \st -> getNtfRcvQueue st smpQueue + nMsgMeta <- agentCbDecrypt rcvDhSecret nmsgNonce encNMsgMeta `catchError` const (pure "") + let nMsgMetaParsed = parse smpP (INTERNAL "error parsing NMsgMeta") nMsgMeta + case nMsgMetaParsed of + Right NMsgMeta {msgId, msgTs} -> liftIO . print $ "getNotificationMessage', ntfTs = " <> show ntfTs <> ", msgId = " <> show msgId <> ", msgTs = " <> show msgTs + Left _ -> liftIO . print $ "getNotificationMessage', ntfTs = " <> show ntfTs <> ", failed to parse NMsgMeta" + getConnectionMessage' c connId + _ -> throwError $ CMD PROHIBITED -- | Send message to the connection (SEND command) in Reader monad sendMessage' :: forall m. AgentMonad m => AgentClient -> ConnId -> MsgFlags -> MsgBody -> m AgentMsgId diff --git a/src/Simplex/Messaging/Agent/Store.hs b/src/Simplex/Messaging/Agent/Store.hs index 6d688dc8c..e26dbb925 100644 --- a/src/Simplex/Messaging/Agent/Store.hs +++ b/src/Simplex/Messaging/Agent/Store.hs @@ -21,7 +21,7 @@ import Simplex.Messaging.Agent.Protocol import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Crypto.Ratchet (RatchetX448, SkippedMsgDiff, SkippedMsgKeys) import Simplex.Messaging.Notifications.Client -import Simplex.Messaging.Notifications.Protocol (DeviceToken, NtfTknStatus, NtfTokenId) +import Simplex.Messaging.Notifications.Protocol (DeviceToken, NtfTknStatus, NtfTokenId, SMPQueueNtf) import Simplex.Messaging.Protocol ( MsgBody, MsgFlags, @@ -106,6 +106,10 @@ class Monad m => MonadAgentStore s m where getNextNtfSubAction :: s -> NtfServer -> m (Maybe (NtfSubscription, NtfSubAction, RcvQueue)) getNextNtfSubSMPAction :: s -> SMPServer -> m (Maybe (NtfSubscription, NtfSubSMPAction, RcvQueue)) + -- Notification decryption + getActiveNtfToken :: s -> m (Maybe NtfToken) + getNtfRcvQueue :: s -> SMPQueueNtf -> m (ConnId, RcvDhSecret) + -- * Queue types -- | A receive queue. SMP queue through which the agent receives messages from a sender. diff --git a/src/Simplex/Messaging/Agent/Store/SQLite.hs b/src/Simplex/Messaging/Agent/Store/SQLite.hs index 99e1f955e..7cb48d350 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite.hs @@ -61,9 +61,9 @@ import Simplex.Messaging.Crypto.Ratchet (RatchetX448, SkippedMsgDiff (..), Skipp import Simplex.Messaging.Encoding import Simplex.Messaging.Encoding.String import Simplex.Messaging.Notifications.Client (NtfServer, NtfSubAction, NtfSubOrSMPAction (..), NtfSubSMPAction, NtfSubscription (..), NtfTknAction, NtfToken (..)) -import Simplex.Messaging.Notifications.Protocol (DeviceToken (..), NtfTknStatus (..), NtfTokenId) +import Simplex.Messaging.Notifications.Protocol (DeviceToken (..), NtfTknStatus (..), NtfTokenId, SMPQueueNtf (..)) import Simplex.Messaging.Parsers (blobFieldParser, fromTextField_) -import Simplex.Messaging.Protocol (MsgBody, MsgFlags, NotifierId, NtfPrivateSignKey, NtfPublicVerifyKey, ProtocolServer (..)) +import Simplex.Messaging.Protocol (MsgBody, MsgFlags, NotifierId, NtfPrivateSignKey, NtfPublicVerifyKey, ProtocolServer (..), RcvDhSecret) import qualified Simplex.Messaging.Protocol as SMP import Simplex.Messaging.Util (bshow, eitherToMaybe, liftIOEither) import Simplex.Messaging.Version @@ -832,6 +832,39 @@ instance (MonadUnliftIO m, MonadError StoreError m) => MonadAgentStore SQLiteSto let ntfServer = ProtocolServer ntfHost ntfPort ntfKeyHash in (NtfSubscription {connId, smpServer, ntfQueueId, ntfServer, ntfSubId, ntfSubStatus, ntfSubActionTs}, ntfSubAction) + getActiveNtfToken :: SQLiteStore -> m (Maybe NtfToken) + getActiveNtfToken st = + liftIO . withTransaction st $ \db -> + maybeFirstRow ntfToken $ + DB.query + db + [sql| + SELECT s.ntf_host, s.ntf_port, s.ntf_key_hash, + t.provider, t.device_token, t.tkn_id, t.tkn_pub_key, t.tkn_priv_key, t.tkn_pub_dh_key, t.tkn_priv_dh_key, t.tkn_dh_secret, t.tkn_status, t.tkn_action + FROM ntf_tokens t + JOIN ntf_servers s USING (ntf_host, ntf_port) + WHERE t.tkn_status = ? + |] + (Only NTActive) + where + ntfToken ((host, port, keyHash) :. (provider, dt, ntfTokenId, ntfPubKey, ntfPrivKey, ntfDhPubKey, ntfDhPrivKey, ntfDhSecret, ntfTknStatus, ntfTknAction)) = + let ntfServer = ProtocolServer {host, port, keyHash} + ntfDhKeys = (ntfDhPubKey, ntfDhPrivKey) + in NtfToken {deviceToken = DeviceToken provider dt, ntfServer, ntfTokenId, ntfPubKey, ntfPrivKey, ntfDhKeys, ntfDhSecret, ntfTknStatus, ntfTknAction} + + getNtfRcvQueue :: SQLiteStore -> SMPQueueNtf -> m (ConnId, RcvDhSecret) + getNtfRcvQueue st SMPQueueNtf {smpServer = (SMPServer host port _), notifierId} = + liftIOEither . withTransaction st $ \db -> do + firstRow id SEConnNotFound $ + DB.query + db + [sql| + SELECT conn_id, rcv_dh_secret + FROM rcv_queues + WHERE host = ? AND port = ? AND ntf_id = ? + |] + (host, port, notifierId) + -- * Auxiliary helpers instance ToField QueueStatus where toField = toField . serializeQueueStatus diff --git a/src/Simplex/Messaging/Crypto.hs b/src/Simplex/Messaging/Crypto.hs index 6ce1af779..0f0494606 100644 --- a/src/Simplex/Messaging/Crypto.hs +++ b/src/Simplex/Messaging/Crypto.hs @@ -871,7 +871,7 @@ cbDecrypt secret (CbNonce nonce) packet tag = Poly1305.auth rs c newtype CbNonce = CbNonce {unCbNonce :: ByteString} - deriving (Show) + deriving (Eq, Show) instance StrEncoding CbNonce where strEncode (CbNonce s) = strEncode s diff --git a/src/Simplex/Messaging/Notifications/Protocol.hs b/src/Simplex/Messaging/Notifications/Protocol.hs index e4bb044ae..b658ffd4d 100644 --- a/src/Simplex/Messaging/Notifications/Protocol.hs +++ b/src/Simplex/Messaging/Notifications/Protocol.hs @@ -325,7 +325,11 @@ instance Encoding SMPQueueNtf where smpEncode SMPQueueNtf {smpServer, notifierId} = smpEncode (smpServer, notifierId) smpP = do (smpServer, notifierId) <- smpP - pure $ SMPQueueNtf smpServer notifierId + pure $ SMPQueueNtf {smpServer, notifierId} + +instance StrEncoding SMPQueueNtf where + strEncode SMPQueueNtf {smpServer, notifierId} = strEncode smpServer <> "/" <> strEncode notifierId + strP = SMPQueueNtf <$> strP <* A.char '/' <*> strP data PushProvider = PPApns deriving (Eq, Ord, Show) diff --git a/src/Simplex/Messaging/Notifications/Server.hs b/src/Simplex/Messaging/Notifications/Server.hs index 507b42f0d..5d22aab5d 100644 --- a/src/Simplex/Messaging/Notifications/Server.hs +++ b/src/Simplex/Messaging/Notifications/Server.hs @@ -24,7 +24,7 @@ import Simplex.Messaging.Client.Agent import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Notifications.Protocol import Simplex.Messaging.Notifications.Server.Env -import Simplex.Messaging.Notifications.Server.Push.APNS +import Simplex.Messaging.Notifications.Server.Push.APNS (PNMessageData (..), PushNotification (..), PushProviderClient) import Simplex.Messaging.Notifications.Server.Store import Simplex.Messaging.Notifications.Transport import Simplex.Messaging.Protocol (ErrorType (..), SMPServer, SignedTransmission, Transmission, encodeTransmission, tGet, tPut) @@ -107,12 +107,14 @@ ntfSubscriber NtfSubscriber {smpSubscribers, newSubQ, smpAgent = ca@SMPClientAge receiveSMP = forever $ do (srv, _sessId, ntfId, msg) <- atomically $ readTBQueue msgQ case msg of - SMP.NMSG -> do - NtfPushServer {pushQ} <- asks pushServer + SMP.NMSG nmsgNonce encNMsgMeta -> do + ntfTs <- liftIO getSystemTime + let smpQueue = SMPQueueNtf srv ntfId st <- asks store + NtfPushServer {pushQ} <- asks pushServer atomically $ - findNtfSubscriptionToken st (SMPQueueNtf srv ntfId) - >>= mapM_ (\tkn -> writeTBQueue pushQ (tkn, PNMessage srv ntfId)) + findNtfSubscriptionToken st smpQueue + >>= mapM_ (\tkn -> writeTBQueue pushQ (tkn, PNMessage PNMessageData {smpQueue, ntfTs, nmsgNonce, encNMsgMeta})) _ -> pure () pure () @@ -143,7 +145,7 @@ ntfPush s@NtfPushServer {pushQ} = liftIO . forever . runExceptT $ do atomically $ modifyTVar tknStatus $ \status' -> if status' == NTActive then NTActive else NTConfirmed (NTActive, PNCheckMessages) -> do deliverNotification pp tkn ntf - (NTActive, PNMessage _ _) -> do + (NTActive, PNMessage {}) -> do deliverNotification pp tkn ntf _ -> do logError "bad notification token status" diff --git a/src/Simplex/Messaging/Notifications/Server/Push/APNS.hs b/src/Simplex/Messaging/Notifications/Server/Push/APNS.hs index 7c62f630a..42c669c95 100644 --- a/src/Simplex/Messaging/Notifications/Server/Push/APNS.hs +++ b/src/Simplex/Messaging/Notifications/Server/Push/APNS.hs @@ -46,7 +46,7 @@ import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Encoding.String import Simplex.Messaging.Notifications.Protocol import Simplex.Messaging.Notifications.Server.Store (NtfTknData (..)) -import Simplex.Messaging.Protocol (NotifierId, SMPServer) +import Simplex.Messaging.Protocol (EncNMsgMeta) import Simplex.Messaging.Transport.HTTP2.Client import System.Environment (getEnv) import UnliftIO.STM @@ -95,10 +95,24 @@ readECPrivateKey f = do data PushNotification = PNVerification NtfRegCode - | PNMessage SMPServer NotifierId + | PNMessage PNMessageData | PNAlert Text | PNCheckMessages +data PNMessageData = PNMessageData + { smpQueue :: SMPQueueNtf, + ntfTs :: SystemTime, + nmsgNonce :: C.CbNonce, + encNMsgMeta :: EncNMsgMeta + } + +instance StrEncoding PNMessageData where + strEncode PNMessageData {smpQueue, ntfTs, nmsgNonce, encNMsgMeta} = + strEncode (smpQueue, ntfTs, nmsgNonce, encNMsgMeta) + strP = do + (smpQueue, ntfTs, nmsgNonce, encNMsgMeta) <- strP + pure PNMessageData {smpQueue, ntfTs, nmsgNonce, encNMsgMeta} + data APNSNotification = APNSNotification {aps :: APNSNotificationBody, notificationData :: Maybe J.Value} deriving (Show, Generic) @@ -189,7 +203,7 @@ defaultAPNSPushClientConfig = authKeyFileEnv = "APNS_KEY_FILE", -- the environment variables APNS_KEY_FILE and APNS_KEY_ID must be set, or the server would fail to start authKeyAlg = "ES256", authKeyIdEnv = "APNS_KEY_ID", - paddedNtfLength = 256, + paddedNtfLength = 512, appName = "chat.simplex.app", appTeamId = "5NN7GUYB6T", apnsHost = "api.sandbox.push.apple.com", @@ -259,9 +273,9 @@ apnsNotification NtfTknData {tknDhSecret} nonce paddedLen = \case PNVerification (NtfRegCode code) -> encrypt code $ \code' -> apn APNSBackground {contentAvailable = 1} . Just $ J.object ["verification" .= code', "nonce" .= nonce] - PNMessage srv nId -> - encrypt (strEncode srv <> "/" <> strEncode nId) $ \ntfQueue -> - apn apnMutableContent . Just $ J.object ["checkMessage" .= ntfQueue, "nonce" .= nonce] + PNMessage pnMessageData -> + encrypt (strEncode pnMessageData) $ \ntfData -> + apn apnMutableContent . Just $ J.object ["checkMessage" .= ntfData, "nonce" .= nonce] PNAlert text -> Right $ apn (apnAlert $ APNSAlertText text) Nothing PNCheckMessages -> Right $ apn APNSBackground {contentAvailable = 1} . Just $ J.object ["checkMessages" .= True] where diff --git a/src/Simplex/Messaging/Protocol.hs b/src/Simplex/Messaging/Protocol.hs index 0c8ff448d..dfdb6c52d 100644 --- a/src/Simplex/Messaging/Protocol.hs +++ b/src/Simplex/Messaging/Protocol.hs @@ -78,6 +78,8 @@ module Simplex.Messaging.Protocol NtfPublicVerifyKey, MsgId, MsgBody, + EncNMsgMeta, + NMsgMeta (..), MsgFlags (..), noMsgFlags, @@ -241,13 +243,28 @@ data BrokerMsg where -- MSG :: MsgId -> SystemTime -> MsgBody -> BrokerMsg MSG :: MsgId -> SystemTime -> MsgFlags -> MsgBody -> BrokerMsg NID :: NotifierId -> BrokerMsg - NMSG :: BrokerMsg + NMSG :: C.CbNonce -> EncNMsgMeta -> BrokerMsg END :: BrokerMsg OK :: BrokerMsg ERR :: ErrorType -> BrokerMsg PONG :: BrokerMsg deriving (Eq, Show) +type EncNMsgMeta = ByteString + +data NMsgMeta = NMsgMeta + { msgId :: MsgId, + msgTs :: SystemTime + } + +instance Encoding NMsgMeta where + smpEncode NMsgMeta {msgId, msgTs} = + smpEncode (msgId, msgTs) + smpP = do + -- Tail here is to allow extension in the future clients/servers + (msgId, msgTs, Tail _) <- smpP + pure NMsgMeta {msgId, msgTs} + newtype MsgFlags = MsgFlags {notification :: Bool} deriving (Eq, Show) @@ -686,7 +703,7 @@ instance ProtocolEncoding BrokerMsg where | v == 1 -> e (MSG_, ' ', msgId, ts, Tail msgBody) | otherwise -> e (MSG_, ' ', msgId, ts, flags, ' ', Tail msgBody) NID nId -> e (NID_, ' ', nId) - NMSG -> e NMSG_ + NMSG nmsgNonce encNMsgMeta -> e (NMSG_, ' ', nmsgNonce, encNMsgMeta) END -> e END_ OK -> e OK_ ERR err -> e (ERR_, ' ', err) @@ -701,7 +718,7 @@ instance ProtocolEncoding BrokerMsg where | otherwise -> MSG <$> _smpP <*> smpP <*> smpP <*> (unTail <$> _smpP) IDS_ -> IDS <$> (QIK <$> _smpP <*> smpP <*> smpP) NID_ -> NID <$> _smpP - NMSG_ -> pure NMSG + NMSG_ -> NMSG <$> _smpP <*> smpP END_ -> pure END OK_ -> pure OK ERR_ -> ERR <$> _smpP diff --git a/src/Simplex/Messaging/Server.hs b/src/Simplex/Messaging/Server.hs index c5259e244..8fbf4dca0 100644 --- a/src/Simplex/Messaging/Server.hs +++ b/src/Simplex/Messaging/Server.hs @@ -42,6 +42,7 @@ import Control.Monad.Reader import Crypto.Random import Data.ByteString.Char8 (ByteString) import qualified Data.ByteString.Char8 as B +import Data.Either (fromRight) import Data.Functor (($>)) import Data.List (intercalate) import qualified Data.Map.Strict as M @@ -56,6 +57,7 @@ import Data.Time.Clock.System (SystemTime (..), getSystemTime) import Data.Type.Equality import Network.Socket (ServiceName) import qualified Simplex.Messaging.Crypto as C +import Simplex.Messaging.Encoding (Encoding (smpEncode)) import Simplex.Messaging.Encoding.String import Simplex.Messaging.Protocol import Simplex.Messaging.Server.Env.STM @@ -520,11 +522,12 @@ client clnt@Client {subscriptions, ntfSubscriptions, rcvQ, sndQ} Server {subscri ms <- asks msgStore ServerConfig {messageExpiration, msgQueueQuota} <- asks config old <- liftIO $ mapM expireBeforeEpoch messageExpiration + ntfNonceDrg <- asks idsDrg resp@(_, _, sent) <- atomically $ do q <- getMsgQueue ms (recipientId qr) msgQueueQuota mapM_ (deleteExpiredMsgs q) old ifM (isFull q) (pure $ err QUOTA) $ do - when (notification flags) trySendNotification + when (notification flags) $ trySendNotification msg ntfNonceDrg writeMsg q msg pure ok when (sent == OK) $ do @@ -540,15 +543,23 @@ client clnt@Client {subscriptions, ntfSubscriptions, rcvQ, sndQ} Server {subscri let c = C.cbEncrypt (rcvDhSecret qr) (C.cbNonce msgId) msgBody (maxMessageLength + 2) pure $ Message msgId ts flags <$> c - trySendNotification :: STM () - trySendNotification = + trySendNotification :: Message -> TVar ChaChaDRG -> STM () + trySendNotification msg ntfNonceDrg = forM_ (notifier qr) $ \(nId, _) -> - mapM_ (writeNtf nId) =<< TM.lookup nId notifiers + mapM_ (writeNtf nId msg ntfNonceDrg) =<< TM.lookup nId notifiers - writeNtf :: NotifierId -> Client -> STM () - writeNtf nId Client {sndQ = q} = - unlessM (isFullTBQueue sndQ) $ - writeTBQueue q (CorrId "", nId, NMSG) + writeNtf :: NotifierId -> Message -> TVar ChaChaDRG -> Client -> STM () + writeNtf nId msg ntfNonceDrg Client {sndQ = q} = + unlessM (isFullTBQueue sndQ) $ do + (nmsgNonce, encNMsgMeta) <- mkMessageNotification msg ntfNonceDrg + writeTBQueue q (CorrId "", nId, NMSG nmsgNonce encNMsgMeta) + + mkMessageNotification :: Message -> TVar ChaChaDRG -> STM (C.CbNonce, EncNMsgMeta) + mkMessageNotification Message {msgId, ts} ntfNonceDrg = do + cbNonce <- C.pseudoRandomCbNonce ntfNonceDrg + let msgMeta = NMsgMeta {msgId, msgTs = ts} + encNMsgMeta = C.cbEncrypt (rcvDhSecret qr) cbNonce (smpEncode msgMeta) 128 + pure . (cbNonce,) $ fromRight "" encNMsgMeta deliverMessage :: RecipientId -> TVar Sub -> MsgQueue -> Maybe Message -> m (Transmission BrokerMsg) deliverMessage rId sub q msg_ = @@ -646,4 +657,4 @@ restoreServerMessages = asks (storeMsgsFile . config) >>= mapM_ restoreMessages full <- atomically $ do q <- getMsgQueue ms rId quota ifM (isFull q) (pure True) (writeMsg q msg $> False) - when full . B.putStrLn $ "message queue " <> strEncode rId <> " is full, message not restored: " <> strEncode (msgId msg) + when full . B.putStrLn $ "message queue " <> strEncode rId <> " is full, message not restored: " <> strEncode (msgId (msg :: Message)) diff --git a/tests/AgentTests/NotificationTests.hs b/tests/AgentTests/NotificationTests.hs index 11eb5b934..03bc0af9b 100644 --- a/tests/AgentTests/NotificationTests.hs +++ b/tests/AgentTests/NotificationTests.hs @@ -27,7 +27,7 @@ import Simplex.Messaging.Agent.Protocol import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Notifications.Protocol import Simplex.Messaging.Notifications.Server.Push.APNS -import Simplex.Messaging.Protocol (ErrorType (AUTH)) +import Simplex.Messaging.Protocol (ErrorType (AUTH), MsgFlags (MsgFlags)) import qualified Simplex.Messaging.Protocol as SMP import Simplex.Messaging.Transport (ATransport) import Simplex.Messaging.Util (tryE) @@ -189,7 +189,7 @@ testNotificationSubscriptionExistingConnection :: APNSMockServer -> IO () testNotificationSubscriptionExistingConnection APNSMockServer {apnsQ} = do alice <- getSMPAgentClient agentCfg initAgentServers bob <- getSMPAgentClient agentCfg {dbFile = testDB2} initAgentServers - Right () <- runExceptT $ do + Right (bobId, checkMessage, nonce) <- runExceptT $ do -- establish connection (bobId, qInfo) <- createConnection alice SCMInvitation aliceId <- joinConnection bob qInfo "bob's connInfo" @@ -204,15 +204,29 @@ testNotificationSubscriptionExistingConnection APNSMockServer {apnsQ} = do APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData}, sendApnsResponse} <- atomically $ readTBQueue apnsQ verification <- ntfData .-> "verification" - nonce <- C.cbNonce <$> ntfData .-> "nonce" + verificationNonce <- C.cbNonce <$> ntfData .-> "nonce" liftIO $ sendApnsResponse APNSRespOk - verifyNtfToken alice tkn verification nonce + verifyNtfToken alice tkn verification verificationNonce NTActive <- checkNtfToken alice tkn -- send message liftIO $ threadDelay 50000 1 <- msgId <$> sendMessage bob aliceId (SMP.MsgFlags True) "hello" get bob ##> ("", aliceId, SENT $ baseId + 1) - messageNotification apnsQ + -- notification + (checkMessage, nonce) <- messageNotification apnsQ + pure (bobId, checkMessage, nonce) + + -- alice client already has subscription for the connection + Left (CMD PROHIBITED) <- runExceptT $ getNotificationMessage alice checkMessage nonce + + -- aliceNtf client doesn't have subscription and is allowed to get notification message + aliceNtf <- getSMPAgentClient agentCfg initAgentServers + Right () <- runExceptT $ do + Just (_msgId, MsgFlags True) <- getNotificationMessage aliceNtf checkMessage nonce + pure () + disconnectAgentClient aliceNtf + + Right () <- runExceptT $ do get alice =##> \case ("", c, Msg "hello") -> c == bobId; _ -> False ackMessage alice bobId $ baseId + 1 -- no unexpected notifications should follow @@ -254,26 +268,26 @@ testNotificationSubscriptionNewConnection APNSMockServer {apnsQ} = do (bobId, qInfo) <- createConnection alice SCMInvitation liftIO $ threadDelay 500000 aliceId <- joinConnection bob qInfo "bob's connInfo" - messageNotification apnsQ + void $ messageNotification apnsQ ("", _, CONF confId "bob's connInfo") <- get alice liftIO $ threadDelay 500000 allowConnection alice bobId confId "alice's connInfo" - messageNotification apnsQ + void $ messageNotification apnsQ get bob ##> ("", aliceId, INFO "alice's connInfo") - messageNotification apnsQ + void $ messageNotification apnsQ get alice ##> ("", bobId, CON) - messageNotification apnsQ + void $ messageNotification apnsQ get bob ##> ("", aliceId, CON) -- bob sends message 1 <- msgId <$> sendMessage bob aliceId (SMP.MsgFlags True) "hello" get bob ##> ("", aliceId, SENT $ baseId + 1) - messageNotification apnsQ + void $ messageNotification apnsQ get alice =##> \case ("", c, Msg "hello") -> c == bobId; _ -> False ackMessage alice bobId $ baseId + 1 -- alice sends message 2 <- msgId <$> sendMessage alice bobId (SMP.MsgFlags True) "hey there" get alice ##> ("", bobId, SENT $ baseId + 2) - messageNotification apnsQ + void $ messageNotification apnsQ get bob =##> \case ("", c, Msg "hey there") -> c == aliceId; _ -> False ackMessage bob aliceId $ baseId + 2 -- no unexpected notifications should follow @@ -285,12 +299,13 @@ testNotificationSubscriptionNewConnection APNSMockServer {apnsQ} = do baseId = 3 msgId = subtract baseId -messageNotification :: TBQueue APNSMockRequest -> ExceptT AgentErrorType IO () +messageNotification :: TBQueue APNSMockRequest -> ExceptT AgentErrorType IO (ByteString, C.CbNonce) messageNotification apnsQ = do 500000 `timeout` atomically (readTBQueue apnsQ) >>= \case Nothing -> error "no notification" Just APNSMockRequest {notification = APNSNotification {aps = APNSMutableContent {}, notificationData = Just ntfData}, sendApnsResponse} -> do - _ <- ntfData .-> "checkMessage" - _ <- C.cbNonce <$> ntfData .-> "nonce" + checkMessage <- ntfData .-> "checkMessage" + nonce <- C.cbNonce <$> ntfData .-> "nonce" liftIO $ sendApnsResponse APNSRespOk + pure (checkMessage, nonce) _ -> error "bad notification" diff --git a/tests/NtfServerTests.hs b/tests/NtfServerTests.hs index 99955cc5f..f12bc35c1 100644 --- a/tests/NtfServerTests.hs +++ b/tests/NtfServerTests.hs @@ -1,4 +1,5 @@ {-# LANGUAGE DataKinds #-} +{-# LANGUAGE DuplicateRecordFields #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE OverloadedStrings #-} @@ -27,11 +28,14 @@ import ServerTests _SEND', pattern Resp, ) +import qualified Simplex.Messaging.Agent.Protocol as AP import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Encoding import Simplex.Messaging.Encoding.String import Simplex.Messaging.Notifications.Protocol import Simplex.Messaging.Notifications.Server.Push.APNS +import qualified Simplex.Messaging.Notifications.Server.Push.APNS as APNS +import Simplex.Messaging.Parsers (parse) import Simplex.Messaging.Protocol hiding (notification) import Simplex.Messaging.Transport import Test.Hspec @@ -91,7 +95,7 @@ testNotificationSubscription (ATransport t) = smpTest2 t $ \rh sh -> ntfTest t $ \nh -> do -- create queue - (sId, rId, rKey, _dhShared) <- createAndSecureQueue rh sPub + (sId, rId, rKey, rcvDhSecret) <- createAndSecureQueue rh sPub -- register and verify token RespNtf "1" "" (NRTknId tId ntfDh) <- signSendRecvNtf nh tknKey ("1", "", TNEW $ NewNtfTkn tkn tknPub dhPub) APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData}, sendApnsResponse = send} <- @@ -116,12 +120,19 @@ testNotificationSubscription (ATransport t) = let APNSNotification {aps = APNSMutableContent {}, notificationData = Just ntfData'} = notification Right checkMessage = ntfData' .-> "checkMessage" Right nonce' = C.cbNonce <$> ntfData' .-> "nonce" - Right smpQueueURI = C.cbDecrypt dhSecret nonce' checkMessage - smpQueueURI `shouldBe` strEncode srv <> "/" <> strEncode nId + Right ntfDataDecrypted = C.cbDecrypt dhSecret nonce' checkMessage + Right APNS.PNMessageData {smpQueue = SMPQueueNtf {smpServer, notifierId}, nmsgNonce, encNMsgMeta} = + parse strP (AP.INTERNAL "error parsing PNMessageData") ntfDataDecrypted + Right nMsgMeta = C.cbDecrypt rcvDhSecret nmsgNonce encNMsgMeta + Right NMsgMeta {msgId, msgTs} = parse smpP (AP.INTERNAL "error parsing NMsgMeta") nMsgMeta + smpServer `shouldBe` srv + notifierId `shouldBe` nId send' APNSRespOk -- receive message - let dec _nonce = C.cbDecrypt _dhShared (C.cbNonce _nonce) - Resp "" _ (MSG mId1 _ _ msg1) <- tGet rh - (dec mId1 msg1, Right "hello") #== "delivered from queue" + Resp "" _ (MSG mId1 mTs _ msg1) <- tGet rh + mId1 `shouldBe` msgId + mTs `shouldBe` msgTs + let decryptedMsg = C.cbDecrypt rcvDhSecret (C.cbNonce mId1) msg1 + (decryptedMsg, Right "hello") #== "delivered from queue" Resp "6" _ OK <- signSendRecv rh rKey ("6", rId, ACK mId1) pure () diff --git a/tests/ServerTests.hs b/tests/ServerTests.hs index 6e561772b..e72b21d7a 100644 --- a/tests/ServerTests.hs +++ b/tests/ServerTests.hs @@ -417,7 +417,7 @@ testWithStoreLog at@(ATransport t) = Resp "bcda" _ OK <- signSendRecv h sKey1 ("bcda", sId1, _SEND' "hello") Resp "" _ (MSG mId1 _ _ msg1) <- tGet h (C.cbDecrypt dhShared (C.cbNonce mId1) msg1, Right "hello") #== "delivered from queue 1" - Resp "" _ NMSG <- tGet h1 + Resp "" _ (NMSG _ _) <- tGet h1 (sId2, rId2, rKey2, dhShared2) <- createAndSecureQueue h sPub2 atomically $ writeTVar senderId2 sId2 @@ -447,7 +447,7 @@ testWithStoreLog at@(ATransport t) = Resp "bcda" _ OK <- signSendRecv h sKey1 ("bcda", sId1, _SEND' "hello") Resp "cdab" _ (MSG mId3 _ _ msg3) <- signSendRecv h rKey1 ("cdab", rId1, SUB) (C.cbDecrypt dh1 (C.cbNonce mId3) msg3, Right "hello") #== "delivered from restored queue" - Resp "" _ NMSG <- tGet h1 + Resp "" _ (NMSG _ _) <- tGet h1 -- this queue is removed - not restored sId2 <- readTVarIO senderId2 Resp "cdab" _ (ERR AUTH) <- signSendRecv h sKey2 ("cdab", sId2, _SEND "hello too") @@ -616,13 +616,13 @@ testMessageNotifications (ATransport t) = Resp "" _ (MSG mId1 _ _ msg1) <- tGet rh (dec mId1 msg1, Right "hello") #== "delivered from queue" Resp "3a" _ OK <- signSendRecv rh rKey ("3a", rId, ACK mId1) - Resp "" _ NMSG <- tGet nh1 + Resp "" _ (NMSG _ _) <- tGet nh1 Resp "4" _ OK <- signSendRecv nh2 nKey ("4", nId, NSUB) Resp "" _ END <- tGet nh1 Resp "5" _ OK <- signSendRecv sh sKey ("5", sId, _SEND' "hello again") Resp "" _ (MSG mId2 _ _ msg2) <- tGet rh (dec mId2 msg2, Right "hello again") #== "delivered from queue again" - Resp "" _ NMSG <- tGet nh2 + Resp "" _ (NMSG _ _) <- tGet nh2 1000 `timeout` tGet @BrokerMsg nh1 >>= \case Nothing -> return () Just _ -> error "nothing else should be delivered to the 1st notifier's TCP connection" From d1db7d6f79b527a689af1807e7d91a92076d2d8c Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Sat, 18 Jun 2022 18:01:04 +0100 Subject: [PATCH 066/131] withStore runs in a single transaction (#408) * remove store typeclass * make withStore run single transaction * combine 2 store calls * refactor * add exports * fix catch * correction --- src/Simplex/Messaging/Agent.hs | 166 ++- src/Simplex/Messaging/Agent/Client.hs | 21 +- .../Messaging/Agent/NtfSubSupervisor.hs | 37 +- src/Simplex/Messaging/Agent/Store.hs | 80 +- src/Simplex/Messaging/Agent/Store/SQLite.hs | 1326 ++++++++--------- src/Simplex/Messaging/Util.hs | 4 + tests/AgentTests/SQLiteTests.hs | 328 ++-- 7 files changed, 945 insertions(+), 1017 deletions(-) diff --git a/src/Simplex/Messaging/Agent.hs b/src/Simplex/Messaging/Agent.hs index d863c23ad..12441a15d 100644 --- a/src/Simplex/Messaging/Agent.hs +++ b/src/Simplex/Messaging/Agent.hs @@ -81,13 +81,14 @@ import qualified Data.Text as T import Data.Time.Clock import Data.Time.Clock.System (systemToUTCTime) import Data.Word (Word16) +import qualified Database.SQLite.Simple as DB import Simplex.Messaging.Agent.Client import Simplex.Messaging.Agent.Env.SQLite import Simplex.Messaging.Agent.NtfSubSupervisor import Simplex.Messaging.Agent.Protocol import Simplex.Messaging.Agent.RetryInterval import Simplex.Messaging.Agent.Store -import Simplex.Messaging.Agent.Store.SQLite (AgentStoreMonad, SQLiteStore) +import Simplex.Messaging.Agent.Store.SQLite import Simplex.Messaging.Client (ProtocolClient (..), ServerTransmission) import qualified Simplex.Messaging.Crypto as C import qualified Simplex.Messaging.Crypto.Ratchet as CR @@ -100,7 +101,7 @@ import Simplex.Messaging.Parsers (parse) import Simplex.Messaging.Protocol (BrokerMsg, ErrorType (AUTH), MsgBody, MsgFlags, NMsgMeta (..)) import qualified Simplex.Messaging.Protocol as SMP import qualified Simplex.Messaging.TMap as TM -import Simplex.Messaging.Util (bshow, liftError, tryError, unlessM, whenM, ($>>=)) +import Simplex.Messaging.Util (bshow, liftE, liftError, tryError, unlessM, whenM, ($>>=)) import Simplex.Messaging.Version import System.Random (randomR) import UnliftIO.Async (async, race_) @@ -255,7 +256,7 @@ newConn c connId cMode = do g <- asks idsDrg agentVersion <- asks $ smpAgentVersion . config let cData = ConnData {connId, connAgentVersion = agentVersion, duplexHandshake = Nothing} -- connection mode is determined by the accepting agent - connId' <- withStore c $ \st -> createRcvConn st g cData rq cMode + connId' <- withStore c $ \db -> createRcvConn db g cData rq cMode addSubscription c rq connId' ns <- asks ntfSupervisor atomically $ sendNtfSubCommand ns (connId', NSCCreate) @@ -265,7 +266,7 @@ newConn c connId cMode = do SCMContact -> pure (connId', CRContactUri crData) SCMInvitation -> do (pk1, pk2, e2eRcvParams) <- liftIO $ CR.generateE2EParams CR.e2eEncryptVersion - withStore c $ \st -> createRatchetX3dhKeys st connId' pk1 pk2 + withStore' c $ \db -> createRatchetX3dhKeys db connId' pk1 pk2 pure (connId', CRInvitationUri crData $ toVersionRangeT e2eRcvParams CR.e2eEncryptVRange) joinConn :: AgentMonad m => AgentClient -> ConnId -> ConnectionRequestUri c -> ConnInfo -> m ConnId @@ -284,9 +285,9 @@ joinConn c connId (CRInvitationUri (ConnReqUriData _ agentVRange (qUri :| _)) e2 g <- asks idsDrg let duplexHS = connAgentVersion /= 1 cData = ConnData {connId, connAgentVersion, duplexHandshake = Just duplexHS} - connId' <- withStore c $ \st -> do - connId' <- createSndConn st g cData sq - createRatchet st connId' rc + connId' <- withStore c $ \db -> runExceptT $ do + connId' <- ExceptT $ createSndConn db g cData sq + liftIO $ createRatchet db connId' rc pure connId' let cData' = (cData :: ConnData) {connId = connId'} tryError (confirmQueue aVersion c connId' sq cInfo $ Just e2eSndParams) >>= \case @@ -295,7 +296,7 @@ joinConn c connId (CRInvitationUri (ConnReqUriData _ agentVRange (qUri :| _)) e2 pure connId' Left e -> do -- TODO recovery for failure on network timeout, see rfcs/2022-04-20-smp-conf-timeout-recovery.md - withStore c (`deleteConn` connId') + withStore' c (`deleteConn` connId') throwError e _ -> throwError $ AGENT A_VERSION joinConn c connId (CRContactUri (ConnReqUriData _ agentVRange (qUri :| _))) cInfo = do @@ -317,18 +318,20 @@ createReplyQueue c connId = do -- TODO reply queue version should be the same as send queue, ignoring it in v1 let qInfo = toVersionT qUri SMP.smpClientVersion addSubscription c rq connId - withStore c $ \st -> upgradeSndConnToDuplex st connId rq + withStore c $ \db -> upgradeSndConnToDuplex db connId rq ns <- asks ntfSupervisor atomically $ sendNtfSubCommand ns (connId, NSCCreate) pure qInfo -- | Approve confirmation (LET command) in Reader monad allowConnection' :: AgentMonad m => AgentClient -> ConnId -> ConfirmationId -> ConnInfo -> m () -allowConnection' c connId confId ownConnInfo = do +allowConnection' c connId confId ownConnInfo = withStore c (`getConn` connId) >>= \case SomeConn _ (RcvConnection cData rq) -> do - AcceptedConfirmation {senderConf, ratchetState} <- withStore c $ \st -> acceptConfirmation st confId ownConnInfo - withStore c $ \st -> createRatchet st connId ratchetState + AcceptedConfirmation {senderConf} <- withStore c $ \db -> runExceptT $ do + conf <- ExceptT $ acceptConfirmation db confId ownConnInfo + liftIO $ createRatchet db connId $ ratchetState (conf :: AcceptedConfirmation) + pure conf processConfirmation c rq senderConf mapM_ (connectReplyQueues c cData ownConnInfo) (L.nonEmpty $ smpReplyQueues senderConf) _ -> throwError $ CMD PROHIBITED @@ -339,21 +342,21 @@ acceptContact' c connId invId ownConnInfo = do Invitation {contactConnId, connReq} <- withStore c (`getInvitation` invId) withStore c (`getConn` contactConnId) >>= \case SomeConn _ ContactConnection {} -> do - withStore c $ \st -> acceptInvitation st invId ownConnInfo + withStore' c $ \db -> acceptInvitation db invId ownConnInfo joinConn c connId connReq ownConnInfo _ -> throwError $ CMD PROHIBITED -- | Reject contact (RJCT command) in Reader monad rejectContact' :: AgentMonad m => AgentClient -> ConnId -> InvitationId -> m () rejectContact' c contactConnId invId = - withStore c $ \st -> deleteInvitation st contactConnId invId + withStore c $ \db -> deleteInvitation db contactConnId invId processConfirmation :: AgentMonad m => AgentClient -> RcvQueue -> SMPConfirmation -> m () processConfirmation c rq@RcvQueue {e2ePrivKey} SMPConfirmation {senderKey, e2ePubKey} = do let dhSecret = C.dh' e2ePubKey e2ePrivKey - withStore c $ \st -> setRcvQueueConfirmedE2E st rq dhSecret + withStore' c $ \db -> setRcvQueueConfirmedE2E db rq dhSecret secureQueue c rq senderKey - withStore c $ \st -> setRcvQueueStatus st rq Secured + withStore' c $ \db -> setRcvQueueStatus db rq Secured -- | Subscribe to receive connection messages (SUB command) in Reader monad subscribeConnection' :: forall m. AgentMonad m => AgentClient -> ConnId -> m () @@ -394,11 +397,11 @@ getConnectionMessage' c connId = do getNotificationMessage' :: forall m. AgentMonad m => AgentClient -> ByteString -> C.CbNonce -> m (Maybe (SMP.MsgId, MsgFlags)) getNotificationMessage' c encMessageInfo nonce = do - withStore c getActiveNtfToken >>= \case + withStore' c getActiveNtfToken >>= \case Just NtfToken {ntfDhSecret = Just dhSecret} -> do ntfData <- agentCbDecrypt dhSecret nonce encMessageInfo PNMessageData {smpQueue, ntfTs, nmsgNonce, encNMsgMeta} <- liftEither (parse strP (INTERNAL "error parsing PNMessageData") ntfData) - (connId, rcvDhSecret) <- withStore c $ \st -> getNtfRcvQueue st smpQueue + (connId, rcvDhSecret) <- withStore c (`getNtfRcvQueue` smpQueue) nMsgMeta <- agentCbDecrypt rcvDhSecret nmsgNonce encNMsgMeta `catchError` const (pure "") let nMsgMetaParsed = parse smpP (INTERNAL "error parsing NMsgMeta") nMsgMeta case nMsgMetaParsed of @@ -426,18 +429,18 @@ enqueueMessage c cData@ConnData {connId, connAgentVersion} sq msgFlags aMessage pure $ unId msgId where storeSentMsg :: m InternalId - storeSentMsg = withStore c $ \st -> do + storeSentMsg = withStore c $ \db -> runExceptT $ do internalTs <- liftIO getCurrentTime - (internalId, internalSndId, prevMsgHash) <- updateSndIds st connId + (internalId, internalSndId, prevMsgHash) <- liftIO $ updateSndIds db connId let privHeader = APrivHeader (unSndId internalSndId) prevMsgHash agentMsg = AgentMessage privHeader aMessage agentMsgStr = smpEncode agentMsg internalHash = C.sha256Hash agentMsgStr - encAgentMessage <- agentRatchetEncrypt st connId agentMsgStr e2eEncUserMsgLength + encAgentMessage <- agentRatchetEncrypt db connId agentMsgStr e2eEncUserMsgLength let msgBody = smpEncode $ AgentMsgEnvelope {agentVersion = connAgentVersion, encAgentMessage} msgType = agentMessageType agentMsg msgData = SndMsgData {internalId, internalSndId, internalTs, msgType, msgFlags, msgBody, internalHash, prevMsgHash} - createSndMsg st connId msgData + liftIO $ createSndMsg db connId msgData pure internalId resumeMsgDelivery :: forall m. AgentMonad m => AgentClient -> ConnData -> SndQueue -> m () @@ -447,7 +450,7 @@ resumeMsgDelivery c cData@ConnData {connId} sq@SndQueue {server, sndId} = do async (runSmpQueueMsgDelivery c cData sq) >>= \a -> atomically (TM.insert qKey a $ smpQueueMsgDeliveries c) unlessM connQueued $ - withStore c (`getPendingMsgs` connId) + withStore' c (`getPendingMsgs` connId) >>= queuePendingMsgs c connId sq where queueDelivering qKey = atomically $ TM.member qKey (smpQueueMsgDeliveries c) @@ -477,7 +480,7 @@ runSmpQueueMsgDelivery c@AgentClient {subQ} cData@ConnData {connId, duplexHandsh msgId <- atomically $ readTQueue mq atomically $ beginAgentOperation c AONetwork let mId = unId msgId - withStore c (\st -> E.try $ getPendingMsgData st connId msgId) >>= \case + E.try (withStore c $ \db -> getPendingMsgData db connId msgId) >>= \case Left (e :: E.SomeException) -> notify $ MERR mId (INTERNAL $ show e) Right (rq_, PendingMsgData {msgType, msgBody, msgFlags, internalTs}) -> @@ -521,12 +524,13 @@ runSmpQueueMsgDelivery c@AgentClient {subQ} cData@ConnData {connId, duplexHandsh Right () -> do case msgType of AM_CONN_INFO -> do - withStore c $ \st -> setSndQueueStatus st sq Confirmed - when (isJust rq_) $ withStore c (`removeConfirmations` connId) + withStore' c $ \db -> do + setSndQueueStatus db sq Confirmed + when (isJust rq_) $ removeConfirmations db connId -- TODO possibly notification flag should be ON for one of the parties, to result in contact connected notification unless (duplexHandshake == Just True) . void $ enqueueMessage c cData sq SMP.noMsgFlags HELLO AM_HELLO_ -> do - withStore c $ \st -> setSndQueueStatus st sq Active + withStore' c $ \db -> setSndQueueStatus db sq Active case rq_ of -- party initiating connection (in v1) Just RcvQueue {status} -> @@ -551,7 +555,7 @@ runSmpQueueMsgDelivery c@AgentClient {subQ} cData@ConnData {connId, duplexHandsh delMsg msgId where delMsg :: InternalId -> m () - delMsg msgId = withStore c $ \st -> deleteMsg st connId msgId + delMsg msgId = withStore' c $ \db -> deleteMsg db connId msgId notify :: ACommand 'Agent -> m () notify cmd = atomically $ writeTBQueue subQ ("", connId, cmd) notifyDel :: InternalId -> ACommand 'Agent -> m () @@ -574,11 +578,11 @@ ackMessage' c connId msgId = do ack :: RcvQueue -> m () ack rq = do let mId = InternalId msgId - srvMsgId <- withStore c $ \st -> setMsgUserAck st connId mId + srvMsgId <- withStore c $ \db -> setMsgUserAck db connId mId sendAck c rq srvMsgId `catchError` \case SMP SMP.NO_MSG -> pure () e -> throwError e - withStore c $ \st -> deleteMsg st connId mId + withStore' c $ \db -> deleteMsg db connId mId -- | Suspend SMP agent connection (OFF command) in Reader monad suspendConnection' :: AgentMonad m => AgentClient -> ConnId -> m () @@ -596,7 +600,7 @@ deleteConnection' c connId = SomeConn _ (DuplexConnection _ rq _) -> delete rq SomeConn _ (RcvConnection _ rq) -> delete rq SomeConn _ (ContactConnection _ rq) -> delete rq - SomeConn _ (SndConnection _ _) -> withStore c (`deleteConn` connId) + SomeConn _ (SndConnection _ _) -> withStore' c (`deleteConn` connId) where delete :: RcvQueue -> m () delete rq = do @@ -605,7 +609,7 @@ deleteConnection' c connId = atomically $ do removeSubscription c connId sendNtfSubCommand ns (connId, NSCDelete) - withStore c (`deleteConn` connId) + withStore' c (`deleteConn` connId) -- | Change servers to be used for creating new queues, in Reader monad setSMPServers' :: AgentMonad m => AgentClient -> NonEmpty SMPServer -> m () @@ -614,7 +618,7 @@ setSMPServers' c servers = do registerNtfToken' :: forall m. AgentMonad m => AgentClient -> DeviceToken -> m NtfTknStatus registerNtfToken' c deviceToken = - withStore c (`getDeviceNtfToken` deviceToken) >>= \case + withStore' c (`getDeviceNtfToken` deviceToken) >>= \case (Just tkn@NtfToken {ntfTokenId, ntfTknStatus, ntfTknAction}, prevTokens) -> do mapM_ (deleteToken_ c) prevTokens ns <- asks ntfSupervisor @@ -634,7 +638,7 @@ registerNtfToken' c deviceToken = -- agentNtfCheckToken c tknId tkn >>= \case (Just tknId, Just NTADelete) -> do agentNtfDeleteToken c tknId tkn - withStore c $ \st -> removeNtfToken st tkn + withStore' c (`removeNtfToken` tkn) atomically $ nsRemoveNtfToken ns pure NTExpired _ -> pure ntfTknStatus @@ -646,7 +650,7 @@ registerNtfToken' c deviceToken = tknKeys <- liftIO $ C.generateSignatureKeyPair a dhKeys <- liftIO C.generateKeyPair' let tkn = newNtfToken deviceToken ntfServer tknKeys dhKeys - withStore c $ \st -> createNtfToken st tkn + withStore' c (`createNtfToken` tkn) registerToken tkn pure NTRegistered _ -> throwError $ CMD PROHIBITED @@ -656,14 +660,14 @@ registerNtfToken' c deviceToken = registerToken tkn@NtfToken {ntfPubKey, ntfDhKeys = (pubDhKey, privDhKey)} = do (tknId, srvPubDhKey) <- agentNtfRegisterToken c tkn ntfPubKey pubDhKey let dhSecret = C.dh' srvPubDhKey privDhKey - withStore c $ \st -> updateNtfTokenRegistration st tkn tknId dhSecret + withStore' c $ \db -> updateNtfTokenRegistration db tkn tknId dhSecret ns <- asks ntfSupervisor atomically $ nsUpdateToken ns tkn -- TODO decrypt verification code verifyNtfToken' :: AgentMonad m => AgentClient -> DeviceToken -> ByteString -> C.CbNonce -> m () verifyNtfToken' c deviceToken code nonce = - withStore c (`getDeviceNtfToken` deviceToken) >>= \case + withStore' c (`getDeviceNtfToken` deviceToken) >>= \case (Just tkn@NtfToken {ntfTokenId = Just tknId, ntfDhSecret = Just dhSecret}, _) -> do code' <- liftEither . bimap cryptoError NtfRegCode $ C.cbDecrypt dhSecret nonce code void . withToken c tkn (Just (NTConfirmed, NTAVerify code')) (NTActive, Just NTACheck) $ do @@ -673,7 +677,7 @@ verifyNtfToken' c deviceToken code nonce = enableNtfCron' :: AgentMonad m => AgentClient -> DeviceToken -> Word16 -> m () enableNtfCron' c deviceToken interval = do when (interval < 20) . throwError $ CMD PROHIBITED - withStore c (`getDeviceNtfToken` deviceToken) >>= \case + withStore' c (`getDeviceNtfToken` deviceToken) >>= \case (Just tkn@NtfToken {ntfTokenId = Just tknId, ntfTknStatus = NTActive}, _) -> void . withToken c tkn (Just (NTActive, NTACron interval)) (cronSuccess interval) $ agentNtfEnableCron c tknId tkn interval @@ -686,13 +690,13 @@ cronSuccess interval checkNtfToken' :: AgentMonad m => AgentClient -> DeviceToken -> m NtfTknStatus checkNtfToken' c deviceToken = - withStore c (`getDeviceNtfToken` deviceToken) >>= \case + withStore' c (`getDeviceNtfToken` deviceToken) >>= \case (Just tkn@NtfToken {ntfTokenId = Just tknId}, _) -> agentNtfCheckToken c tknId tkn _ -> throwError $ CMD PROHIBITED deleteNtfToken' :: AgentMonad m => AgentClient -> DeviceToken -> m () deleteNtfToken' c deviceToken = - withStore c (`getDeviceNtfToken` deviceToken) >>= \case + withStore' c (`getDeviceNtfToken` deviceToken) >>= \case (Just tkn, _) -> deleteToken_ c tkn _ -> throwError $ CMD PROHIBITED @@ -701,30 +705,30 @@ deleteToken_ c tkn@NtfToken {ntfTokenId, ntfTknStatus} = do ns <- asks ntfSupervisor forM_ ntfTokenId $ \tknId -> do let ntfTknAction = Just NTADelete - withStore c $ \st -> updateNtfToken st tkn ntfTknStatus ntfTknAction + withStore' c $ \db -> updateNtfToken db tkn ntfTknStatus ntfTknAction atomically $ nsUpdateToken ns tkn {ntfTknStatus, ntfTknAction} agentNtfDeleteToken c tknId tkn `catchError` \case NTF AUTH -> pure () e -> throwError e - withStore c $ \st -> removeNtfToken st tkn + withStore' c $ \db -> removeNtfToken db tkn atomically $ nsRemoveNtfToken ns withToken :: AgentMonad m => AgentClient -> NtfToken -> Maybe (NtfTknStatus, NtfTknAction) -> (NtfTknStatus, Maybe NtfTknAction) -> m a -> m NtfTknStatus withToken c tkn@NtfToken {deviceToken} from_ (toStatus, toAction_) f = do ns <- asks ntfSupervisor forM_ from_ $ \(status, action) -> do - withStore c $ \st -> updateNtfToken st tkn status (Just action) + withStore' c $ \db -> updateNtfToken db tkn status (Just action) atomically $ nsUpdateToken ns tkn {ntfTknStatus = status, ntfTknAction = Just action} tryError f >>= \case Right _ -> do - withStore c $ \st -> updateNtfToken st tkn toStatus toAction_ + withStore' c $ \db -> updateNtfToken db tkn toStatus toAction_ let updatedToken = tkn {ntfTknStatus = toStatus, ntfTknAction = toAction_} if toStatus == NTActive then initializeNtfSubQ c updatedToken else atomically $ nsUpdateToken ns updatedToken pure toStatus Left e@(NTF AUTH) -> do - withStore c $ \st -> removeNtfToken st tkn + withStore' c $ \db -> removeNtfToken db tkn atomically $ nsRemoveNtfToken ns void $ registerNtfToken' c deviceToken throwError e @@ -774,7 +778,7 @@ subscriber c@AgentClient {msgQ} = forever $ do processSMPTransmission :: forall m. AgentMonad m => AgentClient -> ServerTransmission BrokerMsg -> m () processSMPTransmission c@AgentClient {smpClients, subQ} (srv, sessId, rId, cmd) = - withStore c (\st -> getRcvConn st srv rId) >>= \case + withStore c (\db -> getRcvConn db srv rId) >>= \case SomeConn _ conn@(DuplexConnection cData rq _) -> processSMP conn cData rq SomeConn _ conn@(RcvConnection cData rq) -> processSMP conn cData rq SomeConn _ conn@(ContactConnection cData rq) -> processSMP conn cData rq @@ -803,19 +807,19 @@ processSMPTransmission c@AgentClient {smpClients, subQ} (srv, sessId, rId, cmd) (SMP.PHEmpty, AgentMsgEnvelope _ encAgentMsg) -> tryError agentClientMsg >>= \case Right (Just (msgId, msgMeta, aMessage)) -> case aMessage of - HELLO -> helloMsg >> ack >> withStore c (\st -> deleteMsg st connId msgId) - REPLY cReq -> replyMsg cReq >> ack >> withStore c (\st -> deleteMsg st connId msgId) + HELLO -> helloMsg >> ack >> withStore' c (\db -> deleteMsg db connId msgId) + REPLY cReq -> replyMsg cReq >> ack >> withStore' c (\db -> deleteMsg db connId msgId) -- note that there is no ACK sent for A_MSG, it is sent with agent's user ACK command A_MSG body -> do logServer "<--" c srv rId "MSG " notify $ MSG msgMeta msgFlags body Right _ -> prohibited >> ack Left e@(AGENT A_DUPLICATE) -> do - withStore c (\st -> getLastMsg st connId srvMsgId) >>= \case + withStore' c (\db -> getLastMsg db connId srvMsgId) >>= \case Just RcvMsg {internalId, msgMeta, msgBody = agentMsgBody, userAck} | userAck -> do ack - withStore c $ \st -> deleteMsg st connId internalId + withStore' c $ \db -> deleteMsg db connId internalId | otherwise -> do liftEither (parse smpP (AGENT A_MESSAGE) agentMsgBody) >>= \case AgentMessage _ (A_MSG body) -> do @@ -826,20 +830,20 @@ processSMPTransmission c@AgentClient {smpClients, subQ} (srv, sessId, rId, cmd) Left e -> throwError e where agentClientMsg :: m (Maybe (InternalId, MsgMeta, AMessage)) - agentClientMsg = withStore c $ \st -> do - agentMsgBody <- agentRatchetDecrypt st connId encAgentMsg + agentClientMsg = withStore c $ \db -> runExceptT $ do + agentMsgBody <- agentRatchetDecrypt db connId encAgentMsg liftEither (parse smpP (SEAgentError $ AGENT A_MESSAGE) agentMsgBody) >>= \case agentMsg@(AgentMessage APrivHeader {sndMsgId, prevMsgHash} aMessage) -> do let msgType = agentMessageType agentMsg internalHash = C.sha256Hash agentMsgBody internalTs <- liftIO getCurrentTime - (internalId, internalRcvId, prevExtSndId, prevRcvMsgHash) <- updateRcvIds st connId + (internalId, internalRcvId, prevExtSndId, prevRcvMsgHash) <- liftIO $ updateRcvIds db connId let integrity = checkMsgIntegrity prevExtSndId sndMsgId prevRcvMsgHash prevMsgHash recipient = (unId internalId, internalTs) broker = (srvMsgId, systemToUTCTime srvTs) msgMeta = MsgMeta {integrity, recipient, broker, sndMsgId} rcvMsg = RcvMsgData {msgMeta, msgType, msgFlags, msgBody = agentMsgBody, internalRcvId, internalHash, externalPrevSndHash = prevMsgHash} - createRcvMsg st connId rcvMsg + liftIO $ createRcvMsg db connId rcvMsg pure $ Just (internalId, msgMeta, aMessage) _ -> pure Nothing _ -> prohibited >> ack @@ -900,7 +904,7 @@ processSMPTransmission c@AgentClient {smpClients, subQ} (srv, sessId, rId, cmd) New -> case (conn, e2eEncryption) of -- party initiating connection (RcvConnection {}, Just e2eSndParams) -> do - (pk1, rcDHRs) <- withStore c $ \st -> getRatchetX3dhKeys st connId + (pk1, rcDHRs) <- withStore c $ (`getRatchetX3dhKeys` connId) let rc = CR.initRcvRatchet rcDHRs $ CR.x3dhRcv pk1 rcDHRs e2eSndParams (agentMsgBody_, rc', skipped) <- liftError cryptoError $ CR.rcDecrypt rc M.empty encConnInfo case (agentMsgBody_, skipped) of @@ -915,14 +919,14 @@ processSMPTransmission c@AgentClient {smpClients, subQ} (srv, sessId, rId, cmd) processConf connInfo senderConf duplexHS = do let newConfirmation = NewConfirmation {connId, senderConf, ratchetState = rc'} g <- asks idsDrg - confId <- withStore c $ \st -> do - setHandshakeVersion st connId agentVersion duplexHS - createConfirmation st g newConfirmation + confId <- withStore c $ \db -> do + setHandshakeVersion db connId agentVersion duplexHS + createConfirmation db g newConfirmation notify $ CONF confId connInfo _ -> prohibited -- party accepting connection (DuplexConnection _ _ sq, Nothing) -> do - withStore c (\st -> agentRatchetDecrypt st connId encConnInfo) >>= parseMessage >>= \case + withStore c (\db -> runExceptT $ agentRatchetDecrypt db connId encConnInfo) >>= parseMessage >>= \case AgentConnInfo connInfo -> do notify $ INFO connInfo processConfirmation c rq $ SMPConfirmation {senderKey, e2ePubKey, connInfo, smpReplyQueues = []} @@ -937,7 +941,7 @@ processSMPTransmission c@AgentClient {smpClients, subQ} (srv, sessId, rId, cmd) case status of Active -> prohibited _ -> do - withStore c $ \st -> setRcvQueueStatus st rq Active + withStore' c $ \db -> setRcvQueueStatus db rq Active case conn of DuplexConnection _ _ sq@SndQueue {status = sndStatus} -- `sndStatus == Active` when HELLO was previously sent, and this is the reply HELLO @@ -970,7 +974,7 @@ processSMPTransmission c@AgentClient {smpClients, subQ} (srv, sessId, rId, cmd) ContactConnection {} -> do g <- asks idsDrg let newInv = NewInvitation {contactConnId = connId, connReq, recipientConnInfo = cInfo} - invId <- withStore c $ \st -> createInvitation st g newInv + invId <- withStore c $ \db -> createInvitation db g newInv notify $ REQ invId cInfo _ -> prohibited @@ -990,7 +994,7 @@ connectReplyQueues c cData@ConnData {connId} ownConnInfo (qInfo :| _) = do Nothing -> throwError $ AGENT A_VERSION Just qInfo' -> do sq <- newSndQueue qInfo' - withStore c $ \st -> upgradeRcvConnToDuplex st connId sq + withStore c $ \db -> upgradeRcvConnToDuplex db connId sq enqueueConfirmation c cData sq ownConnInfo Nothing confirmQueue :: forall m. AgentMonad m => Compatible Version -> AgentClient -> ConnId -> SndQueue -> ConnInfo -> Maybe (CR.E2ERatchetParams 'C.X448) -> m () @@ -998,12 +1002,12 @@ confirmQueue (Compatible agentVersion) c connId sq connInfo e2eEncryption = do aMessage <- mkAgentMessage agentVersion msg <- mkConfirmation aMessage sendConfirmation c sq msg - withStore c $ \st -> setSndQueueStatus st sq Confirmed + withStore' c $ \db -> setSndQueueStatus db sq Confirmed where mkConfirmation :: AgentMessage -> m MsgBody - mkConfirmation aMessage = withStore c $ \st -> do - void $ updateSndIds st connId - encConnInfo <- agentRatchetEncrypt st connId (smpEncode aMessage) e2eEncConnInfoLength + mkConfirmation aMessage = withStore c $ \db -> runExceptT $ do + void . liftIO $ updateSndIds db connId + encConnInfo <- agentRatchetEncrypt db connId (smpEncode aMessage) e2eEncConnInfoLength pure . smpEncode $ AgentConfirmation {agentVersion, e2eEncryption, encConnInfo} mkAgentMessage :: Version -> m AgentMessage mkAgentMessage 1 = pure $ AgentConnInfo connInfo @@ -1018,34 +1022,34 @@ enqueueConfirmation c cData@ConnData {connId, connAgentVersion} sq connInfo e2eE queuePendingMsgs c connId sq [msgId] where storeConfirmation :: m InternalId - storeConfirmation = withStore c $ \st -> do + storeConfirmation = withStore c $ \db -> runExceptT $ do internalTs <- liftIO getCurrentTime - (internalId, internalSndId, prevMsgHash) <- updateSndIds st connId + (internalId, internalSndId, prevMsgHash) <- liftIO $ updateSndIds db connId let agentMsg = AgentConnInfo connInfo agentMsgStr = smpEncode agentMsg internalHash = C.sha256Hash agentMsgStr - encConnInfo <- agentRatchetEncrypt st connId agentMsgStr e2eEncConnInfoLength + encConnInfo <- agentRatchetEncrypt db connId agentMsgStr e2eEncConnInfoLength let msgBody = smpEncode $ AgentConfirmation {agentVersion = connAgentVersion, e2eEncryption, encConnInfo} msgType = agentMessageType agentMsg msgData = SndMsgData {internalId, internalSndId, internalTs, msgType, msgBody, msgFlags = SMP.MsgFlags {notification = True}, internalHash, prevMsgHash} - createSndMsg st connId msgData + liftIO $ createSndMsg db connId msgData pure internalId -- encoded AgentMessage -> encoded EncAgentMessage -agentRatchetEncrypt :: AgentStoreMonad m => SQLiteStore -> ConnId -> ByteString -> Int -> m ByteString -agentRatchetEncrypt st connId msg paddedLen = do - rc <- getRatchet st connId - (encMsg, rc') <- liftError (SEAgentError . cryptoError) $ CR.rcEncrypt rc paddedLen msg - updateRatchet st connId rc' CR.SMDNoChange +agentRatchetEncrypt :: DB.Connection -> ConnId -> ByteString -> Int -> ExceptT StoreError IO ByteString +agentRatchetEncrypt db connId msg paddedLen = do + rc <- ExceptT $ getRatchet db connId + (encMsg, rc') <- liftE (SEAgentError . cryptoError) $ CR.rcEncrypt rc paddedLen msg + liftIO $ updateRatchet db connId rc' CR.SMDNoChange pure encMsg -- encoded EncAgentMessage -> encoded AgentMessage -agentRatchetDecrypt :: AgentStoreMonad m => SQLiteStore -> ConnId -> ByteString -> m ByteString -agentRatchetDecrypt st connId encAgentMsg = do - rc <- getRatchet st connId - skipped <- getSkippedMsgKeys st connId - (agentMsgBody_, rc', skippedDiff) <- liftError (SEAgentError . cryptoError) $ CR.rcDecrypt rc skipped encAgentMsg - updateRatchet st connId rc' skippedDiff +agentRatchetDecrypt :: DB.Connection -> ConnId -> ByteString -> ExceptT StoreError IO ByteString +agentRatchetDecrypt db connId encAgentMsg = do + rc <- ExceptT $ getRatchet db connId + skipped <- liftIO $ getSkippedMsgKeys db connId + (agentMsgBody_, rc', skippedDiff) <- liftE (SEAgentError . cryptoError) $ CR.rcDecrypt rc skipped encAgentMsg + liftIO $ updateRatchet db connId rc' skippedDiff liftEither $ first (SEAgentError . cryptoError) agentMsgBody_ newSndQueue :: (MonadUnliftIO m, MonadReader Env m) => Compatible SMPQueueInfo -> m SndQueue diff --git a/src/Simplex/Messaging/Agent/Client.hs b/src/Simplex/Messaging/Agent/Client.hs index ceae7fc56..6248cd321 100644 --- a/src/Simplex/Messaging/Agent/Client.hs +++ b/src/Simplex/Messaging/Agent/Client.hs @@ -50,6 +50,7 @@ module Simplex.Messaging.Agent.Client endAgentOperation, notifyAgentPhaseChanged, withStore, + withStore', ) where @@ -72,11 +73,12 @@ import Data.Set (Set) import Data.Text.Encoding import Data.Word (Word16) import Database.SQLite.Simple (SQLError) +import qualified Database.SQLite.Simple as DB import Simplex.Messaging.Agent.Env.SQLite import Simplex.Messaging.Agent.Protocol import Simplex.Messaging.Agent.RetryInterval import Simplex.Messaging.Agent.Store -import Simplex.Messaging.Agent.Store.SQLite (AgentStoreMonad, SQLiteStore (..)) +import Simplex.Messaging.Agent.Store.SQLite (SQLiteStore (..), withTransaction) import Simplex.Messaging.Client import Simplex.Messaging.Client.Agent () import qualified Simplex.Messaging.Crypto as C @@ -644,20 +646,19 @@ notifyAgentPhaseChanged AgentClient {subQ, agentEnv = Env {agentPhase, agentOper writeTBQueue subQ ("", "", PHASE p) writeTVar agentPhase (p, True) -withStore :: AgentMonad m => AgentClient -> (forall m'. AgentStoreMonad m' => SQLiteStore -> m' a) -> m a +withStore' :: AgentMonad m => AgentClient -> (DB.Connection -> IO a) -> m a +withStore' c action = withStore c $ fmap Right . action + +withStore :: AgentMonad m => AgentClient -> (DB.Connection -> IO (Either StoreError a)) -> m a withStore c action = do st <- asks store atomically $ beginAgentOperation c AODatabase - r <- runExceptT (action st `E.catch` handleInternal) + r <- liftIO $ withTransaction st action `E.catch` handleInternal atomically $ endAgentOperation c AODatabase - case r of - Right res -> pure res - Left e -> throwError $ storeError e + liftEither $ first storeError r where - -- TODO when parsing exception happens in store, the agent hangs; - -- changing SQLError to SomeException does not help - handleInternal :: (MonadError StoreError m') => SQLError -> m' a - handleInternal e = throwError . SEInternal $ bshow e + handleInternal :: SQLError -> IO (Either StoreError a) + handleInternal = pure . Left . SEInternal . bshow storeError :: StoreError -> AgentErrorType storeError = \case SEConnNotFound -> CONN NOT_FOUND diff --git a/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs b/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs index 84b51ec5b..db82204b3 100644 --- a/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs +++ b/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs @@ -18,7 +18,7 @@ where import Control.Concurrent.Async (Async, uninterruptibleCancel) import Control.Concurrent.STM (stateTVar) import Control.Monad -import Control.Monad.Except (runExceptT) +import Control.Monad.Except import Control.Monad.IO.Unlift (MonadUnliftIO) import Control.Monad.Reader import Data.Bifunctor (first) @@ -30,6 +30,7 @@ import Simplex.Messaging.Agent.Env.SQLite import Simplex.Messaging.Agent.Protocol (ConnId) import qualified Simplex.Messaging.Agent.Protocol as AP import Simplex.Messaging.Agent.Store +import Simplex.Messaging.Agent.Store.SQLite import Simplex.Messaging.Client.Agent () import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Notifications.Client @@ -58,18 +59,20 @@ processNtfSub c (connId, cmd) = do case cmd of NSCCreate -> do -- TODO merge getNtfSubscription and getRcvQueue into one method to read both in same transaction? - sub_ <- withStore c $ \st -> getNtfSubscription st connId - RcvQueue {notifierId, server = smpServer} <- withStore c $ \st -> getRcvQueue st connId + (sub_, RcvQueue {notifierId, server = smpServer}) <- withStore c $ \db -> runExceptT $ do + sub_ <- liftIO $ getNtfSubscription db connId + q <- ExceptT $ getRcvQueue db connId + pure (sub_, q) case (sub_, ntfServer_) of (Nothing, Just ntfServer) -> do currentTime <- liftIO getCurrentTime case notifierId of (Just nId) -> do let newSub = newNtfSubscription connId smpServer (Just nId) ntfServer NASKey currentTime - withStore c $ \st -> createNtfSubscription st newSub (NtfSubAction NSACreate) + withStore' c $ \db -> createNtfSubscription db newSub (NtfSubAction NSACreate) _ -> do let newSub = newNtfSubscription connId smpServer Nothing ntfServer NASNew currentTime - withStore c $ \st -> createNtfSubscription st newSub (NtfSubSMPAction NSAKey) + withStore' c $ \db -> createNtfSubscription db newSub (NtfSubSMPAction NSAKey) -- TODO optimize? -- TODO - read action in getNtfSubscription and decide which worker to create -- TODO - SMP worker can create Ntf worker on NKEY completion @@ -87,7 +90,7 @@ processNtfSub c (connId, cmd) = do _ -> pure () -- error - notification server not configured NSCDelete -> do -- TODO delete notifier ID and Key from SMP server (SDEL, then NDEL) - withStore c $ \st -> markNtfSubscriptionForDeletion st connId + withStore c (`markNtfSubscriptionForDeletion` connId) case ntfServer_ of (Just ntfServer) -> addNtfWorker ntfServer _ -> pure () @@ -116,7 +119,7 @@ runNtfWorker c srv doWork = forever $ do void . atomically $ readTMVar doWork getNtfToken >>= \case Just tkn@NtfToken {ntfTokenId = Just tknId, ntfTknStatus} -> do - nextSub_ <- withStore c (`getNextNtfSubAction` srv) + nextSub_ <- withStore' c (`getNextNtfSubAction` srv) ts <- liftIO getCurrentTime case nextSub_ of Nothing -> noWorkToDo @@ -128,8 +131,8 @@ runNtfWorker c srv doWork = forever $ do | ntfTknStatus == NTActive -> do nSubId <- agentNtfCreateSubscription c tknId tkn (SMPQueueNtf smpServer nId) ntfPrivKey let actionTs = addUTCTime 30 ts - withStore c $ \st -> - updateNtfSubscription st connId ntfSub {ntfSubId = Just nSubId, ntfSubStatus = NASCreated NSNew, ntfSubActionTs = actionTs} (NtfSubAction NSACheck) + withStore' c $ \db -> + updateNtfSubscription db connId ntfSub {ntfSubId = Just nSubId, ntfSubStatus = NASCreated NSNew, ntfSubActionTs = actionTs} (NtfSubAction NSACheck) | otherwise -> ntfInternalError c connId "NSACreate - token not active" _ -> ntfInternalError c connId "NSACreate - no notifier key or ID" NSACheck -> case ntfSubId of @@ -148,8 +151,8 @@ runNtfWorker c srv doWork = forever $ do let nextCheckTs = addUTCTime checkInterval ts updateSub (NASCreated toStatus) (NtfSubAction NSACheck) nextCheckTs updateSub toStatus toAction actionTs = - withStore c $ \st -> - updateNtfSubscription st connId ntfSub {ntfSubStatus = toStatus, ntfSubActionTs = actionTs} toAction + withStore' c $ \db -> + updateNtfSubscription db connId ntfSub {ntfSubStatus = toStatus, ntfSubActionTs = actionTs} toAction _ -> noWorkToDo delay <- asks $ ntfWorkerThrottle . config liftIO $ threadDelay delay @@ -161,7 +164,7 @@ runNtfSMPWorker c srv doWork = forever $ do void . atomically $ readTMVar doWork getNtfToken >>= \case Just NtfToken {ntfTknStatus} -> do - nextSub_ <- withStore c (`getNextNtfSubSMPAction` srv) + nextSub_ <- withStore' c (`getNextNtfSubSMPAction` srv) ts <- liftIO getCurrentTime case nextSub_ of Nothing -> noWorkToDo @@ -176,15 +179,15 @@ runNtfSMPWorker c srv doWork = forever $ do _ -> do C.SignAlg a <- asks (cmdSignAlg . config) (ntfPubKey, ntfPrivKey) <- liftIO $ C.generateSignatureKeyPair a - withStore c $ \st -> setRcvQueueNotifierKey st connId ntfPubKey ntfPrivKey + withStore' c $ \db -> setRcvQueueNotifierKey db connId ntfPubKey ntfPrivKey enableNotificationsWithNKey ntfPubKey | otherwise -> ntfInternalError c connId "NSAKey - token not active" where enableNotificationsWithNKey ntfPubKey = do nId <- enableQueueNotifications c rq ntfPubKey - withStore c $ \st -> do - setRcvQueueNotifierId st connId nId - updateNtfSubscription st connId ntfSub {ntfQueueId = Just nId, ntfSubStatus = NASKey, ntfSubActionTs = ts} (NtfSubAction NSACreate) + withStore' c $ \db -> do + setRcvQueueNotifierId db connId nId + updateNtfSubscription db connId ntfSub {ntfQueueId = Just nId, ntfSubStatus = NASKey, ntfSubActionTs = ts} (NtfSubAction NSACreate) ns <- asks ntfSupervisor atomically $ sendNtfSubCommand ns (connId, NSCNtfWorker ntfServer) _ -> noWorkToDo @@ -211,7 +214,7 @@ diffInMicros a b = (`div` 1000000) . fromInteger . fromPico . nominalDiffTimeToS ntfInternalError :: AgentMonad m => AgentClient -> ConnId -> String -> m () ntfInternalError c@AgentClient {subQ} connId internalErrStr = do - withStore c $ \st -> setNullNtfSubscriptionAction st connId + withStore' c $ \db -> setNullNtfSubscriptionAction db connId atomically $ writeTBQueue subQ ("", connId, AP.ERR $ AP.INTERNAL internalErrStr) getNtfToken :: AgentMonad m => m (Maybe NtfToken) diff --git a/src/Simplex/Messaging/Agent/Store.hs b/src/Simplex/Messaging/Agent/Store.hs index e26dbb925..45110705e 100644 --- a/src/Simplex/Messaging/Agent/Store.hs +++ b/src/Simplex/Messaging/Agent/Store.hs @@ -9,9 +9,7 @@ module Simplex.Messaging.Agent.Store where -import Control.Concurrent.STM (TVar) import Control.Exception (Exception) -import Crypto.Random (ChaChaDRG) import Data.ByteString.Char8 (ByteString) import Data.Int (Int64) import Data.Kind (Type) @@ -19,9 +17,7 @@ import Data.Time (UTCTime) import Data.Type.Equality import Simplex.Messaging.Agent.Protocol import qualified Simplex.Messaging.Crypto as C -import Simplex.Messaging.Crypto.Ratchet (RatchetX448, SkippedMsgDiff, SkippedMsgKeys) -import Simplex.Messaging.Notifications.Client -import Simplex.Messaging.Notifications.Protocol (DeviceToken, NtfTknStatus, NtfTokenId, SMPQueueNtf) +import Simplex.Messaging.Crypto.Ratchet (RatchetX448) import Simplex.Messaging.Protocol ( MsgBody, MsgFlags, @@ -36,80 +32,6 @@ import Simplex.Messaging.Protocol import qualified Simplex.Messaging.Protocol as SMP import Simplex.Messaging.Version --- * Store management - --- | Store class type. Defines store access methods for implementations. -class Monad m => MonadAgentStore s m where - -- Queue and Connection management - createRcvConn :: s -> TVar ChaChaDRG -> ConnData -> RcvQueue -> SConnectionMode c -> m ConnId - createSndConn :: s -> TVar ChaChaDRG -> ConnData -> SndQueue -> m ConnId - getConn :: s -> ConnId -> m SomeConn - getRcvConn :: s -> SMPServer -> SMP.RecipientId -> m SomeConn - deleteConn :: s -> ConnId -> m () - upgradeRcvConnToDuplex :: s -> ConnId -> SndQueue -> m () - upgradeSndConnToDuplex :: s -> ConnId -> RcvQueue -> m () - setRcvQueueStatus :: s -> RcvQueue -> QueueStatus -> m () - setRcvQueueConfirmedE2E :: s -> RcvQueue -> C.DhSecretX25519 -> m () - setSndQueueStatus :: s -> SndQueue -> QueueStatus -> m () - getRcvQueue :: s -> ConnId -> m RcvQueue - - -- RcvQueue notifier key and ID - setRcvQueueNotifierKey :: s -> ConnId -> NtfPublicVerifyKey -> NtfPrivateSignKey -> m () - setRcvQueueNotifierId :: s -> ConnId -> NotifierId -> m () - - -- Confirmations - createConfirmation :: s -> TVar ChaChaDRG -> NewConfirmation -> m ConfirmationId - acceptConfirmation :: s -> ConfirmationId -> ConnInfo -> m AcceptedConfirmation - getAcceptedConfirmation :: s -> ConnId -> m AcceptedConfirmation - removeConfirmations :: s -> ConnId -> m () - setHandshakeVersion :: s -> ConnId -> Version -> Bool -> m () - - -- Invitations - sent via Contact connections - createInvitation :: s -> TVar ChaChaDRG -> NewInvitation -> m InvitationId - getInvitation :: s -> InvitationId -> m Invitation - acceptInvitation :: s -> InvitationId -> ConnInfo -> m () - deleteInvitation :: s -> ConnId -> InvitationId -> m () - - -- Msg management - updateRcvIds :: s -> ConnId -> m (InternalId, InternalRcvId, PrevExternalSndId, PrevRcvMsgHash) - createRcvMsg :: s -> ConnId -> RcvMsgData -> m () - updateSndIds :: s -> ConnId -> m (InternalId, InternalSndId, PrevSndMsgHash) - createSndMsg :: s -> ConnId -> SndMsgData -> m () - getPendingMsgData :: s -> ConnId -> InternalId -> m (Maybe RcvQueue, PendingMsgData) - getPendingMsgs :: s -> ConnId -> m [InternalId] - setMsgUserAck :: s -> ConnId -> InternalId -> m MsgId - getLastMsg :: s -> ConnId -> SMP.MsgId -> m (Maybe RcvMsg) - deleteMsg :: s -> ConnId -> InternalId -> m () - - -- Double ratchet persistence - createRatchetX3dhKeys :: s -> ConnId -> C.PrivateKeyX448 -> C.PrivateKeyX448 -> m () - getRatchetX3dhKeys :: s -> ConnId -> m (C.PrivateKeyX448, C.PrivateKeyX448) - createRatchet :: s -> ConnId -> RatchetX448 -> m () - getRatchet :: s -> ConnId -> m RatchetX448 - getSkippedMsgKeys :: s -> ConnId -> m SkippedMsgKeys - updateRatchet :: s -> ConnId -> RatchetX448 -> SkippedMsgDiff -> m () - - -- Notification device token persistence - createNtfToken :: s -> NtfToken -> m () - getDeviceNtfToken :: s -> DeviceToken -> m (Maybe NtfToken, [NtfToken]) - updateNtfTokenRegistration :: s -> NtfToken -> NtfTokenId -> C.DhSecretX25519 -> m () - updateNtfToken :: s -> NtfToken -> NtfTknStatus -> Maybe NtfTknAction -> m () - removeNtfToken :: s -> NtfToken -> m () - - -- Notification subscription persistence - getNtfSubscription :: s -> ConnId -> m (Maybe NtfSubscription) - createNtfSubscription :: s -> NtfSubscription -> NtfSubOrSMPAction -> m () - markNtfSubscriptionForDeletion :: s -> ConnId -> m () - updateNtfSubscription :: s -> ConnId -> NtfSubscription -> NtfSubOrSMPAction -> m () - setNullNtfSubscriptionAction :: s -> ConnId -> m () - deleteNtfSubscription :: s -> ConnId -> m () - getNextNtfSubAction :: s -> NtfServer -> m (Maybe (NtfSubscription, NtfSubAction, RcvQueue)) - getNextNtfSubSMPAction :: s -> SMPServer -> m (Maybe (NtfSubscription, NtfSubSMPAction, RcvQueue)) - - -- Notification decryption - getActiveNtfToken :: s -> m (Maybe NtfToken) - getNtfRcvQueue :: s -> SMPQueueNtf -> m (ConnId, RcvDhSecret) - -- * Queue types -- | A receive queue. SMP queue through which the agent receives messages from a sender. diff --git a/src/Simplex/Messaging/Agent/Store/SQLite.hs b/src/Simplex/Messaging/Agent/Store/SQLite.hs index 7cb48d350..56aa2ccf0 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite.hs @@ -20,20 +20,82 @@ module Simplex.Messaging.Agent.Store.SQLite ( SQLiteStore (..), - AgentStoreMonad, createSQLiteStore, connectSQLiteStore, + + -- * Queues and connections + createRcvConn, + createSndConn, + getConn, + getRcvConn, + deleteConn, + upgradeRcvConnToDuplex, + upgradeSndConnToDuplex, + setRcvQueueStatus, + setRcvQueueConfirmedE2E, + setSndQueueStatus, + getRcvQueue, + -- RcvQueue notifier key and ID + setRcvQueueNotifierKey, + setRcvQueueNotifierId, + -- Confirmations + createConfirmation, + acceptConfirmation, + getAcceptedConfirmation, + removeConfirmations, + setHandshakeVersion, + -- Invitations - sent via Contact connections + createInvitation, + getInvitation, + acceptInvitation, + deleteInvitation, + -- Messages + updateRcvIds, + createRcvMsg, + updateSndIds, + createSndMsg, + getPendingMsgData, + getPendingMsgs, + setMsgUserAck, + getLastMsg, + deleteMsg, + -- Double ratchet persistence + createRatchetX3dhKeys, + getRatchetX3dhKeys, + createRatchet, + getRatchet, + getSkippedMsgKeys, + updateRatchet, + -- Notification device token persistence + createNtfToken, + getDeviceNtfToken, + updateNtfTokenRegistration, + updateNtfToken, + removeNtfToken, + -- Notification subscription persistence + getNtfSubscription, + createNtfSubscription, + markNtfSubscriptionForDeletion, + updateNtfSubscription, + setNullNtfSubscriptionAction, + deleteNtfSubscription, + getNextNtfSubAction, + getNextNtfSubSMPAction, + getActiveNtfToken, + getNtfRcvQueue, + + -- * utilities withConnection, withTransaction, firstRow, + firstRow', + maybeFirstRow, ) where import Control.Concurrent (threadDelay) -import Control.Concurrent.STM -import Control.Exception (bracket) +import Control.Concurrent.STM (stateTVar) import Control.Monad.Except -import Control.Monad.IO.Unlift (MonadUnliftIO) import Crypto.Random (ChaChaDRG, randomBytesGenerate) import Data.Bifunctor (first, second) import Data.ByteString (ByteString) @@ -47,11 +109,12 @@ import Data.Text (Text) import qualified Data.Text as T import Data.Text.Encoding (decodeLatin1, encodeUtf8) import Data.Time.Clock (UTCTime, getCurrentTime) -import Database.SQLite.Simple (FromRow, NamedParam (..), Only (..), SQLError, ToRow, field, (:.) (..)) +import Database.SQLite.Simple (FromRow, NamedParam (..), Only (..), Query (..), SQLError, ToRow, field, (:.) (..)) import qualified Database.SQLite.Simple as DB import Database.SQLite.Simple.FromField import Database.SQLite.Simple.QQ (sql) import Database.SQLite.Simple.ToField (ToField (..)) +import qualified Database.SQLite3 as SQLite3 import Simplex.Messaging.Agent.Protocol import Simplex.Messaging.Agent.Store import Simplex.Messaging.Agent.Store.SQLite.Migrations (Migration) @@ -65,13 +128,15 @@ import Simplex.Messaging.Notifications.Protocol (DeviceToken (..), NtfTknStatus import Simplex.Messaging.Parsers (blobFieldParser, fromTextField_) import Simplex.Messaging.Protocol (MsgBody, MsgFlags, NotifierId, NtfPrivateSignKey, NtfPublicVerifyKey, ProtocolServer (..), RcvDhSecret) import qualified Simplex.Messaging.Protocol as SMP -import Simplex.Messaging.Util (bshow, eitherToMaybe, liftIOEither) +import Simplex.Messaging.Util (bshow, eitherToMaybe, ($>>=), (<$$>)) import Simplex.Messaging.Version import System.Directory (copyFile, createDirectoryIfMissing, doesFileExist) import System.Exit (exitFailure) import System.FilePath (takeDirectory) import System.IO (hFlush, stdout) +import UnliftIO.Exception (bracket) import qualified UnliftIO.Exception as E +import UnliftIO.STM -- * SQLite Store implementation @@ -130,24 +195,27 @@ connectSQLiteStore dbFilePath = do connectDB :: FilePath -> IO DB.Connection connectDB path = do dbConn <- DB.open path - DB.execute_ dbConn "PRAGMA foreign_keys = ON;" - -- DB.execute_ dbConn "PRAGMA trusted_schema = OFF;" - DB.execute_ dbConn "PRAGMA secure_delete = ON;" - DB.execute_ dbConn "PRAGMA auto_vacuum = FULL;" + SQLite3.exec (DB.connectionHandle dbConn) . fromQuery $ + [sql| + PRAGMA foreign_keys = ON; + -- PRAGMA trusted_schema = OFF; + PRAGMA secure_delete = ON; + PRAGMA auto_vacuum = FULL; + |] -- _printPragmas dbConn path pure dbConn -_printPragmas :: DB.Connection -> FilePath -> IO () -_printPragmas db path = do - foreign_keys <- DB.query_ db "PRAGMA foreign_keys;" :: IO [[Int]] - print $ path <> " foreign_keys: " <> show foreign_keys - -- when run via sqlite-simple query for trusted_schema seems to return empty list - trusted_schema <- DB.query_ db "PRAGMA trusted_schema;" :: IO [[Int]] - print $ path <> " trusted_schema: " <> show trusted_schema - secure_delete <- DB.query_ db "PRAGMA secure_delete;" :: IO [[Int]] - print $ path <> " secure_delete: " <> show secure_delete - auto_vacuum <- DB.query_ db "PRAGMA auto_vacuum;" :: IO [[Int]] - print $ path <> " auto_vacuum: " <> show auto_vacuum +-- _printPragmas :: DB.Connection -> FilePath -> IO () +-- _printPragmas db path = do +-- foreign_keys <- DB.query_ db "PRAGMA foreign_keys;" :: IO [[Int]] +-- print $ path <> " foreign_keys: " <> show foreign_keys +-- -- when run via sqlite-simple query for trusted_schema seems to return empty list +-- trusted_schema <- DB.query_ db "PRAGMA trusted_schema;" :: IO [[Int]] +-- print $ path <> " trusted_schema: " <> show trusted_schema +-- secure_delete <- DB.query_ db "PRAGMA secure_delete;" :: IO [[Int]] +-- print $ path <> " secure_delete: " <> show secure_delete +-- auto_vacuum <- DB.query_ db "PRAGMA auto_vacuum;" :: IO [[Int]] +-- print $ path <> " auto_vacuum: " <> show auto_vacuum checkConstraint :: StoreError -> IO (Either StoreError a) -> IO (Either StoreError a) checkConstraint err action = action `E.catch` (pure . Left . handleSQLError err) @@ -164,7 +232,7 @@ withConnection SQLiteStore {dbConnection} = (atomically . putTMVar dbConnection) withTransaction :: forall a. SQLiteStore -> (DB.Connection -> IO a) -> IO a -withTransaction st action = withConnection st $ loop 100 100_000 +withTransaction st action = withConnection st $ loop 500 2_000_000 where loop :: Int -> Int -> DB.Connection -> IO a loop t tLim db = @@ -176,694 +244,621 @@ withTransaction st action = withConnection st $ loop 100 100_000 else E.throwIO e createConn_ :: - (MonadUnliftIO m, MonadError StoreError m) => - SQLiteStore -> TVar ChaChaDRG -> ConnData -> - (DB.Connection -> ByteString -> IO ()) -> - m ByteString -createConn_ st gVar cData create = - liftIOEither . checkConstraint SEConnDuplicate . withTransaction st $ \db -> - case cData of - ConnData {connId = ""} -> createWithRandomId gVar $ create db - ConnData {connId} -> create db connId $> Right connId + (ByteString -> IO ()) -> + IO (Either StoreError ByteString) +createConn_ gVar cData create = checkConstraint SEConnDuplicate $ case cData of + ConnData {connId = ""} -> createWithRandomId gVar create + ConnData {connId} -> create connId $> Right connId -type AgentStoreMonad m = (MonadUnliftIO m, MonadError StoreError m, MonadAgentStore SQLiteStore m) +createRcvConn :: DB.Connection -> TVar ChaChaDRG -> ConnData -> RcvQueue -> SConnectionMode c -> IO (Either StoreError ConnId) +createRcvConn db gVar cData q@RcvQueue {server} cMode = + createConn_ gVar cData $ \connId -> do + upsertServer_ db server + DB.execute db "INSERT INTO connections (conn_id, conn_mode, smp_agent_version, duplex_handshake) VALUES (?, ?, ?, ?)" (connId, cMode, connAgentVersion cData, duplexHandshake cData) + insertRcvQueue_ db connId q -instance (MonadUnliftIO m, MonadError StoreError m) => MonadAgentStore SQLiteStore m where - createRcvConn :: SQLiteStore -> TVar ChaChaDRG -> ConnData -> RcvQueue -> SConnectionMode c -> m ConnId - createRcvConn st gVar cData q@RcvQueue {server} cMode = - createConn_ st gVar cData $ \db connId -> do +createSndConn :: DB.Connection -> TVar ChaChaDRG -> ConnData -> SndQueue -> IO (Either StoreError ConnId) +createSndConn db gVar cData q@SndQueue {server} = + createConn_ gVar cData $ \connId -> do + upsertServer_ db server + DB.execute db "INSERT INTO connections (conn_id, conn_mode, smp_agent_version, duplex_handshake) VALUES (?, ?, ?, ?)" (connId, SCMInvitation, connAgentVersion cData, duplexHandshake cData) + insertSndQueue_ db connId q + +getRcvConn :: DB.Connection -> SMPServer -> SMP.RecipientId -> IO (Either StoreError SomeConn) +getRcvConn db ProtocolServer {host, port} rcvId = + DB.queryNamed + db + [sql| + SELECT q.conn_id + FROM rcv_queues q + WHERE q.host = :host AND q.port = :port AND q.rcv_id = :rcv_id; + |] + [":host" := host, ":port" := port, ":rcv_id" := rcvId] + >>= \case + [Only connId] -> getConn db connId + _ -> pure $ Left SEConnNotFound + +deleteConn :: DB.Connection -> ConnId -> IO () +deleteConn db connId = + DB.executeNamed + db + "DELETE FROM connections WHERE conn_id = :conn_id;" + [":conn_id" := connId] + +upgradeRcvConnToDuplex :: DB.Connection -> ConnId -> SndQueue -> IO (Either StoreError ()) +upgradeRcvConnToDuplex db connId sq@SndQueue {server} = + getConn db connId $>>= \case + (SomeConn _ RcvConnection {}) -> do upsertServer_ db server - DB.execute db "INSERT INTO connections (conn_id, conn_mode, smp_agent_version, duplex_handshake) VALUES (?, ?, ?, ?)" (connId, cMode, connAgentVersion cData, duplexHandshake cData) - insertRcvQueue_ db connId q + insertSndQueue_ db connId sq + pure $ Right () + (SomeConn c _) -> pure . Left . SEBadConnType $ connType c - createSndConn :: SQLiteStore -> TVar ChaChaDRG -> ConnData -> SndQueue -> m ConnId - createSndConn st gVar cData q@SndQueue {server} = - createConn_ st gVar cData $ \db connId -> do +upgradeSndConnToDuplex :: DB.Connection -> ConnId -> RcvQueue -> IO (Either StoreError ()) +upgradeSndConnToDuplex db connId rq@RcvQueue {server} = + getConn db connId >>= \case + Right (SomeConn _ SndConnection {}) -> do upsertServer_ db server - DB.execute db "INSERT INTO connections (conn_id, conn_mode, smp_agent_version, duplex_handshake) VALUES (?, ?, ?, ?)" (connId, SCMInvitation, connAgentVersion cData, duplexHandshake cData) - insertSndQueue_ db connId q + insertRcvQueue_ db connId rq + pure $ Right () + Right (SomeConn c _) -> pure . Left . SEBadConnType $ connType c + _ -> pure $ Left SEConnNotFound - getConn :: SQLiteStore -> ConnId -> m SomeConn - getConn st connId = - liftIOEither . withTransaction st $ \db -> - getConn_ db connId +setRcvQueueStatus :: DB.Connection -> RcvQueue -> QueueStatus -> IO () +setRcvQueueStatus db RcvQueue {rcvId, server = ProtocolServer {host, port}} status = + -- ? return error if queue does not exist? + DB.executeNamed + db + [sql| + UPDATE rcv_queues + SET status = :status + WHERE host = :host AND port = :port AND rcv_id = :rcv_id; + |] + [":status" := status, ":host" := host, ":port" := port, ":rcv_id" := rcvId] - getRcvConn :: SQLiteStore -> SMPServer -> SMP.RecipientId -> m SomeConn - getRcvConn st ProtocolServer {host, port} rcvId = - liftIOEither . withTransaction st $ \db -> - DB.queryNamed +setRcvQueueConfirmedE2E :: DB.Connection -> RcvQueue -> C.DhSecretX25519 -> IO () +setRcvQueueConfirmedE2E db RcvQueue {rcvId, server = ProtocolServer {host, port}} e2eDhSecret = + DB.executeNamed + db + [sql| + UPDATE rcv_queues + SET e2e_dh_secret = :e2e_dh_secret, + status = :status + WHERE host = :host AND port = :port AND rcv_id = :rcv_id + |] + [ ":status" := Confirmed, + ":e2e_dh_secret" := e2eDhSecret, + ":host" := host, + ":port" := port, + ":rcv_id" := rcvId + ] + +setSndQueueStatus :: DB.Connection -> SndQueue -> QueueStatus -> IO () +setSndQueueStatus db SndQueue {sndId, server = ProtocolServer {host, port}} status = + -- ? return error if queue does not exist? + DB.executeNamed + db + [sql| + UPDATE snd_queues + SET status = :status + WHERE host = :host AND port = :port AND snd_id = :snd_id; + |] + [":status" := status, ":host" := host, ":port" := port, ":snd_id" := sndId] + +getRcvQueue :: DB.Connection -> ConnId -> IO (Either StoreError RcvQueue) +getRcvQueue db connId = + maybe (Left SEConnNotFound) Right <$> getRcvQueueByConnId_ db connId + +setRcvQueueNotifierKey :: DB.Connection -> ConnId -> NtfPublicVerifyKey -> NtfPrivateSignKey -> IO () +setRcvQueueNotifierKey db connId ntfPublicKey ntfPrivateKey = + DB.execute + db + [sql| + UPDATE rcv_queues + SET ntf_public_key = ?, ntf_private_key = ? + WHERE conn_id = ? + |] + (ntfPublicKey, ntfPrivateKey, connId) + +setRcvQueueNotifierId :: DB.Connection -> ConnId -> NotifierId -> IO () +setRcvQueueNotifierId db connId nId = + DB.execute + db + [sql| + UPDATE rcv_queues + SET ntf_id = ? + WHERE conn_id = ? + |] + (nId, connId) + +createConfirmation :: DB.Connection -> TVar ChaChaDRG -> NewConfirmation -> IO (Either StoreError ConfirmationId) +createConfirmation db gVar NewConfirmation {connId, senderConf = SMPConfirmation {senderKey, e2ePubKey, connInfo, smpReplyQueues}, ratchetState} = + createWithRandomId gVar $ \confirmationId -> + DB.execute + db + [sql| + INSERT INTO conn_confirmations + (confirmation_id, conn_id, sender_key, e2e_snd_pub_key, ratchet_state, sender_conn_info, smp_reply_queues, accepted) VALUES (?, ?, ?, ?, ?, ?, ?, 0); + |] + (confirmationId, connId, senderKey, e2ePubKey, ratchetState, connInfo, smpReplyQueues) + +acceptConfirmation :: DB.Connection -> ConfirmationId -> ConnInfo -> IO (Either StoreError AcceptedConfirmation) +acceptConfirmation db confirmationId ownConnInfo = do + DB.executeNamed + db + [sql| + UPDATE conn_confirmations + SET accepted = 1, + own_conn_info = :own_conn_info + WHERE confirmation_id = :confirmation_id; + |] + [ ":own_conn_info" := ownConnInfo, + ":confirmation_id" := confirmationId + ] + firstRow confirmation SEConfirmationNotFound $ + DB.query + db + [sql| + SELECT conn_id, sender_key, e2e_snd_pub_key, ratchet_state, sender_conn_info, smp_reply_queues + FROM conn_confirmations + WHERE confirmation_id = ?; + |] + (Only confirmationId) + where + confirmation (connId, senderKey, e2ePubKey, ratchetState, connInfo, smpReplyQueues_) = + AcceptedConfirmation + { confirmationId, + connId, + senderConf = SMPConfirmation {senderKey, e2ePubKey, connInfo, smpReplyQueues = fromMaybe [] smpReplyQueues_}, + ratchetState, + ownConnInfo + } + +getAcceptedConfirmation :: DB.Connection -> ConnId -> IO (Either StoreError AcceptedConfirmation) +getAcceptedConfirmation db connId = + firstRow confirmation SEConfirmationNotFound $ + DB.query + db + [sql| + SELECT confirmation_id, sender_key, e2e_snd_pub_key, ratchet_state, sender_conn_info, smp_reply_queues, own_conn_info + FROM conn_confirmations + WHERE conn_id = ? AND accepted = 1; + |] + (Only connId) + where + confirmation (confirmationId, senderKey, e2ePubKey, ratchetState, connInfo, smpReplyQueues_, ownConnInfo) = + AcceptedConfirmation + { confirmationId, + connId, + senderConf = SMPConfirmation {senderKey, e2ePubKey, connInfo, smpReplyQueues = fromMaybe [] smpReplyQueues_}, + ratchetState, + ownConnInfo + } + +removeConfirmations :: DB.Connection -> ConnId -> IO () +removeConfirmations db connId = + DB.executeNamed + db + [sql| + DELETE FROM conn_confirmations + WHERE conn_id = :conn_id; + |] + [":conn_id" := connId] + +setHandshakeVersion :: DB.Connection -> ConnId -> Version -> Bool -> IO () +setHandshakeVersion db connId aVersion duplexHS = + DB.execute db "UPDATE connections SET smp_agent_version = ?, duplex_handshake = ? WHERE conn_id = ?" (aVersion, duplexHS, connId) + +createInvitation :: DB.Connection -> TVar ChaChaDRG -> NewInvitation -> IO (Either StoreError InvitationId) +createInvitation db gVar NewInvitation {contactConnId, connReq, recipientConnInfo} = + createWithRandomId gVar $ \invitationId -> + DB.execute + db + [sql| + INSERT INTO conn_invitations + (invitation_id, contact_conn_id, cr_invitation, recipient_conn_info, accepted) VALUES (?, ?, ?, ?, 0); + |] + (invitationId, contactConnId, connReq, recipientConnInfo) + +getInvitation :: DB.Connection -> InvitationId -> IO (Either StoreError Invitation) +getInvitation db invitationId = + firstRow invitation SEInvitationNotFound $ + DB.query + db + [sql| + SELECT contact_conn_id, cr_invitation, recipient_conn_info, own_conn_info, accepted + FROM conn_invitations + WHERE invitation_id = ? + AND accepted = 0 + |] + (Only invitationId) + where + invitation (contactConnId, connReq, recipientConnInfo, ownConnInfo, accepted) = + Invitation {invitationId, contactConnId, connReq, recipientConnInfo, ownConnInfo, accepted} + +acceptInvitation :: DB.Connection -> InvitationId -> ConnInfo -> IO () +acceptInvitation db invitationId ownConnInfo = + DB.executeNamed + db + [sql| + UPDATE conn_invitations + SET accepted = 1, + own_conn_info = :own_conn_info + WHERE invitation_id = :invitation_id + |] + [ ":own_conn_info" := ownConnInfo, + ":invitation_id" := invitationId + ] + +deleteInvitation :: DB.Connection -> ConnId -> InvitationId -> IO (Either StoreError ()) +deleteInvitation db contactConnId invId = + getConn db contactConnId $>>= \case + SomeConn SCContact _ -> + Right <$> DB.execute db "DELETE FROM conn_invitations WHERE contact_conn_id = ? AND invitation_id = ?" (contactConnId, invId) + _ -> pure $ Left SEConnNotFound + +updateRcvIds :: DB.Connection -> ConnId -> IO (InternalId, InternalRcvId, PrevExternalSndId, PrevRcvMsgHash) +updateRcvIds db connId = do + (lastInternalId, lastInternalRcvId, lastExternalSndId, lastRcvHash) <- retrieveLastIdsAndHashRcv_ db connId + let internalId = InternalId $ unId lastInternalId + 1 + internalRcvId = InternalRcvId $ unRcvId lastInternalRcvId + 1 + updateLastIdsRcv_ db connId internalId internalRcvId + pure (internalId, internalRcvId, lastExternalSndId, lastRcvHash) + +createRcvMsg :: DB.Connection -> ConnId -> RcvMsgData -> IO () +createRcvMsg db connId rcvMsgData = do + insertRcvMsgBase_ db connId rcvMsgData + insertRcvMsgDetails_ db connId rcvMsgData + updateHashRcv_ db connId rcvMsgData + +updateSndIds :: DB.Connection -> ConnId -> IO (InternalId, InternalSndId, PrevSndMsgHash) +updateSndIds db connId = do + (lastInternalId, lastInternalSndId, prevSndHash) <- retrieveLastIdsAndHashSnd_ db connId + let internalId = InternalId $ unId lastInternalId + 1 + internalSndId = InternalSndId $ unSndId lastInternalSndId + 1 + updateLastIdsSnd_ db connId internalId internalSndId + pure (internalId, internalSndId, prevSndHash) + +createSndMsg :: DB.Connection -> ConnId -> SndMsgData -> IO () +createSndMsg db connId sndMsgData = do + insertSndMsgBase_ db connId sndMsgData + insertSndMsgDetails_ db connId sndMsgData + updateHashSnd_ db connId sndMsgData + +getPendingMsgData :: DB.Connection -> ConnId -> InternalId -> IO (Either StoreError (Maybe RcvQueue, PendingMsgData)) +getPendingMsgData db connId msgId = do + rq_ <- getRcvQueueByConnId_ db connId + (rq_,) <$$> firstRow pendingMsgData SEMsgNotFound getMsgData_ + where + getMsgData_ = + DB.query db [sql| - SELECT q.conn_id - FROM rcv_queues q - WHERE q.host = :host AND q.port = :port AND q.rcv_id = :rcv_id; + SELECT m.msg_type, m.msg_flags, m.msg_body, m.internal_ts + FROM messages m + JOIN snd_messages s ON s.conn_id = m.conn_id AND s.internal_id = m.internal_id + WHERE m.conn_id = ? AND m.internal_id = ? |] - [":host" := host, ":port" := port, ":rcv_id" := rcvId] - >>= \case - [Only connId] -> getConn_ db connId - _ -> pure $ Left SEConnNotFound + (connId, msgId) + pendingMsgData :: (AgentMessageType, MsgFlags, MsgBody, InternalTs) -> PendingMsgData + pendingMsgData (msgType, msgFlags, msgBody, internalTs) = PendingMsgData {msgId, msgType, msgFlags, msgBody, internalTs} - deleteConn :: SQLiteStore -> ConnId -> m () - deleteConn st connId = - liftIO . withTransaction st $ \db -> - DB.executeNamed - db - "DELETE FROM connections WHERE conn_id = :conn_id;" - [":conn_id" := connId] +getPendingMsgs :: DB.Connection -> ConnId -> IO [InternalId] +getPendingMsgs db connId = + map fromOnly + <$> DB.query db "SELECT internal_id FROM snd_messages WHERE conn_id = ?" (Only connId) - upgradeRcvConnToDuplex :: SQLiteStore -> ConnId -> SndQueue -> m () - upgradeRcvConnToDuplex st connId sq@SndQueue {server} = - liftIOEither . withTransaction st $ \db -> - getConn_ db connId >>= \case - Right (SomeConn _ RcvConnection {}) -> do - upsertServer_ db server - insertSndQueue_ db connId sq - pure $ Right () - Right (SomeConn c _) -> pure . Left . SEBadConnType $ connType c - _ -> pure $ Left SEConnNotFound +setMsgUserAck :: DB.Connection -> ConnId -> InternalId -> IO (Either StoreError SMP.MsgId) +setMsgUserAck db connId agentMsgId = do + DB.execute db "UPDATE rcv_messages SET user_ack = ? WHERE conn_id = ? AND internal_id = ?" (True, connId, agentMsgId) + firstRow fromOnly SEMsgNotFound $ + DB.query db "SELECT broker_id FROM rcv_messages WHERE conn_id = ? AND internal_id = ?" (connId, agentMsgId) - upgradeSndConnToDuplex :: SQLiteStore -> ConnId -> RcvQueue -> m () - upgradeSndConnToDuplex st connId rq@RcvQueue {server} = - liftIOEither . withTransaction st $ \db -> - getConn_ db connId >>= \case - Right (SomeConn _ SndConnection {}) -> do - upsertServer_ db server - insertRcvQueue_ db connId rq - pure $ Right () - Right (SomeConn c _) -> pure . Left . SEBadConnType $ connType c - _ -> pure $ Left SEConnNotFound +getLastMsg :: DB.Connection -> ConnId -> SMP.MsgId -> IO (Maybe RcvMsg) +getLastMsg db connId msgId = + maybeFirstRow rcvMsg $ + DB.query + db + [sql| + SELECT + r.internal_id, m.internal_ts, r.broker_id, r.broker_ts, r.external_snd_id, r.integrity, + m.msg_body, r.user_ack + FROM rcv_messages r + JOIN messages m ON r.internal_id = m.internal_id + JOIN connections c ON r.conn_id = c.conn_id AND c.last_internal_msg_id = r.internal_id + WHERE r.conn_id = ? AND r.broker_id = ? + |] + (connId, msgId) + where + rcvMsg (agentMsgId, internalTs, brokerId, brokerTs, sndMsgId, integrity, msgBody, userAck) = + let msgMeta = MsgMeta {recipient = (agentMsgId, internalTs), broker = (brokerId, brokerTs), sndMsgId, integrity} + in RcvMsg {internalId = InternalId agentMsgId, msgMeta, msgBody, userAck} - setRcvQueueStatus :: SQLiteStore -> RcvQueue -> QueueStatus -> m () - setRcvQueueStatus st RcvQueue {rcvId, server = ProtocolServer {host, port}} status = - -- ? throw error if queue does not exist? - liftIO . withTransaction st $ \db -> - DB.executeNamed +deleteMsg :: DB.Connection -> ConnId -> InternalId -> IO () +deleteMsg db connId msgId = + DB.execute db "DELETE FROM messages WHERE conn_id = ? AND internal_id = ?;" (connId, msgId) + +createRatchetX3dhKeys :: DB.Connection -> ConnId -> C.PrivateKeyX448 -> C.PrivateKeyX448 -> IO () +createRatchetX3dhKeys db connId x3dhPrivKey1 x3dhPrivKey2 = + DB.execute db "INSERT INTO ratchets (conn_id, x3dh_priv_key_1, x3dh_priv_key_2) VALUES (?, ?, ?)" (connId, x3dhPrivKey1, x3dhPrivKey2) + +getRatchetX3dhKeys :: DB.Connection -> ConnId -> IO (Either StoreError (C.PrivateKeyX448, C.PrivateKeyX448)) +getRatchetX3dhKeys db connId = + fmap hasKeys $ + firstRow id SEX3dhKeysNotFound $ + DB.query db "SELECT x3dh_priv_key_1, x3dh_priv_key_2 FROM ratchets WHERE conn_id = ?" (Only connId) + where + hasKeys = \case + Right (Just k1, Just k2) -> Right (k1, k2) + _ -> Left SEX3dhKeysNotFound + +createRatchet :: DB.Connection -> ConnId -> RatchetX448 -> IO () +createRatchet db connId rc = + DB.executeNamed + db + [sql| + INSERT INTO ratchets (conn_id, ratchet_state) + VALUES (:conn_id, :ratchet_state) + ON CONFLICT (conn_id) DO UPDATE SET + ratchet_state = :ratchet_state, + x3dh_priv_key_1 = NULL, + x3dh_priv_key_2 = NULL + |] + [":conn_id" := connId, ":ratchet_state" := rc] + +getRatchet :: DB.Connection -> ConnId -> IO (Either StoreError RatchetX448) +getRatchet db connId = + firstRow' ratchet SERatchetNotFound $ DB.query db "SELECT ratchet_state FROM ratchets WHERE conn_id = ?" (Only connId) + where + ratchet = maybe (Left SERatchetNotFound) Right . fromOnly + +getSkippedMsgKeys :: DB.Connection -> ConnId -> IO SkippedMsgKeys +getSkippedMsgKeys db connId = + skipped <$> DB.query db "SELECT header_key, msg_n, msg_key FROM skipped_messages WHERE conn_id = ?" (Only connId) + where + skipped ms = foldl' addSkippedKey M.empty ms + addSkippedKey smks (hk, msgN, mk) = M.alter (Just . addMsgKey) hk smks + where + addMsgKey = maybe (M.singleton msgN mk) (M.insert msgN mk) + +updateRatchet :: DB.Connection -> ConnId -> RatchetX448 -> SkippedMsgDiff -> IO () +updateRatchet db connId rc skipped = do + DB.execute db "UPDATE ratchets SET ratchet_state = ? WHERE conn_id = ?" (rc, connId) + case skipped of + SMDNoChange -> pure () + SMDRemove hk msgN -> + DB.execute db "DELETE FROM skipped_messages WHERE conn_id = ? AND header_key = ? AND msg_n = ?" (connId, hk, msgN) + SMDAdd smks -> + forM_ (M.assocs smks) $ \(hk, mks) -> + forM_ (M.assocs mks) $ \(msgN, mk) -> + DB.execute db "INSERT INTO skipped_messages (conn_id, header_key, msg_n, msg_key) VALUES (?, ?, ?, ?)" (connId, hk, msgN, mk) + +createNtfToken :: DB.Connection -> NtfToken -> IO () +createNtfToken db NtfToken {deviceToken = DeviceToken provider token, ntfServer = srv@ProtocolServer {host, port}, ntfTokenId, ntfPubKey, ntfPrivKey, ntfDhKeys = (ntfDhPubKey, ntfDhPrivKey), ntfDhSecret, ntfTknStatus, ntfTknAction} = do + upsertNtfServer_ db srv + DB.execute + db + [sql| + INSERT INTO ntf_tokens + (provider, device_token, ntf_host, ntf_port, tkn_id, tkn_pub_key, tkn_priv_key, tkn_pub_dh_key, tkn_priv_dh_key, tkn_dh_secret, tkn_status, tkn_action) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) + |] + (provider, token, host, port, ntfTokenId, ntfPubKey, ntfPrivKey, ntfDhPubKey, ntfDhPrivKey, ntfDhSecret, ntfTknStatus, ntfTknAction) + +getDeviceNtfToken :: DB.Connection -> DeviceToken -> IO (Maybe NtfToken, [NtfToken]) +getDeviceNtfToken db t = do + tokens <- + map ntfToken + <$> DB.query_ db [sql| - UPDATE rcv_queues - SET status = :status - WHERE host = :host AND port = :port AND rcv_id = :rcv_id; + SELECT s.ntf_host, s.ntf_port, s.ntf_key_hash, + t.provider, t.device_token, t.tkn_id, t.tkn_pub_key, t.tkn_priv_key, t.tkn_pub_dh_key, t.tkn_priv_dh_key, t.tkn_dh_secret, t.tkn_status, t.tkn_action + FROM ntf_tokens t + JOIN ntf_servers s USING (ntf_host, ntf_port) |] - [":status" := status, ":host" := host, ":port" := port, ":rcv_id" := rcvId] + pure . first listToMaybe $ partition ((t ==) . deviceToken) tokens + where + ntfToken ((host, port, keyHash) :. (provider, dt, ntfTokenId, ntfPubKey, ntfPrivKey, ntfDhPubKey, ntfDhPrivKey, ntfDhSecret, ntfTknStatus, ntfTknAction)) = + let ntfServer = ProtocolServer {host, port, keyHash} + ntfDhKeys = (ntfDhPubKey, ntfDhPrivKey) + in NtfToken {deviceToken = DeviceToken provider dt, ntfServer, ntfTokenId, ntfPubKey, ntfPrivKey, ntfDhKeys, ntfDhSecret, ntfTknStatus, ntfTknAction} - setRcvQueueConfirmedE2E :: SQLiteStore -> RcvQueue -> C.DhSecretX25519 -> m () - setRcvQueueConfirmedE2E st RcvQueue {rcvId, server = ProtocolServer {host, port}} e2eDhSecret = - liftIO . withTransaction st $ \db -> - DB.executeNamed - db - [sql| - UPDATE rcv_queues - SET e2e_dh_secret = :e2e_dh_secret, - status = :status - WHERE host = :host AND port = :port AND rcv_id = :rcv_id - |] - [ ":status" := Confirmed, - ":e2e_dh_secret" := e2eDhSecret, - ":host" := host, - ":port" := port, - ":rcv_id" := rcvId - ] +updateNtfTokenRegistration :: DB.Connection -> NtfToken -> NtfTokenId -> C.DhSecretX25519 -> IO () +updateNtfTokenRegistration db NtfToken {deviceToken = DeviceToken provider token, ntfServer = ProtocolServer {host, port}} tknId ntfDhSecret = do + updatedAt <- getCurrentTime + DB.execute + db + [sql| + UPDATE ntf_tokens + SET tkn_id = ?, tkn_dh_secret = ?, tkn_status = ?, tkn_action = ?, updated_at = ? + WHERE provider = ? AND device_token = ? AND ntf_host = ? AND ntf_port = ? + |] + (tknId, ntfDhSecret, NTRegistered, Nothing :: Maybe NtfTknAction, updatedAt, provider, token, host, port) - setSndQueueStatus :: SQLiteStore -> SndQueue -> QueueStatus -> m () - setSndQueueStatus st SndQueue {sndId, server = ProtocolServer {host, port}} status = - -- ? throw error if queue does not exist? - liftIO . withTransaction st $ \db -> - DB.executeNamed - db - [sql| - UPDATE snd_queues - SET status = :status - WHERE host = :host AND port = :port AND snd_id = :snd_id; - |] - [":status" := status, ":host" := host, ":port" := port, ":snd_id" := sndId] +updateNtfToken :: DB.Connection -> NtfToken -> NtfTknStatus -> Maybe NtfTknAction -> IO () +updateNtfToken db NtfToken {deviceToken = DeviceToken provider token, ntfServer = ProtocolServer {host, port}} tknStatus tknAction = do + updatedAt <- getCurrentTime + DB.execute + db + [sql| + UPDATE ntf_tokens + SET tkn_status = ?, tkn_action = ?, updated_at = ? + WHERE provider = ? AND device_token = ? AND ntf_host = ? AND ntf_port = ? + |] + (tknStatus, tknAction, updatedAt, provider, token, host, port) - getRcvQueue :: SQLiteStore -> ConnId -> m RcvQueue - getRcvQueue st connId = - liftIOEither . withTransaction st $ \db -> do - rq_ <- getRcvQueueByConnId_ db connId - pure $ maybe (Left SEConnNotFound) Right rq_ +removeNtfToken :: DB.Connection -> NtfToken -> IO () +removeNtfToken db NtfToken {deviceToken = DeviceToken provider token, ntfServer = ProtocolServer {host, port}} = + DB.execute + db + [sql| + DELETE FROM ntf_tokens + WHERE provider = ? AND device_token = ? AND ntf_host = ? AND ntf_port = ? + |] + (provider, token, host, port) - setRcvQueueNotifierKey :: SQLiteStore -> ConnId -> NtfPublicVerifyKey -> NtfPrivateSignKey -> m () - setRcvQueueNotifierKey st connId ntfPublicKey ntfPrivateKey = - liftIO . withTransaction st $ \db -> - DB.execute - db - [sql| - UPDATE rcv_queues - SET ntf_public_key = ?, ntf_private_key = ? - WHERE conn_id = ? - |] - (ntfPublicKey, ntfPrivateKey, connId) +getNtfSubscription :: DB.Connection -> ConnId -> IO (Maybe NtfSubscription) +getNtfSubscription db connId = + maybeFirstRow ntfSubscription $ + DB.query + db + [sql| + SELECT s.host, s.port, s.key_hash, ns.ntf_host, ns.ntf_port, ns.ntf_key_hash, + nsb.smp_ntf_id, nsb.ntf_sub_id, nsb.ntf_sub_status, nsb.ntf_sub_action_ts + FROM ntf_subscriptions nsb + JOIN servers s ON s.host = nsb.smp_host AND s.port = nsb.smp_port + JOIN ntf_servers ns USING (ntf_host, ntf_port) + WHERE nsb.conn_id = ? + |] + (Only connId) + where + ntfSubscription (smpHost, smpPort, smpKeyHash, ntfHost, ntfPort, ntfKeyHash, ntfQueueId, ntfSubId, ntfSubStatus, ntfSubActionTs) = + let smpServer = SMPServer smpHost smpPort smpKeyHash + ntfServer = ProtocolServer ntfHost ntfPort ntfKeyHash + in NtfSubscription {connId, smpServer, ntfQueueId, ntfServer, ntfSubId, ntfSubStatus, ntfSubActionTs} - setRcvQueueNotifierId :: SQLiteStore -> ConnId -> NotifierId -> m () - setRcvQueueNotifierId st connId nId = - liftIO . withTransaction st $ \db -> - DB.execute - db - [sql| - UPDATE rcv_queues - SET ntf_id = ? - WHERE conn_id = ? - |] - (nId, connId) +createNtfSubscription :: DB.Connection -> NtfSubscription -> NtfSubOrSMPAction -> IO () +createNtfSubscription db NtfSubscription {connId, smpServer = (SMPServer host port _), ntfQueueId, ntfServer = (SMPServer ntfHost ntfPort _), ntfSubId, ntfSubStatus, ntfSubActionTs} ntfAction = + DB.execute + db + [sql| + INSERT INTO ntf_subscriptions + (conn_id, smp_host, smp_port, smp_ntf_id, ntf_host, ntf_port, ntf_sub_id, + ntf_sub_status, ntf_sub_action, ntf_sub_smp_action, ntf_sub_action_ts) + VALUES (?,?,?,?,?,?,?,?,?,?,?) + |] + ( (connId, host, port, ntfQueueId, ntfHost, ntfPort, ntfSubId) + :. (ntfSubStatus, ntfSubAction, ntfSubSMPAction, ntfSubActionTs) + ) + where + (ntfSubAction, ntfSubSMPAction) = ntfSubAndSMPAction ntfAction - createConfirmation :: SQLiteStore -> TVar ChaChaDRG -> NewConfirmation -> m ConfirmationId - createConfirmation st gVar NewConfirmation {connId, senderConf = SMPConfirmation {senderKey, e2ePubKey, connInfo, smpReplyQueues}, ratchetState} = - liftIOEither . withTransaction st $ \db -> - createWithRandomId gVar $ \confirmationId -> +markNtfSubscriptionForDeletion :: DB.Connection -> ConnId -> IO (Either StoreError ()) +markNtfSubscriptionForDeletion _db _rcvQueue = pure $ Left SENotImplemented + +updateNtfSubscription :: DB.Connection -> ConnId -> NtfSubscription -> NtfSubOrSMPAction -> IO () +updateNtfSubscription db connId NtfSubscription {ntfQueueId, ntfSubId, ntfSubStatus, ntfSubActionTs} ntfAction = do + r <- maybeFirstRow fromOnly $ DB.query db "SELECT updated_by_supervisor FROM ntf_subscriptions WHERE conn_id = ?" (Only connId) + forM_ r $ \updatedBySupervisor -> do + updatedAt <- getCurrentTime + if updatedBySupervisor + then DB.execute db [sql| - INSERT INTO conn_confirmations - (confirmation_id, conn_id, sender_key, e2e_snd_pub_key, ratchet_state, sender_conn_info, smp_reply_queues, accepted) VALUES (?, ?, ?, ?, ?, ?, ?, 0); + UPDATE ntf_subscriptions + SET smp_ntf_id = ?, ntf_sub_id = ?, ntf_sub_status = ?, updated_by_supervisor = ?, updated_at = ? + WHERE conn_id = ? |] - (confirmationId, connId, senderKey, e2ePubKey, ratchetState, connInfo, smpReplyQueues) - - acceptConfirmation :: SQLiteStore -> ConfirmationId -> ConnInfo -> m AcceptedConfirmation - acceptConfirmation st confirmationId ownConnInfo = - liftIOEither . withTransaction st $ \db -> do - DB.executeNamed - db - [sql| - UPDATE conn_confirmations - SET accepted = 1, - own_conn_info = :own_conn_info - WHERE confirmation_id = :confirmation_id; - |] - [ ":own_conn_info" := ownConnInfo, - ":confirmation_id" := confirmationId - ] - firstRow confirmation SEConfirmationNotFound $ - DB.query - db - [sql| - SELECT conn_id, sender_key, e2e_snd_pub_key, ratchet_state, sender_conn_info, smp_reply_queues - FROM conn_confirmations - WHERE confirmation_id = ?; - |] - (Only confirmationId) - where - confirmation (connId, senderKey, e2ePubKey, ratchetState, connInfo, smpReplyQueues_) = - AcceptedConfirmation - { confirmationId, - connId, - senderConf = SMPConfirmation {senderKey, e2ePubKey, connInfo, smpReplyQueues = fromMaybe [] smpReplyQueues_}, - ratchetState, - ownConnInfo - } - - getAcceptedConfirmation :: SQLiteStore -> ConnId -> m AcceptedConfirmation - getAcceptedConfirmation st connId = - liftIOEither . withTransaction st $ \db -> - firstRow confirmation SEConfirmationNotFound $ - DB.query - db - [sql| - SELECT confirmation_id, sender_key, e2e_snd_pub_key, ratchet_state, sender_conn_info, smp_reply_queues, own_conn_info - FROM conn_confirmations - WHERE conn_id = ? AND accepted = 1; - |] - (Only connId) - where - confirmation (confirmationId, senderKey, e2ePubKey, ratchetState, connInfo, smpReplyQueues_, ownConnInfo) = - AcceptedConfirmation - { confirmationId, - connId, - senderConf = SMPConfirmation {senderKey, e2ePubKey, connInfo, smpReplyQueues = fromMaybe [] smpReplyQueues_}, - ratchetState, - ownConnInfo - } - - removeConfirmations :: SQLiteStore -> ConnId -> m () - removeConfirmations st connId = - liftIO . withTransaction st $ \db -> - DB.executeNamed - db - [sql| - DELETE FROM conn_confirmations - WHERE conn_id = :conn_id; - |] - [":conn_id" := connId] - - setHandshakeVersion :: SQLiteStore -> ConnId -> Version -> Bool -> m () - setHandshakeVersion st connId aVersion duplexHS = - liftIO . withTransaction st $ \db -> - DB.execute db "UPDATE connections SET smp_agent_version = ?, duplex_handshake = ? WHERE conn_id = ?" (aVersion, duplexHS, connId) - - createInvitation :: SQLiteStore -> TVar ChaChaDRG -> NewInvitation -> m InvitationId - createInvitation st gVar NewInvitation {contactConnId, connReq, recipientConnInfo} = - liftIOEither . withTransaction st $ \db -> - createWithRandomId gVar $ \invitationId -> + (ntfQueueId, ntfSubId, ntfSubStatus, False, updatedAt, connId) + else DB.execute db [sql| - INSERT INTO conn_invitations - (invitation_id, contact_conn_id, cr_invitation, recipient_conn_info, accepted) VALUES (?, ?, ?, ?, 0); + UPDATE ntf_subscriptions + SET smp_ntf_id = ?, ntf_sub_id = ?, ntf_sub_status = ?, ntf_sub_action = ?, ntf_sub_smp_action = ?, ntf_sub_action_ts = ?, updated_by_supervisor = ?, updated_at = ? + WHERE conn_id = ? |] - (invitationId, contactConnId, connReq, recipientConnInfo) + (ntfQueueId, ntfSubId, ntfSubStatus, ntfSubAction, ntfSubSMPAction, ntfSubActionTs, False, updatedAt, connId) + where + (ntfSubAction, ntfSubSMPAction) = ntfSubAndSMPAction ntfAction - getInvitation :: SQLiteStore -> InvitationId -> m Invitation - getInvitation st invitationId = - liftIOEither . withTransaction st $ \db -> - firstRow invitation SEInvitationNotFound $ - DB.query - db - [sql| - SELECT contact_conn_id, cr_invitation, recipient_conn_info, own_conn_info, accepted - FROM conn_invitations - WHERE invitation_id = ? - AND accepted = 0 - |] - (Only invitationId) - where - invitation (contactConnId, connReq, recipientConnInfo, ownConnInfo, accepted) = - Invitation {invitationId, contactConnId, connReq, recipientConnInfo, ownConnInfo, accepted} - - acceptInvitation :: SQLiteStore -> InvitationId -> ConnInfo -> m () - acceptInvitation st invitationId ownConnInfo = - liftIO . withTransaction st $ \db -> do - DB.executeNamed - db - [sql| - UPDATE conn_invitations - SET accepted = 1, - own_conn_info = :own_conn_info - WHERE invitation_id = :invitation_id - |] - [ ":own_conn_info" := ownConnInfo, - ":invitation_id" := invitationId - ] - - deleteInvitation :: SQLiteStore -> ConnId -> InvitationId -> m () - deleteInvitation st contactConnId invId = - liftIOEither . withTransaction st $ \db -> - runExceptT $ - ExceptT (getConn_ db contactConnId) >>= \case - SomeConn SCContact _ -> - liftIO $ DB.execute db "DELETE FROM conn_invitations WHERE contact_conn_id = ? AND invitation_id = ?" (contactConnId, invId) - _ -> throwError SEConnNotFound - - updateRcvIds :: SQLiteStore -> ConnId -> m (InternalId, InternalRcvId, PrevExternalSndId, PrevRcvMsgHash) - updateRcvIds st connId = - liftIO . withTransaction st $ \db -> do - (lastInternalId, lastInternalRcvId, lastExternalSndId, lastRcvHash) <- retrieveLastIdsAndHashRcv_ db connId - let internalId = InternalId $ unId lastInternalId + 1 - internalRcvId = InternalRcvId $ unRcvId lastInternalRcvId + 1 - updateLastIdsRcv_ db connId internalId internalRcvId - pure (internalId, internalRcvId, lastExternalSndId, lastRcvHash) - - createRcvMsg :: SQLiteStore -> ConnId -> RcvMsgData -> m () - createRcvMsg st connId rcvMsgData = - liftIO . withTransaction st $ \db -> do - insertRcvMsgBase_ db connId rcvMsgData - insertRcvMsgDetails_ db connId rcvMsgData - updateHashRcv_ db connId rcvMsgData - - updateSndIds :: SQLiteStore -> ConnId -> m (InternalId, InternalSndId, PrevSndMsgHash) - updateSndIds st connId = - liftIO . withTransaction st $ \db -> do - (lastInternalId, lastInternalSndId, prevSndHash) <- retrieveLastIdsAndHashSnd_ db connId - let internalId = InternalId $ unId lastInternalId + 1 - internalSndId = InternalSndId $ unSndId lastInternalSndId + 1 - updateLastIdsSnd_ db connId internalId internalSndId - pure (internalId, internalSndId, prevSndHash) - - createSndMsg :: SQLiteStore -> ConnId -> SndMsgData -> m () - createSndMsg st connId sndMsgData = - liftIO . withTransaction st $ \db -> do - insertSndMsgBase_ db connId sndMsgData - insertSndMsgDetails_ db connId sndMsgData - updateHashSnd_ db connId sndMsgData - - getPendingMsgData :: SQLiteStore -> ConnId -> InternalId -> m (Maybe RcvQueue, PendingMsgData) - getPendingMsgData st connId msgId = - liftIOEither . withTransaction st $ \db -> runExceptT $ do - rq_ <- liftIO $ getRcvQueueByConnId_ db connId - msgData <- - ExceptT . firstRow pendingMsgData SEMsgNotFound $ - DB.query - db - [sql| - SELECT m.msg_type, m.msg_flags, m.msg_body, m.internal_ts - FROM messages m - JOIN snd_messages s ON s.conn_id = m.conn_id AND s.internal_id = m.internal_id - WHERE m.conn_id = ? AND m.internal_id = ? - |] - (connId, msgId) - pure (rq_, msgData) - where - pendingMsgData :: (AgentMessageType, MsgFlags, MsgBody, InternalTs) -> PendingMsgData - pendingMsgData (msgType, msgFlags, msgBody, internalTs) = PendingMsgData {msgId, msgType, msgFlags, msgBody, internalTs} - - getPendingMsgs :: SQLiteStore -> ConnId -> m [InternalId] - getPendingMsgs st connId = - liftIO . withTransaction st $ \db -> - map fromOnly - <$> DB.query db "SELECT internal_id FROM snd_messages WHERE conn_id = ?" (Only connId) - - setMsgUserAck :: SQLiteStore -> ConnId -> InternalId -> m SMP.MsgId - setMsgUserAck st connId agentMsgId = - liftIOEither . withTransaction st $ \db -> do - DB.execute db "UPDATE rcv_messages SET user_ack = ? WHERE conn_id = ? AND internal_id = ?" (True, connId, agentMsgId) - firstRow fromOnly SEMsgNotFound $ - DB.query db "SELECT broker_id FROM rcv_messages WHERE conn_id = ? AND internal_id = ?" (connId, agentMsgId) - - getLastMsg :: SQLiteStore -> ConnId -> SMP.MsgId -> m (Maybe RcvMsg) - getLastMsg st connId msgId = - liftIO . withTransaction st $ \db -> - fmap rcvMsg . listToMaybe - <$> DB.query - db - [sql| - SELECT - r.internal_id, m.internal_ts, r.broker_id, r.broker_ts, r.external_snd_id, r.integrity, - m.msg_body, r.user_ack - FROM rcv_messages r - JOIN messages m ON r.internal_id = m.internal_id - JOIN connections c ON r.conn_id = c.conn_id AND c.last_internal_msg_id = r.internal_id - WHERE r.conn_id = ? AND r.broker_id = ? - |] - (connId, msgId) - where - rcvMsg (agentMsgId, internalTs, brokerId, brokerTs, sndMsgId, integrity, msgBody, userAck) = - let msgMeta = MsgMeta {recipient = (agentMsgId, internalTs), broker = (brokerId, brokerTs), sndMsgId, integrity} - in RcvMsg {internalId = InternalId agentMsgId, msgMeta, msgBody, userAck} - - deleteMsg :: SQLiteStore -> ConnId -> InternalId -> m () - deleteMsg st connId msgId = - liftIO . withTransaction st $ \db -> - DB.execute db "DELETE FROM messages WHERE conn_id = ? AND internal_id = ?;" (connId, msgId) - - createRatchetX3dhKeys :: SQLiteStore -> ConnId -> C.PrivateKeyX448 -> C.PrivateKeyX448 -> m () - createRatchetX3dhKeys st connId x3dhPrivKey1 x3dhPrivKey2 = - liftIO . withTransaction st $ \db -> - DB.execute db "INSERT INTO ratchets (conn_id, x3dh_priv_key_1, x3dh_priv_key_2) VALUES (?, ?, ?)" (connId, x3dhPrivKey1, x3dhPrivKey2) - - getRatchetX3dhKeys :: SQLiteStore -> ConnId -> m (C.PrivateKeyX448, C.PrivateKeyX448) - getRatchetX3dhKeys st connId = - liftIOEither . withTransaction st $ \db -> - fmap hasKeys $ - firstRow id SEX3dhKeysNotFound $ - DB.query db "SELECT x3dh_priv_key_1, x3dh_priv_key_2 FROM ratchets WHERE conn_id = ?" (Only connId) - where - hasKeys = \case - Right (Just k1, Just k2) -> Right (k1, k2) - _ -> Left SEX3dhKeysNotFound - - createRatchet :: SQLiteStore -> ConnId -> RatchetX448 -> m () - createRatchet st connId rc = - liftIO . withTransaction st $ \db -> do - DB.executeNamed - db - [sql| - INSERT INTO ratchets (conn_id, ratchet_state) - VALUES (:conn_id, :ratchet_state) - ON CONFLICT (conn_id) DO UPDATE SET - ratchet_state = :ratchet_state, - x3dh_priv_key_1 = NULL, - x3dh_priv_key_2 = NULL - |] - [":conn_id" := connId, ":ratchet_state" := rc] - - getRatchet :: SQLiteStore -> ConnId -> m RatchetX448 - getRatchet st connId = - liftIOEither . withTransaction st $ \db -> - ratchet - <$> DB.query db "SELECT ratchet_state FROM ratchets WHERE conn_id = ?" (Only connId) - where - ratchet (Only (Just rc) : _) = Right rc - ratchet _ = Left SERatchetNotFound - - getSkippedMsgKeys :: SQLiteStore -> ConnId -> m SkippedMsgKeys - getSkippedMsgKeys st connId = - liftIO . withTransaction st $ \db -> - skipped <$> DB.query db "SELECT header_key, msg_n, msg_key FROM skipped_messages WHERE conn_id = ?" (Only connId) - where - skipped ms = foldl' addSkippedKey M.empty ms - addSkippedKey smks (hk, msgN, mk) = M.alter (Just . addMsgKey) hk smks - where - addMsgKey = maybe (M.singleton msgN mk) (M.insert msgN mk) - - updateRatchet :: SQLiteStore -> ConnId -> RatchetX448 -> SkippedMsgDiff -> m () - updateRatchet st connId rc skipped = - liftIO . withTransaction st $ \db -> do - DB.execute db "UPDATE ratchets SET ratchet_state = ? WHERE conn_id = ?" (rc, connId) - case skipped of - SMDNoChange -> pure () - SMDRemove hk msgN -> - DB.execute db "DELETE FROM skipped_messages WHERE conn_id = ? AND header_key = ? AND msg_n = ?" (connId, hk, msgN) - SMDAdd smks -> - forM_ (M.assocs smks) $ \(hk, mks) -> - forM_ (M.assocs mks) $ \(msgN, mk) -> - DB.execute db "INSERT INTO skipped_messages (conn_id, header_key, msg_n, msg_key) VALUES (?, ?, ?, ?)" (connId, hk, msgN, mk) - - createNtfToken :: SQLiteStore -> NtfToken -> m () - createNtfToken st NtfToken {deviceToken = DeviceToken provider token, ntfServer = srv@ProtocolServer {host, port}, ntfTokenId, ntfPubKey, ntfPrivKey, ntfDhKeys = (ntfDhPubKey, ntfDhPrivKey), ntfDhSecret, ntfTknStatus, ntfTknAction} = - liftIO . withTransaction st $ \db -> do - upsertNtfServer_ db srv - DB.execute - db - [sql| - INSERT INTO ntf_tokens - (provider, device_token, ntf_host, ntf_port, tkn_id, tkn_pub_key, tkn_priv_key, tkn_pub_dh_key, tkn_priv_dh_key, tkn_dh_secret, tkn_status, tkn_action) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) - |] - (provider, token, host, port, ntfTokenId, ntfPubKey, ntfPrivKey, ntfDhPubKey, ntfDhPrivKey, ntfDhSecret, ntfTknStatus, ntfTknAction) - - getDeviceNtfToken :: SQLiteStore -> DeviceToken -> m (Maybe NtfToken, [NtfToken]) - getDeviceNtfToken st t = - liftIO . withTransaction st $ \db -> do - tokens <- - map ntfToken - <$> DB.query_ - db - [sql| - SELECT s.ntf_host, s.ntf_port, s.ntf_key_hash, - t.provider, t.device_token, t.tkn_id, t.tkn_pub_key, t.tkn_priv_key, t.tkn_pub_dh_key, t.tkn_priv_dh_key, t.tkn_dh_secret, t.tkn_status, t.tkn_action - FROM ntf_tokens t - JOIN ntf_servers s USING (ntf_host, ntf_port) - |] - pure . first listToMaybe $ partition ((t ==) . deviceToken) tokens - where - ntfToken ((host, port, keyHash) :. (provider, dt, ntfTokenId, ntfPubKey, ntfPrivKey, ntfDhPubKey, ntfDhPrivKey, ntfDhSecret, ntfTknStatus, ntfTknAction)) = - let ntfServer = ProtocolServer {host, port, keyHash} - ntfDhKeys = (ntfDhPubKey, ntfDhPrivKey) - in NtfToken {deviceToken = DeviceToken provider dt, ntfServer, ntfTokenId, ntfPubKey, ntfPrivKey, ntfDhKeys, ntfDhSecret, ntfTknStatus, ntfTknAction} - - updateNtfTokenRegistration :: SQLiteStore -> NtfToken -> NtfTokenId -> C.DhSecretX25519 -> m () - updateNtfTokenRegistration st NtfToken {deviceToken = DeviceToken provider token, ntfServer = ProtocolServer {host, port}} tknId ntfDhSecret = - liftIO . withTransaction st $ \db -> do +setNullNtfSubscriptionAction :: DB.Connection -> ConnId -> IO () +setNullNtfSubscriptionAction db connId = do + r <- maybeFirstRow fromOnly $ DB.query db "SELECT updated_by_supervisor FROM ntf_subscriptions WHERE conn_id = ?" (Only connId) + forM_ r $ \updatedBySupervisor -> + unless updatedBySupervisor $ do updatedAt <- getCurrentTime DB.execute db [sql| - UPDATE ntf_tokens - SET tkn_id = ?, tkn_dh_secret = ?, tkn_status = ?, tkn_action = ?, updated_at = ? - WHERE provider = ? AND device_token = ? AND ntf_host = ? AND ntf_port = ? + UPDATE ntf_subscriptions + SET ntf_sub_action = ?, ntf_sub_smp_action = ?, ntf_sub_action_ts = ?, updated_by_supervisor = ?, updated_at = ? + WHERE conn_id = ? |] - (tknId, ntfDhSecret, NTRegistered, Nothing :: Maybe NtfTknAction, updatedAt, provider, token, host, port) + (Nothing :: Maybe NtfSubAction, Nothing :: Maybe NtfSubSMPAction, Nothing :: Maybe UTCTime, False, updatedAt, connId) - updateNtfToken :: SQLiteStore -> NtfToken -> NtfTknStatus -> Maybe NtfTknAction -> m () - updateNtfToken st NtfToken {deviceToken = DeviceToken provider token, ntfServer = ProtocolServer {host, port}} tknStatus tknAction = - liftIO . withTransaction st $ \db -> do - updatedAt <- getCurrentTime - DB.execute +deleteNtfSubscription :: DB.Connection -> ConnId -> IO (Either StoreError ()) +deleteNtfSubscription _db _connId = pure $ Left SENotImplemented + +getNextNtfSubAction :: DB.Connection -> NtfServer -> IO (Maybe (NtfSubscription, NtfSubAction, RcvQueue)) +getNextNtfSubAction db ntfServer@(ProtocolServer ntfHost ntfPort _) = do + maybeFirstRow ntfSubscription getNtfSubAction_ $>>= \(ntfSub@NtfSubscription {connId}, ntfSubAction) -> do + DB.execute db "UPDATE ntf_subscriptions SET updated_by_supervisor = ? WHERE conn_id = ?" (False, connId) + (ntfSub,ntfSubAction,) <$$> getRcvQueueByConnId_ db connId + where + getNtfSubAction_ = + DB.query db [sql| - UPDATE ntf_tokens - SET tkn_status = ?, tkn_action = ?, updated_at = ? - WHERE provider = ? AND device_token = ? AND ntf_host = ? AND ntf_port = ? + SELECT ns.conn_id, s.host, s.port, s.key_hash, + ns.smp_ntf_id, ns.ntf_sub_id, ns.ntf_sub_status, ns.ntf_sub_action_ts, ns.ntf_sub_action + FROM ntf_subscriptions ns + JOIN servers s ON s.host = ns.smp_host AND s.port = ns.smp_port + WHERE ns.ntf_host = ? AND ns.ntf_port = ? AND ns.ntf_sub_action IS NOT NULL + ORDER BY ns.ntf_sub_action_ts ASC + LIMIT 1 |] - (tknStatus, tknAction, updatedAt, provider, token, host, port) + (ntfHost, ntfPort) + ntfSubscription (connId, smpHost, smpPort, smpKeyHash, ntfQueueId, ntfSubId, ntfSubStatus, ntfSubActionTs, ntfSubAction) = + let smpServer = SMPServer smpHost smpPort smpKeyHash + in (NtfSubscription {connId, smpServer, ntfQueueId, ntfServer, ntfSubId, ntfSubStatus, ntfSubActionTs}, ntfSubAction) - removeNtfToken :: SQLiteStore -> NtfToken -> m () - removeNtfToken st NtfToken {deviceToken = DeviceToken provider token, ntfServer = ProtocolServer {host, port}} = - liftIO . withTransaction st $ \db -> - DB.execute +getNextNtfSubSMPAction :: DB.Connection -> SMPServer -> IO (Maybe (NtfSubscription, NtfSubSMPAction, RcvQueue)) +getNextNtfSubSMPAction db smpServer@(SMPServer smpHost smpPort _) = do + maybeFirstRow ntfSubscription getNtfSubAction_ $>>= \(ntfSub@NtfSubscription {connId}, ntfSubAction) -> do + DB.execute db "UPDATE ntf_subscriptions SET updated_by_supervisor = ? WHERE conn_id = ?" (False, connId) + (ntfSub,ntfSubAction,) <$$> getRcvQueueByConnId_ db connId + where + getNtfSubAction_ = + DB.query db [sql| - DELETE FROM ntf_tokens - WHERE provider = ? AND device_token = ? AND ntf_host = ? AND ntf_port = ? + SELECT ns.conn_id, s.ntf_host, s.ntf_port, s.ntf_key_hash, + ns.smp_ntf_id, ns.ntf_sub_id, ns.ntf_sub_status, ns.ntf_sub_action_ts, ns.ntf_sub_smp_action + FROM ntf_subscriptions ns + JOIN ntf_servers s USING (ntf_host, ntf_port) + WHERE ns.smp_host = ? AND ns.smp_port = ? AND ns.ntf_sub_smp_action IS NOT NULL + ORDER BY ns.ntf_sub_action_ts ASC + LIMIT 1 |] - (provider, token, host, port) + (smpHost, smpPort) + ntfSubscription (connId, ntfHost, ntfPort, ntfKeyHash, ntfQueueId, ntfSubId, ntfSubStatus, ntfSubActionTs, ntfSubAction) = + let ntfServer = ProtocolServer ntfHost ntfPort ntfKeyHash + in (NtfSubscription {connId, smpServer, ntfQueueId, ntfServer, ntfSubId, ntfSubStatus, ntfSubActionTs}, ntfSubAction) - getNtfSubscription :: SQLiteStore -> ConnId -> m (Maybe NtfSubscription) - getNtfSubscription st connId = - liftIO . withTransaction st $ \db -> - maybeFirstRow ntfSubscription $ - DB.query - db - [sql| - SELECT s.host, s.port, s.key_hash, ns.ntf_host, ns.ntf_port, ns.ntf_key_hash, - nsb.smp_ntf_id, nsb.ntf_sub_id, nsb.ntf_sub_status, nsb.ntf_sub_action_ts - FROM ntf_subscriptions nsb - JOIN servers s ON s.host = nsb.smp_host AND s.port = nsb.smp_port - JOIN ntf_servers ns USING (ntf_host, ntf_port) - WHERE nsb.conn_id = ? - |] - (Only connId) - where - ntfSubscription (smpHost, smpPort, smpKeyHash, ntfHost, ntfPort, ntfKeyHash, ntfQueueId, ntfSubId, ntfSubStatus, ntfSubActionTs) = - let smpServer = SMPServer smpHost smpPort smpKeyHash - ntfServer = ProtocolServer ntfHost ntfPort ntfKeyHash - in NtfSubscription {connId, smpServer, ntfQueueId, ntfServer, ntfSubId, ntfSubStatus, ntfSubActionTs} +getActiveNtfToken :: DB.Connection -> IO (Maybe NtfToken) +getActiveNtfToken db = + maybeFirstRow ntfToken $ + DB.query + db + [sql| + SELECT s.ntf_host, s.ntf_port, s.ntf_key_hash, + t.provider, t.device_token, t.tkn_id, t.tkn_pub_key, t.tkn_priv_key, t.tkn_pub_dh_key, t.tkn_priv_dh_key, t.tkn_dh_secret, t.tkn_status, t.tkn_action + FROM ntf_tokens t + JOIN ntf_servers s USING (ntf_host, ntf_port) + WHERE t.tkn_status = ? + |] + (Only NTActive) + where + ntfToken ((host, port, keyHash) :. (provider, dt, ntfTokenId, ntfPubKey, ntfPrivKey, ntfDhPubKey, ntfDhPrivKey, ntfDhSecret, ntfTknStatus, ntfTknAction)) = + let ntfServer = ProtocolServer {host, port, keyHash} + ntfDhKeys = (ntfDhPubKey, ntfDhPrivKey) + in NtfToken {deviceToken = DeviceToken provider dt, ntfServer, ntfTokenId, ntfPubKey, ntfPrivKey, ntfDhKeys, ntfDhSecret, ntfTknStatus, ntfTknAction} - createNtfSubscription :: SQLiteStore -> NtfSubscription -> NtfSubOrSMPAction -> m () - createNtfSubscription st NtfSubscription {connId, smpServer = (SMPServer host port _), ntfQueueId, ntfServer = (SMPServer ntfHost ntfPort _), ntfSubId, ntfSubStatus, ntfSubActionTs} ntfAction = - liftIO . withTransaction st $ \db -> - DB.execute - db - [sql| - INSERT INTO ntf_subscriptions - (conn_id, smp_host, smp_port, smp_ntf_id, ntf_host, ntf_port, ntf_sub_id, - ntf_sub_status, ntf_sub_action, ntf_sub_smp_action, ntf_sub_action_ts) - VALUES (?,?,?,?,?,?,?,?,?,?,?) - |] - ( (connId, host, port, ntfQueueId, ntfHost, ntfPort, ntfSubId) - :. (ntfSubStatus, ntfSubAction, ntfSubSMPAction, ntfSubActionTs) - ) - where - (ntfSubAction, ntfSubSMPAction) = ntfSubAndSMPAction ntfAction - - markNtfSubscriptionForDeletion :: SQLiteStore -> ConnId -> m () - markNtfSubscriptionForDeletion _st _rcvQueue = throwError SENotImplemented - - updateNtfSubscription :: SQLiteStore -> ConnId -> NtfSubscription -> NtfSubOrSMPAction -> m () - updateNtfSubscription st connId NtfSubscription {ntfQueueId, ntfSubId, ntfSubStatus, ntfSubActionTs} ntfAction = - liftIO . withTransaction st $ \db -> do - r <- maybeFirstRow fromOnly $ DB.query db "SELECT updated_by_supervisor FROM ntf_subscriptions WHERE conn_id = ?" (Only connId) - forM_ r $ \updatedBySupervisor -> do - updatedAt <- getCurrentTime - if updatedBySupervisor - then - DB.execute - db - [sql| - UPDATE ntf_subscriptions - SET smp_ntf_id = ?, ntf_sub_id = ?, ntf_sub_status = ?, updated_by_supervisor = ?, updated_at = ? - WHERE conn_id = ? - |] - (ntfQueueId, ntfSubId, ntfSubStatus, False, updatedAt, connId) - else - DB.execute - db - [sql| - UPDATE ntf_subscriptions - SET smp_ntf_id = ?, ntf_sub_id = ?, ntf_sub_status = ?, ntf_sub_action = ?, ntf_sub_smp_action = ?, ntf_sub_action_ts = ?, updated_by_supervisor = ?, updated_at = ? - WHERE conn_id = ? - |] - (ntfQueueId, ntfSubId, ntfSubStatus, ntfSubAction, ntfSubSMPAction, ntfSubActionTs, False, updatedAt, connId) - where - (ntfSubAction, ntfSubSMPAction) = ntfSubAndSMPAction ntfAction - - setNullNtfSubscriptionAction :: SQLiteStore -> ConnId -> m () - setNullNtfSubscriptionAction st connId = - liftIO . withTransaction st $ \db -> do - r <- maybeFirstRow fromOnly $ DB.query db "SELECT updated_by_supervisor FROM ntf_subscriptions WHERE conn_id = ?" (Only connId) - forM_ r $ \updatedBySupervisor -> - unless updatedBySupervisor $ do - updatedAt <- getCurrentTime - DB.execute - db - [sql| - UPDATE ntf_subscriptions - SET ntf_sub_action = ?, ntf_sub_smp_action = ?, ntf_sub_action_ts = ?, updated_by_supervisor = ?, updated_at = ? - WHERE conn_id = ? - |] - (Nothing :: Maybe NtfSubAction, Nothing :: Maybe NtfSubSMPAction, Nothing :: Maybe UTCTime, False, updatedAt, connId) - - deleteNtfSubscription :: SQLiteStore -> ConnId -> m () - deleteNtfSubscription _st _connId = throwError SENotImplemented - - getNextNtfSubAction :: SQLiteStore -> NtfServer -> m (Maybe (NtfSubscription, NtfSubAction, RcvQueue)) - getNextNtfSubAction st ntfServer@(ProtocolServer ntfHost ntfPort _) = - liftIO . withTransaction st $ \db -> do - r <- - maybeFirstRow ntfSubscription $ - DB.query - db - [sql| - SELECT ns.conn_id, s.host, s.port, s.key_hash, - ns.smp_ntf_id, ns.ntf_sub_id, ns.ntf_sub_status, ns.ntf_sub_action_ts, ns.ntf_sub_action - FROM ntf_subscriptions ns - JOIN servers s ON s.host = ns.smp_host AND s.port = ns.smp_port - WHERE ns.ntf_host = ? AND ns.ntf_port = ? AND ns.ntf_sub_action IS NOT NULL - ORDER BY ns.ntf_sub_action_ts ASC - LIMIT 1 - |] - (ntfHost, ntfPort) - case r of - Just (ntfSub@NtfSubscription {connId}, ntfSubAction) -> do - DB.execute db "UPDATE ntf_subscriptions SET updated_by_supervisor = ? WHERE conn_id = ?" (False, connId) - rq_ <- getRcvQueueByConnId_ db connId - pure $ (\rq -> Just (ntfSub, ntfSubAction, rq)) =<< rq_ - Nothing -> pure Nothing - where - ntfSubscription (connId, smpHost, smpPort, smpKeyHash, ntfQueueId, ntfSubId, ntfSubStatus, ntfSubActionTs, ntfSubAction) = - let smpServer = SMPServer smpHost smpPort smpKeyHash - in (NtfSubscription {connId, smpServer, ntfQueueId, ntfServer, ntfSubId, ntfSubStatus, ntfSubActionTs}, ntfSubAction) - - getNextNtfSubSMPAction :: SQLiteStore -> SMPServer -> m (Maybe (NtfSubscription, NtfSubSMPAction, RcvQueue)) - getNextNtfSubSMPAction st smpServer@(SMPServer smpHost smpPort _) = - liftIO . withTransaction st $ \db -> do - r <- - maybeFirstRow ntfSubscription $ - DB.query - db - [sql| - SELECT ns.conn_id, s.ntf_host, s.ntf_port, s.ntf_key_hash, - ns.smp_ntf_id, ns.ntf_sub_id, ns.ntf_sub_status, ns.ntf_sub_action_ts, ns.ntf_sub_smp_action - FROM ntf_subscriptions ns - JOIN ntf_servers s USING (ntf_host, ntf_port) - WHERE ns.smp_host = ? AND ns.smp_port = ? AND ns.ntf_sub_smp_action IS NOT NULL - ORDER BY ns.ntf_sub_action_ts ASC - LIMIT 1 - |] - (smpHost, smpPort) - case r of - Just (ntfSub@NtfSubscription {connId}, ntfSubAction) -> do - DB.execute db "UPDATE ntf_subscriptions SET updated_by_supervisor = ? WHERE conn_id = ?" (False, connId) - rq_ <- getRcvQueueByConnId_ db connId - pure $ (\rq -> Just (ntfSub, ntfSubAction, rq)) =<< rq_ - Nothing -> pure Nothing - where - ntfSubscription (connId, ntfHost, ntfPort, ntfKeyHash, ntfQueueId, ntfSubId, ntfSubStatus, ntfSubActionTs, ntfSubAction) = - let ntfServer = ProtocolServer ntfHost ntfPort ntfKeyHash - in (NtfSubscription {connId, smpServer, ntfQueueId, ntfServer, ntfSubId, ntfSubStatus, ntfSubActionTs}, ntfSubAction) - - getActiveNtfToken :: SQLiteStore -> m (Maybe NtfToken) - getActiveNtfToken st = - liftIO . withTransaction st $ \db -> - maybeFirstRow ntfToken $ - DB.query - db - [sql| - SELECT s.ntf_host, s.ntf_port, s.ntf_key_hash, - t.provider, t.device_token, t.tkn_id, t.tkn_pub_key, t.tkn_priv_key, t.tkn_pub_dh_key, t.tkn_priv_dh_key, t.tkn_dh_secret, t.tkn_status, t.tkn_action - FROM ntf_tokens t - JOIN ntf_servers s USING (ntf_host, ntf_port) - WHERE t.tkn_status = ? - |] - (Only NTActive) - where - ntfToken ((host, port, keyHash) :. (provider, dt, ntfTokenId, ntfPubKey, ntfPrivKey, ntfDhPubKey, ntfDhPrivKey, ntfDhSecret, ntfTknStatus, ntfTknAction)) = - let ntfServer = ProtocolServer {host, port, keyHash} - ntfDhKeys = (ntfDhPubKey, ntfDhPrivKey) - in NtfToken {deviceToken = DeviceToken provider dt, ntfServer, ntfTokenId, ntfPubKey, ntfPrivKey, ntfDhKeys, ntfDhSecret, ntfTknStatus, ntfTknAction} - - getNtfRcvQueue :: SQLiteStore -> SMPQueueNtf -> m (ConnId, RcvDhSecret) - getNtfRcvQueue st SMPQueueNtf {smpServer = (SMPServer host port _), notifierId} = - liftIOEither . withTransaction st $ \db -> do - firstRow id SEConnNotFound $ - DB.query - db - [sql| - SELECT conn_id, rcv_dh_secret - FROM rcv_queues - WHERE host = ? AND port = ? AND ntf_id = ? - |] - (host, port, notifierId) +getNtfRcvQueue :: DB.Connection -> SMPQueueNtf -> IO (Either StoreError (ConnId, RcvDhSecret)) +getNtfRcvQueue db SMPQueueNtf {smpServer = (SMPServer host port _), notifierId} = + firstRow id SEConnNotFound $ + DB.query + db + [sql| + SELECT conn_id, rcv_dh_secret + FROM rcv_queues + WHERE host = ? AND port = ? AND ntf_id = ? + |] + (host, port, notifierId) -- * Auxiliary helpers @@ -929,9 +924,8 @@ firstRow f e a = second f . listToEither e <$> a maybeFirstRow :: Functor f => (a -> b) -> f [a] -> f (Maybe b) maybeFirstRow f q = fmap f . listToMaybe <$> q --- TODO move from simplex-chat --- firstRow' :: (a -> Either e b) -> e -> IO [a] -> IO (Either e b) --- firstRow' f e a = (f <=< listToEither e) <$> a +firstRow' :: (a -> Either e b) -> e -> IO [a] -> IO (Either e b) +firstRow' f e a = (f <=< listToEither e) <$> a {- ORMOLU_DISABLE -} -- SQLite.Simple only has these up to 10 fields, which is insufficient for some of our queries @@ -1015,8 +1009,8 @@ insertSndQueue_ dbConn connId SndQueue {..} = do -- * getConn helpers -getConn_ :: DB.Connection -> ConnId -> IO (Either StoreError SomeConn) -getConn_ dbConn connId = +getConn :: DB.Connection -> ConnId -> IO (Either StoreError SomeConn) +getConn dbConn connId = getConnData_ dbConn connId >>= \case Nothing -> pure $ Left SEConnNotFound Just (connData, cMode) -> do diff --git a/src/Simplex/Messaging/Util.hs b/src/Simplex/Messaging/Util.hs index 8940f239b..5c0876196 100644 --- a/src/Simplex/Messaging/Util.hs +++ b/src/Simplex/Messaging/Util.hs @@ -56,6 +56,10 @@ tryE :: Monad m => ExceptT e m a -> ExceptT e m (Either e a) tryE m = (Right <$> m) `catchE` (pure . Left) {-# INLINE tryE #-} +liftE :: (e -> e') -> ExceptT e IO a -> ExceptT e' IO a +liftE f a = ExceptT $ first f <$> runExceptT a +{-# INLINE liftE #-} + ifM :: Monad m => m Bool -> m a -> m a -> m a ifM ba t f = ba >>= \b -> if b then t else f {-# INLINE ifM #-} diff --git a/tests/AgentTests/SQLiteTests.hs b/tests/AgentTests/SQLiteTests.hs index 47a818503..2afc1d316 100644 --- a/tests/AgentTests/SQLiteTests.hs +++ b/tests/AgentTests/SQLiteTests.hs @@ -11,7 +11,6 @@ module AgentTests.SQLiteTests (storeTests) where import Control.Concurrent.Async (concurrently_) import Control.Concurrent.STM import Control.Monad (replicateM_) -import Control.Monad.Except (ExceptT, runExceptT) import Crypto.Random (drgNew) import Data.ByteString.Char8 (ByteString) import qualified Data.Text as T @@ -55,29 +54,23 @@ createStore = do createSQLiteStore (testDB <> show r) Migrations.app True removeStore :: SQLiteStore -> IO () -removeStore store = do - close store - removeFile $ dbFilePath store +removeStore db = do + close db + removeFile $ dbFilePath db where close :: SQLiteStore -> IO () close st = mapM_ DB.close =<< atomically (tryTakeTMVar $ dbConnection st) -returnsResult :: (Eq a, Eq e, Show a, Show e) => ExceptT e IO a -> a -> Expectation -action `returnsResult` r = runExceptT action `shouldReturn` Right r - -throwsError :: (Eq a, Eq e, Show a, Show e) => ExceptT e IO a -> e -> Expectation -action `throwsError` e = runExceptT action `shouldReturn` Left e - -- TODO add null port tests storeTests :: Spec storeTests = do withStore2 $ do describe "stress test" testConcurrentWrites withStore $ do - describe "store setup" $ do + describe "db setup" $ do testCompiledThreadsafe testForeignKeysEnabled - describe "store methods" $ do + describe "db methods" $ do describe "Queue and Connection management" $ do describe "createRcvConn" $ do testCreateRcvConn @@ -112,28 +105,29 @@ testConcurrentWrites :: SpecWith (SQLiteStore, SQLiteStore) testConcurrentWrites = it "should complete multiple concurrent write transactions w/t sqlite busy errors" $ \(s1, s2) -> do g <- newTVarIO =<< drgNew - _ <- runExceptT $ createRcvConn s1 g cData1 rcvQueue1 SCMInvitation + _ <- withTransaction s1 $ \db -> + createRcvConn db g cData1 rcvQueue1 SCMInvitation let ConnData {connId} = cData1 concurrently_ (runTest s1 connId) (runTest s2 connId) where - runTest :: SQLiteStore -> ConnId -> IO (Either StoreError ()) - runTest store connId = runExceptT . replicateM_ 100 $ do - (internalId, internalRcvId, _, _) <- updateRcvIds store connId + runTest :: SQLiteStore -> ConnId -> IO () + runTest st connId = replicateM_ 100 . withTransaction st $ \db -> do + (internalId, internalRcvId, _, _) <- updateRcvIds db connId let rcvMsgData = mkRcvMsgData internalId internalRcvId 0 "0" "hash_dummy" - createRcvMsg store connId rcvMsgData + createRcvMsg db connId rcvMsgData testCompiledThreadsafe :: SpecWith SQLiteStore testCompiledThreadsafe = - it "compiled sqlite library should be threadsafe" . withStoreConnection $ \db -> do + it "compiled sqlite library should be threadsafe" . withStoreTransaction $ \db -> do compileOptions <- DB.query_ db "pragma COMPILE_OPTIONS;" :: IO [[T.Text]] compileOptions `shouldNotContain` [["THREADSAFE=0"]] -withStoreConnection :: (DB.Connection -> IO a) -> SQLiteStore -> IO a -withStoreConnection = flip withConnection +withStoreTransaction :: (DB.Connection -> IO a) -> SQLiteStore -> IO a +withStoreTransaction = flip withTransaction testForeignKeysEnabled :: SpecWith SQLiteStore testForeignKeysEnabled = - it "foreign keys should be enabled" . withStoreConnection $ \db -> do + it "foreign keys should be enabled" . withStoreTransaction $ \db -> do let inconsistentQuery = [sql| INSERT INTO snd_queues @@ -186,125 +180,125 @@ sndQueue1 = testCreateRcvConn :: SpecWith SQLiteStore testCreateRcvConn = - it "should create RcvConnection and add SndQueue" $ \store -> do + it "should create RcvConnection and add SndQueue" . withStoreTransaction $ \db -> do g <- newTVarIO =<< drgNew - createRcvConn store g cData1 rcvQueue1 SCMInvitation - `returnsResult` "conn1" - getConn store "conn1" - `returnsResult` SomeConn SCRcv (RcvConnection cData1 rcvQueue1) - upgradeRcvConnToDuplex store "conn1" sndQueue1 - `returnsResult` () - getConn store "conn1" - `returnsResult` SomeConn SCDuplex (DuplexConnection cData1 rcvQueue1 sndQueue1) + createRcvConn db g cData1 rcvQueue1 SCMInvitation + `shouldReturn` Right "conn1" + getConn db "conn1" + `shouldReturn` Right (SomeConn SCRcv (RcvConnection cData1 rcvQueue1)) + upgradeRcvConnToDuplex db "conn1" sndQueue1 + `shouldReturn` Right () + getConn db "conn1" + `shouldReturn` Right (SomeConn SCDuplex (DuplexConnection cData1 rcvQueue1 sndQueue1)) testCreateRcvConnRandomId :: SpecWith SQLiteStore testCreateRcvConnRandomId = - it "should create RcvConnection and add SndQueue with random ID" $ \store -> do + it "should create RcvConnection and add SndQueue with random ID" . withStoreTransaction $ \db -> do g <- newTVarIO =<< drgNew - Right connId <- runExceptT $ createRcvConn store g cData1 {connId = ""} rcvQueue1 SCMInvitation - getConn store connId - `returnsResult` SomeConn SCRcv (RcvConnection cData1 {connId} rcvQueue1) - upgradeRcvConnToDuplex store connId sndQueue1 - `returnsResult` () - getConn store connId - `returnsResult` SomeConn SCDuplex (DuplexConnection cData1 {connId} rcvQueue1 sndQueue1) + Right connId <- createRcvConn db g cData1 {connId = ""} rcvQueue1 SCMInvitation + getConn db connId + `shouldReturn` Right (SomeConn SCRcv (RcvConnection cData1 {connId} rcvQueue1)) + upgradeRcvConnToDuplex db connId sndQueue1 + `shouldReturn` Right () + getConn db connId + `shouldReturn` Right (SomeConn SCDuplex (DuplexConnection cData1 {connId} rcvQueue1 sndQueue1)) testCreateRcvConnDuplicate :: SpecWith SQLiteStore testCreateRcvConnDuplicate = - it "should throw error on attempt to create duplicate RcvConnection" $ \store -> do + it "should throw error on attempt to create duplicate RcvConnection" . withStoreTransaction $ \db -> do g <- newTVarIO =<< drgNew - _ <- runExceptT $ createRcvConn store g cData1 rcvQueue1 SCMInvitation - createRcvConn store g cData1 rcvQueue1 SCMInvitation - `throwsError` SEConnDuplicate + _ <- createRcvConn db g cData1 rcvQueue1 SCMInvitation + createRcvConn db g cData1 rcvQueue1 SCMInvitation + `shouldReturn` Left (SEConnDuplicate) testCreateSndConn :: SpecWith SQLiteStore testCreateSndConn = - it "should create SndConnection and add RcvQueue" $ \store -> do + it "should create SndConnection and add RcvQueue" . withStoreTransaction $ \db -> do g <- newTVarIO =<< drgNew - createSndConn store g cData1 sndQueue1 - `returnsResult` "conn1" - getConn store "conn1" - `returnsResult` SomeConn SCSnd (SndConnection cData1 sndQueue1) - upgradeSndConnToDuplex store "conn1" rcvQueue1 - `returnsResult` () - getConn store "conn1" - `returnsResult` SomeConn SCDuplex (DuplexConnection cData1 rcvQueue1 sndQueue1) + createSndConn db g cData1 sndQueue1 + `shouldReturn` Right "conn1" + getConn db "conn1" + `shouldReturn` Right (SomeConn SCSnd (SndConnection cData1 sndQueue1)) + upgradeSndConnToDuplex db "conn1" rcvQueue1 + `shouldReturn` Right () + getConn db "conn1" + `shouldReturn` Right (SomeConn SCDuplex (DuplexConnection cData1 rcvQueue1 sndQueue1)) testCreateSndConnRandomID :: SpecWith SQLiteStore testCreateSndConnRandomID = - it "should create SndConnection and add RcvQueue with random ID" $ \store -> do + it "should create SndConnection and add RcvQueue with random ID" . withStoreTransaction $ \db -> do g <- newTVarIO =<< drgNew - Right connId <- runExceptT $ createSndConn store g cData1 {connId = ""} sndQueue1 - getConn store connId - `returnsResult` SomeConn SCSnd (SndConnection cData1 {connId} sndQueue1) - upgradeSndConnToDuplex store connId rcvQueue1 - `returnsResult` () - getConn store connId - `returnsResult` SomeConn SCDuplex (DuplexConnection cData1 {connId} rcvQueue1 sndQueue1) + Right connId <- createSndConn db g cData1 {connId = ""} sndQueue1 + getConn db connId + `shouldReturn` Right (SomeConn SCSnd (SndConnection cData1 {connId} sndQueue1)) + upgradeSndConnToDuplex db connId rcvQueue1 + `shouldReturn` Right () + getConn db connId + `shouldReturn` Right (SomeConn SCDuplex (DuplexConnection cData1 {connId} rcvQueue1 sndQueue1)) testCreateSndConnDuplicate :: SpecWith SQLiteStore testCreateSndConnDuplicate = - it "should throw error on attempt to create duplicate SndConnection" $ \store -> do + it "should throw error on attempt to create duplicate SndConnection" . withStoreTransaction $ \db -> do g <- newTVarIO =<< drgNew - _ <- runExceptT $ createSndConn store g cData1 sndQueue1 - createSndConn store g cData1 sndQueue1 - `throwsError` SEConnDuplicate + _ <- createSndConn db g cData1 sndQueue1 + createSndConn db g cData1 sndQueue1 + `shouldReturn` Left (SEConnDuplicate) testGetRcvConn :: SpecWith SQLiteStore testGetRcvConn = - it "should get connection using rcv queue id and server" $ \store -> do + it "should get connection using rcv queue id and server" . withStoreTransaction $ \db -> do let smpServer = SMPServer "smp.simplex.im" "5223" testKeyHash let recipientId = "1234" g <- newTVarIO =<< drgNew - _ <- runExceptT $ createRcvConn store g cData1 rcvQueue1 SCMInvitation - getRcvConn store smpServer recipientId - `returnsResult` SomeConn SCRcv (RcvConnection cData1 rcvQueue1) + _ <- createRcvConn db g cData1 rcvQueue1 SCMInvitation + getRcvConn db smpServer recipientId + `shouldReturn` Right (SomeConn SCRcv (RcvConnection cData1 rcvQueue1)) testDeleteRcvConn :: SpecWith SQLiteStore testDeleteRcvConn = - it "should create RcvConnection and delete it" $ \store -> do + it "should create RcvConnection and delete it" . withStoreTransaction $ \db -> do g <- newTVarIO =<< drgNew - _ <- runExceptT $ createRcvConn store g cData1 rcvQueue1 SCMInvitation - getConn store "conn1" - `returnsResult` SomeConn SCRcv (RcvConnection cData1 rcvQueue1) - deleteConn store "conn1" - `returnsResult` () + _ <- createRcvConn db g cData1 rcvQueue1 SCMInvitation + getConn db "conn1" + `shouldReturn` Right (SomeConn SCRcv (RcvConnection cData1 rcvQueue1)) + deleteConn db "conn1" + `shouldReturn` () -- TODO check queues are deleted as well - getConn store "conn1" - `throwsError` SEConnNotFound + getConn db "conn1" + `shouldReturn` Left (SEConnNotFound) testDeleteSndConn :: SpecWith SQLiteStore testDeleteSndConn = - it "should create SndConnection and delete it" $ \store -> do + it "should create SndConnection and delete it" . withStoreTransaction $ \db -> do g <- newTVarIO =<< drgNew - _ <- runExceptT $ createSndConn store g cData1 sndQueue1 - getConn store "conn1" - `returnsResult` SomeConn SCSnd (SndConnection cData1 sndQueue1) - deleteConn store "conn1" - `returnsResult` () + _ <- createSndConn db g cData1 sndQueue1 + getConn db "conn1" + `shouldReturn` Right (SomeConn SCSnd (SndConnection cData1 sndQueue1)) + deleteConn db "conn1" + `shouldReturn` () -- TODO check queues are deleted as well - getConn store "conn1" - `throwsError` SEConnNotFound + getConn db "conn1" + `shouldReturn` Left (SEConnNotFound) testDeleteDuplexConn :: SpecWith SQLiteStore testDeleteDuplexConn = - it "should create DuplexConnection and delete it" $ \store -> do + it "should create DuplexConnection and delete it" . withStoreTransaction $ \db -> do g <- newTVarIO =<< drgNew - _ <- runExceptT $ createRcvConn store g cData1 rcvQueue1 SCMInvitation - _ <- runExceptT $ upgradeRcvConnToDuplex store "conn1" sndQueue1 - getConn store "conn1" - `returnsResult` SomeConn SCDuplex (DuplexConnection cData1 rcvQueue1 sndQueue1) - deleteConn store "conn1" - `returnsResult` () + _ <- createRcvConn db g cData1 rcvQueue1 SCMInvitation + _ <- upgradeRcvConnToDuplex db "conn1" sndQueue1 + getConn db "conn1" + `shouldReturn` Right (SomeConn SCDuplex (DuplexConnection cData1 rcvQueue1 sndQueue1)) + deleteConn db "conn1" + `shouldReturn` () -- TODO check queues are deleted as well - getConn store "conn1" - `throwsError` SEConnNotFound + getConn db "conn1" + `shouldReturn` Left (SEConnNotFound) testUpgradeRcvConnToDuplex :: SpecWith SQLiteStore testUpgradeRcvConnToDuplex = - it "should throw error on attempt to add SndQueue to SndConnection or DuplexConnection" $ \store -> do + it "should throw error on attempt to add SndQueue to SndConnection or DuplexConnection" . withStoreTransaction $ \db -> do g <- newTVarIO =<< drgNew - _ <- runExceptT $ createSndConn store g cData1 sndQueue1 + _ <- createSndConn db g cData1 sndQueue1 let anotherSndQueue = SndQueue { server = SMPServer "smp.simplex.im" "5223" testKeyHash, @@ -315,17 +309,17 @@ testUpgradeRcvConnToDuplex = e2eDhSecret = testDhSecret, status = New } - upgradeRcvConnToDuplex store "conn1" anotherSndQueue - `throwsError` SEBadConnType CSnd - _ <- runExceptT $ upgradeSndConnToDuplex store "conn1" rcvQueue1 - upgradeRcvConnToDuplex store "conn1" anotherSndQueue - `throwsError` SEBadConnType CDuplex + upgradeRcvConnToDuplex db "conn1" anotherSndQueue + `shouldReturn` Left (SEBadConnType CSnd) + _ <- upgradeSndConnToDuplex db "conn1" rcvQueue1 + upgradeRcvConnToDuplex db "conn1" anotherSndQueue + `shouldReturn` Left (SEBadConnType CDuplex) testUpgradeSndConnToDuplex :: SpecWith SQLiteStore testUpgradeSndConnToDuplex = - it "should throw error on attempt to add RcvQueue to RcvConnection or DuplexConnection" $ \store -> do + it "should throw error on attempt to add RcvQueue to RcvConnection or DuplexConnection" . withStoreTransaction $ \db -> do g <- newTVarIO =<< drgNew - _ <- runExceptT $ createRcvConn store g cData1 rcvQueue1 SCMInvitation + _ <- createRcvConn db g cData1 rcvQueue1 SCMInvitation let anotherRcvQueue = RcvQueue { server = SMPServer "smp.simplex.im" "5223" testKeyHash, @@ -340,52 +334,52 @@ testUpgradeSndConnToDuplex = ntfPrivateKey = Nothing, notifierId = Nothing } - upgradeSndConnToDuplex store "conn1" anotherRcvQueue - `throwsError` SEBadConnType CRcv - _ <- runExceptT $ upgradeRcvConnToDuplex store "conn1" sndQueue1 - upgradeSndConnToDuplex store "conn1" anotherRcvQueue - `throwsError` SEBadConnType CDuplex + upgradeSndConnToDuplex db "conn1" anotherRcvQueue + `shouldReturn` Left (SEBadConnType CRcv) + _ <- upgradeRcvConnToDuplex db "conn1" sndQueue1 + upgradeSndConnToDuplex db "conn1" anotherRcvQueue + `shouldReturn` Left (SEBadConnType CDuplex) testSetRcvQueueStatus :: SpecWith SQLiteStore testSetRcvQueueStatus = - it "should update status of RcvQueue" $ \store -> do + it "should update status of RcvQueue" . withStoreTransaction $ \db -> do g <- newTVarIO =<< drgNew - _ <- runExceptT $ createRcvConn store g cData1 rcvQueue1 SCMInvitation - getConn store "conn1" - `returnsResult` SomeConn SCRcv (RcvConnection cData1 rcvQueue1) - setRcvQueueStatus store rcvQueue1 Confirmed - `returnsResult` () - getConn store "conn1" - `returnsResult` SomeConn SCRcv (RcvConnection cData1 rcvQueue1 {status = Confirmed}) + _ <- createRcvConn db g cData1 rcvQueue1 SCMInvitation + getConn db "conn1" + `shouldReturn` Right (SomeConn SCRcv (RcvConnection cData1 rcvQueue1)) + setRcvQueueStatus db rcvQueue1 Confirmed + `shouldReturn` () + getConn db "conn1" + `shouldReturn` Right (SomeConn SCRcv (RcvConnection cData1 rcvQueue1 {status = Confirmed})) testSetSndQueueStatus :: SpecWith SQLiteStore testSetSndQueueStatus = - it "should update status of SndQueue" $ \store -> do + it "should update status of SndQueue" . withStoreTransaction $ \db -> do g <- newTVarIO =<< drgNew - _ <- runExceptT $ createSndConn store g cData1 sndQueue1 - getConn store "conn1" - `returnsResult` SomeConn SCSnd (SndConnection cData1 sndQueue1) - setSndQueueStatus store sndQueue1 Confirmed - `returnsResult` () - getConn store "conn1" - `returnsResult` SomeConn SCSnd (SndConnection cData1 sndQueue1 {status = Confirmed}) + _ <- createSndConn db g cData1 sndQueue1 + getConn db "conn1" + `shouldReturn` Right (SomeConn SCSnd (SndConnection cData1 sndQueue1)) + setSndQueueStatus db sndQueue1 Confirmed + `shouldReturn` () + getConn db "conn1" + `shouldReturn` Right (SomeConn SCSnd (SndConnection cData1 sndQueue1 {status = Confirmed})) testSetQueueStatusDuplex :: SpecWith SQLiteStore testSetQueueStatusDuplex = - it "should update statuses of RcvQueue and SndQueue in DuplexConnection" $ \store -> do + it "should update statuses of RcvQueue and SndQueue in DuplexConnection" . withStoreTransaction $ \db -> do g <- newTVarIO =<< drgNew - _ <- runExceptT $ createRcvConn store g cData1 rcvQueue1 SCMInvitation - _ <- runExceptT $ upgradeRcvConnToDuplex store "conn1" sndQueue1 - getConn store "conn1" - `returnsResult` SomeConn SCDuplex (DuplexConnection cData1 rcvQueue1 sndQueue1) - setRcvQueueStatus store rcvQueue1 Secured - `returnsResult` () - getConn store "conn1" - `returnsResult` SomeConn SCDuplex (DuplexConnection cData1 rcvQueue1 {status = Secured} sndQueue1) - setSndQueueStatus store sndQueue1 Confirmed - `returnsResult` () - getConn store "conn1" - `returnsResult` SomeConn SCDuplex (DuplexConnection cData1 rcvQueue1 {status = Secured} sndQueue1 {status = Confirmed}) + _ <- createRcvConn db g cData1 rcvQueue1 SCMInvitation + _ <- upgradeRcvConnToDuplex db "conn1" sndQueue1 + getConn db "conn1" + `shouldReturn` Right (SomeConn SCDuplex (DuplexConnection cData1 rcvQueue1 sndQueue1)) + setRcvQueueStatus db rcvQueue1 Secured + `shouldReturn` () + getConn db "conn1" + `shouldReturn` Right (SomeConn SCDuplex (DuplexConnection cData1 rcvQueue1 {status = Secured} sndQueue1)) + setSndQueueStatus db sndQueue1 Confirmed + `shouldReturn` () + getConn db "conn1" + `shouldReturn` Right (SomeConn SCDuplex (DuplexConnection cData1 rcvQueue1 {status = Secured} sndQueue1 {status = Confirmed})) hw :: ByteString hw = encodeUtf8 "Hello world!" @@ -411,22 +405,24 @@ mkRcvMsgData internalId internalRcvId externalSndId brokerId internalHash = externalPrevSndHash = "hash_from_sender" } -testCreateRcvMsg_ :: SQLiteStore -> PrevExternalSndId -> PrevRcvMsgHash -> ConnId -> RcvMsgData -> Expectation -testCreateRcvMsg_ st expectedPrevSndId expectedPrevHash connId rcvMsgData@RcvMsgData {..} = do +testCreateRcvMsg_ :: DB.Connection -> PrevExternalSndId -> PrevRcvMsgHash -> ConnId -> RcvMsgData -> Expectation +testCreateRcvMsg_ db expectedPrevSndId expectedPrevHash connId rcvMsgData@RcvMsgData {..} = do let MsgMeta {recipient = (internalId, _)} = msgMeta - updateRcvIds st connId - `returnsResult` (InternalId internalId, internalRcvId, expectedPrevSndId, expectedPrevHash) - createRcvMsg st connId rcvMsgData - `returnsResult` () + updateRcvIds db connId + `shouldReturn` (InternalId internalId, internalRcvId, expectedPrevSndId, expectedPrevHash) + createRcvMsg db connId rcvMsgData + `shouldReturn` () testCreateRcvMsg :: SpecWith SQLiteStore testCreateRcvMsg = it "should reserve internal ids and create a RcvMsg" $ \st -> do g <- newTVarIO =<< drgNew let ConnData {connId} = cData1 - _ <- runExceptT $ createRcvConn st g cData1 rcvQueue1 SCMInvitation - testCreateRcvMsg_ st 0 "" connId $ mkRcvMsgData (InternalId 1) (InternalRcvId 1) 1 "1" "hash_dummy" - testCreateRcvMsg_ st 1 "hash_dummy" connId $ mkRcvMsgData (InternalId 2) (InternalRcvId 2) 2 "2" "new_hash_dummy" + _ <- withTransaction st $ \db -> do + createRcvConn db g cData1 rcvQueue1 SCMInvitation + withTransaction st $ \db -> do + testCreateRcvMsg_ db 0 "" connId $ mkRcvMsgData (InternalId 1) (InternalRcvId 1) 1 "1" "hash_dummy" + testCreateRcvMsg_ db 1 "hash_dummy" connId $ mkRcvMsgData (InternalId 2) (InternalRcvId 2) 2 "2" "new_hash_dummy" mkSndMsgData :: InternalId -> InternalSndId -> MsgHash -> SndMsgData mkSndMsgData internalId internalSndId internalHash = @@ -441,32 +437,36 @@ mkSndMsgData internalId internalSndId internalHash = prevMsgHash = internalHash } -testCreateSndMsg_ :: SQLiteStore -> PrevSndMsgHash -> ConnId -> SndMsgData -> Expectation -testCreateSndMsg_ store expectedPrevHash connId sndMsgData@SndMsgData {..} = do - updateSndIds store connId - `returnsResult` (internalId, internalSndId, expectedPrevHash) - createSndMsg store connId sndMsgData - `returnsResult` () +testCreateSndMsg_ :: DB.Connection -> PrevSndMsgHash -> ConnId -> SndMsgData -> Expectation +testCreateSndMsg_ db expectedPrevHash connId sndMsgData@SndMsgData {..} = do + updateSndIds db connId + `shouldReturn` (internalId, internalSndId, expectedPrevHash) + createSndMsg db connId sndMsgData + `shouldReturn` () testCreateSndMsg :: SpecWith SQLiteStore testCreateSndMsg = - it "should create a SndMsg and return InternalId and PrevSndMsgHash" $ \store -> do + it "should create a SndMsg and return InternalId and PrevSndMsgHash" $ \st -> do g <- newTVarIO =<< drgNew let ConnData {connId} = cData1 - _ <- runExceptT $ createSndConn store g cData1 sndQueue1 - testCreateSndMsg_ store "" connId $ mkSndMsgData (InternalId 1) (InternalSndId 1) "hash_dummy" - testCreateSndMsg_ store "hash_dummy" connId $ mkSndMsgData (InternalId 2) (InternalSndId 2) "new_hash_dummy" + _ <- withTransaction st $ \db -> do + createSndConn db g cData1 sndQueue1 + withTransaction st $ \db -> do + testCreateSndMsg_ db "" connId $ mkSndMsgData (InternalId 1) (InternalSndId 1) "hash_dummy" + testCreateSndMsg_ db "hash_dummy" connId $ mkSndMsgData (InternalId 2) (InternalSndId 2) "new_hash_dummy" testCreateRcvAndSndMsgs :: SpecWith SQLiteStore testCreateRcvAndSndMsgs = - it "should create multiple RcvMsg and SndMsg, correctly ordering internal Ids and returning previous state" $ \store -> do - g <- newTVarIO =<< drgNew + it "should create multiple RcvMsg and SndMsg, correctly ordering internal Ids and returning previous state" $ \st -> do let ConnData {connId} = cData1 - _ <- runExceptT $ createRcvConn store g cData1 rcvQueue1 SCMInvitation - _ <- runExceptT $ upgradeRcvConnToDuplex store "conn1" sndQueue1 - testCreateRcvMsg_ store 0 "" connId $ mkRcvMsgData (InternalId 1) (InternalRcvId 1) 1 "1" "rcv_hash_1" - testCreateRcvMsg_ store 1 "rcv_hash_1" connId $ mkRcvMsgData (InternalId 2) (InternalRcvId 2) 2 "2" "rcv_hash_2" - testCreateSndMsg_ store "" connId $ mkSndMsgData (InternalId 3) (InternalSndId 1) "snd_hash_1" - testCreateRcvMsg_ store 2 "rcv_hash_2" connId $ mkRcvMsgData (InternalId 4) (InternalRcvId 3) 3 "3" "rcv_hash_3" - testCreateSndMsg_ store "snd_hash_1" connId $ mkSndMsgData (InternalId 5) (InternalSndId 2) "snd_hash_2" - testCreateSndMsg_ store "snd_hash_2" connId $ mkSndMsgData (InternalId 6) (InternalSndId 3) "snd_hash_3" + _ <- withTransaction st $ \db -> do + g <- newTVarIO =<< drgNew + createRcvConn db g cData1 rcvQueue1 SCMInvitation + withTransaction st $ \db -> do + _ <- upgradeRcvConnToDuplex db "conn1" sndQueue1 + testCreateRcvMsg_ db 0 "" connId $ mkRcvMsgData (InternalId 1) (InternalRcvId 1) 1 "1" "rcv_hash_1" + testCreateRcvMsg_ db 1 "rcv_hash_1" connId $ mkRcvMsgData (InternalId 2) (InternalRcvId 2) 2 "2" "rcv_hash_2" + testCreateSndMsg_ db "" connId $ mkSndMsgData (InternalId 3) (InternalSndId 1) "snd_hash_1" + testCreateRcvMsg_ db 2 "rcv_hash_2" connId $ mkRcvMsgData (InternalId 4) (InternalRcvId 3) 3 "3" "rcv_hash_3" + testCreateSndMsg_ db "snd_hash_1" connId $ mkSndMsgData (InternalId 5) (InternalSndId 2) "snd_hash_2" + testCreateSndMsg_ db "snd_hash_2" connId $ mkSndMsgData (InternalId 6) (InternalSndId 3) "snd_hash_3" From 0ab44b18362114752e529405da33b85f091495ec Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Sun, 19 Jun 2022 13:23:35 +0100 Subject: [PATCH 067/131] get multiple messages when notification is processed (#411) * get multiple messages when notification is processed * change notification property --- src/Simplex/Messaging/Agent.hs | 42 ++++++++++++------- src/Simplex/Messaging/Agent/Client.hs | 4 +- src/Simplex/Messaging/Agent/Env/SQLite.hs | 2 + src/Simplex/Messaging/Agent/Protocol.hs | 14 +++++++ src/Simplex/Messaging/Client.hs | 6 +-- .../Notifications/Server/Push/APNS.hs | 4 +- src/Simplex/Messaging/Protocol.hs | 15 ++++++- tests/AgentTests/NotificationTests.hs | 18 ++++---- tests/NtfServerTests.hs | 4 +- 9 files changed, 74 insertions(+), 35 deletions(-) diff --git a/src/Simplex/Messaging/Agent.hs b/src/Simplex/Messaging/Agent.hs index 12441a15d..8ea17b5c1 100644 --- a/src/Simplex/Messaging/Agent.hs +++ b/src/Simplex/Messaging/Agent.hs @@ -98,10 +98,10 @@ import Simplex.Messaging.Notifications.Client import Simplex.Messaging.Notifications.Protocol (DeviceToken, NtfRegCode (NtfRegCode), NtfTknStatus (..)) import Simplex.Messaging.Notifications.Server.Push.APNS (PNMessageData (..)) import Simplex.Messaging.Parsers (parse) -import Simplex.Messaging.Protocol (BrokerMsg, ErrorType (AUTH), MsgBody, MsgFlags, NMsgMeta (..)) +import Simplex.Messaging.Protocol (BrokerMsg, ErrorType (AUTH), MsgBody, MsgFlags, SMPMsgMeta) import qualified Simplex.Messaging.Protocol as SMP import qualified Simplex.Messaging.TMap as TM -import Simplex.Messaging.Util (bshow, liftE, liftError, tryError, unlessM, whenM, ($>>=)) +import Simplex.Messaging.Util (bshow, eitherToMaybe, liftE, liftError, tryError, unlessM, whenM, ($>>=)) import Simplex.Messaging.Version import System.Random (randomR) import UnliftIO.Async (async, race_) @@ -155,11 +155,11 @@ subscribeConnection :: AgentErrorMonad m => AgentClient -> ConnId -> m () subscribeConnection c = withAgentEnv c . subscribeConnection' c -- | Get connection message (GET command) -getConnectionMessage :: AgentErrorMonad m => AgentClient -> ConnId -> m (Maybe (SMP.MsgId, MsgFlags)) +getConnectionMessage :: AgentErrorMonad m => AgentClient -> ConnId -> m (Maybe SMPMsgMeta) getConnectionMessage c = withAgentEnv c . getConnectionMessage' c -- | Get connection message for received notification -getNotificationMessage :: AgentErrorMonad m => AgentClient -> ByteString -> C.CbNonce -> m (Maybe (SMP.MsgId, MsgFlags)) +getNotificationMessage :: AgentErrorMonad m => AgentClient -> C.CbNonce -> ByteString -> m (NotificationInfo, [SMPMsgMeta]) getNotificationMessage c = withAgentEnv c .: getNotificationMessage' c resubscribeConnection :: AgentErrorMonad m => AgentClient -> ConnId -> m () @@ -386,7 +386,7 @@ resubscribeConnection' c connId = (atomically $ hasActiveSubscription c connId) (subscribeConnection' c connId) -getConnectionMessage' :: AgentMonad m => AgentClient -> ConnId -> m (Maybe (SMP.MsgId, MsgFlags)) +getConnectionMessage' :: AgentMonad m => AgentClient -> ConnId -> m (Maybe SMPMsgMeta) getConnectionMessage' c connId = do whenM (atomically $ hasActiveSubscription c connId) . throwError $ CMD PROHIBITED withStore c (`getConn` connId) >>= \case @@ -395,20 +395,32 @@ getConnectionMessage' c connId = do SomeConn _ ContactConnection {} -> throwError $ CMD PROHIBITED SomeConn _ SndConnection {} -> throwError $ CONN SIMPLEX -getNotificationMessage' :: forall m. AgentMonad m => AgentClient -> ByteString -> C.CbNonce -> m (Maybe (SMP.MsgId, MsgFlags)) -getNotificationMessage' c encMessageInfo nonce = do +getNotificationMessage' :: forall m. AgentMonad m => AgentClient -> C.CbNonce -> ByteString -> m (NotificationInfo, [SMPMsgMeta]) +getNotificationMessage' c nonce encNtfInfo = do withStore' c getActiveNtfToken >>= \case Just NtfToken {ntfDhSecret = Just dhSecret} -> do - ntfData <- agentCbDecrypt dhSecret nonce encMessageInfo + ntfData <- agentCbDecrypt dhSecret nonce encNtfInfo PNMessageData {smpQueue, ntfTs, nmsgNonce, encNMsgMeta} <- liftEither (parse strP (INTERNAL "error parsing PNMessageData") ntfData) - (connId, rcvDhSecret) <- withStore c (`getNtfRcvQueue` smpQueue) - nMsgMeta <- agentCbDecrypt rcvDhSecret nmsgNonce encNMsgMeta `catchError` const (pure "") - let nMsgMetaParsed = parse smpP (INTERNAL "error parsing NMsgMeta") nMsgMeta - case nMsgMetaParsed of - Right NMsgMeta {msgId, msgTs} -> liftIO . print $ "getNotificationMessage', ntfTs = " <> show ntfTs <> ", msgId = " <> show msgId <> ", msgTs = " <> show msgTs - Left _ -> liftIO . print $ "getNotificationMessage', ntfTs = " <> show ntfTs <> ", failed to parse NMsgMeta" - getConnectionMessage' c connId + (ntfConnId, rcvDhSecret) <- withStore c (`getNtfRcvQueue` smpQueue) + ntfMsgMeta <- (eitherToMaybe . smpDecode <$> agentCbDecrypt rcvDhSecret nmsgNonce encNMsgMeta) `catchError` \_ -> pure Nothing + maxMsgs <- asks $ ntfMaxMessages . config + (NotificationInfo {ntfConnId, ntfTs, ntfMsgMeta},) <$> getNtfMessages ntfConnId maxMsgs ntfMsgMeta [] _ -> throwError $ CMD PROHIBITED + where + getNtfMessages ntfConnId maxMs nMeta ms + | length ms < maxMs = + getConnectionMessage' c ntfConnId >>= \case + Just m@SMP.SMPMsgMeta {msgId, msgTs, msgFlags} -> case nMeta of + Just SMP.NMsgMeta {msgId = msgId', msgTs = msgTs'} + | msgId == msgId' || msgTs > msgTs' -> pure $ reverse (m : ms) + | otherwise -> getMsg (m : ms) + _ + | SMP.notification msgFlags -> pure $ reverse (m : ms) + | otherwise -> getMsg (m : ms) + _ -> pure $ reverse ms + | otherwise = pure $ reverse ms + where + getMsg = getNtfMessages ntfConnId maxMs nMeta -- | Send message to the connection (SEND command) in Reader monad sendMessage' :: forall m. AgentMonad m => AgentClient -> ConnId -> MsgFlags -> MsgBody -> m AgentMsgId diff --git a/src/Simplex/Messaging/Agent/Client.hs b/src/Simplex/Messaging/Agent/Client.hs index 6248cd321..f69813da5 100644 --- a/src/Simplex/Messaging/Agent/Client.hs +++ b/src/Simplex/Messaging/Agent/Client.hs @@ -85,7 +85,7 @@ import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Encoding import Simplex.Messaging.Notifications.Client import Simplex.Messaging.Notifications.Protocol -import Simplex.Messaging.Protocol (BrokerMsg, ErrorType, MsgFlags (..), MsgId, NotifierId, NtfPrivateSignKey, NtfPublicVerifyKey, ProtocolServer (..), QueueId, QueueIdsKeys (..), SndPublicVerifyKey) +import Simplex.Messaging.Protocol (BrokerMsg, ErrorType, MsgFlags (..), MsgId, NotifierId, NtfPrivateSignKey, NtfPublicVerifyKey, ProtocolServer (..), QueueId, QueueIdsKeys (..), SMPMsgMeta, SndPublicVerifyKey) import qualified Simplex.Messaging.Protocol as SMP import Simplex.Messaging.TMap (TMap) import qualified Simplex.Messaging.TMap as TM @@ -508,7 +508,7 @@ sendInvitation c (Compatible SMPQueueInfo {smpServer, senderId, dhPublicKey}) co agentCbEncryptOnce dhPublicKey . smpEncode $ SMP.ClientMessage SMP.PHEmpty $ smpEncode agentEnvelope -getQueueMessage :: AgentMonad m => AgentClient -> RcvQueue -> ConnId -> m (Maybe (MsgId, MsgFlags)) +getQueueMessage :: AgentMonad m => AgentClient -> RcvQueue -> ConnId -> m (Maybe SMPMsgMeta) getQueueMessage c@AgentClient {getMsgLocks} RcvQueue {server, rcvId, rcvPrivateKey} connId = E.bracket (atomically createTakeLock) (atomically . (`putTMVar` ())) $ \_ -> withLogClient c server rcvId "GET" $ \smp -> diff --git a/src/Simplex/Messaging/Agent/Env/SQLite.hs b/src/Simplex/Messaging/Agent/Env/SQLite.hs index ffd5af8b7..de3c01b33 100644 --- a/src/Simplex/Messaging/Agent/Env/SQLite.hs +++ b/src/Simplex/Messaging/Agent/Env/SQLite.hs @@ -71,6 +71,7 @@ data AgentConfig = AgentConfig resubscriptionConcurrency :: Int, ntfWorkerThrottle :: Int, ntfSubCheckInterval :: NominalDiffTime, + ntfMaxMessages :: Int, caCertificateFile :: FilePath, privateKeyFile :: FilePath, certificateFile :: FilePath, @@ -104,6 +105,7 @@ defaultAgentConfig = resubscriptionConcurrency = 16, ntfWorkerThrottle = 1000000, -- microseconds ntfSubCheckInterval = nominalDay, + ntfMaxMessages = 4, -- CA certificate private key is not needed for initialization -- ! we do not generate these caCertificateFile = "/etc/opt/simplex-agent/ca.crt", diff --git a/src/Simplex/Messaging/Agent/Protocol.hs b/src/Simplex/Messaging/Agent/Protocol.hs index 4425c83b0..f445f2c03 100644 --- a/src/Simplex/Messaging/Agent/Protocol.hs +++ b/src/Simplex/Messaging/Agent/Protocol.hs @@ -83,6 +83,7 @@ module Simplex.Messaging.Agent.Protocol ACorrId, AgentMsgId, AgentPhase (..), + NotificationInfo (..), -- * Encode/decode serializeCommand, @@ -119,6 +120,7 @@ import qualified Data.List.NonEmpty as L import Data.Maybe (isJust) import Data.Text (Text) import Data.Time.Clock (UTCTime) +import Data.Time.Clock.System (SystemTime) import Data.Time.ISO8601 import Data.Type.Equality import Data.Typeable () @@ -135,6 +137,7 @@ import Simplex.Messaging.Protocol MsgBody, MsgFlags, MsgId, + NMsgMeta, SMPServer, SndPublicVerifyKey, SrvLoc (..), @@ -252,6 +255,17 @@ instance StrEncoding AgentPhase where "SUSPENDED" -> pure APSuspended _ -> fail "bad AgentPhase" +instance ToJSON AgentPhase where + toEncoding = strToJEncoding + toJSON = strToJSON + +data NotificationInfo = NotificationInfo + { ntfConnId :: ConnId, + ntfTs :: SystemTime, + ntfMsgMeta :: Maybe NMsgMeta + } + deriving (Show) + data ConnectionMode = CMInvitation | CMContact deriving (Eq, Show) diff --git a/src/Simplex/Messaging/Client.hs b/src/Simplex/Messaging/Client.hs index 92d749091..33bc01529 100644 --- a/src/Simplex/Messaging/Client.hs +++ b/src/Simplex/Messaging/Client.hs @@ -291,13 +291,13 @@ subscribeSMPQueue c@ProtocolClient {protocolServer, sessionId, msgQ} rpKey rId = -- | Get message from SMP queue. The server returns ERR PROHIBITED if a client uses SUB and GET via the same transport connection for the same queue -- -- https://github.covm/simplex-chat/simplexmq/blob/master/protocol/simplex-messaging.md#receive-a-message-from-the-queue -getSMPMessage :: SMPClient -> RcvPrivateSignKey -> RecipientId -> ExceptT ProtocolClientError IO (Maybe (MsgId, MsgFlags)) +getSMPMessage :: SMPClient -> RcvPrivateSignKey -> RecipientId -> ExceptT ProtocolClientError IO (Maybe SMP.SMPMsgMeta) getSMPMessage c@ProtocolClient {protocolServer, sessionId, msgQ} rpKey rId = sendSMPCommand c (Just rpKey) rId GET >>= \case OK -> pure Nothing - cmd@(MSG msgId _ msgFlags _) -> do + cmd@(MSG msgId msgTs msgFlags _) -> do lift . atomically $ mapM_ (`writeTBQueue` (protocolServer, sessionId, rId, cmd)) msgQ - pure $ Just (msgId, msgFlags) + pure $ Just SMP.SMPMsgMeta {msgId, msgTs, msgFlags} _ -> throwE PCEUnexpectedResponse -- | Subscribe to the SMP queue notifications. diff --git a/src/Simplex/Messaging/Notifications/Server/Push/APNS.hs b/src/Simplex/Messaging/Notifications/Server/Push/APNS.hs index 42c669c95..b2e1e2a45 100644 --- a/src/Simplex/Messaging/Notifications/Server/Push/APNS.hs +++ b/src/Simplex/Messaging/Notifications/Server/Push/APNS.hs @@ -272,10 +272,10 @@ apnsNotification :: NtfTknData -> C.CbNonce -> Int -> PushNotification -> Either apnsNotification NtfTknData {tknDhSecret} nonce paddedLen = \case PNVerification (NtfRegCode code) -> encrypt code $ \code' -> - apn APNSBackground {contentAvailable = 1} . Just $ J.object ["verification" .= code', "nonce" .= nonce] + apn APNSBackground {contentAvailable = 1} . Just $ J.object ["nonce" .= nonce, "verification" .= code'] PNMessage pnMessageData -> encrypt (strEncode pnMessageData) $ \ntfData -> - apn apnMutableContent . Just $ J.object ["checkMessage" .= ntfData, "nonce" .= nonce] + apn apnMutableContent . Just $ J.object ["nonce" .= nonce, "message" .= ntfData] PNAlert text -> Right $ apn (apnAlert $ APNSAlertText text) Nothing PNCheckMessages -> Right $ apn APNSBackground {contentAvailable = 1} . Just $ J.object ["checkMessages" .= True] where diff --git a/src/Simplex/Messaging/Protocol.hs b/src/Simplex/Messaging/Protocol.hs index dfdb6c52d..79c3e74c0 100644 --- a/src/Simplex/Messaging/Protocol.hs +++ b/src/Simplex/Messaging/Protocol.hs @@ -79,6 +79,7 @@ module Simplex.Messaging.Protocol MsgId, MsgBody, EncNMsgMeta, + SMPMsgMeta (..), NMsgMeta (..), MsgFlags (..), noMsgFlags, @@ -252,10 +253,18 @@ data BrokerMsg where type EncNMsgMeta = ByteString +data SMPMsgMeta = SMPMsgMeta + { msgId :: MsgId, + msgTs :: SystemTime, + msgFlags :: MsgFlags + } + deriving (Show) + data NMsgMeta = NMsgMeta { msgId :: MsgId, msgTs :: SystemTime } + deriving (Show) instance Encoding NMsgMeta where smpEncode NMsgMeta {msgId, msgTs} = @@ -265,8 +274,10 @@ instance Encoding NMsgMeta where (msgId, msgTs, Tail _) <- smpP pure NMsgMeta {msgId, msgTs} -newtype MsgFlags = MsgFlags {notification :: Bool} - deriving (Eq, Show) +data MsgFlags = MsgFlags {notification :: Bool} + deriving (Eq, Show, Generic) + +instance ToJSON MsgFlags where toEncoding = J.genericToEncoding J.defaultOptions instance Encoding MsgFlags where smpEncode MsgFlags {notification} = smpEncode notification diff --git a/tests/AgentTests/NotificationTests.hs b/tests/AgentTests/NotificationTests.hs index 03bc0af9b..3666f007f 100644 --- a/tests/AgentTests/NotificationTests.hs +++ b/tests/AgentTests/NotificationTests.hs @@ -27,7 +27,7 @@ import Simplex.Messaging.Agent.Protocol import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Notifications.Protocol import Simplex.Messaging.Notifications.Server.Push.APNS -import Simplex.Messaging.Protocol (ErrorType (AUTH), MsgFlags (MsgFlags)) +import Simplex.Messaging.Protocol (ErrorType (AUTH), MsgFlags (MsgFlags), SMPMsgMeta (..)) import qualified Simplex.Messaging.Protocol as SMP import Simplex.Messaging.Transport (ATransport) import Simplex.Messaging.Util (tryE) @@ -189,7 +189,7 @@ testNotificationSubscriptionExistingConnection :: APNSMockServer -> IO () testNotificationSubscriptionExistingConnection APNSMockServer {apnsQ} = do alice <- getSMPAgentClient agentCfg initAgentServers bob <- getSMPAgentClient agentCfg {dbFile = testDB2} initAgentServers - Right (bobId, checkMessage, nonce) <- runExceptT $ do + Right (bobId, nonce, message) <- runExceptT $ do -- establish connection (bobId, qInfo) <- createConnection alice SCMInvitation aliceId <- joinConnection bob qInfo "bob's connInfo" @@ -213,16 +213,16 @@ testNotificationSubscriptionExistingConnection APNSMockServer {apnsQ} = do 1 <- msgId <$> sendMessage bob aliceId (SMP.MsgFlags True) "hello" get bob ##> ("", aliceId, SENT $ baseId + 1) -- notification - (checkMessage, nonce) <- messageNotification apnsQ - pure (bobId, checkMessage, nonce) + (nonce, message) <- messageNotification apnsQ + pure (bobId, nonce, message) -- alice client already has subscription for the connection - Left (CMD PROHIBITED) <- runExceptT $ getNotificationMessage alice checkMessage nonce + Left (CMD PROHIBITED) <- runExceptT $ getNotificationMessage alice nonce message -- aliceNtf client doesn't have subscription and is allowed to get notification message aliceNtf <- getSMPAgentClient agentCfg initAgentServers Right () <- runExceptT $ do - Just (_msgId, MsgFlags True) <- getNotificationMessage aliceNtf checkMessage nonce + (_, [SMPMsgMeta {msgFlags = MsgFlags True}]) <- getNotificationMessage aliceNtf nonce message pure () disconnectAgentClient aliceNtf @@ -299,13 +299,13 @@ testNotificationSubscriptionNewConnection APNSMockServer {apnsQ} = do baseId = 3 msgId = subtract baseId -messageNotification :: TBQueue APNSMockRequest -> ExceptT AgentErrorType IO (ByteString, C.CbNonce) +messageNotification :: TBQueue APNSMockRequest -> ExceptT AgentErrorType IO (C.CbNonce, ByteString) messageNotification apnsQ = do 500000 `timeout` atomically (readTBQueue apnsQ) >>= \case Nothing -> error "no notification" Just APNSMockRequest {notification = APNSNotification {aps = APNSMutableContent {}, notificationData = Just ntfData}, sendApnsResponse} -> do - checkMessage <- ntfData .-> "checkMessage" nonce <- C.cbNonce <$> ntfData .-> "nonce" + message <- ntfData .-> "message" liftIO $ sendApnsResponse APNSRespOk - pure (checkMessage, nonce) + pure (nonce, message) _ -> error "bad notification" diff --git a/tests/NtfServerTests.hs b/tests/NtfServerTests.hs index f12bc35c1..f7bf3ea27 100644 --- a/tests/NtfServerTests.hs +++ b/tests/NtfServerTests.hs @@ -118,9 +118,9 @@ testNotificationSubscription (ATransport t) = -- receive notification APNSMockRequest {notification, sendApnsResponse = send'} <- atomically $ readTBQueue apnsQ let APNSNotification {aps = APNSMutableContent {}, notificationData = Just ntfData'} = notification - Right checkMessage = ntfData' .-> "checkMessage" Right nonce' = C.cbNonce <$> ntfData' .-> "nonce" - Right ntfDataDecrypted = C.cbDecrypt dhSecret nonce' checkMessage + Right message = ntfData' .-> "message" + Right ntfDataDecrypted = C.cbDecrypt dhSecret nonce' message Right APNS.PNMessageData {smpQueue = SMPQueueNtf {smpServer, notifierId}, nmsgNonce, encNMsgMeta} = parse strP (AP.INTERNAL "error parsing PNMessageData") ntfDataDecrypted Right nMsgMeta = C.cbDecrypt rcvDhSecret nmsgNonce encNMsgMeta From 7a72488275ba9ae80647a52e63c14a240d77f763 Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Mon, 20 Jun 2022 17:16:26 +0400 Subject: [PATCH 068/131] ntf: use separate key to encrypt NMsgMeta (#410) * ntf: use separate key to encrypt NMsgMeta * key negotiation * save key on server, use for encryption * refactor? * store error * NtfQueueCreds * server - NtfCreds * comment, rename * fix type * ClientNtfCreds * encoding --- src/Simplex/Messaging/Agent.hs | 4 +- src/Simplex/Messaging/Agent/Client.hs | 12 ++--- .../Messaging/Agent/NtfSubSupervisor.hs | 46 ++++++++----------- src/Simplex/Messaging/Agent/Store.hs | 17 +++++-- src/Simplex/Messaging/Agent/Store/SQLite.hs | 43 ++++++++--------- .../Store/SQLite/Migrations/M20220608_v2.hs | 2 + .../Store/SQLite/Migrations/agent_schema.sql | 1 + src/Simplex/Messaging/Client.hs | 8 ++-- src/Simplex/Messaging/Protocol.hs | 20 +++++--- src/Simplex/Messaging/Server.hs | 44 ++++++++++-------- src/Simplex/Messaging/Server/Env/STM.hs | 4 +- src/Simplex/Messaging/Server/QueueStore.hs | 19 +++++++- .../Messaging/Server/QueueStore/STM.hs | 12 ++--- src/Simplex/Messaging/Server/StoreLog.hs | 18 ++++---- tests/AgentTests/SQLiteTests.hs | 8 +--- tests/NtfServerTests.hs | 6 ++- tests/ServerTests.hs | 6 ++- 17 files changed, 147 insertions(+), 123 deletions(-) diff --git a/src/Simplex/Messaging/Agent.hs b/src/Simplex/Messaging/Agent.hs index 8ea17b5c1..7fc5bee69 100644 --- a/src/Simplex/Messaging/Agent.hs +++ b/src/Simplex/Messaging/Agent.hs @@ -401,8 +401,8 @@ getNotificationMessage' c nonce encNtfInfo = do Just NtfToken {ntfDhSecret = Just dhSecret} -> do ntfData <- agentCbDecrypt dhSecret nonce encNtfInfo PNMessageData {smpQueue, ntfTs, nmsgNonce, encNMsgMeta} <- liftEither (parse strP (INTERNAL "error parsing PNMessageData") ntfData) - (ntfConnId, rcvDhSecret) <- withStore c (`getNtfRcvQueue` smpQueue) - ntfMsgMeta <- (eitherToMaybe . smpDecode <$> agentCbDecrypt rcvDhSecret nmsgNonce encNMsgMeta) `catchError` \_ -> pure Nothing + (ntfConnId, rcvNtfDhSecret) <- withStore c (`getNtfRcvQueue` smpQueue) + ntfMsgMeta <- (eitherToMaybe . smpDecode <$> agentCbDecrypt rcvNtfDhSecret nmsgNonce encNMsgMeta) `catchError` \_ -> pure Nothing maxMsgs <- asks $ ntfMaxMessages . config (NotificationInfo {ntfConnId, ntfTs, ntfMsgMeta},) <$> getNtfMessages ntfConnId maxMsgs ntfMsgMeta [] _ -> throwError $ CMD PROHIBITED diff --git a/src/Simplex/Messaging/Agent/Client.hs b/src/Simplex/Messaging/Agent/Client.hs index f69813da5..ea62e9204 100644 --- a/src/Simplex/Messaging/Agent/Client.hs +++ b/src/Simplex/Messaging/Agent/Client.hs @@ -85,7 +85,7 @@ import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Encoding import Simplex.Messaging.Notifications.Client import Simplex.Messaging.Notifications.Protocol -import Simplex.Messaging.Protocol (BrokerMsg, ErrorType, MsgFlags (..), MsgId, NotifierId, NtfPrivateSignKey, NtfPublicVerifyKey, ProtocolServer (..), QueueId, QueueIdsKeys (..), SMPMsgMeta, SndPublicVerifyKey) +import Simplex.Messaging.Protocol (BrokerMsg, ErrorType, MsgFlags (..), MsgId, NotifierId, NtfPrivateSignKey, NtfPublicVerifyKey, ProtocolServer (..), QueueId, QueueIdsKeys (..), RcvNtfPublicDhKey, SMPMsgMeta, SndPublicVerifyKey) import qualified Simplex.Messaging.Protocol as SMP import Simplex.Messaging.TMap (TMap) import qualified Simplex.Messaging.TMap as TM @@ -421,9 +421,7 @@ newRcvQueue_ a c srv = do e2eDhSecret = Nothing, sndId = Just sndId, status = New, - ntfPublicKey = Nothing, - ntfPrivateKey = Nothing, - notifierId = Nothing + clientNtfCreds = Nothing } pure (rq, SMPQueueUri srv sndId SMP.smpClientVRange e2eDhKey) @@ -530,10 +528,10 @@ secureQueue c RcvQueue {server, rcvId, rcvPrivateKey} senderKey = withLogClient c server rcvId "KEY " $ \smp -> secureSMPQueue smp rcvPrivateKey rcvId senderKey -enableQueueNotifications :: AgentMonad m => AgentClient -> RcvQueue -> NtfPublicVerifyKey -> m NotifierId -enableQueueNotifications c RcvQueue {server, rcvId, rcvPrivateKey} notifierKey = +enableQueueNotifications :: AgentMonad m => AgentClient -> RcvQueue -> NtfPublicVerifyKey -> RcvNtfPublicDhKey -> m (NotifierId, RcvNtfPublicDhKey) +enableQueueNotifications c RcvQueue {server, rcvId, rcvPrivateKey} notifierKey rcvNtfPublicDhKey = withLogClient c server rcvId "NKEY " $ \smp -> - enableSMPQueueNotifications smp rcvPrivateKey rcvId notifierKey + enableSMPQueueNotifications smp rcvPrivateKey rcvId notifierKey rcvNtfPublicDhKey sendAck :: AgentMonad m => AgentClient -> RcvQueue -> MsgId -> m () sendAck c RcvQueue {server, rcvId, rcvPrivateKey} msgId = diff --git a/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs b/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs index db82204b3..4d00cf6c1 100644 --- a/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs +++ b/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs @@ -59,16 +59,16 @@ processNtfSub c (connId, cmd) = do case cmd of NSCCreate -> do -- TODO merge getNtfSubscription and getRcvQueue into one method to read both in same transaction? - (sub_, RcvQueue {notifierId, server = smpServer}) <- withStore c $ \db -> runExceptT $ do + (sub_, RcvQueue {server = smpServer, clientNtfCreds}) <- withStore c $ \db -> runExceptT $ do sub_ <- liftIO $ getNtfSubscription db connId q <- ExceptT $ getRcvQueue db connId pure (sub_, q) case (sub_, ntfServer_) of (Nothing, Just ntfServer) -> do currentTime <- liftIO getCurrentTime - case notifierId of - (Just nId) -> do - let newSub = newNtfSubscription connId smpServer (Just nId) ntfServer NASKey currentTime + case clientNtfCreds of + Just ClientNtfCreds {notifierId} -> do + let newSub = newNtfSubscription connId smpServer (Just notifierId) ntfServer NASKey currentTime withStore' c $ \db -> createNtfSubscription db newSub (NtfSubAction NSACreate) _ -> do let newSub = newNtfSubscription connId smpServer Nothing ntfServer NASNew currentTime @@ -123,13 +123,13 @@ runNtfWorker c srv doWork = forever $ do ts <- liftIO getCurrentTime case nextSub_ of Nothing -> noWorkToDo - Just (ntfSub@NtfSubscription {connId, smpServer, ntfSubId}, ntfSubAction, RcvQueue {ntfPrivateKey, notifierId}) -> + Just (ntfSub@NtfSubscription {connId, smpServer, ntfSubId}, ntfSubAction, RcvQueue {clientNtfCreds}) -> unlessM (rescheduleAction doWork ts ntfSub) $ case ntfSubAction of - NSACreate -> case (ntfPrivateKey, notifierId) of - (Just ntfPrivKey, Just nId) + NSACreate -> case clientNtfCreds of + Just ClientNtfCreds {ntfPrivateKey, notifierId} | ntfTknStatus == NTActive -> do - nSubId <- agentNtfCreateSubscription c tknId tkn (SMPQueueNtf smpServer nId) ntfPrivKey + nSubId <- agentNtfCreateSubscription c tknId tkn (SMPQueueNtf smpServer notifierId) ntfPrivateKey let actionTs = addUTCTime 30 ts withStore' c $ \db -> updateNtfSubscription db connId ntfSub {ntfSubId = Just nSubId, ntfSubStatus = NASCreated NSNew, ntfSubActionTs = actionTs} (NtfSubAction NSACheck) @@ -168,28 +168,22 @@ runNtfSMPWorker c srv doWork = forever $ do ts <- liftIO getCurrentTime case nextSub_ of Nothing -> noWorkToDo - Just (ntfSub@NtfSubscription {connId, ntfServer}, ntfSubAction, rq@RcvQueue {ntfPublicKey}) -> + Just (ntfSub@NtfSubscription {connId, ntfServer}, ntfSubAction, rq) -> unlessM (rescheduleAction doWork ts ntfSub) $ case ntfSubAction of NSAKey - | ntfTknStatus == NTActive -> - case ntfPublicKey of - Just ntfPubKey -> - enableNotificationsWithNKey ntfPubKey - _ -> do - C.SignAlg a <- asks (cmdSignAlg . config) - (ntfPubKey, ntfPrivKey) <- liftIO $ C.generateSignatureKeyPair a - withStore' c $ \db -> setRcvQueueNotifierKey db connId ntfPubKey ntfPrivKey - enableNotificationsWithNKey ntfPubKey + | ntfTknStatus == NTActive -> do + C.SignAlg a <- asks (cmdSignAlg . config) + (ntfPublicKey, ntfPrivateKey) <- liftIO $ C.generateSignatureKeyPair a + (rcvNtfPubDhKey, rcvNtfPrivDhKey) <- liftIO C.generateKeyPair' + (notifierId, rcvNtfSrvPubDhKey) <- enableQueueNotifications c rq ntfPublicKey rcvNtfPubDhKey + let rcvNtfDhSecret = C.dh' rcvNtfSrvPubDhKey rcvNtfPrivDhKey + withStore' c $ \st -> do + setRcvQueueNtfCreds st connId ClientNtfCreds {ntfPublicKey, ntfPrivateKey, notifierId, rcvNtfDhSecret} + updateNtfSubscription st connId ntfSub {ntfQueueId = Just notifierId, ntfSubStatus = NASKey, ntfSubActionTs = ts} (NtfSubAction NSACreate) + ns <- asks ntfSupervisor + atomically $ sendNtfSubCommand ns (connId, NSCNtfWorker ntfServer) | otherwise -> ntfInternalError c connId "NSAKey - token not active" - where - enableNotificationsWithNKey ntfPubKey = do - nId <- enableQueueNotifications c rq ntfPubKey - withStore' c $ \db -> do - setRcvQueueNotifierId db connId nId - updateNtfSubscription db connId ntfSub {ntfQueueId = Just nId, ntfSubStatus = NASKey, ntfSubActionTs = ts} (NtfSubAction NSACreate) - ns <- asks ntfSupervisor - atomically $ sendNtfSubCommand ns (connId, NSCNtfWorker ntfServer) _ -> noWorkToDo delay <- asks $ ntfWorkerThrottle . config liftIO $ threadDelay delay diff --git a/src/Simplex/Messaging/Agent/Store.hs b/src/Simplex/Messaging/Agent/Store.hs index 45110705e..08ef5d264 100644 --- a/src/Simplex/Messaging/Agent/Store.hs +++ b/src/Simplex/Messaging/Agent/Store.hs @@ -26,6 +26,7 @@ import Simplex.Messaging.Protocol NtfPrivateSignKey, NtfPublicVerifyKey, RcvDhSecret, + RcvNtfDhSecret, RcvPrivateSignKey, SndPrivateSignKey, ) @@ -51,11 +52,19 @@ data RcvQueue = RcvQueue sndId :: Maybe SMP.SenderId, -- | queue status status :: QueueStatus, - -- | key pair to be used by the notification server to sign transmissions - ntfPublicKey :: Maybe NtfPublicVerifyKey, - ntfPrivateKey :: Maybe NtfPrivateSignKey, + -- | credentials used in context of notifications + clientNtfCreds :: Maybe ClientNtfCreds + } + deriving (Eq, Show) + +data ClientNtfCreds = ClientNtfCreds + { -- | key pair to be used by the notification server to sign transmissions + ntfPublicKey :: NtfPublicVerifyKey, + ntfPrivateKey :: NtfPrivateSignKey, -- | queue ID to be used by the notification server for NSUB command - notifierId :: Maybe NotifierId + notifierId :: NotifierId, + -- | shared DH secret used to encrypt/decrypt notification metadata (NMsgMeta) from server to recipient + rcvNtfDhSecret :: RcvNtfDhSecret } deriving (Eq, Show) diff --git a/src/Simplex/Messaging/Agent/Store/SQLite.hs b/src/Simplex/Messaging/Agent/Store/SQLite.hs index 56aa2ccf0..a2d95aad6 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite.hs @@ -35,9 +35,7 @@ module Simplex.Messaging.Agent.Store.SQLite setRcvQueueConfirmedE2E, setSndQueueStatus, getRcvQueue, - -- RcvQueue notifier key and ID - setRcvQueueNotifierKey, - setRcvQueueNotifierId, + setRcvQueueNtfCreds, -- Confirmations createConfirmation, acceptConfirmation, @@ -126,7 +124,7 @@ import Simplex.Messaging.Encoding.String import Simplex.Messaging.Notifications.Client (NtfServer, NtfSubAction, NtfSubOrSMPAction (..), NtfSubSMPAction, NtfSubscription (..), NtfTknAction, NtfToken (..)) import Simplex.Messaging.Notifications.Protocol (DeviceToken (..), NtfTknStatus (..), NtfTokenId, SMPQueueNtf (..)) import Simplex.Messaging.Parsers (blobFieldParser, fromTextField_) -import Simplex.Messaging.Protocol (MsgBody, MsgFlags, NotifierId, NtfPrivateSignKey, NtfPublicVerifyKey, ProtocolServer (..), RcvDhSecret) +import Simplex.Messaging.Protocol (MsgBody, MsgFlags, ProtocolServer (..), RcvNtfDhSecret) import qualified Simplex.Messaging.Protocol as SMP import Simplex.Messaging.Util (bshow, eitherToMaybe, ($>>=), (<$$>)) import Simplex.Messaging.Version @@ -351,27 +349,16 @@ getRcvQueue :: DB.Connection -> ConnId -> IO (Either StoreError RcvQueue) getRcvQueue db connId = maybe (Left SEConnNotFound) Right <$> getRcvQueueByConnId_ db connId -setRcvQueueNotifierKey :: DB.Connection -> ConnId -> NtfPublicVerifyKey -> NtfPrivateSignKey -> IO () -setRcvQueueNotifierKey db connId ntfPublicKey ntfPrivateKey = +setRcvQueueNtfCreds :: DB.Connection -> ConnId -> ClientNtfCreds -> IO () +setRcvQueueNtfCreds db connId ClientNtfCreds {ntfPublicKey, ntfPrivateKey, notifierId, rcvNtfDhSecret} = DB.execute db [sql| UPDATE rcv_queues - SET ntf_public_key = ?, ntf_private_key = ? + SET ntf_public_key = ?, ntf_private_key = ?, ntf_id = ?, rcv_ntf_dh_secret = ? WHERE conn_id = ? |] - (ntfPublicKey, ntfPrivateKey, connId) - -setRcvQueueNotifierId :: DB.Connection -> ConnId -> NotifierId -> IO () -setRcvQueueNotifierId db connId nId = - DB.execute - db - [sql| - UPDATE rcv_queues - SET ntf_id = ? - WHERE conn_id = ? - |] - (nId, connId) + (ntfPublicKey, ntfPrivateKey, notifierId, rcvNtfDhSecret, connId) createConfirmation :: DB.Connection -> TVar ChaChaDRG -> NewConfirmation -> IO (Either StoreError ConfirmationId) createConfirmation db gVar NewConfirmation {connId, senderConf = SMPConfirmation {senderKey, e2ePubKey, connInfo, smpReplyQueues}, ratchetState} = @@ -848,17 +835,20 @@ getActiveNtfToken db = ntfDhKeys = (ntfDhPubKey, ntfDhPrivKey) in NtfToken {deviceToken = DeviceToken provider dt, ntfServer, ntfTokenId, ntfPubKey, ntfPrivKey, ntfDhKeys, ntfDhSecret, ntfTknStatus, ntfTknAction} -getNtfRcvQueue :: DB.Connection -> SMPQueueNtf -> IO (Either StoreError (ConnId, RcvDhSecret)) +getNtfRcvQueue :: DB.Connection -> SMPQueueNtf -> IO (Either StoreError (ConnId, RcvNtfDhSecret)) getNtfRcvQueue db SMPQueueNtf {smpServer = (SMPServer host port _), notifierId} = - firstRow id SEConnNotFound $ + firstRow' res SEConnNotFound $ DB.query db [sql| - SELECT conn_id, rcv_dh_secret + SELECT conn_id, rcv_ntf_dh_secret FROM rcv_queues WHERE host = ? AND port = ? AND ntf_id = ? |] (host, port, notifierId) + where + res (connId, Just rcvNtfDhSecret) = Right (connId, rcvNtfDhSecret) + res _ = Left SEConnNotFound -- * Auxiliary helpers @@ -1039,16 +1029,19 @@ getRcvQueueByConnId_ dbConn connId = [sql| SELECT s.key_hash, q.host, q.port, q.rcv_id, q.rcv_private_key, q.rcv_dh_secret, q.e2e_priv_key, q.e2e_dh_secret, q.snd_id, q.status, - q.ntf_public_key, q.ntf_private_key, q.ntf_id + q.ntf_public_key, q.ntf_private_key, q.ntf_id, q.rcv_ntf_dh_secret FROM rcv_queues q INNER JOIN servers s ON q.host = s.host AND q.port = s.port WHERE q.conn_id = ?; |] (Only connId) where - rcvQueue ((keyHash, host, port, rcvId, rcvPrivateKey, rcvDhSecret, e2ePrivKey, e2eDhSecret, sndId, status) :. (ntfPublicKey, ntfPrivateKey, notifierId)) = + rcvQueue ((keyHash, host, port, rcvId, rcvPrivateKey, rcvDhSecret, e2ePrivKey, e2eDhSecret, sndId, status) :. (ntfPublicKey_, ntfPrivateKey_, notifierId_, rcvNtfDhSecret_)) = let server = SMPServer host port keyHash - in RcvQueue {server, rcvId, rcvPrivateKey, rcvDhSecret, e2ePrivKey, e2eDhSecret, sndId, status, ntfPublicKey, ntfPrivateKey, notifierId} + clientNtfCreds = case (ntfPublicKey_, ntfPrivateKey_, notifierId_, rcvNtfDhSecret_) of + (Just ntfPublicKey, Just ntfPrivateKey, Just notifierId, Just rcvNtfDhSecret) -> Just $ ClientNtfCreds {ntfPublicKey, ntfPrivateKey, notifierId, rcvNtfDhSecret} + _ -> Nothing + in RcvQueue {server, rcvId, rcvPrivateKey, rcvDhSecret, e2ePrivKey, e2eDhSecret, sndId, status, clientNtfCreds} getSndQueueByConnId_ :: DB.Connection -> ConnId -> IO (Maybe SndQueue) getSndQueueByConnId_ dbConn connId = diff --git a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220608_v2.hs b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220608_v2.hs index 2760322d5..753b3c164 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220608_v2.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220608_v2.hs @@ -22,6 +22,8 @@ ALTER TABLE rcv_queues ADD COLUMN ntf_private_key BLOB; ALTER TABLE rcv_queues ADD COLUMN ntf_id BLOB; +ALTER TABLE rcv_queues ADD COLUMN rcv_ntf_dh_secret BLOB; + CREATE UNIQUE INDEX idx_rcv_queues_ntf ON rcv_queues (host, port, ntf_id); CREATE TABLE ntf_subscriptions ( diff --git a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql index bdd3efeab..2a64a78f4 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql +++ b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql @@ -39,6 +39,7 @@ CREATE TABLE rcv_queues( ntf_public_key BLOB, ntf_private_key BLOB, ntf_id BLOB, + rcv_ntf_dh_secret BLOB, PRIMARY KEY(host, port, rcv_id), FOREIGN KEY(host, port) REFERENCES servers ON DELETE RESTRICT ON UPDATE CASCADE, diff --git a/src/Simplex/Messaging/Client.hs b/src/Simplex/Messaging/Client.hs index 33bc01529..f6938411e 100644 --- a/src/Simplex/Messaging/Client.hs +++ b/src/Simplex/Messaging/Client.hs @@ -315,10 +315,10 @@ secureSMPQueue c rpKey rId senderKey = okSMPCommand (KEY senderKey) c rpKey rId -- | Enable notifications for the queue for push notifications server. -- -- https://github.com/simplex-chat/simplexmq/blob/master/protocol/simplex-messaging.md#enable-notifications-command -enableSMPQueueNotifications :: SMPClient -> RcvPrivateSignKey -> RecipientId -> NtfPublicVerifyKey -> ExceptT ProtocolClientError IO NotifierId -enableSMPQueueNotifications c rpKey rId notifierKey = - sendSMPCommand c (Just rpKey) rId (NKEY notifierKey) >>= \case - NID nId -> pure nId +enableSMPQueueNotifications :: SMPClient -> RcvPrivateSignKey -> RecipientId -> NtfPublicVerifyKey -> RcvNtfPublicDhKey -> ExceptT ProtocolClientError IO (NotifierId, RcvNtfPublicDhKey) +enableSMPQueueNotifications c rpKey rId notifierKey rcvNtfPublicDhKey = + sendSMPCommand c (Just rpKey) rId (NKEY notifierKey rcvNtfPublicDhKey) >>= \case + NID nId rcvNtfSrvPublicDhKey -> pure (nId, rcvNtfSrvPublicDhKey) _ -> throwE PCEUnexpectedResponse -- | Send SMP message. diff --git a/src/Simplex/Messaging/Protocol.hs b/src/Simplex/Messaging/Protocol.hs index 79c3e74c0..f9ed5e321 100644 --- a/src/Simplex/Messaging/Protocol.hs +++ b/src/Simplex/Messaging/Protocol.hs @@ -76,6 +76,8 @@ module Simplex.Messaging.Protocol SndPublicVerifyKey, NtfPrivateSignKey, NtfPublicVerifyKey, + RcvNtfPublicDhKey, + RcvNtfDhSecret, MsgId, MsgBody, EncNMsgMeta, @@ -218,7 +220,7 @@ data Command (p :: Party) where NEW :: RcvPublicVerifyKey -> RcvPublicDhKey -> Command Recipient SUB :: Command Recipient KEY :: SndPublicVerifyKey -> Command Recipient - NKEY :: NtfPublicVerifyKey -> Command Recipient + NKEY :: NtfPublicVerifyKey -> RcvNtfPublicDhKey -> Command Recipient GET :: Command Recipient -- ACK v1 has to be supported for encoding/decoding -- ACK :: Command Recipient @@ -243,7 +245,7 @@ data BrokerMsg where -- MSG v1 has to be supported for encoding/decoding -- MSG :: MsgId -> SystemTime -> MsgBody -> BrokerMsg MSG :: MsgId -> SystemTime -> MsgFlags -> MsgBody -> BrokerMsg - NID :: NotifierId -> BrokerMsg + NID :: NotifierId -> RcvNtfPublicDhKey -> BrokerMsg NMSG :: C.CbNonce -> EncNMsgMeta -> BrokerMsg END :: BrokerMsg OK :: BrokerMsg @@ -540,6 +542,12 @@ type NtfPrivateSignKey = C.APrivateSignKey -- | Public key used by SMP server to verify authorization of NSUB command sent by push notifications server. type NtfPublicVerifyKey = C.APublicVerifyKey +-- | Public key used for DH exchange to encrypt notification metadata from server to recipient +type RcvNtfPublicDhKey = C.PublicKeyX25519 + +-- | DH Secret used to encrypt notification metadata from server to recipient +type RcvNtfDhSecret = C.DhSecretX25519 + -- | SMP message server ID. type MsgId = ByteString @@ -642,7 +650,7 @@ instance PartyI p => ProtocolEncoding (Command p) where NEW rKey dhKey -> e (NEW_, ' ', rKey, dhKey) SUB -> e SUB_ KEY k -> e (KEY_, ' ', k) - NKEY k -> e (NKEY_, ' ', k) + NKEY k dhKey -> e (NKEY_, ' ', k, dhKey) GET -> e GET_ ACK msgId | v == 1 -> e ACK_ @@ -689,7 +697,7 @@ instance ProtocolEncoding Cmd where NEW_ -> NEW <$> _smpP <*> smpP SUB_ -> pure SUB KEY_ -> KEY <$> _smpP - NKEY_ -> NKEY <$> _smpP + NKEY_ -> NKEY <$> _smpP <*> smpP GET_ -> pure GET ACK_ | v == 1 -> pure $ ACK "" @@ -713,7 +721,7 @@ instance ProtocolEncoding BrokerMsg where MSG msgId ts flags msgBody | v == 1 -> e (MSG_, ' ', msgId, ts, Tail msgBody) | otherwise -> e (MSG_, ' ', msgId, ts, flags, ' ', Tail msgBody) - NID nId -> e (NID_, ' ', nId) + NID nId srvNtfDh -> e (NID_, ' ', nId, srvNtfDh) NMSG nmsgNonce encNMsgMeta -> e (NMSG_, ' ', nmsgNonce, encNMsgMeta) END -> e END_ OK -> e OK_ @@ -728,7 +736,7 @@ instance ProtocolEncoding BrokerMsg where | v == 1 -> MSG <$> _smpP <*> smpP <*> pure noMsgFlags <*> (unTail <$> smpP) | otherwise -> MSG <$> _smpP <*> smpP <*> smpP <*> (unTail <$> _smpP) IDS_ -> IDS <$> (QIK <$> _smpP <*> smpP <*> smpP) - NID_ -> NID <$> _smpP + NID_ -> NID <$> _smpP <*> smpP NMSG_ -> NMSG <$> _smpP <*> smpP END_ -> pure END OK_ -> pure OK diff --git a/src/Simplex/Messaging/Server.hs b/src/Simplex/Messaging/Server.hs index 8fbf4dca0..e21647d0c 100644 --- a/src/Simplex/Messaging/Server.hs +++ b/src/Simplex/Messaging/Server.hs @@ -271,7 +271,7 @@ verifyTransmission sig_ signed queueId cmd = do Cmd SRecipient _ -> verifyCmd SRecipient $ verifyCmdSignature sig_ signed . recipientKey Cmd SSender SEND {} -> verifyCmd SSender $ verifyMaybe . senderKey Cmd SSender PING -> pure True - Cmd SNotifier NSUB -> verifyCmd SNotifier $ verifyMaybe . fmap snd . notifier + Cmd SNotifier NSUB -> verifyCmd SNotifier $ verifyMaybe . fmap notifierKey . notifier where verifyCmd :: SParty p -> (QueueRec -> Bool) -> m Bool verifyCmd party f = do @@ -333,7 +333,7 @@ client clnt@Client {subscriptions, ntfSubscriptions, rcvQ, sndQ} Server {subscri GET -> getMessage ACK msgId -> acknowledgeMsg msgId KEY sKey -> secureQueue_ st sKey - NKEY nKey -> addQueueNotifier_ st nKey + NKEY nKey dhKey -> addQueueNotifier_ st nKey dhKey OFF -> suspendQueue_ st DEL -> delQueueAndMsgs st where @@ -387,19 +387,23 @@ client clnt@Client {subscriptions, ntfSubscriptions, rcvQ, sndQ} Server {subscri atomically $ modifyTVar (qSecured stats) (+ 1) atomically $ (corrId,queueId,) . either ERR (const OK) <$> secureQueue st queueId sKey - addQueueNotifier_ :: QueueStore -> NtfPublicVerifyKey -> m (Transmission BrokerMsg) - addQueueNotifier_ st nKey = (corrId,queueId,) <$> addNotifierRetry 3 + addQueueNotifier_ :: QueueStore -> NtfPublicVerifyKey -> RcvNtfPublicDhKey -> m (Transmission BrokerMsg) + addQueueNotifier_ st notifierKey dhKey = do + (rcvPublicDhKey, privDhKey) <- liftIO C.generateKeyPair' + let rcvNtfDhSecret = C.dh' dhKey privDhKey + (corrId,queueId,) <$> addNotifierRetry 3 rcvPublicDhKey rcvNtfDhSecret where - addNotifierRetry :: Int -> m BrokerMsg - addNotifierRetry 0 = pure $ ERR INTERNAL - addNotifierRetry n = do - nId <- randomId =<< asks (queueIdBytes . config) - atomically (addQueueNotifier st queueId nId nKey) >>= \case - Left DUPLICATE_ -> addNotifierRetry $ n - 1 + addNotifierRetry :: Int -> RcvNtfPublicDhKey -> RcvNtfDhSecret -> m BrokerMsg + addNotifierRetry 0 _ _ = pure $ ERR INTERNAL + addNotifierRetry n rcvPublicDhKey rcvNtfDhSecret = do + notifierId <- randomId =<< asks (queueIdBytes . config) + let ntfCreds = NtfCreds {notifierId, notifierKey, rcvNtfDhSecret} + atomically (addQueueNotifier st queueId ntfCreds) >>= \case + Left DUPLICATE_ -> addNotifierRetry (n - 1) rcvPublicDhKey rcvNtfDhSecret Left e -> pure $ ERR e Right _ -> do - withLog $ \s -> logAddNotifier s queueId nId nKey - pure $ NID nId + withLog $ \s -> logAddNotifier s queueId ntfCreds + pure $ NID notifierId rcvPublicDhKey suspendQueue_ :: QueueStore -> m (Transmission BrokerMsg) suspendQueue_ st = do @@ -545,20 +549,20 @@ client clnt@Client {subscriptions, ntfSubscriptions, rcvQ, sndQ} Server {subscri trySendNotification :: Message -> TVar ChaChaDRG -> STM () trySendNotification msg ntfNonceDrg = - forM_ (notifier qr) $ \(nId, _) -> - mapM_ (writeNtf nId msg ntfNonceDrg) =<< TM.lookup nId notifiers + forM_ (notifier qr) $ \NtfCreds {notifierId, rcvNtfDhSecret} -> + mapM_ (writeNtf notifierId msg rcvNtfDhSecret ntfNonceDrg) =<< TM.lookup notifierId notifiers - writeNtf :: NotifierId -> Message -> TVar ChaChaDRG -> Client -> STM () - writeNtf nId msg ntfNonceDrg Client {sndQ = q} = + writeNtf :: NotifierId -> Message -> RcvNtfDhSecret -> TVar ChaChaDRG -> Client -> STM () + writeNtf nId msg rcvNtfDhSecret ntfNonceDrg Client {sndQ = q} = unlessM (isFullTBQueue sndQ) $ do - (nmsgNonce, encNMsgMeta) <- mkMessageNotification msg ntfNonceDrg + (nmsgNonce, encNMsgMeta) <- mkMessageNotification msg rcvNtfDhSecret ntfNonceDrg writeTBQueue q (CorrId "", nId, NMSG nmsgNonce encNMsgMeta) - mkMessageNotification :: Message -> TVar ChaChaDRG -> STM (C.CbNonce, EncNMsgMeta) - mkMessageNotification Message {msgId, ts} ntfNonceDrg = do + mkMessageNotification :: Message -> RcvNtfDhSecret -> TVar ChaChaDRG -> STM (C.CbNonce, EncNMsgMeta) + mkMessageNotification Message {msgId, ts} rcvNtfDhSecret ntfNonceDrg = do cbNonce <- C.pseudoRandomCbNonce ntfNonceDrg let msgMeta = NMsgMeta {msgId, msgTs = ts} - encNMsgMeta = C.cbEncrypt (rcvDhSecret qr) cbNonce (smpEncode msgMeta) 128 + encNMsgMeta = C.cbEncrypt rcvNtfDhSecret cbNonce (smpEncode msgMeta) 128 pure . (cbNonce,) $ fromRight "" encNMsgMeta deliverMessage :: RecipientId -> TVar Sub -> MsgQueue -> Maybe Message -> m (Transmission BrokerMsg) diff --git a/src/Simplex/Messaging/Server/Env/STM.hs b/src/Simplex/Messaging/Server/Env/STM.hs index 3224662e4..b64495e0e 100644 --- a/src/Simplex/Messaging/Server/Env/STM.hs +++ b/src/Simplex/Messaging/Server/Env/STM.hs @@ -24,7 +24,7 @@ import Simplex.Messaging.Crypto (KeyHash (..)) import Simplex.Messaging.Protocol import Simplex.Messaging.Server.Expiration import Simplex.Messaging.Server.MsgStore.STM -import Simplex.Messaging.Server.QueueStore (QueueRec (..)) +import Simplex.Messaging.Server.QueueStore (NtfCreds (..), QueueRec (..)) import Simplex.Messaging.Server.QueueStore.STM import Simplex.Messaging.Server.StoreLog import Simplex.Messaging.TMap (TMap) @@ -190,4 +190,4 @@ newEnv config@ServerConfig {caCertificateFile, certificateFile, privateKeyFile, addNotifier :: QueueRec -> Map NotifierId RecipientId -> Map NotifierId RecipientId addNotifier q = case notifier q of Nothing -> id - Just (nId, _) -> M.insert nId (recipientId q) + Just NtfCreds {notifierId} -> M.insert notifierId (recipientId q) diff --git a/src/Simplex/Messaging/Server/QueueStore.hs b/src/Simplex/Messaging/Server/QueueStore.hs index 544bf35b9..f059d3170 100644 --- a/src/Simplex/Messaging/Server/QueueStore.hs +++ b/src/Simplex/Messaging/Server/QueueStore.hs @@ -1,9 +1,11 @@ {-# LANGUAGE DataKinds #-} {-# LANGUAGE KindSignatures #-} {-# LANGUAGE MultiParamTypeClasses #-} +{-# LANGUAGE NamedFieldPuns #-} module Simplex.Messaging.Server.QueueStore where +import Simplex.Messaging.Encoding.String import Simplex.Messaging.Protocol data QueueRec = QueueRec @@ -12,17 +14,30 @@ data QueueRec = QueueRec rcvDhSecret :: RcvDhSecret, senderId :: SenderId, senderKey :: Maybe SndPublicVerifyKey, - notifier :: Maybe (NotifierId, NtfPublicVerifyKey), + notifier :: Maybe NtfCreds, status :: QueueStatus } deriving (Eq, Show) +data NtfCreds = NtfCreds + { notifierId :: NotifierId, + notifierKey :: NtfPublicVerifyKey, + rcvNtfDhSecret :: RcvNtfDhSecret + } + deriving (Eq, Show) + +instance StrEncoding NtfCreds where + strEncode NtfCreds {notifierId, notifierKey, rcvNtfDhSecret} = strEncode (notifierId, notifierKey, rcvNtfDhSecret) + strP = do + (notifierId, notifierKey, rcvNtfDhSecret) <- strP + pure NtfCreds {notifierId, notifierKey, rcvNtfDhSecret} + data QueueStatus = QueueActive | QueueOff deriving (Eq, Show) class MonadQueueStore s m where addQueue :: s -> QueueRec -> m (Either ErrorType ()) getQueue :: s -> SParty p -> QueueId -> m (Either ErrorType QueueRec) secureQueue :: s -> RecipientId -> SndPublicVerifyKey -> m (Either ErrorType QueueRec) - addQueueNotifier :: s -> RecipientId -> NotifierId -> NtfPublicVerifyKey -> m (Either ErrorType QueueRec) + addQueueNotifier :: s -> RecipientId -> NtfCreds -> m (Either ErrorType QueueRec) suspendQueue :: s -> RecipientId -> m (Either ErrorType ()) deleteQueue :: s -> RecipientId -> m (Either ErrorType ()) diff --git a/src/Simplex/Messaging/Server/QueueStore/STM.hs b/src/Simplex/Messaging/Server/QueueStore/STM.hs index d04c8f14b..00e87d520 100644 --- a/src/Simplex/Messaging/Server/QueueStore/STM.hs +++ b/src/Simplex/Messaging/Server/QueueStore/STM.hs @@ -61,15 +61,15 @@ instance MonadQueueStore QueueStore STM where Just _ -> pure Nothing _ -> writeTVar qVar q {senderKey = Just sKey} $> Just q - addQueueNotifier :: QueueStore -> RecipientId -> NotifierId -> NtfPublicVerifyKey -> STM (Either ErrorType QueueRec) - addQueueNotifier QueueStore {queues, notifiers} rId nId nKey = do - ifM (TM.member nId notifiers) (pure $ Left DUPLICATE_) $ + addQueueNotifier :: QueueStore -> RecipientId -> NtfCreds -> STM (Either ErrorType QueueRec) + addQueueNotifier QueueStore {queues, notifiers} rId ntfCreds@NtfCreds {notifierId} = do + ifM (TM.member notifierId notifiers) (pure $ Left DUPLICATE_) $ withQueue rId queues $ \qVar -> readTVar qVar >>= \q -> case notifier q of Just _ -> pure Nothing _ -> do - writeTVar qVar q {notifier = Just (nId, nKey)} - TM.insert nId rId notifiers + writeTVar qVar q {notifier = Just ntfCreds} + TM.insert notifierId rId notifiers pure $ Just q suspendQueue :: QueueStore -> RecipientId -> STM (Either ErrorType ()) @@ -82,7 +82,7 @@ instance MonadQueueStore QueueStore STM where Just qVar -> readTVar qVar >>= \q -> do TM.delete (senderId q) senders - forM_ (notifier q) $ \(nId, _) -> TM.delete nId notifiers + forM_ (notifier q) $ \NtfCreds {notifierId} -> TM.delete notifierId notifiers pure $ Right () _ -> pure $ Left AUTH diff --git a/src/Simplex/Messaging/Server/StoreLog.hs b/src/Simplex/Messaging/Server/StoreLog.hs index e0928c20e..b18b57f20 100644 --- a/src/Simplex/Messaging/Server/StoreLog.hs +++ b/src/Simplex/Messaging/Server/StoreLog.hs @@ -34,7 +34,7 @@ import Data.Map.Strict (Map) import qualified Data.Map.Strict as M import Simplex.Messaging.Encoding.String import Simplex.Messaging.Protocol -import Simplex.Messaging.Server.QueueStore (QueueRec (..), QueueStatus (..)) +import Simplex.Messaging.Server.QueueStore (NtfCreds (..), QueueRec (..), QueueStatus (..)) import Simplex.Messaging.Transport (trimCR) import System.Directory (doesFileExist) import System.IO @@ -48,7 +48,7 @@ data StoreLog (a :: IOMode) where data StoreLogRecord = CreateQueue QueueRec | SecureQueue QueueId SndPublicVerifyKey - | AddNotifier QueueId NotifierId NtfPublicVerifyKey + | AddNotifier QueueId NtfCreds | DeleteQueue QueueId instance StrEncoding QueueRec where @@ -62,7 +62,7 @@ instance StrEncoding QueueRec where ] <> maybe "" notifierStr notifier where - notifierStr (nId, nKey) = " nid=" <> strEncode nId <> " nk=" <> strEncode nKey + notifierStr ntfCreds = " notifier=" <> strEncode ntfCreds strP = do recipientId <- "rid=" *> strP_ @@ -70,20 +70,20 @@ instance StrEncoding QueueRec where rcvDhSecret <- "rdh=" *> strP_ senderId <- "sid=" *> strP_ senderKey <- "sk=" *> strP - notifier <- optional $ (,) <$> (" nid=" *> strP_) <*> ("nk=" *> strP) + notifier <- optional $ " notifier=" *> strP pure QueueRec {recipientId, recipientKey, rcvDhSecret, senderId, senderKey, notifier, status = QueueActive} instance StrEncoding StoreLogRecord where strEncode = \case CreateQueue q -> strEncode (Str "CREATE", q) SecureQueue rId sKey -> strEncode (Str "SECURE", rId, sKey) - AddNotifier rId nId nKey -> strEncode (Str "NOTIFIER", rId, nId, nKey) + AddNotifier rId ntfCreds -> strEncode (Str "NOTIFIER", rId, ntfCreds) DeleteQueue rId -> strEncode (Str "DELETE", rId) strP = "CREATE " *> (CreateQueue <$> strP) <|> "SECURE " *> (SecureQueue <$> strP_ <*> strP) - <|> "NOTIFIER " *> (AddNotifier <$> strP_ <*> strP_ <*> strP) + <|> "NOTIFIER " *> (AddNotifier <$> strP_ <*> strP) <|> "DELETE " *> (DeleteQueue <$> strP) openWriteStoreLog :: FilePath -> IO (StoreLog 'WriteMode) @@ -115,8 +115,8 @@ logCreateQueue s = writeStoreLogRecord s . CreateQueue logSecureQueue :: StoreLog 'WriteMode -> QueueId -> SndPublicVerifyKey -> IO () logSecureQueue s qId sKey = writeStoreLogRecord s $ SecureQueue qId sKey -logAddNotifier :: StoreLog 'WriteMode -> QueueId -> NotifierId -> NtfPublicVerifyKey -> IO () -logAddNotifier s qId nId nKey = writeStoreLogRecord s $ AddNotifier qId nId nKey +logAddNotifier :: StoreLog 'WriteMode -> QueueId -> NtfCreds -> IO () +logAddNotifier s qId ntfCreds = writeStoreLogRecord s $ AddNotifier qId ntfCreds logDeleteQueue :: StoreLog 'WriteMode -> QueueId -> IO () logDeleteQueue s = writeStoreLogRecord s . DeleteQueue @@ -149,7 +149,7 @@ readQueues (ReadStoreLog _ h) = LB.hGetContents h >>= returnResult . procStoreLo procLogRecord m = \case CreateQueue q -> M.insert (recipientId q) q m SecureQueue qId sKey -> M.adjust (\q -> q {senderKey = Just sKey}) qId m - AddNotifier qId nId nKey -> M.adjust (\q -> q {notifier = Just (nId, nKey)}) qId m + AddNotifier qId ntfCreds -> M.adjust (\q -> q {notifier = Just ntfCreds}) qId m DeleteQueue qId -> M.delete qId m printError :: LogParsingError -> IO () printError (e, s) = B.putStrLn $ "Error parsing log: " <> B.pack e <> " - " <> s diff --git a/tests/AgentTests/SQLiteTests.hs b/tests/AgentTests/SQLiteTests.hs index 2afc1d316..f1a75ad87 100644 --- a/tests/AgentTests/SQLiteTests.hs +++ b/tests/AgentTests/SQLiteTests.hs @@ -161,9 +161,7 @@ rcvQueue1 = e2eDhSecret = Nothing, sndId = Just "2345", status = New, - ntfPublicKey = Nothing, - ntfPrivateKey = Nothing, - notifierId = Nothing + clientNtfCreds = Nothing } sndQueue1 :: SndQueue @@ -330,9 +328,7 @@ testUpgradeSndConnToDuplex = e2eDhSecret = Nothing, sndId = Just "4567", status = New, - ntfPublicKey = Nothing, - ntfPrivateKey = Nothing, - notifierId = Nothing + clientNtfCreds = Nothing } upgradeSndConnToDuplex db "conn1" anotherRcvQueue `shouldReturn` Left (SEBadConnType CRcv) diff --git a/tests/NtfServerTests.hs b/tests/NtfServerTests.hs index f7bf3ea27..be71a64e5 100644 --- a/tests/NtfServerTests.hs +++ b/tests/NtfServerTests.hs @@ -108,9 +108,11 @@ testNotificationSubscription (ATransport t) = RespNtf "2" _ NROk <- signSendRecvNtf nh tknKey ("2", tId, TVFY code) RespNtf "2a" _ (NRTkn NTActive) <- signSendRecvNtf nh tknKey ("2a", tId, TCHK) -- enable queue notifications - Resp "3" _ (NID nId) <- signSendRecv rh rKey ("3", rId, NKEY nPub) + (rcvNtfPubDhKey, rcvNtfPrivDhKey) <- C.generateKeyPair' + Resp "3" _ (NID nId rcvNtfSrvPubDhKey) <- signSendRecv rh rKey ("3", rId, NKEY nPub rcvNtfPubDhKey) let srv = SMPServer SMP.testHost SMP.testPort SMP.testKeyHash q = SMPQueueNtf srv nId + rcvNtfDhSecret = C.dh' rcvNtfSrvPubDhKey rcvNtfPrivDhKey RespNtf "4" _ (NRSubId _subId) <- signSendRecvNtf nh tknKey ("4", "", SNEW $ NewNtfSub tId q nKey) -- send message threadDelay 50000 @@ -123,7 +125,7 @@ testNotificationSubscription (ATransport t) = Right ntfDataDecrypted = C.cbDecrypt dhSecret nonce' message Right APNS.PNMessageData {smpQueue = SMPQueueNtf {smpServer, notifierId}, nmsgNonce, encNMsgMeta} = parse strP (AP.INTERNAL "error parsing PNMessageData") ntfDataDecrypted - Right nMsgMeta = C.cbDecrypt rcvDhSecret nmsgNonce encNMsgMeta + Right nMsgMeta = C.cbDecrypt rcvNtfDhSecret nmsgNonce encNMsgMeta Right NMsgMeta {msgId, msgTs} = parse smpP (AP.INTERNAL "error parsing NMsgMeta") nMsgMeta smpServer `shouldBe` srv notifierId `shouldBe` nId diff --git a/tests/ServerTests.hs b/tests/ServerTests.hs index e72b21d7a..591f4c085 100644 --- a/tests/ServerTests.hs +++ b/tests/ServerTests.hs @@ -406,7 +406,8 @@ testWithStoreLog at@(ATransport t) = withSmpServerStoreLogOn at testPort . runTest t $ \h -> runClient t $ \h1 -> do (sId1, rId1, rKey1, dhShared) <- createAndSecureQueue h sPub1 - Resp "abcd" _ (NID nId) <- signSendRecv h rKey1 ("abcd", rId1, NKEY nPub) + (rcvNtfPubDhKey, _) <- C.generateKeyPair' + Resp "abcd" _ (NID nId _) <- signSendRecv h rKey1 ("abcd", rId1, NKEY nPub rcvNtfPubDhKey) atomically $ do writeTVar recipientId1 rId1 writeTVar recipientKey1 $ Just rKey1 @@ -610,7 +611,8 @@ testMessageNotifications (ATransport t) = smpTest4 t $ \rh sh nh1 nh2 -> do (sId, rId, rKey, dhShared) <- createAndSecureQueue rh sPub let dec nonce = C.cbDecrypt dhShared (C.cbNonce nonce) - Resp "1" _ (NID nId) <- signSendRecv rh rKey ("1", rId, NKEY nPub) + (rcvNtfPubDhKey, _) <- C.generateKeyPair' + Resp "1" _ (NID nId _) <- signSendRecv rh rKey ("1", rId, NKEY nPub rcvNtfPubDhKey) Resp "2" _ OK <- signSendRecv nh1 nKey ("2", nId, NSUB) Resp "3" _ OK <- signSendRecv sh sKey ("3", sId, _SEND' "hello") Resp "" _ (MSG mId1 _ _ msg1) <- tGet rh From 756bd1eca70dd3e79f252860bb2178eeae766973 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Mon, 20 Jun 2022 14:36:59 +0100 Subject: [PATCH 069/131] remove timeout from stopping server (#412) * remove timeout from stopping server * s/0/infinity/ --- scripts/smp-server-linode.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/smp-server-linode.sh b/scripts/smp-server-linode.sh index aedf975de..ba26a6a0d 100644 --- a/scripts/smp-server-linode.sh +++ b/scripts/smp-server-linode.sh @@ -158,6 +158,7 @@ Description=SMP server Type=simple ExecStart=/bin/sh -c "exec $binary start >> /var/opt/simplex/smp-server.log 2>&1" KillSignal=SIGINT +TimeoutStopSec=infinity Restart=always RestartSec=10 LimitNOFILE=1000000 From 08a4b41691ebdfc3c3f09c785ef5b4e476469ce9 Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Tue, 21 Jun 2022 12:47:40 +0400 Subject: [PATCH 070/131] v2.3.2 (#414) --- apps/ntf-server/Main.hs | 2 +- package.yaml | 2 +- simplexmq.cabal | 2 +- src/Simplex/Messaging/Transport.hs | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/apps/ntf-server/Main.hs b/apps/ntf-server/Main.hs index 5111955cd..6d40a8bdc 100644 --- a/apps/ntf-server/Main.hs +++ b/apps/ntf-server/Main.hs @@ -41,7 +41,7 @@ ntfServerCLIConfig = fingerprintFile = combine cfgPath "fingerprint", defaultServerPort = "443", executableName = "ntf-server", - serverVersion = "SMP notifications server v0.1.0", + serverVersion = "SMP notifications server v0.2.0", mkIniFile = \enableStoreLog defaultServerPort -> "[STORE_LOG]\n\ \# The server uses STM memory for persistence,\n\ diff --git a/package.yaml b/package.yaml index 18a3d2a8f..a961b028b 100644 --- a/package.yaml +++ b/package.yaml @@ -1,5 +1,5 @@ name: simplexmq -version: 2.3.1 +version: 2.3.2 synopsis: SimpleXMQ message broker description: | This package includes <./docs/Simplex-Messaging-Server.html server>, diff --git a/simplexmq.cabal b/simplexmq.cabal index dcf76cd0d..bd5c45242 100644 --- a/simplexmq.cabal +++ b/simplexmq.cabal @@ -5,7 +5,7 @@ cabal-version: 1.12 -- see: https://github.com/sol/hpack name: simplexmq -version: 2.3.1 +version: 2.3.2 synopsis: SimpleXMQ message broker description: This package includes <./docs/Simplex-Messaging-Server.html server>, <./docs/Simplex-Messaging-Client.html client> and diff --git a/src/Simplex/Messaging/Transport.hs b/src/Simplex/Messaging/Transport.hs index 7f73b8f64..c5f74dd46 100644 --- a/src/Simplex/Messaging/Transport.hs +++ b/src/Simplex/Messaging/Transport.hs @@ -96,7 +96,7 @@ supportedSMPServerVRange :: VersionRange supportedSMPServerVRange = mkVersionRange 1 2 simplexMQVersion :: String -simplexMQVersion = "2.3.1" +simplexMQVersion = "2.3.2" -- * Transport connection class From 30b36cd75685dff868dbbe38b14f720155d5d56c Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Tue, 21 Jun 2022 13:59:50 +0400 Subject: [PATCH 071/131] ntf: notification server linode script (#413) --- scripts/ntf-server-linode.sh | 181 +++++++++++++++++++++++++++++++++++ 1 file changed, 181 insertions(+) create mode 100644 scripts/ntf-server-linode.sh diff --git a/scripts/ntf-server-linode.sh b/scripts/ntf-server-linode.sh new file mode 100644 index 000000000..ca7415007 --- /dev/null +++ b/scripts/ntf-server-linode.sh @@ -0,0 +1,181 @@ +#!/bin/bash + +# +# TODO review +# +# + +# Log all stdout output to stackscript.log +exec &> >(tee -i /var/log/stackscript.log) + +# Uncomment next line to enable debugging features +# set -xeo pipefail + +cd $HOME + +# https://superuser.com/questions/1638779/automatic-yess-to-linux-update-upgrade +# https://superuser.com/questions/1412054/non-interactive-apt-upgrade +sudo DEBIAN_FRONTEND=noninteractive \ + apt-get \ + -o Dpkg::Options::=--force-confold \ + -o Dpkg::Options::=--force-confdef \ + -y --allow-downgrades --allow-remove-essential --allow-change-held-packages \ + update + +sudo DEBIAN_FRONTEND=noninteractive \ + apt-get \ + -o Dpkg::Options::=--force-confold \ + -o Dpkg::Options::=--force-confdef \ + -y --allow-downgrades --allow-remove-essential --allow-change-held-packages \ + dist-upgrade + +# TODO install unattended-upgrades +sudo DEBIAN_FRONTEND=noninteractive \ + apt-get \ + -o Dpkg::Options::=--force-confold \ + -o Dpkg::Options::=--force-confdef \ + -y --allow-downgrades --allow-remove-essential --allow-change-held-packages \ + install jq + +# Add firewall +echo "y" | ufw enable + +# Open ports +ufw allow ssh +ufw allow https +ufw allow 5223 + +# Increase file descriptors limit +echo 'fs.file-max = 1000000' >> /etc/sysctl.conf +echo 'fs.inode-max = 1000000' >> /etc/sysctl.conf +echo 'root soft nofile unlimited' >> /etc/security/limits.conf +echo 'root hard nofile unlimited' >> /etc/security/limits.conf + +# Download latest release +bin_dir="/opt/simplex-notifications/bin" +binary="$bin_dir/ntf-server" +mkdir -p $bin_dir +curl -L -o $binary https://github.com/simplex-chat/simplexmq/releases/latest/download/ntf-server-ubuntu-20_04-x86-64 +chmod +x $binary + +# / Add to PATH +cat > /etc/profile.d/simplex.sh << EOF +#!/bin/bash + +export PATH="$PATH:$bin_dir" + +EOF +# Add to PATH / + +# Source and test PATH +source /etc/profile.d/simplex.sh +ntf-server --version + +# Initialize server +init_opts=() + +ip_address=$(curl ifconfig.me) +init_opts+=(--ip $ip_address) + +[[ -n "$FQDN" ]] && init_opts+=(-n $FQDN) + +ntf-server init "${init_opts[@]}" + +# Server fingerprint +fingerprint=$(cat /etc/opt/simplex-notifications/fingerprint) + +# Determine server address to specify in welcome script and Linode tag +# ! If FQDN was provided and used as part of server initialization, server's certificate will not pass validation at client +# ! if client tries to connect by server's IP address, so we have to specify FQDN as server address in Linode tag and +# ! in welcome script regardless of creation of A record in Linode +# ! https://hackage.haskell.org/package/x509-validation-1.6.10/docs/src/Data-X509-Validation.html#validateCertificateName +if [[ -n "$FQDN" ]]; then + server_address=$FQDN +else + server_address=$ip_address +fi + +# Set up welcome script +on_login_script="/opt/simplex-notifications/on_login.sh" + +# TODO fix address +# / Welcome script +cat > $on_login_script << EOF +#!/bin/bash + +fingerprint=\$1 +server_address=\$2 + +cat << EOF2 +******************************************************************************** + +SimpleX notifications server address: smp://\$fingerprint@\$server_address +Check server status with: systemctl status ntf-server + +To keep this server secure, the UFW firewall is enabled. +All ports are BLOCKED except 22 (SSH), 443 (HTTPS), 5223 (notifications server). + +******************************************************************************** +To stop seeing this message delete line - bash /opt/simplex-notifications/on_login.sh - from /root/.bashrc +EOF2 + +EOF +# Welcome script / + +chmod +x $on_login_script +echo "bash $on_login_script $fingerprint $server_address" >> /root/.bashrc + +# Create A record and update Linode's tags +if [[ -n "$API_TOKEN" ]]; then + if [[ -n "$FQDN" ]]; then + domain_address=$(echo $FQDN | rev | cut -d "." -f 1,2 | rev) + domain_id=$(curl -H "Authorization: Bearer $API_TOKEN" https://api.linode.com/v4/domains \ + | jq --arg da "$domain_address" '.data[] | select( .domain == $da ) | .id') + if [[ -n $domain_id ]]; then + curl \ + -s -H "Content-Type: application/json" \ + -H "Authorization: Bearer $API_TOKEN" \ + -X POST -d "{\"type\":\"A\",\"name\":\"$FQDN\",\"target\":\"$ip_address\"}" \ + https://api.linode.com/v4/domains/${domain_id}/records + fi + fi + + version=$(ntf-server --version | cut -d ' ' -f 3-) + + curl \ + -s -H "Content-Type: application/json" \ + -H "Authorization: Bearer $API_TOKEN" \ + -X PUT -d "{\"tags\":[\"$server_address\",\"$fingerprint\",\"$version\"]}" \ + https://api.linode.com/v4/linode/instances/$LINODE_ID +fi + +# / Create systemd service +cat > /etc/systemd/system/ntf-server.service << EOF +[Unit] +Description=SimpleX notifications server + +[Service] +Environment="APNS_KEY_FILE=/etc/opt/simplex-notifications/AuthKey.p8" +Environment="APNS_KEY_ID=$APNS_KEY_ID" +Type=simple +ExecStart=/bin/sh -c "exec $binary start >> /var/opt/simplex-notifications/ntf-server.log 2>&1" +KillSignal=SIGINT +Restart=always +RestartSec=10 +LimitNOFILE=1000000 +LimitNOFILESoft=1000000 + +[Install] +WantedBy=multi-user.target + +EOF +# Create systemd service / + +# Start systemd service +chmod 644 /etc/systemd/system/ntf-server.service +sudo systemctl enable ntf-server +# ! APNS key file and certificate have to be copied manually +# sudo systemctl start ntf-server + +# Reboot Linode to apply upgrades +# sudo reboot From cd9cb19765e502e1472ff30e08c3a7fe6c3d9a1e Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Tue, 21 Jun 2022 14:09:05 +0400 Subject: [PATCH 072/131] ntf: remove debug output from test --- tests/AgentTests/NotificationTests.hs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/AgentTests/NotificationTests.hs b/tests/AgentTests/NotificationTests.hs index 3666f007f..c360d87eb 100644 --- a/tests/AgentTests/NotificationTests.hs +++ b/tests/AgentTests/NotificationTests.hs @@ -142,9 +142,7 @@ testNtfTokenSecondRegistration APNSMockServer {apnsQ} = do NTActive <- checkNtfToken a tkn -- and the second is not yet verified liftIO $ threadDelay 50000 - r <- checkNtfToken a' tkn - liftIO . print $ "checkNtfToken a' tkn: " <> show r - unless (r == NTConfirmed) $ error "bad status" + NTConfirmed <- checkNtfToken a' tkn -- now the second token registration is verified verifyNtfToken a' tkn verification' nonce' -- the first registration is removed From d78fb8761e3f2e87ebbfe3f75f490fed992fc463 Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Tue, 21 Jun 2022 17:45:28 +0400 Subject: [PATCH 073/131] ntf: change default port for notifications server SMP client agent (#415) --- src/Simplex/Messaging/Client/Agent.hs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Simplex/Messaging/Client/Agent.hs b/src/Simplex/Messaging/Client/Agent.hs index dd3b0e90b..bdb118e6f 100644 --- a/src/Simplex/Messaging/Client/Agent.hs +++ b/src/Simplex/Messaging/Client/Agent.hs @@ -5,6 +5,7 @@ {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE RankNTypes #-} {-# LANGUAGE ScopedTypeVariables #-} +{-# LANGUAGE TypeApplications #-} {-# OPTIONS_GHC -fno-warn-orphans #-} module Simplex.Messaging.Client.Agent where @@ -28,6 +29,7 @@ import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Protocol (BrokerMsg, ProtocolServer (..), QueueId, SMPServer) import Simplex.Messaging.TMap (TMap) import qualified Simplex.Messaging.TMap as TM +import Simplex.Messaging.Transport import Simplex.Messaging.Util (catchAll_, tryE, whenM, ($>>=)) import System.Timeout (timeout) import UnliftIO (async, forConcurrently_) @@ -61,7 +63,7 @@ data SMPClientAgentConfig = SMPClientAgentConfig defaultSMPClientAgentConfig :: SMPClientAgentConfig defaultSMPClientAgentConfig = SMPClientAgentConfig - { smpCfg = defaultClientConfig, + { smpCfg = defaultClientConfig {defaultTransport = ("5223", transport @TLS)}, reconnectInterval = RetryInterval { initialInterval = second, From 5b61c6bccdce8563bca2041484054a28707d8e50 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Tue, 21 Jun 2022 18:52:26 +0100 Subject: [PATCH 074/131] revert tls to 1.5.7 (#416) --- package.yaml | 2 +- simplexmq.cabal | 10 +++++----- stack.yaml | 3 ++- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/package.yaml b/package.yaml index 18a3d2a8f..37d7c6b93 100644 --- a/package.yaml +++ b/package.yaml @@ -62,7 +62,7 @@ dependencies: - time == 1.9.* - time-compat == 1.9.* - time-manager == 0.0.* - - tls >= 1.6.0 && < 1.7 + - tls >= 1.5.7 && < 1.6 - transformers == 0.5.* - unliftio == 0.2.* - unliftio-core == 0.2.* diff --git a/simplexmq.cabal b/simplexmq.cabal index c3519aa03..107351f71 100644 --- a/simplexmq.cabal +++ b/simplexmq.cabal @@ -129,7 +129,7 @@ library , time ==1.9.* , time-compat ==1.9.* , time-manager ==0.0.* - , tls >=1.6.0 && <1.7 + , tls >=1.5.7 && <1.6 , transformers ==0.5.* , unliftio ==0.2.* , unliftio-core ==0.2.* @@ -190,7 +190,7 @@ executable ntf-server , time ==1.9.* , time-compat ==1.9.* , time-manager ==0.0.* - , tls >=1.6.0 && <1.7 + , tls >=1.5.7 && <1.6 , transformers ==0.5.* , unliftio ==0.2.* , unliftio-core ==0.2.* @@ -251,7 +251,7 @@ executable smp-agent , time ==1.9.* , time-compat ==1.9.* , time-manager ==0.0.* - , tls >=1.6.0 && <1.7 + , tls >=1.5.7 && <1.6 , transformers ==0.5.* , unliftio ==0.2.* , unliftio-core ==0.2.* @@ -312,7 +312,7 @@ executable smp-server , time ==1.9.* , time-compat ==1.9.* , time-manager ==0.0.* - , tls >=1.6.0 && <1.7 + , tls >=1.5.7 && <1.6 , transformers ==0.5.* , unliftio ==0.2.* , unliftio-core ==0.2.* @@ -393,7 +393,7 @@ test-suite smp-server-test , time-compat ==1.9.* , time-manager ==0.0.* , timeit ==2.0.* - , tls >=1.6.0 && <1.7 + , tls >=1.5.7 && <1.6 , transformers ==0.5.* , unliftio ==0.2.* , unliftio-core ==0.2.* diff --git a/stack.yaml b/stack.yaml index f6a60ed74..13f199146 100644 --- a/stack.yaml +++ b/stack.yaml @@ -38,7 +38,8 @@ extra-deps: - cryptostore-0.2.1.0@sha256:9896e2984f36a1c8790f057fd5ce3da4cbcaf8aa73eb2d9277916886978c5b19,3881 - network-3.1.2.7@sha256:e3d78b13db9512aeb106e44a334ab42b7aa48d26c097299084084cb8be5c5568,4888 - simple-logger-0.1.0@sha256:be8ede4bd251a9cac776533bae7fb643369ebd826eb948a9a18df1a8dd252ff8,1079 - - tls-1.6.0@sha256:7ae39373fd2de27fb80e90f76d22aeeb9a074a0ddd120cbd02c9c52f516a9e55,6987 # below dependancies are to update Aeson to 2.0.3 + - tls-1.5.7@sha256:1cc30253a9696b65a9cafc0317fbf09f7dcea15e3a145ed6c9c0e28c632fa23a,6991 + # below dependancies are to update Aeson to 2.0.3 - OneTuple-0.3.1@sha256:a848c096c9d29e82ffdd30a9998aa2931cbccb3a1bc137539d80f6174d31603e,2262 - attoparsec-0.14.4@sha256:79584bdada8b730cb5138fca8c35c76fbef75fc1d1e01e6b1d815a5ee9843191,5810 - hashable-1.4.0.2@sha256:0cddd0229d1aac305ea0404409c0bbfab81f075817bd74b8b2929eff58333e55,5005 From 0d9d549cead36dbc8404ee8617dde8d01a81704e Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Wed, 22 Jun 2022 08:12:18 +0100 Subject: [PATCH 075/131] unlock next GET with ACK (#418) --- src/Simplex/Messaging/Agent.hs | 4 +-- src/Simplex/Messaging/Agent/Client.hs | 37 +++++++++++++++------------ 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/src/Simplex/Messaging/Agent.hs b/src/Simplex/Messaging/Agent.hs index 7fc5bee69..03452a84e 100644 --- a/src/Simplex/Messaging/Agent.hs +++ b/src/Simplex/Messaging/Agent.hs @@ -390,8 +390,8 @@ getConnectionMessage' :: AgentMonad m => AgentClient -> ConnId -> m (Maybe SMPMs getConnectionMessage' c connId = do whenM (atomically $ hasActiveSubscription c connId) . throwError $ CMD PROHIBITED withStore c (`getConn` connId) >>= \case - SomeConn _ (DuplexConnection _ rq _) -> getQueueMessage c rq connId - SomeConn _ (RcvConnection _ rq) -> getQueueMessage c rq connId + SomeConn _ (DuplexConnection _ rq _) -> getQueueMessage c rq + SomeConn _ (RcvConnection _ rq) -> getQueueMessage c rq SomeConn _ ContactConnection {} -> throwError $ CMD PROHIBITED SomeConn _ SndConnection {} -> throwError $ CONN SIMPLEX diff --git a/src/Simplex/Messaging/Agent/Client.hs b/src/Simplex/Messaging/Agent/Client.hs index ea62e9204..e62aa5e04 100644 --- a/src/Simplex/Messaging/Agent/Client.hs +++ b/src/Simplex/Messaging/Agent/Client.hs @@ -117,7 +117,7 @@ data AgentClient = AgentClient connMsgsQueued :: TMap ConnId Bool, smpQueueMsgQueues :: TMap (ConnId, SMPServer, SMP.SenderId) (TQueue InternalId), smpQueueMsgDeliveries :: TMap (ConnId, SMPServer, SMP.SenderId) (Async ()), - getMsgLocks :: TMap (ConnId, SMPServer, SMP.RecipientId) (TMVar ()), + getMsgLocks :: TMap (SMPServer, SMP.RecipientId) (TMVar ()), reconnections :: TVar [Async ()], asyncClients :: TVar [Async ()], clientId :: Int, @@ -205,6 +205,7 @@ getSMPServerClient c@AgentClient {active, smpClients, msgQ} srv = do whenM (readTVarIO active) $ do let conns = M.keys cs unless (null conns) . notifySub "" $ DOWN srv conns + atomically $ mapM_ (releaseGetLock c) cs unliftIO u reconnectServer reconnectServer :: m () @@ -335,6 +336,7 @@ closeAgentClient c = liftIO $ do clear subscrConns clear connMsgsQueued clear smpQueueMsgQueues + clear getMsgLocks where clientTimeout sel = tcpTimeout . sel . config $ agentEnv c clear :: (AgentClient -> TMap k a) -> IO () @@ -427,7 +429,7 @@ newRcvQueue_ a c srv = do subscribeQueue :: AgentMonad m => AgentClient -> RcvQueue -> ConnId -> m () subscribeQueue c rq@RcvQueue {server, rcvPrivateKey, rcvId} connId = do - whenM (atomically . TM.member (connId, server, rcvId) $ getMsgLocks c) . throwError $ CMD PROHIBITED + whenM (atomically . TM.member (server, rcvId) $ getMsgLocks c) . throwError $ CMD PROHIBITED atomically $ addPendingSubscription c rq connId withLogClient c server rcvId "SUB" $ \smp -> do liftIO (runExceptT $ subscribeSMPQueue smp rcvPrivateKey rcvId) >>= \case @@ -506,22 +508,18 @@ sendInvitation c (Compatible SMPQueueInfo {smpServer, senderId, dhPublicKey}) co agentCbEncryptOnce dhPublicKey . smpEncode $ SMP.ClientMessage SMP.PHEmpty $ smpEncode agentEnvelope -getQueueMessage :: AgentMonad m => AgentClient -> RcvQueue -> ConnId -> m (Maybe SMPMsgMeta) -getQueueMessage c@AgentClient {getMsgLocks} RcvQueue {server, rcvId, rcvPrivateKey} connId = - E.bracket (atomically createTakeLock) (atomically . (`putTMVar` ())) $ \_ -> - withLogClient c server rcvId "GET" $ \smp -> - getSMPMessage smp rcvPrivateKey rcvId +getQueueMessage :: AgentMonad m => AgentClient -> RcvQueue -> m (Maybe SMPMsgMeta) +getQueueMessage c RcvQueue {server, rcvId, rcvPrivateKey} = do + atomically createTakeGetLock + withLogClient c server rcvId "GET" $ \smp -> + getSMPMessage smp rcvPrivateKey rcvId where - k = (connId, server, rcvId) - createTakeLock = do - l <- TM.lookup k getMsgLocks >>= maybe newLock pure - takeTMVar l - pure l + createTakeGetLock = TM.alterF takeLock (server, rcvId) $ getMsgLocks c where - newLock = do - l <- newTMVar () - TM.insert k l getMsgLocks - pure l + takeLock l_ = do + l <- maybe (newTMVar ()) pure l_ + takeTMVar l + pure $ Just l secureQueue :: AgentMonad m => AgentClient -> RcvQueue -> SndPublicVerifyKey -> m () secureQueue c RcvQueue {server, rcvId, rcvPrivateKey} senderKey = @@ -534,9 +532,14 @@ enableQueueNotifications c RcvQueue {server, rcvId, rcvPrivateKey} notifierKey r enableSMPQueueNotifications smp rcvPrivateKey rcvId notifierKey rcvNtfPublicDhKey sendAck :: AgentMonad m => AgentClient -> RcvQueue -> MsgId -> m () -sendAck c RcvQueue {server, rcvId, rcvPrivateKey} msgId = +sendAck c rq@RcvQueue {server, rcvId, rcvPrivateKey} msgId = do withLogClient c server rcvId "ACK" $ \smp -> ackSMPMessage smp rcvPrivateKey rcvId msgId + atomically $ releaseGetLock c rq + +releaseGetLock :: AgentClient -> RcvQueue -> STM () +releaseGetLock c RcvQueue {server, rcvId} = + TM.lookup (server, rcvId) (getMsgLocks c) >>= mapM_ (void . (`tryPutTMVar` ())) suspendQueue :: AgentMonad m => AgentClient -> RcvQueue -> m () suspendQueue c RcvQueue {server, rcvId, rcvPrivateKey} = From ffb4b4763c05e0c171e03a5eb341130fc00782bb Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Wed, 22 Jun 2022 11:00:46 +0100 Subject: [PATCH 076/131] allow repeated NKEY (#419) * allow repeated NKEY * test --- src/Simplex/Messaging/Server/QueueStore/STM.hs | 17 ++++++++--------- tests/ServerTests.hs | 4 +++- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/Simplex/Messaging/Server/QueueStore/STM.hs b/src/Simplex/Messaging/Server/QueueStore/STM.hs index 00e87d520..101c26251 100644 --- a/src/Simplex/Messaging/Server/QueueStore/STM.hs +++ b/src/Simplex/Messaging/Server/QueueStore/STM.hs @@ -62,15 +62,14 @@ instance MonadQueueStore QueueStore STM where _ -> writeTVar qVar q {senderKey = Just sKey} $> Just q addQueueNotifier :: QueueStore -> RecipientId -> NtfCreds -> STM (Either ErrorType QueueRec) - addQueueNotifier QueueStore {queues, notifiers} rId ntfCreds@NtfCreds {notifierId} = do - ifM (TM.member notifierId notifiers) (pure $ Left DUPLICATE_) $ - withQueue rId queues $ \qVar -> - readTVar qVar >>= \q -> case notifier q of - Just _ -> pure Nothing - _ -> do - writeTVar qVar q {notifier = Just ntfCreds} - TM.insert notifierId rId notifiers - pure $ Just q + addQueueNotifier QueueStore {queues, notifiers} rId ntfCreds@NtfCreds {notifierId = nId} = do + ifM (TM.member nId notifiers) (pure $ Left DUPLICATE_) $ + withQueue rId queues $ \qVar -> do + q <- readTVar qVar + forM_ (notifier q) $ (`TM.delete` notifiers) . notifierId + writeTVar qVar q {notifier = Just ntfCreds} + TM.insert nId rId notifiers + pure $ Just q suspendQueue :: QueueStore -> RecipientId -> STM (Either ErrorType ()) suspendQueue QueueStore {queues} rId = diff --git a/tests/ServerTests.hs b/tests/ServerTests.hs index 591f4c085..72d87fb28 100644 --- a/tests/ServerTests.hs +++ b/tests/ServerTests.hs @@ -612,7 +612,9 @@ testMessageNotifications (ATransport t) = (sId, rId, rKey, dhShared) <- createAndSecureQueue rh sPub let dec nonce = C.cbDecrypt dhShared (C.cbNonce nonce) (rcvNtfPubDhKey, _) <- C.generateKeyPair' - Resp "1" _ (NID nId _) <- signSendRecv rh rKey ("1", rId, NKEY nPub rcvNtfPubDhKey) + Resp "1" _ (NID nId' _) <- signSendRecv rh rKey ("1", rId, NKEY nPub rcvNtfPubDhKey) + Resp "1a" _ (NID nId _) <- signSendRecv rh rKey ("1a", rId, NKEY nPub rcvNtfPubDhKey) + nId' `shouldNotBe` nId Resp "2" _ OK <- signSendRecv nh1 nKey ("2", nId, NSUB) Resp "3" _ OK <- signSendRecv sh sKey ("3", sId, _SEND' "hello") Resp "" _ (MSG mId1 _ _ msg1) <- tGet rh From ef4d4c9e16abfaca32f1b9a95d50082b2da0bf06 Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Wed, 22 Jun 2022 20:32:32 +0400 Subject: [PATCH 077/131] ntf: remove notification subscription (#417) --- protocol/simplex-messaging.md | 41 ++++++++++++++++--- .../Messaging/Notifications/Server/Store.hs | 1 + src/Simplex/Messaging/Protocol.hs | 6 +++ src/Simplex/Messaging/Server.hs | 6 +++ src/Simplex/Messaging/Server/QueueStore.hs | 1 + .../Messaging/Server/QueueStore/STM.hs | 8 ++++ src/Simplex/Messaging/Server/StoreLog.hs | 8 ++++ tests/ServerTests.hs | 10 ++++- 8 files changed, 75 insertions(+), 6 deletions(-) diff --git a/protocol/simplex-messaging.md b/protocol/simplex-messaging.md index ecd108094..e48f31b68 100644 --- a/protocol/simplex-messaging.md +++ b/protocol/simplex-messaging.md @@ -24,6 +24,7 @@ - [Subscribe to queue](#subscribe-to-queue) - [Secure queue command](#secure-queue-command) - [Enable notifications command](#enable-notifications-command) + - [Disable notifications command](#disable-notifications-command) - [Acknowledge message delivery](#acknowledge-message-delivery) - [Suspend queue](#suspend-queue) - [Delete queue](#delete-queue) @@ -355,6 +356,7 @@ To protect the privacy of the recipients, there are several commands in SMP prot The clients can optionally instruct a dedicated push notification server to subscribe to notifications and deliver push notifications to the device, which can then retrieve the messages in the background and send local notifications to the user - this is out of scope of SMP protocol. The commands that SMP protocol provides to allow it: - `enableNotifications` (`"NKEY"`) with `notifierId` (`"NID"`) response - see [Enable notifications command](#enable-notifications-command). +- `disableNotifications` (`"NDEL"`) - see [Disable notifications command](#disable-notifications-command). - `subscribeNotifications` (`"NSUB"`) - see [Subscribe to queue notifications](#subscribe-to-queue-notifications). - `messageNotification` (`"NMSG"`) - see [Deliver message notification](#deliver-message-notification). @@ -401,7 +403,7 @@ Commands syntax below is provided using [ABNF][8] with [case-sensitive strings e ```abnf smpCommand = ping / recipientCmd / send / subscribeNotifications / serverMsg -recipientCmd = create / subscribe / secure / enableNotifications / +recipientCmd = create / subscribe / secure / enableNotifications / disableNotifications / acknowledge / suspend / delete serverMsg = queueIds / message / notifierId / messageNotification / unsubscribed / ok / error @@ -502,22 +504,42 @@ Once the queue is secured only signed messages can be sent to it. This command is sent by the recipient to the server to add notifier's key to the queue, to allow push notifications server to receive notifications when the message arrives, via a separate queue ID, without receiving message content. ```abnf -enableNotifications = %s"NKEY " notifierKey +enableNotifications = %s"NKEY " notifierKey recipientNotificationDhPublicKey notifierKey = length x509encoded ; the notifier's Ed25519 or Ed448 public key public key to verify NSUB command for this queue + +recipientNotificationDhPublicKey = length x509encoded +; the recipient's Curve25519 key for DH exchange to derive the secret +; that the server will use to encrypt notification metadata (encryptedNMsgMeta in NMSG) +; using [NaCl crypto_box][16] encryption scheme (curve25519xsalsa20poly1305). ``` The server will respond with `notifierId` response if notifications were enabled and the notifier's key was successfully added to the queue: ```abnf -notifierId = %s"NID " notifierId +notifierId = %s"NID " notifierId srvNotificationDhPublicKey notifierId = length *OCTET ; 16-24 bytes +srvNotificationDhPublicKey = length x509encoded +; the server's Curve25519 key for DH exchange to derive the secret +; that the server will use to encrypt notification metadata to the recipient (encryptedNMsgMeta in NMSG) ``` This response is sent with the recipient's queue ID (the third part of the transmission). To receive the message notifications, `subscribeNotifications` command ("NSUB") must be sent signed with the notifier's key. +#### Disable notifications command + +This command is sent by the recipient to the server to remove notifier's credentials from the queue: + +```abnf +disableNotifications = %s"NDEL" +``` + +The server must respond `ok` to this command if it was successful. + +Once notifier's credentials are removed server will no longer send "NMSG" for this queue to notifier. + #### Acknowledge message delivery The recipient should send the acknowledgement of message delivery once the message was stored in the client, to notify the server that the message should be deleted: @@ -735,10 +757,19 @@ See its syntax in [Enable notifications command](#enable-notifications-command) The server must deliver message notifications to all simplex queues that were subscribed with `subscribeNotifications` command ("NSUB") on the currently open transport connection. The syntax for the message notification delivery is: ```abnf -messageNotification = %s"NMSG" +messageNotification = %s"NMSG " nmsgNonce encryptedNMsgMeta + +encryptedNMsgMeta = +; metadata E2E encrypted between server and recipient containing server's message ID and timestamp (allows extension), +; to be passed to the recipient by the notifier for them to decrypt +; with key negotiated in NKEY and NID commands using nmsgNonce + +nmsgNonce = +; nonce used by the server for encryption of message metadata, to be passed to the recipient by the notifier +; for them to use in decryption of E2E encrypted metadata ``` -Message notification does not contain any message data or meta-data, it only notifies that the message is available. +Message notification does not contain any message data or non E2E encrypted metadata. #### Subscription END notification diff --git a/src/Simplex/Messaging/Notifications/Server/Store.hs b/src/Simplex/Messaging/Notifications/Server/Store.hs index c7d9ac89f..50291f5ca 100644 --- a/src/Simplex/Messaging/Notifications/Server/Store.hs +++ b/src/Simplex/Messaging/Notifications/Server/Store.hs @@ -24,6 +24,7 @@ import Simplex.Messaging.Util (whenM, ($>>=)) data NtfStore = NtfStore { tokens :: TMap NtfTokenId NtfTknData, + -- multiple registrations exist to protect from malicious registrations if token is compromised tokenRegistrations :: TMap DeviceToken (TMap ByteString NtfTokenId), subscriptions :: TMap NtfSubscriptionId NtfSubData, tokenSubscriptions :: TMap NtfTokenId (TVar (Set NtfSubscriptionId)), diff --git a/src/Simplex/Messaging/Protocol.hs b/src/Simplex/Messaging/Protocol.hs index f9ed5e321..6bda98677 100644 --- a/src/Simplex/Messaging/Protocol.hs +++ b/src/Simplex/Messaging/Protocol.hs @@ -221,6 +221,7 @@ data Command (p :: Party) where SUB :: Command Recipient KEY :: SndPublicVerifyKey -> Command Recipient NKEY :: NtfPublicVerifyKey -> RcvNtfPublicDhKey -> Command Recipient + NDEL :: Command Recipient GET :: Command Recipient -- ACK v1 has to be supported for encoding/decoding -- ACK :: Command Recipient @@ -303,6 +304,7 @@ data CommandTag (p :: Party) where SUB_ :: CommandTag Recipient KEY_ :: CommandTag Recipient NKEY_ :: CommandTag Recipient + NDEL_ :: CommandTag Recipient GET_ :: CommandTag Recipient ACK_ :: CommandTag Recipient OFF_ :: CommandTag Recipient @@ -342,6 +344,7 @@ instance PartyI p => Encoding (CommandTag p) where SUB_ -> "SUB" KEY_ -> "KEY" NKEY_ -> "NKEY" + NDEL_ -> "NDEL" GET_ -> "GET" ACK_ -> "ACK" OFF_ -> "OFF" @@ -357,6 +360,7 @@ instance ProtocolMsgTag CmdTag where "SUB" -> Just $ CT SRecipient SUB_ "KEY" -> Just $ CT SRecipient KEY_ "NKEY" -> Just $ CT SRecipient NKEY_ + "NDEL" -> Just $ CT SRecipient NDEL_ "GET" -> Just $ CT SRecipient GET_ "ACK" -> Just $ CT SRecipient ACK_ "OFF" -> Just $ CT SRecipient OFF_ @@ -651,6 +655,7 @@ instance PartyI p => ProtocolEncoding (Command p) where SUB -> e SUB_ KEY k -> e (KEY_, ' ', k) NKEY k dhKey -> e (NKEY_, ' ', k, dhKey) + NDEL -> e NDEL_ GET -> e GET_ ACK msgId | v == 1 -> e ACK_ @@ -698,6 +703,7 @@ instance ProtocolEncoding Cmd where SUB_ -> pure SUB KEY_ -> KEY <$> _smpP NKEY_ -> NKEY <$> _smpP <*> smpP + NDEL_ -> pure NDEL GET_ -> pure GET ACK_ | v == 1 -> pure $ ACK "" diff --git a/src/Simplex/Messaging/Server.hs b/src/Simplex/Messaging/Server.hs index e21647d0c..a32fc93a3 100644 --- a/src/Simplex/Messaging/Server.hs +++ b/src/Simplex/Messaging/Server.hs @@ -334,6 +334,7 @@ client clnt@Client {subscriptions, ntfSubscriptions, rcvQ, sndQ} Server {subscri ACK msgId -> acknowledgeMsg msgId KEY sKey -> secureQueue_ st sKey NKEY nKey dhKey -> addQueueNotifier_ st nKey dhKey + NDEL -> deleteQueueNotifier_ st OFF -> suspendQueue_ st DEL -> delQueueAndMsgs st where @@ -405,6 +406,11 @@ client clnt@Client {subscriptions, ntfSubscriptions, rcvQ, sndQ} Server {subscri withLog $ \s -> logAddNotifier s queueId ntfCreds pure $ NID notifierId rcvPublicDhKey + deleteQueueNotifier_ :: QueueStore -> m (Transmission BrokerMsg) + deleteQueueNotifier_ st = do + withLog (`logDeleteNotifier` queueId) + okResp <$> atomically (deleteQueueNotifier st queueId) + suspendQueue_ :: QueueStore -> m (Transmission BrokerMsg) suspendQueue_ st = do withLog (`logDeleteQueue` queueId) diff --git a/src/Simplex/Messaging/Server/QueueStore.hs b/src/Simplex/Messaging/Server/QueueStore.hs index f059d3170..b8a2f10ad 100644 --- a/src/Simplex/Messaging/Server/QueueStore.hs +++ b/src/Simplex/Messaging/Server/QueueStore.hs @@ -39,5 +39,6 @@ class MonadQueueStore s m where getQueue :: s -> SParty p -> QueueId -> m (Either ErrorType QueueRec) secureQueue :: s -> RecipientId -> SndPublicVerifyKey -> m (Either ErrorType QueueRec) addQueueNotifier :: s -> RecipientId -> NtfCreds -> m (Either ErrorType QueueRec) + deleteQueueNotifier :: s -> RecipientId -> m (Either ErrorType ()) suspendQueue :: s -> RecipientId -> m (Either ErrorType ()) deleteQueue :: s -> RecipientId -> m (Either ErrorType ()) diff --git a/src/Simplex/Messaging/Server/QueueStore/STM.hs b/src/Simplex/Messaging/Server/QueueStore/STM.hs index 101c26251..aaf68a526 100644 --- a/src/Simplex/Messaging/Server/QueueStore/STM.hs +++ b/src/Simplex/Messaging/Server/QueueStore/STM.hs @@ -71,6 +71,14 @@ instance MonadQueueStore QueueStore STM where TM.insert nId rId notifiers pure $ Just q + deleteQueueNotifier :: QueueStore -> RecipientId -> STM (Either ErrorType ()) + deleteQueueNotifier QueueStore {queues, notifiers} rId = + withQueue rId queues $ \qVar -> do + q <- readTVar qVar + forM_ (notifier q) $ \NtfCreds {notifierId} -> TM.delete notifierId notifiers + writeTVar qVar q {notifier = Nothing} + pure $ Just () + suspendQueue :: QueueStore -> RecipientId -> STM (Either ErrorType ()) suspendQueue QueueStore {queues} rId = withQueue rId queues $ \qVar -> modifyTVar' qVar (\q -> q {status = QueueOff}) $> Just () diff --git a/src/Simplex/Messaging/Server/StoreLog.hs b/src/Simplex/Messaging/Server/StoreLog.hs index b18b57f20..49ec62c1f 100644 --- a/src/Simplex/Messaging/Server/StoreLog.hs +++ b/src/Simplex/Messaging/Server/StoreLog.hs @@ -17,6 +17,7 @@ module Simplex.Messaging.Server.StoreLog logSecureQueue, logAddNotifier, logDeleteQueue, + logDeleteNotifier, readWriteStoreLog, ) where @@ -50,6 +51,7 @@ data StoreLogRecord | SecureQueue QueueId SndPublicVerifyKey | AddNotifier QueueId NtfCreds | DeleteQueue QueueId + | DeleteNotifier QueueId instance StrEncoding QueueRec where strEncode QueueRec {recipientId, recipientKey, rcvDhSecret, senderId, senderKey, notifier} = @@ -79,12 +81,14 @@ instance StrEncoding StoreLogRecord where SecureQueue rId sKey -> strEncode (Str "SECURE", rId, sKey) AddNotifier rId ntfCreds -> strEncode (Str "NOTIFIER", rId, ntfCreds) DeleteQueue rId -> strEncode (Str "DELETE", rId) + DeleteNotifier rId -> strEncode (Str "NDELETE", rId) strP = "CREATE " *> (CreateQueue <$> strP) <|> "SECURE " *> (SecureQueue <$> strP_ <*> strP) <|> "NOTIFIER " *> (AddNotifier <$> strP_ <*> strP) <|> "DELETE " *> (DeleteQueue <$> strP) + <|> "NDELETE" *> (DeleteNotifier <$> strP) openWriteStoreLog :: FilePath -> IO (StoreLog 'WriteMode) openWriteStoreLog f = WriteStoreLog f <$> openFile f WriteMode @@ -121,6 +125,9 @@ logAddNotifier s qId ntfCreds = writeStoreLogRecord s $ AddNotifier qId ntfCreds logDeleteQueue :: StoreLog 'WriteMode -> QueueId -> IO () logDeleteQueue s = writeStoreLogRecord s . DeleteQueue +logDeleteNotifier :: StoreLog 'WriteMode -> QueueId -> IO () +logDeleteNotifier s = writeStoreLogRecord s . DeleteNotifier + readWriteStoreLog :: StoreLog 'ReadMode -> IO (Map RecipientId QueueRec, StoreLog 'WriteMode) readWriteStoreLog s@(ReadStoreLog f _) = do qs <- readQueues s @@ -151,5 +158,6 @@ readQueues (ReadStoreLog _ h) = LB.hGetContents h >>= returnResult . procStoreLo SecureQueue qId sKey -> M.adjust (\q -> q {senderKey = Just sKey}) qId m AddNotifier qId ntfCreds -> M.adjust (\q -> q {notifier = Just ntfCreds}) qId m DeleteQueue qId -> M.delete qId m + DeleteNotifier qId -> M.adjust (\q -> q {notifier = Nothing}) qId m printError :: LogParsingError -> IO () printError (e, s) = B.putStrLn $ "Error parsing log: " <> B.pack e <> " - " <> s diff --git a/tests/ServerTests.hs b/tests/ServerTests.hs index 72d87fb28..4b6c0f47c 100644 --- a/tests/ServerTests.hs +++ b/tests/ServerTests.hs @@ -625,11 +625,19 @@ testMessageNotifications (ATransport t) = Resp "" _ END <- tGet nh1 Resp "5" _ OK <- signSendRecv sh sKey ("5", sId, _SEND' "hello again") Resp "" _ (MSG mId2 _ _ msg2) <- tGet rh + Resp "5a" _ OK <- signSendRecv rh rKey ("5a", rId, ACK mId2) (dec mId2 msg2, Right "hello again") #== "delivered from queue again" Resp "" _ (NMSG _ _) <- tGet nh2 1000 `timeout` tGet @BrokerMsg nh1 >>= \case - Nothing -> return () + Nothing -> pure () Just _ -> error "nothing else should be delivered to the 1st notifier's TCP connection" + Resp "6" _ OK <- signSendRecv rh rKey ("6", rId, NDEL) + Resp "7" _ OK <- signSendRecv sh sKey ("7", sId, _SEND' "hello there") + Resp "" _ (MSG mId3 _ _ msg3) <- tGet rh + (dec mId3 msg3, Right "hello there") #== "delivered from queue again" + 1000 `timeout` tGet @BrokerMsg nh2 >>= \case + Nothing -> pure () + Just _ -> error "nothing else should be delivered to the 2nd notifier's TCP connection" testMsgExpireOnSend :: forall c. Transport c => TProxy c -> Spec testMsgExpireOnSend t = From f10e3f697cc34a0e89b41332e098c1fecacff6bd Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Thu, 23 Jun 2022 08:35:33 +0100 Subject: [PATCH 078/131] notification server TRPL command (#420) * notification server TRPL command * test * client methods * only remove current token registration --- src/Simplex/Messaging/Agent/Client.hs | 5 ++++ src/Simplex/Messaging/Notifications/Client.hs | 3 +++ .../Messaging/Notifications/Protocol.hs | 9 ++++++- src/Simplex/Messaging/Notifications/Server.hs | 10 ++++++++ .../Messaging/Notifications/Server/Store.hs | 9 +++++++ tests/NtfServerTests.hs | 24 +++++++++++++++++++ 6 files changed, 59 insertions(+), 1 deletion(-) diff --git a/src/Simplex/Messaging/Agent/Client.hs b/src/Simplex/Messaging/Agent/Client.hs index e62aa5e04..0d49e825c 100644 --- a/src/Simplex/Messaging/Agent/Client.hs +++ b/src/Simplex/Messaging/Agent/Client.hs @@ -32,6 +32,7 @@ module Simplex.Messaging.Agent.Client agentNtfRegisterToken, agentNtfVerifyToken, agentNtfCheckToken, + agentNtfReplaceToken, agentNtfDeleteToken, agentNtfEnableCron, agentNtfCreateSubscription, @@ -570,6 +571,10 @@ agentNtfCheckToken :: AgentMonad m => AgentClient -> NtfTokenId -> NtfToken -> m agentNtfCheckToken c tknId NtfToken {ntfServer, ntfPrivKey} = withLogClient c ntfServer tknId "TCHK" $ \ntf -> ntfCheckToken ntf ntfPrivKey tknId +agentNtfReplaceToken :: AgentMonad m => AgentClient -> NtfTokenId -> NtfToken -> DeviceToken -> m () +agentNtfReplaceToken c tknId NtfToken {ntfServer, ntfPrivKey} token = + withLogClient c ntfServer tknId "TRPL" $ \ntf -> ntfReplaceToken ntf ntfPrivKey tknId token + agentNtfDeleteToken :: AgentMonad m => AgentClient -> NtfTokenId -> NtfToken -> m () agentNtfDeleteToken c tknId NtfToken {ntfServer, ntfPrivKey} = withLogClient c ntfServer tknId "TDEL" $ \ntf -> ntfDeleteToken ntf ntfPrivKey tknId diff --git a/src/Simplex/Messaging/Notifications/Client.hs b/src/Simplex/Messaging/Notifications/Client.hs index 1be641a0b..789d76bac 100644 --- a/src/Simplex/Messaging/Notifications/Client.hs +++ b/src/Simplex/Messaging/Notifications/Client.hs @@ -42,6 +42,9 @@ ntfCheckToken c pKey tknId = NRTkn stat -> pure stat _ -> throwE PCEUnexpectedResponse +ntfReplaceToken :: NtfClient -> C.APrivateSignKey -> NtfTokenId -> DeviceToken -> ExceptT ProtocolClientError IO () +ntfReplaceToken c pKey tknId token = okNtfCommand (TRPL token) c pKey tknId + ntfDeleteToken :: NtfClient -> C.APrivateSignKey -> NtfTokenId -> ExceptT ProtocolClientError IO () ntfDeleteToken = okNtfCommand TDEL diff --git a/src/Simplex/Messaging/Notifications/Protocol.hs b/src/Simplex/Messaging/Notifications/Protocol.hs index b658ffd4d..32b1e6d97 100644 --- a/src/Simplex/Messaging/Notifications/Protocol.hs +++ b/src/Simplex/Messaging/Notifications/Protocol.hs @@ -55,6 +55,7 @@ data NtfCommandTag (e :: NtfEntity) where TNEW_ :: NtfCommandTag 'Token TVFY_ :: NtfCommandTag 'Token TCHK_ :: NtfCommandTag 'Token + TRPL_ :: NtfCommandTag 'Token TDEL_ :: NtfCommandTag 'Token TCRN_ :: NtfCommandTag 'Token SNEW_ :: NtfCommandTag 'Subscription @@ -71,6 +72,7 @@ instance NtfEntityI e => Encoding (NtfCommandTag e) where TNEW_ -> "TNEW" TVFY_ -> "TVFY" TCHK_ -> "TCHK" + TRPL_ -> "TRPL" TDEL_ -> "TDEL" TCRN_ -> "TCRN" SNEW_ -> "SNEW" @@ -88,6 +90,7 @@ instance ProtocolMsgTag NtfCmdTag where "TNEW" -> Just $ NCT SToken TNEW_ "TVFY" -> Just $ NCT SToken TVFY_ "TCHK" -> Just $ NCT SToken TCHK_ + "TRPL" -> Just $ NCT SToken TRPL_ "TDEL" -> Just $ NCT SToken TDEL_ "TCRN" -> Just $ NCT SToken TCRN_ "SNEW" -> Just $ NCT SSubscription SNEW_ @@ -157,6 +160,8 @@ data NtfCommand (e :: NtfEntity) where TVFY :: NtfRegCode -> NtfCommand 'Token -- | check token status TCHK :: NtfCommand 'Token + -- | replace device token (while keeping all existing subscriptions) + TRPL :: DeviceToken -> NtfCommand 'Token -- | delete token - all subscriptions will be removed and no more notifications will be sent TDEL :: NtfCommand 'Token -- | enable periodic background notification to fetch the new messages - interval is in minutes, minimum is 20, 0 to disable @@ -182,6 +187,7 @@ instance NtfEntityI e => ProtocolEncoding (NtfCommand e) where TNEW newTkn -> e (TNEW_, ' ', newTkn) TVFY code -> e (TVFY_, ' ', code) TCHK -> e TCHK_ + TRPL tkn -> e (TRPL_, ' ', tkn) TDEL -> e TDEL_ TCRN int -> e (TCRN_, ' ', int) SNEW newSub -> e (SNEW_, ' ', newSub) @@ -221,6 +227,7 @@ instance ProtocolEncoding NtfCmd where TNEW_ -> TNEW <$> _smpP TVFY_ -> TVFY <$> _smpP TCHK_ -> pure TCHK + TRPL_ -> TRPL <$> _smpP TDEL_ -> pure TDEL TCRN_ -> TCRN <$> _smpP NCT SSubscription tag -> @@ -244,7 +251,7 @@ data NtfResponseTag instance Encoding NtfResponseTag where smpEncode = \case - NRTknId_ -> "IDTKN" + NRTknId_ -> "IDTKN" -- it should be "TID", "SID" NRSubId_ -> "IDSUB" NROk_ -> "OK" NRErr_ -> "ERR" diff --git a/src/Simplex/Messaging/Notifications/Server.hs b/src/Simplex/Messaging/Notifications/Server.hs index 5d22aab5d..c9b945c29 100644 --- a/src/Simplex/Messaging/Notifications/Server.hs +++ b/src/Simplex/Messaging/Notifications/Server.hs @@ -296,6 +296,16 @@ client NtfServerClient {rcvQ, sndQ} NtfSubscriber {newSubQ} NtfPushServer {pushQ logDebug "TVFY - incorrect code or token status" pure $ NRErr AUTH TCHK -> pure $ NRTkn status + TRPL token' -> do + logDebug "TRPL - replace token" + st <- asks store + regCode <- getRegCode + atomically $ do + removeTokenRegistration st tkn + writeTVar tknStatus NTRegistered + addNtfToken st tknId tkn {token = token', tknRegCode = regCode} + writeTBQueue pushQ (tkn, PNVerification regCode) + pure NROk TDEL -> do logDebug "TDEL" st <- asks store diff --git a/src/Simplex/Messaging/Notifications/Server/Store.hs b/src/Simplex/Messaging/Notifications/Server/Store.hs index 50291f5ca..4699cb58e 100644 --- a/src/Simplex/Messaging/Notifications/Server/Store.hs +++ b/src/Simplex/Messaging/Notifications/Server/Store.hs @@ -112,6 +112,15 @@ removeInactiveTokenRegistrations st NtfTknData {ntfTknId = tId, token} = TM.delete tId' $ tokens st pure $ map snd tIds +removeTokenRegistration :: NtfStore -> NtfTknData -> STM () +removeTokenRegistration st NtfTknData {ntfTknId = tId, token, tknVerifyKey} = + TM.lookup token (tokenRegistrations st) >>= mapM_ removeReg + where + removeReg regs = + TM.lookup k regs + >>= mapM_ (\tId' -> when (tId == tId') $ TM.delete k regs) + k = C.toPubKey C.pubKeyBytes tknVerifyKey + deleteNtfToken :: NtfStore -> NtfTokenId -> STM () deleteNtfToken st tknId = do TM.lookupDelete tknId (tokens st) diff --git a/tests/NtfServerTests.hs b/tests/NtfServerTests.hs index be71a64e5..657eab235 100644 --- a/tests/NtfServerTests.hs +++ b/tests/NtfServerTests.hs @@ -138,3 +138,27 @@ testNotificationSubscription (ATransport t) = (decryptedMsg, Right "hello") #== "delivered from queue" Resp "6" _ OK <- signSendRecv rh rKey ("6", rId, ACK mId1) pure () + -- replace token + let tkn' = DeviceToken PPApns "efgh" + RespNtf "7" tId' NROk <- signSendRecvNtf nh tknKey ("7", tId, TRPL tkn') + tId `shouldBe` tId' + APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData2}, sendApnsResponse = send2} <- + atomically $ readTBQueue apnsQ + send2 APNSRespOk + let Right verification2 = ntfData2 .-> "verification" + Right nonce2 = C.cbNonce <$> ntfData2 .-> "nonce" + Right code2 = NtfRegCode <$> C.cbDecrypt dhSecret nonce2 verification2 + RespNtf "8" _ NROk <- signSendRecvNtf nh tknKey ("8", tId, TVFY code2) + RespNtf "8a" _ (NRTkn NTActive) <- signSendRecvNtf nh tknKey ("8a", tId, TCHK) + -- send message + Resp "9" _ OK <- signSendRecv sh sKey ("9", sId, _SEND' "hello 2") + APNSMockRequest {notification = notification3, sendApnsResponse = send3} <- atomically $ readTBQueue apnsQ + let APNSNotification {aps = APNSMutableContent {}, notificationData = Just ntfData3} = notification3 + Right nonce3 = C.cbNonce <$> ntfData3 .-> "nonce" + Right message3 = ntfData3 .-> "message" + Right ntfDataDecrypted3 = C.cbDecrypt dhSecret nonce3 message3 + Right APNS.PNMessageData {smpQueue = SMPQueueNtf {smpServer = smpServer3, notifierId = notifierId3}} = + parse strP (AP.INTERNAL "error parsing PNMessageData") ntfDataDecrypted3 + smpServer3 `shouldBe` srv + notifierId3 `shouldBe` nId + send3 APNSRespOk From a92827c986c3646f051392f2fc8782140f2a9048 Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Thu, 23 Jun 2022 17:05:43 +0400 Subject: [PATCH 079/131] ntf: notification server SDEL command; subscription deletion in agent (#421) --- src/Simplex/Messaging/Agent.hs | 11 ++++++ src/Simplex/Messaging/Agent/Client.hs | 11 ++++++ src/Simplex/Messaging/Agent/Env/SQLite.hs | 2 +- .../Messaging/Agent/NtfSubSupervisor.hs | 26 ++++++++++---- src/Simplex/Messaging/Agent/Store/SQLite.hs | 35 +++++++++++++++---- src/Simplex/Messaging/Client.hs | 7 ++++ src/Simplex/Messaging/Notifications/Client.hs | 15 ++++++-- src/Simplex/Messaging/Notifications/Server.hs | 12 +++++-- .../Messaging/Notifications/Server/Store.hs | 11 ++++++ tests/AgentTests/NotificationTests.hs | 11 ++++-- 10 files changed, 118 insertions(+), 23 deletions(-) diff --git a/src/Simplex/Messaging/Agent.hs b/src/Simplex/Messaging/Agent.hs index 03452a84e..449f710bc 100644 --- a/src/Simplex/Messaging/Agent.hs +++ b/src/Simplex/Messaging/Agent.hs @@ -58,6 +58,7 @@ module Simplex.Messaging.Agent enableNtfCron, checkNtfToken, deleteNtfToken, + deleteNtfSub, setAgentPhase, logConnection, ) @@ -205,6 +206,10 @@ checkNtfToken c = withAgentEnv c . checkNtfToken' c deleteNtfToken :: AgentErrorMonad m => AgentClient -> DeviceToken -> m () deleteNtfToken c = withAgentEnv c . deleteNtfToken' c +-- | Delete notification subscription for connection +deleteNtfSub :: AgentErrorMonad m => AgentClient -> ConnId -> m () +deleteNtfSub c = withAgentEnv c . deleteNtfSub' c + setAgentPhase :: AgentErrorMonad m => AgentClient -> AgentPhase -> m () setAgentPhase c = withAgentEnv c . setAgentPhase' c @@ -712,6 +717,12 @@ deleteNtfToken' c deviceToken = (Just tkn, _) -> deleteToken_ c tkn _ -> throwError $ CMD PROHIBITED +-- | Delete notification subscription for connection, in Reader monad +deleteNtfSub' :: AgentMonad m => AgentClient -> ConnId -> m () +deleteNtfSub' _c connId = do + ns <- asks ntfSupervisor + atomically $ sendNtfSubCommand ns (connId, NSCDelete) + deleteToken_ :: AgentMonad m => AgentClient -> NtfToken -> m () deleteToken_ c tkn@NtfToken {ntfTokenId, ntfTknStatus} = do ns <- asks ntfSupervisor diff --git a/src/Simplex/Messaging/Agent/Client.hs b/src/Simplex/Messaging/Agent/Client.hs index 0d49e825c..9e6d36e83 100644 --- a/src/Simplex/Messaging/Agent/Client.hs +++ b/src/Simplex/Messaging/Agent/Client.hs @@ -28,6 +28,7 @@ module Simplex.Messaging.Agent.Client RetryInterval (..), secureQueue, enableQueueNotifications, + disableQueueNotifications, sendAgentMessage, agentNtfRegisterToken, agentNtfVerifyToken, @@ -37,6 +38,7 @@ module Simplex.Messaging.Agent.Client agentNtfEnableCron, agentNtfCreateSubscription, agentNtfCheckSubscription, + agentNtfDeleteSubscription, agentCbEncrypt, agentCbDecrypt, cryptoError, @@ -532,6 +534,11 @@ enableQueueNotifications c RcvQueue {server, rcvId, rcvPrivateKey} notifierKey r withLogClient c server rcvId "NKEY " $ \smp -> enableSMPQueueNotifications smp rcvPrivateKey rcvId notifierKey rcvNtfPublicDhKey +disableQueueNotifications :: AgentMonad m => AgentClient -> RcvQueue -> m () +disableQueueNotifications c RcvQueue {server, rcvId, rcvPrivateKey} = + withLogClient c server rcvId "NDEL" $ \smp -> + disableSMPQueueNotifications smp rcvPrivateKey rcvId + sendAck :: AgentMonad m => AgentClient -> RcvQueue -> MsgId -> m () sendAck c rq@RcvQueue {server, rcvId, rcvPrivateKey} msgId = do withLogClient c server rcvId "ACK" $ \smp -> @@ -591,6 +598,10 @@ agentNtfCheckSubscription :: AgentMonad m => AgentClient -> NtfSubscriptionId -> agentNtfCheckSubscription c subId NtfToken {ntfServer, ntfPrivKey} = withLogClient c ntfServer subId "SCHK" $ \ntf -> ntfCheckSubscription ntf ntfPrivKey subId +agentNtfDeleteSubscription :: AgentMonad m => AgentClient -> NtfSubscriptionId -> NtfToken -> m () +agentNtfDeleteSubscription c subId NtfToken {ntfServer, ntfPrivKey} = + withLogClient c ntfServer subId "SDEL" $ \ntf -> ntfDeleteSubscription ntf ntfPrivKey subId + agentCbEncrypt :: AgentMonad m => SndQueue -> Maybe C.PublicKeyX25519 -> ByteString -> m ByteString agentCbEncrypt SndQueue {e2eDhSecret} e2ePubKey msg = do cmNonce <- liftIO C.randomCbNonce diff --git a/src/Simplex/Messaging/Agent/Env/SQLite.hs b/src/Simplex/Messaging/Agent/Env/SQLite.hs index de3c01b33..f2571d7e3 100644 --- a/src/Simplex/Messaging/Agent/Env/SQLite.hs +++ b/src/Simplex/Messaging/Agent/Env/SQLite.hs @@ -153,7 +153,7 @@ data NtfSupervisor = NtfSupervisor ntfSMPWorkers :: TMap SMPServer (TMVar (), Async ()) } -data NtfSupervisorCommand = NSCCreate | NSCDelete | NSCNtfWorker NtfServer +data NtfSupervisorCommand = NSCCreate | NSCDelete | NSCNtfWorker NtfServer | NSCNtfSMPWorker SMPServer newNtfSubSupervisor :: Natural -> STM NtfSupervisor newNtfSubSupervisor qSize = do diff --git a/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs b/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs index 4d00cf6c1..d2628325b 100644 --- a/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs +++ b/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs @@ -72,7 +72,7 @@ processNtfSub c (connId, cmd) = do withStore' c $ \db -> createNtfSubscription db newSub (NtfSubAction NSACreate) _ -> do let newSub = newNtfSubscription connId smpServer Nothing ntfServer NASNew currentTime - withStore' c $ \db -> createNtfSubscription db newSub (NtfSubSMPAction NSAKey) + withStore' c $ \db -> createNtfSubscription db newSub (NtfSubSMPAction NSASmpKey) -- TODO optimize? -- TODO - read action in getNtfSubscription and decide which worker to create -- TODO - SMP worker can create Ntf worker on NKEY completion @@ -81,7 +81,7 @@ processNtfSub c (connId, cmd) = do (Just _, Just ntfServer) -> do -- TODO subscription may have to be updated depending on current state: -- TODO - e.g., if it was previously marked for deletion action has to be updated - -- TODO - should action depend on subscription status or always be NSAKey (NSACreate if notifierId exists) + -- TODO - should action depend on subscription status or always be NSASmpKey (NSACreate if notifierId exists) -- TODO in case worker is currently deleting it? When deleting worker should check for updated_by_supervisor -- TODO and if it is set perform update instead of delete. If worker was not deleting it yet it should -- TODO idempotently replay commands. @@ -89,13 +89,14 @@ processNtfSub c (connId, cmd) = do addNtfWorker ntfServer _ -> pure () -- error - notification server not configured NSCDelete -> do - -- TODO delete notifier ID and Key from SMP server (SDEL, then NDEL) - withStore c (`markNtfSubscriptionForDeletion` connId) + withStore' c (`markNtfSubscriptionForDeletion` connId) case ntfServer_ of (Just ntfServer) -> addNtfWorker ntfServer _ -> pure () NSCNtfWorker ntfServer -> addNtfWorker ntfServer + NSCNtfSMPWorker smpServer -> + addNtfSMPWorker smpServer where addNtfWorker = addWorker ntfWorkers runNtfWorker addNtfSMPWorker = addWorker ntfSMPWorkers runNtfSMPWorker @@ -144,7 +145,15 @@ runNtfWorker c srv doWork = forever $ do NSEnd -> updateSubNextCheck NSEnd NSSMPAuth -> updateSub (NASCreated NSSMPAuth) (NtfSubAction NSADelete) ts Nothing -> ntfInternalError c connId "NSACheck - no subscription ID" - NSADelete -> pure () + NSADelete -> case ntfSubId of + Just nSubId -> + agentNtfDeleteSubscription c nSubId tkn + `E.finally` do + withStore' c $ \db -> + updateNtfSubscription db connId ntfSub {ntfSubId = Nothing, ntfSubStatus = NASOff, ntfSubActionTs = ts} (NtfSubSMPAction NSASmpDelete) + ns <- asks ntfSupervisor + atomically $ sendNtfSubCommand ns (connId, NSCNtfSMPWorker smpServer) + Nothing -> ntfInternalError c connId "NSADelete - no subscription ID" where updateSubNextCheck toStatus = do checkInterval <- asks $ ntfSubCheckInterval . config @@ -171,7 +180,7 @@ runNtfSMPWorker c srv doWork = forever $ do Just (ntfSub@NtfSubscription {connId, ntfServer}, ntfSubAction, rq) -> unlessM (rescheduleAction doWork ts ntfSub) $ case ntfSubAction of - NSAKey + NSASmpKey | ntfTknStatus == NTActive -> do C.SignAlg a <- asks (cmdSignAlg . config) (ntfPublicKey, ntfPrivateKey) <- liftIO $ C.generateSignatureKeyPair a @@ -183,7 +192,10 @@ runNtfSMPWorker c srv doWork = forever $ do updateNtfSubscription st connId ntfSub {ntfQueueId = Just notifierId, ntfSubStatus = NASKey, ntfSubActionTs = ts} (NtfSubAction NSACreate) ns <- asks ntfSupervisor atomically $ sendNtfSubCommand ns (connId, NSCNtfWorker ntfServer) - | otherwise -> ntfInternalError c connId "NSAKey - token not active" + | otherwise -> ntfInternalError c connId "NSASmpKey - token not active" + NSASmpDelete -> do + disableQueueNotifications c rq + withStore' c $ \db -> deleteNtfSubscription db connId _ -> noWorkToDo delay <- asks $ ntfWorkerThrottle . config liftIO $ threadDelay delay diff --git a/src/Simplex/Messaging/Agent/Store/SQLite.hs b/src/Simplex/Messaging/Agent/Store/SQLite.hs index a2d95aad6..3f4b96529 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite.hs @@ -121,8 +121,8 @@ import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Crypto.Ratchet (RatchetX448, SkippedMsgDiff (..), SkippedMsgKeys) import Simplex.Messaging.Encoding import Simplex.Messaging.Encoding.String -import Simplex.Messaging.Notifications.Client (NtfServer, NtfSubAction, NtfSubOrSMPAction (..), NtfSubSMPAction, NtfSubscription (..), NtfTknAction, NtfToken (..)) -import Simplex.Messaging.Notifications.Protocol (DeviceToken (..), NtfTknStatus (..), NtfTokenId, SMPQueueNtf (..)) +import Simplex.Messaging.Notifications.Client (NtfAgentSubStatus (NASDeleted), NtfServer, NtfSubAction (NSADelete), NtfSubOrSMPAction (..), NtfSubSMPAction, NtfSubscription (..), NtfTknAction, NtfToken (..)) +import Simplex.Messaging.Notifications.Protocol (DeviceToken (..), NtfSubscriptionId, NtfTknStatus (..), NtfTokenId, SMPQueueNtf (..)) import Simplex.Messaging.Parsers (blobFieldParser, fromTextField_) import Simplex.Messaging.Protocol (MsgBody, MsgFlags, ProtocolServer (..), RcvNtfDhSecret) import qualified Simplex.Messaging.Protocol as SMP @@ -722,8 +722,17 @@ createNtfSubscription db NtfSubscription {connId, smpServer = (SMPServer host po where (ntfSubAction, ntfSubSMPAction) = ntfSubAndSMPAction ntfAction -markNtfSubscriptionForDeletion :: DB.Connection -> ConnId -> IO (Either StoreError ()) -markNtfSubscriptionForDeletion _db _rcvQueue = pure $ Left SENotImplemented +markNtfSubscriptionForDeletion :: DB.Connection -> ConnId -> IO () +markNtfSubscriptionForDeletion db connId = do + updatedAt <- getCurrentTime + DB.execute + db + [sql| + UPDATE ntf_subscriptions + SET ntf_sub_action = ?, ntf_sub_smp_action = ?, ntf_sub_action_ts = ?, updated_by_supervisor = ?, updated_at = ? + WHERE conn_id = ? + |] + (Just NSADelete, Nothing :: Maybe NtfSubSMPAction, updatedAt, True, updatedAt, connId) updateNtfSubscription :: DB.Connection -> ConnId -> NtfSubscription -> NtfSubOrSMPAction -> IO () updateNtfSubscription db connId NtfSubscription {ntfQueueId, ntfSubId, ntfSubStatus, ntfSubActionTs} ntfAction = do @@ -767,8 +776,22 @@ setNullNtfSubscriptionAction db connId = do |] (Nothing :: Maybe NtfSubAction, Nothing :: Maybe NtfSubSMPAction, Nothing :: Maybe UTCTime, False, updatedAt, connId) -deleteNtfSubscription :: DB.Connection -> ConnId -> IO (Either StoreError ()) -deleteNtfSubscription _db _connId = pure $ Left SENotImplemented +deleteNtfSubscription :: DB.Connection -> ConnId -> IO () +deleteNtfSubscription db connId = do + r <- maybeFirstRow fromOnly $ DB.query db "SELECT updated_by_supervisor FROM ntf_subscriptions WHERE conn_id = ?" (Only connId) + forM_ r $ \updatedBySupervisor -> do + updatedAt <- getCurrentTime + if updatedBySupervisor + then + DB.execute + db + [sql| + UPDATE ntf_subscriptions + SET smp_ntf_id = ?, ntf_sub_id = ?, ntf_sub_status = ?, updated_by_supervisor = ?, updated_at = ? + WHERE conn_id = ? + |] + (Nothing :: Maybe SMP.NotifierId, Nothing :: Maybe NtfSubscriptionId, NASDeleted, False, updatedAt, connId) + else DB.execute db "DELETE FROM ntf_subscriptions WHERE conn_id = ?" (Only connId) getNextNtfSubAction :: DB.Connection -> NtfServer -> IO (Maybe (NtfSubscription, NtfSubAction, RcvQueue)) getNextNtfSubAction db ntfServer@(ProtocolServer ntfHost ntfPort _) = do diff --git a/src/Simplex/Messaging/Client.hs b/src/Simplex/Messaging/Client.hs index f6938411e..cb32ba3be 100644 --- a/src/Simplex/Messaging/Client.hs +++ b/src/Simplex/Messaging/Client.hs @@ -36,6 +36,7 @@ module Simplex.Messaging.Client subscribeSMPQueueNotifications, secureSMPQueue, enableSMPQueueNotifications, + disableSMPQueueNotifications, sendSMPMessage, ackSMPMessage, suspendSMPQueue, @@ -321,6 +322,12 @@ enableSMPQueueNotifications c rpKey rId notifierKey rcvNtfPublicDhKey = NID nId rcvNtfSrvPublicDhKey -> pure (nId, rcvNtfSrvPublicDhKey) _ -> throwE PCEUnexpectedResponse +-- | Disable notifications for the queue for push notifications server. +-- +-- https://github.com/simplex-chat/simplexmq/blob/master/protocol/simplex-messaging.md#disable-notifications-command +disableSMPQueueNotifications :: SMPClient -> RcvPrivateSignKey -> RecipientId -> ExceptT ProtocolClientError IO () +disableSMPQueueNotifications = okSMPCommand NDEL + -- | Send SMP message. -- -- https://github.com/simplex-chat/simplexmq/blob/master/protocol/simplex-messaging.md#send-message diff --git a/src/Simplex/Messaging/Notifications/Client.hs b/src/Simplex/Messaging/Notifications/Client.hs index 789d76bac..1ac72f590 100644 --- a/src/Simplex/Messaging/Notifications/Client.hs +++ b/src/Simplex/Messaging/Notifications/Client.hs @@ -162,15 +162,18 @@ instance FromField NtfSubAction where fromField = blobFieldDecoder smpDecode instance ToField NtfSubAction where toField = toField . smpEncode data NtfSubSMPAction - = NSAKey + = NSASmpKey + | NSASmpDelete deriving (Show) instance Encoding NtfSubSMPAction where smpEncode = \case - NSAKey -> "K" + NSASmpKey -> "K" + NSASmpDelete -> "D" smpP = A.anyChar >>= \case - 'K' -> pure NSAKey + 'K' -> pure NSASmpKey + 'D' -> pure NSASmpDelete _ -> fail "bad NtfSubSMPAction" instance FromField NtfSubSMPAction where fromField = blobFieldDecoder smpDecode @@ -185,6 +188,10 @@ data NtfAgentSubStatus | -- | state after SNEW - subscription created on notification server NASCreated NtfSubStatus | -- | state after SDEL (subscription is deleted on notification server) + NASOff + | -- | state after NDEL (notifier credentials are deleted on SMP server) + -- Can only exist transiently - if subscription record was updated by notification supervisor mid worker operation, + -- and hence got updated instead of being fully deleted in the database post operation by worker NASDeleted deriving (Eq, Show) @@ -193,6 +200,7 @@ instance Encoding NtfAgentSubStatus where NASNew -> "NEW" NASKey -> "KEY" NASCreated status -> "CREATED " <> smpEncode status + NASOff -> "OFF" NASDeleted -> "DELETED" smpP = A.takeTill (== ' ') >>= \case @@ -201,6 +209,7 @@ instance Encoding NtfAgentSubStatus where "CREATED" -> do _ <- A.space NASCreated <$> smpP + "OFF" -> pure NASOff "DELETED" -> pure NASDeleted _ -> fail "bad NtfAgentSubStatus" diff --git a/src/Simplex/Messaging/Notifications/Server.hs b/src/Simplex/Messaging/Notifications/Server.hs index c9b945c29..ea17a164e 100644 --- a/src/Simplex/Messaging/Notifications/Server.hs +++ b/src/Simplex/Messaging/Notifications/Server.hs @@ -252,7 +252,7 @@ verifyNtfTransmission (sig_, signed, (corrId, entId, _)) cmd = do verifyToken' t_ = verifyToken t_ . const client :: forall m. (MonadUnliftIO m, MonadReader NtfEnv m) => NtfServerClient -> NtfSubscriber -> NtfPushServer -> m () -client NtfServerClient {rcvQ, sndQ} NtfSubscriber {newSubQ} NtfPushServer {pushQ, intervalNotifiers} = +client NtfServerClient {rcvQ, sndQ} NtfSubscriber {newSubQ, smpAgent = ca} NtfPushServer {pushQ, intervalNotifiers} = forever $ atomically (readTBQueue rcvQ) >>= processCommand @@ -347,7 +347,7 @@ client NtfServerClient {rcvQ, sndQ} NtfSubscriber {newSubQ} NtfPushServer {pushQ Just _ -> writeTBQueue newSubQ (NtfSub sub) $> NRSubId subId _ -> pure $ NRErr AUTH ) - NtfReqCmd SSubscription (NtfSub NtfSubData {notifierKey = registeredNKey, subStatus}) (corrId, subId, cmd) -> do + NtfReqCmd SSubscription (NtfSub NtfSubData {smpQueue = SMPQueueNtf {smpServer, notifierId}, notifierKey = registeredNKey, subStatus}) (corrId, subId, cmd) -> do status <- readTVarIO subStatus (corrId,subId,) <$> case cmd of SNEW (NewNtfSub _ _ notifierKey) -> do @@ -358,7 +358,13 @@ client NtfServerClient {rcvQ, sndQ} NtfSubscriber {newSubQ} NtfPushServer {pushQ then NRSubId subId else NRErr AUTH SCHK -> pure $ NRSub status - SDEL -> pure NROk + SDEL -> do + logDebug "SDEL" + st <- asks store + atomically $ do + deleteNtfSubscription st subId + removeSubscription ca smpServer (SPNotifier, notifierId) + pure NROk PING -> pure NRPong getId :: m NtfEntityId getId = getRandomBytes =<< asks (subIdBytes . config) diff --git a/src/Simplex/Messaging/Notifications/Server/Store.hs b/src/Simplex/Messaging/Notifications/Server/Store.hs index 4699cb58e..018608658 100644 --- a/src/Simplex/Messaging/Notifications/Server/Store.hs +++ b/src/Simplex/Messaging/Notifications/Server/Store.hs @@ -121,6 +121,7 @@ removeTokenRegistration st NtfTknData {ntfTknId = tId, token, tknVerifyKey} = >>= mapM_ (\tId' -> when (tId == tId') $ TM.delete k regs) k = C.toPubKey C.pubKeyBytes tknVerifyKey +-- TODO delete token subscriptions deleteNtfToken :: NtfStore -> NtfTokenId -> STM () deleteNtfToken st tknId = do TM.lookupDelete tknId (tokens st) @@ -177,6 +178,16 @@ addNtfSubscription st subId sub@NtfSubData {smpQueue, tokenId} = -- return Nothing if subscription existed before pure $ Just () +deleteNtfSubscription :: NtfStore -> NtfSubscriptionId -> STM () +deleteNtfSubscription st subId = do + TM.lookupDelete subId (subscriptions st) + >>= mapM_ + ( \NtfSubData {smpQueue, tokenId} -> do + TM.delete smpQueue $ subscriptionLookup st + ts_ <- TM.lookup tokenId (tokenSubscriptions st) + forM_ ts_ $ \ts -> modifyTVar' ts $ S.delete subId + ) + -- getNtfRec :: NtfStore -> SNtfEntity e -> NtfEntityId -> STM (Maybe (NtfEntityRec e)) -- getNtfRec st ent entId = case ent of -- SToken -> NtfTkn <$$> TM.lookup entId (tokens st) diff --git a/tests/AgentTests/NotificationTests.hs b/tests/AgentTests/NotificationTests.hs index c360d87eb..f138b1308 100644 --- a/tests/AgentTests/NotificationTests.hs +++ b/tests/AgentTests/NotificationTests.hs @@ -187,7 +187,7 @@ testNotificationSubscriptionExistingConnection :: APNSMockServer -> IO () testNotificationSubscriptionExistingConnection APNSMockServer {apnsQ} = do alice <- getSMPAgentClient agentCfg initAgentServers bob <- getSMPAgentClient agentCfg {dbFile = testDB2} initAgentServers - Right (bobId, nonce, message) <- runExceptT $ do + Right (bobId, aliceId, nonce, message) <- runExceptT $ do -- establish connection (bobId, qInfo) <- createConnection alice SCMInvitation aliceId <- joinConnection bob qInfo "bob's connInfo" @@ -212,7 +212,7 @@ testNotificationSubscriptionExistingConnection APNSMockServer {apnsQ} = do get bob ##> ("", aliceId, SENT $ baseId + 1) -- notification (nonce, message) <- messageNotification apnsQ - pure (bobId, nonce, message) + pure (bobId, aliceId, nonce, message) -- alice client already has subscription for the connection Left (CMD PROHIBITED) <- runExceptT $ getNotificationMessage alice nonce message @@ -227,7 +227,12 @@ testNotificationSubscriptionExistingConnection APNSMockServer {apnsQ} = do Right () <- runExceptT $ do get alice =##> \case ("", c, Msg "hello") -> c == bobId; _ -> False ackMessage alice bobId $ baseId + 1 - -- no unexpected notifications should follow + -- delete notification subscription + deleteNtfSub alice bobId + -- send message + 2 <- msgId <$> sendMessage bob aliceId (SMP.MsgFlags True) "hello again" + get bob ##> ("", aliceId, SENT $ baseId + 2) + -- no notifications should follow 500000 `timeout` atomically (readTBQueue apnsQ) >>= \case Nothing -> pure () _ -> error "unexpected notification" From 70e7c71762abf7fea364d587da55ae3b7bac9b32 Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Fri, 24 Jun 2022 16:51:42 +0400 Subject: [PATCH 080/131] ntf: read rcv queue separately from ntf subscription, fix deletion race condition (#423) --- src/Simplex/Messaging/Agent.hs | 7 ++- .../Messaging/Agent/NtfSubSupervisor.hs | 45 ++++++++++--------- src/Simplex/Messaging/Agent/Store/SQLite.hs | 12 ++--- .../Store/SQLite/Migrations/M20220608_v2.hs | 4 +- .../Store/SQLite/Migrations/agent_schema.sql | 4 +- 5 files changed, 35 insertions(+), 37 deletions(-) diff --git a/src/Simplex/Messaging/Agent.hs b/src/Simplex/Messaging/Agent.hs index 449f710bc..f76501074 100644 --- a/src/Simplex/Messaging/Agent.hs +++ b/src/Simplex/Messaging/Agent.hs @@ -622,11 +622,10 @@ deleteConnection' c connId = delete :: RcvQueue -> m () delete rq = do deleteQueue c rq - ns <- asks ntfSupervisor - atomically $ do - removeSubscription c connId - sendNtfSubCommand ns (connId, NSCDelete) + atomically $ removeSubscription c connId withStore' c (`deleteConn` connId) + ns <- asks ntfSupervisor + atomically $ sendNtfSubCommand ns (connId, NSCDelete) -- | Change servers to be used for creating new queues, in Reader monad setSMPServers' :: AgentMonad m => AgentClient -> NonEmpty SMPServer -> m () diff --git a/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs b/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs index d2628325b..cc6ea7ce7 100644 --- a/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs +++ b/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs @@ -124,18 +124,18 @@ runNtfWorker c srv doWork = forever $ do ts <- liftIO getCurrentTime case nextSub_ of Nothing -> noWorkToDo - Just (ntfSub@NtfSubscription {connId, smpServer, ntfSubId}, ntfSubAction, RcvQueue {clientNtfCreds}) -> + Just (ntfSub@NtfSubscription {connId, smpServer, ntfSubId}, ntfSubAction) -> unlessM (rescheduleAction doWork ts ntfSub) $ case ntfSubAction of - NSACreate -> case clientNtfCreds of - Just ClientNtfCreds {ntfPrivateKey, notifierId} - | ntfTknStatus == NTActive -> do + NSACreate -> do + rq_ <- withStore' c (`getRcvQueue` connId) + case (rq_, ntfTknStatus) of + (Right RcvQueue {clientNtfCreds = Just ClientNtfCreds {ntfPrivateKey, notifierId}}, NTActive) -> do nSubId <- agentNtfCreateSubscription c tknId tkn (SMPQueueNtf smpServer notifierId) ntfPrivateKey let actionTs = addUTCTime 30 ts withStore' c $ \db -> updateNtfSubscription db connId ntfSub {ntfSubId = Just nSubId, ntfSubStatus = NASCreated NSNew, ntfSubActionTs = actionTs} (NtfSubAction NSACheck) - | otherwise -> ntfInternalError c connId "NSACreate - token not active" - _ -> ntfInternalError c connId "NSACreate - no notifier key or ID" + _ -> ntfInternalError c connId "NSACreate - no notifier queue credentials or token not active" NSACheck -> case ntfSubId of Just nSubId -> agentNtfCheckSubscription c nSubId tkn >>= \case @@ -177,24 +177,27 @@ runNtfSMPWorker c srv doWork = forever $ do ts <- liftIO getCurrentTime case nextSub_ of Nothing -> noWorkToDo - Just (ntfSub@NtfSubscription {connId, ntfServer}, ntfSubAction, rq) -> + Just (ntfSub@NtfSubscription {connId, ntfServer}, ntfSubAction) -> unlessM (rescheduleAction doWork ts ntfSub) $ case ntfSubAction of - NSASmpKey - | ntfTknStatus == NTActive -> do - C.SignAlg a <- asks (cmdSignAlg . config) - (ntfPublicKey, ntfPrivateKey) <- liftIO $ C.generateSignatureKeyPair a - (rcvNtfPubDhKey, rcvNtfPrivDhKey) <- liftIO C.generateKeyPair' - (notifierId, rcvNtfSrvPubDhKey) <- enableQueueNotifications c rq ntfPublicKey rcvNtfPubDhKey - let rcvNtfDhSecret = C.dh' rcvNtfSrvPubDhKey rcvNtfPrivDhKey - withStore' c $ \st -> do - setRcvQueueNtfCreds st connId ClientNtfCreds {ntfPublicKey, ntfPrivateKey, notifierId, rcvNtfDhSecret} - updateNtfSubscription st connId ntfSub {ntfQueueId = Just notifierId, ntfSubStatus = NASKey, ntfSubActionTs = ts} (NtfSubAction NSACreate) - ns <- asks ntfSupervisor - atomically $ sendNtfSubCommand ns (connId, NSCNtfWorker ntfServer) - | otherwise -> ntfInternalError c connId "NSASmpKey - token not active" + NSASmpKey -> do + rq_ <- withStore' c (`getRcvQueue` connId) + case (rq_, ntfTknStatus) of + (Right rq, NTActive) -> do + C.SignAlg a <- asks (cmdSignAlg . config) + (ntfPublicKey, ntfPrivateKey) <- liftIO $ C.generateSignatureKeyPair a + (rcvNtfPubDhKey, rcvNtfPrivDhKey) <- liftIO C.generateKeyPair' + (notifierId, rcvNtfSrvPubDhKey) <- enableQueueNotifications c rq ntfPublicKey rcvNtfPubDhKey + let rcvNtfDhSecret = C.dh' rcvNtfSrvPubDhKey rcvNtfPrivDhKey + withStore' c $ \st -> do + setRcvQueueNtfCreds st connId ClientNtfCreds {ntfPublicKey, ntfPrivateKey, notifierId, rcvNtfDhSecret} + updateNtfSubscription st connId ntfSub {ntfQueueId = Just notifierId, ntfSubStatus = NASKey, ntfSubActionTs = ts} (NtfSubAction NSACreate) + ns <- asks ntfSupervisor + atomically $ sendNtfSubCommand ns (connId, NSCNtfWorker ntfServer) + _ -> ntfInternalError c connId "NSASmpKey - no rcv queue or token not active" NSASmpDelete -> do - disableQueueNotifications c rq + rq_ <- withStore' c (`getRcvQueue` connId) + forM_ rq_ $ \rq -> disableQueueNotifications c rq withStore' c $ \db -> deleteNtfSubscription db connId _ -> noWorkToDo delay <- asks $ ntfWorkerThrottle . config diff --git a/src/Simplex/Messaging/Agent/Store/SQLite.hs b/src/Simplex/Messaging/Agent/Store/SQLite.hs index 3f4b96529..cabb883c3 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite.hs @@ -793,11 +793,11 @@ deleteNtfSubscription db connId = do (Nothing :: Maybe SMP.NotifierId, Nothing :: Maybe NtfSubscriptionId, NASDeleted, False, updatedAt, connId) else DB.execute db "DELETE FROM ntf_subscriptions WHERE conn_id = ?" (Only connId) -getNextNtfSubAction :: DB.Connection -> NtfServer -> IO (Maybe (NtfSubscription, NtfSubAction, RcvQueue)) +getNextNtfSubAction :: DB.Connection -> NtfServer -> IO (Maybe (NtfSubscription, NtfSubAction)) getNextNtfSubAction db ntfServer@(ProtocolServer ntfHost ntfPort _) = do - maybeFirstRow ntfSubscription getNtfSubAction_ $>>= \(ntfSub@NtfSubscription {connId}, ntfSubAction) -> do + maybeFirstRow ntfSubscription getNtfSubAction_ $>>= \ntfSub@(NtfSubscription {connId}, _) -> do DB.execute db "UPDATE ntf_subscriptions SET updated_by_supervisor = ? WHERE conn_id = ?" (False, connId) - (ntfSub,ntfSubAction,) <$$> getRcvQueueByConnId_ db connId + pure $ Just ntfSub where getNtfSubAction_ = DB.query @@ -816,11 +816,11 @@ getNextNtfSubAction db ntfServer@(ProtocolServer ntfHost ntfPort _) = do let smpServer = SMPServer smpHost smpPort smpKeyHash in (NtfSubscription {connId, smpServer, ntfQueueId, ntfServer, ntfSubId, ntfSubStatus, ntfSubActionTs}, ntfSubAction) -getNextNtfSubSMPAction :: DB.Connection -> SMPServer -> IO (Maybe (NtfSubscription, NtfSubSMPAction, RcvQueue)) +getNextNtfSubSMPAction :: DB.Connection -> SMPServer -> IO (Maybe (NtfSubscription, NtfSubSMPAction)) getNextNtfSubSMPAction db smpServer@(SMPServer smpHost smpPort _) = do - maybeFirstRow ntfSubscription getNtfSubAction_ $>>= \(ntfSub@NtfSubscription {connId}, ntfSubAction) -> do + maybeFirstRow ntfSubscription getNtfSubAction_ $>>= \ntfSub@(NtfSubscription {connId}, _) -> do DB.execute db "UPDATE ntf_subscriptions SET updated_by_supervisor = ? WHERE conn_id = ?" (False, connId) - (ntfSub,ntfSubAction,) <$$> getRcvQueueByConnId_ db connId + pure $ Just ntfSub where getNtfSubAction_ = DB.query diff --git a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220608_v2.hs b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220608_v2.hs index 753b3c164..544ccf60f 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220608_v2.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220608_v2.hs @@ -27,7 +27,7 @@ ALTER TABLE rcv_queues ADD COLUMN rcv_ntf_dh_secret BLOB; CREATE UNIQUE INDEX idx_rcv_queues_ntf ON rcv_queues (host, port, ntf_id); CREATE TABLE ntf_subscriptions ( - conn_id BLOB NOT NULL, -- ? make nullable for when connection is deleted but we still need to delete subscription + conn_id BLOB NOT NULL, smp_host TEXT NULL, smp_port TEXT NULL, smp_ntf_id BLOB, @@ -42,8 +42,6 @@ CREATE TABLE ntf_subscriptions ( created_at TEXT NOT NULL DEFAULT (datetime('now')), updated_at TEXT NOT NULL DEFAULT (datetime('now')), PRIMARY KEY (conn_id), - -- FOREIGN KEY (conn_id) REFERENCES connections - -- ON DELETE SET NULL ON UPDATE CASCADE, FOREIGN KEY (smp_host, smp_port) REFERENCES servers (host, port) ON DELETE SET NULL ON UPDATE CASCADE, FOREIGN KEY (ntf_host, ntf_port) REFERENCES ntf_servers diff --git a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql index 2a64a78f4..5a4436e8b 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql +++ b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql @@ -171,7 +171,7 @@ tkn_dh_secret BLOB, -- DH secret for e2e encryption of notifications ) WITHOUT ROWID; CREATE UNIQUE INDEX idx_rcv_queues_ntf ON rcv_queues(host, port, ntf_id); CREATE TABLE ntf_subscriptions( - conn_id BLOB NOT NULL, -- ? make nullable for when connection is deleted but we still need to delete subscription + conn_id BLOB NOT NULL, smp_host TEXT NULL, smp_port TEXT NULL, smp_ntf_id BLOB, @@ -186,8 +186,6 @@ CREATE TABLE ntf_subscriptions( created_at TEXT NOT NULL DEFAULT(datetime('now')), updated_at TEXT NOT NULL DEFAULT(datetime('now')), PRIMARY KEY(conn_id), - -- FOREIGN KEY(conn_id) REFERENCES connections - -- ON DELETE SET NULL ON UPDATE CASCADE, FOREIGN KEY(smp_host, smp_port) REFERENCES servers(host, port) ON DELETE SET NULL ON UPDATE CASCADE, FOREIGN KEY(ntf_host, ntf_port) REFERENCES ntf_servers From b82452fe03171d3eba59151c8170ad3880590f42 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Fri, 24 Jun 2022 15:35:50 +0100 Subject: [PATCH 081/131] add category to mutable content notification (#424) --- src/Simplex/Messaging/Notifications/Server/Push/APNS.hs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Simplex/Messaging/Notifications/Server/Push/APNS.hs b/src/Simplex/Messaging/Notifications/Server/Push/APNS.hs index b2e1e2a45..44118a28f 100644 --- a/src/Simplex/Messaging/Notifications/Server/Push/APNS.hs +++ b/src/Simplex/Messaging/Notifications/Server/Push/APNS.hs @@ -121,9 +121,9 @@ instance ToJSON APNSNotification where toEncoding = J.genericToEncoding J.defaultOptions {J.omitNothingFields = True} data APNSNotificationBody - = APNSAlert {alert :: APNSAlertBody, badge :: Maybe Int, sound :: Maybe Text, category :: Maybe Text} - | APNSBackground {contentAvailable :: Int} + = APNSBackground {contentAvailable :: Int} | APNSMutableContent {mutableContent :: Int, alert :: APNSAlertBody, category :: Maybe Text} + | APNSAlert {alert :: APNSAlertBody, badge :: Maybe Int, sound :: Maybe Text, category :: Maybe Text} deriving (Show, Generic) apnsJSONOptions :: J.Options @@ -268,6 +268,9 @@ disconnectApnsHTTP2Client :: APNSPushClient -> IO () disconnectApnsHTTP2Client APNSPushClient {https2Client} = readTVarIO https2Client >>= mapM_ closeHTTP2Client >> atomically (writeTVar https2Client Nothing) +ntfCategoryCheckMessage :: Text +ntfCategoryCheckMessage = "NTF_CAT_CHECK_MESSAGE" + apnsNotification :: NtfTknData -> C.CbNonce -> Int -> PushNotification -> Either C.CryptoError APNSNotification apnsNotification NtfTknData {tknDhSecret} nonce paddedLen = \case PNVerification (NtfRegCode code) -> @@ -282,7 +285,7 @@ apnsNotification NtfTknData {tknDhSecret} nonce paddedLen = \case encrypt :: ByteString -> (Text -> APNSNotification) -> Either C.CryptoError APNSNotification encrypt ntfData f = f . safeDecodeUtf8 . U.encode <$> C.cbEncrypt tknDhSecret nonce ntfData paddedLen apn aps notificationData = APNSNotification {aps, notificationData} - apnMutableContent = APNSMutableContent {mutableContent = 1, alert = APNSAlertText "Encrypted message or some other app event", category = Nothing} + apnMutableContent = APNSMutableContent {mutableContent = 1, alert = APNSAlertText "Encrypted message or another app event", category = Just ntfCategoryCheckMessage} apnAlert alert = APNSAlert {alert, badge = Nothing, sound = Nothing, category = Nothing} safeDecodeUtf8 = decodeUtf8With onError where onError _ _ = Just '?' From 58622c6aafc066746844e72a8f1fa9a3c4afc3dc Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Fri, 24 Jun 2022 20:40:25 +0400 Subject: [PATCH 082/131] ntf: catch worker errors (#425) --- .../Messaging/Agent/NtfSubSupervisor.hs | 163 ++++++++++-------- 1 file changed, 93 insertions(+), 70 deletions(-) diff --git a/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs b/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs index cc6ea7ce7..5e7fd56b8 100644 --- a/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs +++ b/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs @@ -27,8 +27,9 @@ import qualified Data.Map.Strict as M import Data.Time (UTCTime, addUTCTime, diffUTCTime, getCurrentTime, nominalDiffTimeToSeconds) import Simplex.Messaging.Agent.Client import Simplex.Messaging.Agent.Env.SQLite -import Simplex.Messaging.Agent.Protocol (ConnId) +import Simplex.Messaging.Agent.Protocol (AgentErrorType (..), BrokerErrorType (..), ConnId) import qualified Simplex.Messaging.Agent.Protocol as AP +import Simplex.Messaging.Agent.RetryInterval import Simplex.Messaging.Agent.Store import Simplex.Messaging.Agent.Store.SQLite import Simplex.Messaging.Client.Agent () @@ -115,95 +116,117 @@ processNtfSub c (connId, cmd) = do Just (doWork, _) -> void . atomically $ tryPutTMVar doWork () -runNtfWorker :: AgentMonad m => AgentClient -> NtfServer -> TMVar () -> m () +runNtfWorker :: forall m. AgentMonad m => AgentClient -> NtfServer -> TMVar () -> m () runNtfWorker c srv doWork = forever $ do void . atomically $ readTMVar doWork getNtfToken >>= \case - Just tkn@NtfToken {ntfTokenId = Just tknId, ntfTknStatus} -> do + Just tkn -> do nextSub_ <- withStore' c (`getNextNtfSubAction` srv) - ts <- liftIO getCurrentTime case nextSub_ of Nothing -> noWorkToDo - Just (ntfSub@NtfSubscription {connId, smpServer, ntfSubId}, ntfSubAction) -> - unlessM (rescheduleAction doWork ts ntfSub) $ - case ntfSubAction of - NSACreate -> do - rq_ <- withStore' c (`getRcvQueue` connId) - case (rq_, ntfTknStatus) of - (Right RcvQueue {clientNtfCreds = Just ClientNtfCreds {ntfPrivateKey, notifierId}}, NTActive) -> do - nSubId <- agentNtfCreateSubscription c tknId tkn (SMPQueueNtf smpServer notifierId) ntfPrivateKey - let actionTs = addUTCTime 30 ts - withStore' c $ \db -> - updateNtfSubscription db connId ntfSub {ntfSubId = Just nSubId, ntfSubStatus = NASCreated NSNew, ntfSubActionTs = actionTs} (NtfSubAction NSACheck) - _ -> ntfInternalError c connId "NSACreate - no notifier queue credentials or token not active" - NSACheck -> case ntfSubId of - Just nSubId -> - agentNtfCheckSubscription c nSubId tkn >>= \case - NSNew -> updateSubNextCheck NSNew - NSPending -> updateSubNextCheck NSPending - NSActive -> updateSubNextCheck NSActive - NSEnd -> updateSubNextCheck NSEnd - NSSMPAuth -> updateSub (NASCreated NSSMPAuth) (NtfSubAction NSADelete) ts - Nothing -> ntfInternalError c connId "NSACheck - no subscription ID" - NSADelete -> case ntfSubId of - Just nSubId -> - agentNtfDeleteSubscription c nSubId tkn - `E.finally` do - withStore' c $ \db -> - updateNtfSubscription db connId ntfSub {ntfSubId = Nothing, ntfSubStatus = NASOff, ntfSubActionTs = ts} (NtfSubSMPAction NSASmpDelete) - ns <- asks ntfSupervisor - atomically $ sendNtfSubCommand ns (connId, NSCNtfSMPWorker smpServer) - Nothing -> ntfInternalError c connId "NSADelete - no subscription ID" - where - updateSubNextCheck toStatus = do - checkInterval <- asks $ ntfSubCheckInterval . config - let nextCheckTs = addUTCTime checkInterval ts - updateSub (NASCreated toStatus) (NtfSubAction NSACheck) nextCheckTs - updateSub toStatus toAction actionTs = - withStore' c $ \db -> - updateNtfSubscription db connId ntfSub {ntfSubStatus = toStatus, ntfSubActionTs = actionTs} toAction + Just ntfSub@(NtfSubscription {connId}, _) -> do + ri <- asks $ reconnectInterval . config + withRetryInterval ri $ \loop -> + processAction tkn ntfSub + `catchError` ( \e -> + case e of + BROKER NETWORK -> loop + BROKER TIMEOUT -> loop + _ -> ntfInternalError c connId (show e) + ) _ -> noWorkToDo - delay <- asks $ ntfWorkerThrottle . config - liftIO $ threadDelay delay + throttle <- asks $ ntfWorkerThrottle . config + liftIO $ threadDelay throttle where noWorkToDo = void . atomically $ tryTakeTMVar doWork + processAction :: NtfToken -> (NtfSubscription, NtfSubAction) -> m () + processAction tkn@NtfToken {ntfTokenId, ntfTknStatus} (ntfSub@NtfSubscription {connId, smpServer, ntfSubId}, ntfSubAction) = do + ts <- liftIO getCurrentTime + unlessM (rescheduleAction doWork ts ntfSub) $ + case ntfSubAction of + NSACreate -> do + rq_ <- withStore' c (`getRcvQueue` connId) + case (rq_, ntfTknStatus, ntfTokenId) of + (Right RcvQueue {clientNtfCreds = Just ClientNtfCreds {ntfPrivateKey, notifierId}}, NTActive, Just tknId) -> do + nSubId <- agentNtfCreateSubscription c tknId tkn (SMPQueueNtf smpServer notifierId) ntfPrivateKey + let actionTs = addUTCTime 30 ts + withStore' c $ \db -> + updateNtfSubscription db connId ntfSub {ntfSubId = Just nSubId, ntfSubStatus = NASCreated NSNew, ntfSubActionTs = actionTs} (NtfSubAction NSACheck) + _ -> ntfInternalError c connId "NSACreate - no notifier queue credentials or token not active" + NSACheck -> case ntfSubId of + Just nSubId -> + agentNtfCheckSubscription c nSubId tkn >>= \case + NSNew -> updateSubNextCheck ts NSNew + NSPending -> updateSubNextCheck ts NSPending + NSActive -> updateSubNextCheck ts NSActive + NSEnd -> updateSubNextCheck ts NSEnd + NSSMPAuth -> updateSub (NASCreated NSSMPAuth) (NtfSubAction NSADelete) ts + Nothing -> ntfInternalError c connId "NSACheck - no subscription ID" + NSADelete -> case ntfSubId of + Just nSubId -> + agentNtfDeleteSubscription c nSubId tkn + `E.finally` do + withStore' c $ \db -> + updateNtfSubscription db connId ntfSub {ntfSubId = Nothing, ntfSubStatus = NASOff, ntfSubActionTs = ts} (NtfSubSMPAction NSASmpDelete) + ns <- asks ntfSupervisor + atomically $ sendNtfSubCommand ns (connId, NSCNtfSMPWorker smpServer) + Nothing -> ntfInternalError c connId "NSADelete - no subscription ID" + where + updateSubNextCheck ts toStatus = do + checkInterval <- asks $ ntfSubCheckInterval . config + let nextCheckTs = addUTCTime checkInterval ts + updateSub (NASCreated toStatus) (NtfSubAction NSACheck) nextCheckTs + updateSub toStatus toAction actionTs = + withStore' c $ \db -> + updateNtfSubscription db connId ntfSub {ntfSubStatus = toStatus, ntfSubActionTs = actionTs} toAction runNtfSMPWorker :: forall m. AgentMonad m => AgentClient -> SMPServer -> TMVar () -> m () runNtfSMPWorker c srv doWork = forever $ do void . atomically $ readTMVar doWork getNtfToken >>= \case - Just NtfToken {ntfTknStatus} -> do + Just tkn -> do nextSub_ <- withStore' c (`getNextNtfSubSMPAction` srv) - ts <- liftIO getCurrentTime case nextSub_ of Nothing -> noWorkToDo - Just (ntfSub@NtfSubscription {connId, ntfServer}, ntfSubAction) -> - unlessM (rescheduleAction doWork ts ntfSub) $ - case ntfSubAction of - NSASmpKey -> do - rq_ <- withStore' c (`getRcvQueue` connId) - case (rq_, ntfTknStatus) of - (Right rq, NTActive) -> do - C.SignAlg a <- asks (cmdSignAlg . config) - (ntfPublicKey, ntfPrivateKey) <- liftIO $ C.generateSignatureKeyPair a - (rcvNtfPubDhKey, rcvNtfPrivDhKey) <- liftIO C.generateKeyPair' - (notifierId, rcvNtfSrvPubDhKey) <- enableQueueNotifications c rq ntfPublicKey rcvNtfPubDhKey - let rcvNtfDhSecret = C.dh' rcvNtfSrvPubDhKey rcvNtfPrivDhKey - withStore' c $ \st -> do - setRcvQueueNtfCreds st connId ClientNtfCreds {ntfPublicKey, ntfPrivateKey, notifierId, rcvNtfDhSecret} - updateNtfSubscription st connId ntfSub {ntfQueueId = Just notifierId, ntfSubStatus = NASKey, ntfSubActionTs = ts} (NtfSubAction NSACreate) - ns <- asks ntfSupervisor - atomically $ sendNtfSubCommand ns (connId, NSCNtfWorker ntfServer) - _ -> ntfInternalError c connId "NSASmpKey - no rcv queue or token not active" - NSASmpDelete -> do - rq_ <- withStore' c (`getRcvQueue` connId) - forM_ rq_ $ \rq -> disableQueueNotifications c rq - withStore' c $ \db -> deleteNtfSubscription db connId + Just ntfSub@(NtfSubscription {connId}, _) -> do + ri <- asks $ reconnectInterval . config + withRetryInterval ri $ \loop -> + processAction tkn ntfSub + `catchError` ( \e -> + case e of + BROKER NETWORK -> loop + BROKER TIMEOUT -> loop + _ -> ntfInternalError c connId (show e) + ) _ -> noWorkToDo - delay <- asks $ ntfWorkerThrottle . config - liftIO $ threadDelay delay + throttle <- asks $ ntfWorkerThrottle . config + liftIO $ threadDelay throttle where noWorkToDo = void . atomically $ tryTakeTMVar doWork + processAction :: NtfToken -> (NtfSubscription, NtfSubSMPAction) -> m () + processAction NtfToken {ntfTknStatus} (ntfSub@NtfSubscription {connId, ntfServer}, ntfSubAction) = do + ts <- liftIO getCurrentTime + unlessM (rescheduleAction doWork ts ntfSub) $ + case ntfSubAction of + NSASmpKey -> do + rq_ <- withStore' c (`getRcvQueue` connId) + case (rq_, ntfTknStatus) of + (Right rq, NTActive) -> do + C.SignAlg a <- asks (cmdSignAlg . config) + (ntfPublicKey, ntfPrivateKey) <- liftIO $ C.generateSignatureKeyPair a + (rcvNtfPubDhKey, rcvNtfPrivDhKey) <- liftIO C.generateKeyPair' + (notifierId, rcvNtfSrvPubDhKey) <- enableQueueNotifications c rq ntfPublicKey rcvNtfPubDhKey + let rcvNtfDhSecret = C.dh' rcvNtfSrvPubDhKey rcvNtfPrivDhKey + withStore' c $ \st -> do + setRcvQueueNtfCreds st connId ClientNtfCreds {ntfPublicKey, ntfPrivateKey, notifierId, rcvNtfDhSecret} + updateNtfSubscription st connId ntfSub {ntfQueueId = Just notifierId, ntfSubStatus = NASKey, ntfSubActionTs = ts} (NtfSubAction NSACreate) + ns <- asks ntfSupervisor + atomically $ sendNtfSubCommand ns (connId, NSCNtfWorker ntfServer) + _ -> ntfInternalError c connId "NSASmpKey - no rcv queue or token not active" + NSASmpDelete -> do + rq_ <- withStore' c (`getRcvQueue` connId) + forM_ rq_ $ \rq -> disableQueueNotifications c rq + withStore' c $ \db -> deleteNtfSubscription db connId rescheduleAction :: AgentMonad m => TMVar () -> UTCTime -> NtfSubscription -> m Bool rescheduleAction doWork ts NtfSubscription {ntfSubActionTs} From 9e0e6b1afc2aefdd1608c120ad8e0634d8004e79 Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Fri, 24 Jun 2022 20:40:35 +0400 Subject: [PATCH 083/131] ntf: server retry to deliver notification (#426) * ntf: server retry to deliver notification * refactor * Update src/Simplex/Messaging/Notifications/Server.hs Co-authored-by: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Co-authored-by: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> --- src/Simplex/Messaging/Notifications/Server.hs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/Simplex/Messaging/Notifications/Server.hs b/src/Simplex/Messaging/Notifications/Server.hs index ea17a164e..e832af5c2 100644 --- a/src/Simplex/Messaging/Notifications/Server.hs +++ b/src/Simplex/Messaging/Notifications/Server.hs @@ -24,7 +24,7 @@ import Simplex.Messaging.Client.Agent import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Notifications.Protocol import Simplex.Messaging.Notifications.Server.Env -import Simplex.Messaging.Notifications.Server.Push.APNS (PNMessageData (..), PushNotification (..), PushProviderClient) +import Simplex.Messaging.Notifications.Server.Push.APNS (PNMessageData (..), PushNotification (..), PushProviderClient, PushProviderError (..)) import Simplex.Messaging.Notifications.Server.Store import Simplex.Messaging.Notifications.Transport import Simplex.Messaging.Protocol (ErrorType (..), SMPServer, SignedTransmission, Transmission, encodeTransmission, tGet, tPut) @@ -133,7 +133,7 @@ ntfSubscriber NtfSubscriber {smpSubscribers, newSubQ, smpAgent = ca@SMPClientAge -- update subscription status pure () -ntfPush :: MonadUnliftIO m => NtfPushServer -> m () +ntfPush :: forall m. MonadUnliftIO m => NtfPushServer -> m () ntfPush s@NtfPushServer {pushQ} = liftIO . forever . runExceptT $ do (tkn@NtfTknData {token = DeviceToken pp _, tknStatus}, ntf) <- atomically (readTBQueue pushQ) logDebug $ "sending push notification to " <> T.pack (show pp) @@ -153,8 +153,18 @@ ntfPush s@NtfPushServer {pushQ} = liftIO . forever . runExceptT $ do deliverNotification :: PushProvider -> PushProviderClient deliverNotification pp tkn ntf = do deliver <- liftIO $ getPushClient s pp - -- TODO retry later based on the error - deliver tkn ntf `catchError` \e -> logError (T.pack $ "Push provider error (" <> show pp <> "): " <> show e) >> throwError e + -- TODO latest pending notification per subscription + retryDeliver deliver (2 :: Integer) + where + retryDeliver deliver n = + deliver tkn ntf `catchError` \e -> case e of + PPConnection _ -> deliverOrErr deliver n e + PPRetryLater -> deliverOrErr deliver n e + _ -> err e + deliverOrErr deliver n e + | n > 0 = threadDelay 500000 >> retryDeliver deliver (n - 1) + | otherwise = err e + err e = logError (T.pack $ "Push provider error (" <> show pp <> "): " <> show e) >> throwError e runNtfClientTransport :: (Transport c, MonadUnliftIO m, MonadReader NtfEnv m) => THandle c -> m () runNtfClientTransport th@THandle {sessionId} = do From c2f1dabcd0a2b8f7959b6946623c9620e036a103 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Sat, 25 Jun 2022 08:13:07 +0100 Subject: [PATCH 084/131] fix agent phase, test (#427) --- src/Simplex/Messaging/Agent/Client.hs | 2 +- src/Simplex/Messaging/Agent/Env/SQLite.hs | 2 +- tests/AgentTests/FunctionalAPITests.hs | 47 ++++++++++++++++++----- 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/src/Simplex/Messaging/Agent/Client.hs b/src/Simplex/Messaging/Agent/Client.hs index 9e6d36e83..4cf746f5c 100644 --- a/src/Simplex/Messaging/Agent/Client.hs +++ b/src/Simplex/Messaging/Agent/Client.hs @@ -644,7 +644,7 @@ cryptoError = \case endAgentOperation :: AgentClient -> AgentOperation -> STM () endAgentOperation c@AgentClient {agentEnv = Env {agentOperations}} op = do - TM.alter (Just . maybe 0 (\n -> min 0 $ n - 1)) op agentOperations + TM.alter (Just . maybe 0 (\n -> max 0 $ n - 1)) op agentOperations notifyAgentPhaseChanged c beginAgentOperation :: AgentClient -> AgentOperation -> STM () diff --git a/src/Simplex/Messaging/Agent/Env/SQLite.hs b/src/Simplex/Messaging/Agent/Env/SQLite.hs index f2571d7e3..7883706e9 100644 --- a/src/Simplex/Messaging/Agent/Env/SQLite.hs +++ b/src/Simplex/Messaging/Agent/Env/SQLite.hs @@ -127,7 +127,7 @@ data Env = Env } data AgentOperation = AONetwork | AODatabase - deriving (Eq, Ord) + deriving (Eq, Ord, Show) disallowedOperations :: AgentPhase -> [AgentOperation] disallowedOperations = \case diff --git a/tests/AgentTests/FunctionalAPITests.hs b/tests/AgentTests/FunctionalAPITests.hs index b630e95cc..7a9a62f45 100644 --- a/tests/AgentTests/FunctionalAPITests.hs +++ b/tests/AgentTests/FunctionalAPITests.hs @@ -89,6 +89,9 @@ functionalAPITests t = do testInactiveClientDisconnected t it "should NOT disconnect active clients" $ testActiveClientNotDisconnected t + describe "Agent phases" $ do + it "should update clients when agent phase changes" $ + withSmpServer t testAgentPhaseChanges testAgentClient :: IO () testAgentClient = do @@ -321,18 +324,11 @@ testDuplicateMessage t = do alice <- getSMPAgentClient agentCfg initAgentServers bob <- getSMPAgentClient agentCfg {dbFile = testDB2} initAgentServers (aliceId, bobId, bob1) <- withSmpServerStoreMsgLogOn t testPort $ \_ -> do - Right (aliceId, bobId) <- runExceptT $ do - (bobId, qInfo) <- createConnection alice SCMInvitation - aliceId <- joinConnection bob qInfo "bob's connInfo" - ("", _, CONF confId "bob's connInfo") <- get alice - allowConnection alice bobId confId "alice's connInfo" - get alice ##> ("", bobId, CON) - get bob ##> ("", aliceId, INFO "alice's connInfo") - get bob ##> ("", aliceId, CON) + Right (aliceId, bobId) <- runExceptT $ makeConnection alice bob + Right () <- runExceptT $ do 4 <- sendMessage alice bobId SMP.noMsgFlags "hello" get alice ##> ("", bobId, SENT 4) get bob =##> \case ("", c, Msg "hello") -> c == aliceId; _ -> False - pure (aliceId, bobId) disconnectAgentClient bob -- if the agent user did not send ACK, the message will be delivered again @@ -372,6 +368,17 @@ testDuplicateMessage t = do get bob2 =##> \case ("", c, Msg "hello 3") -> c == aliceId; _ -> False pure () +makeConnection :: AgentClient -> AgentClient -> ExceptT AgentErrorType IO (ConnId, ConnId) +makeConnection alice bob = do + (bobId, qInfo) <- createConnection alice SCMInvitation + aliceId <- joinConnection bob qInfo "bob's connInfo" + ("", _, CONF confId "bob's connInfo") <- get alice + allowConnection alice bobId confId "alice's connInfo" + get alice ##> ("", bobId, CON) + get bob ##> ("", aliceId, INFO "alice's connInfo") + get bob ##> ("", aliceId, CON) + pure (aliceId, bobId) + testInactiveClientDisconnected :: ATransport -> IO () testInactiveClientDisconnected t = do let cfg' = cfg {inactiveClientExpiration = Just ExpirationConfig {ttl = 1, checkInterval = 1}} @@ -410,6 +417,28 @@ testActiveClientNotDisconnected t = do get alice ##> ("", "", DOWN testSMPServer [connId]) milliseconds ts = systemSeconds ts * 1000 + fromIntegral (systemNanoseconds ts `div` 1000000) +testAgentPhaseChanges :: IO () +testAgentPhaseChanges = do + a <- getSMPAgentClient agentCfg initAgentServers + b <- getSMPAgentClient agentCfg {dbFile = testDB2} initAgentServers + Right () <- runExceptT $ do + (aId, bId) <- makeConnection a b + 4 <- sendMessage a bId SMP.noMsgFlags "hello" + get a ##> ("", bId, SENT 4) + get b =##> \case ("", c, Msg "hello") -> c == aId; _ -> False + ackMessage b aId 4 + setAgentPhase b APPaused + get b ##> ("", "", PHASE APPaused) + 5 <- sendMessage a bId SMP.noMsgFlags "hello 2" + get a ##> ("", bId, SENT 5) + Nothing <- 100000 `timeout` get b + setAgentPhase b APSuspended + get b ##> ("", "", PHASE APSuspended) + setAgentPhase b APActive + get b ##> ("", "", PHASE APActive) + get b =##> \case ("", c, Msg "hello 2") -> c == aId; _ -> False + pure () + exchangeGreetings :: AgentClient -> ConnId -> AgentClient -> ConnId -> ExceptT AgentErrorType IO () exchangeGreetings alice bobId bob aliceId = do 4 <- sendMessage alice bobId SMP.noMsgFlags "hello" From d2321b750dfaa682560f24433e7122b05d158550 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Sat, 25 Jun 2022 11:48:15 +0100 Subject: [PATCH 085/131] get/set notification mode (#428) * get/set notification mode * update method --- src/Simplex/Messaging/Agent.hs | 14 ++++++++++++++ src/Simplex/Messaging/Agent/Protocol.hs | 23 +++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/Simplex/Messaging/Agent.hs b/src/Simplex/Messaging/Agent.hs index f76501074..99090613d 100644 --- a/src/Simplex/Messaging/Agent.hs +++ b/src/Simplex/Messaging/Agent.hs @@ -58,6 +58,8 @@ module Simplex.Messaging.Agent enableNtfCron, checkNtfToken, deleteNtfToken, + setNtfMode, + getNtfMode, deleteNtfSub, setAgentPhase, logConnection, @@ -206,6 +208,12 @@ checkNtfToken c = withAgentEnv c . checkNtfToken' c deleteNtfToken :: AgentErrorMonad m => AgentClient -> DeviceToken -> m () deleteNtfToken c = withAgentEnv c . deleteNtfToken' c +setNtfMode :: AgentErrorMonad m => AgentClient -> DeviceToken -> NotificationsMode -> m () +setNtfMode c = withAgentEnv c .: setNtfMode' c + +getNtfMode :: AgentErrorMonad m => AgentClient -> m (NtfTknStatus, NotificationsMode) +getNtfMode c = withAgentEnv c $ getNtfMode' c + -- | Delete notification subscription for connection deleteNtfSub :: AgentErrorMonad m => AgentClient -> ConnId -> m () deleteNtfSub c = withAgentEnv c . deleteNtfSub' c @@ -716,6 +724,12 @@ deleteNtfToken' c deviceToken = (Just tkn, _) -> deleteToken_ c tkn _ -> throwError $ CMD PROHIBITED +setNtfMode' :: AgentMonad m => AgentClient -> DeviceToken -> NotificationsMode -> m () +setNtfMode' _c _deviceToken _ntfMode = throwError $ CMD PROHIBITED + +getNtfMode' :: AgentMonad m => AgentClient -> m (NtfTknStatus, NotificationsMode) +getNtfMode' _c = throwError $ CMD PROHIBITED + -- | Delete notification subscription for connection, in Reader monad deleteNtfSub' :: AgentMonad m => AgentClient -> ConnId -> m () deleteNtfSub' _c connId = do diff --git a/src/Simplex/Messaging/Agent/Protocol.hs b/src/Simplex/Messaging/Agent/Protocol.hs index f445f2c03..03077e020 100644 --- a/src/Simplex/Messaging/Agent/Protocol.hs +++ b/src/Simplex/Messaging/Agent/Protocol.hs @@ -83,6 +83,7 @@ module Simplex.Messaging.Agent.Protocol ACorrId, AgentMsgId, AgentPhase (..), + NotificationsMode (..), NotificationInfo (..), -- * Encode/decode @@ -131,6 +132,7 @@ import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Crypto.Ratchet (E2ERatchetParams, E2ERatchetParamsUri) import Simplex.Messaging.Encoding import Simplex.Messaging.Encoding.String +import Simplex.Messaging.Notifications.Protocol (NtfTknStatus) import Simplex.Messaging.Parsers import Simplex.Messaging.Protocol ( ErrorType, @@ -228,6 +230,7 @@ data ACommand (p :: AParty) where OK :: ACommand Agent ERR :: AgentErrorType -> ACommand Agent PHASE :: AgentPhase -> ACommand Agent + NTFMODE :: NtfTknStatus -> NotificationsMode -> ACommand Agent deriving instance Eq (ACommand p) @@ -259,6 +262,25 @@ instance ToJSON AgentPhase where toEncoding = strToJEncoding toJSON = strToJSON +data NotificationsMode = NMOff | NMPeriodic | NMInstant + deriving (Eq, Show) + +instance StrEncoding NotificationsMode where + strEncode = \case + NMOff -> "OFF" + NMPeriodic -> "PERIODIC" + NMInstant -> "INSTANT" + strP = + A.takeTill (== ' ') >>= \case + "OFF" -> pure NMOff + "PERIODIC" -> pure NMPeriodic + "INSTANT" -> pure NMInstant + _ -> fail "bad NotificationsMode" + +instance ToJSON NotificationsMode where + toEncoding = strToJEncoding + toJSON = strToJSON + data NotificationInfo = NotificationInfo { ntfConnId :: ConnId, ntfTs :: SystemTime, @@ -947,6 +969,7 @@ serializeCommand = \case ERR e -> "ERR " <> strEncode e OK -> "OK" PHASE p -> "PHASE " <> strEncode p + NTFMODE t m -> "NTFMODE " <> smpEncode t <> " " <> strEncode m where showTs :: UTCTime -> ByteString showTs = B.pack . formatISO8601Millis From 2809963a2eb3e5066a88ddf354a20d13b96d775a Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Sat, 25 Jun 2022 17:34:08 +0400 Subject: [PATCH 086/131] ntf: register token with NotificationsMode (#429) --- simplexmq.cabal | 1 + src/Simplex/Messaging/Agent.hs | 33 +++++++---------- src/Simplex/Messaging/Agent/Protocol.hs | 6 ++++ src/Simplex/Messaging/Agent/Store/SQLite.hs | 35 ++++++++++++++----- .../Agent/Store/SQLite/Migrations.hs | 4 ++- .../Migrations/M20220625_v2_ntf_mode.hs | 12 +++++++ .../Store/SQLite/Migrations/agent_schema.sql | 3 +- src/Simplex/Messaging/Notifications/Client.hs | 12 ++++--- tests/AgentTests/NotificationTests.hs | 18 +++++----- 9 files changed, 79 insertions(+), 45 deletions(-) create mode 100644 src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220625_v2_ntf_mode.hs diff --git a/simplexmq.cabal b/simplexmq.cabal index 588f65696..318dd8dc9 100644 --- a/simplexmq.cabal +++ b/simplexmq.cabal @@ -49,6 +49,7 @@ library Simplex.Messaging.Agent.Store.SQLite.Migrations.M20220301_snd_queue_keys Simplex.Messaging.Agent.Store.SQLite.Migrations.M20220322_notifications Simplex.Messaging.Agent.Store.SQLite.Migrations.M20220608_v2 + Simplex.Messaging.Agent.Store.SQLite.Migrations.M20220625_v2_ntf_mode Simplex.Messaging.Client Simplex.Messaging.Client.Agent Simplex.Messaging.Crypto diff --git a/src/Simplex/Messaging/Agent.hs b/src/Simplex/Messaging/Agent.hs index 99090613d..c6dd94ce5 100644 --- a/src/Simplex/Messaging/Agent.hs +++ b/src/Simplex/Messaging/Agent.hs @@ -58,7 +58,6 @@ module Simplex.Messaging.Agent enableNtfCron, checkNtfToken, deleteNtfToken, - setNtfMode, getNtfMode, deleteNtfSub, setAgentPhase, @@ -191,8 +190,8 @@ setNtfServers :: AgentErrorMonad m => AgentClient -> [NtfServer] -> m () setNtfServers c = withAgentEnv c . setNtfServers' c -- | Register device notifications token -registerNtfToken :: AgentErrorMonad m => AgentClient -> DeviceToken -> m NtfTknStatus -registerNtfToken c = withAgentEnv c . registerNtfToken' c +registerNtfToken :: AgentErrorMonad m => AgentClient -> DeviceToken -> NotificationsMode -> m NtfTknStatus +registerNtfToken c = withAgentEnv c .: registerNtfToken' c -- | Verify device notifications token verifyNtfToken :: AgentErrorMonad m => AgentClient -> DeviceToken -> ByteString -> C.CbNonce -> m () @@ -208,10 +207,7 @@ checkNtfToken c = withAgentEnv c . checkNtfToken' c deleteNtfToken :: AgentErrorMonad m => AgentClient -> DeviceToken -> m () deleteNtfToken c = withAgentEnv c . deleteNtfToken' c -setNtfMode :: AgentErrorMonad m => AgentClient -> DeviceToken -> NotificationsMode -> m () -setNtfMode c = withAgentEnv c .: setNtfMode' c - -getNtfMode :: AgentErrorMonad m => AgentClient -> m (NtfTknStatus, NotificationsMode) +getNtfMode :: AgentErrorMonad m => AgentClient -> m (DeviceToken, NtfTknStatus, NotificationsMode) getNtfMode c = withAgentEnv c $ getNtfMode' c -- | Delete notification subscription for connection @@ -640,10 +636,10 @@ setSMPServers' :: AgentMonad m => AgentClient -> NonEmpty SMPServer -> m () setSMPServers' c servers = do atomically $ writeTVar (smpServers c) servers -registerNtfToken' :: forall m. AgentMonad m => AgentClient -> DeviceToken -> m NtfTknStatus -registerNtfToken' c deviceToken = +registerNtfToken' :: forall m. AgentMonad m => AgentClient -> DeviceToken -> NotificationsMode -> m NtfTknStatus +registerNtfToken' c deviceToken ntfMode = withStore' c (`getDeviceNtfToken` deviceToken) >>= \case - (Just tkn@NtfToken {ntfTokenId, ntfTknStatus, ntfTknAction}, prevTokens) -> do + (Just tkn@NtfToken {ntfTokenId, ntfTknStatus, ntfTknAction, ntfMode = _currentNtfMode}, prevTokens) -> do mapM_ (deleteToken_ c) prevTokens ns <- asks ntfSupervisor case (ntfTokenId, ntfTknAction) of @@ -655,7 +651,7 @@ registerNtfToken' c deviceToken = (Just tknId, Just (NTACron interval)) -> t tkn (cronSuccess interval) $ agentNtfEnableCron c tknId tkn interval (Just _tknId, Just NTACheck) -> do - if ntfTknStatus == NTActive + if ntfTknStatus == NTActive && ntfMode == NMInstant then initializeNtfSubQ c tkn else atomically $ nsUpdateToken ns tkn pure ntfTknStatus -- TODO @@ -673,7 +669,7 @@ registerNtfToken' c deviceToken = C.SignAlg a -> do tknKeys <- liftIO $ C.generateSignatureKeyPair a dhKeys <- liftIO C.generateKeyPair' - let tkn = newNtfToken deviceToken ntfServer tknKeys dhKeys + let tkn = newNtfToken deviceToken ntfServer tknKeys dhKeys ntfMode withStore' c (`createNtfToken` tkn) registerToken tkn pure NTRegistered @@ -724,10 +720,7 @@ deleteNtfToken' c deviceToken = (Just tkn, _) -> deleteToken_ c tkn _ -> throwError $ CMD PROHIBITED -setNtfMode' :: AgentMonad m => AgentClient -> DeviceToken -> NotificationsMode -> m () -setNtfMode' _c _deviceToken _ntfMode = throwError $ CMD PROHIBITED - -getNtfMode' :: AgentMonad m => AgentClient -> m (NtfTknStatus, NotificationsMode) +getNtfMode' :: AgentMonad m => AgentClient -> m (DeviceToken, NtfTknStatus, NotificationsMode) getNtfMode' _c = throwError $ CMD PROHIBITED -- | Delete notification subscription for connection, in Reader monad @@ -750,7 +743,7 @@ deleteToken_ c tkn@NtfToken {ntfTokenId, ntfTknStatus} = do atomically $ nsRemoveNtfToken ns withToken :: AgentMonad m => AgentClient -> NtfToken -> Maybe (NtfTknStatus, NtfTknAction) -> (NtfTknStatus, Maybe NtfTknAction) -> m a -> m NtfTknStatus -withToken c tkn@NtfToken {deviceToken} from_ (toStatus, toAction_) f = do +withToken c tkn@NtfToken {deviceToken, ntfMode} from_ (toStatus, toAction_) f = do ns <- asks ntfSupervisor forM_ from_ $ \(status, action) -> do withStore' c $ \db -> updateNtfToken db tkn status (Just action) @@ -759,14 +752,14 @@ withToken c tkn@NtfToken {deviceToken} from_ (toStatus, toAction_) f = do Right _ -> do withStore' c $ \db -> updateNtfToken db tkn toStatus toAction_ let updatedToken = tkn {ntfTknStatus = toStatus, ntfTknAction = toAction_} - if toStatus == NTActive + if toStatus == NTActive && ntfMode == NMInstant then initializeNtfSubQ c updatedToken else atomically $ nsUpdateToken ns updatedToken pure toStatus Left e@(NTF AUTH) -> do withStore' c $ \db -> removeNtfToken db tkn atomically $ nsRemoveNtfToken ns - void $ registerNtfToken' c deviceToken + void $ registerNtfToken' c deviceToken ntfMode throwError e Left e -> throwError e @@ -780,7 +773,7 @@ initializeNtfSubQ c tkn = do -- TODO -- There should probably be another function to cancel all subscriptions that would flush the queue first, --- so that superwisor stops processing pending commands? +-- so that supervisor stops processing pending commands? -- It is an optimization, but I am thinking how it would behave if a user were to flip on/off quickly several times. setNtfServers' :: AgentMonad m => AgentClient -> [NtfServer] -> m () diff --git a/src/Simplex/Messaging/Agent/Protocol.hs b/src/Simplex/Messaging/Agent/Protocol.hs index 03077e020..9a54f044c 100644 --- a/src/Simplex/Messaging/Agent/Protocol.hs +++ b/src/Simplex/Messaging/Agent/Protocol.hs @@ -125,6 +125,8 @@ import Data.Time.Clock.System (SystemTime) import Data.Time.ISO8601 import Data.Type.Equality import Data.Typeable () +import Database.SQLite.Simple.FromField +import Database.SQLite.Simple.ToField import GHC.Generics (Generic) import Generic.Random (genericArbitraryU) import Simplex.Messaging.Agent.QueryString @@ -281,6 +283,10 @@ instance ToJSON NotificationsMode where toEncoding = strToJEncoding toJSON = strToJSON +instance ToField NotificationsMode where toField = toField . strEncode + +instance FromField NotificationsMode where fromField = blobFieldDecoder $ parseAll strP + data NotificationInfo = NotificationInfo { ntfConnId :: ConnId, ntfTs :: SystemTime, diff --git a/src/Simplex/Messaging/Agent/Store/SQLite.hs b/src/Simplex/Messaging/Agent/Store/SQLite.hs index cabb883c3..e7dbb257a 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite.hs @@ -69,6 +69,7 @@ module Simplex.Messaging.Agent.Store.SQLite getDeviceNtfToken, updateNtfTokenRegistration, updateNtfToken, + setNtfTokenNtfMode, removeNtfToken, -- Notification subscription persistence getNtfSubscription, @@ -623,15 +624,15 @@ updateRatchet db connId rc skipped = do DB.execute db "INSERT INTO skipped_messages (conn_id, header_key, msg_n, msg_key) VALUES (?, ?, ?, ?)" (connId, hk, msgN, mk) createNtfToken :: DB.Connection -> NtfToken -> IO () -createNtfToken db NtfToken {deviceToken = DeviceToken provider token, ntfServer = srv@ProtocolServer {host, port}, ntfTokenId, ntfPubKey, ntfPrivKey, ntfDhKeys = (ntfDhPubKey, ntfDhPrivKey), ntfDhSecret, ntfTknStatus, ntfTknAction} = do +createNtfToken db NtfToken {deviceToken = DeviceToken provider token, ntfServer = srv@ProtocolServer {host, port}, ntfTokenId, ntfPubKey, ntfPrivKey, ntfDhKeys = (ntfDhPubKey, ntfDhPrivKey), ntfDhSecret, ntfTknStatus, ntfTknAction, ntfMode} = do upsertNtfServer_ db srv DB.execute db [sql| INSERT INTO ntf_tokens - (provider, device_token, ntf_host, ntf_port, tkn_id, tkn_pub_key, tkn_priv_key, tkn_pub_dh_key, tkn_priv_dh_key, tkn_dh_secret, tkn_status, tkn_action) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) + (provider, device_token, ntf_host, ntf_port, tkn_id, tkn_pub_key, tkn_priv_key, tkn_pub_dh_key, tkn_priv_dh_key, tkn_dh_secret, tkn_status, tkn_action, ntf_mode) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) |] - (provider, token, host, port, ntfTokenId, ntfPubKey, ntfPrivKey, ntfDhPubKey, ntfDhPrivKey, ntfDhSecret, ntfTknStatus, ntfTknAction) + ((provider, token, host, port, ntfTokenId, ntfPubKey, ntfPrivKey, ntfDhPubKey, ntfDhPrivKey, ntfDhSecret) :. (ntfTknStatus, ntfTknAction, ntfMode)) getDeviceNtfToken :: DB.Connection -> DeviceToken -> IO (Maybe NtfToken, [NtfToken]) getDeviceNtfToken db t = do @@ -641,16 +642,18 @@ getDeviceNtfToken db t = do db [sql| SELECT s.ntf_host, s.ntf_port, s.ntf_key_hash, - t.provider, t.device_token, t.tkn_id, t.tkn_pub_key, t.tkn_priv_key, t.tkn_pub_dh_key, t.tkn_priv_dh_key, t.tkn_dh_secret, t.tkn_status, t.tkn_action + t.provider, t.device_token, t.tkn_id, t.tkn_pub_key, t.tkn_priv_key, t.tkn_pub_dh_key, t.tkn_priv_dh_key, t.tkn_dh_secret, + t.tkn_status, t.tkn_action, t.ntf_mode FROM ntf_tokens t JOIN ntf_servers s USING (ntf_host, ntf_port) |] pure . first listToMaybe $ partition ((t ==) . deviceToken) tokens where - ntfToken ((host, port, keyHash) :. (provider, dt, ntfTokenId, ntfPubKey, ntfPrivKey, ntfDhPubKey, ntfDhPrivKey, ntfDhSecret, ntfTknStatus, ntfTknAction)) = + ntfToken ((host, port, keyHash) :. (provider, dt, ntfTokenId, ntfPubKey, ntfPrivKey, ntfDhPubKey, ntfDhPrivKey, ntfDhSecret) :. (ntfTknStatus, ntfTknAction, ntfMode_)) = let ntfServer = ProtocolServer {host, port, keyHash} ntfDhKeys = (ntfDhPubKey, ntfDhPrivKey) - in NtfToken {deviceToken = DeviceToken provider dt, ntfServer, ntfTokenId, ntfPubKey, ntfPrivKey, ntfDhKeys, ntfDhSecret, ntfTknStatus, ntfTknAction} + ntfMode = fromMaybe NMOff ntfMode_ + in NtfToken {deviceToken = DeviceToken provider dt, ntfServer, ntfTokenId, ntfPubKey, ntfPrivKey, ntfDhKeys, ntfDhSecret, ntfTknStatus, ntfTknAction, ntfMode} updateNtfTokenRegistration :: DB.Connection -> NtfToken -> NtfTokenId -> C.DhSecretX25519 -> IO () updateNtfTokenRegistration db NtfToken {deviceToken = DeviceToken provider token, ntfServer = ProtocolServer {host, port}} tknId ntfDhSecret = do @@ -676,6 +679,18 @@ updateNtfToken db NtfToken {deviceToken = DeviceToken provider token, ntfServer |] (tknStatus, tknAction, updatedAt, provider, token, host, port) +setNtfTokenNtfMode :: DB.Connection -> NtfToken -> NotificationsMode -> IO () +setNtfTokenNtfMode db NtfToken {deviceToken = DeviceToken provider token, ntfServer = ProtocolServer {host, port}} ntfMode = do + updatedAt <- getCurrentTime + DB.execute + db + [sql| + UPDATE ntf_tokens + SET ntf_mode = ?, updated_at = ? + WHERE provider = ? AND device_token = ? AND ntf_host = ? AND ntf_port = ? + |] + (ntfMode, updatedAt, provider, token, host, port) + removeNtfToken :: DB.Connection -> NtfToken -> IO () removeNtfToken db NtfToken {deviceToken = DeviceToken provider token, ntfServer = ProtocolServer {host, port}} = DB.execute @@ -846,17 +861,19 @@ getActiveNtfToken db = db [sql| SELECT s.ntf_host, s.ntf_port, s.ntf_key_hash, - t.provider, t.device_token, t.tkn_id, t.tkn_pub_key, t.tkn_priv_key, t.tkn_pub_dh_key, t.tkn_priv_dh_key, t.tkn_dh_secret, t.tkn_status, t.tkn_action + t.provider, t.device_token, t.tkn_id, t.tkn_pub_key, t.tkn_priv_key, t.tkn_pub_dh_key, t.tkn_priv_dh_key, t.tkn_dh_secret, + t.tkn_status, t.tkn_action, t.ntf_mode FROM ntf_tokens t JOIN ntf_servers s USING (ntf_host, ntf_port) WHERE t.tkn_status = ? |] (Only NTActive) where - ntfToken ((host, port, keyHash) :. (provider, dt, ntfTokenId, ntfPubKey, ntfPrivKey, ntfDhPubKey, ntfDhPrivKey, ntfDhSecret, ntfTknStatus, ntfTknAction)) = + ntfToken ((host, port, keyHash) :. (provider, dt, ntfTokenId, ntfPubKey, ntfPrivKey, ntfDhPubKey, ntfDhPrivKey, ntfDhSecret) :. (ntfTknStatus, ntfTknAction, ntfMode_)) = let ntfServer = ProtocolServer {host, port, keyHash} ntfDhKeys = (ntfDhPubKey, ntfDhPrivKey) - in NtfToken {deviceToken = DeviceToken provider dt, ntfServer, ntfTokenId, ntfPubKey, ntfPrivKey, ntfDhKeys, ntfDhSecret, ntfTknStatus, ntfTknAction} + ntfMode = fromMaybe NMOff ntfMode_ + in NtfToken {deviceToken = DeviceToken provider dt, ntfServer, ntfTokenId, ntfPubKey, ntfPrivKey, ntfDhKeys, ntfDhSecret, ntfTknStatus, ntfTknAction, ntfMode} getNtfRcvQueue :: DB.Connection -> SMPQueueNtf -> IO (Either StoreError (ConnId, RcvNtfDhSecret)) getNtfRcvQueue db SMPQueueNtf {smpServer = (SMPServer host port _), notifierId} = diff --git a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations.hs b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations.hs index 83a660edc..6b3061792 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations.hs @@ -28,6 +28,7 @@ import Simplex.Messaging.Agent.Store.SQLite.Migrations.M20220101_initial import Simplex.Messaging.Agent.Store.SQLite.Migrations.M20220301_snd_queue_keys import Simplex.Messaging.Agent.Store.SQLite.Migrations.M20220322_notifications import Simplex.Messaging.Agent.Store.SQLite.Migrations.M20220608_v2 +import Simplex.Messaging.Agent.Store.SQLite.Migrations.M20220625_v2_ntf_mode data Migration = Migration {name :: String, up :: Text} deriving (Show) @@ -37,7 +38,8 @@ schemaMigrations = [ ("20220101_initial", m20220101_initial), ("20220301_snd_queue_keys", m20220301_snd_queue_keys), ("20220322_notifications", m20220322_notifications), - ("20220607_v2", m20220608_v2) + ("20220607_v2", m20220608_v2), + ("m20220625_v2_ntf_mode", m20220625_v2_ntf_mode) ] -- | The list of migrations in ascending order by date diff --git a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220625_v2_ntf_mode.hs b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220625_v2_ntf_mode.hs new file mode 100644 index 000000000..1ba921be8 --- /dev/null +++ b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220625_v2_ntf_mode.hs @@ -0,0 +1,12 @@ +{-# LANGUAGE QuasiQuotes #-} + +module Simplex.Messaging.Agent.Store.SQLite.Migrations.M20220625_v2_ntf_mode where + +import Database.SQLite.Simple (Query) +import Database.SQLite.Simple.QQ (sql) + +m20220625_v2_ntf_mode :: Query +m20220625_v2_ntf_mode = + [sql| +ALTER TABLE ntf_tokens ADD COLUMN ntf_mode TEXT NULL; +|] diff --git a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql index 5a4436e8b..daf7cd724 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql +++ b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql @@ -164,7 +164,8 @@ tkn_dh_secret BLOB, -- DH secret for e2e encryption of notifications tkn_status TEXT NOT NULL, tkn_action BLOB, created_at TEXT NOT NULL DEFAULT(datetime('now')), - updated_at TEXT NOT NULL DEFAULT(datetime('now')), -- this is to check token status periodically to know when it was last checked + updated_at TEXT NOT NULL DEFAULT(datetime('now')), + ntf_mode TEXT NULL, -- this is to check token status periodically to know when it was last checked PRIMARY KEY(provider, device_token, ntf_host, ntf_port), FOREIGN KEY(ntf_host, ntf_port) REFERENCES ntf_servers ON DELETE RESTRICT ON UPDATE CASCADE diff --git a/src/Simplex/Messaging/Notifications/Client.hs b/src/Simplex/Messaging/Notifications/Client.hs index 1ac72f590..d73abdfb1 100644 --- a/src/Simplex/Messaging/Notifications/Client.hs +++ b/src/Simplex/Messaging/Notifications/Client.hs @@ -15,7 +15,7 @@ import Data.Time (UTCTime) import Data.Word (Word16) import Database.SQLite.Simple.FromField (FromField (..)) import Database.SQLite.Simple.ToField (ToField (..)) -import Simplex.Messaging.Agent.Protocol (ConnId) +import Simplex.Messaging.Agent.Protocol (ConnId, NotificationsMode (..)) import Simplex.Messaging.Client import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Encoding @@ -119,12 +119,13 @@ data NtfToken = NtfToken -- | token status ntfTknStatus :: NtfTknStatus, -- | pending token action and the earliest time - ntfTknAction :: Maybe NtfTknAction + ntfTknAction :: Maybe NtfTknAction, + ntfMode :: NotificationsMode } deriving (Show) -newNtfToken :: DeviceToken -> NtfServer -> C.ASignatureKeyPair -> C.KeyPair 'C.X25519 -> NtfToken -newNtfToken deviceToken ntfServer (ntfPubKey, ntfPrivKey) ntfDhKeys = +newNtfToken :: DeviceToken -> NtfServer -> C.ASignatureKeyPair -> C.KeyPair 'C.X25519 -> NotificationsMode -> NtfToken +newNtfToken deviceToken ntfServer (ntfPubKey, ntfPrivKey) ntfDhKeys ntfMode = NtfToken { deviceToken, ntfServer, @@ -134,7 +135,8 @@ newNtfToken deviceToken ntfServer (ntfPubKey, ntfPrivKey) ntfDhKeys = ntfDhKeys, ntfDhSecret = Nothing, ntfTknStatus = NTNew, - ntfTknAction = Just NTARegister + ntfTknAction = Just NTARegister, + ntfMode } data NtfSubOrSMPAction = NtfSubAction NtfSubAction | NtfSubSMPAction NtfSubSMPAction diff --git a/tests/AgentTests/NotificationTests.hs b/tests/AgentTests/NotificationTests.hs index f138b1308..99ca7d8e5 100644 --- a/tests/AgentTests/NotificationTests.hs +++ b/tests/AgentTests/NotificationTests.hs @@ -66,7 +66,7 @@ testNotificationToken APNSMockServer {apnsQ} = do a <- getSMPAgentClient agentCfg initAgentServers Right () <- runExceptT $ do let tkn = DeviceToken PPApns "abcd" - NTRegistered <- registerNtfToken a tkn + NTRegistered <- registerNtfToken a tkn NMPeriodic APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData}, sendApnsResponse} <- atomically $ readTBQueue apnsQ verification <- ntfData .-> "verification" @@ -96,13 +96,13 @@ testNtfTokenRepeatRegistration APNSMockServer {apnsQ} = do a <- getSMPAgentClient agentCfg initAgentServers Right () <- runExceptT $ do let tkn = DeviceToken PPApns "abcd" - NTRegistered <- registerNtfToken a tkn + NTRegistered <- registerNtfToken a tkn NMPeriodic APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData}, sendApnsResponse} <- atomically $ readTBQueue apnsQ verification <- ntfData .-> "verification" nonce <- C.cbNonce <$> ntfData .-> "nonce" liftIO $ sendApnsResponse APNSRespOk - NTRegistered <- registerNtfToken a tkn + NTRegistered <- registerNtfToken a tkn NMPeriodic APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData'}, sendApnsResponse = sendApnsResponse'} <- atomically $ readTBQueue apnsQ _ <- ntfData' .-> "verification" @@ -123,7 +123,7 @@ testNtfTokenSecondRegistration APNSMockServer {apnsQ} = do a' <- getSMPAgentClient agentCfg {dbFile = testDB2} initAgentServers Right () <- runExceptT $ do let tkn = DeviceToken PPApns "abcd" - NTRegistered <- registerNtfToken a tkn + NTRegistered <- registerNtfToken a tkn NMPeriodic APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData}, sendApnsResponse} <- atomically $ readTBQueue apnsQ verification <- ntfData .-> "verification" @@ -131,7 +131,7 @@ testNtfTokenSecondRegistration APNSMockServer {apnsQ} = do liftIO $ sendApnsResponse APNSRespOk verifyNtfToken a tkn verification nonce - NTRegistered <- registerNtfToken a' tkn + NTRegistered <- registerNtfToken a' tkn NMPeriodic APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData'}, sendApnsResponse = sendApnsResponse'} <- atomically $ readTBQueue apnsQ verification' <- ntfData' .-> "verification" @@ -158,7 +158,7 @@ testNtfTokenServerRestart t APNSMockServer {apnsQ} = do a <- getSMPAgentClient agentCfg initAgentServers let tkn = DeviceToken PPApns "abcd" Right ntfData <- withNtfServer t . runExceptT $ do - NTRegistered <- registerNtfToken a tkn + NTRegistered <- registerNtfToken a tkn NMPeriodic APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData}, sendApnsResponse} <- atomically $ readTBQueue apnsQ liftIO $ sendApnsResponse APNSRespOk @@ -198,7 +198,7 @@ testNotificationSubscriptionExistingConnection APNSMockServer {apnsQ} = do get bob ##> ("", aliceId, CON) -- register notification token let tkn = DeviceToken PPApns "abcd" - NTRegistered <- registerNtfToken alice tkn + NTRegistered <- registerNtfToken alice tkn NMInstant APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData}, sendApnsResponse} <- atomically $ readTBQueue apnsQ verification <- ntfData .-> "verification" @@ -248,7 +248,7 @@ testNotificationSubscriptionNewConnection APNSMockServer {apnsQ} = do Right () <- runExceptT $ do -- alice registers notification token let aliceTkn = DeviceToken PPApns "abcd" - NTRegistered <- registerNtfToken alice aliceTkn + NTRegistered <- registerNtfToken alice aliceTkn NMPeriodic APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData}, sendApnsResponse} <- atomically $ readTBQueue apnsQ verification <- ntfData .-> "verification" @@ -258,7 +258,7 @@ testNotificationSubscriptionNewConnection APNSMockServer {apnsQ} = do NTActive <- checkNtfToken alice aliceTkn -- bob registers notification token let bobTkn = DeviceToken PPApns "bcde" - NTRegistered <- registerNtfToken bob bobTkn + NTRegistered <- registerNtfToken bob bobTkn NMPeriodic APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData'}, sendApnsResponse = sendApnsResponse'} <- atomically $ readTBQueue apnsQ verification' <- ntfData' .-> "verification" From a8260290e7896a1e75ae595c255171175237cb06 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Sat, 25 Jun 2022 16:17:39 +0100 Subject: [PATCH 087/131] token encodings (#430) --- src/Simplex/Messaging/Agent.hs | 10 +++--- .../Messaging/Notifications/Protocol.hs | 33 +++++++++++++------ 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/src/Simplex/Messaging/Agent.hs b/src/Simplex/Messaging/Agent.hs index c6dd94ce5..514a8b53e 100644 --- a/src/Simplex/Messaging/Agent.hs +++ b/src/Simplex/Messaging/Agent.hs @@ -58,7 +58,7 @@ module Simplex.Messaging.Agent enableNtfCron, checkNtfToken, deleteNtfToken, - getNtfMode, + getNtfToken, deleteNtfSub, setAgentPhase, logConnection, @@ -207,8 +207,8 @@ checkNtfToken c = withAgentEnv c . checkNtfToken' c deleteNtfToken :: AgentErrorMonad m => AgentClient -> DeviceToken -> m () deleteNtfToken c = withAgentEnv c . deleteNtfToken' c -getNtfMode :: AgentErrorMonad m => AgentClient -> m (DeviceToken, NtfTknStatus, NotificationsMode) -getNtfMode c = withAgentEnv c $ getNtfMode' c +getNtfToken :: AgentErrorMonad m => AgentClient -> m (DeviceToken, NtfTknStatus, NotificationsMode) +getNtfToken c = withAgentEnv c $ getNtfToken' c -- | Delete notification subscription for connection deleteNtfSub :: AgentErrorMonad m => AgentClient -> ConnId -> m () @@ -720,8 +720,8 @@ deleteNtfToken' c deviceToken = (Just tkn, _) -> deleteToken_ c tkn _ -> throwError $ CMD PROHIBITED -getNtfMode' :: AgentMonad m => AgentClient -> m (DeviceToken, NtfTknStatus, NotificationsMode) -getNtfMode' _c = throwError $ CMD PROHIBITED +getNtfToken' :: AgentMonad m => AgentClient -> m (DeviceToken, NtfTknStatus, NotificationsMode) +getNtfToken' _c = throwError $ CMD PROHIBITED -- | Delete notification subscription for connection, in Reader monad deleteNtfSub' :: AgentMonad m => AgentClient -> ConnId -> m () diff --git a/src/Simplex/Messaging/Notifications/Protocol.hs b/src/Simplex/Messaging/Notifications/Protocol.hs index 32b1e6d97..5a0f85363 100644 --- a/src/Simplex/Messaging/Notifications/Protocol.hs +++ b/src/Simplex/Messaging/Notifications/Protocol.hs @@ -10,7 +10,7 @@ module Simplex.Messaging.Notifications.Protocol where -import Data.Aeson (FromJSON (..), ToJSON (..)) +import Data.Aeson (FromJSON (..), ToJSON (..), (.=)) import qualified Data.Aeson as J import qualified Data.Aeson.Encoding as JE import qualified Data.Attoparsec.ByteString.Char8 as A @@ -29,7 +29,7 @@ import Simplex.Messaging.Encoding.String import Simplex.Messaging.Notifications.Transport (ntfClientHandshake) import Simplex.Messaging.Parsers (fromTextField_) import Simplex.Messaging.Protocol hiding (Command (..), CommandTag (..)) -import Simplex.Messaging.Util ((<$?>)) +import Simplex.Messaging.Util (eitherToMaybe, (<$?>)) data NtfEntity = Token | Subscription deriving (Show) @@ -349,16 +349,17 @@ instance Encoding PushProvider where 'A' -> pure PPApns _ -> fail "bad PushProvider" -instance TextEncoding PushProvider where - textEncode = \case - PPApns -> "apple" - textDecode = \case - "apple" -> Just PPApns - _ -> Nothing +instance StrEncoding PushProvider where + strEncode = \case + PPApns -> "apns" + strP = + A.takeTill (== ' ') >>= \case + "apns" -> pure PPApns + _ -> fail "bad PushProvider" -instance FromField PushProvider where fromField = fromTextField_ textDecode +instance FromField PushProvider where fromField = fromTextField_ $ eitherToMaybe . strDecode . encodeUtf8 -instance ToField PushProvider where toField = toField . textEncode +instance ToField PushProvider where toField = toField . decodeLatin1 . strEncode data DeviceToken = DeviceToken PushProvider ByteString deriving (Eq, Ord, Show) @@ -367,6 +368,18 @@ instance Encoding DeviceToken where smpEncode (DeviceToken p t) = smpEncode (p, t) smpP = DeviceToken <$> smpP <*> smpP +instance StrEncoding DeviceToken where + strEncode (DeviceToken p t) = strEncode p <> " " <> t + strP = DeviceToken <$> strP <* A.space <*> hexStringP + where + hexStringP = + A.takeWhile (\c -> (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f')) >>= \s -> + if even (B.length s) then pure s else fail "odd number of hex characters" + +instance ToJSON DeviceToken where + toEncoding (DeviceToken pp t) = J.pairs $ "pushProvider" .= decodeLatin1 (strEncode pp) <> "token" .= decodeLatin1 t + toJSON (DeviceToken pp t) = J.object ["pushProvider" .= decodeLatin1 (strEncode pp), "token" .= decodeLatin1 t] + type NtfEntityId = ByteString type NtfSubscriptionId = NtfEntityId From 51d0b48ce15433d8473770522b1eb814688d2aea Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Sun, 26 Jun 2022 14:15:33 +0100 Subject: [PATCH 088/131] suspend/activate agent (#432) * suspend/activate agent * deliver pending messages before agent is suspended * refactor --- src/Simplex/Messaging/Agent.hs | 65 ++++++++++---- src/Simplex/Messaging/Agent/Client.hs | 100 ++++++++++++++++++---- src/Simplex/Messaging/Agent/Env/SQLite.hs | 17 +--- src/Simplex/Messaging/Agent/Protocol.hs | 31 +------ tests/AgentTests/FunctionalAPITests.hs | 80 ++++++++++++++--- 5 files changed, 201 insertions(+), 92 deletions(-) diff --git a/src/Simplex/Messaging/Agent.hs b/src/Simplex/Messaging/Agent.hs index 514a8b53e..797c469aa 100644 --- a/src/Simplex/Messaging/Agent.hs +++ b/src/Simplex/Messaging/Agent.hs @@ -60,7 +60,8 @@ module Simplex.Messaging.Agent deleteNtfToken, getNtfToken, deleteNtfSub, - setAgentPhase, + activateAgent, + suspendAgent, logConnection, ) where @@ -84,6 +85,7 @@ import Data.Time.Clock import Data.Time.Clock.System (systemToUTCTime) import Data.Word (Word16) import qualified Database.SQLite.Simple as DB +-- import GHC.Conc (unsafeIOToSTM) import Simplex.Messaging.Agent.Client import Simplex.Messaging.Agent.Env.SQLite import Simplex.Messaging.Agent.NtfSubSupervisor @@ -107,7 +109,7 @@ import Simplex.Messaging.Util (bshow, eitherToMaybe, liftE, liftError, tryError, import Simplex.Messaging.Version import System.Random (randomR) import UnliftIO.Async (async, race_) -import UnliftIO.Concurrent (forkFinally) +import UnliftIO.Concurrent (forkFinally, forkIO, threadDelay) import qualified UnliftIO.Exception as E import UnliftIO.STM @@ -214,8 +216,13 @@ getNtfToken c = withAgentEnv c $ getNtfToken' c deleteNtfSub :: AgentErrorMonad m => AgentClient -> ConnId -> m () deleteNtfSub c = withAgentEnv c . deleteNtfSub' c -setAgentPhase :: AgentErrorMonad m => AgentClient -> AgentPhase -> m () -setAgentPhase c = withAgentEnv c . setAgentPhase' c +-- | Activate operations +activateAgent :: AgentErrorMonad m => AgentClient -> m () +activateAgent c = withAgentEnv c $ activateAgent' c + +-- | Suspend operations with max delay to deliver pending messages +suspendAgent :: AgentErrorMonad m => AgentClient -> Int -> m () +suspendAgent c = withAgentEnv c . suspendAgent' c withAgentEnv :: AgentClient -> ReaderT Env m a -> m a withAgentEnv c = (`runReaderT` agentEnv c) @@ -479,6 +486,9 @@ resumeMsgDelivery c cData@ConnData {connId} sq@SndQueue {server, sndId} = do queuePendingMsgs :: AgentMonad m => AgentClient -> ConnId -> SndQueue -> [InternalId] -> m () queuePendingMsgs c connId sq msgIds = atomically $ do + modifyTVar' (msgDeliveryOp c) $ \s -> s {opsInProgress = opsInProgress s + length msgIds} + -- s <- readTVar (msgDeliveryOp c) + -- unsafeIOToSTM $ putStrLn $ "msgDeliveryOp: " <> show (opsInProgress s) q <- getPendingMsgQ c connId sq mapM_ (writeTQueue q) msgIds @@ -497,9 +507,11 @@ runSmpQueueMsgDelivery c@AgentClient {subQ} cData@ConnData {connId, duplexHandsh mq <- atomically $ getPendingMsgQ c connId sq ri <- asks $ reconnectInterval . config forever $ do - atomically $ endAgentOperation c AONetwork + atomically $ endAgentOperation c AOSndNetwork msgId <- atomically $ readTQueue mq - atomically $ beginAgentOperation c AONetwork + atomically $ do + beginAgentOperation c AOSndNetwork + endAgentOperation c AOMsgDelivery let mId = unId msgId E.try (withStore c $ \db -> getPendingMsgData db connId msgId) >>= \case Left (e :: E.SomeException) -> @@ -583,9 +595,9 @@ runSmpQueueMsgDelivery c@AgentClient {subQ} cData@ConnData {connId, duplexHandsh notifyDel msgId cmd = notify cmd >> delMsg msgId connError msgId = notifyDel msgId . ERR . CONN retrySending loop = do - atomically $ do - endAgentOperation c AONetwork - beginAgentOperation c AONetwork + -- end... is in a separate atomically because if begin... blocks, SUSPENDED won't be sent + atomically $ endAgentOperation c AOSndNetwork + atomically $ beginAgentOperation c AOSndNetwork loop ackMessage' :: forall m. AgentMonad m => AgentClient -> ConnId -> AgentMsgId -> m () @@ -779,12 +791,31 @@ initializeNtfSubQ c tkn = do setNtfServers' :: AgentMonad m => AgentClient -> [NtfServer] -> m () setNtfServers' c = atomically . writeTVar (ntfServers c) -setAgentPhase' :: AgentMonad m => AgentClient -> AgentPhase -> m () -setAgentPhase' c p = do - aPhase <- asks agentPhase - atomically $ do - writeTVar aPhase (p, False) - notifyAgentPhaseChanged c +activateAgent' :: AgentMonad m => AgentClient -> m () +activateAgent' c = atomically $ do + writeTVar (agentState c) ASActive + activate databaseOp + activate sndNetworkOp + activate msgDeliveryOp + activate rcvNetworkOp + where + activate opSel = modifyTVar' (opSel c) $ \s -> s {opSuspended = False} + +suspendAgent' :: AgentMonad m => AgentClient -> Int -> m () +suspendAgent' c@AgentClient {agentState = as} maxDelay = do + state <- + atomically $ do + writeTVar as ASSuspending + suspendOperation c AORcvNetwork $ + suspendOperation c AOMsgDelivery $ + suspendSendingAndDatabase c + readTVar as + when (state == ASSuspending) . void . forkIO $ do + threadDelay maxDelay + -- liftIO $ putStrLn "suspendAgent after timeout" + atomically . whenSuspending c $ do + -- unsafeIOToSTM $ putStrLn $ "in timeout: suspendSendingAndDatabase" + suspendSendingAndDatabase c getSMPServer :: AgentMonad m => AgentClient -> m SMPServer getSMPServer c = do @@ -798,9 +829,9 @@ getSMPServer c = do subscriber :: (MonadUnliftIO m, MonadReader Env m) => AgentClient -> m () subscriber c@AgentClient {msgQ} = forever $ do - atomically $ endAgentOperation c AONetwork + atomically $ endAgentOperation c AORcvNetwork t <- atomically $ readTBQueue msgQ - atomically $ beginAgentOperation c AONetwork + atomically $ beginAgentOperation c AORcvNetwork withAgentLock c (runExceptT $ processSMPTransmission c t) >>= \case Left e -> liftIO $ print e Right _ -> return () diff --git a/src/Simplex/Messaging/Agent/Client.hs b/src/Simplex/Messaging/Agent/Client.hs index 4cf746f5c..c0f661580 100644 --- a/src/Simplex/Messaging/Agent/Client.hs +++ b/src/Simplex/Messaging/Agent/Client.hs @@ -49,9 +49,15 @@ module Simplex.Messaging.Agent.Client removeSubscription, hasActiveSubscription, agentDbPath, + AgentOperation (..), + AgentOpState (..), + AgentState (..), beginAgentOperation, endAgentOperation, - notifyAgentPhaseChanged, + suspendSendingAndDatabase, + suspendOperation, + notifySuspended, + whenSuspending, withStore, withStore', ) @@ -77,6 +83,7 @@ import Data.Text.Encoding import Data.Word (Word16) import Database.SQLite.Simple (SQLError) import qualified Database.SQLite.Simple as DB +-- import GHC.Conc (unsafeIOToSTM) import Simplex.Messaging.Agent.Env.SQLite import Simplex.Messaging.Agent.Protocol import Simplex.Messaging.Agent.RetryInterval @@ -120,6 +127,11 @@ data AgentClient = AgentClient connMsgsQueued :: TMap ConnId Bool, smpQueueMsgQueues :: TMap (ConnId, SMPServer, SMP.SenderId) (TQueue InternalId), smpQueueMsgDeliveries :: TMap (ConnId, SMPServer, SMP.SenderId) (Async ()), + rcvNetworkOp :: TVar AgentOpState, + msgDeliveryOp :: TVar AgentOpState, + sndNetworkOp :: TVar AgentOpState, + databaseOp :: TVar AgentOpState, + agentState :: TVar AgentState, getMsgLocks :: TMap (SMPServer, SMP.RecipientId) (TMVar ()), reconnections :: TVar [Async ()], asyncClients :: TVar [Async ()], @@ -128,6 +140,21 @@ data AgentClient = AgentClient lock :: TMVar () } +data AgentOperation = AORcvNetwork | AOMsgDelivery | AOSndNetwork | AODatabase + deriving (Eq, Show) + +agentOpSel :: AgentOperation -> (AgentClient -> TVar AgentOpState) +agentOpSel = \case + AORcvNetwork -> rcvNetworkOp + AOMsgDelivery -> msgDeliveryOp + AOSndNetwork -> sndNetworkOp + AODatabase -> databaseOp + +data AgentOpState = AgentOpState {opSuspended :: Bool, opsInProgress :: Int} + +data AgentState = ASActive | ASSuspending | ASSuspended + deriving (Eq, Show) + newAgentClient :: InitialAgentServers -> Env -> STM AgentClient newAgentClient InitialAgentServers {smp, ntf} agentEnv = do let qSize = tbqSize $ config agentEnv @@ -145,12 +172,17 @@ newAgentClient InitialAgentServers {smp, ntf} agentEnv = do connMsgsQueued <- TM.empty smpQueueMsgQueues <- TM.empty smpQueueMsgDeliveries <- TM.empty + rcvNetworkOp <- newTVar $ AgentOpState False 0 + msgDeliveryOp <- newTVar $ AgentOpState False 0 + sndNetworkOp <- newTVar $ AgentOpState False 0 + databaseOp <- newTVar $ AgentOpState False 0 + agentState <- newTVar ASActive getMsgLocks <- TM.empty reconnections <- newTVar [] asyncClients <- newTVar [] clientId <- stateTVar (clientCounter agentEnv) $ \i -> (i + 1, i + 1) lock <- newTMVar () - return AgentClient {active, rcvQ, subQ, msgQ, smpServers, smpClients, ntfServers, ntfClients, subscrSrvrs, pendingSubscrSrvrs, subscrConns, connMsgsQueued, smpQueueMsgQueues, smpQueueMsgDeliveries, getMsgLocks, reconnections, asyncClients, clientId, agentEnv, lock} + return AgentClient {active, rcvQ, subQ, msgQ, smpServers, smpClients, ntfServers, ntfClients, subscrSrvrs, pendingSubscrSrvrs, subscrConns, connMsgsQueued, smpQueueMsgQueues, smpQueueMsgDeliveries, rcvNetworkOp, msgDeliveryOp, sndNetworkOp, databaseOp, agentState, getMsgLocks, reconnections, asyncClients, clientId, agentEnv, lock} agentDbPath :: AgentClient -> FilePath agentDbPath AgentClient {agentEnv = Env {store = SQLiteStore {dbFilePath}}} = dbFilePath @@ -643,25 +675,55 @@ cryptoError = \case e -> INTERNAL $ show e endAgentOperation :: AgentClient -> AgentOperation -> STM () -endAgentOperation c@AgentClient {agentEnv = Env {agentOperations}} op = do - TM.alter (Just . maybe 0 (\n -> max 0 $ n - 1)) op agentOperations - notifyAgentPhaseChanged c +endAgentOperation c op = endOperation c op $ case op of + AORcvNetwork -> + suspendOperation c AOMsgDelivery $ + suspendSendingAndDatabase c + AOMsgDelivery -> + suspendSendingAndDatabase c + AOSndNetwork -> + suspendOperation c AODatabase $ + notifySuspended c + AODatabase -> + notifySuspended c + +suspendSendingAndDatabase :: AgentClient -> STM () +suspendSendingAndDatabase c = + suspendOperation c AOSndNetwork $ + suspendOperation c AODatabase $ + notifySuspended c + +suspendOperation :: AgentClient -> AgentOperation -> STM () -> STM () +suspendOperation c op endedAction = do + n <- stateTVar (agentOpSel op c) $ \s -> (opsInProgress s, s {opSuspended = True}) + -- unsafeIOToSTM $ putStrLn $ "suspendOperation_ " <> show op <> " " <> show n + when (n == 0) $ whenSuspending c endedAction + +notifySuspended :: AgentClient -> STM () +notifySuspended c = do + -- unsafeIOToSTM $ putStrLn "notifySuspended" + writeTBQueue (subQ c) ("", "", SUSPENDED) + writeTVar (agentState c) ASSuspended + +endOperation :: AgentClient -> AgentOperation -> STM () -> STM () +endOperation c op endedAction = do + (suspended, n) <- stateTVar (agentOpSel op c) $ \s -> + let n = max 0 (opsInProgress s - 1) + in ((opSuspended s, n), s {opsInProgress = n}) + -- unsafeIOToSTM $ putStrLn $ "endOperation: " <> show op <> " " <> show suspended <> " " <> show n + when (suspended && n == 0) $ whenSuspending c endedAction + +whenSuspending :: AgentClient -> STM () -> STM () +whenSuspending c = whenM ((== ASSuspending) <$> readTVar (agentState c)) beginAgentOperation :: AgentClient -> AgentOperation -> STM () -beginAgentOperation AgentClient {agentEnv = Env {agentPhase, agentOperations}} op = do - (p, _) <- readTVar agentPhase - when (op `elem` disallowedOperations p) retry - TM.alter (Just . maybe 1 (+ 1)) op agentOperations - -notifyAgentPhaseChanged :: AgentClient -> STM () -notifyAgentPhaseChanged AgentClient {subQ, agentEnv = Env {agentPhase, agentOperations}} = do - (p, notified) <- readTVar agentPhase - unless notified $ do - ops <- readTVar agentOperations - let opsPaused = all (maybe True (== 0) . (`M.lookup` ops)) $ disallowedOperations p - when opsPaused $ do - writeTBQueue subQ ("", "", PHASE p) - writeTVar agentPhase (p, True) +beginAgentOperation c op = do + let opVar = agentOpSel op c + s <- readTVar opVar + -- unsafeIOToSTM $ putStrLn $ "beginOperation? " <> show op <> " " <> show (opsInProgress s) + when (opSuspended s) retry + -- unsafeIOToSTM $ putStrLn $ "beginOperation! " <> show op <> " " <> show (opsInProgress s + 1) + writeTVar opVar $! s {opsInProgress = opsInProgress s + 1} withStore' :: AgentMonad m => AgentClient -> (DB.Connection -> IO a) -> m a withStore' c action = withStore c $ fmap Right . action diff --git a/src/Simplex/Messaging/Agent/Env/SQLite.hs b/src/Simplex/Messaging/Agent/Env/SQLite.hs index 7883706e9..ce3cc0d2d 100644 --- a/src/Simplex/Messaging/Agent/Env/SQLite.hs +++ b/src/Simplex/Messaging/Agent/Env/SQLite.hs @@ -17,8 +17,6 @@ module Simplex.Messaging.Agent.Env.SQLite defaultAgentConfig, defaultReconnectInterval, Env (..), - AgentOperation (..), - disallowedOperations, newSMPAgentEnv, NtfSupervisor (..), NtfSupervisorCommand (..), @@ -121,30 +119,17 @@ data Env = Env idsDrg :: TVar ChaChaDRG, clientCounter :: TVar Int, randomServer :: TVar StdGen, - agentPhase :: TVar (AgentPhase, Bool), - agentOperations :: TMap AgentOperation Int, ntfSupervisor :: NtfSupervisor } -data AgentOperation = AONetwork | AODatabase - deriving (Eq, Ord, Show) - -disallowedOperations :: AgentPhase -> [AgentOperation] -disallowedOperations = \case - APActive -> [] - APPaused -> [AONetwork] - APSuspended -> [AONetwork, AODatabase] - newSMPAgentEnv :: (MonadUnliftIO m, MonadRandom m) => AgentConfig -> m Env newSMPAgentEnv config@AgentConfig {dbFile, yesToMigrations} = do idsDrg <- newTVarIO =<< drgNew store <- liftIO $ createSQLiteStore dbFile Migrations.app yesToMigrations clientCounter <- newTVarIO 0 randomServer <- newTVarIO =<< liftIO newStdGen - agentPhase <- newTVarIO (APActive, True) - agentOperations <- atomically TM.empty ntfSupervisor <- atomically . newNtfSubSupervisor $ tbqSize config - return Env {config, store, idsDrg, clientCounter, randomServer, agentPhase, agentOperations, ntfSupervisor} + return Env {config, store, idsDrg, clientCounter, randomServer, ntfSupervisor} data NtfSupervisor = NtfSupervisor { ntfTkn :: TVar (Maybe NtfToken), diff --git a/src/Simplex/Messaging/Agent/Protocol.hs b/src/Simplex/Messaging/Agent/Protocol.hs index 9a54f044c..ebb4ab301 100644 --- a/src/Simplex/Messaging/Agent/Protocol.hs +++ b/src/Simplex/Messaging/Agent/Protocol.hs @@ -82,7 +82,6 @@ module Simplex.Messaging.Agent.Protocol QueueStatus (..), ACorrId, AgentMsgId, - AgentPhase (..), NotificationsMode (..), NotificationInfo (..), @@ -231,39 +230,13 @@ data ACommand (p :: AParty) where DEL :: ACommand Client OK :: ACommand Agent ERR :: AgentErrorType -> ACommand Agent - PHASE :: AgentPhase -> ACommand Agent + SUSPENDED :: ACommand Agent NTFMODE :: NtfTknStatus -> NotificationsMode -> ACommand Agent deriving instance Eq (ACommand p) deriving instance Show (ACommand p) --- | Agent phase allows to have two agent processes concurrently working with the same database -data AgentPhase - = -- | agent is operating normally - APActive - | -- | agent is paused - no new send/receive operations will be started - they will STM-retry - APPaused - | -- | agent is suspended - no new send/receive/database operations will be started - they will STM-retry - APSuspended - deriving (Eq, Show) - -instance StrEncoding AgentPhase where - strEncode = \case - APActive -> "ACTIVE" - APPaused -> "PAUSED" - APSuspended -> "SUSPENDED" - strP = - A.takeTill (== ' ') >>= \case - "ACTIVE" -> pure APActive - "PAUSED" -> pure APPaused - "SUSPENDED" -> pure APSuspended - _ -> fail "bad AgentPhase" - -instance ToJSON AgentPhase where - toEncoding = strToJEncoding - toJSON = strToJSON - data NotificationsMode = NMOff | NMPeriodic | NMInstant deriving (Eq, Show) @@ -974,7 +947,7 @@ serializeCommand = \case CON -> "CON" ERR e -> "ERR " <> strEncode e OK -> "OK" - PHASE p -> "PHASE " <> strEncode p + SUSPENDED -> "SUSPENDED" NTFMODE t m -> "NTFMODE " <> smpEncode t <> " " <> strEncode m where showTs :: UTCTime -> ByteString diff --git a/tests/AgentTests/FunctionalAPITests.hs b/tests/AgentTests/FunctionalAPITests.hs index 7a9a62f45..0882c7e1b 100644 --- a/tests/AgentTests/FunctionalAPITests.hs +++ b/tests/AgentTests/FunctionalAPITests.hs @@ -89,9 +89,13 @@ functionalAPITests t = do testInactiveClientDisconnected t it "should NOT disconnect active clients" $ testActiveClientNotDisconnected t - describe "Agent phases" $ do - it "should update clients when agent phase changes" $ - withSmpServer t testAgentPhaseChanges + describe "Suspending agent" $ do + it "should update client when agent is suspended" $ + withSmpServer t testSuspendingAgent + it "should complete sending messages when agent is suspended" $ + testSuspendingAgentCompleteSending t + it "should suspend agent on timeout, even if pending messages not sent" $ + testSuspendingAgentTimeout t testAgentClient :: IO () testAgentClient = do @@ -417,8 +421,8 @@ testActiveClientNotDisconnected t = do get alice ##> ("", "", DOWN testSMPServer [connId]) milliseconds ts = systemSeconds ts * 1000 + fromIntegral (systemNanoseconds ts `div` 1000000) -testAgentPhaseChanges :: IO () -testAgentPhaseChanges = do +testSuspendingAgent :: IO () +testSuspendingAgent = do a <- getSMPAgentClient agentCfg initAgentServers b <- getSMPAgentClient agentCfg {dbFile = testDB2} initAgentServers Right () <- runExceptT $ do @@ -427,18 +431,72 @@ testAgentPhaseChanges = do get a ##> ("", bId, SENT 4) get b =##> \case ("", c, Msg "hello") -> c == aId; _ -> False ackMessage b aId 4 - setAgentPhase b APPaused - get b ##> ("", "", PHASE APPaused) + suspendAgent b 1000000 + get b ##> ("", "", SUSPENDED) 5 <- sendMessage a bId SMP.noMsgFlags "hello 2" get a ##> ("", bId, SENT 5) Nothing <- 100000 `timeout` get b - setAgentPhase b APSuspended - get b ##> ("", "", PHASE APSuspended) - setAgentPhase b APActive - get b ##> ("", "", PHASE APActive) + activateAgent b get b =##> \case ("", c, Msg "hello 2") -> c == aId; _ -> False pure () +testSuspendingAgentCompleteSending :: ATransport -> IO () +testSuspendingAgentCompleteSending t = do + a <- getSMPAgentClient agentCfg initAgentServers + b <- getSMPAgentClient agentCfg {dbFile = testDB2} initAgentServers + Right (aId, bId) <- withSmpServerStoreLogOn t testPort $ \_ -> runExceptT $ do + (aId, bId) <- makeConnection a b + 4 <- sendMessage a bId SMP.noMsgFlags "hello" + get a ##> ("", bId, SENT 4) + get b =##> \case ("", c, Msg "hello") -> c == aId; _ -> False + ackMessage b aId 4 + pure (aId, bId) + + Right () <- runExceptT $ do + ("", "", DOWN {}) <- get a + ("", "", DOWN {}) <- get b + 5 <- sendMessage b aId SMP.noMsgFlags "hello too" + 6 <- sendMessage b aId SMP.noMsgFlags "how are you?" + liftIO $ threadDelay 100000 + suspendAgent b 5000000 + + Right () <- withSmpServerStoreLogOn t testPort $ \_ -> runExceptT $ do + get b =##> \case ("", c, SENT 5) -> c == aId; ("", "", UP {}) -> True; _ -> False + get b =##> \case ("", c, SENT 5) -> c == aId; ("", "", UP {}) -> True; _ -> False + get b =##> \case ("", c, SENT 6) -> c == aId; _ -> False + ("", "", SUSPENDED) <- get b + + ("", "", UP {}) <- get a + get a =##> \case ("", c, Msg "hello too") -> c == bId; _ -> False + ackMessage a bId 5 + get a =##> \case ("", c, Msg "how are you?") -> c == bId; _ -> False + ackMessage a bId 6 + + pure () + +testSuspendingAgentTimeout :: ATransport -> IO () +testSuspendingAgentTimeout t = do + a <- getSMPAgentClient agentCfg initAgentServers + b <- getSMPAgentClient agentCfg {dbFile = testDB2} initAgentServers + Right (aId, _) <- withSmpServer t . runExceptT $ do + (aId, bId) <- makeConnection a b + 4 <- sendMessage a bId SMP.noMsgFlags "hello" + get a ##> ("", bId, SENT 4) + get b =##> \case ("", c, Msg "hello") -> c == aId; _ -> False + ackMessage b aId 4 + pure (aId, bId) + + Right () <- runExceptT $ do + ("", "", DOWN {}) <- get a + ("", "", DOWN {}) <- get b + 5 <- sendMessage b aId SMP.noMsgFlags "hello too" + 6 <- sendMessage b aId SMP.noMsgFlags "how are you?" + suspendAgent b 100000 + ("", "", SUSPENDED) <- get b + pure () + + pure () + exchangeGreetings :: AgentClient -> ConnId -> AgentClient -> ConnId -> ExceptT AgentErrorType IO () exchangeGreetings alice bobId bob aliceId = do 4 <- sendMessage alice bobId SMP.noMsgFlags "hello" From 3d6ad64d6214dcaa05cd6993b4682bef42ddeb17 Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Mon, 27 Jun 2022 21:54:35 +0400 Subject: [PATCH 089/131] ntf: registerNtfToken rework, notification modes (#431) * check mode for new subscriptions * check token inside actions * migration - apple -> apns * wip * register logic, modes * update mode, cron config, verify token changes * refactor * fix test * NTFMODE * server: delete subscriptions on deleteToken * refactor markNtfSubscriptionForDeletion * remove NTFMODE * remove subscriptions when token is deleted * refactor * lint * test * check ntfMode Co-authored-by: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> --- src/Simplex/Messaging/Agent.hs | 135 +++++++++-------- src/Simplex/Messaging/Agent/Env/SQLite.hs | 5 +- .../Messaging/Agent/NtfSubSupervisor.hs | 139 ++++++++++-------- src/Simplex/Messaging/Agent/Protocol.hs | 7 +- src/Simplex/Messaging/Agent/Store/SQLite.hs | 85 ++++++----- .../Migrations/M20220322_notifications.hs | 2 +- .../Migrations/M20220625_v2_ntf_mode.hs | 2 + .../Store/SQLite/Migrations/agent_schema.sql | 2 +- src/Simplex/Messaging/Notifications/Client.hs | 3 - src/Simplex/Messaging/Notifications/Server.hs | 9 +- .../Messaging/Notifications/Server/Store.hs | 57 +++---- tests/AgentTests/NotificationTests.hs | 76 +++++++++- 12 files changed, 300 insertions(+), 222 deletions(-) diff --git a/src/Simplex/Messaging/Agent.hs b/src/Simplex/Messaging/Agent.hs index 797c469aa..8b33d3176 100644 --- a/src/Simplex/Messaging/Agent.hs +++ b/src/Simplex/Messaging/Agent.hs @@ -55,7 +55,6 @@ module Simplex.Messaging.Agent setNtfServers, registerNtfToken, verifyNtfToken, - enableNtfCron, checkNtfToken, deleteNtfToken, getNtfToken, @@ -83,7 +82,6 @@ import Data.Maybe (isJust) import qualified Data.Text as T import Data.Time.Clock import Data.Time.Clock.System (systemToUTCTime) -import Data.Word (Word16) import qualified Database.SQLite.Simple as DB -- import GHC.Conc (unsafeIOToSTM) import Simplex.Messaging.Agent.Client @@ -99,7 +97,7 @@ import qualified Simplex.Messaging.Crypto.Ratchet as CR import Simplex.Messaging.Encoding import Simplex.Messaging.Encoding.String (StrEncoding (..)) import Simplex.Messaging.Notifications.Client -import Simplex.Messaging.Notifications.Protocol (DeviceToken, NtfRegCode (NtfRegCode), NtfTknStatus (..)) +import Simplex.Messaging.Notifications.Protocol (DeviceToken, NtfRegCode (NtfRegCode), NtfTknStatus (..), NtfTokenId) import Simplex.Messaging.Notifications.Server.Push.APNS (PNMessageData (..)) import Simplex.Messaging.Parsers (parse) import Simplex.Messaging.Protocol (BrokerMsg, ErrorType (AUTH), MsgBody, MsgFlags, SMPMsgMeta) @@ -199,10 +197,6 @@ registerNtfToken c = withAgentEnv c .: registerNtfToken' c verifyNtfToken :: AgentErrorMonad m => AgentClient -> DeviceToken -> ByteString -> C.CbNonce -> m () verifyNtfToken c = withAgentEnv c .:. verifyNtfToken' c --- | Enable/disable periodic notifications -enableNtfCron :: AgentErrorMonad m => AgentClient -> DeviceToken -> Word16 -> m () -enableNtfCron c = withAgentEnv c .: enableNtfCron' c - checkNtfToken :: AgentErrorMonad m => AgentClient -> DeviceToken -> m NtfTknStatus checkNtfToken c = withAgentEnv c . checkNtfToken' c @@ -641,7 +635,7 @@ deleteConnection' c connId = atomically $ removeSubscription c connId withStore' c (`deleteConn` connId) ns <- asks ntfSupervisor - atomically $ sendNtfSubCommand ns (connId, NSCDelete) + atomically $ writeTBQueue (ntfSubQ ns) (connId, NSCDelete) -- | Change servers to be used for creating new queues, in Reader monad setSMPServers' :: AgentMonad m => AgentClient -> NonEmpty SMPServer -> m () @@ -649,31 +643,50 @@ setSMPServers' c servers = do atomically $ writeTVar (smpServers c) servers registerNtfToken' :: forall m. AgentMonad m => AgentClient -> DeviceToken -> NotificationsMode -> m NtfTknStatus -registerNtfToken' c deviceToken ntfMode = - withStore' c (`getDeviceNtfToken` deviceToken) >>= \case - (Just tkn@NtfToken {ntfTokenId, ntfTknStatus, ntfTknAction, ntfMode = _currentNtfMode}, prevTokens) -> do - mapM_ (deleteToken_ c) prevTokens - ns <- asks ntfSupervisor - case (ntfTokenId, ntfTknAction) of - (Nothing, Just NTARegister) -> registerToken tkn $> NTRegistered +registerNtfToken' c suppliedDeviceToken suppliedNtfMode = + withStore' c getSavedNtfToken >>= \case + Just tkn@NtfToken {deviceToken = savedDeviceToken, ntfTokenId, ntfTknStatus, ntfTknAction, ntfMode = savedNtfMode} -> do + status <- case (ntfTokenId, ntfTknAction) of + (Nothing, Just NTARegister) -> do + when (savedDeviceToken /= suppliedDeviceToken) $ withStore' c $ \db -> updateDeviceToken db tkn suppliedDeviceToken + registerToken tkn $> NTRegistered -- TODO minimal time before repeat registration - (Just _, Nothing) -> when (ntfTknStatus == NTRegistered) (registerToken tkn) $> NTRegistered - (Just tknId, Just (NTAVerify code)) -> - t tkn (NTActive, Just NTACheck) $ agentNtfVerifyToken c tknId tkn code - (Just tknId, Just (NTACron interval)) -> - t tkn (cronSuccess interval) $ agentNtfEnableCron c tknId tkn interval - (Just _tknId, Just NTACheck) -> do - if ntfTknStatus == NTActive && ntfMode == NMInstant - then initializeNtfSubQ c tkn - else atomically $ nsUpdateToken ns tkn - pure ntfTknStatus -- TODO - -- agentNtfCheckToken c tknId tkn >>= \case + (Just tknId, Nothing) + | savedDeviceToken == suppliedDeviceToken -> + when (ntfTknStatus == NTRegistered) (registerToken tkn) $> NTRegistered + | otherwise -> replaceToken tknId $> NTRegistered + (Just tknId, Just (NTAVerify code)) + | savedDeviceToken == suppliedDeviceToken -> + t tkn (NTActive, Just NTACheck) $ agentNtfVerifyToken c tknId tkn code + | otherwise -> replaceToken tknId $> NTRegistered + (Just tknId, Just NTACheck) + | savedDeviceToken == suppliedDeviceToken -> do + ns <- asks ntfSupervisor + atomically $ nsUpdateToken ns tkn {ntfMode = suppliedNtfMode} + when (ntfTknStatus == NTActive) $ do + cron <- asks $ ntfCron . config + agentNtfEnableCron c tknId tkn cron + when (suppliedNtfMode == NMInstant) $ initializeNtfSubs c + when (suppliedNtfMode == NMPeriodic && savedNtfMode == NMInstant) $ smpDeleteNtfSubs c + pure ntfTknStatus -- TODO + -- agentNtfCheckToken c tknId tkn >>= \case + | otherwise -> replaceToken tknId $> NTRegistered (Just tknId, Just NTADelete) -> do agentNtfDeleteToken c tknId tkn withStore' c (`removeNtfToken` tkn) + ns <- asks ntfSupervisor atomically $ nsRemoveNtfToken ns pure NTExpired _ -> pure ntfTknStatus + withStore' c $ \db -> updateNtfMode db tkn suppliedNtfMode + pure status + where + replaceToken :: NtfTokenId -> m () + replaceToken tknId = do + agentNtfReplaceToken c tknId tkn suppliedDeviceToken + withStore' c $ \db -> updateDeviceToken db tkn suppliedDeviceToken + ns <- asks ntfSupervisor + atomically $ nsUpdateToken ns tkn {deviceToken = suppliedDeviceToken, ntfTknStatus = NTRegistered, ntfMode = suppliedNtfMode} _ -> getNtfServer c >>= \case Just ntfServer -> @@ -681,7 +694,7 @@ registerNtfToken' c deviceToken ntfMode = C.SignAlg a -> do tknKeys <- liftIO $ C.generateSignatureKeyPair a dhKeys <- liftIO C.generateKeyPair' - let tkn = newNtfToken deviceToken ntfServer tknKeys dhKeys ntfMode + let tkn = newNtfToken suppliedDeviceToken ntfServer tknKeys dhKeys suppliedNtfMode withStore' c (`createNtfToken` tkn) registerToken tkn pure NTRegistered @@ -694,52 +707,52 @@ registerNtfToken' c deviceToken ntfMode = let dhSecret = C.dh' srvPubDhKey privDhKey withStore' c $ \db -> updateNtfTokenRegistration db tkn tknId dhSecret ns <- asks ntfSupervisor - atomically $ nsUpdateToken ns tkn + atomically $ nsUpdateToken ns tkn {deviceToken = suppliedDeviceToken, ntfTknStatus = NTRegistered, ntfMode = suppliedNtfMode} -- TODO decrypt verification code verifyNtfToken' :: AgentMonad m => AgentClient -> DeviceToken -> ByteString -> C.CbNonce -> m () verifyNtfToken' c deviceToken code nonce = - withStore' c (`getDeviceNtfToken` deviceToken) >>= \case - (Just tkn@NtfToken {ntfTokenId = Just tknId, ntfDhSecret = Just dhSecret}, _) -> do + withStore' c getSavedNtfToken >>= \case + Just tkn@NtfToken {deviceToken = savedDeviceToken, ntfTokenId = Just tknId, ntfDhSecret = Just dhSecret, ntfMode} -> do + when (deviceToken /= savedDeviceToken) . throwError $ CMD PROHIBITED code' <- liftEither . bimap cryptoError NtfRegCode $ C.cbDecrypt dhSecret nonce code - void . withToken c tkn (Just (NTConfirmed, NTAVerify code')) (NTActive, Just NTACheck) $ do - agentNtfVerifyToken c tknId tkn code' + toStatus <- + withToken c tkn (Just (NTConfirmed, NTAVerify code')) (NTActive, Just NTACheck) $ + agentNtfVerifyToken c tknId tkn code' + when (toStatus == NTActive) $ do + cron <- asks $ ntfCron . config + agentNtfEnableCron c tknId tkn cron + when (ntfMode == NMInstant) $ initializeNtfSubs c _ -> throwError $ CMD PROHIBITED -enableNtfCron' :: AgentMonad m => AgentClient -> DeviceToken -> Word16 -> m () -enableNtfCron' c deviceToken interval = do - when (interval < 20) . throwError $ CMD PROHIBITED - withStore' c (`getDeviceNtfToken` deviceToken) >>= \case - (Just tkn@NtfToken {ntfTokenId = Just tknId, ntfTknStatus = NTActive}, _) -> - void . withToken c tkn (Just (NTActive, NTACron interval)) (cronSuccess interval) $ - agentNtfEnableCron c tknId tkn interval - _ -> throwError $ CMD PROHIBITED - -cronSuccess :: Word16 -> (NtfTknStatus, Maybe NtfTknAction) -cronSuccess interval - | interval == 0 = (NTActive, Just NTACheck) - | otherwise = (NTActive, Just $ NTACron interval) - checkNtfToken' :: AgentMonad m => AgentClient -> DeviceToken -> m NtfTknStatus checkNtfToken' c deviceToken = - withStore' c (`getDeviceNtfToken` deviceToken) >>= \case - (Just tkn@NtfToken {ntfTokenId = Just tknId}, _) -> agentNtfCheckToken c tknId tkn + withStore' c getSavedNtfToken >>= \case + Just tkn@NtfToken {deviceToken = savedDeviceToken, ntfTokenId = Just tknId} -> do + when (deviceToken /= savedDeviceToken) . throwError $ CMD PROHIBITED + agentNtfCheckToken c tknId tkn _ -> throwError $ CMD PROHIBITED deleteNtfToken' :: AgentMonad m => AgentClient -> DeviceToken -> m () deleteNtfToken' c deviceToken = - withStore' c (`getDeviceNtfToken` deviceToken) >>= \case - (Just tkn, _) -> deleteToken_ c tkn + withStore' c getSavedNtfToken >>= \case + Just tkn@NtfToken {deviceToken = savedDeviceToken} -> do + when (deviceToken /= savedDeviceToken) . throwError $ CMD PROHIBITED + deleteToken_ c tkn + smpDeleteNtfSubs c _ -> throwError $ CMD PROHIBITED getNtfToken' :: AgentMonad m => AgentClient -> m (DeviceToken, NtfTknStatus, NotificationsMode) -getNtfToken' _c = throwError $ CMD PROHIBITED +getNtfToken' c = + withStore' c getSavedNtfToken >>= \case + Just NtfToken {deviceToken, ntfTknStatus, ntfMode} -> pure (deviceToken, ntfTknStatus, ntfMode) + _ -> throwError $ CMD PROHIBITED -- | Delete notification subscription for connection, in Reader monad deleteNtfSub' :: AgentMonad m => AgentClient -> ConnId -> m () deleteNtfSub' _c connId = do ns <- asks ntfSupervisor - atomically $ sendNtfSubCommand ns (connId, NSCDelete) + atomically $ writeTBQueue (ntfSubQ ns) (connId, NSCDelete) deleteToken_ :: AgentMonad m => AgentClient -> NtfToken -> m () deleteToken_ c tkn@NtfToken {ntfTokenId, ntfTknStatus} = do @@ -764,9 +777,7 @@ withToken c tkn@NtfToken {deviceToken, ntfMode} from_ (toStatus, toAction_) f = Right _ -> do withStore' c $ \db -> updateNtfToken db tkn toStatus toAction_ let updatedToken = tkn {ntfTknStatus = toStatus, ntfTknAction = toAction_} - if toStatus == NTActive && ntfMode == NMInstant - then initializeNtfSubQ c updatedToken - else atomically $ nsUpdateToken ns updatedToken + atomically $ nsUpdateToken ns updatedToken pure toStatus Left e@(NTF AUTH) -> do withStore' c $ \db -> removeNtfToken db tkn @@ -775,14 +786,18 @@ withToken c tkn@NtfToken {deviceToken, ntfMode} from_ (toStatus, toAction_) f = throwError e Left e -> throwError e -initializeNtfSubQ :: AgentMonad m => AgentClient -> NtfToken -> m () -initializeNtfSubQ c tkn = do +initializeNtfSubs :: AgentMonad m => AgentClient -> m () +initializeNtfSubs c = do ns <- asks ntfSupervisor - connIds <- atomically $ do - nsUpdateToken ns tkn - getSubscriptions c + connIds <- atomically $ getSubscriptions c forM_ connIds $ \connId -> atomically $ sendNtfSubCommand ns (connId, NSCCreate) +smpDeleteNtfSubs :: AgentMonad m => AgentClient -> m () +smpDeleteNtfSubs c = do + ns <- asks ntfSupervisor + connIds <- atomically $ getSubscriptions c + forM_ connIds $ \connId -> atomically $ writeTBQueue (ntfSubQ ns) (connId, NSCSmpDelete) + -- TODO -- There should probably be another function to cancel all subscriptions that would flush the queue first, -- so that supervisor stops processing pending commands? diff --git a/src/Simplex/Messaging/Agent/Env/SQLite.hs b/src/Simplex/Messaging/Agent/Env/SQLite.hs index ce3cc0d2d..f8199221b 100644 --- a/src/Simplex/Messaging/Agent/Env/SQLite.hs +++ b/src/Simplex/Messaging/Agent/Env/SQLite.hs @@ -46,6 +46,7 @@ import Simplex.Messaging.Version import System.Random (StdGen, newStdGen) import UnliftIO (Async) import UnliftIO.STM +import Data.Word (Word16) -- | Agent monad with MonadReader Env and MonadError AgentErrorType type AgentMonad m = (MonadUnliftIO m, MonadReader Env m, MonadError AgentErrorType m) @@ -67,6 +68,7 @@ data AgentConfig = AgentConfig reconnectInterval :: RetryInterval, helloTimeout :: NominalDiffTime, resubscriptionConcurrency :: Int, + ntfCron :: Word16, ntfWorkerThrottle :: Int, ntfSubCheckInterval :: NominalDiffTime, ntfMaxMessages :: Int, @@ -101,6 +103,7 @@ defaultAgentConfig = reconnectInterval = defaultReconnectInterval, helloTimeout = 2 * nominalDay, resubscriptionConcurrency = 16, + ntfCron = 20, -- minutes ntfWorkerThrottle = 1000000, -- microseconds ntfSubCheckInterval = nominalDay, ntfMaxMessages = 4, @@ -138,7 +141,7 @@ data NtfSupervisor = NtfSupervisor ntfSMPWorkers :: TMap SMPServer (TMVar (), Async ()) } -data NtfSupervisorCommand = NSCCreate | NSCDelete | NSCNtfWorker NtfServer | NSCNtfSMPWorker SMPServer +data NtfSupervisorCommand = NSCCreate | NSCDelete | NSCSmpDelete | NSCNtfWorker NtfServer | NSCNtfSMPWorker SMPServer newNtfSubSupervisor :: Natural -> STM NtfSupervisor newNtfSubSupervisor qSize = do diff --git a/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs b/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs index 5e7fd56b8..83bcfbbf2 100644 --- a/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs +++ b/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs @@ -27,7 +27,7 @@ import qualified Data.Map.Strict as M import Data.Time (UTCTime, addUTCTime, diffUTCTime, getCurrentTime, nominalDiffTimeToSeconds) import Simplex.Messaging.Agent.Client import Simplex.Messaging.Agent.Env.SQLite -import Simplex.Messaging.Agent.Protocol (AgentErrorType (..), BrokerErrorType (..), ConnId) +import Simplex.Messaging.Agent.Protocol (AgentErrorType (..), BrokerErrorType (..), ConnId, NotificationsMode (..)) import qualified Simplex.Messaging.Agent.Protocol as AP import Simplex.Messaging.Agent.RetryInterval import Simplex.Messaging.Agent.Store @@ -90,10 +90,16 @@ processNtfSub c (connId, cmd) = do addNtfWorker ntfServer _ -> pure () -- error - notification server not configured NSCDelete -> do - withStore' c (`markNtfSubscriptionForDeletion` connId) + withStore' c $ \db -> markNtfSubscriptionForDeletion db connId (NtfSubAction NSADelete) case ntfServer_ of (Just ntfServer) -> addNtfWorker ntfServer _ -> pure () + NSCSmpDelete -> do + withStore' c (`getRcvQueue` connId) >>= \case + Right RcvQueue {server = smpServer} -> do + withStore' c $ \db -> markNtfSubscriptionForDeletion db connId (NtfSubSMPAction NSASmpDelete) + addNtfSMPWorker smpServer + Left _ -> pure () NSCNtfWorker ntfServer -> addNtfWorker ntfServer NSCNtfSMPWorker smpServer -> @@ -119,57 +125,61 @@ processNtfSub c (connId, cmd) = do runNtfWorker :: forall m. AgentMonad m => AgentClient -> NtfServer -> TMVar () -> m () runNtfWorker c srv doWork = forever $ do void . atomically $ readTMVar doWork - getNtfToken >>= \case - Just tkn -> do - nextSub_ <- withStore' c (`getNextNtfSubAction` srv) - case nextSub_ of - Nothing -> noWorkToDo - Just ntfSub@(NtfSubscription {connId}, _) -> do - ri <- asks $ reconnectInterval . config - withRetryInterval ri $ \loop -> - processAction tkn ntfSub - `catchError` ( \e -> - case e of - BROKER NETWORK -> loop - BROKER TIMEOUT -> loop - _ -> ntfInternalError c connId (show e) - ) - _ -> noWorkToDo + nextSub_ <- withStore' c (`getNextNtfSubAction` srv) + case nextSub_ of + Nothing -> noWorkToDo + Just ntfSub@(NtfSubscription {connId}, _) -> do + ri <- asks $ reconnectInterval . config + withRetryInterval ri $ \loop -> + processAction ntfSub + `catchError` ( \e -> + case e of + BROKER NETWORK -> loop + BROKER TIMEOUT -> loop + _ -> ntfInternalError c connId (show e) + ) throttle <- asks $ ntfWorkerThrottle . config liftIO $ threadDelay throttle where noWorkToDo = void . atomically $ tryTakeTMVar doWork - processAction :: NtfToken -> (NtfSubscription, NtfSubAction) -> m () - processAction tkn@NtfToken {ntfTokenId, ntfTknStatus} (ntfSub@NtfSubscription {connId, smpServer, ntfSubId}, ntfSubAction) = do + processAction :: (NtfSubscription, NtfSubAction) -> m () + processAction (ntfSub@NtfSubscription {connId, smpServer, ntfSubId}, ntfSubAction) = do ts <- liftIO getCurrentTime unlessM (rescheduleAction doWork ts ntfSub) $ case ntfSubAction of - NSACreate -> do - rq_ <- withStore' c (`getRcvQueue` connId) - case (rq_, ntfTknStatus, ntfTokenId) of - (Right RcvQueue {clientNtfCreds = Just ClientNtfCreds {ntfPrivateKey, notifierId}}, NTActive, Just tknId) -> do - nSubId <- agentNtfCreateSubscription c tknId tkn (SMPQueueNtf smpServer notifierId) ntfPrivateKey - let actionTs = addUTCTime 30 ts - withStore' c $ \db -> - updateNtfSubscription db connId ntfSub {ntfSubId = Just nSubId, ntfSubStatus = NASCreated NSNew, ntfSubActionTs = actionTs} (NtfSubAction NSACheck) - _ -> ntfInternalError c connId "NSACreate - no notifier queue credentials or token not active" - NSACheck -> case ntfSubId of - Just nSubId -> - agentNtfCheckSubscription c nSubId tkn >>= \case - NSNew -> updateSubNextCheck ts NSNew - NSPending -> updateSubNextCheck ts NSPending - NSActive -> updateSubNextCheck ts NSActive - NSEnd -> updateSubNextCheck ts NSEnd - NSSMPAuth -> updateSub (NASCreated NSSMPAuth) (NtfSubAction NSADelete) ts - Nothing -> ntfInternalError c connId "NSACheck - no subscription ID" + NSACreate -> + getNtfToken >>= \case + Just tkn@NtfToken {ntfTokenId = Just tknId, ntfTknStatus = NTActive, ntfMode = NMInstant} -> do + RcvQueue {clientNtfCreds} <- withStore c (`getRcvQueue` connId) + case clientNtfCreds of + Just ClientNtfCreds {ntfPrivateKey, notifierId} -> do + nSubId <- agentNtfCreateSubscription c tknId tkn (SMPQueueNtf smpServer notifierId) ntfPrivateKey + let actionTs = addUTCTime 30 ts + withStore' c $ \db -> + updateNtfSubscription db connId ntfSub {ntfSubId = Just nSubId, ntfSubStatus = NASCreated NSNew, ntfSubActionTs = actionTs} (NtfSubAction NSACheck) + _ -> ntfInternalError c connId "NSACreate - no notifier queue credentials" + _ -> ntfInternalError c connId "NSACreate - no active token" + NSACheck -> + getNtfToken >>= \case + Just tkn -> + case ntfSubId of + Just nSubId -> + agentNtfCheckSubscription c nSubId tkn >>= \case + NSNew -> updateSubNextCheck ts NSNew + NSPending -> updateSubNextCheck ts NSPending + NSActive -> updateSubNextCheck ts NSActive + NSEnd -> updateSubNextCheck ts NSEnd + NSSMPAuth -> updateSub (NASCreated NSSMPAuth) (NtfSubAction NSADelete) ts -- TODO re-create subscription? + Nothing -> ntfInternalError c connId "NSACheck - no subscription ID" + _ -> ntfInternalError c connId "NSACheck - no active token" NSADelete -> case ntfSubId of Just nSubId -> - agentNtfDeleteSubscription c nSubId tkn + (getNtfToken >>= \tkn -> forM_ tkn $ agentNtfDeleteSubscription c nSubId) `E.finally` do withStore' c $ \db -> updateNtfSubscription db connId ntfSub {ntfSubId = Nothing, ntfSubStatus = NASOff, ntfSubActionTs = ts} (NtfSubSMPAction NSASmpDelete) ns <- asks ntfSupervisor - atomically $ sendNtfSubCommand ns (connId, NSCNtfSMPWorker smpServer) + atomically $ writeTBQueue (ntfSubQ ns) (connId, NSCNtfSMPWorker smpServer) Nothing -> ntfInternalError c connId "NSADelete - no subscription ID" where updateSubNextCheck ts toStatus = do @@ -183,35 +193,32 @@ runNtfWorker c srv doWork = forever $ do runNtfSMPWorker :: forall m. AgentMonad m => AgentClient -> SMPServer -> TMVar () -> m () runNtfSMPWorker c srv doWork = forever $ do void . atomically $ readTMVar doWork - getNtfToken >>= \case - Just tkn -> do - nextSub_ <- withStore' c (`getNextNtfSubSMPAction` srv) - case nextSub_ of - Nothing -> noWorkToDo - Just ntfSub@(NtfSubscription {connId}, _) -> do - ri <- asks $ reconnectInterval . config - withRetryInterval ri $ \loop -> - processAction tkn ntfSub - `catchError` ( \e -> - case e of - BROKER NETWORK -> loop - BROKER TIMEOUT -> loop - _ -> ntfInternalError c connId (show e) - ) - _ -> noWorkToDo + nextSub_ <- withStore' c (`getNextNtfSubSMPAction` srv) + case nextSub_ of + Nothing -> noWorkToDo + Just ntfSub@(NtfSubscription {connId}, _) -> do + ri <- asks $ reconnectInterval . config + withRetryInterval ri $ \loop -> + processAction ntfSub + `catchError` ( \e -> + case e of + BROKER NETWORK -> loop + BROKER TIMEOUT -> loop + _ -> ntfInternalError c connId (show e) + ) throttle <- asks $ ntfWorkerThrottle . config liftIO $ threadDelay throttle where noWorkToDo = void . atomically $ tryTakeTMVar doWork - processAction :: NtfToken -> (NtfSubscription, NtfSubSMPAction) -> m () - processAction NtfToken {ntfTknStatus} (ntfSub@NtfSubscription {connId, ntfServer}, ntfSubAction) = do + processAction :: (NtfSubscription, NtfSubSMPAction) -> m () + processAction (ntfSub@NtfSubscription {connId, ntfServer}, ntfSubAction) = do ts <- liftIO getCurrentTime unlessM (rescheduleAction doWork ts ntfSub) $ case ntfSubAction of - NSASmpKey -> do - rq_ <- withStore' c (`getRcvQueue` connId) - case (rq_, ntfTknStatus) of - (Right rq, NTActive) -> do + NSASmpKey -> + getNtfToken >>= \case + Just NtfToken {ntfTknStatus = NTActive, ntfMode = NMInstant} -> do + rq <- withStore c (`getRcvQueue` connId) C.SignAlg a <- asks (cmdSignAlg . config) (ntfPublicKey, ntfPrivateKey) <- liftIO $ C.generateSignatureKeyPair a (rcvNtfPubDhKey, rcvNtfPrivDhKey) <- liftIO C.generateKeyPair' @@ -222,7 +229,7 @@ runNtfSMPWorker c srv doWork = forever $ do updateNtfSubscription st connId ntfSub {ntfQueueId = Just notifierId, ntfSubStatus = NASKey, ntfSubActionTs = ts} (NtfSubAction NSACreate) ns <- asks ntfSupervisor atomically $ sendNtfSubCommand ns (connId, NSCNtfWorker ntfServer) - _ -> ntfInternalError c connId "NSASmpKey - no rcv queue or token not active" + _ -> ntfInternalError c connId "NSASmpKey - no active token" NSASmpDelete -> do rq_ <- withStore' c (`getRcvQueue` connId) forM_ rq_ $ \rq -> disableQueueNotifications c rq @@ -263,7 +270,11 @@ nsRemoveNtfToken ns = writeTVar (ntfTkn ns) Nothing sendNtfSubCommand :: NtfSupervisor -> (ConnId, NtfSupervisorCommand) -> STM () sendNtfSubCommand ns cmd = readTVar (ntfTkn ns) - >>= mapM_ (\NtfToken {ntfTknStatus} -> when (ntfTknStatus == NTActive) $ writeTBQueue (ntfSubQ ns) cmd) + >>= mapM_ + ( \NtfToken {ntfTknStatus, ntfMode} -> + when (ntfTknStatus == NTActive && ntfMode == NMInstant) $ + writeTBQueue (ntfSubQ ns) cmd + ) closeNtfSupervisor :: NtfSupervisor -> IO () closeNtfSupervisor ns = do diff --git a/src/Simplex/Messaging/Agent/Protocol.hs b/src/Simplex/Messaging/Agent/Protocol.hs index ebb4ab301..938d75b9e 100644 --- a/src/Simplex/Messaging/Agent/Protocol.hs +++ b/src/Simplex/Messaging/Agent/Protocol.hs @@ -133,7 +133,6 @@ import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Crypto.Ratchet (E2ERatchetParams, E2ERatchetParamsUri) import Simplex.Messaging.Encoding import Simplex.Messaging.Encoding.String -import Simplex.Messaging.Notifications.Protocol (NtfTknStatus) import Simplex.Messaging.Parsers import Simplex.Messaging.Protocol ( ErrorType, @@ -231,23 +230,20 @@ data ACommand (p :: AParty) where OK :: ACommand Agent ERR :: AgentErrorType -> ACommand Agent SUSPENDED :: ACommand Agent - NTFMODE :: NtfTknStatus -> NotificationsMode -> ACommand Agent deriving instance Eq (ACommand p) deriving instance Show (ACommand p) -data NotificationsMode = NMOff | NMPeriodic | NMInstant +data NotificationsMode = NMPeriodic | NMInstant deriving (Eq, Show) instance StrEncoding NotificationsMode where strEncode = \case - NMOff -> "OFF" NMPeriodic -> "PERIODIC" NMInstant -> "INSTANT" strP = A.takeTill (== ' ') >>= \case - "OFF" -> pure NMOff "PERIODIC" -> pure NMPeriodic "INSTANT" -> pure NMInstant _ -> fail "bad NotificationsMode" @@ -948,7 +944,6 @@ serializeCommand = \case ERR e -> "ERR " <> strEncode e OK -> "OK" SUSPENDED -> "SUSPENDED" - NTFMODE t m -> "NTFMODE " <> smpEncode t <> " " <> strEncode m where showTs :: UTCTime -> ByteString showTs = B.pack . formatISO8601Millis diff --git a/src/Simplex/Messaging/Agent/Store/SQLite.hs b/src/Simplex/Messaging/Agent/Store/SQLite.hs index e7dbb257a..06b1f5c0d 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite.hs @@ -66,10 +66,11 @@ module Simplex.Messaging.Agent.Store.SQLite updateRatchet, -- Notification device token persistence createNtfToken, - getDeviceNtfToken, + getSavedNtfToken, updateNtfTokenRegistration, + updateDeviceToken, + updateNtfMode, updateNtfToken, - setNtfTokenNtfMode, removeNtfToken, -- Notification subscription persistence getNtfSubscription, @@ -96,12 +97,12 @@ import Control.Concurrent (threadDelay) import Control.Concurrent.STM (stateTVar) import Control.Monad.Except import Crypto.Random (ChaChaDRG, randomBytesGenerate) -import Data.Bifunctor (first, second) +import Data.Bifunctor (second) import Data.ByteString (ByteString) import qualified Data.ByteString.Base64.URL as U import Data.Char (toLower) import Data.Functor (($>)) -import Data.List (find, foldl', partition) +import Data.List (find, foldl') import qualified Data.Map.Strict as M import Data.Maybe (fromMaybe, listToMaybe) import Data.Text (Text) @@ -122,7 +123,7 @@ import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Crypto.Ratchet (RatchetX448, SkippedMsgDiff (..), SkippedMsgKeys) import Simplex.Messaging.Encoding import Simplex.Messaging.Encoding.String -import Simplex.Messaging.Notifications.Client (NtfAgentSubStatus (NASDeleted), NtfServer, NtfSubAction (NSADelete), NtfSubOrSMPAction (..), NtfSubSMPAction, NtfSubscription (..), NtfTknAction, NtfToken (..)) +import Simplex.Messaging.Notifications.Client (NtfAgentSubStatus (..), NtfServer, NtfSubAction (..), NtfSubOrSMPAction (..), NtfSubSMPAction (..), NtfSubscription (..), NtfTknAction (..), NtfToken (..)) import Simplex.Messaging.Notifications.Protocol (DeviceToken (..), NtfSubscriptionId, NtfTknStatus (..), NtfTokenId, SMPQueueNtf (..)) import Simplex.Messaging.Parsers (blobFieldParser, fromTextField_) import Simplex.Messaging.Protocol (MsgBody, MsgFlags, ProtocolServer (..), RcvNtfDhSecret) @@ -634,25 +635,23 @@ createNtfToken db NtfToken {deviceToken = DeviceToken provider token, ntfServer |] ((provider, token, host, port, ntfTokenId, ntfPubKey, ntfPrivKey, ntfDhPubKey, ntfDhPrivKey, ntfDhSecret) :. (ntfTknStatus, ntfTknAction, ntfMode)) -getDeviceNtfToken :: DB.Connection -> DeviceToken -> IO (Maybe NtfToken, [NtfToken]) -getDeviceNtfToken db t = do - tokens <- - map ntfToken - <$> DB.query_ - db - [sql| - SELECT s.ntf_host, s.ntf_port, s.ntf_key_hash, - t.provider, t.device_token, t.tkn_id, t.tkn_pub_key, t.tkn_priv_key, t.tkn_pub_dh_key, t.tkn_priv_dh_key, t.tkn_dh_secret, - t.tkn_status, t.tkn_action, t.ntf_mode - FROM ntf_tokens t - JOIN ntf_servers s USING (ntf_host, ntf_port) - |] - pure . first listToMaybe $ partition ((t ==) . deviceToken) tokens +getSavedNtfToken :: DB.Connection -> IO (Maybe NtfToken) +getSavedNtfToken db = do + maybeFirstRow ntfToken $ + DB.query_ + db + [sql| + SELECT s.ntf_host, s.ntf_port, s.ntf_key_hash, + t.provider, t.device_token, t.tkn_id, t.tkn_pub_key, t.tkn_priv_key, t.tkn_pub_dh_key, t.tkn_priv_dh_key, t.tkn_dh_secret, + t.tkn_status, t.tkn_action, t.ntf_mode + FROM ntf_tokens t + JOIN ntf_servers s USING (ntf_host, ntf_port) + |] where ntfToken ((host, port, keyHash) :. (provider, dt, ntfTokenId, ntfPubKey, ntfPrivKey, ntfDhPubKey, ntfDhPrivKey, ntfDhSecret) :. (ntfTknStatus, ntfTknAction, ntfMode_)) = let ntfServer = ProtocolServer {host, port, keyHash} ntfDhKeys = (ntfDhPubKey, ntfDhPrivKey) - ntfMode = fromMaybe NMOff ntfMode_ + ntfMode = fromMaybe NMPeriodic ntfMode_ in NtfToken {deviceToken = DeviceToken provider dt, ntfServer, ntfTokenId, ntfPubKey, ntfPrivKey, ntfDhKeys, ntfDhSecret, ntfTknStatus, ntfTknAction, ntfMode} updateNtfTokenRegistration :: DB.Connection -> NtfToken -> NtfTokenId -> C.DhSecretX25519 -> IO () @@ -667,6 +666,30 @@ updateNtfTokenRegistration db NtfToken {deviceToken = DeviceToken provider token |] (tknId, ntfDhSecret, NTRegistered, Nothing :: Maybe NtfTknAction, updatedAt, provider, token, host, port) +updateDeviceToken :: DB.Connection -> NtfToken -> DeviceToken -> IO () +updateDeviceToken db NtfToken {deviceToken = DeviceToken provider token, ntfServer = ProtocolServer {host, port}} (DeviceToken toProvider toToken) = do + updatedAt <- getCurrentTime + DB.execute + db + [sql| + UPDATE ntf_tokens + SET provider = ?, device_token = ?, tkn_status = ?, tkn_action = ?, updated_at = ? + WHERE provider = ? AND device_token = ? AND ntf_host = ? AND ntf_port = ? + |] + (toProvider, toToken, NTRegistered, Nothing :: Maybe NtfTknAction, updatedAt, provider, token, host, port) + +updateNtfMode :: DB.Connection -> NtfToken -> NotificationsMode -> IO () +updateNtfMode db NtfToken {deviceToken = DeviceToken provider token, ntfServer = ProtocolServer {host, port}} ntfMode = do + updatedAt <- getCurrentTime + DB.execute + db + [sql| + UPDATE ntf_tokens + SET ntf_mode = ?, updated_at = ? + WHERE provider = ? AND device_token = ? AND ntf_host = ? AND ntf_port = ? + |] + (ntfMode, updatedAt, provider, token, host, port) + updateNtfToken :: DB.Connection -> NtfToken -> NtfTknStatus -> Maybe NtfTknAction -> IO () updateNtfToken db NtfToken {deviceToken = DeviceToken provider token, ntfServer = ProtocolServer {host, port}} tknStatus tknAction = do updatedAt <- getCurrentTime @@ -679,18 +702,6 @@ updateNtfToken db NtfToken {deviceToken = DeviceToken provider token, ntfServer |] (tknStatus, tknAction, updatedAt, provider, token, host, port) -setNtfTokenNtfMode :: DB.Connection -> NtfToken -> NotificationsMode -> IO () -setNtfTokenNtfMode db NtfToken {deviceToken = DeviceToken provider token, ntfServer = ProtocolServer {host, port}} ntfMode = do - updatedAt <- getCurrentTime - DB.execute - db - [sql| - UPDATE ntf_tokens - SET ntf_mode = ?, updated_at = ? - WHERE provider = ? AND device_token = ? AND ntf_host = ? AND ntf_port = ? - |] - (ntfMode, updatedAt, provider, token, host, port) - removeNtfToken :: DB.Connection -> NtfToken -> IO () removeNtfToken db NtfToken {deviceToken = DeviceToken provider token, ntfServer = ProtocolServer {host, port}} = DB.execute @@ -737,8 +748,8 @@ createNtfSubscription db NtfSubscription {connId, smpServer = (SMPServer host po where (ntfSubAction, ntfSubSMPAction) = ntfSubAndSMPAction ntfAction -markNtfSubscriptionForDeletion :: DB.Connection -> ConnId -> IO () -markNtfSubscriptionForDeletion db connId = do +markNtfSubscriptionForDeletion :: DB.Connection -> ConnId -> NtfSubOrSMPAction -> IO () +markNtfSubscriptionForDeletion db connId ntfAction = do updatedAt <- getCurrentTime DB.execute db @@ -747,7 +758,9 @@ markNtfSubscriptionForDeletion db connId = do SET ntf_sub_action = ?, ntf_sub_smp_action = ?, ntf_sub_action_ts = ?, updated_by_supervisor = ?, updated_at = ? WHERE conn_id = ? |] - (Just NSADelete, Nothing :: Maybe NtfSubSMPAction, updatedAt, True, updatedAt, connId) + (ntfSubAction, ntfSubSMPAction, updatedAt, True, updatedAt, connId) + where + (ntfSubAction, ntfSubSMPAction) = ntfSubAndSMPAction ntfAction updateNtfSubscription :: DB.Connection -> ConnId -> NtfSubscription -> NtfSubOrSMPAction -> IO () updateNtfSubscription db connId NtfSubscription {ntfQueueId, ntfSubId, ntfSubStatus, ntfSubActionTs} ntfAction = do @@ -872,7 +885,7 @@ getActiveNtfToken db = ntfToken ((host, port, keyHash) :. (provider, dt, ntfTokenId, ntfPubKey, ntfPrivKey, ntfDhPubKey, ntfDhPrivKey, ntfDhSecret) :. (ntfTknStatus, ntfTknAction, ntfMode_)) = let ntfServer = ProtocolServer {host, port, keyHash} ntfDhKeys = (ntfDhPubKey, ntfDhPrivKey) - ntfMode = fromMaybe NMOff ntfMode_ + ntfMode = fromMaybe NMPeriodic ntfMode_ in NtfToken {deviceToken = DeviceToken provider dt, ntfServer, ntfTokenId, ntfPubKey, ntfPrivKey, ntfDhKeys, ntfDhSecret, ntfTknStatus, ntfTknAction, ntfMode} getNtfRcvQueue :: DB.Connection -> SMPQueueNtf -> IO (Either StoreError (ConnId, RcvNtfDhSecret)) diff --git a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220322_notifications.hs b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220322_notifications.hs index 4ee6695d4..3ff01fd87 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220322_notifications.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220322_notifications.hs @@ -18,7 +18,7 @@ CREATE TABLE ntf_servers ( ) WITHOUT ROWID; CREATE TABLE ntf_tokens ( - provider TEXT NOT NULL, -- apn + provider TEXT NOT NULL, -- apns device_token TEXT NOT NULL, -- ! this field is mislabeled and is actually saved as binary ntf_host TEXT NOT NULL, ntf_port TEXT NOT NULL, diff --git a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220625_v2_ntf_mode.hs b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220625_v2_ntf_mode.hs index 1ba921be8..d0a82a28f 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220625_v2_ntf_mode.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220625_v2_ntf_mode.hs @@ -9,4 +9,6 @@ m20220625_v2_ntf_mode :: Query m20220625_v2_ntf_mode = [sql| ALTER TABLE ntf_tokens ADD COLUMN ntf_mode TEXT NULL; + +DELETE FROM ntf_tokens; |] diff --git a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql index daf7cd724..bda5853e2 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql +++ b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql @@ -151,7 +151,7 @@ CREATE TABLE ntf_servers( PRIMARY KEY(ntf_host, ntf_port) ) WITHOUT ROWID; CREATE TABLE ntf_tokens( - provider TEXT NOT NULL, -- apn + provider TEXT NOT NULL, -- apns device_token TEXT NOT NULL, -- ! this field is mislabeled and is actually saved as binary ntf_host TEXT NOT NULL, ntf_port TEXT NOT NULL, diff --git a/src/Simplex/Messaging/Notifications/Client.hs b/src/Simplex/Messaging/Notifications/Client.hs index d73abdfb1..2432687b2 100644 --- a/src/Simplex/Messaging/Notifications/Client.hs +++ b/src/Simplex/Messaging/Notifications/Client.hs @@ -80,7 +80,6 @@ data NtfTknAction = NTARegister | NTAVerify NtfRegCode -- code to verify token | NTACheck - | NTACron Word16 | NTADelete deriving (Show) @@ -89,14 +88,12 @@ instance Encoding NtfTknAction where NTARegister -> "R" NTAVerify code -> smpEncode ('V', code) NTACheck -> "C" - NTACron interval -> smpEncode ('I', interval) NTADelete -> "D" smpP = A.anyChar >>= \case 'R' -> pure NTARegister 'V' -> NTAVerify <$> smpP 'C' -> pure NTACheck - 'I' -> NTACron <$> smpP 'D' -> pure NTADelete _ -> fail "bad NtfTknAction" diff --git a/src/Simplex/Messaging/Notifications/Server.hs b/src/Simplex/Messaging/Notifications/Server.hs index e832af5c2..fbae87dc5 100644 --- a/src/Simplex/Messaging/Notifications/Server.hs +++ b/src/Simplex/Messaging/Notifications/Server.hs @@ -319,7 +319,9 @@ client NtfServerClient {rcvQ, sndQ} NtfSubscriber {newSubQ, smpAgent = ca} NtfPu TDEL -> do logDebug "TDEL" st <- asks store - atomically $ deleteNtfToken st tknId + qs <- atomically $ deleteNtfToken st tknId + forM_ qs $ \SMPQueueNtf {smpServer, notifierId} -> + atomically $ removeSubscription ca smpServer (SPNotifier, notifierId) cancelInvervalNotifications tknId pure NROk TCRN 0 -> do @@ -371,9 +373,8 @@ client NtfServerClient {rcvQ, sndQ} NtfSubscriber {newSubQ, smpAgent = ca} NtfPu SDEL -> do logDebug "SDEL" st <- asks store - atomically $ do - deleteNtfSubscription st subId - removeSubscription ca smpServer (SPNotifier, notifierId) + atomically $ deleteNtfSubscription st subId + atomically $ removeSubscription ca smpServer (SPNotifier, notifierId) pure NROk PING -> pure NRPong getId :: m NtfEntityId diff --git a/src/Simplex/Messaging/Notifications/Server/Store.hs b/src/Simplex/Messaging/Notifications/Server/Store.hs index 018608658..2d063d93a 100644 --- a/src/Simplex/Messaging/Notifications/Server/Store.hs +++ b/src/Simplex/Messaging/Notifications/Server/Store.hs @@ -12,7 +12,9 @@ module Simplex.Messaging.Notifications.Server.Store where import Control.Concurrent.STM import Control.Monad import Data.ByteString.Char8 (ByteString) +import Data.Functor (($>)) import qualified Data.Map.Strict as M +import Data.Maybe (catMaybes) import Data.Set (Set) import qualified Data.Set as S import qualified Simplex.Messaging.Crypto as C @@ -121,8 +123,7 @@ removeTokenRegistration st NtfTknData {ntfTknId = tId, token, tknVerifyKey} = >>= mapM_ (\tId' -> when (tId == tId') $ TM.delete k regs) k = C.toPubKey C.pubKeyBytes tknVerifyKey --- TODO delete token subscriptions -deleteNtfToken :: NtfStore -> NtfTokenId -> STM () +deleteNtfToken :: NtfStore -> NtfTokenId -> STM [SMPQueueNtf] deleteNtfToken st tknId = do TM.lookupDelete tknId (tokens st) >>= mapM_ @@ -134,6 +135,22 @@ deleteNtfToken st tknId = do whenM (TM.null tIds) $ TM.delete token regs ) ) + + qs <- + TM.lookupDelete tknId (tokenSubscriptions st) + >>= mapM + ( readTVar + >=> mapM + ( \subId -> do + TM.lookupDelete subId (subscriptions st) + >>= mapM + ( \NtfSubData {smpQueue} -> + TM.delete smpQueue (subscriptionLookup st) $> smpQueue + ) + ) + . S.toList + ) + pure $ maybe [] catMaybes qs where regs = tokenRegistrations st regKey = C.toPubKey C.pubKeyBytes @@ -187,39 +204,3 @@ deleteNtfSubscription st subId = do ts_ <- TM.lookup tokenId (tokenSubscriptions st) forM_ ts_ $ \ts -> modifyTVar' ts $ S.delete subId ) - --- getNtfRec :: NtfStore -> SNtfEntity e -> NtfEntityId -> STM (Maybe (NtfEntityRec e)) --- getNtfRec st ent entId = case ent of --- SToken -> NtfTkn <$$> TM.lookup entId (tokens st) --- SSubscription -> pure Nothing - --- getNtfVerifyKey :: NtfStore -> SNtfEntity e -> NtfEntityId -> STM (Maybe (NtfEntityRec e, C.APublicVerifyKey)) --- getNtfVerifyKey st ent entId = --- getNtfRec st ent entId >>= \case --- Just r@(NtfTkn NtfTknData {tknVerifyKey}) -> pure $ Just (r, tknVerifyKey) --- Just r@(NtfSub NtfSubData {tokenId}) -> --- getNtfRec st SToken tokenId >>= \case --- Just (NtfTkn NtfTknData {tknVerifyKey}) -> pure $ Just (r, tknVerifyKey) --- _ -> pure Nothing --- _ -> pure Nothing - --- mkNtfSubsciption :: SMPQueueNtf -> NtfTokenId -> STM NtfSubsciption --- mkNtfSubsciption smpQueue tokenId = do --- subStatus <- newTVar NSNew --- pure NtfSubsciption {smpQueue, tokenId, subStatus} - --- getNtfSub :: NtfSubscriptionsStore -> NtfSubsciptionId -> STM (Maybe NtfSubsciption) --- getNtfSub st subId = pure Nothing -- maybe (pure $ Left AUTH) (fmap Right . readTVar) . M.lookup subId . subscriptions =<< readTVar st - --- getNtfSubViaSMPQueue :: NtfSubscriptionsStore -> SMPQueueNtf -> STM (Maybe NtfSubsciption) --- getNtfSubViaSMPQueue st smpQueue = pure Nothing - --- -- replace keeping status --- updateNtfSub :: NtfSubscriptionsStore -> NtfSubsciption -> SMPQueueNtf -> NtfTokenId -> C.DhSecretX25519 -> STM (Maybe ()) --- updateNtfSub st sub smpQueue tokenId dhSecret = pure Nothing - --- addNtfSub :: NtfSubscriptionsStore -> NtfSubsciptionId -> NtfSubsciption -> STM (Maybe ()) --- addNtfSub st subId sub = pure Nothing - --- deleteNtfSub :: NtfSubscriptionsStore -> NtfSubsciptionId -> STM () --- deleteNtfSub st subId = pure () diff --git a/tests/AgentTests/NotificationTests.hs b/tests/AgentTests/NotificationTests.hs index 99ca7d8e5..81b78ace1 100644 --- a/tests/AgentTests/NotificationTests.hs +++ b/tests/AgentTests/NotificationTests.hs @@ -31,13 +31,18 @@ import Simplex.Messaging.Protocol (ErrorType (AUTH), MsgFlags (MsgFlags), SMPMsg import qualified Simplex.Messaging.Protocol as SMP import Simplex.Messaging.Transport (ATransport) import Simplex.Messaging.Util (tryE) -import System.Directory (removeFile) +import System.Directory (doesFileExist, removeFile) import Test.Hspec import UnliftIO +removeFileIfExists :: FilePath -> IO () +removeFileIfExists filePath = do + fileExists <- doesFileExist filePath + when fileExists $ removeFile filePath + notificationTests :: ATransport -> Spec notificationTests t = - after_ (removeFile testDB) $ do + after_ (removeFile testDB >> removeFileIfExists testDB2) $ do describe "Managing notification tokens" $ do it "should register and verify notification token" $ withAPNSMockServer $ \apns -> @@ -60,6 +65,10 @@ notificationTests t = withSmpServer t $ withAPNSMockServer $ \apns -> withNtfServer t $ testNotificationSubscriptionNewConnection apns + it "should change notifications mode" $ \_ -> + withSmpServer t $ + withAPNSMockServer $ \apns -> + withNtfServer t $ testChangeNotificationsMode apns testNotificationToken :: APNSMockServer -> IO () testNotificationToken APNSMockServer {apnsQ} = do @@ -73,7 +82,6 @@ testNotificationToken APNSMockServer {apnsQ} = do nonce <- C.cbNonce <$> ntfData .-> "nonce" liftIO $ sendApnsResponse APNSRespOk verifyNtfToken a tkn verification nonce - enableNtfCron a tkn 30 NTActive <- checkNtfToken a tkn deleteNtfToken a tkn -- agent deleted this token @@ -110,7 +118,6 @@ testNtfTokenRepeatRegistration APNSMockServer {apnsQ} = do liftIO $ sendApnsResponse' APNSRespOk -- can still use the first verification code, it is the same after decryption verifyNtfToken a tkn verification nonce - enableNtfCron a tkn 30 NTActive <- checkNtfToken a tkn pure () pure () @@ -149,7 +156,6 @@ testNtfTokenSecondRegistration APNSMockServer {apnsQ} = do Left (NTF AUTH) <- tryE $ checkNtfToken a tkn -- and the second is active NTActive <- checkNtfToken a' tkn - enableNtfCron a' tkn 30 pure () pure () @@ -180,7 +186,7 @@ testNtfTokenServerRestart t APNSMockServer {apnsQ} = do liftIO $ sendApnsResponse' APNSRespOk verifyNtfToken a' tkn verification' nonce' NTActive <- checkNtfToken a' tkn - enableNtfCron a' tkn 30 + pure () pure () testNotificationSubscriptionExistingConnection :: APNSMockServer -> IO () @@ -248,7 +254,7 @@ testNotificationSubscriptionNewConnection APNSMockServer {apnsQ} = do Right () <- runExceptT $ do -- alice registers notification token let aliceTkn = DeviceToken PPApns "abcd" - NTRegistered <- registerNtfToken alice aliceTkn NMPeriodic + NTRegistered <- registerNtfToken alice aliceTkn NMInstant APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData}, sendApnsResponse} <- atomically $ readTBQueue apnsQ verification <- ntfData .-> "verification" @@ -258,7 +264,7 @@ testNotificationSubscriptionNewConnection APNSMockServer {apnsQ} = do NTActive <- checkNtfToken alice aliceTkn -- bob registers notification token let bobTkn = DeviceToken PPApns "bcde" - NTRegistered <- registerNtfToken bob bobTkn NMPeriodic + NTRegistered <- registerNtfToken bob bobTkn NMInstant APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData'}, sendApnsResponse = sendApnsResponse'} <- atomically $ readTBQueue apnsQ verification' <- ntfData' .-> "verification" @@ -302,6 +308,60 @@ testNotificationSubscriptionNewConnection APNSMockServer {apnsQ} = do baseId = 3 msgId = subtract baseId +testChangeNotificationsMode :: APNSMockServer -> IO () +testChangeNotificationsMode APNSMockServer {apnsQ} = do + alice <- getSMPAgentClient agentCfg initAgentServers + bob <- getSMPAgentClient agentCfg {dbFile = testDB2} initAgentServers + Right () <- runExceptT $ do + -- establish connection + (bobId, qInfo) <- createConnection alice SCMInvitation + aliceId <- joinConnection bob qInfo "bob's connInfo" + ("", _, CONF confId "bob's connInfo") <- get alice + allowConnection alice bobId confId "alice's connInfo" + get bob ##> ("", aliceId, INFO "alice's connInfo") + get alice ##> ("", bobId, CON) + get bob ##> ("", aliceId, CON) + -- register notification token, set mode to NMPeriodic + let tkn = DeviceToken PPApns "abcd" + NTRegistered <- registerNtfToken alice tkn NMPeriodic + APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData}, sendApnsResponse} <- + atomically $ readTBQueue apnsQ + verification <- ntfData .-> "verification" + verificationNonce <- C.cbNonce <$> ntfData .-> "nonce" + liftIO $ sendApnsResponse APNSRespOk + verifyNtfToken alice tkn verification verificationNonce + NTActive <- checkNtfToken alice tkn + -- send message, no notification + 1 <- msgId <$> sendMessage bob aliceId (SMP.MsgFlags True) "hello" + get bob ##> ("", aliceId, SENT $ baseId + 1) + get alice =##> \case ("", c, Msg "hello") -> c == bobId; _ -> False + ackMessage alice bobId $ baseId + 1 + -- set mode to NMInstant + NTActive <- registerNtfToken alice tkn NMInstant + -- send message, receive notification + liftIO $ threadDelay 500000 + 2 <- msgId <$> sendMessage bob aliceId (SMP.MsgFlags True) "hello again" + get bob ##> ("", aliceId, SENT $ baseId + 2) + void $ messageNotification apnsQ + get alice =##> \case ("", c, Msg "hello again") -> c == bobId; _ -> False + ackMessage alice bobId $ baseId + 2 + -- reset mode to NMPeriodic + NTActive <- registerNtfToken alice tkn NMPeriodic + -- send message, no notification + liftIO $ threadDelay 500000 + 3 <- msgId <$> sendMessage bob aliceId (SMP.MsgFlags True) "hello there" + get bob ##> ("", aliceId, SENT $ baseId + 3) + get alice =##> \case ("", c, Msg "hello there") -> c == bobId; _ -> False + ackMessage alice bobId $ baseId + 3 + -- no notifications should follow + 500000 `timeout` atomically (readTBQueue apnsQ) >>= \case + Nothing -> pure () + _ -> error "unexpected notification" + pure () + where + baseId = 3 + msgId = subtract baseId + messageNotification :: TBQueue APNSMockRequest -> ExceptT AgentErrorType IO (C.CbNonce, ByteString) messageNotification apnsQ = do 500000 `timeout` atomically (readTBQueue apnsQ) >>= \case From 0c7c0d066d390f50ad08874814313adf7d8521a7 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Mon, 27 Jun 2022 19:20:23 +0100 Subject: [PATCH 090/131] support dev/prod APNS hosts (#433) * support dev/prod APNS hosts * fix test --- .../Messaging/Notifications/Protocol.hs | 20 +++++++++----- .../Messaging/Notifications/Server/Env.hs | 12 +++------ .../Notifications/Server/Push/APNS.hs | 27 +++++++++++-------- tests/AgentTests/NotificationTests.hs | 16 +++++------ tests/NtfClient.hs | 3 +-- tests/NtfServerTests.hs | 10 +++---- 6 files changed, 48 insertions(+), 40 deletions(-) diff --git a/src/Simplex/Messaging/Notifications/Protocol.hs b/src/Simplex/Messaging/Notifications/Protocol.hs index 5a0f85363..b9deb45d2 100644 --- a/src/Simplex/Messaging/Notifications/Protocol.hs +++ b/src/Simplex/Messaging/Notifications/Protocol.hs @@ -338,23 +338,31 @@ instance StrEncoding SMPQueueNtf where strEncode SMPQueueNtf {smpServer, notifierId} = strEncode smpServer <> "/" <> strEncode notifierId strP = SMPQueueNtf <$> strP <* A.char '/' <*> strP -data PushProvider = PPApns +data PushProvider = PPApnsDev | PPApnsProd | PPApnsTest deriving (Eq, Ord, Show) instance Encoding PushProvider where smpEncode = \case - PPApns -> "A" + PPApnsDev -> "AD" + PPApnsProd -> "AP" + PPApnsTest -> "AT" smpP = - A.anyChar >>= \case - 'A' -> pure PPApns + A.take 2 >>= \case + "AD" -> pure PPApnsDev + "AP" -> pure PPApnsProd + "AT" -> pure PPApnsTest _ -> fail "bad PushProvider" instance StrEncoding PushProvider where strEncode = \case - PPApns -> "apns" + PPApnsDev -> "apns_dev" + PPApnsProd -> "apns_prod" + PPApnsTest -> "apns_test" strP = A.takeTill (== ' ') >>= \case - "apns" -> pure PPApns + "apns_dev" -> pure PPApnsDev + "apns_prod" -> pure PPApnsProd + "apns_test" -> pure PPApnsTest _ -> fail "bad PushProvider" instance FromField PushProvider where fromField = fromTextField_ $ eitherToMaybe . strDecode . encodeUtf8 diff --git a/src/Simplex/Messaging/Notifications/Server/Env.hs b/src/Simplex/Messaging/Notifications/Server/Env.hs index 2605f51d1..d0b9770b4 100644 --- a/src/Simplex/Messaging/Notifications/Server/Env.hs +++ b/src/Simplex/Messaging/Notifications/Server/Env.hs @@ -8,7 +8,6 @@ module Simplex.Messaging.Notifications.Server.Env where import Control.Concurrent.Async (Async) -import Control.Monad (void) import Control.Monad.IO.Unlift import Crypto.Random import Data.ByteString.Char8 (ByteString) @@ -71,8 +70,6 @@ newNtfServerEnv config@NtfServerConfig {subQSize, pushQSize, smpAgentCfg, apnsCo store <- atomically newNtfStore subscriber <- atomically $ newNtfSubscriber subQSize smpAgentCfg pushServer <- atomically $ newNtfPushServer pushQSize apnsConfig - -- TODO not creating APNS client on start to pass CI test, has to be replaced with mock APNS server - void . liftIO $ newPushClient pushServer PPApns tlsServerParams <- liftIO $ loadTLSServerParams caCertificateFile certificateFile privateKeyFile Fingerprint fp <- liftIO $ loadFingerprint caCertificateFile pure NtfEnv {config, subscriber, pushServer, store, idsDrg, tlsServerParams, serverIdentity = C.KeyHash fp} @@ -120,11 +117,10 @@ newNtfPushServer qSize apnsConfig = do pure NtfPushServer {pushQ, pushClients, intervalNotifiers, apnsConfig} newPushClient :: NtfPushServer -> PushProvider -> IO PushProviderClient -newPushClient NtfPushServer {apnsConfig, pushClients} = \case - PPApns -> do - c <- apnsPushProviderClient <$> createAPNSPushClient apnsConfig - atomically $ TM.insert PPApns c pushClients - pure c +newPushClient NtfPushServer {apnsConfig, pushClients} pp = do + c <- apnsPushProviderClient <$> createAPNSPushClient (apnsProviderHost pp) apnsConfig + atomically $ TM.insert pp c pushClients + pure c getPushClient :: NtfPushServer -> PushProvider -> IO PushProviderClient getPushClient s@NtfPushServer {pushClients} pp = diff --git a/src/Simplex/Messaging/Notifications/Server/Push/APNS.hs b/src/Simplex/Messaging/Notifications/Server/Push/APNS.hs index 44118a28f..ca029f8da 100644 --- a/src/Simplex/Messaging/Notifications/Server/Push/APNS.hs +++ b/src/Simplex/Messaging/Notifications/Server/Push/APNS.hs @@ -190,12 +190,17 @@ data APNSPushClientConfig = APNSPushClientConfig paddedNtfLength :: Int, appName :: ByteString, appTeamId :: Text, - apnsHost :: HostName, apnsPort :: ServiceName, http2cfg :: HTTP2ClientConfig } deriving (Show) +apnsProviderHost :: PushProvider -> HostName +apnsProviderHost = \case + PPApnsTest -> "localhost" + PPApnsDev -> "api.sandbox.push.apple.com" + PPApnsProd -> "api.push.apple.com" + defaultAPNSPushClientConfig :: APNSPushClientConfig defaultAPNSPushClientConfig = APNSPushClientConfig @@ -206,7 +211,6 @@ defaultAPNSPushClientConfig = paddedNtfLength = 512, appName = "chat.simplex.app", appTeamId = "5NN7GUYB6T", - apnsHost = "api.sandbox.push.apple.com", apnsPort = "443", http2cfg = defaultHTTP2ClientConfig } @@ -217,19 +221,20 @@ data APNSPushClient = APNSPushClient jwtHeader :: JWTHeader, jwtToken :: TVar (JWTToken, SignedJWTToken), nonceDrg :: TVar ChaChaDRG, + apnsHost :: HostName, apnsCfg :: APNSPushClientConfig } -createAPNSPushClient :: APNSPushClientConfig -> IO APNSPushClient -createAPNSPushClient apnsCfg@APNSPushClientConfig {authKeyFileEnv, authKeyAlg, authKeyIdEnv, appTeamId} = do +createAPNSPushClient :: HostName -> APNSPushClientConfig -> IO APNSPushClient +createAPNSPushClient apnsHost apnsCfg@APNSPushClientConfig {authKeyFileEnv, authKeyAlg, authKeyIdEnv, appTeamId} = do https2Client <- newTVarIO Nothing - void $ connectHTTPS2 apnsCfg https2Client + void $ connectHTTPS2 apnsHost apnsCfg https2Client privateKey <- readECPrivateKey =<< getEnv authKeyFileEnv authKeyId <- T.pack <$> getEnv authKeyIdEnv let jwtHeader = JWTHeader {alg = authKeyAlg, kid = authKeyId} jwtToken <- newTVarIO =<< mkApnsJWTToken appTeamId jwtHeader privateKey nonceDrg <- drgNew >>= newTVarIO - pure APNSPushClient {https2Client, privateKey, jwtHeader, jwtToken, nonceDrg, apnsCfg} + pure APNSPushClient {https2Client, privateKey, jwtHeader, jwtToken, nonceDrg, apnsHost, apnsCfg} getApnsJWTToken :: APNSPushClient -> IO SignedJWTToken getApnsJWTToken APNSPushClient {apnsCfg = APNSPushClientConfig {appTeamId, tokenTTL}, privateKey, jwtHeader, jwtToken} = do @@ -250,8 +255,8 @@ mkApnsJWTToken appTeamId jwtHeader privateKey = do signedJWT <- signedJWTToken privateKey jwt pure (jwt, signedJWT) -connectHTTPS2 :: APNSPushClientConfig -> TVar (Maybe HTTP2Client) -> IO (Either HTTP2ClientError HTTP2Client) -connectHTTPS2 APNSPushClientConfig {apnsHost, apnsPort, http2cfg} https2Client = do +connectHTTPS2 :: HostName -> APNSPushClientConfig -> TVar (Maybe HTTP2Client) -> IO (Either HTTP2ClientError HTTP2Client) +connectHTTPS2 apnsHost APNSPushClientConfig {apnsPort, http2cfg} https2Client = do r <- getHTTP2Client apnsHost apnsPort http2cfg disconnected case r of Right client -> atomically . writeTVar https2Client $ Just client @@ -261,8 +266,8 @@ connectHTTPS2 APNSPushClientConfig {apnsHost, apnsPort, http2cfg} https2Client = disconnected = atomically $ writeTVar https2Client Nothing getApnsHTTP2Client :: APNSPushClient -> IO (Either HTTP2ClientError HTTP2Client) -getApnsHTTP2Client APNSPushClient {https2Client, apnsCfg} = - readTVarIO https2Client >>= maybe (connectHTTPS2 apnsCfg https2Client) (pure . Right) +getApnsHTTP2Client APNSPushClient {https2Client, apnsHost, apnsCfg} = + readTVarIO https2Client >>= maybe (connectHTTPS2 apnsHost apnsCfg https2Client) (pure . Right) disconnectApnsHTTP2Client :: APNSPushClient -> IO () disconnectApnsHTTP2Client APNSPushClient {https2Client} = @@ -324,7 +329,7 @@ data APNSErrorResponse = APNSErrorResponse {reason :: Text} deriving (Generic, FromJSON) apnsPushProviderClient :: APNSPushClient -> PushProviderClient -apnsPushProviderClient c@APNSPushClient {nonceDrg, apnsCfg} tkn@NtfTknData {token = DeviceToken PPApns tknStr} pn = do +apnsPushProviderClient c@APNSPushClient {nonceDrg, apnsCfg} tkn@NtfTknData {token = DeviceToken _ tknStr} pn = do http2 <- liftHTTPS2 $ getApnsHTTP2Client c nonce <- atomically $ C.pseudoRandomCbNonce nonceDrg apnsNtf <- liftEither $ first PPCryptoError $ apnsNotification tkn nonce (paddedNtfLength apnsCfg) pn diff --git a/tests/AgentTests/NotificationTests.hs b/tests/AgentTests/NotificationTests.hs index 81b78ace1..8bfa033a4 100644 --- a/tests/AgentTests/NotificationTests.hs +++ b/tests/AgentTests/NotificationTests.hs @@ -74,7 +74,7 @@ testNotificationToken :: APNSMockServer -> IO () testNotificationToken APNSMockServer {apnsQ} = do a <- getSMPAgentClient agentCfg initAgentServers Right () <- runExceptT $ do - let tkn = DeviceToken PPApns "abcd" + let tkn = DeviceToken PPApnsTest "abcd" NTRegistered <- registerNtfToken a tkn NMPeriodic APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData}, sendApnsResponse} <- atomically $ readTBQueue apnsQ @@ -103,7 +103,7 @@ testNtfTokenRepeatRegistration APNSMockServer {apnsQ} = do -- withGlobalLogging logCfg $ do a <- getSMPAgentClient agentCfg initAgentServers Right () <- runExceptT $ do - let tkn = DeviceToken PPApns "abcd" + let tkn = DeviceToken PPApnsTest "abcd" NTRegistered <- registerNtfToken a tkn NMPeriodic APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData}, sendApnsResponse} <- atomically $ readTBQueue apnsQ @@ -129,7 +129,7 @@ testNtfTokenSecondRegistration APNSMockServer {apnsQ} = do a <- getSMPAgentClient agentCfg initAgentServers a' <- getSMPAgentClient agentCfg {dbFile = testDB2} initAgentServers Right () <- runExceptT $ do - let tkn = DeviceToken PPApns "abcd" + let tkn = DeviceToken PPApnsTest "abcd" NTRegistered <- registerNtfToken a tkn NMPeriodic APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData}, sendApnsResponse} <- atomically $ readTBQueue apnsQ @@ -162,7 +162,7 @@ testNtfTokenSecondRegistration APNSMockServer {apnsQ} = do testNtfTokenServerRestart :: ATransport -> APNSMockServer -> IO () testNtfTokenServerRestart t APNSMockServer {apnsQ} = do a <- getSMPAgentClient agentCfg initAgentServers - let tkn = DeviceToken PPApns "abcd" + let tkn = DeviceToken PPApnsTest "abcd" Right ntfData <- withNtfServer t . runExceptT $ do NTRegistered <- registerNtfToken a tkn NMPeriodic APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData}, sendApnsResponse} <- @@ -203,7 +203,7 @@ testNotificationSubscriptionExistingConnection APNSMockServer {apnsQ} = do get alice ##> ("", bobId, CON) get bob ##> ("", aliceId, CON) -- register notification token - let tkn = DeviceToken PPApns "abcd" + let tkn = DeviceToken PPApnsTest "abcd" NTRegistered <- registerNtfToken alice tkn NMInstant APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData}, sendApnsResponse} <- atomically $ readTBQueue apnsQ @@ -253,7 +253,7 @@ testNotificationSubscriptionNewConnection APNSMockServer {apnsQ} = do bob <- getSMPAgentClient agentCfg {dbFile = testDB2} initAgentServers Right () <- runExceptT $ do -- alice registers notification token - let aliceTkn = DeviceToken PPApns "abcd" + let aliceTkn = DeviceToken PPApnsTest "abcd" NTRegistered <- registerNtfToken alice aliceTkn NMInstant APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData}, sendApnsResponse} <- atomically $ readTBQueue apnsQ @@ -263,7 +263,7 @@ testNotificationSubscriptionNewConnection APNSMockServer {apnsQ} = do verifyNtfToken alice aliceTkn verification nonce NTActive <- checkNtfToken alice aliceTkn -- bob registers notification token - let bobTkn = DeviceToken PPApns "bcde" + let bobTkn = DeviceToken PPApnsTest "bcde" NTRegistered <- registerNtfToken bob bobTkn NMInstant APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData'}, sendApnsResponse = sendApnsResponse'} <- atomically $ readTBQueue apnsQ @@ -322,7 +322,7 @@ testChangeNotificationsMode APNSMockServer {apnsQ} = do get alice ##> ("", bobId, CON) get bob ##> ("", aliceId, CON) -- register notification token, set mode to NMPeriodic - let tkn = DeviceToken PPApns "abcd" + let tkn = DeviceToken PPApnsTest "abcd" NTRegistered <- registerNtfToken alice tkn NMPeriodic APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData}, sendApnsResponse} <- atomically $ readTBQueue apnsQ diff --git a/tests/NtfClient.hs b/tests/NtfClient.hs index 4662e921f..caa7e16fc 100644 --- a/tests/NtfClient.hs +++ b/tests/NtfClient.hs @@ -81,8 +81,7 @@ ntfServerCfg = smpAgentCfg = defaultSMPClientAgentConfig, apnsConfig = defaultAPNSPushClientConfig - { apnsHost = "localhost", - apnsPort = apnsTestPort, + { apnsPort = apnsTestPort, http2cfg = defaultHTTP2ClientConfig {caStoreFile = "tests/fixtures/ca.crt"} }, inactiveClientExpiration = Just defaultInactiveClientExpiration, diff --git a/tests/NtfServerTests.hs b/tests/NtfServerTests.hs index 657eab235..32175b19f 100644 --- a/tests/NtfServerTests.hs +++ b/tests/NtfServerTests.hs @@ -51,9 +51,9 @@ ntfSyntaxTests (ATransport t) = do it "unknown command" $ ("", "abcd", "1234", ('H', 'E', 'L', 'L', 'O')) >#> ("", "abcd", "1234", ERR $ CMD UNKNOWN) describe "NEW" $ do it "no parameters" $ (sampleSig, "bcda", "", TNEW_) >#> ("", "bcda", "", ERR $ CMD SYNTAX) - it "many parameters" $ (sampleSig, "cdab", "", (TNEW_, (' ', '\x01', 'A'), ('T', 'A', "abcd" :: ByteString), samplePubKey, sampleDhPubKey)) >#> ("", "cdab", "", ERR $ CMD SYNTAX) - it "no signature" $ ("", "dabc", "", (TNEW_, ' ', ('T', 'A', "abcd" :: ByteString), samplePubKey, sampleDhPubKey)) >#> ("", "dabc", "", ERR $ CMD NO_AUTH) - it "token ID" $ (sampleSig, "abcd", "12345678", (TNEW_, ' ', ('T', 'A', "abcd" :: ByteString), samplePubKey, sampleDhPubKey)) >#> ("", "abcd", "12345678", ERR $ CMD HAS_AUTH) + it "many parameters" $ (sampleSig, "cdab", "", (TNEW_, (' ', '\x01', 'A'), ('T', 'A', 'T', "abcd" :: ByteString), samplePubKey, sampleDhPubKey)) >#> ("", "cdab", "", ERR $ CMD SYNTAX) + it "no signature" $ ("", "dabc", "", (TNEW_, ' ', ('T', 'A', 'T', "abcd" :: ByteString), samplePubKey, sampleDhPubKey)) >#> ("", "dabc", "", ERR $ CMD NO_AUTH) + it "token ID" $ (sampleSig, "abcd", "12345678", (TNEW_, ' ', ('T', 'A', 'T', "abcd" :: ByteString), samplePubKey, sampleDhPubKey)) >#> ("", "abcd", "12345678", ERR $ CMD HAS_AUTH) where (>#>) :: Encoding smp => @@ -90,7 +90,7 @@ testNotificationSubscription (ATransport t) = (nPub, nKey) <- C.generateSignatureKeyPair C.SEd25519 (tknPub, tknKey) <- C.generateSignatureKeyPair C.SEd25519 (dhPub, dhPriv :: C.PrivateKeyX25519) <- C.generateKeyPair' - let tkn = DeviceToken PPApns "abcd" + let tkn = DeviceToken PPApnsTest "abcd" withAPNSMockServer $ \APNSMockServer {apnsQ} -> smpTest2 t $ \rh sh -> ntfTest t $ \nh -> do @@ -139,7 +139,7 @@ testNotificationSubscription (ATransport t) = Resp "6" _ OK <- signSendRecv rh rKey ("6", rId, ACK mId1) pure () -- replace token - let tkn' = DeviceToken PPApns "efgh" + let tkn' = DeviceToken PPApnsTest "efgh" RespNtf "7" tId' NROk <- signSendRecvNtf nh tknKey ("7", tId, TRPL tkn') tId `shouldBe` tId' APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData2}, sendApnsResponse = send2} <- From ba40d75886c14d70fb6af61c22f917069d7b478f Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Mon, 27 Jun 2022 19:33:36 +0100 Subject: [PATCH 091/131] verifyNtfToken: change args order (#434) --- src/Simplex/Messaging/Agent.hs | 6 +++--- tests/AgentTests/NotificationTests.hs | 24 ++++++++++++------------ 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/Simplex/Messaging/Agent.hs b/src/Simplex/Messaging/Agent.hs index 8b33d3176..c10fe37e9 100644 --- a/src/Simplex/Messaging/Agent.hs +++ b/src/Simplex/Messaging/Agent.hs @@ -194,7 +194,7 @@ registerNtfToken :: AgentErrorMonad m => AgentClient -> DeviceToken -> Notificat registerNtfToken c = withAgentEnv c .: registerNtfToken' c -- | Verify device notifications token -verifyNtfToken :: AgentErrorMonad m => AgentClient -> DeviceToken -> ByteString -> C.CbNonce -> m () +verifyNtfToken :: AgentErrorMonad m => AgentClient -> DeviceToken -> C.CbNonce -> ByteString -> m () verifyNtfToken c = withAgentEnv c .:. verifyNtfToken' c checkNtfToken :: AgentErrorMonad m => AgentClient -> DeviceToken -> m NtfTknStatus @@ -710,8 +710,8 @@ registerNtfToken' c suppliedDeviceToken suppliedNtfMode = atomically $ nsUpdateToken ns tkn {deviceToken = suppliedDeviceToken, ntfTknStatus = NTRegistered, ntfMode = suppliedNtfMode} -- TODO decrypt verification code -verifyNtfToken' :: AgentMonad m => AgentClient -> DeviceToken -> ByteString -> C.CbNonce -> m () -verifyNtfToken' c deviceToken code nonce = +verifyNtfToken' :: AgentMonad m => AgentClient -> DeviceToken -> C.CbNonce -> ByteString -> m () +verifyNtfToken' c deviceToken nonce code = withStore' c getSavedNtfToken >>= \case Just tkn@NtfToken {deviceToken = savedDeviceToken, ntfTokenId = Just tknId, ntfDhSecret = Just dhSecret, ntfMode} -> do when (deviceToken /= savedDeviceToken) . throwError $ CMD PROHIBITED diff --git a/tests/AgentTests/NotificationTests.hs b/tests/AgentTests/NotificationTests.hs index 8bfa033a4..5213bc67a 100644 --- a/tests/AgentTests/NotificationTests.hs +++ b/tests/AgentTests/NotificationTests.hs @@ -81,7 +81,7 @@ testNotificationToken APNSMockServer {apnsQ} = do verification <- ntfData .-> "verification" nonce <- C.cbNonce <$> ntfData .-> "nonce" liftIO $ sendApnsResponse APNSRespOk - verifyNtfToken a tkn verification nonce + verifyNtfToken a tkn nonce verification NTActive <- checkNtfToken a tkn deleteNtfToken a tkn -- agent deleted this token @@ -117,7 +117,7 @@ testNtfTokenRepeatRegistration APNSMockServer {apnsQ} = do _ <- C.cbNonce <$> ntfData' .-> "nonce" liftIO $ sendApnsResponse' APNSRespOk -- can still use the first verification code, it is the same after decryption - verifyNtfToken a tkn verification nonce + verifyNtfToken a tkn nonce verification NTActive <- checkNtfToken a tkn pure () pure () @@ -136,7 +136,7 @@ testNtfTokenSecondRegistration APNSMockServer {apnsQ} = do verification <- ntfData .-> "verification" nonce <- C.cbNonce <$> ntfData .-> "nonce" liftIO $ sendApnsResponse APNSRespOk - verifyNtfToken a tkn verification nonce + verifyNtfToken a tkn nonce verification NTRegistered <- registerNtfToken a' tkn NMPeriodic APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData'}, sendApnsResponse = sendApnsResponse'} <- @@ -151,7 +151,7 @@ testNtfTokenSecondRegistration APNSMockServer {apnsQ} = do liftIO $ threadDelay 50000 NTConfirmed <- checkNtfToken a' tkn -- now the second token registration is verified - verifyNtfToken a' tkn verification' nonce' + verifyNtfToken a' tkn nonce' verification' -- the first registration is removed Left (NTF AUTH) <- tryE $ checkNtfToken a tkn -- and the second is active @@ -178,13 +178,13 @@ testNtfTokenServerRestart t APNSMockServer {apnsQ} = do Right () <- withNtfServer t . runExceptT $ do verification <- ntfData .-> "verification" nonce <- C.cbNonce <$> ntfData .-> "nonce" - Left (NTF AUTH) <- tryE $ verifyNtfToken a' tkn verification nonce + Left (NTF AUTH) <- tryE $ verifyNtfToken a' tkn nonce verification APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData'}, sendApnsResponse = sendApnsResponse'} <- atomically $ readTBQueue apnsQ verification' <- ntfData' .-> "verification" nonce' <- C.cbNonce <$> ntfData' .-> "nonce" liftIO $ sendApnsResponse' APNSRespOk - verifyNtfToken a' tkn verification' nonce' + verifyNtfToken a' tkn nonce' verification' NTActive <- checkNtfToken a' tkn pure () pure () @@ -208,9 +208,9 @@ testNotificationSubscriptionExistingConnection APNSMockServer {apnsQ} = do APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData}, sendApnsResponse} <- atomically $ readTBQueue apnsQ verification <- ntfData .-> "verification" - verificationNonce <- C.cbNonce <$> ntfData .-> "nonce" + vNonce <- C.cbNonce <$> ntfData .-> "nonce" liftIO $ sendApnsResponse APNSRespOk - verifyNtfToken alice tkn verification verificationNonce + verifyNtfToken alice tkn vNonce verification NTActive <- checkNtfToken alice tkn -- send message liftIO $ threadDelay 50000 @@ -260,7 +260,7 @@ testNotificationSubscriptionNewConnection APNSMockServer {apnsQ} = do verification <- ntfData .-> "verification" nonce <- C.cbNonce <$> ntfData .-> "nonce" liftIO $ sendApnsResponse APNSRespOk - verifyNtfToken alice aliceTkn verification nonce + verifyNtfToken alice aliceTkn nonce verification NTActive <- checkNtfToken alice aliceTkn -- bob registers notification token let bobTkn = DeviceToken PPApnsTest "bcde" @@ -270,7 +270,7 @@ testNotificationSubscriptionNewConnection APNSMockServer {apnsQ} = do verification' <- ntfData' .-> "verification" nonce' <- C.cbNonce <$> ntfData' .-> "nonce" liftIO $ sendApnsResponse' APNSRespOk - verifyNtfToken bob bobTkn verification' nonce' + verifyNtfToken bob bobTkn nonce' verification' NTActive <- checkNtfToken bob bobTkn -- establish connection liftIO $ threadDelay 50000 @@ -327,9 +327,9 @@ testChangeNotificationsMode APNSMockServer {apnsQ} = do APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData}, sendApnsResponse} <- atomically $ readTBQueue apnsQ verification <- ntfData .-> "verification" - verificationNonce <- C.cbNonce <$> ntfData .-> "nonce" + vNonce <- C.cbNonce <$> ntfData .-> "nonce" liftIO $ sendApnsResponse APNSRespOk - verifyNtfToken alice tkn verification verificationNonce + verifyNtfToken alice tkn vNonce verification NTActive <- checkNtfToken alice tkn -- send message, no notification 1 <- msgId <$> sendMessage bob aliceId (SMP.MsgFlags True) "hello" From b0ac0744e26398eddf55fb7e17ec87fabc9356e3 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Tue, 28 Jun 2022 12:36:02 +0100 Subject: [PATCH 092/131] fix server ACK for v1 SMP clients (#436) * fix server ACK for v1 SMP clients * add import --- src/Simplex/Messaging/Server.hs | 2 +- src/Simplex/Messaging/Server/MsgStore/STM.hs | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Simplex/Messaging/Server.hs b/src/Simplex/Messaging/Server.hs index a32fc93a3..8de05d83f 100644 --- a/src/Simplex/Messaging/Server.hs +++ b/src/Simplex/Messaging/Server.hs @@ -498,7 +498,7 @@ client clnt@Client {subscriptions, ntfSubscriptions, rcvQ, sndQ} Server {subscri getDelivered sub = do s@Sub {delivered} <- readTVar sub tryTakeTMVar delivered $>>= \msgId' -> - if B.null msgId || msgId == msgId' + if msgId == msgId' || B.null msgId then pure $ Just s else putTMVar delivered msgId' $> Nothing updateStats :: m () diff --git a/src/Simplex/Messaging/Server/MsgStore/STM.hs b/src/Simplex/Messaging/Server/MsgStore/STM.hs index a1bcbe157..3a268115b 100644 --- a/src/Simplex/Messaging/Server/MsgStore/STM.hs +++ b/src/Simplex/Messaging/Server/MsgStore/STM.hs @@ -11,6 +11,7 @@ module Simplex.Messaging.Server.MsgStore.STM where import Control.Concurrent.STM.TBQueue (flushTBQueue) import Control.Monad (when) +import qualified Data.ByteString.Char8 as B import Data.Functor (($>)) import Data.Int (Int64) import Data.Time.Clock.System (SystemTime (systemSeconds)) @@ -60,7 +61,7 @@ instance MonadMsgQueue MsgQueue STM where tryDelMsg (MsgQueue q) msgId' = tryPeekTBQueue q >>= \case Just Message {msgId} - | msgId == msgId' -> tryReadTBQueue q $> True + | msgId == msgId' || B.null msgId' -> tryReadTBQueue q $> True | otherwise -> pure False _ -> pure False @@ -69,7 +70,7 @@ instance MonadMsgQueue MsgQueue STM where tryDelPeekMsg (MsgQueue q) msgId' = tryPeekTBQueue q >>= \case msg_@(Just Message {msgId}) - | msgId == msgId' -> (True,) <$> (tryReadTBQueue q >> tryPeekTBQueue q) + | msgId == msgId' || B.null msgId' -> (True,) <$> (tryReadTBQueue q >> tryPeekTBQueue q) | otherwise -> pure (False, msg_) _ -> pure (False, Nothing) From 85d507d5d3e431ae50ad2c5211b08aca166c9345 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Tue, 28 Jun 2022 17:10:50 +0100 Subject: [PATCH 093/131] ntf-server store log (#435) * ntf-server store log * ntf serevr: restore log when server is started, save compacted store log * log ntf server store changes * test, store log works * update ntf-server exe --- apps/ntf-server/Main.hs | 5 +- simplexmq.cabal | 1 + src/Simplex/Messaging/Agent/Client.hs | 2 +- src/Simplex/Messaging/Crypto.hs | 12 + .../Messaging/Notifications/Protocol.hs | 8 + src/Simplex/Messaging/Notifications/Server.hs | 114 ++++----- .../Messaging/Notifications/Server/Env.hs | 10 +- .../Notifications/Server/Push/APNS.hs | 3 +- .../Messaging/Notifications/Server/Store.hs | 26 +- .../Notifications/Server/StoreLog.hs | 227 ++++++++++++++++++ src/Simplex/Messaging/Server/StoreLog.hs | 8 +- src/Simplex/Messaging/Transport/Server.hs | 2 +- tests/AgentTests/FunctionalAPITests.hs | 1 + tests/AgentTests/NotificationTests.hs | 79 +++--- tests/NtfClient.hs | 15 +- 15 files changed, 394 insertions(+), 119 deletions(-) create mode 100644 src/Simplex/Messaging/Notifications/Server/StoreLog.hs diff --git a/apps/ntf-server/Main.hs b/apps/ntf-server/Main.hs index 6d40a8bdc..d17929a65 100644 --- a/apps/ntf-server/Main.hs +++ b/apps/ntf-server/Main.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE DuplicateRecordFields #-} {-# LANGUAGE NamedFieldPuns #-} module Main where @@ -56,7 +57,7 @@ ntfServerCLIConfig = <> defaultServerPort <> "\n\ \websockets: off\n", - mkServerConfig = \_storeLogFile transports _ -> + mkServerConfig = \storeLogFile transports _ -> NtfServerConfig { transports, subIdBytes = 24, @@ -67,6 +68,8 @@ ntfServerCLIConfig = smpAgentCfg = defaultSMPClientAgentConfig, apnsConfig = defaultAPNSPushClientConfig, inactiveClientExpiration = Nothing, + storeLogFile, + resubscribeDelay = 100000, -- 100ms caCertificateFile = caCrtFile, privateKeyFile = serverKeyFile, certificateFile = serverCrtFile diff --git a/simplexmq.cabal b/simplexmq.cabal index 318dd8dc9..5ba13f091 100644 --- a/simplexmq.cabal +++ b/simplexmq.cabal @@ -62,6 +62,7 @@ library Simplex.Messaging.Notifications.Server.Env Simplex.Messaging.Notifications.Server.Push.APNS Simplex.Messaging.Notifications.Server.Store + Simplex.Messaging.Notifications.Server.StoreLog Simplex.Messaging.Notifications.Transport Simplex.Messaging.Parsers Simplex.Messaging.Protocol diff --git a/src/Simplex/Messaging/Agent/Client.hs b/src/Simplex/Messaging/Agent/Client.hs index c0f661580..e23cf3f09 100644 --- a/src/Simplex/Messaging/Agent/Client.hs +++ b/src/Simplex/Messaging/Agent/Client.hs @@ -180,7 +180,7 @@ newAgentClient InitialAgentServers {smp, ntf} agentEnv = do getMsgLocks <- TM.empty reconnections <- newTVar [] asyncClients <- newTVar [] - clientId <- stateTVar (clientCounter agentEnv) $ \i -> (i + 1, i + 1) + clientId <- stateTVar (clientCounter agentEnv) $ \i -> let i' = i + 1 in (i', i') lock <- newTMVar () return AgentClient {active, rcvQ, subQ, msgQ, smpServers, smpClients, ntfServers, ntfClients, subscrSrvrs, pendingSubscrSrvrs, subscrConns, connMsgsQueued, smpQueueMsgQueues, smpQueueMsgDeliveries, rcvNetworkOp, msgDeliveryOp, sndNetworkOp, databaseOp, agentState, getMsgLocks, reconnections, asyncClients, clientId, agentEnv, lock} diff --git a/src/Simplex/Messaging/Crypto.hs b/src/Simplex/Messaging/Crypto.hs index 0f0494606..81dc8e6d5 100644 --- a/src/Simplex/Messaging/Crypto.hs +++ b/src/Simplex/Messaging/Crypto.hs @@ -250,6 +250,12 @@ deriving instance Eq (PrivateKey a) deriving instance Show (PrivateKey a) +instance StrEncoding (PrivateKey X25519) where + strEncode = strEncode . encodePrivKey + {-# INLINE strEncode #-} + strDecode = decodePrivKey + {-# INLINE strDecode #-} + data APrivateKey = forall a. AlgorithmI a => @@ -296,6 +302,12 @@ instance Encoding APrivateSignKey where smpDecode = decodePrivKey {-# INLINE smpDecode #-} +instance StrEncoding APrivateSignKey where + strEncode = strEncode . encodePrivKey + {-# INLINE strEncode #-} + strDecode = decodePrivKey + {-# INLINE strDecode #-} + data APublicVerifyKey = forall a. (AlgorithmI a, SignatureAlgorithm a) => diff --git a/src/Simplex/Messaging/Notifications/Protocol.hs b/src/Simplex/Messaging/Notifications/Protocol.hs index b9deb45d2..789a11136 100644 --- a/src/Simplex/Messaging/Notifications/Protocol.hs +++ b/src/Simplex/Messaging/Notifications/Protocol.hs @@ -423,6 +423,10 @@ instance Encoding NtfSubStatus where "SMP_AUTH" -> pure NSSMPAuth _ -> fail "bad NtfSubStatus" +instance StrEncoding NtfSubStatus where + strEncode = smpEncode + strP = smpP + data NtfTknStatus = -- | Token created in DB NTNew @@ -456,6 +460,10 @@ instance Encoding NtfTknStatus where "EXPIRED" -> pure NTExpired _ -> fail "bad NtfTknStatus" +instance StrEncoding NtfTknStatus where + strEncode = smpEncode + strP = smpP + instance FromField NtfTknStatus where fromField = fromTextField_ $ either (const Nothing) Just . smpDecode . encodeUtf8 instance ToField NtfTknStatus where toField = toField . decodeLatin1 . smpEncode diff --git a/src/Simplex/Messaging/Notifications/Server.hs b/src/Simplex/Messaging/Notifications/Server.hs index fbae87dc5..7a54d937b 100644 --- a/src/Simplex/Messaging/Notifications/Server.hs +++ b/src/Simplex/Messaging/Notifications/Server.hs @@ -10,6 +10,7 @@ module Simplex.Messaging.Notifications.Server where +import Control.Concurrent.STM (stateTVar) import Control.Logger.Simple import Control.Monad.Except import Control.Monad.IO.Unlift (MonadUnliftIO) @@ -17,6 +18,7 @@ import Control.Monad.Reader import Crypto.Random (MonadRandom) import Data.ByteString.Char8 (ByteString) import Data.Functor (($>)) +import Data.Map.Strict (Map) import qualified Data.Text as T import Data.Time.Clock.System (getSystemTime) import Network.Socket (ServiceName) @@ -26,6 +28,7 @@ import Simplex.Messaging.Notifications.Protocol import Simplex.Messaging.Notifications.Server.Env import Simplex.Messaging.Notifications.Server.Push.APNS (PNMessageData (..), PushNotification (..), PushProviderClient, PushProviderError (..)) import Simplex.Messaging.Notifications.Server.Store +import Simplex.Messaging.Notifications.Server.StoreLog import Simplex.Messaging.Notifications.Transport import Simplex.Messaging.Protocol (ErrorType (..), SMPServer, SignedTransmission, Transmission, encodeTransmission, tGet, tPut) import qualified Simplex.Messaging.Protocol as SMP @@ -34,8 +37,8 @@ import qualified Simplex.Messaging.TMap as TM import Simplex.Messaging.Transport (ATransport (..), THandle (..), TProxy, Transport (..)) import Simplex.Messaging.Transport.Server (runTransportServer) import Simplex.Messaging.Util -import UnliftIO (async, uninterruptibleCancel) -import UnliftIO.Concurrent (forkFinally, threadDelay) +import UnliftIO (IOMode (..), async, uninterruptibleCancel) +import UnliftIO.Concurrent (forkFinally, forkIO, threadDelay) import UnliftIO.Exception import UnliftIO.STM @@ -51,7 +54,10 @@ ntfServer :: forall m. (MonadUnliftIO m, MonadReader NtfEnv m) => NtfServerConfi ntfServer NtfServerConfig {transports} started = do s <- asks subscriber ps <- asks pushServer + subs <- readTVarIO =<< asks (subscriptions . store) + void . forkIO $ resubscribe s subs raceAny_ (ntfSubscriber s : ntfPush ps : map runServer transports) + `finally` withNtfLog closeStoreLog where runServer :: (ServiceName, ATransport) -> m () runServer (tcpPort, ATransport t) = do @@ -65,6 +71,14 @@ ntfServer NtfServerConfig {transports} started = do Right th -> runNtfClientTransport th Left _ -> pure () +resubscribe :: (MonadUnliftIO m, MonadReader NtfEnv m) => NtfSubscriber -> Map NtfSubscriptionId NtfSubData -> m () +resubscribe NtfSubscriber {newSubQ} subs = do + d <- asks $ resubscribeDelay . config + forM_ subs $ \sub -> do + atomically $ writeTBQueue newSubQ $ NtfSub sub + threadDelay d + liftIO $ logInfo "SMP connections resubscribed" + ntfSubscriber :: forall m. (MonadUnliftIO m, MonadReader NtfEnv m) => NtfSubscriber -> m () ntfSubscriber NtfSubscriber {smpSubscribers, newSubQ, smpAgent = ca@SMPClientAgent {msgQ, agentQ}} = do raceAny_ [subscribe, receiveSMP, receiveAgent] @@ -133,22 +147,25 @@ ntfSubscriber NtfSubscriber {smpSubscribers, newSubQ, smpAgent = ca@SMPClientAge -- update subscription status pure () -ntfPush :: forall m. MonadUnliftIO m => NtfPushServer -> m () -ntfPush s@NtfPushServer {pushQ} = liftIO . forever . runExceptT $ do - (tkn@NtfTknData {token = DeviceToken pp _, tknStatus}, ntf) <- atomically (readTBQueue pushQ) - logDebug $ "sending push notification to " <> T.pack (show pp) +ntfPush :: forall m. (MonadUnliftIO m, MonadReader NtfEnv m) => NtfPushServer -> m () +ntfPush s@NtfPushServer {pushQ} = forever $ do + (tkn@NtfTknData {ntfTknId, token = DeviceToken pp _, tknStatus}, ntf) <- atomically (readTBQueue pushQ) + liftIO $ logDebug $ "sending push notification to " <> T.pack (show pp) status <- readTVarIO tknStatus case (status, ntf) of (_, PNVerification _) -> do -- TODO check token status - deliverNotification pp tkn ntf - atomically $ modifyTVar tknStatus $ \status' -> if status' == NTActive then NTActive else NTConfirmed + liftIO (runExceptT $ deliverNotification pp tkn ntf) >>= \case + Right _ -> do + status_ <- atomically $ stateTVar tknStatus $ \status' -> if status' == NTActive then (Nothing, NTActive) else (Just NTConfirmed, NTConfirmed) + forM_ status_ $ \status' -> withNtfLog $ \sl -> logTokenStatus sl ntfTknId status' + _ -> pure () (NTActive, PNCheckMessages) -> do - deliverNotification pp tkn ntf + liftIO . void . runExceptT $ deliverNotification pp tkn ntf (NTActive, PNMessage {}) -> do - deliverNotification pp tkn ntf + liftIO . void . runExceptT $ deliverNotification pp tkn ntf _ -> do - logError "bad notification token status" + liftIO $ logError "bad notification token status" where deliverNotification :: PushProvider -> PushProviderClient deliverNotification pp tkn ntf = do @@ -277,12 +294,12 @@ client NtfServerClient {rcvQ, sndQ} NtfSubscriber {newSubQ, smpAgent = ca} NtfPu let dhSecret = C.dh' dhPubKey srvDhPrivKey tknId <- getId regCode <- getRegCode - atomically $ do - tkn <- mkNtfTknData tknId newTkn ks dhSecret regCode - addNtfToken st tknId tkn - writeTBQueue pushQ (tkn, PNVerification regCode) + tkn <- atomically $ mkNtfTknData tknId newTkn ks dhSecret regCode + atomically $ addNtfToken st tknId tkn + atomically $ writeTBQueue pushQ (tkn, PNVerification regCode) + withNtfLog (`logCreateToken` tkn) pure (corrId, "", NRTknId tknId srvDhPubKey) - NtfReqCmd SToken (NtfTkn tkn@NtfTknData {ntfTknId, tknStatus, tknRegCode, tknDhSecret, tknDhKeys = (srvDhPubKey, srvDhPrivKey)}) (corrId, tknId, cmd) -> do + NtfReqCmd SToken (NtfTkn tkn@NtfTknData {ntfTknId, tknStatus, tknRegCode, tknDhSecret, tknDhKeys = (srvDhPubKey, srvDhPrivKey), tknCronInterval}) (corrId, tknId, cmd) -> do status <- readTVarIO tknStatus (corrId,tknId,) <$> case cmd of TNEW (NewNtfTkn _ _ dhPubKey) -> do @@ -301,6 +318,7 @@ client NtfServerClient {rcvQ, sndQ} NtfSubscriber {newSubQ, smpAgent = ca} NtfPu atomically $ writeTVar tknStatus NTActive tIds <- atomically $ removeInactiveTokenRegistrations st tkn forM_ tIds cancelInvervalNotifications + withNtfLog $ \s -> logTokenStatus s tknId NTActive pure NROk | otherwise -> do logDebug "TVFY - incorrect code or token status" @@ -315,6 +333,7 @@ client NtfServerClient {rcvQ, sndQ} NtfSubscriber {newSubQ, smpAgent = ca} NtfPu writeTVar tknStatus NTRegistered addNtfToken st tknId tkn {token = token', tknRegCode = regCode} writeTBQueue pushQ (tkn, PNVerification regCode) + withNtfLog $ \s -> logUpdateToken s tknId token' regCode pure NROk TDEL -> do logDebug "TDEL" @@ -323,21 +342,26 @@ client NtfServerClient {rcvQ, sndQ} NtfSubscriber {newSubQ, smpAgent = ca} NtfPu forM_ qs $ \SMPQueueNtf {smpServer, notifierId} -> atomically $ removeSubscription ca smpServer (SPNotifier, notifierId) cancelInvervalNotifications tknId + withNtfLog (`logDeleteToken` tknId) pure NROk TCRN 0 -> do logDebug "TCRN 0" + atomically $ writeTVar tknCronInterval 0 cancelInvervalNotifications tknId + withNtfLog $ \s -> logTokenCron s tknId 0 pure NROk TCRN int | int < 20 -> pure $ NRErr QUOTA | otherwise -> do logDebug "TCRN" + atomically $ writeTVar tknCronInterval int atomically (TM.lookup tknId intervalNotifiers) >>= \case Nothing -> runIntervalNotifier int Just IntervalNotifier {interval, action} -> unless (interval == int) $ do uninterruptibleCancel action runIntervalNotifier int + withNtfLog $ \s -> logTokenCron s tknId int pure NROk where runIntervalNotifier interval = do @@ -352,13 +376,13 @@ client NtfServerClient {rcvQ, sndQ} NtfSubscriber {newSubQ, smpAgent = ca} NtfPu logDebug "SNEW - new subscription" st <- asks store subId <- getId - atomically $ do - sub <- mkNtfSubData newSub - (corrId,"",) - <$> ( addNtfSubscription st subId sub >>= \case - Just _ -> writeTBQueue newSubQ (NtfSub sub) $> NRSubId subId - _ -> pure $ NRErr AUTH - ) + sub <- atomically $ mkNtfSubData subId newSub + resp <- + atomically (addNtfSubscription st subId sub) >>= \case + Just _ -> atomically (writeTBQueue newSubQ $ NtfSub sub) $> NRSubId subId + _ -> pure $ NRErr AUTH + withNtfLog (`logCreateSubscription` sub) + pure (corrId, "", resp) NtfReqCmd SSubscription (NtfSub NtfSubData {smpQueue = SMPQueueNtf {smpServer, notifierId}, notifierKey = registeredNKey, subStatus}) (corrId, subId, cmd) -> do status <- readTVarIO subStatus (corrId,subId,) <$> case cmd of @@ -375,6 +399,7 @@ client NtfServerClient {rcvQ, sndQ} NtfSubscriber {newSubQ, smpAgent = ca} NtfPu st <- asks store atomically $ deleteNtfSubscription st subId atomically $ removeSubscription ca smpServer (SPNotifier, notifierId) + withNtfLog (`logDeleteSubscription` subId) pure NROk PING -> pure NRPong getId :: m NtfEntityId @@ -390,44 +415,5 @@ client NtfServerClient {rcvQ, sndQ} NtfSubscriber {newSubQ, smpAgent = ca} NtfPu atomically (TM.lookupDelete tknId intervalNotifiers) >>= mapM_ (uninterruptibleCancel . action) --- NReqCreate corrId tokenId smpQueue -> pure (corrId, "", NROk) --- do --- st <- asks store --- (pubDhKey, privDhKey) <- liftIO C.generateKeyPair' --- let dhSecret = C.dh' dhPubKey privDhKey --- sub <- atomically $ mkNtfSubsciption smpQueue token verifyKey dhSecret --- addSubRetry 3 st sub >>= \case --- Nothing -> pure (corrId, "", NRErr INTERNAL) --- Just sId -> do --- atomically $ writeTBQueue subQ sub --- pure (corrId, sId, NRSubId pubDhKey) --- where --- addSubRetry :: Int -> NtfSubscriptionsStore -> NtfSubsciption -> m (Maybe NtfSubsciptionId) --- addSubRetry 0 _ _ = pure Nothing --- addSubRetry n st sub = do --- sId <- getId --- -- create QueueRec record with these ids and keys --- atomically (addNtfSub st sId sub) >>= \case --- Nothing -> addSubRetry (n - 1) st sub --- _ -> pure $ Just sId --- getId :: m NtfSubsciptionId --- getId = do --- n <- asks $ subIdBytes . config --- gVar <- asks idsDrg --- atomically (randomBytes n gVar) --- NReqCommand sub@NtfSubsciption {tokenId, subStatus} (corrId, subId, cmd) -> --- (corrId,subId,) <$> case cmd of --- NCSubCreate tokenId smpQueue -> pure NROk --- do --- st <- asks store --- (pubDhKey, privDhKey) <- liftIO C.generateKeyPair' --- let dhSecret = C.dh' (dhPubKey newSub) privDhKey --- atomically (updateNtfSub st sub newSub dhSecret) >>= \case --- Nothing -> pure $ NRErr INTERNAL --- _ -> atomically $ do --- whenM ((== NSEnd) <$> readTVar status) $ writeTBQueue subQ sub --- pure $ NRSubId pubDhKey --- NCSubCheck -> NRStat <$> readTVarIO subStatus --- NCSubDelete -> do --- st <- asks store --- atomically (deleteNtfSub st subId) $> NROk +withNtfLog :: (MonadUnliftIO m, MonadReader NtfEnv m) => (StoreLog 'WriteMode -> IO a) -> m () +withNtfLog action = liftIO . mapM_ action =<< asks storeLog diff --git a/src/Simplex/Messaging/Notifications/Server/Env.hs b/src/Simplex/Messaging/Notifications/Server/Env.hs index d0b9770b4..f1f59ad06 100644 --- a/src/Simplex/Messaging/Notifications/Server/Env.hs +++ b/src/Simplex/Messaging/Notifications/Server/Env.hs @@ -22,12 +22,14 @@ import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Notifications.Protocol import Simplex.Messaging.Notifications.Server.Push.APNS import Simplex.Messaging.Notifications.Server.Store +import Simplex.Messaging.Notifications.Server.StoreLog import Simplex.Messaging.Protocol (CorrId, SMPServer, Transmission) import Simplex.Messaging.Server.Expiration import Simplex.Messaging.TMap (TMap) import qualified Simplex.Messaging.TMap as TM import Simplex.Messaging.Transport (ATransport) import Simplex.Messaging.Transport.Server (loadFingerprint, loadTLSServerParams) +import System.IO (IOMode (..)) import UnliftIO.STM data NtfServerConfig = NtfServerConfig @@ -40,6 +42,8 @@ data NtfServerConfig = NtfServerConfig smpAgentCfg :: SMPClientAgentConfig, apnsConfig :: APNSPushClientConfig, inactiveClientExpiration :: Maybe ExpirationConfig, + storeLogFile :: Maybe FilePath, + resubscribeDelay :: Int, -- microseconds -- CA certificate private key is not needed for initialization caCertificateFile :: FilePath, privateKeyFile :: FilePath, @@ -58,6 +62,7 @@ data NtfEnv = NtfEnv subscriber :: NtfSubscriber, pushServer :: NtfPushServer, store :: NtfStore, + storeLog :: Maybe (StoreLog 'WriteMode), idsDrg :: TVar ChaChaDRG, serverIdentity :: C.KeyHash, tlsServerParams :: T.ServerParams, @@ -65,14 +70,15 @@ data NtfEnv = NtfEnv } newNtfServerEnv :: (MonadUnliftIO m, MonadRandom m) => NtfServerConfig -> m NtfEnv -newNtfServerEnv config@NtfServerConfig {subQSize, pushQSize, smpAgentCfg, apnsConfig, caCertificateFile, certificateFile, privateKeyFile} = do +newNtfServerEnv config@NtfServerConfig {subQSize, pushQSize, smpAgentCfg, apnsConfig, storeLogFile, caCertificateFile, certificateFile, privateKeyFile} = do idsDrg <- newTVarIO =<< drgNew store <- atomically newNtfStore + storeLog <- liftIO $ mapM (`readWriteNtfStore` store) storeLogFile subscriber <- atomically $ newNtfSubscriber subQSize smpAgentCfg pushServer <- atomically $ newNtfPushServer pushQSize apnsConfig tlsServerParams <- liftIO $ loadTLSServerParams caCertificateFile certificateFile privateKeyFile Fingerprint fp <- liftIO $ loadFingerprint caCertificateFile - pure NtfEnv {config, subscriber, pushServer, store, idsDrg, tlsServerParams, serverIdentity = C.KeyHash fp} + pure NtfEnv {config, subscriber, pushServer, store, storeLog, idsDrg, tlsServerParams, serverIdentity = C.KeyHash fp} data NtfSubscriber = NtfSubscriber { smpSubscribers :: TMap SMPServer SMPSubscriber, diff --git a/src/Simplex/Messaging/Notifications/Server/Push/APNS.hs b/src/Simplex/Messaging/Notifications/Server/Push/APNS.hs index ca029f8da..8e6d1ed0c 100644 --- a/src/Simplex/Messaging/Notifications/Server/Push/APNS.hs +++ b/src/Simplex/Messaging/Notifications/Server/Push/APNS.hs @@ -10,6 +10,7 @@ module Simplex.Messaging.Notifications.Server.Push.APNS where +import Control.Exception (Exception) import Control.Logger.Simple import Control.Monad.Except import Crypto.Hash.Algorithms (SHA256 (..)) @@ -320,7 +321,7 @@ data PushProviderError | PPTokenInvalid | PPRetryLater | PPPermanentError - deriving (Show) + deriving (Show, Exception) type PushProviderClient = NtfTknData -> PushNotification -> ExceptT PushProviderError IO () diff --git a/src/Simplex/Messaging/Notifications/Server/Store.hs b/src/Simplex/Messaging/Notifications/Server/Store.hs index 2d063d93a..38ef6cc72 100644 --- a/src/Simplex/Messaging/Notifications/Server/Store.hs +++ b/src/Simplex/Messaging/Notifications/Server/Store.hs @@ -17,6 +17,7 @@ import qualified Data.Map.Strict as M import Data.Maybe (catMaybes) import Data.Set (Set) import qualified Data.Set as S +import Data.Word (Word16) import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Notifications.Protocol import Simplex.Messaging.Protocol (NtfPrivateSignKey) @@ -49,26 +50,19 @@ data NtfTknData = NtfTknData tknVerifyKey :: C.APublicVerifyKey, tknDhKeys :: C.KeyPair 'C.X25519, tknDhSecret :: C.DhSecretX25519, - tknRegCode :: NtfRegCode + tknRegCode :: NtfRegCode, + tknCronInterval :: TVar Word16 } mkNtfTknData :: NtfTokenId -> NewNtfEntity 'Token -> C.KeyPair 'C.X25519 -> C.DhSecretX25519 -> NtfRegCode -> STM NtfTknData mkNtfTknData ntfTknId (NewNtfTkn token tknVerifyKey _) tknDhKeys tknDhSecret tknRegCode = do tknStatus <- newTVar NTRegistered - pure NtfTknData {ntfTknId, token, tknStatus, tknVerifyKey, tknDhKeys, tknDhSecret, tknRegCode} - --- data NtfSubscriptionsStore = NtfSubscriptionsStore - --- { subscriptions :: TMap NtfSubsciptionId NtfSubsciption, --- activeSubscriptions :: TMap (SMPServer, NotifierId) NtfSubsciptionId --- } --- do --- subscriptions <- newTVar M.empty --- activeSubscriptions <- newTVar M.empty --- pure NtfSubscriptionsStore {subscriptions, activeSubscriptions} + tknCronInterval <- newTVar 0 + pure NtfTknData {ntfTknId, token, tknStatus, tknVerifyKey, tknDhKeys, tknDhSecret, tknRegCode, tknCronInterval} data NtfSubData = NtfSubData - { smpQueue :: SMPQueueNtf, + { ntfSubId :: NtfSubscriptionId, + smpQueue :: SMPQueueNtf, notifierKey :: NtfPrivateSignKey, tokenId :: NtfTokenId, subStatus :: TVar NtfSubStatus @@ -175,10 +169,10 @@ getActiveNtfToken st tknId = tStatus <- readTVar tknStatus pure $ if tStatus == NTActive then Just tkn else Nothing -mkNtfSubData :: NewNtfEntity 'Subscription -> STM NtfSubData -mkNtfSubData (NewNtfSub tokenId smpQueue notifierKey) = do +mkNtfSubData :: NtfSubscriptionId -> NewNtfEntity 'Subscription -> STM NtfSubData +mkNtfSubData ntfSubId (NewNtfSub tokenId smpQueue notifierKey) = do subStatus <- newTVar NSNew - pure NtfSubData {smpQueue, tokenId, subStatus, notifierKey} + pure NtfSubData {ntfSubId, smpQueue, tokenId, subStatus, notifierKey} addNtfSubscription :: NtfStore -> NtfSubscriptionId -> NtfSubData -> STM (Maybe ()) addNtfSubscription st subId sub@NtfSubData {smpQueue, tokenId} = diff --git a/src/Simplex/Messaging/Notifications/Server/StoreLog.hs b/src/Simplex/Messaging/Notifications/Server/StoreLog.hs new file mode 100644 index 000000000..7ad0b2fe6 --- /dev/null +++ b/src/Simplex/Messaging/Notifications/Server/StoreLog.hs @@ -0,0 +1,227 @@ +{-# LANGUAGE DataKinds #-} +{-# LANGUAGE DuplicateRecordFields #-} +{-# LANGUAGE GADTs #-} +{-# LANGUAGE LambdaCase #-} +{-# LANGUAGE NamedFieldPuns #-} +{-# LANGUAGE OverloadedStrings #-} + +module Simplex.Messaging.Notifications.Server.StoreLog + ( StoreLog, + NtfStoreLogRecord (..), + readWriteNtfStore, + logCreateToken, + logTokenStatus, + logUpdateToken, + logTokenCron, + logDeleteToken, + logCreateSubscription, + logSubscriptionStatus, + logDeleteSubscription, + closeStoreLog, + ) +where + +import Control.Concurrent.STM +import Control.Monad (void) +import qualified Data.Attoparsec.ByteString.Char8 as A +import qualified Data.ByteString.Char8 as B +import Data.Word (Word16) +import qualified Simplex.Messaging.Crypto as C +import Simplex.Messaging.Encoding.String +import Simplex.Messaging.Notifications.Protocol +import Simplex.Messaging.Notifications.Server.Store +import Simplex.Messaging.Protocol (NtfPrivateSignKey) +import Simplex.Messaging.Server.StoreLog +import Simplex.Messaging.Util (whenM) +import System.Directory (doesFileExist, renameFile) +import System.IO + +data NtfStoreLogRecord + = CreateToken NtfTknRec + | TokenStatus NtfTokenId NtfTknStatus + | UpdateToken NtfTokenId DeviceToken NtfRegCode + | TokenCron NtfTokenId Word16 + | DeleteToken NtfTokenId + | CreateSubscription NtfSubRec + | SubscriptionStatus NtfSubscriptionId NtfSubStatus + | DeleteSubscription NtfSubscriptionId + +data NtfTknRec = NtfTknRec + { ntfTknId :: NtfTokenId, + token :: DeviceToken, + tknStatus :: NtfTknStatus, + tknVerifyKey :: C.APublicVerifyKey, + tknDhKeys :: C.KeyPair 'C.X25519, + tknDhSecret :: C.DhSecretX25519, + tknRegCode :: NtfRegCode, + tknCronInterval :: Word16 + } + +mkTknData :: NtfTknRec -> STM NtfTknData +mkTknData NtfTknRec {ntfTknId, token, tknStatus = status, tknVerifyKey, tknDhKeys, tknDhSecret, tknRegCode, tknCronInterval = cronInt} = do + tknStatus <- newTVar status + tknCronInterval <- newTVar cronInt + pure NtfTknData {ntfTknId, token, tknStatus, tknVerifyKey, tknDhKeys, tknDhSecret, tknRegCode, tknCronInterval} + +mkTknRec :: NtfTknData -> STM NtfTknRec +mkTknRec NtfTknData {ntfTknId, token, tknStatus = status, tknVerifyKey, tknDhKeys, tknDhSecret, tknRegCode, tknCronInterval = cronInt} = do + tknStatus <- readTVar status + tknCronInterval <- readTVar cronInt + pure NtfTknRec {ntfTknId, token, tknStatus, tknVerifyKey, tknDhKeys, tknDhSecret, tknRegCode, tknCronInterval} + +data NtfSubRec = NtfSubRec + { ntfSubId :: NtfSubscriptionId, + smpQueue :: SMPQueueNtf, + notifierKey :: NtfPrivateSignKey, + tokenId :: NtfTokenId, + subStatus :: NtfSubStatus + } + +mkSubData :: NtfSubRec -> STM NtfSubData +mkSubData NtfSubRec {ntfSubId, smpQueue, notifierKey, tokenId, subStatus = status} = do + subStatus <- newTVar status + pure NtfSubData {ntfSubId, smpQueue, notifierKey, tokenId, subStatus} + +mkSubRec :: NtfSubData -> STM NtfSubRec +mkSubRec NtfSubData {ntfSubId, smpQueue, notifierKey, tokenId, subStatus = status} = do + subStatus <- readTVar status + pure NtfSubRec {ntfSubId, smpQueue, notifierKey, tokenId, subStatus} + +instance StrEncoding NtfStoreLogRecord where + strEncode = \case + CreateToken tknRec -> strEncode (Str "TCREATE", tknRec) + TokenStatus tknId tknStatus -> strEncode (Str "TSTATUS", tknId, tknStatus) + UpdateToken tknId token regCode -> strEncode (Str "TUPDATE", tknId, token, regCode) + TokenCron tknId cronInt -> strEncode (Str "TCRON", tknId, cronInt) + DeleteToken tknId -> strEncode (Str "TDELETE", tknId) + CreateSubscription subRec -> strEncode (Str "SCREATE", subRec) + SubscriptionStatus subId subStatus -> strEncode (Str "SSTATUS", subId, subStatus) + DeleteSubscription subId -> strEncode (Str "SDELETE", subId) + strP = + A.choice + [ "TCREATE " *> (CreateToken <$> strP), + "TSTATUS " *> (TokenStatus <$> strP_ <*> strP), + "TUPDATE " *> (UpdateToken <$> strP_ <*> strP_ <*> strP), + "TCRON " *> (TokenCron <$> strP_ <*> strP), + "TDELETE " *> (DeleteToken <$> strP), + "SCREATE " *> (CreateSubscription <$> strP), + "SSTATUS " *> (SubscriptionStatus <$> strP_ <*> strP), + "SDELETE " *> (DeleteSubscription <$> strP) + ] + +instance StrEncoding NtfTknRec where + strEncode NtfTknRec {ntfTknId, token, tknStatus, tknVerifyKey, tknDhKeys, tknDhSecret, tknRegCode, tknCronInterval} = + B.unwords + [ "tknId=" <> strEncode ntfTknId, + "token=" <> strEncode token, + "tokenStatus=" <> strEncode tknStatus, + "verifyKey=" <> strEncode tknVerifyKey, + "dhKeys=" <> strEncode tknDhKeys, + "dhSecret=" <> strEncode tknDhSecret, + "regCode=" <> strEncode tknRegCode, + "cron=" <> strEncode tknCronInterval + ] + strP = do + ntfTknId <- "tknId=" *> strP_ + token <- "token=" *> strP_ + tknStatus <- "tokenStatus=" *> strP_ + tknVerifyKey <- "verifyKey=" *> strP_ + tknDhKeys <- "dhKeys=" *> strP_ + tknDhSecret <- "dhSecret=" *> strP_ + tknRegCode <- "regCode=" *> strP_ + tknCronInterval <- "cron=" *> strP + pure NtfTknRec {ntfTknId, token, tknStatus, tknVerifyKey, tknDhKeys, tknDhSecret, tknRegCode, tknCronInterval} + +instance StrEncoding NtfSubRec where + strEncode NtfSubRec {ntfSubId, smpQueue, notifierKey, tokenId, subStatus} = + B.unwords + [ "subId=" <> strEncode ntfSubId, + "smpQueue=" <> strEncode smpQueue, + "notifierKey=" <> strEncode notifierKey, + "tknId=" <> strEncode tokenId, + "subStatus=" <> strEncode subStatus + ] + strP = do + ntfSubId <- "subId=" *> strP_ + smpQueue <- "smpQueue=" *> strP_ + notifierKey <- "notifierKey=" *> strP_ + tokenId <- "tknId=" *> strP_ + subStatus <- "subStatus=" *> strP + pure NtfSubRec {ntfSubId, smpQueue, notifierKey, tokenId, subStatus} + +logNtfStoreRecord :: StoreLog 'WriteMode -> NtfStoreLogRecord -> IO () +logNtfStoreRecord = writeStoreLogRecord + +logCreateToken :: StoreLog 'WriteMode -> NtfTknData -> IO () +logCreateToken s tkn = logNtfStoreRecord s . CreateToken =<< atomically (mkTknRec tkn) + +logTokenStatus :: StoreLog 'WriteMode -> NtfTokenId -> NtfTknStatus -> IO () +logTokenStatus s tknId tknStatus = logNtfStoreRecord s $ TokenStatus tknId tknStatus + +logUpdateToken :: StoreLog 'WriteMode -> NtfTokenId -> DeviceToken -> NtfRegCode -> IO () +logUpdateToken s tknId token regCode = logNtfStoreRecord s $ UpdateToken tknId token regCode + +logTokenCron :: StoreLog 'WriteMode -> NtfTokenId -> Word16 -> IO () +logTokenCron s tknId cronInt = logNtfStoreRecord s $ TokenCron tknId cronInt + +logDeleteToken :: StoreLog 'WriteMode -> NtfTokenId -> IO () +logDeleteToken s tknId = logNtfStoreRecord s $ DeleteToken tknId + +logCreateSubscription :: StoreLog 'WriteMode -> NtfSubData -> IO () +logCreateSubscription s sub = logNtfStoreRecord s . CreateSubscription =<< atomically (mkSubRec sub) + +logSubscriptionStatus :: StoreLog 'WriteMode -> NtfSubscriptionId -> NtfSubStatus -> IO () +logSubscriptionStatus s subId subStatus = logNtfStoreRecord s $ SubscriptionStatus subId subStatus + +logDeleteSubscription :: StoreLog 'WriteMode -> NtfSubscriptionId -> IO () +logDeleteSubscription s subId = logNtfStoreRecord s $ DeleteSubscription subId + +readWriteNtfStore :: FilePath -> NtfStore -> IO (StoreLog 'WriteMode) +readWriteNtfStore f st = do + whenM (doesFileExist f) $ do + readNtfStore f st + renameFile f $ f <> ".bak" + s <- openWriteStoreLog f + writeNtfStore s st + pure s + +readNtfStore :: FilePath -> NtfStore -> IO () +readNtfStore f st = mapM_ addNtfLogRecord . B.lines =<< B.readFile f + where + addNtfLogRecord s = case strDecode s of + Left e -> B.putStrLn $ "Log parsing error (" <> B.pack e <> "): " <> B.take 100 s + Right lr -> atomically $ case lr of + CreateToken r@NtfTknRec {ntfTknId} -> do + tkn <- mkTknData r + addNtfToken st ntfTknId tkn + TokenStatus tknId status -> + getNtfToken st tknId + >>= mapM_ (\NtfTknData {tknStatus} -> writeTVar tknStatus status) + UpdateToken tknId token' tknRegCode -> + getNtfToken st tknId + >>= mapM_ + ( \tkn@NtfTknData {tknStatus} -> do + removeTokenRegistration st tkn + writeTVar tknStatus NTRegistered + addNtfToken st tknId tkn {token = token', tknRegCode} + ) + TokenCron tknId cronInt -> + getNtfToken st tknId + >>= mapM_ (\NtfTknData {tknCronInterval} -> writeTVar tknCronInterval cronInt) + DeleteToken tknId -> + void $ deleteNtfToken st tknId + CreateSubscription r@NtfSubRec {ntfSubId} -> do + sub <- mkSubData r + void $ addNtfSubscription st ntfSubId sub + SubscriptionStatus subId status -> + getNtfSubscription st subId + >>= mapM_ (\NtfSubData {subStatus} -> writeTVar subStatus status) + DeleteSubscription subId -> + deleteNtfSubscription st subId + +writeNtfStore :: StoreLog 'WriteMode -> NtfStore -> IO () +writeNtfStore s NtfStore {tokens, subscriptions} = do + atomically (readTVar tokens >>= mapM mkTknRec) + >>= mapM_ (writeStoreLogRecord s . CreateToken) + atomically (readTVar subscriptions >>= mapM mkSubRec) + >>= mapM_ (writeStoreLogRecord s . CreateSubscription) diff --git a/src/Simplex/Messaging/Server/StoreLog.hs b/src/Simplex/Messaging/Server/StoreLog.hs index 49ec62c1f..3d8f9625d 100644 --- a/src/Simplex/Messaging/Server/StoreLog.hs +++ b/src/Simplex/Messaging/Server/StoreLog.hs @@ -13,6 +13,7 @@ module Simplex.Messaging.Server.StoreLog openReadStoreLog, storeLogFilePath, closeStoreLog, + writeStoreLogRecord, logCreateQueue, logSecureQueue, logAddNotifier, @@ -91,7 +92,10 @@ instance StrEncoding StoreLogRecord where <|> "NDELETE" *> (DeleteNotifier <$> strP) openWriteStoreLog :: FilePath -> IO (StoreLog 'WriteMode) -openWriteStoreLog f = WriteStoreLog f <$> openFile f WriteMode +openWriteStoreLog f = do + h <- openFile f WriteMode + hSetBuffering h LineBuffering + pure $ WriteStoreLog f h openReadStoreLog :: FilePath -> IO (StoreLog 'ReadMode) openReadStoreLog f = do @@ -108,7 +112,7 @@ closeStoreLog = \case WriteStoreLog _ h -> hClose h ReadStoreLog _ h -> hClose h -writeStoreLogRecord :: StoreLog 'WriteMode -> StoreLogRecord -> IO () +writeStoreLogRecord :: StrEncoding r => StoreLog 'WriteMode -> r -> IO () writeStoreLogRecord (WriteStoreLog _ h) r = do B.hPutStrLn h $ strEncode r hFlush h diff --git a/src/Simplex/Messaging/Transport/Server.hs b/src/Simplex/Messaging/Transport/Server.hs index 2920b1d9b..84d04e68a 100644 --- a/src/Simplex/Messaging/Transport/Server.hs +++ b/src/Simplex/Messaging/Transport/Server.hs @@ -54,7 +54,7 @@ runTCPServer started port server = do (closeServer started clients) $ \sock -> forever . E.bracketOnError (accept sock) (close . fst) $ \(conn, _peer) -> do -- catchAll_ is needed here in case the connection was closed earlier - cId <- atomically $ stateTVar clientId $ \cId -> (cId + 1, cId + 1) + cId <- atomically $ stateTVar clientId $ \cId -> let cId' = cId + 1 in (cId', cId') let closeConn _ = atomically (TM.delete cId clients) >> gracefulClose conn 5000 `catchAll_` pure () tId <- mkWeakThreadId =<< server conn `forkFinally` closeConn atomically $ TM.insert cId tId clients diff --git a/tests/AgentTests/FunctionalAPITests.hs b/tests/AgentTests/FunctionalAPITests.hs index 0882c7e1b..1e9aafad5 100644 --- a/tests/AgentTests/FunctionalAPITests.hs +++ b/tests/AgentTests/FunctionalAPITests.hs @@ -8,6 +8,7 @@ module AgentTests.FunctionalAPITests ( functionalAPITests, + makeConnection, get, (##>), (=##>), diff --git a/tests/AgentTests/NotificationTests.hs b/tests/AgentTests/NotificationTests.hs index 5213bc67a..48c68be52 100644 --- a/tests/AgentTests/NotificationTests.hs +++ b/tests/AgentTests/NotificationTests.hs @@ -9,8 +9,8 @@ module AgentTests.NotificationTests where -- import Control.Logger.Simple (LogConfig (..), LogLevel (..), setLogLevel, withGlobalLogging) -import AgentTests.FunctionalAPITests (get, (##>), (=##>), pattern Msg) -import Control.Concurrent (threadDelay) +import AgentTests.FunctionalAPITests (get, makeConnection, (##>), (=##>), pattern Msg) +import Control.Concurrent (killThread, threadDelay) import Control.Monad.Except import qualified Data.Aeson as J import qualified Data.Aeson.Types as JT @@ -69,6 +69,11 @@ notificationTests t = withSmpServer t $ withAPNSMockServer $ \apns -> withNtfServer t $ testChangeNotificationsMode apns + describe "Notifications server store log" $ do + it "should save and restore tokens and subscriptions" $ \_ -> + withSmpServer t $ + withAPNSMockServer $ \apns -> + testNotificationsStoreLog t apns testNotificationToken :: APNSMockServer -> IO () testNotificationToken APNSMockServer {apnsQ} = do @@ -253,25 +258,9 @@ testNotificationSubscriptionNewConnection APNSMockServer {apnsQ} = do bob <- getSMPAgentClient agentCfg {dbFile = testDB2} initAgentServers Right () <- runExceptT $ do -- alice registers notification token - let aliceTkn = DeviceToken PPApnsTest "abcd" - NTRegistered <- registerNtfToken alice aliceTkn NMInstant - APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData}, sendApnsResponse} <- - atomically $ readTBQueue apnsQ - verification <- ntfData .-> "verification" - nonce <- C.cbNonce <$> ntfData .-> "nonce" - liftIO $ sendApnsResponse APNSRespOk - verifyNtfToken alice aliceTkn nonce verification - NTActive <- checkNtfToken alice aliceTkn + _ <- registerTestToken alice "abcd" NMInstant apnsQ -- bob registers notification token - let bobTkn = DeviceToken PPApnsTest "bcde" - NTRegistered <- registerNtfToken bob bobTkn NMInstant - APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData'}, sendApnsResponse = sendApnsResponse'} <- - atomically $ readTBQueue apnsQ - verification' <- ntfData' .-> "verification" - nonce' <- C.cbNonce <$> ntfData' .-> "nonce" - liftIO $ sendApnsResponse' APNSRespOk - verifyNtfToken bob bobTkn nonce' verification' - NTActive <- checkNtfToken bob bobTkn + _ <- registerTestToken bob "bcde" NMInstant apnsQ -- establish connection liftIO $ threadDelay 50000 (bobId, qInfo) <- createConnection alice SCMInvitation @@ -308,6 +297,19 @@ testNotificationSubscriptionNewConnection APNSMockServer {apnsQ} = do baseId = 3 msgId = subtract baseId +registerTestToken :: AgentClient -> ByteString -> NotificationsMode -> TBQueue APNSMockRequest -> ExceptT AgentErrorType IO DeviceToken +registerTestToken a token mode apnsQ = do + let tkn = DeviceToken PPApnsTest token + NTRegistered <- registerNtfToken a tkn mode + APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData'}, sendApnsResponse = sendApnsResponse'} <- + atomically $ readTBQueue apnsQ + verification' <- ntfData' .-> "verification" + nonce' <- C.cbNonce <$> ntfData' .-> "nonce" + liftIO $ sendApnsResponse' APNSRespOk + verifyNtfToken a tkn nonce' verification' + NTActive <- checkNtfToken a tkn + pure tkn + testChangeNotificationsMode :: APNSMockServer -> IO () testChangeNotificationsMode APNSMockServer {apnsQ} = do alice <- getSMPAgentClient agentCfg initAgentServers @@ -322,15 +324,7 @@ testChangeNotificationsMode APNSMockServer {apnsQ} = do get alice ##> ("", bobId, CON) get bob ##> ("", aliceId, CON) -- register notification token, set mode to NMPeriodic - let tkn = DeviceToken PPApnsTest "abcd" - NTRegistered <- registerNtfToken alice tkn NMPeriodic - APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData}, sendApnsResponse} <- - atomically $ readTBQueue apnsQ - verification <- ntfData .-> "verification" - vNonce <- C.cbNonce <$> ntfData .-> "nonce" - liftIO $ sendApnsResponse APNSRespOk - verifyNtfToken alice tkn vNonce verification - NTActive <- checkNtfToken alice tkn + tkn <- registerTestToken alice "abcd" NMPeriodic apnsQ -- send message, no notification 1 <- msgId <$> sendMessage bob aliceId (SMP.MsgFlags True) "hello" get bob ##> ("", aliceId, SENT $ baseId + 1) @@ -362,6 +356,33 @@ testChangeNotificationsMode APNSMockServer {apnsQ} = do baseId = 3 msgId = subtract baseId +testNotificationsStoreLog :: ATransport -> APNSMockServer -> IO () +testNotificationsStoreLog t APNSMockServer {apnsQ} = do + alice <- getSMPAgentClient agentCfg initAgentServers + bob <- getSMPAgentClient agentCfg {dbFile = testDB2} initAgentServers + Right (aliceId, bobId) <- withNtfServerStoreLog t $ \threadId -> runExceptT $ do + _ <- registerTestToken alice "abcd" NMInstant apnsQ + (aliceId, bobId) <- makeConnection alice bob + liftIO $ threadDelay 250000 + 4 <- sendMessage bob aliceId (SMP.MsgFlags True) "hello" + get bob ##> ("", aliceId, SENT 4) + void $ messageNotification apnsQ + get alice =##> \case ("", c, Msg "hello") -> c == bobId; _ -> False + ackMessage alice bobId 4 + liftIO $ killThread threadId + pure (aliceId, bobId) + + liftIO $ threadDelay 250000 + + Right () <- withNtfServerStoreLog t $ \threadId -> runExceptT $ do + liftIO $ threadDelay 250000 + 5 <- sendMessage bob aliceId (SMP.MsgFlags True) "hello again" + get bob ##> ("", aliceId, SENT 5) + void $ messageNotification apnsQ + get alice =##> \case ("", c, Msg "hello again") -> c == bobId; _ -> False + liftIO $ killThread threadId + pure () + messageNotification :: TBQueue APNSMockRequest -> ExceptT AgentErrorType IO (C.CbNonce, ByteString) messageNotification apnsQ = do 500000 `timeout` atomically (readTBQueue apnsQ) >>= \case diff --git a/tests/NtfClient.hs b/tests/NtfClient.hs index caa7e16fc..86ab111e6 100644 --- a/tests/NtfClient.hs +++ b/tests/NtfClient.hs @@ -62,6 +62,9 @@ apnsTestPort = "6010" testKeyHash :: C.KeyHash testKeyHash = "LcJUMfVhwD8yxjAiSaDzzGF3-kLG4Uh0Fl_ZIjrRwjI=" +ntfTestStoreLogFile :: FilePath +ntfTestStoreLogFile = "tests/tmp/ntf-server-store.log" + testNtfClient :: (Transport c, MonadUnliftIO m) => (THandle c -> m a) -> m a testNtfClient client = runTransportClient testHost ntfTestPort (Just testKeyHash) (Just defaultKeepAliveOpts) $ \h -> @@ -85,16 +88,24 @@ ntfServerCfg = http2cfg = defaultHTTP2ClientConfig {caStoreFile = "tests/fixtures/ca.crt"} }, inactiveClientExpiration = Just defaultInactiveClientExpiration, + storeLogFile = Nothing, + resubscribeDelay = 1000, -- CA certificate private key is not needed for initialization caCertificateFile = "tests/fixtures/ca.crt", privateKeyFile = "tests/fixtures/server.key", certificateFile = "tests/fixtures/server.crt" } +withNtfServerStoreLog :: (MonadUnliftIO m, MonadRandom m) => ATransport -> (ThreadId -> m a) -> m a +withNtfServerStoreLog t = withNtfServerCfg t ntfServerCfg {storeLogFile = Just ntfTestStoreLogFile} + withNtfServerThreadOn :: (MonadUnliftIO m, MonadRandom m) => ATransport -> ServiceName -> (ThreadId -> m a) -> m a -withNtfServerThreadOn t port' = +withNtfServerThreadOn t port' = withNtfServerCfg t ntfServerCfg {transports = [(port', t)]} + +withNtfServerCfg :: (MonadUnliftIO m, MonadRandom m) => ATransport -> NtfServerConfig -> (ThreadId -> m a) -> m a +withNtfServerCfg t cfg = serverBracket - (\started -> runNtfServerBlocking started ntfServerCfg {transports = [(port', t)]}) + (\started -> runNtfServerBlocking started cfg {transports = [(ntfTestPort, t)]}) (pure ()) serverBracket :: MonadUnliftIO m => (TMVar Bool -> m ()) -> m () -> (ThreadId -> m a) -> m a From 09b51dc44ffa2a55e441d5664fab0777f8dbbfb9 Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Tue, 28 Jun 2022 22:03:06 +0400 Subject: [PATCH 094/131] ntf: retry to deliver notification with new push client (#437) * ntf: retry to deliver notification with new push client * update statuses * remove token update * increase tokenTTL --- src/Simplex/Messaging/Notifications/Server.hs | 42 +++++++++++-------- .../Notifications/Server/Push/APNS.hs | 2 +- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/src/Simplex/Messaging/Notifications/Server.hs b/src/Simplex/Messaging/Notifications/Server.hs index 7a54d937b..9c256e2cf 100644 --- a/src/Simplex/Messaging/Notifications/Server.hs +++ b/src/Simplex/Messaging/Notifications/Server.hs @@ -26,7 +26,7 @@ import Simplex.Messaging.Client.Agent import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Notifications.Protocol import Simplex.Messaging.Notifications.Server.Env -import Simplex.Messaging.Notifications.Server.Push.APNS (PNMessageData (..), PushNotification (..), PushProviderClient, PushProviderError (..)) +import Simplex.Messaging.Notifications.Server.Push.APNS (PNMessageData (..), PushNotification (..), PushProviderError (..)) import Simplex.Messaging.Notifications.Server.Store import Simplex.Messaging.Notifications.Server.StoreLog import Simplex.Messaging.Notifications.Transport @@ -155,33 +155,41 @@ ntfPush s@NtfPushServer {pushQ} = forever $ do case (status, ntf) of (_, PNVerification _) -> do -- TODO check token status - liftIO (runExceptT $ deliverNotification pp tkn ntf) >>= \case + deliverNotification pp tkn ntf >>= \case Right _ -> do status_ <- atomically $ stateTVar tknStatus $ \status' -> if status' == NTActive then (Nothing, NTActive) else (Just NTConfirmed, NTConfirmed) forM_ status_ $ \status' -> withNtfLog $ \sl -> logTokenStatus sl ntfTknId status' _ -> pure () (NTActive, PNCheckMessages) -> do - liftIO . void . runExceptT $ deliverNotification pp tkn ntf + void $ deliverNotification pp tkn ntf (NTActive, PNMessage {}) -> do - liftIO . void . runExceptT $ deliverNotification pp tkn ntf + void $ deliverNotification pp tkn ntf _ -> do liftIO $ logError "bad notification token status" where - deliverNotification :: PushProvider -> PushProviderClient - deliverNotification pp tkn ntf = do + deliverNotification :: PushProvider -> NtfTknData -> PushNotification -> m (Either PushProviderError ()) + deliverNotification pp tkn@NtfTknData {ntfTknId, tknStatus} ntf = do deliver <- liftIO $ getPushClient s pp - -- TODO latest pending notification per subscription - retryDeliver deliver (2 :: Integer) + liftIO (runExceptT $ deliver tkn ntf) >>= \case + Right _ -> pure $ Right () + Left e -> case e of + PPConnection _ -> retryDeliver + PPRetryLater -> retryDeliver + -- TODO alert + PPCryptoError _ -> err e + PPResponseError _ _ -> err e + PPTokenInvalid -> updateTknStatus NTInvalid >> err e + PPPermanentError -> err e where - retryDeliver deliver n = - deliver tkn ntf `catchError` \e -> case e of - PPConnection _ -> deliverOrErr deliver n e - PPRetryLater -> deliverOrErr deliver n e - _ -> err e - deliverOrErr deliver n e - | n > 0 = threadDelay 500000 >> retryDeliver deliver (n - 1) - | otherwise = err e - err e = logError (T.pack $ "Push provider error (" <> show pp <> "): " <> show e) >> throwError e + retryDeliver :: m (Either PushProviderError ()) + retryDeliver = do + deliver <- liftIO $ newPushClient s pp + liftIO (runExceptT $ deliver tkn ntf) >>= either err (pure . Right) + updateTknStatus :: NtfTknStatus -> m () + updateTknStatus status = do + atomically $ writeTVar tknStatus status + withNtfLog $ \sl -> logTokenStatus sl ntfTknId status + err e = logError (T.pack $ "Push provider error (" <> show pp <> "): " <> show e) $> Left e runNtfClientTransport :: (Transport c, MonadUnliftIO m, MonadReader NtfEnv m) => THandle c -> m () runNtfClientTransport th@THandle {sessionId} = do diff --git a/src/Simplex/Messaging/Notifications/Server/Push/APNS.hs b/src/Simplex/Messaging/Notifications/Server/Push/APNS.hs index 8e6d1ed0c..13d22b594 100644 --- a/src/Simplex/Messaging/Notifications/Server/Push/APNS.hs +++ b/src/Simplex/Messaging/Notifications/Server/Push/APNS.hs @@ -205,7 +205,7 @@ apnsProviderHost = \case defaultAPNSPushClientConfig :: APNSPushClientConfig defaultAPNSPushClientConfig = APNSPushClientConfig - { tokenTTL = 1200, -- 20 minutes + { tokenTTL = 1800, -- 30 minutes authKeyFileEnv = "APNS_KEY_FILE", -- the environment variables APNS_KEY_FILE and APNS_KEY_ID must be set, or the server would fail to start authKeyAlg = "ES256", authKeyIdEnv = "APNS_KEY_ID", From 926e515bc06f598c3e9a60e04cb85edad03f53fd Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Wed, 29 Jun 2022 08:38:51 +0100 Subject: [PATCH 095/131] fix v2 connecting via v1 contact (#438) --- src/Simplex/Messaging/Agent.hs | 5 ++--- src/Simplex/Messaging/Agent/Client.hs | 5 ++--- tests/AgentTests/FunctionalAPITests.hs | 7 ++++++- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/Simplex/Messaging/Agent.hs b/src/Simplex/Messaging/Agent.hs index c10fe37e9..61c38ab02 100644 --- a/src/Simplex/Messaging/Agent.hs +++ b/src/Simplex/Messaging/Agent.hs @@ -314,10 +314,9 @@ joinConn c connId (CRContactUri (ConnReqUriData _ agentVRange (qUri :| _))) cInf case ( qUri `compatibleVersion` SMP.smpClientVRange, agentVRange `compatibleVersion` aVRange ) of - (Just qInfo, Just _) -> do - -- TODO in agent v2 - use found compatible version rather than current + (Just qInfo, Just version) -> do (connId', cReq) <- newConn c connId SCMInvitation - sendInvitation c qInfo cReq cInfo + sendInvitation c qInfo version cReq cInfo pure connId' _ -> throwError $ AGENT A_VERSION diff --git a/src/Simplex/Messaging/Agent/Client.hs b/src/Simplex/Messaging/Agent/Client.hs index e23cf3f09..81e60d5f2 100644 --- a/src/Simplex/Messaging/Agent/Client.hs +++ b/src/Simplex/Messaging/Agent/Client.hs @@ -529,8 +529,8 @@ sendConfirmation c sq@SndQueue {server, sndId, sndPublicKey = Just sndPublicKey, liftClient SMP $ sendSMPMessage smp Nothing sndId (SMP.MsgFlags {notification = True}) msg sendConfirmation _ _ _ = throwError $ INTERNAL "sendConfirmation called without snd_queue public key(s) in the database" -sendInvitation :: forall m. AgentMonad m => AgentClient -> Compatible SMPQueueInfo -> ConnectionRequestUri 'CMInvitation -> ConnInfo -> m () -sendInvitation c (Compatible SMPQueueInfo {smpServer, senderId, dhPublicKey}) connReq connInfo = +sendInvitation :: forall m. AgentMonad m => AgentClient -> Compatible SMPQueueInfo -> Compatible Version -> ConnectionRequestUri 'CMInvitation -> ConnInfo -> m () +sendInvitation c (Compatible SMPQueueInfo {smpServer, senderId, dhPublicKey}) (Compatible agentVersion) connReq connInfo = withLogClient_ c smpServer senderId "SEND " $ \smp -> do msg <- mkInvitation liftClient SMP $ sendSMPMessage smp Nothing senderId MsgFlags {notification = True} msg @@ -538,7 +538,6 @@ sendInvitation c (Compatible SMPQueueInfo {smpServer, senderId, dhPublicKey}) co mkInvitation :: m ByteString -- this is only encrypted with per-queue E2E, not with double ratchet mkInvitation = do - agentVersion <- asks $ smpAgentVersion . config let agentEnvelope = AgentInvitation {agentVersion, connReq, connInfo} agentCbEncryptOnce dhPublicKey . smpEncode $ SMP.ClientMessage SMP.PHEmpty $ smpEncode agentEnvelope diff --git a/tests/AgentTests/FunctionalAPITests.hs b/tests/AgentTests/FunctionalAPITests.hs index 1e9aafad5..ab58813da 100644 --- a/tests/AgentTests/FunctionalAPITests.hs +++ b/tests/AgentTests/FunctionalAPITests.hs @@ -1,4 +1,5 @@ {-# LANGUAGE DataKinds #-} +{-# LANGUAGE DuplicateRecordFields #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE LambdaCase #-} {-# LANGUAGE NamedFieldPuns #-} @@ -25,6 +26,7 @@ import SMPClient (cfg, testPort, withSmpServer, withSmpServerConfigOn, withSmpSe import Simplex.Messaging.Agent import Simplex.Messaging.Agent.Env.SQLite (AgentConfig (..)) import Simplex.Messaging.Agent.Protocol +import Simplex.Messaging.Client (ProtocolClientConfig (..)) import Simplex.Messaging.Protocol (ErrorType (..), MsgBody) import qualified Simplex.Messaging.Protocol as SMP import Simplex.Messaging.Server.Env.STM (ServerConfig (..)) @@ -46,8 +48,11 @@ get c = atomically (readTBQueue $ subQ c) pattern Msg :: MsgBody -> ACommand 'Agent pattern Msg msgBody <- MSG MsgMeta {integrity = MsgOk} _ msgBody +smpCfgV1 :: ProtocolClientConfig +smpCfgV1 = (smpCfg agentCfg) {smpServerVRange = mkVersionRange 1 1} + agentCfgV1 :: AgentConfig -agentCfgV1 = agentCfg {smpAgentVersion = 1, smpAgentVRange = mkVersionRange 1 1} +agentCfgV1 = agentCfg {smpAgentVersion = 1, smpAgentVRange = mkVersionRange 1 1, smpCfg = smpCfgV1} functionalAPITests :: ATransport -> Spec functionalAPITests t = do From c9501a23a0626bf441756c4342acf08a5900aad8 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Wed, 29 Jun 2022 08:40:21 +0100 Subject: [PATCH 096/131] remove TODOs --- src/Simplex/Messaging/Agent.hs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Simplex/Messaging/Agent.hs b/src/Simplex/Messaging/Agent.hs index 61c38ab02..00368f11d 100644 --- a/src/Simplex/Messaging/Agent.hs +++ b/src/Simplex/Messaging/Agent.hs @@ -287,7 +287,6 @@ joinConn c connId (CRInvitationUri (ConnReqUriData _ agentVRange (qUri :| _)) e2 agentVRange `compatibleVersion` aVRange ) of (Just qInfo, Just (Compatible e2eRcvParams@(CR.E2ERatchetParams _ _ rcDHRr)), Just aVersion@(Compatible connAgentVersion)) -> do - -- TODO in agent v2 - use found compatible version rather than current (pk1, pk2, e2eSndParams) <- liftIO . CR.generateE2EParams $ version e2eRcvParams (_, rcDHRs) <- liftIO C.generateKeyPair' let rc = CR.initSndRatchet rcDHRr rcDHRs $ CR.x3dhSnd pk1 pk2 e2eRcvParams @@ -560,7 +559,7 @@ runSmpQueueMsgDelivery c@AgentClient {subQ} cData@ConnData {connId, duplexHandsh case rq_ of -- party initiating connection (in v1) Just RcvQueue {status} -> - -- TODO it is unclear why subscribeQueue was needed here, + -- it is unclear why subscribeQueue was needed here, -- message delivery can only be enabled for queues that were created in the current session or subscribed -- subscribeQueue c rq connId -- @@ -708,7 +707,6 @@ registerNtfToken' c suppliedDeviceToken suppliedNtfMode = ns <- asks ntfSupervisor atomically $ nsUpdateToken ns tkn {deviceToken = suppliedDeviceToken, ntfTknStatus = NTRegistered, ntfMode = suppliedNtfMode} --- TODO decrypt verification code verifyNtfToken' :: AgentMonad m => AgentClient -> DeviceToken -> C.CbNonce -> ByteString -> m () verifyNtfToken' c deviceToken nonce code = withStore' c getSavedNtfToken >>= \case @@ -862,7 +860,6 @@ processSMPTransmission c@AgentClient {smpClients, subQ} (srv, sessId, rId, cmd) processSMP conn cData@ConnData {connId, duplexHandshake} rq@RcvQueue {rcvDhSecret, e2ePrivKey, e2eDhSecret, status} = case cmd of SMP.MSG srvMsgId srvTs msgFlags msgBody' -> handleNotifyAck $ do - -- TODO deduplicate with previously received msgBody <- agentCbDecrypt rcvDhSecret (C.cbNonce srvMsgId) msgBody' clientMsg@SMP.ClientMsgEnvelope {cmHeader = SMP.PubHeader phVer e2ePubKey_} <- parseMessage msgBody From 2c7cad5378d843490981131ef55a5ab4871727d5 Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Wed, 29 Jun 2022 15:14:44 +0400 Subject: [PATCH 097/131] ntf: update subscription status, error handling on SMP errors / agent events (#439) * ntf: error handling on SMP errors / agent events * refactor * CASubError case * Update src/Simplex/Messaging/Notifications/Server.hs Co-authored-by: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> * Update src/Simplex/Messaging/Notifications/Server.hs Co-authored-by: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> * refactor * Update src/Simplex/Messaging/Notifications/Server.hs Co-authored-by: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> * Update src/Simplex/Messaging/Notifications/Server.hs Co-authored-by: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> * fix lint Co-authored-by: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> --- .../Messaging/Agent/NtfSubSupervisor.hs | 5 +- .../Messaging/Notifications/Protocol.hs | 10 ++++ src/Simplex/Messaging/Notifications/Server.hs | 47 ++++++++++++++----- 3 files changed, 47 insertions(+), 15 deletions(-) diff --git a/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs b/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs index 83bcfbbf2..f6226fb7d 100644 --- a/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs +++ b/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs @@ -165,11 +165,8 @@ runNtfWorker c srv doWork = forever $ do case ntfSubId of Just nSubId -> agentNtfCheckSubscription c nSubId tkn >>= \case - NSNew -> updateSubNextCheck ts NSNew - NSPending -> updateSubNextCheck ts NSPending - NSActive -> updateSubNextCheck ts NSActive - NSEnd -> updateSubNextCheck ts NSEnd NSSMPAuth -> updateSub (NASCreated NSSMPAuth) (NtfSubAction NSADelete) ts -- TODO re-create subscription? + status -> updateSubNextCheck ts status Nothing -> ntfInternalError c connId "NSACheck - no subscription ID" _ -> ntfInternalError c connId "NSACheck - no active token" NSADelete -> case ntfSubId of diff --git a/src/Simplex/Messaging/Notifications/Protocol.hs b/src/Simplex/Messaging/Notifications/Protocol.hs index 789a11136..e025cf7e6 100644 --- a/src/Simplex/Messaging/Notifications/Protocol.hs +++ b/src/Simplex/Messaging/Notifications/Protocol.hs @@ -401,10 +401,14 @@ data NtfSubStatus NSPending | -- | connected and subscribed to SMP server NSActive + | -- | disconnected/unsubscribed from SMP server + NSInactive | -- | NEND received (we currently do not support it) NSEnd | -- | SMP AUTH error NSSMPAuth + | -- | SMP error other than AUTH + NSSMPErr ErrorType deriving (Eq, Show) instance Encoding NtfSubStatus where @@ -412,15 +416,21 @@ instance Encoding NtfSubStatus where NSNew -> "NEW" NSPending -> "PENDING" -- e.g. after SMP server disconnect/timeout while ntf server is retrying to connect NSActive -> "ACTIVE" + NSInactive -> "INACTIVE" NSEnd -> "END" NSSMPAuth -> "SMP_AUTH" + NSSMPErr err -> "SMP_ERR " <> smpEncode err smpP = A.takeTill (== ' ') >>= \case "NEW" -> pure NSNew "PENDING" -> pure NSPending "ACTIVE" -> pure NSActive + "INACTIVE" -> pure NSInactive "END" -> pure NSEnd "SMP_AUTH" -> pure NSSMPAuth + "SMP_ERR" -> do + _ <- A.space + NSSMPErr <$> smpP _ -> fail "bad NtfSubStatus" instance StrEncoding NtfSubStatus where diff --git a/src/Simplex/Messaging/Notifications/Server.hs b/src/Simplex/Messaging/Notifications/Server.hs index 9c256e2cf..481a0ef15 100644 --- a/src/Simplex/Messaging/Notifications/Server.hs +++ b/src/Simplex/Messaging/Notifications/Server.hs @@ -22,6 +22,7 @@ import Data.Map.Strict (Map) import qualified Data.Text as T import Data.Time.Clock.System (getSystemTime) import Network.Socket (ServiceName) +import Simplex.Messaging.Client (ProtocolClientError (..)) import Simplex.Messaging.Client.Agent import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Notifications.Protocol @@ -109,10 +110,11 @@ ntfSubscriber NtfSubscriber {smpSubscribers, newSubQ, smpAgent = ca@SMPClientAge forever $ atomically (peekTBQueue subscriberSubQ) >>= \case NtfSub NtfSubData {smpQueue, notifierKey} -> do + updateSubStatus smpQueue NSPending let SMPQueueNtf {smpServer, notifierId} = smpQueue liftIO (runExceptT $ subscribeQueue ca smpServer ((SPNotifier, notifierId), notifierKey)) >>= \case Right _ -> do - -- update subscription status + updateSubStatus smpQueue NSActive _ <- atomically $ readTBQueue subscriberSubQ pure () Left _e -> pure () @@ -120,15 +122,16 @@ ntfSubscriber NtfSubscriber {smpSubscribers, newSubQ, smpAgent = ca@SMPClientAge receiveSMP :: m () receiveSMP = forever $ do (srv, _sessId, ntfId, msg) <- atomically $ readTBQueue msgQ + let smpQueue = SMPQueueNtf srv ntfId case msg of SMP.NMSG nmsgNonce encNMsgMeta -> do ntfTs <- liftIO getSystemTime - let smpQueue = SMPQueueNtf srv ntfId st <- asks store NtfPushServer {pushQ} <- asks pushServer atomically $ findNtfSubscriptionToken st smpQueue >>= mapM_ (\tkn -> writeTBQueue pushQ (tkn, PNMessage PNMessageData {smpQueue, ntfTs, nmsgNonce, encNMsgMeta})) + SMP.END -> updateSubStatus smpQueue NSInactive _ -> pure () pure () @@ -136,16 +139,38 @@ ntfSubscriber NtfSubscriber {smpSubscribers, newSubQ, smpAgent = ca@SMPClientAge forever $ atomically (readTBQueue agentQ) >>= \case CAConnected _ -> pure () - CADisconnected _srv _subs -> do - -- update subscription statuses - pure () + CADisconnected srv subs -> + forM_ subs $ \(_, ntfId) -> do + let smpQueue = SMPQueueNtf srv ntfId + updateSubStatus smpQueue NSInactive CAReconnected _ -> pure () - CAResubscribed _srv _sub -> do - -- update subscription status - pure () - CASubError _srv _sub _err -> do - -- update subscription status - pure () + CAResubscribed srv sub -> do + let ntfId = snd sub + smpQueue = SMPQueueNtf srv ntfId + updateSubStatus smpQueue NSActive + CASubError srv sub err -> do + let ntfId = snd sub + smpQueue = SMPQueueNtf srv ntfId + case err of + PCEProtocolError e -> case e of + AUTH -> updateSubStatus smpQueue NSSMPAuth + _ -> updateSubStatus smpQueue (NSSMPErr e) + PCEResponseError e -> logErr e + PCEUnexpectedResponse -> logErr err + PCESignatureError e -> logErr e + PCEIOError e -> logErr e + _ -> pure () + where + logErr e = logError (T.pack $ "ntfSubscriber receiveAgent error: " <> show e) + + updateSubStatus smpQueue status = do + st <- asks store + atomically (findNtfSubscription st smpQueue) + >>= mapM_ + ( \NtfSubData {ntfSubId, subStatus} -> do + atomically $ writeTVar subStatus status + withNtfLog $ \sl -> logSubscriptionStatus sl ntfSubId status + ) ntfPush :: forall m. (MonadUnliftIO m, MonadReader NtfEnv m) => NtfPushServer -> m () ntfPush s@NtfPushServer {pushQ} = forever $ do From 6c143726668e7d56e8737b4a0f65b4807bc309a3 Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Wed, 29 Jun 2022 15:35:29 +0400 Subject: [PATCH 098/131] fix linter warning --- src/Simplex/Messaging/Agent.hs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Simplex/Messaging/Agent.hs b/src/Simplex/Messaging/Agent.hs index 00368f11d..0dadd4795 100644 --- a/src/Simplex/Messaging/Agent.hs +++ b/src/Simplex/Messaging/Agent.hs @@ -313,9 +313,9 @@ joinConn c connId (CRContactUri (ConnReqUriData _ agentVRange (qUri :| _))) cInf case ( qUri `compatibleVersion` SMP.smpClientVRange, agentVRange `compatibleVersion` aVRange ) of - (Just qInfo, Just version) -> do + (Just qInfo, Just vrsn) -> do (connId', cReq) <- newConn c connId SCMInvitation - sendInvitation c qInfo version cReq cInfo + sendInvitation c qInfo vrsn cReq cInfo pure connId' _ -> throwError $ AGENT A_VERSION From 07bf2bca3b169b5e2c03e9d485e1623a8df2d2bd Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Wed, 29 Jun 2022 16:52:36 +0400 Subject: [PATCH 099/131] v3.0.0-beta.0 --- apps/ntf-server/Main.hs | 2 +- package.yaml | 2 +- simplexmq.cabal | 2 +- src/Simplex/Messaging/Transport.hs | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/apps/ntf-server/Main.hs b/apps/ntf-server/Main.hs index d17929a65..ee1324912 100644 --- a/apps/ntf-server/Main.hs +++ b/apps/ntf-server/Main.hs @@ -42,7 +42,7 @@ ntfServerCLIConfig = fingerprintFile = combine cfgPath "fingerprint", defaultServerPort = "443", executableName = "ntf-server", - serverVersion = "SMP notifications server v0.2.0", + serverVersion = "SMP notifications server v1.0.0-beta.0", mkIniFile = \enableStoreLog defaultServerPort -> "[STORE_LOG]\n\ \# The server uses STM memory for persistence,\n\ diff --git a/package.yaml b/package.yaml index b3d653a0f..3c2963a5f 100644 --- a/package.yaml +++ b/package.yaml @@ -1,5 +1,5 @@ name: simplexmq -version: 2.3.2 +version: 3.0.0 synopsis: SimpleXMQ message broker description: | This package includes <./docs/Simplex-Messaging-Server.html server>, diff --git a/simplexmq.cabal b/simplexmq.cabal index 5ba13f091..a471a2e10 100644 --- a/simplexmq.cabal +++ b/simplexmq.cabal @@ -5,7 +5,7 @@ cabal-version: 1.12 -- see: https://github.com/sol/hpack name: simplexmq -version: 2.3.2 +version: 3.0.0 synopsis: SimpleXMQ message broker description: This package includes <./docs/Simplex-Messaging-Server.html server>, <./docs/Simplex-Messaging-Client.html client> and diff --git a/src/Simplex/Messaging/Transport.hs b/src/Simplex/Messaging/Transport.hs index c5f74dd46..b20a347e2 100644 --- a/src/Simplex/Messaging/Transport.hs +++ b/src/Simplex/Messaging/Transport.hs @@ -96,7 +96,7 @@ supportedSMPServerVRange :: VersionRange supportedSMPServerVRange = mkVersionRange 1 2 simplexMQVersion :: String -simplexMQVersion = "2.3.2" +simplexMQVersion = "3.0.0-beta.0" -- * Transport connection class From 8dc051179aa139d5d3296aa9373aca6a2697a4a0 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Wed, 29 Jun 2022 17:22:41 +0100 Subject: [PATCH 100/131] backwards compatible message log (#440) --- src/Simplex/Messaging/Server/MsgStore.hs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/Simplex/Messaging/Server/MsgStore.hs b/src/Simplex/Messaging/Server/MsgStore.hs index d5045022f..43092dc8b 100644 --- a/src/Simplex/Messaging/Server/MsgStore.hs +++ b/src/Simplex/Messaging/Server/MsgStore.hs @@ -1,13 +1,16 @@ {-# LANGUAGE FunctionalDependencies #-} {-# LANGUAGE NamedFieldPuns #-} +{-# LANGUAGE OverloadedStrings #-} module Simplex.Messaging.Server.MsgStore where +import Control.Applicative ((<|>)) +import qualified Data.ByteString.Char8 as B import Data.Int (Int64) import Data.Time.Clock.System (SystemTime) import Numeric.Natural import Simplex.Messaging.Encoding.String -import Simplex.Messaging.Protocol (MsgBody, MsgFlags, MsgId, RecipientId) +import Simplex.Messaging.Protocol (MsgBody, MsgFlags, MsgId, RecipientId, noMsgFlags) data Message = Message { msgId :: MsgId, @@ -17,9 +20,18 @@ data Message = Message } instance StrEncoding Message where - strEncode Message {msgId, ts, msgFlags, msgBody} = strEncode (msgId, ts, msgFlags, msgBody) + strEncode Message {msgId, ts, msgFlags, msgBody} = + B.unwords + [ strEncode msgId, + strEncode ts, + "flags=" <> strEncode msgFlags, + strEncode msgBody + ] strP = do - (msgId, ts, msgFlags, msgBody) <- strP + msgId <- strP_ + ts <- strP_ + msgFlags <- ("flags=" *> strP_) <|> pure noMsgFlags + msgBody <- strP pure Message {msgId, ts, msgFlags, msgBody} data MsgLogRecord = MsgLogRecord RecipientId Message From 79d67694edd4b76ee246dbebdc53ac47234312f3 Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Thu, 30 Jun 2022 13:39:24 +0400 Subject: [PATCH 101/131] ntf: flush ntfSubQ on deleting all subscriptions (#442) --- src/Simplex/Messaging/Agent.hs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Simplex/Messaging/Agent.hs b/src/Simplex/Messaging/Agent.hs index 0dadd4795..001d551b0 100644 --- a/src/Simplex/Messaging/Agent.hs +++ b/src/Simplex/Messaging/Agent.hs @@ -65,7 +65,7 @@ module Simplex.Messaging.Agent ) where -import Control.Concurrent.STM (stateTVar) +import Control.Concurrent.STM (flushTBQueue, stateTVar) import Control.Logger.Simple (logInfo, showText) import Control.Monad.Except import Control.Monad.IO.Unlift (MonadUnliftIO) @@ -792,6 +792,7 @@ initializeNtfSubs c = do smpDeleteNtfSubs :: AgentMonad m => AgentClient -> m () smpDeleteNtfSubs c = do ns <- asks ntfSupervisor + void . atomically . flushTBQueue $ ntfSubQ ns connIds <- atomically $ getSubscriptions c forM_ connIds $ \connId -> atomically $ writeTBQueue (ntfSubQ ns) (connId, NSCSmpDelete) From 56ef95e8aed38c6291d54d766fa6d9d347d70201 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Thu, 30 Jun 2022 11:01:34 +0100 Subject: [PATCH 102/131] ntf: fix null action ts (#443) Co-authored-by: JRoberts <8711996+jr-simplex@users.noreply.github.com> --- src/Simplex/Messaging/Agent/Client.hs | 3 +- src/Simplex/Messaging/Agent/Env/SQLite.hs | 3 +- .../Messaging/Agent/NtfSubSupervisor.hs | 63 ++++++++++--------- src/Simplex/Messaging/Agent/Store/SQLite.hs | 56 +++++++++-------- .../Store/SQLite/Migrations/M20220608_v2.hs | 2 +- .../Store/SQLite/Migrations/agent_schema.sql | 2 +- src/Simplex/Messaging/Notifications/Client.hs | 40 ++++++++---- 7 files changed, 94 insertions(+), 75 deletions(-) diff --git a/src/Simplex/Messaging/Agent/Client.hs b/src/Simplex/Messaging/Agent/Client.hs index 81e60d5f2..cc7e38026 100644 --- a/src/Simplex/Messaging/Agent/Client.hs +++ b/src/Simplex/Messaging/Agent/Client.hs @@ -81,7 +81,6 @@ import Data.Maybe (catMaybes) import Data.Set (Set) import Data.Text.Encoding import Data.Word (Word16) -import Database.SQLite.Simple (SQLError) import qualified Database.SQLite.Simple as DB -- import GHC.Conc (unsafeIOToSTM) import Simplex.Messaging.Agent.Env.SQLite @@ -735,7 +734,7 @@ withStore c action = do atomically $ endAgentOperation c AODatabase liftEither $ first storeError r where - handleInternal :: SQLError -> IO (Either StoreError a) + handleInternal :: E.SomeException -> IO (Either StoreError a) handleInternal = pure . Left . SEInternal . bshow storeError :: StoreError -> AgentErrorType storeError = \case diff --git a/src/Simplex/Messaging/Agent/Env/SQLite.hs b/src/Simplex/Messaging/Agent/Env/SQLite.hs index f8199221b..469f543e8 100644 --- a/src/Simplex/Messaging/Agent/Env/SQLite.hs +++ b/src/Simplex/Messaging/Agent/Env/SQLite.hs @@ -29,6 +29,7 @@ import Control.Monad.Reader import Crypto.Random import Data.List.NonEmpty (NonEmpty) import Data.Time.Clock (NominalDiffTime, nominalDay) +import Data.Word (Word16) import Network.Socket import Numeric.Natural import Simplex.Messaging.Agent.Protocol @@ -46,7 +47,6 @@ import Simplex.Messaging.Version import System.Random (StdGen, newStdGen) import UnliftIO (Async) import UnliftIO.STM -import Data.Word (Word16) -- | Agent monad with MonadReader Env and MonadError AgentErrorType type AgentMonad m = (MonadUnliftIO m, MonadReader Env m, MonadError AgentErrorType m) @@ -142,6 +142,7 @@ data NtfSupervisor = NtfSupervisor } data NtfSupervisorCommand = NSCCreate | NSCDelete | NSCSmpDelete | NSCNtfWorker NtfServer | NSCNtfSMPWorker SMPServer + deriving (Show) newNtfSubSupervisor :: Natural -> STM NtfSupervisor newNtfSubSupervisor qSize = do diff --git a/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs b/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs index f6226fb7d..76911ef13 100644 --- a/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs +++ b/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs @@ -4,6 +4,7 @@ {-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE ScopedTypeVariables #-} +{-# LANGUAGE TupleSections #-} module Simplex.Messaging.Agent.NtfSubSupervisor ( runNtfSupervisor, @@ -59,7 +60,6 @@ processNtfSub c (connId, cmd) = do ntfServer_ <- getNtfServer c case cmd of NSCCreate -> do - -- TODO merge getNtfSubscription and getRcvQueue into one method to read both in same transaction? (sub_, RcvQueue {server = smpServer, clientNtfCreds}) <- withStore c $ \db -> runExceptT $ do sub_ <- liftIO $ getNtfSubscription db connId q <- ExceptT $ getRcvQueue db connId @@ -69,11 +69,11 @@ processNtfSub c (connId, cmd) = do currentTime <- liftIO getCurrentTime case clientNtfCreds of Just ClientNtfCreds {notifierId} -> do - let newSub = newNtfSubscription connId smpServer (Just notifierId) ntfServer NASKey currentTime - withStore' c $ \db -> createNtfSubscription db newSub (NtfSubAction NSACreate) + let newSub = newNtfSubscription connId smpServer (Just notifierId) ntfServer NASKey + withStore' c $ \db -> createNtfSubscription db NtfSubActionData {action = NtfSubNTFAction NSACreate, actionTs = currentTime, ntfSubscription = newSub} _ -> do - let newSub = newNtfSubscription connId smpServer Nothing ntfServer NASNew currentTime - withStore' c $ \db -> createNtfSubscription db newSub (NtfSubSMPAction NSASmpKey) + let newSub = newNtfSubscription connId smpServer Nothing ntfServer NASNew + withStore' c $ \db -> createNtfSubscription db NtfSubActionData {action = NtfSubSMPAction NSASmpKey, actionTs = currentTime, ntfSubscription = newSub} -- TODO optimize? -- TODO - read action in getNtfSubscription and decide which worker to create -- TODO - SMP worker can create Ntf worker on NKEY completion @@ -90,7 +90,7 @@ processNtfSub c (connId, cmd) = do addNtfWorker ntfServer _ -> pure () -- error - notification server not configured NSCDelete -> do - withStore' c $ \db -> markNtfSubscriptionForDeletion db connId (NtfSubAction NSADelete) + withStore' c $ \db -> markNtfSubscriptionForDeletion db connId (NtfSubNTFAction NSADelete) case ntfServer_ of (Just ntfServer) -> addNtfWorker ntfServer _ -> pure () @@ -128,10 +128,10 @@ runNtfWorker c srv doWork = forever $ do nextSub_ <- withStore' c (`getNextNtfSubAction` srv) case nextSub_ of Nothing -> noWorkToDo - Just ntfSub@(NtfSubscription {connId}, _) -> do + Just a@NtfSubNTFActionData {ntfSubscription = NtfSubscription {connId}} -> do ri <- asks $ reconnectInterval . config withRetryInterval ri $ \loop -> - processAction ntfSub + processAction a `catchError` ( \e -> case e of BROKER NETWORK -> loop @@ -142,11 +142,11 @@ runNtfWorker c srv doWork = forever $ do liftIO $ threadDelay throttle where noWorkToDo = void . atomically $ tryTakeTMVar doWork - processAction :: (NtfSubscription, NtfSubAction) -> m () - processAction (ntfSub@NtfSubscription {connId, smpServer, ntfSubId}, ntfSubAction) = do + processAction :: NtfSubNTFActionData -> m () + processAction NtfSubNTFActionData {ntfAction, actionTs, ntfSubscription = sub@NtfSubscription {connId, smpServer, ntfSubId}} = do ts <- liftIO getCurrentTime - unlessM (rescheduleAction doWork ts ntfSub) $ - case ntfSubAction of + unlessM (rescheduleAction doWork ts actionTs) $ + case ntfAction of NSACreate -> getNtfToken >>= \case Just tkn@NtfToken {ntfTokenId = Just tknId, ntfTknStatus = NTActive, ntfMode = NMInstant} -> do @@ -154,9 +154,10 @@ runNtfWorker c srv doWork = forever $ do case clientNtfCreds of Just ClientNtfCreds {ntfPrivateKey, notifierId} -> do nSubId <- agentNtfCreateSubscription c tknId tkn (SMPQueueNtf smpServer notifierId) ntfPrivateKey - let actionTs = addUTCTime 30 ts + -- TODO smaller retry until Active, less frequently (daily?) once Active + let actionTs' = addUTCTime 30 ts withStore' c $ \db -> - updateNtfSubscription db connId ntfSub {ntfSubId = Just nSubId, ntfSubStatus = NASCreated NSNew, ntfSubActionTs = actionTs} (NtfSubAction NSACheck) + updateNtfSubscription db connId NtfSubActionData {ntfSubscription = sub {ntfSubId = Just nSubId, ntfSubStatus = NASCreated NSNew}, action = NtfSubNTFAction NSACheck, actionTs = actionTs'} _ -> ntfInternalError c connId "NSACreate - no notifier queue credentials" _ -> ntfInternalError c connId "NSACreate - no active token" NSACheck -> @@ -165,7 +166,7 @@ runNtfWorker c srv doWork = forever $ do case ntfSubId of Just nSubId -> agentNtfCheckSubscription c nSubId tkn >>= \case - NSSMPAuth -> updateSub (NASCreated NSSMPAuth) (NtfSubAction NSADelete) ts -- TODO re-create subscription? + NSSMPAuth -> updateSub (NASCreated NSSMPAuth) (NtfSubNTFAction NSADelete) ts -- TODO re-create subscription? status -> updateSubNextCheck ts status Nothing -> ntfInternalError c connId "NSACheck - no subscription ID" _ -> ntfInternalError c connId "NSACheck - no active token" @@ -174,7 +175,7 @@ runNtfWorker c srv doWork = forever $ do (getNtfToken >>= \tkn -> forM_ tkn $ agentNtfDeleteSubscription c nSubId) `E.finally` do withStore' c $ \db -> - updateNtfSubscription db connId ntfSub {ntfSubId = Nothing, ntfSubStatus = NASOff, ntfSubActionTs = ts} (NtfSubSMPAction NSASmpDelete) + updateNtfSubscription db connId NtfSubActionData {ntfSubscription = sub {ntfSubId = Nothing, ntfSubStatus = NASOff}, action = NtfSubSMPAction NSASmpDelete, actionTs = ts} ns <- asks ntfSupervisor atomically $ writeTBQueue (ntfSubQ ns) (connId, NSCNtfSMPWorker smpServer) Nothing -> ntfInternalError c connId "NSADelete - no subscription ID" @@ -182,10 +183,10 @@ runNtfWorker c srv doWork = forever $ do updateSubNextCheck ts toStatus = do checkInterval <- asks $ ntfSubCheckInterval . config let nextCheckTs = addUTCTime checkInterval ts - updateSub (NASCreated toStatus) (NtfSubAction NSACheck) nextCheckTs - updateSub toStatus toAction actionTs = + updateSub (NASCreated toStatus) (NtfSubNTFAction NSACheck) nextCheckTs + updateSub toStatus toAction actionTs' = withStore' c $ \db -> - updateNtfSubscription db connId ntfSub {ntfSubStatus = toStatus, ntfSubActionTs = actionTs} toAction + updateNtfSubscription db connId NtfSubActionData {ntfSubscription = sub {ntfSubStatus = toStatus}, action = toAction, actionTs = actionTs'} runNtfSMPWorker :: forall m. AgentMonad m => AgentClient -> SMPServer -> TMVar () -> m () runNtfSMPWorker c srv doWork = forever $ do @@ -193,10 +194,10 @@ runNtfSMPWorker c srv doWork = forever $ do nextSub_ <- withStore' c (`getNextNtfSubSMPAction` srv) case nextSub_ of Nothing -> noWorkToDo - Just ntfSub@(NtfSubscription {connId}, _) -> do + Just a@NtfSubSMPActionData {ntfSubscription = NtfSubscription {connId}} -> do ri <- asks $ reconnectInterval . config withRetryInterval ri $ \loop -> - processAction ntfSub + processAction a `catchError` ( \e -> case e of BROKER NETWORK -> loop @@ -207,11 +208,11 @@ runNtfSMPWorker c srv doWork = forever $ do liftIO $ threadDelay throttle where noWorkToDo = void . atomically $ tryTakeTMVar doWork - processAction :: (NtfSubscription, NtfSubSMPAction) -> m () - processAction (ntfSub@NtfSubscription {connId, ntfServer}, ntfSubAction) = do + processAction :: NtfSubSMPActionData -> m () + processAction NtfSubSMPActionData {smpAction, actionTs, ntfSubscription = sub@NtfSubscription {connId, ntfServer}} = do ts <- liftIO getCurrentTime - unlessM (rescheduleAction doWork ts ntfSub) $ - case ntfSubAction of + unlessM (rescheduleAction doWork ts actionTs) $ + case smpAction of NSASmpKey -> getNtfToken >>= \case Just NtfToken {ntfTknStatus = NTActive, ntfMode = NMInstant} -> do @@ -223,7 +224,7 @@ runNtfSMPWorker c srv doWork = forever $ do let rcvNtfDhSecret = C.dh' rcvNtfSrvPubDhKey rcvNtfPrivDhKey withStore' c $ \st -> do setRcvQueueNtfCreds st connId ClientNtfCreds {ntfPublicKey, ntfPrivateKey, notifierId, rcvNtfDhSecret} - updateNtfSubscription st connId ntfSub {ntfQueueId = Just notifierId, ntfSubStatus = NASKey, ntfSubActionTs = ts} (NtfSubAction NSACreate) + updateNtfSubscription st connId NtfSubActionData {ntfSubscription = sub {ntfQueueId = Just notifierId, ntfSubStatus = NASKey}, action = NtfSubNTFAction NSACreate, actionTs = ts} ns <- asks ntfSupervisor atomically $ sendNtfSubCommand ns (connId, NSCNtfWorker ntfServer) _ -> ntfInternalError c connId "NSASmpKey - no active token" @@ -232,13 +233,13 @@ runNtfSMPWorker c srv doWork = forever $ do forM_ rq_ $ \rq -> disableQueueNotifications c rq withStore' c $ \db -> deleteNtfSubscription db connId -rescheduleAction :: AgentMonad m => TMVar () -> UTCTime -> NtfSubscription -> m Bool -rescheduleAction doWork ts NtfSubscription {ntfSubActionTs} - | ntfSubActionTs <= ts = pure False +rescheduleAction :: AgentMonad m => TMVar () -> UTCTime -> UTCTime -> m Bool +rescheduleAction doWork ts actionTs + | actionTs <= ts = pure False | otherwise = do void . atomically $ tryTakeTMVar doWork void . forkIO $ do - threadDelay $ diffInMicros ntfSubActionTs ts + threadDelay $ diffInMicros actionTs ts void . atomically $ tryPutTMVar doWork () pure True @@ -280,7 +281,7 @@ closeNtfSupervisor ns = do cancelNtfWorkers_ :: TMap ProtocolServer (TMVar (), Async ()) -> IO () cancelNtfWorkers_ wsVar = do - ws <- atomically $ stateTVar wsVar $ \ws -> (ws, M.empty) + ws <- atomically $ stateTVar wsVar (,M.empty) forM_ ws $ uninterruptibleCancel . snd getNtfServer :: AgentMonad m => AgentClient -> m (Maybe NtfServer) diff --git a/src/Simplex/Messaging/Agent/Store/SQLite.hs b/src/Simplex/Messaging/Agent/Store/SQLite.hs index 06b1f5c0d..3a4d7319e 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite.hs @@ -123,7 +123,7 @@ import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Crypto.Ratchet (RatchetX448, SkippedMsgDiff (..), SkippedMsgKeys) import Simplex.Messaging.Encoding import Simplex.Messaging.Encoding.String -import Simplex.Messaging.Notifications.Client (NtfAgentSubStatus (..), NtfServer, NtfSubAction (..), NtfSubOrSMPAction (..), NtfSubSMPAction (..), NtfSubscription (..), NtfTknAction (..), NtfToken (..)) +import Simplex.Messaging.Notifications.Client import Simplex.Messaging.Notifications.Protocol (DeviceToken (..), NtfSubscriptionId, NtfTknStatus (..), NtfTokenId, SMPQueueNtf (..)) import Simplex.Messaging.Parsers (blobFieldParser, fromTextField_) import Simplex.Messaging.Protocol (MsgBody, MsgFlags, ProtocolServer (..), RcvNtfDhSecret) @@ -719,7 +719,7 @@ getNtfSubscription db connId = db [sql| SELECT s.host, s.port, s.key_hash, ns.ntf_host, ns.ntf_port, ns.ntf_key_hash, - nsb.smp_ntf_id, nsb.ntf_sub_id, nsb.ntf_sub_status, nsb.ntf_sub_action_ts + nsb.smp_ntf_id, nsb.ntf_sub_id, nsb.ntf_sub_status FROM ntf_subscriptions nsb JOIN servers s ON s.host = nsb.smp_host AND s.port = nsb.smp_port JOIN ntf_servers ns USING (ntf_host, ntf_port) @@ -727,13 +727,13 @@ getNtfSubscription db connId = |] (Only connId) where - ntfSubscription (smpHost, smpPort, smpKeyHash, ntfHost, ntfPort, ntfKeyHash, ntfQueueId, ntfSubId, ntfSubStatus, ntfSubActionTs) = + ntfSubscription (smpHost, smpPort, smpKeyHash, ntfHost, ntfPort, ntfKeyHash, ntfQueueId, ntfSubId, ntfSubStatus) = let smpServer = SMPServer smpHost smpPort smpKeyHash ntfServer = ProtocolServer ntfHost ntfPort ntfKeyHash - in NtfSubscription {connId, smpServer, ntfQueueId, ntfServer, ntfSubId, ntfSubStatus, ntfSubActionTs} + in NtfSubscription {connId, smpServer, ntfQueueId, ntfServer, ntfSubId, ntfSubStatus} -createNtfSubscription :: DB.Connection -> NtfSubscription -> NtfSubOrSMPAction -> IO () -createNtfSubscription db NtfSubscription {connId, smpServer = (SMPServer host port _), ntfQueueId, ntfServer = (SMPServer ntfHost ntfPort _), ntfSubId, ntfSubStatus, ntfSubActionTs} ntfAction = +createNtfSubscription :: DB.Connection -> NtfSubActionData -> IO () +createNtfSubscription db NtfSubActionData {action, actionTs, ntfSubscription = NtfSubscription {connId, smpServer = (SMPServer host port _), ntfQueueId, ntfServer = (SMPServer ntfHost ntfPort _), ntfSubId, ntfSubStatus}} = DB.execute db [sql| @@ -743,12 +743,12 @@ createNtfSubscription db NtfSubscription {connId, smpServer = (SMPServer host po VALUES (?,?,?,?,?,?,?,?,?,?,?) |] ( (connId, host, port, ntfQueueId, ntfHost, ntfPort, ntfSubId) - :. (ntfSubStatus, ntfSubAction, ntfSubSMPAction, ntfSubActionTs) + :. (ntfSubStatus, ntfSubAction, ntfSubSMPAction, actionTs) ) where - (ntfSubAction, ntfSubSMPAction) = ntfSubAndSMPAction ntfAction + (ntfSubAction, ntfSubSMPAction) = ntfSubAndSMPAction action -markNtfSubscriptionForDeletion :: DB.Connection -> ConnId -> NtfSubOrSMPAction -> IO () +markNtfSubscriptionForDeletion :: DB.Connection -> ConnId -> NtfSubAction -> IO () markNtfSubscriptionForDeletion db connId ntfAction = do updatedAt <- getCurrentTime DB.execute @@ -762,8 +762,8 @@ markNtfSubscriptionForDeletion db connId ntfAction = do where (ntfSubAction, ntfSubSMPAction) = ntfSubAndSMPAction ntfAction -updateNtfSubscription :: DB.Connection -> ConnId -> NtfSubscription -> NtfSubOrSMPAction -> IO () -updateNtfSubscription db connId NtfSubscription {ntfQueueId, ntfSubId, ntfSubStatus, ntfSubActionTs} ntfAction = do +updateNtfSubscription :: DB.Connection -> ConnId -> NtfSubActionData -> IO () +updateNtfSubscription db connId NtfSubActionData {action, actionTs, ntfSubscription = NtfSubscription {ntfQueueId, ntfSubId, ntfSubStatus}} = do r <- maybeFirstRow fromOnly $ DB.query db "SELECT updated_by_supervisor FROM ntf_subscriptions WHERE conn_id = ?" (Only connId) forM_ r $ \updatedBySupervisor -> do updatedAt <- getCurrentTime @@ -785,9 +785,9 @@ updateNtfSubscription db connId NtfSubscription {ntfQueueId, ntfSubId, ntfSubSta SET smp_ntf_id = ?, ntf_sub_id = ?, ntf_sub_status = ?, ntf_sub_action = ?, ntf_sub_smp_action = ?, ntf_sub_action_ts = ?, updated_by_supervisor = ?, updated_at = ? WHERE conn_id = ? |] - (ntfQueueId, ntfSubId, ntfSubStatus, ntfSubAction, ntfSubSMPAction, ntfSubActionTs, False, updatedAt, connId) + (ntfQueueId, ntfSubId, ntfSubStatus, ntfSubAction, ntfSubSMPAction, actionTs, False, updatedAt, connId) where - (ntfSubAction, ntfSubSMPAction) = ntfSubAndSMPAction ntfAction + (ntfSubAction, ntfSubSMPAction) = ntfSubAndSMPAction action setNullNtfSubscriptionAction :: DB.Connection -> ConnId -> IO () setNullNtfSubscriptionAction db connId = do @@ -802,7 +802,7 @@ setNullNtfSubscriptionAction db connId = do SET ntf_sub_action = ?, ntf_sub_smp_action = ?, ntf_sub_action_ts = ?, updated_by_supervisor = ?, updated_at = ? WHERE conn_id = ? |] - (Nothing :: Maybe NtfSubAction, Nothing :: Maybe NtfSubSMPAction, Nothing :: Maybe UTCTime, False, updatedAt, connId) + (Nothing :: Maybe NtfSubNTFAction, Nothing :: Maybe NtfSubSMPAction, Nothing :: Maybe UTCTime, False, updatedAt, connId) deleteNtfSubscription :: DB.Connection -> ConnId -> IO () deleteNtfSubscription db connId = do @@ -821,11 +821,11 @@ deleteNtfSubscription db connId = do (Nothing :: Maybe SMP.NotifierId, Nothing :: Maybe NtfSubscriptionId, NASDeleted, False, updatedAt, connId) else DB.execute db "DELETE FROM ntf_subscriptions WHERE conn_id = ?" (Only connId) -getNextNtfSubAction :: DB.Connection -> NtfServer -> IO (Maybe (NtfSubscription, NtfSubAction)) +getNextNtfSubAction :: DB.Connection -> NtfServer -> IO (Maybe NtfSubNTFActionData) getNextNtfSubAction db ntfServer@(ProtocolServer ntfHost ntfPort _) = do - maybeFirstRow ntfSubscription getNtfSubAction_ $>>= \ntfSub@(NtfSubscription {connId}, _) -> do + maybeFirstRow ntfSubscriptionData getNtfSubAction_ $>>= \a@NtfSubNTFActionData {ntfSubscription = NtfSubscription {connId}} -> do DB.execute db "UPDATE ntf_subscriptions SET updated_by_supervisor = ? WHERE conn_id = ?" (False, connId) - pure $ Just ntfSub + pure $ Just a where getNtfSubAction_ = DB.query @@ -840,15 +840,16 @@ getNextNtfSubAction db ntfServer@(ProtocolServer ntfHost ntfPort _) = do LIMIT 1 |] (ntfHost, ntfPort) - ntfSubscription (connId, smpHost, smpPort, smpKeyHash, ntfQueueId, ntfSubId, ntfSubStatus, ntfSubActionTs, ntfSubAction) = + ntfSubscriptionData (connId, smpHost, smpPort, smpKeyHash, ntfQueueId, ntfSubId, ntfSubStatus, actionTs, ntfAction) = let smpServer = SMPServer smpHost smpPort smpKeyHash - in (NtfSubscription {connId, smpServer, ntfQueueId, ntfServer, ntfSubId, ntfSubStatus, ntfSubActionTs}, ntfSubAction) + ntfSubscription = NtfSubscription {connId, smpServer, ntfQueueId, ntfServer, ntfSubId, ntfSubStatus} + in NtfSubNTFActionData {ntfAction, actionTs, ntfSubscription} -getNextNtfSubSMPAction :: DB.Connection -> SMPServer -> IO (Maybe (NtfSubscription, NtfSubSMPAction)) +getNextNtfSubSMPAction :: DB.Connection -> SMPServer -> IO (Maybe NtfSubSMPActionData) getNextNtfSubSMPAction db smpServer@(SMPServer smpHost smpPort _) = do - maybeFirstRow ntfSubscription getNtfSubAction_ $>>= \ntfSub@(NtfSubscription {connId}, _) -> do + maybeFirstRow ntfSubscriptionData getNtfSubAction_ $>>= \a@NtfSubSMPActionData {ntfSubscription = NtfSubscription {connId}} -> do DB.execute db "UPDATE ntf_subscriptions SET updated_by_supervisor = ? WHERE conn_id = ?" (False, connId) - pure $ Just ntfSub + pure $ Just a where getNtfSubAction_ = DB.query @@ -858,14 +859,15 @@ getNextNtfSubSMPAction db smpServer@(SMPServer smpHost smpPort _) = do ns.smp_ntf_id, ns.ntf_sub_id, ns.ntf_sub_status, ns.ntf_sub_action_ts, ns.ntf_sub_smp_action FROM ntf_subscriptions ns JOIN ntf_servers s USING (ntf_host, ntf_port) - WHERE ns.smp_host = ? AND ns.smp_port = ? AND ns.ntf_sub_smp_action IS NOT NULL + WHERE ns.smp_host = ? AND ns.smp_port = ? AND ns.ntf_sub_smp_action IS NOT NULL AND ns.ntf_sub_action_ts IS NOT NULL ORDER BY ns.ntf_sub_action_ts ASC LIMIT 1 |] (smpHost, smpPort) - ntfSubscription (connId, ntfHost, ntfPort, ntfKeyHash, ntfQueueId, ntfSubId, ntfSubStatus, ntfSubActionTs, ntfSubAction) = + ntfSubscriptionData (connId, ntfHost, ntfPort, ntfKeyHash, ntfQueueId, ntfSubId, ntfSubStatus, actionTs, smpAction) = let ntfServer = ProtocolServer ntfHost ntfPort ntfKeyHash - in (NtfSubscription {connId, smpServer, ntfQueueId, ntfServer, ntfSubId, ntfSubStatus, ntfSubActionTs}, ntfSubAction) + ntfSubscription = NtfSubscription {connId, smpServer, ntfQueueId, ntfServer, ntfSubId, ntfSubStatus} + in NtfSubSMPActionData {ntfSubscription, smpAction, actionTs} getActiveNtfToken :: DB.Connection -> IO (Maybe NtfToken) getActiveNtfToken db = @@ -1311,6 +1313,6 @@ createWithRandomId gVar create = tryCreate 3 randomId :: TVar ChaChaDRG -> Int -> IO ByteString randomId gVar n = U.encode <$> (atomically . stateTVar gVar $ randomBytesGenerate n) -ntfSubAndSMPAction :: NtfSubOrSMPAction -> (Maybe NtfSubAction, Maybe NtfSubSMPAction) -ntfSubAndSMPAction (NtfSubAction nsa) = (Just nsa, Nothing) +ntfSubAndSMPAction :: NtfSubAction -> (Maybe NtfSubNTFAction, Maybe NtfSubSMPAction) +ntfSubAndSMPAction (NtfSubNTFAction nsa) = (Just nsa, Nothing) ntfSubAndSMPAction (NtfSubSMPAction nsa) = (Nothing, Just nsa) diff --git a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220608_v2.hs b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220608_v2.hs index 544ccf60f..eb769bcea 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220608_v2.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220608_v2.hs @@ -35,7 +35,7 @@ CREATE TABLE ntf_subscriptions ( ntf_port TEXT NOT NULL, ntf_sub_id BLOB, ntf_sub_status TEXT NOT NULL, -- see NtfAgentSubStatus - ntf_sub_action TEXT, -- if there is an action required on this subscription: NtfSubAction + ntf_sub_action TEXT, -- if there is an action required on this subscription: NtfSubNTFAction ntf_sub_smp_action TEXT, -- action with SMP server: NtfSubSMPAction; only one of this and ntf_sub_action can (should) be not null in same record ntf_sub_action_ts TEXT, -- the earliest time for the action, e.g. checks can be scheduled every X hours updated_by_supervisor INTEGER NOT NULL DEFAULT 0, -- to be checked on updates by workers to not overwrite supervisor command (state still should be updated) diff --git a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql index bda5853e2..5deac05d5 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql +++ b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql @@ -180,7 +180,7 @@ CREATE TABLE ntf_subscriptions( ntf_port TEXT NOT NULL, ntf_sub_id BLOB, ntf_sub_status TEXT NOT NULL, -- see NtfAgentSubStatus - ntf_sub_action TEXT, -- if there is an action required on this subscription: NtfSubAction + ntf_sub_action TEXT, -- if there is an action required on this subscription: NtfSubNTFAction ntf_sub_smp_action TEXT, -- action with SMP server: NtfSubSMPAction; only one of this and ntf_sub_action can(should) be not null in same record ntf_sub_action_ts TEXT, -- the earliest time for the action, e.g. checks can be scheduled every X hours updated_by_supervisor INTEGER NOT NULL DEFAULT 0, -- to be checked on updates by workers to not overwrite supervisor command(state still should be updated) diff --git a/src/Simplex/Messaging/Notifications/Client.hs b/src/Simplex/Messaging/Notifications/Client.hs index 2432687b2..2e8577cce 100644 --- a/src/Simplex/Messaging/Notifications/Client.hs +++ b/src/Simplex/Messaging/Notifications/Client.hs @@ -136,15 +136,27 @@ newNtfToken deviceToken ntfServer (ntfPubKey, ntfPrivKey) ntfDhKeys ntfMode = ntfMode } -data NtfSubOrSMPAction = NtfSubAction NtfSubAction | NtfSubSMPAction NtfSubSMPAction +data NtfSubAction = NtfSubNTFAction NtfSubNTFAction | NtfSubSMPAction NtfSubSMPAction -data NtfSubAction +data NtfSubActionData = NtfSubActionData + { action :: NtfSubAction, + actionTs :: UTCTime, + ntfSubscription :: NtfSubscription + } + +data NtfSubNTFActionData = NtfSubNTFActionData + { ntfAction :: NtfSubNTFAction, + actionTs :: UTCTime, + ntfSubscription :: NtfSubscription + } + +data NtfSubNTFAction = NSACreate | NSACheck | NSADelete deriving (Show) -instance Encoding NtfSubAction where +instance Encoding NtfSubNTFAction where smpEncode = \case NSACreate -> "N" NSACheck -> "C" @@ -154,11 +166,17 @@ instance Encoding NtfSubAction where 'N' -> pure NSACreate 'C' -> pure NSACheck 'D' -> pure NSADelete - _ -> fail "bad NtfSubAction" + _ -> fail "bad NtfSubNTFAction" -instance FromField NtfSubAction where fromField = blobFieldDecoder smpDecode +instance FromField NtfSubNTFAction where fromField = blobFieldDecoder smpDecode -instance ToField NtfSubAction where toField = toField . smpEncode +instance ToField NtfSubNTFAction where toField = toField . smpEncode + +data NtfSubSMPActionData = NtfSubSMPActionData + { smpAction :: NtfSubSMPAction, + actionTs :: UTCTime, + ntfSubscription :: NtfSubscription + } data NtfSubSMPAction = NSASmpKey @@ -222,19 +240,17 @@ data NtfSubscription = NtfSubscription ntfQueueId :: Maybe NotifierId, ntfServer :: NtfServer, ntfSubId :: Maybe NtfSubscriptionId, - ntfSubStatus :: NtfAgentSubStatus, - ntfSubActionTs :: UTCTime + ntfSubStatus :: NtfAgentSubStatus } deriving (Show) -newNtfSubscription :: ConnId -> SMPServer -> Maybe NotifierId -> NtfServer -> NtfAgentSubStatus -> UTCTime -> NtfSubscription -newNtfSubscription connId smpServer ntfQueueId ntfServer ntfSubStatus ntfSubActionTs = +newNtfSubscription :: ConnId -> SMPServer -> Maybe NotifierId -> NtfServer -> NtfAgentSubStatus -> NtfSubscription +newNtfSubscription connId smpServer ntfQueueId ntfServer ntfSubStatus = NtfSubscription { connId, smpServer, ntfQueueId, ntfServer, ntfSubId = Nothing, - ntfSubStatus, - ntfSubActionTs + ntfSubStatus } From 9695786aa54d11f1103b34f3014db23accc142cd Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Thu, 30 Jun 2022 14:31:36 +0400 Subject: [PATCH 103/131] ntf: separate types into their own module (#444) --- simplexmq.cabal | 1 + src/Simplex/Messaging/Agent.hs | 2 +- src/Simplex/Messaging/Agent/Client.hs | 1 + src/Simplex/Messaging/Agent/Env/SQLite.hs | 3 +- .../Messaging/Agent/NtfSubSupervisor.hs | 2 +- src/Simplex/Messaging/Agent/Store/SQLite.hs | 2 +- src/Simplex/Messaging/Notifications/Client.hs | 193 ---------------- src/Simplex/Messaging/Notifications/Types.hs | 215 ++++++++++++++++++ 8 files changed, 221 insertions(+), 198 deletions(-) create mode 100644 src/Simplex/Messaging/Notifications/Types.hs diff --git a/simplexmq.cabal b/simplexmq.cabal index a471a2e10..977d122e0 100644 --- a/simplexmq.cabal +++ b/simplexmq.cabal @@ -64,6 +64,7 @@ library Simplex.Messaging.Notifications.Server.Store Simplex.Messaging.Notifications.Server.StoreLog Simplex.Messaging.Notifications.Transport + Simplex.Messaging.Notifications.Types Simplex.Messaging.Parsers Simplex.Messaging.Protocol Simplex.Messaging.Server diff --git a/src/Simplex/Messaging/Agent.hs b/src/Simplex/Messaging/Agent.hs index 001d551b0..f6b42d91a 100644 --- a/src/Simplex/Messaging/Agent.hs +++ b/src/Simplex/Messaging/Agent.hs @@ -96,9 +96,9 @@ import qualified Simplex.Messaging.Crypto as C import qualified Simplex.Messaging.Crypto.Ratchet as CR import Simplex.Messaging.Encoding import Simplex.Messaging.Encoding.String (StrEncoding (..)) -import Simplex.Messaging.Notifications.Client import Simplex.Messaging.Notifications.Protocol (DeviceToken, NtfRegCode (NtfRegCode), NtfTknStatus (..), NtfTokenId) import Simplex.Messaging.Notifications.Server.Push.APNS (PNMessageData (..)) +import Simplex.Messaging.Notifications.Types import Simplex.Messaging.Parsers (parse) import Simplex.Messaging.Protocol (BrokerMsg, ErrorType (AUTH), MsgBody, MsgFlags, SMPMsgMeta) import qualified Simplex.Messaging.Protocol as SMP diff --git a/src/Simplex/Messaging/Agent/Client.hs b/src/Simplex/Messaging/Agent/Client.hs index cc7e38026..7f7038be7 100644 --- a/src/Simplex/Messaging/Agent/Client.hs +++ b/src/Simplex/Messaging/Agent/Client.hs @@ -94,6 +94,7 @@ import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Encoding import Simplex.Messaging.Notifications.Client import Simplex.Messaging.Notifications.Protocol +import Simplex.Messaging.Notifications.Types import Simplex.Messaging.Protocol (BrokerMsg, ErrorType, MsgFlags (..), MsgId, NotifierId, NtfPrivateSignKey, NtfPublicVerifyKey, ProtocolServer (..), QueueId, QueueIdsKeys (..), RcvNtfPublicDhKey, SMPMsgMeta, SndPublicVerifyKey) import qualified Simplex.Messaging.Protocol as SMP import Simplex.Messaging.TMap (TMap) diff --git a/src/Simplex/Messaging/Agent/Env/SQLite.hs b/src/Simplex/Messaging/Agent/Env/SQLite.hs index 469f543e8..ae31dc4b9 100644 --- a/src/Simplex/Messaging/Agent/Env/SQLite.hs +++ b/src/Simplex/Messaging/Agent/Env/SQLite.hs @@ -3,7 +3,6 @@ {-# LANGUAGE DuplicateRecordFields #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE GADTs #-} -{-# LANGUAGE LambdaCase #-} {-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE NumericUnderscores #-} {-# LANGUAGE RankNTypes #-} @@ -39,7 +38,7 @@ import qualified Simplex.Messaging.Agent.Store.SQLite.Migrations as Migrations import Simplex.Messaging.Client import Simplex.Messaging.Client.Agent () import qualified Simplex.Messaging.Crypto as C -import Simplex.Messaging.Notifications.Client (NtfServer, NtfToken) +import Simplex.Messaging.Notifications.Types import Simplex.Messaging.TMap (TMap) import qualified Simplex.Messaging.TMap as TM import Simplex.Messaging.Transport (TLS, Transport (..)) diff --git a/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs b/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs index 76911ef13..f760bfcf7 100644 --- a/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs +++ b/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs @@ -35,8 +35,8 @@ import Simplex.Messaging.Agent.Store import Simplex.Messaging.Agent.Store.SQLite import Simplex.Messaging.Client.Agent () import qualified Simplex.Messaging.Crypto as C -import Simplex.Messaging.Notifications.Client import Simplex.Messaging.Notifications.Protocol (NtfSubStatus (..), NtfTknStatus (..), SMPQueueNtf (..)) +import Simplex.Messaging.Notifications.Types import Simplex.Messaging.Protocol import Simplex.Messaging.TMap (TMap) import qualified Simplex.Messaging.TMap as TM diff --git a/src/Simplex/Messaging/Agent/Store/SQLite.hs b/src/Simplex/Messaging/Agent/Store/SQLite.hs index 3a4d7319e..b71cfa8eb 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite.hs @@ -123,7 +123,7 @@ import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Crypto.Ratchet (RatchetX448, SkippedMsgDiff (..), SkippedMsgKeys) import Simplex.Messaging.Encoding import Simplex.Messaging.Encoding.String -import Simplex.Messaging.Notifications.Client +import Simplex.Messaging.Notifications.Types import Simplex.Messaging.Notifications.Protocol (DeviceToken (..), NtfSubscriptionId, NtfTknStatus (..), NtfTokenId, SMPQueueNtf (..)) import Simplex.Messaging.Parsers (blobFieldParser, fromTextField_) import Simplex.Messaging.Protocol (MsgBody, MsgFlags, ProtocolServer (..), RcvNtfDhSecret) diff --git a/src/Simplex/Messaging/Notifications/Client.hs b/src/Simplex/Messaging/Notifications/Client.hs index 2e8577cce..51487a618 100644 --- a/src/Simplex/Messaging/Notifications/Client.hs +++ b/src/Simplex/Messaging/Notifications/Client.hs @@ -1,29 +1,15 @@ {-# LANGUAGE DataKinds #-} -{-# LANGUAGE DuplicateRecordFields #-} {-# LANGUAGE LambdaCase #-} -{-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE OverloadedStrings #-} -{-# LANGUAGE ScopedTypeVariables #-} module Simplex.Messaging.Notifications.Client where import Control.Monad.Except import Control.Monad.Trans.Except -import qualified Data.Attoparsec.ByteString.Char8 as A -import Data.Text.Encoding (decodeLatin1, encodeUtf8) -import Data.Time (UTCTime) import Data.Word (Word16) -import Database.SQLite.Simple.FromField (FromField (..)) -import Database.SQLite.Simple.ToField (ToField (..)) -import Simplex.Messaging.Agent.Protocol (ConnId, NotificationsMode (..)) import Simplex.Messaging.Client import qualified Simplex.Messaging.Crypto as C -import Simplex.Messaging.Encoding import Simplex.Messaging.Notifications.Protocol -import Simplex.Messaging.Parsers (blobFieldDecoder, fromTextField_) -import Simplex.Messaging.Protocol (NotifierId, ProtocolServer, SMPServer) - -type NtfServer = ProtocolServer type NtfClient = ProtocolClient NtfResponse @@ -75,182 +61,3 @@ okNtfCommand cmd c pKey entId = sendNtfCommand c (Just pKey) entId cmd >>= \case NROk -> return () _ -> throwE PCEUnexpectedResponse - -data NtfTknAction - = NTARegister - | NTAVerify NtfRegCode -- code to verify token - | NTACheck - | NTADelete - deriving (Show) - -instance Encoding NtfTknAction where - smpEncode = \case - NTARegister -> "R" - NTAVerify code -> smpEncode ('V', code) - NTACheck -> "C" - NTADelete -> "D" - smpP = - A.anyChar >>= \case - 'R' -> pure NTARegister - 'V' -> NTAVerify <$> smpP - 'C' -> pure NTACheck - 'D' -> pure NTADelete - _ -> fail "bad NtfTknAction" - -instance FromField NtfTknAction where fromField = blobFieldDecoder smpDecode - -instance ToField NtfTknAction where toField = toField . smpEncode - -data NtfToken = NtfToken - { deviceToken :: DeviceToken, - ntfServer :: NtfServer, - ntfTokenId :: Maybe NtfTokenId, - -- | key used by the ntf server to verify transmissions - ntfPubKey :: C.APublicVerifyKey, - -- | key used by the ntf client to sign transmissions - ntfPrivKey :: C.APrivateSignKey, - -- | client's DH keys (to repeat registration if necessary) - ntfDhKeys :: C.KeyPair 'C.X25519, - -- | shared DH secret used to encrypt/decrypt notifications e2e - ntfDhSecret :: Maybe C.DhSecretX25519, - -- | token status - ntfTknStatus :: NtfTknStatus, - -- | pending token action and the earliest time - ntfTknAction :: Maybe NtfTknAction, - ntfMode :: NotificationsMode - } - deriving (Show) - -newNtfToken :: DeviceToken -> NtfServer -> C.ASignatureKeyPair -> C.KeyPair 'C.X25519 -> NotificationsMode -> NtfToken -newNtfToken deviceToken ntfServer (ntfPubKey, ntfPrivKey) ntfDhKeys ntfMode = - NtfToken - { deviceToken, - ntfServer, - ntfTokenId = Nothing, - ntfPubKey, - ntfPrivKey, - ntfDhKeys, - ntfDhSecret = Nothing, - ntfTknStatus = NTNew, - ntfTknAction = Just NTARegister, - ntfMode - } - -data NtfSubAction = NtfSubNTFAction NtfSubNTFAction | NtfSubSMPAction NtfSubSMPAction - -data NtfSubActionData = NtfSubActionData - { action :: NtfSubAction, - actionTs :: UTCTime, - ntfSubscription :: NtfSubscription - } - -data NtfSubNTFActionData = NtfSubNTFActionData - { ntfAction :: NtfSubNTFAction, - actionTs :: UTCTime, - ntfSubscription :: NtfSubscription - } - -data NtfSubNTFAction - = NSACreate - | NSACheck - | NSADelete - deriving (Show) - -instance Encoding NtfSubNTFAction where - smpEncode = \case - NSACreate -> "N" - NSACheck -> "C" - NSADelete -> "D" - smpP = - A.anyChar >>= \case - 'N' -> pure NSACreate - 'C' -> pure NSACheck - 'D' -> pure NSADelete - _ -> fail "bad NtfSubNTFAction" - -instance FromField NtfSubNTFAction where fromField = blobFieldDecoder smpDecode - -instance ToField NtfSubNTFAction where toField = toField . smpEncode - -data NtfSubSMPActionData = NtfSubSMPActionData - { smpAction :: NtfSubSMPAction, - actionTs :: UTCTime, - ntfSubscription :: NtfSubscription - } - -data NtfSubSMPAction - = NSASmpKey - | NSASmpDelete - deriving (Show) - -instance Encoding NtfSubSMPAction where - smpEncode = \case - NSASmpKey -> "K" - NSASmpDelete -> "D" - smpP = - A.anyChar >>= \case - 'K' -> pure NSASmpKey - 'D' -> pure NSASmpDelete - _ -> fail "bad NtfSubSMPAction" - -instance FromField NtfSubSMPAction where fromField = blobFieldDecoder smpDecode - -instance ToField NtfSubSMPAction where toField = toField . smpEncode - -data NtfAgentSubStatus - = -- | subscription started - NASNew - | -- | state after NKEY - notifier ID is assigned to queue on SMP server - NASKey - | -- | state after SNEW - subscription created on notification server - NASCreated NtfSubStatus - | -- | state after SDEL (subscription is deleted on notification server) - NASOff - | -- | state after NDEL (notifier credentials are deleted on SMP server) - -- Can only exist transiently - if subscription record was updated by notification supervisor mid worker operation, - -- and hence got updated instead of being fully deleted in the database post operation by worker - NASDeleted - deriving (Eq, Show) - -instance Encoding NtfAgentSubStatus where - smpEncode = \case - NASNew -> "NEW" - NASKey -> "KEY" - NASCreated status -> "CREATED " <> smpEncode status - NASOff -> "OFF" - NASDeleted -> "DELETED" - smpP = - A.takeTill (== ' ') >>= \case - "NEW" -> pure NASNew - "KEY" -> pure NASKey - "CREATED" -> do - _ <- A.space - NASCreated <$> smpP - "OFF" -> pure NASOff - "DELETED" -> pure NASDeleted - _ -> fail "bad NtfAgentSubStatus" - -instance FromField NtfAgentSubStatus where fromField = fromTextField_ $ either (const Nothing) Just . smpDecode . encodeUtf8 - -instance ToField NtfAgentSubStatus where toField = toField . decodeLatin1 . smpEncode - -data NtfSubscription = NtfSubscription - { connId :: ConnId, - smpServer :: SMPServer, - ntfQueueId :: Maybe NotifierId, - ntfServer :: NtfServer, - ntfSubId :: Maybe NtfSubscriptionId, - ntfSubStatus :: NtfAgentSubStatus - } - deriving (Show) - -newNtfSubscription :: ConnId -> SMPServer -> Maybe NotifierId -> NtfServer -> NtfAgentSubStatus -> NtfSubscription -newNtfSubscription connId smpServer ntfQueueId ntfServer ntfSubStatus = - NtfSubscription - { connId, - smpServer, - ntfQueueId, - ntfServer, - ntfSubId = Nothing, - ntfSubStatus - } diff --git a/src/Simplex/Messaging/Notifications/Types.hs b/src/Simplex/Messaging/Notifications/Types.hs new file mode 100644 index 000000000..69d4e30e9 --- /dev/null +++ b/src/Simplex/Messaging/Notifications/Types.hs @@ -0,0 +1,215 @@ +{-# LANGUAGE DataKinds #-} +{-# LANGUAGE DuplicateRecordFields #-} +{-# LANGUAGE LambdaCase #-} +{-# LANGUAGE NamedFieldPuns #-} +{-# LANGUAGE OverloadedStrings #-} + +module Simplex.Messaging.Notifications.Types + ( NtfServer, + NtfTknAction (..), + NtfToken (..), + newNtfToken, + NtfSubAction (..), + NtfSubActionData (..), + NtfSubNTFActionData (..), + NtfSubNTFAction (..), + NtfSubSMPActionData (..), + NtfSubSMPAction (..), + NtfAgentSubStatus (..), + NtfSubscription (..), + newNtfSubscription, + ) +where + +import qualified Data.Attoparsec.ByteString.Char8 as A +import Data.Text.Encoding (decodeLatin1, encodeUtf8) +import Data.Time (UTCTime) +import Database.SQLite.Simple.FromField (FromField (..)) +import Database.SQLite.Simple.ToField (ToField (..)) +import Simplex.Messaging.Agent.Protocol (ConnId, NotificationsMode (..)) +import qualified Simplex.Messaging.Crypto as C +import Simplex.Messaging.Encoding +import Simplex.Messaging.Notifications.Protocol +import Simplex.Messaging.Parsers (blobFieldDecoder, fromTextField_) +import Simplex.Messaging.Protocol (NotifierId, ProtocolServer, SMPServer) + +type NtfServer = ProtocolServer + +data NtfTknAction + = NTARegister + | NTAVerify NtfRegCode -- code to verify token + | NTACheck + | NTADelete + deriving (Show) + +instance Encoding NtfTknAction where + smpEncode = \case + NTARegister -> "R" + NTAVerify code -> smpEncode ('V', code) + NTACheck -> "C" + NTADelete -> "D" + smpP = + A.anyChar >>= \case + 'R' -> pure NTARegister + 'V' -> NTAVerify <$> smpP + 'C' -> pure NTACheck + 'D' -> pure NTADelete + _ -> fail "bad NtfTknAction" + +instance FromField NtfTknAction where fromField = blobFieldDecoder smpDecode + +instance ToField NtfTknAction where toField = toField . smpEncode + +data NtfToken = NtfToken + { deviceToken :: DeviceToken, + ntfServer :: NtfServer, + ntfTokenId :: Maybe NtfTokenId, + -- | key used by the ntf server to verify transmissions + ntfPubKey :: C.APublicVerifyKey, + -- | key used by the ntf client to sign transmissions + ntfPrivKey :: C.APrivateSignKey, + -- | client's DH keys (to repeat registration if necessary) + ntfDhKeys :: C.KeyPair 'C.X25519, + -- | shared DH secret used to encrypt/decrypt notifications e2e + ntfDhSecret :: Maybe C.DhSecretX25519, + -- | token status + ntfTknStatus :: NtfTknStatus, + -- | pending token action and the earliest time + ntfTknAction :: Maybe NtfTknAction, + ntfMode :: NotificationsMode + } + deriving (Show) + +newNtfToken :: DeviceToken -> NtfServer -> C.ASignatureKeyPair -> C.KeyPair 'C.X25519 -> NotificationsMode -> NtfToken +newNtfToken deviceToken ntfServer (ntfPubKey, ntfPrivKey) ntfDhKeys ntfMode = + NtfToken + { deviceToken, + ntfServer, + ntfTokenId = Nothing, + ntfPubKey, + ntfPrivKey, + ntfDhKeys, + ntfDhSecret = Nothing, + ntfTknStatus = NTNew, + ntfTknAction = Just NTARegister, + ntfMode + } + +data NtfSubAction = NtfSubNTFAction NtfSubNTFAction | NtfSubSMPAction NtfSubSMPAction + +data NtfSubActionData = NtfSubActionData + { action :: NtfSubAction, + actionTs :: UTCTime, + ntfSubscription :: NtfSubscription + } + +data NtfSubNTFActionData = NtfSubNTFActionData + { ntfAction :: NtfSubNTFAction, + actionTs :: UTCTime, + ntfSubscription :: NtfSubscription + } + +data NtfSubNTFAction + = NSACreate + | NSACheck + | NSADelete + deriving (Show) + +instance Encoding NtfSubNTFAction where + smpEncode = \case + NSACreate -> "N" + NSACheck -> "C" + NSADelete -> "D" + smpP = + A.anyChar >>= \case + 'N' -> pure NSACreate + 'C' -> pure NSACheck + 'D' -> pure NSADelete + _ -> fail "bad NtfSubNTFAction" + +instance FromField NtfSubNTFAction where fromField = blobFieldDecoder smpDecode + +instance ToField NtfSubNTFAction where toField = toField . smpEncode + +data NtfSubSMPActionData = NtfSubSMPActionData + { smpAction :: NtfSubSMPAction, + actionTs :: UTCTime, + ntfSubscription :: NtfSubscription + } + +data NtfSubSMPAction + = NSASmpKey + | NSASmpDelete + deriving (Show) + +instance Encoding NtfSubSMPAction where + smpEncode = \case + NSASmpKey -> "K" + NSASmpDelete -> "D" + smpP = + A.anyChar >>= \case + 'K' -> pure NSASmpKey + 'D' -> pure NSASmpDelete + _ -> fail "bad NtfSubSMPAction" + +instance FromField NtfSubSMPAction where fromField = blobFieldDecoder smpDecode + +instance ToField NtfSubSMPAction where toField = toField . smpEncode + +data NtfAgentSubStatus + = -- | subscription started + NASNew + | -- | state after NKEY - notifier ID is assigned to queue on SMP server + NASKey + | -- | state after SNEW - subscription created on notification server + NASCreated NtfSubStatus + | -- | state after SDEL (subscription is deleted on notification server) + NASOff + | -- | state after NDEL (notifier credentials are deleted on SMP server) + -- Can only exist transiently - if subscription record was updated by notification supervisor mid worker operation, + -- and hence got updated instead of being fully deleted in the database post operation by worker + NASDeleted + deriving (Eq, Show) + +instance Encoding NtfAgentSubStatus where + smpEncode = \case + NASNew -> "NEW" + NASKey -> "KEY" + NASCreated status -> "CREATED " <> smpEncode status + NASOff -> "OFF" + NASDeleted -> "DELETED" + smpP = + A.takeTill (== ' ') >>= \case + "NEW" -> pure NASNew + "KEY" -> pure NASKey + "CREATED" -> do + _ <- A.space + NASCreated <$> smpP + "OFF" -> pure NASOff + "DELETED" -> pure NASDeleted + _ -> fail "bad NtfAgentSubStatus" + +instance FromField NtfAgentSubStatus where fromField = fromTextField_ $ either (const Nothing) Just . smpDecode . encodeUtf8 + +instance ToField NtfAgentSubStatus where toField = toField . decodeLatin1 . smpEncode + +data NtfSubscription = NtfSubscription + { connId :: ConnId, + smpServer :: SMPServer, + ntfQueueId :: Maybe NotifierId, + ntfServer :: NtfServer, + ntfSubId :: Maybe NtfSubscriptionId, + ntfSubStatus :: NtfAgentSubStatus + } + deriving (Show) + +newNtfSubscription :: ConnId -> SMPServer -> Maybe NotifierId -> NtfServer -> NtfAgentSubStatus -> NtfSubscription +newNtfSubscription connId smpServer ntfQueueId ntfServer ntfSubStatus = + NtfSubscription + { connId, + smpServer, + ntfQueueId, + ntfServer, + ntfSubId = Nothing, + ntfSubStatus + } \ No newline at end of file From c82fae72f2e3278cf6dd7c4e95ec4d07404fa75d Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Thu, 30 Jun 2022 15:34:16 +0400 Subject: [PATCH 104/131] ntf: refactor NtfSubAction (#445) --- .../Messaging/Agent/NtfSubSupervisor.hs | 28 +++++++------- src/Simplex/Messaging/Agent/Store/SQLite.hs | 35 +++++++++--------- src/Simplex/Messaging/Notifications/Types.hs | 37 ++----------------- 3 files changed, 35 insertions(+), 65 deletions(-) diff --git a/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs b/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs index f760bfcf7..8e863d8fc 100644 --- a/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs +++ b/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs @@ -70,10 +70,10 @@ processNtfSub c (connId, cmd) = do case clientNtfCreds of Just ClientNtfCreds {notifierId} -> do let newSub = newNtfSubscription connId smpServer (Just notifierId) ntfServer NASKey - withStore' c $ \db -> createNtfSubscription db NtfSubActionData {action = NtfSubNTFAction NSACreate, actionTs = currentTime, ntfSubscription = newSub} + withStore' c $ \db -> createNtfSubscription db newSub (NtfSubNTFAction NSACreate) currentTime _ -> do let newSub = newNtfSubscription connId smpServer Nothing ntfServer NASNew - withStore' c $ \db -> createNtfSubscription db NtfSubActionData {action = NtfSubSMPAction NSASmpKey, actionTs = currentTime, ntfSubscription = newSub} + withStore' c $ \db -> createNtfSubscription db newSub (NtfSubSMPAction NSASmpKey) currentTime -- TODO optimize? -- TODO - read action in getNtfSubscription and decide which worker to create -- TODO - SMP worker can create Ntf worker on NKEY completion @@ -125,10 +125,10 @@ processNtfSub c (connId, cmd) = do runNtfWorker :: forall m. AgentMonad m => AgentClient -> NtfServer -> TMVar () -> m () runNtfWorker c srv doWork = forever $ do void . atomically $ readTMVar doWork - nextSub_ <- withStore' c (`getNextNtfSubAction` srv) + nextSub_ <- withStore' c (`getNextNtfSubNTFAction` srv) case nextSub_ of Nothing -> noWorkToDo - Just a@NtfSubNTFActionData {ntfSubscription = NtfSubscription {connId}} -> do + Just a@(NtfSubscription {connId}, _, _) -> do ri <- asks $ reconnectInterval . config withRetryInterval ri $ \loop -> processAction a @@ -142,11 +142,11 @@ runNtfWorker c srv doWork = forever $ do liftIO $ threadDelay throttle where noWorkToDo = void . atomically $ tryTakeTMVar doWork - processAction :: NtfSubNTFActionData -> m () - processAction NtfSubNTFActionData {ntfAction, actionTs, ntfSubscription = sub@NtfSubscription {connId, smpServer, ntfSubId}} = do + processAction :: (NtfSubscription, NtfSubNTFAction, NtfActionTs) -> m () + processAction (sub@NtfSubscription {connId, smpServer, ntfSubId}, action, actionTs) = do ts <- liftIO getCurrentTime unlessM (rescheduleAction doWork ts actionTs) $ - case ntfAction of + case action of NSACreate -> getNtfToken >>= \case Just tkn@NtfToken {ntfTokenId = Just tknId, ntfTknStatus = NTActive, ntfMode = NMInstant} -> do @@ -157,7 +157,7 @@ runNtfWorker c srv doWork = forever $ do -- TODO smaller retry until Active, less frequently (daily?) once Active let actionTs' = addUTCTime 30 ts withStore' c $ \db -> - updateNtfSubscription db connId NtfSubActionData {ntfSubscription = sub {ntfSubId = Just nSubId, ntfSubStatus = NASCreated NSNew}, action = NtfSubNTFAction NSACheck, actionTs = actionTs'} + updateNtfSubscription db connId sub {ntfSubId = Just nSubId, ntfSubStatus = NASCreated NSNew} (NtfSubNTFAction NSACheck) actionTs' _ -> ntfInternalError c connId "NSACreate - no notifier queue credentials" _ -> ntfInternalError c connId "NSACreate - no active token" NSACheck -> @@ -175,7 +175,7 @@ runNtfWorker c srv doWork = forever $ do (getNtfToken >>= \tkn -> forM_ tkn $ agentNtfDeleteSubscription c nSubId) `E.finally` do withStore' c $ \db -> - updateNtfSubscription db connId NtfSubActionData {ntfSubscription = sub {ntfSubId = Nothing, ntfSubStatus = NASOff}, action = NtfSubSMPAction NSASmpDelete, actionTs = ts} + updateNtfSubscription db connId sub {ntfSubId = Nothing, ntfSubStatus = NASOff} (NtfSubSMPAction NSASmpDelete) ts ns <- asks ntfSupervisor atomically $ writeTBQueue (ntfSubQ ns) (connId, NSCNtfSMPWorker smpServer) Nothing -> ntfInternalError c connId "NSADelete - no subscription ID" @@ -186,7 +186,7 @@ runNtfWorker c srv doWork = forever $ do updateSub (NASCreated toStatus) (NtfSubNTFAction NSACheck) nextCheckTs updateSub toStatus toAction actionTs' = withStore' c $ \db -> - updateNtfSubscription db connId NtfSubActionData {ntfSubscription = sub {ntfSubStatus = toStatus}, action = toAction, actionTs = actionTs'} + updateNtfSubscription db connId sub {ntfSubStatus = toStatus} toAction actionTs' runNtfSMPWorker :: forall m. AgentMonad m => AgentClient -> SMPServer -> TMVar () -> m () runNtfSMPWorker c srv doWork = forever $ do @@ -194,7 +194,7 @@ runNtfSMPWorker c srv doWork = forever $ do nextSub_ <- withStore' c (`getNextNtfSubSMPAction` srv) case nextSub_ of Nothing -> noWorkToDo - Just a@NtfSubSMPActionData {ntfSubscription = NtfSubscription {connId}} -> do + Just a@(NtfSubscription {connId}, _, _) -> do ri <- asks $ reconnectInterval . config withRetryInterval ri $ \loop -> processAction a @@ -208,8 +208,8 @@ runNtfSMPWorker c srv doWork = forever $ do liftIO $ threadDelay throttle where noWorkToDo = void . atomically $ tryTakeTMVar doWork - processAction :: NtfSubSMPActionData -> m () - processAction NtfSubSMPActionData {smpAction, actionTs, ntfSubscription = sub@NtfSubscription {connId, ntfServer}} = do + processAction :: (NtfSubscription, NtfSubSMPAction, NtfActionTs) -> m () + processAction (sub@NtfSubscription {connId, ntfServer}, smpAction, actionTs) = do ts <- liftIO getCurrentTime unlessM (rescheduleAction doWork ts actionTs) $ case smpAction of @@ -224,7 +224,7 @@ runNtfSMPWorker c srv doWork = forever $ do let rcvNtfDhSecret = C.dh' rcvNtfSrvPubDhKey rcvNtfPrivDhKey withStore' c $ \st -> do setRcvQueueNtfCreds st connId ClientNtfCreds {ntfPublicKey, ntfPrivateKey, notifierId, rcvNtfDhSecret} - updateNtfSubscription st connId NtfSubActionData {ntfSubscription = sub {ntfQueueId = Just notifierId, ntfSubStatus = NASKey}, action = NtfSubNTFAction NSACreate, actionTs = ts} + updateNtfSubscription st connId sub {ntfQueueId = Just notifierId, ntfSubStatus = NASKey} (NtfSubNTFAction NSACreate) ts ns <- asks ntfSupervisor atomically $ sendNtfSubCommand ns (connId, NSCNtfWorker ntfServer) _ -> ntfInternalError c connId "NSASmpKey - no active token" diff --git a/src/Simplex/Messaging/Agent/Store/SQLite.hs b/src/Simplex/Messaging/Agent/Store/SQLite.hs index b71cfa8eb..0dacb98d8 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite.hs @@ -79,7 +79,7 @@ module Simplex.Messaging.Agent.Store.SQLite updateNtfSubscription, setNullNtfSubscriptionAction, deleteNtfSubscription, - getNextNtfSubAction, + getNextNtfSubNTFAction, getNextNtfSubSMPAction, getActiveNtfToken, getNtfRcvQueue, @@ -123,8 +123,8 @@ import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Crypto.Ratchet (RatchetX448, SkippedMsgDiff (..), SkippedMsgKeys) import Simplex.Messaging.Encoding import Simplex.Messaging.Encoding.String -import Simplex.Messaging.Notifications.Types import Simplex.Messaging.Notifications.Protocol (DeviceToken (..), NtfSubscriptionId, NtfTknStatus (..), NtfTokenId, SMPQueueNtf (..)) +import Simplex.Messaging.Notifications.Types import Simplex.Messaging.Parsers (blobFieldParser, fromTextField_) import Simplex.Messaging.Protocol (MsgBody, MsgFlags, ProtocolServer (..), RcvNtfDhSecret) import qualified Simplex.Messaging.Protocol as SMP @@ -732,8 +732,9 @@ getNtfSubscription db connId = ntfServer = ProtocolServer ntfHost ntfPort ntfKeyHash in NtfSubscription {connId, smpServer, ntfQueueId, ntfServer, ntfSubId, ntfSubStatus} -createNtfSubscription :: DB.Connection -> NtfSubActionData -> IO () -createNtfSubscription db NtfSubActionData {action, actionTs, ntfSubscription = NtfSubscription {connId, smpServer = (SMPServer host port _), ntfQueueId, ntfServer = (SMPServer ntfHost ntfPort _), ntfSubId, ntfSubStatus}} = +createNtfSubscription :: DB.Connection -> NtfSubscription -> NtfSubAction -> NtfActionTs -> IO () +createNtfSubscription db ntfSubscription action actionTs = do + let NtfSubscription {connId, smpServer = (SMPServer host port _), ntfQueueId, ntfServer = (SMPServer ntfHost ntfPort _), ntfSubId, ntfSubStatus} = ntfSubscription DB.execute db [sql| @@ -762,8 +763,8 @@ markNtfSubscriptionForDeletion db connId ntfAction = do where (ntfSubAction, ntfSubSMPAction) = ntfSubAndSMPAction ntfAction -updateNtfSubscription :: DB.Connection -> ConnId -> NtfSubActionData -> IO () -updateNtfSubscription db connId NtfSubActionData {action, actionTs, ntfSubscription = NtfSubscription {ntfQueueId, ntfSubId, ntfSubStatus}} = do +updateNtfSubscription :: DB.Connection -> ConnId -> NtfSubscription -> NtfSubAction -> NtfActionTs -> IO () +updateNtfSubscription db connId NtfSubscription {ntfQueueId, ntfSubId, ntfSubStatus} action actionTs = do r <- maybeFirstRow fromOnly $ DB.query db "SELECT updated_by_supervisor FROM ntf_subscriptions WHERE conn_id = ?" (Only connId) forM_ r $ \updatedBySupervisor -> do updatedAt <- getCurrentTime @@ -821,9 +822,9 @@ deleteNtfSubscription db connId = do (Nothing :: Maybe SMP.NotifierId, Nothing :: Maybe NtfSubscriptionId, NASDeleted, False, updatedAt, connId) else DB.execute db "DELETE FROM ntf_subscriptions WHERE conn_id = ?" (Only connId) -getNextNtfSubAction :: DB.Connection -> NtfServer -> IO (Maybe NtfSubNTFActionData) -getNextNtfSubAction db ntfServer@(ProtocolServer ntfHost ntfPort _) = do - maybeFirstRow ntfSubscriptionData getNtfSubAction_ $>>= \a@NtfSubNTFActionData {ntfSubscription = NtfSubscription {connId}} -> do +getNextNtfSubNTFAction :: DB.Connection -> NtfServer -> IO (Maybe (NtfSubscription, NtfSubNTFAction, NtfActionTs)) +getNextNtfSubNTFAction db ntfServer@(ProtocolServer ntfHost ntfPort _) = do + maybeFirstRow ntfSubAction getNtfSubAction_ $>>= \a@(NtfSubscription {connId}, _, _) -> do DB.execute db "UPDATE ntf_subscriptions SET updated_by_supervisor = ? WHERE conn_id = ?" (False, connId) pure $ Just a where @@ -840,14 +841,14 @@ getNextNtfSubAction db ntfServer@(ProtocolServer ntfHost ntfPort _) = do LIMIT 1 |] (ntfHost, ntfPort) - ntfSubscriptionData (connId, smpHost, smpPort, smpKeyHash, ntfQueueId, ntfSubId, ntfSubStatus, actionTs, ntfAction) = + ntfSubAction (connId, smpHost, smpPort, smpKeyHash, ntfQueueId, ntfSubId, ntfSubStatus, actionTs, action) = let smpServer = SMPServer smpHost smpPort smpKeyHash ntfSubscription = NtfSubscription {connId, smpServer, ntfQueueId, ntfServer, ntfSubId, ntfSubStatus} - in NtfSubNTFActionData {ntfAction, actionTs, ntfSubscription} + in (ntfSubscription, action, actionTs) -getNextNtfSubSMPAction :: DB.Connection -> SMPServer -> IO (Maybe NtfSubSMPActionData) +getNextNtfSubSMPAction :: DB.Connection -> SMPServer -> IO (Maybe (NtfSubscription, NtfSubSMPAction, NtfActionTs)) getNextNtfSubSMPAction db smpServer@(SMPServer smpHost smpPort _) = do - maybeFirstRow ntfSubscriptionData getNtfSubAction_ $>>= \a@NtfSubSMPActionData {ntfSubscription = NtfSubscription {connId}} -> do + maybeFirstRow ntfSubAction getNtfSubAction_ $>>= \a@(NtfSubscription {connId}, _, _) -> do DB.execute db "UPDATE ntf_subscriptions SET updated_by_supervisor = ? WHERE conn_id = ?" (False, connId) pure $ Just a where @@ -864,10 +865,10 @@ getNextNtfSubSMPAction db smpServer@(SMPServer smpHost smpPort _) = do LIMIT 1 |] (smpHost, smpPort) - ntfSubscriptionData (connId, ntfHost, ntfPort, ntfKeyHash, ntfQueueId, ntfSubId, ntfSubStatus, actionTs, smpAction) = + ntfSubAction (connId, ntfHost, ntfPort, ntfKeyHash, ntfQueueId, ntfSubId, ntfSubStatus, actionTs, action) = let ntfServer = ProtocolServer ntfHost ntfPort ntfKeyHash ntfSubscription = NtfSubscription {connId, smpServer, ntfQueueId, ntfServer, ntfSubId, ntfSubStatus} - in NtfSubSMPActionData {ntfSubscription, smpAction, actionTs} + in (ntfSubscription, action, actionTs) getActiveNtfToken :: DB.Connection -> IO (Maybe NtfToken) getActiveNtfToken db = @@ -1314,5 +1315,5 @@ randomId :: TVar ChaChaDRG -> Int -> IO ByteString randomId gVar n = U.encode <$> (atomically . stateTVar gVar $ randomBytesGenerate n) ntfSubAndSMPAction :: NtfSubAction -> (Maybe NtfSubNTFAction, Maybe NtfSubSMPAction) -ntfSubAndSMPAction (NtfSubNTFAction nsa) = (Just nsa, Nothing) -ntfSubAndSMPAction (NtfSubSMPAction nsa) = (Nothing, Just nsa) +ntfSubAndSMPAction (NtfSubNTFAction action) = (Just action, Nothing) +ntfSubAndSMPAction (NtfSubSMPAction action) = (Nothing, Just action) diff --git a/src/Simplex/Messaging/Notifications/Types.hs b/src/Simplex/Messaging/Notifications/Types.hs index 69d4e30e9..ab6ac13f7 100644 --- a/src/Simplex/Messaging/Notifications/Types.hs +++ b/src/Simplex/Messaging/Notifications/Types.hs @@ -4,22 +4,7 @@ {-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE OverloadedStrings #-} -module Simplex.Messaging.Notifications.Types - ( NtfServer, - NtfTknAction (..), - NtfToken (..), - newNtfToken, - NtfSubAction (..), - NtfSubActionData (..), - NtfSubNTFActionData (..), - NtfSubNTFAction (..), - NtfSubSMPActionData (..), - NtfSubSMPAction (..), - NtfAgentSubStatus (..), - NtfSubscription (..), - newNtfSubscription, - ) -where +module Simplex.Messaging.Notifications.Types where import qualified Data.Attoparsec.ByteString.Char8 as A import Data.Text.Encoding (decodeLatin1, encodeUtf8) @@ -97,17 +82,7 @@ newNtfToken deviceToken ntfServer (ntfPubKey, ntfPrivKey) ntfDhKeys ntfMode = data NtfSubAction = NtfSubNTFAction NtfSubNTFAction | NtfSubSMPAction NtfSubSMPAction -data NtfSubActionData = NtfSubActionData - { action :: NtfSubAction, - actionTs :: UTCTime, - ntfSubscription :: NtfSubscription - } - -data NtfSubNTFActionData = NtfSubNTFActionData - { ntfAction :: NtfSubNTFAction, - actionTs :: UTCTime, - ntfSubscription :: NtfSubscription - } +type NtfActionTs = UTCTime data NtfSubNTFAction = NSACreate @@ -131,12 +106,6 @@ instance FromField NtfSubNTFAction where fromField = blobFieldDecoder smpDecode instance ToField NtfSubNTFAction where toField = toField . smpEncode -data NtfSubSMPActionData = NtfSubSMPActionData - { smpAction :: NtfSubSMPAction, - actionTs :: UTCTime, - ntfSubscription :: NtfSubscription - } - data NtfSubSMPAction = NSASmpKey | NSASmpDelete @@ -212,4 +181,4 @@ newNtfSubscription connId smpServer ntfQueueId ntfServer ntfSubStatus = ntfServer, ntfSubId = Nothing, ntfSubStatus - } \ No newline at end of file + } From f6a321e198ca9d5b2e828ed102a882cfd4ec6533 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Thu, 30 Jun 2022 16:04:01 +0100 Subject: [PATCH 105/131] handle SMP errors in ntf subscriber (#441) --- src/Simplex/Messaging/Notifications/Server.hs | 50 ++++++++++++------- .../Messaging/Notifications/Server/Env.hs | 16 +++--- 2 files changed, 42 insertions(+), 24 deletions(-) diff --git a/src/Simplex/Messaging/Notifications/Server.hs b/src/Simplex/Messaging/Notifications/Server.hs index 481a0ef15..f6d3dbf37 100644 --- a/src/Simplex/Messaging/Notifications/Server.hs +++ b/src/Simplex/Messaging/Notifications/Server.hs @@ -38,8 +38,9 @@ import qualified Simplex.Messaging.TMap as TM import Simplex.Messaging.Transport (ATransport (..), THandle (..), TProxy, Transport (..)) import Simplex.Messaging.Transport.Server (runTransportServer) import Simplex.Messaging.Util +import System.Mem.Weak (deRefWeak) import UnliftIO (IOMode (..), async, uninterruptibleCancel) -import UnliftIO.Concurrent (forkFinally, forkIO, threadDelay) +import UnliftIO.Concurrent (forkIO, killThread, mkWeakThreadId, threadDelay) import UnliftIO.Exception import UnliftIO.STM @@ -57,8 +58,7 @@ ntfServer NtfServerConfig {transports} started = do ps <- asks pushServer subs <- readTVarIO =<< asks (subscriptions . store) void . forkIO $ resubscribe s subs - raceAny_ (ntfSubscriber s : ntfPush ps : map runServer transports) - `finally` withNtfLog closeStoreLog + raceAny_ (ntfSubscriber s : ntfPush ps : map runServer transports) `finally` stopServer where runServer :: (ServiceName, ATransport) -> m () runServer (tcpPort, ATransport t) = do @@ -72,6 +72,11 @@ ntfServer NtfServerConfig {transports} started = do Right th -> runNtfClientTransport th Left _ -> pure () + stopServer :: m () + stopServer = do + withNtfLog closeStoreLog + asks (smpSubscribers . subscriber) >>= readTVarIO >>= mapM_ (\SMPSubscriber {subThreadId} -> readTVarIO subThreadId >>= mapM_ (liftIO . deRefWeak >=> mapM_ killThread)) + resubscribe :: (MonadUnliftIO m, MonadReader NtfEnv m) => NtfSubscriber -> Map NtfSubscriptionId NtfSubData -> m () resubscribe NtfSubscriber {newSubQ} subs = do d <- asks $ resubscribeDelay . config @@ -90,34 +95,43 @@ ntfSubscriber NtfSubscriber {smpSubscribers, newSubQ, smpAgent = ca@SMPClientAge atomically (readTBQueue newSubQ) >>= \case sub@(NtfSub NtfSubData {smpQueue = SMPQueueNtf {smpServer}}) -> do SMPSubscriber {newSubQ = subscriberSubQ} <- getSMPSubscriber smpServer - atomically $ writeTBQueue subscriberSubQ sub + atomically $ writeTQueue subscriberSubQ sub getSMPSubscriber :: SMPServer -> m SMPSubscriber getSMPSubscriber smpServer = atomically (TM.lookup smpServer smpSubscribers) >>= maybe createSMPSubscriber pure where createSMPSubscriber = do - qSize <- asks $ subQSize . config - newSubscriber <- atomically $ newSMPSubscriber qSize - atomically $ TM.insert smpServer newSubscriber smpSubscribers - _ <- runSMPSubscriber newSubscriber `forkFinally` \_ -> atomically (TM.delete smpServer smpSubscribers >> failSubscriptions newSubscriber) - pure newSubscriber - -- TODO mark subscriptions as failed - failSubscriptions _ = pure () + sub@SMPSubscriber {subThreadId} <- atomically newSMPSubscriber + atomically $ TM.insert smpServer sub smpSubscribers + tId <- mkWeakThreadId =<< forkIO (runSMPSubscriber sub) + atomically . writeTVar subThreadId $ Just tId + pure sub runSMPSubscriber :: SMPSubscriber -> m () runSMPSubscriber SMPSubscriber {newSubQ = subscriberSubQ} = forever $ - atomically (peekTBQueue subscriberSubQ) >>= \case + atomically (peekTQueue subscriberSubQ) >>= \case NtfSub NtfSubData {smpQueue, notifierKey} -> do updateSubStatus smpQueue NSPending let SMPQueueNtf {smpServer, notifierId} = smpQueue liftIO (runExceptT $ subscribeQueue ca smpServer ((SPNotifier, notifierId), notifierKey)) >>= \case - Right _ -> do - updateSubStatus smpQueue NSActive - _ <- atomically $ readTBQueue subscriberSubQ - pure () - Left _e -> pure () + Right _ -> update smpQueue NSActive + Left err -> case err of + PCEProtocolError AUTH -> update smpQueue NSSMPAuth + PCEProtocolError e -> update smpQueue $ NSSMPErr e + PCEIOError e -> log' $ "IOError " <> T.pack (show e) + PCEResponseError e -> log' $ "ResponseError " <> T.pack (show e) + PCEUnexpectedResponse -> log' "UnexpectedResponse" + PCETransportError e -> log' $ "TransportError " <> T.pack (show e) + PCESignatureError e -> log' $ "SignatureError " <> T.pack (show e) + PCEResponseTimeout -> pure () + PCENetworkError -> pure () + where + update smpQueue status = do + updateSubStatus smpQueue status + void . atomically $ readTQueue subscriberSubQ + log' e = logError $ "SMPSubscriber subscribeQueue " <> e receiveSMP :: m () receiveSMP = forever $ do @@ -161,7 +175,7 @@ ntfSubscriber NtfSubscriber {smpSubscribers, newSubQ, smpAgent = ca@SMPClientAge PCEIOError e -> logErr e _ -> pure () where - logErr e = logError (T.pack $ "ntfSubscriber receiveAgent error: " <> show e) + logErr e = logError $ "ntfSubscriber receiveAgent error: " <> T.pack (show e) updateSubStatus smpQueue status = do st <- asks store diff --git a/src/Simplex/Messaging/Notifications/Server/Env.hs b/src/Simplex/Messaging/Notifications/Server/Env.hs index f1f59ad06..a6f08b682 100644 --- a/src/Simplex/Messaging/Notifications/Server/Env.hs +++ b/src/Simplex/Messaging/Notifications/Server/Env.hs @@ -7,6 +7,7 @@ module Simplex.Messaging.Notifications.Server.Env where +import Control.Concurrent (ThreadId) import Control.Concurrent.Async (Async) import Control.Monad.IO.Unlift import Crypto.Random @@ -30,6 +31,7 @@ import qualified Simplex.Messaging.TMap as TM import Simplex.Messaging.Transport (ATransport) import Simplex.Messaging.Transport.Server (loadFingerprint, loadTLSServerParams) import System.IO (IOMode (..)) +import System.Mem.Weak (Weak) import UnliftIO.STM data NtfServerConfig = NtfServerConfig @@ -93,14 +95,16 @@ newNtfSubscriber qSize smpAgentCfg = do smpAgent <- newSMPClientAgent smpAgentCfg pure NtfSubscriber {smpSubscribers, newSubQ, smpAgent} -newtype SMPSubscriber = SMPSubscriber - { newSubQ :: TBQueue (NtfEntityRec 'Subscription) +data SMPSubscriber = SMPSubscriber + { newSubQ :: TQueue (NtfEntityRec 'Subscription), + subThreadId :: TVar (Maybe (Weak ThreadId)) } -newSMPSubscriber :: Natural -> STM SMPSubscriber -newSMPSubscriber qSize = do - newSubQ <- newTBQueue qSize - pure SMPSubscriber {newSubQ} +newSMPSubscriber :: STM SMPSubscriber +newSMPSubscriber = do + newSubQ <- newTQueue + subThreadId <- newTVar Nothing + pure SMPSubscriber {newSubQ, subThreadId} data NtfPushServer = NtfPushServer { pushQ :: TBQueue (NtfTknData, PushNotification), From c3012173e346b06b5625de5268dc157e74d29fca Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Thu, 30 Jun 2022 20:18:12 +0400 Subject: [PATCH 106/131] ntf: recover from failed/in-deletion subscriptions (#446) * ntf: recover from failed/in-deletion subscriptions * check NASNew status on subscription recover * carryOnWithDeletion * logs * SCHK --- .../Messaging/Agent/NtfSubSupervisor.hs | 81 ++++++++++++------- src/Simplex/Messaging/Agent/Store/SQLite.hs | 41 +++++++--- src/Simplex/Messaging/Notifications/Server.hs | 4 +- src/Simplex/Messaging/Notifications/Types.hs | 11 +++ src/Simplex/Messaging/Util.hs | 6 ++ 5 files changed, 103 insertions(+), 40 deletions(-) diff --git a/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs b/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs index 8e863d8fc..4decd8c93 100644 --- a/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs +++ b/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs @@ -18,6 +18,7 @@ where import Control.Concurrent.Async (Async, uninterruptibleCancel) import Control.Concurrent.STM (stateTVar) +import Control.Logger.Simple (logInfo) import Control.Monad import Control.Monad.Except import Control.Monad.IO.Unlift (MonadUnliftIO) @@ -40,7 +41,7 @@ import Simplex.Messaging.Notifications.Types import Simplex.Messaging.Protocol import Simplex.Messaging.TMap (TMap) import qualified Simplex.Messaging.TMap as TM -import Simplex.Messaging.Util (unlessM) +import Simplex.Messaging.Util (tshow, unlessM) import System.Random (randomR) import UnliftIO (async) import UnliftIO.Concurrent (forkIO, threadDelay) @@ -56,14 +57,17 @@ runNtfSupervisor c = forever $ do Right _ -> return () processNtfSub :: forall m. AgentMonad m => AgentClient -> (ConnId, NtfSupervisorCommand) -> m () -processNtfSub c (connId, cmd) = do +processNtfSub c@AgentClient {subQ} (connId, cmd) = do + logInfo $ "processNtfSub - connId = " <> tshow connId <> " - cmd = " <> tshow cmd ntfServer_ <- getNtfServer c + ts <- liftIO getCurrentTime case cmd of NSCCreate -> do (sub_, RcvQueue {server = smpServer, clientNtfCreds}) <- withStore c $ \db -> runExceptT $ do sub_ <- liftIO $ getNtfSubscription db connId q <- ExceptT $ getRcvQueue db connId pure (sub_, q) + logInfo $ "processNtfSub, NSCCreate - sub_ = " <> tshow sub_ case (sub_, ntfServer_) of (Nothing, Just ntfServer) -> do currentTime <- liftIO getCurrentTime @@ -79,27 +83,39 @@ processNtfSub c (connId, cmd) = do -- TODO - SMP worker can create Ntf worker on NKEY completion addNtfSMPWorker smpServer addNtfWorker ntfServer - (Just _, Just ntfServer) -> do - -- TODO subscription may have to be updated depending on current state: - -- TODO - e.g., if it was previously marked for deletion action has to be updated - -- TODO - should action depend on subscription status or always be NSASmpKey (NSACreate if notifierId exists) - -- TODO in case worker is currently deleting it? When deleting worker should check for updated_by_supervisor - -- TODO and if it is set perform update instead of delete. If worker was not deleting it yet it should - -- TODO idempotently replay commands. + (Just (sub@NtfSubscription {ntfSubStatus}, action), Just ntfServer) -> do + case action of + -- action was set to NULL after worker internal error + Nothing -> resetSubscription + Just (a, _) -> + -- subscription was marked for deletion + when (isDeleteNtfSubAction a) $ + if ntfSubStatus == NASNew || ntfSubStatus == NASOff || ntfSubStatus == NASDeleted + then resetSubscription + else withStore' c $ \db -> + supervisorUpdateNtfSubscription db sub {ntfServer} (NtfSubNTFAction NSACreate) ts addNtfSMPWorker smpServer addNtfWorker ntfServer - _ -> pure () -- error - notification server not configured + where + resetSubscription :: m () + resetSubscription = withStore' c $ \db -> + supervisorUpdateNtfSubscription db sub {ntfQueueId = Nothing, ntfServer, ntfSubId = Nothing, ntfSubStatus = NASNew} (NtfSubSMPAction NSASmpKey) ts + _ -> pure () -- err "NSCCreate - notification server not configured" NSCDelete -> do - withStore' c $ \db -> markNtfSubscriptionForDeletion db connId (NtfSubNTFAction NSADelete) - case ntfServer_ of - (Just ntfServer) -> addNtfWorker ntfServer - _ -> pure () + sub_ <- withStore' c $ \db -> do + supervisorUpdateNtfSubAction db connId (NtfSubNTFAction NSADelete) ts + getNtfSubscription db connId + logInfo $ "processNtfSub, NSCDelete - sub_ = " <> tshow sub_ + case sub_ of + (Just (NtfSubscription {ntfServer}, _)) -> addNtfWorker ntfServer + _ -> pure () -- err "NSCDelete - no subscription" NSCSmpDelete -> do withStore' c (`getRcvQueue` connId) >>= \case - Right RcvQueue {server = smpServer} -> do - withStore' c $ \db -> markNtfSubscriptionForDeletion db connId (NtfSubSMPAction NSASmpDelete) + Right rq@RcvQueue {server = smpServer} -> do + logInfo $ "processNtfSub, NSCSmpDelete - rq = " <> tshow rq + withStore' c $ \db -> supervisorUpdateNtfSubAction db connId (NtfSubSMPAction NSASmpDelete) ts addNtfSMPWorker smpServer - Left _ -> pure () + _ -> err "NSCSmpDelete - no rcv queue" NSCNtfWorker ntfServer -> addNtfWorker ntfServer NSCNtfSMPWorker smpServer -> @@ -121,18 +137,22 @@ processNtfSub c (connId, cmd) = do atomically $ TM.insert srv (doWork, worker) ws Just (doWork, _) -> void . atomically $ tryPutTMVar doWork () + err :: String -> m () + err internalErrStr = atomically $ writeTBQueue subQ ("", connId, AP.ERR $ AP.INTERNAL internalErrStr) runNtfWorker :: forall m. AgentMonad m => AgentClient -> NtfServer -> TMVar () -> m () runNtfWorker c srv doWork = forever $ do void . atomically $ readTMVar doWork nextSub_ <- withStore' c (`getNextNtfSubNTFAction` srv) + logInfo $ "runNtfWorker, nextSub_ " <> tshow nextSub_ case nextSub_ of Nothing -> noWorkToDo Just a@(NtfSubscription {connId}, _, _) -> do ri <- asks $ reconnectInterval . config withRetryInterval ri $ \loop -> processAction a - `catchError` ( \e -> + `catchError` ( \e -> do + logInfo $ "runNtfWorker, error " <> tshow e case e of BROKER NETWORK -> loop BROKER TIMEOUT -> loop @@ -157,7 +177,7 @@ runNtfWorker c srv doWork = forever $ do -- TODO smaller retry until Active, less frequently (daily?) once Active let actionTs' = addUTCTime 30 ts withStore' c $ \db -> - updateNtfSubscription db connId sub {ntfSubId = Just nSubId, ntfSubStatus = NASCreated NSNew} (NtfSubNTFAction NSACheck) actionTs' + updateNtfSubscription db sub {ntfSubId = Just nSubId, ntfSubStatus = NASCreated NSNew} (NtfSubNTFAction NSACheck) actionTs' _ -> ntfInternalError c connId "NSACreate - no notifier queue credentials" _ -> ntfInternalError c connId "NSACreate - no active token" NSACheck -> @@ -173,12 +193,15 @@ runNtfWorker c srv doWork = forever $ do NSADelete -> case ntfSubId of Just nSubId -> (getNtfToken >>= \tkn -> forM_ tkn $ agentNtfDeleteSubscription c nSubId) - `E.finally` do - withStore' c $ \db -> - updateNtfSubscription db connId sub {ntfSubId = Nothing, ntfSubStatus = NASOff} (NtfSubSMPAction NSASmpDelete) ts - ns <- asks ntfSupervisor - atomically $ writeTBQueue (ntfSubQ ns) (connId, NSCNtfSMPWorker smpServer) - Nothing -> ntfInternalError c connId "NSADelete - no subscription ID" + `E.finally` carryOnWithDeletion + Nothing -> carryOnWithDeletion + where + carryOnWithDeletion :: m () + carryOnWithDeletion = do + withStore' c $ \db -> + updateNtfSubscription db sub {ntfSubId = Nothing, ntfSubStatus = NASOff} (NtfSubSMPAction NSASmpDelete) ts + ns <- asks ntfSupervisor + atomically $ writeTBQueue (ntfSubQ ns) (connId, NSCNtfSMPWorker smpServer) where updateSubNextCheck ts toStatus = do checkInterval <- asks $ ntfSubCheckInterval . config @@ -186,19 +209,21 @@ runNtfWorker c srv doWork = forever $ do updateSub (NASCreated toStatus) (NtfSubNTFAction NSACheck) nextCheckTs updateSub toStatus toAction actionTs' = withStore' c $ \db -> - updateNtfSubscription db connId sub {ntfSubStatus = toStatus} toAction actionTs' + updateNtfSubscription db sub {ntfSubStatus = toStatus} toAction actionTs' runNtfSMPWorker :: forall m. AgentMonad m => AgentClient -> SMPServer -> TMVar () -> m () runNtfSMPWorker c srv doWork = forever $ do void . atomically $ readTMVar doWork nextSub_ <- withStore' c (`getNextNtfSubSMPAction` srv) + logInfo $ "runNtfSMPWorker, nextSub_ " <> tshow nextSub_ case nextSub_ of Nothing -> noWorkToDo Just a@(NtfSubscription {connId}, _, _) -> do ri <- asks $ reconnectInterval . config withRetryInterval ri $ \loop -> processAction a - `catchError` ( \e -> + `catchError` ( \e -> do + logInfo $ "runNtfSMPWorker, error " <> tshow e case e of BROKER NETWORK -> loop BROKER TIMEOUT -> loop @@ -224,7 +249,7 @@ runNtfSMPWorker c srv doWork = forever $ do let rcvNtfDhSecret = C.dh' rcvNtfSrvPubDhKey rcvNtfPrivDhKey withStore' c $ \st -> do setRcvQueueNtfCreds st connId ClientNtfCreds {ntfPublicKey, ntfPrivateKey, notifierId, rcvNtfDhSecret} - updateNtfSubscription st connId sub {ntfQueueId = Just notifierId, ntfSubStatus = NASKey} (NtfSubNTFAction NSACreate) ts + updateNtfSubscription st sub {ntfQueueId = Just notifierId, ntfSubStatus = NASKey} (NtfSubNTFAction NSACreate) ts ns <- asks ntfSupervisor atomically $ sendNtfSubCommand ns (connId, NSCNtfWorker ntfServer) _ -> ntfInternalError c connId "NSASmpKey - no active token" diff --git a/src/Simplex/Messaging/Agent/Store/SQLite.hs b/src/Simplex/Messaging/Agent/Store/SQLite.hs index 0dacb98d8..c4b92f2d5 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite.hs @@ -75,7 +75,8 @@ module Simplex.Messaging.Agent.Store.SQLite -- Notification subscription persistence getNtfSubscription, createNtfSubscription, - markNtfSubscriptionForDeletion, + supervisorUpdateNtfSubscription, + supervisorUpdateNtfSubAction, updateNtfSubscription, setNullNtfSubscriptionAction, deleteNtfSubscription, @@ -712,14 +713,14 @@ removeNtfToken db NtfToken {deviceToken = DeviceToken provider token, ntfServer |] (provider, token, host, port) -getNtfSubscription :: DB.Connection -> ConnId -> IO (Maybe NtfSubscription) +getNtfSubscription :: DB.Connection -> ConnId -> IO (Maybe (NtfSubscription, Maybe (NtfSubAction, NtfActionTs))) getNtfSubscription db connId = maybeFirstRow ntfSubscription $ DB.query db [sql| SELECT s.host, s.port, s.key_hash, ns.ntf_host, ns.ntf_port, ns.ntf_key_hash, - nsb.smp_ntf_id, nsb.ntf_sub_id, nsb.ntf_sub_status + nsb.smp_ntf_id, nsb.ntf_sub_id, nsb.ntf_sub_status, nsb.ntf_sub_action, nsb.ntf_sub_smp_action, nsb.ntf_sub_action_ts FROM ntf_subscriptions nsb JOIN servers s ON s.host = nsb.smp_host AND s.port = nsb.smp_port JOIN ntf_servers ns USING (ntf_host, ntf_port) @@ -727,10 +728,14 @@ getNtfSubscription db connId = |] (Only connId) where - ntfSubscription (smpHost, smpPort, smpKeyHash, ntfHost, ntfPort, ntfKeyHash, ntfQueueId, ntfSubId, ntfSubStatus) = + ntfSubscription (smpHost, smpPort, smpKeyHash, ntfHost, ntfPort, ntfKeyHash, ntfQueueId, ntfSubId, ntfSubStatus, ntfAction_, smpAction_, actionTs_) = let smpServer = SMPServer smpHost smpPort smpKeyHash ntfServer = ProtocolServer ntfHost ntfPort ntfKeyHash - in NtfSubscription {connId, smpServer, ntfQueueId, ntfServer, ntfSubId, ntfSubStatus} + action = case (ntfAction_, smpAction_, actionTs_) of + (Just ntfAction, Nothing, Just actionTs) -> Just (NtfSubNTFAction ntfAction, actionTs) + (Nothing, Just smpAction, Just actionTs) -> Just (NtfSubSMPAction smpAction, actionTs) + _ -> Nothing + in (NtfSubscription {connId, smpServer, ntfQueueId, ntfServer, ntfSubId, ntfSubStatus}, action) createNtfSubscription :: DB.Connection -> NtfSubscription -> NtfSubAction -> NtfActionTs -> IO () createNtfSubscription db ntfSubscription action actionTs = do @@ -749,8 +754,22 @@ createNtfSubscription db ntfSubscription action actionTs = do where (ntfSubAction, ntfSubSMPAction) = ntfSubAndSMPAction action -markNtfSubscriptionForDeletion :: DB.Connection -> ConnId -> NtfSubAction -> IO () -markNtfSubscriptionForDeletion db connId ntfAction = do +supervisorUpdateNtfSubscription :: DB.Connection -> NtfSubscription -> NtfSubAction -> NtfActionTs -> IO () +supervisorUpdateNtfSubscription db NtfSubscription {connId, ntfQueueId, ntfServer = (ProtocolServer ntfHost ntfPort _), ntfSubId, ntfSubStatus} action actionTs = do + updatedAt <- getCurrentTime + DB.execute + db + [sql| + UPDATE ntf_subscriptions + SET smp_ntf_id = ?, ntf_host = ?, ntf_port = ?, ntf_sub_id = ?, ntf_sub_status = ?, ntf_sub_action = ?, ntf_sub_smp_action = ?, ntf_sub_action_ts = ?, updated_by_supervisor = ?, updated_at = ? + WHERE conn_id = ? + |] + ((ntfQueueId, ntfHost, ntfPort, ntfSubId) :. (ntfSubStatus, ntfSubAction, ntfSubSMPAction, actionTs, True, updatedAt, connId)) + where + (ntfSubAction, ntfSubSMPAction) = ntfSubAndSMPAction action + +supervisorUpdateNtfSubAction :: DB.Connection -> ConnId -> NtfSubAction -> NtfActionTs -> IO () +supervisorUpdateNtfSubAction db connId action actionTs = do updatedAt <- getCurrentTime DB.execute db @@ -759,12 +778,12 @@ markNtfSubscriptionForDeletion db connId ntfAction = do SET ntf_sub_action = ?, ntf_sub_smp_action = ?, ntf_sub_action_ts = ?, updated_by_supervisor = ?, updated_at = ? WHERE conn_id = ? |] - (ntfSubAction, ntfSubSMPAction, updatedAt, True, updatedAt, connId) + (ntfSubAction, ntfSubSMPAction, actionTs, True, updatedAt, connId) where - (ntfSubAction, ntfSubSMPAction) = ntfSubAndSMPAction ntfAction + (ntfSubAction, ntfSubSMPAction) = ntfSubAndSMPAction action -updateNtfSubscription :: DB.Connection -> ConnId -> NtfSubscription -> NtfSubAction -> NtfActionTs -> IO () -updateNtfSubscription db connId NtfSubscription {ntfQueueId, ntfSubId, ntfSubStatus} action actionTs = do +updateNtfSubscription :: DB.Connection -> NtfSubscription -> NtfSubAction -> NtfActionTs -> IO () +updateNtfSubscription db NtfSubscription {connId, ntfQueueId, ntfSubId, ntfSubStatus} action actionTs = do r <- maybeFirstRow fromOnly $ DB.query db "SELECT updated_by_supervisor FROM ntf_subscriptions WHERE conn_id = ?" (Only connId) forM_ r $ \updatedBySupervisor -> do updatedAt <- getCurrentTime diff --git a/src/Simplex/Messaging/Notifications/Server.hs b/src/Simplex/Messaging/Notifications/Server.hs index f6d3dbf37..bbef83255 100644 --- a/src/Simplex/Messaging/Notifications/Server.hs +++ b/src/Simplex/Messaging/Notifications/Server.hs @@ -440,7 +440,9 @@ client NtfServerClient {rcvQ, sndQ} NtfSubscriber {newSubQ, smpAgent = ca} NtfPu if notifierKey == registeredNKey then NRSubId subId else NRErr AUTH - SCHK -> pure $ NRSub status + SCHK -> do + logDebug "SCHK" + pure $ NRSub status SDEL -> do logDebug "SDEL" st <- asks store diff --git a/src/Simplex/Messaging/Notifications/Types.hs b/src/Simplex/Messaging/Notifications/Types.hs index ab6ac13f7..d679d00f2 100644 --- a/src/Simplex/Messaging/Notifications/Types.hs +++ b/src/Simplex/Messaging/Notifications/Types.hs @@ -81,6 +81,17 @@ newNtfToken deviceToken ntfServer (ntfPubKey, ntfPrivKey) ntfDhKeys ntfMode = } data NtfSubAction = NtfSubNTFAction NtfSubNTFAction | NtfSubSMPAction NtfSubSMPAction + deriving (Show) + +isDeleteNtfSubAction :: NtfSubAction -> Bool +isDeleteNtfSubAction = \case + NtfSubNTFAction a -> case a of + NSACreate -> False + NSACheck -> False + NSADelete -> True + NtfSubSMPAction a -> case a of + NSASmpKey -> False + NSASmpDelete -> True type NtfActionTs = UTCTime diff --git a/src/Simplex/Messaging/Util.hs b/src/Simplex/Messaging/Util.hs index 5c0876196..c1e4146d6 100644 --- a/src/Simplex/Messaging/Util.hs +++ b/src/Simplex/Messaging/Util.hs @@ -10,6 +10,8 @@ import Control.Monad.Trans.Except import Data.Bifunctor (first) import Data.ByteString.Char8 (ByteString) import qualified Data.ByteString.Char8 as B +import Data.Text (Text) +import qualified Data.Text as T import UnliftIO.Async raceAny_ :: MonadUnliftIO m => [m a] -> m () @@ -32,6 +34,10 @@ bshow :: Show a => a -> ByteString bshow = B.pack . show {-# INLINE bshow #-} +tshow :: Show a => a -> Text +tshow = T.pack . show +{-# INLINE tshow #-} + maybeWord :: (a -> ByteString) -> Maybe a -> ByteString maybeWord f = maybe "" $ B.cons ' ' . f {-# INLINE maybeWord #-} From 16f5fdeeb09bcd2dfac2ecc52ebe6e10701bdff4 Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Fri, 1 Jul 2022 12:18:01 +0400 Subject: [PATCH 107/131] ntf: delete notifier creds on NSASmpDelete (#447) * ntf: delete notifier creds on NSASmpDelete * fix --- src/Simplex/Messaging/Agent/NtfSubSupervisor.hs | 10 ++++++---- src/Simplex/Messaging/Agent/Store/SQLite.hs | 10 +++++++--- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs b/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs index 4decd8c93..166c21486 100644 --- a/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs +++ b/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs @@ -247,14 +247,16 @@ runNtfSMPWorker c srv doWork = forever $ do (rcvNtfPubDhKey, rcvNtfPrivDhKey) <- liftIO C.generateKeyPair' (notifierId, rcvNtfSrvPubDhKey) <- enableQueueNotifications c rq ntfPublicKey rcvNtfPubDhKey let rcvNtfDhSecret = C.dh' rcvNtfSrvPubDhKey rcvNtfPrivDhKey - withStore' c $ \st -> do - setRcvQueueNtfCreds st connId ClientNtfCreds {ntfPublicKey, ntfPrivateKey, notifierId, rcvNtfDhSecret} - updateNtfSubscription st sub {ntfQueueId = Just notifierId, ntfSubStatus = NASKey} (NtfSubNTFAction NSACreate) ts + withStore' c $ \db -> do + setRcvQueueNtfCreds db connId $ Just ClientNtfCreds {ntfPublicKey, ntfPrivateKey, notifierId, rcvNtfDhSecret} + updateNtfSubscription db sub {ntfQueueId = Just notifierId, ntfSubStatus = NASKey} (NtfSubNTFAction NSACreate) ts ns <- asks ntfSupervisor atomically $ sendNtfSubCommand ns (connId, NSCNtfWorker ntfServer) _ -> ntfInternalError c connId "NSASmpKey - no active token" NSASmpDelete -> do - rq_ <- withStore' c (`getRcvQueue` connId) + rq_ <- withStore' c $ \db -> do + setRcvQueueNtfCreds db connId Nothing + getRcvQueue db connId forM_ rq_ $ \rq -> disableQueueNotifications c rq withStore' c $ \db -> deleteNtfSubscription db connId diff --git a/src/Simplex/Messaging/Agent/Store/SQLite.hs b/src/Simplex/Messaging/Agent/Store/SQLite.hs index c4b92f2d5..bd28f365f 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite.hs @@ -352,8 +352,8 @@ getRcvQueue :: DB.Connection -> ConnId -> IO (Either StoreError RcvQueue) getRcvQueue db connId = maybe (Left SEConnNotFound) Right <$> getRcvQueueByConnId_ db connId -setRcvQueueNtfCreds :: DB.Connection -> ConnId -> ClientNtfCreds -> IO () -setRcvQueueNtfCreds db connId ClientNtfCreds {ntfPublicKey, ntfPrivateKey, notifierId, rcvNtfDhSecret} = +setRcvQueueNtfCreds :: DB.Connection -> ConnId -> Maybe ClientNtfCreds -> IO () +setRcvQueueNtfCreds db connId clientNtfCreds = DB.execute db [sql| @@ -361,7 +361,11 @@ setRcvQueueNtfCreds db connId ClientNtfCreds {ntfPublicKey, ntfPrivateKey, notif SET ntf_public_key = ?, ntf_private_key = ?, ntf_id = ?, rcv_ntf_dh_secret = ? WHERE conn_id = ? |] - (ntfPublicKey, ntfPrivateKey, notifierId, rcvNtfDhSecret, connId) + (ntfPublicKey_, ntfPrivateKey_, notifierId_, rcvNtfDhSecret_, connId) + where + (ntfPublicKey_, ntfPrivateKey_, notifierId_, rcvNtfDhSecret_) = case clientNtfCreds of + Just ClientNtfCreds {ntfPublicKey, ntfPrivateKey, notifierId, rcvNtfDhSecret} -> (Just ntfPublicKey, Just ntfPrivateKey, Just notifierId, Just rcvNtfDhSecret) + Nothing -> (Nothing, Nothing, Nothing, Nothing) createConfirmation :: DB.Connection -> TVar ChaChaDRG -> NewConfirmation -> IO (Either StoreError ConfirmationId) createConfirmation db gVar NewConfirmation {connId, senderConf = SMPConfirmation {senderKey, e2ePubKey, connInfo, smpReplyQueues}, ratchetState} = From 0392f849aa1f3613c5b6a68f217aeaaa09113c6e Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Fri, 1 Jul 2022 13:19:56 +0400 Subject: [PATCH 108/131] ntf: reset subscription on SCHK AUTH error; launch only required workers (#449) --- .../Messaging/Agent/NtfSubSupervisor.hs | 91 +++++++++++-------- src/Simplex/Messaging/Agent/Store/SQLite.hs | 6 +- 2 files changed, 57 insertions(+), 40 deletions(-) diff --git a/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs b/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs index 166c21486..4a0a6ec67 100644 --- a/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs +++ b/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs @@ -59,69 +59,75 @@ runNtfSupervisor c = forever $ do processNtfSub :: forall m. AgentMonad m => AgentClient -> (ConnId, NtfSupervisorCommand) -> m () processNtfSub c@AgentClient {subQ} (connId, cmd) = do logInfo $ "processNtfSub - connId = " <> tshow connId <> " - cmd = " <> tshow cmd - ntfServer_ <- getNtfServer c - ts <- liftIO getCurrentTime case cmd of NSCCreate -> do - (sub_, RcvQueue {server = smpServer, clientNtfCreds}) <- withStore c $ \db -> runExceptT $ do - sub_ <- liftIO $ getNtfSubscription db connId + (a, RcvQueue {server = smpServer, clientNtfCreds}) <- withStore c $ \db -> runExceptT $ do + a <- liftIO $ getNtfSubscription db connId q <- ExceptT $ getRcvQueue db connId - pure (sub_, q) - logInfo $ "processNtfSub, NSCCreate - sub_ = " <> tshow sub_ - case (sub_, ntfServer_) of - (Nothing, Just ntfServer) -> do - currentTime <- liftIO getCurrentTime - case clientNtfCreds of - Just ClientNtfCreds {notifierId} -> do - let newSub = newNtfSubscription connId smpServer (Just notifierId) ntfServer NASKey - withStore' c $ \db -> createNtfSubscription db newSub (NtfSubNTFAction NSACreate) currentTime - _ -> do - let newSub = newNtfSubscription connId smpServer Nothing ntfServer NASNew - withStore' c $ \db -> createNtfSubscription db newSub (NtfSubSMPAction NSASmpKey) currentTime - -- TODO optimize? - -- TODO - read action in getNtfSubscription and decide which worker to create - -- TODO - SMP worker can create Ntf worker on NKEY completion - addNtfSMPWorker smpServer - addNtfWorker ntfServer - (Just (sub@NtfSubscription {ntfSubStatus}, action), Just ntfServer) -> do - case action of + pure (a, q) + logInfo $ "processNtfSub, NSCCreate - a = " <> tshow a + case a of + Nothing -> do + withNtfServer c $ \confNtfServer -> do + case clientNtfCreds of + Just ClientNtfCreds {notifierId} -> do + let newSub = newNtfSubscription connId smpServer (Just notifierId) confNtfServer NASKey + ts <- liftIO getCurrentTime + withStore' c $ \db -> createNtfSubscription db newSub (NtfSubNTFAction NSACreate) ts + addNtfNTFWorker confNtfServer + Nothing -> do + let newSub = newNtfSubscription connId smpServer Nothing confNtfServer NASNew + ts <- liftIO getCurrentTime + withStore' c $ \db -> createNtfSubscription db newSub (NtfSubSMPAction NSASmpKey) ts + addNtfSMPWorker smpServer + (Just (sub@NtfSubscription {ntfSubStatus, ntfServer = subNtfServer}, action_)) -> do + case action_ of -- action was set to NULL after worker internal error Nothing -> resetSubscription - Just (a, _) -> - -- subscription was marked for deletion - when (isDeleteNtfSubAction a) $ + Just (action, _) + -- subscription was marked for deletion / is being deleted + | isDeleteNtfSubAction action -> do if ntfSubStatus == NASNew || ntfSubStatus == NASOff || ntfSubStatus == NASDeleted then resetSubscription - else withStore' c $ \db -> - supervisorUpdateNtfSubscription db sub {ntfServer} (NtfSubNTFAction NSACreate) ts - addNtfSMPWorker smpServer - addNtfWorker ntfServer + else withNtfServer c $ \confNtfServer -> do + ts <- liftIO getCurrentTime + withStore' c $ \db -> + supervisorUpdateNtfSubscription db sub {ntfServer = confNtfServer} (NtfSubNTFAction NSACreate) ts + addNtfNTFWorker confNtfServer + | otherwise -> case action of + NtfSubNTFAction _ -> addNtfNTFWorker subNtfServer + NtfSubSMPAction _ -> addNtfSMPWorker smpServer where resetSubscription :: m () - resetSubscription = withStore' c $ \db -> - supervisorUpdateNtfSubscription db sub {ntfQueueId = Nothing, ntfServer, ntfSubId = Nothing, ntfSubStatus = NASNew} (NtfSubSMPAction NSASmpKey) ts - _ -> pure () -- err "NSCCreate - notification server not configured" + resetSubscription = + withNtfServer c $ \confNtfServer -> do + ts <- liftIO getCurrentTime + withStore' c $ \db -> + supervisorUpdateNtfSubscription db sub {ntfQueueId = Nothing, ntfServer = confNtfServer, ntfSubId = Nothing, ntfSubStatus = NASNew} (NtfSubSMPAction NSASmpKey) ts + addNtfSMPWorker smpServer NSCDelete -> do sub_ <- withStore' c $ \db -> do + ts <- liftIO getCurrentTime supervisorUpdateNtfSubAction db connId (NtfSubNTFAction NSADelete) ts getNtfSubscription db connId logInfo $ "processNtfSub, NSCDelete - sub_ = " <> tshow sub_ case sub_ of - (Just (NtfSubscription {ntfServer}, _)) -> addNtfWorker ntfServer + (Just (NtfSubscription {ntfServer}, _)) -> addNtfNTFWorker ntfServer _ -> pure () -- err "NSCDelete - no subscription" NSCSmpDelete -> do withStore' c (`getRcvQueue` connId) >>= \case Right rq@RcvQueue {server = smpServer} -> do logInfo $ "processNtfSub, NSCSmpDelete - rq = " <> tshow rq + ts <- liftIO getCurrentTime withStore' c $ \db -> supervisorUpdateNtfSubAction db connId (NtfSubSMPAction NSASmpDelete) ts addNtfSMPWorker smpServer _ -> err "NSCSmpDelete - no rcv queue" NSCNtfWorker ntfServer -> - addNtfWorker ntfServer + addNtfNTFWorker ntfServer NSCNtfSMPWorker smpServer -> addNtfSMPWorker smpServer where - addNtfWorker = addWorker ntfWorkers runNtfWorker + addNtfNTFWorker = addWorker ntfWorkers runNtfWorker addNtfSMPWorker = addWorker ntfSMPWorkers runNtfSMPWorker addWorker :: (NtfSupervisor -> TMap ProtocolServer (TMVar (), Async ())) -> @@ -140,6 +146,13 @@ processNtfSub c@AgentClient {subQ} (connId, cmd) = do err :: String -> m () err internalErrStr = atomically $ writeTBQueue subQ ("", connId, AP.ERR $ AP.INTERNAL internalErrStr) +withNtfServer :: AgentMonad m => AgentClient -> (NtfServer -> m ()) -> m () +withNtfServer c action = do + ntfServer_ <- getNtfServer c + case ntfServer_ of + Just confNtfServer -> action confNtfServer + _ -> pure () -- err "NSCCreate - notification server not configured" + runNtfWorker :: forall m. AgentMonad m => AgentClient -> NtfServer -> TMVar () -> m () runNtfWorker c srv doWork = forever $ do void . atomically $ readTMVar doWork @@ -186,7 +199,11 @@ runNtfWorker c srv doWork = forever $ do case ntfSubId of Just nSubId -> agentNtfCheckSubscription c nSubId tkn >>= \case - NSSMPAuth -> updateSub (NASCreated NSSMPAuth) (NtfSubNTFAction NSADelete) ts -- TODO re-create subscription? + NSSMPAuth -> withNtfServer c $ \confNtfServer -> do + withStore' c $ \db -> + updateNtfSubscription db sub {ntfServer = confNtfServer, ntfQueueId = Nothing, ntfSubId = Nothing, ntfSubStatus = NASNew} (NtfSubSMPAction NSASmpKey) ts + ns <- asks ntfSupervisor + atomically $ writeTBQueue (ntfSubQ ns) (connId, NSCNtfSMPWorker smpServer) status -> updateSubNextCheck ts status Nothing -> ntfInternalError c connId "NSACheck - no subscription ID" _ -> ntfInternalError c connId "NSACheck - no active token" diff --git a/src/Simplex/Messaging/Agent/Store/SQLite.hs b/src/Simplex/Messaging/Agent/Store/SQLite.hs index bd28f365f..82bacf5cc 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite.hs @@ -787,7 +787,7 @@ supervisorUpdateNtfSubAction db connId action actionTs = do (ntfSubAction, ntfSubSMPAction) = ntfSubAndSMPAction action updateNtfSubscription :: DB.Connection -> NtfSubscription -> NtfSubAction -> NtfActionTs -> IO () -updateNtfSubscription db NtfSubscription {connId, ntfQueueId, ntfSubId, ntfSubStatus} action actionTs = do +updateNtfSubscription db NtfSubscription {connId, ntfQueueId, ntfServer = (ProtocolServer ntfHost ntfPort _), ntfSubId, ntfSubStatus} action actionTs = do r <- maybeFirstRow fromOnly $ DB.query db "SELECT updated_by_supervisor FROM ntf_subscriptions WHERE conn_id = ?" (Only connId) forM_ r $ \updatedBySupervisor -> do updatedAt <- getCurrentTime @@ -806,10 +806,10 @@ updateNtfSubscription db NtfSubscription {connId, ntfQueueId, ntfSubId, ntfSubSt db [sql| UPDATE ntf_subscriptions - SET smp_ntf_id = ?, ntf_sub_id = ?, ntf_sub_status = ?, ntf_sub_action = ?, ntf_sub_smp_action = ?, ntf_sub_action_ts = ?, updated_by_supervisor = ?, updated_at = ? + SET smp_ntf_id = ?, ntf_host = ?, ntf_port = ?, ntf_sub_id = ?, ntf_sub_status = ?, ntf_sub_action = ?, ntf_sub_smp_action = ?, ntf_sub_action_ts = ?, updated_by_supervisor = ?, updated_at = ? WHERE conn_id = ? |] - (ntfQueueId, ntfSubId, ntfSubStatus, ntfSubAction, ntfSubSMPAction, actionTs, False, updatedAt, connId) + ((ntfQueueId, ntfHost, ntfPort, ntfSubId) :. (ntfSubStatus, ntfSubAction, ntfSubSMPAction, actionTs, False, updatedAt, connId)) where (ntfSubAction, ntfSubSMPAction) = ntfSubAndSMPAction action From 82eff6c7570cfeb1c171128aa9f83a6b1483a804 Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Fri, 1 Jul 2022 14:09:05 +0400 Subject: [PATCH 109/131] ntf: internal error if ntf server is not configured at SCHK on AUTH error (#450) * ntf: internal error if ntf server is not configured at SCHK on AUTH error * refactor * rename Co-authored-by: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> --- .../Messaging/Agent/NtfSubSupervisor.hs | 37 +++++++++---------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs b/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs index 4a0a6ec67..a456b3eb7 100644 --- a/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs +++ b/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs @@ -68,15 +68,15 @@ processNtfSub c@AgentClient {subQ} (connId, cmd) = do logInfo $ "processNtfSub, NSCCreate - a = " <> tshow a case a of Nothing -> do - withNtfServer c $ \confNtfServer -> do + withNtfServer c $ \ntfServer -> do case clientNtfCreds of Just ClientNtfCreds {notifierId} -> do - let newSub = newNtfSubscription connId smpServer (Just notifierId) confNtfServer NASKey + let newSub = newNtfSubscription connId smpServer (Just notifierId) ntfServer NASKey ts <- liftIO getCurrentTime withStore' c $ \db -> createNtfSubscription db newSub (NtfSubNTFAction NSACreate) ts - addNtfNTFWorker confNtfServer + addNtfNTFWorker ntfServer Nothing -> do - let newSub = newNtfSubscription connId smpServer Nothing confNtfServer NASNew + let newSub = newNtfSubscription connId smpServer Nothing ntfServer NASNew ts <- liftIO getCurrentTime withStore' c $ \db -> createNtfSubscription db newSub (NtfSubSMPAction NSASmpKey) ts addNtfSMPWorker smpServer @@ -89,21 +89,21 @@ processNtfSub c@AgentClient {subQ} (connId, cmd) = do | isDeleteNtfSubAction action -> do if ntfSubStatus == NASNew || ntfSubStatus == NASOff || ntfSubStatus == NASDeleted then resetSubscription - else withNtfServer c $ \confNtfServer -> do + else withNtfServer c $ \ntfServer -> do ts <- liftIO getCurrentTime withStore' c $ \db -> - supervisorUpdateNtfSubscription db sub {ntfServer = confNtfServer} (NtfSubNTFAction NSACreate) ts - addNtfNTFWorker confNtfServer + supervisorUpdateNtfSubscription db sub {ntfServer} (NtfSubNTFAction NSACreate) ts + addNtfNTFWorker ntfServer | otherwise -> case action of NtfSubNTFAction _ -> addNtfNTFWorker subNtfServer NtfSubSMPAction _ -> addNtfSMPWorker smpServer where resetSubscription :: m () resetSubscription = - withNtfServer c $ \confNtfServer -> do + withNtfServer c $ \ntfServer -> do ts <- liftIO getCurrentTime withStore' c $ \db -> - supervisorUpdateNtfSubscription db sub {ntfQueueId = Nothing, ntfServer = confNtfServer, ntfSubId = Nothing, ntfSubStatus = NASNew} (NtfSubSMPAction NSASmpKey) ts + supervisorUpdateNtfSubscription db sub {ntfQueueId = Nothing, ntfServer, ntfSubId = Nothing, ntfSubStatus = NASNew} (NtfSubSMPAction NSASmpKey) ts addNtfSMPWorker smpServer NSCDelete -> do sub_ <- withStore' c $ \db -> do @@ -147,11 +147,7 @@ processNtfSub c@AgentClient {subQ} (connId, cmd) = do err internalErrStr = atomically $ writeTBQueue subQ ("", connId, AP.ERR $ AP.INTERNAL internalErrStr) withNtfServer :: AgentMonad m => AgentClient -> (NtfServer -> m ()) -> m () -withNtfServer c action = do - ntfServer_ <- getNtfServer c - case ntfServer_ of - Just confNtfServer -> action confNtfServer - _ -> pure () -- err "NSCCreate - notification server not configured" +withNtfServer c action = getNtfServer c >>= mapM_ action runNtfWorker :: forall m. AgentMonad m => AgentClient -> NtfServer -> TMVar () -> m () runNtfWorker c srv doWork = forever $ do @@ -199,11 +195,14 @@ runNtfWorker c srv doWork = forever $ do case ntfSubId of Just nSubId -> agentNtfCheckSubscription c nSubId tkn >>= \case - NSSMPAuth -> withNtfServer c $ \confNtfServer -> do - withStore' c $ \db -> - updateNtfSubscription db sub {ntfServer = confNtfServer, ntfQueueId = Nothing, ntfSubId = Nothing, ntfSubStatus = NASNew} (NtfSubSMPAction NSASmpKey) ts - ns <- asks ntfSupervisor - atomically $ writeTBQueue (ntfSubQ ns) (connId, NSCNtfSMPWorker smpServer) + NSSMPAuth -> do + getNtfServer c >>= \case + Just ntfServer -> do + withStore' c $ \db -> + updateNtfSubscription db sub {ntfServer, ntfQueueId = Nothing, ntfSubId = Nothing, ntfSubStatus = NASNew} (NtfSubSMPAction NSASmpKey) ts + ns <- asks ntfSupervisor + atomically $ writeTBQueue (ntfSubQ ns) (connId, NSCNtfSMPWorker smpServer) + _ -> ntfInternalError c connId "NSACheck - failed to reset subscription, notification server not configured" status -> updateSubNextCheck ts status Nothing -> ntfInternalError c connId "NSACheck - no subscription ID" _ -> ntfInternalError c connId "NSACheck - no active token" From 238a2e7fe90906a02b09c13b7dfd2652e4261759 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Fri, 1 Jul 2022 12:22:20 +0100 Subject: [PATCH 110/131] handle ntf supervisor errors (#451) --- src/Simplex/Messaging/Agent/NtfSubSupervisor.hs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs b/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs index a456b3eb7..7073448fc 100644 --- a/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs +++ b/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs @@ -18,7 +18,7 @@ where import Control.Concurrent.Async (Async, uninterruptibleCancel) import Control.Concurrent.STM (stateTVar) -import Control.Logger.Simple (logInfo) +import Control.Logger.Simple (logError, logInfo) import Control.Monad import Control.Monad.Except import Control.Monad.IO.Unlift (MonadUnliftIO) @@ -49,12 +49,14 @@ import qualified UnliftIO.Exception as E import UnliftIO.STM runNtfSupervisor :: (MonadUnliftIO m, MonadReader Env m) => AgentClient -> m () -runNtfSupervisor c = forever $ do +runNtfSupervisor c = forever . handleError $ do ns <- asks ntfSupervisor cmd <- atomically . readTBQueue $ ntfSubQ ns runExceptT (processNtfSub c cmd) >>= \case Left e -> liftIO $ print e Right _ -> return () + where + handleError = E.handle $ \(e :: E.SomeException) -> logError $ "runNtfSupervisor error " <> tshow e processNtfSub :: forall m. AgentMonad m => AgentClient -> (ConnId, NtfSupervisorCommand) -> m () processNtfSub c@AgentClient {subQ} (connId, cmd) = do From 6b6ea78eac049ed589ceb8debdfdb18059ddb375 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Fri, 1 Jul 2022 12:25:08 +0100 Subject: [PATCH 111/131] ntf server error handling (#448) * ntf server error handling * refactor * fix --- src/Simplex/Messaging/Agent/Client.hs | 2 +- .../Messaging/Agent/NtfSubSupervisor.hs | 2 +- src/Simplex/Messaging/Client.hs | 18 ++--- src/Simplex/Messaging/Notifications/Client.hs | 11 ++-- .../Messaging/Notifications/Protocol.hs | 16 ++--- src/Simplex/Messaging/Notifications/Server.hs | 66 +++++++++---------- src/Simplex/Messaging/Protocol.hs | 2 +- 7 files changed, 57 insertions(+), 60 deletions(-) diff --git a/src/Simplex/Messaging/Agent/Client.hs b/src/Simplex/Messaging/Agent/Client.hs index 7f7038be7..9dad6b6f9 100644 --- a/src/Simplex/Messaging/Agent/Client.hs +++ b/src/Simplex/Messaging/Agent/Client.hs @@ -422,7 +422,7 @@ protocolClientError :: (ErrorType -> AgentErrorType) -> ProtocolClientError -> A protocolClientError protocolError_ = \case PCEProtocolError e -> protocolError_ e PCEResponseError e -> BROKER $ RESPONSE e - PCEUnexpectedResponse -> BROKER UNEXPECTED + PCEUnexpectedResponse _ -> BROKER UNEXPECTED PCEResponseTimeout -> BROKER TIMEOUT PCENetworkError -> BROKER NETWORK PCETransportError e -> BROKER $ TRANSPORT e diff --git a/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs b/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs index 7073448fc..aa75dac01 100644 --- a/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs +++ b/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs @@ -197,7 +197,7 @@ runNtfWorker c srv doWork = forever $ do case ntfSubId of Just nSubId -> agentNtfCheckSubscription c nSubId tkn >>= \case - NSSMPAuth -> do + NSAuth -> do getNtfServer c >>= \case Just ntfServer -> do withStore' c $ \db -> diff --git a/src/Simplex/Messaging/Client.hs b/src/Simplex/Messaging/Client.hs index cb32ba3be..49f492611 100644 --- a/src/Simplex/Messaging/Client.hs +++ b/src/Simplex/Messaging/Client.hs @@ -224,7 +224,7 @@ getProtocolClient protocolServer cfg@ProtocolClientConfig {qSize, tcpTimeout, tc Right r -> case protocolError r of Just e -> Left $ PCEProtocolError e _ -> Right r - else Left PCEUnexpectedResponse + else Left . PCEUnexpectedResponse $ bshow respOrErr where sendMsg :: QueueId -> Either ErrorType msg -> IO () sendMsg qId = \case @@ -248,7 +248,7 @@ data ProtocolClientError -- e.g. server should respond `IDS` or `ERR` to `NEW` command, -- other responses would result in this error. -- Forwarded to the agent client as `ERR BROKER UNEXPECTED`. - PCEUnexpectedResponse + PCEUnexpectedResponse ByteString | -- | Used for TCP connection and command response timeouts. -- Forwarded to the agent client as `ERR BROKER TIMEOUT`. PCEResponseTimeout @@ -276,7 +276,7 @@ createSMPQueue :: createSMPQueue c rpKey rKey dhKey = sendSMPCommand c (Just rpKey) "" (NEW rKey dhKey) >>= \case IDS qik -> pure qik - _ -> throwE PCEUnexpectedResponse + r -> throwE . PCEUnexpectedResponse $ bshow r -- | Subscribe to the SMP queue. -- @@ -287,7 +287,7 @@ subscribeSMPQueue c@ProtocolClient {protocolServer, sessionId, msgQ} rpKey rId = OK -> return () cmd@MSG {} -> lift . atomically $ mapM_ (`writeTBQueue` (protocolServer, sessionId, rId, cmd)) msgQ - _ -> throwE PCEUnexpectedResponse + r -> throwE . PCEUnexpectedResponse $ bshow r -- | Get message from SMP queue. The server returns ERR PROHIBITED if a client uses SUB and GET via the same transport connection for the same queue -- @@ -299,7 +299,7 @@ getSMPMessage c@ProtocolClient {protocolServer, sessionId, msgQ} rpKey rId = cmd@(MSG msgId msgTs msgFlags _) -> do lift . atomically $ mapM_ (`writeTBQueue` (protocolServer, sessionId, rId, cmd)) msgQ pure $ Just SMP.SMPMsgMeta {msgId, msgTs, msgFlags} - _ -> throwE PCEUnexpectedResponse + r -> throwE . PCEUnexpectedResponse $ bshow r -- | Subscribe to the SMP queue notifications. -- @@ -320,7 +320,7 @@ enableSMPQueueNotifications :: SMPClient -> RcvPrivateSignKey -> RecipientId -> enableSMPQueueNotifications c rpKey rId notifierKey rcvNtfPublicDhKey = sendSMPCommand c (Just rpKey) rId (NKEY notifierKey rcvNtfPublicDhKey) >>= \case NID nId rcvNtfSrvPublicDhKey -> pure (nId, rcvNtfSrvPublicDhKey) - _ -> throwE PCEUnexpectedResponse + r -> throwE . PCEUnexpectedResponse $ bshow r -- | Disable notifications for the queue for push notifications server. -- @@ -335,7 +335,7 @@ sendSMPMessage :: SMPClient -> Maybe SndPrivateSignKey -> SenderId -> MsgFlags - sendSMPMessage c spKey sId flags msg = sendSMPCommand c spKey sId (SEND flags msg) >>= \case OK -> pure () - _ -> throwE PCEUnexpectedResponse + r -> throwE . PCEUnexpectedResponse $ bshow r -- | Acknowledge message delivery (server deletes the message). -- @@ -346,7 +346,7 @@ ackSMPMessage c@ProtocolClient {protocolServer, sessionId, msgQ} rpKey rId msgId OK -> return () cmd@MSG {} -> lift . atomically $ mapM_ (`writeTBQueue` (protocolServer, sessionId, rId, cmd)) msgQ - _ -> throwE PCEUnexpectedResponse + r -> throwE . PCEUnexpectedResponse $ bshow r -- | Irreversibly suspend SMP queue. -- The existing messages from the queue will still be delivered. @@ -365,7 +365,7 @@ okSMPCommand :: PartyI p => Command p -> SMPClient -> C.APrivateSignKey -> Queue okSMPCommand cmd c pKey qId = sendSMPCommand c (Just pKey) qId cmd >>= \case OK -> return () - _ -> throwE PCEUnexpectedResponse + r -> throwE . PCEUnexpectedResponse $ bshow r -- | Send SMP command sendSMPCommand :: PartyI p => SMPClient -> Maybe C.APrivateSignKey -> QueueId -> Command p -> ExceptT ProtocolClientError IO BrokerMsg diff --git a/src/Simplex/Messaging/Notifications/Client.hs b/src/Simplex/Messaging/Notifications/Client.hs index 51487a618..8f690e955 100644 --- a/src/Simplex/Messaging/Notifications/Client.hs +++ b/src/Simplex/Messaging/Notifications/Client.hs @@ -10,6 +10,7 @@ import Data.Word (Word16) import Simplex.Messaging.Client import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Notifications.Protocol +import Simplex.Messaging.Util (bshow) type NtfClient = ProtocolClient NtfResponse @@ -17,7 +18,7 @@ ntfRegisterToken :: NtfClient -> C.APrivateSignKey -> NewNtfEntity 'Token -> Exc ntfRegisterToken c pKey newTkn = sendNtfCommand c (Just pKey) "" (TNEW newTkn) >>= \case NRTknId tknId dhKey -> pure (tknId, dhKey) - _ -> throwE PCEUnexpectedResponse + r -> throwE . PCEUnexpectedResponse $ bshow r ntfVerifyToken :: NtfClient -> C.APrivateSignKey -> NtfTokenId -> NtfRegCode -> ExceptT ProtocolClientError IO () ntfVerifyToken c pKey tknId code = okNtfCommand (TVFY code) c pKey tknId @@ -26,7 +27,7 @@ ntfCheckToken :: NtfClient -> C.APrivateSignKey -> NtfTokenId -> ExceptT Protoco ntfCheckToken c pKey tknId = sendNtfCommand c (Just pKey) tknId TCHK >>= \case NRTkn stat -> pure stat - _ -> throwE PCEUnexpectedResponse + r -> throwE . PCEUnexpectedResponse $ bshow r ntfReplaceToken :: NtfClient -> C.APrivateSignKey -> NtfTokenId -> DeviceToken -> ExceptT ProtocolClientError IO () ntfReplaceToken c pKey tknId token = okNtfCommand (TRPL token) c pKey tknId @@ -41,13 +42,13 @@ ntfCreateSubscription :: NtfClient -> C.APrivateSignKey -> NewNtfEntity 'Subscri ntfCreateSubscription c pKey newSub = sendNtfCommand c (Just pKey) "" (SNEW newSub) >>= \case NRSubId subId -> pure subId - _ -> throwE PCEUnexpectedResponse + r -> throwE . PCEUnexpectedResponse $ bshow r ntfCheckSubscription :: NtfClient -> C.APrivateSignKey -> NtfSubscriptionId -> ExceptT ProtocolClientError IO NtfSubStatus ntfCheckSubscription c pKey subId = sendNtfCommand c (Just pKey) subId SCHK >>= \case NRSub stat -> pure stat - _ -> throwE PCEUnexpectedResponse + r -> throwE . PCEUnexpectedResponse $ bshow r ntfDeleteSubscription :: NtfClient -> C.APrivateSignKey -> NtfSubscriptionId -> ExceptT ProtocolClientError IO () ntfDeleteSubscription = okNtfCommand SDEL @@ -60,4 +61,4 @@ okNtfCommand :: NtfEntityI e => NtfCommand e -> NtfClient -> C.APrivateSignKey - okNtfCommand cmd c pKey entId = sendNtfCommand c (Just pKey) entId cmd >>= \case NROk -> return () - _ -> throwE PCEUnexpectedResponse + r -> throwE . PCEUnexpectedResponse $ bshow r diff --git a/src/Simplex/Messaging/Notifications/Protocol.hs b/src/Simplex/Messaging/Notifications/Protocol.hs index e025cf7e6..9b8e55184 100644 --- a/src/Simplex/Messaging/Notifications/Protocol.hs +++ b/src/Simplex/Messaging/Notifications/Protocol.hs @@ -403,12 +403,12 @@ data NtfSubStatus NSActive | -- | disconnected/unsubscribed from SMP server NSInactive - | -- | NEND received (we currently do not support it) + | -- | END received NSEnd | -- | SMP AUTH error - NSSMPAuth + NSAuth | -- | SMP error other than AUTH - NSSMPErr ErrorType + NSErr ByteString deriving (Eq, Show) instance Encoding NtfSubStatus where @@ -418,8 +418,8 @@ instance Encoding NtfSubStatus where NSActive -> "ACTIVE" NSInactive -> "INACTIVE" NSEnd -> "END" - NSSMPAuth -> "SMP_AUTH" - NSSMPErr err -> "SMP_ERR " <> smpEncode err + NSAuth -> "AUTH" + NSErr err -> "ERR " <> err smpP = A.takeTill (== ' ') >>= \case "NEW" -> pure NSNew @@ -427,10 +427,8 @@ instance Encoding NtfSubStatus where "ACTIVE" -> pure NSActive "INACTIVE" -> pure NSInactive "END" -> pure NSEnd - "SMP_AUTH" -> pure NSSMPAuth - "SMP_ERR" -> do - _ <- A.space - NSSMPErr <$> smpP + "AUTH" -> pure NSAuth + "ERR" -> NSErr <$> (A.space *> A.takeByteString) _ -> fail "bad NtfSubStatus" instance StrEncoding NtfSubStatus where diff --git a/src/Simplex/Messaging/Notifications/Server.hs b/src/Simplex/Messaging/Notifications/Server.hs index bbef83255..ffb475988 100644 --- a/src/Simplex/Messaging/Notifications/Server.hs +++ b/src/Simplex/Messaging/Notifications/Server.hs @@ -111,27 +111,20 @@ ntfSubscriber NtfSubscriber {smpSubscribers, newSubQ, smpAgent = ca@SMPClientAge runSMPSubscriber :: SMPSubscriber -> m () runSMPSubscriber SMPSubscriber {newSubQ = subscriberSubQ} = forever $ - atomically (peekTQueue subscriberSubQ) >>= \case - NtfSub NtfSubData {smpQueue, notifierKey} -> do + atomically (peekTQueue subscriberSubQ) + >>= \(NtfSub NtfSubData {smpQueue, notifierKey}) -> do updateSubStatus smpQueue NSPending let SMPQueueNtf {smpServer, notifierId} = smpQueue liftIO (runExceptT $ subscribeQueue ca smpServer ((SPNotifier, notifierId), notifierKey)) >>= \case - Right _ -> update smpQueue NSActive - Left err -> case err of - PCEProtocolError AUTH -> update smpQueue NSSMPAuth - PCEProtocolError e -> update smpQueue $ NSSMPErr e - PCEIOError e -> log' $ "IOError " <> T.pack (show e) - PCEResponseError e -> log' $ "ResponseError " <> T.pack (show e) - PCEUnexpectedResponse -> log' "UnexpectedResponse" - PCETransportError e -> log' $ "TransportError " <> T.pack (show e) - PCESignatureError e -> log' $ "SignatureError " <> T.pack (show e) - PCEResponseTimeout -> pure () - PCENetworkError -> pure () - where - update smpQueue status = do - updateSubStatus smpQueue status - void . atomically $ readTQueue subscriberSubQ - log' e = logError $ "SMPSubscriber subscribeQueue " <> e + Right _ -> do + updateSubStatus smpQueue NSActive + void . atomically $ readTQueue subscriberSubQ + Left err -> do + handleSubError smpQueue err + case err of + PCEResponseTimeout -> pure () + PCENetworkError -> pure () + _ -> void . atomically $ readTQueue subscriberSubQ receiveSMP :: m () receiveSMP = forever $ do @@ -145,7 +138,7 @@ ntfSubscriber NtfSubscriber {smpSubscribers, newSubQ, smpAgent = ca@SMPClientAge atomically $ findNtfSubscriptionToken st smpQueue >>= mapM_ (\tkn -> writeTBQueue pushQ (tkn, PNMessage PNMessageData {smpQueue, ntfTs, nmsgNonce, encNMsgMeta})) - SMP.END -> updateSubStatus smpQueue NSInactive + SMP.END -> updateSubStatus smpQueue NSEnd _ -> pure () pure () @@ -162,20 +155,23 @@ ntfSubscriber NtfSubscriber {smpSubscribers, newSubQ, smpAgent = ca@SMPClientAge let ntfId = snd sub smpQueue = SMPQueueNtf srv ntfId updateSubStatus smpQueue NSActive - CASubError srv sub err -> do - let ntfId = snd sub - smpQueue = SMPQueueNtf srv ntfId - case err of - PCEProtocolError e -> case e of - AUTH -> updateSubStatus smpQueue NSSMPAuth - _ -> updateSubStatus smpQueue (NSSMPErr e) - PCEResponseError e -> logErr e - PCEUnexpectedResponse -> logErr err - PCESignatureError e -> logErr e - PCEIOError e -> logErr e - _ -> pure () - where - logErr e = logError $ "ntfSubscriber receiveAgent error: " <> T.pack (show e) + CASubError srv (_, ntfId) err -> + handleSubError (SMPQueueNtf srv ntfId) err + + handleSubError :: SMPQueueNtf -> ProtocolClientError -> m () + handleSubError smpQueue = \case + PCEProtocolError AUTH -> updateSubStatus smpQueue NSAuth + PCEProtocolError e -> updateErr "SMP error " e + PCEIOError e -> updateErr "IOError " e + PCEResponseError e -> updateErr "ResponseError " e + PCEUnexpectedResponse r -> updateErr "UnexpectedResponse " r + PCETransportError e -> updateErr "TransportError " e + PCESignatureError e -> updateErr "SignatureError " e + PCEResponseTimeout -> pure () + PCENetworkError -> pure () + where + updateErr :: Show e => ByteString -> e -> m () + updateErr errType e = updateSubStatus smpQueue . NSErr $ errType <> bshow e updateSubStatus smpQueue status = do st <- asks store @@ -370,7 +366,9 @@ client NtfServerClient {rcvQ, sndQ} NtfSubscriber {newSubQ, smpAgent = ca} NtfPu | otherwise -> do logDebug "TVFY - incorrect code or token status" pure $ NRErr AUTH - TCHK -> pure $ NRTkn status + TCHK -> do + logDebug "TCHK" + pure $ NRTkn status TRPL token' -> do logDebug "TRPL - replace token" st <- asks store diff --git a/src/Simplex/Messaging/Protocol.hs b/src/Simplex/Messaging/Protocol.hs index 6bda98677..029e8eeb3 100644 --- a/src/Simplex/Messaging/Protocol.hs +++ b/src/Simplex/Messaging/Protocol.hs @@ -628,7 +628,7 @@ transmissionP = do command <- A.takeByteString pure RawTransmission {signature, signed, sessId, corrId, entityId, command} -class (ProtocolEncoding msg, ProtocolEncoding (ProtocolCommand msg)) => Protocol msg where +class (ProtocolEncoding msg, ProtocolEncoding (ProtocolCommand msg), Show msg) => Protocol msg where type ProtocolCommand msg = cmd | cmd -> msg protocolClientHandshake :: forall c. Transport c => c -> C.KeyHash -> VersionRange -> ExceptT TransportError IO (THandle c) protocolPing :: ProtocolCommand msg From e622e12a7fa8b9e3b5b5110a6c234cadab1188d1 Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Fri, 1 Jul 2022 15:27:32 +0400 Subject: [PATCH 112/131] v3.0.0-beta.2 / v1.0.0-beta.2 (ntf) --- apps/ntf-server/Main.hs | 2 +- src/Simplex/Messaging/Transport.hs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/ntf-server/Main.hs b/apps/ntf-server/Main.hs index ee1324912..20d2aa37a 100644 --- a/apps/ntf-server/Main.hs +++ b/apps/ntf-server/Main.hs @@ -42,7 +42,7 @@ ntfServerCLIConfig = fingerprintFile = combine cfgPath "fingerprint", defaultServerPort = "443", executableName = "ntf-server", - serverVersion = "SMP notifications server v1.0.0-beta.0", + serverVersion = "SMP notifications server v1.0.0-beta.2", mkIniFile = \enableStoreLog defaultServerPort -> "[STORE_LOG]\n\ \# The server uses STM memory for persistence,\n\ diff --git a/src/Simplex/Messaging/Transport.hs b/src/Simplex/Messaging/Transport.hs index b20a347e2..ce5a37868 100644 --- a/src/Simplex/Messaging/Transport.hs +++ b/src/Simplex/Messaging/Transport.hs @@ -96,7 +96,7 @@ supportedSMPServerVRange :: VersionRange supportedSMPServerVRange = mkVersionRange 1 2 simplexMQVersion :: String -simplexMQVersion = "3.0.0-beta.0" +simplexMQVersion = "3.0.0-beta.2" -- * Transport connection class From 5ff80f068869388a65e0ef6518178712d7e46094 Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Fri, 1 Jul 2022 21:46:11 +0400 Subject: [PATCH 113/131] ntf: test more scenarios of notifications mode change; test token change (#454) --- tests/AgentTests/NotificationTests.hs | 111 +++++++++++++++++++++----- 1 file changed, 90 insertions(+), 21 deletions(-) diff --git a/tests/AgentTests/NotificationTests.hs b/tests/AgentTests/NotificationTests.hs index 48c68be52..b15f3f4b5 100644 --- a/tests/AgentTests/NotificationTests.hs +++ b/tests/AgentTests/NotificationTests.hs @@ -69,6 +69,10 @@ notificationTests t = withSmpServer t $ withAPNSMockServer $ \apns -> withNtfServer t $ testChangeNotificationsMode apns + it "should change token" $ \_ -> + withSmpServer t $ + withAPNSMockServer $ \apns -> + withNtfServer t $ testChangeToken apns describe "Notifications server store log" $ do it "should save and restore tokens and subscriptions" $ \_ -> withSmpServer t $ @@ -244,9 +248,7 @@ testNotificationSubscriptionExistingConnection APNSMockServer {apnsQ} = do 2 <- msgId <$> sendMessage bob aliceId (SMP.MsgFlags True) "hello again" get bob ##> ("", aliceId, SENT $ baseId + 2) -- no notifications should follow - 500000 `timeout` atomically (readTBQueue apnsQ) >>= \case - Nothing -> pure () - _ -> error "unexpected notification" + noNotification apnsQ pure () where baseId = 3 @@ -289,9 +291,7 @@ testNotificationSubscriptionNewConnection APNSMockServer {apnsQ} = do get bob =##> \case ("", c, Msg "hey there") -> c == aliceId; _ -> False ackMessage bob aliceId $ baseId + 2 -- no unexpected notifications should follow - 500000 `timeout` atomically (readTBQueue apnsQ) >>= \case - Nothing -> pure () - _ -> error "unexpected notification" + noNotification apnsQ pure () where baseId = 3 @@ -323,34 +323,97 @@ testChangeNotificationsMode APNSMockServer {apnsQ} = do get bob ##> ("", aliceId, INFO "alice's connInfo") get alice ##> ("", bobId, CON) get bob ##> ("", aliceId, CON) - -- register notification token, set mode to NMPeriodic - tkn <- registerTestToken alice "abcd" NMPeriodic apnsQ - -- send message, no notification + -- register notification token, set mode to NMInstant + tkn <- registerTestToken alice "abcd" NMInstant apnsQ + -- send message, receive notification + liftIO $ threadDelay 500000 1 <- msgId <$> sendMessage bob aliceId (SMP.MsgFlags True) "hello" get bob ##> ("", aliceId, SENT $ baseId + 1) + void $ messageNotification apnsQ get alice =##> \case ("", c, Msg "hello") -> c == bobId; _ -> False ackMessage alice bobId $ baseId + 1 + -- set mode to NMPeriodic + NTActive <- registerNtfToken alice tkn NMPeriodic + -- send message, no notification + liftIO $ threadDelay 500000 + 2 <- msgId <$> sendMessage bob aliceId (SMP.MsgFlags True) "hello again" + get bob ##> ("", aliceId, SENT $ baseId + 2) + noNotification apnsQ + get alice =##> \case ("", c, Msg "hello again") -> c == bobId; _ -> False + ackMessage alice bobId $ baseId + 2 -- set mode to NMInstant NTActive <- registerNtfToken alice tkn NMInstant -- send message, receive notification liftIO $ threadDelay 500000 - 2 <- msgId <$> sendMessage bob aliceId (SMP.MsgFlags True) "hello again" - get bob ##> ("", aliceId, SENT $ baseId + 2) - void $ messageNotification apnsQ - get alice =##> \case ("", c, Msg "hello again") -> c == bobId; _ -> False - ackMessage alice bobId $ baseId + 2 - -- reset mode to NMPeriodic - NTActive <- registerNtfToken alice tkn NMPeriodic - -- send message, no notification - liftIO $ threadDelay 500000 3 <- msgId <$> sendMessage bob aliceId (SMP.MsgFlags True) "hello there" get bob ##> ("", aliceId, SENT $ baseId + 3) + void $ messageNotification apnsQ get alice =##> \case ("", c, Msg "hello there") -> c == bobId; _ -> False ackMessage alice bobId $ baseId + 3 + -- turn off notifications + deleteNtfToken alice tkn + -- send message, no notification + liftIO $ threadDelay 500000 + 4 <- msgId <$> sendMessage bob aliceId (SMP.MsgFlags True) "why hello there" + get bob ##> ("", aliceId, SENT $ baseId + 4) + noNotification apnsQ + get alice =##> \case ("", c, Msg "why hello there") -> c == bobId; _ -> False + ackMessage alice bobId $ baseId + 4 + -- turn on notifications, set mode to NMInstant + void $ registerTestToken alice "abcd" NMInstant apnsQ + -- send message, receive notification + liftIO $ threadDelay 500000 + 5 <- msgId <$> sendMessage bob aliceId (SMP.MsgFlags True) "hey" + get bob ##> ("", aliceId, SENT $ baseId + 5) + void $ messageNotification apnsQ + get alice =##> \case ("", c, Msg "hey") -> c == bobId; _ -> False + ackMessage alice bobId $ baseId + 5 -- no notifications should follow - 500000 `timeout` atomically (readTBQueue apnsQ) >>= \case - Nothing -> pure () - _ -> error "unexpected notification" + noNotification apnsQ + pure () + where + baseId = 3 + msgId = subtract baseId + +testChangeToken :: APNSMockServer -> IO () +testChangeToken APNSMockServer {apnsQ} = do + alice <- getSMPAgentClient agentCfg initAgentServers + bob <- getSMPAgentClient agentCfg {dbFile = testDB2} initAgentServers + Right (aliceId, bobId) <- runExceptT $ do + -- establish connection + (bobId, qInfo) <- createConnection alice SCMInvitation + aliceId <- joinConnection bob qInfo "bob's connInfo" + ("", _, CONF confId "bob's connInfo") <- get alice + allowConnection alice bobId confId "alice's connInfo" + get bob ##> ("", aliceId, INFO "alice's connInfo") + get alice ##> ("", bobId, CON) + get bob ##> ("", aliceId, CON) + -- register notification token, set mode to NMInstant + void $ registerTestToken alice "abcd" NMInstant apnsQ + -- send message, receive notification + liftIO $ threadDelay 500000 + 1 <- msgId <$> sendMessage bob aliceId (SMP.MsgFlags True) "hello" + get bob ##> ("", aliceId, SENT $ baseId + 1) + void $ messageNotification apnsQ + get alice =##> \case ("", c, Msg "hello") -> c == bobId; _ -> False + ackMessage alice bobId $ baseId + 1 + pure (aliceId, bobId) + disconnectAgentClient alice + + alice1 <- getSMPAgentClient agentCfg initAgentServers + Right () <- runExceptT $ do + subscribeConnection alice1 bobId + -- change notification token + void $ registerTestToken alice1 "bcde" NMInstant apnsQ + -- send message, receive notification + liftIO $ threadDelay 500000 + 2 <- msgId <$> sendMessage bob aliceId (SMP.MsgFlags True) "hello there" + get bob ##> ("", aliceId, SENT $ baseId + 2) + void $ messageNotification apnsQ + get alice1 =##> \case ("", c, Msg "hello there") -> c == bobId; _ -> False + ackMessage alice1 bobId $ baseId + 2 + -- no notifications should follow + noNotification apnsQ pure () where baseId = 3 @@ -393,3 +456,9 @@ messageNotification apnsQ = do liftIO $ sendApnsResponse APNSRespOk pure (nonce, message) _ -> error "bad notification" + +noNotification :: TBQueue APNSMockRequest -> ExceptT AgentErrorType IO () +noNotification apnsQ = do + 500000 `timeout` atomically (readTBQueue apnsQ) >>= \case + Nothing -> pure () + _ -> error "unexpected notification" From 77f1d45021431dd63fc3b94429120bc2e54bc93e Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Fri, 1 Jul 2022 22:38:39 +0100 Subject: [PATCH 114/131] fix invalid ini syntax (#452) --- apps/ntf-server/Main.hs | 2 +- apps/smp-server/Main.hs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/apps/ntf-server/Main.hs b/apps/ntf-server/Main.hs index 20d2aa37a..cb54ca2e9 100644 --- a/apps/ntf-server/Main.hs +++ b/apps/ntf-server/Main.hs @@ -51,7 +51,7 @@ ntfServerCLIConfig = \# and restoring it when the server is started.\n\ \# Log is compacted on start (deleted objects are removed).\n\ \# The messages are not logged.\n" - <> ("enable: " <> (if enableStoreLog then "on" else "off # on") <> "\n\n") + <> ("enable: " <> (if enableStoreLog then "on" else "off") <> "\n\n") <> "[TRANSPORT]\n\ \port: " <> defaultServerPort diff --git a/apps/smp-server/Main.hs b/apps/smp-server/Main.hs index 6d8d41418..2e6661802 100644 --- a/apps/smp-server/Main.hs +++ b/apps/smp-server/Main.hs @@ -59,10 +59,10 @@ smpServerCLIConfig = \# This option enables saving memory to append only log,\n\ \# and restoring it when the server is started.\n\ \# Log is compacted on start (deleted objects are removed).\n" - <> ("enable: " <> (if enableStoreLog then "on" else "off # on") <> "\n") + <> ("enable: " <> (if enableStoreLog then "on" else "off") <> "\n") <> "# The messages are optionally saved and restored when the server restarts,\n\ \# they are deleted after restarting.\n" - <> ("restore_messages: " <> (if enableStoreLog then "on" else "off # on") <> "\n\n") + <> ("restore_messages: " <> (if enableStoreLog then "on" else "off") <> "\n\n") <> "[TRANSPORT]\n" <> ("port: " <> defaultServerPort <> "\n") <> "websockets: off\n\n" From f9cd7e5416013803c899dda59110a362e853407d Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Fri, 1 Jul 2022 22:43:18 +0100 Subject: [PATCH 115/131] suspend ntf operations when agent is suspended (#453) * suspend ntf operations when agent is suspended * end and begin ntf operation on loop --- src/Simplex/Messaging/Agent.hs | 11 +- src/Simplex/Messaging/Agent/Client.hs | 22 ++-- src/Simplex/Messaging/Agent/Env/SQLite.hs | 6 +- .../Messaging/Agent/NtfSubSupervisor.hs | 100 ++++++++++-------- tests/SMPAgentClient.hs | 3 +- 5 files changed, 84 insertions(+), 58 deletions(-) diff --git a/src/Simplex/Messaging/Agent.hs b/src/Simplex/Messaging/Agent.hs index f6b42d91a..bca2b3f30 100644 --- a/src/Simplex/Messaging/Agent.hs +++ b/src/Simplex/Messaging/Agent.hs @@ -811,6 +811,7 @@ activateAgent' c = atomically $ do activate sndNetworkOp activate msgDeliveryOp activate rcvNetworkOp + activate ntfNetworkOp where activate opSel = modifyTVar' (opSel c) $ \s -> s {opSuspended = False} @@ -819,6 +820,7 @@ suspendAgent' c@AgentClient {agentState = as} maxDelay = do state <- atomically $ do writeTVar as ASSuspending + suspendOperation c AONtfNetwork $ pure () suspendOperation c AORcvNetwork $ suspendOperation c AOMsgDelivery $ suspendSendingAndDatabase c @@ -842,12 +844,11 @@ getSMPServer c = do subscriber :: (MonadUnliftIO m, MonadReader Env m) => AgentClient -> m () subscriber c@AgentClient {msgQ} = forever $ do - atomically $ endAgentOperation c AORcvNetwork t <- atomically $ readTBQueue msgQ - atomically $ beginAgentOperation c AORcvNetwork - withAgentLock c (runExceptT $ processSMPTransmission c t) >>= \case - Left e -> liftIO $ print e - Right _ -> return () + agentOperationBracket c AORcvNetwork $ + withAgentLock c (runExceptT $ processSMPTransmission c t) >>= \case + Left e -> liftIO $ print e + Right _ -> return () processSMPTransmission :: forall m. AgentMonad m => AgentClient -> ServerTransmission BrokerMsg -> m () processSMPTransmission c@AgentClient {smpClients, subQ} (srv, sessId, rId, cmd) = diff --git a/src/Simplex/Messaging/Agent/Client.hs b/src/Simplex/Messaging/Agent/Client.hs index 9dad6b6f9..78407769f 100644 --- a/src/Simplex/Messaging/Agent/Client.hs +++ b/src/Simplex/Messaging/Agent/Client.hs @@ -52,6 +52,7 @@ module Simplex.Messaging.Agent.Client AgentOperation (..), AgentOpState (..), AgentState (..), + agentOperationBracket, beginAgentOperation, endAgentOperation, suspendSendingAndDatabase, @@ -127,6 +128,7 @@ data AgentClient = AgentClient connMsgsQueued :: TMap ConnId Bool, smpQueueMsgQueues :: TMap (ConnId, SMPServer, SMP.SenderId) (TQueue InternalId), smpQueueMsgDeliveries :: TMap (ConnId, SMPServer, SMP.SenderId) (Async ()), + ntfNetworkOp :: TVar AgentOpState, rcvNetworkOp :: TVar AgentOpState, msgDeliveryOp :: TVar AgentOpState, sndNetworkOp :: TVar AgentOpState, @@ -140,11 +142,12 @@ data AgentClient = AgentClient lock :: TMVar () } -data AgentOperation = AORcvNetwork | AOMsgDelivery | AOSndNetwork | AODatabase +data AgentOperation = AONtfNetwork | AORcvNetwork | AOMsgDelivery | AOSndNetwork | AODatabase deriving (Eq, Show) agentOpSel :: AgentOperation -> (AgentClient -> TVar AgentOpState) agentOpSel = \case + AONtfNetwork -> ntfNetworkOp AORcvNetwork -> rcvNetworkOp AOMsgDelivery -> msgDeliveryOp AOSndNetwork -> sndNetworkOp @@ -172,6 +175,7 @@ newAgentClient InitialAgentServers {smp, ntf} agentEnv = do connMsgsQueued <- TM.empty smpQueueMsgQueues <- TM.empty smpQueueMsgDeliveries <- TM.empty + ntfNetworkOp <- newTVar $ AgentOpState False 0 rcvNetworkOp <- newTVar $ AgentOpState False 0 msgDeliveryOp <- newTVar $ AgentOpState False 0 sndNetworkOp <- newTVar $ AgentOpState False 0 @@ -182,7 +186,7 @@ newAgentClient InitialAgentServers {smp, ntf} agentEnv = do asyncClients <- newTVar [] clientId <- stateTVar (clientCounter agentEnv) $ \i -> let i' = i + 1 in (i', i') lock <- newTMVar () - return AgentClient {active, rcvQ, subQ, msgQ, smpServers, smpClients, ntfServers, ntfClients, subscrSrvrs, pendingSubscrSrvrs, subscrConns, connMsgsQueued, smpQueueMsgQueues, smpQueueMsgDeliveries, rcvNetworkOp, msgDeliveryOp, sndNetworkOp, databaseOp, agentState, getMsgLocks, reconnections, asyncClients, clientId, agentEnv, lock} + return AgentClient {active, rcvQ, subQ, msgQ, smpServers, smpClients, ntfServers, ntfClients, subscrSrvrs, pendingSubscrSrvrs, subscrConns, connMsgsQueued, smpQueueMsgQueues, smpQueueMsgDeliveries, ntfNetworkOp, rcvNetworkOp, msgDeliveryOp, sndNetworkOp, databaseOp, agentState, getMsgLocks, reconnections, asyncClients, clientId, agentEnv, lock} agentDbPath :: AgentClient -> FilePath agentDbPath AgentClient {agentEnv = Env {store = SQLiteStore {dbFilePath}}} = dbFilePath @@ -675,6 +679,7 @@ cryptoError = \case endAgentOperation :: AgentClient -> AgentOperation -> STM () endAgentOperation c op = endOperation c op $ case op of + AONtfNetwork -> pure () AORcvNetwork -> suspendOperation c AOMsgDelivery $ suspendSendingAndDatabase c @@ -724,16 +729,21 @@ beginAgentOperation c op = do -- unsafeIOToSTM $ putStrLn $ "beginOperation! " <> show op <> " " <> show (opsInProgress s + 1) writeTVar opVar $! s {opsInProgress = opsInProgress s + 1} +agentOperationBracket :: MonadUnliftIO m => AgentClient -> AgentOperation -> m a -> m a +agentOperationBracket c op action = + E.bracket + (atomically $ beginAgentOperation c op) + (\_ -> atomically $ endAgentOperation c op) + (\_ -> action) + withStore' :: AgentMonad m => AgentClient -> (DB.Connection -> IO a) -> m a withStore' c action = withStore c $ fmap Right . action withStore :: AgentMonad m => AgentClient -> (DB.Connection -> IO (Either StoreError a)) -> m a withStore c action = do st <- asks store - atomically $ beginAgentOperation c AODatabase - r <- liftIO $ withTransaction st action `E.catch` handleInternal - atomically $ endAgentOperation c AODatabase - liftEither $ first storeError r + liftEitherError storeError . agentOperationBracket c AODatabase $ + withTransaction st action `E.catch` handleInternal where handleInternal :: E.SomeException -> IO (Either StoreError a) handleInternal = pure . Left . SEInternal . bshow diff --git a/src/Simplex/Messaging/Agent/Env/SQLite.hs b/src/Simplex/Messaging/Agent/Env/SQLite.hs index ae31dc4b9..4af3c0411 100644 --- a/src/Simplex/Messaging/Agent/Env/SQLite.hs +++ b/src/Simplex/Messaging/Agent/Env/SQLite.hs @@ -68,7 +68,8 @@ data AgentConfig = AgentConfig helloTimeout :: NominalDiffTime, resubscriptionConcurrency :: Int, ntfCron :: Word16, - ntfWorkerThrottle :: Int, + ntfWorkerDelay :: Int, + ntfSMPWorkerDelay :: Int, ntfSubCheckInterval :: NominalDiffTime, ntfMaxMessages :: Int, caCertificateFile :: FilePath, @@ -103,7 +104,8 @@ defaultAgentConfig = helloTimeout = 2 * nominalDay, resubscriptionConcurrency = 16, ntfCron = 20, -- minutes - ntfWorkerThrottle = 1000000, -- microseconds + ntfWorkerDelay = 100000, -- microseconds + ntfSMPWorkerDelay = 500000, -- microseconds ntfSubCheckInterval = nominalDay, ntfMaxMessages = 4, -- CA certificate private key is not needed for initialization diff --git a/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs b/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs index aa75dac01..96a9ec669 100644 --- a/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs +++ b/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs @@ -26,6 +26,7 @@ import Control.Monad.Reader import Data.Bifunctor (first) import Data.Fixed (Fixed (MkFixed), Pico) import qualified Data.Map.Strict as M +import Data.Text (Text) import Data.Time (UTCTime, addUTCTime, diffUTCTime, getCurrentTime, nominalDiffTimeToSeconds) import Simplex.Messaging.Agent.Client import Simplex.Messaging.Agent.Env.SQLite @@ -49,14 +50,17 @@ import qualified UnliftIO.Exception as E import UnliftIO.STM runNtfSupervisor :: (MonadUnliftIO m, MonadReader Env m) => AgentClient -> m () -runNtfSupervisor c = forever . handleError $ do +runNtfSupervisor c = do ns <- asks ntfSupervisor - cmd <- atomically . readTBQueue $ ntfSubQ ns - runExceptT (processNtfSub c cmd) >>= \case - Left e -> liftIO $ print e - Right _ -> return () + forever . handleError $ do + cmd <- atomically . readTBQueue $ ntfSubQ ns + agentOperationBracket c AONtfNetwork $ + runExceptT (processNtfSub c cmd) >>= \case + Left e -> liftIO $ print e + Right _ -> return () where - handleError = E.handle $ \(e :: E.SomeException) -> logError $ "runNtfSupervisor error " <> tshow e + handleError = E.handle $ \(e :: E.SomeException) -> do + logError $ "runNtfSupervisor error " <> tshow e processNtfSub :: forall m. AgentMonad m => AgentClient -> (ConnId, NtfSupervisorCommand) -> m () processNtfSub c@AgentClient {subQ} (connId, cmd) = do @@ -152,26 +156,24 @@ withNtfServer :: AgentMonad m => AgentClient -> (NtfServer -> m ()) -> m () withNtfServer c action = getNtfServer c >>= mapM_ action runNtfWorker :: forall m. AgentMonad m => AgentClient -> NtfServer -> TMVar () -> m () -runNtfWorker c srv doWork = forever $ do - void . atomically $ readTMVar doWork - nextSub_ <- withStore' c (`getNextNtfSubNTFAction` srv) - logInfo $ "runNtfWorker, nextSub_ " <> tshow nextSub_ - case nextSub_ of - Nothing -> noWorkToDo - Just a@(NtfSubscription {connId}, _, _) -> do - ri <- asks $ reconnectInterval . config - withRetryInterval ri $ \loop -> - processAction a - `catchError` ( \e -> do - logInfo $ "runNtfWorker, error " <> tshow e - case e of - BROKER NETWORK -> loop - BROKER TIMEOUT -> loop - _ -> ntfInternalError c connId (show e) - ) - throttle <- asks $ ntfWorkerThrottle . config - liftIO $ threadDelay throttle +runNtfWorker c srv doWork = do + delay <- asks $ ntfWorkerDelay . config + forever $ do + void . atomically $ readTMVar doWork + agentOperationBracket c AONtfNetwork runNtfOperation + threadDelay delay where + runNtfOperation :: m () + runNtfOperation = do + nextSub_ <- withStore' c (`getNextNtfSubNTFAction` srv) + logInfo $ "runNtfWorker, nextSub_ " <> tshow nextSub_ + case nextSub_ of + Nothing -> noWorkToDo + Just a@(NtfSubscription {connId}, _, _) -> do + ri <- asks $ reconnectInterval . config + withRetryInterval ri $ \loop -> + processAction a + `catchError` retryOnError c "NtfWorker" loop (ntfInternalError c connId . show) noWorkToDo = void . atomically $ tryTakeTMVar doWork processAction :: (NtfSubscription, NtfSubNTFAction, NtfActionTs) -> m () processAction (sub@NtfSubscription {connId, smpServer, ntfSubId}, action, actionTs) = do @@ -230,26 +232,23 @@ runNtfWorker c srv doWork = forever $ do updateNtfSubscription db sub {ntfSubStatus = toStatus} toAction actionTs' runNtfSMPWorker :: forall m. AgentMonad m => AgentClient -> SMPServer -> TMVar () -> m () -runNtfSMPWorker c srv doWork = forever $ do - void . atomically $ readTMVar doWork - nextSub_ <- withStore' c (`getNextNtfSubSMPAction` srv) - logInfo $ "runNtfSMPWorker, nextSub_ " <> tshow nextSub_ - case nextSub_ of - Nothing -> noWorkToDo - Just a@(NtfSubscription {connId}, _, _) -> do - ri <- asks $ reconnectInterval . config - withRetryInterval ri $ \loop -> - processAction a - `catchError` ( \e -> do - logInfo $ "runNtfSMPWorker, error " <> tshow e - case e of - BROKER NETWORK -> loop - BROKER TIMEOUT -> loop - _ -> ntfInternalError c connId (show e) - ) - throttle <- asks $ ntfWorkerThrottle . config - liftIO $ threadDelay throttle +runNtfSMPWorker c srv doWork = do + delay <- asks $ ntfSMPWorkerDelay . config + forever $ do + void . atomically $ readTMVar doWork + agentOperationBracket c AONtfNetwork runNtfSMPOperation + threadDelay delay where + runNtfSMPOperation = do + nextSub_ <- withStore' c (`getNextNtfSubSMPAction` srv) + logInfo $ "runNtfSMPWorker, nextSub_ " <> tshow nextSub_ + case nextSub_ of + Nothing -> noWorkToDo + Just a@(NtfSubscription {connId}, _, _) -> do + ri <- asks $ reconnectInterval . config + withRetryInterval ri $ \loop -> + processAction a + `catchError` retryOnError c "NtfSMPWorker" loop (ntfInternalError c connId . show) noWorkToDo = void . atomically $ tryTakeTMVar doWork processAction :: (NtfSubscription, NtfSubSMPAction, NtfActionTs) -> m () processAction (sub@NtfSubscription {connId, ntfServer}, smpAction, actionTs) = do @@ -294,6 +293,19 @@ fromPico (MkFixed i) = i diffInMicros :: UTCTime -> UTCTime -> Int diffInMicros a b = (`div` 1000000) . fromInteger . fromPico . nominalDiffTimeToSeconds $ diffUTCTime a b +retryOnError :: AgentMonad m => AgentClient -> Text -> m () -> (AgentErrorType -> m ()) -> AgentErrorType -> m () +retryOnError c name loop done e = do + logError $ name <> " error: " <> tshow e + case e of + BROKER NETWORK -> retryLoop + BROKER TIMEOUT -> retryLoop + _ -> done e + where + retryLoop = do + atomically $ endAgentOperation c AONtfNetwork + atomically $ beginAgentOperation c AONtfNetwork + loop + ntfInternalError :: AgentMonad m => AgentClient -> ConnId -> String -> m () ntfInternalError c@AgentClient {subQ} connId internalErrStr = do withStore' c $ \db -> setNullNtfSubscriptionAction db connId diff --git a/tests/SMPAgentClient.hs b/tests/SMPAgentClient.hs index fd3d5b980..08cc3ea37 100644 --- a/tests/SMPAgentClient.hs +++ b/tests/SMPAgentClient.hs @@ -183,7 +183,8 @@ agentCfg = defaultTransport = (ntfTestPort, transport @TLS) }, reconnectInterval = defaultReconnectInterval {initialInterval = 50_000}, - ntfWorkerThrottle = 1000, + ntfWorkerDelay = 1000, + ntfSMPWorkerDelay = 1000, caCertificateFile = "tests/fixtures/ca.crt", privateKeyFile = "tests/fixtures/server.key", certificateFile = "tests/fixtures/server.crt" From 57d83ae42d877f748831469d311eb95fe248c5c8 Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Sat, 2 Jul 2022 12:27:09 +0400 Subject: [PATCH 116/131] ntf: notify client about ntf supervisor internal errors (#455) Co-authored-by: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> --- .../Messaging/Agent/NtfSubSupervisor.hs | 46 ++++++++++--------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs b/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs index 96a9ec669..22ee8377f 100644 --- a/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs +++ b/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs @@ -49,21 +49,24 @@ import UnliftIO.Concurrent (forkIO, threadDelay) import qualified UnliftIO.Exception as E import UnliftIO.STM -runNtfSupervisor :: (MonadUnliftIO m, MonadReader Env m) => AgentClient -> m () +runNtfSupervisor :: forall m. (MonadUnliftIO m, MonadReader Env m) => AgentClient -> m () runNtfSupervisor c = do ns <- asks ntfSupervisor - forever . handleError $ do - cmd <- atomically . readTBQueue $ ntfSubQ ns - agentOperationBracket c AONtfNetwork $ + forever $ do + cmd@(connId, _) <- atomically . readTBQueue $ ntfSubQ ns + handleError connId . agentOperationBracket c AONtfNetwork $ runExceptT (processNtfSub c cmd) >>= \case - Left e -> liftIO $ print e + Left e -> notifyErr connId e Right _ -> return () where - handleError = E.handle $ \(e :: E.SomeException) -> do + handleError :: ConnId -> m () -> m () + handleError connId = E.handle $ \(e :: E.SomeException) -> do logError $ "runNtfSupervisor error " <> tshow e + notifyErr connId e + notifyErr connId e = notifyInternalError c connId $ "runNtfSupervisor error " <> show e processNtfSub :: forall m. AgentMonad m => AgentClient -> (ConnId, NtfSupervisorCommand) -> m () -processNtfSub c@AgentClient {subQ} (connId, cmd) = do +processNtfSub c (connId, cmd) = do logInfo $ "processNtfSub - connId = " <> tshow connId <> " - cmd = " <> tshow cmd case cmd of NSCCreate -> do @@ -127,7 +130,7 @@ processNtfSub c@AgentClient {subQ} (connId, cmd) = do ts <- liftIO getCurrentTime withStore' c $ \db -> supervisorUpdateNtfSubAction db connId (NtfSubSMPAction NSASmpDelete) ts addNtfSMPWorker smpServer - _ -> err "NSCSmpDelete - no rcv queue" + _ -> notifyInternalError c connId "NSCSmpDelete - no rcv queue" NSCNtfWorker ntfServer -> addNtfNTFWorker ntfServer NSCNtfSMPWorker smpServer -> @@ -149,8 +152,6 @@ processNtfSub c@AgentClient {subQ} (connId, cmd) = do atomically $ TM.insert srv (doWork, worker) ws Just (doWork, _) -> void . atomically $ tryPutTMVar doWork () - err :: String -> m () - err internalErrStr = atomically $ writeTBQueue subQ ("", connId, AP.ERR $ AP.INTERNAL internalErrStr) withNtfServer :: AgentMonad m => AgentClient -> (NtfServer -> m ()) -> m () withNtfServer c action = getNtfServer c >>= mapM_ action @@ -173,7 +174,7 @@ runNtfWorker c srv doWork = do ri <- asks $ reconnectInterval . config withRetryInterval ri $ \loop -> processAction a - `catchError` retryOnError c "NtfWorker" loop (ntfInternalError c connId . show) + `catchError` retryOnError c "NtfWorker" loop (workerInternalError c connId . show) noWorkToDo = void . atomically $ tryTakeTMVar doWork processAction :: (NtfSubscription, NtfSubNTFAction, NtfActionTs) -> m () processAction (sub@NtfSubscription {connId, smpServer, ntfSubId}, action, actionTs) = do @@ -191,8 +192,8 @@ runNtfWorker c srv doWork = do let actionTs' = addUTCTime 30 ts withStore' c $ \db -> updateNtfSubscription db sub {ntfSubId = Just nSubId, ntfSubStatus = NASCreated NSNew} (NtfSubNTFAction NSACheck) actionTs' - _ -> ntfInternalError c connId "NSACreate - no notifier queue credentials" - _ -> ntfInternalError c connId "NSACreate - no active token" + _ -> workerInternalError c connId "NSACreate - no notifier queue credentials" + _ -> workerInternalError c connId "NSACreate - no active token" NSACheck -> getNtfToken >>= \case Just tkn -> @@ -206,10 +207,10 @@ runNtfWorker c srv doWork = do updateNtfSubscription db sub {ntfServer, ntfQueueId = Nothing, ntfSubId = Nothing, ntfSubStatus = NASNew} (NtfSubSMPAction NSASmpKey) ts ns <- asks ntfSupervisor atomically $ writeTBQueue (ntfSubQ ns) (connId, NSCNtfSMPWorker smpServer) - _ -> ntfInternalError c connId "NSACheck - failed to reset subscription, notification server not configured" + _ -> workerInternalError c connId "NSACheck - failed to reset subscription, notification server not configured" status -> updateSubNextCheck ts status - Nothing -> ntfInternalError c connId "NSACheck - no subscription ID" - _ -> ntfInternalError c connId "NSACheck - no active token" + Nothing -> workerInternalError c connId "NSACheck - no subscription ID" + _ -> workerInternalError c connId "NSACheck - no active token" NSADelete -> case ntfSubId of Just nSubId -> (getNtfToken >>= \tkn -> forM_ tkn $ agentNtfDeleteSubscription c nSubId) @@ -248,7 +249,7 @@ runNtfSMPWorker c srv doWork = do ri <- asks $ reconnectInterval . config withRetryInterval ri $ \loop -> processAction a - `catchError` retryOnError c "NtfSMPWorker" loop (ntfInternalError c connId . show) + `catchError` retryOnError c "NtfSMPWorker" loop (workerInternalError c connId . show) noWorkToDo = void . atomically $ tryTakeTMVar doWork processAction :: (NtfSubscription, NtfSubSMPAction, NtfActionTs) -> m () processAction (sub@NtfSubscription {connId, ntfServer}, smpAction, actionTs) = do @@ -269,7 +270,7 @@ runNtfSMPWorker c srv doWork = do updateNtfSubscription db sub {ntfQueueId = Just notifierId, ntfSubStatus = NASKey} (NtfSubNTFAction NSACreate) ts ns <- asks ntfSupervisor atomically $ sendNtfSubCommand ns (connId, NSCNtfWorker ntfServer) - _ -> ntfInternalError c connId "NSASmpKey - no active token" + _ -> workerInternalError c connId "NSASmpKey - no active token" NSASmpDelete -> do rq_ <- withStore' c $ \db -> do setRcvQueueNtfCreds db connId Nothing @@ -306,10 +307,13 @@ retryOnError c name loop done e = do atomically $ beginAgentOperation c AONtfNetwork loop -ntfInternalError :: AgentMonad m => AgentClient -> ConnId -> String -> m () -ntfInternalError c@AgentClient {subQ} connId internalErrStr = do +workerInternalError :: AgentMonad m => AgentClient -> ConnId -> String -> m () +workerInternalError c connId internalErrStr = do withStore' c $ \db -> setNullNtfSubscriptionAction db connId - atomically $ writeTBQueue subQ ("", connId, AP.ERR $ AP.INTERNAL internalErrStr) + notifyInternalError c connId internalErrStr + +notifyInternalError :: (MonadUnliftIO m) => AgentClient -> ConnId -> String -> m () +notifyInternalError AgentClient {subQ} connId internalErrStr = atomically $ writeTBQueue subQ ("", connId, AP.ERR $ AP.INTERNAL internalErrStr) getNtfToken :: AgentMonad m => m (Maybe NtfToken) getNtfToken = do From 2c52c514c0b3afdcfa5d69854ddff99cc58f709a Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Sat, 2 Jul 2022 15:07:20 +0100 Subject: [PATCH 117/131] ntf: fix TRPL to send verification to the new token (#456) --- src/Simplex/Messaging/Notifications/Server.hs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Simplex/Messaging/Notifications/Server.hs b/src/Simplex/Messaging/Notifications/Server.hs index ffb475988..fcdc0e085 100644 --- a/src/Simplex/Messaging/Notifications/Server.hs +++ b/src/Simplex/Messaging/Notifications/Server.hs @@ -376,8 +376,9 @@ client NtfServerClient {rcvQ, sndQ} NtfSubscriber {newSubQ, smpAgent = ca} NtfPu atomically $ do removeTokenRegistration st tkn writeTVar tknStatus NTRegistered - addNtfToken st tknId tkn {token = token', tknRegCode = regCode} - writeTBQueue pushQ (tkn, PNVerification regCode) + let tkn' = tkn {token = token', tknRegCode = regCode} + addNtfToken st tknId tkn' + writeTBQueue pushQ (tkn', PNVerification regCode) withNtfLog $ \s -> logUpdateToken s tknId token' regCode pure NROk TDEL -> do From 40ae809033de1d7712c35196dc71563873a5b258 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Sat, 2 Jul 2022 15:08:41 +0100 Subject: [PATCH 118/131] v3.0.0-beta.3 / v1.0.0-beta.3 (ntf) --- apps/ntf-server/Main.hs | 2 +- src/Simplex/Messaging/Transport.hs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/ntf-server/Main.hs b/apps/ntf-server/Main.hs index cb54ca2e9..94c7f7edd 100644 --- a/apps/ntf-server/Main.hs +++ b/apps/ntf-server/Main.hs @@ -42,7 +42,7 @@ ntfServerCLIConfig = fingerprintFile = combine cfgPath "fingerprint", defaultServerPort = "443", executableName = "ntf-server", - serverVersion = "SMP notifications server v1.0.0-beta.2", + serverVersion = "SMP notifications server v1.0.0-beta.3", mkIniFile = \enableStoreLog defaultServerPort -> "[STORE_LOG]\n\ \# The server uses STM memory for persistence,\n\ diff --git a/src/Simplex/Messaging/Transport.hs b/src/Simplex/Messaging/Transport.hs index ce5a37868..99a124cf5 100644 --- a/src/Simplex/Messaging/Transport.hs +++ b/src/Simplex/Messaging/Transport.hs @@ -96,7 +96,7 @@ supportedSMPServerVRange :: VersionRange supportedSMPServerVRange = mkVersionRange 1 2 simplexMQVersion :: String -simplexMQVersion = "3.0.0-beta.2" +simplexMQVersion = "3.0.0-beta.3" -- * Transport connection class From 4c0164c49effe8d1b3fd98aca184245a8b4cac93 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Sun, 3 Jul 2022 13:05:16 +0100 Subject: [PATCH 119/131] fix: support msgFlag NULL in sql (#458) --- src/Simplex/Messaging/Agent/Store/SQLite.hs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Simplex/Messaging/Agent/Store/SQLite.hs b/src/Simplex/Messaging/Agent/Store/SQLite.hs index 82bacf5cc..06066629f 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite.hs @@ -536,8 +536,10 @@ getPendingMsgData db connId msgId = do WHERE m.conn_id = ? AND m.internal_id = ? |] (connId, msgId) - pendingMsgData :: (AgentMessageType, MsgFlags, MsgBody, InternalTs) -> PendingMsgData - pendingMsgData (msgType, msgFlags, msgBody, internalTs) = PendingMsgData {msgId, msgType, msgFlags, msgBody, internalTs} + pendingMsgData :: (AgentMessageType, Maybe MsgFlags, MsgBody, InternalTs) -> PendingMsgData + pendingMsgData (msgType, msgFlags_, msgBody, internalTs) = + let msgFlags = fromMaybe SMP.noMsgFlags msgFlags_ + in PendingMsgData {msgId, msgType, msgFlags, msgBody, internalTs} getPendingMsgs :: DB.Connection -> ConnId -> IO [InternalId] getPendingMsgs db connId = From bc26dc1d682a56597c899e5363d4290ef5bd4950 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Mon, 4 Jul 2022 10:45:35 +0100 Subject: [PATCH 120/131] save and restore server stats on restart (#460) --- apps/smp-server/Main.hs | 1 + simplexmq.cabal | 1 + src/Simplex/Messaging/Encoding/String.hs | 22 ++++- src/Simplex/Messaging/Server.hs | 45 ++++++++-- src/Simplex/Messaging/Server/Env/STM.hs | 32 +------ src/Simplex/Messaging/Server/Stats.hs | 101 +++++++++++++++++++++++ tests/SMPClient.hs | 8 +- 7 files changed, 171 insertions(+), 39 deletions(-) create mode 100644 src/Simplex/Messaging/Server/Stats.hs diff --git a/apps/smp-server/Main.hs b/apps/smp-server/Main.hs index 2e6661802..e5a9dbc9d 100644 --- a/apps/smp-server/Main.hs +++ b/apps/smp-server/Main.hs @@ -103,6 +103,7 @@ smpServerCLIConfig = else Nothing, logStatsInterval = Just 86400, -- seconds logStatsStartTime = 0, -- seconds from 00:00 UTC + serverStatsFile = Just $ combine logPath "smp-server-stats.log", smpServerVRange = supportedSMPServerVRange } } diff --git a/simplexmq.cabal b/simplexmq.cabal index 977d122e0..aa1b64e13 100644 --- a/simplexmq.cabal +++ b/simplexmq.cabal @@ -75,6 +75,7 @@ library Simplex.Messaging.Server.MsgStore.STM Simplex.Messaging.Server.QueueStore Simplex.Messaging.Server.QueueStore.STM + Simplex.Messaging.Server.Stats Simplex.Messaging.Server.StoreLog Simplex.Messaging.TMap Simplex.Messaging.Transport diff --git a/src/Simplex/Messaging/Encoding/String.hs b/src/Simplex/Messaging/Encoding/String.hs index ed9473983..2ff374e46 100644 --- a/src/Simplex/Messaging/Encoding/String.hs +++ b/src/Simplex/Messaging/Encoding/String.hs @@ -28,9 +28,13 @@ import qualified Data.ByteString.Char8 as B import Data.Char (isAlphaNum) import Data.Int (Int64) import qualified Data.List.NonEmpty as L +import Data.Set (Set) +import qualified Data.Set as S import Data.Text (Text) import Data.Text.Encoding (decodeLatin1, encodeUtf8) +import Data.Time.Clock (UTCTime) import Data.Time.Clock.System (SystemTime (..)) +import Data.Time.Format.ISO8601 import Data.Word (Word16) import Simplex.Messaging.Encoding import Simplex.Messaging.Parsers (parseAll) @@ -101,14 +105,26 @@ instance StrEncoding Bool where strP = smpP {-# INLINE strP #-} +instance StrEncoding Int where + strEncode = B.pack . show + {-# INLINE strEncode #-} + strP = A.decimal + {-# INLINE strP #-} + instance StrEncoding Int64 where strEncode = B.pack . show + {-# INLINE strEncode #-} strP = A.decimal + {-# INLINE strP #-} instance StrEncoding SystemTime where strEncode = strEncode . systemSeconds strP = MkSystemTime <$> strP <*> pure 0 +instance StrEncoding UTCTime where + strEncode = B.pack . iso8601Show + strP = maybe (Left "bad UTCTime") Right . iso8601ParseM . B.unpack <$?> A.takeTill (\c -> c == ' ' || c == '\n') + -- lists encode/parse as comma-separated strings strEncodeList :: StrEncoding a => [a] -> ByteString strEncodeList = B.intercalate "," . map strEncode @@ -121,8 +137,12 @@ instance StrEncoding a => StrEncoding (L.NonEmpty a) where strEncode = strEncodeList . L.toList strP = L.fromList <$> listItem `A.sepBy1'` A.char ',' +instance (StrEncoding a, Ord a) => StrEncoding (Set a) where + strEncode = strEncodeList . S.toList + strP = S.fromList <$> listItem `A.sepBy'` A.char ',' + listItem :: StrEncoding a => Parser a -listItem = parseAll strP <$?> A.takeTill (== ',') +listItem = parseAll strP <$?> A.takeTill (\c -> c == ',' || c == ' ' || c == '\n') instance (StrEncoding a, StrEncoding b) => StrEncoding (a, b) where strEncode (a, b) = B.unwords [strEncode a, strEncode b] diff --git a/src/Simplex/Messaging/Server.hs b/src/Simplex/Messaging/Server.hs index 8de05d83f..9d82ec6d7 100644 --- a/src/Simplex/Messaging/Server.hs +++ b/src/Simplex/Messaging/Server.hs @@ -50,10 +50,12 @@ import Data.Maybe (isNothing) import Data.Set (Set) import qualified Data.Set as S import qualified Data.Text as T +import Data.Text.Encoding (decodeLatin1) import Data.Time.Calendar.Month.Compat (pattern MonthDay) import Data.Time.Calendar.OrdinalDate (mondayStartWeek) import Data.Time.Clock (UTCTime (..), diffTimeToPicoseconds, getCurrentTime) import Data.Time.Clock.System (SystemTime (..), getSystemTime) +import Data.Time.Format.ISO8601 (iso8601Show) import Data.Type.Equality import Network.Socket (ServiceName) import qualified Simplex.Messaging.Crypto as C @@ -66,6 +68,7 @@ import Simplex.Messaging.Server.MsgStore import Simplex.Messaging.Server.MsgStore.STM (MsgQueue) import Simplex.Messaging.Server.QueueStore import Simplex.Messaging.Server.QueueStore.STM (QueueStore) +import Simplex.Messaging.Server.Stats import Simplex.Messaging.Server.StoreLog import Simplex.Messaging.TMap (TMap) import qualified Simplex.Messaging.TMap as TM @@ -98,13 +101,14 @@ smpServer :: forall m. (MonadUnliftIO m, MonadReader Env m) => TMVar Bool -> m ( smpServer started = do s <- asks server cfg@ServerConfig {transports} <- asks config + restoreServerStats restoreServerMessages raceAny_ ( serverThread s subscribedQ subscribers subscriptions cancelSub : serverThread s ntfSubscribedQ notifiers ntfSubscriptions (\_ -> pure ()) : map runServer transports <> expireMessagesThread_ cfg <> serverStatsThread_ cfg ) - `finally` (withLog closeStoreLog >> saveServerMessages) + `finally` (withLog closeStoreLog >> saveServerMessages >> saveServerStats) where runServer :: (ServiceName, ATransport) -> m () runServer (tcpPort, ATransport t) = do @@ -182,7 +186,7 @@ smpServer started = do MonthDay _ mDay = day (dayMsgQueues', weekMsgQueues', monthMsgQueues') <- atomically $ (,,) <$> periodCount 1 dayMsgQueues <*> periodCount wDay weekMsgQueues <*> periodCount mDay monthMsgQueues - logInfo . T.pack $ intercalate "," [show fromTime', show qCreated', show qSecured', show qDeleted', show msgSent', show msgRecv', show dayMsgQueues', weekMsgQueues', monthMsgQueues'] + logInfo . T.pack $ intercalate "," [iso8601Show fromTime', show qCreated', show qSecured', show qDeleted', show msgSent', show msgRecv', show dayMsgQueues', weekMsgQueues', monthMsgQueues'] threadDelay interval where periodCount :: Int -> TVar (Set RecipientId) -> STM String @@ -638,33 +642,58 @@ randomId n = do gVar <- asks idsDrg atomically (C.pseudoRandomBytes n gVar) -saveServerMessages :: forall m. (MonadUnliftIO m, MonadReader Env m) => m () +saveServerMessages :: (MonadUnliftIO m, MonadReader Env m) => m () saveServerMessages = asks (storeMsgsFile . config) >>= mapM_ saveMessages where saveMessages f = do - liftIO $ putStrLn $ "saving messages to file " <> f + logInfo $ "saving messages to file " <> T.pack f ms <- asks msgStore liftIO . withFile f WriteMode $ \h -> readTVarIO ms >>= mapM_ (saveQueueMsgs ms h) . M.keys + logInfo "messages saved" where saveQueueMsgs ms h rId = atomically (flushMsgQueue ms rId) >>= mapM_ (B.hPutStrLn h . strEncode . MsgLogRecord rId) -restoreServerMessages :: forall m. (MonadUnliftIO m, MonadReader Env m) => m () +restoreServerMessages :: (MonadUnliftIO m, MonadReader Env m) => m () restoreServerMessages = asks (storeMsgsFile . config) >>= mapM_ restoreMessages where restoreMessages f = whenM (doesFileExist f) $ do - liftIO $ putStrLn $ "restoring messages from file " <> f + logInfo $ "restoring messages from file " <> T.pack f ms <- asks msgStore quota <- asks $ msgQueueQuota . config liftIO $ mapM_ (restoreMsg ms quota) . B.lines =<< B.readFile f renameFile f $ f <> ".bak" + logInfo $ "messages restored" where restoreMsg ms quota s = case strDecode s of - Left e -> B.putStrLn $ "message parsing error (" <> B.pack e <> "): " <> B.take 100 s + Left e -> logError . decodeLatin1 $ "message parsing error (" <> B.pack e <> "): " <> B.take 100 s Right (MsgLogRecord rId msg) -> do full <- atomically $ do q <- getMsgQueue ms rId quota ifM (isFull q) (pure True) (writeMsg q msg $> False) - when full . B.putStrLn $ "message queue " <> strEncode rId <> " is full, message not restored: " <> strEncode (msgId (msg :: Message)) + when full . logError . decodeLatin1 $ "message queue " <> strEncode rId <> " is full, message not restored: " <> strEncode (msgId (msg :: Message)) + +saveServerStats :: (MonadUnliftIO m, MonadReader Env m) => m () +saveServerStats = + asks (serverStatsFile . config) + >>= mapM_ (\f -> asks serverStats >>= atomically . getServerStatsData >>= liftIO . saveStats f) + where + saveStats f stats = do + logInfo $ "saving server stats to file " <> T.pack f + B.writeFile f $ strEncode stats + logInfo $ "server stats saved" + +restoreServerStats :: (MonadUnliftIO m, MonadReader Env m) => m () +restoreServerStats = asks (serverStatsFile . config) >>= mapM_ restoreStats + where + restoreStats f = whenM (doesFileExist f) $ do + logInfo $ "restoring server stats from file " <> T.pack f + liftIO (strDecode <$> B.readFile f) >>= \case + Right d -> do + s <- asks serverStats + atomically $ setServerStatsData s d + renameFile f $ f <> ".bak" + logInfo "server stats restored" + Left e -> logInfo $ "error restoring server stats: " <> T.pack e diff --git a/src/Simplex/Messaging/Server/Env/STM.hs b/src/Simplex/Messaging/Server/Env/STM.hs index b64495e0e..565438eab 100644 --- a/src/Simplex/Messaging/Server/Env/STM.hs +++ b/src/Simplex/Messaging/Server/Env/STM.hs @@ -12,9 +12,7 @@ import Crypto.Random import Data.ByteString.Char8 (ByteString) import Data.Map.Strict (Map) import qualified Data.Map.Strict as M -import Data.Set (Set) -import qualified Data.Set as S -import Data.Time.Clock (UTCTime, getCurrentTime) +import Data.Time.Clock (getCurrentTime) import Data.Time.Clock.System (SystemTime) import Data.X509.Validation (Fingerprint (..)) import Network.Socket (ServiceName) @@ -26,6 +24,7 @@ import Simplex.Messaging.Server.Expiration import Simplex.Messaging.Server.MsgStore.STM import Simplex.Messaging.Server.QueueStore (NtfCreds (..), QueueRec (..)) import Simplex.Messaging.Server.QueueStore.STM +import Simplex.Messaging.Server.Stats import Simplex.Messaging.Server.StoreLog import Simplex.Messaging.TMap (TMap) import qualified Simplex.Messaging.TMap as TM @@ -57,6 +56,8 @@ data ServerConfig = ServerConfig -- | time of the day when the stats are logged first, to log at consistent times, -- irrespective of when the server is started (seconds from 00:00 UTC) logStatsStartTime :: Int, + -- | file to save and restore stats + serverStatsFile :: Maybe FilePath, -- | CA certificate private key is not needed for initialization caCertificateFile :: FilePath, privateKeyFile :: FilePath, @@ -108,18 +109,6 @@ data Client = Client activeAt :: TVar SystemTime } -data ServerStats = ServerStats - { qCreated :: TVar Int, - qSecured :: TVar Int, - qDeleted :: TVar Int, - msgSent :: TVar Int, - msgRecv :: TVar Int, - dayMsgQueues :: TVar (Set RecipientId), - weekMsgQueues :: TVar (Set RecipientId), - monthMsgQueues :: TVar (Set RecipientId), - fromTime :: TVar UTCTime - } - data SubscriptionThread = NoSub | SubPending | SubThread (Weak ThreadId) | ProhibitSub data Sub = Sub @@ -145,19 +134,6 @@ newClient qSize sessionId ts = do activeAt <- newTVar ts return Client {subscriptions, ntfSubscriptions, rcvQ, sndQ, sessionId, connected, activeAt} -newServerStats :: UTCTime -> STM ServerStats -newServerStats ts = do - qCreated <- newTVar 0 - qSecured <- newTVar 0 - qDeleted <- newTVar 0 - msgSent <- newTVar 0 - msgRecv <- newTVar 0 - dayMsgQueues <- newTVar S.empty - weekMsgQueues <- newTVar S.empty - monthMsgQueues <- newTVar S.empty - fromTime <- newTVar ts - pure ServerStats {qCreated, qSecured, qDeleted, msgSent, msgRecv, dayMsgQueues, weekMsgQueues, monthMsgQueues, fromTime} - newSubscription :: SubscriptionThread -> STM Sub newSubscription subThread = do delivered <- newEmptyTMVar diff --git a/src/Simplex/Messaging/Server/Stats.hs b/src/Simplex/Messaging/Server/Stats.hs new file mode 100644 index 000000000..f99dc135e --- /dev/null +++ b/src/Simplex/Messaging/Server/Stats.hs @@ -0,0 +1,101 @@ +{-# LANGUAGE NamedFieldPuns #-} +{-# LANGUAGE OverloadedStrings #-} + +module Simplex.Messaging.Server.Stats where + +import Control.Applicative (optional) +import qualified Data.Attoparsec.ByteString.Char8 as A +import qualified Data.ByteString.Char8 as B +import Data.Set (Set) +import qualified Data.Set as S +import Data.Time.Clock (UTCTime) +import Simplex.Messaging.Encoding.String +import Simplex.Messaging.Protocol (RecipientId) +import UnliftIO.STM + +data ServerStats = ServerStats + { fromTime :: TVar UTCTime, + qCreated :: TVar Int, + qSecured :: TVar Int, + qDeleted :: TVar Int, + msgSent :: TVar Int, + msgRecv :: TVar Int, + dayMsgQueues :: TVar (Set RecipientId), + weekMsgQueues :: TVar (Set RecipientId), + monthMsgQueues :: TVar (Set RecipientId) + } + +data ServerStatsData = ServerStatsData + { _fromTime :: UTCTime, + _qCreated :: Int, + _qSecured :: Int, + _qDeleted :: Int, + _msgSent :: Int, + _msgRecv :: Int, + _dayMsgQueues :: Set RecipientId, + _weekMsgQueues :: Set RecipientId, + _monthMsgQueues :: Set RecipientId + } + +newServerStats :: UTCTime -> STM ServerStats +newServerStats ts = do + fromTime <- newTVar ts + qCreated <- newTVar 0 + qSecured <- newTVar 0 + qDeleted <- newTVar 0 + msgSent <- newTVar 0 + msgRecv <- newTVar 0 + dayMsgQueues <- newTVar S.empty + weekMsgQueues <- newTVar S.empty + monthMsgQueues <- newTVar S.empty + pure ServerStats {fromTime, qCreated, qSecured, qDeleted, msgSent, msgRecv, dayMsgQueues, weekMsgQueues, monthMsgQueues} + +getServerStatsData :: ServerStats -> STM ServerStatsData +getServerStatsData s = do + _fromTime <- readTVar $ fromTime s + _qCreated <- readTVar $ qCreated s + _qSecured <- readTVar $ qSecured s + _qDeleted <- readTVar $ qDeleted s + _msgSent <- readTVar $ msgSent s + _msgRecv <- readTVar $ msgRecv s + _dayMsgQueues <- readTVar $ dayMsgQueues s + _weekMsgQueues <- readTVar $ weekMsgQueues s + _monthMsgQueues <- readTVar $ monthMsgQueues s + pure ServerStatsData {_fromTime, _qCreated, _qSecured, _qDeleted, _msgSent, _msgRecv, _dayMsgQueues, _weekMsgQueues, _monthMsgQueues} + +setServerStatsData :: ServerStats -> ServerStatsData -> STM () +setServerStatsData s d = do + writeTVar (fromTime s) (_fromTime d) + writeTVar (qCreated s) (_qCreated d) + writeTVar (qSecured s) (_qSecured d) + writeTVar (qDeleted s) (_qDeleted d) + writeTVar (msgSent s) (_msgSent d) + writeTVar (msgRecv s) (_msgRecv d) + writeTVar (dayMsgQueues s) (_dayMsgQueues d) + writeTVar (weekMsgQueues s) (_weekMsgQueues d) + writeTVar (monthMsgQueues s) (_monthMsgQueues d) + +instance StrEncoding ServerStatsData where + strEncode ServerStatsData {_fromTime, _qCreated, _qSecured, _qDeleted, _msgSent, _msgRecv, _dayMsgQueues, _weekMsgQueues, _monthMsgQueues} = + B.unlines + [ "fromTime=" <> strEncode _fromTime, + "qCreated=" <> strEncode _qCreated, + "qSecured=" <> strEncode _qSecured, + "qDeleted=" <> strEncode _qDeleted, + "msgSent=" <> strEncode _msgSent, + "msgRecv=" <> strEncode _msgRecv, + "dayMsgQueues=" <> strEncode _dayMsgQueues, + "weekMsgQueues=" <> strEncode _weekMsgQueues, + "monthMsgQueues=" <> strEncode _monthMsgQueues + ] + strP = do + _fromTime <- "fromTime=" *> strP <* A.endOfLine + _qCreated <- "qCreated=" *> strP <* A.endOfLine + _qSecured <- "qSecured=" *> strP <* A.endOfLine + _qDeleted <- "qDeleted=" *> strP <* A.endOfLine + _msgSent <- "msgSent=" *> strP <* A.endOfLine + _msgRecv <- "msgRecv=" *> strP <* A.endOfLine + _dayMsgQueues <- "dayMsgQueues=" *> strP <* A.endOfLine + _weekMsgQueues <- "weekMsgQueues=" *> strP <* A.endOfLine + _monthMsgQueues <- "monthMsgQueues=" *> strP <* optional A.endOfLine + pure ServerStatsData {_fromTime, _qCreated, _qSecured, _qDeleted, _msgSent, _msgRecv, _dayMsgQueues, _weekMsgQueues, _monthMsgQueues} diff --git a/tests/SMPClient.hs b/tests/SMPClient.hs index 4761e2dae..c3927fa27 100644 --- a/tests/SMPClient.hs +++ b/tests/SMPClient.hs @@ -46,6 +46,9 @@ testStoreLogFile = "tests/tmp/smp-server-store.log" testStoreMsgsFile :: FilePath testStoreMsgsFile = "tests/tmp/smp-server-messages.log" +testServerStatsFile :: FilePath +testServerStatsFile = "tests/tmp/smp-server-stats.log" + testSMPClient :: (Transport c, MonadUnliftIO m) => (THandle c -> m a) -> m a testSMPClient client = runTransportClient testHost testPort (Just testKeyHash) (Just defaultKeepAliveOpts) $ \h -> @@ -69,6 +72,7 @@ cfg = inactiveClientExpiration = Just defaultInactiveClientExpiration, logStatsInterval = Nothing, logStatsStartTime = 0, + serverStatsFile = Nothing, caCertificateFile = "tests/fixtures/ca.crt", privateKeyFile = "tests/fixtures/server.key", certificateFile = "tests/fixtures/server.crt", @@ -76,10 +80,10 @@ cfg = } withSmpServerStoreMsgLogOn :: (MonadUnliftIO m, MonadRandom m) => ATransport -> ServiceName -> (ThreadId -> m a) -> m a -withSmpServerStoreMsgLogOn t = withSmpServerConfigOn t cfg {storeLogFile = Just testStoreLogFile, storeMsgsFile = Just testStoreMsgsFile} +withSmpServerStoreMsgLogOn t = withSmpServerConfigOn t cfg {storeLogFile = Just testStoreLogFile, storeMsgsFile = Just testStoreMsgsFile, serverStatsFile = Just testServerStatsFile} withSmpServerStoreLogOn :: (MonadUnliftIO m, MonadRandom m) => ATransport -> ServiceName -> (ThreadId -> m a) -> m a -withSmpServerStoreLogOn t = withSmpServerConfigOn t cfg {storeLogFile = Just testStoreLogFile} +withSmpServerStoreLogOn t = withSmpServerConfigOn t cfg {storeLogFile = Just testStoreLogFile, serverStatsFile = Just testServerStatsFile} withSmpServerConfigOn :: (MonadUnliftIO m, MonadRandom m) => ATransport -> ServerConfig -> ServiceName -> (ThreadId -> m a) -> m a withSmpServerConfigOn t cfg' port' = From 36b4179b8bca1cf861e7aa8d2f9e67e984d841ba Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Mon, 4 Jul 2022 10:48:04 +0100 Subject: [PATCH 121/131] update version v3.0.0-beta.4 --- apps/ntf-server/Main.hs | 2 +- src/Simplex/Messaging/Transport.hs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/ntf-server/Main.hs b/apps/ntf-server/Main.hs index 94c7f7edd..f56797823 100644 --- a/apps/ntf-server/Main.hs +++ b/apps/ntf-server/Main.hs @@ -42,7 +42,7 @@ ntfServerCLIConfig = fingerprintFile = combine cfgPath "fingerprint", defaultServerPort = "443", executableName = "ntf-server", - serverVersion = "SMP notifications server v1.0.0-beta.3", + serverVersion = "SMP notifications server v1.0.0-beta.4", mkIniFile = \enableStoreLog defaultServerPort -> "[STORE_LOG]\n\ \# The server uses STM memory for persistence,\n\ diff --git a/src/Simplex/Messaging/Transport.hs b/src/Simplex/Messaging/Transport.hs index 99a124cf5..dec917e95 100644 --- a/src/Simplex/Messaging/Transport.hs +++ b/src/Simplex/Messaging/Transport.hs @@ -96,7 +96,7 @@ supportedSMPServerVRange :: VersionRange supportedSMPServerVRange = mkVersionRange 1 2 simplexMQVersion :: String -simplexMQVersion = "3.0.0-beta.3" +simplexMQVersion = "3.0.0-beta.4" -- * Transport connection class From e3d2d6fc91bab9f3aa1e6a18d4af97adb685d0b5 Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Mon, 4 Jul 2022 14:26:27 +0400 Subject: [PATCH 122/131] fix NDELETE parsing (#461) --- src/Simplex/Messaging/Server/StoreLog.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Simplex/Messaging/Server/StoreLog.hs b/src/Simplex/Messaging/Server/StoreLog.hs index 3d8f9625d..1f15132f0 100644 --- a/src/Simplex/Messaging/Server/StoreLog.hs +++ b/src/Simplex/Messaging/Server/StoreLog.hs @@ -89,7 +89,7 @@ instance StrEncoding StoreLogRecord where <|> "SECURE " *> (SecureQueue <$> strP_ <*> strP) <|> "NOTIFIER " *> (AddNotifier <$> strP_ <*> strP) <|> "DELETE " *> (DeleteQueue <$> strP) - <|> "NDELETE" *> (DeleteNotifier <$> strP) + <|> "NDELETE " *> (DeleteNotifier <$> strP) openWriteStoreLog :: FilePath -> IO (StoreLog 'WriteMode) openWriteStoreLog f = do From 9c1b43791c3f6e8a6e094a77c36c22ca94630003 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Tue, 5 Jul 2022 21:08:05 +0100 Subject: [PATCH 123/131] SMP v3: encrypt message timestamp and flags together with the body between server and recipient (#457) * SMP v3: encrypt message timestamp and flags together with the body between server and recipient * v3 tests * update protocol doc * add test for max size message * delay in notifications test * simplify v3 * encrypt server message to the recipient when sent * refactor * exit on error restoring the messages * refactor, increase test timeout * style * add prints to the test * remove error from unsafeMaxLenBS * update protocol * lint, improve test, change func param --- package.yaml | 2 +- protocol/simplex-messaging.md | 75 ++--- simplexmq.cabal | 10 +- src/Simplex/Messaging/Agent.hs | 8 +- src/Simplex/Messaging/Agent/Client.hs | 25 +- src/Simplex/Messaging/Agent/Protocol.hs | 2 +- src/Simplex/Messaging/Client.hs | 38 ++- src/Simplex/Messaging/Crypto.hs | 63 +++- src/Simplex/Messaging/Encoding.hs | 6 + src/Simplex/Messaging/Notifications/Server.hs | 2 +- .../Messaging/Notifications/Server/Env.hs | 1 - src/Simplex/Messaging/Protocol.hs | 127 +++++++- src/Simplex/Messaging/Server.hs | 151 ++++++---- src/Simplex/Messaging/Server/Env/STM.hs | 8 +- src/Simplex/Messaging/Server/MsgStore.hs | 36 +-- src/Simplex/Messaging/Server/MsgStore/STM.hs | 6 +- src/Simplex/Messaging/Transport.hs | 2 +- src/Simplex/Messaging/Transport/Client.hs | 1 - tests/AgentTests.hs | 4 +- tests/AgentTests/FunctionalAPITests.hs | 2 +- tests/AgentTests/NotificationTests.hs | 8 +- tests/NtfServerTests.hs | 9 +- tests/SMPClient.hs | 7 + tests/ServerTests.hs | 273 ++++++++++++++---- 24 files changed, 618 insertions(+), 248 deletions(-) diff --git a/package.yaml b/package.yaml index 3c2963a5f..bc5eb8ca9 100644 --- a/package.yaml +++ b/package.yaml @@ -28,7 +28,7 @@ dependencies: - asn1-types == 0.3.* - async == 2.2.* - attoparsec == 0.14.* - - base >= 4.7 && < 5 + - base >= 4.14 && < 5 - base64-bytestring >= 1.0 && < 1.3 - bytestring == 0.10.* - case-insensitive == 1.2.* diff --git a/protocol/simplex-messaging.md b/protocol/simplex-messaging.md index e48f31b68..3c2cad22c 100644 --- a/protocol/simplex-messaging.md +++ b/protocol/simplex-messaging.md @@ -147,61 +147,61 @@ To create and start using a simplex queue Alice and Bob follow these steps: 1. Alice creates a simplex queue on the server: - 1. Decides which SMP server to use (can be the same or different server that Alice uses for other queues) and opens secure encrypted transport connection to the chosen SMP server (see [Appendix A](#appendix-a)). + 1. Decides which SMP server to use (can be the same or different server that Alice uses for other queues) and opens secure encrypted transport connection to the chosen SMP server (see [Appendix A](#appendix-a)). - 2. Generates a new random public/private key pair (encryption key - `EK`) that she did not use before for Bob to encrypt the messages. + 2. Generates a new random public/private key pair (encryption key - `EK`) that she did not use before for Bob to encrypt the messages. - 3. Generates another new random public/private key pair (recipient key - `RK`) that she did not use before for her to sign commands and to decrypt the transmissions received from the server. + 3. Generates another new random public/private key pair (recipient key - `RK`) that she did not use before for her to sign commands and to decrypt the transmissions received from the server. - 4. Generates one more random key pair (recipient DH key - `RDHK`) to negotiate symmetric key that will be used by the server to encrypt message bodies delivered to Alice (to avoid shared cipher-text inside transport connection). + 4. Generates one more random key pair (recipient DH key - `RDHK`) to negotiate symmetric key that will be used by the server to encrypt message bodies delivered to Alice (to avoid shared cipher-text inside transport connection). - 5. Sends `"NEW"` command to the server to create a simplex queue (see `create` in [Create queue command](#create-queue-command)). This command contains previously generated unique "public" keys `RK` and `RDHK`. `RK` will be used to verify the following commands related to the same queue signed by its private counterpart, for example to subscribe to the messages received to this queue or to update the queue, e.g. by setting the key required to send the messages (initially Alice creates the queue that accepts unsigned messages, so anybody could send the message via this queue if they knew the queue sender's ID and server address). + 5. Sends `"NEW"` command to the server to create a simplex queue (see `create` in [Create queue command](#create-queue-command)). This command contains previously generated unique "public" keys `RK` and `RDHK`. `RK` will be used to verify the following commands related to the same queue signed by its private counterpart, for example to subscribe to the messages received to this queue or to update the queue, e.g. by setting the key required to send the messages (initially Alice creates the queue that accepts unsigned messages, so anybody could send the message via this queue if they knew the queue sender's ID and server address). - 6. The server sends `"IDS"` response with queue IDs (`queueIds`): + 6. The server sends `"IDS"` response with queue IDs (`queueIds`): - - Recipient ID `RID` for Alice to manage the queue and to receive the messages. + - Recipient ID `RID` for Alice to manage the queue and to receive the messages. - - Sender ID `SID` for Bob to send messages to the queue. + - Sender ID `SID` for Bob to send messages to the queue. - - Server public DH key (`SDHK`) to negotiate a shared secret for message body encryption, that Alice uses to derive a shared secret with the server `SS`. + - Server public DH key (`SDHK`) to negotiate a shared secret for message body encryption, that Alice uses to derive a shared secret with the server `SS`. 2. Alice sends an out-of-band message to Bob via the alternative channel that both Alice and Bob trust (see [protocol abstract](#simplex-messaging-protocol-abstract)). The message must include: - - Unique "public" key (`EK`) that Bob must use for E2E key agreement. + - Unique "public" key (`EK`) that Bob must use for E2E key agreement. - - SMP server hostname and information to open secure encrypted transport connection (see [Appendix A](#appendix-a)). + - SMP server hostname and information to open secure encrypted transport connection (see [Appendix A](#appendix-a)). - - Sender queue ID `SID` for Bob to use. + - Sender queue ID `SID` for Bob to use. 3. Bob, having received the out-of-band message from Alice, connects to the queue: - 1. Generates a new random public/private key pair (sender key - `SK`) that he did not use before for him to sign messages sent to Alice's server. + 1. Generates a new random public/private key pair (sender key - `SK`) that he did not use before for him to sign messages sent to Alice's server. - 2. Prepares the confirmation message for Alice to secure the queue. This message includes: + 2. Prepares the confirmation message for Alice to secure the queue. This message includes: - - Previously generated "public" key `SK` that will be used by Alice's server to authenticate Bob's messages, once the queue is secured. + - Previously generated "public" key `SK` that will be used by Alice's server to authenticate Bob's messages, once the queue is secured. - - Optionally, any additional information (application specific, e.g. Bob's profile name and details). + - Optionally, any additional information (application specific, e.g. Bob's profile name and details). - 3. Encrypts the confirmation body with the "public" key `EK` (that Alice provided via the out-of-band message). + 3. Encrypts the confirmation body with the "public" key `EK` (that Alice provided via the out-of-band message). - 4. Sends the encrypted message to the server with queue ID `SID` (see `send` in [Send message](#send-message)). This initial message to the queue must not be signed - signed messages will be rejected until Alice secures the queue (below). + 4. Sends the encrypted message to the server with queue ID `SID` (see `send` in [Send message](#send-message)). This initial message to the queue must not be signed - signed messages will be rejected until Alice secures the queue (below). 4. Alice receives Bob's message from the server using recipient queue ID `RID` (possibly, via the same transport connection she already has opened - see `message` in [Deliver queue message](#deliver-queue-message)): - 1. She decrypts received message body using the secret `SS`. + 1. She decrypts received message body using the secret `SS`. - 2. She decrypts received message with [key agreed with sender using] "private" key `EK`. + 2. She decrypts received message with [key agreed with sender using] "private" key `EK`. - 3. Even though anybody could have sent the message to the queue with ID `SID` before it is secured (e.g. if communication is compromised), Alice would ignore all messages until the decryption succeeds (i.e. the result contains the expected message format). Optionally, in the client application, she also may identify Bob using the information provided, but it is out of scope of SMP protocol. + 3. Even though anybody could have sent the message to the queue with ID `SID` before it is secured (e.g. if communication is compromised), Alice would ignore all messages until the decryption succeeds (i.e. the result contains the expected message format). Optionally, in the client application, she also may identify Bob using the information provided, but it is out of scope of SMP protocol. 5. Alice secures the queue `RID` with `"KEY"` command so only Bob can send messages to it (see [Secure queue command](#secure-queue-command)): - 1. She sends the `KEY` command with `RID` signed with "private" key `RK` to update the queue to only accept requests signed by "private" key `SK` provided by Bob. This command contains unique "public" key `SK` previously generated by Bob. + 1. She sends the `KEY` command with `RID` signed with "private" key `RK` to update the queue to only accept requests signed by "private" key `SK` provided by Bob. This command contains unique "public" key `SK` previously generated by Bob. - 2. From this moment the server will accept only signed commands to `SID`, so only Bob will be able to send messages to the queue `SID` (corresponding to `RID` that Alice has). + 2. From this moment the server will accept only signed commands to `SID`, so only Bob will be able to send messages to the queue `SID` (corresponding to `RID` that Alice has). - 3. Once queue is secured, Alice deletes `SID` and `SK` - even if Alice's client is compromised in the future, the attacker would not be able to send messages pretending to be Bob. + 3. Once queue is secured, Alice deletes `SID` and `SK` - even if Alice's client is compromised in the future, the attacker would not be able to send messages pretending to be Bob. 6. The simplex queue `RID` is now ready to be used. @@ -215,21 +215,21 @@ Bob now can securely send messages to Alice: 1. Bob sends the message: - 1. He encrypts the message to Alice with "public" key `EK` (provided by Alice, only known to Bob, used only for one simplex queue). + 1. He encrypts the message to Alice with "public" key `EK` (provided by Alice, only known to Bob, used only for one simplex queue). - 2. He signs `"SEND"` command to the server queue `SID` using the "private" key `SK` (that only he knows, used only for this queue). + 2. He signs `"SEND"` command to the server queue `SID` using the "private" key `SK` (that only he knows, used only for this queue). - 3. He sends the command to the server (see `send` in [Send message](#send-message)), that the server will authenticate using the "public" key `SK` (that Alice earlier received from Bob and provided to the server via `"KEY"` command). + 3. He sends the command to the server (see `send` in [Send message](#send-message)), that the server will authenticate using the "public" key `SK` (that Alice earlier received from Bob and provided to the server via `"KEY"` command). 2. Alice receives the message(s): - 1. She signs `"SUB"` command to the server to subscribe to the queue `RID` with the "private" key `RK` (see `subscribe` in [Subscribe to queue](#subscribe-to-queue)). + 1. She signs `"SUB"` command to the server to subscribe to the queue `RID` with the "private" key `RK` (see `subscribe` in [Subscribe to queue](#subscribe-to-queue)). - 2. The server, having authenticated Alice's command with the "public" key `RK` that she provided, delivers Bob's message(s) (see `message` in [Deliver queue message](#deliver-queue-message)). + 2. The server, having authenticated Alice's command with the "public" key `RK` that she provided, delivers Bob's message(s) (see `message` in [Deliver queue message](#deliver-queue-message)). - 3. She decrypts Bob's message(s) with the "private" key `EK` (that only she has). + 3. She decrypts Bob's message(s) with the "private" key `EK` (that only she has). - 4. She acknowledges the message reception to the server with `"ACK"` so that the server can delete the message and deliver the next messages. + 4. She acknowledges the message reception to the server with `"ACK"` so that the server can delete the message and deliver the next messages. This flow is show on sequence diagram below. @@ -360,6 +360,8 @@ The clients can optionally instruct a dedicated push notification server to subs - `subscribeNotifications` (`"NSUB"`) - see [Subscribe to queue notifications](#subscribe-to-queue-notifications). - `messageNotification` (`"NMSG"`) - see [Deliver message notification](#deliver-message-notification). +[`SEND` command](#send-message) includes the notification flag to instruct SMP server whether to send the notification - this flag is forwarded to the recepient inside encrypted envelope, together with the timestamp and the message body, so even if TLS is compromised this flag cannot be used for traffic correlation. + ## SMP Transmission structure Each transport block (SMP transmission) has a fixed size of 16384 bytes for traffic uniformity. @@ -587,7 +589,9 @@ Currently SMP defines only one command that can be used by senders - `send` mess This command is sent to the server by the sender both to confirm the queue after the sender received out-of-band message from the recipient and to send messages after the queue is secured: ```abnf -send = %s"SEND " smpEncMessage +send = %s"SEND " msgFlags SP smpEncMessage +msgFlags = notificationFlag reserved +notificationFlag = %s"T" / %s"F" smpEncMessage = smpPubHeader sentMsgBody ; message up to 16088 bytes smpPubHeader = smpClientVersion ("1" senderPublicDhKey / "0") smpClientVersion = word16 @@ -729,9 +733,11 @@ See its syntax in [Create queue command](#create-queue-command) The server must deliver messages to all subscribed simplex queues on the currently open transport connection. The syntax for the message delivery is: ```abnf -message = %s"MSG " msgId SP timestamp SP encryptedMsgBody +message = %s"MSG " msgId encryptedRcvMsgBody encryptedMsgBody = ; server-encrypted padded sent msgBody paddedSentMsgBody = ; maxMessageLength = 16088 +encryptedRcvMsgBody = ; server-encrypted meta-data and padded sent msgBody +rcvMsgBody = timestamp msgFlags SP paddedSentMsgBody msgId = length 24*24OCTET timestamp = 8*8OCTET ``` @@ -823,6 +829,7 @@ ok = %s"OK" Both the recipient and the sender can use TCP or some other, possibly higher level, transport protocol to communicate with the server. The default TCP port for SMP server is 5223. For scenarios when meta-data privacy is critical, it is recommended that clients: + - communicating over Tor network, - establish a separate connection for each SMP queue, - send noise traffic (using PING command). @@ -830,12 +837,14 @@ For scenarios when meta-data privacy is critical, it is recommended that clients In addition to that, the servers can be deployed as Tor onion services. The transport protocol should provide the following: + - server authentication (by matching server certificate hash with `serverIdentity`), - forward secrecy (by encrypting the traffic using ephemeral keys agreed during transport handshake), - integrity (preventing data modification by the attacker without detection), - unique channel binding (`sessionIdentifier`) to include in the signed part of SMP transmissions. By default, the client and server communicate using [TLS 1.3 protocol][13] restricted to: + - TLS_CHACHA20_POLY1305_SHA256 cipher suite (for better performance on mobile devices), - ed25519 and ed448 EdDSA algorithms for signatures, - x25519 and x448 ECDHE groups for key exchange. diff --git a/simplexmq.cabal b/simplexmq.cabal index aa1b64e13..8d98fe5c7 100644 --- a/simplexmq.cabal +++ b/simplexmq.cabal @@ -101,7 +101,7 @@ library , asn1-types ==0.3.* , async ==2.2.* , attoparsec ==0.14.* - , base >=4.7 && <5 + , base >=4.14 && <5 , base64-bytestring >=1.0 && <1.3 , bytestring ==0.10.* , case-insensitive ==1.2.* @@ -161,7 +161,7 @@ executable ntf-server , asn1-types ==0.3.* , async ==2.2.* , attoparsec ==0.14.* - , base >=4.7 && <5 + , base >=4.14 && <5 , base64-bytestring >=1.0 && <1.3 , bytestring ==0.10.* , case-insensitive ==1.2.* @@ -222,7 +222,7 @@ executable smp-agent , asn1-types ==0.3.* , async ==2.2.* , attoparsec ==0.14.* - , base >=4.7 && <5 + , base >=4.14 && <5 , base64-bytestring >=1.0 && <1.3 , bytestring ==0.10.* , case-insensitive ==1.2.* @@ -283,7 +283,7 @@ executable smp-server , asn1-types ==0.3.* , async ==2.2.* , attoparsec ==0.14.* - , base >=4.7 && <5 + , base >=4.14 && <5 , base64-bytestring >=1.0 && <1.3 , bytestring ==0.10.* , case-insensitive ==1.2.* @@ -361,7 +361,7 @@ test-suite smp-server-test , asn1-types ==0.3.* , async ==2.2.* , attoparsec ==0.14.* - , base >=4.7 && <5 + , base >=4.14 && <5 , base64-bytestring >=1.0 && <1.3 , bytestring ==0.10.* , case-insensitive ==1.2.* diff --git a/src/Simplex/Messaging/Agent.hs b/src/Simplex/Messaging/Agent.hs index bca2b3f30..70a192f94 100644 --- a/src/Simplex/Messaging/Agent.hs +++ b/src/Simplex/Messaging/Agent.hs @@ -851,7 +851,7 @@ subscriber c@AgentClient {msgQ} = forever $ do Right _ -> return () processSMPTransmission :: forall m. AgentMonad m => AgentClient -> ServerTransmission BrokerMsg -> m () -processSMPTransmission c@AgentClient {smpClients, subQ} (srv, sessId, rId, cmd) = +processSMPTransmission c@AgentClient {smpClients, subQ} (srv, v, sessId, rId, cmd) = withStore c (\db -> getRcvConn db srv rId) >>= \case SomeConn _ conn@(DuplexConnection cData rq _) -> processSMP conn cData rq SomeConn _ conn@(RcvConnection cData rq) -> processSMP conn cData rq @@ -859,10 +859,10 @@ processSMPTransmission c@AgentClient {smpClients, subQ} (srv, sessId, rId, cmd) _ -> atomically $ writeTBQueue subQ ("", "", ERR $ CONN NOT_FOUND) where processSMP :: Connection c -> ConnData -> RcvQueue -> m () - processSMP conn cData@ConnData {connId, duplexHandshake} rq@RcvQueue {rcvDhSecret, e2ePrivKey, e2eDhSecret, status} = + processSMP conn cData@ConnData {connId, duplexHandshake} rq@RcvQueue {e2ePrivKey, e2eDhSecret, status} = case cmd of - SMP.MSG srvMsgId srvTs msgFlags msgBody' -> handleNotifyAck $ do - msgBody <- agentCbDecrypt rcvDhSecret (C.cbNonce srvMsgId) msgBody' + SMP.MSG msg@SMP.RcvMessage {msgId = srvMsgId} -> handleNotifyAck $ do + SMP.ClientRcvMsgBody {msgTs = srvTs, msgFlags, msgBody} <- decryptSMPMessage v rq msg clientMsg@SMP.ClientMsgEnvelope {cmHeader = SMP.PubHeader phVer e2ePubKey_} <- parseMessage msgBody unless (phVer `isCompatible` SMP.smpClientVRange) . throwError $ AGENT A_VERSION diff --git a/src/Simplex/Messaging/Agent/Client.hs b/src/Simplex/Messaging/Agent/Client.hs index 78407769f..506bc5293 100644 --- a/src/Simplex/Messaging/Agent/Client.hs +++ b/src/Simplex/Messaging/Agent/Client.hs @@ -11,6 +11,7 @@ {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE RankNTypes #-} {-# LANGUAGE ScopedTypeVariables #-} +{-# LANGUAGE TupleSections #-} {-# LANGUAGE TypeApplications #-} module Simplex.Messaging.Agent.Client @@ -21,6 +22,7 @@ module Simplex.Messaging.Agent.Client newRcvQueue, subscribeQueue, getQueueMessage, + decryptSMPMessage, addSubscription, getSubscriptions, sendConfirmation, @@ -83,7 +85,6 @@ import Data.Set (Set) import Data.Text.Encoding import Data.Word (Word16) import qualified Database.SQLite.Simple as DB --- import GHC.Conc (unsafeIOToSTM) import Simplex.Messaging.Agent.Env.SQLite import Simplex.Messaging.Agent.Protocol import Simplex.Messaging.Agent.RetryInterval @@ -96,7 +97,8 @@ import Simplex.Messaging.Encoding import Simplex.Messaging.Notifications.Client import Simplex.Messaging.Notifications.Protocol import Simplex.Messaging.Notifications.Types -import Simplex.Messaging.Protocol (BrokerMsg, ErrorType, MsgFlags (..), MsgId, NotifierId, NtfPrivateSignKey, NtfPublicVerifyKey, ProtocolServer (..), QueueId, QueueIdsKeys (..), RcvNtfPublicDhKey, SMPMsgMeta, SndPublicVerifyKey) +import Simplex.Messaging.Parsers (parse) +import Simplex.Messaging.Protocol (BrokerMsg, ErrorType, MsgFlags (..), MsgId, NotifierId, NtfPrivateSignKey, NtfPublicVerifyKey, ProtocolServer (..), QueueId, QueueIdsKeys (..), RcvMessage (..), RcvNtfPublicDhKey, SMPMsgMeta (..), SndPublicVerifyKey) import qualified Simplex.Messaging.Protocol as SMP import Simplex.Messaging.TMap (TMap) import qualified Simplex.Messaging.TMap as TM @@ -547,11 +549,13 @@ sendInvitation c (Compatible SMPQueueInfo {smpServer, senderId, dhPublicKey}) (C SMP.ClientMessage SMP.PHEmpty $ smpEncode agentEnvelope getQueueMessage :: AgentMonad m => AgentClient -> RcvQueue -> m (Maybe SMPMsgMeta) -getQueueMessage c RcvQueue {server, rcvId, rcvPrivateKey} = do +getQueueMessage c rq@RcvQueue {server, rcvId, rcvPrivateKey} = do atomically createTakeGetLock - withLogClient c server rcvId "GET" $ \smp -> - getSMPMessage smp rcvPrivateKey rcvId + (v, msg_) <- withLogClient c server rcvId "GET" $ \smp -> + (thVersion smp,) <$> getSMPMessage smp rcvPrivateKey rcvId + mapM (decryptMeta v) msg_ where + decryptMeta v msg@SMP.RcvMessage {msgId} = SMP.rcvMessageMeta msgId <$> decryptSMPMessage v rq msg createTakeGetLock = TM.alterF takeLock (server, rcvId) $ getMsgLocks c where takeLock l_ = do @@ -559,6 +563,13 @@ getQueueMessage c RcvQueue {server, rcvId, rcvPrivateKey} = do takeTMVar l pure $ Just l +decryptSMPMessage :: AgentMonad m => Version -> RcvQueue -> SMP.RcvMessage -> m SMP.ClientRcvMsgBody +decryptSMPMessage v rq SMP.RcvMessage {msgId, msgTs, msgFlags, msgBody = SMP.EncRcvMsgBody body} + | v == 1 || v == 2 = SMP.ClientRcvMsgBody msgTs msgFlags <$> decrypt body + | otherwise = liftEither . parse SMP.clientRcvMsgBodyP (AGENT A_MESSAGE) =<< decrypt body + where + decrypt = agentCbDecrypt (rcvDhSecret rq) (C.cbNonce msgId) + secureQueue :: AgentMonad m => AgentClient -> RcvQueue -> SndPublicVerifyKey -> m () secureQueue c RcvQueue {server, rcvId, rcvPrivateKey} senderKey = withLogClient c server rcvId "KEY " $ \smp -> @@ -582,7 +593,7 @@ sendAck c rq@RcvQueue {server, rcvId, rcvPrivateKey} msgId = do releaseGetLock :: AgentClient -> RcvQueue -> STM () releaseGetLock c RcvQueue {server, rcvId} = - TM.lookup (server, rcvId) (getMsgLocks c) >>= mapM_ (void . (`tryPutTMVar` ())) + TM.lookup (server, rcvId) (getMsgLocks c) >>= mapM_ (`tryPutTMVar` ()) suspendQueue :: AgentMonad m => AgentClient -> RcvQueue -> m () suspendQueue c RcvQueue {server, rcvId, rcvPrivateKey} = @@ -734,7 +745,7 @@ agentOperationBracket c op action = E.bracket (atomically $ beginAgentOperation c op) (\_ -> atomically $ endAgentOperation c op) - (\_ -> action) + (const action) withStore' :: AgentMonad m => AgentClient -> (DB.Connection -> IO a) -> m a withStore' c action = withStore c $ fmap Right . action diff --git a/src/Simplex/Messaging/Agent/Protocol.hs b/src/Simplex/Messaging/Agent/Protocol.hs index 938d75b9e..0f60edf36 100644 --- a/src/Simplex/Messaging/Agent/Protocol.hs +++ b/src/Simplex/Messaging/Agent/Protocol.hs @@ -903,7 +903,7 @@ commandP = msgErrResp = ACmd SAgent .: MERR <$> A.decimal <* A.space <*> strP message = ACmd SAgent .:. MSG <$> msgMetaP <* A.space <*> smpP <* A.space <*> A.takeByteString ackCmd = ACmd SClient . ACK <$> A.decimal - connections = strP `A.sepBy'` (A.char ',') + connections = strP `A.sepBy'` A.char ',' msgMetaP = do integrity <- strP recipient <- " R=" *> partyMeta A.decimal diff --git a/src/Simplex/Messaging/Client.hs b/src/Simplex/Messaging/Client.hs index 49f492611..c39f5b1e2 100644 --- a/src/Simplex/Messaging/Client.hs +++ b/src/Simplex/Messaging/Client.hs @@ -24,7 +24,7 @@ -- See https://github.com/simplex-chat/simplexmq/blob/master/protocol/simplex-messaging.md module Simplex.Messaging.Client ( -- * Connect (disconnect) client to (from) SMP server - ProtocolClient (sessionId), + ProtocolClient (thVersion, sessionId), SMPClient, getProtocolClient, closeProtocolClient, @@ -56,7 +56,7 @@ import Control.Concurrent.Async import Control.Concurrent.STM import Control.Exception import Control.Monad -import Control.Monad.Trans.Class +import Control.Monad.IO.Class (liftIO) import Control.Monad.Trans.Except import Data.ByteString.Char8 (ByteString) import qualified Data.ByteString.Char8 as B @@ -95,7 +95,7 @@ data ProtocolClient msg = ProtocolClient type SMPClient = ProtocolClient SMP.BrokerMsg -- | Type synonym for transmission from some SPM server queue. -type ServerTransmission msg = (ProtocolServer, SessionId, QueueId, msg) +type ServerTransmission msg = (ProtocolServer, Version, SessionId, QueueId, msg) -- | protocol client configuration. data ProtocolClientConfig = ProtocolClientConfig @@ -208,7 +208,7 @@ getProtocolClient protocolServer cfg@ProtocolClientConfig {qSize, tcpTimeout, tc runExceptT $ sendProtocolCommand c Nothing "" protocolPing process :: ProtocolClient msg -> IO () - process ProtocolClient {sessionId, rcvQ, sentCommands} = forever $ do + process c@ProtocolClient {rcvQ, sentCommands} = forever $ do (_, _, (corrId, qId, respOrErr)) <- atomically $ readTBQueue rcvQ if B.null $ bs corrId then sendMsg qId respOrErr @@ -228,7 +228,7 @@ getProtocolClient protocolServer cfg@ProtocolClientConfig {qSize, tcpTimeout, tc where sendMsg :: QueueId -> Either ErrorType msg -> IO () sendMsg qId = \case - Right cmd -> atomically $ mapM_ (`writeTBQueue` (protocolServer, sessionId, qId, cmd)) msgQ + Right msg -> atomically $ mapM_ (`writeTBQueue` serverTransmission c qId msg) msgQ -- TODO send everything else to errQ and log in agent _ -> return () @@ -282,23 +282,30 @@ createSMPQueue c rpKey rKey dhKey = -- -- https://github.com/simplex-chat/simplexmq/blob/master/protocol/simplex-messaging.md#subscribe-to-queue subscribeSMPQueue :: SMPClient -> RcvPrivateSignKey -> RecipientId -> ExceptT ProtocolClientError IO () -subscribeSMPQueue c@ProtocolClient {protocolServer, sessionId, msgQ} rpKey rId = +subscribeSMPQueue c rpKey rId = sendSMPCommand c (Just rpKey) rId SUB >>= \case OK -> return () - cmd@MSG {} -> - lift . atomically $ mapM_ (`writeTBQueue` (protocolServer, sessionId, rId, cmd)) msgQ + cmd@MSG {} -> writeSMPMessage c rId cmd r -> throwE . PCEUnexpectedResponse $ bshow r +writeSMPMessage :: SMPClient -> RecipientId -> BrokerMsg -> ExceptT ProtocolClientError IO () +writeSMPMessage c rId msg = + liftIO . atomically $ mapM_ (`writeTBQueue` serverTransmission c rId msg) (msgQ c) + +serverTransmission :: ProtocolClient msg -> RecipientId -> msg -> ServerTransmission msg +serverTransmission ProtocolClient {protocolServer, thVersion, sessionId} entityId message = + (protocolServer, thVersion, sessionId, entityId, message) + -- | Get message from SMP queue. The server returns ERR PROHIBITED if a client uses SUB and GET via the same transport connection for the same queue -- -- https://github.covm/simplex-chat/simplexmq/blob/master/protocol/simplex-messaging.md#receive-a-message-from-the-queue -getSMPMessage :: SMPClient -> RcvPrivateSignKey -> RecipientId -> ExceptT ProtocolClientError IO (Maybe SMP.SMPMsgMeta) -getSMPMessage c@ProtocolClient {protocolServer, sessionId, msgQ} rpKey rId = +getSMPMessage :: SMPClient -> RcvPrivateSignKey -> RecipientId -> ExceptT ProtocolClientError IO (Maybe RcvMessage) +getSMPMessage c rpKey rId = sendSMPCommand c (Just rpKey) rId GET >>= \case OK -> pure Nothing - cmd@(MSG msgId msgTs msgFlags _) -> do - lift . atomically $ mapM_ (`writeTBQueue` (protocolServer, sessionId, rId, cmd)) msgQ - pure $ Just SMP.SMPMsgMeta {msgId, msgTs, msgFlags} + cmd@(MSG msg) -> do + writeSMPMessage c rId cmd + pure $ Just msg r -> throwE . PCEUnexpectedResponse $ bshow r -- | Subscribe to the SMP queue notifications. @@ -341,11 +348,10 @@ sendSMPMessage c spKey sId flags msg = -- -- https://github.com/simplex-chat/simplexmq/blob/master/protocol/simplex-messaging.md#acknowledge-message-delivery ackSMPMessage :: SMPClient -> RcvPrivateSignKey -> QueueId -> MsgId -> ExceptT ProtocolClientError IO () -ackSMPMessage c@ProtocolClient {protocolServer, sessionId, msgQ} rpKey rId msgId = +ackSMPMessage c rpKey rId msgId = sendSMPCommand c (Just rpKey) rId (ACK msgId) >>= \case OK -> return () - cmd@MSG {} -> - lift . atomically $ mapM_ (`writeTBQueue` (protocolServer, sessionId, rId, cmd)) msgQ + cmd@MSG {} -> writeSMPMessage c rId cmd r -> throwE . PCEUnexpectedResponse $ bshow r -- | Irreversibly suspend SMP queue. diff --git a/src/Simplex/Messaging/Crypto.hs b/src/Simplex/Messaging/Crypto.hs index 81dc8e6d5..8f2924dd0 100644 --- a/src/Simplex/Messaging/Crypto.hs +++ b/src/Simplex/Messaging/Crypto.hs @@ -8,6 +8,7 @@ {-# LANGUAGE LambdaCase #-} {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE PatternSynonyms #-} {-# LANGUAGE RankNTypes #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE StandaloneDeriving #-} @@ -101,6 +102,7 @@ module Simplex.Messaging.Crypto -- * NaCl crypto_box CbNonce (unCbNonce), cbEncrypt, + cbEncryptMaxLenBS, cbDecrypt, cbNonce, randomCbNonce, @@ -118,6 +120,13 @@ module Simplex.Messaging.Crypto -- * Cryptography error type CryptoError (..), + + -- * Limited size ByteStrings + MaxLenBS, + pattern MaxLenBS, + maxLenBS, + unsafeMaxLenBS, + appendMaxLenBS, ) where @@ -153,11 +162,11 @@ import Data.Constraint (Dict (..)) import Data.Kind (Constraint, Type) import Data.String import Data.Type.Equality -import Data.Typeable (Typeable) +import Data.Typeable (Proxy (Proxy), Typeable) import Data.X509 import Database.SQLite.Simple.FromField (FromField (..)) import Database.SQLite.Simple.ToField (ToField (..)) -import GHC.TypeLits (ErrorMessage (..), TypeError) +import GHC.TypeLits (ErrorMessage (..), KnownNat, Nat, TypeError, natVal, type (+)) import Network.Transport.Internal (decodeWord16, encodeWord16) import Simplex.Messaging.Encoding import Simplex.Messaging.Encoding.String @@ -809,6 +818,41 @@ unPad padded (lenWrd, rest) = B.splitAt 2 padded len = fromIntegral $ decodeWord16 lenWrd +newtype MaxLenBS (i :: Nat) = MLBS {unMaxLenBS :: ByteString} + +pattern MaxLenBS :: ByteString -> MaxLenBS i +pattern MaxLenBS s <- MLBS s + +{-# COMPLETE MaxLenBS #-} + +instance KnownNat i => Encoding (MaxLenBS i) where + smpEncode (MLBS s) = smpEncode s + smpP = first show . maxLenBS <$?> smpP + +instance KnownNat i => StrEncoding (MaxLenBS i) where + strEncode (MLBS s) = strEncode s + strP = first show . maxLenBS <$?> strP + +maxLenBS :: forall i. KnownNat i => ByteString -> Either CryptoError (MaxLenBS i) +maxLenBS s + | B.length s > maxLength @i = Left CryptoLargeMsgError + | otherwise = Right $ MLBS s + +unsafeMaxLenBS :: forall i. KnownNat i => ByteString -> MaxLenBS i +unsafeMaxLenBS = MLBS + +padMaxLenBS :: forall i. KnownNat i => MaxLenBS i -> MaxLenBS (i + 2) +padMaxLenBS (MLBS msg) = MLBS $ encodeWord16 (fromIntegral len) <> msg <> B.replicate padLen '#' + where + len = B.length msg + padLen = maxLength @i - len + +appendMaxLenBS :: (KnownNat i, KnownNat j) => MaxLenBS i -> MaxLenBS j -> MaxLenBS (i + j) +appendMaxLenBS (MLBS s1) (MLBS s2) = MLBS $ s1 <> s2 + +maxLength :: forall i. KnownNat i => Int +maxLength = fromIntegral (natVal $ Proxy @i) + initAEAD :: forall c. AES.BlockCipher c => Key -> IV -> ExceptT CryptoError IO (AES.AEAD c) initAEAD (Key aesKey) (IV ivBytes) = do iv <- makeIV @c ivBytes @@ -864,12 +908,17 @@ dh' (PublicKeyX448 k) (PrivateKeyX448 pk _) = DhSecretX448 $ X448.dh k pk -- | NaCl @crypto_box@ encrypt with a shared DH secret and 192-bit nonce. cbEncrypt :: DhSecret X25519 -> CbNonce -> ByteString -> Int -> Either CryptoError ByteString -cbEncrypt secret (CbNonce nonce) msg paddedLen = cryptoBox <$> pad msg paddedLen +cbEncrypt secret (CbNonce nonce) msg paddedLen = cryptoBox secret nonce <$> pad msg paddedLen + +-- | NaCl @crypto_box@ encrypt with a shared DH secret and 192-bit nonce. +cbEncryptMaxLenBS :: KnownNat i => DhSecret X25519 -> CbNonce -> MaxLenBS i -> ByteString +cbEncryptMaxLenBS secret (CbNonce nonce) = cryptoBox secret nonce . unMaxLenBS . padMaxLenBS + +cryptoBox :: DhSecret 'X25519 -> ByteString -> ByteString -> ByteString +cryptoBox secret nonce s = BA.convert tag <> c where - cryptoBox s = BA.convert tag <> c - where - (rs, c) = xSalsa20 secret nonce s - tag = Poly1305.auth rs c + (rs, c) = xSalsa20 secret nonce s + tag = Poly1305.auth rs c -- | NaCl @crypto_box@ decrypt with a shared DH secret and 192-bit nonce. cbDecrypt :: DhSecret X25519 -> CbNonce -> ByteString -> Either CryptoError ByteString diff --git a/src/Simplex/Messaging/Encoding.hs b/src/Simplex/Messaging/Encoding.hs index 695065419..6f007d8dd 100644 --- a/src/Simplex/Messaging/Encoding.hs +++ b/src/Simplex/Messaging/Encoding.hs @@ -197,3 +197,9 @@ instance (Encoding a, Encoding b, Encoding c, Encoding d, Encoding e, Encoding f {-# INLINE smpEncode #-} smpP = (,,,,,,) <$> smpP <*> smpP <*> smpP <*> smpP <*> smpP <*> smpP <*> smpP {-# INLINE smpP #-} + +instance (Encoding a, Encoding b, Encoding c, Encoding d, Encoding e, Encoding f, Encoding g, Encoding h) => Encoding (a, b, c, d, e, f, g, h) where + smpEncode (a, b, c, d, e, f, g, h) = smpEncode a <> smpEncode b <> smpEncode c <> smpEncode d <> smpEncode e <> smpEncode f <> smpEncode g <> smpEncode h + {-# INLINE smpEncode #-} + smpP = (,,,,,,,) <$> smpP <*> smpP <*> smpP <*> smpP <*> smpP <*> smpP <*> smpP <*> smpP + {-# INLINE smpP #-} diff --git a/src/Simplex/Messaging/Notifications/Server.hs b/src/Simplex/Messaging/Notifications/Server.hs index fcdc0e085..0dca24d99 100644 --- a/src/Simplex/Messaging/Notifications/Server.hs +++ b/src/Simplex/Messaging/Notifications/Server.hs @@ -128,7 +128,7 @@ ntfSubscriber NtfSubscriber {smpSubscribers, newSubQ, smpAgent = ca@SMPClientAge receiveSMP :: m () receiveSMP = forever $ do - (srv, _sessId, ntfId, msg) <- atomically $ readTBQueue msgQ + (srv, _, _, ntfId, msg) <- atomically $ readTBQueue msgQ let smpQueue = SMPQueueNtf srv ntfId case msg of SMP.NMSG nmsgNonce encNMsgMeta -> do diff --git a/src/Simplex/Messaging/Notifications/Server/Env.hs b/src/Simplex/Messaging/Notifications/Server/Env.hs index a6f08b682..c60239b32 100644 --- a/src/Simplex/Messaging/Notifications/Server/Env.hs +++ b/src/Simplex/Messaging/Notifications/Server/Env.hs @@ -2,7 +2,6 @@ {-# LANGUAGE DuplicateRecordFields #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE KindSignatures #-} -{-# LANGUAGE LambdaCase #-} {-# LANGUAGE NamedFieldPuns #-} module Simplex.Messaging.Notifications.Server.Env where diff --git a/src/Simplex/Messaging/Protocol.hs b/src/Simplex/Messaging/Protocol.hs index 029e8eeb3..52dc7d0b3 100644 --- a/src/Simplex/Messaging/Protocol.hs +++ b/src/Simplex/Messaging/Protocol.hs @@ -16,9 +16,13 @@ {-# LANGUAGE TupleSections #-} {-# LANGUAGE TypeApplications #-} {-# LANGUAGE TypeFamilyDependencies #-} +{-# LANGUAGE TypeOperators #-} {-# LANGUAGE UndecidableInstances #-} +{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-} {-# OPTIONS_GHC -fno-warn-unticked-promoted-constructors #-} +{-# HLINT ignore "Use newtype instead of data" #-} + -- | -- Module : Simplex.Messaging.ProtocolEncoding -- Copyright : (c) simplex.chat @@ -78,12 +82,20 @@ module Simplex.Messaging.Protocol NtfPublicVerifyKey, RcvNtfPublicDhKey, RcvNtfDhSecret, + Message (..), + RcvMessage (..), MsgId, MsgBody, + MaxMessageLen, + MaxRcvMessageLen, + EncRcvMsgBody (..), + RcvMsgBody (..), + ClientRcvMsgBody (..), EncNMsgMeta, SMPMsgMeta (..), NMsgMeta (..), MsgFlags (..), + rcvMessageMeta, noMsgFlags, -- * Parse and serialize @@ -92,6 +104,8 @@ module Simplex.Messaging.Protocol encodeTransmission, transmissionP, _smpP, + encodeRcvMsgBody, + clientRcvMsgBodyP, -- * TCP transport functions tPut, @@ -114,9 +128,10 @@ import qualified Data.ByteString.Char8 as B import Data.Kind import Data.Maybe (isNothing) import Data.String -import Data.Time.Clock.System (SystemTime) +import Data.Time.Clock.System (SystemTime (..)) import Data.Type.Equality import GHC.Generics (Generic) +import GHC.TypeLits (type (+)) import Generic.Random (genericArbitraryU) import Network.Socket (HostName, ServiceName) import qualified Simplex.Messaging.Crypto as C @@ -137,6 +152,11 @@ smpClientVRange = mkVersionRange 1 smpClientVersion maxMessageLength :: Int maxMessageLength = 16088 +type MaxMessageLen = 16088 + +-- 16 extra bytes: 8 for timestamp and 8 for flags (7 flags and the space, only 1 flag is currently used) +type MaxRcvMessageLen = MaxMessageLen + 16 -- 16104, the padded size is 16106 + -- it is shorter to allow per-queue e2e encryption DH key in the "public" header e2eEncConfirmationLength :: Int e2eEncConfirmationLength = 15936 @@ -243,9 +263,10 @@ deriving instance Eq (Command p) data BrokerMsg where -- SMP broker messages (responses, client messages, notifications) IDS :: QueueIdsKeys -> BrokerMsg - -- MSG v1 has to be supported for encoding/decoding - -- MSG :: MsgId -> SystemTime -> MsgBody -> BrokerMsg - MSG :: MsgId -> SystemTime -> MsgFlags -> MsgBody -> BrokerMsg + -- MSG v1/2 has to be supported for encoding/decoding + -- v1: MSG :: MsgId -> SystemTime -> MsgBody -> BrokerMsg + -- v2: MsgId -> SystemTime -> MsgFlags -> MsgBody -> BrokerMsg + MSG :: RcvMessage -> BrokerMsg NID :: NotifierId -> RcvNtfPublicDhKey -> BrokerMsg NMSG :: C.CbNonce -> EncNMsgMeta -> BrokerMsg END :: BrokerMsg @@ -254,6 +275,79 @@ data BrokerMsg where PONG :: BrokerMsg deriving (Eq, Show) +data RcvMessage = RcvMessage + { msgId :: MsgId, + msgTs :: SystemTime, + msgFlags :: MsgFlags, + msgBody :: EncRcvMsgBody -- e2e encrypted, with extra encryption for recipient + } + deriving (Eq, Show) + +-- | received message without server/recipient encryption +data Message = Message + { msgId :: MsgId, + msgTs :: SystemTime, + msgFlags :: MsgFlags, + msgBody :: C.MaxLenBS MaxMessageLen + } + +instance StrEncoding RcvMessage where + strEncode RcvMessage {msgId, msgTs, msgFlags, msgBody = EncRcvMsgBody body} = + B.unwords + [ strEncode msgId, + strEncode msgTs, + "flags=" <> strEncode msgFlags, + strEncode body + ] + strP = do + msgId <- strP_ + msgTs <- strP_ + msgFlags <- ("flags=" *> strP_) <|> pure noMsgFlags + msgBody <- EncRcvMsgBody <$> strP + pure RcvMessage {msgId, msgTs, msgFlags, msgBody} + +newtype EncRcvMsgBody = EncRcvMsgBody ByteString + deriving (Eq, Show) + +data RcvMsgBody = RcvMsgBody + { msgTs :: SystemTime, + msgFlags :: MsgFlags, + msgBody :: C.MaxLenBS MaxMessageLen + } + +encodeRcvMsgBody :: RcvMsgBody -> C.MaxLenBS MaxRcvMessageLen +encodeRcvMsgBody RcvMsgBody {msgTs, msgFlags, msgBody} = + let rcvMeta :: C.MaxLenBS 16 = C.unsafeMaxLenBS $ smpEncode (msgTs, msgFlags, ' ') + in C.appendMaxLenBS rcvMeta msgBody + +data ClientRcvMsgBody = ClientRcvMsgBody + { msgTs :: SystemTime, + msgFlags :: MsgFlags, + msgBody :: ByteString + } + +clientRcvMsgBodyP :: Parser ClientRcvMsgBody +clientRcvMsgBodyP = do + msgTs <- smpP + msgFlags <- smpP + Tail msgBody <- _smpP + pure ClientRcvMsgBody {msgTs, msgFlags, msgBody} + +instance StrEncoding Message where + strEncode Message {msgId, msgTs, msgFlags, msgBody} = + B.unwords + [ strEncode msgId, + strEncode msgTs, + "flags=" <> strEncode msgFlags, + strEncode msgBody + ] + strP = do + msgId <- strP_ + msgTs <- strP_ + msgFlags <- ("flags=" *> strP_) <|> pure noMsgFlags + msgBody <- strP + pure Message {msgId, msgTs, msgFlags, msgBody} + type EncNMsgMeta = ByteString data SMPMsgMeta = SMPMsgMeta @@ -263,6 +357,9 @@ data SMPMsgMeta = SMPMsgMeta } deriving (Show) +rcvMessageMeta :: MsgId -> ClientRcvMsgBody -> SMPMsgMeta +rcvMessageMeta msgId ClientRcvMsgBody {msgTs, msgFlags} = SMPMsgMeta {msgId, msgTs, msgFlags} + data NMsgMeta = NMsgMeta { msgId :: MsgId, msgTs :: SystemTime @@ -277,11 +374,13 @@ instance Encoding NMsgMeta where (msgId, msgTs, Tail _) <- smpP pure NMsgMeta {msgId, msgTs} +-- it must be data for correct JSON encoding data MsgFlags = MsgFlags {notification :: Bool} deriving (Eq, Show, Generic) instance ToJSON MsgFlags where toEncoding = J.genericToEncoding J.defaultOptions +-- this encoding should not become bigger than 7 bytes (currently it is 1 byte) instance Encoding MsgFlags where smpEncode MsgFlags {notification} = smpEncode notification smpP = do @@ -572,7 +671,7 @@ data ErrorType QUOTA | -- | ACK command is sent without message to be acknowledged NO_MSG - | -- | sent message is too large (> maxMessageLength = 16078 bytes) + | -- | sent message is too large (> maxMessageLength = 16088 bytes) LARGE_MSG | -- | internal server error INTERNAL @@ -724,9 +823,10 @@ instance ProtocolEncoding BrokerMsg where type Tag BrokerMsg = BrokerMsgTag encodeProtocol v = \case IDS (QIK rcvId sndId srvDh) -> e (IDS_, ' ', rcvId, sndId, srvDh) - MSG msgId ts flags msgBody - | v == 1 -> e (MSG_, ' ', msgId, ts, Tail msgBody) - | otherwise -> e (MSG_, ' ', msgId, ts, flags, ' ', Tail msgBody) + MSG RcvMessage {msgId, msgTs, msgFlags, msgBody = EncRcvMsgBody body} + | v == 1 -> e (MSG_, ' ', msgId, msgTs, Tail body) + | v == 2 -> e (MSG_, ' ', msgId, msgTs, msgFlags, ' ', Tail body) + | otherwise -> e (MSG_, ' ', msgId, Tail body) NID nId srvNtfDh -> e (NID_, ' ', nId, srvNtfDh) NMSG nmsgNonce encNMsgMeta -> e (NMSG_, ' ', nmsgNonce, encNMsgMeta) END -> e END_ @@ -738,9 +838,14 @@ instance ProtocolEncoding BrokerMsg where e = smpEncode protocolP v = \case - MSG_ - | v == 1 -> MSG <$> _smpP <*> smpP <*> pure noMsgFlags <*> (unTail <$> smpP) - | otherwise -> MSG <$> _smpP <*> smpP <*> smpP <*> (unTail <$> _smpP) + MSG_ -> do + msgId <- _smpP + MSG <$> case v of + 1 -> RcvMessage msgId <$> smpP <*> pure noMsgFlags <*> bodyP + 2 -> RcvMessage msgId <$> smpP <*> smpP <*> (A.space *> bodyP) + _ -> RcvMessage msgId (MkSystemTime 0 0) noMsgFlags <$> bodyP + where + bodyP = EncRcvMsgBody . unTail <$> smpP IDS_ -> IDS <$> (QIK <$> _smpP <*> smpP <*> smpP) NID_ -> NID <$> _smpP <*> smpP NMSG_ -> NMSG <$> _smpP <*> smpP diff --git a/src/Simplex/Messaging/Server.hs b/src/Simplex/Messaging/Server.hs index 9d82ec6d7..11fa2d09b 100644 --- a/src/Simplex/Messaging/Server.hs +++ b/src/Simplex/Messaging/Server.hs @@ -40,6 +40,7 @@ import Control.Monad.Except import Control.Monad.IO.Unlift import Control.Monad.Reader import Crypto.Random +import Data.Bifunctor (first) import Data.ByteString.Char8 (ByteString) import qualified Data.ByteString.Char8 as B import Data.Either (fromRight) @@ -57,6 +58,7 @@ import Data.Time.Clock (UTCTime (..), diffTimeToPicoseconds, getCurrentTime) import Data.Time.Clock.System (SystemTime (..), getSystemTime) import Data.Time.Format.ISO8601 (iso8601Show) import Data.Type.Equality +import GHC.TypeLits (KnownNat) import Network.Socket (ServiceName) import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Encoding (Encoding (smpEncode)) @@ -75,6 +77,7 @@ import qualified Simplex.Messaging.TMap as TM import Simplex.Messaging.Transport import Simplex.Messaging.Transport.Server import Simplex.Messaging.Util +import System.Exit (exitFailure) import System.Mem.Weak (deRefWeak) import UnliftIO.Concurrent import UnliftIO.Directory (doesFileExist, renameFile) @@ -169,7 +172,7 @@ smpServer started = do logServerStats :: Int -> Int -> m () logServerStats startAt logInterval = do initialDelay <- (startAt -) . fromIntegral . (`div` 1000000_000000) . diffTimeToPicoseconds . utctDayTime <$> liftIO getCurrentTime - logInfo $ "fromTime,qCreated,qSecured,qDeleted,msgSent,msgRecv,dayMsgQueues,weekMsgQueues,monthMsgQueues" + logInfo "fromTime,qCreated,qSecured,qDeleted,msgSent,msgRecv,dayMsgQueues,weekMsgQueues,monthMsgQueues" threadDelay $ 1000000 * (initialDelay + if initialDelay < 0 then 86400 else 0) ServerStats {fromTime, qCreated, qSecured, qDeleted, msgSent, msgRecv, dayMsgQueues, weekMsgQueues, monthMsgQueues} <- asks serverStats let interval = 1000000 * logInterval @@ -202,10 +205,10 @@ smpServer started = do Left _ -> pure () runClientTransport :: (Transport c, MonadUnliftIO m, MonadReader Env m) => THandle c -> m () -runClientTransport th@THandle {sessionId} = do +runClientTransport th@THandle {thVersion, sessionId} = do q <- asks $ tbqSize . config ts <- liftIO getSystemTime - c <- atomically $ newClient q sessionId ts + c <- atomically $ newClient q thVersion sessionId ts s <- asks server expCfg <- asks $ inactiveClientExpiration . config raceAny_ ([send th c, client c s, receive th c] <> disconnectThread_ c expCfg) @@ -311,7 +314,7 @@ dummyKeyEd448 :: C.PublicKey 'C.Ed448 dummyKeyEd448 = "MEMwBQYDK2VxAzoA6ibQc9XpkSLtwrf7PLvp81qW/etiumckVFImCMRdftcG/XopbOSaq9qyLhrgJWKOLyNrQPNVvpMA" client :: forall m. (MonadUnliftIO m, MonadReader Env m) => Client -> Server -> m () -client clnt@Client {subscriptions, ntfSubscriptions, rcvQ, sndQ} Server {subscribedQ, ntfSubscribedQ, notifiers} = +client clnt@Client {thVersion, subscriptions, ntfSubscriptions, rcvQ, sndQ} Server {subscribedQ, ntfSubscribedQ, notifiers} = forever $ atomically (readTBQueue rcvQ) >>= processCommand @@ -333,9 +336,9 @@ client clnt@Client {subscriptions, ntfSubscriptions, rcvQ, sndQ} Server {subscri (asks $ allowNewQueues . config) (createQueue st rKey dhKey) (pure (corrId, queueId, ERR AUTH)) - SUB -> subscribeQueue queueId - GET -> getMessage - ACK msgId -> acknowledgeMsg msgId + SUB -> subscribeQueue st queueId + GET -> getMessage st + ACK msgId -> acknowledgeMsg st msgId KEY sKey -> secureQueue_ st sKey NKEY nKey dhKey -> addQueueNotifier_ st nKey dhKey NDEL -> deleteQueueNotifier_ st @@ -372,7 +375,7 @@ client clnt@Client {subscriptions, ntfSubscriptions, rcvQ, sndQ} Server {subscri withLog (`logCreateById` rId) stats <- asks serverStats atomically $ modifyTVar (qCreated stats) (+ 1) - subscribeQueue rId $> IDS (qik ids) + subscribeQueue st rId $> IDS (qik ids) logCreateById :: StoreLog 'WriteMode -> RecipientId -> IO () logCreateById s rId = @@ -420,8 +423,8 @@ client clnt@Client {subscriptions, ntfSubscriptions, rcvQ, sndQ} Server {subscri withLog (`logDeleteQueue` queueId) okResp <$> atomically (suspendQueue st queueId) - subscribeQueue :: RecipientId -> m (Transmission BrokerMsg) - subscribeQueue rId = + subscribeQueue :: QueueStore -> RecipientId -> m (Transmission BrokerMsg) + subscribeQueue st rId = atomically (TM.lookup rId subscriptions) >>= \case Nothing -> atomically newSub >>= deliver @@ -443,10 +446,10 @@ client clnt@Client {subscriptions, ntfSubscriptions, rcvQ, sndQ} Server {subscri deliver sub = do q <- getStoreMsgQueue rId msg_ <- atomically $ tryPeekMsg q - deliverMessage rId sub q msg_ + deliverMessage st rId sub q msg_ - getMessage :: m (Transmission BrokerMsg) - getMessage = + getMessage :: QueueStore -> m (Transmission BrokerMsg) + getMessage st = atomically (TM.lookup queueId subscriptions) >>= \case Nothing -> atomically newSub >>= getMessage_ @@ -465,13 +468,21 @@ client clnt@Client {subscriptions, ntfSubscriptions, rcvQ, sndQ} Server {subscri TM.insert queueId sub subscriptions pure s getMessage_ :: Sub -> m (Transmission BrokerMsg) - getMessage_ s = do + getMessage_ s = withRcvQueue st queueId $ \qr -> do q <- getStoreMsgQueue queueId atomically $ tryPeekMsg q >>= \case - Just msg -> setDelivered s msg $> (corrId, queueId, msgCmd msg) + Just msg -> + let encMsg = encryptMsg qr msg + in setDelivered s msg $> (corrId, queueId, MSG encMsg) _ -> pure (corrId, queueId, OK) + withRcvQueue :: QueueStore -> RecipientId -> (QueueRec -> m (Transmission BrokerMsg)) -> m (Transmission BrokerMsg) + withRcvQueue st rId action = + atomically (getQueue st SRecipient rId) >>= \case + Left e -> pure (corrId, rId, ERR e) + Right qr -> action qr + subscribeNotifications :: m (Transmission BrokerMsg) subscribeNotifications = atomically $ do unlessM (TM.member queueId ntfSubscriptions) $ do @@ -479,8 +490,8 @@ client clnt@Client {subscriptions, ntfSubscriptions, rcvQ, sndQ} Server {subscri TM.insert queueId () ntfSubscriptions pure ok - acknowledgeMsg :: MsgId -> m (Transmission BrokerMsg) - acknowledgeMsg msgId = do + acknowledgeMsg :: QueueStore -> MsgId -> m (Transmission BrokerMsg) + acknowledgeMsg st msgId = do atomically (TM.lookup queueId subscriptions) >>= \case Nothing -> pure $ err NO_MSG Just sub -> @@ -495,7 +506,7 @@ client clnt@Client {subscriptions, ntfSubscriptions, rcvQ, sndQ} Server {subscri _ -> do (msgDeleted, msg_) <- atomically $ tryDelPeekMsg q msgId when msgDeleted updateStats - deliverMessage queueId sub q msg_ + deliverMessage st queueId sub q msg_ _ -> pure $ err NO_MSG where getDelivered :: TVar Sub -> STM (Maybe Sub) @@ -520,7 +531,7 @@ client clnt@Client {subscriptions, ntfSubscriptions, rcvQ, sndQ} Server {subscri updatePeriod pSel = modifyTVar (pSel stats) (S.insert qId) sendMessage :: QueueStore -> MsgFlags -> MsgBody -> m (Transmission BrokerMsg) - sendMessage st flags msgBody + sendMessage st msgFlags msgBody | B.length msgBody > maxMessageLength = pure $ err LARGE_MSG | otherwise = do qr <- atomically $ getQueue st SSender queueId @@ -530,7 +541,7 @@ client clnt@Client {subscriptions, ntfSubscriptions, rcvQ, sndQ} Server {subscri storeMessage qr = case status qr of QueueOff -> return $ err AUTH QueueActive -> - mkMessage >>= \case + mapM mkMessage (C.maxLenBS msgBody) >>= \case Left _ -> pure $ err LARGE_MSG Right msg -> do ms <- asks msgStore @@ -541,7 +552,7 @@ client clnt@Client {subscriptions, ntfSubscriptions, rcvQ, sndQ} Server {subscri q <- getMsgQueue ms (recipientId qr) msgQueueQuota mapM_ (deleteExpiredMsgs q) old ifM (isFull q) (pure $ err QUOTA) $ do - when (notification flags) $ trySendNotification msg ntfNonceDrg + when (notification msgFlags) $ trySendNotification msg ntfNonceDrg writeMsg q msg pure ok when (sent == OK) $ do @@ -550,12 +561,11 @@ client clnt@Client {subscriptions, ntfSubscriptions, rcvQ, sndQ} Server {subscri atomically $ updateActiveQueues stats $ recipientId qr pure resp where - mkMessage :: m (Either C.CryptoError Message) - mkMessage = do + mkMessage :: C.MaxLenBS MaxMessageLen -> m Message + mkMessage body = do msgId <- randomId =<< asks (msgIdBytes . config) - ts <- liftIO getSystemTime - let c = C.cbEncrypt (rcvDhSecret qr) (C.cbNonce msgId) msgBody (maxMessageLength + 2) - pure $ Message msgId ts flags <$> c + msgTs <- liftIO getSystemTime + pure $ Message msgId msgTs msgFlags body trySendNotification :: Message -> TVar ChaChaDRG -> STM () trySendNotification msg ntfNonceDrg = @@ -569,36 +579,48 @@ client clnt@Client {subscriptions, ntfSubscriptions, rcvQ, sndQ} Server {subscri writeTBQueue q (CorrId "", nId, NMSG nmsgNonce encNMsgMeta) mkMessageNotification :: Message -> RcvNtfDhSecret -> TVar ChaChaDRG -> STM (C.CbNonce, EncNMsgMeta) - mkMessageNotification Message {msgId, ts} rcvNtfDhSecret ntfNonceDrg = do + mkMessageNotification Message {msgId, msgTs} rcvNtfDhSecret ntfNonceDrg = do cbNonce <- C.pseudoRandomCbNonce ntfNonceDrg - let msgMeta = NMsgMeta {msgId, msgTs = ts} + let msgMeta = NMsgMeta {msgId, msgTs} encNMsgMeta = C.cbEncrypt rcvNtfDhSecret cbNonce (smpEncode msgMeta) 128 pure . (cbNonce,) $ fromRight "" encNMsgMeta - deliverMessage :: RecipientId -> TVar Sub -> MsgQueue -> Maybe Message -> m (Transmission BrokerMsg) - deliverMessage rId sub q msg_ = + deliverMessage :: QueueStore -> RecipientId -> TVar Sub -> MsgQueue -> Maybe Message -> m (Transmission BrokerMsg) + deliverMessage st rId sub q msg_ = withRcvQueue st rId $ \qr -> do readTVarIO sub >>= \case s@Sub {subThread = NoSub} -> case msg_ of - Just msg -> atomically (setDelivered s msg) $> (corrId, rId, msgCmd msg) - _ -> forkSub $> ok + Just msg -> + let encMsg = encryptMsg qr msg + in atomically (setDelivered s msg) $> (corrId, rId, MSG encMsg) + _ -> forkSub qr $> ok _ -> pure ok where - forkSub :: m () - forkSub = do + forkSub :: QueueRec -> m () + forkSub qr = do atomically . modifyTVar sub $ \s -> s {subThread = SubPending} t <- mkWeakThreadId =<< forkIO subscriber atomically . modifyTVar sub $ \case s@Sub {subThread = SubPending} -> s {subThread = SubThread t} s -> s + where + subscriber = atomically $ do + msg <- peekMsg q + let encMsg = encryptMsg qr msg + writeTBQueue sndQ (CorrId "", rId, MSG encMsg) + s <- readTVar sub + void $ setDelivered s msg + writeTVar sub s {subThread = NoSub} - subscriber :: m () - subscriber = atomically $ do - msg <- peekMsg q - writeTBQueue sndQ (CorrId "", rId, msgCmd msg) - s <- readTVar sub - void $ setDelivered s msg - writeTVar sub s {subThread = NoSub} + encryptMsg :: QueueRec -> Message -> RcvMessage + encryptMsg qr Message {msgId, msgTs, msgFlags, msgBody} + | thVersion == 1 || thVersion == 2 = encrypt msgBody + | otherwise = encrypt $ encodeRcvMsgBody RcvMsgBody {msgTs, msgFlags, msgBody} + where + encrypt :: KnownNat i => C.MaxLenBS i -> RcvMessage + encrypt body = + let encBody = EncRcvMsgBody $ C.cbEncryptMaxLenBS (rcvDhSecret qr) (C.cbNonce msgId) body + in RcvMessage msgId msgTs msgFlags encBody setDelivered :: Sub -> Message -> STM Bool setDelivered s Message {msgId} = tryPutTMVar (delivered s) msgId @@ -609,9 +631,6 @@ client clnt@Client {subscriptions, ntfSubscriptions, rcvQ, sndQ} Server {subscri quota <- asks $ msgQueueQuota . config atomically $ getMsgQueue ms rId quota - msgCmd :: Message -> BrokerMsg - msgCmd Message {msgId, ts, msgFlags, msgBody} = MSG msgId ts msgFlags msgBody - delQueueAndMsgs :: QueueStore -> m (Transmission BrokerMsg) delQueueAndMsgs st = do withLog (`logDeleteQueue` queueId) @@ -654,26 +673,44 @@ saveServerMessages = asks (storeMsgsFile . config) >>= mapM_ saveMessages where saveQueueMsgs ms h rId = atomically (flushMsgQueue ms rId) - >>= mapM_ (B.hPutStrLn h . strEncode . MsgLogRecord rId) + >>= mapM_ (B.hPutStrLn h . strEncode . MLRv3 rId) -restoreServerMessages :: (MonadUnliftIO m, MonadReader Env m) => m () +restoreServerMessages :: forall m. (MonadUnliftIO m, MonadReader Env m) => m () restoreServerMessages = asks (storeMsgsFile . config) >>= mapM_ restoreMessages where restoreMessages f = whenM (doesFileExist f) $ do logInfo $ "restoring messages from file " <> T.pack f + st <- asks queueStore ms <- asks msgStore quota <- asks $ msgQueueQuota . config - liftIO $ mapM_ (restoreMsg ms quota) . B.lines =<< B.readFile f - renameFile f $ f <> ".bak" - logInfo $ "messages restored" + runExceptT (liftIO (B.readFile f) >>= mapM_ (restoreMsg st ms quota) . B.lines) >>= \case + Left e -> do + logError . T.pack $ "error restoring messages: " <> e + liftIO exitFailure + _ -> do + renameFile f $ f <> ".bak" + logInfo "messages restored" where - restoreMsg ms quota s = case strDecode s of - Left e -> logError . decodeLatin1 $ "message parsing error (" <> B.pack e <> "): " <> B.take 100 s - Right (MsgLogRecord rId msg) -> do - full <- atomically $ do - q <- getMsgQueue ms rId quota - ifM (isFull q) (pure True) (writeMsg q msg $> False) - when full . logError . decodeLatin1 $ "message queue " <> strEncode rId <> " is full, message not restored: " <> strEncode (msgId (msg :: Message)) + restoreMsg st ms quota s = do + r <- liftEither . first (msgErr "parsing") $ strDecode s + case r of + MLRv3 rId msg -> addToMsgQueue rId msg + MLRv1 rId encMsg -> do + qr <- liftEitherError (msgErr "queue unknown") . atomically $ getQueue st SRecipient rId + msg' <- updateMsgV1toV3 qr encMsg + addToMsgQueue rId msg' + where + addToMsgQueue rId msg = do + full <- atomically $ do + q <- getMsgQueue ms rId quota + ifM (isFull q) (pure True) (writeMsg q msg $> False) + when full . logError . decodeLatin1 $ "message queue " <> strEncode rId <> " is full, message not restored: " <> strEncode (msgId (msg :: Message)) + updateMsgV1toV3 QueueRec {rcvDhSecret} RcvMessage {msgId, msgTs, msgFlags, msgBody = EncRcvMsgBody body} = do + let nonce = C.cbNonce msgId + msgBody <- liftEither . first (msgErr "v1 message decryption") $ C.maxLenBS =<< C.cbDecrypt rcvDhSecret nonce body + pure Message {msgId, msgTs, msgFlags, msgBody} + msgErr :: Show e => String -> e -> String + msgErr op e = op <> " error (" <> show e <> "): " <> B.unpack (B.take 100 s) saveServerStats :: (MonadUnliftIO m, MonadReader Env m) => m () saveServerStats = @@ -683,7 +720,7 @@ saveServerStats = saveStats f stats = do logInfo $ "saving server stats to file " <> T.pack f B.writeFile f $ strEncode stats - logInfo $ "server stats saved" + logInfo "server stats saved" restoreServerStats :: (MonadUnliftIO m, MonadReader Env m) => m () restoreServerStats = asks (serverStatsFile . config) >>= mapM_ restoreStats diff --git a/src/Simplex/Messaging/Server/Env/STM.hs b/src/Simplex/Messaging/Server/Env/STM.hs index 565438eab..94b4b5478 100644 --- a/src/Simplex/Messaging/Server/Env/STM.hs +++ b/src/Simplex/Messaging/Server/Env/STM.hs @@ -2,7 +2,6 @@ {-# LANGUAGE DuplicateRecordFields #-} {-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE ScopedTypeVariables #-} -{-# LANGUAGE TupleSections #-} module Simplex.Messaging.Server.Env.STM where @@ -104,6 +103,7 @@ data Client = Client ntfSubscriptions :: TMap NotifierId (), rcvQ :: TBQueue (Transmission Cmd), sndQ :: TBQueue (Transmission BrokerMsg), + thVersion :: Version, sessionId :: ByteString, connected :: TVar Bool, activeAt :: TVar SystemTime @@ -124,15 +124,15 @@ newServer qSize = do notifiers <- TM.empty return Server {subscribedQ, subscribers, ntfSubscribedQ, notifiers} -newClient :: Natural -> ByteString -> SystemTime -> STM Client -newClient qSize sessionId ts = do +newClient :: Natural -> Version -> ByteString -> SystemTime -> STM Client +newClient qSize thVersion sessionId ts = do subscriptions <- TM.empty ntfSubscriptions <- TM.empty rcvQ <- newTBQueue qSize sndQ <- newTBQueue qSize connected <- newTVar True activeAt <- newTVar ts - return Client {subscriptions, ntfSubscriptions, rcvQ, sndQ, sessionId, connected, activeAt} + return Client {subscriptions, ntfSubscriptions, rcvQ, sndQ, thVersion, sessionId, connected, activeAt} newSubscription :: SubscriptionThread -> STM Sub newSubscription subThread = do diff --git a/src/Simplex/Messaging/Server/MsgStore.hs b/src/Simplex/Messaging/Server/MsgStore.hs index 43092dc8b..476a03f3f 100644 --- a/src/Simplex/Messaging/Server/MsgStore.hs +++ b/src/Simplex/Messaging/Server/MsgStore.hs @@ -1,44 +1,22 @@ {-# LANGUAGE FunctionalDependencies #-} -{-# LANGUAGE NamedFieldPuns #-} +{-# LANGUAGE LambdaCase #-} {-# LANGUAGE OverloadedStrings #-} module Simplex.Messaging.Server.MsgStore where import Control.Applicative ((<|>)) -import qualified Data.ByteString.Char8 as B import Data.Int (Int64) -import Data.Time.Clock.System (SystemTime) import Numeric.Natural import Simplex.Messaging.Encoding.String -import Simplex.Messaging.Protocol (MsgBody, MsgFlags, MsgId, RecipientId, noMsgFlags) +import Simplex.Messaging.Protocol (Message (..), MsgId, RcvMessage (..), RecipientId) -data Message = Message - { msgId :: MsgId, - ts :: SystemTime, - msgFlags :: MsgFlags, - msgBody :: MsgBody - } - -instance StrEncoding Message where - strEncode Message {msgId, ts, msgFlags, msgBody} = - B.unwords - [ strEncode msgId, - strEncode ts, - "flags=" <> strEncode msgFlags, - strEncode msgBody - ] - strP = do - msgId <- strP_ - ts <- strP_ - msgFlags <- ("flags=" *> strP_) <|> pure noMsgFlags - msgBody <- strP - pure Message {msgId, ts, msgFlags, msgBody} - -data MsgLogRecord = MsgLogRecord RecipientId Message +data MsgLogRecord = MLRv3 RecipientId Message | MLRv1 RecipientId RcvMessage instance StrEncoding MsgLogRecord where - strEncode (MsgLogRecord rId msg) = strEncode (rId, msg) - strP = MsgLogRecord <$> strP_ <*> strP + strEncode = \case + MLRv3 rId msg -> strEncode (Str "v3", rId, msg) + MLRv1 rId msg -> strEncode (rId, msg) + strP = "v3 " *> (MLRv3 <$> strP_ <*> strP) <|> MLRv1 <$> strP_ <*> strP class MonadMsgStore s q m | s -> q where getMsgQueue :: s -> RecipientId -> Natural -> m q diff --git a/src/Simplex/Messaging/Server/MsgStore/STM.hs b/src/Simplex/Messaging/Server/MsgStore/STM.hs index 3a268115b..5905a1789 100644 --- a/src/Simplex/Messaging/Server/MsgStore/STM.hs +++ b/src/Simplex/Messaging/Server/MsgStore/STM.hs @@ -16,7 +16,7 @@ import Data.Functor (($>)) import Data.Int (Int64) import Data.Time.Clock.System (SystemTime (systemSeconds)) import Numeric.Natural -import Simplex.Messaging.Protocol (MsgId, RecipientId) +import Simplex.Messaging.Protocol (Message (..), MsgId, RecipientId) import Simplex.Messaging.Server.MsgStore import Simplex.Messaging.TMap (TMap) import qualified Simplex.Messaging.TMap as TM @@ -78,6 +78,6 @@ instance MonadMsgQueue MsgQueue STM where deleteExpiredMsgs (MsgQueue q) old = loop where loop = tryPeekTBQueue q >>= mapM_ delOldMsg - delOldMsg Message {ts} = - when (systemSeconds ts < old) $ + delOldMsg Message {msgTs} = + when (systemSeconds msgTs < old) $ tryReadTBQueue q >> loop diff --git a/src/Simplex/Messaging/Transport.hs b/src/Simplex/Messaging/Transport.hs index dec917e95..12d8df752 100644 --- a/src/Simplex/Messaging/Transport.hs +++ b/src/Simplex/Messaging/Transport.hs @@ -93,7 +93,7 @@ smpBlockSize :: Int smpBlockSize = 16384 supportedSMPServerVRange :: VersionRange -supportedSMPServerVRange = mkVersionRange 1 2 +supportedSMPServerVRange = mkVersionRange 1 3 simplexMQVersion :: String simplexMQVersion = "3.0.0-beta.4" diff --git a/src/Simplex/Messaging/Transport/Client.hs b/src/Simplex/Messaging/Transport/Client.hs index edb29094d..d708742fa 100644 --- a/src/Simplex/Messaging/Transport/Client.hs +++ b/src/Simplex/Messaging/Transport/Client.hs @@ -1,4 +1,3 @@ -{-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE ScopedTypeVariables #-} module Simplex.Messaging.Transport.Client diff --git a/tests/AgentTests.hs b/tests/AgentTests.hs index c921e91c2..b3d9a9870 100644 --- a/tests/AgentTests.hs +++ b/tests/AgentTests.hs @@ -325,11 +325,11 @@ testServerConnectionAfterError t _ = do bob #: ("1", "alice", "SUB") #> ("1", "alice", ERR (BROKER NETWORK)) alice #: ("1", "bob", "SUB") #> ("1", "bob", ERR (BROKER NETWORK)) withServer $ do - alice <# ("", "bob", SENT 4) + alice <#= \case ("", "bob", SENT 4) -> True; ("", "", UP s ["bob"]) -> s == server; _ -> False + alice <#= \case ("", "bob", SENT 4) -> True; ("", "", UP s ["bob"]) -> s == server; _ -> False bob <# ("", "", UP server ["alice"]) bob <#= \case ("", "alice", Msg "hello") -> True; _ -> False bob #: ("2", "alice", "ACK 4") #> ("2", "alice", OK) - alice <# ("", "", UP server ["bob"]) alice #: ("1", "bob", "SEND F 11\nhello again") #> ("1", "bob", MID 5) alice <# ("", "bob", SENT 5) bob <#= \case ("", "alice", Msg "hello again") -> True; _ -> False diff --git a/tests/AgentTests/FunctionalAPITests.hs b/tests/AgentTests/FunctionalAPITests.hs index ab58813da..7a312d7d0 100644 --- a/tests/AgentTests/FunctionalAPITests.hs +++ b/tests/AgentTests/FunctionalAPITests.hs @@ -469,7 +469,7 @@ testSuspendingAgentCompleteSending t = do Right () <- withSmpServerStoreLogOn t testPort $ \_ -> runExceptT $ do get b =##> \case ("", c, SENT 5) -> c == aId; ("", "", UP {}) -> True; _ -> False get b =##> \case ("", c, SENT 5) -> c == aId; ("", "", UP {}) -> True; _ -> False - get b =##> \case ("", c, SENT 6) -> c == aId; _ -> False + get b =##> \case ("", c, SENT 6) -> c == aId; ("", "", UP {}) -> True; _ -> False ("", "", SUSPENDED) <- get b ("", "", UP {}) <- get a diff --git a/tests/AgentTests/NotificationTests.hs b/tests/AgentTests/NotificationTests.hs index b15f3f4b5..c7e8c9419 100644 --- a/tests/AgentTests/NotificationTests.hs +++ b/tests/AgentTests/NotificationTests.hs @@ -268,25 +268,31 @@ testNotificationSubscriptionNewConnection APNSMockServer {apnsQ} = do (bobId, qInfo) <- createConnection alice SCMInvitation liftIO $ threadDelay 500000 aliceId <- joinConnection bob qInfo "bob's connInfo" + liftIO $ print 0 void $ messageNotification apnsQ ("", _, CONF confId "bob's connInfo") <- get alice liftIO $ threadDelay 500000 allowConnection alice bobId confId "alice's connInfo" + liftIO $ print 1 void $ messageNotification apnsQ get bob ##> ("", aliceId, INFO "alice's connInfo") + liftIO $ print 2 void $ messageNotification apnsQ get alice ##> ("", bobId, CON) + liftIO $ print 3 void $ messageNotification apnsQ get bob ##> ("", aliceId, CON) -- bob sends message 1 <- msgId <$> sendMessage bob aliceId (SMP.MsgFlags True) "hello" get bob ##> ("", aliceId, SENT $ baseId + 1) + liftIO $ print 4 void $ messageNotification apnsQ get alice =##> \case ("", c, Msg "hello") -> c == bobId; _ -> False ackMessage alice bobId $ baseId + 1 -- alice sends message 2 <- msgId <$> sendMessage alice bobId (SMP.MsgFlags True) "hey there" get alice ##> ("", bobId, SENT $ baseId + 2) + liftIO $ print 5 void $ messageNotification apnsQ get bob =##> \case ("", c, Msg "hey there") -> c == aliceId; _ -> False ackMessage bob aliceId $ baseId + 2 @@ -448,7 +454,7 @@ testNotificationsStoreLog t APNSMockServer {apnsQ} = do messageNotification :: TBQueue APNSMockRequest -> ExceptT AgentErrorType IO (C.CbNonce, ByteString) messageNotification apnsQ = do - 500000 `timeout` atomically (readTBQueue apnsQ) >>= \case + 1000000 `timeout` atomically (readTBQueue apnsQ) >>= \case Nothing -> error "no notification" Just APNSMockRequest {notification = APNSNotification {aps = APNSMutableContent {}, notificationData = Just ntfData}, sendApnsResponse} -> do nonce <- C.cbNonce <$> ntfData .-> "nonce" diff --git a/tests/NtfServerTests.hs b/tests/NtfServerTests.hs index 32175b19f..f273862ad 100644 --- a/tests/NtfServerTests.hs +++ b/tests/NtfServerTests.hs @@ -13,6 +13,7 @@ import Control.Concurrent (threadDelay) import Control.Monad.Except (runExceptT) import qualified Data.Aeson as J import qualified Data.Aeson.Types as JT +import Data.Bifunctor (first) import qualified Data.ByteString.Base64.URL as U import Data.ByteString.Char8 (ByteString) import Data.Text.Encoding (encodeUtf8) @@ -35,7 +36,7 @@ import Simplex.Messaging.Encoding.String import Simplex.Messaging.Notifications.Protocol import Simplex.Messaging.Notifications.Server.Push.APNS import qualified Simplex.Messaging.Notifications.Server.Push.APNS as APNS -import Simplex.Messaging.Parsers (parse) +import Simplex.Messaging.Parsers (parse, parseAll) import Simplex.Messaging.Protocol hiding (notification) import Simplex.Messaging.Transport import Test.Hspec @@ -131,11 +132,11 @@ testNotificationSubscription (ATransport t) = notifierId `shouldBe` nId send' APNSRespOk -- receive message - Resp "" _ (MSG mId1 mTs _ msg1) <- tGet rh + Resp "" _ (MSG RcvMessage {msgId = mId1, msgBody = EncRcvMsgBody body}) <- tGet rh + Right ClientRcvMsgBody {msgTs = mTs, msgBody} <- pure $ parseAll clientRcvMsgBodyP =<< first show (C.cbDecrypt rcvDhSecret (C.cbNonce mId1) body) mId1 `shouldBe` msgId mTs `shouldBe` msgTs - let decryptedMsg = C.cbDecrypt rcvDhSecret (C.cbNonce mId1) msg1 - (decryptedMsg, Right "hello") #== "delivered from queue" + (msgBody, "hello") #== "delivered from queue" Resp "6" _ OK <- signSendRecv rh rKey ("6", rId, ACK mId1) pure () -- replace token diff --git a/tests/SMPClient.hs b/tests/SMPClient.hs index c3927fa27..ff4abde50 100644 --- a/tests/SMPClient.hs +++ b/tests/SMPClient.hs @@ -22,6 +22,7 @@ import Simplex.Messaging.Server.Env.STM import Simplex.Messaging.Transport import Simplex.Messaging.Transport.Client import Simplex.Messaging.Transport.KeepAlive +import Simplex.Messaging.Version import Test.Hspec import UnliftIO.Concurrent import qualified UnliftIO.Exception as E @@ -56,6 +57,9 @@ testSMPClient client = Right th -> client th Left e -> error $ show e +cfgV2 :: ServerConfig +cfgV2 = cfg {smpServerVRange = mkVersionRange 1 2} + cfg :: ServerConfig cfg = ServerConfig @@ -79,6 +83,9 @@ cfg = smpServerVRange = supportedSMPServerVRange } +withSmpServerStoreMsgLogOnV2 :: (MonadUnliftIO m, MonadRandom m) => ATransport -> ServiceName -> (ThreadId -> m a) -> m a +withSmpServerStoreMsgLogOnV2 t = withSmpServerConfigOn t cfgV2 {storeLogFile = Just testStoreLogFile, storeMsgsFile = Just testStoreMsgsFile} + withSmpServerStoreMsgLogOn :: (MonadUnliftIO m, MonadRandom m) => ATransport -> ServiceName -> (ThreadId -> m a) -> m a withSmpServerStoreMsgLogOn t = withSmpServerConfigOn t cfg {storeLogFile = Just testStoreLogFile, storeMsgsFile = Just testStoreMsgsFile, serverStatsFile = Just testServerStatsFile} diff --git a/tests/ServerTests.hs b/tests/ServerTests.hs index 4b6c0f47c..8c387ee09 100644 --- a/tests/ServerTests.hs +++ b/tests/ServerTests.hs @@ -1,4 +1,5 @@ {-# LANGUAGE DataKinds #-} +{-# LANGUAGE DuplicateRecordFields #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE LambdaCase #-} {-# LANGUAGE NamedFieldPuns #-} @@ -13,6 +14,7 @@ import Control.Concurrent (ThreadId, killThread, threadDelay) import Control.Concurrent.STM import Control.Exception (SomeException, try) import Control.Monad.Except (forM, forM_, runExceptT) +import Data.Bifunctor (first) import Data.ByteString.Base64 import Data.ByteString.Char8 (ByteString) import qualified Data.ByteString.Char8 as B @@ -20,6 +22,7 @@ import SMPClient import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Encoding import Simplex.Messaging.Encoding.String +import Simplex.Messaging.Parsers (parseAll) import Simplex.Messaging.Protocol import Simplex.Messaging.Server.Env.STM (ServerConfig (..)) import Simplex.Messaging.Server.Expiration @@ -34,7 +37,8 @@ serverTests :: ATransport -> Spec serverTests t@(ATransport t') = do describe "SMP syntax" $ syntaxTests t describe "SMP queues" $ do - describe "NEW and KEY commands, SEND messages" $ testCreateSecure t + describe "NEW and KEY commands, SEND messages (v2)" $ testCreateSecureV2 t' + describe "NEW and KEY commands, SEND messages (v3)" $ testCreateSecure t describe "NEW, OFF and DEL commands, SEND messages" $ testCreateDelete t describe "Stress test" $ stressTest t describe "allowNewQueues setting" $ testAllowNewQueues t' @@ -45,6 +49,7 @@ serverTests t@(ATransport t') = do describe "GET & SUB commands" $ testGetSubCommands t' describe "Store log" $ testWithStoreLog t describe "Restore messages" $ testRestoreMessages t + describe "Restore messages (v2)" $ testRestoreMessagesV2 t describe "Timing of AUTH error" $ testTiming t describe "Message notifications" $ testMessageNotifications t describe "Message expiration" $ do @@ -58,6 +63,9 @@ pattern Resp corrId queueId command <- (_, _, (corrId, queueId, Right command)) pattern Ids :: RecipientId -> SenderId -> RcvPublicDhKey -> BrokerMsg pattern Ids rId sId srvDh <- IDS (QIK rId sId srvDh) +pattern Msg :: MsgId -> MsgBody -> BrokerMsg +pattern Msg msgId body <- MSG RcvMessage {msgId, msgBody = EncRcvMsgBody body} + sendRecv :: forall c p. (Transport c, PartyI p) => THandle c -> (Maybe C.ASignature, ByteString, ByteString, Command p) -> IO (SignedTransmission BrokerMsg) sendRecv h@THandle {thVersion, sessionId} (sgn, corrId, qId, cmd) = do let t = encodeTransmission thVersion sessionId (CorrId corrId, qId, cmd) @@ -80,21 +88,29 @@ _SEND = SEND noMsgFlags _SEND' :: MsgBody -> Command 'Sender _SEND' = SEND MsgFlags {notification = True} -testCreateSecure :: ATransport -> Spec -testCreateSecure (ATransport t) = +decryptMsgV2 :: C.DhSecret 'C.X25519 -> ByteString -> ByteString -> Either C.CryptoError ByteString +decryptMsgV2 dhShared = C.cbDecrypt dhShared . C.cbNonce + +decryptMsgV3 :: C.DhSecret 'C.X25519 -> ByteString -> ByteString -> Either String MsgBody +decryptMsgV3 dhShared nonce body = do + ClientRcvMsgBody {msgBody} <- parseAll clientRcvMsgBodyP =<< first show (C.cbDecrypt dhShared (C.cbNonce nonce) body) + pure msgBody + +testCreateSecureV2 :: forall c. Transport c => TProxy c -> Spec +testCreateSecureV2 _ = it "should create (NEW) and secure (KEY) queue" $ - smpTest t $ \h -> do + withSmpServerConfigOn (transport @c) cfgV2 testPort $ \_ -> testSMPClient @c $ \h -> do (rPub, rKey) <- C.generateSignatureKeyPair C.SEd448 (dhPub, dhPriv :: C.PrivateKeyX25519) <- C.generateKeyPair' Resp "abcd" rId1 (Ids rId sId srvDh) <- signSendRecv h rKey ("abcd", "", NEW rPub dhPub) - let dec nonce = C.cbDecrypt (C.dh' srvDh dhPriv) (C.cbNonce nonce) + let dec = decryptMsgV2 $ C.dh' srvDh dhPriv (rId1, "") #== "creates queue" Resp "bcda" sId1 ok1 <- sendRecv h ("", "bcda", sId, _SEND "hello") (ok1, OK) #== "accepts unsigned SEND" (sId1, sId) #== "same queue ID in response 1" - Resp "" _ (MSG mId1 _ _ msg1) <- tGet h + Resp "" _ (Msg mId1 msg1) <- tGet h (dec mId1 msg1, Right "hello") #== "delivers message" Resp "cdab" _ ok4 <- signSendRecv h rKey ("cdab", rId, ACK mId1) @@ -124,7 +140,7 @@ testCreateSecure (ATransport t) = Resp "bcda" _ ok3 <- signSendRecv h sKey ("bcda", sId, _SEND "hello again") (ok3, OK) #== "accepts signed SEND" - Resp "" _ (MSG mId2 _ _ msg2) <- tGet h + Resp "" _ (Msg mId2 msg2) <- tGet h (dec mId2 msg2, Right "hello again") #== "delivers message 2" Resp "cdab" _ ok5 <- signSendRecv h rKey ("cdab", rId, ACK mId2) @@ -133,6 +149,77 @@ testCreateSecure (ATransport t) = Resp "dabc" _ err5 <- sendRecv h ("", "dabc", sId, _SEND "hello") (err5, ERR AUTH) #== "rejects unsigned SEND" + let maxAllowedMessage = B.replicate maxMessageLength '-' + Resp "bcda" _ OK <- signSendRecv h sKey ("bcda", sId, _SEND maxAllowedMessage) + Resp "" _ (Msg mId3 msg3) <- tGet h + (dec mId3 msg3, Right maxAllowedMessage) #== "delivers message of max size" + + let biggerMessage = B.replicate (maxMessageLength + 1) '-' + Resp "bcda" _ (ERR LARGE_MSG) <- signSendRecv h sKey ("bcda", sId, _SEND biggerMessage) + pure () + +testCreateSecure :: ATransport -> Spec +testCreateSecure (ATransport t) = + it "should create (NEW) and secure (KEY) queue" $ + smpTest t $ \h -> do + (rPub, rKey) <- C.generateSignatureKeyPair C.SEd448 + (dhPub, dhPriv :: C.PrivateKeyX25519) <- C.generateKeyPair' + Resp "abcd" rId1 (Ids rId sId srvDh) <- signSendRecv h rKey ("abcd", "", NEW rPub dhPub) + let dec = decryptMsgV3 $ C.dh' srvDh dhPriv + (rId1, "") #== "creates queue" + + Resp "bcda" sId1 ok1 <- sendRecv h ("", "bcda", sId, _SEND "hello") + (ok1, OK) #== "accepts unsigned SEND" + (sId1, sId) #== "same queue ID in response 1" + + Resp "" _ (Msg mId1 msg1) <- tGet h + (dec mId1 msg1, Right "hello") #== "delivers message" + + Resp "cdab" _ ok4 <- signSendRecv h rKey ("cdab", rId, ACK mId1) + (ok4, OK) #== "replies OK when message acknowledged if no more messages" + + Resp "dabc" _ err6 <- signSendRecv h rKey ("dabc", rId, ACK mId1) + (err6, ERR NO_MSG) #== "replies ERR when message acknowledged without messages" + + (sPub, sKey) <- C.generateSignatureKeyPair C.SEd448 + Resp "abcd" sId2 err1 <- signSendRecv h sKey ("abcd", sId, _SEND "hello") + (err1, ERR AUTH) #== "rejects signed SEND" + (sId2, sId) #== "same queue ID in response 2" + + Resp "bcda" _ err2 <- sendRecv h (sampleSig, "bcda", rId, KEY sPub) + (err2, ERR AUTH) #== "rejects KEY with wrong signature" + + Resp "cdab" _ err3 <- signSendRecv h rKey ("cdab", sId, KEY sPub) + (err3, ERR AUTH) #== "rejects KEY with sender's ID" + + Resp "dabc" rId2 ok2 <- signSendRecv h rKey ("dabc", rId, KEY sPub) + (ok2, OK) #== "secures queue" + (rId2, rId) #== "same queue ID in response 3" + + Resp "abcd" _ err4 <- signSendRecv h rKey ("abcd", rId, KEY sPub) + (err4, ERR AUTH) #== "rejects KEY if already secured" + + Resp "bcda" _ ok3 <- signSendRecv h sKey ("bcda", sId, _SEND "hello again") + (ok3, OK) #== "accepts signed SEND" + + Resp "" _ (Msg mId2 msg2) <- tGet h + (dec mId2 msg2, Right "hello again") #== "delivers message 2" + + Resp "cdab" _ ok5 <- signSendRecv h rKey ("cdab", rId, ACK mId2) + (ok5, OK) #== "replies OK when message acknowledged 2" + + Resp "dabc" _ err5 <- sendRecv h ("", "dabc", sId, _SEND "hello") + (err5, ERR AUTH) #== "rejects unsigned SEND" + + let maxAllowedMessage = B.replicate maxMessageLength '-' + Resp "bcda" _ OK <- signSendRecv h sKey ("bcda", sId, _SEND maxAllowedMessage) + Resp "" _ (Msg mId3 msg3) <- tGet h + (dec mId3 msg3, Right maxAllowedMessage) #== "delivers message of max size" + + let biggerMessage = B.replicate (maxMessageLength + 1) '-' + Resp "bcda" _ (ERR LARGE_MSG) <- signSendRecv h sKey ("bcda", sId, _SEND biggerMessage) + pure () + testCreateDelete :: ATransport -> Spec testCreateDelete (ATransport t) = it "should create (NEW), suspend (OFF) and delete (DEL) queue" $ @@ -140,7 +227,7 @@ testCreateDelete (ATransport t) = (rPub, rKey) <- C.generateSignatureKeyPair C.SEd25519 (dhPub, dhPriv :: C.PrivateKeyX25519) <- C.generateKeyPair' Resp "abcd" rId1 (Ids rId sId srvDh) <- signSendRecv rh rKey ("abcd", "", NEW rPub dhPub) - let dec nonce = C.cbDecrypt (C.dh' srvDh dhPriv) (C.cbNonce nonce) + let dec = decryptMsgV3 $ C.dh' srvDh dhPriv (rId1, "") #== "creates queue" (sPub, sKey) <- C.generateSignatureKeyPair C.SEd25519 @@ -153,7 +240,7 @@ testCreateDelete (ATransport t) = Resp "dabc" _ ok7 <- signSendRecv sh sKey ("dabc", sId, _SEND "hello 2") (ok7, OK) #== "accepts signed SEND 2 - this message is not delivered because the first is not ACKed" - Resp "" _ (MSG mId1 _ _ msg1) <- tGet rh + Resp "" _ (Msg mId1 msg1) <- tGet rh (dec mId1 msg1, Right "hello") #== "delivers message" Resp "abcd" _ err1 <- sendRecv rh (sampleSig, "abcd", rId, OFF) @@ -175,7 +262,7 @@ testCreateDelete (ATransport t) = Resp "bcda" _ ok4 <- signSendRecv rh rKey ("bcda", rId, OFF) (ok4, OK) #== "accepts OFF when suspended" - Resp "cdab" _ (MSG mId2 _ _ msg2) <- signSendRecv rh rKey ("cdab", rId, SUB) + Resp "cdab" _ (Msg mId2 msg2) <- signSendRecv rh rKey ("cdab", rId, SUB) (dec mId2 msg2, Right "hello") #== "accepts SUB when suspended and delivers the message again (because was not ACKed)" Resp "dabc" _ err5 <- sendRecv rh (sampleSig, "dabc", rId, DEL) @@ -237,14 +324,14 @@ testDuplex (ATransport t) = (arPub, arKey) <- C.generateSignatureKeyPair C.SEd448 (aDhPub, aDhPriv :: C.PrivateKeyX25519) <- C.generateKeyPair' Resp "abcd" _ (Ids aRcv aSnd aSrvDh) <- signSendRecv alice arKey ("abcd", "", NEW arPub aDhPub) - let aDec nonce = C.cbDecrypt (C.dh' aSrvDh aDhPriv) (C.cbNonce nonce) + let aDec = decryptMsgV3 $ C.dh' aSrvDh aDhPriv -- aSnd ID is passed to Bob out-of-band (bsPub, bsKey) <- C.generateSignatureKeyPair C.SEd448 Resp "bcda" _ OK <- sendRecv bob ("", "bcda", aSnd, _SEND $ "key " <> strEncode bsPub) -- "key ..." is ad-hoc, not a part of SMP protocol - Resp "" _ (MSG mId1 _ _ msg1) <- tGet alice + Resp "" _ (Msg mId1 msg1) <- tGet alice Resp "cdab" _ OK <- signSendRecv alice arKey ("cdab", aRcv, ACK mId1) Right ["key", bobKey] <- pure $ B.words <$> aDec mId1 msg1 (bobKey, strEncode bsPub) #== "key received from Bob" @@ -253,11 +340,11 @@ testDuplex (ATransport t) = (brPub, brKey) <- C.generateSignatureKeyPair C.SEd448 (bDhPub, bDhPriv :: C.PrivateKeyX25519) <- C.generateKeyPair' Resp "abcd" _ (Ids bRcv bSnd bSrvDh) <- signSendRecv bob brKey ("abcd", "", NEW brPub bDhPub) - let bDec nonce = C.cbDecrypt (C.dh' bSrvDh bDhPriv) (C.cbNonce nonce) + let bDec = decryptMsgV3 $ C.dh' bSrvDh bDhPriv Resp "bcda" _ OK <- signSendRecv bob bsKey ("bcda", aSnd, _SEND $ "reply_id " <> encode bSnd) -- "reply_id ..." is ad-hoc, not a part of SMP protocol - Resp "" _ (MSG mId2 _ _ msg2) <- tGet alice + Resp "" _ (Msg mId2 msg2) <- tGet alice Resp "cdab" _ OK <- signSendRecv alice arKey ("cdab", aRcv, ACK mId2) Right ["reply_id", bId] <- pure $ B.words <$> aDec mId2 msg2 (bId, encode bSnd) #== "reply queue ID received from Bob" @@ -266,7 +353,7 @@ testDuplex (ATransport t) = Resp "dabc" _ OK <- sendRecv alice ("", "dabc", bSnd, _SEND $ "key " <> strEncode asPub) -- "key ..." is ad-hoc, not a part of SMP protocol - Resp "" _ (MSG mId3 _ _ msg3) <- tGet bob + Resp "" _ (Msg mId3 msg3) <- tGet bob Resp "abcd" _ OK <- signSendRecv bob brKey ("abcd", bRcv, ACK mId3) Right ["key", aliceKey] <- pure $ B.words <$> bDec mId3 msg3 (aliceKey, strEncode asPub) #== "key received from Alice" @@ -274,13 +361,13 @@ testDuplex (ATransport t) = Resp "cdab" _ OK <- signSendRecv bob bsKey ("cdab", aSnd, _SEND "hi alice") - Resp "" _ (MSG mId4 _ _ msg4) <- tGet alice + Resp "" _ (Msg mId4 msg4) <- tGet alice Resp "dabc" _ OK <- signSendRecv alice arKey ("dabc", aRcv, ACK mId4) (aDec mId4 msg4, Right "hi alice") #== "message received from Bob" Resp "abcd" _ OK <- signSendRecv alice asKey ("abcd", bSnd, _SEND "how are you bob") - Resp "" _ (MSG mId5 _ _ msg5) <- tGet bob + Resp "" _ (Msg mId5 msg5) <- tGet bob Resp "bcda" _ OK <- signSendRecv bob brKey ("bcda", bRcv, ACK mId5) (bDec mId5 msg5, Right "how are you bob") #== "message received from alice" @@ -291,18 +378,18 @@ testSwitchSub (ATransport t) = (rPub, rKey) <- C.generateSignatureKeyPair C.SEd448 (dhPub, dhPriv :: C.PrivateKeyX25519) <- C.generateKeyPair' Resp "abcd" _ (Ids rId sId srvDh) <- signSendRecv rh1 rKey ("abcd", "", NEW rPub dhPub) - let dec nonce = C.cbDecrypt (C.dh' srvDh dhPriv) (C.cbNonce nonce) + let dec = decryptMsgV3 $ C.dh' srvDh dhPriv Resp "bcda" _ ok1 <- sendRecv sh ("", "bcda", sId, _SEND "test1") (ok1, OK) #== "sent test message 1" Resp "cdab" _ ok2 <- sendRecv sh ("", "cdab", sId, _SEND "test2, no ACK") (ok2, OK) #== "sent test message 2" - Resp "" _ (MSG mId1 _ _ msg1) <- tGet rh1 + Resp "" _ (Msg mId1 msg1) <- tGet rh1 (dec mId1 msg1, Right "test1") #== "test message 1 delivered to the 1st TCP connection" - Resp "abcd" _ (MSG mId2 _ _ msg2) <- signSendRecv rh1 rKey ("abcd", rId, ACK mId1) + Resp "abcd" _ (Msg mId2 msg2) <- signSendRecv rh1 rKey ("abcd", rId, ACK mId1) (dec mId2 msg2, Right "test2, no ACK") #== "test message 2 delivered, no ACK" - Resp "bcda" _ (MSG mId2' _ _ msg2') <- signSendRecv rh2 rKey ("bcda", rId, SUB) + Resp "bcda" _ (Msg mId2' msg2') <- signSendRecv rh2 rKey ("bcda", rId, SUB) (dec mId2' msg2', Right "test2, no ACK") #== "same simplex queue via another TCP connection, tes2 delivered again (no ACK in 1st queue)" Resp "cdab" _ OK <- signSendRecv rh2 rKey ("cdab", rId, ACK mId2') @@ -311,7 +398,7 @@ testSwitchSub (ATransport t) = Resp "dabc" _ OK <- sendRecv sh ("", "dabc", sId, _SEND "test3") - Resp "" _ (MSG mId3 _ _ msg3) <- tGet rh2 + Resp "" _ (Msg mId3 msg3) <- tGet rh2 (dec mId3 msg3, Right "test3") #== "delivered to the 2nd TCP connection" Resp "abcd" _ err <- signSendRecv rh1 rKey ("abcd", rId, ACK mId3) @@ -334,9 +421,9 @@ testGetCommand t = atomically . putTMVar queue =<< createAndSecureQueue rh sPub testSMPClient @c $ \rh -> do (sId, rId, rKey, dhShared) <- atomically $ takeTMVar queue - let dec nonce = C.cbDecrypt dhShared (C.cbNonce nonce) + let dec = decryptMsgV3 dhShared Resp "1" _ OK <- signSendRecv sh sKey ("1", sId, _SEND "hello") - Resp "2" _ (MSG mId1 _ _ msg1) <- signSendRecv rh rKey ("2", rId, GET) + Resp "2" _ (Msg mId1 msg1) <- signSendRecv rh rKey ("2", rId, GET) (dec mId1 msg1, Right "hello") #== "retrieved from queue" Resp "3" _ OK <- signSendRecv rh rKey ("3", rId, ACK mId1) Resp "4" _ OK <- signSendRecv rh rKey ("4", rId, GET) @@ -348,14 +435,14 @@ testGetSubCommands t = (sPub, sKey) <- C.generateSignatureKeyPair C.SEd25519 smpTest3 t $ \rh1 rh2 sh -> do (sId, rId, rKey, dhShared) <- createAndSecureQueue rh1 sPub - let dec nonce = C.cbDecrypt dhShared (C.cbNonce nonce) + let dec = decryptMsgV3 dhShared Resp "1" _ OK <- signSendRecv sh sKey ("1", sId, _SEND "hello 1") Resp "1a" _ OK <- signSendRecv sh sKey ("1a", sId, _SEND "hello 2") Resp "1b" _ OK <- signSendRecv sh sKey ("1b", sId, _SEND "hello 3") Resp "1c" _ OK <- signSendRecv sh sKey ("1c", sId, _SEND "hello 4") -- both get the same if not ACK'd - Resp "" _ (MSG mId1 _ _ msg1) <- tGet rh1 - Resp "2" _ (MSG mId1' _ _ msg1') <- signSendRecv rh2 rKey ("2", rId, GET) + Resp "" _ (Msg mId1 msg1) <- tGet rh1 + Resp "2" _ (Msg mId1' msg1') <- signSendRecv rh2 rKey ("2", rId, GET) (dec mId1 msg1, Right "hello 1") #== "received from queue via SUB" (dec mId1' msg1', Right "hello 1") #== "retrieved from queue with GET" mId1 `shouldBe` mId1' @@ -364,27 +451,27 @@ testGetSubCommands t = Resp "3" _ (ERR (CMD PROHIBITED)) <- signSendRecv rh1 rKey ("3", rId, GET) Resp "3a" _ (ERR (CMD PROHIBITED)) <- signSendRecv rh2 rKey ("3a", rId, SUB) -- ACK for SUB delivers the next message - Resp "4" _ (MSG mId2 _ _ msg2) <- signSendRecv rh1 rKey ("4", rId, ACK mId1) + Resp "4" _ (Msg mId2 msg2) <- signSendRecv rh1 rKey ("4", rId, ACK mId1) (dec mId2 msg2, Right "hello 2") #== "received from queue via SUB" -- bad msgId returns error Resp "5" _ (ERR NO_MSG) <- signSendRecv rh2 rKey ("5", rId, ACK "1234") -- already ACK'd by subscriber, but still returns OK when msgId matches Resp "5a" _ OK <- signSendRecv rh2 rKey ("5a", rId, ACK mId1) -- msg2 is not lost - even if subscriber does not ACK it, it is delivered to getter - Resp "6" _ (MSG mId2' _ _ msg2') <- signSendRecv rh2 rKey ("6", rId, GET) + Resp "6" _ (Msg mId2' msg2') <- signSendRecv rh2 rKey ("6", rId, GET) (dec mId2' msg2', Right "hello 2") #== "retrieved from queue with GET" mId2 `shouldBe` mId2' msg2 `shouldBe` msg2' -- getter ACK returns OK, even though there is the next message Resp "7" _ OK <- signSendRecv rh2 rKey ("7", rId, ACK mId2') - Resp "8" _ (MSG mId3 _ _ msg3) <- signSendRecv rh2 rKey ("8", rId, GET) + Resp "8" _ (Msg mId3 msg3) <- signSendRecv rh2 rKey ("8", rId, GET) (dec mId3 msg3, Right "hello 3") #== "retrieved from queue with GET" -- subscriber ACK does not lose message - Resp "9" _ (MSG mId3' _ _ msg3') <- signSendRecv rh1 rKey ("9", rId, ACK mId2') + Resp "9" _ (Msg mId3' msg3') <- signSendRecv rh1 rKey ("9", rId, ACK mId2') (dec mId3' msg3', Right "hello 3") #== "retrieved from queue with GET" mId3 `shouldBe` mId3' msg3 `shouldBe` msg3' - Resp "10" _ (MSG mId4 _ _ msg4) <- signSendRecv rh1 rKey ("10", rId, ACK mId3) + Resp "10" _ (Msg mId4 msg4) <- signSendRecv rh1 rKey ("10", rId, ACK mId3) (dec mId4 msg4, Right "hello 4") #== "retrieved from queue with GET" Resp "11" _ OK <- signSendRecv rh1 rKey ("11", rId, ACK mId4) -- no more messages for getter too @@ -416,15 +503,15 @@ testWithStoreLog at@(ATransport t) = writeTVar notifierId nId Resp "dabc" _ OK <- signSendRecv h1 nKey ("dabc", nId, NSUB) Resp "bcda" _ OK <- signSendRecv h sKey1 ("bcda", sId1, _SEND' "hello") - Resp "" _ (MSG mId1 _ _ msg1) <- tGet h - (C.cbDecrypt dhShared (C.cbNonce mId1) msg1, Right "hello") #== "delivered from queue 1" + Resp "" _ (Msg mId1 msg1) <- tGet h + (decryptMsgV3 dhShared mId1 msg1, Right "hello") #== "delivered from queue 1" Resp "" _ (NMSG _ _) <- tGet h1 (sId2, rId2, rKey2, dhShared2) <- createAndSecureQueue h sPub2 atomically $ writeTVar senderId2 sId2 Resp "cdab" _ OK <- signSendRecv h sKey2 ("cdab", sId2, _SEND "hello too") - Resp "" _ (MSG mId2 _ _ msg2) <- tGet h - (C.cbDecrypt dhShared2 (C.cbNonce mId2) msg2, Right "hello too") #== "delivered from queue 2" + Resp "" _ (Msg mId2 msg2) <- tGet h + (decryptMsgV3 dhShared2 mId2 msg2, Right "hello too") #== "delivered from queue 2" Resp "dabc" _ OK <- signSendRecv h rKey2 ("dabc", rId2, DEL) pure () @@ -446,8 +533,8 @@ testWithStoreLog at@(ATransport t) = nId <- readTVarIO notifierId Resp "dabc" _ OK <- signSendRecv h1 nKey ("dabc", nId, NSUB) Resp "bcda" _ OK <- signSendRecv h sKey1 ("bcda", sId1, _SEND' "hello") - Resp "cdab" _ (MSG mId3 _ _ msg3) <- signSendRecv h rKey1 ("cdab", rId1, SUB) - (C.cbDecrypt dh1 (C.cbNonce mId3) msg3, Right "hello") #== "delivered from restored queue" + Resp "cdab" _ (Msg mId3 msg3) <- signSendRecv h rKey1 ("cdab", rId1, SUB) + (decryptMsgV3 dh1 mId3 msg3, Right "hello") #== "delivered from restored queue" Resp "" _ (NMSG _ _) <- tGet h1 -- this queue is removed - not restored sId2 <- readTVarIO senderId2 @@ -489,9 +576,9 @@ testRestoreMessages at@(ATransport t) = writeTVar dhShared $ Just dh writeTVar senderId sId Resp "1" _ OK <- signSendRecv h sKey ("1", sId, _SEND "hello") - Resp "" _ (MSG mId1 _ _ msg1) <- tGet h1 + Resp "" _ (Msg mId1 msg1) <- tGet h1 Resp "1a" _ OK <- signSendRecv h1 rKey ("1a", rId, ACK mId1) - (C.cbDecrypt dh (C.cbNonce mId1) msg1, Right "hello") #== "message delivered" + (decryptMsgV3 dh mId1 msg1, Right "hello") #== "message delivered" -- messages below are delivered after server restart sId <- readTVarIO senderId Resp "2" _ OK <- signSendRecv h sKey ("2", sId, _SEND "hello 2") @@ -506,12 +593,13 @@ testRestoreMessages at@(ATransport t) = rId <- readTVarIO recipientId Just rKey <- readTVarIO recipientKey Just dh <- readTVarIO dhShared - Resp "2" _ (MSG mId2 _ _ msg2) <- signSendRecv h rKey ("2", rId, SUB) - (C.cbDecrypt dh (C.cbNonce mId2) msg2, Right "hello 2") #== "restored message delivered" - Resp "3" _ (MSG mId3 _ _ msg3) <- signSendRecv h rKey ("3", rId, ACK mId2) - (C.cbDecrypt dh (C.cbNonce mId3) msg3, Right "hello 3") #== "restored message delivered" - Resp "4" _ (MSG mId4 _ _ msg4) <- signSendRecv h rKey ("4", rId, ACK mId3) - (C.cbDecrypt dh (C.cbNonce mId4) msg4, Right "hello 4") #== "restored message delivered" + let dec = decryptMsgV3 dh + Resp "2" _ (Msg mId2 msg2) <- signSendRecv h rKey ("2", rId, SUB) + (dec mId2 msg2, Right "hello 2") #== "restored message delivered" + Resp "3" _ (Msg mId3 msg3) <- signSendRecv h rKey ("3", rId, ACK mId2) + (dec mId3 msg3, Right "hello 3") #== "restored message delivered" + Resp "4" _ (Msg mId4 msg4) <- signSendRecv h rKey ("4", rId, ACK mId3) + (dec mId4 msg4, Right "hello 4") #== "restored message delivered" logSize testStoreLogFile `shouldReturn` 1 -- the last message is not removed because it was not ACK'd @@ -521,9 +609,78 @@ testRestoreMessages at@(ATransport t) = rId <- readTVarIO recipientId Just rKey <- readTVarIO recipientKey Just dh <- readTVarIO dhShared - Resp "4" _ (MSG mId4 _ _ msg4) <- signSendRecv h rKey ("4", rId, SUB) + Resp "4" _ (Msg mId4 msg4) <- signSendRecv h rKey ("4", rId, SUB) Resp "5" _ OK <- signSendRecv h rKey ("5", rId, ACK mId4) - (C.cbDecrypt dh (C.cbNonce mId4) msg4, Right "hello 4") #== "restored message delivered" + (decryptMsgV3 dh mId4 msg4, Right "hello 4") #== "restored message delivered" + + logSize testStoreLogFile `shouldReturn` 1 + logSize testStoreMsgsFile `shouldReturn` 0 + + removeFile testStoreLogFile + removeFile testStoreMsgsFile + where + runTest :: Transport c => TProxy c -> (THandle c -> IO ()) -> ThreadId -> Expectation + runTest _ test' server = do + testSMPClient test' `shouldReturn` () + killThread server + + runClient :: Transport c => TProxy c -> (THandle c -> IO ()) -> Expectation + runClient _ test' = testSMPClient test' `shouldReturn` () + +testRestoreMessagesV2 :: ATransport -> Spec +testRestoreMessagesV2 at@(ATransport t) = + it "should store messages on exit and restore on start" $ do + (sPub, sKey) <- C.generateSignatureKeyPair C.SEd25519 + recipientId <- newTVarIO "" + recipientKey <- newTVarIO Nothing + dhShared <- newTVarIO Nothing + senderId <- newTVarIO "" + + withSmpServerStoreMsgLogOnV2 at testPort . runTest t $ \h -> do + runClient t $ \h1 -> do + (sId, rId, rKey, dh) <- createAndSecureQueue h1 sPub + atomically $ do + writeTVar recipientId rId + writeTVar recipientKey $ Just rKey + writeTVar dhShared $ Just dh + writeTVar senderId sId + Resp "1" _ OK <- signSendRecv h sKey ("1", sId, _SEND "hello") + Resp "" _ (Msg mId1 msg1) <- tGet h1 + Resp "1a" _ OK <- signSendRecv h1 rKey ("1a", rId, ACK mId1) + (decryptMsgV2 dh mId1 msg1, Right "hello") #== "message delivered" + -- messages below are delivered after server restart + sId <- readTVarIO senderId + Resp "2" _ OK <- signSendRecv h sKey ("2", sId, _SEND "hello 2") + Resp "3" _ OK <- signSendRecv h sKey ("3", sId, _SEND "hello 3") + Resp "4" _ OK <- signSendRecv h sKey ("4", sId, _SEND "hello 4") + pure () + + logSize testStoreLogFile `shouldReturn` 2 + logSize testStoreMsgsFile `shouldReturn` 3 + + withSmpServerStoreMsgLogOnV2 at testPort . runTest t $ \h -> do + rId <- readTVarIO recipientId + Just rKey <- readTVarIO recipientKey + Just dh <- readTVarIO dhShared + let dec = decryptMsgV2 dh + Resp "2" _ (Msg mId2 msg2) <- signSendRecv h rKey ("2", rId, SUB) + (dec mId2 msg2, Right "hello 2") #== "restored message delivered" + Resp "3" _ (Msg mId3 msg3) <- signSendRecv h rKey ("3", rId, ACK mId2) + (dec mId3 msg3, Right "hello 3") #== "restored message delivered" + Resp "4" _ (Msg mId4 msg4) <- signSendRecv h rKey ("4", rId, ACK mId3) + (dec mId4 msg4, Right "hello 4") #== "restored message delivered" + + logSize testStoreLogFile `shouldReturn` 1 + -- the last message is not removed because it was not ACK'd + logSize testStoreMsgsFile `shouldReturn` 1 + + withSmpServerStoreMsgLogOnV2 at testPort . runTest t $ \h -> do + rId <- readTVarIO recipientId + Just rKey <- readTVarIO recipientKey + Just dh <- readTVarIO dhShared + Resp "4" _ (Msg mId4 msg4) <- signSendRecv h rKey ("4", rId, SUB) + Resp "5" _ OK <- signSendRecv h rKey ("5", rId, ACK mId4) + (decryptMsgV2 dh mId4 msg4, Right "hello 4") #== "restored message delivered" logSize testStoreLogFile `shouldReturn` 1 logSize testStoreMsgsFile `shouldReturn` 0 @@ -568,7 +725,7 @@ testTiming (ATransport t) = (rPub, rKey) <- generateKeys goodKeySize (dhPub, dhPriv :: C.PrivateKeyX25519) <- C.generateKeyPair' Resp "abcd" "" (Ids rId sId srvDh) <- signSendRecv rh rKey ("abcd", "", NEW rPub dhPub) - let dec nonce = C.cbDecrypt (C.dh' srvDh dhPriv) (C.cbNonce nonce) + let dec = decryptMsgV3 $ C.dh' srvDh dhPriv Resp "cdab" _ OK <- signSendRecv rh rKey ("cdab", rId, SUB) (_, badKey) <- generateKeys badKeySize @@ -578,7 +735,7 @@ testTiming (ATransport t) = Resp "dabc" _ OK <- signSendRecv rh rKey ("dabc", rId, KEY sPub) Resp "bcda" _ OK <- signSendRecv sh sKey ("bcda", sId, _SEND "hello") - Resp "" _ (MSG mId _ _ msg) <- tGet rh + Resp "" _ (Msg mId msg) <- tGet rh (dec mId msg, Right "hello") #== "delivered from queue" runTimingTest sh badKey sId $ _SEND "hello" @@ -610,21 +767,21 @@ testMessageNotifications (ATransport t) = (nPub, nKey) <- C.generateSignatureKeyPair C.SEd25519 smpTest4 t $ \rh sh nh1 nh2 -> do (sId, rId, rKey, dhShared) <- createAndSecureQueue rh sPub - let dec nonce = C.cbDecrypt dhShared (C.cbNonce nonce) + let dec = decryptMsgV3 dhShared (rcvNtfPubDhKey, _) <- C.generateKeyPair' Resp "1" _ (NID nId' _) <- signSendRecv rh rKey ("1", rId, NKEY nPub rcvNtfPubDhKey) Resp "1a" _ (NID nId _) <- signSendRecv rh rKey ("1a", rId, NKEY nPub rcvNtfPubDhKey) nId' `shouldNotBe` nId Resp "2" _ OK <- signSendRecv nh1 nKey ("2", nId, NSUB) Resp "3" _ OK <- signSendRecv sh sKey ("3", sId, _SEND' "hello") - Resp "" _ (MSG mId1 _ _ msg1) <- tGet rh + Resp "" _ (Msg mId1 msg1) <- tGet rh (dec mId1 msg1, Right "hello") #== "delivered from queue" Resp "3a" _ OK <- signSendRecv rh rKey ("3a", rId, ACK mId1) Resp "" _ (NMSG _ _) <- tGet nh1 Resp "4" _ OK <- signSendRecv nh2 nKey ("4", nId, NSUB) Resp "" _ END <- tGet nh1 Resp "5" _ OK <- signSendRecv sh sKey ("5", sId, _SEND' "hello again") - Resp "" _ (MSG mId2 _ _ msg2) <- tGet rh + Resp "" _ (Msg mId2 msg2) <- tGet rh Resp "5a" _ OK <- signSendRecv rh rKey ("5a", rId, ACK mId2) (dec mId2 msg2, Right "hello again") #== "delivered from queue again" Resp "" _ (NMSG _ _) <- tGet nh2 @@ -633,7 +790,7 @@ testMessageNotifications (ATransport t) = Just _ -> error "nothing else should be delivered to the 1st notifier's TCP connection" Resp "6" _ OK <- signSendRecv rh rKey ("6", rId, NDEL) Resp "7" _ OK <- signSendRecv sh sKey ("7", sId, _SEND' "hello there") - Resp "" _ (MSG mId3 _ _ msg3) <- tGet rh + Resp "" _ (Msg mId3 msg3) <- tGet rh (dec mId3 msg3, Right "hello there") #== "delivered from queue again" 1000 `timeout` tGet @BrokerMsg nh2 >>= \case Nothing -> pure () @@ -647,12 +804,12 @@ testMsgExpireOnSend t = withSmpServerConfigOn (ATransport t) cfg' testPort $ \_ -> testSMPClient @c $ \sh -> do (sId, rId, rKey, dhShared) <- testSMPClient @c $ \rh -> createAndSecureQueue rh sPub - let dec nonce = C.cbDecrypt dhShared (C.cbNonce nonce) + let dec = decryptMsgV3 dhShared Resp "1" _ OK <- signSendRecv sh sKey ("1", sId, _SEND "hello (should expire)") threadDelay 2500000 Resp "2" _ OK <- signSendRecv sh sKey ("2", sId, _SEND "hello (should NOT expire)") testSMPClient @c $ \rh -> do - Resp "3" _ (MSG mId _ _ msg) <- signSendRecv rh rKey ("3", rId, SUB) + Resp "3" _ (Msg mId msg) <- signSendRecv rh rKey ("3", rId, SUB) (dec mId msg, Right "hello (should NOT expire)") #== "delivered" 1000 `timeout` tGet @BrokerMsg rh >>= \case Nothing -> return () @@ -682,11 +839,11 @@ testMsgNOTExpireOnInterval t = withSmpServerConfigOn (ATransport t) cfg' testPort $ \_ -> testSMPClient @c $ \sh -> do (sId, rId, rKey, dhShared) <- testSMPClient @c $ \rh -> createAndSecureQueue rh sPub - let dec nonce = C.cbDecrypt dhShared (C.cbNonce nonce) + let dec = decryptMsgV3 dhShared Resp "1" _ OK <- signSendRecv sh sKey ("1", sId, _SEND "hello (should NOT expire)") threadDelay 2500000 testSMPClient @c $ \rh -> do - Resp "2" _ (MSG mId _ _ msg) <- signSendRecv rh rKey ("2", rId, SUB) + Resp "2" _ (Msg mId msg) <- signSendRecv rh rKey ("2", rId, SUB) (dec mId msg, Right "hello (should NOT expire)") #== "delivered" 1000 `timeout` tGet @BrokerMsg rh >>= \case Nothing -> return () From abd694bd5af57b596f48214fe3c0d0e8982fc793 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Tue, 5 Jul 2022 21:56:14 +0100 Subject: [PATCH 124/131] parameterize protocol server by scheme (#459) --- src/Simplex/Messaging/Agent.hs | 6 +- src/Simplex/Messaging/Agent/Client.hs | 24 ++-- src/Simplex/Messaging/Agent/Env/SQLite.hs | 1 + .../Messaging/Agent/NtfSubSupervisor.hs | 8 +- src/Simplex/Messaging/Agent/Store/SQLite.hs | 18 +-- src/Simplex/Messaging/Client.hs | 8 +- .../Messaging/Notifications/Protocol.hs | 5 +- src/Simplex/Messaging/Notifications/Types.hs | 5 +- src/Simplex/Messaging/Protocol.hs | 114 +++++++++++++++--- tests/AgentTests/ConnectionRequestTests.hs | 7 +- tests/SMPAgentClient.hs | 2 +- 11 files changed, 135 insertions(+), 63 deletions(-) diff --git a/src/Simplex/Messaging/Agent.hs b/src/Simplex/Messaging/Agent.hs index 70a192f94..8cc8ec03a 100644 --- a/src/Simplex/Messaging/Agent.hs +++ b/src/Simplex/Messaging/Agent.hs @@ -12,6 +12,7 @@ {-# LANGUAGE RankNTypes #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE TupleSections #-} +{-# LANGUAGE TypeApplications #-} -- | -- Module : Simplex.Messaging.Agent @@ -100,7 +101,7 @@ import Simplex.Messaging.Notifications.Protocol (DeviceToken, NtfRegCode (NtfReg import Simplex.Messaging.Notifications.Server.Push.APNS (PNMessageData (..)) import Simplex.Messaging.Notifications.Types import Simplex.Messaging.Parsers (parse) -import Simplex.Messaging.Protocol (BrokerMsg, ErrorType (AUTH), MsgBody, MsgFlags, SMPMsgMeta) +import Simplex.Messaging.Protocol (BrokerMsg, ErrorType (AUTH), MsgBody, MsgFlags, NtfServer, SMPMsgMeta) import qualified Simplex.Messaging.Protocol as SMP import qualified Simplex.Messaging.TMap as TM import Simplex.Messaging.Util (bshow, eitherToMaybe, liftE, liftError, tryError, unlessM, whenM, ($>>=)) @@ -637,8 +638,7 @@ deleteConnection' c connId = -- | Change servers to be used for creating new queues, in Reader monad setSMPServers' :: AgentMonad m => AgentClient -> NonEmpty SMPServer -> m () -setSMPServers' c servers = do - atomically $ writeTVar (smpServers c) servers +setSMPServers' c = atomically . writeTVar (smpServers c) registerNtfToken' :: forall m. AgentMonad m => AgentClient -> DeviceToken -> NotificationsMode -> m NtfTknStatus registerNtfToken' c suppliedDeviceToken suppliedNtfMode = diff --git a/src/Simplex/Messaging/Agent/Client.hs b/src/Simplex/Messaging/Agent/Client.hs index 506bc5293..4fb5f2822 100644 --- a/src/Simplex/Messaging/Agent/Client.hs +++ b/src/Simplex/Messaging/Agent/Client.hs @@ -98,7 +98,7 @@ import Simplex.Messaging.Notifications.Client import Simplex.Messaging.Notifications.Protocol import Simplex.Messaging.Notifications.Types import Simplex.Messaging.Parsers (parse) -import Simplex.Messaging.Protocol (BrokerMsg, ErrorType, MsgFlags (..), MsgId, NotifierId, NtfPrivateSignKey, NtfPublicVerifyKey, ProtocolServer (..), QueueId, QueueIdsKeys (..), RcvMessage (..), RcvNtfPublicDhKey, SMPMsgMeta (..), SndPublicVerifyKey) +import Simplex.Messaging.Protocol (BrokerMsg, ErrorType, MsgFlags (..), MsgId, NotifierId, NtfPrivateSignKey, NtfPublicVerifyKey, NtfServer, ProtoServer, ProtocolServer (..), QueueId, QueueIdsKeys (..), RcvMessage (..), RcvNtfPublicDhKey, SMPMsgMeta (..), SndPublicVerifyKey) import qualified Simplex.Messaging.Protocol as SMP import Simplex.Messaging.TMap (TMap) import qualified Simplex.Messaging.TMap as TM @@ -194,7 +194,7 @@ agentDbPath :: AgentClient -> FilePath agentDbPath AgentClient {agentEnv = Env {store = SQLiteStore {dbFilePath}}} = dbFilePath class ProtocolServerClient msg where - getProtocolServerClient :: AgentMonad m => AgentClient -> ProtocolServer -> m (ProtocolClient msg) + getProtocolServerClient :: AgentMonad m => AgentClient -> ProtoServer msg -> m (ProtocolClient msg) clientProtocolError :: ErrorType -> AgentErrorType instance ProtocolServerClient BrokerMsg where @@ -311,7 +311,7 @@ getNtfServerClient c@AgentClient {active, ntfClients} srv = do atomically $ TM.delete srv ntfClients logInfo . decodeUtf8 $ "Agent disconnected from " <> showServer srv -getClientVar :: forall a. ProtocolServer -> TMap ProtocolServer (TMVar a) -> STM (Either (TMVar a) (TMVar a)) +getClientVar :: forall a s. ProtocolServer s -> TMap (ProtocolServer s) (TMVar a) -> STM (Either (TMVar a) (TMVar a)) getClientVar srv clients = maybe (Left <$> newClientVar) (pure . Right) =<< TM.lookup srv clients where newClientVar :: STM (TMVar a) @@ -333,8 +333,8 @@ newProtocolClient :: forall msg m. AgentMonad m => AgentClient -> - ProtocolServer -> - TMap ProtocolServer (ClientVar msg) -> + ProtoServer msg -> + TMap (ProtoServer msg) (ClientVar msg) -> m (ProtocolClient msg) -> m () -> ClientVar msg -> @@ -383,7 +383,7 @@ closeAgentClient c = liftIO $ do clear :: (AgentClient -> TMap k a) -> IO () clear sel = atomically $ writeTVar (sel c) M.empty -closeProtocolServerClients :: Int -> TMap ProtocolServer (ClientVar msg) -> IO () +closeProtocolServerClients :: Int -> TMap (ProtoServer msg) (ClientVar msg) -> IO () closeProtocolServerClients tcpTimeout cs = readTVarIO cs >>= mapM_ (forkIO . closeClient) >> atomically (writeTVar cs M.empty) where closeClient cVar = @@ -400,7 +400,7 @@ withAgentLock AgentClient {lock} = (void . atomically $ takeTMVar lock) (atomically $ putTMVar lock ()) -withClient_ :: forall a m msg. (AgentMonad m, ProtocolServerClient msg) => AgentClient -> ProtocolServer -> (ProtocolClient msg -> m a) -> m a +withClient_ :: forall a m msg. (AgentMonad m, ProtocolServerClient msg) => AgentClient -> ProtoServer msg -> (ProtocolClient msg -> m a) -> m a withClient_ c srv action = (getProtocolServerClient c srv >>= action) `catchError` logServerError where logServerError :: AgentErrorType -> m a @@ -408,17 +408,17 @@ withClient_ c srv action = (getProtocolServerClient c srv >>= action) `catchErro logServer "<--" c srv "" $ bshow e throwError e -withLogClient_ :: (AgentMonad m, ProtocolServerClient msg) => AgentClient -> ProtocolServer -> QueueId -> ByteString -> (ProtocolClient msg -> m a) -> m a +withLogClient_ :: (AgentMonad m, ProtocolServerClient msg) => AgentClient -> ProtoServer msg -> QueueId -> ByteString -> (ProtocolClient msg -> m a) -> m a withLogClient_ c srv qId cmdStr action = do logServer "-->" c srv qId cmdStr res <- withClient_ c srv action logServer "<--" c srv qId "OK" return res -withClient :: forall m msg a. (AgentMonad m, ProtocolServerClient msg) => AgentClient -> ProtocolServer -> (ProtocolClient msg -> ExceptT ProtocolClientError IO a) -> m a +withClient :: forall m msg a. (AgentMonad m, ProtocolServerClient msg) => AgentClient -> ProtoServer msg -> (ProtocolClient msg -> ExceptT ProtocolClientError IO a) -> m a withClient c srv action = withClient_ c srv $ liftClient (clientProtocolError @msg) . action -withLogClient :: forall m msg a. (AgentMonad m, ProtocolServerClient msg) => AgentClient -> ProtocolServer -> QueueId -> ByteString -> (ProtocolClient msg -> ExceptT ProtocolClientError IO a) -> m a +withLogClient :: forall m msg a. (AgentMonad m, ProtocolServerClient msg) => AgentClient -> ProtoServer msg -> QueueId -> ByteString -> (ProtocolClient msg -> ExceptT ProtocolClientError IO a) -> m a withLogClient c srv qId cmdStr action = withLogClient_ c srv qId cmdStr $ liftClient (clientProtocolError @msg) . action liftClient :: AgentMonad m => (ErrorType -> AgentErrorType) -> ExceptT ProtocolClientError IO a -> m a @@ -516,11 +516,11 @@ getSubscriptions AgentClient {subscrConns} = do m <- readTVar subscrConns pure $ M.keysSet m -logServer :: MonadIO m => ByteString -> AgentClient -> SMPServer -> QueueId -> ByteString -> m () +logServer :: MonadIO m => ByteString -> AgentClient -> ProtocolServer s -> QueueId -> ByteString -> m () logServer dir AgentClient {clientId} srv qId cmdStr = logInfo . decodeUtf8 $ B.unwords ["A", "(" <> bshow clientId <> ")", dir, showServer srv, ":", logSecret qId, cmdStr] -showServer :: SMPServer -> ByteString +showServer :: ProtocolServer s -> ByteString showServer ProtocolServer {host, port} = B.pack $ host <> if null port then "" else ':' : port diff --git a/src/Simplex/Messaging/Agent/Env/SQLite.hs b/src/Simplex/Messaging/Agent/Env/SQLite.hs index 4af3c0411..6dd481ee9 100644 --- a/src/Simplex/Messaging/Agent/Env/SQLite.hs +++ b/src/Simplex/Messaging/Agent/Env/SQLite.hs @@ -39,6 +39,7 @@ import Simplex.Messaging.Client import Simplex.Messaging.Client.Agent () import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Notifications.Types +import Simplex.Messaging.Protocol (NtfServer) import Simplex.Messaging.TMap (TMap) import qualified Simplex.Messaging.TMap as TM import Simplex.Messaging.Transport (TLS, Transport (..)) diff --git a/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs b/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs index 22ee8377f..45aee73e2 100644 --- a/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs +++ b/src/Simplex/Messaging/Agent/NtfSubSupervisor.hs @@ -139,9 +139,9 @@ processNtfSub c (connId, cmd) = do addNtfNTFWorker = addWorker ntfWorkers runNtfWorker addNtfSMPWorker = addWorker ntfSMPWorkers runNtfSMPWorker addWorker :: - (NtfSupervisor -> TMap ProtocolServer (TMVar (), Async ())) -> - (AgentClient -> ProtocolServer -> TMVar () -> m ()) -> - ProtocolServer -> + (NtfSupervisor -> TMap (ProtocolServer s) (TMVar (), Async ())) -> + (AgentClient -> ProtocolServer s -> TMVar () -> m ()) -> + ProtocolServer s -> m () addWorker wsSel runWorker srv = do ws <- asks $ wsSel . ntfSupervisor @@ -340,7 +340,7 @@ closeNtfSupervisor ns = do cancelNtfWorkers_ $ ntfWorkers ns cancelNtfWorkers_ $ ntfSMPWorkers ns -cancelNtfWorkers_ :: TMap ProtocolServer (TMVar (), Async ()) -> IO () +cancelNtfWorkers_ :: TMap (ProtocolServer s) (TMVar (), Async ()) -> IO () cancelNtfWorkers_ wsVar = do ws <- atomically $ stateTVar wsVar (,M.empty) forM_ ws $ uninterruptibleCancel . snd diff --git a/src/Simplex/Messaging/Agent/Store/SQLite.hs b/src/Simplex/Messaging/Agent/Store/SQLite.hs index 06066629f..8a75f9ddf 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite.hs @@ -127,7 +127,7 @@ import Simplex.Messaging.Encoding.String import Simplex.Messaging.Notifications.Protocol (DeviceToken (..), NtfSubscriptionId, NtfTknStatus (..), NtfTokenId, SMPQueueNtf (..)) import Simplex.Messaging.Notifications.Types import Simplex.Messaging.Parsers (blobFieldParser, fromTextField_) -import Simplex.Messaging.Protocol (MsgBody, MsgFlags, ProtocolServer (..), RcvNtfDhSecret) +import Simplex.Messaging.Protocol (MsgBody, MsgFlags, NtfServer, ProtocolServer (..), RcvNtfDhSecret, pattern NtfServer) import qualified Simplex.Messaging.Protocol as SMP import Simplex.Messaging.Util (bshow, eitherToMaybe, ($>>=), (<$$>)) import Simplex.Messaging.Version @@ -656,7 +656,7 @@ getSavedNtfToken db = do |] where ntfToken ((host, port, keyHash) :. (provider, dt, ntfTokenId, ntfPubKey, ntfPrivKey, ntfDhPubKey, ntfDhPrivKey, ntfDhSecret) :. (ntfTknStatus, ntfTknAction, ntfMode_)) = - let ntfServer = ProtocolServer {host, port, keyHash} + let ntfServer = NtfServer host port keyHash ntfDhKeys = (ntfDhPubKey, ntfDhPrivKey) ntfMode = fromMaybe NMPeriodic ntfMode_ in NtfToken {deviceToken = DeviceToken provider dt, ntfServer, ntfTokenId, ntfPubKey, ntfPrivKey, ntfDhKeys, ntfDhSecret, ntfTknStatus, ntfTknAction, ntfMode} @@ -736,7 +736,7 @@ getNtfSubscription db connId = where ntfSubscription (smpHost, smpPort, smpKeyHash, ntfHost, ntfPort, ntfKeyHash, ntfQueueId, ntfSubId, ntfSubStatus, ntfAction_, smpAction_, actionTs_) = let smpServer = SMPServer smpHost smpPort smpKeyHash - ntfServer = ProtocolServer ntfHost ntfPort ntfKeyHash + ntfServer = NtfServer ntfHost ntfPort ntfKeyHash action = case (ntfAction_, smpAction_, actionTs_) of (Just ntfAction, Nothing, Just actionTs) -> Just (NtfSubNTFAction ntfAction, actionTs) (Nothing, Just smpAction, Just actionTs) -> Just (NtfSubSMPAction smpAction, actionTs) @@ -745,7 +745,7 @@ getNtfSubscription db connId = createNtfSubscription :: DB.Connection -> NtfSubscription -> NtfSubAction -> NtfActionTs -> IO () createNtfSubscription db ntfSubscription action actionTs = do - let NtfSubscription {connId, smpServer = (SMPServer host port _), ntfQueueId, ntfServer = (SMPServer ntfHost ntfPort _), ntfSubId, ntfSubStatus} = ntfSubscription + let NtfSubscription {connId, smpServer = (SMPServer host port _), ntfQueueId, ntfServer = (NtfServer ntfHost ntfPort _), ntfSubId, ntfSubStatus} = ntfSubscription DB.execute db [sql| @@ -761,7 +761,7 @@ createNtfSubscription db ntfSubscription action actionTs = do (ntfSubAction, ntfSubSMPAction) = ntfSubAndSMPAction action supervisorUpdateNtfSubscription :: DB.Connection -> NtfSubscription -> NtfSubAction -> NtfActionTs -> IO () -supervisorUpdateNtfSubscription db NtfSubscription {connId, ntfQueueId, ntfServer = (ProtocolServer ntfHost ntfPort _), ntfSubId, ntfSubStatus} action actionTs = do +supervisorUpdateNtfSubscription db NtfSubscription {connId, ntfQueueId, ntfServer = (NtfServer ntfHost ntfPort _), ntfSubId, ntfSubStatus} action actionTs = do updatedAt <- getCurrentTime DB.execute db @@ -789,7 +789,7 @@ supervisorUpdateNtfSubAction db connId action actionTs = do (ntfSubAction, ntfSubSMPAction) = ntfSubAndSMPAction action updateNtfSubscription :: DB.Connection -> NtfSubscription -> NtfSubAction -> NtfActionTs -> IO () -updateNtfSubscription db NtfSubscription {connId, ntfQueueId, ntfServer = (ProtocolServer ntfHost ntfPort _), ntfSubId, ntfSubStatus} action actionTs = do +updateNtfSubscription db NtfSubscription {connId, ntfQueueId, ntfServer = (NtfServer ntfHost ntfPort _), ntfSubId, ntfSubStatus} action actionTs = do r <- maybeFirstRow fromOnly $ DB.query db "SELECT updated_by_supervisor FROM ntf_subscriptions WHERE conn_id = ?" (Only connId) forM_ r $ \updatedBySupervisor -> do updatedAt <- getCurrentTime @@ -848,7 +848,7 @@ deleteNtfSubscription db connId = do else DB.execute db "DELETE FROM ntf_subscriptions WHERE conn_id = ?" (Only connId) getNextNtfSubNTFAction :: DB.Connection -> NtfServer -> IO (Maybe (NtfSubscription, NtfSubNTFAction, NtfActionTs)) -getNextNtfSubNTFAction db ntfServer@(ProtocolServer ntfHost ntfPort _) = do +getNextNtfSubNTFAction db ntfServer@(NtfServer ntfHost ntfPort _) = do maybeFirstRow ntfSubAction getNtfSubAction_ $>>= \a@(NtfSubscription {connId}, _, _) -> do DB.execute db "UPDATE ntf_subscriptions SET updated_by_supervisor = ? WHERE conn_id = ?" (False, connId) pure $ Just a @@ -891,7 +891,7 @@ getNextNtfSubSMPAction db smpServer@(SMPServer smpHost smpPort _) = do |] (smpHost, smpPort) ntfSubAction (connId, ntfHost, ntfPort, ntfKeyHash, ntfQueueId, ntfSubId, ntfSubStatus, actionTs, action) = - let ntfServer = ProtocolServer ntfHost ntfPort ntfKeyHash + let ntfServer = NtfServer ntfHost ntfPort ntfKeyHash ntfSubscription = NtfSubscription {connId, smpServer, ntfQueueId, ntfServer, ntfSubId, ntfSubStatus} in (ntfSubscription, action, actionTs) @@ -911,7 +911,7 @@ getActiveNtfToken db = (Only NTActive) where ntfToken ((host, port, keyHash) :. (provider, dt, ntfTokenId, ntfPubKey, ntfPrivKey, ntfDhPubKey, ntfDhPrivKey, ntfDhSecret) :. (ntfTknStatus, ntfTknAction, ntfMode_)) = - let ntfServer = ProtocolServer {host, port, keyHash} + let ntfServer = NtfServer host port keyHash ntfDhKeys = (ntfDhPubKey, ntfDhPrivKey) ntfMode = fromMaybe NMPeriodic ntfMode_ in NtfToken {deviceToken = DeviceToken provider dt, ntfServer, ntfTokenId, ntfPubKey, ntfPrivKey, ntfDhKeys, ntfDhSecret, ntfTknStatus, ntfTknAction, ntfMode} diff --git a/src/Simplex/Messaging/Client.hs b/src/Simplex/Messaging/Client.hs index c39f5b1e2..a04be51ba 100644 --- a/src/Simplex/Messaging/Client.hs +++ b/src/Simplex/Messaging/Client.hs @@ -83,7 +83,7 @@ data ProtocolClient msg = ProtocolClient connected :: TVar Bool, sessionId :: SessionId, thVersion :: Version, - protocolServer :: ProtocolServer, + protocolServer :: ProtoServer msg, tcpTimeout :: Int, clientCorrId :: TVar Natural, sentCommands :: TMap CorrId (Request msg), @@ -95,7 +95,7 @@ data ProtocolClient msg = ProtocolClient type SMPClient = ProtocolClient SMP.BrokerMsg -- | Type synonym for transmission from some SPM server queue. -type ServerTransmission msg = (ProtocolServer, Version, SessionId, QueueId, msg) +type ServerTransmission msg = (ProtoServer msg, Version, SessionId, QueueId, msg) -- | protocol client configuration. data ProtocolClientConfig = ProtocolClientConfig @@ -137,7 +137,7 @@ type Response msg = Either ProtocolClientError msg -- -- A single queue can be used for multiple 'SMPClient' instances, -- as 'SMPServerTransmission' includes server information. -getProtocolClient :: forall msg. Protocol msg => ProtocolServer -> ProtocolClientConfig -> Maybe (TBQueue (ServerTransmission msg)) -> IO () -> IO (Either ProtocolClientError (ProtocolClient msg)) +getProtocolClient :: forall msg. Protocol msg => ProtoServer msg -> ProtocolClientConfig -> Maybe (TBQueue (ServerTransmission msg)) -> IO () -> IO (Either ProtocolClientError (ProtocolClient msg)) getProtocolClient protocolServer cfg@ProtocolClientConfig {qSize, tcpTimeout, tcpKeepAlive, smpPing, smpServerVRange} msgQ disconnected = (atomically mkProtocolClient >>= runClient useTransport) `catch` \(e :: IOException) -> pure . Left $ PCEIOError e @@ -378,7 +378,7 @@ sendSMPCommand :: PartyI p => SMPClient -> Maybe C.APrivateSignKey -> QueueId -> sendSMPCommand c pKey qId cmd = sendProtocolCommand c pKey qId (Cmd sParty cmd) -- | Send Protocol command -sendProtocolCommand :: forall msg. ProtocolEncoding (ProtocolCommand msg) => ProtocolClient msg -> Maybe C.APrivateSignKey -> QueueId -> ProtocolCommand msg -> ExceptT ProtocolClientError IO msg +sendProtocolCommand :: forall msg. ProtocolEncoding (ProtoCommand msg) => ProtocolClient msg -> Maybe C.APrivateSignKey -> QueueId -> ProtoCommand msg -> ExceptT ProtocolClientError IO msg sendProtocolCommand ProtocolClient {sndQ, sentCommands, clientCorrId, sessionId, thVersion, tcpTimeout} pKey qId cmd = do corrId <- lift_ getNextCorrId t <- signTransmission $ encodeTransmission thVersion sessionId (corrId, qId, cmd) diff --git a/src/Simplex/Messaging/Notifications/Protocol.hs b/src/Simplex/Messaging/Notifications/Protocol.hs index 9b8e55184..28421de66 100644 --- a/src/Simplex/Messaging/Notifications/Protocol.hs +++ b/src/Simplex/Messaging/Notifications/Protocol.hs @@ -146,7 +146,8 @@ instance Encoding ANewNtfEntity where _ -> fail "bad ANewNtfEntity" instance Protocol NtfResponse where - type ProtocolCommand NtfResponse = NtfCmd + type ProtoCommand NtfResponse = NtfCmd + type ProtoType NtfResponse = 'PNTF protocolClientHandshake = ntfClientHandshake protocolPing = NtfCmd SSubscription PING protocolError = \case @@ -323,7 +324,7 @@ instance ProtocolEncoding NtfResponse where | otherwise = Left $ CMD HAS_AUTH data SMPQueueNtf = SMPQueueNtf - { smpServer :: ProtocolServer, + { smpServer :: SMPServer, notifierId :: NotifierId } deriving (Eq, Ord, Show) diff --git a/src/Simplex/Messaging/Notifications/Types.hs b/src/Simplex/Messaging/Notifications/Types.hs index d679d00f2..3b6f7782d 100644 --- a/src/Simplex/Messaging/Notifications/Types.hs +++ b/src/Simplex/Messaging/Notifications/Types.hs @@ -3,6 +3,7 @@ {-# LANGUAGE LambdaCase #-} {-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE PatternSynonyms #-} module Simplex.Messaging.Notifications.Types where @@ -16,9 +17,7 @@ import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Encoding import Simplex.Messaging.Notifications.Protocol import Simplex.Messaging.Parsers (blobFieldDecoder, fromTextField_) -import Simplex.Messaging.Protocol (NotifierId, ProtocolServer, SMPServer) - -type NtfServer = ProtocolServer +import Simplex.Messaging.Protocol (NotifierId, NtfServer, SMPServer) data NtfTknAction = NTARegister diff --git a/src/Simplex/Messaging/Protocol.hs b/src/Simplex/Messaging/Protocol.hs index 52dc7d0b3..42713ad2d 100644 --- a/src/Simplex/Messaging/Protocol.hs +++ b/src/Simplex/Messaging/Protocol.hs @@ -63,9 +63,13 @@ module Simplex.Messaging.Protocol ClientMessage (..), PrivHeader (..), Protocol (..), + ProtocolType (..), ProtocolServer (..), + ProtoServer, SMPServer, pattern SMPServer, + NtfServer, + pattern NtfServer, SrvLoc (..), CorrId (..), QueueId, @@ -546,41 +550,109 @@ instance Encoding ClientMessage where smpEncode (ClientMessage h msg) = smpEncode h <> msg smpP = ClientMessage <$> smpP <*> A.takeByteString -type SMPServer = ProtocolServer +type SMPServer = ProtocolServer 'PSMP -pattern SMPServer :: HostName -> ServiceName -> C.KeyHash -> ProtocolServer -pattern SMPServer host port keyHash = ProtocolServer host port keyHash +pattern SMPServer :: HostName -> ServiceName -> C.KeyHash -> ProtocolServer 'PSMP +pattern SMPServer host port keyHash = ProtocolServer SPSMP host port keyHash {-# COMPLETE SMPServer #-} --- | SMP server location and transport key digest (hash). -data ProtocolServer = ProtocolServer - { host :: HostName, +type NtfServer = ProtocolServer 'PNTF + +pattern NtfServer :: HostName -> ServiceName -> C.KeyHash -> ProtocolServer 'PNTF +pattern NtfServer host port keyHash = ProtocolServer SPNTF host port keyHash + +{-# COMPLETE NtfServer #-} + +data ProtocolType = PSMP | PNTF + deriving (Eq, Ord, Show) + +instance StrEncoding ProtocolType where + strEncode = \case + PSMP -> "smp" + PNTF -> "ntf" + strP = + A.takeTill (== ':') >>= \case + "smp" -> pure PSMP + "ntf" -> pure PNTF + _ -> fail "bad ProtocolType" + +data SProtocolType (p :: ProtocolType) where + SPSMP :: SProtocolType 'PSMP + SPNTF :: SProtocolType 'PNTF + +deriving instance Eq (SProtocolType p) + +deriving instance Ord (SProtocolType p) + +deriving instance Show (SProtocolType p) + +data AProtocolType = forall p. ProtocolTypeI p => AProtocolType (SProtocolType p) + +instance TestEquality SProtocolType where + testEquality SPSMP SPSMP = Just Refl + testEquality SPNTF SPNTF = Just Refl + testEquality _ _ = Nothing + +protocolType :: SProtocolType p -> ProtocolType +protocolType = \case + SPSMP -> PSMP + SPNTF -> PNTF + +aProtocolType :: ProtocolType -> AProtocolType +aProtocolType = \case + PSMP -> AProtocolType SPSMP + PNTF -> AProtocolType SPNTF + +instance ProtocolTypeI p => StrEncoding (SProtocolType p) where + strEncode = strEncode . protocolType + strP = (\(AProtocolType p) -> checkProtocolType p) <$?> strP + +instance StrEncoding AProtocolType where + strEncode (AProtocolType p) = strEncode p + strP = aProtocolType <$> strP + +checkProtocolType :: forall t p p'. (ProtocolTypeI p, ProtocolTypeI p') => t p' -> Either String (t p) +checkProtocolType p = case testEquality (protocolTypeI @p) (protocolTypeI @p') of + Just Refl -> Right p + Nothing -> Left "bad ProtocolType" + +class ProtocolTypeI (p :: ProtocolType) where + protocolTypeI :: SProtocolType p + +instance ProtocolTypeI 'PSMP where protocolTypeI = SPSMP + +instance ProtocolTypeI 'PNTF where protocolTypeI = SPNTF + +-- | server location and transport key digest (hash). +data ProtocolServer p = ProtocolServer + { scheme :: SProtocolType p, + host :: HostName, port :: ServiceName, keyHash :: C.KeyHash } deriving (Eq, Ord, Show) -instance IsString ProtocolServer where +instance ProtocolTypeI p => IsString (ProtocolServer p) where fromString = parseString strDecode -instance Encoding ProtocolServer where +instance ProtocolTypeI p => Encoding (ProtocolServer p) where smpEncode ProtocolServer {host, port, keyHash} = smpEncode (host, port, keyHash) smpP = do (host, port, keyHash) <- smpP - pure ProtocolServer {host, port, keyHash} + pure ProtocolServer {scheme = protocolTypeI @p, host, port, keyHash} -instance StrEncoding ProtocolServer where - strEncode ProtocolServer {host, port, keyHash} = - "smp://" <> strEncode keyHash <> "@" <> strEncode (SrvLoc host port) +instance ProtocolTypeI p => StrEncoding (ProtocolServer p) where + strEncode ProtocolServer {scheme, host, port, keyHash} = + strEncode scheme <> "://" <> strEncode keyHash <> "@" <> strEncode (SrvLoc host port) strP = do - _ <- "smp://" + scheme <- strP <* "://" keyHash <- strP <* A.char '@' SrvLoc host port <- strP - pure ProtocolServer {host, port, keyHash} + pure ProtocolServer {scheme, host, port, keyHash} -instance ToJSON ProtocolServer where +instance ProtocolTypeI p => ToJSON (ProtocolServer p) where toJSON = strToJSON toEncoding = strToJEncoding @@ -727,14 +799,18 @@ transmissionP = do command <- A.takeByteString pure RawTransmission {signature, signed, sessId, corrId, entityId, command} -class (ProtocolEncoding msg, ProtocolEncoding (ProtocolCommand msg), Show msg) => Protocol msg where - type ProtocolCommand msg = cmd | cmd -> msg +class (ProtocolEncoding msg, ProtocolEncoding (ProtoCommand msg), Show msg) => Protocol msg where + type ProtoCommand msg = cmd | cmd -> msg + type ProtoType msg = (sch :: ProtocolType) | sch -> msg protocolClientHandshake :: forall c. Transport c => c -> C.KeyHash -> VersionRange -> ExceptT TransportError IO (THandle c) - protocolPing :: ProtocolCommand msg + protocolPing :: ProtoCommand msg protocolError :: msg -> Maybe ErrorType +type ProtoServer msg = ProtocolServer (ProtoType msg) + instance Protocol BrokerMsg where - type ProtocolCommand BrokerMsg = Cmd + type ProtoCommand BrokerMsg = Cmd + type ProtoType BrokerMsg = 'PSMP protocolClientHandshake = smpClientHandshake protocolPing = Cmd SSender PING protocolError = \case diff --git a/tests/AgentTests/ConnectionRequestTests.hs b/tests/AgentTests/ConnectionRequestTests.hs index af4f749bc..cf5bb0a20 100644 --- a/tests/AgentTests/ConnectionRequestTests.hs +++ b/tests/AgentTests/ConnectionRequestTests.hs @@ -19,12 +19,7 @@ uri :: String uri = "smp.simplex.im" srv :: SMPServer -srv = - ProtocolServer - { host = "smp.simplex.im", - port = "5223", - keyHash = C.KeyHash "\215m\248\251" - } +srv = SMPServer "smp.simplex.im" "5223" (C.KeyHash "\215m\248\251") queue :: SMPQueueUri queue = diff --git a/tests/SMPAgentClient.hs b/tests/SMPAgentClient.hs index 08cc3ea37..9a5bbeee7 100644 --- a/tests/SMPAgentClient.hs +++ b/tests/SMPAgentClient.hs @@ -162,7 +162,7 @@ initAgentServers :: InitialAgentServers initAgentServers = InitialAgentServers { smp = L.fromList [testSMPServer], - ntf = ["smp://LcJUMfVhwD8yxjAiSaDzzGF3-kLG4Uh0Fl_ZIjrRwjI=@localhost:6001"] + ntf = ["ntf://LcJUMfVhwD8yxjAiSaDzzGF3-kLG4Uh0Fl_ZIjrRwjI=@localhost:6001"] } agentCfg :: AgentConfig From 4339218c57f81d5713016e20a4bc55d2d38590c8 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Tue, 5 Jul 2022 22:30:44 +0100 Subject: [PATCH 125/131] v3.0.0-rc.0 --- apps/ntf-server/Main.hs | 2 +- src/Simplex/Messaging/Transport.hs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/ntf-server/Main.hs b/apps/ntf-server/Main.hs index f56797823..abc4a4d5f 100644 --- a/apps/ntf-server/Main.hs +++ b/apps/ntf-server/Main.hs @@ -42,7 +42,7 @@ ntfServerCLIConfig = fingerprintFile = combine cfgPath "fingerprint", defaultServerPort = "443", executableName = "ntf-server", - serverVersion = "SMP notifications server v1.0.0-beta.4", + serverVersion = "SMP notifications server v1.0.0-rc.0", mkIniFile = \enableStoreLog defaultServerPort -> "[STORE_LOG]\n\ \# The server uses STM memory for persistence,\n\ diff --git a/src/Simplex/Messaging/Transport.hs b/src/Simplex/Messaging/Transport.hs index 12d8df752..3039554c8 100644 --- a/src/Simplex/Messaging/Transport.hs +++ b/src/Simplex/Messaging/Transport.hs @@ -96,7 +96,7 @@ supportedSMPServerVRange :: VersionRange supportedSMPServerVRange = mkVersionRange 1 3 simplexMQVersion :: String -simplexMQVersion = "3.0.0-beta.4" +simplexMQVersion = "3.0.0-rc.0" -- * Transport connection class From cc798145d2e26be8986747dfe3e5524b45699fab Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Wed, 6 Jul 2022 13:58:58 +0100 Subject: [PATCH 126/131] suspend agent without delay (#463) * suspend agent without delay * suspend/activate in separate atomically --- src/Simplex/Messaging/Agent.hs | 17 +++++++++-------- src/Simplex/Messaging/Agent/Client.hs | 4 ++++ 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/Simplex/Messaging/Agent.hs b/src/Simplex/Messaging/Agent.hs index 8cc8ec03a..ce0fdcd66 100644 --- a/src/Simplex/Messaging/Agent.hs +++ b/src/Simplex/Messaging/Agent.hs @@ -805,17 +805,18 @@ setNtfServers' :: AgentMonad m => AgentClient -> [NtfServer] -> m () setNtfServers' c = atomically . writeTVar (ntfServers c) activateAgent' :: AgentMonad m => AgentClient -> m () -activateAgent' c = atomically $ do - writeTVar (agentState c) ASActive - activate databaseOp - activate sndNetworkOp - activate msgDeliveryOp - activate rcvNetworkOp - activate ntfNetworkOp +activateAgent' c = do + atomically $ writeTVar (agentState c) ASActive + mapM_ activate $ reverse agentOperations where - activate opSel = modifyTVar' (opSel c) $ \s -> s {opSuspended = False} + activate opSel = atomically $ modifyTVar' (opSel c) $ \s -> s {opSuspended = False} suspendAgent' :: AgentMonad m => AgentClient -> Int -> m () +suspendAgent' c 0 = do + atomically $ writeTVar (agentState c) ASSuspended + mapM_ suspend agentOperations + where + suspend opSel = atomically $ modifyTVar' (opSel c) $ \s -> s {opSuspended = True} suspendAgent' c@AgentClient {agentState = as} maxDelay = do state <- atomically $ do diff --git a/src/Simplex/Messaging/Agent/Client.hs b/src/Simplex/Messaging/Agent/Client.hs index 4fb5f2822..fc3a6d43c 100644 --- a/src/Simplex/Messaging/Agent/Client.hs +++ b/src/Simplex/Messaging/Agent/Client.hs @@ -54,6 +54,7 @@ module Simplex.Messaging.Agent.Client AgentOperation (..), AgentOpState (..), AgentState (..), + agentOperations, agentOperationBracket, beginAgentOperation, endAgentOperation, @@ -155,6 +156,9 @@ agentOpSel = \case AOSndNetwork -> sndNetworkOp AODatabase -> databaseOp +agentOperations :: [AgentClient -> TVar AgentOpState] +agentOperations = [ntfNetworkOp, rcvNetworkOp, msgDeliveryOp, sndNetworkOp, databaseOp] + data AgentOpState = AgentOpState {opSuspended :: Bool, opsInProgress :: Int} data AgentState = ASActive | ASSuspending | ASSuspended From 991548b64de06af93dfedb00c6c8fe9cd96ebe1a Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Wed, 6 Jul 2022 18:20:49 +0100 Subject: [PATCH 127/131] ntf server: do not resubscribe to error/ended subscriptions on restart (#464) --- apps/ntf-server/Main.hs | 2 +- src/Simplex/Messaging/Notifications/Protocol.hs | 10 ++++++++++ src/Simplex/Messaging/Notifications/Server.hs | 7 ++++--- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/apps/ntf-server/Main.hs b/apps/ntf-server/Main.hs index abc4a4d5f..1c61bc247 100644 --- a/apps/ntf-server/Main.hs +++ b/apps/ntf-server/Main.hs @@ -69,7 +69,7 @@ ntfServerCLIConfig = apnsConfig = defaultAPNSPushClientConfig, inactiveClientExpiration = Nothing, storeLogFile, - resubscribeDelay = 100000, -- 100ms + resubscribeDelay = 50000, -- 50ms caCertificateFile = caCrtFile, privateKeyFile = serverKeyFile, certificateFile = serverCrtFile diff --git a/src/Simplex/Messaging/Notifications/Protocol.hs b/src/Simplex/Messaging/Notifications/Protocol.hs index 28421de66..d7f568509 100644 --- a/src/Simplex/Messaging/Notifications/Protocol.hs +++ b/src/Simplex/Messaging/Notifications/Protocol.hs @@ -412,6 +412,16 @@ data NtfSubStatus NSErr ByteString deriving (Eq, Show) +ntfShouldSubscribe :: NtfSubStatus -> Bool +ntfShouldSubscribe = \case + NSNew -> True + NSPending -> True + NSActive -> True + NSInactive -> True + NSEnd -> False + NSAuth -> False + NSErr _ -> False + instance Encoding NtfSubStatus where smpEncode = \case NSNew -> "NEW" diff --git a/src/Simplex/Messaging/Notifications/Server.hs b/src/Simplex/Messaging/Notifications/Server.hs index 0dca24d99..70c5832b3 100644 --- a/src/Simplex/Messaging/Notifications/Server.hs +++ b/src/Simplex/Messaging/Notifications/Server.hs @@ -80,9 +80,10 @@ ntfServer NtfServerConfig {transports} started = do resubscribe :: (MonadUnliftIO m, MonadReader NtfEnv m) => NtfSubscriber -> Map NtfSubscriptionId NtfSubData -> m () resubscribe NtfSubscriber {newSubQ} subs = do d <- asks $ resubscribeDelay . config - forM_ subs $ \sub -> do - atomically $ writeTBQueue newSubQ $ NtfSub sub - threadDelay d + forM_ subs $ \sub@NtfSubData {} -> + whenM (ntfShouldSubscribe <$> readTVarIO (subStatus sub)) $ do + atomically $ writeTBQueue newSubQ $ NtfSub sub + threadDelay d liftIO $ logInfo "SMP connections resubscribed" ntfSubscriber :: forall m. (MonadUnliftIO m, MonadReader NtfEnv m) => NtfSubscriber -> m () From 7a238812b76d2bae0ceb453dbf41e140dd65ca0e Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Fri, 8 Jul 2022 14:46:01 +0100 Subject: [PATCH 128/131] ntf server: fix resubscribing to SMP server after it was restarted, test (#465) --- src/Simplex/Messaging/Agent.hs | 10 ++++ src/Simplex/Messaging/Client/Agent.hs | 6 +-- src/Simplex/Messaging/Notifications/Server.hs | 11 ++-- .../Notifications/Server/Push/APNS.hs | 2 + tests/AgentTests/NotificationTests.hs | 53 +++++++++++++++++-- 5 files changed, 71 insertions(+), 11 deletions(-) diff --git a/src/Simplex/Messaging/Agent.hs b/src/Simplex/Messaging/Agent.hs index ce0fdcd66..cd7269474 100644 --- a/src/Simplex/Messaging/Agent.hs +++ b/src/Simplex/Messaging/Agent.hs @@ -59,6 +59,7 @@ module Simplex.Messaging.Agent checkNtfToken, deleteNtfToken, getNtfToken, + getNtfTokenData, deleteNtfSub, activateAgent, suspendAgent, @@ -207,6 +208,9 @@ deleteNtfToken c = withAgentEnv c . deleteNtfToken' c getNtfToken :: AgentErrorMonad m => AgentClient -> m (DeviceToken, NtfTknStatus, NotificationsMode) getNtfToken c = withAgentEnv c $ getNtfToken' c +getNtfTokenData :: AgentErrorMonad m => AgentClient -> m NtfToken +getNtfTokenData c = withAgentEnv c $ getNtfTokenData' c + -- | Delete notification subscription for connection deleteNtfSub :: AgentErrorMonad m => AgentClient -> ConnId -> m () deleteNtfSub c = withAgentEnv c . deleteNtfSub' c @@ -745,6 +749,12 @@ getNtfToken' c = Just NtfToken {deviceToken, ntfTknStatus, ntfMode} -> pure (deviceToken, ntfTknStatus, ntfMode) _ -> throwError $ CMD PROHIBITED +getNtfTokenData' :: AgentMonad m => AgentClient -> m NtfToken +getNtfTokenData' c = + withStore' c getSavedNtfToken >>= \case + Just tkn -> pure tkn + _ -> throwError $ CMD PROHIBITED + -- | Delete notification subscription for connection, in Reader monad deleteNtfSub' :: AgentMonad m => AgentClient -> ConnId -> m () deleteNtfSub' _c connId = do diff --git a/src/Simplex/Messaging/Client/Agent.hs b/src/Simplex/Messaging/Client/Agent.hs index bdb118e6f..664959eb2 100644 --- a/src/Simplex/Messaging/Client/Agent.hs +++ b/src/Simplex/Messaging/Client/Agent.hs @@ -30,7 +30,7 @@ import Simplex.Messaging.Protocol (BrokerMsg, ProtocolServer (..), QueueId, SMPS import Simplex.Messaging.TMap (TMap) import qualified Simplex.Messaging.TMap as TM import Simplex.Messaging.Transport -import Simplex.Messaging.Util (catchAll_, tryE, whenM, ($>>=)) +import Simplex.Messaging.Util (catchAll_, tryE, unlessM, ($>>=)) import System.Timeout (timeout) import UnliftIO (async, forConcurrently_) import UnliftIO.Exception (Exception) @@ -47,7 +47,7 @@ data SMPClientAgentEvent | CASubError SMPServer SMPSub ProtocolClientError data SMPSubParty = SPRecipient | SPNotifier - deriving (Eq, Ord) + deriving (Eq, Ord, Show) type SMPSub = (SMPSubParty, QueueId) @@ -203,7 +203,7 @@ getSMPServerClient' ca@SMPClientAgent {agentCfg, smpClients, msgQ} srv = notify $ CAReconnected srv cs <- atomically $ mapM readTVar =<< TM.lookup srv (pendingSrvSubs ca) forConcurrently_ (maybe [] M.assocs cs) $ \sub@(s, _) -> - whenM (atomically $ hasSub (srvSubs ca) srv s) $ + unlessM (atomically $ hasSub (srvSubs ca) srv s) $ subscribe_ smp sub `catchE` handleError s where subscribe_ :: SMPClient -> (SMPSub, C.APrivateSignKey) -> ExceptT ProtocolClientError IO () diff --git a/src/Simplex/Messaging/Notifications/Server.hs b/src/Simplex/Messaging/Notifications/Server.hs index 70c5832b3..254e8707c 100644 --- a/src/Simplex/Messaging/Notifications/Server.hs +++ b/src/Simplex/Messaging/Notifications/Server.hs @@ -31,7 +31,7 @@ import Simplex.Messaging.Notifications.Server.Push.APNS (PNMessageData (..), Pus import Simplex.Messaging.Notifications.Server.Store import Simplex.Messaging.Notifications.Server.StoreLog import Simplex.Messaging.Notifications.Transport -import Simplex.Messaging.Protocol (ErrorType (..), SMPServer, SignedTransmission, Transmission, encodeTransmission, tGet, tPut) +import Simplex.Messaging.Protocol (ErrorType (..), ProtocolServer (host), SMPServer, SignedTransmission, Transmission, encodeTransmission, tGet, tPut) import qualified Simplex.Messaging.Protocol as SMP import Simplex.Messaging.Server import qualified Simplex.Messaging.TMap as TM @@ -147,16 +147,19 @@ ntfSubscriber NtfSubscriber {smpSubscribers, newSubQ, smpAgent = ca@SMPClientAge forever $ atomically (readTBQueue agentQ) >>= \case CAConnected _ -> pure () - CADisconnected srv subs -> + CADisconnected srv subs -> do + logInfo . T.pack $ "SMP server disconnected " <> host srv <> " (" <> show (length subs) <> ") subscriptions" forM_ subs $ \(_, ntfId) -> do let smpQueue = SMPQueueNtf srv ntfId updateSubStatus smpQueue NSInactive - CAReconnected _ -> pure () + CAReconnected srv -> + logInfo $ "SMP server reconnected " <> T.pack (host srv) CAResubscribed srv sub -> do let ntfId = snd sub smpQueue = SMPQueueNtf srv ntfId updateSubStatus smpQueue NSActive - CASubError srv (_, ntfId) err -> + CASubError srv (_, ntfId) err -> do + logError . T.pack $ "SMP subscription error on server " <> host srv <> ": " <> show err handleSubError (SMPQueueNtf srv ntfId) err handleSubError :: SMPQueueNtf -> ProtocolClientError -> m () diff --git a/src/Simplex/Messaging/Notifications/Server/Push/APNS.hs b/src/Simplex/Messaging/Notifications/Server/Push/APNS.hs index 13d22b594..8f89594e0 100644 --- a/src/Simplex/Messaging/Notifications/Server/Push/APNS.hs +++ b/src/Simplex/Messaging/Notifications/Server/Push/APNS.hs @@ -99,6 +99,7 @@ data PushNotification | PNMessage PNMessageData | PNAlert Text | PNCheckMessages + deriving (Show) data PNMessageData = PNMessageData { smpQueue :: SMPQueueNtf, @@ -106,6 +107,7 @@ data PNMessageData = PNMessageData nmsgNonce :: C.CbNonce, encNMsgMeta :: EncNMsgMeta } + deriving (Show) instance StrEncoding PNMessageData where strEncode PNMessageData {smpQueue, ntfTs, nmsgNonce, encNMsgMeta} = diff --git a/tests/AgentTests/NotificationTests.hs b/tests/AgentTests/NotificationTests.hs index c7e8c9419..10e0ce7a5 100644 --- a/tests/AgentTests/NotificationTests.hs +++ b/tests/AgentTests/NotificationTests.hs @@ -14,19 +14,22 @@ import Control.Concurrent (killThread, threadDelay) import Control.Monad.Except import qualified Data.Aeson as J import qualified Data.Aeson.Types as JT -import Data.Bifunctor (bimap) +import Data.Bifunctor (bimap, first) import qualified Data.ByteString.Base64.URL as U import Data.ByteString.Char8 (ByteString) import Data.Text.Encoding (encodeUtf8) import NtfClient import SMPAgentClient (agentCfg, initAgentServers, testDB, testDB2) -import SMPClient (withSmpServer) +import SMPClient (testPort, withSmpServer, withSmpServerStoreLogOn) import Simplex.Messaging.Agent +import Simplex.Messaging.Agent.Client (AgentClient) import Simplex.Messaging.Agent.Env.SQLite (AgentConfig (..)) import Simplex.Messaging.Agent.Protocol import qualified Simplex.Messaging.Crypto as C +import Simplex.Messaging.Encoding.String import Simplex.Messaging.Notifications.Protocol import Simplex.Messaging.Notifications.Server.Push.APNS +import Simplex.Messaging.Notifications.Types (NtfToken (..)) import Simplex.Messaging.Protocol (ErrorType (AUTH), MsgFlags (MsgFlags), SMPMsgMeta (..)) import qualified Simplex.Messaging.Protocol as SMP import Simplex.Messaging.Transport (ATransport) @@ -73,11 +76,15 @@ notificationTests t = withSmpServer t $ withAPNSMockServer $ \apns -> withNtfServer t $ testChangeToken apns - describe "Notifications server store log" $ do + describe "Notifications server store log" $ it "should save and restore tokens and subscriptions" $ \_ -> withSmpServer t $ withAPNSMockServer $ \apns -> testNotificationsStoreLog t apns + describe "Notifications after SMP server restart" $ + it "should resume subscriptions after SMP server is restarted" $ \_ -> + withAPNSMockServer $ \apns -> + withNtfServer t $ testNotificationsSMPRestart t apns testNotificationToken :: APNSMockServer -> IO () testNotificationToken APNSMockServer {apnsQ} = do @@ -430,8 +437,8 @@ testNotificationsStoreLog t APNSMockServer {apnsQ} = do alice <- getSMPAgentClient agentCfg initAgentServers bob <- getSMPAgentClient agentCfg {dbFile = testDB2} initAgentServers Right (aliceId, bobId) <- withNtfServerStoreLog t $ \threadId -> runExceptT $ do - _ <- registerTestToken alice "abcd" NMInstant apnsQ (aliceId, bobId) <- makeConnection alice bob + _ <- registerTestToken alice "abcd" NMInstant apnsQ liftIO $ threadDelay 250000 4 <- sendMessage bob aliceId (SMP.MsgFlags True) "hello" get bob ##> ("", aliceId, SENT 4) @@ -452,6 +459,37 @@ testNotificationsStoreLog t APNSMockServer {apnsQ} = do liftIO $ killThread threadId pure () +testNotificationsSMPRestart :: ATransport -> APNSMockServer -> IO () +testNotificationsSMPRestart t APNSMockServer {apnsQ} = do + alice <- getSMPAgentClient agentCfg initAgentServers + bob <- getSMPAgentClient agentCfg {dbFile = testDB2} initAgentServers + Right (aliceId, bobId) <- withSmpServerStoreLogOn t testPort $ \threadId -> runExceptT $ do + (aliceId, bobId) <- makeConnection alice bob + _ <- registerTestToken alice "abcd" NMInstant apnsQ + liftIO $ threadDelay 250000 + 4 <- sendMessage bob aliceId (SMP.MsgFlags True) "hello" + get bob ##> ("", aliceId, SENT 4) + void $ messageNotification apnsQ + get alice =##> \case ("", c, Msg "hello") -> c == bobId; _ -> False + ackMessage alice bobId 4 + liftIO $ killThread threadId + pure (aliceId, bobId) + + Right () <- runExceptT $ do + get alice =##> \case ("", "", DOWN _ [c]) -> c == bobId; _ -> False + get bob =##> \case ("", "", DOWN _ [c]) -> c == aliceId; _ -> False + + Right () <- withSmpServerStoreLogOn t testPort $ \threadId -> runExceptT $ do + get alice =##> \case ("", "", UP _ [c]) -> c == bobId; _ -> False + get bob =##> \case ("", "", UP _ [c]) -> c == aliceId; _ -> False + liftIO $ threadDelay 1000000 + 5 <- sendMessage bob aliceId (SMP.MsgFlags True) "hello again" + get bob ##> ("", aliceId, SENT 5) + _ <- messageNotificationData alice apnsQ + get alice =##> \case ("", c, Msg "hello again") -> c == bobId; _ -> False + liftIO $ killThread threadId + pure () + messageNotification :: TBQueue APNSMockRequest -> ExceptT AgentErrorType IO (C.CbNonce, ByteString) messageNotification apnsQ = do 1000000 `timeout` atomically (readTBQueue apnsQ) >>= \case @@ -463,6 +501,13 @@ messageNotification apnsQ = do pure (nonce, message) _ -> error "bad notification" +messageNotificationData :: AgentClient -> TBQueue APNSMockRequest -> ExceptT AgentErrorType IO PNMessageData +messageNotificationData c apnsQ = do + (nonce, message) <- messageNotification apnsQ + NtfToken {ntfDhSecret = Just dhSecret} <- getNtfTokenData c + Right pnMsgData <- liftEither . first INTERNAL $ Right . strDecode =<< first show (C.cbDecrypt dhSecret nonce message) + pure pnMsgData + noNotification :: TBQueue APNSMockRequest -> ExceptT AgentErrorType IO () noNotification apnsQ = do 500000 `timeout` atomically (readTBQueue apnsQ) >>= \case From cb5ad1619caf5d46f39a496e3be436d53b8a521f Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Fri, 8 Jul 2022 14:48:08 +0100 Subject: [PATCH 129/131] update version v3.0.0-rc.1 --- apps/ntf-server/Main.hs | 2 +- src/Simplex/Messaging/Transport.hs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/ntf-server/Main.hs b/apps/ntf-server/Main.hs index 1c61bc247..ddb004f5a 100644 --- a/apps/ntf-server/Main.hs +++ b/apps/ntf-server/Main.hs @@ -42,7 +42,7 @@ ntfServerCLIConfig = fingerprintFile = combine cfgPath "fingerprint", defaultServerPort = "443", executableName = "ntf-server", - serverVersion = "SMP notifications server v1.0.0-rc.0", + serverVersion = "SMP notifications server v1.0.0-rc.1", mkIniFile = \enableStoreLog defaultServerPort -> "[STORE_LOG]\n\ \# The server uses STM memory for persistence,\n\ diff --git a/src/Simplex/Messaging/Transport.hs b/src/Simplex/Messaging/Transport.hs index 3039554c8..44d6a153a 100644 --- a/src/Simplex/Messaging/Transport.hs +++ b/src/Simplex/Messaging/Transport.hs @@ -96,7 +96,7 @@ supportedSMPServerVRange :: VersionRange supportedSMPServerVRange = mkVersionRange 1 3 simplexMQVersion :: String -simplexMQVersion = "3.0.0-rc.0" +simplexMQVersion = "3.0.0-rc.1" -- * Transport connection class From 9f5e6bfd1908e0f401e9a258e210535732546e90 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Sat, 9 Jul 2022 12:04:13 +0100 Subject: [PATCH 130/131] ntf protocol version negotiation (#467) --- .../Messaging/Notifications/Transport.hs | 57 +++++++++++++++++-- src/Simplex/Messaging/Transport.hs | 3 + 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/src/Simplex/Messaging/Notifications/Transport.hs b/src/Simplex/Messaging/Notifications/Transport.hs index f988be495..d5a50ee54 100644 --- a/src/Simplex/Messaging/Notifications/Transport.hs +++ b/src/Simplex/Messaging/Notifications/Transport.hs @@ -1,7 +1,13 @@ +{-# LANGUAGE DuplicateRecordFields #-} +{-# LANGUAGE LambdaCase #-} +{-# LANGUAGE NamedFieldPuns #-} +{-# LANGUAGE ScopedTypeVariables #-} + module Simplex.Messaging.Notifications.Transport where import Control.Monad.Except import qualified Simplex.Messaging.Crypto as C +import Simplex.Messaging.Encoding import Simplex.Messaging.Transport import Simplex.Messaging.Version @@ -11,13 +17,56 @@ ntfBlockSize = 512 supportedNTFServerVRange :: VersionRange supportedNTFServerVRange = mkVersionRange 1 1 +data NtfServerHandshake = NtfServerHandshake + { ntfVersionRange :: VersionRange, + sessionId :: SessionId + } + +data NtfClientHandshake = NtfClientHandshake + { -- | agreed SMP notifications server protocol version + ntfVersion :: Version, + -- | server identity - CA certificate fingerprint + keyHash :: C.KeyHash + } + +instance Encoding NtfServerHandshake where + smpEncode NtfServerHandshake {ntfVersionRange, sessionId} = + smpEncode (ntfVersionRange, sessionId) + smpP = do + (ntfVersionRange, sessionId) <- smpP + pure NtfServerHandshake {ntfVersionRange, sessionId} + +instance Encoding NtfClientHandshake where + smpEncode NtfClientHandshake {ntfVersion, keyHash} = smpEncode (ntfVersion, keyHash) + smpP = do + (ntfVersion, keyHash) <- smpP + pure NtfClientHandshake {ntfVersion, keyHash} + -- | Notifcations server transport handshake. -ntfServerHandshake :: Transport c => c -> C.KeyHash -> VersionRange -> ExceptT TransportError IO (THandle c) -ntfServerHandshake c _ _ = pure $ ntfTHandle c +ntfServerHandshake :: forall c. Transport c => c -> C.KeyHash -> VersionRange -> ExceptT TransportError IO (THandle c) +ntfServerHandshake c kh ntfVRange = do + let th@THandle {sessionId} = ntfTHandle c + sendHandshake th $ NtfServerHandshake {sessionId, ntfVersionRange = ntfVRange} + getHandshake th >>= \case + NtfClientHandshake {ntfVersion, keyHash} + | keyHash /= kh -> + throwError $ TEHandshake IDENTITY + | ntfVersion `isCompatible` ntfVRange -> do + pure (th :: THandle c) {thVersion = ntfVersion} + | otherwise -> throwError $ TEHandshake VERSION -- | Notifcations server client transport handshake. -ntfClientHandshake :: Transport c => c -> C.KeyHash -> VersionRange -> ExceptT TransportError IO (THandle c) -ntfClientHandshake c _ _ = pure $ ntfTHandle c +ntfClientHandshake :: forall c. Transport c => c -> C.KeyHash -> VersionRange -> ExceptT TransportError IO (THandle c) +ntfClientHandshake c keyHash ntfVRange = do + let th@THandle {sessionId} = ntfTHandle c + NtfServerHandshake {sessionId = sessId, ntfVersionRange} <- getHandshake th + if sessionId /= sessId + then throwError TEBadSession + else case ntfVersionRange `compatibleVersion` ntfVRange of + Just (Compatible ntfVersion) -> do + sendHandshake th $ NtfClientHandshake {ntfVersion, keyHash} + pure (th :: THandle c) {thVersion = ntfVersion} + Nothing -> throwError $ TEHandshake VERSION ntfTHandle :: Transport c => c -> THandle c ntfTHandle c = THandle {connection = c, sessionId = tlsUnique c, blockSize = ntfBlockSize, thVersion = 0} diff --git a/src/Simplex/Messaging/Transport.hs b/src/Simplex/Messaging/Transport.hs index 44d6a153a..7016052fc 100644 --- a/src/Simplex/Messaging/Transport.hs +++ b/src/Simplex/Messaging/Transport.hs @@ -46,12 +46,15 @@ module Simplex.Messaging.Transport -- * SMP transport THandle (..), TransportError (..), + HandshakeError (..), smpServerHandshake, smpClientHandshake, tPutBlock, tGetBlock, serializeTransportError, transportErrorP, + sendHandshake, + getHandshake, -- * Trim trailing CR trimCR, From 23496f1a34a1d2f2193a2ef45846aff65f87e0f4 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Sat, 9 Jul 2022 12:12:36 +0100 Subject: [PATCH 131/131] v3.0.0 (#466) * v3.0.0 * update changelog --- CHANGELOG.md | 27 +++++++++++++++++++++++++++ apps/ntf-server/Main.hs | 2 +- src/Simplex/Messaging/Transport.hs | 2 +- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f5208269d..2716924f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,30 @@ +# 3.0.0 + +SMP server: + +- restore undeliverd messages when the server is restarted. +- SMP protocol v3 to support push notification: + - updated SEND and MSG to add message flags (for notification flag that contros whether the notification is sent and for any future extensions) and to move message meta-data sent to the recipient into the encrypted envelope. + - update NKEY and NID to add e2e encryption keys (for the notification meta-data encryption between SMP server and the client), and update NMSG to include this meta-data. + - update ACK command to include message ID (to avoid acknowledging unprocessed message). + - add NDEL commands to remove notification subscription credentials from SMP queue. + - add GET command to receive messages without subscription - to be used in iOS notification service extension to receive messages without terminating app subscriptions. + +SMP agent: + +- new protocol for duplex connection handshake reducing traffic and connection time. +- support for SMP notifications server and managing device token. +- remove redundant FQDN validation from TLS handshake to prepare for access via Tor. +- support for fully stopping agent and for termporary suspending agent operations. +- improve management of duplicate message delivery. + +SMP notifications server v1.0: + +- SMP notifications protocol with version negotiation during handshake. +- device token registration and verification (via background notification). +- SMP notification subscriptions and push notifications via APNS. +- restoring notification subscriptions when the server is restarted. + # 2.3.0 SMP server: diff --git a/apps/ntf-server/Main.hs b/apps/ntf-server/Main.hs index ddb004f5a..53384bd1d 100644 --- a/apps/ntf-server/Main.hs +++ b/apps/ntf-server/Main.hs @@ -42,7 +42,7 @@ ntfServerCLIConfig = fingerprintFile = combine cfgPath "fingerprint", defaultServerPort = "443", executableName = "ntf-server", - serverVersion = "SMP notifications server v1.0.0-rc.1", + serverVersion = "SMP notifications server v1.0.0", mkIniFile = \enableStoreLog defaultServerPort -> "[STORE_LOG]\n\ \# The server uses STM memory for persistence,\n\ diff --git a/src/Simplex/Messaging/Transport.hs b/src/Simplex/Messaging/Transport.hs index 7016052fc..51b644f3b 100644 --- a/src/Simplex/Messaging/Transport.hs +++ b/src/Simplex/Messaging/Transport.hs @@ -99,7 +99,7 @@ supportedSMPServerVRange :: VersionRange supportedSMPServerVRange = mkVersionRange 1 3 simplexMQVersion :: String -simplexMQVersion = "3.0.0-rc.1" +simplexMQVersion = "3.0.0" -- * Transport connection class