Esempio n. 1
0
        protected override void SetSwitchingSystemSettingsTo(SystemSettings settings, Proxy proxy)
        {
            // argument checks
            Debug.Assert(settings != null);
            SystemSettingsForWindows actualSettings = settings as SystemSettingsForWindows;

            if (actualSettings == null)
            {
                throw CreateArgumentIsNotSystemSettingsForWindowsException(nameof(settings));
            }
            Debug.Assert(proxy != null);

            // set the base class level settings
            base.SetSwitchingSystemSettingsTo(settings, proxy);

            // set this class level settings
            string proxyEndPoint = proxy.MainListenerEndPoint.ToString();

            Debug.Assert(actualSettings.AutoConfigURL == null);
            actualSettings.ProxyEnable   = 1;
            actualSettings.ProxyServer   = $"http={proxyEndPoint};https={proxyEndPoint}";
            actualSettings.ProxyOverride = this.ProxyOverride;
            Debug.Assert(actualSettings.AutoDetect == false);
            actualSettings.HttpProxyEnvironmentVariable  = $"http://{proxyEndPoint}";
            actualSettings.HttpsProxyEnvironmentVariable = $"http://{proxyEndPoint}";

            return;
        }
Esempio n. 2
0
        protected override IActualProxy GetSystemActualProxy(SystemSettings systemSettings)
        {
            // argument checks
            Debug.Assert(systemSettings != null);
            SystemSettingsForWindows actualSystemSettings = systemSettings as SystemSettingsForWindows;

            if (actualSystemSettings == null)
            {
                return(base.GetSystemActualProxy(systemSettings));
            }

            // detect actual proxy
            IActualProxy actualProxy = null;

            if ((actualSystemSettings.ProxyEnable ?? 0) == 0)
            {
                // proxy is not explicitly specified
                bool   autoDetect    = actualSystemSettings.AutoDetect;
                string autoConfigURL = actualSystemSettings.AutoConfigURL;
                if (autoDetect || string.IsNullOrEmpty(autoConfigURL) == false)
                {
                    actualProxy = new AutoConfigActualProxy(autoDetect, autoConfigURL);
                }
            }
            else
            {
                // proxy is explicitly specified
                // detect actually effective proxy by the base class implementation
                // Note that the explicitly specified proxy setting is not used
                // in some case such as dialup or VPN connection.
                ;
            }

            return((actualProxy != null) ? actualProxy : base.GetSystemActualProxy(systemSettings));
        }
Esempio n. 3
0
        protected override void SetCurrentSystemSettingsTo(SystemSettings settings)
        {
            // argument checks
            Debug.Assert(settings != null);
            SystemSettingsForWindows actualSettings = settings as SystemSettingsForWindows;

            if (actualSettings == null)
            {
                throw CreateArgumentIsNotSystemSettingsForWindowsException(nameof(settings));
            }

            // set the base class level settings
            base.SetCurrentSystemSettingsTo(settings);

            // set this class level settings
            // read Internet Options from the registry
            using (RegistryKey key = OpenInternetSettingsKey(writable: false)) {
                // AutoConfigURL
                actualSettings.AutoConfigURL = (string)key.GetValue(RegistryNames.AutoConfigURL, defaultValue: null);

                // ProxyEnable
                actualSettings.ProxyEnable = (int?)key.GetValue(RegistryNames.ProxyEnable, defaultValue: null);

                // ProxyServer
                actualSettings.ProxyServer = (string)key.GetValue(RegistryNames.ProxyServer, defaultValue: null);

                // ProxyOverride
                actualSettings.ProxyOverride = (string)key.GetValue(RegistryNames.ProxyOverride, defaultValue: null);

                // AutoDetect
                using (RegistryKey connectionsKey = OpenConnectionsKey(key, writable: false)) {
                    bool   autoDetect = false;
                    byte[] bytes      = (byte[])connectionsKey.GetValue(RegistryNames.DefaultConnectionSettings, defaultValue: null);
                    if (bytes != null && AutoDetectByteIndex < bytes.Length)
                    {
                        autoDetect = (bytes[AutoDetectByteIndex] & AutoDetectFlag) != 0;
                    }
                    actualSettings.AutoDetect = autoDetect;
                }
            }

            // read User Environment Variables from the registry
            using (RegistryKey key = OpenEnvironmentKey(writable: false)) {
                // HttpProxyEnvironmentVariable
                actualSettings.HttpProxyEnvironmentVariable = (string)key.GetValue(EnvironmentNames.HttpProxy, defaultValue: null);

                // HttpsProxyEnvironmentVariable
                actualSettings.HttpsProxyEnvironmentVariable = (string)key.GetValue(EnvironmentNames.HttpsProxy, defaultValue: null);
            }

            return;
        }
Esempio n. 4
0
        public SystemSettingsForWindows(SystemSettingsForWindows src) : base(src)
        {
            // argument checks
            if (src == null)
            {
                throw new ArgumentNullException(nameof(src));
            }

            // clone members
            this.AutoConfigURL = src.AutoConfigURL;
            this.ProxyEnable   = src.ProxyEnable;
            this.ProxyServer   = src.ProxyServer;
            this.ProxyOverride = src.ProxyOverride;
            this.AutoDetect    = src.AutoDetect;
            this.HttpProxyEnvironmentVariable  = src.HttpProxyEnvironmentVariable;
            this.HttpsProxyEnvironmentVariable = src.HttpsProxyEnvironmentVariable;

            return;
        }
