Esempio n. 1
0
        /// <summary>
        /// Load a configuration from local/remote location.
        /// </summary>
        /// <param name="configName">Configuration name</param>
        /// <param name="configUri">Configuration File URI</param>
        /// <param name="configType">Configuration Type</param>
        /// <param name="version">Configuration Version (expected)</param>
        /// <param name="settings">Configuration Settings</param>
        /// <param name="password">Password (if required)</param>
        /// <returns>Loaded Configruation</returns>
        public Configuration Load(string configName, string configUri,
                                  EConfigType configType, Version version,
                                  ConfigurationSettings settings, string password = null)
        {
            Configuration config = null;

            if (!loadedConfigs.ContainsKey(configName))
            {
                lock (loadedConfigs)
                {
                    if (!loadedConfigs.ContainsKey(configName))
                    {
                        config = loader.Load(configName, configUri, configType, version, settings, password);
                        ConfigLoaded(config);
                    }
                }
            }
            config = loadedConfigs[configName];
            if (!config.Header.Version.Equals(version))
            {
                throw new ConfigurationException(String.Format("Versions not compatible. [expected=%s][actual=%s]",
                                                               version.ToString(), config.Header.Version.ToString()));
            }
            return(config);
        }
Esempio n. 2
0
        /// <summary>
        /// Load a configuration from local/remote location.
        /// Will load using default settings.
        /// </summary>
        /// <param name="configName">Configuration name</param>
        /// <param name="configUri">Configuration File URI</param>
        /// <param name="configType">Configuration Type</param>
        /// <param name="version">Configuration Version (expected)</param>
        /// <param name="password">Password (if required)</param>
        /// <returns>Loaded Configruation</returns>
        public Configuration Load(string configName, string configUri,
                                  EConfigType configType, Version version,
                                  string password = null)
        {
            ConfigurationSettings settings = new ConfigurationSettings();

            return(Load(configName, configUri, configType, version, settings, password));
        }
Esempio n. 3
0
 /// <summary>
 /// Parse the passed string as the config type.
 /// </summary>
 /// <param name="type">Type place holder</param>
 /// <param name="value">String value</param>
 /// <returns>Parsed Enum Value</returns>
 public static EConfigType Parse(this EConfigType type, string value)
 {
     if (!String.IsNullOrWhiteSpace(value))
     {
         value = value.Trim().ToUpper();
         return((EConfigType)Enum.Parse(typeof(EConfigType), value));
     }
     return(default(EConfigType));
 }
Esempio n. 4
0
 /// <summary>
 /// Get a new configuration writer instance of the parser for the specified configuration type.
 /// </summary>
 /// <param name="type">Configuration type</param>
 /// <returns>Writer instance</returns>
 public static AbstractConfigWriter GetWriter(EConfigType type)
 {
     switch (type)
     {
     case EConfigType.XML:
         return(new XmlConfigWriter());
     }
     return(null);
 }
Esempio n. 5
0
        /// <summary>
        /// Method will try to get the configuration parser based on the extension of the
        /// specified configuration file name.
        /// </summary>
        /// <param name="filename">Configuration filename</param>
        /// <returns>Configuration Parser instance</returns>
        public static AbstractConfigParser GetParser(string filename)
        {
            Preconditions.CheckArgument(filename);

            string ext = Path.GetExtension(filename);

            if (!String.IsNullOrWhiteSpace(ext))
            {
                EConfigType type = EConfigType.Unknown.Parse(ext);
                return(GetParser(type));
            }
            return(null);
        }
Esempio n. 6
0
        public ECoreType GetCoreType(EConfigType eConfigType)
        {
            if (coreTypeItem == null)
            {
                return(ECoreType.Xray);
            }
            var item = coreTypeItem.FirstOrDefault(it => it.configType == eConfigType);

            if (item == null)
            {
                return(ECoreType.Xray);
            }
            return(item.coreType);
        }
