diff options
7 files changed, 171 insertions, 39 deletions
diff --git a/MediaBrowser.Common.Implementations/BaseApplicationHost.cs b/MediaBrowser.Common.Implementations/BaseApplicationHost.cs index 9d2dd0838..506774b4a 100644 --- a/MediaBrowser.Common.Implementations/BaseApplicationHost.cs +++ b/MediaBrowser.Common.Implementations/BaseApplicationHost.cs @@ -389,7 +389,7 @@ namespace MediaBrowser.Common.Implementations NetworkManager = CreateNetworkManager(); RegisterSingleInstance(NetworkManager); - SecurityManager = new PluginSecurityManager(this, HttpClient, JsonSerializer, ApplicationPaths, NetworkManager); + SecurityManager = new PluginSecurityManager(this, HttpClient, JsonSerializer, ApplicationPaths, NetworkManager, LogManager); RegisterSingleInstance(SecurityManager); InstallationManager = new InstallationManager(Logger, this, ApplicationPaths, HttpClient, JsonSerializer, SecurityManager, NetworkManager, ConfigurationManager); diff --git a/MediaBrowser.Common.Implementations/MediaBrowser.Common.Implementations.csproj b/MediaBrowser.Common.Implementations/MediaBrowser.Common.Implementations.csproj index 69c880916..dfe7d70ee 100644 --- a/MediaBrowser.Common.Implementations/MediaBrowser.Common.Implementations.csproj +++ b/MediaBrowser.Common.Implementations/MediaBrowser.Common.Implementations.csproj @@ -92,6 +92,7 @@ <Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="ScheduledTasks\ScheduledTaskWorker.cs" /> <Compile Include="ScheduledTasks\TaskManager.cs" /> + <Compile Include="ScheduledTasks\Tasks\StatisticsTask.cs" /> <Compile Include="ScheduledTasks\Tasks\DeleteCacheFileTask.cs" /> <Compile Include="ScheduledTasks\Tasks\DeleteLogFileTask.cs" /> <Compile Include="ScheduledTasks\Tasks\PluginUpdateTask.cs" /> diff --git a/MediaBrowser.Common.Implementations/ScheduledTasks/Tasks/StatisticsTask.cs b/MediaBrowser.Common.Implementations/ScheduledTasks/Tasks/StatisticsTask.cs new file mode 100644 index 000000000..41216460d --- /dev/null +++ b/MediaBrowser.Common.Implementations/ScheduledTasks/Tasks/StatisticsTask.cs @@ -0,0 +1,120 @@ +using System.Reflection; +using MediaBrowser.Common.Configuration; +using MediaBrowser.Common.Net; +using MediaBrowser.Common.ScheduledTasks; +using MediaBrowser.Model.Logging; +using System; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; + +namespace MediaBrowser.Common.Implementations.ScheduledTasks.Tasks +{ + /// <summary> + /// Class ReloadLoggerFileTask + /// </summary> + public class StatisticsTask : IScheduledTask, IConfigurableScheduledTask + { + /// <summary> + /// Gets or sets the log manager. + /// </summary> + /// <value>The log manager.</value> + private ILogManager LogManager { get; set; } + /// <summary> + /// Gets or sets the app host + /// </summary> + /// <value>The application host.</value> + private IApplicationHost ApplicationHost { get; set; } + + /// <summary> + /// The network manager + /// </summary> + private INetworkManager NetworkManager { get; set; } + + /// <summary> + /// The http client + /// </summary> + private IHttpClient HttpClient { get; set; } + + /// <summary> + /// Initializes a new instance of the <see cref="ReloadLoggerFileTask" /> class. + /// </summary> + /// <param name="logManager">The logManager.</param> + /// <param name="appHost"></param> + /// <param name="httpClient"></param> + public StatisticsTask(ILogManager logManager, IApplicationHost appHost, INetworkManager networkManager, IHttpClient httpClient) + { + LogManager = logManager; + ApplicationHost = appHost; + NetworkManager = networkManager; + HttpClient = httpClient; + } + + /// <summary> + /// Gets the default triggers. + /// </summary> + /// <returns>IEnumerable{BaseTaskTrigger}.</returns> + public IEnumerable<ITaskTrigger> GetDefaultTriggers() + { + var trigger = new DailyTrigger { TimeOfDay = TimeSpan.FromHours(20) }; //8pm - when the system is most likely to be active + var trigger2 = new StartupTrigger(); //and also at system start + + return new ITaskTrigger[] { trigger, trigger2 }; + } + + /// <summary> + /// Executes the internal. + /// </summary> + /// <param name="cancellationToken">The cancellation token.</param> + /// <param name="progress">The progress.</param> + /// <returns>Task.</returns> + public async Task Execute(CancellationToken cancellationToken, IProgress<double> progress) + { + cancellationToken.ThrowIfCancellationRequested(); + + progress.Report(0); + var mac = NetworkManager.GetMacAddress(); + var data = new Dictionary<string, string> { { "feature", Assembly.GetExecutingAssembly().GetName().ToString() }, { "mac", mac }, { "ver", ApplicationHost.ApplicationVersion.ToString() }, { "platform", Environment.OSVersion.VersionString } }; + await HttpClient.Post(Constants.Constants.MbAdminUrl + "service/registration/ping", data, CancellationToken.None).ConfigureAwait(false); + progress.Report(100); + + } + + /// <summary> + /// Gets the name. + /// </summary> + /// <value>The name.</value> + public string Name + { + get { return "Collect stats"; } + } + + /// <summary> + /// Gets the description. + /// </summary> + /// <value>The description.</value> + public string Description + { + get { return "Pings the admin site just so we know how many folks are out there and what version they are on."; } + } + + /// <summary> + /// Gets the category. + /// </summary> + /// <value>The category.</value> + public string Category + { + get { return "Application"; } + } + + public bool IsHidden + { + get { return true; } + } + + public bool IsEnabled + { + get { return true; } + } + } +} diff --git a/MediaBrowser.Common.Implementations/Security/MBLicenseFile.cs b/MediaBrowser.Common.Implementations/Security/MBLicenseFile.cs index c5d5f28d6..77a7bf0f6 100644 --- a/MediaBrowser.Common.Implementations/Security/MBLicenseFile.cs +++ b/MediaBrowser.Common.Implementations/Security/MBLicenseFile.cs @@ -56,6 +56,16 @@ namespace MediaBrowser.Common.Implementations.Security } + public void RemoveRegCheck(string featureId) + { + using (var provider = new MD5CryptoServiceProvider()) + { + UpdateRecords.Remove(new Guid(provider.ComputeHash(Encoding.Unicode.GetBytes(featureId)))); + Save(); + } + + } + public DateTime LastChecked(string featureId) { using (var provider = new MD5CryptoServiceProvider()) diff --git a/MediaBrowser.Common.Implementations/Security/MBRegistration.cs b/MediaBrowser.Common.Implementations/Security/MBRegistration.cs index 1f9e63e68..79f60c3ac 100644 --- a/MediaBrowser.Common.Implementations/Security/MBRegistration.cs +++ b/MediaBrowser.Common.Implementations/Security/MBRegistration.cs @@ -1,6 +1,7 @@ using MediaBrowser.Common.Configuration; using MediaBrowser.Common.Net; using MediaBrowser.Model.Entities; +using MediaBrowser.Model.Logging; using MediaBrowser.Model.Serialization; using System; using System.Collections.Generic; @@ -13,10 +14,11 @@ namespace MediaBrowser.Common.Implementations.Security { private static MBLicenseFile _licenseFile; - private const string MBValidateUrl = "http://mb3admin.com/admin/service/registration/validate"; + private const string MBValidateUrl = Constants.Constants.MbAdminUrl+"service/registration/validate"; private static IApplicationPaths _appPaths; private static INetworkManager _networkManager; + private static ILogger _logger; private static MBLicenseFile LicenseFile { @@ -35,37 +37,46 @@ namespace MediaBrowser.Common.Implementations.Security set { LicenseFile.LegacyKey = value; LicenseFile.Save(); } } - public static void Init(IApplicationPaths appPaths, INetworkManager networkManager) + public static void Init(IApplicationPaths appPaths, INetworkManager networkManager, ILogManager logManager) { // Ugly alert (static init) _appPaths = appPaths; _networkManager = networkManager; + _logger = logManager.GetLogger("SecurityManager"); } - public static async Task<MBRegistrationRecord> GetRegistrationStatus(IHttpClient httpClient, IJsonSerializer jsonSerializer, string feature, string mb2Equivalent = null) + public static async Task<MBRegistrationRecord> GetRegistrationStatus(IHttpClient httpClient, IJsonSerializer jsonSerializer, string feature, string mb2Equivalent = null, string version = null) { - var mac = _networkManager.GetMacAddress(); - var data = new Dictionary<string, string> {{"feature", feature}, {"key",SupporterKey}, {"mac",mac}, {"mb2equiv",mb2Equivalent}, {"legacykey", LegacyKey} }; + //check the reg file first to alleviate strain on the MB admin server - must actually check in every 30 days tho + var reg = new RegRecord {/*registered = LicenseFile.LastChecked(feature) > DateTime.UtcNow.AddDays(-30)*/}; - var reg = new RegRecord(); - try + if (!reg.registered) { - using (var json = await httpClient.Post(MBValidateUrl, data, CancellationToken.None).ConfigureAwait(false)) + var mac = _networkManager.GetMacAddress(); + var data = new Dictionary<string, string> { { "feature", feature }, { "key", SupporterKey }, { "mac", mac }, { "mb2equiv", mb2Equivalent }, { "legacykey", LegacyKey }, { "ver", version }, { "platform", Environment.OSVersion.VersionString } }; + + try { - reg = jsonSerializer.DeserializeFromStream<RegRecord>(json); - } + using (var json = await httpClient.Post(MBValidateUrl, data, CancellationToken.None).ConfigureAwait(false)) + { + reg = jsonSerializer.DeserializeFromStream<RegRecord>(json); + } - if (reg.registered) + if (reg.registered) + { + LicenseFile.AddRegCheck(feature); + } + else + { + LicenseFile.RemoveRegCheck(feature); + } + + } + catch (Exception e) { - LicenseFile.AddRegCheck(feature); + _logger.ErrorException("Error checking registration status of {0}", e, feature); } - - } - catch (Exception) - { - //if we have trouble obtaining from web - allow it if we've validated in the past 30 days - reg.registered = LicenseFile.LastChecked(feature) > DateTime.UtcNow.AddDays(-30); } return new MBRegistrationRecord {IsRegistered = reg.registered, ExpirationDate = reg.expDate, RegChecked = true}; diff --git a/MediaBrowser.Common.Implementations/Security/PluginSecurityManager.cs b/MediaBrowser.Common.Implementations/Security/PluginSecurityManager.cs index 19a1ed646..d60e6cca3 100644 --- a/MediaBrowser.Common.Implementations/Security/PluginSecurityManager.cs +++ b/MediaBrowser.Common.Implementations/Security/PluginSecurityManager.cs @@ -2,6 +2,7 @@ using MediaBrowser.Common.Net; using MediaBrowser.Common.Security; using MediaBrowser.Model.Entities; +using MediaBrowser.Model.Logging; using MediaBrowser.Model.Serialization; using System; using System.Collections.Generic; @@ -37,7 +38,7 @@ namespace MediaBrowser.Common.Implementations.Security { get { - LazyInitializer.EnsureInitialized(ref _isMbSupporter, ref _isMbSupporterInitialized, ref _isMbSupporterSyncLock, () => GetRegistrationStatus("MBSupporter").Result.IsRegistered); + LazyInitializer.EnsureInitialized(ref _isMbSupporter, ref _isMbSupporterInitialized, ref _isMbSupporterSyncLock, () => GetRegistrationStatus("MBSupporter", null, _appHost.ApplicationVersion.ToString()).Result.IsRegistered); return _isMbSupporter.Value; } } @@ -60,7 +61,8 @@ namespace MediaBrowser.Common.Implementations.Security /// <summary> /// Initializes a new instance of the <see cref="PluginSecurityManager" /> class. /// </summary> - public PluginSecurityManager(IApplicationHost appHost, IHttpClient httpClient, IJsonSerializer jsonSerializer, IApplicationPaths appPaths, INetworkManager networkManager) + public PluginSecurityManager(IApplicationHost appHost, IHttpClient httpClient, IJsonSerializer jsonSerializer, + IApplicationPaths appPaths, INetworkManager networkManager, ILogManager logManager) { if (httpClient == null) { @@ -72,6 +74,7 @@ namespace MediaBrowser.Common.Implementations.Security _appHost = appHost; _httpClient = httpClient; _jsonSerializer = jsonSerializer; + MBRegistration.Init(_applciationPaths, _networkManager, logManager); } /// <summary> @@ -92,13 +95,11 @@ namespace MediaBrowser.Common.Implementations.Security /// </summary> /// <param name="feature">The feature.</param> /// <param name="mb2Equivalent">The MB2 equivalent.</param> + /// <param name="version">The version of this feature</param> /// <returns>Task{MBRegistrationRecord}.</returns> - public async Task<MBRegistrationRecord> GetRegistrationStatus(string feature, string mb2Equivalent = null) + public async Task<MBRegistrationRecord> GetRegistrationStatus(string feature, string mb2Equivalent = null, string version = null) { - // Do this on demend instead of in the constructor to delay the external assembly load - // Todo: Refactor external methods to take app paths as a param - MBRegistration.Init(_applciationPaths, _networkManager); - return await MBRegistration.GetRegistrationStatus(_httpClient, _jsonSerializer, feature, mb2Equivalent).ConfigureAwait(false); + return await MBRegistration.GetRegistrationStatus(_httpClient, _jsonSerializer, feature, mb2Equivalent, version).ConfigureAwait(false); } /// <summary> @@ -109,16 +110,10 @@ namespace MediaBrowser.Common.Implementations.Security { get { - // Do this on demend instead of in the constructor to delay the external assembly load - // Todo: Refactor external methods to take app paths as a param - MBRegistration.Init(_applciationPaths, _networkManager); return MBRegistration.SupporterKey; } set { - // Do this on demend instead of in the constructor to delay the external assembly load - // Todo: Refactor external methods to take app paths as a param - MBRegistration.Init(_applciationPaths, _networkManager); if (value != MBRegistration.SupporterKey) { MBRegistration.SupporterKey = value; @@ -136,16 +131,10 @@ namespace MediaBrowser.Common.Implementations.Security { get { - // Do this on demend instead of in the constructor to delay the external assembly load - // Todo: Refactor external methods to take app paths as a param - MBRegistration.Init(_applciationPaths, _networkManager); return MBRegistration.LegacyKey; } set { - // Do this on demend instead of in the constructor to delay the external assembly load - // Todo: Refactor external methods to take app paths as a param - MBRegistration.Init(_applciationPaths, _networkManager); if (value != MBRegistration.LegacyKey) { MBRegistration.LegacyKey = value; diff --git a/MediaBrowser.Common/Security/ISecurityManager.cs b/MediaBrowser.Common/Security/ISecurityManager.cs index 82a6809be..b7dd8b617 100644 --- a/MediaBrowser.Common/Security/ISecurityManager.cs +++ b/MediaBrowser.Common/Security/ISecurityManager.cs @@ -28,8 +28,9 @@ namespace MediaBrowser.Common.Security /// </summary> /// <param name="feature">The feature.</param> /// <param name="mb2Equivalent">The MB2 equivalent.</param> + /// <param name="version">The version of the feature</param> /// <returns>Task{MBRegistrationRecord}.</returns> - Task<MBRegistrationRecord> GetRegistrationStatus(string feature, string mb2Equivalent = null); + Task<MBRegistrationRecord> GetRegistrationStatus(string feature, string mb2Equivalent = null, string version = null); /// <summary> /// Load all registration info for all entities that require registration |
