Exemplo n.º 1
0
 public async Task HandleAsync()
 {
     switch (request.ObjectType)
     {
     case ObjectType.Request:
     {
         ClientConnection    clientConnection = null;
         CommunicationObject userObject;
         var clientConnections = serviceProvider.ConnectionsService.GetUserClientConnections(request.UserId);
         if (clientConnections != null)
         {
             clientConnection = clientConnections.FirstOrDefault(opt => opt.IsProxiedClientConnection && opt.ProxyNodeWebSocket != null);
         }
         if (clientConnection == null)
         {
             clientConnection = new ClientConnection(request.UserId, current.NodeWebSocket);
             serviceProvider.ConnectionsService.AddOrUpdateUserConnection(request.UserId, clientConnection);
         }
         if (clientConnection.IsEncryptedConnection)
         {
             byte[] decryptedData = Encryptor.SymmetricDataDecrypt(
                 request.CommunicationData,
                 clientConnection.SignPublicKey,
                 clientConnection.SymmetricKey,
                 NodeData.Instance.NodeKeys.Password).DecryptedData;
             userObject = ObjectSerializer.BytesToCommunicationObject(decryptedData);
         }
         else
         {
             userObject = ObjectSerializer.BytesToCommunicationObject(request.CommunicationData);
         }
         IRequestHandler requestHandler = ClientWebSocketRequestManager.InitRequestHandler(
             (Request)userObject,
             clientConnection,
             clientRequestService,
             serviceProvider);
         Response response;
         try
         {
             response = await requestHandler.CreateResponseAsync().ConfigureAwait(false);
         }
         catch
         {
             response = new ResultResponse(request.RequestId, null, ErrorCode.UnknownError);
         }
         SendResponse(response, clientConnection, current);
     }
     break;
     }
 }
Exemplo n.º 2
0
        public async Task HandleRequestAsync(Request request, int requestSize)
        {
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            try
            {
                Response response = default;
                try
                {
                    if (!clientConnection.Banned.GetValueOrDefault(false) || allowedRequestTypes.Contains(request.RequestType))
                    {
                        IRequestHandler requestHandler = InitRequestHandler(
                            request,
                            clientConnection,
                            clientRequestService,
                            appServiceProvider);
                        Logger.WriteLog(request, clientConnection);
                        if (!requestHandler.IsRequestValid())
                        {
                            throw new InvalidRequestDataException();
                        }
                        response = await requestHandler.CreateResponseAsync().ConfigureAwait(false);
                    }
                    else
                    {
                        response = new ResultResponse(request.RequestId, "User is banned.", ErrorCode.PermissionDenied);
                    }
                }
                catch (InvalidRequestDataException ex)
                {
                    Logger.WriteLog(ex, request);
                    response = new ResultResponse(request.RequestId, "Invalid request data.", ErrorCode.InvalidRequestData);
                }
                catch (UnknownRequestTypeException)
                {
                    response = new ResultResponse(request.RequestId, "Invalid request type.", ErrorCode.InvalidRequestData);
                }
                catch (UnauthorizedUserException)
                {
                    response = new ResultResponse(request.RequestId, "User is not authorized.", ErrorCode.UserNotAuthorized);
                }
                catch (PermissionDeniedException ex)
                {
                    response = new ResultResponse(request.RequestId, ex.Message, ErrorCode.PermissionDenied);
                }
                catch (Exception ex)
                {
                    Logger.WriteLog(ex, request);
                    response = new ResultResponse(request?.RequestId ?? -1, "Internal server error.", ErrorCode.UnknownError);
                }
                finally
                {
                    long currentTime = DateTime.UtcNow.ToUnixTime();
                    await SendResponseAsync(response).ConfigureAwait(false);

                    stopwatch.Stop();
                    Logger.WriteRequestMetrics(request.RequestType, stopwatch.ElapsedMilliseconds, requestSize, request.RequestId);
                    if (currentTime - prevRequestTime >= 5)
                    {
                        await appServiceProvider.UpdateUsersService.UpdateUserActivityTimeAsync(clientConnection).ConfigureAwait(false);
                    }
                    prevRequestTime = currentTime;
                    Logger.WriteLog(response, clientConnection);
                }
            }
            catch (Exception ex)
            {
                Logger.WriteLog(ex);
            }
        }