Exemplo n.º 1
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);
                    }
                }
            }
        }
Exemplo n.º 2
0
        public void Save()
        {
            bool remember = GetBool("remember");

            lock (this)
            {
                try
                {
                    XmlDocument    xmlDoc         = new XmlDocument();
                    XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);

                    XmlElement rootNode = xmlDoc.CreateElement("eddie");
                    xmlDoc.InsertBefore(xmlDeclaration, xmlDoc.DocumentElement);

                    XmlElement optionsNode = xmlDoc.CreateElement("options");
                    rootNode.AppendChild(optionsNode);

                    xmlDoc.AppendChild(rootNode);

                    foreach (Option option in Options.Values)
                    {
                        bool skip = false;

                        if ((remember == false) && (option.Code == "login"))
                        {
                            skip = true;
                        }
                        if ((remember == false) && (option.Code == "password"))
                        {
                            skip = true;
                        }

                        if ((option.Value == "") || (option.Value == option.Default))
                        {
                            skip = true;
                        }

                        if (skip == false)
                        {
                            XmlElement itemNode = xmlDoc.CreateElement("option");
                            itemNode.SetAttribute("name", option.Code);
                            itemNode.SetAttribute("value", option.Value);
                            optionsNode.AppendChild(itemNode);
                        }
                    }

                    XmlElement providersNode = xmlDoc.CreateElement("providers");
                    rootNode.AppendChild(providersNode);
                    foreach (Provider provider in Engine.Instance.ProvidersManager.Providers)
                    {
                        XmlNode providerNode = xmlDoc.ImportNode(provider.Storage.DocumentElement, true);
                        providersNode.AppendChild(providerNode);
                    }

                    if (Engine.Instance.ProvidersManager.Providers.Count == 1)
                    {
                        if (Engine.Instance.ProvidersManager.Providers[0].Code == "AirVPN")
                        {
                            // Move providers->AirVPN to root.
                            XmlElement xmlAirVPN = providersNode.GetFirstElementByTagName("AirVPN");
                            if (xmlAirVPN != null)
                            {
                                foreach (XmlElement xmlChild in xmlAirVPN.ChildNodes)
                                {
                                    ExtensionsXml.XmlCopyElement(xmlChild, xmlDoc.DocumentElement);
                                }
                                providersNode.RemoveChild(xmlAirVPN);
                            }
                            if (providersNode.ChildNodes.Count == 0)
                            {
                                providersNode.ParentNode.RemoveChild(providersNode);
                            }
                        }
                    }

                    // Compute password
                    if ((SaveFormat == "v2s") && (Platform.Instance.OsCredentialSystemName() == ""))
                    {
                        SaveFormat = "v2n";
                    }

                    if ((Platform.Instance.OsCredentialSystemName() != "") && (m_loadFormat == "v2s") && (SaveFormat != "v2s"))
                    {
                        Platform.Instance.OsCredentialSystemDelete(Id);
                    }

                    if (SaveFormat == "v2n")
                    {
                        SavePassword = Constants.PasswordIfEmpty;
                    }
                    else if (SaveFormat == "v2s")
                    {
                        if ((m_loadFormat != "v2s") || (SavePassword == "") || (SavePassword != m_loadPassword))
                        {
                            SavePassword = RandomGenerator.GetRandomPassword();
                            if (Platform.Instance.OsCredentialSystemWrite(Id, SavePassword) == false)
                            {
                                // User not authorize the OS keychain, or fail. Revert to plain mode.
                                SaveFormat   = "v2n";
                                SavePassword = Constants.PasswordIfEmpty;
                            }
                        }
                    }

                    byte[] plainData = Encoding.UTF8.GetBytes(xmlDoc.OuterXml);
                    byte[] encrypted = Storage.EncodeFormat(SaveFormat, Id, plainData, SavePassword);
                    Platform.Instance.FileContentsWriteBytes(SavePath, encrypted);
                    Platform.Instance.FileEnsurePermission(SavePath, "600");

                    m_loadFormat   = SaveFormat;
                    m_loadPassword = SavePassword;
                }
                catch (Exception ex)
                {
                    Engine.Instance.Logs.Log(LogType.Fatal, LanguageManager.GetText("OptionsWriteFailed", SavePath, ex.Message));
                }
            }
        }