Пример #1
0
        public bool Test(CommandSettings settings, string targetUrl)
        {
            SystemSettingsSwitcherSettings systemSettingsSwitcherSettings = settings.SystemSettingsSwitcher;
            bool       backup         = systemSettingsSwitcherSettings.EnableSystemSettingsSwitch;
            string     logLevelLog    = string.Empty;
            TraceLevel backupLogLevel = Logger.LogLevel;
            TraceLevel tempLogLevel   = backupLogLevel;

            if (tempLogLevel < TraceLevel.Info)
            {
                tempLogLevel = TraceLevel.Info;
                logLevelLog  = $" The LogLevel is changed to '{tempLogLevel}' temporarily.";
            }

            // test suppressing system settings switch
            try {
                LogStart($"Start a test connection to '{targetUrl}'.{logLevelLog}");
                Logger.LogLevel = tempLogLevel;
                systemSettingsSwitcherSettings.EnableSystemSettingsSwitch = false;

                using (RunningProxyState proxyState = StartProxy(settings, saveCredentials: false, checkPreviousBackup: false)) {
                    // In SystemSettingsSwitcher.TestWebProxy() called from StartProxy() above,
                    // MAPE already tested connectivity to the targetUrl with the actual proxy.
                    // In this test, on the other hand, connectivity is tested with the MAPE main listener as the proxy.
                    IPEndPoint       proxyEndPoint = ListenerSettings.GetEndPoint(settings.Proxy.MainListener);
                    WebClientForTest webClient     = new WebClientForTest();
                    webClient.Timeout = 180 * 1000; // 3 minutes
                    webClient.Proxy   = new WebProxy(proxyEndPoint.Address.ToString(), proxyEndPoint.Port);

                    webClient.DownloadData(targetUrl);                      // an exception is thrown on error

                    // wait for stop for 3 seconds
                    proxyState.Stop(systemSessionEnding: false, millisecondsTimeout: 3000);
                }
            } finally {
                systemSettingsSwitcherSettings.EnableSystemSettingsSwitch = backup;
                Logger.LogLevel = backupLogLevel;
                LogStop($"Stop the test connection.");
            }

            return(true);
        }
Пример #2
0
        protected override void RunProxyImpl(CommandSettings settings)
        {
            // argument checks
            Debug.Assert(settings != null);

            using (ControllerThreadSynchronizer synchronizer = new ControllerThreadSynchronizer()) {
                // prepare Ctrl+C handler
                ConsoleCancelEventHandler onCtrlC = (o, e) => {
                    e.Cancel = true;
                    AwakeControllerThread(ControllerThreadSynchronizer.EventKind.Quit);
                };

                // connect Ctrl+C handler
                Console.CancelKeyPress += onCtrlC;
                try {
                    ControllerThreadSynchronizer.EventKind eventKind = ControllerThreadSynchronizer.EventKind.None;
                    do
                    {
                        Debug.Assert(eventKind == ControllerThreadSynchronizer.EventKind.None || eventKind == ControllerThreadSynchronizer.EventKind.Resume);

                        // run the proxy
                        bool completed = false;
                        using (RunningProxyState runningProxyState = StartProxy(settings, saveCredentials: true, checkPreviousBackup: false)) {
                            // log & message
                            LogProxyStarted(eventKind == ControllerThreadSynchronizer.EventKind.Resume);
                            Console.WriteLine(Resources.CLICommandBase_Message_StartListening);
                            Console.WriteLine(Resources.CLICommandBase_Message_StartingNote);

                            // wait for Ctrl+C or other events
                            RegisterControllerThreadSynchronizer(synchronizer);
                            eventKind = synchronizer.WaitForEvent();

                            // stop the proxy
                            completed = runningProxyState.Stop(eventKind == ControllerThreadSynchronizer.EventKind.SystemSessionEnding, 5000);
                        }
                        LogProxyStopped(completed, eventKind == ControllerThreadSynchronizer.EventKind.Suspend);
                        Console.WriteLine(completed ? Resources.CLICommandBase_Message_Completed : Resources.CLICommandBase_Message_NotCompleted);

                        // resume the thread which originally accepts the event
                        synchronizer.NotifyEventHandledAndWaitForAcknowledgment();

                        // decide the next step
                        while (eventKind == ControllerThreadSynchronizer.EventKind.Suspend)
                        {
                            // wait for the next event
                            RegisterControllerThreadSynchronizer(synchronizer);
                            eventKind = synchronizer.WaitForEvent();
                            synchronizer.NotifyEventHandledAndWaitForAcknowledgment();
                        }
                        if (eventKind != ControllerThreadSynchronizer.EventKind.Resume)
                        {
                            // quit
                            Debug.Assert(eventKind == ControllerThreadSynchronizer.EventKind.Quit || eventKind == ControllerThreadSynchronizer.EventKind.SystemSessionEnding);
                            break;
                        }
                    } while (true);
                } finally {
                    // disconnect Ctrl+C handler
                    Console.CancelKeyPress -= onCtrlC;
                }
            }

            return;
        }