コード例 #1
0
        }// end of method CreateDefaultAppPreferences

        /// <summary>
        ///
        /// </summary>
        private void GetConfigurationData()
        {
            if (File.Exists(CONFIG_FILE))
            {
                StreamReader  srReader     = File.OpenText(CONFIG_FILE);
                Type          tType        = m_widget_config.GetType();
                XmlSerializer xsSerializer = new XmlSerializer(tType);
                object        oData        = xsSerializer.Deserialize(srReader);
                m_widget_config = (ConfigurationData)oData;
                srReader.Close();
            } // end of if block
        }     // end of method GetConfigurationData
コード例 #2
0
        }     // end of method CreateDefaultUserPreferences

        /// <summary>
        /// Create a default configuration file for the program
        /// </summary>
        private static void CreateDefaultAppConfiguration()
        {
            // if the directory does not exist then no previous configuration exists
            if (!Directory.Exists(SETTING_DIRECTORY))
            {
                Directory.CreateDirectory(SETTING_DIRECTORY);

                try
                {
                    using (var stream = File.Create(CONFIG_FILE)) { };
                }// end of try block
                catch (IOException e)
                {
                    UtilityMethod.LogMessage(UtilityMethod.LogLevel.SEVERE, e.Message,
                                             $"{TAG}::CreatedefaultPreferencesPropertiesFile [line: " +
                                             $"{UtilityMethod.GetExceptionLineNumber(e)}]");
                } // end of catch block
            }     // end of if block
            else if (!File.Exists(CONFIG_FILE))
            {
                try
                {
                    using (var stream = File.Create(CONFIG_FILE)) { };
                }// end of try block
                catch (IOException e)
                {
                    UtilityMethod.LogMessage(UtilityMethod.LogLevel.SEVERE, e.Message,
                                             $"{TAG}::CreatedefaultPreferencesPropertiesFile [line: " +
                                             $"{UtilityMethod.GetExceptionLineNumber(e)}]");
                } // end of catch block
            }     // end of if block

            try
            {
                using (StreamWriter swWriter = File.CreateText(CONFIG_FILE))
                {
                    int screenHeight = Screen.PrimaryScreen.WorkingArea.Height / 2;
                    int screenWidth  = Screen.PrimaryScreen.WorkingArea.Width / 2;

                    m_widget_config = new ConfigurationData(new Point(screenWidth, screenHeight));

                    Type          tType        = m_widget_config.GetType();
                    XmlSerializer xsSerializer = new XmlSerializer(tType);
                    xsSerializer.Serialize(swWriter, m_widget_config);
                } // end of using block
            }     // end of try block
            catch (FileNotFoundException e)
            {
                UtilityMethod.LogMessage(UtilityMethod.LogLevel.SEVERE, e.Message,
                                         $"{TAG}::CreatedefaultPreferencesPropertiesFile [line: " +
                                         $"{UtilityMethod.GetExceptionLineNumber(e)}]");
            }// end of catch block
            catch (IOException e)
            {
                UtilityMethod.LogMessage(UtilityMethod.LogLevel.SEVERE, e.Message,
                                         $"{TAG}::CreatedefaultPreferencesPropertiesFile [line: " +
                                         $"{UtilityMethod.GetExceptionLineNumber(e)}]");
            } // end of catch block
        }     // end of method CreateDefaultAppConfiguration
コード例 #3
0
        private ObservableCollection <T> GetCastedListOfProperties <T>(ConfigurationData configuration) where T : new()
        {
            var properties     = configuration.GetType().GetProperties();
            var propertyOfType = properties.FirstOrDefault(x => x.PropertyType == typeof(ObservableCollection <T>));

            if (propertyOfType == null)
            {
                throw new Exception("Something wrong with configuration");
            }
            return((ObservableCollection <T>)propertyOfType.GetValue(configuration));
        }