Esempio n. 7
0
        /// <summary>
        /// Get a new configuration parser instance of the parser for the specified configuration source type.
        /// </summary>
        /// <param name="type">Configuration source type</param>
        /// <returns>Parser instance</returns>
        public static AbstractConfigParser GetParser(EConfigType type)
        {
            if (type != default(EConfigType))
            {
                switch (type)
                {
                case EConfigType.JSON:
                    return(new JSONConfigParser());

                case EConfigType.XML:
                    return(new XmlConfigParser());
                }
            }
            return(null);
        }
        // Returns true if an error occured
        private static bool loadSections(BinaryReader reader, MainWindow mainWindow)
        {
            bool error = false;

            // Get number of config sections
            int count = reader.ReadInt32();

            // For every config section
            for (int i = 0; i < count; i++)
            {
                // Get config section type
                EConfigType type = (EConfigType)reader.ReadInt32();

                // Get length of data for specific section
                int bufferLength = reader.ReadInt32();

                // Read buffer for section to isolate from others
                byte[] buffer = reader.ReadBytes(bufferLength);

                // Find section for loading
                IConfig config = ConfigSections.FirstOrDefault(x => x.Type == type);

                // Possibly removed section, skip it
                if (config == null)
                {
                    continue;
                }

                // Create isolated binary reader for section loading
                using (MemoryStream bufferStream = new MemoryStream(buffer))
                    using (BinaryReader sectionReader = new BinaryReader(bufferStream))
                    {
                        // Set main window
                        config.MainWindow = mainWindow;

                        try
                        {
                            // Load config
                            config.Load(sectionReader);
                        }
                        catch { error = true; }
                    }
            }

            return(error);
        }
Esempio n. 9
0
        public ECoreType GetCoreType(VmessItem vmessItem, EConfigType eConfigType)
        {
            if (vmessItem != null && vmessItem.coreType != null)
            {
                return((ECoreType)vmessItem.coreType);
            }

            if (_config.coreTypeItem == null)
            {
                return(ECoreType.Xray);
            }
            var item = _config.coreTypeItem.FirstOrDefault(it => it.configType == eConfigType);

            if (item == null)
            {
                return(ECoreType.Xray);
            }
            return(item.coreType);
        }
Esempio n. 10
0
        private void ShowServerForm(EConfigType configType, int index)
        {
            BaseServerForm fm;

            if (configType == EConfigType.Custom)
            {
                fm = new AddServer2Form();
            }
            else
            {
                fm = new AddServerForm();
            }
            fm.vmessItem   = index >= 0 ? lstVmess[index] : null;
            fm.groupId     = groupId;
            fm.eConfigType = configType;
            if (fm.ShowDialog() == DialogResult.OK)
            {
                RefreshServers();
                _ = LoadV2ray();
            }
        }
Esempio n. 11
0
 /// <summary>
 /// Initialize this client environment from the specified configuration file and version.
 /// </summary>
 /// <param name="configfile">Configuration file path.</param>
 /// <param name="type">Configuration file type (in-case file type cannot be deciphered).</param>
 /// <param name="version">Configuration version (expected)</param>
 /// <param name="password">Password, if configuration has encrypted nodes.</param>
 protected void Init(string configfile,
                     EConfigType type,
                     Version version, string password = null)
 {
     try
     {
         AbstractConfigParser parser = ConfigProviderFactory.GetParser(type);
         if (parser == null)
         {
             throw new ConfigurationException(String.Format(
                                                  "Cannot get configuration parser instance. [file={0}]",
                                                  configfile));
         }
         Init(parser, configfile, version, password);
     }
     catch (Exception e)
     {
         state.SetError(e);
         throw new ConfigurationException(e);
     }
 }
Esempio n. 12
0
        /// <summary>
        /// Load a configuration from local/remote location.
        /// </summary>
        /// <param name="configName">Configuration name</param>
        /// <param name="configUri">Configuration File URI</param>
        /// <param name="configType">Configuration Type</param>
        /// <param name="version">Configuration Version (expected)</param>
        /// <param name="settings">Configuration Settings</param>
        /// <param name="password">Password (if required)</param>
        /// <returns>Loaded Configruation</returns>
        public Configuration Load(string configName, string configUri,
                                  EConfigType configType, Version version,
                                  ConfigurationSettings settings, string password = null)
        {
            Preconditions.CheckArgument(configName);
            Preconditions.CheckArgument(configUri);
            Preconditions.CheckArgument(configType);
            Preconditions.CheckArgument(version);

            LogUtils.Info(String.Format("Loading Configuration. [name={0}][version={1}][uri={2}]", configName, version.ToString(), configUri));

            Uri uri = new Uri(configUri);

            using (AbstractReader reader = ConfigProviderFactory.GetReader(uri))
            {
                AbstractConfigParser parser = ConfigProviderFactory.GetParser(configType);
                Postconditions.CheckCondition(parser);

                parser.Parse(configName, reader, version, settings, password);

                return(parser.GetConfiguration());
            }
        }