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; } }
private async Task SendResponseAsync(Response response) { if (response == null) { return; } try { byte[] data; byte[] responseBytes = ObjectSerializer.CommunicationObjectToBytes(response, NullValueHandling.Include); if (clientConnection.IsEncryptedConnection) { data = Encryptor.SymmetricDataEncrypt( responseBytes, NodeData.Instance.NodeKeys.SignPrivateKey, clientConnection.SymmetricKey, MessageDataType.Response, NodeData.Instance.NodeKeys.Password); } else { data = responseBytes; } await clientConnection.ClientSocket.SendAsync( data, WebSocketMessageType.Binary, true, CancellationToken.None).ConfigureAwait(false); if (response.ResponseType == ResponseType.EncryptedKey) { clientConnection.SentKey = true; } } catch (Exception ex) { Logger.WriteLog(ex); } }
private async Task SendNoticeToClientsAsync(IEnumerable <ClientConnection> clients, Notice notice) { try { if (clients.IsNullOrEmpty()) { return; } foreach (var client in clients) { try { if (client.IsProxiedClientConnection && client.ClientSocket == null && client.ProxyNodeWebSocket != null) { NodeConnection nodeConnection = connectionsService.GetNodeConnections().FirstOrDefault(opt => opt.NodeWebSocket == client.ProxyNodeWebSocket); byte[] noticeData = ObjectSerializer.CommunicationObjectToBytes(notice); if (client.IsEncryptedConnection) { noticeData = Encryptor.SymmetricDataEncrypt( noticeData, NodeData.Instance.NodeKeys.SignPrivateKey, client.SymmetricKey, MessageDataType.Notice, NodeData.Instance.NodeKeys.Password); } nodeNoticeService.SendProxyUsersNotificationsNodeNoticeAsync( noticeData, client.UserId.GetValueOrDefault(), client.PublicKey, nodeConnection); } else if (client.ClientSocket != null) { byte[] noticeData = ObjectSerializer.NoticeToBytes(notice); if (client.IsEncryptedConnection) { noticeData = Encryptor.SymmetricDataEncrypt( noticeData, NodeData.Instance.NodeKeys.SignPrivateKey, client.SymmetricKey, MessageDataType.Binary, NodeData.Instance.NodeKeys.Password); } await client.ClientSocket.SendAsync( noticeData, WebSocketMessageType.Binary, true, CancellationToken.None) .ConfigureAwait(false); } } catch (WebSocketException) { continue; } } } catch (Exception ex) { Logger.WriteLog(ex, notice.ToString()); } }
public async Task <Response> CreateResponseAsync() { if (request.NodeId == null || request.NodeId == NodeSettings.Configs.Node.Id) { byte[] symmetricKey = Encryptor.GetSymmetricKey( 256, RandomExtensions.NextInt64(), 100000, NodeData.Instance.NodeKeys.Password); byte[] publicKey; byte[] signPublicKey; if (request.UserId != null && request.PublicKeyId != null && request.SignPublicKeyId != null) { var userKey = await keysService.GetUserKeyAsync(request.PublicKeyId.Value, request.UserId.Value, false).ConfigureAwait(false); var signKey = await keysService.GetUserKeyAsync(request.SignPublicKeyId.Value, request.UserId.Value, true).ConfigureAwait(false); if (userKey != null && signKey != null) { publicKey = userKey.Data; signPublicKey = signKey.Data; clientConnection.UserId = userKey.UserId; connectionsService.AddOrUpdateUserConnection(userKey.UserId.Value, clientConnection); } else { return(new ResultResponse(request.RequestId, "Key not found", ObjectsLibrary.Enums.ErrorCode.ObjectDoesNotExists)); } } else { publicKey = request.PublicKey; signPublicKey = request.SignPublicKey; } byte[] encryptedKey = Encryptor.AsymmetricDataEncrypt( symmetricKey, publicKey, NodeData.Instance.NodeKeys.SignPrivateKey, NodeData.Instance.NodeKeys.Password); clientConnection.SymmetricKey = symmetricKey; clientConnection.PublicKey = publicKey; clientConnection.SignPublicKey = signPublicKey; return(new EncryptedKeyResponse(request.RequestId, encryptedKey)); } else { await MetricsHelper.Instance.SetCrossNodeApiInvolvedAsync(request.RequestId).ConfigureAwait(false); var nodeConnection = connectionsService.GetNodeConnection(request.NodeId.Value); if (nodeConnection != null) { clientConnection.UserId = request.UserId; clientConnection.ProxyNodeWebSocket = nodeConnection.NodeWebSocket; clientConnection.PublicKey = request.PublicKey; clientConnection.SignPublicKey = request.SignPublicKey; if (request.UserId != null) { connectionsService.AddOrUpdateUserConnection(request.UserId.GetValueOrDefault(), clientConnection); } var response = await nodeRequestSender.SendProxyUsersCommunicationsNodeRequestAsync( ObjectSerializer.CommunicationObjectToBytes(request), clientConnection.UserId.GetValueOrDefault(), nodeConnection, ObjectType.Request, clientConnection.PublicKey, clientConnection.SignPublicKey).ConfigureAwait(false); return(ObjectSerializer.BytesToResponse(response.CommunicationData)); } return(new ResultResponse(request.RequestId, "Internal server error.", ObjectsLibrary.Enums.ErrorCode.UnknownError)); } }