Ejemplo n.º 1
0
        public async Task HandleAsync()
        {
            try
            {
                List <long> nodesIds = await conversationsService.GetConversationNodesIdsAsync(request.ConversationType, request.ConversationId).ConfigureAwait(false);

                if (!nodesIds.Contains(current.Node.Id))
                {
                    NodeWebSocketCommunicationManager.SendResponse(new ResultNodeResponse(request.RequestId, ErrorCode.PermissionDenied, "No rights to load messages."), current);
                    return;
                }
                List <MessageDto> messages = await loadMessagesService.GetMessagesAsync(
                    request.ConversationId,
                    request.ConversationType,
                    request.Direction.GetValueOrDefault(true),
                    request.MessageId,
                    request.AttachmentsTypes,
                    request.Length).ConfigureAwait(false);

                MessagesNodeResponse response = new MessagesNodeResponse(request.RequestId, messages);
                NodeWebSocketCommunicationManager.SendResponse(response, current);
            }
            catch (Exception ex)
            {
                Logger.WriteLog(ex);
                NodeWebSocketCommunicationManager.SendResponse(new ResultNodeResponse(request.RequestId, ErrorCode.UnknownError), current);
            }
        }
        public async Task HandleAsync()
        {
            NodeResponse response = default;

            try
            {
                TokenVm token = await tokensService.CheckTokenAsync(request.Token, NodeSettings.Configs.Node.Id).ConfigureAwait(false);

                UserVm user = await loadUsersService.GetUserAsync(token.UserId).ConfigureAwait(false);

                response = new UserTokensNodeResponse(request.RequestId, token, user);
            }
            catch (InvalidTokenException)
            {
                response = new ResultNodeResponse(request.RequestId, ErrorCode.InvalidAccessToken);
            }
            catch (TokensTimeoutException)
            {
                response = new ResultNodeResponse(request.RequestId, ErrorCode.AccessTokenTimeout);
            }
            catch (Exception ex)
            {
                Logger.WriteLog(ex);
                response = new ResultNodeResponse(request.RequestId, ErrorCode.UnknownError);
            }
            finally
            {
                NodeWebSocketCommunicationManager.SendResponse(response, current);
            }
        }
Ejemplo n.º 3
0
        public async Task HandleAsync()
        {
            var users = await loadUsersService.FindUsersByPhonesAsync(request.Phones).ConfigureAwait(false);

            NodeWebSocketCommunicationManager.SendResponse(
                new UsersNodeResponse(request.RequestId, privacyService.ApplyPrivacySettings(users, request.Phones, request.RequestorId)),
                current);
        }
Ejemplo n.º 4
0
        public async Task HandleAsync()
        {
            NodeResponse response;

            try
            {
                switch (request.RequestType)
                {
                case Enums.NodeRequestType.GetChats:
                {
                    IEnumerable <ChatVm> chats = await loadChatsService.GetChatsByIdAsync(request.ObjectsId, request.RequestorUserId).ConfigureAwait(false);

                    response = new ChatsNodeResponse(request.RequestId, chats);
                }
                break;

                case Enums.NodeRequestType.GetUsers:
                {
                    IEnumerable <UserVm> users = await loadUsersService.GetUsersByIdAsync(request.ObjectsId, request.RequestorUserId).ConfigureAwait(false);

                    users = await privacyService.ApplyPrivacySettingsAsync(users, request.RequestorUserId).ConfigureAwait(false);

                    response = new UsersNodeResponse(request.RequestId, users);
                }
                break;

                case Enums.NodeRequestType.GetChannels:
                {
                    IEnumerable <ChannelDto> channels = await loadChannelsService.GetChannelsWithSubscribersAsync(request.ObjectsId).ConfigureAwait(false);

                    response = new ChannelsNodeResponse(request.RequestId, channels);
                }
                break;

                case Enums.NodeRequestType.GetFiles:
                {
                    IEnumerable <FileInfoVm> files = await filesService.GetFilesInfoAsync(request.FilesIds).ConfigureAwait(false);

                    response = new FilesInformationResponse(request.RequestId, files);
                }
                break;

                default:
                    response = new ResultNodeResponse(request.RequestId, ObjectsLibrary.Enums.ErrorCode.InvalidRequestData, "Unsupported request type.");
                    break;
                }
            }
            catch
            {
                response = new ResultNodeResponse(request.RequestId, ObjectsLibrary.Enums.ErrorCode.UnknownError, "An error occurred while processing the request.");
            }
            NodeWebSocketCommunicationManager.SendResponse(response, nodeConnection);
        }
