MBLicenseFile.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using MediaBrowser.Common.Configuration;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. namespace MediaBrowser.Common.Implementations.Security
  8. {
  9. internal class MBLicenseFile
  10. {
  11. private readonly IApplicationPaths _appPaths;
  12. public string RegKey
  13. {
  14. get { return _regKey; }
  15. set
  16. {
  17. if (value != _regKey)
  18. {
  19. //if key is changed - clear out our saved validations
  20. UpdateRecords.Clear();
  21. _regKey = value;
  22. }
  23. }
  24. }
  25. private string Filename
  26. {
  27. get
  28. {
  29. return Path.Combine(_appPaths.ConfigurationDirectoryPath, "mb.lic");
  30. }
  31. }
  32. public string LegacyKey { get; set; }
  33. private Dictionary<Guid, DateTime> UpdateRecords { get; set; }
  34. private readonly object _lck = new object();
  35. private string _regKey;
  36. public MBLicenseFile(IApplicationPaths appPaths)
  37. {
  38. _appPaths = appPaths;
  39. UpdateRecords = new Dictionary<Guid, DateTime>();
  40. Load();
  41. }
  42. public void AddRegCheck(string featureId)
  43. {
  44. using (var provider = new MD5CryptoServiceProvider())
  45. {
  46. UpdateRecords[new Guid(provider.ComputeHash(Encoding.Unicode.GetBytes(featureId)))] = DateTime.UtcNow;
  47. Save();
  48. }
  49. }
  50. public void RemoveRegCheck(string featureId)
  51. {
  52. using (var provider = new MD5CryptoServiceProvider())
  53. {
  54. UpdateRecords.Remove(new Guid(provider.ComputeHash(Encoding.Unicode.GetBytes(featureId))));
  55. Save();
  56. }
  57. }
  58. public DateTime LastChecked(string featureId)
  59. {
  60. using (var provider = new MD5CryptoServiceProvider())
  61. {
  62. DateTime last;
  63. lock(_lck) UpdateRecords.TryGetValue(new Guid(provider.ComputeHash(Encoding.Unicode.GetBytes(featureId))), out last);
  64. return last < DateTime.UtcNow ? last : DateTime.MinValue; // guard agains people just putting a large number in the file
  65. }
  66. }
  67. private void Load()
  68. {
  69. string[] contents = null;
  70. var licenseFile = Filename;
  71. lock (_lck)
  72. {
  73. try
  74. {
  75. contents = File.ReadAllLines(licenseFile);
  76. }
  77. catch (FileNotFoundException)
  78. {
  79. (File.Create(licenseFile)).Close();
  80. }
  81. }
  82. if (contents != null && contents.Length > 0)
  83. {
  84. //first line is reg key
  85. RegKey = contents[0];
  86. //next is legacy key
  87. if (contents.Length > 1) LegacyKey = contents[1];
  88. //the rest of the lines should be pairs of features and timestamps
  89. for (var i = 2; i < contents.Length; i = i + 2)
  90. {
  91. var feat = Guid.Parse(contents[i]);
  92. UpdateRecords[feat] = new DateTime(Convert.ToInt64(contents[i + 1]));
  93. }
  94. }
  95. }
  96. public void Save()
  97. {
  98. //build our array
  99. var lines = new List<string> {RegKey, LegacyKey};
  100. foreach (var pair in UpdateRecords)
  101. {
  102. lines.Add(pair.Key.ToString());
  103. lines.Add(pair.Value.Ticks.ToString());
  104. }
  105. var licenseFile = Filename;
  106. Directory.CreateDirectory(Path.GetDirectoryName(licenseFile));
  107. lock (_lck) File.WriteAllLines(licenseFile, lines);
  108. }
  109. }
  110. }