|
|
|
@ -4,132 +4,45 @@ using System.Linq;
|
|
|
|
|
using System.Xml.Linq;
|
|
|
|
|
using System.Xml.XPath;
|
|
|
|
|
using NLog;
|
|
|
|
|
using Ninject;
|
|
|
|
|
using NzbDrone.Common;
|
|
|
|
|
using NzbDrone.Model;
|
|
|
|
|
using NzbDrone.Common.Model;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Providers
|
|
|
|
|
namespace NzbDrone.Common
|
|
|
|
|
{
|
|
|
|
|
public class ConfigProvider
|
|
|
|
|
public class ConfigFileProvider
|
|
|
|
|
{
|
|
|
|
|
private readonly EnviromentProvider _enviromentProvider;
|
|
|
|
|
private static readonly Logger Logger = LogManager.GetLogger("Host.ConfigProvider");
|
|
|
|
|
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
|
|
|
|
|
|
|
|
|
|
[Inject]
|
|
|
|
|
public ConfigProvider(EnviromentProvider enviromentProvider)
|
|
|
|
|
{
|
|
|
|
|
_enviromentProvider = enviromentProvider;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ConfigProvider()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
private readonly string _configFile;
|
|
|
|
|
public ConfigFileProvider(EnviromentProvider enviromentProvider)
|
|
|
|
|
{
|
|
|
|
|
_enviromentProvider = enviromentProvider;
|
|
|
|
|
_configFile = _enviromentProvider.GetConfigPath();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual int PortNumber
|
|
|
|
|
public virtual int Port
|
|
|
|
|
{
|
|
|
|
|
get { return GetValueInt("Port", 8989); }
|
|
|
|
|
set { SetValue("Port", value); }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual bool LaunchBrowser
|
|
|
|
|
{
|
|
|
|
|
get { return GetValueBoolean("LaunchBrowser", true); }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual string IISDirectory
|
|
|
|
|
{
|
|
|
|
|
get { return Path.Combine(_enviromentProvider.ApplicationPath, "IISExpress"); }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual string IISExePath
|
|
|
|
|
{
|
|
|
|
|
get { return Path.Combine(IISDirectory, "iisexpress.exe"); }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual string IISConfigPath
|
|
|
|
|
{
|
|
|
|
|
get { return Path.Combine(IISDirectory, "AppServer", "applicationhost.config"); }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual string AppDataDirectory
|
|
|
|
|
{
|
|
|
|
|
get { return Path.Combine(_enviromentProvider.ApplicationPath, "NzbDrone.Web", "App_Data"); }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual string ConfigFile
|
|
|
|
|
{
|
|
|
|
|
get { return Path.Combine(AppDataDirectory, "Config.xml"); }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual string NlogConfigPath
|
|
|
|
|
{
|
|
|
|
|
get { return Path.Combine(_enviromentProvider.ApplicationPath, "NzbDrone.Web\\log.config"); }
|
|
|
|
|
set { SetValue("LaunchBrowser", value); }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual AuthenticationType AuthenticationType
|
|
|
|
|
{
|
|
|
|
|
get { return (AuthenticationType)GetValueInt("AuthenticationType", 0); }
|
|
|
|
|
set { SetValue("AuthenticationType", (int)value); }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void UpdateIISConfig(string configPath)
|
|
|
|
|
{
|
|
|
|
|
Logger.Info(@"Server configuration file: {0}", configPath);
|
|
|
|
|
Logger.Info(@"Configuring server to: [http://localhost:{0}]", PortNumber);
|
|
|
|
|
|
|
|
|
|
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", PortNumber))
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
bindings.Add(
|
|
|
|
|
new XElement("binding",
|
|
|
|
|
new XAttribute("protocol", "http"),
|
|
|
|
|
new XAttribute("bindingInformation", String.Format("*:{0}:", PortNumber))
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
//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";
|
|
|
|
|
|
|
|
|
|
configXml.Save(configPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void CreateDefaultConfigFile()
|
|
|
|
|
public virtual string GetValue(string key, object defaultValue, string parent = null)
|
|
|
|
|
{
|
|
|
|
|
//Create the config file here
|
|
|
|
|
Directory.CreateDirectory(AppDataDirectory);
|
|
|
|
|
|
|
|
|
|
if (!File.Exists(ConfigFile))
|
|
|
|
|
{
|
|
|
|
|
WriteDefaultConfig();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetValue(string key, object defaultValue, string parent = null)
|
|
|
|
|
{
|
|
|
|
|
var xDoc = XDocument.Load(ConfigFile);
|
|
|
|
|
var xDoc = XDocument.Load(_configFile);
|
|
|
|
|
var config = xDoc.Descendants("Config").Single();
|
|
|
|
|
|
|
|
|
|
var parentContainer = config;
|
|
|
|
@ -142,7 +55,7 @@ namespace NzbDrone.Providers
|
|
|
|
|
SetValue(key, defaultValue, parent);
|
|
|
|
|
|
|
|
|
|
//Reload the configFile
|
|
|
|
|
xDoc = XDocument.Load(ConfigFile);
|
|
|
|
|
xDoc = XDocument.Load(_configFile);
|
|
|
|
|
config = xDoc.Descendants("Config").Single();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -173,7 +86,7 @@ namespace NzbDrone.Providers
|
|
|
|
|
|
|
|
|
|
public virtual void SetValue(string key, object value, string parent = null)
|
|
|
|
|
{
|
|
|
|
|
var xDoc = XDocument.Load(ConfigFile);
|
|
|
|
|
var xDoc = XDocument.Load(_configFile);
|
|
|
|
|
var config = xDoc.Descendants("Config").Single();
|
|
|
|
|
|
|
|
|
|
var parentContainer = config;
|
|
|
|
@ -197,16 +110,66 @@ namespace NzbDrone.Providers
|
|
|
|
|
else
|
|
|
|
|
parentContainer.Descendants(key).Single().Value = value.ToString();
|
|
|
|
|
|
|
|
|
|
xDoc.Save(ConfigFile);
|
|
|
|
|
xDoc.Save(_configFile);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void WriteDefaultConfig()
|
|
|
|
|
public virtual void CreateDefaultConfigFile()
|
|
|
|
|
{
|
|
|
|
|
if (!File.Exists(_configFile))
|
|
|
|
|
{
|
|
|
|
|
var xDoc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"));
|
|
|
|
|
|
|
|
|
|
xDoc.Add(new XElement("Config"));
|
|
|
|
|
|
|
|
|
|
xDoc.Save(ConfigFile);
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|