MBRegistration.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Common.Net;
  3. using MediaBrowser.Model.Entities;
  4. using MediaBrowser.Model.Serialization;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace MediaBrowser.Common.Implementations.Security
  10. {
  11. public static class MBRegistration
  12. {
  13. private static MBLicenseFile _licenseFile;
  14. private const string MBValidateUrl = "http://mb3admin.com/admin/service/registration/validate";
  15. private static IApplicationPaths _appPaths;
  16. private static INetworkManager _networkManager;
  17. private static MBLicenseFile LicenseFile
  18. {
  19. get { return _licenseFile ?? (_licenseFile = new MBLicenseFile(_appPaths)); }
  20. }
  21. public static string SupporterKey
  22. {
  23. get { return LicenseFile.RegKey; }
  24. set { LicenseFile.RegKey = value; LicenseFile.Save(); }
  25. }
  26. public static string LegacyKey
  27. {
  28. get { return LicenseFile.LegacyKey; }
  29. set { LicenseFile.LegacyKey = value; LicenseFile.Save(); }
  30. }
  31. public static void Init(IApplicationPaths appPaths, INetworkManager networkManager)
  32. {
  33. // Ugly alert (static init)
  34. _appPaths = appPaths;
  35. _networkManager = networkManager;
  36. }
  37. public static async Task<MBRegistrationRecord> GetRegistrationStatus(IHttpClient httpClient, IJsonSerializer jsonSerializer, string feature, string mb2Equivalent = null)
  38. {
  39. var mac = _networkManager.GetMacAddress();
  40. var data = new Dictionary<string, string> {{"feature", feature}, {"key",SupporterKey}, {"mac",mac}, {"mb2equiv",mb2Equivalent}, {"legacykey", LegacyKey} };
  41. var reg = new RegRecord();
  42. try
  43. {
  44. using (var json = await httpClient.Post(MBValidateUrl, data, CancellationToken.None).ConfigureAwait(false))
  45. {
  46. reg = jsonSerializer.DeserializeFromStream<RegRecord>(json);
  47. }
  48. if (reg.registered)
  49. {
  50. LicenseFile.AddRegCheck(feature);
  51. }
  52. }
  53. catch (Exception)
  54. {
  55. //if we have trouble obtaining from web - allow it if we've validated in the past 30 days
  56. reg.registered = LicenseFile.LastChecked(feature) > DateTime.UtcNow.AddDays(-30);
  57. }
  58. return new MBRegistrationRecord {IsRegistered = reg.registered, ExpirationDate = reg.expDate, RegChecked = true};
  59. }
  60. }
  61. class RegRecord
  62. {
  63. public string featId { get; set; }
  64. public bool registered { get; set; }
  65. public DateTime expDate { get; set; }
  66. }
  67. }