2010-10-15 07:10:44 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
2011-10-01 03:12:18 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Xml.Linq;
|
2011-10-07 06:36:04 +00:00
|
|
|
|
using System.Xml.XPath;
|
2010-10-15 07:10:44 +00:00
|
|
|
|
using NLog;
|
|
|
|
|
using NLog.Config;
|
2011-10-08 04:51:35 +00:00
|
|
|
|
using Ninject;
|
2011-10-23 05:26:43 +00:00
|
|
|
|
using NzbDrone.Common;
|
2011-10-08 00:17:46 +00:00
|
|
|
|
using NzbDrone.Model;
|
2010-10-15 07:10:44 +00:00
|
|
|
|
|
2011-10-07 01:30:44 +00:00
|
|
|
|
namespace NzbDrone.Providers
|
2010-10-15 07:10:44 +00:00
|
|
|
|
{
|
2011-10-07 06:36:04 +00:00
|
|
|
|
public class ConfigProvider
|
2010-10-15 07:10:44 +00:00
|
|
|
|
{
|
2011-10-08 04:51:35 +00:00
|
|
|
|
private readonly EnviromentProvider _enviromentProvider;
|
2011-10-07 06:43:35 +00:00
|
|
|
|
private static readonly Logger Logger = LogManager.GetLogger("Host.ConfigProvider");
|
2011-04-10 02:44:01 +00:00
|
|
|
|
|
2011-10-08 04:51:35 +00:00
|
|
|
|
[Inject]
|
|
|
|
|
public ConfigProvider(EnviromentProvider enviromentProvider)
|
2010-10-15 07:10:44 +00:00
|
|
|
|
{
|
2011-10-08 04:51:35 +00:00
|
|
|
|
_enviromentProvider = enviromentProvider;
|
|
|
|
|
}
|
2010-10-15 07:10:44 +00:00
|
|
|
|
|
2011-10-08 04:51:35 +00:00
|
|
|
|
public ConfigProvider()
|
|
|
|
|
{
|
2010-10-15 07:10:44 +00:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-08 04:51:35 +00:00
|
|
|
|
public virtual int PortNumber
|
2010-10-15 07:10:44 +00:00
|
|
|
|
{
|
2011-10-08 00:17:46 +00:00
|
|
|
|
get { return GetValueInt("Port", 8989); }
|
2011-10-01 03:12:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-10-07 06:36:04 +00:00
|
|
|
|
public virtual bool LaunchBrowser
|
2011-10-01 03:12:18 +00:00
|
|
|
|
{
|
2011-10-08 00:17:46 +00:00
|
|
|
|
get { return GetValueBoolean("LaunchBrowser", true); }
|
2011-10-01 03:12:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-10-08 04:51:35 +00:00
|
|
|
|
public virtual string IISDirectory
|
2011-10-01 03:12:18 +00:00
|
|
|
|
{
|
2011-10-08 04:51:35 +00:00
|
|
|
|
get { return Path.Combine(_enviromentProvider.ApplicationPath, "IISExpress"); }
|
2011-10-01 03:12:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-10-08 04:51:35 +00:00
|
|
|
|
public virtual string IISExePath
|
2011-10-01 03:12:18 +00:00
|
|
|
|
{
|
2011-10-08 04:51:35 +00:00
|
|
|
|
get { return Path.Combine(IISDirectory, "iisexpress.exe"); }
|
2010-10-15 07:10:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-10-08 04:51:35 +00:00
|
|
|
|
public virtual string IISConfigPath
|
2011-10-07 01:30:44 +00:00
|
|
|
|
{
|
2011-10-08 04:51:35 +00:00
|
|
|
|
get { return Path.Combine(IISDirectory, "AppServer", "applicationhost.config"); }
|
2011-10-07 01:30:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-10-08 04:51:35 +00:00
|
|
|
|
public virtual string AppDataDirectory
|
2011-10-07 06:36:04 +00:00
|
|
|
|
{
|
2011-10-08 04:51:35 +00:00
|
|
|
|
get { return Path.Combine(_enviromentProvider.ApplicationPath, "NzbDrone.Web", "App_Data"); }
|
2011-10-07 06:36:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-10-08 04:51:35 +00:00
|
|
|
|
public virtual string ConfigFile
|
2011-10-07 06:36:04 +00:00
|
|
|
|
{
|
2011-10-08 04:51:35 +00:00
|
|
|
|
get { return Path.Combine(AppDataDirectory, "Config.xml"); }
|
2011-10-07 06:36:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-10-08 04:51:35 +00:00
|
|
|
|
public virtual string NlogConfigPath
|
2011-10-07 06:36:04 +00:00
|
|
|
|
{
|
2011-10-08 04:51:35 +00:00
|
|
|
|
get { return Path.Combine(_enviromentProvider.ApplicationPath, "NzbDrone.Web\\log.config"); }
|
2011-10-07 06:36:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-10-08 00:17:46 +00:00
|
|
|
|
public virtual AuthenticationType AuthenticationType
|
|
|
|
|
{
|
|
|
|
|
get { return (AuthenticationType)GetValueInt("AuthenticationType", 0); }
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-07 06:36:04 +00:00
|
|
|
|
public virtual void UpdateIISConfig(string configPath)
|
|
|
|
|
{
|
|
|
|
|
Logger.Info(@"Server configuration file: {0}", configPath);
|
2011-10-08 04:51:35 +00:00
|
|
|
|
Logger.Info(@"Configuring server to: [http://localhost:{0}]", PortNumber);
|
2011-10-07 06:36:04 +00:00
|
|
|
|
|
|
|
|
|
var configXml = XDocument.Load(configPath);
|
|
|
|
|
|
|
|
|
|
var bindings =
|
|
|
|
|
configXml.XPathSelectElement("configuration/system.applicationHost/sites").Elements("site").Where(
|
|
|
|
|
d => d.Attribute("name").Value.ToLowerInvariant() == "nzbdrone").First().Element("bindings");
|
|
|
|
|
bindings.Descendants().Remove();
|
|
|
|
|
bindings.Add(
|
|
|
|
|
new XElement("binding",
|
|
|
|
|
new XAttribute("protocol", "http"),
|
2011-10-08 04:51:35 +00:00
|
|
|
|
new XAttribute("bindingInformation", String.Format("*:{0}:localhost", PortNumber))
|
2011-10-07 06:36:04 +00:00
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
bindings.Add(
|
2011-10-07 06:57:43 +00:00
|
|
|
|
new XElement("binding",
|
|
|
|
|
new XAttribute("protocol", "http"),
|
2011-10-08 04:51:35 +00:00
|
|
|
|
new XAttribute("bindingInformation", String.Format("*:{0}:", PortNumber))
|
2011-10-07 06:57:43 +00:00
|
|
|
|
));
|
2011-10-07 06:36:04 +00:00
|
|
|
|
|
2011-10-08 00:17:46 +00:00
|
|
|
|
//Update the authenticationTypes
|
|
|
|
|
|
|
|
|
|
var location = configXml.XPathSelectElement("configuration").Elements("location").Where(
|
|
|
|
|
d => d.Attribute("path").Value.ToLowerInvariant() == "nzbdrone").First();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var authenticationTypes = location.XPathSelectElements("system.webServer/security/authentication").First().Descendants();
|
|
|
|
|
|
|
|
|
|
//Set all authentication types enabled to false
|
|
|
|
|
foreach (var child in authenticationTypes)
|
|
|
|
|
{
|
|
|
|
|
child.Attribute("enabled").Value = "false";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var configuredAuthType = String.Format("{0}Authentication", AuthenticationType.ToString()).ToLowerInvariant();
|
|
|
|
|
|
|
|
|
|
//Set the users authenticationType to true
|
|
|
|
|
authenticationTypes.Where(t => t.Name.ToString().ToLowerInvariant() == configuredAuthType).Single().Attribute("enabled").Value = "true";
|
|
|
|
|
|
2011-10-07 06:36:04 +00:00
|
|
|
|
configXml.Save(configPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void CreateDefaultConfigFile()
|
2011-10-01 03:12:18 +00:00
|
|
|
|
{
|
|
|
|
|
//Create the config file here
|
|
|
|
|
Directory.CreateDirectory(AppDataDirectory);
|
|
|
|
|
|
|
|
|
|
if (!File.Exists(ConfigFile))
|
|
|
|
|
{
|
|
|
|
|
WriteDefaultConfig();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-09 01:23:03 +00:00
|
|
|
|
private string GetValue(string key, object defaultValue, string parent = null)
|
2011-10-01 03:12:18 +00:00
|
|
|
|
{
|
2011-10-08 00:17:46 +00:00
|
|
|
|
var xDoc = XDocument.Load(ConfigFile);
|
|
|
|
|
var config = xDoc.Descendants("Config").Single();
|
2011-10-01 03:12:18 +00:00
|
|
|
|
|
2011-10-08 00:17:46 +00:00
|
|
|
|
var parentContainer = config;
|
2011-10-01 03:12:18 +00:00
|
|
|
|
|
2011-10-08 00:17:46 +00:00
|
|
|
|
if (!String.IsNullOrEmpty(parent))
|
|
|
|
|
{
|
|
|
|
|
//Add the parent
|
|
|
|
|
if (config.Descendants(parent).Count() != 1)
|
|
|
|
|
{
|
|
|
|
|
SetValue(key, defaultValue, parent);
|
|
|
|
|
|
|
|
|
|
//Reload the configFile
|
|
|
|
|
xDoc = XDocument.Load(ConfigFile);
|
|
|
|
|
config = xDoc.Descendants("Config").Single();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
parentContainer = config.Descendants(parent).Single();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var valueHolder = parentContainer.Descendants(key).ToList();
|
|
|
|
|
|
|
|
|
|
if (valueHolder.Count() == 1)
|
|
|
|
|
return valueHolder.First().Value;
|
|
|
|
|
|
|
|
|
|
//Save the value
|
|
|
|
|
SetValue(key, defaultValue, parent);
|
|
|
|
|
|
|
|
|
|
//return the default value
|
|
|
|
|
return defaultValue.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual int GetValueInt(string key, int defaultValue, string parent = null)
|
|
|
|
|
{
|
|
|
|
|
return Convert.ToInt32(GetValue(key, defaultValue, parent));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual bool GetValueBoolean(string key, bool defaultValue, string parent = null)
|
|
|
|
|
{
|
|
|
|
|
return Convert.ToBoolean(GetValue(key, defaultValue, parent));
|
2011-10-01 03:12:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-10-08 00:17:46 +00:00
|
|
|
|
public virtual void SetValue(string key, object value, string parent = null)
|
2011-10-01 03:12:18 +00:00
|
|
|
|
{
|
|
|
|
|
var xDoc = XDocument.Load(ConfigFile);
|
|
|
|
|
var config = xDoc.Descendants("Config").Single();
|
|
|
|
|
|
|
|
|
|
var parentContainer = config;
|
|
|
|
|
|
2011-10-08 00:17:46 +00:00
|
|
|
|
if (!String.IsNullOrEmpty(parent))
|
|
|
|
|
{
|
|
|
|
|
//Add the parent container if it doesn't already exist
|
|
|
|
|
if (config.Descendants(parent).Count() != 1)
|
|
|
|
|
{
|
|
|
|
|
config.Add(new XElement(parent));
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-01 03:12:18 +00:00
|
|
|
|
parentContainer = config.Descendants(parent).Single();
|
2011-10-08 00:17:46 +00:00
|
|
|
|
}
|
2011-10-01 03:12:18 +00:00
|
|
|
|
|
2011-10-08 00:17:46 +00:00
|
|
|
|
var keyHolder = parentContainer.Descendants(key);
|
2011-10-01 03:12:18 +00:00
|
|
|
|
|
2011-10-08 00:17:46 +00:00
|
|
|
|
if (keyHolder.Count() != 1)
|
|
|
|
|
parentContainer.Add(new XElement(key, value));
|
2011-10-01 03:12:18 +00:00
|
|
|
|
|
2011-10-08 00:17:46 +00:00
|
|
|
|
else
|
|
|
|
|
parentContainer.Descendants(key).Single().Value = value.ToString();
|
|
|
|
|
|
|
|
|
|
xDoc.Save(ConfigFile);
|
2011-10-01 03:12:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-10-08 00:17:46 +00:00
|
|
|
|
public virtual void WriteDefaultConfig()
|
2011-10-01 03:12:18 +00:00
|
|
|
|
{
|
2011-10-08 00:17:46 +00:00
|
|
|
|
var xDoc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"));
|
|
|
|
|
|
|
|
|
|
xDoc.Add(new XElement("Config"));
|
|
|
|
|
|
|
|
|
|
xDoc.Save(ConfigFile);
|
2011-04-10 02:44:01 +00:00
|
|
|
|
}
|
2010-10-15 07:10:44 +00:00
|
|
|
|
}
|
2011-04-10 02:44:01 +00:00
|
|
|
|
}
|