Exemple #1
0
        void LoadParam(AXMLNode N)
        {
            bool Protected = N[sProtected] == "1";
            Func <string, string> F;

            if (Protected)
            {
                F = (x) => { string s = N[x]; if (s.StartsWith(sProtected + "!"))
                             {
                                 s = Unprotect(s.Substring(sProtected.Length + 1));
                             }
                             ; return(s); };
            }
            else
            {
                F = (x) => N[x];
            }
            CurrentVpn         = new VpnItem();
            CurrentVpn.vpnName = N["vpnName"];
            CurrentVpn.vpnUser = N["vpnUser"];
            CurrentVpn.vpnPwd  = F("vpnPwd");
            CurrentVpn.ipMask  = N["ipMask"];
            CurrentVpn.Routes  = N["Routes"];

            if (!Protected)
            {
                SettingsChanged = true;
            }
        }
Exemple #2
0
 public VPNConnect()
 {
     VpnDict    = new Dictionary <string, VpnItem>();
     CurrentVpn = new VpnItem();
     Settings   = new VPNSettings {
         Connect = this
     };
 }
Exemple #3
0
        public void SaveSettings()
        {
            //s = Application.StartupPath; s += @"\Settings.xml";
            XmlDocument D = new XmlDocument();
            Boolean     F = File.Exists(SettingsPath);

            if (F)
            {
                try
                {
                    D.Load(SettingsPath);
                }
                catch { F = false; }
            }
            if (!F)
            {
                D.LoadXml(@"<?xml version=""1.0"" encoding=""windows-1251""?><Option/>");
            }

            /*XmlNodeList Ns = D.DocumentElement.ChildNodes;
             * XmlNode N;
             * if (Ns.Count == 0)
             * {
             *  N = D.CreateElement("S");
             *  D.DocumentElement.AppendChild(N);
             * }
             * else { N = Ns[0]; }
             * SaveParam(D, N);
             */
            XmlNode vpnNode = D.DocumentElement.SelectSingleNode("vpnConnect");

            if (vpnNode == null)
            {
                vpnNode = D.CreateElement("vpnConnect");
                D.DocumentElement.AppendChild(vpnNode);
            }
            else
            {
                vpnNode.RemoveAll();
            }
            foreach (VpnItem item in Connect.VpnDict.Values)
            {
                CurrentVpn = item;
                XmlNode N = D.CreateElement("item");
                SaveParam(D, N);
                vpnNode.AppendChild(N);
            }
            var A = D.CreateAttribute("Current");

            A.Value = Connect.CurrentVpn.vpnName;
            vpnNode.Attributes.SetNamedItem(A);
            D.Save(SettingsPath);
        }
Exemple #4
0
        private bool SelectItem()
        {
            string k = (string)cmbItems.SelectedItem;

            if (k == null)
            {
                k = cmbItems.Text;
            }
            VpnItem item = null;
            var     res  = VpnConnect.VpnDict.TryGetValue(k, out item);

            if (res)
            {
                VpnConnect.CurrentVpn = item;
            }
            return(res);
        }
Exemple #5
0
        public bool LoadSettings(string s1 = "")
        { // Чтение настроечных параметров
            SettingsChanged = false;
            string s, s2;

            if (s1 == "")
            {
                s = Application.StartupPath; s += @"\Settings.xml";
            }
            else
            {
                s = s1;
            }
            SettingsPath = s;
            if (!File.Exists(s))
            {
                return(false);
            }
            XmlDocument D = new XmlDocument();

            try
            {
                D.Load(s);
                XmlNodeList Ns = D.DocumentElement.ChildNodes;
                if (Ns.Count > 0)
                {
                    var AC = Ns[0].Attributes;
                    var A  = AC[@"Path"];
                    if (A != null)
                    {
                        s2 = A.Value;
                    }
                    else
                    {
                        s2 = "";
                    }
                    if ((s1 == "") && (s2 != ""))
                    {
                        LoadSettings(s2);
                    }
                    else
                    {
                        XmlNode vpnNode = D.DocumentElement.SelectSingleNode("vpnConnect");
                        if (vpnNode != null)
                        {
                            XmlNodeList items = vpnNode.SelectNodes("item");
                            if (items.Count == 0)
                            {
                                vpnNode = null;
                            }
                        }
                        if (vpnNode == null)
                        {
                            LoadParam(new AXMLNode(Ns[0]));
                            SettingsChanged = true;
                            Connect.VpnDict.Clear();
                            Connect.VpnDict.Add(CurrentVpn.vpnName, CurrentVpn);
                        }
                        else
                        {
                            XmlNodeList items = vpnNode.SelectNodes("item");
                            Connect.VpnDict.Clear();
                            VpnItem first = null;
                            foreach (XmlNode item in items)
                            {
                                var aitem = new AXMLNode(item);
                                LoadParam(aitem);
                                if (first == null)
                                {
                                    first = CurrentVpn;
                                }
                                try
                                {
                                    Connect.VpnDict.Add(CurrentVpn.vpnName, CurrentVpn);
                                }
                                catch { }
                            }
                            if (first != null)
                            {
                                CurrentVpn = first;
                            }
                            try
                            {
                                string current = (string)vpnNode.Attributes["Current"].Value;
                                if (Connect.VpnDict.TryGetValue(current, out first))
                                {
                                    CurrentVpn = first;
                                }
                                Connect.CurrentVpn = CurrentVpn;
                            }
                            catch { }
                        }
                    }
                }
            }
            catch (Exception e) { MessageBox.Show(e.Message); }
            D = null;
            if (SettingsChanged)
            {
                SaveSettings();
            }
            return(true);
        }