예제 #1
0
        /// <summary>
        /// Checks whether the profile was fully initialized.
        /// </summary>
        /// <returns>true if the identity's profile was initialized properly, false otherwise.</returns>
        public bool IsProfileInitialized()
        {
            SemVer ver = new SemVer(Version);

            return(ver.IsValid());
        }
예제 #2
0
        /// <summary>
        /// Processing of a message received from a client.
        /// </summary>
        /// <param name="Client">TCP client who send the message.</param>
        /// <param name="IncomingMessage">Full ProtoBuf message to be processed.</param>
        /// <returns>true if the conversation with the client should continue, false if a protocol violation error occurred and the client should be disconnected.</returns>
        public async Task <bool> ProcessMessageAsync(ClientBase Client, IProtocolMessage IncomingMessage)
        {
            IncomingClient      client          = (IncomingClient)Client;
            ProxProtocolMessage incomingMessage = (ProxProtocolMessage)IncomingMessage;
            ProxMessageBuilder  messageBuilder  = client.MessageBuilder;

            bool res = false;

            log.Debug("()");
            try
            {
                // Update time until this client's connection is considered inactive.
                client.NextKeepAliveTime = DateTime.UtcNow.AddMilliseconds(client.KeepAliveIntervalMs);
                log.Trace("Client ID {0} NextKeepAliveTime updated to {1}.", client.Id.ToHex(), client.NextKeepAliveTime.ToString("yyyy-MM-dd HH:mm:ss"));

                log.Trace("Received message type is {0}, message ID is {1}.", incomingMessage.MessageTypeCase, incomingMessage.Id);
                switch (incomingMessage.MessageTypeCase)
                {
                case Message.MessageTypeOneofCase.Request:
                {
                    ProxProtocolMessage responseMessage = messageBuilder.CreateErrorProtocolViolationResponse(incomingMessage);
                    Request             request         = incomingMessage.Request;
                    log.Trace("Request conversation type is {0}.", request.ConversationTypeCase);
                    switch (request.ConversationTypeCase)
                    {
                    case Request.ConversationTypeOneofCase.SingleRequest:
                    {
                        SingleRequest singleRequest = request.SingleRequest;
                        SemVer        version       = new SemVer(singleRequest.Version);
                        log.Trace("Single request type is {0}, version is {1}.", singleRequest.RequestTypeCase, version);

                        if (!version.IsValid())
                        {
                            responseMessage.Response.Details = "version";
                            break;
                        }

                        switch (singleRequest.RequestTypeCase)
                        {
                        case SingleRequest.RequestTypeOneofCase.Ping:
                            responseMessage = ProcessMessagePingRequest(client, incomingMessage);
                            break;

                        case SingleRequest.RequestTypeOneofCase.ListRoles:
                            responseMessage = ProcessMessageListRolesRequest(client, incomingMessage);
                            break;

                        default:
                            log.Warn("Invalid request type '{0}'.", singleRequest.RequestTypeCase);
                            break;
                        }

                        break;
                    }

                    case Request.ConversationTypeOneofCase.ConversationRequest:
                    {
                        ConversationRequest conversationRequest = request.ConversationRequest;
                        log.Trace("Conversation request type is {0}.", conversationRequest.RequestTypeCase);
                        if (conversationRequest.Signature.Length > 0)
                        {
                            log.Trace("Conversation signature is '{0}'.", conversationRequest.Signature.ToByteArray().ToHex());
                        }
                        else
                        {
                            log.Trace("No signature provided.");
                        }

                        switch (conversationRequest.RequestTypeCase)
                        {
                        case ConversationRequest.RequestTypeOneofCase.Start:
                            responseMessage = ProcessMessageStartConversationRequest(client, incomingMessage);
                            break;

                        case ConversationRequest.RequestTypeOneofCase.VerifyIdentity:
                            responseMessage = ProcessMessageVerifyIdentityRequest(client, incomingMessage);
                            break;

                        default:
                            log.Warn("Invalid request type '{0}'.", conversationRequest.RequestTypeCase);
                            // Connection will be closed in ReceiveMessageLoop.
                            break;
                        }

                        break;
                    }

                    default:
                        log.Error("Unknown conversation type '{0}'.", request.ConversationTypeCase);
                        // Connection will be closed in ReceiveMessageLoop.
                        break;
                    }

                    if (responseMessage != null)
                    {
                        // Send response to client.
                        res = await client.SendMessageAsync(responseMessage);

                        if (res)
                        {
                            // If the message was sent successfully to the target, we close the connection in case it was a protocol violation error response.
                            if (responseMessage.MessageTypeCase == Message.MessageTypeOneofCase.Response)
                            {
                                res = responseMessage.Response.Status != Status.ErrorProtocolViolation;
                            }
                        }
                    }
                    else
                    {
                        // If there is no response to send immediately to the client,
                        // we want to keep the connection open.
                        res = true;
                    }
                    break;
                }

                case Message.MessageTypeOneofCase.Response:
                {
                    Response response = incomingMessage.Response;
                    log.Trace("Response status is {0}, details are '{1}', conversation type is {2}.", response.Status, response.Details, response.ConversationTypeCase);

                    // Find associated request. If it does not exist, disconnect the client as it
                    // send a response without receiving a request. This is protocol violation,
                    // but as this is a reponse, we have no how to inform the client about it,
                    // so we just disconnect it.
                    UnfinishedRequest unfinishedRequest = client.GetAndRemoveUnfinishedRequest(incomingMessage.Id);
                    if ((unfinishedRequest != null) && (unfinishedRequest.RequestMessage != null))
                    {
                        ProxProtocolMessage requestMessage = (ProxProtocolMessage)unfinishedRequest.RequestMessage;
                        Request             request        = requestMessage.Request;
                        // We now check whether the response message type corresponds with the request type.
                        // This is only valid if the status is Ok. If the message types do not match, we disconnect
                        // for the protocol violation again.
                        bool typeMatch       = false;
                        bool isErrorResponse = response.Status != Status.Ok;
                        if (!isErrorResponse)
                        {
                            if (response.ConversationTypeCase == Response.ConversationTypeOneofCase.SingleResponse)
                            {
                                typeMatch = (request.ConversationTypeCase == Request.ConversationTypeOneofCase.SingleRequest) &&
                                            ((int)response.SingleResponse.ResponseTypeCase == (int)request.SingleRequest.RequestTypeCase);
                            }
                            else
                            {
                                typeMatch = (request.ConversationTypeCase == Request.ConversationTypeOneofCase.ConversationRequest) &&
                                            ((int)response.ConversationResponse.ResponseTypeCase == (int)request.ConversationRequest.RequestTypeCase);
                            }
                        }
                        else
                        {
                            typeMatch = true;
                        }

                        if (typeMatch)
                        {
                            // Now we know the types match, so we can rely on request type even if response is just an error.
                            switch (request.ConversationTypeCase)
                            {
                            case Request.ConversationTypeOneofCase.SingleRequest:
                            {
                                SingleRequest singleRequest = request.SingleRequest;
                                switch (singleRequest.RequestTypeCase)
                                {
                                default:
                                    log.Warn("Invalid conversation type '{0}' of the corresponding request.", request.ConversationTypeCase);
                                    // Connection will be closed in ReceiveMessageLoop.
                                    break;
                                }

                                break;
                            }

                            case Request.ConversationTypeOneofCase.ConversationRequest:
                            {
                                ConversationRequest conversationRequest = request.ConversationRequest;
                                switch (conversationRequest.RequestTypeCase)
                                {
                                default:
                                    log.Warn("Invalid type '{0}' of the corresponding request.", conversationRequest.RequestTypeCase);
                                    // Connection will be closed in ReceiveMessageLoop.
                                    break;
                                }
                                break;
                            }

                            default:
                                log.Error("Unknown conversation type '{0}' of the corresponding request.", request.ConversationTypeCase);
                                // Connection will be closed in ReceiveMessageLoop.
                                break;
                            }
                        }
                        else
                        {
                            log.Warn("Message type of the response ID {0} does not match the message type of the request ID {1}, the connection will be closed.", incomingMessage.Id, unfinishedRequest.RequestMessage.Id);
                            // Connection will be closed in ReceiveMessageLoop.
                        }
                    }
                    else
                    {
                        log.Warn("No unfinished request found for incoming response ID {0}, the connection will be closed.", incomingMessage.Id);
                        // Connection will be closed in ReceiveMessageLoop.
                    }

                    break;
                }

                default:
                    log.Error("Unknown message type '{0}', connection to the client will be closed.", incomingMessage.MessageTypeCase);
                    await SendProtocolViolation(client);

                    // Connection will be closed in ReceiveMessageLoop.
                    break;
                }
            }
            catch (Exception e)
            {
                log.Error("Exception occurred, connection to the client will be closed: {0}", e.ToString());
                await SendProtocolViolation(client);

                // Connection will be closed in ReceiveMessageLoop.
            }

            if (res && client.ForceDisconnect)
            {
                log.Debug("Connection to the client will be forcefully closed.");
                res = false;
            }

            log.Debug("(-):{0}", res);
            return(res);
        }
        private void OnGUI()
        {
            if (shouldSetStyles)
            {
                SetStyles();
                shouldSetStyles = false;
            }

            var guiColor = GUI.color;

            GUILayout.Label("Versioning", bigLabelCenter);
            GUILayout.BeginHorizontal();
            GUI.color = Color.yellow;
            GUILayout.Label($"Current {version}", labelRight);
            GUILayout.Label($"(Committed {DlogVersion.Version.GetValue()})", label);
            GUILayout.EndHorizontal();
            GUILayout.BeginHorizontal();
            GUILayout.FlexibleSpace();
            GUI.enabled = version != DlogVersion.Version.GetValue();
            if (GUILayout.Button("Reset", smallButton))
            {
                version = DlogVersion.Version.GetValue();
            }

            if (GUILayout.Button("Commit", smallButton))
            {
                CommitVersion();
            }

            GUI.enabled = true;
            GUILayout.FlexibleSpace();
            GUI.color = guiColor;
            GUILayout.EndHorizontal();
            GUILayout.BeginHorizontal(GUILayout.MaxHeight(32));
            GUI.backgroundColor = Color.red;
            GUILayout.Space(32);
            GUILayoutHelper.CenterVertically(() => {
                if (GUILayout.Button("Bump MAJOR"))
                {
                    version.BumpMajor();
                }
            });
            GUILayoutHelper.CenterVertically(() => {
                if (GUILayout.Button("Bump MINOR"))
                {
                    version.BumpMinor();
                }
            });
            GUILayoutHelper.CenterVertically(() => {
                if (GUILayout.Button("Bump PATCH"))
                {
                    version.BumpPatch();
                }
            });
            GUILayout.Space(32);
            GUI.backgroundColor = guiColor;
            GUILayout.EndHorizontal();
            GUILayout.BeginHorizontal(GUILayout.MaxHeight(32));
            GUILayout.Space(32);
            GUILayoutHelper.CenterVertically(() => { GUILayout.Label("(Enter Manually) Version", labelRight); });
            GUILayoutHelper.CenterVertically(() => { enteredVersion = GUILayout.TextField(enteredVersion); });
            if (!SemVer.IsValid(enteredVersion))
            {
                GUI.enabled = false;
            }
            GUILayoutHelper.CenterVertically(() => {
                if (GUILayout.Button("Set", button))
                {
                    version = (SemVer)enteredVersion;
                }
            });
            GUI.enabled = true;
            GUILayout.Space(32);
            GUILayout.EndHorizontal();
        }
