Exemple #1
0
        public void StopProxy(bool systemSessionEnding, int millisecondsTimeout = 0, bool suspending = false)
        {
            // stop proxy
            bool completed = false;

            lock (this) {
                // state checks
                if (this.runningProxyState == null)
                {
                    return;
                }
                if (suspending)
                {
                    Debug.Assert(this.starter != null);
                    // Note that CreateStarter() returns null if proxying should not be resumed
                    this.starter = CreateStarter(resuming: true);
                }

                completed = this.runningProxyState.Stop(systemSessionEnding, millisecondsTimeout);
                DisposableUtil.ClearDisposableObject(ref this.runningProxyState);
            }

            // notify
            OnProxyStateChanged();

            // log
            LogProxyStopped(completed, suspending);

            return;
        }
            public void Dispose()
            {
                // dispose members
                DisposableUtil.ClearDisposableObject(ref this.syncEvent);

                return;
            }
Exemple #3
0
        public override void Dispose()
        {
            // ensure that it stops listening
            Stop();

            // clear members
            lock (this) {
                this.serverBasicCredentialCache = null;
                Debug.Assert(this.Runner == null);
                Debug.Assert(this.connections == null);
                DisposableUtil.ClearDisposableObject(ref this.actualProxy);
                List <Listener> temp = this.listeners;
                this.listeners = null;
                if (temp != null)
                {
                    temp.ForEach(
                        (listener) => {
                        try {
                            listener.Dispose();
                        } catch {
                            // continue
                            LogVerbose($"Fail to dispose {listener.ComponentName}.");
                        }
                    }
                        );
                    temp.Clear();
                }
            }

            return;
        }
Exemple #4
0
        public SetupContext(CommandSettings settings, SystemSettingsSwitcher switcher)
        {
            // argument checks
            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }
            if (switcher == null)
            {
                throw new ArgumentNullException(nameof(switcher));
            }

            // initialize members
            this.Settings = settings;

            // ActualProxy
            this.DefaultActualProxyHostName            = SystemSettingsSwitcher.GetDefaultActualProxyHostName();
            this.DefaultActualProxyPort                = SystemSettingsSwitcher.GetDefaultActualProxyPort();
            this.DefaultActualProxyConfigurationScript = SystemSettingsSwitcher.GetDefaultActualProxyConfigurationScript();
            IActualProxy actualProxy = switcher.DetectSystemActualProxy();

            if (actualProxy != null)
            {
                this.ProxyDetected = true;
                DisposableUtil.ClearDisposableObject(ref actualProxy);
            }
            if (this.ProxyDetected == false && settings.SystemSettingsSwitcher.ActualProxy == null)
            {
                // The authentication proxy cannot be detected automatically.
                // User must set its information explicitly.
                this.NeedActualProxy = true;
            }

            return;
        }
        public override void Dispose()
        {
            // dispose resources
            DisposableUtil.ClearDisposableObject(ref this.output);
            DisposableUtil.ClearDisposableObject(ref this.sample);

            return;
        }
Exemple #6
0
        public override void Dispose()
        {
            // dispose this class level
            this.chunkingOutput = null;
            DisposableUtil.ClearDisposableObject(ref this.bodyStream);

            // dispose the base class level
            base.Dispose();
        }
Exemple #7
0
        protected override void OnExit(ExitEventArgs e)
        {
            // process this class level tasks
            StopProxy();
            this.Command.ProxyStateChanged -= command_ProxyStateChanged;
            DisposableUtil.ClearDisposableObject(ref this.notifyIcon);

            // process the base class level tasks
            base.OnExit(e);
        }
Exemple #8
0
        public override void ResetBuffer()
        {
            // reset this class level
            this.unflushedStart = 0;
            this.chunkingOutput = null;                 // this object does not have its ownership
            this.bodyLength     = 0;
            DisposableUtil.ClearDisposableObject(ref this.bodyStream);

            // reset the base class level
            base.ResetBuffer();
        }
            public void Args_target_null()
            {
                // ARRANGE
                SampleDisposable arg = null;

                // ACT
                DisposableUtil.ClearDisposableObject(ref arg);

                // ASSERT
                // no ArgumentNullException expected
                Assert.Equal(null, arg);
            }
            public void General()
            {
                // ARRANGE
                SampleDisposable sample = new SampleDisposable();
                SampleDisposable arg    = sample;

                // ACT
                DisposableUtil.ClearDisposableObject(ref arg);

                // ASSERT
                Assert.Equal(null, arg);
                Assert.Equal(1, sample.DisposedCount);
            }
            public void Error()
            {
                // ARRANGE
                SampleDisposable sample     = new SampleDisposable(throwException: true);
                SampleDisposable arg        = sample;
                TestLogMonitor   logMonitor = new TestLogMonitor();

                // ACT
                logMonitor.StartLogging(filterByCurrentThread: true);
                try {
                    DisposableUtil.ClearDisposableObject(ref arg);
                } finally {
                    logMonitor.StopLogging();
                }

                // ASSERT
                Assert.Equal(null, arg);
                Assert.Equal(1, sample.DisposedCount);
                LogEntry expectedEntry = new LogEntry(null, TraceEventType.Error, $"Fail to dispose the object at 'ClearDisposableObject.Error()': {sample.DisposeErrorMessage}");

                logMonitor.AssertContains(expectedEntry);
            }
Exemple #12
0
            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;
            }