Skip to content

Commit 3ccf854

Browse files
authored
servers: maintain xor-hash of all associated queue IDs in PostgreSQL (#1668)
* servers: maintain xor-hash of all associated queue IDs in PostgreSQL (#1615) * ntf server: maintain xor-hash of all associated queue IDs via PostgreSQL triggers * smp server: xor hash with triggers * fix sql and using pgcrypto extension in tests * track counts and hashes in smp/ntf servers via triggers, smp server stats for service subscription, update SMP protocol to pass expected count and hash in SSUB/NSSUB commands * agent migrations with functions/triggers * remove agent triggers * try tracking service subs in the agent (WIP, does not compile) * Revert "try tracking service subs in the agent (WIP, does not compile)" This reverts commit 59e9081. * comment * agent database triggers * service subscriptions in the client * test / fix client services * update schema * fix postgres migration * update schema * move schema test to the end * use static function with SQLite to avoid dynamic wrapper
1 parent 1ca4677 commit 3ccf854

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+2968
-330
lines changed

simplexmq.cabal

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ library
167167
Simplex.Messaging.Agent.Store.Postgres.Migrations.M20250702_conn_invitations_remove_cascade_delete
168168
Simplex.Messaging.Agent.Store.Postgres.Migrations.M20251009_queue_to_subscribe
169169
Simplex.Messaging.Agent.Store.Postgres.Migrations.M20251010_client_notices
170+
Simplex.Messaging.Agent.Store.Postgres.Migrations.M20251020_service_certs
170171
else
171172
exposed-modules:
172173
Simplex.Messaging.Agent.Store.SQLite
@@ -217,12 +218,14 @@ library
217218
Simplex.Messaging.Agent.Store.SQLite.Migrations.M20251009_queue_to_subscribe
218219
Simplex.Messaging.Agent.Store.SQLite.Migrations.M20251010_client_notices
219220
Simplex.Messaging.Agent.Store.SQLite.Migrations.M20251020_service_certs
221+
Simplex.Messaging.Agent.Store.SQLite.Util
220222
if flag(client_postgres) || flag(server_postgres)
221223
exposed-modules:
222224
Simplex.Messaging.Agent.Store.Postgres
223225
Simplex.Messaging.Agent.Store.Postgres.Common
224226
Simplex.Messaging.Agent.Store.Postgres.DB
225227
Simplex.Messaging.Agent.Store.Postgres.Migrations
228+
Simplex.Messaging.Agent.Store.Postgres.Migrations.Util
226229
Simplex.Messaging.Agent.Store.Postgres.Util
227230
if !flag(client_library)
228231
exposed-modules:

src/Simplex/Messaging/Agent.hs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,6 @@ import Simplex.Messaging.Protocol
211211
ErrorType (AUTH),
212212
MsgBody,
213213
MsgFlags (..),
214-
IdsHash,
215214
NtfServer,
216215
ProtoServerWithAuth (..),
217216
ProtocolServer (..),
@@ -222,6 +221,7 @@ import Simplex.Messaging.Protocol
222221
SMPMsgMeta,
223222
SParty (..),
224223
SProtocolType (..),
224+
ServiceSub (..),
225225
SndPublicAuthKey,
226226
SubscriptionMode (..),
227227
UserProtocol,
@@ -500,7 +500,7 @@ resubscribeConnections :: AgentClient -> [ConnId] -> AE (Map ConnId (Either Agen
500500
resubscribeConnections c = withAgentEnv c . resubscribeConnections' c
501501
{-# INLINE resubscribeConnections #-}
502502

503-
subscribeClientServices :: AgentClient -> UserId -> AE (Map SMPServer (Either AgentErrorType (Int64, IdsHash)))
503+
subscribeClientServices :: AgentClient -> UserId -> AE (Map SMPServer (Either AgentErrorType ServiceSub))
504504
subscribeClientServices c = withAgentEnv c . subscribeClientServices' c
505505
{-# INLINE subscribeClientServices #-}
506506

@@ -594,6 +594,7 @@ testProtocolServer c nm userId srv = withAgentEnv' c $ case protocolTypeI @p of
594594
SPNTF -> runNTFServerTest c nm userId srv
595595

596596
-- | set SOCKS5 proxy on/off and optionally set TCP timeouts for fast network
597+
-- TODO [certs rcv] should fail if any user is enabled to use services and per-connection isolation is chosen
597598
setNetworkConfig :: AgentClient -> NetworkConfig -> IO ()
598599
setNetworkConfig c@AgentClient {useNetworkConfig, proxySessTs} cfg' = do
599600
ts <- getCurrentTime
@@ -771,6 +772,7 @@ deleteUser' c@AgentClient {smpServersStats, xftpServersStats} userId delSMPQueue
771772
whenM (withStore' c (`deleteUserWithoutConns` userId)) . atomically $
772773
writeTBQueue (subQ c) ("", "", AEvt SAENone $ DEL_USER userId)
773774

775+
-- TODO [certs rcv] should fail enabling if per-connection isolation is set
774776
setUserService' :: AgentClient -> UserId -> Bool -> AM ()
775777
setUserService' c userId enable = do
776778
wasEnabled <- liftIO $ fromMaybe False <$> TM.lookupIO userId (useClientServices c)
@@ -1507,15 +1509,15 @@ resubscribeConnections' c connIds = do
15071509
[] -> pure True
15081510
rqs' -> anyM $ map (atomically . hasActiveSubscription c) rqs'
15091511

1510-
-- TODO [certs rcv] compare hash with lock
1511-
subscribeClientServices' :: AgentClient -> UserId -> AM (Map SMPServer (Either AgentErrorType (Int64, IdsHash)))
1512+
-- TODO [certs rcv] compare hash. possibly, it should return both expected and returned counts
1513+
subscribeClientServices' :: AgentClient -> UserId -> AM (Map SMPServer (Either AgentErrorType ServiceSub))
15121514
subscribeClientServices' c userId =
15131515
ifM useService subscribe $ throwError $ CMD PROHIBITED "no user service allowed"
15141516
where
15151517
useService = liftIO $ (Just True ==) <$> TM.lookupIO userId (useClientServices c)
15161518
subscribe = do
15171519
srvs <- withStore' c (`getClientServiceServers` userId)
1518-
lift $ M.fromList . zip srvs <$> mapConcurrently (tryAllErrors' . subscribeClientService c userId) srvs
1520+
lift $ M.fromList <$> mapConcurrently (\(srv, ServiceSub _ n idsHash) -> fmap (srv,) $ tryAllErrors' $ subscribeClientService c userId srv n idsHash) srvs
15191521

15201522
-- requesting messages sequentially, to reduce memory usage
15211523
getConnectionMessages' :: AgentClient -> NonEmpty ConnMsgReq -> AM' (NonEmpty (Either AgentErrorType (Maybe SMPMsgMeta)))
@@ -2829,12 +2831,13 @@ processSMPTransmissions :: AgentClient -> ServerTransmissionBatch SMPVersion Err
28292831
processSMPTransmissions c@AgentClient {subQ} (tSess@(userId, srv, _), _v, sessId, ts) = do
28302832
upConnIds <- newTVarIO []
28312833
forM_ ts $ \(entId, t) -> case t of
2832-
STEvent msgOrErr ->
2833-
withRcvConn entId $ \rq@RcvQueue {connId} conn -> case msgOrErr of
2834-
Right msg -> runProcessSMP rq conn (toConnData conn) msg
2835-
Left e -> lift $ do
2836-
processClientNotice rq e
2837-
notifyErr connId e
2834+
STEvent msgOrErr
2835+
| entId == SMP.NoEntity -> pure () -- TODO [certs rcv] process SALL
2836+
| otherwise -> withRcvConn entId $ \rq@RcvQueue {connId} conn -> case msgOrErr of
2837+
Right msg -> runProcessSMP rq conn (toConnData conn) msg
2838+
Left e -> lift $ do
2839+
processClientNotice rq e
2840+
notifyErr connId e
28382841
STResponse (Cmd SRecipient cmd) respOrErr ->
28392842
withRcvConn entId $ \rq conn -> case cmd of
28402843
SMP.SUB -> case respOrErr of
@@ -2870,7 +2873,7 @@ processSMPTransmissions c@AgentClient {subQ} (tSess@(userId, srv, _), _v, sessId
28702873
processSubOk :: RcvQueue -> TVar [ConnId] -> IO ()
28712874
processSubOk rq@RcvQueue {connId} upConnIds =
28722875
atomically . whenM (isPendingSub rq) $ do
2873-
SS.addActiveSub tSess sessId (rcvQueueSub rq) $ currentSubs c
2876+
SS.addActiveSub tSess sessId rq $ currentSubs c
28742877
modifyTVar' upConnIds (connId :)
28752878
processSubErr :: RcvQueue -> SMPClientError -> AM' ()
28762879
processSubErr rq@RcvQueue {connId} e = do

0 commit comments

Comments
 (0)