/// <summary> /// /// </summary> /// <param name="owner"></param> /// <param name="settings"></param> public SystemSettingsSwitcher(CommandBase owner, SystemSettingsSwitcherSettings settings) { // argument checks if (owner == null) { throw new ArgumentNullException(nameof(owner)); } // settings can be null // initialize members this.Owner = owner; bool enabled; string configurationScript; DnsEndPoint actualProxyEndPoint; if (settings == null) { // simple initialization (ex. to restore only) enabled = true; configurationScript = null; actualProxyEndPoint = null; } else { // usual initialization enabled = settings.EnableSystemSettingsSwitch; ActualProxySettings actualProxySettings = settings.ActualProxy; if (actualProxySettings != null) { configurationScript = actualProxySettings.ConfigurationScript; if (string.IsNullOrEmpty(configurationScript)) { actualProxyEndPoint = new DnsEndPoint(actualProxySettings.Host, actualProxySettings.Port); } else { actualProxyEndPoint = null; } } else { configurationScript = null; actualProxyEndPoint = null; } } this.Enabled = enabled; this.ActualProxyConfigurationScript = configurationScript; this.ActualProxyEndPoint = actualProxyEndPoint; return; }
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); }
public override SystemSettingsSwitcher CreateSystemSettingsSwitcher(CommandBase owner, SystemSettingsSwitcherSettings settings) { // argument checks SystemSettingsSwitcherForWindowsSettings actualSettings = null; if (settings != null) { actualSettings = settings as SystemSettingsSwitcherForWindowsSettings; if (actualSettings == null) { throw new ArgumentNullException($"It must be {nameof(SystemSettingsSwitcherForWindowsSettings)} class.", nameof(settings)); } } return(new SystemSettingsSwitcherForWindows(owner, actualSettings)); }
public void Start(CommandSettings commandSettings, bool saveCredentials, bool checkPreviousBackup) { // argument checks if (commandSettings == null) { throw new ArgumentNullException(nameof(commandSettings)); } SystemSettingsSwitcherSettings systemSettingsSwitcherSettings = commandSettings.SystemSettingsSwitcher; if (systemSettingsSwitcherSettings == null) { throw new ArgumentNullException(nameof(commandSettings.SystemSettingsSwitcher)); } ProxySettings proxySettings = commandSettings.Proxy; if (proxySettings == null) { throw new ArgumentNullException(nameof(commandSettings.Proxy)); } // state checks if (this.proxy != null) { throw new InvalidOperationException("The proxy is already started."); } Debug.Assert(this.switcher == null); Debug.Assert(this.backup == null); try { ComponentFactory componentFactory = this.Owner.ComponentFactory; // check the state of previous backup if (checkPreviousBackup) { this.Owner.CheckPreviousBackup(); } // create a system settings swither SystemSettingsSwitcher switcher = componentFactory.CreateSystemSettingsSwitcher(this.Owner, systemSettingsSwitcherSettings); this.switcher = switcher; // setup credential dictionary lock (this.credentialsLocker) { IEnumerable <CredentialSettings> credentials = commandSettings.Credentials; this.dictionary = (credentials == null)? new Dictionary <string, CredentialSettings>(): credentials.ToDictionary(c => c.EndPoint); this.isCredentialsDirty = false; } // create a proxy Proxy proxy = componentFactory.CreateProxy(proxySettings); this.proxy = proxy; // detect the current proxy IActualProxy actualProxy = switcher.GetActualProxy(); if (actualProxy == null) { // no actual proxy to which it connects throw new Exception(Resources.CommandBase_Message_NoActualProxy); } try { // log CommandBase owner = this.Owner; if (owner.ShouldLog(TraceEventType.Verbose)) { // log the actual proxy owner.LogVerbose($"ActualProxy is '{actualProxy.Description}'"); // log whether system settings is being switched string label = switcher.Enabled ? "enabled" : "disabled"; owner.LogVerbose($"SystemSettingsSwitch: {label}"); } // test actual proxy if (switcher.TestWebProxy(actualProxy) == false) { string message = string.Format(Resources.SystemSettingsSwitcher_Message_ProxyIsNotConnectable, actualProxy.Description); throw new Exception(message); } // start the proxy proxy.ActualProxy = actualProxy; actualProxy = null; // its ownership has been moved to the proxy object. proxy.Start(this); this.saveCredentials = saveCredentials; // switch system settings this.backup = switcher.Switch(proxy); if (this.backup != null) { // save backup settings owner.SaveSystemSettingsBackup(this.backup); } this.commandSettings = commandSettings; } catch { DisposableUtil.ClearDisposableObject(ref actualProxy); throw; } } catch { Stop(systemSessionEnding: false); throw; } return; }
public virtual SystemSettingsSwitcher CreateSystemSettingsSwitcher(CommandBase owner, SystemSettingsSwitcherSettings settings) { return(new SystemSettingsSwitcher(owner, settings)); }