From 2ca4b7d03adfa3cc7c9c6a597a11762142d5b34b Mon Sep 17 00:00:00 2001 From: LukePulverenti Date: Mon, 4 Mar 2013 00:43:06 -0500 Subject: Created IConfigurationManager --- .../BaseApplicationHost.cs | 125 +++++++++++++++----- .../BaseApplicationPaths.cs | 3 +- .../Configuration/BaseConfigurationManager.cs | 123 +++++++++++++++++++ .../HttpClientManager/HttpClientManager.cs | 3 +- .../HttpServer/BaseRestService.cs | 11 +- .../MediaBrowser.Common.Implementations.csproj | 5 + .../ScheduledTasks/ScheduledTaskWorker.cs | 3 +- .../ScheduledTasks/TaskManager.cs | 3 +- .../ScheduledTasks/Tasks/DeleteCacheFileTask.cs | 16 +-- .../ScheduledTasks/Tasks/DeleteLogFileTask.cs | 20 ++-- .../ScheduledTasks/Tasks/ReloadLoggerTask.cs | 16 +-- .../ScheduledTasks/Tasks/SystemUpdateTask.cs | 25 ++-- .../Security/PluginSecurityManager.cs | 131 +++++++++++++++++++++ .../ServerManager/ServerManager.cs | 37 +++--- .../Updates/PackageManager.cs | 64 ++++++---- 15 files changed, 475 insertions(+), 110 deletions(-) create mode 100644 MediaBrowser.Common.Implementations/Configuration/BaseConfigurationManager.cs create mode 100644 MediaBrowser.Common.Implementations/Security/PluginSecurityManager.cs (limited to 'MediaBrowser.Common.Implementations') diff --git a/MediaBrowser.Common.Implementations/BaseApplicationHost.cs b/MediaBrowser.Common.Implementations/BaseApplicationHost.cs index 4e1b4634e..b25c1808d 100644 --- a/MediaBrowser.Common.Implementations/BaseApplicationHost.cs +++ b/MediaBrowser.Common.Implementations/BaseApplicationHost.cs @@ -1,4 +1,9 @@ -using System.Threading.Tasks; +using MediaBrowser.Common.Configuration; +using MediaBrowser.Common.Implementations.Logging; +using MediaBrowser.Common.Implementations.NetworkManagement; +using MediaBrowser.Common.Implementations.ScheduledTasks; +using MediaBrowser.Common.Implementations.Security; +using MediaBrowser.Common.Implementations.Serialization; using MediaBrowser.Common.Implementations.Udp; using MediaBrowser.Common.Implementations.Updates; using MediaBrowser.Common.Implementations.WebSocket; @@ -6,9 +11,11 @@ using MediaBrowser.Common.Kernel; using MediaBrowser.Common.Net; using MediaBrowser.Common.Plugins; using MediaBrowser.Common.ScheduledTasks; +using MediaBrowser.Common.Security; using MediaBrowser.Common.Updates; using MediaBrowser.Model.Logging; using MediaBrowser.Model.Serialization; +using MediaBrowser.Model.Updates; using SimpleInjector; using System; using System.Collections.Generic; @@ -16,16 +23,18 @@ using System.IO; using System.Linq; using System.Reflection; using System.Threading; +using System.Threading.Tasks; namespace MediaBrowser.Common.Implementations { - public abstract class BaseApplicationHost + public abstract class BaseApplicationHost : IApplicationHost + where TApplicationPathsType : class, IApplicationPaths, new() { /// /// Gets or sets the logger. /// /// The logger. - public ILogger Logger { get; protected set; } + protected ILogger Logger { get; private set; } /// /// Gets or sets the plugins. @@ -43,13 +52,23 @@ namespace MediaBrowser.Common.Implementations /// Gets the application paths. /// /// The application paths. - protected IApplicationPaths ApplicationPaths { get; private set; } + protected TApplicationPathsType ApplicationPaths = new TApplicationPathsType(); /// /// The container /// protected readonly Container Container = new Container(); + /// + /// The json serializer + /// + protected readonly IJsonSerializer JsonSerializer = new JsonSerializer(); + + /// + /// The _XML serializer + /// + protected readonly IXmlSerializer XmlSerializer = new XmlSerializer(); + /// /// Gets assemblies that failed to load /// @@ -109,12 +128,26 @@ namespace MediaBrowser.Common.Implementations } } + /// + /// Gets the kernel. + /// + /// The kernel. + protected IKernel Kernel { get; private set; } + protected ITaskManager TaskManager { get; private set; } + protected ISecurityManager SecurityManager { get; private set; } + + protected IConfigurationManager ConfigurationManager { get; private set; } + /// /// Initializes a new instance of the class. /// protected BaseApplicationHost() { FailedAssemblies = new List(); + + LogManager = new NlogManager(ApplicationPaths.LogDirectoryPath, LogFilePrefixName); + + ConfigurationManager = GetConfigurationManager(); } /// @@ -125,15 +158,25 @@ namespace MediaBrowser.Common.Implementations { return Task.Run(() => { - ApplicationPaths = GetApplicationPaths(); - - LogManager = GetLogManager(); - Logger = LogManager.GetLogger("App"); IsFirstRun = !File.Exists(ApplicationPaths.SystemConfigurationFilePath); DiscoverTypes(); + + LogManager.ReloadLogger(ConfigurationManager.CommonConfiguration.EnableDebugLevelLogging ? LogSeverity.Debug : LogSeverity.Info); + + Logger.Info("Version {0} initializing", ApplicationVersion); + + Kernel = GetKernel(); + + RegisterResources(); + + FindParts(); + + Task.Run(() => ConfigureAutoRunAtStartup()); + + Kernel.Init(); }); } @@ -144,16 +187,13 @@ namespace MediaBrowser.Common.Implementations protected abstract IEnumerable GetComposablePartAssemblies(); /// - /// Gets the log manager. + /// Gets the name of the log file prefix. /// - /// ILogManager. - protected abstract ILogManager GetLogManager(); + /// The name of the log file prefix. + protected abstract string LogFilePrefixName { get; } - /// - /// Gets the application paths. - /// - /// IApplicationPaths. - protected abstract IApplicationPaths GetApplicationPaths(); + protected abstract IKernel GetKernel(); + protected abstract IConfigurationManager GetConfigurationManager(); /// /// Finds the parts. @@ -184,21 +224,44 @@ namespace MediaBrowser.Common.Implementations /// /// Registers resources that classes will depend on /// - protected virtual void RegisterResources(ITaskManager taskManager, INetworkManager networkManager, IServerManager serverManager) + protected virtual void RegisterResources() { + RegisterSingleInstance(ConfigurationManager); + RegisterSingleInstance(this); + + RegisterSingleInstance(ApplicationPaths); + + var networkManager = new NetworkManager(); + + var serverManager = new ServerManager.ServerManager(this, Kernel, networkManager, JsonSerializer, Logger, ConfigurationManager); + + TaskManager = new TaskManager(ApplicationPaths, JsonSerializer, Logger, serverManager); + + RegisterSingleInstance(JsonSerializer); + RegisterSingleInstance(XmlSerializer); + RegisterSingleInstance(LogManager); RegisterSingleInstance(Logger); - RegisterSingleInstance(ApplicationPaths); - RegisterSingleInstance(taskManager); + RegisterSingleInstance(Kernel); + + RegisterSingleInstance(TaskManager); RegisterSingleInstance(() => new AlchemyServer(Logger)); RegisterSingleInstance(ProtobufSerializer); RegisterSingleInstance(new UdpServer(Logger), false); - RegisterSingleInstance(new PackageManager()); - RegisterSingleInstance(new HttpClientManager.HttpClientManager(ApplicationPaths, Logger)); - RegisterSingleInstance(networkManager); - RegisterSingleInstance(serverManager); + var httpClient = new HttpClientManager.HttpClientManager(ApplicationPaths, Logger); + + RegisterSingleInstance(httpClient); + + RegisterSingleInstance(networkManager); + RegisterSingleInstance(serverManager); + + SecurityManager = new PluginSecurityManager(Kernel, httpClient, JsonSerializer, ApplicationPaths); + + RegisterSingleInstance(SecurityManager); + + RegisterSingleInstance(new PackageManager(SecurityManager, networkManager, httpClient, ApplicationPaths, JsonSerializer, Logger)); } /// @@ -336,7 +399,7 @@ namespace MediaBrowser.Common.Implementations Logger.Info("Composing instances of " + currentType.Name); - var parts = AllConcreteTypes.Where(currentType.IsAssignableFrom).Select(CreateInstance).Cast().ToArray(); + var parts = AllConcreteTypes.AsParallel().Where(currentType.IsAssignableFrom).Select(CreateInstance).Cast().ToArray(); if (manageLiftime) { @@ -361,10 +424,8 @@ namespace MediaBrowser.Common.Implementations /// /// Configures the auto run at startup. /// - /// if set to true [autorun]. - public void ConfigureAutoRunAtStartup(bool autorun) + private void ConfigureAutoRunAtStartup() { - } /// @@ -409,5 +470,15 @@ namespace MediaBrowser.Common.Implementations } } } + + public abstract void Restart(); + + public abstract bool CanSelfUpdate { get; } + + public abstract Task CheckForApplicationUpdate(CancellationToken cancellationToken, IProgress progress); + + public abstract Task UpdateApplication(PackageVersionInfo package, CancellationToken cancellationToken, IProgress progress); + + public abstract void Shutdown(); } } diff --git a/MediaBrowser.Common.Implementations/BaseApplicationPaths.cs b/MediaBrowser.Common.Implementations/BaseApplicationPaths.cs index 37bd62200..d16933616 100644 --- a/MediaBrowser.Common.Implementations/BaseApplicationPaths.cs +++ b/MediaBrowser.Common.Implementations/BaseApplicationPaths.cs @@ -1,4 +1,5 @@ -using MediaBrowser.Common.Kernel; +using MediaBrowser.Common.Configuration; +using MediaBrowser.Common.Kernel; using System; using System.Configuration; using System.IO; diff --git a/MediaBrowser.Common.Implementations/Configuration/BaseConfigurationManager.cs b/MediaBrowser.Common.Implementations/Configuration/BaseConfigurationManager.cs new file mode 100644 index 000000000..2f50f5f7a --- /dev/null +++ b/MediaBrowser.Common.Implementations/Configuration/BaseConfigurationManager.cs @@ -0,0 +1,123 @@ +using MediaBrowser.Common.Configuration; +using MediaBrowser.Common.Events; +using MediaBrowser.Model.Configuration; +using MediaBrowser.Model.Logging; +using MediaBrowser.Model.Serialization; +using System; +using System.Threading; + +namespace MediaBrowser.Common.Implementations.Configuration +{ + /// + /// Class BaseConfigurationManager + /// + public abstract class BaseConfigurationManager : IConfigurationManager + { + /// + /// Gets the type of the configuration. + /// + /// The type of the configuration. + protected abstract Type ConfigurationType { get; } + + /// + /// Occurs when [configuration updated]. + /// + public event EventHandler ConfigurationUpdated; + + /// + /// Gets the logger. + /// + /// The logger. + protected ILogger Logger { get; private set; } + /// + /// Gets the XML serializer. + /// + /// The XML serializer. + protected IXmlSerializer XmlSerializer { get; private set; } + + /// + /// Gets or sets the application paths. + /// + /// The application paths. + public IApplicationPaths CommonApplicationPaths { get; private set; } + + /// + /// The _configuration loaded + /// + private bool _configurationLoaded; + /// + /// The _configuration sync lock + /// + private object _configurationSyncLock = new object(); + /// + /// The _configuration + /// + private BaseApplicationConfiguration _configuration; + /// + /// Gets the system configuration + /// + /// The configuration. + public BaseApplicationConfiguration CommonConfiguration + { + get + { + // Lazy load + LazyInitializer.EnsureInitialized(ref _configuration, ref _configurationLoaded, ref _configurationSyncLock, () => (BaseApplicationConfiguration)ConfigurationHelper.GetXmlConfiguration(ConfigurationType, CommonApplicationPaths.SystemConfigurationFilePath, XmlSerializer)); + return _configuration; + } + protected set + { + _configuration = value; + + _configurationLoaded = value != null; + } + } + + /// + /// Initializes a new instance of the class. + /// + /// The application paths. + /// The log manager. + /// The XML serializer. + protected BaseConfigurationManager(IApplicationPaths applicationPaths, ILogManager logManager, IXmlSerializer xmlSerializer) + { + CommonApplicationPaths = applicationPaths; + XmlSerializer = xmlSerializer; + Logger = logManager.GetLogger(GetType().Name); + } + + /// + /// The _save lock + /// + private readonly object _configurationSaveLock = new object(); + + /// + /// Saves the configuration. + /// + public void SaveConfiguration() + { + lock (_configurationSaveLock) + { + XmlSerializer.SerializeToFile(CommonConfiguration, CommonApplicationPaths.SystemConfigurationFilePath); + } + + EventHelper.QueueEventIfNotNull(ConfigurationUpdated, this, EventArgs.Empty, Logger); + } + + /// + /// Replaces the configuration. + /// + /// The new configuration. + /// newConfiguration + public void ReplaceConfiguration(BaseApplicationConfiguration newConfiguration) + { + if (newConfiguration == null) + { + throw new ArgumentNullException("newConfiguration"); + } + + CommonConfiguration = newConfiguration; + SaveConfiguration(); + } + } +} diff --git a/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs b/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs index e01718f45..f9363e0d9 100644 --- a/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs +++ b/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs @@ -1,4 +1,5 @@ -using MediaBrowser.Common.IO; +using MediaBrowser.Common.Configuration; +using MediaBrowser.Common.IO; using MediaBrowser.Common.Kernel; using MediaBrowser.Common.Net; using MediaBrowser.Model.Logging; diff --git a/MediaBrowser.Common.Implementations/HttpServer/BaseRestService.cs b/MediaBrowser.Common.Implementations/HttpServer/BaseRestService.cs index bf487b760..382183b58 100644 --- a/MediaBrowser.Common.Implementations/HttpServer/BaseRestService.cs +++ b/MediaBrowser.Common.Implementations/HttpServer/BaseRestService.cs @@ -1,7 +1,5 @@ -using System.Collections.Generic; -using MediaBrowser.Common.Extensions; +using MediaBrowser.Common.Extensions; using MediaBrowser.Common.IO; -using MediaBrowser.Common.Kernel; using MediaBrowser.Common.Net; using MediaBrowser.Model.Logging; using ServiceStack.Common; @@ -9,6 +7,7 @@ using ServiceStack.Common.Web; using ServiceStack.ServiceHost; using ServiceStack.ServiceInterface; using System; +using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; @@ -22,12 +21,6 @@ namespace MediaBrowser.Common.Implementations.HttpServer /// public class BaseRestService : Service, IRestfulService { - /// - /// Gets or sets the kernel. - /// - /// The kernel. - public IKernel Kernel { get; set; } - /// /// Gets or sets the logger. /// diff --git a/MediaBrowser.Common.Implementations/MediaBrowser.Common.Implementations.csproj b/MediaBrowser.Common.Implementations/MediaBrowser.Common.Implementations.csproj index 831b111ec..84769637a 100644 --- a/MediaBrowser.Common.Implementations/MediaBrowser.Common.Implementations.csproj +++ b/MediaBrowser.Common.Implementations/MediaBrowser.Common.Implementations.csproj @@ -38,6 +38,9 @@ ..\packages\Alchemy.2.2.1\lib\net40\Alchemy.dll + + ..\ThirdParty\PluginSecurity\Mediabrowser.PluginSecurity.dll + ..\packages\NLog.2.0.0.2000\lib\net40\NLog.dll @@ -104,6 +107,7 @@ + @@ -123,6 +127,7 @@ + diff --git a/MediaBrowser.Common.Implementations/ScheduledTasks/ScheduledTaskWorker.cs b/MediaBrowser.Common.Implementations/ScheduledTasks/ScheduledTaskWorker.cs index eada38556..d5adf3265 100644 --- a/MediaBrowser.Common.Implementations/ScheduledTasks/ScheduledTaskWorker.cs +++ b/MediaBrowser.Common.Implementations/ScheduledTasks/ScheduledTaskWorker.cs @@ -1,4 +1,5 @@ -using MediaBrowser.Common.Extensions; +using MediaBrowser.Common.Configuration; +using MediaBrowser.Common.Extensions; using MediaBrowser.Common.Kernel; using MediaBrowser.Common.ScheduledTasks; using MediaBrowser.Model.Logging; diff --git a/MediaBrowser.Common.Implementations/ScheduledTasks/TaskManager.cs b/MediaBrowser.Common.Implementations/ScheduledTasks/TaskManager.cs index efd3478a1..946887303 100644 --- a/MediaBrowser.Common.Implementations/ScheduledTasks/TaskManager.cs +++ b/MediaBrowser.Common.Implementations/ScheduledTasks/TaskManager.cs @@ -1,4 +1,5 @@ -using MediaBrowser.Common.Kernel; +using MediaBrowser.Common.Configuration; +using MediaBrowser.Common.Kernel; using MediaBrowser.Common.ScheduledTasks; using MediaBrowser.Model.Logging; using MediaBrowser.Model.Serialization; diff --git a/MediaBrowser.Common.Implementations/ScheduledTasks/Tasks/DeleteCacheFileTask.cs b/MediaBrowser.Common.Implementations/ScheduledTasks/Tasks/DeleteCacheFileTask.cs index a9c82c357..c7a19c0c0 100644 --- a/MediaBrowser.Common.Implementations/ScheduledTasks/Tasks/DeleteCacheFileTask.cs +++ b/MediaBrowser.Common.Implementations/ScheduledTasks/Tasks/DeleteCacheFileTask.cs @@ -1,4 +1,4 @@ -using MediaBrowser.Common.Kernel; +using MediaBrowser.Common.Configuration; using MediaBrowser.Common.ScheduledTasks; using MediaBrowser.Model.Logging; using System; @@ -16,10 +16,10 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks.Tasks public class DeleteCacheFileTask : IScheduledTask { /// - /// Gets or sets the kernel. + /// Gets or sets the application paths. /// - /// The kernel. - private IKernel Kernel { get; set; } + /// The application paths. + private IApplicationPaths ApplicationPaths { get; set; } /// /// Gets or sets the logger. /// @@ -29,11 +29,11 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks.Tasks /// /// Initializes a new instance of the class. /// - /// The kernel. + /// The app paths. /// The logger. - public DeleteCacheFileTask(IKernel kernel, ILogger logger) + public DeleteCacheFileTask(IApplicationPaths appPaths, ILogger logger) { - Kernel = kernel; + ApplicationPaths = appPaths; Logger = logger; } @@ -60,7 +60,7 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks.Tasks { var minDateModified = DateTime.UtcNow.AddMonths(-2); - DeleteCacheFilesFromDirectory(cancellationToken, Kernel.ApplicationPaths.CachePath, minDateModified, progress); + DeleteCacheFilesFromDirectory(cancellationToken, ApplicationPaths.CachePath, minDateModified, progress); }); } diff --git a/MediaBrowser.Common.Implementations/ScheduledTasks/Tasks/DeleteLogFileTask.cs b/MediaBrowser.Common.Implementations/ScheduledTasks/Tasks/DeleteLogFileTask.cs index a7d8a68a0..faeb39735 100644 --- a/MediaBrowser.Common.Implementations/ScheduledTasks/Tasks/DeleteLogFileTask.cs +++ b/MediaBrowser.Common.Implementations/ScheduledTasks/Tasks/DeleteLogFileTask.cs @@ -1,4 +1,4 @@ -using MediaBrowser.Common.Kernel; +using MediaBrowser.Common.Configuration; using MediaBrowser.Common.ScheduledTasks; using MediaBrowser.Model.Logging; using System; @@ -16,10 +16,10 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks.Tasks public class DeleteLogFileTask : IScheduledTask { /// - /// Gets or sets the kernel. + /// Gets or sets the configuration manager. /// - /// The kernel. - private IKernel Kernel { get; set; } + /// The configuration manager. + private IConfigurationManager ConfigurationManager { get; set; } /// /// Gets or sets the logger. /// @@ -29,11 +29,11 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks.Tasks /// /// Initializes a new instance of the class. /// - /// The kernel. + /// The configuration manager. /// The logger. - public DeleteLogFileTask(IKernel kernel, ILogger logger) + public DeleteLogFileTask(IConfigurationManager configurationManager, ILogger logger) { - Kernel = kernel; + ConfigurationManager = configurationManager; Logger = logger; } @@ -59,9 +59,9 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks.Tasks return Task.Run(() => { // Delete log files more than n days old - var minDateModified = DateTime.UtcNow.AddDays(-(Kernel.Configuration.LogFileRetentionDays)); + var minDateModified = DateTime.UtcNow.AddDays(-(ConfigurationManager.CommonConfiguration.LogFileRetentionDays)); - var filesToDelete = new DirectoryInfo(Kernel.ApplicationPaths.LogDirectoryPath).EnumerateFileSystemInfos("*", SearchOption.AllDirectories) + var filesToDelete = new DirectoryInfo(ConfigurationManager.CommonApplicationPaths.LogDirectoryPath).EnumerateFileSystemInfos("*", SearchOption.AllDirectories) .Where(f => f.LastWriteTimeUtc < minDateModified) .ToList(); @@ -100,7 +100,7 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks.Tasks /// The description. public string Description { - get { return string.Format("Deletes log files that are more than {0} days old.", Kernel.Configuration.LogFileRetentionDays); } + get { return string.Format("Deletes log files that are more than {0} days old.", ConfigurationManager.CommonConfiguration.LogFileRetentionDays); } } /// diff --git a/MediaBrowser.Common.Implementations/ScheduledTasks/Tasks/ReloadLoggerTask.cs b/MediaBrowser.Common.Implementations/ScheduledTasks/Tasks/ReloadLoggerTask.cs index ea1e8b938..a02f9dec6 100644 --- a/MediaBrowser.Common.Implementations/ScheduledTasks/Tasks/ReloadLoggerTask.cs +++ b/MediaBrowser.Common.Implementations/ScheduledTasks/Tasks/ReloadLoggerTask.cs @@ -1,4 +1,4 @@ -using MediaBrowser.Common.Kernel; +using MediaBrowser.Common.Configuration; using MediaBrowser.Common.ScheduledTasks; using MediaBrowser.Model.Logging; using System; @@ -24,22 +24,22 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks.Tasks /// The logger. private ILogger Logger { get; set; } /// - /// Gets or sets the kernel. + /// Gets or sets the configuration manager. /// - /// The kernel. - private IKernel Kernel { get; set; } + /// The configuration manager. + private IConfigurationManager ConfigurationManager { get; set; } /// /// Initializes a new instance of the class. /// /// The logManager. /// The logger. - /// The kernel. - public ReloadLoggerFileTask(ILogManager logManager, ILogger logger, IKernel kernel) + /// The configuration manager. + public ReloadLoggerFileTask(ILogManager logManager, ILogger logger, IConfigurationManager configurationManager) { LogManager = logManager; Logger = logger; - Kernel = kernel; + ConfigurationManager = configurationManager; } /// @@ -65,7 +65,7 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks.Tasks progress.Report(0); - return Task.Run(() => LogManager.ReloadLogger(Kernel.Configuration.EnableDebugLevelLogging ? LogSeverity.Debug : LogSeverity.Info)); + return Task.Run(() => LogManager.ReloadLogger(ConfigurationManager.CommonConfiguration.EnableDebugLevelLogging ? LogSeverity.Debug : LogSeverity.Info)); } /// diff --git a/MediaBrowser.Common.Implementations/ScheduledTasks/Tasks/SystemUpdateTask.cs b/MediaBrowser.Common.Implementations/ScheduledTasks/Tasks/SystemUpdateTask.cs index 0091e14c0..7da1b9c5a 100644 --- a/MediaBrowser.Common.Implementations/ScheduledTasks/Tasks/SystemUpdateTask.cs +++ b/MediaBrowser.Common.Implementations/ScheduledTasks/Tasks/SystemUpdateTask.cs @@ -1,4 +1,5 @@ -using MediaBrowser.Common.Kernel; +using MediaBrowser.Common.Configuration; +using MediaBrowser.Common.Kernel; using MediaBrowser.Common.ScheduledTasks; using MediaBrowser.Model.Logging; using System; @@ -19,27 +20,35 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks.Tasks private readonly IApplicationHost _appHost; /// - /// Gets or sets the kernel. + /// Gets or sets the configuration manager. /// - /// The kernel. - private IKernel Kernel { get; set; } + /// The configuration manager. + private IConfigurationManager ConfigurationManager { get; set; } /// /// Gets or sets the logger. /// /// The logger. private ILogger Logger { get; set; } + /// + /// Gets or sets the kernel. + /// + /// The kernel. + private IKernel Kernel { get; set; } + /// /// Initializes a new instance of the class. /// /// The app host. - /// The kernel. + /// The configuration manager. /// The logger. - public SystemUpdateTask(IApplicationHost appHost, IKernel kernel, ILogger logger) + /// The kernel. + public SystemUpdateTask(IApplicationHost appHost, IConfigurationManager configurationManager, ILogger logger, IKernel kernel) { _appHost = appHost; - Kernel = kernel; + ConfigurationManager = configurationManager; Logger = logger; + Kernel = kernel; } /// @@ -88,7 +97,7 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks.Tasks cancellationToken.ThrowIfCancellationRequested(); - if (Kernel.Configuration.EnableAutoUpdate) + if (ConfigurationManager.CommonConfiguration.EnableAutoUpdate) { Logger.Info("Update Revision {0} available. Updating...", updateInfo.AvailableVersion); diff --git a/MediaBrowser.Common.Implementations/Security/PluginSecurityManager.cs b/MediaBrowser.Common.Implementations/Security/PluginSecurityManager.cs new file mode 100644 index 000000000..a56637371 --- /dev/null +++ b/MediaBrowser.Common.Implementations/Security/PluginSecurityManager.cs @@ -0,0 +1,131 @@ +using MediaBrowser.Common.Configuration; +using MediaBrowser.Common.Security; +using MediaBrowser.Model.Serialization; +using Mediabrowser.Model.Entities; +using Mediabrowser.PluginSecurity; +using MediaBrowser.Common.Kernel; +using MediaBrowser.Common.Net; +using System; +using System.Threading; +using System.Threading.Tasks; + +namespace MediaBrowser.Common.Implementations.Security +{ + /// + /// Class PluginSecurityManager + /// + public class PluginSecurityManager : ISecurityManager + { + /// + /// The _is MB supporter + /// + private bool? _isMBSupporter; + /// + /// The _is MB supporter initialized + /// + private bool _isMBSupporterInitialized; + /// + /// The _is MB supporter sync lock + /// + private object _isMBSupporterSyncLock = new object(); + + /// + /// Gets a value indicating whether this instance is MB supporter. + /// + /// true if this instance is MB supporter; otherwise, false. + public bool IsMBSupporter + { + get + { + LazyInitializer.EnsureInitialized(ref _isMBSupporter, ref _isMBSupporterInitialized, ref _isMBSupporterSyncLock, () => GetRegistrationStatus("MBSupporter").Result.IsRegistered); + return _isMBSupporter.Value; + } + } + + private IHttpClient _httpClient; + private IJsonSerializer _jsonSerializer; + + /// + /// The _kernel + /// + private readonly IKernel _kernel; + + /// + /// Initializes a new instance of the class. + /// + /// The kernel. + public PluginSecurityManager(IKernel kernel, IHttpClient httpClient, IJsonSerializer jsonSerializer, IApplicationPaths appPaths) + { + if (kernel == null) + { + throw new ArgumentNullException("kernel"); + } + + if (httpClient == null) + { + throw new ArgumentNullException("httpClient"); + } + + _kernel = kernel; + _httpClient = httpClient; + _jsonSerializer = jsonSerializer; + //MBRegistration.Init(appPaths); + } + + /// + /// Gets the registration status. + /// + /// The feature. + /// The MB2 equivalent. + /// Task{MBRegistrationRecord}. + public async Task GetRegistrationStatus(string feature, string mb2Equivalent = null) + { + return await MBRegistration.GetRegistrationStatus(_httpClient, _jsonSerializer, feature, mb2Equivalent).ConfigureAwait(false); + } + + /// + /// Gets or sets the supporter key. + /// + /// The supporter key. + public string SupporterKey + { + get { return MBRegistration.SupporterKey; } + set + { + if (value != MBRegistration.SupporterKey) + { + MBRegistration.SupporterKey = value; + // Clear this so it will re-evaluate + ResetSupporterInfo(); + // And we'll need to restart to re-evaluate the status of plug-ins + _kernel.NotifyPendingRestart(); + + } + } + } + + /// + /// Gets or sets the legacy key. + /// + /// The legacy key. + public string LegacyKey + { + get { return MBRegistration.LegacyKey; } + set + { + MBRegistration.LegacyKey = value; + // And we'll need to restart to re-evaluate the status of plug-ins + _kernel.NotifyPendingRestart(); + } + } + + /// + /// Resets the supporter info. + /// + private void ResetSupporterInfo() + { + _isMBSupporter = null; + _isMBSupporterInitialized = false; + } + } +} diff --git a/MediaBrowser.Common.Implementations/ServerManager/ServerManager.cs b/MediaBrowser.Common.Implementations/ServerManager/ServerManager.cs index 31f6922c2..d18971f72 100644 --- a/MediaBrowser.Common.Implementations/ServerManager/ServerManager.cs +++ b/MediaBrowser.Common.Implementations/ServerManager/ServerManager.cs @@ -1,4 +1,5 @@ -using MediaBrowser.Common.Kernel; +using MediaBrowser.Common.Configuration; +using MediaBrowser.Common.Kernel; using MediaBrowser.Common.Net; using MediaBrowser.Model.Logging; using MediaBrowser.Model.Serialization; @@ -77,6 +78,12 @@ namespace MediaBrowser.Common.Implementations.ServerManager /// private readonly IKernel _kernel; + /// + /// Gets or sets the configuration manager. + /// + /// The configuration manager. + private IConfigurationManager ConfigurationManager { get; set; } + /// /// Gets a value indicating whether [supports web socket]. /// @@ -92,14 +99,14 @@ namespace MediaBrowser.Common.Implementations.ServerManager /// The web socket port number. public int WebSocketPortNumber { - get { return SupportsNativeWebSocket ? _kernel.Configuration.HttpServerPortNumber : _kernel.Configuration.LegacyWebSocketPortNumber; } + get { return SupportsNativeWebSocket ? ConfigurationManager.CommonConfiguration.HttpServerPortNumber : ConfigurationManager.CommonConfiguration.LegacyWebSocketPortNumber; } } /// /// Gets the web socket listeners. /// /// The web socket listeners. - private List WebSocketListeners = new List(); + private readonly List _webSocketListeners = new List(); /// /// Initializes a new instance of the class. @@ -109,8 +116,9 @@ namespace MediaBrowser.Common.Implementations.ServerManager /// The network manager. /// The json serializer. /// The logger. + /// The configuration manager. /// applicationHost - public ServerManager(IApplicationHost applicationHost, IKernel kernel, INetworkManager networkManager, IJsonSerializer jsonSerializer, ILogger logger) + public ServerManager(IApplicationHost applicationHost, IKernel kernel, INetworkManager networkManager, IJsonSerializer jsonSerializer, ILogger logger, IConfigurationManager configurationManager) { if (applicationHost == null) { @@ -138,6 +146,7 @@ namespace MediaBrowser.Common.Implementations.ServerManager _kernel = kernel; _applicationHost = applicationHost; _networkManager = networkManager; + ConfigurationManager = configurationManager; } /// @@ -158,7 +167,7 @@ namespace MediaBrowser.Common.Implementations.ServerManager ReloadExternalWebSocketServer(); } - _kernel.ConfigurationUpdated += _kernel_ConfigurationUpdated; + ConfigurationManager.ConfigurationUpdated += _kernel_ConfigurationUpdated; } /// @@ -176,7 +185,7 @@ namespace MediaBrowser.Common.Implementations.ServerManager ExternalWebSocketServer = _applicationHost.Resolve(); - ExternalWebSocketServer.Start(_kernel.Configuration.LegacyWebSocketPortNumber); + ExternalWebSocketServer.Start(ConfigurationManager.CommonConfiguration.LegacyWebSocketPortNumber); ExternalWebSocketServer.WebSocketConnected += HttpServer_WebSocketConnected; } @@ -199,7 +208,7 @@ namespace MediaBrowser.Common.Implementations.ServerManager try { HttpServer = _applicationHost.Resolve(); - HttpServer.EnableHttpRequestLogging = _kernel.Configuration.EnableHttpLevelLogging; + HttpServer.EnableHttpRequestLogging = ConfigurationManager.CommonConfiguration.EnableHttpLevelLogging; HttpServer.Start(_kernel.HttpServerUrlPrefix); } catch (HttpListenerException ex) @@ -240,7 +249,7 @@ namespace MediaBrowser.Common.Implementations.ServerManager /// The result. private async void ProcessWebSocketMessageReceived(WebSocketMessageInfo result) { - var tasks = WebSocketListeners.Select(i => Task.Run(async () => + var tasks = _webSocketListeners.Select(i => Task.Run(async () => { try { @@ -435,7 +444,7 @@ namespace MediaBrowser.Common.Implementations.ServerManager private void RegisterServerWithAdministratorAccess() { // Create a temp file path to extract the bat file to - var tmpFile = Path.Combine(_kernel.ApplicationPaths.TempDirectory, Guid.NewGuid() + ".bat"); + var tmpFile = Path.Combine(ConfigurationManager.CommonApplicationPaths.TempDirectory, Guid.NewGuid() + ".bat"); // Extract the bat file using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("MediaBrowser.Common.Implementations.ServerManager.RegisterServer.bat")) @@ -450,10 +459,10 @@ namespace MediaBrowser.Common.Implementations.ServerManager { FileName = tmpFile, - Arguments = string.Format("{0} {1} {2} {3}", _kernel.Configuration.HttpServerPortNumber, + Arguments = string.Format("{0} {1} {2} {3}", ConfigurationManager.CommonConfiguration.HttpServerPortNumber, _kernel.HttpServerUrlPrefix, _kernel.UdpServerPortNumber, - _kernel.Configuration.LegacyWebSocketPortNumber), + ConfigurationManager.CommonConfiguration.LegacyWebSocketPortNumber), CreateNoWindow = true, WindowStyle = ProcessWindowStyle.Hidden, @@ -508,14 +517,14 @@ namespace MediaBrowser.Common.Implementations.ServerManager /// void _kernel_ConfigurationUpdated(object sender, EventArgs e) { - HttpServer.EnableHttpRequestLogging = _kernel.Configuration.EnableHttpLevelLogging; + HttpServer.EnableHttpRequestLogging = ConfigurationManager.CommonConfiguration.EnableHttpLevelLogging; if (!string.Equals(HttpServer.UrlPrefix, _kernel.HttpServerUrlPrefix, StringComparison.OrdinalIgnoreCase)) { ReloadHttpServer(); } - if (!SupportsNativeWebSocket && ExternalWebSocketServer != null && ExternalWebSocketServer.Port != _kernel.Configuration.LegacyWebSocketPortNumber) + if (!SupportsNativeWebSocket && ExternalWebSocketServer != null && ExternalWebSocketServer.Port != ConfigurationManager.CommonConfiguration.LegacyWebSocketPortNumber) { ReloadExternalWebSocketServer(); } @@ -527,7 +536,7 @@ namespace MediaBrowser.Common.Implementations.ServerManager /// The listeners. public void AddWebSocketListeners(IEnumerable listeners) { - WebSocketListeners.AddRange(listeners); + _webSocketListeners.AddRange(listeners); } } } diff --git a/MediaBrowser.Common.Implementations/Updates/PackageManager.cs b/MediaBrowser.Common.Implementations/Updates/PackageManager.cs index cef631e60..f73857da1 100644 --- a/MediaBrowser.Common.Implementations/Updates/PackageManager.cs +++ b/MediaBrowser.Common.Implementations/Updates/PackageManager.cs @@ -1,37 +1,57 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Security.Cryptography; -using System.Threading; -using System.Threading.Tasks; -using MediaBrowser.Common.Kernel; +using MediaBrowser.Common.Configuration; using MediaBrowser.Common.Net; using MediaBrowser.Common.Security; using MediaBrowser.Common.Updates; -using MediaBrowser.Model.IO; using MediaBrowser.Model.Logging; using MediaBrowser.Model.Serialization; using MediaBrowser.Model.Updates; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Security.Cryptography; +using System.Threading; +using System.Threading.Tasks; namespace MediaBrowser.Common.Implementations.Updates { public class PackageManager : IPackageManager { - public async Task> GetAvailablePackages(IHttpClient client, - INetworkManager networkManager, - ISecurityManager securityManager, - ResourcePool resourcePool, - IJsonSerializer serializer, - CancellationToken cancellationToken) + private readonly ISecurityManager _securityManager; + private readonly INetworkManager _networkManager; + private readonly IHttpClient _httpClient; + private readonly IApplicationPaths _appPaths; + private readonly IJsonSerializer _jsonSerializer; + private readonly ILogger _logger; + + /// + /// Initializes a new instance of the class. + /// + /// The security manager. + /// The network manager. + /// The HTTP client. + /// The application paths. + /// The json serializer. + /// The logger. + public PackageManager(ISecurityManager securityManager, INetworkManager networkManager, IHttpClient httpClient, IApplicationPaths applicationPaths, IJsonSerializer jsonSerializer, ILogger logger) + { + _securityManager = securityManager; + _networkManager = networkManager; + _httpClient = httpClient; + _appPaths = applicationPaths; + _jsonSerializer = jsonSerializer; + _logger = logger; + } + + public async Task> GetAvailablePackages(CancellationToken cancellationToken) { - var data = new Dictionary { { "key", securityManager.SupporterKey }, { "mac", networkManager.GetMacAddress() } }; + var data = new Dictionary { { "key", _securityManager.SupporterKey }, { "mac", _networkManager.GetMacAddress() } }; - using (var json = await client.Post(Constants.Constants.MBAdminUrl + "service/package/retrieveall", data, resourcePool.Mb, cancellationToken).ConfigureAwait(false)) + using (var json = await _httpClient.Post(Constants.Constants.MBAdminUrl + "service/package/retrieveall", data, cancellationToken).ConfigureAwait(false)) { cancellationToken.ThrowIfCancellationRequested(); - var packages = serializer.DeserializeFromStream>(json).ToList(); + var packages = _jsonSerializer.DeserializeFromStream>(json).ToList(); foreach (var package in packages) { package.versions = package.versions.Where(v => !string.IsNullOrWhiteSpace(v.sourceUrl)) @@ -43,15 +63,15 @@ namespace MediaBrowser.Common.Implementations.Updates } - public async Task InstallPackage(IHttpClient client, ILogger logger, ResourcePool resourcePool, IProgress progress, IApplicationPaths appPaths, PackageVersionInfo package, CancellationToken cancellationToken) + public async Task InstallPackage(IProgress progress, PackageVersionInfo package, CancellationToken cancellationToken) { // Target based on if it is an archive or single assembly // zip archives are assumed to contain directory structures relative to our ProgramDataPath var isArchive = string.Equals(Path.GetExtension(package.targetFilename), ".zip", StringComparison.OrdinalIgnoreCase); - var target = Path.Combine(isArchive ? appPaths.TempUpdatePath : appPaths.PluginsPath, package.targetFilename); + var target = Path.Combine(isArchive ? _appPaths.TempUpdatePath : _appPaths.PluginsPath, package.targetFilename); // Download to temporary file so that, if interrupted, it won't destroy the existing installation - var tempFile = await client.GetTempFile(package.sourceUrl, resourcePool.Mb, cancellationToken, progress).ConfigureAwait(false); + var tempFile = await _httpClient.GetTempFile(package.sourceUrl, cancellationToken, progress).ConfigureAwait(false); cancellationToken.ThrowIfCancellationRequested(); @@ -79,7 +99,7 @@ namespace MediaBrowser.Common.Implementations.Updates } catch (IOException e) { - logger.ErrorException("Error attempting to move file from {0} to {1}", e, tempFile, target); + _logger.ErrorException("Error attempting to move file from {0} to {1}", e, tempFile, target); throw; } -- cgit v1.2.3 From 9cdda84b062d990af285d7f25507c892ce05e180 Mon Sep 17 00:00:00 2001 From: LukePulverenti Date: Mon, 4 Mar 2013 01:06:38 -0500 Subject: updated plugin security --- .../Security/PluginSecurityManager.cs | 2 +- .../Providers/Music/LastfmArtistProvider.cs | 11 +++++++---- .../Providers/Music/LastfmBaseArtistProvider.cs | 5 +++-- Nuget/MediaBrowser.Common.Internal.nuspec | 4 ++-- Nuget/MediaBrowser.Common.nuspec | 2 +- Nuget/MediaBrowser.Server.Core.nuspec | 4 ++-- 6 files changed, 16 insertions(+), 12 deletions(-) (limited to 'MediaBrowser.Common.Implementations') diff --git a/MediaBrowser.Common.Implementations/Security/PluginSecurityManager.cs b/MediaBrowser.Common.Implementations/Security/PluginSecurityManager.cs index a56637371..d1a4940ef 100644 --- a/MediaBrowser.Common.Implementations/Security/PluginSecurityManager.cs +++ b/MediaBrowser.Common.Implementations/Security/PluginSecurityManager.cs @@ -69,7 +69,7 @@ namespace MediaBrowser.Common.Implementations.Security _kernel = kernel; _httpClient = httpClient; _jsonSerializer = jsonSerializer; - //MBRegistration.Init(appPaths); + MBRegistration.Init(appPaths); } /// diff --git a/MediaBrowser.Controller/Providers/Music/LastfmArtistProvider.cs b/MediaBrowser.Controller/Providers/Music/LastfmArtistProvider.cs index 5203b6f06..ba32ba9e6 100644 --- a/MediaBrowser.Controller/Providers/Music/LastfmArtistProvider.cs +++ b/MediaBrowser.Controller/Providers/Music/LastfmArtistProvider.cs @@ -6,6 +6,7 @@ using System.Text; using System.Threading; using System.Threading.Tasks; using MediaBrowser.Common.Net; +using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Entities.Audio; using MediaBrowser.Model.Logging; @@ -16,8 +17,10 @@ namespace MediaBrowser.Controller.Providers.Music { public class LastfmArtistProvider : LastfmBaseArtistProvider { - public LastfmArtistProvider(IJsonSerializer jsonSerializer, IHttpClient httpClient, ILogManager logManager) - : base(jsonSerializer, httpClient, logManager) + internal readonly SemaphoreSlim LastfmResourcePool = new SemaphoreSlim(5, 5); + + public LastfmArtistProvider(IJsonSerializer jsonSerializer, IHttpClient httpClient, ILogManager logManager, IServerConfigurationManager configurationManager) + : base(jsonSerializer, httpClient, logManager, configurationManager) { } @@ -30,7 +33,7 @@ namespace MediaBrowser.Controller.Providers.Music try { - using (var json = await HttpClient.Get(url, Kernel.Instance.ResourcePools.MovieDb, cancellationToken).ConfigureAwait(false)) + using (var json = await HttpClient.Get(url, LastfmResourcePool, cancellationToken).ConfigureAwait(false)) { searchResult = JsonSerializer.DeserializeFromStream(json); } @@ -61,7 +64,7 @@ namespace MediaBrowser.Controller.Providers.Music try { - using (var json = await HttpClient.Get(url, Kernel.Instance.ResourcePools.Lastfm, cancellationToken).ConfigureAwait(false)) + using (var json = await HttpClient.Get(url, LastfmResourcePool, cancellationToken).ConfigureAwait(false)) { result = JsonSerializer.DeserializeFromStream(json); } diff --git a/MediaBrowser.Controller/Providers/Music/LastfmBaseArtistProvider.cs b/MediaBrowser.Controller/Providers/Music/LastfmBaseArtistProvider.cs index 85f141a56..444c0fefd 100644 --- a/MediaBrowser.Controller/Providers/Music/LastfmBaseArtistProvider.cs +++ b/MediaBrowser.Controller/Providers/Music/LastfmBaseArtistProvider.cs @@ -5,6 +5,7 @@ using System.Net; using System.Text; using System.Threading.Tasks; using MediaBrowser.Common.Net; +using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Entities.Audio; using MediaBrowser.Model.Logging; using MediaBrowser.Model.Net; @@ -14,8 +15,8 @@ namespace MediaBrowser.Controller.Providers.Music { public abstract class LastfmBaseArtistProvider : LastfmBaseProvider { - protected LastfmBaseArtistProvider(IJsonSerializer jsonSerializer, IHttpClient httpClient, ILogManager logManager) - : base(jsonSerializer, httpClient, logManager) + protected LastfmBaseArtistProvider(IJsonSerializer jsonSerializer, IHttpClient httpClient, ILogManager logManager, IServerConfigurationManager configurationManager) + : base(jsonSerializer, httpClient, logManager, configurationManager) { LocalMetaFileName = "MBArtist.json"; } diff --git a/Nuget/MediaBrowser.Common.Internal.nuspec b/Nuget/MediaBrowser.Common.Internal.nuspec index b74f7a55f..e3f8d15a1 100644 --- a/Nuget/MediaBrowser.Common.Internal.nuspec +++ b/Nuget/MediaBrowser.Common.Internal.nuspec @@ -2,7 +2,7 @@ MediaBrowser.Common.Internal - 3.0.26 + 3.0.28 MediaBrowser.Common.Internal Luke ebr,Luke,scottisafool @@ -12,7 +12,7 @@ Contains common components shared by Media Browser Theatre and Media Browser Server. Not intended for plugin developer consumption. Copyright © Media Browser 2013 - + diff --git a/Nuget/MediaBrowser.Common.nuspec b/Nuget/MediaBrowser.Common.nuspec index bdc564592..725057b94 100644 --- a/Nuget/MediaBrowser.Common.nuspec +++ b/Nuget/MediaBrowser.Common.nuspec @@ -2,7 +2,7 @@ MediaBrowser.Common - 3.0.26 + 3.0.28 MediaBrowser.Common Media Browser Team ebr,Luke,scottisafool diff --git a/Nuget/MediaBrowser.Server.Core.nuspec b/Nuget/MediaBrowser.Server.Core.nuspec index 2d127a890..ce4445203 100644 --- a/Nuget/MediaBrowser.Server.Core.nuspec +++ b/Nuget/MediaBrowser.Server.Core.nuspec @@ -2,7 +2,7 @@ MediaBrowser.Server.Core - 3.0.26 + 3.0.28 Media Browser.Server.Core Media Browser Team ebr,Luke,scottisafool @@ -12,7 +12,7 @@ Contains core components required to build plugins for Media Browser Server. Copyright © Media Browser 2013 - + -- cgit v1.2.3