Esempio n. 5
0
        public SetupContextForWindows(CommandForWindowsSettings settings, SystemSettingsSwitcherForWindows switcher) : base(settings, switcher)
        {
            // argument checks
            Debug.Assert(settings != null);
            SystemSettingsSwitcherForWindowsSettings switcherSettings = settings.SystemSettingsSwitcher;

            Debug.Assert(switcherSettings != null);
            Debug.Assert(switcher != null);

            // ProxyOverride
            this.DefaultProxyOverride = SystemSettingsSwitcherForWindows.GetDefaultProxyOverride();
            if (settings.InitialSetupLevel == 0 || base.NeedActualProxy)
            {
                // User must set ProxyOverride
                this.NeedProxyOverride = true;

                if (string.IsNullOrEmpty(switcherSettings.ProxyOverride))
                {
                    // give default value
                    // DefaultProxyOverride may be set in config file
                    switcherSettings.ProxyOverride = this.DefaultProxyOverride;
                }
            }

            if (settings.InitialSetupLevel == 1)
            {
                // After MAPE supports auto detect and auto config script,
                // ProxyOverride should be reviewed because only overrides which
                // are not handled by auto config should be specified.
                SystemSettingsForWindows current = switcher.GetCurrentSystemSettings();
                if (
                    (current.AutoDetect || string.IsNullOrEmpty(current.AutoConfigURL) == false) &&
                    string.CompareOrdinal(switcherSettings.ProxyOverride, this.DefaultProxyOverride) != 0
                    )
                {
                    this.NeedProxyOverride        = true;
                    this.ShouldResetProxyOverride = true;
                }
            }

            return;
        }
Esempio n. 6
0
        protected override bool SwitchTo(SystemSettings settings, SystemSettings backup)
        {
            // argument checks
            Debug.Assert(settings != null);
            SystemSettingsForWindows actualSettings = settings as SystemSettingsForWindows;

            if (actualSettings == null)
            {
                throw CreateArgumentIsNotSystemSettingsForWindowsException(nameof(settings));
            }
            // backup can be null
            SystemSettingsForWindows actualBackup = backup as SystemSettingsForWindows;

            if (backup != null && actualBackup == null)
            {
                throw CreateArgumentIsNotSystemSettingsForWindowsException(nameof(backup));
            }

            // adjust settings
            string proxyOverride = actualSettings.ProxyOverride;

            if (actualBackup != null && string.IsNullOrEmpty(actualBackup.ProxyOverride) == false)
            {
                // use the current ProxyOverride if it is defined explicitly
                proxyOverride = actualBackup.ProxyOverride;
            }

            // set Internet Options in the registry
            using (RegistryKey key = OpenInternetSettingsKey(writable: true)) {
                // AutoConfigURL
                SetValue(key, RegistryNames.AutoConfigURL, actualSettings.AutoConfigURL);

                // ProxyEnable
                SetValue(key, RegistryNames.ProxyEnable, actualSettings.ProxyEnable);

                // ProxyServer
                SetValue(key, RegistryNames.ProxyServer, actualSettings.ProxyServer);

                // ProxyOverride
                SetValue(key, RegistryNames.ProxyOverride, proxyOverride);

                // AutoDetect
                using (RegistryKey connectionsKey = OpenConnectionsKey(key, writable: true)) {
                    byte[] bytes = (byte[])connectionsKey.GetValue(RegistryNames.DefaultConnectionSettings, defaultValue: null);
                    if (bytes != null && AutoDetectByteIndex < bytes.Length)
                    {
                        byte oldFlags = bytes[AutoDetectByteIndex];
                        byte newFlags = actualSettings.AutoDetect ? (byte)(oldFlags | AutoDetectFlag) : (byte)(oldFlags & ~AutoDetectFlag);
                        if (oldFlags != newFlags)
                        {
                            // set AutoDetect flag
                            bytes[AutoDetectByteIndex] = newFlags;

                            // update revision
                            // Do not increment the revision.
                            // Incrementing revision confuses Windows' Internet Option System.
                            // It seems that it detects difference based on this value.
#if false
                            uint   revision      = BitConverter.ToUInt32(bytes, ConnectionsRevisionIndex);
                            byte[] revisionBytes = BitConverter.GetBytes(++revision);
                            Debug.Assert(revisionBytes.Length == 4);
                            Array.Copy(revisionBytes, 0, bytes, ConnectionsRevisionIndex, 4);
#endif

                            // save the bytes
                            connectionsKey.SetValue(RegistryNames.DefaultConnectionSettings, bytes, RegistryValueKind.Binary);
                        }
                    }
                }
            }

            // set User Environment Variables in the registry
            using (RegistryKey key = OpenEnvironmentKey(writable: true)) {
                // HttpProxyEnvironmentVariable
                SetValue(key, EnvironmentNames.HttpProxy, actualSettings.HttpProxyEnvironmentVariable);

                // HttpsProxyEnvironmentVariable
                SetValue(key, EnvironmentNames.HttpsProxy, actualSettings.HttpsProxyEnvironmentVariable);
            }

            return(true);
        }