Example #1
0
        /// <summary>
        /// Handles a received client message.
        /// </summary>
        /// <param name="connection">
        /// The connection over which the message was received.
        /// </param>
        /// <param name="message">
        /// The client's message to us.
        /// </param>
        private void OnClientMessage(IPipeServer server, BaseMessage message)
        {
            // This is so gross, but unfortuantely we can't just switch on a type. We can come up
            // with a nice mapping system so we can do a switch, but this can wait.

            m_logger.Debug("Got IPC message from client.");

            if (m_ipcQueue.HandleMessage(message))
            {
                return;
            }

            var msgRealType = message.GetType();

            Action <BaseMessage> callback = null;

            if (m_callbacks.TryGetValue(msgRealType, out callback))
            {
                m_logger.Debug("Client message is {0}", msgRealType.Name);
                callback?.Invoke(message);
            }
            if (msgRealType == typeof(Messages.AuthenticationMessage))
            {
                m_logger.Debug("Client message is {0}", nameof(Messages.AuthenticationMessage));
                var cast = (Messages.AuthenticationMessage)message;

                if (cast != null)
                {
                    if (string.IsNullOrEmpty(cast.Username) || string.IsNullOrWhiteSpace(cast.Username))
                    {
                        PushMessage(new AuthenticationMessage(AuthenticationAction.InvalidInput));
                        return;
                    }

                    if (cast.Password == null || cast.Password.Length <= 0)
                    {
                        PushMessage(new AuthenticationMessage(AuthenticationAction.InvalidInput));
                        return;
                    }

                    var args = new AuthenticationRequestArgs(cast);

                    AttemptAuthentication?.Invoke(args);
                }
            }
            else if (msgRealType == typeof(Messages.DeactivationMessage))
            {
                m_logger.Debug("Client message is {0}", nameof(Messages.DeactivationMessage));

                var cast = (Messages.DeactivationMessage)message;

                if (cast != null && cast.Command == DeactivationCommand.Requested)
                {
                    var args = new DeactivationRequestEventArgs();

                    // This fills args.DeactivationCommand.
                    DeactivationRequested?.Invoke(args);

                    PushMessage(new DeactivationMessage(args.DeactivationCommand));
                }
            }
            else if (msgRealType == typeof(Messages.RelaxedPolicyMessage))
            {
                m_logger.Debug("Client message is {0}", nameof(Messages.RelaxedPolicyMessage));

                var cast = (Messages.RelaxedPolicyMessage)message;

                if (cast != null)
                {
                    var args = new RelaxedPolicyEventArgs(cast);
                    RelaxedPolicyRequested?.Invoke(args);
                }
            }
            else if (msgRealType == typeof(Messages.ClientToClientMessage))
            {
                m_logger.Debug("Client message is {0}", nameof(Messages.ClientToClientMessage));

                var cast = (Messages.ClientToClientMessage)message;

                if (cast != null)
                {
                    // Just relay this message to all clients.
                    PushMessage(cast);
                }
            }
            else if (msgRealType == typeof(Messages.FilterStatusMessage))
            {
                m_logger.Debug("Client message is {0}", nameof(Messages.FilterStatusMessage));

                var cast = (Messages.FilterStatusMessage)message;

                if (cast != null)
                {
                    ClientServerStateQueried?.Invoke(new StateChangeEventArgs(cast));
                }
            }
            else if (msgRealType == typeof(Messages.ClientUpdateResponseMessage))
            {
                m_logger.Debug("Client message is {0}", nameof(Messages.ClientUpdateResponseMessage));

                var cast = (Messages.ClientUpdateResponseMessage)message;

                if (cast != null)
                {
                    if (cast.Accepted)
                    {
                        m_logger.Debug("Client has accepted update.");
                        ClientAcceptedPendingUpdate?.Invoke();
                    }
                }
            }
            else if (msgRealType == typeof(Messages.BlockActionReviewRequestMessage))
            {
                m_logger.Debug("Client message is {0}", nameof(Messages.BlockActionReviewRequestMessage));

                var cast = (Messages.BlockActionReviewRequestMessage)message;

                if (cast != null)
                {
                    Uri output;
                    if (Uri.TryCreate(cast.FullRequestUrl, UriKind.Absolute, out output))
                    {
                        // Here we'll just recycle the block action message and handler.
                        ClientRequestsBlockActionReview?.Invoke(new NotifyBlockActionMessage(BlockType.OtherContentClassification, output, string.Empty, cast.CategoryName, DateTime.Now));
                    }
                    else
                    {
                        m_logger.Info("Failed to create absolute URI for string \"{0}\".", cast.FullRequestUrl);
                    }
                }
            }
            else if (msgRealType == typeof(Messages.CaptivePortalDetectionMessage))
            {
                m_logger.Debug("Server message is {0}", nameof(Messages.CaptivePortalDetectionMessage));
                var cast = (Messages.CaptivePortalDetectionMessage)message;
                if (cast != null)
                {
                    RequestCaptivePortalDetection?.Invoke(cast);
                }
            }
            else if (msgRealType == typeof(Messages.CertificateExemptionMessage))
            {
                m_logger.Debug("Server message is {0}", nameof(Messages.CertificateExemptionMessage));
                var cast = (Messages.CertificateExemptionMessage)message;
                if (cast != null)
                {
                    this.OnCertificateExemptionGranted?.Invoke(new CertificateExemptionEventArgs(cast));
                }
            }
            else if (msgRealType == typeof(Messages.DiagnosticsMessage))
            {
                m_logger.Debug("Server message is {0}", nameof(Messages.DiagnosticsMessage));
                var cast = (Messages.DiagnosticsMessage)message;
                if (cast != null)
                {
                    this.OnDiagnosticsEnable?.Invoke(cast);
                }
            }
            else
            {
                // Unknown type.
            }
        }
        private void OnServerMessage(NamedPipeConnection <BaseMessage, BaseMessage> connection, BaseMessage message)
        {
            // This is so gross, but unfortuantely we can't just switch on a type.
            // We can come up with a nice mapping system so we can do a switch,
            // but this can wait.

            if (Default.m_ipcQueue.HandleMessage(message))
            {
                return;
            }

            if (m_ipcQueue.HandleMessage(message))
            {
                return;
            }

            m_logger.Debug("Got IPC message from server.");

            var msgRealType = message.GetType();

            if (msgRealType == typeof(AuthenticationMessage))
            {
                AuthMessage = (AuthenticationMessage)message;
                m_logger.Debug("Server message is {0}", nameof(AuthenticationMessage));
                var cast = (AuthenticationMessage)message;
                if (cast != null)
                {
                    AuthenticationResultReceived?.Invoke(cast);
                }
            }
            else if (msgRealType == typeof(Messages.DeactivationMessage))
            {
                m_logger.Debug("Server message is {0}", nameof(Messages.DeactivationMessage));
                var cast = (Messages.DeactivationMessage)message;
                if (cast != null)
                {
                    DeactivationResultReceived?.Invoke(cast.Command);
                }
            }
            else if (msgRealType == typeof(Messages.FilterStatusMessage))
            {
                m_logger.Debug("Server message is {0}", nameof(Messages.FilterStatusMessage));
                var cast = (Messages.FilterStatusMessage)message;
                if (cast != null)
                {
                    StateChanged?.Invoke(new StateChangeEventArgs(cast));
                }
            }
            else if (msgRealType == typeof(Messages.NotifyBlockActionMessage))
            {
                m_logger.Debug("Server message is {0}", nameof(Messages.NotifyBlockActionMessage));
                var cast = (Messages.NotifyBlockActionMessage)message;
                if (cast != null)
                {
                    BlockActionReceived?.Invoke(cast);
                }
            }
            else if (msgRealType == typeof(Messages.RelaxedPolicyMessage))
            {
                m_logger.Debug("Server message is {0}", nameof(Messages.RelaxedPolicyMessage));
                var cast = (Messages.RelaxedPolicyMessage)message;
                if (cast != null)
                {
                    switch (cast.Command)
                    {
                    case RelaxedPolicyCommand.Info:
                    {
                        RelaxedPolicyInfoReceived?.Invoke(cast);
                    }
                    break;

                    case RelaxedPolicyCommand.Expired:
                    {
                        RelaxedPolicyExpired?.Invoke();
                    }
                    break;
                    }
                }
            }
            else if (msgRealType == typeof(Messages.ClientToClientMessage))
            {
                m_logger.Debug("Server message is {0}", nameof(Messages.ClientToClientMessage));
                var cast = (Messages.ClientToClientMessage)message;
                if (cast != null)
                {
                    ClientToClientCommandReceived?.Invoke(cast);
                }
            }
            else if (msgRealType == typeof(Messages.ServerUpdateQueryMessage))
            {
                m_logger.Debug("Server message is {0}", nameof(Messages.ServerUpdateQueryMessage));
                var cast = (Messages.ServerUpdateQueryMessage)message;
                if (cast != null)
                {
                    ServerAppUpdateRequestReceived?.Invoke(cast);
                }
            }
            else if (msgRealType == typeof(Messages.ServerUpdateNotificationMessage))
            {
                m_logger.Debug("Server message is {0}", nameof(Messages.ServerUpdateNotificationMessage));
                var cast = (Messages.ServerUpdateNotificationMessage)message;
                if (cast != null)
                {
                    ServerUpdateStarting?.Invoke();
                }
            }
            else if (msgRealType == typeof(Messages.CaptivePortalDetectionMessage))
            {
                m_logger.Debug("Server message is {0}", nameof(Messages.CaptivePortalDetectionMessage));
                var cast = (Messages.CaptivePortalDetectionMessage)message;
                if (cast != null)
                {
                    CaptivePortalDetectionReceived?.Invoke(cast);
                }
            }
            else
            {
                // Unknown type.
            }
        }
