Ejemplo n.º 1
0
        /// <summary>
        /// Main entry point for handling a Fiddler operation
        /// </summary>
        /// <param name="portal">DevicePortal reference for communicating with the device.</param>
        /// <param name="parameters">Parsed command line parameters.</param>
        public static void HandleOperation(DevicePortal portal, ParameterHelper parameters)
        {
            if (parameters.HasFlag(ParameterHelper.HelpFlag))
            {
                Console.WriteLine(FiddlerUsageMessage);
                return;
            }

            string state = parameters.GetParameterValue("state");

            if (string.IsNullOrEmpty(state))
            {
                Console.WriteLine("/state parameter is required.");
                Console.WriteLine();
                Console.WriteLine(FiddlerUsageMessage);
                return;
            }

            try
            {
                if (string.Equals(state, "on", StringComparison.OrdinalIgnoreCase))
                {
                    string proxyAddress = parameters.GetParameterValue("proxyaddress");
                    string proxyPort    = parameters.GetParameterValue("proxyport");

                    if (string.IsNullOrEmpty(proxyAddress) || string.IsNullOrEmpty(proxyPort))
                    {
                        Console.WriteLine("/proxyaddress and /proxyport are required for enabling Fiddler.");
                        Console.WriteLine();
                        Console.WriteLine(FiddlerUsageMessage);
                        return;
                    }

                    Task fiddlerEnableTask = portal.EnableFiddlerTracingAsync(proxyAddress, proxyPort, parameters.GetParameterValue("certpath"));
                    fiddlerEnableTask.Wait();
                    Console.WriteLine("Fiddler enabled.");
                }
                else if (string.Equals(state, "off", StringComparison.OrdinalIgnoreCase))
                {
                    Task fiddlerDisableTask = portal.DisableFiddlerTracingAsync();
                    fiddlerDisableTask.Wait();
                    Console.WriteLine("Fiddler disabled.");
                }
                else
                {
                    Console.WriteLine("Unknown state parameter: {0}. Must be 'on' or 'off'.", state);
                    Console.WriteLine();
                    Console.WriteLine(FiddlerUsageMessage);
                    return;
                }

                if (parameters.HasFlag("reboot"))
                {
                    Task rebootTask = portal.RebootAsync();
                    rebootTask.Wait();
                    Console.WriteLine("Console rebooting...");
                }
                else
                {
                    Console.WriteLine("A reboot is required before this takes effect.");
                }
            }
            catch (AggregateException e)
            {
                if (e.InnerException is DevicePortalException)
                {
                    DevicePortalException innerException = e.InnerException as DevicePortalException;

                    Console.WriteLine(string.Format("Exception encountered: {0}, hr = 0x{1:X} : {2}", innerException.StatusCode, innerException.HResult, innerException.Reason));
                }
                else
                {
                    Console.WriteLine(string.Format("Unexpected exception encountered: {0}", e.Message));
                }

                return;
            }
        }