/// <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()); }); } }
public NetworkConfiguration(string name, NetworkRulesSet networkRulesSet, ProxySettings proxySettings) { this.name = name; this.networkRulesSet = networkRulesSet; this.proxySettings = proxySettings; }
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); } }
private void m_networkChangeDetector_ProxyChanged(string name, NetworkInfo networkInfo, ProxySettings proxySettings, string reason) { string info; string detailedInfo; log.Info("Proxy Changed"); // Afficher tooltip indiquant disponibilité if (networkInfo != null && proxySettings != null) { info = String.Format("Configuration \"{0}\" available\nInterface name: {1}\nReason: {2}", name, networkInfo.IfName, reason); m_notifyIcon.BalloonTipIcon = ToolTipIcon.Info; } else if (networkInfo == null) { info = String.Format("Configuration \"{0}\" available\nReason: {1}", name, reason); m_notifyIcon.BalloonTipIcon = ToolTipIcon.Info; } else { info = String.Format("No rule found, no change"); m_notifyIcon.BalloonTipIcon = ToolTipIcon.Warning; } m_notifyIcon.BalloonTipText = info; Assembly assembly = Assembly.GetExecutingAssembly(); FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location); string version = fvi.FileVersion; detailedInfo = "V" + fvi.FileVersion; detailedInfo += "\n\nUpdate date: " + System.DateTime.Now; detailedInfo += "\n\n" + info; if (networkInfo != null) { detailedInfo += "\n\nNetwork details: "; detailedInfo += "\n Interface name: " + networkInfo.IfName; detailedInfo += "\n Type: " + networkInfo.Type; detailedInfo += "\n Description: " + networkInfo.Description; detailedInfo += "\n IP(s): " + String.Join(", ", networkInfo.IP); detailedInfo += "\n Network(s): " + String.Join(", ", networkInfo.NetworkIP); detailedInfo += "\n DNS: " + String.Join(", ", networkInfo.DNS); } if (proxySettings != null) { detailedInfo += "\n\nProxy details: "; detailedInfo += "\n " + proxySettings; } if (InvokeRequired) { UIA(() => m_statusForm.Status = detailedInfo); } else { m_statusForm.Status = detailedInfo; } m_notifyIcon.ShowBalloonTip(3000); }