/// <summary>
        /// Saves the Configuration object to file.
        /// </summary>
        /// <param name="config">A Configuration object.</param>
        public static void Save(Configuration config)
        {
            config.configs = SortByOnlineConfig(config.configs);

            FileStream   configFileStream   = null;
            StreamWriter configStreamWriter = null;

            try
            {
                configFileStream   = File.Open(CONFIG_FILE, FileMode.Create);
                configStreamWriter = new StreamWriter(configFileStream);
                var jsonString = JsonConvert.SerializeObject(config, Formatting.Indented);
                configStreamWriter.Write(jsonString);
                configStreamWriter.Flush();
                // NLog
                config.nLogConfig.SetLogLevel(config.isVerboseLogging ? verboseLogLevel : NLogConfig.LogLevel.Info);
                NLogConfig.SaveXML(config.nLogConfig);
            }
            catch (Exception e)
            {
                logger.LogUsefulException(e);
            }
            finally
            {
                if (configStreamWriter != null)
                {
                    configStreamWriter.Dispose();
                }
                if (configFileStream != null)
                {
                    configFileStream.Dispose();
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Load the NLog config xml file content
        /// </summary>
        public static NLogConfig LoadXML()
        {
            NLogConfig config = new NLogConfig();

            config.doc.Load(NLOG_CONFIG_FILE_NAME);
            config.logLevelElement    = (XmlElement)SelectSingleNode(config.doc, "//nlog:logger[@name='*']");
            config.logFileNameElement = (XmlElement)SelectSingleNode(config.doc, "//nlog:target[@name='file']");
            return(config);
        }
Ejemplo n.º 3
0
 public static void Save(Configuration config)
 {
     config.configs = SortByOnlineConfig(config.configs);
     if (config.index >= config.configs.Count)
     {
         config.index = config.configs.Count - 1;
     }
     if (config.index < -1)
     {
         config.index = -1;
     }
     if (config.index == -1 && string.IsNullOrEmpty(config.strategy))
     {
         config.index = 0;
     }
     config.isDefault = false;
     try
     {
         using (StreamWriter sw = new StreamWriter(File.Open(CONFIG_FILE, FileMode.Create)))
         {
             string jsonString = JsonConvert.SerializeObject(config, Formatting.Indented);
             sw.Write(jsonString);
             sw.Flush();
         }
         try
         {
             // apply changes to NLog.config
             config.nLogConfig.SetLogLevel(config.isVerboseLogging ? verboseLogLevel : NLogConfig.LogLevel.Info);
             NLogConfig.SaveXML(config.nLogConfig);
         }
         catch (Exception e)
         {
             logger.Error(e, "Cannot set the log level to NLog config file. Please check if the nlog config file exists with corresponding XML nodes.");
         }
     }
     catch (IOException e)
     {
         logger.LogUsefulException(e);
     }
 }
Ejemplo n.º 4
0
        public static Configuration Load()
        {
            Configuration config;

            try
            {
                string configContent = File.ReadAllText(CONFIG_FILE);
                config           = JsonConvert.DeserializeObject <Configuration>(configContent);
                config.isDefault = false;
                if (UpdateChecker.Asset.CompareVersion(UpdateChecker.Version, config.version ?? "0") > 0)
                {
                    config.updated = true;
                }

                if (config.configs.Count == 0)
                {
                    config.configs.Add(GetDefaultServer());
                }
                if (config.index == -1 && string.IsNullOrEmpty(config.strategy))
                {
                    config.index = 0;
                }
                if (!System.Net.Sockets.Socket.OSSupportsIPv6)
                {
                    config.isIPv6Enabled = false; // disable IPv6 if os not support
                }
                //TODO if remote host(server) do not support IPv6 (or DNS resolve AAAA TYPE record) disable IPv6?

                config.proxy.CheckConfig();
            }
            catch (Exception e)
            {
                if (!(e is FileNotFoundException))
                {
                    logger.LogUsefulException(e);
                }
                config = new Configuration();
                config.configs.Add(GetDefaultServer());
            }

            try
            {
                config.nLogConfig = NLogConfig.LoadXML();
                switch (config.nLogConfig.GetLogLevel())
                {
                case NLogConfig.LogLevel.Fatal:
                case NLogConfig.LogLevel.Error:
                case NLogConfig.LogLevel.Warn:
                case NLogConfig.LogLevel.Info:
                    config.isVerboseLogging = false;
                    break;

                case NLogConfig.LogLevel.Debug:
                case NLogConfig.LogLevel.Trace:
                    config.isVerboseLogging = true;
                    break;
                }
            }
            catch (Exception e)
            {
                // todo: route the error to UI since there is no log file in this scenario
                logger.Error(e, "Cannot get the log level from NLog config file. Please check if the nlog config file exists with corresponding XML nodes.");
            }

            return(config);
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Save the content to NLog config xml file
 /// </summary>
 public static void SaveXML(NLogConfig nLogConfig)
 {
     nLogConfig.doc.Save(NLOG_CONFIG_FILE_NAME);
 }
        /// <summary>
        /// Process the loaded configurations and set up things.
        /// </summary>
        /// <param name="config">A reference of Configuration object.</param>
        public static void Process(ref Configuration config)
        {
            // Verify if the configured geosite groups exist.
            // Reset to default if ANY one of the configured group doesn't exist.
            if (!ValidateGeositeGroupList(config.geositeDirectGroups))
            {
                ResetGeositeDirectGroup(ref config.geositeDirectGroups);
            }
            if (!ValidateGeositeGroupList(config.geositeProxiedGroups))
            {
                ResetGeositeProxiedGroup(ref config.geositeProxiedGroups);
            }

            // Mark the first run of a new version.
            var appVersion    = new Version(UpdateChecker.Version);
            var configVersion = new Version(config.version);

            if (appVersion.CompareTo(configVersion) > 0)
            {
                config.firstRunOnNewVersion = true;
            }
            // Add an empty server configuration
            if (config.configs.Count == 0)
            {
                config.configs.Add(GetDefaultServer());
            }
            // Selected server
            if (config.index == -1 && string.IsNullOrEmpty(config.strategy))
            {
                config.index = 0;
            }
            if (config.index >= config.configs.Count)
            {
                config.index = config.configs.Count - 1;
            }
            // Check OS IPv6 support
            if (!System.Net.Sockets.Socket.OSSupportsIPv6)
            {
                config.isIPv6Enabled = false;
            }
            config.proxy.CheckConfig();
            // Replace $version with the version number.
            config.userAgentString = config.userAgent.Replace("$version", config.version);

            // NLog log level
            try
            {
                config.nLogConfig = NLogConfig.LoadXML();
                switch (config.nLogConfig.GetLogLevel())
                {
                case NLogConfig.LogLevel.Fatal:
                case NLogConfig.LogLevel.Error:
                case NLogConfig.LogLevel.Warn:
                case NLogConfig.LogLevel.Info:
                    config.isVerboseLogging = false;
                    break;

                case NLogConfig.LogLevel.Debug:
                case NLogConfig.LogLevel.Trace:
                    config.isVerboseLogging = true;
                    break;
                }
            }
            catch (Exception e)
            {
                MessageBox.Show($"Cannot get the log level from NLog config file. Please check if the nlog config file exists with corresponding XML nodes.\n{e.Message}");
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Process the loaded configurations and set up things.
        /// </summary>
        /// <param name="config">A reference of Configuration object.</param>
        public static void Process(ref Configuration config)
        {
            // Verify if the configured geosite group exists.
            // Reset to default if no such group.
            if (!GeositeUpdater.CheckGeositeGroup(config.geositeGroup))
            {
#if DEBUG
                logger.Debug($"Current group: {config.geositeGroup}");
                foreach (var group in GeositeUpdater.Geosites.Keys)
                {
                    logger.Debug($"{group}");
                }
#endif
                config.geositeGroup = "geolocation-!cn";
                logger.Warn("The specified Geosite group doesn't exist. Using default group.");
            }

            // Mark the first run of a new version.
            if (UpdateChecker.Asset.CompareVersion(UpdateChecker.Version, config.version ?? "0") > 0)
            {
                config.firstRunOnNewVersion = true;
            }
            // Add an empty server configuration
            if (config.configs.Count == 0)
            {
                config.configs.Add(GetDefaultServer());
            }
            // Selected server
            if (config.index == -1 && string.IsNullOrEmpty(config.strategy))
            {
                config.index = 0;
            }
            if (config.index >= config.configs.Count)
            {
                config.index = config.configs.Count - 1;
            }
            // Check OS IPv6 support
            if (!System.Net.Sockets.Socket.OSSupportsIPv6)
            {
                config.isIPv6Enabled = false;
            }
            config.proxy.CheckConfig();
            // Replace $version with the version number.
            config.userAgentString = config.userAgent.Replace("$version", config.version);

            // NLog log level
            try
            {
                config.nLogConfig = NLogConfig.LoadXML();
                switch (config.nLogConfig.GetLogLevel())
                {
                case NLogConfig.LogLevel.Fatal:
                case NLogConfig.LogLevel.Error:
                case NLogConfig.LogLevel.Warn:
                case NLogConfig.LogLevel.Info:
                    config.isVerboseLogging = false;
                    break;

                case NLogConfig.LogLevel.Debug:
                case NLogConfig.LogLevel.Trace:
                    config.isVerboseLogging = true;
                    break;
                }
            }
            catch (Exception e)
            {
                MessageBox.Show($"Cannot get the log level from NLog config file. Please check if the nlog config file exists with corresponding XML nodes.\n{e.Message}");
            }
        }
Ejemplo n.º 8
0
        public static Configuration Load()
        {
            try
            {
                string        configContent = File.ReadAllText(CONFIG_FILE);
                Configuration config        = JsonConvert.DeserializeObject <Configuration>(configContent);
                config.isDefault = false;

                if (config.configs == null)
                {
                    config.configs = new List <Server>();
                }
                if (config.configs.Count == 0)
                {
                    config.configs.Add(GetDefaultServer());
                }
                if (config.localPort == 0)
                {
                    config.localPort = 1080;
                }
                if (config.index == -1 && config.strategy == null)
                {
                    config.index = 0;
                }
                if (config.logViewer == null)
                {
                    config.logViewer = new LogViewerConfig();
                }
                if (config.proxy == null)
                {
                    config.proxy = new ProxyConfig();
                }
                if (config.hotkey == null)
                {
                    config.hotkey = new HotkeyConfig();
                }
                if (!System.Net.Sockets.Socket.OSSupportsIPv6)
                {
                    config.isIPv6Enabled = false; // disable IPv6 if os not support
                }
                //TODO if remote host(server) do not support IPv6 (or DNS resolve AAAA TYPE record) disable IPv6?

                config.proxy.CheckConfig();

                try
                {
                    config.nLogConfig = NLogConfig.LoadXML();
                    switch (config.nLogConfig.GetLogLevel())
                    {
                    case NLogConfig.LogLevel.Fatal:
                    case NLogConfig.LogLevel.Error:
                    case NLogConfig.LogLevel.Warn:
                    case NLogConfig.LogLevel.Info:
                        config.isVerboseLogging = false;
                        break;

                    case NLogConfig.LogLevel.Debug:
                    case NLogConfig.LogLevel.Trace:
                        config.isVerboseLogging = true;
                        break;
                    }
                }
                catch (Exception e)
                {
                    // todo: route the error to UI since there is no log file in this scenario
                    logger.Error(e, "Cannot get the log level from NLog config file. Please check if the nlog config file exists with corresponding XML nodes.");
                }

                return(config);
            }
            catch (Exception e)
            {
                if (!(e is FileNotFoundException))
                {
                    logger.LogUsefulException(e);
                }
                return(new Configuration
                {
                    index = 0,
                    isDefault = true,
                    localPort = 1080,
                    autoCheckUpdate = true,
                    configs = new List <Server>()
                    {
                        GetDefaultServer()
                    },
                    logViewer = new LogViewerConfig(),
                    proxy = new ProxyConfig(),
                    hotkey = new HotkeyConfig(),
                });
            }
        }