Example #3
0
        protected void OnServerMessage(BaseMessage message)
        {
            // This is so gross, but unfortuantely we can't just switch on a type.
            // We can come up with a nice mapping system so we can do a switch,
            // but this can wait.

            if (Default.ipcQueue.HandleMessage(message))
            {
                return;
            }

            if (ipcQueue.HandleMessage(message))
            {
                return;
            }

            logger.Debug("Got IPC message from server.");

            var msgRealType = message.GetType();

            Action <BaseMessage> callback = null;

            if (m_callbacks.TryGetValue(msgRealType, out callback))
            {
                logger.Debug("Server message is {0}", msgRealType.Name);
                callback?.Invoke(message);
            }
            else if (msgRealType == typeof(AuthenticationMessage))
            {
                AuthMessage = (AuthenticationMessage)message;
                logger.Debug("Server message is {0}", nameof(AuthenticationMessage));
                var cast = (AuthenticationMessage)message;
                if (cast != null)
                {
                    AuthenticationResultReceived?.Invoke(cast);
                }
            }
            else if (msgRealType == typeof(Messages.DeactivationMessage))
            {
                logger.Debug("Server message is {0}", nameof(Messages.DeactivationMessage));
                var cast = (Messages.DeactivationMessage)message;
                if (cast != null)
                {
                    DeactivationResultReceived?.Invoke(cast.Command);
                }
            }
            else if (msgRealType == typeof(Messages.FilterStatusMessage))
            {
                logger.Debug("Server message is {0}", nameof(Messages.FilterStatusMessage));
                var cast = (Messages.FilterStatusMessage)message;
                if (cast != null)
                {
                    StateChanged?.Invoke(new StateChangeEventArgs(cast));
                }
            }
            else if (msgRealType == typeof(Messages.NotifyBlockActionMessage))
            {
                logger.Debug("Server message is {0}", nameof(Messages.NotifyBlockActionMessage));
                var cast = (Messages.NotifyBlockActionMessage)message;
                if (cast != null)
                {
                    BlockActionReceived?.Invoke(cast);
                }
            }
            else if (msgRealType == typeof(Messages.RelaxedPolicyMessage))
            {
                logger.Debug("Server message is {0}", nameof(Messages.RelaxedPolicyMessage));
                var cast = (Messages.RelaxedPolicyMessage)message;
                if (cast != null)
                {
                    switch (cast.Command)
                    {
                    case RelaxedPolicyCommand.Info:
                    {
                        RelaxedPolicyInfoReceived?.Invoke(cast);
                    }
                    break;

                    case RelaxedPolicyCommand.Expired:
                    {
                        RelaxedPolicyExpired?.Invoke();
                    }
                    break;
                    }
                }
            }
            else if (msgRealType == typeof(Messages.ClientToClientMessage))
            {
                logger.Debug("Server message is {0}", nameof(Messages.ClientToClientMessage));
                var cast = (Messages.ClientToClientMessage)message;
                if (cast != null)
                {
                    ClientToClientCommandReceived?.Invoke(cast);
                }
            }
            else if (msgRealType == typeof(Messages.ServerUpdateQueryMessage))
            {
                logger.Debug("Server message is {0}", nameof(Messages.ServerUpdateQueryMessage));
                var cast = (Messages.ServerUpdateQueryMessage)message;
                if (cast != null)
                {
                    ServerAppUpdateRequestReceived?.Invoke(cast);
                }
            }
            else if (msgRealType == typeof(Messages.CaptivePortalDetectionMessage))
            {
                logger.Debug("Server message is {0}", nameof(Messages.CaptivePortalDetectionMessage));
                var cast = (Messages.CaptivePortalDetectionMessage)message;
                if (cast != null)
                {
                    CaptivePortalDetectionReceived?.Invoke(cast);
                }
            }
            else if (msgRealType == typeof(Messages.CertificateExemptionMessage))
            {
                logger.Debug("Server message is {0}", nameof(Messages.CertificateExemptionMessage));
                var cast = (Messages.CertificateExemptionMessage)message;
                if (cast != null)
                {
                    AddCertificateExemptionRequest?.Invoke(cast);
                }
            }
            else if (msgRealType == typeof(Messages.DiagnosticsInfoMessage))
            {
                logger.Debug("Server message is {0}", nameof(Messages.DiagnosticsInfoMessage));
                var cast = (Messages.DiagnosticsInfoMessage)message;
                if (cast != null)
                {
                    OnDiagnosticsInfo?.Invoke(cast);
                }
            }
            else
            {
                // Unknown type.
                logger.Info("Unknown type is {0}", msgRealType.Name);
            }
        }