MBRegistration.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using Mediabrowser.Model.Entities;
  2. using MediaBrowser.Common.Configuration;
  3. using MediaBrowser.Common.Net;
  4. using MediaBrowser.Model.Serialization;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Management;
  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 = "http://mb3admin.com/admin/service/registration/validate";
  16. private static IApplicationPaths _appPaths;
  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)
  32. {
  33. // Ugly alert (static init)
  34. _appPaths = appPaths;
  35. }
  36. public static async Task<MBRegistrationRecord> GetRegistrationStatus(IHttpClient httpClient, IJsonSerializer jsonSerializer, string feature, string mb2Equivalent = null)
  37. {
  38. var mac = GetMacAddress();
  39. var data = new Dictionary<string, string> {{"feature", feature}, {"key",SupporterKey}, {"mac",mac}, {"mb2equiv",mb2Equivalent}, {"legacykey", LegacyKey} };
  40. var reg = new RegRecord();
  41. try
  42. {
  43. using (var json = await httpClient.Post(MBValidateUrl, data, CancellationToken.None).ConfigureAwait(false))
  44. {
  45. reg = jsonSerializer.DeserializeFromStream<RegRecord>(json);
  46. }
  47. if (reg.registered)
  48. {
  49. LicenseFile.AddRegCheck(feature);
  50. }
  51. }
  52. catch (Exception)
  53. {
  54. //if we have trouble obtaining from web - allow it if we've validated in the past 30 days
  55. reg.registered = LicenseFile.LastChecked(feature) > DateTime.UtcNow.AddDays(-30);
  56. }
  57. return new MBRegistrationRecord {IsRegistered = reg.registered, ExpirationDate = reg.expDate, RegChecked = true};
  58. }
  59. /// <summary>
  60. /// Returns MAC Address from first Network Card in Computer
  61. /// </summary>
  62. /// <returns>[string] MAC Address</returns>
  63. public static string GetMacAddress()
  64. {
  65. var mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
  66. var moc = mc.GetInstances();
  67. var macAddress = String.Empty;
  68. foreach (ManagementObject mo in moc)
  69. {
  70. if (macAddress == String.Empty) // only return MAC Address from first card
  71. {
  72. try
  73. {
  74. if ((bool)mo["IPEnabled"]) macAddress = mo["MacAddress"].ToString();
  75. }
  76. catch
  77. {
  78. mo.Dispose();
  79. return "";
  80. }
  81. }
  82. mo.Dispose();
  83. }
  84. return macAddress.Replace(":", "");
  85. }
  86. }
  87. class RegRecord
  88. {
  89. public string featId { get; set; }
  90. public bool registered { get; set; }
  91. public DateTime expDate { get; set; }
  92. }
  93. }