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);
        }
        /// <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}");
            }
        }
        /// <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}");
            }
        }
Esempio n. 4
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(),
                });
            }
        }