Example #1
0
        public virtual bool TestWebProxy(IActualProxy actualProxy)
        {
            // get test url specified in application config file
            // (not in the settings file because this information is supposed to be set for site)
            string targetUrl = SystemSettingsSwitcher.GetProxyTestUrl();

            Debug.Assert(string.IsNullOrEmpty(targetUrl) == false);
            Uri         target         = new Uri(targetUrl);
            DnsEndPoint targetEndPoint = new DnsEndPoint(target.Host, target.Port);

            WebClientForTest webClient = new WebClientForTest();

            webClient.Timeout = 10 * 1000;      // 10 seconds

            // test each proxy candidate
            CommandBase owner  = this.Owner;
            bool        result = false;
            IReadOnlyCollection <DnsEndPoint> endPoints = actualProxy.GetProxyEndPoints(target);

            if (endPoints != null)
            {
                foreach (DnsEndPoint endPoint in endPoints)
                {
                    owner.LogVerbose($"ActualProxy check: to {endPoint.Host}:{endPoint.Port}");
                    try {
                        webClient.Proxy = new WebProxy(endPoint.Host, endPoint.Port);
                        webClient.DownloadData(targetUrl);                          // an exception is thrown on error
                        result = true;
                    } catch (WebException exception) {
                        // Note that a protocol error indicates that the end point exists
                        result = (exception.Status == WebExceptionStatus.ProtocolError);
                        if (result)
                        {
                            owner.LogVerbose($"ActualProxy check: {exception.Status} -> OK");
                        }
                        else
                        {
                            owner.LogError($"ActualProxy check: {exception.Status} -> NG");
                        }
                    }
                    if (result)
                    {
                        // a valid proxy is found
                        break;
                    }
                }
            }
            if (result)
            {
                owner.LogVerbose("ActualProxy check: OK - there is a valid actual proxy.");
            }
            else
            {
                owner.LogError("ActualProxy check: NG - no valid proxy.");
            }

            return(result);
        }
Example #2
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);
        }