Exemplo n.º 1
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()); });
            }
        }
Exemplo n.º 2
0
        void _networkChangeDetector_ProxyChanged(string name, NetworkInfo networkInfo, ProxySettings proxySettings, string reason)
        {
            try
            {
                log.DebugFormat("ProxyChanged event: {0}, network {1}, proxy {2}, reason", name, networkInfo, proxySettings, reason);

                lastUpdateTime = DateTime.Now;
                _activeConfiguration = _configurationsList.First((c) => c.Name == name);
                this.interfaceName = networkInfo != null ? networkInfo.IfName : "N/A";
                this.addresses = networkInfo != null ? String.Join(", ", networkInfo.IP) : "";
                this.reason = reason;
                _currentNetworks = _networkChangeDetector.CurrentNetworks;
                _currentNetwork = networkInfo != null ? _currentNetworks.FirstOrDefault((n) => networkInfo.IfName == n.IfName) : null;
                _currentProxySettings = proxySettings;

                RaisePropertyChanged("LastUpdateTime");
                RaisePropertyChanged("ActiveConfiguration");
                RaisePropertyChanged("InterfaceName");
                RaisePropertyChanged("Addresses");
                RaisePropertyChanged("Reason");
                RaisePropertyChanged("CurrentNetworks");
                RaisePropertyChanged("CurrentNetwork");
                RaisePropertyChanged("CurrentProxySettings");

                log.Debug("Sending ChangeMessage");
                ChangeMessage msg = new ChangeMessage() { ConfName = name, Network = networkInfo, ProxySettings = proxySettings, Reason = reason };
                Messenger.Default.Send<ChangeMessage>(msg);
            }
            catch (Exception ex)
            {
                log.Error("Failure", ex);
            }
        }