aboutsummaryrefslogtreecommitdiff
path: root/Emby.Server.Implementations/Security/PluginSecurityManager.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Emby.Server.Implementations/Security/PluginSecurityManager.cs')
-rw-r--r--Emby.Server.Implementations/Security/PluginSecurityManager.cs264
1 files changed, 107 insertions, 157 deletions
diff --git a/Emby.Server.Implementations/Security/PluginSecurityManager.cs b/Emby.Server.Implementations/Security/PluginSecurityManager.cs
index 615ffa1f4..c9c68703e 100644
--- a/Emby.Server.Implementations/Security/PluginSecurityManager.cs
+++ b/Emby.Server.Implementations/Security/PluginSecurityManager.cs
@@ -26,30 +26,11 @@ namespace Emby.Server.Implementations.Security
private const string MBValidateUrl = "https://mb3admin.com/admin/service/registration/validate";
private const string AppstoreRegUrl = /*MbAdmin.HttpsUrl*/ "https://mb3admin.com/admin/service/appstore/register";
- /// <summary>
- /// The _is MB supporter
- /// </summary>
- private bool? _isMbSupporter;
- /// <summary>
- /// The _is MB supporter initialized
- /// </summary>
- private bool _isMbSupporterInitialized;
- /// <summary>
- /// The _is MB supporter sync lock
- /// </summary>
- private object _isMbSupporterSyncLock = new object();
-
- /// <summary>
- /// Gets a value indicating whether this instance is MB supporter.
- /// </summary>
- /// <value><c>true</c> if this instance is MB supporter; otherwise, <c>false</c>.</value>
- public bool IsMBSupporter
+ public async Task<bool> IsSupporter()
{
- get
- {
- LazyInitializer.EnsureInitialized(ref _isMbSupporter, ref _isMbSupporterInitialized, ref _isMbSupporterSyncLock, () => GetSupporterRegistrationStatus().Result.IsRegistered);
- return _isMbSupporter.Value;
- }
+ var result = await GetRegistrationStatusInternal("MBSupporter", false, _appHost.ApplicationVersion.ToString(), CancellationToken.None).ConfigureAwait(false);
+
+ return result.IsRegistered;
}
private MBLicenseFile _licenseFile;
@@ -66,15 +47,6 @@ namespace Emby.Server.Implementations.Security
private readonly IFileSystem _fileSystem;
private readonly ICryptoProvider _cryptographyProvider;
- private IEnumerable<IRequiresRegistration> _registeredEntities;
- protected IEnumerable<IRequiresRegistration> RegisteredEntities
- {
- get
- {
- return _registeredEntities ?? (_registeredEntities = _appHost.GetExports<IRequiresRegistration>());
- }
- }
-
/// <summary>
/// Initializes a new instance of the <see cref="PluginSecurityManager" /> class.
/// </summary>
@@ -96,45 +68,12 @@ namespace Emby.Server.Implementations.Security
}
/// <summary>
- /// Load all registration info for all entities that require registration
- /// </summary>
- /// <returns></returns>
- public async Task LoadAllRegistrationInfo()
- {
- var tasks = new List<Task>();
-
- ResetSupporterInfo();
- tasks.AddRange(RegisteredEntities.Select(i => i.LoadRegistrationInfoAsync()));
- await Task.WhenAll(tasks);
- }
-
- /// <summary>
/// Gets the registration status.
/// This overload supports existing plug-ins.
/// </summary>
- /// <param name="feature">The feature.</param>
- /// <param name="mb2Equivalent">The MB2 equivalent.</param>
- /// <returns>Task{MBRegistrationRecord}.</returns>
- public Task<MBRegistrationRecord> GetRegistrationStatus(string feature, string mb2Equivalent = null)
- {
- return GetRegistrationStatusInternal(feature, mb2Equivalent);
- }
-
- /// <summary>
- /// Gets the registration status.
- /// </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 Task<MBRegistrationRecord> GetRegistrationStatus(string feature, string mb2Equivalent, string version)
- {
- return GetRegistrationStatusInternal(feature, mb2Equivalent, version);
- }
-
- private Task<MBRegistrationRecord> GetSupporterRegistrationStatus()
+ public Task<MBRegistrationRecord> GetRegistrationStatus(string feature)
{
- return GetRegistrationStatusInternal("MBSupporter", null, _appHost.ApplicationVersion.ToString());
+ return GetRegistrationStatusInternal(feature, false, null, CancellationToken.None);
}
/// <summary>
@@ -149,20 +88,24 @@ namespace Emby.Server.Implementations.Security
}
set
{
- var newValue = value;
- if (newValue != null)
- {
- newValue = newValue.Trim();
- }
+ throw new Exception("Please call UpdateSupporterKey");
+ }
+ }
- if (newValue != LicenseFile.RegKey)
- {
- LicenseFile.RegKey = newValue;
- LicenseFile.Save();
+ public async Task UpdateSupporterKey(string newValue)
+ {
+ if (newValue != null)
+ {
+ newValue = newValue.Trim();
+ }
- // re-load registration info
- Task.Run(() => LoadAllRegistrationInfo());
- }
+ if (!string.Equals(newValue, LicenseFile.RegKey, StringComparison.Ordinal))
+ {
+ LicenseFile.RegKey = newValue;
+ LicenseFile.Save();
+
+ // Reset this
+ await GetRegistrationStatusInternal("MBSupporter", true, _appHost.ApplicationVersion.ToString(), CancellationToken.None).ConfigureAwait(false);
}
}
@@ -187,7 +130,7 @@ namespace Emby.Server.Implementations.Security
{
using (var response = await _httpClient.Post(options).ConfigureAwait(false))
{
- var reg = _jsonSerializer.DeserializeFromStream<RegRecord>(response.Content);
+ var reg = await _jsonSerializer.DeserializeFromStreamAsync<RegRecord>(response.Content).ConfigureAwait(false);
if (reg == null)
{
@@ -197,7 +140,7 @@ namespace Emby.Server.Implementations.Security
}
if (!String.IsNullOrEmpty(reg.key))
{
- SupporterKey = reg.key;
+ await UpdateSupporterKey(reg.key).ConfigureAwait(false);
}
}
@@ -241,97 +184,113 @@ namespace Emby.Server.Implementations.Security
}
}
- private async Task<MBRegistrationRecord> GetRegistrationStatusInternal(string feature,
- string mb2Equivalent = null,
- string version = null)
- {
- var regInfo = LicenseFile.GetRegInfo(feature);
- var lastChecked = regInfo == null ? DateTime.MinValue : regInfo.LastChecked;
- var expDate = regInfo == null ? DateTime.MinValue : regInfo.ExpirationDate;
+ private SemaphoreSlim _regCheckLock = new SemaphoreSlim(1, 1);
- var maxCacheDays = 14;
- var nextCheckDate = new [] { expDate, lastChecked.AddDays(maxCacheDays) }.Min();
+ private async Task<MBRegistrationRecord> GetRegistrationStatusInternal(string feature, bool forceCallToServer, string version, CancellationToken cancellationToken)
+ {
+ await _regCheckLock.WaitAsync(cancellationToken).ConfigureAwait(false);
- if (nextCheckDate > DateTime.UtcNow.AddDays(maxCacheDays))
+ try
{
- nextCheckDate = DateTime.MinValue;
- }
+ var regInfo = LicenseFile.GetRegInfo(feature);
+ var lastChecked = regInfo == null ? DateTime.MinValue : regInfo.LastChecked;
+ var expDate = regInfo == null ? DateTime.MinValue : regInfo.ExpirationDate;
- //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
- {
- // Cache the result for up to a week
- registered = regInfo != null && nextCheckDate >= DateTime.UtcNow && expDate >= DateTime.UtcNow,
- expDate = expDate
- };
+ var maxCacheDays = 14;
+ var nextCheckDate = new[] { expDate, lastChecked.AddDays(maxCacheDays) }.Min();
- var success = reg.registered;
+ if (nextCheckDate > DateTime.UtcNow.AddDays(maxCacheDays))
+ {
+ nextCheckDate = DateTime.MinValue;
+ }
- if (!(lastChecked > DateTime.UtcNow.AddDays(-1)) || !reg.registered)
- {
- var data = new Dictionary<string, string>
+ //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
{
- { "feature", feature },
- { "key", SupporterKey },
- { "mac", _appHost.SystemId },
- { "systemid", _appHost.SystemId },
- { "mb2equiv", mb2Equivalent },
- { "ver", version },
- { "platform", _appHost.OperatingSystemDisplayName }
+ // Cache the result for up to a week
+ registered = regInfo != null && nextCheckDate >= DateTime.UtcNow && expDate >= DateTime.UtcNow,
+ expDate = expDate
};
- try
+ var key = SupporterKey;
+
+ if (!forceCallToServer && string.IsNullOrWhiteSpace(key))
{
- var options = new HttpRequestOptions
- {
- Url = MBValidateUrl,
+ return new MBRegistrationRecord();
+ }
- // Seeing block length errors
- EnableHttpCompression = false,
- BufferContent = false
- };
+ var success = reg.registered;
- options.SetPostData(data);
+ if (!(lastChecked > DateTime.UtcNow.AddDays(-1)) || (!reg.registered))
+ {
+ var data = new Dictionary<string, string>
+ {
+ { "feature", feature },
+ { "key", key },
+ { "mac", _appHost.SystemId },
+ { "systemid", _appHost.SystemId },
+ { "ver", version },
+ { "platform", _appHost.OperatingSystemDisplayName }
+ };
- using (var response = (await _httpClient.Post(options).ConfigureAwait(false)))
+ try
{
- using (var json = response.Content)
+ var options = new HttpRequestOptions
{
- reg = _jsonSerializer.DeserializeFromStream<RegRecord>(json);
- success = true;
+ Url = MBValidateUrl,
+
+ // Seeing block length errors
+ EnableHttpCompression = false,
+ BufferContent = false,
+ CancellationToken = cancellationToken
+ };
+
+ options.SetPostData(data);
+
+ using (var response = (await _httpClient.Post(options).ConfigureAwait(false)))
+ {
+ using (var json = response.Content)
+ {
+ reg = await _jsonSerializer.DeserializeFromStreamAsync<RegRecord>(json).ConfigureAwait(false);
+ success = true;
+ }
+ }
+
+ if (reg.registered)
+ {
+ _logger.Info("Registered for feature {0}", feature);
+ LicenseFile.AddRegCheck(feature, reg.expDate);
+ }
+ else
+ {
+ _logger.Info("Not registered for feature {0}", feature);
+ LicenseFile.RemoveRegCheck(feature);
}
- }
- if (reg.registered)
- {
- _logger.Info("Registered for feature {0}", feature);
- LicenseFile.AddRegCheck(feature, reg.expDate);
}
- else
+ catch (Exception e)
{
- _logger.Info("Not registered for feature {0}", feature);
- LicenseFile.RemoveRegCheck(feature);
+ _logger.ErrorException("Error checking registration status of {0}", e, 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
- };
+ 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;
+ record.TrialVersion = IsInTrial(reg.expDate, record.RegChecked, record.IsRegistered);
+ record.IsValid = !record.RegChecked || record.IsRegistered || record.TrialVersion;
- return record;
+ return record;
+ }
+ finally
+ {
+ _regCheckLock.Release();
+ }
}
private bool IsInTrial(DateTime expirationDate, bool regChecked, bool isRegistered)
@@ -346,14 +305,5 @@ namespace Emby.Server.Implementations.Security
return isInTrial && !isRegistered;
}
-
- /// <summary>
- /// Resets the supporter info.
- /// </summary>
- private void ResetSupporterInfo()
- {
- _isMbSupporter = null;
- _isMbSupporterInitialized = false;
- }
}
} \ No newline at end of file