Ejemplo n.º 5
0
        public async Task HandleAsync()
        {
            try
            {
                var pollDto = await pollsService.GetPollAsync(request.PollId, request.ConversationId, request.ConversationType).ConfigureAwait(false);

                NodeWebSocketCommunicationManager.SendResponse(new PollNodeResponse(request.RequestId, pollDto), current);
            }
            catch (Exception ex)
            {
                Logger.WriteLog(ex);
                NodeWebSocketCommunicationManager.SendResponse(new ResultNodeResponse(request.RequestId, ObjectsLibrary.Enums.ErrorCode.UnknownError), current);
            }
        }
        public async Task HandleAsync()
        {
            try
            {
                BlockchainInfo info = await BlockchainReadService.GetBlockchainInformationAsync().ConfigureAwait(false);

                BlockchainInfoNodeResponse response = new BlockchainInfoNodeResponse(request.RequestId, info);
                NodeWebSocketCommunicationManager.SendResponse(response, current);
            }
            catch (Exception ex)
            {
                Logger.WriteLog(ex);
                NodeWebSocketCommunicationManager.SendResponse(new ResultNodeResponse(request.RequestId, ErrorCode.UnknownError), current);
            }
        }
Ejemplo n.º 7
0
        public async Task HandleAsync()
        {
            try
            {
                List <ChatUserDto> chatUsers = await loadChatsService.GetChatUsersAsync(request.UsersId, request.ChatId).ConfigureAwait(false);

                ChatUsersNodeResponse response = new ChatUsersNodeResponse(request.RequestId, ChatUserConverter.GetChatUsersVm(chatUsers));
                NodeWebSocketCommunicationManager.SendResponse(response, current);
            }
            catch (Exception ex)
            {
                Logger.WriteLog(ex);
                NodeWebSocketCommunicationManager.SendResponse(new ResultNodeResponse(request.RequestId, ObjectsLibrary.Enums.ErrorCode.UnknownError), current);
            }
        }
Ejemplo n.º 8
0
        public async Task HandleAsync()
        {
            try
            {
                ChatVm chat = await loadChatsService.GetChatByIdAsync(request.ChatId).ConfigureAwait(false);

                ChatsNodeResponse response = new ChatsNodeResponse(request.RequestId, new List <ChatVm> {
                    chat
                });
                NodeWebSocketCommunicationManager.SendResponse(response, current);
            }
            catch (Exception ex)
            {
                Logger.WriteLog(ex);
                NodeWebSocketCommunicationManager.SendResponse(new ResultNodeResponse(request.RequestId, ErrorCode.UnknownError), current);
            }
        }
Ejemplo n.º 9
0
        public async Task HandleAsync()
        {
            try
            {
                List <UserVm>    users    = null;
                List <ChatVm>    chats    = null;
                List <ChannelVm> channels = null;
                foreach (var searchType in request.SearchTypes)
                {
                    switch (searchType)
                    {
                    case SearchType.Users:
                        users = await loadUsersService.FindUsersByStringQueryAsync(request.SearchQuery, request.NavigationId, request.Direction).ConfigureAwait(false);

                        break;

                    case SearchType.Chats:
                        chats = await loadChatsService.FindChatsByStringQueryAsync(request.SearchQuery, request.NavigationId, request.Direction, request.RequestorId).ConfigureAwait(false);

                        break;

                    case SearchType.Channels:
                        channels = await loadChannelsService.FindChannelsByStringQueryAsync(request.SearchQuery, request.NavigationId, request.Direction).ConfigureAwait(false);

                        break;

                    default:
                        continue;
                    }
                }
                SearchNodeResponse response = new SearchNodeResponse(
                    request.RequestId,
                    privacyService.ApplyPrivacySettings(users, request.SearchQuery, request.RequestorId),
                    channels,
                    chats);
                NodeWebSocketCommunicationManager.SendResponse(response, current);
            }
            catch (Exception ex)
            {
                Logger.WriteLog(ex);
                NodeWebSocketCommunicationManager.SendResponse(new ResultNodeResponse(request.RequestId, ErrorCode.UnknownError, ex.ToString()), current);
            }
        }
Ejemplo n.º 10
0
        private void SendResponse(Response response, ClientConnection clientConnection, NodeConnection nodeConnection)
        {
            byte[] responseData = ObjectSerializer.CommunicationObjectToBytes(response);
            if (clientConnection.IsEncryptedConnection)
            {
                responseData = Encryptor.SymmetricDataEncrypt(
                    responseData,
                    NodeData.Instance.NodeKeys.SignPrivateKey,
                    clientConnection.SymmetricKey,
                    MessageDataType.Response,
                    NodeData.Instance.NodeKeys.Password);
            }
            ProxyUsersCommunicationsNodeResponse nodeResponse = new ProxyUsersCommunicationsNodeResponse(request.RequestId, responseData, request.UserId, request.UserPublicKey);

            NodeWebSocketCommunicationManager.SendResponse(nodeResponse, nodeConnection);
            if (response.ResponseType == ResponseType.EncryptedKey)
            {
                clientConnection.SentKey = true;
            }
        }
