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);
            }
        }
示例#2
0
        public async Task HandleAsync()
        {
            ConversationsUsersNodeResponse response;
            List <ChatUserVm>    chatsUsers    = new List <ChatUserVm>();
            List <ChannelUserVm> channelsUsers = new List <ChannelUserVm>();

            foreach (long id in nodeRequest.ConversationsIds)
            {
                switch (nodeRequest.ConversationType)
                {
                case ObjectsLibrary.Enums.ConversationType.Chat:
                {
                    chatsUsers.AddRange(await loadChatsService.GetChatUsersAsync(id, null, int.MaxValue).ConfigureAwait(false));
                }
                break;

                case ObjectsLibrary.Enums.ConversationType.Channel:
                {
                    channelsUsers.AddRange(await loadChannelsService.GetChannelUsersAsync(id, null, null).ConfigureAwait(false));
                }
                break;
                }
            }
            response = new ConversationsUsersNodeResponse(nodeRequest.RequestId, chatsUsers, channelsUsers);
            await NodeWebSocketCommunicationManager.SendResponseAsync(response, nodeConnection).ConfigureAwait(false);
        }
        public async Task HandleAsync()
        {
            NodeResponse response;

            if (request.KeyId == null)
            {
                response = new PublicKeyNodeResponse(
                    request.RequestId,
                    NodeData.Instance.NodeKeys.PublicKey,
                    NodeData.Instance.NodeKeys.SignPublicKey,
                    NodeData.Instance.NodeKeys.KeyId,
                    NodeData.Instance.NodeKeys.ExpirationTime);
            }
            else
            {
                var key = await keysService.GetNodeKeysAsync(NodeSettings.Configs.Node.Id, request.KeyId.Value);

                if (key == null)
                {
                    response = new ResultNodeResponse(request.RequestId, ObjectsLibrary.Enums.ErrorCode.ObjectDoesNotExists, "The key was not found.");
                }
                else
                {
                    response = new PublicKeyNodeResponse(request.RequestId, key.PublicKey, key.SignPublicKey, key.KeyId, key.ExpirationTime);
                }
            }
            await NodeWebSocketCommunicationManager.SendResponseAsync(response, current).ConfigureAwait(false);
        }
示例#4
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);
            }
        }
示例#5
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);
        }
示例#6
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);
        }
示例#7
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);
            }
        }
示例#9
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);
            }
        }
示例#10
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);
            }
        }
示例#11
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);
            }
        }
示例#12
0
        public override async Task BeginReceiveAsync()
        {
            try
            {
                while (socket.State == WebSocketState.Open)
                {
                    try
                    {
                        byte[] receivedData = await ReceiveBytesAsync(16 * 1024, int.MaxValue).ConfigureAwait(true);

                        NodeWebSocketCommunicationManager manager = new NodeWebSocketCommunicationManager(_appServiceProvider);
                        manager.Handle(receivedData, currentNode);
                    }
                    catch (WebSocketException)
                    {
                        currentNode.PublicKey    = null;
                        currentNode.SymmetricKey = null;
                        if (currentNode.Node != null)
                        {
                            connectionsService.RemoveNodeConnection(currentNode);
                        }

                        return;
                    }
                    catch (TooLargeReceivedDataException ex)
                    {
                        Logger.WriteLog(ex);
                    }
                    catch (Exception ex)
                    {
                        Logger.WriteLog(ex);
                    }
                }
            }
            catch (WebSocketException)
            {
                return;
            }
            catch (Exception ex)
            {
                Logger.WriteLog(ex);
            }
        }
示例#13
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;
            }
        }
示例#14
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);
            }
        }