3131module Simplex.Messaging.Server
3232 ( runSMPServer ,
3333 runSMPServerBlocking ,
34+ controlPortAuth ,
3435 importMessages ,
3536 exportMessages ,
3637 printMessageStats ,
@@ -558,6 +559,7 @@ smpServer started cfg@ServerConfig {transports, transportConfig = tCfg, startOpt
558559 msgNtfNoSub' <- atomicSwapIORef (msgNtfNoSub ss) 0
559560 msgNtfLost' <- atomicSwapIORef (msgNtfLost ss) 0
560561 msgNtfExpired' <- atomicSwapIORef (msgNtfExpired ss) 0
562+ _qBlocked <- atomicSwapIORef (qBlocked ss) 0 -- not logged, only reset
561563 pRelays' <- getResetProxyStatsData pRelays
562564 pRelaysOwn' <- getResetProxyStatsData pRelaysOwn
563565 pMsgFwds' <- getResetProxyStatsData pMsgFwds
@@ -770,12 +772,9 @@ smpServer started cfg@ServerConfig {transports, transportConfig = tCfg, startOpt
770772 CPSkip -> False
771773 _ -> True
772774 processCP h role = \ case
773- CPAuth auth -> atomically $ writeTVar role $! newRole cfg
775+ CPAuth auth -> controlPortAuth h user admin role auth
774776 where
775- newRole ServerConfig {controlPortUserAuth = user, controlPortAdminAuth = admin}
776- | Just auth == admin = CPRAdmin
777- | Just auth == user = CPRUser
778- | otherwise = CPRNone
777+ ServerConfig {controlPortUserAuth = user, controlPortAdminAuth = admin} = cfg
779778 CPSuspend -> withAdminRole $ hPutStrLn h " suspend not implemented"
780779 CPResume -> withAdminRole $ hPutStrLn h " resume not implemented"
781780 CPClients -> withAdminRole $ do
@@ -964,7 +963,7 @@ smpServer started cfg@ServerConfig {transports, transportConfig = tCfg, startOpt
964963 SubPending -> (c1, c2 + 1 , c3, c4)
965964 SubThread _ -> (c1, c2, c3 + 1 , c4)
966965 ProhibitSub -> pure (c1, c2, c3, c4 + 1 )
967- CPDelete sId -> withUserRole $ unliftIO u $ do
966+ CPDelete sId -> withAdminRole $ unliftIO u $ do
968967 st <- asks msgStore
969968 r <- liftIO $ runExceptT $ do
970969 q <- ExceptT $ getQueue st SSender sId
@@ -983,14 +982,20 @@ smpServer started cfg@ServerConfig {transports, transportConfig = tCfg, startOpt
983982 " status: " <> show status <> " , updatedAt: " <> show updatedAt <> " , queueMode: " <> show queueMode
984983 CPBlock sId info -> withUserRole $ unliftIO u $ do
985984 st <- asks msgStore
986- r <- liftIO $ runExceptT $ do
987- q <- ExceptT $ getQueue st SSender sId
988- ExceptT $ blockQueue (queueStore st) q info
989- case r of
990- Left e -> liftIO $ hPutStrLn h $ " error: " <> show e
991- Right () -> do
992- incStat . qBlocked =<< asks serverStats
993- liftIO $ hPutStrLn h " ok"
985+ stats <- asks serverStats
986+ blocked <- liftIO $ readIORef $ qBlocked stats
987+ let quota = dailyBlockQueueQuota cfg
988+ if blocked >= quota && quota /= 0
989+ then liftIO $ hPutStrLn h $ " error: reached limit of " <> show quota <> " queues blocked daily"
990+ else do
991+ r <- liftIO $ runExceptT $ do
992+ q <- ExceptT $ getQueue st SSender sId
993+ ExceptT $ blockQueue (queueStore st) q info
994+ case r of
995+ Left e -> liftIO $ hPutStrLn h $ " error: " <> show e
996+ Right () -> do
997+ incStat $ qBlocked stats
998+ liftIO $ hPutStrLn h " ok"
994999 CPUnblock sId -> withUserRole $ unliftIO u $ do
9951000 st <- asks msgStore
9961001 r <- liftIO $ runExceptT $ do
@@ -1045,6 +1050,20 @@ runClientTransport h@THandle {params = thParams@THandleParams {sessionId}} = do
10451050 where
10461051 hasSubs ServerSubscribers {subClients} = IS. member clientId <$> readTVarIO subClients
10471052
1053+ controlPortAuth :: Handle -> Maybe BasicAuth -> Maybe BasicAuth -> TVar CPClientRole -> BasicAuth -> IO ()
1054+ controlPortAuth h user admin role auth = do
1055+ readTVarIO role >>= \ case
1056+ CPRNone -> do
1057+ atomically $ writeTVar role $! newRole
1058+ hPutStrLn h $ currentRole newRole
1059+ r -> hPutStrLn h $ currentRole r <> if r == newRole then " " else " , start new session to change."
1060+ where
1061+ currentRole r = " Current role is " <> show r
1062+ newRole
1063+ | Just auth == admin = CPRAdmin
1064+ | Just auth == user = CPRUser
1065+ | otherwise = CPRNone
1066+
10481067clientDisconnected :: forall s . Client s -> M s ()
10491068clientDisconnected c@ Client {clientId, subscriptions, ntfSubscriptions, serviceSubsCount, ntfServiceSubsCount, connected, clientTHParams = THandleParams {sessionId, thAuth}, endThreads} = do
10501069 labelMyThread . B. unpack $ " client $" <> encode sessionId <> " disc"
0 commit comments