Пример #1
0
        public void Load()
        {
            lock (this)
            {
                try
                {
                    XmlDocument xmlDoc = new XmlDocument();

                    Providers = xmlDoc.CreateElement("providers");

                    if (Get("profile").ToLowerInvariant() == "none")
                    {
                        return;
                    }

                    string path = GetProfilePath();

                    CompatibilityManager.FixOldProfilePath(path);                     // 2.15

                    Engine.Instance.Logs.Log(LogType.Verbose, MessagesFormatter.Format(Messages.OptionsRead, path));

                    if (Platform.Instance.FileExists(path) == false)
                    {
                        Engine.Instance.Logs.Log(LogType.Verbose, Messages.OptionsNotFound);
                        return;
                    }

                    // CompatibilityManager.FixOldProfile(path); // ClodoTemp
                    xmlDoc.Load(path);

                    ResetAll(true);

                    Providers = UtilsXml.XmlGetFirstElementByTagName(xmlDoc.DocumentElement, "providers");
                    if (Providers == null)
                    {
                        Providers = xmlDoc.CreateElement("providers");
                    }

                    XmlNode nodeOptions = xmlDoc.DocumentElement.GetElementsByTagName("options")[0];
                    Dictionary <string, string> options = new Dictionary <string, string>();
                    foreach (XmlElement e in nodeOptions)
                    {
                        string name  = e.Attributes["name"].Value;
                        string value = e.Attributes["value"].Value;

                        CompatibilityManager.FixOption(ref name, ref value);

                        options[name] = value;
                    }

                    CompatibilityManager.FixOptions(options);
                    foreach (KeyValuePair <string, string> item in options)
                    {
                        Set(item.Key, item.Value);
                    }

                    // For compatibility <3
                    XmlElement xmlManifest = UtilsXml.XmlGetFirstElementByTagName(xmlDoc.DocumentElement, "manifest");
                    if (xmlManifest != null)
                    {
                        XmlElement providerAirVpn = xmlDoc.CreateElement("AirVPN");
                        Providers.AppendChild(providerAirVpn);

                        UtilsXml.XmlCopyElement(xmlManifest, providerAirVpn);

                        XmlElement xmlUser = UtilsXml.XmlGetFirstElementByTagName(xmlDoc.DocumentElement, "user");
                        if (xmlUser != null)                         // Compatibility with old manifest < 2.11
                        {
                            XmlElement oldKeyFormat = xmlUser.SelectSingleNode("keys/key[@id='default']") as XmlElement;
                            if (oldKeyFormat != null)
                            {
                                oldKeyFormat.SetAttribute("name", "Default");
                            }
                        }
                        if (xmlUser != null)
                        {
                            UtilsXml.XmlCopyElement(xmlUser, providerAirVpn);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Engine.Instance.Logs.Log(LogType.Fatal, MessagesFormatter.Format(Messages.OptionsReverted, ex.Message));
                    ResetAll(true);
                }
            }
        }
Пример #2
0
        private void LoadInternal(byte[] plainData)
        {
            lock (this)
            {
                if (plainData == null)
                {
                    throw new Exception("Unknown format");
                }

                XmlDocument xmlDoc = new XmlDocument();

                Providers = xmlDoc.CreateElement("providers");

                // Put the byte array into a stream, rewind it to the beginning and read
                MemoryStream ms = new MemoryStream(plainData);
                ms.Flush();
                ms.Position = 0;
                xmlDoc.Load(ms);

                ResetAll(true);

                Providers = xmlDoc.DocumentElement.GetFirstElementByTagName("providers");
                if (Providers == null)
                {
                    Providers = xmlDoc.CreateElement("providers");
                }

                XmlNode nodeOptions = xmlDoc.DocumentElement.GetElementsByTagName("options")[0];
                Dictionary <string, string> options = new Dictionary <string, string>();
                foreach (XmlElement e in nodeOptions)
                {
                    string name  = e.Attributes["name"].Value;
                    string value = e.Attributes["value"].Value;

                    CompatibilityManager.FixOption(ref name, ref value);
                    if (name != "")
                    {
                        options[name] = value;
                    }
                }

                CompatibilityManager.FixOptions(options);
                foreach (KeyValuePair <string, string> item in options)
                {
                    Set(item.Key, item.Value);
                }

                // For compatibility <3
                XmlElement xmlManifest = xmlDoc.DocumentElement.GetFirstElementByTagName("manifest");
                if (xmlManifest != null)
                {
                    XmlElement providerAirVpn = xmlDoc.CreateElement("AirVPN");
                    Providers.AppendChild(providerAirVpn);

                    ExtensionsXml.XmlCopyElement(xmlManifest, providerAirVpn);

                    XmlElement xmlUser = xmlDoc.DocumentElement.GetFirstElementByTagName("user");
                    if (xmlUser != null)                     // Compatibility with old manifest < 2.11
                    {
                        XmlElement oldKeyFormat = xmlUser.SelectSingleNode("keys/key[@id='default']") as XmlElement;
                        if (oldKeyFormat != null)
                        {
                            oldKeyFormat.SetAttribute("name", "Default");
                        }
                    }
                    if (xmlUser != null)
                    {
                        ExtensionsXml.XmlCopyElement(xmlUser, providerAirVpn);
                    }
                }
            }
        }