MBRegistration.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. var success = reg.registered;
  47. if (!reg.registered)
  48. {
  49. var mac = _networkManager.GetMacAddress();
  50. var data = new Dictionary<string, string>
  51. {
  52. { "feature", feature },
  53. { "key", SupporterKey },
  54. { "mac", mac },
  55. { "mb2equiv", mb2Equivalent },
  56. { "legacykey", LegacyKey },
  57. { "ver", version },
  58. { "platform", Environment.OSVersion.VersionString },
  59. { "isservice", _applicationHost.IsRunningAsService.ToString().ToLower() }
  60. };
  61. try
  62. {
  63. using (var json = await httpClient.Post(MBValidateUrl, data, CancellationToken.None).ConfigureAwait(false))
  64. {
  65. reg = jsonSerializer.DeserializeFromStream<RegRecord>(json);
  66. success = true;
  67. }
  68. if (reg.registered)
  69. {
  70. LicenseFile.AddRegCheck(feature);
  71. }
  72. else
  73. {
  74. LicenseFile.RemoveRegCheck(feature);
  75. }
  76. }
  77. catch (Exception e)
  78. {
  79. _logger.ErrorException("Error checking registration status of {0}", e, feature);
  80. }
  81. }
  82. return new MBRegistrationRecord { IsRegistered = reg.registered, ExpirationDate = reg.expDate, RegChecked = true, RegError = !success};
  83. }
  84. }
  85. }