Esempio n. 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);
        }
Esempio n. 2
0
        protected RunningProxyState StartProxy(CommandSettings settings, bool saveCredentials, bool checkPreviousBackup)
        {
            // argument checks
            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }
            Debug.Assert(settings.SystemSettingsSwitcher != null);
            Debug.Assert(settings.Proxy != null);

            // create a RunningProxyState and start the proxy
            RunningProxyState state = new RunningProxyState(this);

            try {
                state.Start(settings, saveCredentials, checkPreviousBackup);
            } catch (Exception exception) {
                state.Dispose();
                LogError(exception.Message);
                throw;
            }

            return(state);
        }
Esempio n. 3
0
        public void StartProxy(bool forScheduled = false)
        {
            // start proxy
            bool resuming = false;

            lock (this) {
                // state checks
                Starter starter = this.starter;
                if (forScheduled && starter == null)
                {
                    // start only the scheduled proxying
                    return;
                }
                if (this.runningProxyState != null)
                {
                    ClearStarter();                     // no use if proxying is running
                    return;
                }
                if (starter != null)
                {
                    resuming = starter.Resuming;
                }

                // start
                // Note that the starter won't be cleared if StartProxy() throws an exception.
                this.runningProxyState = StartProxy(this.settings, saveCredentials: true, checkPreviousBackup: true);
                ClearStarter();
            }

            // notify
            OnProxyStateChanged();

            // log
            LogProxyStarted(resuming);

            return;
        }
Esempio n. 4
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;
        }