/// <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()); }); } }
/// <summary> /// Recursively read rules /// </summary> /// <param name="nodeList"></param> /// <returns></returns> static private NetworkRulesSet ReadRules(XmlNodeList nodeList) { NetworkRulesSet networkRulesSet = new NetworkRulesSet(); foreach (System.Xml.XmlNode ruleNode in nodeList) { NetworkRule rule = null; if (ruleNode.Name == "And") { NetworkRulesSet rs2 = ReadRules(ruleNode.SelectNodes("*")); rs2.Op = Operator.And; rule = rs2; } else if (ruleNode.Name == "Or") { NetworkRulesSet rs2 = ReadRules(ruleNode.SelectNodes("*")); rs2.Op = Operator.Or; rule = rs2; } else if (ruleNode.Name == "Not") { NetworkRulesSet rs2 = ReadRules(ruleNode.SelectNodes("*")); rs2.Op = Operator.Not; rule = rs2; } else if (ruleNode.Name == "Subnet") { rule = new NetworkRuleSubnet(ruleNode.Attributes["ip"].Value); } else if (ruleNode.Name == "DNS") { rule = new NetworkRuleDNS(ruleNode.Attributes["ip"].Value); } else if (ruleNode.Name == "Interface") { rule = new NetworkRuleIfName(ruleNode.Attributes["name"].Value); } else if (ruleNode.Name == "Ping") { rule = new NetworkRulePingable(ruleNode.Attributes["ip"].Value); } else { log.Warn("Unknown rule " + ruleNode.Name); } if (rule != null) { networkRulesSet.Rules.Add(rule); } } return(networkRulesSet); }
/// <summary> /// Log configuration from XmlDocument /// </summary> /// <param name="doc"></param> /// <returns></returns> public static List<NetworkConfiguration> FromXmlDocument(System.Xml.XmlDocument doc) { List<NetworkConfiguration> configurations = new List<NetworkConfiguration>(); log.Debug("Loading configurations"); // Chargement des règles foreach (System.Xml.XmlNode networkNode in doc.SelectNodes("/Networks/Network")) { NetworkRulesSet networkRulesSet = new NetworkRulesSet(); ProxySettings proxySettings = null; networkRulesSet = ReadRules(networkNode.SelectNodes("Rules/*")); // Chargement des paramètres du configuration System.Xml.XmlNode proxyNode = networkNode.SelectSingleNode("Proxy"); switch (proxyNode.Attributes["type"].Value) { case "PacFile": proxySettings = new ProxyPACSettings(proxyNode.Attributes["url"].Value); break; case "Standard": bool? bypassLocal = proxyNode.Attributes["bypasslocal"] != null ? (bool?)bool.Parse(proxyNode.Attributes["bypasslocal"].Value) : null; string exceptions = proxyNode.Attributes["exceptions"] != null ? proxyNode.Attributes["exceptions"].Value : null; proxySettings = new StandardProxySettings(proxyNode.Attributes["url"].Value, exceptions, bypassLocal); break; case "None": proxySettings = new NoProxySettings(); break; default: throw new Exception("Unknown Proxy type " + proxyNode.Attributes["type"].Value); } // Ajouter la configuration configurations.Add(new NetworkConfiguration(networkNode.Attributes["name"].Value, networkRulesSet, proxySettings)); } log.DebugFormat("Configurations loaded: {0}", configurations.Count); return configurations; }
/// <summary> /// Log configuration from XmlDocument /// </summary> /// <param name="doc"></param> /// <returns></returns> public static List <NetworkConfiguration> FromXmlDocument(System.Xml.XmlDocument doc) { List <NetworkConfiguration> configurations = new List <NetworkConfiguration>(); log.Debug("Loading configurations"); // Chargement des règles foreach (System.Xml.XmlNode networkNode in doc.SelectNodes("/Networks/Network")) { NetworkRulesSet networkRulesSet = new NetworkRulesSet(); ProxySettings proxySettings = null; networkRulesSet = ReadRules(networkNode.SelectNodes("Rules/*")); // Chargement des paramètres du configuration System.Xml.XmlNode proxyNode = networkNode.SelectSingleNode("Proxy"); switch (proxyNode.Attributes["type"].Value) { case "PacFile": proxySettings = new ProxyPACSettings(proxyNode.Attributes["url"].Value); break; case "Standard": bool? bypassLocal = proxyNode.Attributes["bypasslocal"] != null ? (bool?)bool.Parse(proxyNode.Attributes["bypasslocal"].Value) : null; string exceptions = proxyNode.Attributes["exceptions"] != null ? proxyNode.Attributes["exceptions"].Value : null; proxySettings = new StandardProxySettings(proxyNode.Attributes["url"].Value, exceptions, bypassLocal); break; case "None": proxySettings = new NoProxySettings(); break; default: throw new Exception("Unknown Proxy type " + proxyNode.Attributes["type"].Value); } // Ajouter la configuration configurations.Add(new NetworkConfiguration(networkNode.Attributes["name"].Value, networkRulesSet, proxySettings)); } log.DebugFormat("Configurations loaded: {0}", configurations.Count); return(configurations); }
public RulesChecker(NetworkRulesSet networkRulesSet) { m_NetworkRulesSet = networkRulesSet; }
public NetworkConfiguration(string name, NetworkRulesSet networkRulesSet, ProxySettings proxySettings) { this.name = name; this.networkRulesSet = networkRulesSet; this.proxySettings = proxySettings; }
/// <summary> /// Recursively read rules /// </summary> /// <param name="nodeList"></param> /// <returns></returns> private static NetworkRulesSet ReadRules(XmlNodeList nodeList) { NetworkRulesSet networkRulesSet = new NetworkRulesSet(); foreach (System.Xml.XmlNode ruleNode in nodeList) { NetworkRule rule = null; if (ruleNode.Name == "And") { NetworkRulesSet rs2 = ReadRules(ruleNode.SelectNodes("*")); rs2.Op = Operator.And; rule = rs2; } else if (ruleNode.Name == "Or") { NetworkRulesSet rs2 = ReadRules(ruleNode.SelectNodes("*")); rs2.Op = Operator.Or; rule = rs2; } else if (ruleNode.Name == "Not") { NetworkRulesSet rs2 = ReadRules(ruleNode.SelectNodes("*")); rs2.Op = Operator.Not; rule = rs2; } else if (ruleNode.Name == "Subnet") { rule = new NetworkRuleSubnet(ruleNode.Attributes["ip"].Value); } else if (ruleNode.Name == "DNS") { rule = new NetworkRuleDNS(ruleNode.Attributes["ip"].Value); } else if (ruleNode.Name == "Interface") { rule = new NetworkRuleIfName(ruleNode.Attributes["name"].Value); } else if (ruleNode.Name == "Ping") { rule = new NetworkRulePingable(ruleNode.Attributes["ip"].Value); } else { log.Warn("Unknown rule " + ruleNode.Name); } if (rule != null) { networkRulesSet.Rules.Add(rule); } } return networkRulesSet; }