MBRegistration.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Common.Net;
  3. using MediaBrowser.Model.Entities;
  4. using MediaBrowser.Model.Logging;
  5. using MediaBrowser.Model.Serialization;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. namespace MediaBrowser.Common.Implementations.Security
  11. {
  12. public static class MBRegistration
  13. {
  14. private static MBLicenseFile _licenseFile;
  15. private const string MBValidateUrl = Constants.Constants.MbAdminUrl + "service/registration/validate";
  16. private static IApplicationPaths _appPaths;
  17. private static INetworkManager _networkManager;
  18. private static ILogger _logger;
  19. private static IApplicationHost _applicationHost;
  20. private static MBLicenseFile LicenseFile
  21. {
  22. get { return _licenseFile ?? (_licenseFile = new MBLicenseFile(_appPaths)); }
  23. }
  24. public static string SupporterKey
  25. {
  26. get { return LicenseFile.RegKey; }
  27. set { LicenseFile.RegKey = value; LicenseFile.Save(); }
  28. }
  29. public static string LegacyKey
  30. {
  31. get { return LicenseFile.LegacyKey; }
  32. set { LicenseFile.LegacyKey = value; LicenseFile.Save(); }
  33. }
  34. public static void Init(IApplicationPaths appPaths, INetworkManager networkManager, ILogManager logManager, IApplicationHost appHost)
  35. {
  36. // Ugly alert (static init)
  37. _appPaths = appPaths;
  38. _networkManager = networkManager;
  39. _logger = logManager.GetLogger("SecurityManager");
  40. _applicationHost = appHost;
  41. }
  42. public static async Task<MBRegistrationRecord> GetRegistrationStatus(IHttpClient httpClient, IJsonSerializer jsonSerializer, string feature, string mb2Equivalent = null, string version = null)
  43. {
  44. //check the reg file first to alleviate strain on the MB admin server - must actually check in every 30 days tho
  45. var reg = new RegRecord { registered = LicenseFile.LastChecked(feature) > DateTime.UtcNow.AddDays(-30) };
  46. if (!reg.registered)
  47. {
  48. var mac = _networkManager.GetMacAddress();
  49. var data = new Dictionary<string, string>
  50. {
  51. { "feature", feature },
  52. { "key", SupporterKey },
  53. { "mac", mac },
  54. { "mb2equiv", mb2Equivalent },
  55. { "legacykey", LegacyKey },
  56. { "ver", version },
  57. { "platform", Environment.OSVersion.VersionString },
  58. { "isservice", _applicationHost.IsRunningAsService.ToString().ToLower() }
  59. };
  60. try
  61. {
  62. using (var json = await httpClient.Post(MBValidateUrl, data, CancellationToken.None).ConfigureAwait(false))
  63. {
  64. reg = jsonSerializer.DeserializeFromStream<RegRecord>(json);
  65. }
  66. if (reg.registered)
  67. {
  68. LicenseFile.AddRegCheck(feature);
  69. }
  70. else
  71. {
  72. LicenseFile.RemoveRegCheck(feature);
  73. }
  74. }
  75. catch (Exception e)
  76. {
  77. _logger.ErrorException("Error checking registration status of {0}", e, feature);
  78. }
  79. }
  80. return new MBRegistrationRecord { IsRegistered = reg.registered, ExpirationDate = reg.expDate, RegChecked = true };
  81. }
  82. }
  83. class RegRecord
  84. {
  85. public string featId { get; set; }
  86. public bool registered { get; set; }
  87. public DateTime expDate { get; set; }
  88. }
  89. }