@@ -194,7 +194,7 @@ import Simplex.Messaging.Agent.Store.Entity
194194import Simplex.Messaging.Agent.Store.Interface (closeDBStore , execSQL , getCurrentMigrations )
195195import Simplex.Messaging.Agent.Store.Shared (UpMigration (.. ), upMigration )
196196import qualified Simplex.Messaging.Agent.TSessionSubs as SS
197- import Simplex.Messaging.Client (NetworkRequestMode (.. ), SMPClientError , ServerTransmission (.. ), ServerTransmissionBatch , nonBlockingWriteTBQueue , smpErrorClientNotice , temporaryClientError , unexpectedResponse )
197+ import Simplex.Messaging.Client (NetworkRequestMode (.. ), SMPClientError , ServerTransmission (.. ), ServerTransmissionBatch , TransportSessionMode ( .. ), nonBlockingWriteTBQueue , smpErrorClientNotice , temporaryClientError , unexpectedResponse )
198198import qualified Simplex.Messaging.Crypto as C
199199import Simplex.Messaging.Crypto.File (CryptoFile , CryptoFileArgs )
200200import Simplex.Messaging.Crypto.Ratchet (PQEncryption , PQSupport (.. ), pattern PQEncOff , pattern PQEncOn , pattern PQSupportOff , pattern PQSupportOn )
@@ -249,13 +249,15 @@ import UnliftIO.STM
249249type AE a = ExceptT AgentErrorType IO a
250250
251251-- | Creates an SMP agent client instance
252- getSMPAgentClient :: AgentConfig -> InitialAgentServers -> DBStore -> Bool -> IO AgentClient
252+ getSMPAgentClient :: AgentConfig -> InitialAgentServers -> DBStore -> Bool -> AE AgentClient
253253getSMPAgentClient = getSMPAgentClient_ 1
254254{-# INLINE getSMPAgentClient #-}
255255
256- getSMPAgentClient_ :: Int -> AgentConfig -> InitialAgentServers -> DBStore -> Bool -> IO AgentClient
257- getSMPAgentClient_ clientId cfg initServers@ InitialAgentServers {smp, xftp, presetServers} store backgroundMode =
258- newSMPAgentEnv cfg store >>= runReaderT runAgent
256+ getSMPAgentClient_ :: Int -> AgentConfig -> InitialAgentServers -> DBStore -> Bool -> AE AgentClient
257+ getSMPAgentClient_ clientId cfg initServers@ InitialAgentServers {smp, xftp, netCfg, useServices, presetServers} store backgroundMode = do
258+ -- This error should be prevented in the app
259+ when (any id useServices && sessionMode netCfg == TSMEntity ) $ throwE $ CMD PROHIBITED " newAgentClient"
260+ liftIO $ newSMPAgentEnv cfg store >>= runReaderT runAgent
259261 where
260262 runAgent = do
261263 liftIO $ checkServers " SMP" smp >> checkServers " XFTP" xftp
@@ -594,18 +596,22 @@ testProtocolServer c nm userId srv = withAgentEnv' c $ case protocolTypeI @p of
594596 SPNTF -> runNTFServerTest c nm userId srv
595597
596598-- | 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
598- setNetworkConfig :: AgentClient -> NetworkConfig -> IO ()
599+ setNetworkConfig :: AgentClient -> NetworkConfig -> AE ()
599600setNetworkConfig c@ AgentClient {useNetworkConfig, proxySessTs} cfg' = do
600- ts <- getCurrentTime
601- changed <- atomically $ do
602- (_, cfg) <- readTVar useNetworkConfig
603- let changed = cfg /= cfg'
604- ! cfgSlow = slowNetworkConfig cfg'
605- when changed $ writeTVar useNetworkConfig (cfgSlow, cfg')
606- when (socksProxy cfg /= socksProxy cfg') $ writeTVar proxySessTs ts
607- pure changed
608- when changed $ reconnectAllServers c
601+ ts <- liftIO getCurrentTime
602+ (ok, changed) <- atomically $ do
603+ useServices <- readTVar $ useClientServices c
604+ if any id useServices && sessionMode cfg' == TSMEntity
605+ then pure (False , False )
606+ else do
607+ (_, cfg) <- readTVar useNetworkConfig
608+ let changed = cfg /= cfg'
609+ ! cfgSlow = slowNetworkConfig cfg'
610+ when changed $ writeTVar useNetworkConfig (cfgSlow, cfg')
611+ when (socksProxy cfg /= socksProxy cfg') $ writeTVar proxySessTs ts
612+ pure (True , changed)
613+ unless ok $ throwE $ CMD PROHIBITED " setNetworkConfig"
614+ when changed $ liftIO $ reconnectAllServers c
609615
610616setUserNetworkInfo :: AgentClient -> UserNetworkInfo -> IO ()
611617setUserNetworkInfo c@ AgentClient {userNetworkInfo, userNetworkUpdated} ni = withAgentEnv' c $ do
@@ -772,13 +778,19 @@ deleteUser' c@AgentClient {smpServersStats, xftpServersStats} userId delSMPQueue
772778 whenM (withStore' c (`deleteUserWithoutConns` userId)) . atomically $
773779 writeTBQueue (subQ c) (" " , " " , AEvt SAENone $ DEL_USER userId)
774780
775- -- TODO [certs rcv] should fail enabling if per-connection isolation is set
776781setUserService' :: AgentClient -> UserId -> Bool -> AM ()
777782setUserService' c userId enable = do
778- wasEnabled <- liftIO $ fromMaybe False <$> TM. lookupIO userId (useClientServices c)
779- when (enable /= wasEnabled) $ do
780- atomically $ TM. insert userId enable $ useClientServices c
781- unless enable $ withStore' c (`deleteClientServices` userId)
783+ (ok, changed) <- atomically $ do
784+ (cfg, _) <- readTVar $ useNetworkConfig c
785+ if enable && sessionMode cfg == TSMEntity
786+ then pure (False , False )
787+ else do
788+ wasEnabled <- fromMaybe False <$> TM. lookup userId (useClientServices c)
789+ let changed = enable /= wasEnabled
790+ when changed $ TM. insert userId enable $ useClientServices c
791+ pure (True , changed)
792+ unless ok $ throwE $ CMD PROHIBITED " setNetworkConfig"
793+ when (changed && not enable) $ withStore' c (`deleteClientServices` userId)
782794
783795newConnAsync :: ConnectionModeI c => AgentClient -> UserId -> ACorrId -> Bool -> SConnectionMode c -> CR. InitialKeys -> SubscriptionMode -> AM ConnId
784796newConnAsync c userId corrId enableNtfs cMode pqInitKeys subMode = do
0 commit comments