Exemple #1
0
        public static void Update(Configuration config, bool forceDisable)
        {
            bool global  = config.global;
            bool enabled = config.enabled;

            if (forceDisable)
            {
                enabled = false;
            }
            try
            {
                RegistryKey registry =
                    Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
                                                    true);
                if (enabled)
                {
                    if (global)
                    {
                        registry.SetValue("ProxyEnable", 1);
                        registry.SetValue("ProxyServer", "127.0.0.1:" + config.localPort.ToString());
                        registry.SetValue("AutoConfigURL", "");
                    }
                    else
                    {
                        string pacUrl;
                        if (config.useOnlinePac && !string.IsNullOrEmpty(config.pacUrl))
                        {
                            pacUrl = config.pacUrl;
                        }
                        else
                        {
                            pacUrl = "http://127.0.0.1:" + config.localPort.ToString() + "/pac?t=" + GetTimestamp(DateTime.Now);
                        }
                        registry.SetValue("ProxyEnable", 0);
                        var readProxyServer = registry.GetValue("ProxyServer");
                        if (readProxyServer != null && readProxyServer.Equals("127.0.0.1:" + config.localPort.ToString()))
                        {
                            registry.SetValue("ProxyServer", "");
                        }
                        registry.SetValue("AutoConfigURL", pacUrl);
                    }
                }
                else
                {
                    registry.SetValue("ProxyEnable", 0);
                    if (global)
                    {
                        registry.SetValue("ProxyServer", "");
                    }
                    registry.SetValue("AutoConfigURL", "");
                }
                //Set AutoDetectProxy Off
                IEAutoDetectProxy(false);
                SystemProxy.NotifyIE();
                //Must Notify IE first, or the connections do not chanage
                CopyProxySettingFromLan();
            }
            catch (Exception e)
            {
                Logging.LogUsefulException(e);
                // TODO this should be moved into views
                MessageBox.Show(I18N.GetString("Failed to update registry"));
            }
        }
Exemple #2
0
        public static void Update(Configuration config, bool forceDisable)
        {
            int sysProxyMode = config.sysProxyMode;

            if (forceDisable)
            {
                sysProxyMode = (int)ProxyMode.Direct;
            }
            bool    global  = sysProxyMode == (int)ProxyMode.Global;
            bool    enabled = sysProxyMode != (int)ProxyMode.Direct;
            Version win8    = new Version("6.2");

            //if (Environment.OSVersion.Version.CompareTo(win8) < 0)
            {
                using (RegistryKey registry = OpenUserRegKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings", true))
                {
                    try
                    {
                        if (enabled)
                        {
                            if (global)
                            {
                                RegistrySetValue(registry, "ProxyEnable", 1);
                                RegistrySetValue(registry, "ProxyServer", "127.0.0.1:" + config.localPort.ToString());
                                RegistrySetValue(registry, "AutoConfigURL", "");
                            }
                            else
                            {
                                string pacUrl;
                                pacUrl = "http://127.0.0.1:" + config.localPort.ToString() + "/pac?" + "auth=" + config.localAuthPassword + "&t=" + Util.Utils.GetTimestamp(DateTime.Now);
                                RegistrySetValue(registry, "ProxyEnable", 0);
                                RegistrySetValue(registry, "ProxyServer", "");
                                RegistrySetValue(registry, "AutoConfigURL", pacUrl);
                            }
                        }
                        else
                        {
                            RegistrySetValue(registry, "ProxyEnable", 0);
                            RegistrySetValue(registry, "ProxyServer", "");
                            RegistrySetValue(registry, "AutoConfigURL", "");
                        }
                        IEProxyUpdate(config, sysProxyMode);
                        SystemProxy.NotifyIE();
                        //Must Notify IE first, or the connections do not chanage
                        CopyProxySettingFromLan();
                    }
                    catch (Exception e)
                    {
                        Logging.LogUsefulException(e);
                        // TODO this should be moved into views
                        //MessageBox.Show(I18N.GetString("Failed to update registry"));
                    }
                }
            }
            if (Environment.OSVersion.Version.CompareTo(win8) >= 0)
            {
                try
                {
                    if (enabled)
                    {
                        if (global)
                        {
                            WinINet.SetIEProxy(true, true, "127.0.0.1:" + config.localPort.ToString(), "");
                        }
                        else
                        {
                            string pacUrl;
                            pacUrl = $"http://127.0.0.1:{config.localPort}/pac?auth={config.localAuthPassword}&t={Util.Utils.GetTimestamp(DateTime.Now)}";
                            WinINet.SetIEProxy(true, false, "", pacUrl);
                        }
                    }
                    else
                    {
                        WinINet.SetIEProxy(false, false, "", "");
                    }
                }
                catch (Exception ex)
                {
                    Logging.LogUsefulException(ex);
                }
            }
        }