private string GetListenerEndpoint(ListenerSettings listenerSettings) { // listenerSettings can be null IPEndPoint endPoint = ListenerSettings.GetEndPoint(listenerSettings); return($"{endPoint.Address}:{endPoint.Port}"); }
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); }