コード例 #4
0
        //TODO: Remove this section once users have moved off DPAPI
        private bool MigratedFromDPAPI(JToken jsonConfig)
        {
            bool isWindows = Environment.OSVersion.Platform == PlatformID.Win32NT;

            if (!isWindows && DotNetCoreUtil.IsRunningOnDotNetCore)
            {
                // User isn't running Windows, but is running on .NET Core framework, no access to the DPAPI, so don't bother trying to migrate
                return(false);
            }

            LoadValuesFromJson(jsonConfig, false);

            StringItem passwordPropertyValue = null;
            string     passwordValue         = "";

            try
            {
                // try dynamic items first (e.g. all cardigann indexers)
                passwordPropertyValue = (StringItem)configData.GetDynamicByName("password");

                if (passwordPropertyValue == null) // if there's no dynamic password try the static property
                {
                    passwordPropertyValue = (StringItem)configData.GetType().GetProperty("Password").GetValue(configData, null);

                    // protection is based on the item.Name value (property name might be different, example: Abnormal), so check the Name again
                    if (!string.Equals(passwordPropertyValue.Name, "password", StringComparison.InvariantCultureIgnoreCase))
                    {
                        logger.Debug($"Skipping non default password property (unencrpyted password) for [{ID}] while attempting migration");
                        return(false);
                    }
                }

                passwordValue = passwordPropertyValue.Value;
            }
            catch (Exception)
            {
                logger.Debug($"Unable to source password for [{ID}] while attempting migration, likely a tracker without a password setting");
                return(false);
            }

            if (!string.IsNullOrEmpty(passwordValue))
            {
                try
                {
                    protectionService.UnProtect(passwordValue);
                    //Password successfully unprotected using Microsoft.AspNetCore.DataProtection, no further action needed as we've already converted the password previously
                    return(false);
                }
                catch (Exception ex)
                {
                    if (ex.Message != "The provided payload cannot be decrypted because it was not protected with this protection provider.")
                    {
                        logger.Info($"Password could not be unprotected using Microsoft.AspNetCore.DataProtection - {ID} : " + ex);
                    }

                    logger.Info($"Attempting legacy Unprotect - {ID} : ");

                    try
                    {
                        string unprotectedPassword = protectionService.LegacyUnProtect(passwordValue);
                        //Password successfully unprotected using Windows/Mono DPAPI

                        passwordPropertyValue.Value = unprotectedPassword;
                        SaveConfig();
                        IsConfigured = true;

                        logger.Info($"Password successfully migrated for {ID}");

                        return(true);
                    }
                    catch (Exception exception)
                    {
                        logger.Info($"Password could not be unprotected using legacy DPAPI - {ID} : " + exception);
                    }
                }
            }

            return(false);
        }
コード例 #5
0
        //TODO: Remove this section once users have moved off DPAPI
        private bool MigratedFromDPAPI(JToken jsonConfig)
        {
            if (EnvironmentUtil.IsRunningLegacyOwin)
            {
                //Still running legacy Owin and using the DPAPI, we don't want to migrate
                logger.Debug(ID + " - Running Owin, no need to migrate from DPAPI");
                return(false);
            }

            Version dotNetVersion = Microsoft.Extensions.PlatformAbstractions.PlatformServices.Default.Application.RuntimeFramework.Version;
            bool    isWindows     = Environment.OSVersion.Platform == PlatformID.Win32NT;

            if (!isWindows && dotNetVersion.Major < 4)
            {
                // User isn't running Windows, but is running on .NET Core framework, no access to the DPAPI, so don't bother trying to migrate
                return(false);
            }

            LoadValuesFromJson(jsonConfig, false);

            object passwordPropertyValue = null;
            string passwordValue         = "";

            try
            {
                passwordPropertyValue = configData.GetType().GetProperty("Password").GetValue(configData, null);
                passwordValue         = passwordPropertyValue.GetType().GetProperty("Value").GetValue(passwordPropertyValue, null).ToString();
            }
            catch (Exception)
            {
                logger.Debug($"Unable to source password for [{ID}] while attempting migration, likely a public tracker");
                return(false);
            }

            if (!string.IsNullOrEmpty(passwordValue))
            {
                try
                {
                    protectionService.UnProtect(passwordValue);
                    //Password successfully unprotected using Microsoft.AspNetCore.DataProtection, no further action needed as we've already converted the password previously
                    return(false);
                }
                catch (Exception ex)
                {
                    if (ex.Message != "The provided payload cannot be decrypted because it was not protected with this protection provider.")
                    {
                        logger.Info($"Password could not be unprotected using Microsoft.AspNetCore.DataProtection - {ID} : " + ex);
                    }

                    logger.Info($"Attempting legacy Unprotect - {ID} : ");

                    try
                    {
                        string unprotectedPassword = protectionService.LegacyUnProtect(passwordValue);
                        //Password successfully unprotected using Windows/Mono DPAPI

                        passwordPropertyValue.GetType().GetProperty("Value").SetValue(passwordPropertyValue, unprotectedPassword);
                        SaveConfig();
                        IsConfigured = true;

                        logger.Info($"Password successfully migrated for {ID}");

                        return(true);
                    }
                    catch (Exception exception)
                    {
                        logger.Info($"Password could not be unprotected using legacy DPAPI - {ID} : " + exception);
                    }
                }
            }

            return(false);
        }
