aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Common.Implementations
diff options
context:
space:
mode:
Diffstat (limited to 'MediaBrowser.Common.Implementations')
-rw-r--r--MediaBrowser.Common.Implementations/MediaBrowser.Common.Implementations.csproj4
-rw-r--r--MediaBrowser.Common.Implementations/Security/MBLicenseFile.cs61
-rw-r--r--MediaBrowser.Common.Implementations/Security/MBRegistration.cs99
-rw-r--r--MediaBrowser.Common.Implementations/Security/PluginSecurityManager.cs154
-rw-r--r--MediaBrowser.Common.Implementations/Security/SuppporterInfoResponse.cs14
5 files changed, 191 insertions, 141 deletions
diff --git a/MediaBrowser.Common.Implementations/MediaBrowser.Common.Implementations.csproj b/MediaBrowser.Common.Implementations/MediaBrowser.Common.Implementations.csproj
index abb2f20896..4b0c956162 100644
--- a/MediaBrowser.Common.Implementations/MediaBrowser.Common.Implementations.csproj
+++ b/MediaBrowser.Common.Implementations/MediaBrowser.Common.Implementations.csproj
@@ -99,9 +99,9 @@
<Compile Include="ScheduledTasks\Tasks\ReloadLoggerFileTask.cs" />
<Compile Include="ScheduledTasks\Tasks\SystemUpdateTask.cs" />
<Compile Include="Security\MBLicenseFile.cs" />
- <Compile Include="Security\MBRegistration.cs" />
<Compile Include="Security\PluginSecurityManager.cs" />
<Compile Include="Security\RegRecord.cs" />
+ <Compile Include="Security\SuppporterInfoResponse.cs" />
<Compile Include="Serialization\JsonSerializer.cs" />
<Compile Include="Serialization\XmlSerializer.cs" />
<Compile Include="Updates\InstallationManager.cs" />
@@ -135,4 +135,4 @@ xcopy "$(TargetPath)" "$(SolutionDir)\Nuget\dlls\" /y /d /r /i
<Target Name="AfterBuild">
</Target>
-->
-</Project>
+</Project> \ No newline at end of file
diff --git a/MediaBrowser.Common.Implementations/Security/MBLicenseFile.cs b/MediaBrowser.Common.Implementations/Security/MBLicenseFile.cs
index 77a7bf0f68..8f3225f4e9 100644
--- a/MediaBrowser.Common.Implementations/Security/MBLicenseFile.cs
+++ b/MediaBrowser.Common.Implementations/Security/MBLicenseFile.cs
@@ -1,7 +1,10 @@
using MediaBrowser.Common.Configuration;
using System;
+using System.Collections.Concurrent;
using System.Collections.Generic;
+using System.Globalization;
using System.IO;
+using System.Linq;
using System.Security.Cryptography;
using System.Text;
@@ -19,7 +22,7 @@ namespace MediaBrowser.Common.Implementations.Security
if (value != _regKey)
{
//if key is changed - clear out our saved validations
- UpdateRecords.Clear();
+ _updateRecords.Clear();
_regKey = value;
}
}
@@ -33,24 +36,30 @@ namespace MediaBrowser.Common.Implementations.Security
}
}
- public string LegacyKey { get; set; }
- private Dictionary<Guid, DateTime> UpdateRecords { get; set; }
- private readonly object _lck = new object();
+ private readonly ConcurrentDictionary<Guid, DateTime> _updateRecords = new ConcurrentDictionary<Guid, DateTime>();
+ private readonly object _fileLock = new object();
private string _regKey;
public MBLicenseFile(IApplicationPaths appPaths)
{
_appPaths = appPaths;
- UpdateRecords = new Dictionary<Guid, DateTime>();
Load();
}
+ private void SetUpdateRecord(Guid key, DateTime value)
+ {
+ _updateRecords.AddOrUpdate(key, value, (k, v) => value);
+ }
+
public void AddRegCheck(string featureId)
{
using (var provider = new MD5CryptoServiceProvider())
{
- UpdateRecords[new Guid(provider.ComputeHash(Encoding.Unicode.GetBytes(featureId)))] = DateTime.UtcNow;
+ var key = new Guid(provider.ComputeHash(Encoding.Unicode.GetBytes(featureId)));
+ var value = DateTime.UtcNow;
+
+ SetUpdateRecord(key, value);
Save();
}
@@ -60,7 +69,11 @@ namespace MediaBrowser.Common.Implementations.Security
{
using (var provider = new MD5CryptoServiceProvider())
{
- UpdateRecords.Remove(new Guid(provider.ComputeHash(Encoding.Unicode.GetBytes(featureId))));
+ var key = new Guid(provider.ComputeHash(Encoding.Unicode.GetBytes(featureId)));
+ DateTime val;
+
+ _updateRecords.TryRemove(key, out val);
+
Save();
}
@@ -71,8 +84,10 @@ namespace MediaBrowser.Common.Implementations.Security
using (var provider = new MD5CryptoServiceProvider())
{
DateTime last;
- lock(_lck) UpdateRecords.TryGetValue(new Guid(provider.ComputeHash(Encoding.Unicode.GetBytes(featureId))), out last);
- return last < DateTime.UtcNow ? last : DateTime.MinValue; // guard agains people just putting a large number in the file
+ _updateRecords.TryGetValue(new Guid(provider.ComputeHash(Encoding.Unicode.GetBytes(featureId))), out last);
+
+ // guard agains people just putting a large number in the file
+ return last < DateTime.UtcNow ? last : DateTime.MinValue;
}
}
@@ -80,7 +95,7 @@ namespace MediaBrowser.Common.Implementations.Security
{
string[] contents = null;
var licenseFile = Filename;
- lock (_lck)
+ lock (_fileLock)
{
try
{
@@ -95,13 +110,19 @@ namespace MediaBrowser.Common.Implementations.Security
{
//first line is reg key
RegKey = contents[0];
+
//next is legacy key
- if (contents.Length > 1) LegacyKey = contents[1];
+ if (contents.Length > 1)
+ {
+ // Don't need this anymore
+ }
+
//the rest of the lines should be pairs of features and timestamps
for (var i = 2; i < contents.Length; i = i + 2)
{
var feat = Guid.Parse(contents[i]);
- UpdateRecords[feat] = new DateTime(Convert.ToInt64(contents[i + 1]));
+
+ SetUpdateRecord(feat, new DateTime(Convert.ToInt64(contents[i + 1])));
}
}
}
@@ -109,16 +130,24 @@ namespace MediaBrowser.Common.Implementations.Security
public void Save()
{
//build our array
- var lines = new List<string> {RegKey, LegacyKey};
- foreach (var pair in UpdateRecords)
+ var lines = new List<string>
+ {
+ RegKey,
+
+ // Legacy key
+ string.Empty
+ };
+
+ foreach (var pair in _updateRecords
+ .ToList())
{
lines.Add(pair.Key.ToString());
- lines.Add(pair.Value.Ticks.ToString());
+ lines.Add(pair.Value.Ticks.ToString(CultureInfo.InvariantCulture));
}
var licenseFile = Filename;
Directory.CreateDirectory(Path.GetDirectoryName(licenseFile));
- lock (_lck) File.WriteAllLines(licenseFile, lines);
+ lock (_fileLock) File.WriteAllLines(licenseFile, lines);
}
}
}
diff --git a/MediaBrowser.Common.Implementations/Security/MBRegistration.cs b/MediaBrowser.Common.Implementations/Security/MBRegistration.cs
deleted file mode 100644
index dbb1b13e15..0000000000
--- a/MediaBrowser.Common.Implementations/Security/MBRegistration.cs
+++ /dev/null
@@ -1,99 +0,0 @@
-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;
-using System.Threading;
-using System.Threading.Tasks;
-
-namespace MediaBrowser.Common.Implementations.Security
-{
- public static class MBRegistration
- {
-
- private static MBLicenseFile _licenseFile;
- private const string MBValidateUrl = Constants.Constants.MbAdminUrl + "service/registration/validate";
-
- private static IApplicationPaths _appPaths;
- private static INetworkManager _networkManager;
- private static ILogger _logger;
- private static IApplicationHost _applicationHost;
-
- private static MBLicenseFile LicenseFile
- {
- get { return _licenseFile ?? (_licenseFile = new MBLicenseFile(_appPaths)); }
- }
-
- public static string SupporterKey
- {
- get { return LicenseFile.RegKey; }
- set { LicenseFile.RegKey = value; LicenseFile.Save(); }
- }
-
- public static string LegacyKey
- {
- get { return LicenseFile.LegacyKey; }
- set { LicenseFile.LegacyKey = value; LicenseFile.Save(); }
- }
-
- public static void Init(IApplicationPaths appPaths, INetworkManager networkManager, ILogManager logManager, IApplicationHost appHost)
- {
- // Ugly alert (static init)
-
- _appPaths = appPaths;
- _networkManager = networkManager;
- _logger = logManager.GetLogger("SecurityManager");
- _applicationHost = appHost;
- }
-
- public static async Task<MBRegistrationRecord> GetRegistrationStatus(IHttpClient httpClient, IJsonSerializer jsonSerializer, string feature, string mb2Equivalent = null, string version = null)
- {
- //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 success = reg.registered;
-
- if (!reg.registered)
- {
- 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 },
- { "isservice", _applicationHost.IsRunningAsService.ToString().ToLower() }
- };
-
- try
- {
- using (var json = await httpClient.Post(MBValidateUrl, data, CancellationToken.None).ConfigureAwait(false))
- {
- reg = jsonSerializer.DeserializeFromStream<RegRecord>(json);
- success = true;
- }
-
- if (reg.registered)
- {
- LicenseFile.AddRegCheck(feature);
- }
- else
- {
- LicenseFile.RemoveRegCheck(feature);
- }
-
- }
- catch (Exception e)
- {
- _logger.ErrorException("Error checking registration status of {0}", e, feature);
- }
- }
-
- return new MBRegistrationRecord { IsRegistered = reg.registered, ExpirationDate = reg.expDate, RegChecked = true, RegError = !success};
- }
- }
-}
diff --git a/MediaBrowser.Common.Implementations/Security/PluginSecurityManager.cs b/MediaBrowser.Common.Implementations/Security/PluginSecurityManager.cs
index d0b108c7da..22fce1ce1b 100644
--- a/MediaBrowser.Common.Implementations/Security/PluginSecurityManager.cs
+++ b/MediaBrowser.Common.Implementations/Security/PluginSecurityManager.cs
@@ -17,6 +17,8 @@ namespace MediaBrowser.Common.Implementations.Security
/// </summary>
public class PluginSecurityManager : ISecurityManager
{
+ private const string MBValidateUrl = Constants.Constants.MbAdminUrl + "service/registration/validate";
+
/// <summary>
/// The _is MB supporter
/// </summary>
@@ -38,16 +40,23 @@ namespace MediaBrowser.Common.Implementations.Security
{
get
{
- LazyInitializer.EnsureInitialized(ref _isMbSupporter, ref _isMbSupporterInitialized, ref _isMbSupporterSyncLock, () => GetRegistrationStatus("MBSupporter", null, _appHost.ApplicationVersion.ToString()).Result.IsRegistered);
+ LazyInitializer.EnsureInitialized(ref _isMbSupporter, ref _isMbSupporterInitialized, ref _isMbSupporterSyncLock, () => GetSupporterRegistrationStatus().Result.IsRegistered);
return _isMbSupporter.Value;
}
}
+ private MBLicenseFile _licenseFile;
+ private MBLicenseFile LicenseFile
+ {
+ get { return _licenseFile ?? (_licenseFile = new MBLicenseFile(_appPaths)); }
+ }
+
private readonly IHttpClient _httpClient;
private readonly IJsonSerializer _jsonSerializer;
private readonly IApplicationHost _appHost;
- private readonly IApplicationPaths _applciationPaths;
+ private readonly ILogger _logger;
private readonly INetworkManager _networkManager;
+ private readonly IApplicationPaths _appPaths;
private IEnumerable<IRequiresRegistration> _registeredEntities;
protected IEnumerable<IRequiresRegistration> RegisteredEntities
@@ -69,12 +78,12 @@ namespace MediaBrowser.Common.Implementations.Security
throw new ArgumentNullException("httpClient");
}
- _applciationPaths = appPaths;
- _networkManager = networkManager;
_appHost = appHost;
_httpClient = httpClient;
_jsonSerializer = jsonSerializer;
- MBRegistration.Init(_applciationPaths, _networkManager, logManager, _appHost);
+ _networkManager = networkManager;
+ _appPaths = appPaths;
+ _logger = logManager.GetLogger("SecurityManager");
}
/// <summary>
@@ -97,9 +106,9 @@ namespace MediaBrowser.Common.Implementations.Security
/// <param name="feature">The feature.</param>
/// <param name="mb2Equivalent">The MB2 equivalent.</param>
/// <returns>Task{MBRegistrationRecord}.</returns>
- public async Task<MBRegistrationRecord> GetRegistrationStatus(string feature, string mb2Equivalent = null)
+ public Task<MBRegistrationRecord> GetRegistrationStatus(string feature, string mb2Equivalent = null)
{
- return await MBRegistration.GetRegistrationStatus(_httpClient, _jsonSerializer, feature, mb2Equivalent).ConfigureAwait(false);
+ return GetRegistrationStatusInternal(feature, mb2Equivalent);
}
/// <summary>
@@ -109,9 +118,14 @@ namespace MediaBrowser.Common.Implementations.Security
/// <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, string version)
+ public Task<MBRegistrationRecord> GetRegistrationStatus(string feature, string mb2Equivalent, string version)
{
- return await MBRegistration.GetRegistrationStatus(_httpClient, _jsonSerializer, feature, mb2Equivalent, version).ConfigureAwait(false);
+ return GetRegistrationStatusInternal(feature, mb2Equivalent, version);
+ }
+
+ private Task<MBRegistrationRecord> GetSupporterRegistrationStatus()
+ {
+ return GetRegistrationStatusInternal("MBSupporter", null, _appHost.ApplicationVersion.ToString());
}
/// <summary>
@@ -122,38 +136,130 @@ namespace MediaBrowser.Common.Implementations.Security
{
get
{
- return MBRegistration.SupporterKey;
+ return LicenseFile.RegKey;
}
set
{
- if (value != MBRegistration.SupporterKey)
+ if (value != LicenseFile.RegKey)
{
- MBRegistration.SupporterKey = value;
+ LicenseFile.RegKey = value;
+ LicenseFile.Save();
+
// re-load registration info
Task.Run(() => LoadAllRegistrationInfo());
}
}
}
- /// <summary>
- /// Gets or sets the legacy key.
- /// </summary>
- /// <value>The legacy key.</value>
- public string LegacyKey
+ public async Task<SupporterInfo> GetSupporterInfo()
{
- get
+ var key = SupporterKey;
+
+ if (string.IsNullOrWhiteSpace(key))
{
- return MBRegistration.LegacyKey;
+ return new SupporterInfo();
}
- set
+
+ var url = Constants.Constants.MbAdminUrl + "/service/supporter/retrieve?key=" + key;
+
+ using (var stream = await _httpClient.Get(url, CancellationToken.None).ConfigureAwait(false))
{
- if (value != MBRegistration.LegacyKey)
+ var response = _jsonSerializer.DeserializeFromStream<SuppporterInfoResponse>(stream);
+
+
+ var info = new SupporterInfo
{
- MBRegistration.LegacyKey = value;
- // re-load registration info
- Task.Run(() => LoadAllRegistrationInfo());
+ Email = response.email,
+ PlanType = response.planType,
+ SupporterKey = response.supporterKey,
+ ExpirationDate = string.IsNullOrWhiteSpace(response.expDate) ? (DateTime?)null : DateTime.Parse(response.expDate),
+ RegistrationDate = DateTime.Parse(response.regDate),
+ IsActiveSupporter = IsMBSupporter
+ };
+
+ info.IsExpiredSupporter = info.ExpirationDate.HasValue && info.ExpirationDate < DateTime.UtcNow && !string.IsNullOrWhiteSpace(info.SupporterKey);
+
+ // TODO: Now that we've pulled this down, might as well update the cached suppoter registrationinfo?
+
+ return info;
+ }
+ }
+
+ private async Task<MBRegistrationRecord> GetRegistrationStatusInternal(string feature,
+ string mb2Equivalent = null,
+ string version = null)
+ {
+ //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(-7)
+ };
+
+ var success = reg.registered;
+
+ if (!reg.registered)
+ {
+ var mac = _networkManager.GetMacAddress();
+ var data = new Dictionary<string, string>
+ {
+ { "feature", feature },
+ { "key", SupporterKey },
+ { "mac", mac },
+ { "mb2equiv", mb2Equivalent },
+ { "ver", version },
+ { "platform", Environment.OSVersion.VersionString },
+ { "isservice", _appHost.IsRunningAsService.ToString().ToLower() }
+ };
+
+ try
+ {
+ using (var json = await _httpClient.Post(MBValidateUrl, data, CancellationToken.None).ConfigureAwait(false))
+ {
+ reg = _jsonSerializer.DeserializeFromStream<RegRecord>(json);
+ success = true;
+ }
+
+ if (reg.registered)
+ {
+ LicenseFile.AddRegCheck(feature);
+ }
+ else
+ {
+ LicenseFile.RemoveRegCheck(feature);
+ }
+
}
+ catch (Exception e)
+ {
+ _logger.ErrorException("Error checking registration status of {0}", e, feature);
+ }
+ }
+
+ var record = new MBRegistrationRecord
+ {
+ IsRegistered = reg.registered,
+ ExpirationDate = reg.expDate,
+ RegChecked = true,
+ RegError = !success
+ };
+
+ record.TrialVersion = IsInTrial(reg.expDate, record.RegChecked, record.IsRegistered);
+ record.IsValid = !record.RegChecked || (record.IsRegistered || record.TrialVersion);
+
+ return record;
+ }
+
+ private bool IsInTrial(DateTime expirationDate, bool regChecked, bool isRegistered)
+ {
+ //don't set this until we've successfully obtained exp date
+ if (!regChecked)
+ {
+ return false;
}
+
+ var isInTrial = expirationDate > DateTime.UtcNow;
+
+ return (isInTrial && !isRegistered);
}
/// <summary>
diff --git a/MediaBrowser.Common.Implementations/Security/SuppporterInfoResponse.cs b/MediaBrowser.Common.Implementations/Security/SuppporterInfoResponse.cs
new file mode 100644
index 0000000000..49c5af8d82
--- /dev/null
+++ b/MediaBrowser.Common.Implementations/Security/SuppporterInfoResponse.cs
@@ -0,0 +1,14 @@
+
+namespace MediaBrowser.Common.Implementations.Security
+{
+ internal class SuppporterInfoResponse
+ {
+ public string email { get; set; }
+ public string supporterKey { get; set; }
+ public int totalRegs { get; set; }
+ public int totalMachines { get; set; }
+ public string expDate { get; set; }
+ public string regDate { get; set; }
+ public string planType { get; set; }
+ }
+}