Example #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Profile" /> class.
 /// </summary>
 public Profile(Profile profile)
 {
     Name = new Pointer<string>(profile.Name);
     EnableAutomatic = new Pointer<bool>(profile.EnableAutomatic);
     EnableAutomaticScript = new Pointer<bool>(profile.EnableAutomaticScript);
     AutomaticScript = new Pointer<string>(profile.AutomaticScript);
     EnableProxy = new Pointer<bool>(profile.EnableProxy);
     UseSameProxyForAllProtocols = new Pointer<bool>(profile.UseSameProxyForAllProtocols);
     HttpServer = new Pointer<string>(profile.HttpServer);
     HttpPort = new Pointer<int>(profile.HttpPort);
     SecureServer = new Pointer<string>(profile.SecureServer);
     SecurePort = new Pointer<int>(profile.SecurePort);
     FtpServer = new Pointer<string>(profile.FtpServer);
     FtpPort = new Pointer<int>(profile.FtpPort);
     SocksServer = new Pointer<string>(profile.SocksServer);
     SocksPort = new Pointer<int>(profile.SocksPort);
     BypassLocal = new Pointer<bool>(profile.BypassLocal);
     BypassPatterns = new Pointer<string>(profile.BypassPatterns);
     IPAddress = new Pointer<IPAddress>(profile.IPAddress);
     Subnet = new Pointer<IPAddress>(profile.Subnet);
     Gateway = new Pointer<IPAddress>(profile.Gateway);
 }
        private void ApplyProfile(Profile prof)
        {
            using (var settings = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings", true))
            using (var connection = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections", true))
            {
                if (prof.EnableAutomaticScript)
                    settings.SetValue("AutoConfigURL", prof.AutomaticScript.Value);
                else if (settings.GetValueNames().Contains("AutoConfigURL"))
                    settings.DeleteValue("AutoConfigURL");

                if (prof.EnableProxy)
                {
                    settings.SetValue("ProxyEnable", 1);

                    Tuple<string, string, int>[] items;
                    if (prof.UseSameProxyForAllProtocols)
                    {
                        if (!string.IsNullOrWhiteSpace(prof.HttpServer.Value))
                        {
                            if (prof.HttpPort.Value <= 0)
                                settings.SetValue("ProxyServer", string.Format(CultureInfo.InvariantCulture, "{0}:80", prof.HttpServer.Value));
                            else
                                settings.SetValue("ProxyServer", string.Format(CultureInfo.InvariantCulture, "{0}:{1}", prof.HttpServer.Value, prof.HttpPort.Value));
                        }
                        else if (settings.GetValueNames().Contains("ProxyServer"))
                        {
                            settings.DeleteValue("ProxyServer");
                        }
                    }
                    else
                    {
                        items = new[] {
                            Tuple.Create("http", prof.HttpServer.Value, prof.HttpPort.Value),
                            Tuple.Create("https", prof.SecureServer.Value, prof.SecurePort.Value),
                            Tuple.Create("ftp", prof.FtpServer.Value, prof.FtpPort.Value),
                            Tuple.Create("socks", prof.SocksServer.Value, prof.SocksPort.Value)};

                        var sb = new StringBuilder();
                        foreach (var item in items)
                        {
                            if (sb.Length != 0)
                                sb.Append(";");
                            if (!string.IsNullOrWhiteSpace(item.Item2))
                            {
                                if (item.Item3 <= 0)
                                    sb.AppendFormat(CultureInfo.InvariantCulture, "{0}={1}:80", item.Item1, item.Item2);
                                else
                                    sb.AppendFormat(CultureInfo.InvariantCulture, "{0}={1}:{2}", item.Item1, item.Item2, item.Item3);
                            }
                        }
                        if (sb.Length > 0)
                        {
                            settings.SetValue("ProxyServer", sb.ToString());
                        }
                        else if (settings.GetValueNames().Contains("ProxyServer"))
                        {
                            settings.DeleteValue("ProxyServer");
                        }
                    }

                    var local = prof.BypassLocal.Value ? "<local>" : null;
                    var bypass = string.IsNullOrWhiteSpace(prof.BypassPatterns.Value) ? local : prof.BypassPatterns.Value + ";" + local;

                    if (!string.IsNullOrWhiteSpace(bypass))
                        settings.SetValue("ProxyOverride", bypass);
                    else if (settings.GetValueNames().Contains("ProxyOverride"))
                        settings.DeleteValue("ProxyOverride");
                }
                else
                {
                    settings.SetValue("ProxyEnable", 0);
                }

                var cs = ConnectionSettings.Read((byte[])connection.GetValue("DefaultConnectionSettings"));
                cs.Counter++;

                cs.AutodetectScript = (string)settings.GetValue("AutoConfigURL", "");
                if (!string.IsNullOrWhiteSpace(cs.AutodetectScript))
                    cs.Autodetect |= Autodetect.AutoConfig;
                else
                    cs.Autodetect &= ~Autodetect.AutoConfig;

                // No registry backer.
                if (prof.EnableAutomatic)
                    cs.Autodetect |= Autodetect.AutoDetect;
                else
                    cs.Autodetect &= ~Autodetect.AutoDetect;

                cs.ProxyServer = (string)settings.GetValue("ProxyServer", "");
                if ((int)settings.GetValue("ProxyEnable", 0) == 1)
                    cs.Autodetect |= Autodetect.Manual;
                else
                    cs.Autodetect &= ~Autodetect.Manual;

                cs.ExtraData = (string)settings.GetValue("ProxyOverride", "");

                connection.SetValue("DefaultConnectionSettings", cs.Write());
            }

            NativeMethods.RefreshInternetOptions();

            _lastProfile = prof.Name;
            _notify.Text = "AutoProxy: " + _lastProfile;
        }
 private void LoadSettings()
 {
     var profiles = Properties.Settings.Default.Profiles;
     if (string.IsNullOrWhiteSpace(profiles))
     {
         _profiles.Add(CreateDefaultProfile());
     }
     else
     {
         var doc = XDocument.Parse(profiles);
         foreach (var elem in doc.Root.Elements())
         {
             var prof = new Profile();
             prof.ParseElement(elem);
             _profiles.Add(prof);
         }
     }
 }
 private Profile CreateDefaultProfile()
 {
     var prof = new Profile();
     prof.Name.Value = "(Default)";
     return prof;
 }