2019-06-14 03:54:25 +00:00
|
|
|
using System;
|
2013-05-23 05:12:01 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
2014-10-05 05:25:54 +00:00
|
|
|
using System.Text.RegularExpressions;
|
2013-09-11 05:52:10 +00:00
|
|
|
using System.Xml;
|
2013-05-23 05:12:01 +00:00
|
|
|
using System.Xml.Linq;
|
|
|
|
using NzbDrone.Common.Cache;
|
2015-01-23 05:34:10 +00:00
|
|
|
using NzbDrone.Common.Disk;
|
2013-06-28 00:04:52 +00:00
|
|
|
using NzbDrone.Common.EnvironmentInfo;
|
2014-12-02 06:26:25 +00:00
|
|
|
using NzbDrone.Common.Extensions;
|
2015-01-26 02:03:21 +00:00
|
|
|
using NzbDrone.Core.Authentication;
|
2013-07-24 15:08:31 +00:00
|
|
|
using NzbDrone.Core.Configuration.Events;
|
2013-08-25 06:05:49 +00:00
|
|
|
using NzbDrone.Core.Lifecycle;
|
2014-03-14 04:23:47 +00:00
|
|
|
using NzbDrone.Core.Messaging.Commands;
|
2013-09-14 06:36:07 +00:00
|
|
|
using NzbDrone.Core.Messaging.Events;
|
2014-04-28 06:34:29 +00:00
|
|
|
using NzbDrone.Core.Update;
|
2013-09-11 06:33:47 +00:00
|
|
|
|
2013-05-23 05:12:01 +00:00
|
|
|
namespace NzbDrone.Core.Configuration
|
|
|
|
{
|
2014-03-14 04:23:47 +00:00
|
|
|
public interface IConfigFileProvider : IHandleAsync<ApplicationStartedEvent>,
|
|
|
|
IExecute<ResetApiKeyCommand>
|
2013-05-23 05:12:01 +00:00
|
|
|
{
|
|
|
|
Dictionary<string, object> GetConfigDictionary();
|
|
|
|
void SaveConfigDictionary(Dictionary<string, object> configValues);
|
|
|
|
|
2014-10-01 18:34:10 +00:00
|
|
|
string BindAddress { get; }
|
2013-07-14 07:00:50 +00:00
|
|
|
int Port { get; }
|
2013-09-22 19:57:03 +00:00
|
|
|
int SslPort { get; }
|
|
|
|
bool EnableSsl { get; }
|
2013-07-14 07:00:50 +00:00
|
|
|
bool LaunchBrowser { get; }
|
2015-01-26 02:03:21 +00:00
|
|
|
AuthenticationType AuthenticationMethod { get; }
|
2014-09-27 19:39:29 +00:00
|
|
|
bool AnalyticsEnabled { get; }
|
2013-07-24 15:08:31 +00:00
|
|
|
string LogLevel { get; }
|
2019-07-15 01:18:03 +00:00
|
|
|
string ConsoleLogLevel { get; }
|
2019-12-18 21:56:41 +00:00
|
|
|
bool LogSql { get; }
|
|
|
|
int LogRotate { get; }
|
2019-09-03 02:22:25 +00:00
|
|
|
bool FilterSentryEvents { get; }
|
2013-08-10 05:00:07 +00:00
|
|
|
string Branch { get; }
|
2013-09-20 07:31:02 +00:00
|
|
|
string ApiKey { get; }
|
2019-10-14 20:21:00 +00:00
|
|
|
string SslCertPath { get; }
|
|
|
|
string SslCertPassword { get; }
|
2014-01-02 06:56:19 +00:00
|
|
|
string UrlBase { get; }
|
2015-07-22 02:42:38 +00:00
|
|
|
string UiFolder { get; }
|
2015-10-03 17:45:26 +00:00
|
|
|
bool UpdateAutomatically { get; }
|
2014-04-28 06:34:29 +00:00
|
|
|
UpdateMechanism UpdateMechanism { get; }
|
2015-10-03 17:45:26 +00:00
|
|
|
string UpdateScriptPath { get; }
|
2013-05-23 05:12:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public class ConfigFileProvider : IConfigFileProvider
|
|
|
|
{
|
2013-09-21 02:07:42 +00:00
|
|
|
public const string CONFIG_ELEMENT_NAME = "Config";
|
2013-08-25 06:05:49 +00:00
|
|
|
|
2013-09-14 06:36:07 +00:00
|
|
|
private readonly IEventAggregator _eventAggregator;
|
2015-01-23 05:34:10 +00:00
|
|
|
private readonly IDiskProvider _diskProvider;
|
2013-05-23 05:12:01 +00:00
|
|
|
private readonly ICached<string> _cache;
|
|
|
|
|
|
|
|
private readonly string _configFile;
|
2014-10-05 05:25:54 +00:00
|
|
|
private static readonly Regex HiddenCharacterRegex = new Regex("[^a-z0-9]", RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
2013-05-23 05:12:01 +00:00
|
|
|
|
2015-01-23 05:34:10 +00:00
|
|
|
private static readonly object Mutex = new object();
|
|
|
|
|
|
|
|
public ConfigFileProvider(IAppFolderInfo appFolderInfo,
|
|
|
|
ICacheManager cacheManager,
|
|
|
|
IEventAggregator eventAggregator,
|
|
|
|
IDiskProvider diskProvider)
|
2013-05-23 05:12:01 +00:00
|
|
|
{
|
2014-04-01 20:07:41 +00:00
|
|
|
_cache = cacheManager.GetCache<string>(GetType());
|
2013-09-14 06:36:07 +00:00
|
|
|
_eventAggregator = eventAggregator;
|
2015-01-23 05:34:10 +00:00
|
|
|
_diskProvider = diskProvider;
|
2013-09-14 00:10:39 +00:00
|
|
|
_configFile = appFolderInfo.GetConfigPath();
|
2013-05-23 05:12:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public Dictionary<string, object> GetConfigDictionary()
|
|
|
|
{
|
|
|
|
var dict = new Dictionary<string, object>(StringComparer.InvariantCultureIgnoreCase);
|
|
|
|
|
|
|
|
var type = GetType();
|
|
|
|
var properties = type.GetProperties();
|
|
|
|
|
|
|
|
foreach (var propertyInfo in properties)
|
|
|
|
{
|
|
|
|
var value = propertyInfo.GetValue(this, null);
|
|
|
|
|
|
|
|
dict.Add(propertyInfo.Name, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return dict;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SaveConfigDictionary(Dictionary<string, object> configValues)
|
|
|
|
{
|
|
|
|
_cache.Clear();
|
|
|
|
|
|
|
|
var allWithDefaults = GetConfigDictionary();
|
|
|
|
|
|
|
|
foreach (var configValue in configValues)
|
|
|
|
{
|
2014-03-14 04:23:47 +00:00
|
|
|
if (configValue.Key.Equals("ApiKey", StringComparison.InvariantCultureIgnoreCase))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-05-23 05:12:01 +00:00
|
|
|
object currentValue;
|
|
|
|
allWithDefaults.TryGetValue(configValue.Key, out currentValue);
|
2019-12-22 22:08:53 +00:00
|
|
|
if (currentValue == null)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2013-05-23 05:12:01 +00:00
|
|
|
|
|
|
|
var equal = configValue.Value.ToString().Equals(currentValue.ToString());
|
|
|
|
|
|
|
|
if (!equal)
|
|
|
|
{
|
|
|
|
SetValue(configValue.Key.FirstCharToUpper(), configValue.Value.ToString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-14 06:36:07 +00:00
|
|
|
_eventAggregator.PublishEvent(new ConfigFileSavedEvent());
|
2013-05-23 05:12:01 +00:00
|
|
|
}
|
|
|
|
|
2014-10-01 18:34:10 +00:00
|
|
|
public string BindAddress
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
const string defaultValue = "*";
|
|
|
|
|
|
|
|
string bindAddress = GetValue("BindAddress", defaultValue);
|
|
|
|
if (string.IsNullOrWhiteSpace(bindAddress))
|
|
|
|
{
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return bindAddress;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-03 15:06:06 +00:00
|
|
|
public int Port => GetValueInt("Port", 7878);
|
2013-05-23 05:12:01 +00:00
|
|
|
|
2016-12-09 06:54:15 +00:00
|
|
|
public int SslPort => GetValueInt("SslPort", 9898);
|
2013-09-22 19:57:03 +00:00
|
|
|
|
2016-12-09 06:54:15 +00:00
|
|
|
public bool EnableSsl => GetValueBoolean("EnableSsl", false);
|
2013-09-22 19:57:03 +00:00
|
|
|
|
2016-12-09 06:54:15 +00:00
|
|
|
public bool LaunchBrowser => GetValueBoolean("LaunchBrowser", true);
|
2013-05-23 05:12:01 +00:00
|
|
|
|
2017-06-11 07:30:00 +00:00
|
|
|
public string ApiKey
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
var apiKey = GetValue("ApiKey", GenerateApiKey());
|
|
|
|
|
|
|
|
if (apiKey.IsNullOrWhiteSpace())
|
|
|
|
{
|
|
|
|
apiKey = GenerateApiKey();
|
|
|
|
SetValue("ApiKey", apiKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
return apiKey;
|
|
|
|
}
|
|
|
|
}
|
2013-09-20 07:31:02 +00:00
|
|
|
|
2015-01-26 02:03:21 +00:00
|
|
|
public AuthenticationType AuthenticationMethod
|
2013-09-14 00:10:39 +00:00
|
|
|
{
|
2015-01-26 02:03:21 +00:00
|
|
|
get
|
|
|
|
{
|
|
|
|
var enabled = GetValueBoolean("AuthenticationEnabled", false, false);
|
2013-09-14 00:10:39 +00:00
|
|
|
|
2015-01-26 02:03:21 +00:00
|
|
|
if (enabled)
|
|
|
|
{
|
|
|
|
SetValue("AuthenticationMethod", AuthenticationType.Basic);
|
|
|
|
return AuthenticationType.Basic;
|
|
|
|
}
|
2019-08-07 02:20:47 +00:00
|
|
|
|
2015-01-26 02:03:21 +00:00
|
|
|
return GetValueEnum("AuthenticationMethod", AuthenticationType.None);
|
|
|
|
}
|
2013-05-23 05:12:01 +00:00
|
|
|
}
|
|
|
|
|
2016-12-09 06:54:15 +00:00
|
|
|
public bool AnalyticsEnabled => GetValueBoolean("AnalyticsEnabled", true, persist: false);
|
2014-09-27 19:39:29 +00:00
|
|
|
|
2017-01-08 18:42:46 +00:00
|
|
|
// TODO: Change back to "master" for the first stable release.
|
2020-04-25 20:23:19 +00:00
|
|
|
public string Branch => GetValue("Branch", "aphrodite").ToLowerInvariant();
|
2013-07-30 02:12:16 +00:00
|
|
|
|
2020-08-03 20:55:26 +00:00
|
|
|
public string LogLevel => GetValue("LogLevel", "info").ToLowerInvariant();
|
2019-07-15 01:18:03 +00:00
|
|
|
public string ConsoleLogLevel => GetValue("ConsoleLogLevel", string.Empty, persist: false);
|
2019-12-18 21:56:41 +00:00
|
|
|
public bool LogSql => GetValueBoolean("LogSql", false, persist: false);
|
|
|
|
public int LogRotate => GetValueInt("LogRotate", 50, persist: false);
|
2019-09-03 02:22:25 +00:00
|
|
|
public bool FilterSentryEvents => GetValueBoolean("FilterSentryEvents", true, persist: false);
|
2019-10-14 20:21:00 +00:00
|
|
|
public string SslCertPath => GetValue("SslCertPath", "");
|
|
|
|
public string SslCertPassword => GetValue("SslCertPassword", "");
|
2013-09-22 19:57:03 +00:00
|
|
|
|
2014-01-02 06:56:19 +00:00
|
|
|
public string UrlBase
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2014-07-25 01:02:47 +00:00
|
|
|
var urlBase = GetValue("UrlBase", "").Trim('/');
|
2014-01-02 06:56:19 +00:00
|
|
|
|
2014-07-25 01:02:47 +00:00
|
|
|
if (urlBase.IsNullOrWhiteSpace())
|
2014-01-02 06:56:19 +00:00
|
|
|
{
|
|
|
|
return urlBase;
|
|
|
|
}
|
|
|
|
|
|
|
|
return "/" + urlBase.Trim('/').ToLower();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-14 20:20:59 +00:00
|
|
|
public string UiFolder => BuildInfo.IsDebug ? Path.Combine("..", "UI") : "UI";
|
2015-07-22 02:42:38 +00:00
|
|
|
|
2016-12-09 06:54:15 +00:00
|
|
|
public bool UpdateAutomatically => GetValueBoolean("UpdateAutomatically", false, false);
|
2014-04-28 06:34:29 +00:00
|
|
|
|
2016-12-09 06:54:15 +00:00
|
|
|
public UpdateMechanism UpdateMechanism => GetValueEnum("UpdateMechanism", UpdateMechanism.BuiltIn, false);
|
2014-04-28 06:34:29 +00:00
|
|
|
|
2019-06-14 03:54:25 +00:00
|
|
|
public string UpdateScriptPath => GetValue("UpdateScriptPath", "", false);
|
2014-04-28 06:34:29 +00:00
|
|
|
|
2019-12-18 21:56:41 +00:00
|
|
|
public int GetValueInt(string key, int defaultValue, bool persist = true)
|
2013-05-23 05:12:01 +00:00
|
|
|
{
|
2019-12-18 21:56:41 +00:00
|
|
|
return Convert.ToInt32(GetValue(key, defaultValue, persist));
|
2013-05-23 05:12:01 +00:00
|
|
|
}
|
|
|
|
|
2013-09-14 00:10:39 +00:00
|
|
|
public bool GetValueBoolean(string key, bool defaultValue, bool persist = true)
|
2013-05-23 05:12:01 +00:00
|
|
|
{
|
2013-09-14 00:10:39 +00:00
|
|
|
return Convert.ToBoolean(GetValue(key, defaultValue, persist));
|
2013-05-23 05:12:01 +00:00
|
|
|
}
|
|
|
|
|
2014-04-28 06:34:29 +00:00
|
|
|
public T GetValueEnum<T>(string key, T defaultValue, bool persist = true)
|
2013-05-23 05:12:01 +00:00
|
|
|
{
|
2014-04-28 06:34:29 +00:00
|
|
|
return (T)Enum.Parse(typeof(T), GetValue(key, defaultValue), persist);
|
2013-05-23 05:12:01 +00:00
|
|
|
}
|
|
|
|
|
2013-09-14 00:10:39 +00:00
|
|
|
public string GetValue(string key, object defaultValue, bool persist = true)
|
2013-05-23 05:12:01 +00:00
|
|
|
{
|
|
|
|
return _cache.Get(key, () =>
|
2013-07-24 00:35:35 +00:00
|
|
|
{
|
2013-09-11 00:40:21 +00:00
|
|
|
var xDoc = LoadConfigFile();
|
2013-08-25 06:05:49 +00:00
|
|
|
var config = xDoc.Descendants(CONFIG_ELEMENT_NAME).Single();
|
2013-05-23 05:12:01 +00:00
|
|
|
|
2013-07-24 00:35:35 +00:00
|
|
|
var parentContainer = config;
|
2013-05-23 05:12:01 +00:00
|
|
|
|
2013-07-24 00:35:35 +00:00
|
|
|
var valueHolder = parentContainer.Descendants(key).ToList();
|
2013-05-23 05:12:01 +00:00
|
|
|
|
2013-07-24 00:35:35 +00:00
|
|
|
if (valueHolder.Count() == 1)
|
2014-04-28 06:34:29 +00:00
|
|
|
{
|
2014-01-08 01:37:51 +00:00
|
|
|
return valueHolder.First().Value.Trim();
|
2014-04-28 06:34:29 +00:00
|
|
|
}
|
2013-05-23 05:12:01 +00:00
|
|
|
|
2013-07-24 00:35:35 +00:00
|
|
|
//Save the value
|
2013-09-14 00:10:39 +00:00
|
|
|
if (persist)
|
|
|
|
{
|
|
|
|
SetValue(key, defaultValue);
|
|
|
|
}
|
2013-05-23 05:12:01 +00:00
|
|
|
|
2013-07-24 00:35:35 +00:00
|
|
|
//return the default value
|
|
|
|
return defaultValue.ToString();
|
|
|
|
});
|
2013-05-23 05:12:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void SetValue(string key, object value)
|
|
|
|
{
|
2014-01-08 01:37:51 +00:00
|
|
|
var valueString = value.ToString().Trim();
|
2013-09-11 00:40:21 +00:00
|
|
|
var xDoc = LoadConfigFile();
|
2013-08-25 06:05:49 +00:00
|
|
|
var config = xDoc.Descendants(CONFIG_ELEMENT_NAME).Single();
|
2013-05-23 05:12:01 +00:00
|
|
|
|
|
|
|
var parentContainer = config;
|
|
|
|
|
|
|
|
var keyHolder = parentContainer.Descendants(key);
|
|
|
|
|
|
|
|
if (keyHolder.Count() != 1)
|
|
|
|
{
|
2014-01-08 01:37:51 +00:00
|
|
|
parentContainer.Add(new XElement(key, valueString));
|
2013-05-23 05:12:01 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-01-08 01:37:51 +00:00
|
|
|
parentContainer.Descendants(key).Single().Value = valueString;
|
2013-05-23 05:12:01 +00:00
|
|
|
}
|
|
|
|
|
2014-01-08 01:37:51 +00:00
|
|
|
_cache.Set(key, valueString);
|
2013-05-23 05:12:01 +00:00
|
|
|
|
2015-01-23 05:34:10 +00:00
|
|
|
SaveConfigFile(xDoc);
|
2013-05-23 05:12:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void SetValue(string key, Enum value)
|
|
|
|
{
|
|
|
|
SetValue(key, value.ToString().ToLower());
|
|
|
|
}
|
|
|
|
|
|
|
|
private void EnsureDefaultConfigFile()
|
|
|
|
{
|
|
|
|
if (!File.Exists(_configFile))
|
|
|
|
{
|
2013-09-21 02:07:42 +00:00
|
|
|
SaveConfigDictionary(GetConfigDictionary());
|
2013-05-23 05:12:01 +00:00
|
|
|
}
|
|
|
|
}
|
2013-08-25 06:05:49 +00:00
|
|
|
|
|
|
|
private void DeleteOldValues()
|
|
|
|
{
|
2013-09-11 00:40:21 +00:00
|
|
|
var xDoc = LoadConfigFile();
|
2013-08-25 06:05:49 +00:00
|
|
|
var config = xDoc.Descendants(CONFIG_ELEMENT_NAME).Single();
|
|
|
|
|
|
|
|
var type = GetType();
|
|
|
|
var properties = type.GetProperties();
|
|
|
|
|
|
|
|
foreach (var configValue in config.Descendants().ToList())
|
|
|
|
{
|
|
|
|
var name = configValue.Name.LocalName;
|
|
|
|
|
|
|
|
if (!properties.Any(p => p.Name == name))
|
|
|
|
{
|
|
|
|
config.Descendants(name).Remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 05:34:10 +00:00
|
|
|
SaveConfigFile(xDoc);
|
2013-08-25 06:05:49 +00:00
|
|
|
}
|
|
|
|
|
2013-09-11 00:40:21 +00:00
|
|
|
private XDocument LoadConfigFile()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2015-01-23 05:34:10 +00:00
|
|
|
lock (Mutex)
|
|
|
|
{
|
|
|
|
if (_diskProvider.FileExists(_configFile))
|
|
|
|
{
|
2016-05-21 07:22:12 +00:00
|
|
|
var contents = _diskProvider.ReadAllText(_configFile);
|
|
|
|
|
|
|
|
if (contents.IsNullOrWhiteSpace())
|
|
|
|
{
|
2017-01-03 19:24:55 +00:00
|
|
|
throw new InvalidConfigFileException($"{_configFile} is empty. Please delete the config file and Radarr will recreate it.");
|
2016-05-21 07:22:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (contents.All(char.IsControl))
|
|
|
|
{
|
2017-01-03 19:24:55 +00:00
|
|
|
throw new InvalidConfigFileException($"{_configFile} is corrupt. Please delete the config file and Radarr will recreate it.");
|
2016-05-21 07:22:12 +00:00
|
|
|
}
|
|
|
|
|
2015-01-22 18:46:55 +00:00
|
|
|
return XDocument.Parse(_diskProvider.ReadAllText(_configFile));
|
2015-01-23 05:34:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var xDoc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"));
|
|
|
|
xDoc.Add(new XElement(CONFIG_ELEMENT_NAME));
|
|
|
|
|
|
|
|
return xDoc;
|
|
|
|
}
|
2013-09-11 00:40:21 +00:00
|
|
|
}
|
2013-09-11 05:52:10 +00:00
|
|
|
catch (XmlException ex)
|
2013-09-11 00:40:21 +00:00
|
|
|
{
|
2017-01-03 19:24:55 +00:00
|
|
|
throw new InvalidConfigFileException($"{_configFile} is corrupt is invalid. Please delete the config file and Radarr will recreate it.", ex);
|
2013-09-11 00:40:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 05:34:10 +00:00
|
|
|
private void SaveConfigFile(XDocument xDoc)
|
|
|
|
{
|
|
|
|
lock (Mutex)
|
|
|
|
{
|
|
|
|
_diskProvider.WriteAllText(_configFile, xDoc.ToString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-14 04:23:47 +00:00
|
|
|
private string GenerateApiKey()
|
|
|
|
{
|
|
|
|
return Guid.NewGuid().ToString().Replace("-", "");
|
|
|
|
}
|
|
|
|
|
2013-08-25 06:05:49 +00:00
|
|
|
public void HandleAsync(ApplicationStartedEvent message)
|
|
|
|
{
|
2015-01-23 05:34:10 +00:00
|
|
|
EnsureDefaultConfigFile();
|
2013-08-25 06:05:49 +00:00
|
|
|
DeleteOldValues();
|
|
|
|
}
|
2014-03-14 04:23:47 +00:00
|
|
|
|
|
|
|
public void Execute(ResetApiKeyCommand message)
|
|
|
|
{
|
|
|
|
SetValue("ApiKey", GenerateApiKey());
|
|
|
|
}
|
2013-05-23 05:12:01 +00:00
|
|
|
}
|
|
|
|
}
|