2010-10-15 09:10:44 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
2011-10-01 05:12:18 +02:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Xml.Linq;
|
2011-10-07 08:36:04 +02:00
|
|
|
|
using System.Xml.XPath;
|
2010-10-15 09:10:44 +02:00
|
|
|
|
using NLog;
|
2011-11-13 08:27:16 +01:00
|
|
|
|
using NzbDrone.Common.Model;
|
2010-10-15 09:10:44 +02:00
|
|
|
|
|
2011-11-13 08:27:16 +01:00
|
|
|
|
namespace NzbDrone.Common
|
2010-10-15 09:10:44 +02:00
|
|
|
|
{
|
2011-11-13 08:27:16 +01:00
|
|
|
|
public class ConfigFileProvider
|
2010-10-15 09:10:44 +02:00
|
|
|
|
{
|
2012-03-07 03:59:43 +01:00
|
|
|
|
private readonly EnvironmentProvider _environmentProvider;
|
2011-11-13 08:27:16 +01:00
|
|
|
|
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
|
2011-04-10 04:44:01 +02:00
|
|
|
|
|
2011-11-13 08:27:16 +01:00
|
|
|
|
private readonly string _configFile;
|
2011-11-14 01:57:03 +01:00
|
|
|
|
|
2012-03-07 03:59:43 +01:00
|
|
|
|
public ConfigFileProvider(EnvironmentProvider environmentProvider)
|
2011-10-08 06:51:35 +02:00
|
|
|
|
{
|
2012-03-07 03:59:43 +01:00
|
|
|
|
_environmentProvider = environmentProvider;
|
|
|
|
|
_configFile = _environmentProvider.GetConfigPath();
|
2011-11-14 01:22:18 +01:00
|
|
|
|
|
|
|
|
|
CreateDefaultConfigFile();
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-14 01:57:03 +01:00
|
|
|
|
public ConfigFileProvider()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-14 01:22:18 +01:00
|
|
|
|
public virtual Guid Guid
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
var key = "Guid";
|
|
|
|
|
if (string.IsNullOrWhiteSpace(GetValue(key, string.Empty)))
|
|
|
|
|
{
|
|
|
|
|
SetValue(key, Guid.NewGuid().ToString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Guid.Parse(GetValue(key, string.Empty));
|
|
|
|
|
}
|
2010-10-15 09:10:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-13 08:27:16 +01:00
|
|
|
|
public virtual int Port
|
2010-10-15 09:10:44 +02:00
|
|
|
|
{
|
2011-10-08 02:17:46 +02:00
|
|
|
|
get { return GetValueInt("Port", 8989); }
|
2011-11-13 08:27:16 +01:00
|
|
|
|
set { SetValue("Port", value); }
|
2011-10-01 05:12:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-10-07 08:36:04 +02:00
|
|
|
|
public virtual bool LaunchBrowser
|
2011-10-01 05:12:18 +02:00
|
|
|
|
{
|
2011-10-08 02:17:46 +02:00
|
|
|
|
get { return GetValueBoolean("LaunchBrowser", true); }
|
2011-11-13 08:27:16 +01:00
|
|
|
|
set { SetValue("LaunchBrowser", value); }
|
2011-10-07 08:36:04 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-10-08 02:17:46 +02:00
|
|
|
|
public virtual AuthenticationType AuthenticationType
|
|
|
|
|
{
|
|
|
|
|
get { return (AuthenticationType)GetValueInt("AuthenticationType", 0); }
|
2011-11-13 08:27:16 +01:00
|
|
|
|
set { SetValue("AuthenticationType", (int)value); }
|
2011-10-08 02:17:46 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-12-14 03:31:20 +01:00
|
|
|
|
public virtual bool EnableProfiler
|
|
|
|
|
{
|
|
|
|
|
get { return GetValueBoolean("EnableProfiler", false); }
|
|
|
|
|
set { SetValue("EnableProfiler", value); }
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-14 01:22:18 +01:00
|
|
|
|
public virtual int GetValueInt(string key, int defaultValue)
|
|
|
|
|
{
|
|
|
|
|
return Convert.ToInt32(GetValue(key, defaultValue));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual bool GetValueBoolean(string key, bool defaultValue)
|
|
|
|
|
{
|
|
|
|
|
return Convert.ToBoolean(GetValue(key, defaultValue));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual string GetValue(string key, object defaultValue)
|
2011-10-07 08:36:04 +02:00
|
|
|
|
{
|
2011-11-13 08:27:16 +01:00
|
|
|
|
var xDoc = XDocument.Load(_configFile);
|
2011-10-08 02:17:46 +02:00
|
|
|
|
var config = xDoc.Descendants("Config").Single();
|
2011-10-01 05:12:18 +02:00
|
|
|
|
|
2011-10-08 02:17:46 +02:00
|
|
|
|
var parentContainer = config;
|
2011-10-01 05:12:18 +02:00
|
|
|
|
|
2011-10-08 02:17:46 +02:00
|
|
|
|
|
|
|
|
|
var valueHolder = parentContainer.Descendants(key).ToList();
|
|
|
|
|
|
|
|
|
|
if (valueHolder.Count() == 1)
|
|
|
|
|
return valueHolder.First().Value;
|
|
|
|
|
|
|
|
|
|
//Save the value
|
2011-11-14 01:22:18 +01:00
|
|
|
|
SetValue(key, defaultValue);
|
2011-10-08 02:17:46 +02:00
|
|
|
|
|
|
|
|
|
//return the default value
|
|
|
|
|
return defaultValue.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-01 05:12:18 +02:00
|
|
|
|
|
2011-11-14 01:22:18 +01:00
|
|
|
|
public virtual void SetValue(string key, object value)
|
2011-10-01 05:12:18 +02:00
|
|
|
|
{
|
2011-11-13 08:27:16 +01:00
|
|
|
|
var xDoc = XDocument.Load(_configFile);
|
2011-10-01 05:12:18 +02:00
|
|
|
|
var config = xDoc.Descendants("Config").Single();
|
|
|
|
|
|
|
|
|
|
var parentContainer = config;
|
|
|
|
|
|
2011-10-08 02:17:46 +02:00
|
|
|
|
var keyHolder = parentContainer.Descendants(key);
|
2011-10-01 05:12:18 +02:00
|
|
|
|
|
2011-10-08 02:17:46 +02:00
|
|
|
|
if (keyHolder.Count() != 1)
|
|
|
|
|
parentContainer.Add(new XElement(key, value));
|
2011-10-01 05:12:18 +02:00
|
|
|
|
|
2011-10-08 02:17:46 +02:00
|
|
|
|
else
|
|
|
|
|
parentContainer.Descendants(key).Single().Value = value.ToString();
|
|
|
|
|
|
2011-11-13 08:27:16 +01:00
|
|
|
|
xDoc.Save(_configFile);
|
2011-10-01 05:12:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-14 01:22:18 +01:00
|
|
|
|
private void CreateDefaultConfigFile()
|
2011-10-01 05:12:18 +02:00
|
|
|
|
{
|
2011-11-13 08:27:16 +01:00
|
|
|
|
if (!File.Exists(_configFile))
|
|
|
|
|
{
|
|
|
|
|
var xDoc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"));
|
|
|
|
|
|
|
|
|
|
xDoc.Add(new XElement("Config"));
|
|
|
|
|
|
|
|
|
|
xDoc.Save(_configFile);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void UpdateIISConfig(string configPath)
|
|
|
|
|
{
|
|
|
|
|
logger.Info(@"Server configuration file: {0}", configPath);
|
|
|
|
|
logger.Info(@"Configuring server to: [http://localhost:{0}]", Port);
|
|
|
|
|
|
|
|
|
|
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"),
|
|
|
|
|
new XAttribute("bindingInformation", String.Format("*:{0}:localhost", Port))
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
bindings.Add(
|
|
|
|
|
new XElement("binding",
|
|
|
|
|
new XAttribute("protocol", "http"),
|
|
|
|
|
new XAttribute("bindingInformation", String.Format("*:{0}:", Port))
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
//Update the authenticationTypes
|
|
|
|
|
|
|
|
|
|
var location = configXml.XPathSelectElement("configuration").Elements("location").Where(
|
|
|
|
|
d => d.Attribute("path").Value.ToLowerInvariant() == "nzbdrone").First();
|
2011-10-08 02:17:46 +02:00
|
|
|
|
|
|
|
|
|
|
2011-11-13 08:27:16 +01:00
|
|
|
|
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";
|
|
|
|
|
|
|
|
|
|
configXml.Save(configPath);
|
2011-04-10 04:44:01 +02:00
|
|
|
|
}
|
2010-10-15 09:10:44 +02:00
|
|
|
|
}
|
2011-11-13 08:27:16 +01:00
|
|
|
|
}
|