示例#1
0
        public void TestSimpleRule()
        {
            AutoProxySwitcherLib.NetworkChangeDetector ncd = new NetworkChangeDetector();
            RulesChecker.RulesCheckerResult            result;
            NetworkInfo networkInfo;

            ncd.LoadConfigurations("Samples/SimpleRule.xml");

            NetworkInfo ni = new NetworkInfo();

            ni.Description = "Test Network";
            ni.DNS         = new List <string> {
                "192.168.1.252"
            };
            ni.IP = new List <string> {
                "192.168.1.254"
            };
            ni.NetworkIP = new List <string> {
                "192.168.0.0/16"
            };
            ni.Type   = "Wi-Fi";
            ni.IfName = "Test1";

            List <NetworkInfo> networks = new List <NetworkInfo> {
                ni
            };

            NetworkConfiguration configuration = ncd.FindMatchingConfiguration(networks, out result, out networkInfo);

            Console.WriteLine(result.ReasonString);
            Assert.AreSame(configuration, ncd.Configurations[0]);
            Assert.IsTrue(result.ReasonString.Equals("network DNS matches 192.168.1.252"));
        }
示例#2
0
        /// <summary>
        /// Default constructor, only for design mode
        /// </summary>
        public MainViewModel()
        {
            if (DesignerProperties.GetIsInDesignMode(new DependencyObject()))
            {
                // Generate some configurations
                _configurationsList = new List <NetworkConfiguration>();

                NetworkRulesSet rulesSet = new NetworkRulesSet();
                rulesSet.Rules.Add(new AutoProxySwitcherLib.Rules.NetworkRuleDNS("10.0.0.1"));

                for (int i = 0; i < 5; i++)
                {
                    _configurationsList.Add(new NetworkConfiguration("Config #" + i, rulesSet, new StandardProxySettings("http://test:8080", null, null)));
                }

                _activeConfiguration = _configurationsList[2];


                // Generate some networks
                for (int i = 0; i < 10; i++)
                {
                    _currentNetworks.Add(new NetworkInfo()
                    {
                        IfName = "Sample Network #" + i, IP = { "10.0.0.1" }, DNS = { "10.0.0.5" }, NetworkIP = { "10.0.0.0/24" }
                    });
                }

                _currentNetwork       = _currentNetworks[3];
                _currentProxySettings = _activeConfiguration.ProxySettings;

                //
                reason = "Network matching IP x.y.z.t";
            }
            else
            {
                string rulesFile = System.Environment.GetFolderPath(System.Environment.SpecialFolder.CommonApplicationData)
                                   + "\\AutoProxySwitcher\\"
                                   + Environment.ExpandEnvironmentVariables(System.Configuration.ConfigurationManager.AppSettings["RulesFile"]);

                // Create default rules files if it doesn't exists
                if (!System.IO.File.Exists(rulesFile))
                {
                    System.IO.File.Copy("Examples/rules.xml", rulesFile);
                }

                // Load configurations
                _networkChangeDetector = new NetworkChangeDetector();
                _networkChangeDetector.ProxyChanged += _networkChangeDetector_ProxyChanged;
                _networkChangeDetector.LoadConfigurations(rulesFile);

                _configurationsList = _networkChangeDetector.Configurations;

                // Init commands relay
                DebugCommand            = new RelayCommand <string>(m => MessageBox.Show("toto: " + m));
                SetConfigurationCommand = new RelayCommand <string>((conf) => _networkChangeDetector.SetConfiguration(conf));
                AutoDetectCommand       = new RelayCommand(() => _networkChangeDetector.SetConfiguration(null));
                ExitCommand             = new RelayCommand(() => { Messenger.Default.Send <ExitMessage>(new ExitMessage()); });
            }
        }
示例#3
0
        public void TestManyConfigs()
        {
            AutoProxySwitcherLib.NetworkChangeDetector ncd = new NetworkChangeDetector();
            RulesChecker.RulesCheckerResult            result;
            NetworkInfo networkInfo;

            ncd.LoadConfigurations("Samples/ManyConfigs.xml");

            NetworkInfo ni = new NetworkInfo();

            ni.Description = "Test Network";
            ni.DNS         = new List <string> {
                "172.17.4.80", "172.17.4.81"
            };
            ni.IP = new List <string> {
                "172.17.4.145"
            };
            ni.NetworkIP = new List <string> {
                "172.17.4.0/24"
            };
            ni.Type   = "Local Area Connection";
            ni.IfName = "test";

            List <NetworkInfo>   networks;
            NetworkConfiguration configuration;

            networks = new List <NetworkInfo> {
                ni
            };
            configuration = ncd.FindMatchingConfiguration(networks, out result, out networkInfo);

            Console.WriteLine(result.ReasonString);
            Console.WriteLine(configuration.ProxySettings);
            Assert.AreEqual(configuration.Name, "Config 1");
            Assert.IsTrue(result.ReasonString.Equals("network subnet matches 172.17.4.0/24"));


            ni.Description = "Test Network";
            ni.DNS         = new List <string> {
                "172.17.80.80", "172.17.80.81"
            };
            ni.IP = new List <string> {
                "172.17.80.15"
            };
            ni.NetworkIP = new List <string> {
                "172.17.80.0/24"
            };
            ni.Type   = "Local Area Connection";
            ni.IfName = "test";

            networks = new List <NetworkInfo> {
                ni
            };
            configuration = ncd.FindMatchingConfiguration(networks, out result, out networkInfo);

            Console.WriteLine(result.ReasonString);
            Console.WriteLine(configuration.ProxySettings);
            Assert.AreEqual(configuration.Name, "Config 4");
            Assert.IsTrue(result.ReasonString.Equals("network subnet matches 172.17.80.0/24"));

            ni.Description = "Test Network";
            ni.DNS         = new List <string> {
                "172.16.0.5"
            };
            ni.IP = new List <string> {
                "172.16.0.78"
            };
            ni.NetworkIP = new List <string> {
                "172.16.2/24"
            };
            ni.Type   = "Local Area Connection";
            ni.IfName = "test";

            networks = new List <NetworkInfo> {
                ni
            };
            configuration = ncd.FindMatchingConfiguration(networks, out result, out networkInfo);

            Console.WriteLine(result.ReasonString);
            Console.WriteLine(configuration.ProxySettings);
            Assert.AreEqual(configuration.Name, "Default Config");
            Assert.IsTrue(result.ReasonString.Equals("default rule match"));
        }