コード例 #6
0
        //TODO: Remove this section once users have moved off DPAPI
        private bool MigratedFromDPAPI(JToken jsonConfig)
        {
            var  currentAssembly   = Assembly.GetExecutingAssembly();
            bool runningLegacyOwin = new StackTrace().GetFrames()
                                     .Select(x => x.GetMethod().ReflectedType.Assembly).Distinct()
                                     .Where(x => x.GetReferencedAssemblies().Any(y => y.FullName == currentAssembly.FullName))
                                     .Where(x => x.ManifestModule.Name == "Jackett.dll" || x.ManifestModule.Name == "JackettConsole.exe")
                                     .Count() == 2;

            if (runningLegacyOwin)
            {
                //Still running legacy Owin and using the DPAPI, we don't want to migrate
                logger.Debug(ID + " - Running Owin, no need to migrate from DPAPI");
                return(false);
            }

            Version dotNetVersion = Microsoft.Extensions.PlatformAbstractions.PlatformServices.Default.Application.RuntimeFramework.Version;
            bool    isWindows     = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);

            if (!isWindows && dotNetVersion.Major < 4)
            {
                // User isn't running Windows, but is running on .NET Core framewrok, no access to the DPAPI, so don't bother trying to migrate
                return(false);
            }

            LoadValuesFromJson(jsonConfig, false);

            object passwordPropertyValue = null;
            string passwordValue         = "";

            try
            {
                passwordPropertyValue = configData.GetType().GetProperty("Password").GetValue(configData, null);
                passwordValue         = passwordPropertyValue.GetType().GetProperty("Value").GetValue(passwordPropertyValue, null).ToString();
            }
            catch (Exception ex)
            {
                logger.Warn($"Attempt to source password from json failed - {ID} : " + ex.ToString());
                return(false);
            }

            if (!string.IsNullOrEmpty(passwordValue))
            {
                try
                {
                    protectionService.UnProtect(passwordValue);
                    //Password successfully unprotected using Microsoft.AspNetCore.DataProtection, no further action needed as we've already converted the password previously
                    return(false);
                }
                catch (Exception ex)
                {
                    if (ex.Message != "The provided payload cannot be decrypted because it was not protected with this protection provider.")
                    {
                        logger.Info($"Password could not be unprotected using Microsoft.AspNetCore.DataProtection - {ID} : " + ex.ToString());
                    }

                    logger.Info($"Attempting legacy Unprotect - {ID} : ");

                    try
                    {
                        string unprotectedPassword = protectionService.LegacyUnProtect(passwordValue);
                        //Password successfully unprotected using Windows/Mono DPAPI

                        passwordPropertyValue.GetType().GetProperty("Value").SetValue(passwordPropertyValue, unprotectedPassword);
                        SaveConfig();
                        IsConfigured = true;

                        logger.Info($"Password successfully migrated for {ID}");

                        return(true);
                    }
                    catch (Exception exception)
                    {
                        logger.Info($"Password could not be unprotected using legacy DPAPI - {ID} : " + exception.ToString());
                    }
                }
            }

            return(false);
        }