Ejemplo n.º 11
0
        public async Task HandleAsync()
        {
            try
            {
                var         nodeKey     = request.Keys;
                ConnectData connectData = request.GetConnectData(nodeKey.SignPublicKey, NodeData.Instance.NodeKeys.Password, NodeData.Instance.NodeKeys.PrivateKey);
                nodeConnection.Node          = connectData.Node;
                nodeConnection.Node.StartDay = nodeConnection.Node.StartDay.ToUniversalTime();
                var  nodeJson = ObjectSerializer.ObjectToJson(nodeConnection.Node);
                bool isValid  = Encryptor.CheckSign(
                    connectData.LicensorSign,
                    LicensorClient.Instance.GetSignPublicKey(),
                    Encoding.UTF8.GetBytes(nodeJson),
                    NodeData.Instance.NodeKeys.Password);
                if (!isValid)
                {
                    NodeWebSocketCommunicationManager.SendResponse(
                        new ResultNodeResponse(request.RequestId, ErrorCode.AuthorizationProblem, "Wrong sign for node data."), nodeConnection);
                }
                LicenseVm license     = connectData.License;
                long      currentTime = DateTime.UtcNow.ToUnixTime();
                if (!license.IsLicenseValid(currentTime, LicensorClient.Instance.GetSignPublicKey(), NodeData.Instance.NodeKeys.Password, out _))
                {
                    var licenseFromLicensor = await LicensorClient.Instance.GetLicenseAsync(nodeConnection.Node.Id).ConfigureAwait(false);

                    if (!licenseFromLicensor.IsLicenseValid(currentTime, LicensorClient.Instance.GetSignPublicKey(), NodeData.Instance.NodeKeys.Password, out var errorMessage))
                    {
                        var isBlockchainLicenseValid = await BlockchainReadService.IsNodeLicenseValidAsync(nodeConnection.Node.Id, currentTime).ConfigureAwait(false);

                        if (!isBlockchainLicenseValid)
                        {
                            NodeWebSocketCommunicationManager.SendResponse(
                                new ResultNodeResponse(request.RequestId, ErrorCode.AuthorizationProblem, errorMessage), nodeConnection);
                        }
                    }
                }
                nodeConnection.Uri = new Uri($"wss://{nodeConnection.Node.Domains.FirstOrDefault()}:{nodeConnection.Node.NodesPort}");
                ConnectData responseConnectData = new ConnectData
                {
                    Node         = NodeSettings.Configs.Node,
                    LicensorSign = NodeSettings.Configs.LicensorSign.Sign,
                    License      = NodeSettings.Configs.License
                };
                byte[] encryptedData = Encryptor.SymmetricDataEncrypt(
                    ObjectSerializer.ObjectToByteArray(responseConnectData),
                    NodeData.Instance.NodeKeys.SignPrivateKey,
                    connectData.SymmetricKey,
                    MessageDataType.Binary,
                    NodeData.Instance.NodeKeys.Password);
                await NodeWebSocketCommunicationManager.SendResponseAsync(new NodesInformationResponse(request.RequestId, encryptedData), nodeConnection).ConfigureAwait(false);

                nodeConnection.PublicKey    = nodeKey.PublicKey;
                nodeConnection.SymmetricKey = connectData.SymmetricKey;
                nodeConnection.Node.NodeKey.EncPublicKey = nodeKey.PublicKey;
                nodeConnection.PublicKeyExpirationTime   = nodeKey.ExpirationTime;
                nodeConnection.PublicKeyId   = nodeKey.KeyId;
                nodeConnection.SignPublicKey = nodeKey.SignPublicKey;
                nodesService.CreateOrUpdateNodeInformationAsync(nodeConnection.Node);
                connectionsService.AddOrUpdateNodeConnection(nodeConnection.Node.Id, nodeConnection);
                await Task.Delay(TimeSpan.FromSeconds(1)).ConfigureAwait(false);

                await nodeNoticeService.SendPendingMessagesAsync(nodeConnection.Node.Id).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                Logger.WriteLog(ex);
                NodeWebSocketCommunicationManager.SendResponse(new ResultNodeResponse(request.RequestId, ErrorCode.UnknownError, ex.Message), nodeConnection);
            }
        }