Exemple #1
0
        public void Save()
        {
            string path = GetProfilePath();

            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.CommandLineOnly)
                        {
                            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 = Utils.XmlGetFirstElementByTagName(providersNode, "AirVPN");
                            if (xmlAirVPN != null)
                            {
                                foreach (XmlElement xmlChild in xmlAirVPN.ChildNodes)
                                {
                                    Utils.XmlCopyElement(xmlChild, xmlDoc.DocumentElement);
                                }
                                providersNode.RemoveChild(xmlAirVPN);
                            }
                            if (providersNode.ChildNodes.Count == 0)
                            {
                                providersNode.ParentNode.RemoveChild(providersNode);
                            }
                        }
                    }

                    xmlDoc.Save(path);

                    Platform.Instance.FileEnsurePermission(path, "600");
                }
                catch (Exception ex)
                {
                    Engine.Instance.Logs.Log(LogType.Fatal, MessagesFormatter.Format(Messages.OptionsWriteFailed, path, ex.Message));
                }
            }
        }
Exemple #2
0
        public void Load()
        {
            lock (this)
            {
                try
                {
                    XmlDocument xmlDoc = new XmlDocument();

                    Providers = xmlDoc.CreateElement("providers");

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

                    string path = GetProfilePath();

                    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;
                    }

                    xmlDoc.Load(path);

                    ResetAll(true);

                    Providers = Utils.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 = Utils.XmlGetFirstElementByTagName(xmlDoc.DocumentElement, "manifest");
                    if (xmlManifest != null)
                    {
                        XmlElement providerAirVpn = xmlDoc.CreateElement("AirVPN");
                        Providers.AppendChild(providerAirVpn);

                        Utils.XmlCopyElement(xmlManifest, providerAirVpn);

                        XmlElement xmlUser = Utils.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)
                        {
                            Utils.XmlCopyElement(xmlUser, providerAirVpn);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Debug.Trace(ex);
                    Engine.Instance.Logs.Log(LogType.Fatal, Messages.OptionsReverted);
                    ResetAll(true);
                }
            }
        }
Exemple #3
0
        public void Save()
        {
            string path = GetProfilePath();

            bool remember = GetBool("remember");

            lock (this)
            {
                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.CommandLineOnly)
                    {
                        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].GetCode() == "AirVPN")
                    {
                        // Move providers->AirVPN to root.
                        XmlElement xmlAirVPN = Utils.XmlGetFirstElementByTagName(providersNode, "AirVPN");
                        if (xmlAirVPN != null)
                        {
                            foreach (XmlElement xmlChild in xmlAirVPN.ChildNodes)
                            {
                                Utils.XmlCopyElement(xmlChild, xmlDoc.DocumentElement);
                            }
                            providersNode.RemoveChild(xmlAirVPN);
                        }
                        if (providersNode.ChildNodes.Count == 0)
                        {
                            providersNode.ParentNode.RemoveChild(providersNode);
                        }
                    }
                }

                xmlDoc.Save(path);
            }

            if (Platform.Instance.IsUnixSystem())
            {
                Platform.Instance.ShellCmd("chmod 600 \"" + SystemShell.EscapePath(path) + "\"");
            }
        }