예제 #4
0
        /// <summary>
        /// Processing of a message received from a client.
        /// </summary>
        /// <param name="Client">TCP client who send the message.</param>
        /// <param name="IncomingMessage">Full ProtoBuf message to be processed.</param>
        /// <returns>true if the conversation with the client should continue, false if a protocol violation error occurred and the client should be disconnected.</returns>
        public async Task <bool> ProcessMessageAsync(IncomingClient Client, Message IncomingMessage)
        {
            MessageBuilder messageBuilder = Client.MessageBuilder;

            bool res = false;

            log.Debug("()");

            try
            {
                log.Trace("Received message type is {0}, message ID is {1}.", IncomingMessage.MessageTypeCase, IncomingMessage.Id);
                switch (IncomingMessage.MessageTypeCase)
                {
                case Message.MessageTypeOneofCase.Request:
                {
                    Message responseMessage = messageBuilder.CreateErrorProtocolViolationResponse(IncomingMessage);
                    Request request         = IncomingMessage.Request;
                    log.Trace("Request conversation type is {0}.", request.ConversationTypeCase);
                    switch (request.ConversationTypeCase)
                    {
                    case Request.ConversationTypeOneofCase.SingleRequest:
                    {
                        SingleRequest singleRequest = request.SingleRequest;
                        SemVer        version       = new SemVer(singleRequest.Version);
                        log.Trace("Single request type is {0}, version is {1}.", singleRequest.RequestTypeCase, version);

                        if (!version.IsValid())
                        {
                            responseMessage.Response.Details = "version";
                            break;
                        }

                        switch (singleRequest.RequestTypeCase)
                        {
                        case SingleRequest.RequestTypeOneofCase.Ping:
                            responseMessage = ProcessMessagePingRequest(Client, IncomingMessage);
                            break;

                        case SingleRequest.RequestTypeOneofCase.ListRoles:
                            responseMessage = ProcessMessageListRolesRequest(Client, IncomingMessage);
                            break;

                        default:
                            log.Warn("Invalid request type '{0}'.", singleRequest.RequestTypeCase);
                            break;
                        }

                        break;
                    }

                    case Request.ConversationTypeOneofCase.ConversationRequest:
                    {
                        ConversationRequest conversationRequest = request.ConversationRequest;
                        log.Trace("Conversation request type is {0}.", conversationRequest.RequestTypeCase);
                        if (conversationRequest.Signature.Length > 0)
                        {
                            log.Trace("Conversation signature is '{0}'.", Crypto.ToHex(conversationRequest.Signature.ToByteArray()));
                        }
                        else
                        {
                            log.Trace("No signature provided.");
                        }

                        switch (conversationRequest.RequestTypeCase)
                        {
                        case ConversationRequest.RequestTypeOneofCase.Start:
                            responseMessage = ProcessMessageStartConversationRequest(Client, IncomingMessage);
                            break;

                        case ConversationRequest.RequestTypeOneofCase.VerifyIdentity:
                            responseMessage = ProcessMessageVerifyIdentityRequest(Client, IncomingMessage);
                            break;

                        case ConversationRequest.RequestTypeOneofCase.StartNeighborhoodInitialization:
                            responseMessage = ProcessMessageStartNeighborhoodInitializationRequest(Client, IncomingMessage);
                            break;

                        case ConversationRequest.RequestTypeOneofCase.FinishNeighborhoodInitialization:
                            responseMessage = ProcessMessageFinishNeighborhoodInitializationRequest(Client, IncomingMessage);
                            break;

                        case ConversationRequest.RequestTypeOneofCase.NeighborhoodSharedProfileUpdate:
                            responseMessage = ProcessMessageNeighborhoodSharedProfileUpdateRequest(Client, IncomingMessage);
                            break;

                        case ConversationRequest.RequestTypeOneofCase.StopNeighborhoodUpdates:
                            responseMessage = ProcessMessageStopNeighborhoodUpdatesRequest(Client, IncomingMessage);
                            break;

                        default:
                            log.Warn("Invalid request type '{0}'.", conversationRequest.RequestTypeCase);
                            // Connection will be closed in ReceiveMessageLoop.
                            break;
                        }

                        break;
                    }

                    default:
                        log.Error("Unknown conversation type '{0}'.", request.ConversationTypeCase);
                        // Connection will be closed in ReceiveMessageLoop.
                        break;
                    }

                    if (responseMessage != null)
                    {
                        // Send response to client.
                        res = await Client.SendMessageAsync(responseMessage);
                    }
                    else
                    {
                        // If there is no response to send immediately to the client,
                        // we want to keep the connection open.
                        res = true;
                    }
                    break;
                }

                case Message.MessageTypeOneofCase.Response:
                {
                    Response response = IncomingMessage.Response;
                    log.Trace("Response status is {0}, details are '{1}', conversation type is {2}.", response.Status, response.Details, response.ConversationTypeCase);

                    // Find associated request. If it does not exist, disconnect the client as it
                    // send a response without receiving a request. This is protocol violation,
                    // but as this is a reponse, we have no how to inform the client about it,
                    // so we just disconnect it.
                    UnfinishedRequest unfinishedRequest = Client.GetAndRemoveUnfinishedRequest(IncomingMessage.Id);
                    if ((unfinishedRequest != null) && (unfinishedRequest.RequestMessage != null))
                    {
                        Message requestMessage = unfinishedRequest.RequestMessage;
                        Request request        = requestMessage.Request;
                        // We now check whether the response message type corresponds with the request type.
                        // This is only valid if the status is Ok. If the message types do not match, we disconnect
                        // for the protocol violation again.
                        bool typeMatch       = false;
                        bool isErrorResponse = response.Status != Status.Ok;
                        if (!isErrorResponse)
                        {
                            if (response.ConversationTypeCase == Response.ConversationTypeOneofCase.SingleResponse)
                            {
                                typeMatch = (request.ConversationTypeCase == Request.ConversationTypeOneofCase.SingleRequest) &&
                                            ((int)response.SingleResponse.ResponseTypeCase == (int)request.SingleRequest.RequestTypeCase);
                            }
                            else
                            {
                                typeMatch = (request.ConversationTypeCase == Request.ConversationTypeOneofCase.ConversationRequest) &&
                                            ((int)response.ConversationResponse.ResponseTypeCase == (int)request.ConversationRequest.RequestTypeCase);
                            }
                        }
                        else
                        {
                            typeMatch = true;
                        }

                        if (typeMatch)
                        {
                            // Now we know the types match, so we can rely on request type even if response is just an error.
                            switch (request.ConversationTypeCase)
                            {
                            case Request.ConversationTypeOneofCase.SingleRequest:
                            {
                                SingleRequest singleRequest = request.SingleRequest;
                                switch (singleRequest.RequestTypeCase)
                                {
                                default:
                                    log.Warn("Invalid conversation type '{0}' of the corresponding request.", request.ConversationTypeCase);
                                    // Connection will be closed in ReceiveMessageLoop.
                                    break;
                                }

                                break;
                            }

                            case Request.ConversationTypeOneofCase.ConversationRequest:
                            {
                                ConversationRequest conversationRequest = request.ConversationRequest;
                                switch (conversationRequest.RequestTypeCase)
                                {
                                case ConversationRequest.RequestTypeOneofCase.NeighborhoodSharedProfileUpdate:
                                    res = ProcessMessageNeighborhoodSharedProfileUpdateResponse(Client, IncomingMessage, unfinishedRequest);
                                    break;

                                case ConversationRequest.RequestTypeOneofCase.FinishNeighborhoodInitialization:
                                    res = ProcessMessageFinishNeighborhoodInitializationResponse(Client, IncomingMessage, unfinishedRequest);
                                    break;

                                default:
                                    log.Warn("Invalid type '{0}' of the corresponding request.", conversationRequest.RequestTypeCase);
                                    // Connection will be closed in ReceiveMessageLoop.
                                    break;
                                }
                                break;
                            }

                            default:
                                log.Error("Unknown conversation type '{0}' of the corresponding request.", request.ConversationTypeCase);
                                // Connection will be closed in ReceiveMessageLoop.
                                break;
                            }
                        }
                        else
                        {
                            log.Warn("Message type of the response ID {0} does not match the message type of the request ID {1}, the connection will be closed.", IncomingMessage.Id);
                            // Connection will be closed in ReceiveMessageLoop.
                        }
                    }
                    else
                    {
                        log.Warn("No unfinished request found for incoming response ID {0}, the connection will be closed.", IncomingMessage.Id);
                        // Connection will be closed in ReceiveMessageLoop.
                    }

                    break;
                }

                default:
                    log.Error("Unknown message type '{0}', connection to the client will be closed.", IncomingMessage.MessageTypeCase);
                    await SendProtocolViolation(Client);

                    // Connection will be closed in ReceiveMessageLoop.
                    break;
                }
            }
            catch (Exception e)
            {
                log.Error("Exception occurred, connection to the client will be closed: {0}", e.ToString());
                await SendProtocolViolation(Client);

                // Connection will be closed in ReceiveMessageLoop.
            }

            if (res && Client.ForceDisconnect)
            {
                log.Debug("Connection to the client will be forcefully closed.");
                res = false;
            }

            profileServer.AddMessage(IncomingMessage, Client.ServerRole, res ? Client : null);

            log.Debug("(-):{0}", res);
            return(res);
        }